Re: [PATCH v5 09/26] KVM: nSVM: Call enter_guest_mode() before switching to VMCB02
From: Sean Christopherson
Date: Fri Feb 20 2026 - 20:13:06 EST
On Fri, Feb 06, 2026, Yosry Ahmed wrote:
> In preparation for moving more changes that rely on is_guest_mode()
> before switching to VMCB02, move entering guest mode a bit earlier.
>
> Nothing between the new callsite(s) and the old ones rely on
> is_guest_mode(), so this should be safe.
>
> No functional change intended.
>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Yosry Ahmed <yosry.ahmed@xxxxxxxxx>
> ---
> arch/x86/kvm/svm/nested.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> index 29069fc5e8cb..607d99172e2b 100644
> --- a/arch/x86/kvm/svm/nested.c
> +++ b/arch/x86/kvm/svm/nested.c
> @@ -741,9 +741,6 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
>
> nested_svm_transition_tlb_flush(vcpu);
>
> - /* Enter Guest-Mode */
> - enter_guest_mode(vcpu);
> -
> /*
> * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,
> * exit_int_info_err, next_rip, insn_len, insn_bytes.
> @@ -944,6 +941,8 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,
>
> WARN_ON(svm->vmcb == svm->nested.vmcb02.ptr);
>
> + enter_guest_mode(vcpu);
> +
> nested_svm_copy_common_state(svm->vmcb01.ptr, svm->nested.vmcb02.ptr);
>
> svm_switch_vmcb(svm, &svm->nested.vmcb02);
> @@ -1890,6 +1889,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
> svm_copy_vmrun_state(&svm->vmcb01.ptr->save, save);
> nested_copy_vmcb_control_to_cache(svm, ctl);
>
> + enter_guest_mode(vcpu);
> svm_switch_vmcb(svm, &svm->nested.vmcb02);
> nested_vmcb02_prepare_control(svm, svm->vmcb->save.rip, svm->vmcb->save.cs.base);
LOL, guess what! Today end's in 'y', which means there's a nSVM bug! It's a
super minor one though, especially in the broader context, I just happened to
see it when looking at this patch.
As per 3f6821aa147b ("KVM: x86: Forcibly leave nested if RSM to L2 hits shutdown"),
shutdown on RSM is suppose to hit L1, not L2. But if enter_svm_guest_mode() fails,
svm_leave_smm() bails without leaving guest code. Syzkaller probably hasn't found
the bug because nested_run_pending doesn't get set, but it's still technically
wrong.
Of course, as the comment in emulator_leave_smm() says, the *entire* RSM flow is
wrong, because it's not a VM-Enter/VMRUN, it's somethign else entirely.
Anyways, I don't think there's anything to do in this series, but at some point
we should probably do:
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index a2452b8ec49d..5cc9ad9b750d 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4877,13 +4877,15 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)
vmcb12 = map.hva;
nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
+
ret = enter_svm_guest_mode(vcpu, smram64->svm_guest_vmcb_gpa, vmcb12, false);
-
if (ret)
- goto unmap_save;
+ goto leave_nested;
svm->nested.nested_run_pending = 1;
+leave_nested:
+ svm_leave_nested(vcpu);
unmap_save:
kvm_vcpu_unmap(vcpu, &map_save);
unmap_map: