bcachefs: Prefer struct_size over open coded arithmetic
authorErick Archer <erick.archer@gmx.com>
Sun, 10 Mar 2024 11:02:26 +0000 (12:02 +0100)
committerKent Overstreet <kent.overstreet@linux.dev>
Thu, 14 Mar 2024 01:22:26 +0000 (21:22 -0400)
commit3e48999816b1d1dba3ca40b1d7dbc324adb72fe2
tree7bfd6a22e34df4278a861a3ab052ac24924bb6d9
parent1fdb9685ed8b7b871632a3d348a948e1b53a17e5
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
Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Erick Archer <erick.archer@gmx.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/io_read.c
fs/bcachefs/journal_seq_blacklist.c