Skip to contentSkip to conversation

x86/virt/tdx: Minor sparse fixups

PATCH Applied Dave Hansen 4 participants 9 messages Nov 3, 2025 – Nov 18, 2025

Subsystems (4)
lore
Share & tools
lore

Dave Hansen

author maintainer
#0 lore raw

v2 cover x86/virt/tdx: Minor sparse fixups

Changs from v1:

* Add Fixes/Reviewed-by tags (Rick)
* Add some "backwards" KVM changelog blurbs

--

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 v2 1/2 x86/virt/tdx: Remove __user annotation from kernel pointer

From: Dave Hansen <[email protected]>

Separate __user pointer variable declaration from kernel one.

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'. Sparse notices the address space mismatch and will complain about it.

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.

Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")
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;
 

_

maintainer Acked-by on v2 1/2
#2 lore raw

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

Quoted 20 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Separate __user pointer variable declaration from kernel one.

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'. Sparse notices the address space mismatch and will complain about it.

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.

Quoted 15 lines
Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")
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]>
Signature · 1 line
 Kiryl Shutsemau / Kirill A. Shutemov
Reviewed-by on v2 1/2
#3 lore raw

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

In reply to Dave Hansen
Quoted 16 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Separate __user pointer variable declaration from kernel one.

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'. Sparse notices the address space mismatch and will complain about it.

Quoted 7 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.

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

the prefix of the shortlog is still "x86/virt/tdx". I think Sean will change it to "KVM: TDX:", if it gets routed through KVM tree.

Anyway,

Quoted 31 lines
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(-)

diff -puN arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-3 arch/x86/kvm/vmx/tdx.c

arch/x86/kvm/vmx/tdx.c renamed from arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-3 +2 -1
@@ -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;
 

_

#4 lore raw

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

Quoted 21 lines · Xiaoyao Li
Quoted 20 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Separate __user pointer variable declaration from kernel one.

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'. Sparse notices the address space mismatch and will complain about it.

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.

Quoted 6 lines
Quoted 3 lines
Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")

the prefix of the shortlog is still "x86/virt/tdx". I think Sean will change it to "KVM: TDX:", if it gets routed through KVM tree.

Ya, I'll fixup when applying.

author maintainer
#5 lore raw

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

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

Stop using 0 for NULL.

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.

Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")
Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")
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;

_

maintainer Acked-by on v2 2/2
#6 lore raw

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

Quoted 12 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Stop using 0 for NULL.

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.

Quoted 16 lines
Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")
Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")
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]>
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
Reviewed-by on v2 2/2
#7 lore raw

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

In reply to Dave Hansen
Quoted 9 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Stop using 0 for NULL.

sparse moans:

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

Quoted 7 lines

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

Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")
Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")
Quoted 48 lines
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(-)

diff -puN arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-1 arch/x86/kvm/vmx/tdx.c

arch/x86/kvm/vmx/tdx.c renamed from arch/x86/kvm/vmx/tdx.c~tdx-sparse-fix-1 +3 -3
@@ -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;

_

maintainer Applied
#8 lore raw
In reply to Dave Hansen
Quoted 8 lines · Dave Hansen

Changs from v1:

* Add Fixes/Reviewed-by tags (Rick)
* Add some "backwards" KVM changelog blurbs

--

[...]

Applied to kvm-x86 tdx, with the scopes changed to "KVM: TDX:". Thanks!

[1/2] KVM: TDX: Remove __user annotation from kernel pointer

      https://github.com/kvm-x86/linux/commit/228add34dc2f
[2/2] KVM: TDX: Fix sparse warnings from using 0 for NULL
      https://github.com/kvm-x86/linux/commit/27376465e945

--
https://github.com/kvm-x86/linux/tree/next