[PATCH][next] KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings

Gustavo A. R. Silva posted 1 patch 12 hours ago
include/uapi/linux/kvm.h | 21 ++++++++++++++++-----
include/linux/kvm_host.h |  2 +-
2 files changed, 17 insertions(+), 6 deletions(-)
[PATCH][next] KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings
Posted by Gustavo A. R. Silva 12 hours ago
-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