* [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
@ 2025-11-12 17:16 Thorsten Blum
2025-11-12 19:59 ` Edgecombe, Rick P
0 siblings, 1 reply; 8+ messages in thread
From: Thorsten Blum @ 2025-11-12 17:16 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
Kirill A. Shutemov, Rick Edgecombe
Cc: Thorsten Blum, kvm, linux-kernel, linux-coco
Retrieve the number of user entries with get_user() first and return
-E2BIG early if 'user_caps' is too small to fit 'caps'.
Allocate memory for 'caps' only after checking the user buffer's number
of entries, thus removing two gotos and the need for premature freeing.
Use struct_size() instead of manually calculating the number of bytes to
allocate for 'caps', including the nested flexible array.
Finally, copy 'caps' to user space with a single copy_to_user() call.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
arch/x86/kvm/vmx/tdx.c | 32 ++++++++++++--------------------
1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 0a49c863c811..23d638b4a003 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -2282,37 +2282,29 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
if (cmd->flags)
return -EINVAL;
- caps = kzalloc(sizeof(*caps) +
- sizeof(struct kvm_cpuid_entry2) * td_conf->num_cpuid_config,
- GFP_KERNEL);
- if (!caps)
- return -ENOMEM;
-
user_caps = u64_to_user_ptr(cmd->data);
- if (get_user(nr_user_entries, &user_caps->cpuid.nent)) {
- ret = -EFAULT;
- goto out;
- }
+ ret = get_user(nr_user_entries, &user_caps->cpuid.nent);
+ if (ret)
+ return ret;
- if (nr_user_entries < td_conf->num_cpuid_config) {
- ret = -E2BIG;
- goto out;
- }
+ if (nr_user_entries < td_conf->num_cpuid_config)
+ return -E2BIG;
+
+ caps = kzalloc(struct_size(caps, cpuid.entries,
+ td_conf->num_cpuid_config), GFP_KERNEL);
+ if (!caps)
+ return -ENOMEM;
ret = init_kvm_tdx_caps(td_conf, caps);
if (ret)
goto out;
- if (copy_to_user(user_caps, caps, sizeof(*caps))) {
+ if (copy_to_user(user_caps, caps, struct_size(caps, cpuid.entries,
+ caps->cpuid.nent))) {
ret = -EFAULT;
goto out;
}
- if (copy_to_user(user_caps->cpuid.entries, caps->cpuid.entries,
- caps->cpuid.nent *
- sizeof(caps->cpuid.entries[0])))
- ret = -EFAULT;
-
out:
/* kfree() accepts NULL. */
kfree(caps);
--
2.51.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-12 17:16 [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities Thorsten Blum
@ 2025-11-12 19:59 ` Edgecombe, Rick P
2025-11-12 20:24 ` Sean Christopherson
2025-11-12 21:22 ` Thorsten Blum
0 siblings, 2 replies; 8+ messages in thread
From: Edgecombe, Rick P @ 2025-11-12 19:59 UTC (permalink / raw)
To: seanjc@google.com, x86@kernel.org, dave.hansen@linux.intel.com,
thorsten.blum@linux.dev, bp@alien8.de, mingo@redhat.com,
tglx@linutronix.de, hpa@zytor.com, pbonzini@redhat.com,
kas@kernel.org
Cc: kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org
On Wed, 2025-11-12 at 18:16 +0100, Thorsten Blum wrote:
kvm x86 logs are suggested to start with a short summary of the patch. Maybe:
Simplify the logic for copying the KVM_TDX_CAPABILITIES struct to userspace.
It looks like you are conducting a treewide pattern matching cleanup?
> > Retrieve the number of user entries with get_user() first and return
> > -E2BIG early if 'user_caps' is too small to fit 'caps'.
> >
> > Allocate memory for 'caps' only after checking the user buffer's number
> > of entries, thus removing two gotos and the need for premature freeing.
> >
> > Use struct_size() instead of manually calculating the number of bytes to
> > allocate for 'caps', including the nested flexible array.
> >
> > Finally, copy 'caps' to user space with a single copy_to_user() call.
In the handling of get_user(nr_user_entries, &user_caps->cpuid.nent), the old
code forced -EFAULT, this patch doesn't. But it leaves the copy_to_user()'s to
still force EFAULT. Why?
Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> (really the TDX CI)
> >
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > ---
> > arch/x86/kvm/vmx/tdx.c | 32 ++++++++++++--------------------
> > 1 file changed, 12 insertions(+), 20 deletions(-)
> >
> > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> > index 0a49c863c811..23d638b4a003 100644
> > --- a/arch/x86/kvm/vmx/tdx.c
> > +++ b/arch/x86/kvm/vmx/tdx.c
> > @@ -2282,37 +2282,29 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
> > if (cmd->flags)
> > return -EINVAL;
> >
> > - caps = kzalloc(sizeof(*caps) +
> > - sizeof(struct kvm_cpuid_entry2) * td_conf->num_cpuid_config,
> > - GFP_KERNEL);
> > - if (!caps)
> > - return -ENOMEM;
> > -
> > user_caps = u64_to_user_ptr(cmd->data);
> > - if (get_user(nr_user_entries, &user_caps->cpuid.nent)) {
> > - ret = -EFAULT;
> > - goto out;
> > - }
> > + ret = get_user(nr_user_entries, &user_caps->cpuid.nent);
> > + if (ret)
> > + return ret;
> >
> > - if (nr_user_entries < td_conf->num_cpuid_config) {
> > - ret = -E2BIG;
> > - goto out;
> > - }
> > + if (nr_user_entries < td_conf->num_cpuid_config)
> > + return -E2BIG;
> > +
> > + caps = kzalloc(struct_size(caps, cpuid.entries,
> > + td_conf->num_cpuid_config), GFP_KERNEL);
> > + if (!caps)
> > + return -ENOMEM;
> >
> > ret = init_kvm_tdx_caps(td_conf, caps);
> > if (ret)
> > goto out;
> >
> > - if (copy_to_user(user_caps, caps, sizeof(*caps))) {
> > + if (copy_to_user(user_caps, caps, struct_size(caps, cpuid.entries,
> > + caps->cpuid.nent))) {
> > ret = -EFAULT;
> > goto out;
> > }
> >
> > - if (copy_to_user(user_caps->cpuid.entries, caps->cpuid.entries,
> > - caps->cpuid.nent *
> > - sizeof(caps->cpuid.entries[0])))
> > - ret = -EFAULT;
> > -
> > out:
> > /* kfree() accepts NULL. */
> > kfree(caps);
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-12 19:59 ` Edgecombe, Rick P
@ 2025-11-12 20:24 ` Sean Christopherson
2025-11-13 0:41 ` Edgecombe, Rick P
2025-11-12 21:22 ` Thorsten Blum
1 sibling, 1 reply; 8+ messages in thread
From: Sean Christopherson @ 2025-11-12 20:24 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: x86@kernel.org, dave.hansen@linux.intel.com,
thorsten.blum@linux.dev, bp@alien8.de, mingo@redhat.com,
tglx@linutronix.de, hpa@zytor.com, pbonzini@redhat.com,
kas@kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org
On Wed, Nov 12, 2025, Rick P Edgecombe wrote:
> On Wed, 2025-11-12 at 18:16 +0100, Thorsten Blum wrote:
>
> kvm x86 logs are suggested to start with a short summary of the patch. Maybe:
>
> Simplify the logic for copying the KVM_TDX_CAPABILITIES struct to userspace.
Yeah, I have this locally as two separate patches:
KVM: TDX: Use struct_size to simplify tdx_get_capabilities()
KVM: TDX: Check size of user's kvm_tdx_capabilities array before allocating
Your CI caught me just in time; I applied this locally last week, but haven't
fully pushed it to kvm-x86 yet. :-)
> It looks like you are conducting a treewide pattern matching cleanup?
>
> > > Retrieve the number of user entries with get_user() first and return
> > > -E2BIG early if 'user_caps' is too small to fit 'caps'.
> > >
> > > Allocate memory for 'caps' only after checking the user buffer's number
> > > of entries, thus removing two gotos and the need for premature freeing.
> > >
> > > Use struct_size() instead of manually calculating the number of bytes to
> > > allocate for 'caps', including the nested flexible array.
> > >
> > > Finally, copy 'caps' to user space with a single copy_to_user() call.
>
> In the handling of get_user(nr_user_entries, &user_caps->cpuid.nent), the old
> code forced -EFAULT, this patch doesn't. But it leaves the copy_to_user()'s to
> still force EFAULT. Why?
I'll tweak it to explicitly return -EFAULT. Doesn't matter terribly, but KVM's
standard pattern is to explicitly return -EFAULT.
> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> (really the TDX CI)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-12 19:59 ` Edgecombe, Rick P
2025-11-12 20:24 ` Sean Christopherson
@ 2025-11-12 21:22 ` Thorsten Blum
1 sibling, 0 replies; 8+ messages in thread
From: Thorsten Blum @ 2025-11-12 21:22 UTC (permalink / raw)
To: Edgecombe, Rick P
Cc: seanjc@google.com, x86@kernel.org, dave.hansen@linux.intel.com,
bp@alien8.de, mingo@redhat.com, tglx@linutronix.de, hpa@zytor.com,
pbonzini@redhat.com, kas@kernel.org, kvm@vger.kernel.org,
linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org
On 12. Nov 2025, at 20:59, Edgecombe, Rick P wrote:
> It looks like you are conducting a treewide pattern matching cleanup?
Just a few instances here and there, but not really treewide.
> In the handling of get_user(nr_user_entries, &user_caps->cpuid.nent), the old
> code forced -EFAULT, this patch doesn't. But it leaves the copy_to_user()'s to
> still force EFAULT. Why?
get_user() already returns -EFAULT and the error can just be forwarded,
whereas copy_to_user() returns the number of bytes that could not be
copied and we must return -EFAULT manually.
> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> (really the TDX CI)
Thanks,
Thorsten
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-12 20:24 ` Sean Christopherson
@ 2025-11-13 0:41 ` Edgecombe, Rick P
2025-11-13 16:29 ` Sean Christopherson
0 siblings, 1 reply; 8+ messages in thread
From: Edgecombe, Rick P @ 2025-11-13 0:41 UTC (permalink / raw)
To: seanjc@google.com
Cc: bp@alien8.de, x86@kernel.org, thorsten.blum@linux.dev,
hpa@zytor.com, mingo@redhat.com, tglx@linutronix.de,
dave.hansen@linux.intel.com, pbonzini@redhat.com,
kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org, kas@kernel.org
On Wed, 2025-11-12 at 12:24 -0800, Sean Christopherson wrote:
> Your CI caught me just in time; I applied this locally last week, but haven't
> fully pushed it to kvm-x86 yet. :-)
The TDX CI tracks some upstream branches. Is there one in kvm_x86 tree that
would be useful? It's not foolproof enough to warrant sending out automated
mails. But we monitor it and might notice TDX specific issues. Ideally we would
not be chasing generic bugs in like scratch code not headed upstream or
something.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-13 0:41 ` Edgecombe, Rick P
@ 2025-11-13 16:29 ` Sean Christopherson
2025-11-13 18:49 ` Edgecombe, Rick P
0 siblings, 1 reply; 8+ messages in thread
From: Sean Christopherson @ 2025-11-13 16:29 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: bp@alien8.de, x86@kernel.org, thorsten.blum@linux.dev,
hpa@zytor.com, mingo@redhat.com, tglx@linutronix.de,
dave.hansen@linux.intel.com, pbonzini@redhat.com,
kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org, kas@kernel.org
On Thu, Nov 13, 2025, Rick P Edgecombe wrote:
> On Wed, 2025-11-12 at 12:24 -0800, Sean Christopherson wrote:
> > Your CI caught me just in time; I applied this locally last week, but haven't
> > fully pushed it to kvm-x86 yet. :-)
>
> The TDX CI tracks some upstream branches. Is there one in kvm_x86 tree that
> would be useful? It's not foolproof enough to warrant sending out automated
> mails. But we monitor it and might notice TDX specific issues. Ideally we would
> not be chasing generic bugs in like scratch code not headed upstream or
> something.
Assuming you're tracking linux-next, I wouldn't bother adding kvm-x86 as kvm-x86/next
is fed into linux-next. I do push to topic branches, e.g. kvm-x86/tdx, before
merging to kvm-x86/next, but at best you might "gain" a day or two, and the entire
reason I do "half" pushes is so that I can run everything through my testing
before "officially" publishing it to the world.
All in all, explicitly tracking anything kvm-x86 would likely be a net negative.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-13 16:29 ` Sean Christopherson
@ 2025-11-13 18:49 ` Edgecombe, Rick P
2025-11-13 18:55 ` Sean Christopherson
0 siblings, 1 reply; 8+ messages in thread
From: Edgecombe, Rick P @ 2025-11-13 18:49 UTC (permalink / raw)
To: seanjc@google.com
Cc: bp@alien8.de, x86@kernel.org, thorsten.blum@linux.dev,
hpa@zytor.com, mingo@redhat.com, tglx@linutronix.de,
dave.hansen@linux.intel.com, pbonzini@redhat.com,
kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org, kas@kernel.org
On Thu, 2025-11-13 at 08:29 -0800, Sean Christopherson wrote:
> Assuming you're tracking linux-next, I wouldn't bother adding kvm-x86 as kvm-x86/next
> is fed into linux-next. I do push to topic branches, e.g. kvm-x86/tdx, before
> merging to kvm-x86/next, but at best you might "gain" a day or two, and the entire
> reason I do "half" pushes is so that I can run everything through my testing
> before "officially" publishing it to the world.
>
> All in all, explicitly tracking anything kvm-x86 would likely be a net negative.
Yea, linux-next and Linus releases. Ok, we'll leave it. I was just thinking
about your lack of TDX testing setup, and wondering if it could help. All good.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities
2025-11-13 18:49 ` Edgecombe, Rick P
@ 2025-11-13 18:55 ` Sean Christopherson
0 siblings, 0 replies; 8+ messages in thread
From: Sean Christopherson @ 2025-11-13 18:55 UTC (permalink / raw)
To: Rick P Edgecombe
Cc: bp@alien8.de, x86@kernel.org, thorsten.blum@linux.dev,
hpa@zytor.com, mingo@redhat.com, tglx@linutronix.de,
dave.hansen@linux.intel.com, pbonzini@redhat.com,
kvm@vger.kernel.org, linux-coco@lists.linux.dev,
linux-kernel@vger.kernel.org, kas@kernel.org
On Thu, Nov 13, 2025, Rick P Edgecombe wrote:
> On Thu, 2025-11-13 at 08:29 -0800, Sean Christopherson wrote:
> > Assuming you're tracking linux-next, I wouldn't bother adding kvm-x86 as kvm-x86/next
> > is fed into linux-next. I do push to topic branches, e.g. kvm-x86/tdx, before
> > merging to kvm-x86/next, but at best you might "gain" a day or two, and the entire
> > reason I do "half" pushes is so that I can run everything through my testing
> > before "officially" publishing it to the world.
> >
> > All in all, explicitly tracking anything kvm-x86 would likely be a net negative.
>
> Yea, linux-next and Linus releases. Ok, we'll leave it. I was just thinking
> about your lack of TDX testing setup, and wondering if it could help. All good.
Heh, I appreciate the offer, but you probably shouldn't encourage my laziness at
this point :-)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-11-13 18:55 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-12 17:16 [PATCH RESEND] KVM: TDX: Use struct_size and simplify tdx_get_capabilities Thorsten Blum
2025-11-12 19:59 ` Edgecombe, Rick P
2025-11-12 20:24 ` Sean Christopherson
2025-11-13 0:41 ` Edgecombe, Rick P
2025-11-13 16:29 ` Sean Christopherson
2025-11-13 18:49 ` Edgecombe, Rick P
2025-11-13 18:55 ` Sean Christopherson
2025-11-12 21:22 ` Thorsten Blum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).