KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings
PATCH 1/1 KVM: Avoid a few dozen -Wflex-array-member-not-at-end warnings
-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 */ };
--- include/uapi/linux/kvm.h | 21 ++++++++++++++++----- include/linux/kvm_host.h | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-)
include/uapi/linux/kvm.h +16 -5
@@ -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)
include/linux/kvm_host.h +1 -1
@@ -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 ]; };
Signature · 1 line
2.43.0
Hi all,
Friendly ping: who can take this, please? :)
Thanks!
-Gustavo
Quoted 127 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.
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 taggedstruct kvm_stats_desc_hdrinstead ofstruct 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 <[email protected]>--- include/uapi/linux/kvm.h | 21 ++++++++++++++++----- include/linux/kvm_host.h | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-)include/uapi/linux/kvm.h +16
-5@@ -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)include/linux/kvm_host.h +1
-1@@ -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 ]; };
Sean Christopherson
maintainerQuoted 9 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.
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 taggedstruct kvm_stats_desc_hdrinstead ofstruct kvm_stats_desc.
Quoted 6 lines
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]
Eww, the existing code is groooooossssss. It took me far too long to even understand what is getting fixed. I looked this at least three different times before I spotted that there's an actual bug here.
I really, really don't want to fix this by layering more struct shenanigans on top. The code is already harder to read than it should be. It's a little ugly, but AFAICT we can address this by defining kvm_stats_desc.name as a fixed-size array when building for the kernel.
If that isn't feasible/palatable, but my next vote would be to create a proper overlay and add compile-time assertions to ensure the layouts are identical.
--
From: Sean Christopherson <[email protected]>
Date: Fri, 5 Dec 2025 08:47:34 -0800
Subject: [PATCH] KVM: Remove subtle "struct kvm_stats_desc" pseudo-overlay
Remove KVM's internal pseudo-overlay of kvm_stats_desc, which subtly aliases the flexible name[] in the uAPI definition with a fixed-size array of the same name. The unusual embedded structure results in a compiler wranings due to -Wflex-array-member-not-at-end, and also necessitates an extra level of derefencing in KVM. To avoid the "overlay", define the uAPI structure to have a fixed-size name when building for the kernel.
Opportunistically clean up the indentation for the stats macros, and replace spaces with tabs.
No functional change intended.
--- arch/arm64/kvm/guest.c | 4 +- arch/loongarch/kvm/vcpu.c | 2 +- arch/loongarch/kvm/vm.c | 2 +- arch/mips/kvm/mips.c | 4 +- arch/powerpc/kvm/book3s.c | 4 +- arch/powerpc/kvm/booke.c | 4 +- arch/riscv/kvm/vcpu.c | 2 +- arch/riscv/kvm/vm.c | 2 +- arch/s390/kvm/kvm-s390.c | 4 +- arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 83 +++++++++++++++++---------------------- include/uapi/linux/kvm.h | 8 ++++ virt/kvm/binary_stats.c | 2 +- virt/kvm/kvm_main.c | 20 +++++----- 14 files changed, 70 insertions(+), 75 deletions(-)
arch/arm64/kvm/guest.c +2 -2
@@ -29,7 +29,7 @@#include "trace.h" -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS () };
@@ -42,7 +42,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , hvc_exit_stat),STATS_DESC_COUNTER (VCPU , wfe_exit_stat),
arch/loongarch/kvm/vcpu.c +1 -1
@@ -13,7 +13,7 @@#define CREATE_TRACE_POINTS #include "trace.h" -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , int_exits),STATS_DESC_COUNTER (VCPU , idle_exits),
arch/loongarch/kvm/vm.c +1 -1
@@ -10,7 +10,7 @@#include <asm/kvm_eiointc.h> #include <asm/kvm_pch_pic.h> -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS (),STATS_DESC_ICOUNTER (VM , pages),STATS_DESC_ICOUNTER (VM , hugepages),
arch/mips/kvm/mips.c +2 -2
@@ -38,7 +38,7 @@#define VECTORSPACING 0x100/* for EI/VI mode */ #endif -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS () };
@@ -51,7 +51,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , wait_exits),STATS_DESC_COUNTER (VCPU , cache_exits),
arch/powerpc/kvm/book3s.c +2 -2
@@ -38,7 +38,7 @@/* #define EXIT_DEBUG */ -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS (),STATS_DESC_ICOUNTER (VM , num_2M_pages),STATS_DESC_ICOUNTER (VM , num_1G_pages)
@@ -53,7 +53,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , sum_exits),STATS_DESC_COUNTER (VCPU , mmio_exits),
arch/powerpc/kvm/booke.c +2 -2
@@ -36,7 +36,7 @@unsigned long kvmppc_booke_handlers; -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS (),STATS_DESC_ICOUNTER (VM , num_2M_pages),STATS_DESC_ICOUNTER (VM , num_1G_pages)
@@ -51,7 +51,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , sum_exits),STATS_DESC_COUNTER (VCPU , mmio_exits),
arch/riscv/kvm/vcpu.c +1 -1
@@ -24,7 +24,7 @@#define CREATE_TRACE_POINTS #include "trace.h" -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , ecall_exit_stat),STATS_DESC_COUNTER (VCPU , wfi_exit_stat),
arch/riscv/kvm/vm.c +1 -1
@@ -13,7 +13,7 @@#include <linux/kvm_host.h> #include <asm/kvm_mmu.h> -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS () }; static_assert(ARRAY_SIZE(kvm_vm_stats_desc) ==
arch/s390/kvm/kvm-s390.c +2 -2
@@ -65,7 +65,7 @@#define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ (KVM_MAX_VCPUS + LOCAL_IRQS)) -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS (),STATS_DESC_COUNTER (VM , inject_io),STATS_DESC_COUNTER (VM , inject_float_mchk),
@@ -91,7 +91,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , exit_userspace),STATS_DESC_COUNTER (VCPU , exit_null),
arch/x86/kvm/x86.c +2 -2
@@ -237,7 +237,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(enable_ipiv);bool __read_mostly enable_device_posted_irqs = true;EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_device_posted_irqs); -const struct _kvm_stats_desc kvm_vm_stats_desc[] = { +const struct kvm_stats_desc kvm_vm_stats_desc[] = {KVM_GENERIC_VM_STATS (),STATS_DESC_COUNTER (VM , mmu_shadow_zapped),STATS_DESC_COUNTER (VM , mmu_pte_write),
@@ -263,7 +263,7 @@ const struct kvm_stats_header kvm_vm_stats_header = { sizeof(kvm_vm_stats_desc), }; -const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {KVM_GENERIC_VCPU_STATS (),STATS_DESC_COUNTER (VCPU , pf_taken),STATS_DESC_COUNTER (VCPU , pf_fixed),
include/linux/kvm_host.h +35 -48
107 lines not on this page · raw message
include/uapi/linux/kvm.h +8 -0
@@ -14,6 +14,10 @@#include <linux/ioctl.h> #include <asm/kvm.h> +#ifdef __KERNEL__ +#include <linux/kvm_types.h> +#endif +#define KVM_API_VERSION 12/*
@@ -1579,7 +1583,11 @@ struct kvm_stats_desc {__u16 size;__u32 offset;__u32 bucket_size; +#ifdef __KERNEL__ +char name[KVM_STATS_NAME_SIZE ]; +#else char name[]; +#endif };#define KVM_GET_STATS_FD _IO(KVMIO, 0xce)
virt/kvm/binary_stats.c +1 -1
@@ -50,7 +50,7 @@* Return: the number of bytes that has been successfully read */ ssize_t kvm_stats_read (char *id,const struct kvm_stats_header *header, -const struct _kvm_stats_desc *desc, +const struct kvm_stats_desc *desc,void *stats,size_t size_stats,char __user *user_buffer,size_t size,loff_t *offset) {
virt/kvm/kvm_main.c +10 -10
@@ -982,9 +982,9 @@ static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)kvm_free_memslot (kvm, memslot); } -static umode_t kvm_stats_debugfs_mode (const struct _kvm_stats_desc *pdesc) +static umode_t kvm_stats_debugfs_mode (const struct kvm_stats_desc *desc) { -switch (pdesc->desc.flags &KVM_STATS_TYPE_MASK ) { +switch (desc->flags &KVM_STATS_TYPE_MASK ) {case KVM_STATS_TYPE_INSTANT :return 0444 ;case KVM_STATS_TYPE_CUMULATIVE :
@@ -1019,7 +1019,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)struct dentry *dent;char dir_name[ITOA_MAX_LEN *2 ];struct kvm_stat_data *stat_data; -const struct _kvm_stats_desc *pdesc; +const struct kvm_stats_desc *pdesc;int i, ret = -ENOMEM ;int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc + kvm_vcpu_stats_header.num_desc;
@@ -6160,11 +6160,11 @@ static int kvm_stat_data_get(void *data, u64 *val)switch (stat_data->kind) {case KVM_STAT_VM : r =kvm_get_stat_per_vm (stat_data->kvm, - stat_data->desc->desc.offset, val); + stat_data->desc->offset, val);break ;case KVM_STAT_VCPU : r =kvm_get_stat_per_vcpu (stat_data->kvm, - stat_data->desc->desc.offset, val); + stat_data->desc->offset, val);break ; }
@@ -6182,11 +6182,11 @@ static int kvm_stat_data_clear(void *data, u64 val)switch (stat_data->kind) {case KVM_STAT_VM : r =kvm_clear_stat_per_vm (stat_data->kvm, - stat_data->desc->desc.offset); + stat_data->desc->offset);break ;case KVM_STAT_VCPU : r =kvm_clear_stat_per_vcpu (stat_data->kvm, - stat_data->desc->desc.offset); + stat_data->desc->offset);break ; }
@@ -6334,7 +6334,7 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)static void kvm_init_debug (void ) {const struct file_operations *fops; -const struct _kvm_stats_desc *pdesc; +const struct kvm_stats_desc *pdesc;int i; kvm_debugfs_dir =debugfs_create_dir ("kvm" ,NULL );
@@ -6347,7 +6347,7 @@ static void kvm_init_debug(void) fops = &vm_stat_readonly_fops;debugfs_create_file (pdesc->name,kvm_stats_debugfs_mode (pdesc), kvm_debugfs_dir, - (void *)(long )pdesc->desc.offset, fops); + (void *)(long )pdesc->offset, fops); }for (i =0 ; i < kvm_vcpu_stats_header.num_desc; ++i) {
@@ -6358,7 +6358,7 @@ static void kvm_init_debug(void) fops = &vcpu_stat_readonly_fops;debugfs_create_file (pdesc->name,kvm_stats_debugfs_mode (pdesc), kvm_debugfs_dir, - (void *)(long )pdesc->desc.offset, fops); + (void *)(long )pdesc->offset, fops); } }
base-commit: e0c26d47def7382d7dbd9cad58bc653aed75737a --