kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org
Subject: [PATCH][next] KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings
Date: Tue, 21 Oct 2025 19:12:57 +0100	[thread overview]
Message-ID: <aPfNKRpLfhmhYqfP@kspp> (raw)

-Wflex-array-member-not-at-end was introduced in GCC-14, and we are
getting ready to enable it, globally.

So, in order to avoid ending up with a flexible-array member in the
middle of multiple other structs, we use the `__struct_group()` helper
to separate the flexible array from the rest of the members in the
flexible structure, and use the tagged `struct kvm_stats_desc_hdr`
instead of `struct kvm_stats_desc`.

So, with these changes, fix 51 instances of the following type of
warning:

49 ./include/linux/kvm_host.h:1923:31: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
1 .../include/linux/kvm_host.h:1923:31: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
1 +./include/linux/kvm_host.h:1923:31: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Notice that, before and after the changes, struct sizes and member offsets
remain unchanged:

BEFORE

struct kvm_stats_desc {
        __u32                      flags;                /*     0     4 */
        __s16                      exponent;             /*     4     2 */
        __u16                      size;                 /*     6     2 */
        __u32                      offset;               /*     8     4 */
        __u32                      bucket_size;          /*    12     4 */
        char                       name[];               /*    16     0 */

        /* size: 16, cachelines: 1, members: 6 */
        /* last cacheline: 16 bytes */
};

struct _kvm_stats_desc {
        struct kvm_stats_desc      desc;                 /*     0    16 */
        char                       name[48];             /*    16    48 */

        /* size: 64, cachelines: 1, members: 2 */
};

AFTER:

struct kvm_stats_desc {
        union {
                struct {
                        __u32      flags;                /*     0     4 */
                        __s16      exponent;             /*     4     2 */
                        __u16      size;                 /*     6     2 */
                        __u32      offset;               /*     8     4 */
                        __u32      bucket_size;          /*    12     4 */
                };                                       /*     0    16 */
                struct kvm_stats_desc_hdr __hdr;         /*     0    16 */
        };                                               /*     0    16 */
        char                       name[];               /*    16     0 */

        /* size: 16, cachelines: 1, members: 2 */
        /* last cacheline: 16 bytes */
};

struct _kvm_stats_desc {
        struct kvm_stats_desc_hdr  desc;                 /*     0    16 */
        char                       name[48];             /*    16    48 */

        /* size: 64, cachelines: 1, members: 2 */
};


Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/uapi/linux/kvm.h | 21 ++++++++++++++++-----
 include/linux/kvm_host.h |  2 +-
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 6efa98a57ec1..99d13ebc5e82 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -14,6 +14,12 @@
 #include <linux/ioctl.h>
 #include <asm/kvm.h>
 
+#ifdef __KERNEL__
+#include <linux/stddef.h>       /* for offsetof */
+#else
+#include <stddef.h>             /* for offsetof */
+#endif
+
 #define KVM_API_VERSION 12
 
 /*
@@ -1563,13 +1569,18 @@ struct kvm_stats_header {
  *        &kvm_stats_header->name_size.
  */
 struct kvm_stats_desc {
-	__u32 flags;
-	__s16 exponent;
-	__u16 size;
-	__u32 offset;
-	__u32 bucket_size;
+	/* New members MUST be added within the __struct_group() macro below. */
+	__struct_group(kvm_stats_desc_hdr, __hdr, /* no attrs */,
+		__u32 flags;
+		__s16 exponent;
+		__u16 size;
+		__u32 offset;
+		__u32 bucket_size;
+	);
 	char name[];
 };
+_Static_assert(offsetof(struct kvm_stats_desc, name) == sizeof(struct kvm_stats_desc_hdr),
+	       "struct member likely outside of __struct_group()");
 
 #define KVM_GET_STATS_FD  _IO(KVMIO,  0xce)
 
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index fa36e70df088..c630991f72be 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1920,7 +1920,7 @@ struct kvm_stat_data {
 };
 
 struct _kvm_stats_desc {
-	struct kvm_stats_desc desc;
+	struct kvm_stats_desc_hdr desc;
 	char name[KVM_STATS_NAME_SIZE];
 };
 
-- 
2.43.0


             reply	other threads:[~2025-10-21 18:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-21 18:12 Gustavo A. R. Silva [this message]
2025-12-03  5:10 ` [PATCH][next] KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
2025-12-05 17:04 ` Sean Christopherson

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=aPfNKRpLfhmhYqfP@kspp \
    --to=gustavoars@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).