[PATCH v6 6/7] KVM: nSVM: Add helpers to temporarily switch to vmcb01

Nikunj A Dadhania
2026-04-07T06:33:41Z

If KVM needs to update controls that belong in vmcb01 while L2 is active,
it must switch to vmcb01 and update immediately instead of deferring until
the next nested VM-Exit. Deferring updates creates ordering and state
consistency problems, e.g. KVM thinks a control is enabled while vmcb01
still has stale state.

Add helpers to temporarily switch to vmcb01 to ensure updates happen
on-demand.

Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
---
arch/x86/kvm/svm/svm.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index e6477affac9a..32b002f92528 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -1251,6 +1251,30 @@ void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
svm->vmcb = target_vmcb->ptr;
}

+static void svm_load_vmcb01(struct kvm_vcpu *vcpu)
+{
+ struct vcpu_svm *svm = to_svm(vcpu);
+
+ if (!is_guest_mode(vcpu)) {
+ WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr);
+ return;
+ }
+
+ WARN_ON_ONCE(svm->vmcb != svm->nested.vmcb02.ptr);
+ svm_switch_vmcb(svm, &svm->vmcb01);
+}
+
+static void svm_put_vmcb01(struct kvm_vcpu *vcpu)
+{
+ if (!is_guest_mode(vcpu))
+ return;
+
+ svm_switch_vmcb(to_svm(vcpu), &to_svm(vcpu)->nested.vmcb02);
+}
+
+DEFINE_GUARD(svm_vmcb01, struct kvm_vcpu *,
+ svm_load_vmcb01(_T), svm_put_vmcb01(_T))
+
static int svm_vcpu_precreate(struct kvm *kvm)
{
return avic_alloc_physical_id_table(kvm);
--
2.48.1