[PATCH v1 15/28] KVM: nSVM: Drop svm->nested.initialized
From: Yosry Ahmed
Date: Mon Jul 27 2026 - 20:40:43 EST
Do NULL checks on svm->nested.vmcb02.ptr instead of using a dedicated
proxy 'initialized' field. Make sure svm->nested.vmcb02.ptr is
correctly set to NULL on initialization failures, as currently it is
currently left as a dangling pointer.
This makes it possible to directly do a NULL check on
svm->nested.vmcb02.ptr before using it, instead of checking a different
field, which is less error-prone.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/svm/nested.c | 13 +++++--------
arch/x86/kvm/svm/svm.h | 2 --
2 files changed, 5 insertions(+), 10 deletions(-)
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index eb60d6b959d86..a8bf847209618 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1134,7 +1134,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)
return ret;
}
- if (WARN_ON_ONCE(!svm->nested.initialized))
+ if (WARN_ON_ONCE(!svm->nested.vmcb02.ptr))
return -EINVAL;
vmcb12_gpa = kvm_rax_read(vcpu);
@@ -1484,22 +1484,21 @@ int svm_allocate_nested(struct vcpu_svm *svm)
{
struct page *vmcb02_page;
- if (svm->nested.initialized)
+ if (svm->nested.vmcb02.ptr)
return 0;
vmcb02_page = snp_safe_alloc_page();
if (!vmcb02_page)
return -ENOMEM;
- svm->nested.vmcb02.ptr = page_address(vmcb02_page);
- svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
svm->nested.msrpm = svm_vcpu_alloc_msrpm();
if (!svm->nested.msrpm)
goto err_free_vmcb02;
+ svm->nested.vmcb02.ptr = page_address(vmcb02_page);
+ svm->nested.vmcb02.pa = __sme_set(page_to_pfn(vmcb02_page) << PAGE_SHIFT);
svm->nested.asid02 = svm->asid;
- svm->nested.initialized = true;
return 0;
err_free_vmcb02:
@@ -1509,7 +1508,7 @@ int svm_allocate_nested(struct vcpu_svm *svm)
void svm_free_nested(struct vcpu_svm *svm)
{
- if (!svm->nested.initialized)
+ if (!svm->nested.vmcb02.ptr)
return;
if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr))
@@ -1529,8 +1528,6 @@ void svm_free_nested(struct vcpu_svm *svm)
* When the vmcb02 is freed, this optimization becomes invalid.
*/
svm->nested.last_vmcb12_gpa = INVALID_GPA;
-
- svm->nested.initialized = false;
}
void svm_leave_nested(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 196fb39ab0ae9..2e0c9c469ec63 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -234,8 +234,6 @@ struct svm_nested_state {
kvm_tlb_tag_t asid02;
- bool initialized;
-
/*
* Indicates whether MSR bitmap for L2 needs to be rebuilt due to
* changes in MSR bitmap for L1 or switching to a different L2. Note,
--
2.55.0.229.g6434b31f56-goog