Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 1/2] KVM: SVM: Don't clobber RFLAGS.RF loaded by a single-stepped IRET
@ 2026-11-25  6:29 Peng Hao
  2026-11-25  6:30 ` [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF Peng Hao
  0 siblings, 1 reply; 3+ messages in thread
From: Peng Hao @ 2026-11-25  6:29 UTC (permalink / raw)
  To: pbonzini, seanjc; +Cc: kvm

Without vNMI, KVM opens the NMI window after an NMI handler by
intercepting IRET and then single-stepping it with TF and RF set.  When
the single-step #DB arrives, disable_nmi_singlestep() clears TF and RF
again unless they were set in the RFLAGS snapshot taken when the
single-step was armed, i.e. before the IRET ran.

That is wrong for RF.  By the time the single-step trap is taken, the
stepped instruction has completed and the CPU has already cleared the RF
that KVM set, so RF in save.rflags is guest state.  It is only set if
the instruction loaded it, which is what the stepped IRET does when the
NMI interrupted a context that was about to resume from an instruction
breakpoint: the OS sets RF in the saved RFLAGS so the breakpoint is not
hit again, the NMI pushes that RFLAGS and the NMI handler's IRET pops
it.  KVM then clears RF based on the stale snapshot, and the guest takes
a second #DB on the same instruction.

In a Linux guest this shows up as a duplicate perf hardware breakpoint
event when a PMI happens to land right after the breakpoint has been
handled, e.g. while sampling with perf at a high rate.

If the single-step trap fired, fold the post-instruction RF into the
snapshot so that disable_nmi_singlestep() leaves it alone.  RF is still
cleared on the event injection path in svm_vcpu_run(), where the
instruction never ran and RF still holds KVM's value.

TF has the same problem in principle when the stepped IRET pops TF=1,
but there KVM cannot tell its own TF apart from the guest's after the
fact, so it is left as is.

Fixes: 44c11430b52c ("KVM: inject NMI after IRET from a previous NMI, not before.")
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 arch/x86/kvm/svm/svm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 581bc3a23c95..118c2ce4742d 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2087,6 +2087,18 @@ static int db_interception(struct kvm_vcpu *vcpu)
 	}
 
 	if (svm->nmi_singlestep) {
+		/*
+		 * If the single-step trap fired, the stepped instruction has
+		 * completed and RF now holds the guest's value: the CPU clears
+		 * RF when an instruction completes, unless the instruction
+		 * loaded RFLAGS, e.g. an IRET returning to an instruction
+		 * breakpoint that the guest has already handled.  Don't let
+		 * disable_nmi_singlestep() clear the guest's RF.
+		 */
+		if (svm->vmcb->save.dr6 & DR_STEP)
+			svm->nmi_singlestep_guest_rflags |=
+				svm->vmcb->save.rflags & X86_EFLAGS_RF;
+
 		disable_nmi_singlestep(svm);
 		/* Make sure we check for pending NMIs upon entry */
 		kvm_make_request(KVM_REQ_EVENT, vcpu);
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  6:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  6:29 [PATCH 1/2] KVM: SVM: Don't clobber RFLAGS.RF loaded by a single-stepped IRET Peng Hao
2026-11-25  6:30 ` [PATCH 2/2] KVM: selftests: Test that an NMI-window single-step preserves the guest's RF Peng Hao
2026-11-25  6:39   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox