]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: Remove subtle "struct kvm_stats_desc" pseudo-overlay
authorSean Christopherson <seanjc@google.com>
Fri, 5 Dec 2025 23:26:55 +0000 (15:26 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 8 Jan 2026 18:40:48 +0000 (10:40 -0800)
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 compiler
warnings due to -Wflex-array-member-not-at-end, and also necessitates an
extra level of dereferencing 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.

Reported-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Closes: https://lore.kernel.org/all/aPfNKRpLfhmhYqfP@kspp
Acked-by: Marc Zyngier <maz@kernel.org>
Acked-by: Christian Borntraeger <borntraeger@linux.ibm.com>
[..]
Acked-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Bibo Mao <maobibo@loongson.cn>
Acked-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://patch.msgid.link/20251205232655.445294-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
14 files changed:
arch/arm64/kvm/guest.c
arch/loongarch/kvm/vcpu.c
arch/loongarch/kvm/vm.c
arch/mips/kvm/mips.c
arch/powerpc/kvm/book3s.c
arch/powerpc/kvm/booke.c
arch/riscv/kvm/vcpu.c
arch/riscv/kvm/vm.c
arch/s390/kvm/kvm-s390.c
arch/x86/kvm/x86.c
include/linux/kvm_host.h
include/uapi/linux/kvm.h
virt/kvm/binary_stats.c
virt/kvm/kvm_main.c

index 1c87699fd886e7d56b2b6b3298427f738f8cceb8..332c453b87cf8c25918a418d9ca37ebc72ee1234 100644 (file)
@@ -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),
index 656b954c1134b1769f56d044ac6ac03f626c96e4..b3547bd41d21af8c2f764445bf489422b1d538f2 100644 (file)
@@ -14,7 +14,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),
index 194ccbcdc3b3830c794c25e252128a3244729ef2..7deff56e0e1ab99023f75d7f666360782d82fdeb 100644 (file)
@@ -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),
index b0fb92fda4d4235b3d51b5eb94aec25287d4e119..23e69baad453cbc8a752ea1ed5bbac4e755d31b5 100644 (file)
@@ -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),
index d79c5d1098c05c81d2c189f570a3ac4866c3811b..2efbe05caed760f6520016a453a0deca092a39a2 100644 (file)
@@ -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),
index 3401b96be475ef9230890dc09a8949df15003776..f3ddb24ece749fa03f4e1440f94484d29eda0295 100644 (file)
@@ -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),
index a55a95da54d0fa6473e06eefbfc64c35afe80dd7..fdd99ac1e714824f5bbd600e2fdd776d12512c52 100644 (file)
@@ -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),
index 66d91ae6e9b2ad27cbc17e8a9a4ce9be680d9ea2..715a06ae8c1313707e55dfc2e4fbd33eae380d38 100644 (file)
@@ -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) ==
index 56a50524b3eee4e17b5b8a5833c2a560d120c443..495141bf03989af9aca304656ddfd6148dbcbc20 100644 (file)
@@ -64,7 +64,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),
@@ -90,7 +90,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),
index ff8812f3a1293e6e4f0d39a82ee32bfe086b55e6..69e0a033e4ac9fc9864ac9b8623a6b9e93aebe41 100644 (file)
@@ -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),
index d93f75b05ae2272c1ba8e10dde8c34b38f0e2eb6..7428d9949382fdc1cea30f6c5f9fffb628006ecb 100644 (file)
@@ -1927,56 +1927,43 @@ enum kvm_stat_kind {
 
 struct kvm_stat_data {
        struct kvm *kvm;
-       const struct _kvm_stats_desc *desc;
+       const struct kvm_stats_desc *desc;
        enum kvm_stat_kind kind;
 };
 
-struct _kvm_stats_desc {
-       struct kvm_stats_desc desc;
-       char name[KVM_STATS_NAME_SIZE];
-};
-
-#define STATS_DESC_COMMON(type, unit, base, exp, sz, bsz)                     \
-       .flags = type | unit | base |                                          \
-                BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) |              \
-                BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) |              \
-                BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK),               \
-       .exponent = exp,                                                       \
-       .size = sz,                                                            \
+#define STATS_DESC_COMMON(type, unit, base, exp, sz, bsz)              \
+       .flags = type | unit | base |                                   \
+                BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) |       \
+                BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) |       \
+                BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK),        \
+       .exponent = exp,                                                \
+       .size = sz,                                                     \
        .bucket_size = bsz
 
-#define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz)           \
-       {                                                                      \
-               {                                                              \
-                       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),     \
-                       .offset = offsetof(struct kvm_vm_stat, generic.stat)   \
-               },                                                             \
-               .name = #stat,                                                 \
-       }
-#define VCPU_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz)         \
-       {                                                                      \
-               {                                                              \
-                       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),     \
-                       .offset = offsetof(struct kvm_vcpu_stat, generic.stat) \
-               },                                                             \
-               .name = #stat,                                                 \
-       }
-#define VM_STATS_DESC(stat, type, unit, base, exp, sz, bsz)                   \
-       {                                                                      \
-               {                                                              \
-                       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),     \
-                       .offset = offsetof(struct kvm_vm_stat, stat)           \
-               },                                                             \
-               .name = #stat,                                                 \
-       }
-#define VCPU_STATS_DESC(stat, type, unit, base, exp, sz, bsz)                 \
-       {                                                                      \
-               {                                                              \
-                       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),     \
-                       .offset = offsetof(struct kvm_vcpu_stat, stat)         \
-               },                                                             \
-               .name = #stat,                                                 \
-       }
+#define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz)    \
+{                                                                      \
+       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),              \
+       .offset = offsetof(struct kvm_vm_stat, generic.stat),           \
+       .name = #stat,                                                  \
+}
+#define VCPU_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz)  \
+{                                                                      \
+       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),              \
+       .offset = offsetof(struct kvm_vcpu_stat, generic.stat),         \
+       .name = #stat,                                                  \
+}
+#define VM_STATS_DESC(stat, type, unit, base, exp, sz, bsz)            \
+{                                                                      \
+       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),              \
+       .offset = offsetof(struct kvm_vm_stat, stat),                   \
+       .name = #stat,                                                  \
+}
+#define VCPU_STATS_DESC(stat, type, unit, base, exp, sz, bsz)          \
+{                                                                      \
+       STATS_DESC_COMMON(type, unit, base, exp, sz, bsz),              \
+       .offset = offsetof(struct kvm_vcpu_stat, stat),                 \
+       .name = #stat,                                                  \
+}
 /* SCOPE: VM, VM_GENERIC, VCPU, VCPU_GENERIC */
 #define STATS_DESC(SCOPE, stat, type, unit, base, exp, sz, bsz)                       \
        SCOPE##_STATS_DESC(stat, type, unit, base, exp, sz, bsz)
@@ -2053,7 +2040,7 @@ struct _kvm_stats_desc {
        STATS_DESC_IBOOLEAN(VCPU_GENERIC, blocking)
 
 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);
 
@@ -2098,9 +2085,9 @@ static inline void kvm_stats_log_hist_update(u64 *data, size_t size, u64 value)
 
 
 extern const struct kvm_stats_header kvm_vm_stats_header;
-extern const struct _kvm_stats_desc kvm_vm_stats_desc[];
+extern const struct kvm_stats_desc kvm_vm_stats_desc[];
 extern const struct kvm_stats_header kvm_vcpu_stats_header;
-extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[];
+extern const struct kvm_stats_desc kvm_vcpu_stats_desc[];
 
 #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER
 static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq)
index dddb781b0507dcfe44079118ec1948095e21de3d..76bd54848b112620f5dbfd837f8b8de10c275296 100644 (file)
 #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)
index eefca6c69f519ebf109e41a93bcce32ae7ebac18..76ce697c773bf59e5fc9071aca5c8eee2b1b41b4 100644 (file)
@@ -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)
 {
index 5b5b69c97665ec35ee473908678fdf69522bc6ad..cf65fd82d36dd74f623ef0336322e27197a8418a 100644 (file)
@@ -983,9 +983,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:
@@ -1020,7 +1020,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;
@@ -6186,11 +6186,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;
        }
 
@@ -6208,11 +6208,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;
        }
 
@@ -6360,7 +6360,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);
@@ -6373,7 +6373,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) {
@@ -6384,7 +6384,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);
        }
 }