public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
To: Kent Overstreet <kent.overstreet@linux.dev>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH][next] bcachefs: Avoid -Wflex-array-member-not-at-end warning
Date: Wed, 30 Apr 2025 12:27:26 -0600	[thread overview]
Message-ID: <a1edcb19-f463-4d15-be3d-2393191c45fd@embeddedor.com> (raw)
In-Reply-To: <wxh5qw7hzvvyg4texozjbviicfe2xmbjxngwlc3a7xef6jm6kn@wi3f25t2fftd>



On 29/04/25 19:40, Kent Overstreet wrote:
> On Tue, Apr 29, 2025 at 07:15:37PM -0600, Gustavo A. R. Silva wrote:
>> -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?

Yes - so, how about the following to address both issues:

diff --git a/fs/bcachefs/disk_accounting.c b/fs/bcachefs/disk_accounting.c
index 7be71952425c..2dcccf1edbb9 100644
--- a/fs/bcachefs/disk_accounting.c
+++ b/fs/bcachefs/disk_accounting.c
@@ -287,7 +287,7 @@ static inline bool accounting_to_replicas(struct bch_replicas_entry_v1 *r, struc

  static 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_accoun
  int 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++)
diff --git a/fs/bcachefs/ec_types.h b/fs/bcachefs/ec_types.h
index 06144bfd9c19..809446c78951 100644
--- a/fs/bcachefs/ec_types.h
+++ b/fs/bcachefs/ec_types.h
@@ -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 */
diff --git a/fs/bcachefs/journal_io.c b/fs/bcachefs/journal_io.c
index 58e3983d860a..7c09a3c109ab 100644
--- a/fs/bcachefs/journal_io.c
+++ b/fs/bcachefs/journal_io.c
@@ -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;

diff --git a/fs/bcachefs/journal_reclaim.c b/fs/bcachefs/journal_reclaim.c
index 66bfb95f1ea4..0acaf20f2181 100644
--- a/fs/bcachefs/journal_reclaim.c
+++ b/fs/bcachefs/journal_reclaim.c
@@ -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

  reply	other threads:[~2025-04-30 18:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-30  1:15 [PATCH][next] bcachefs: Avoid -Wflex-array-member-not-at-end warning Gustavo A. R. Silva
2025-04-30  1:40 ` Kent Overstreet
2025-04-30 18:27   ` Gustavo A. R. Silva [this message]
2025-04-30 18:32     ` Kent Overstreet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a1edcb19-f463-4d15-be3d-2393191c45fd@embeddedor.com \
    --to=gustavo@embeddedor.com \
    --cc=gustavoars@kernel.org \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcachefs@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox