[PATCH v1 17/28] KVM: nSVM: Always switch VMCB before leaving guest mode
From: Yosry Ahmed
Date: Mon Jul 27 2026 - 20:42:03 EST
Move svm_switch_vmcb() calls ahead of leave_guest_mode(), as nothing
between the calls depends on svm->vmcb being set to vmcb01. This makes
the nested VM-Exit path consistent with the nested VM-Enter path, and
having the calls to svm_switch_vmcb() and leave_guest_mode() close makes
it easier to reason about incoming changes that depend on both VMCB and
guest_mode context.
No functional change intended.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/svm/nested.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index a6a49a5e0d90a..410d0f1aaa63c 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1325,7 +1325,7 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
if (nested_svm_vmexit_update_vmcb12(vcpu))
kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
- /* Exit Guest-Mode */
+ svm_switch_vmcb(svm, &svm->vmcb01);
leave_guest_mode(vcpu);
svm_pmu_handle_nested_transition(svm);
@@ -1352,8 +1352,6 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
if (guest_cpu_cap_has(vcpu, X86_FEATURE_ERAPS))
vmcb01->control.erap_ctl |= ERAP_CONTROL_CLEAR_RAP;
- svm_switch_vmcb(svm, &svm->vmcb01);
-
/*
* Rules for synchronizing int_ctl bits from vmcb02 to vmcb01:
*
@@ -1537,6 +1535,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
vcpu->arch.nested_run_pending = 0;
svm->nested.vmcb12_gpa = INVALID_GPA;
+ svm_switch_vmcb(svm, &svm->vmcb01);
leave_guest_mode(vcpu);
/*
@@ -1548,8 +1547,6 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
*/
__svm_pmu_handle_nested_transition(svm, true);
- svm_switch_vmcb(svm, &svm->vmcb01);
-
nested_svm_transition_tlb_flush(vcpu);
nested_svm_uninit_mmu_context(vcpu);
--
2.55.0.229.g6434b31f56-goog