x86/virt/tdx: Minor sparse fixups
Subsystems (4)
Dave Hansen
maintainerv2 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.
Dave Hansen
maintainerPATCH 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.
---
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
@@ -3054,7 +3054,8 @@ static int tdx_vcpu_get_cpuid_leaf(strucstatic 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;
_
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
__userpointer 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
__userin 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
Signed-off-by Dave Hansen <[email protected]>Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")Reviewed-by Rick Edgecombe <[email protected]>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
Re: [v2][PATCH 1/2] x86/virt/tdx: Remove __user annotation from kernel pointer
In reply to Dave HansenQuoted 16 lines · Dave Hansen
From: Dave Hansen <[email protected]>
Separate
__userpointer 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
__userin 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.
Signed-off-by Dave Hansen <[email protected]>Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")Reviewed-by Rick Edgecombe <[email protected]>
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-3arch/x86/kvm/vmx/tdx.carch/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(strucstatic 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;_
Sean Christopherson
maintainerRe: [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
__userpointer 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
__userin 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
Signed-off-by Dave Hansen <[email protected]>Fixes 488808e682e7 ("KVM: x86: Introduce KVM_TDX_GET_CPUID")Reviewed-by Rick Edgecombe <[email protected]>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.
Dave Hansen
maintainerPATCH v2 2/2 x86/virt/tdx: Fix sparse warnings from using 0 for NULL
In reply to Dave HansenFrom: 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.
---
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
@@ -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;
_
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 pointerfor 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
Signed-off-by Dave Hansen <[email protected]>Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")Reviewed-by Rick Edgecombe <[email protected]>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
Re: [v2][PATCH 2/2] x86/virt/tdx: Fix sparse warnings from using 0 for NULL
In reply to Dave HansenQuoted 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.
Signed-off-by Dave Hansen <[email protected]>Fixes a50f673f25e0 ("KVM: TDX: Do TDX specific vcpu initialization")Fixes 8d032b683c29 ("KVM: TDX: create/destroy VM structure")Reviewed-by Rick Edgecombe <[email protected]>
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-1arch/x86/kvm/vmx/tdx.carch/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;_
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