Skip to contentSkip to conversation

x86/virt/tdx: Minor sparse fixups

PATCH Superseded Dave Hansen 4 participants 11 messages Oct 29, 2025 – Oct 30, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dave Hansen

author maintainer
#0 lore raw

cover x86/virt/tdx: Minor sparse fixups

Sean recently suggested relying on sparse to add type safety in TDX code, hoping that the robots would notice and complain. Well, that plan is not working out so great. TDX is not even sparse clean today and nobody seems to have noticed or cared.

I can see how folks might ignore the 0 vs. NULL complaints. But the misplaced __user is actually bad enough it should be fixed no matter what.

Might as well fix it all up.

Cc Xiaoyao Li <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Rick Edgecombe <[email protected]>
author maintainer
#1 lore raw

PATCH 1/2 x86/virt/tdx: Remove __user annotation from kernel pointer

From: Dave Hansen <[email protected]>

There are two 'kvm_cpuid2' pointers involved here. There's an "input"
side: 'td_cpuid' which is a normal kernel pointer and an 'output' side. The output here is userspace and there is an attempt at properly annotating the variable with __user:

	struct kvm_cpuid2 __user *output, *td_cpuid;

But, alas, this is wrong. The __user in the definition applies to both 'output' and 'td_cpuid'.

Fix it up by completely separating the two definitions so that it is obviously correct without even having to know what the C syntax rules even are.

Cc Xiaoyao Li <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Rick Edgecombe <[email protected]>

---

 b/arch/x86/kvm/vmx/tdx.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c renamed from arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-3 +2 -1
diff -puN arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-3 arch/x86/kvm/vmx/tdx.c
@@ -3054,7 +3054,8 @@ static int tdx_vcpu_get_cpuid_leaf(struc
 
 static int tdx_vcpu_get_cpuid(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *cmd)
 {
-	struct kvm_cpuid2 __user *output, *td_cpuid;
+	struct kvm_cpuid2 __user *output;
+	struct kvm_cpuid2 *td_cpuid;
 	int r = 0, i = 0, leaf;
 	u32 level;
 

_

#2 lore raw

Re: [PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer

For the KVM side of tdx, the commits are getting prefixed with "KVM: TDX: ", and "x86/virt/tdx" is being used arch/x86/virt/vmx/tdx/tdx.c. It's probably not too late to adopt the one true naming scheme. I don't have a strong preference except some consistency and that the maintainers agree :)

Quoted 1 line · Dave Hansen

From: Dave Hansen <[email protected]>

Pretty much the only difference between tip style logs and kvm/x86 style logs is to lead with a short "what is the change" blurb before launching into the background. Like:

KVM: TDX: Remove __user annotation from kernel pointer

Fix sparse warning in tdx_vcpu_get_cpuid().

There are two 'kvm_cpuid2' pointers involved here...

Quoted 6 lines

There are two 'kvm_cpuid2' pointers involved here. There's an "input"
side: 'td_cpuid' which is a normal kernel pointer and an 'output' side. The output here is userspace and there is an attempt at properly annotating the variable with __user:

Quoted 8 lines
	struct kvm_cpuid2 __user *output, *td_cpuid;

But, alas, this is wrong. The __user in the definition applies to both 'output' and 'td_cpuid'.

Fix it up by completely separating the two definitions so that it is obviously correct without even having to know what the C syntax rules even are.

If we want it:

Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")

TIL on the syntax association here.

#3 lore raw

Re: [PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer

Quoted 4 lines · Rick P

For the KVM side of tdx, the commits are getting prefixed with "KVM: TDX: ", and "x86/virt/tdx" is being used arch/x86/virt/vmx/tdx/tdx.c. It's probably not too late to adopt the one true naming scheme. I don't have a strong preference except some consistency and that the maintainers agree :)

Yeah, I just picked one. I honestly don't care what we do in the end. I was also probably just going to send these in the tip/x86/tdx branch unless anyone screams, so I did it the tip-ish way.

Quoted 10 lines
Quoted 10 lines

There are two 'kvm_cpuid2' pointers involved here. There's an "input"
side: 'td_cpuid' which is a normal kernel pointer and an 'output' side. The output here is userspace and there is an attempt at properly annotating the variable with __user:

	struct kvm_cpuid2 __user *output, *td_cpuid;

But, alas, this is wrong. The __user in the definition applies to both 'output' and 'td_cpuid'.

Quoted 6 lines
Quoted 3 lines

Fix it up by completely separating the two definitions so that it is obviously correct without even having to know what the C syntax rules even are.

If we want it:

Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")

Thanks for that. I might dump in in, but sometimes folks can get all hot and bothered about getting true fixes upstream fast. I was planning to go go the slow next-merge-window route with these.

#4 lore raw

Re: [PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer

Quoted 7 lines · Dave Hansen
Quoted 4 lines · Rick P

For the KVM side of tdx, the commits are getting prefixed with "KVM: TDX: ", and "x86/virt/tdx" is being used arch/x86/virt/vmx/tdx/tdx.c. It's probably not too late to adopt the one true naming scheme. I don't have a strong preference except some consistency and that the maintainers agree :)

Yeah, I just picked one. I honestly don't care what we do in the end.

I do. Being able to quickly determine if something touches KVM is valuable. TDX blurs the line since much of the code is split across KVM and x86/virt, but I still find value in differentiating when possible.

Quoted 2 lines

I was also probably just going to send these in the tip/x86/tdx branch unless anyone screams, so I did it the tip-ish way.

But this doesn't have anything to do with what tree the patches get routed through. Scopes are always about what files/content is changing.

I also don't undertand why you would take these through tip. They only touch KVM (which is annoying hard to see since there's no shortlog in the cover letter). Sure, they're minor changes and _probably_ won't conflict with anything, but again I don't see how that matters. These are clearly KVM patches.

#5 lore raw

Re: [PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer

On 10/29/25 16:00, Sean Christopherson wrote:
...

Quoted 4 lines

I also don't undertand why you would take these through tip. They only touch KVM (which is annoying hard to see since there's no shortlog in the cover letter). Sure, they're minor changes and _probably_ won't conflict with anything, but again I don't see how that matters. These are clearly KVM patches.

Hey, the more patches off my plate, the better.

I'm happy to make them more KVM-ish if you want and put the problem statements in backwards. ;)

#6 lore raw

Re: [PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer

Quoted 11 lines · Dave Hansen

On 10/29/25 16:00, Sean Christopherson wrote:
...

Quoted 4 lines

I also don't undertand why you would take these through tip. They only touch KVM (which is annoying hard to see since there's no shortlog in the cover letter). Sure, they're minor changes and _probably_ won't conflict with anything, but again I don't see how that matters. These are clearly KVM patches.

Hey, the more patches off my plate, the better.

I'm happy to make them more KVM-ish if you want and put the problem statements in backwards. ;)

LOL, I'm not _that_ particular. Feel free to send a v2, but I'm also a-ok doing fixups on the shortlogs when applying. I'll also add the Fixes: tags Rick suggested, as KVM x86 policy is to be liberal with Fixes, and only backport explicit stable@ patches.

author maintainer
#7 lore raw

PATCH 2/2 x86/virt/tdx: Fix sparse warnings from using 0 for NULL

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

sparse moans:

... arch/x86/kvm/vmx/tdx.c:859:38: warning: Using plain integer as NULL pointer

for several TDX pointer initializations. While I love a good ptr=0 now and then, it's good to have quiet sparse builds.

Fix the sites up.

Cc Xiaoyao Li <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Rick Edgecombe <[email protected]>

---

 b/arch/x86/kvm/vmx/tdx.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c renamed from arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-1 +3 -3
diff -puN arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-1 arch/x86/kvm/vmx/tdx.c
@@ -856,7 +856,7 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu
 	}
 	if (tdx->vp.tdvpr_page) {
 		tdx_reclaim_control_page(tdx->vp.tdvpr_page);
-		tdx->vp.tdvpr_page = 0;
+		tdx->vp.tdvpr_page = NULL;
 		tdx->vp.tdvpr_pa = 0;
 	}
 
@@ -2642,7 +2642,7 @@ free_tdcs:
 free_tdr:
 	if (tdr_page)
 		__free_page(tdr_page);
-	kvm_tdx->td.tdr_page = 0;
+	kvm_tdx->td.tdr_page = NULL;
 
 free_hkid:
 	tdx_hkid_free(kvm_tdx);
@@ -3016,7 +3016,7 @@ free_tdcx:
 free_tdvpr:
 	if (tdx->vp.tdvpr_page)
 		__free_page(tdx->vp.tdvpr_page);
-	tdx->vp.tdvpr_page = 0;
+	tdx->vp.tdvpr_page = NULL;
 	tdx->vp.tdvpr_pa = 0;
 
 	return ret;

_

#8 lore raw

Re: [PATCH 2/2] x86/virt/tdx: Fix sparse warnings from using 0 for NULL

Quoted 11 lines · Dave Hansen

From: Dave Hansen <[email protected]>

sparse moans:

... arch/x86/kvm/vmx/tdx.c:859:38: warning: Using plain integer as NULL pointer

for several TDX pointer initializations. While I love a good ptr=0 now and then, it's good to have quiet sparse builds.

Fix the sites up.

If we want it:

Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")
Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")
#9 lore raw
In reply to Dave Hansen
Quoted 10 lines · Dave Hansen

Sean recently suggested relying on sparse to add type safety in TDX code, hoping that the robots would notice and complain. Well, that plan is not working out so great. TDX is not even sparse clean today and nobody seems to have noticed or cared.

I can see how folks might ignore the 0 vs. NULL complaints. But the misplaced __user is actually bad enough it should be fixed no matter what.

Might as well fix it all up.

Oof. But my local distro version of sparse spit out a bunch of noise related to "warning: unreplaced symbol" in "include/asm-generic/bitops/generic-non- atomic.h". It also did not find these. But checking with the latest sparse build from source did, and this fixes them.