Skip to content

Commit 3e48999

Browse files
Erick ArcherKent Overstreet
authored andcommitted
bcachefs: Prefer struct_size over open coded arithmetic
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "op" variable is a pointer to "struct promote_op" and this structure ends in a flexible array: struct promote_op { [...] struct bio_vec bi_inline_vecs[]; }; and the "t" variable is a pointer to "struct journal_seq_blacklist_table" and this structure also ends in a flexible array: struct journal_seq_blacklist_table { [...] struct journal_seq_blacklist_table_entry { u64 start; u64 end; bool dirty; } entries[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the argument "size + size * count" in the kzalloc() functions. This way, the code is more readable and safer. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: KSPP#160 [2] Signed-off-by: Erick Archer <[email protected]> Signed-off-by: Kent Overstreet <[email protected]>
1 parent 1fdb968 commit 3e48999

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

fs/bcachefs/io_read.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
174174
if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
175175
return ERR_PTR(-BCH_ERR_nopromote_no_writes);
176176

177-
op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_KERNEL);
177+
op = kzalloc(struct_size(op, bi_inline_vecs, pages), GFP_KERNEL);
178178
if (!op) {
179179
ret = -BCH_ERR_nopromote_enomem;
180180
goto err;

fs/bcachefs/journal_seq_blacklist.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ int bch2_blacklist_table_initialize(struct bch_fs *c)
141141
if (!bl)
142142
return 0;
143143

144-
t = kzalloc(sizeof(*t) + sizeof(t->entries[0]) * nr,
145-
GFP_KERNEL);
144+
t = kzalloc(struct_size(t, entries, nr), GFP_KERNEL);
146145
if (!t)
147146
return -BCH_ERR_ENOMEM_blacklist_table_init;
148147

0 commit comments

Comments
 (0)