Re: [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members
From: David Woodhouse
Date: Mon Feb 23 2026 - 15:00:10 EST
On Mon, 2026-02-23 at 12:38 +0900, Gustavo A. R. Silva wrote:
> > These changes broke userspace compilation, because a *lot* of these
> > structures ending with VLAs are very much used as headers.
> >
> > QEMU does it, for example...
> >
> > uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index)
> > {
> > struct {
> > struct kvm_msrs info;
> > struct kvm_msr_entry entries[1];
> > } msr_data = {};
> >
> >
> > ... which works in C but not in C++, which gives an error I can't work
> > out how to avoid.
> >
> > Am I missing something, or did we break our userspace headers for C++
> > and make them C-only?
> >
> > $ cat test_kvm.cpp
> > #include <cstdlib>
> > #include <asm/kvm.h>
> >
> > struct msr_data {
> > struct kvm_msrs info;
> > struct kvm_msr_entry entries[1];
> > };
>
> Something like this should eventually land:
>
> diff --git a/tools/arch/x86/include/uapi/asm/kvm.h b/tools/arch/x86/include/uapi/asm/kvm.h
> index 7ceff6583652..295b2a54b25d 100644
> --- a/tools/arch/x86/include/uapi/asm/kvm.h
> +++ b/tools/arch/x86/include/uapi/asm/kvm.h
> @@ -192,11 +192,14 @@ struct kvm_msr_entry {
> __u64 data;
> };
>
> -/* for KVM_GET_MSRS and KVM_SET_MSRS */
> -struct kvm_msrs {
> +struct kvm_msrs_hdr {
> __u32 nmsrs; /* number of msrs in entries */
> __u32 pad;
> +};
>
> +/* for KVM_GET_MSRS and KVM_SET_MSRS */
> +struct kvm_msrs {
> + struct kvm_msrs_hdr;
> struct kvm_msr_entry entries[];
> };
I guess that would work, although it's still breaking the userspace
API.
I was thinking of something along the lines of
#ifdef __cplusplus
#define __KERNEL_VLA 0
#else
#define __KERNEL_VLA
#endif
struct kvm_msrs {
__u32 nmsrs; /* number of msrs in entries */
__u32 pad;
struct kvm_msr_entry entries[__KERNEL_VLA];
};
I suppose it could depend on __KERNEL__ instead of __cplusplus too?
Attachment:
smime.p7s
Description: S/MIME cryptographic signature