bcachefs: Avoid -Wflex-array-member-not-at-end warning
PATCH 1/1 bcachefs: Avoid -Wflex-array-member-not-at-end warning
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Use the DEFINE_FLEX() helper for on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.
So, with these changes, fix the following warning:
fs/bcachefs/disk_accounting.c:429:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
--- fs/bcachefs/disk_accounting.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
fs/bcachefs/disk_accounting.c +7 -9
@@ -425,24 +425,22 @@ int bch2_fs_replicas_usage_read(struct bch_fs *c, darray_char *usage)percpu_down_read (&c->mark_lock);darray_for_each (acc->k, i) { -struct { -struct bch_replicas_usage r; -u8 pad[BCH_BKEY_PTRS_MAX ]; - } u; + DEFINE_FLEX(struct bch_replicas_usage, u, r.devs, r.nr_devs, + BCH_BKEY_PTRS_MAX); -if (!accounting_to_replicas (&u.r.r, i->pos)) +if (!accounting_to_replicas (&u->r, i->pos))continue ;u64 sectors;bch2_accounting_mem_read_counters (acc, i - acc->k.data, §ors,1 , false); - u.r.sectors = sectors; + u->sectors = sectors; - ret =darray_make_room (usage,replicas_usage_bytes (&u.r)); + ret =darray_make_room (usage,replicas_usage_bytes (u));if (ret)break ; -memcpy (&darray_top (*usage), &u.r,replicas_usage_bytes (&u.r)); - usage->nr +=replicas_usage_bytes (&u.r); +memcpy (&darray_top (*usage), u,replicas_usage_bytes (u)); + usage->nr +=replicas_usage_bytes (u); }percpu_up_read (&c->mark_lock);
Signature · 1 line
2.43.0
Quoted 10 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Use the
DEFINE_FLEX()helper for on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.So, with these changes, fix the following warning:
fs/bcachefs/disk_accounting.c:429:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
We also have bch_replicas_padded, does that also need a fix?
And, DEFINE_FLEX() is gross, can we try to stop reinventing language syntax with macros?
Could we not have done this with an __attribute__(())?
Or just swap the anonymous struct for a union, since that's apparently what DEFINE_FLEX() does?
Quoted 45 lines
Signed-off-by Gustavo A. R. Silva <[email protected]>--- fs/bcachefs/disk_accounting.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)fs/bcachefs/disk_accounting.c +7
-9@@ -425,24 +425,22 @@ int bch2_fs_replicas_usage_read(struct bch_fs *c, darray_char *usage)percpu_down_read (&c->mark_lock);darray_for_each (acc->k, i) { -struct { -struct bch_replicas_usage r; -u8 pad[BCH_BKEY_PTRS_MAX ]; - } u; + DEFINE_FLEX(struct bch_replicas_usage, u, r.devs, r.nr_devs, + BCH_BKEY_PTRS_MAX); -if (!accounting_to_replicas (&u.r.r, i->pos)) +if (!accounting_to_replicas (&u->r, i->pos))continue ;u64 sectors;bch2_accounting_mem_read_counters (acc, i - acc->k.data, §ors,1 , false); - u.r.sectors = sectors; + u->sectors = sectors; - ret =darray_make_room (usage,replicas_usage_bytes (&u.r)); + ret =darray_make_room (usage,replicas_usage_bytes (u));if (ret)break ; -memcpy (&darray_top (*usage), &u.r,replicas_usage_bytes (&u.r)); - usage->nr +=replicas_usage_bytes (&u.r); +memcpy (&darray_top (*usage), u,replicas_usage_bytes (u)); + usage->nr +=replicas_usage_bytes (u); }percpu_up_read (&c->mark_lock);Signature · 1 line
2.43.0
Quoted 8 lines · Kent Overstreet
Quoted 7 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Use the
DEFINE_FLEX()helper for on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.
Quoted 5 lines
Quoted 3 lines
So, with these changes, fix the following warning:
fs/bcachefs/disk_accounting.c:429:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]We also have
bch_replicas_padded, does that also need a fix?
Yes - so, how about the following to address both issues:
fs/bcachefs/disk_accounting.c +7 -5
@@ -287,7 +287,7 @@ static inline bool accounting_to_replicas(struct bch_replicas_entry_v1 *r, strucstatic int bch2_accounting_update_sb_one (struct bch_fs *c,struct bpos p) { -struct bch_replicas_padded r; +union bch_replicas_padded r;return accounting_to_replicas (&r.e, p) ?bch2_mark_replicas (c, &r.e) :0 ;
@@ -361,7 +361,7 @@ static int __bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accounint bch2_accounting_mem_insert (struct bch_fs *c,struct bkey_s_c_accounting a,enum bch_accounting_mode mode) { -struct bch_replicas_padded r; +union bch_replicas_padded r; if (mode != BCH_ACCOUNTING_read && accounting_to_replicas(&r.e, a.k->p) &&
@@ -425,10 +425,12 @@ int bch2_fs_replicas_usage_read(struct bch_fs *c, darray_char *usage)percpu_down_read (&c->mark_lock);darray_for_each (acc->k, i) { -struct { +union { + u8 bytes[struct_size_t(struct bch_replicas_usage, r.devs, +BCH_BKEY_PTRS_MAX )];struct bch_replicas_usage r; -u8 pad[BCH_BKEY_PTRS_MAX ]; } u; + u.r.r.nr_devs =BCH_BKEY_PTRS_MAX ;if (!accounting_to_replicas (&u.r.r, i->pos))continue ;
@@ -627,7 +629,7 @@ static int bch2_disk_accounting_validate_late(struct btree_trans *trans,switch (acc->type) {case BCH_DISK_ACCOUNTING_replicas: { -struct bch_replicas_padded r; +union bch_replicas_padded r;__accounting_to_replicas (&r.e, acc);for (unsigned i =0 ; i < r.e.nr_devs; i++)
fs/bcachefs/ec_types.h +4 -3
@@ -4,9 +4,10 @@#include "bcachefs_format.h" -struct bch_replicas_padded { +union bch_replicas_padded { + u8 bytes[struct_size_t(struct bch_replicas_entry_v1, + devs,BCH_BKEY_PTRS_MAX )];struct bch_replicas_entry_v1 e; -u8 pad[BCH_BKEY_PTRS_MAX ]; };struct stripe {
@@ -28,7 +29,7 @@ struct gc_stripe {u16 block_sectors[BCH_BKEY_PTRS_MAX ];struct bch_extent_ptr ptrs[BCH_BKEY_PTRS_MAX ]; - struct bch_replicas_padded r; + union bch_replicas_padded r; };#endif /* _BCACHEFS_EC_TYPES_H */
fs/bcachefs/journal_io.c +3 -3
@@ -1404,7 +1404,7 @@ int bch2_journal_read(struct bch_fs *c, }genradix_for_each (&c->journal_entries, radix_iter, _i) { -struct bch_replicas_padded replicas = { +union bch_replicas_padded replicas = { .e.data_type = BCH_DATA_journal, .e.nr_devs =0 , .e.nr_required =1 ,
@@ -1632,7 +1632,7 @@ static CLOSURE_CALLBACK(journal_write_done) closure_type(w, struct journal_buf, io); struct journal *j = container_of(w, struct journal, buf[w->idx]); struct bch_fs *c = container_of(j, struct bch_fs, journal); -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas;u64 seq =le64_to_cpu (w->data->seq);int err =0 ;
@@ -2055,7 +2055,7 @@ CLOSURE_CALLBACK(bch2_journal_write) closure_type(w, struct journal_buf, io); struct journal *j = container_of(w, struct journal, buf[w->idx]); struct bch_fs *c = container_of(j, struct bch_fs, journal); -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas;unsigned nr_rw_members =dev_mask_nr (&c->rw_devs[BCH_DATA_journal]);int ret;
fs/bcachefs/journal_reclaim.c +1 -1
@@ -953,7 +953,7 @@ int bch2_journal_flush_device_pins(struct journal *j, int dev_idx) seq =0 ;spin_lock (&j->lock);while (!ret) { -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas; seq =max (seq,journal_last_seq (j));if (seq >= j->pin.back)
Thanks
-Gustavo
Quoted 11 lines · Gustavo A. R. Silva
Quoted 8 lines · Kent Overstreet
Quoted 7 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Use the
DEFINE_FLEX()helper for on-stack definitions of a flexible structure where the size of the flexible-array member is known at compile-time, and refactor the rest of the code, accordingly.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
So, with these changes, fix the following warning:
fs/bcachefs/disk_accounting.c:429:51: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]We also have
bch_replicas_padded, does that also need a fix?Yes - so, how about the following to address both issues:
Yes, I like that much better
Quoted 121 lines
fs/bcachefs/disk_accounting.c +7
-5@@ -287,7 +287,7 @@ static inline bool accounting_to_replicas(struct bch_replicas_entry_v1 *r, strucstatic int bch2_accounting_update_sb_one (struct bch_fs *c,struct bpos p) { -struct bch_replicas_padded r; +union bch_replicas_padded r;return accounting_to_replicas (&r.e, p) ?bch2_mark_replicas (c, &r.e) :0 ;@@ -361,7 +361,7 @@ static int __bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accounint bch2_accounting_mem_insert (struct bch_fs *c,struct bkey_s_c_accounting a,enum bch_accounting_mode mode) { -struct bch_replicas_padded r; +union bch_replicas_padded r; if (mode != BCH_ACCOUNTING_read && accounting_to_replicas(&r.e, a.k->p) &&@@ -425,10 +425,12 @@ int bch2_fs_replicas_usage_read(struct bch_fs *c, darray_char *usage)percpu_down_read (&c->mark_lock);darray_for_each (acc->k, i) { -struct { +union { + u8 bytes[struct_size_t(struct bch_replicas_usage, r.devs, +BCH_BKEY_PTRS_MAX )];struct bch_replicas_usage r; -u8 pad[BCH_BKEY_PTRS_MAX ]; } u; + u.r.r.nr_devs =BCH_BKEY_PTRS_MAX ;if (!accounting_to_replicas (&u.r.r, i->pos))continue ;@@ -627,7 +629,7 @@ static int bch2_disk_accounting_validate_late(struct btree_trans *trans,switch (acc->type) {case BCH_DISK_ACCOUNTING_replicas: { -struct bch_replicas_padded r; +union bch_replicas_padded r;__accounting_to_replicas (&r.e, acc);for (unsigned i =0 ; i < r.e.nr_devs; i++)fs/bcachefs/ec_types.h +4
-3@@ -4,9 +4,10 @@#include "bcachefs_format.h" -struct bch_replicas_padded { +union bch_replicas_padded { + u8 bytes[struct_size_t(struct bch_replicas_entry_v1, + devs,BCH_BKEY_PTRS_MAX )];struct bch_replicas_entry_v1 e; -u8 pad[BCH_BKEY_PTRS_MAX ]; };struct stripe {@@ -28,7 +29,7 @@ struct gc_stripe {u16 block_sectors[BCH_BKEY_PTRS_MAX ];struct bch_extent_ptr ptrs[BCH_BKEY_PTRS_MAX ]; - struct bch_replicas_padded r; + union bch_replicas_padded r; };#endif /* _BCACHEFS_EC_TYPES_H */ fs/bcachefs/journal_io.c +3
-3@@ -1404,7 +1404,7 @@ int bch2_journal_read(struct bch_fs *c, }genradix_for_each (&c->journal_entries, radix_iter, _i) { -struct bch_replicas_padded replicas = { +union bch_replicas_padded replicas = { .e.data_type = BCH_DATA_journal, .e.nr_devs =0 , .e.nr_required =1 ,@@ -1632,7 +1632,7 @@ static CLOSURE_CALLBACK(journal_write_done) closure_type(w, struct journal_buf, io); struct journal *j = container_of(w, struct journal, buf[w->idx]); struct bch_fs *c = container_of(j, struct bch_fs, journal); -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas;u64 seq =le64_to_cpu (w->data->seq);int err =0 ;@@ -2055,7 +2055,7 @@ CLOSURE_CALLBACK(bch2_journal_write) closure_type(w, struct journal_buf, io); struct journal *j = container_of(w, struct journal, buf[w->idx]); struct bch_fs *c = container_of(j, struct bch_fs, journal); -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas;unsigned nr_rw_members =dev_mask_nr (&c->rw_devs[BCH_DATA_journal]);int ret;fs/bcachefs/journal_reclaim.c +1
-1@@ -953,7 +953,7 @@ int bch2_journal_flush_device_pins(struct journal *j, int dev_idx) seq =0 ;spin_lock (&j->lock);while (!ret) { -struct bch_replicas_padded replicas; +union bch_replicas_padded replicas; seq =max (seq,journal_last_seq (j));if (seq >= j->pin.back)Thanks
-Gustavo