Re: [RFC PATCH 1/5] KVM: nSVM: Do not use L2's RIP for vmcb02's NextRIP after first L2 VMRUN

From: Yosry Ahmed

Date: Wed Feb 18 2026 - 18:38:50 EST


On Wed, Feb 18, 2026 at 03:22:44PM -0800, Sean Christopherson wrote:
> On Thu, Feb 12, 2026, Yosry Ahmed wrote:
> > For guests with NRIPS disabled, L1 does not provide NextRIP when running
> > an L2 with an injected soft interrupt, instead it advances L2's RIP
> > before running it. KVM uses L2's RIP as the NextRIP in vmcb02 to emulate
>
> Should "L2's RIP" be "vmcb12's RIP"? The "L2's RIP" terminology gets really
> confusing in the next paragraph, as NextRIP _is_ L2's (Next)RIP. Hmm, or maybe
> "current RIP"? I.e. "current RIP" vs. "NextRIP"?

I intentionally avoided mentioning vmcb12, because after save/restore
the RIP value we pass into nested_vmcb02_prepare_control() is no longer
what's in vmcb12. I can go with "current RIP".

>
> > a CPU without NRIPS.
> >
> > However, after L2 runs the first time, NextRIP will be updated by the
> > CPU and/or KVM, and L2's RIP is no longer the correct value to use in
> > vmcb02. Hence, after save/restore, do not use L2's RIP if a nested run
> > is not pending (i.e. L2 has run at least once), use the NextRIP value.
>
> Too many negatives in this last sentence, it can just be (I think):
>
> Hence, after save/restore, use the current RIP if and only if a nested
> run is pending, otherwise use NextRIP.

Looks good.

>
> > Fixes: cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")
> > CC: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Yosry Ahmed <yosry.ahmed@xxxxxxxxx>
> > ---
> > arch/x86/kvm/svm/nested.c | 16 ++++++++++------
> > 1 file changed, 10 insertions(+), 6 deletions(-)
> >
> > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> > index de90b104a0dd..eebbe00714e3 100644
> > --- a/arch/x86/kvm/svm/nested.c
> > +++ b/arch/x86/kvm/svm/nested.c
> > @@ -844,14 +844,18 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
> > vmcb02->control.event_inj_err = svm->nested.ctl.event_inj_err;
> >
> > /*
> > - * next_rip is consumed on VMRUN as the return address pushed on the
> > + * NextRIP is consumed on VMRUN as the return address pushed on the
> > * stack for injected soft exceptions/interrupts. If nrips is exposed
> > - * to L1, take it verbatim from vmcb12. If nrips is supported in
> > - * hardware but not exposed to L1, stuff the actual L2 RIP to emulate
> > - * what a nrips=0 CPU would do (L1 is responsible for advancing RIP
> > - * prior to injecting the event).
> > + * to L1, take it verbatim from vmcb12.
> > + *
> > + * If nrips is supported in hardware but not exposed to L1, stuff the
> > + * actual L2 RIP to emulate what a nrips=0 CPU would do (L1 is
> > + * responsible for advancing RIP prior to injecting the event). This is
> > + * only the case for the first L2 run after VMRUN. After that (e.g.
> > + * during save/restore), NextRIP is updated by the CPU and/or KVM, and
> > + * the value of the L2 RIP from vmcb12 should not be used.
> > */
> > - if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS))
> > + if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS) || !svm->nested.nested_run_pending)
>
> This is technically wrong since KVM doesn't require NRIPS. Maybe this?

I hallucinated that making nested depend on nrips was merge, i.e. this
patch: https://lore.kernel.org/kvm/f0302382cf45d7a9527b4aebbfe694bbcfa7aff5.1651440202.git.maciej.szmigiero@xxxxxxxxxx/.

>
> if (boot_cpu_has(X86_FEATURE_NRIPS)) {

I wonder if that's necessary, but I cannot find anything in the APM
about whether the CPU ignores NextRIP if it's not supported. It doesn't
even mention how to use it when injecting soft IRQs, only that it is
needed to properly inject then. Sigh.

> if (guest_cpu_cap_has(vcpu, X86_FEATURE_NRIPS) ||
> !svm->nested.nested_run_pending)
> vmcb02->control.next_rip = svm->nested.ctl.next_rip;
> else
> vmcb02->control.next_rip = vmcb12_rip;
> }
>
>
> > vmcb02->control.next_rip = svm->nested.ctl.next_rip;
> > else if (boot_cpu_has(X86_FEATURE_NRIPS))
> > vmcb02->control.next_rip = vmcb12_rip;
> > --
> > 2.53.0.273.g2a3d683680-goog
> >