public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
To: David Woodhouse <dwmw2@infradead.org>, keescook@chromium.org
Cc: daniel@iogearbox.net, gustavoars@kernel.org, jgg@ziepe.ca,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members
Date: Mon, 23 Feb 2026 12:38:07 +0900	[thread overview]
Message-ID: <da02314c-e6da-4d9e-a2c8-cd3ee096bc0c@embeddedor.com> (raw)
In-Reply-To: <aaa7ac93db25459fa5a629d0da5abf13e93d8301.camel@infradead.org>



On 2/23/26 23:28, David Woodhouse wrote:
> On 2022-07-28 at 10:54:58 -0700, Kees Cook <keescook@chromium.org> wrote:
>> The issue here seems to be a collision between "unknown array size"
>> and known sizes:
>>
>> struct bpf_lpm_trie_key {
>>          __u32   prefixlen;      /* up to 32 for AF_INET, 128 for AF_INET6 */
>>          __u8    data[0];        /* Arbitrary size */
>> };
>>
>> struct lpm_key {
>> 	struct bpf_lpm_trie_key trie_key;
>> 	__u32 data;
>> };
>>
>> This is treating trie_key as a header, which it's not: it's a complete
>> structure. :)
>>
>> Perhaps:
>>
>> struct lpm_key {
>>          __u32 prefixlen;
>>          __u32 data;
>> };
>>
>> I don't see anything else trying to include bpf_lpm_trie_key.
> 
> What was the eventual conclusion here?
> 
> 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[];
  };

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index fdec90e85467..a354ee1ef359 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -261,6 +261,8 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
         -fno-builtin-memcmp -fno-builtin-memcpy \
         -fno-builtin-memset -fno-builtin-strnlen \
         -fno-stack-protector -fno-PIE -fno-strict-aliasing \
+       -Wno-microsoft-anon-tag \
+       -fms-extensions \
         -I$(LINUX_TOOL_INCLUDE) -I$(LINUX_TOOL_ARCH_INCLUDE) \
         -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -Iinclude/$(ARCH) \
         -I ../rseq -I.. $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index fab18e9be66c..0f881ee8c37c 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -1076,7 +1076,7 @@ uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index)
  int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value)
  {
         struct {
-               struct kvm_msrs header;
+               struct kvm_msrs_hdr header;
                 struct kvm_msr_entry entry;
         } buffer = {};


(patch above untested)

-Gustavo

> 
> struct msr_data *alloc_msr_data(void)
> {
> 	return static_cast<struct msr_data *>(malloc(sizeof(struct msr_data)));
> }
> 
>   $ g++    -c -o test_kvm.o test_kvm.cpp
> In file included from test_kvm.cpp:2:
> /usr/include/x86_64-linux-gnu/asm/kvm.h:194:30: error: flexible array member ‘kvm_msrs::entries’ not at end of ‘struct msr_data’
>    194 |         struct kvm_msr_entry entries[];
>        |                              ^~~~~~~
> test_kvm.cpp:6:30: note: next member ‘kvm_msr_entry msr_data::entries [1]’ declared here
>      6 |         struct kvm_msr_entry entries[1];
>        |                              ^~~~~~~
> test_kvm.cpp:4:8: note: in the definition of ‘struct msr_data’
>      4 | struct msr_data {
>        |        ^~~~~~~~
> 


  reply	other threads:[~2026-02-23 18:39 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 [this message]
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
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=da02314c-e6da-4d9e-a2c8-cd3ee096bc0c@embeddedor.com \
    --to=gustavo@embeddedor.com \
    --cc=daniel@iogearbox.net \
    --cc=dwmw2@infradead.org \
    --cc=gustavoars@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=keescook@chromium.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.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