public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: "Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	keescook@chromium.org, daniel@iogearbox.net,
	 gustavoars@kernel.org, jgg@ziepe.ca, kvm@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Thomas Gleixner <tglx@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  "H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH] KVM: x86: Fix C++ user API for structures with variable length arrays
Date: Thu, 5 Mar 2026 10:36:47 -0800	[thread overview]
Message-ID: <aanNPwnH7l-j61Ds@google.com> (raw)
In-Reply-To: <97d40dd0e6abaf28f43d4d8ccf9c547a16c52e33.camel@infradead.org>

On Thu, Feb 26, 2026, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
> 
> Commit 94dfc73e7cf4 ("treewide: uapi: Replace zero-length arrays with
> flexible-array members") broke the userspace API for C++. Not just in
> the sense of 'userspace needs to be updated, but UAPI is supposed to be
> stable", but broken in the sense that I can't actually see *how* the
> structures can be used from C++ in the same way that they were usable
> before.
> 
> These structures ending in VLAs are typically a *header*, which can be
> followed by an arbitrary number of entries. Userspace typically creates
> a larger structure with some non-zero number of entries, for example in
> QEMU's kvm_arch_get_supported_msr_feature():
> 
>     struct {
>         struct kvm_msrs info;
>         struct kvm_msr_entry entries[1];
>     } msr_data = {};
> 
> While that works in C, it fails in C++ with an error like:
>  flexible array member ‘kvm_msrs::entries’ not at end of ‘struct msr_data’
> 
> Fix this by using __DECLARE_FLEX_ARRAY() for the VLA, which is a helper
> provided by <linux/stddef.h> that already uses [0] for C++ compilation.
> 
> Also put the header fields into a struct_group() to provide (in C) a
> separate struct (e.g 'struct kvm_msrs_hdr') without the trailing VLA.

Unless I'm missing something, this is an entirely optional change that needs to
be done separately, especialy since I want to tag this for:

  Cc: stable@vger.kernel.org

I definitely don't hate the __struct_group definitions, but I don't know that I
love them either as they make the code a bit harder to read, and more importantly
there's a non-zero chance that defining the new structurs could break userspace
builds and force an update, e.g. if userspace already concocts its own header
overlay, which would be very unpleasant for a stable@ patch.

If we do define headers, I think I'd want a wrapper around __struct_group() to
prettify the common case and force consistent naming, e.g.

#define kvm_struct_header(NAME, MEMBERS...)				\
	__struct_group(NAME ##_header, h, /* no attrs */, MEMBERS)

struct kvm_msrs {
	kvm_struct_header(kvm_msrs,
		__u32 nmsrs; /* number of msrs in entries */
		__u32 pad;
	);

	__DECLARE_FLEX_ARRAY(struct kvm_msr_entry, entries);
};

But that's likely going to lead to some amount of bikeshedding, e.g. arguably
kvm_header() would be sufficient and easier on the eyes.  Which is all the more
reason to handle it separately.

> Fixes: 94dfc73e7cf4 ("treewide: uapi: Replace zero-length arrays with flexible-array members")
> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
> ---
>  arch/x86/include/uapi/asm/kvm.h | 29 ++++++++++++++++++-----------
>  include/uapi/linux/kvm.h        |  9 ++++++---
>  /* for KVM_GET_PIT and KVM_SET_PIT */
> @@ -397,8 +402,10 @@ struct kvm_xsave {
>  	 * The offsets of the state save areas in struct kvm_xsave follow
>  	 * the contents of CPUID leaf 0xD on the host.
>  	 */
> -	__u32 region[1024];
> -	__u32 extra[];
> +	__struct_group(kvm_xsave_hdr, hdr, /* no attrs */,
> +		__u32 region[1024];
> +	);

This is *very* misleading, as XSTATE itself has a header, but this is something
else entirely (just the always-allocated region).

> +	__DECLARE_FLEX_ARRAY(__u32, extra);
>  };

There are several structs that got missed:

  kvm_pmu_event_filter
  kvm_reg_list
  kvm_signal_mask
  kvm_coalesced_mmio_ring
  kvm_cpuid
  kvm_stats_desc

  parent reply	other threads:[~2026-03-05 18:36 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27 18:04 [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members Gustavo A. R. Silva
2022-06-27 18:27 ` Daniel Borkmann
2022-06-27 18:35   ` Gustavo A. R. Silva
2022-06-28  0:40   ` Jason Gunthorpe
2022-06-28  0:58     ` Gustavo A. R. Silva
2022-06-28  2:21       ` Gustavo A. R. Silva
2022-06-28 13:36         ` Jason Gunthorpe
2022-06-28 13:56           ` Gustavo A. R. Silva
2022-06-28 17:54     ` Kees Cook
2022-06-28 18:44       ` Jason Gunthorpe
2026-02-23 14:28       ` David Woodhouse
2026-02-23  3:38         ` Gustavo A. R. Silva
2026-02-23 19:57           ` David Woodhouse
2026-02-26 11:44           ` [PATCH] KVM: x86: Fix C++ user API for structures with variable length arrays David Woodhouse
2026-02-26 19:02             ` Kees Cook
2026-02-27  8:29               ` David Woodhouse
2026-02-28  0:43                 ` Kees Cook
2026-02-28  8:54                   ` David Woodhouse
2026-03-05 18:36             ` Sean Christopherson [this message]
2026-03-05 19:18               ` David Woodhouse
2026-03-05 19:31                 ` Sean Christopherson
2026-03-05 19:49                   ` [PATCH v2] KVM: x86: Use __DECLARE_FLEX_ARRAY() for UAPI structures with VLAs David Woodhouse
2022-06-27 19:53 ` [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members Stephen Hemminger
2022-06-28 14:18   ` Gustavo A. R. Silva
2022-06-27 22:31 ` Dan Williams
2022-06-28  7:27 ` Geert Uytterhoeven
2022-06-28 18:05   ` Kees Cook

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=aanNPwnH7l-j61Ds@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=gustavo@embeddedor.com \
    --cc=gustavoars@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jgg@ziepe.ca \
    --cc=keescook@chromium.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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