From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
x86@kernel.org, Sean Christopherson <seanjc@google.com>,
Maxim Levitsky <mlevitsk@redhat.com>
Subject: [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts
Date: Mon, 23 Nov 2026 12:55:12 -0500 [thread overview]
Message-ID: <20261123175512.80438-6-mlevitsk@redhat.com> (raw)
In-Reply-To: <20261123175512.80438-1-mlevitsk@redhat.com>
If a nested posted interrupt races with a L0 vm exit on a target vCPU,
that is reflected to L1, then this nestedf posted interrupt is currently
neither processed nor delivered to L1 as a regular interrupt.
Since it is not possible to know if APICv hardware has processed
the interrupt, opt to always process it a moment before nested VM exit.
(call vmx_complete_nested_posted_interrupt)
To ensure that vmx_deliver_nested_posted_interrupt and
vmx_complete_nested_posted_interrupt don't race do the following:
Have a cmpxchg based loop in vmx_deliver_nested_posted_interrupt that
atomically checks that target vCPU is in nested mode and has suitable
posted interrupt configuration, and then set KVM_NESTED_PI_PENDING.
If the above cmpxchg loop succeeds, this means that even if the target vCPU
is in process of exiting the nested guest, it will notice this
KVM_NESTED_PI_PENDING flag when it reaches its
vmx_complete_nested_posted_interrupt, and then it will process it.
If it fails, it means that the target vCPU is no longer in the guest
mode, and thus it is safe to deliver the interrupt as a normal interrupt.
Apply a similar logic on the receiving side:
Use xchg to snapshot the old value of guest state in leave_guest_mode,
and by using this snapshot, determine if we need to process
the posted interrupts.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/kvm/regs.h | 5 ++--
arch/x86/kvm/vmx/nested.c | 30 ++++++++++++++------
arch/x86/kvm/vmx/vmx.c | 60 +++++++++++++++++++--------------------
3 files changed, 54 insertions(+), 41 deletions(-)
diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
index 58950f7d4713..f8b66d85705b 100644
--- a/arch/x86/kvm/regs.h
+++ b/arch/x86/kvm/regs.h
@@ -461,9 +461,9 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
vcpu->stat.guest_mode = 1;
}
-static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
+static inline int leave_guest_mode(struct kvm_vcpu *vcpu)
{
- atomic_set(&vcpu->arch.nested_guest_state, 0);
+ int old_value = atomic_xchg(&vcpu->arch.nested_guest_state, 0);
if (vcpu->arch.load_eoi_exitmap_pending) {
vcpu->arch.load_eoi_exitmap_pending = false;
@@ -471,6 +471,7 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
}
vcpu->stat.guest_mode = 0;
+ return old_value;
}
static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 8d242d46a245..9c2a411bb2bc 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -4025,23 +4025,16 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
}
}
-static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
+static int __vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
int max_irr;
void *vapic_page;
u16 status;
- int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
-
- if (!(nested_state & KVM_NESTED_PI_PENDING))
- return 0;
-
if (!vmx->nested.pi_desc)
goto mmio_needed;
- atomic_andnot(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
-
/*
* Don't test the value of PID.ON.
* It is valid to trigger a posted interrupt without setting this bit
@@ -4078,6 +4071,18 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
return -ENXIO;
}
+static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
+{
+ int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
+
+ if (!(nested_state & KVM_NESTED_PI_PENDING))
+ return 0;
+
+ atomic_andnot(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
+
+ return __vmx_complete_nested_posted_interrupt(vcpu);
+}
+
static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu)
{
struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
@@ -5090,6 +5095,7 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
+ int old_nested_state;
/* Pending MTF traps are discarded on VM-Exit. */
vmx->nested.mtf_pending = false;
@@ -5121,7 +5127,13 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
if (enable_ept && is_pae_paging(vcpu))
vmx_ept_load_pdptrs(vcpu);
- leave_guest_mode(vcpu);
+ old_nested_state = leave_guest_mode(vcpu);
+
+ /* pairs with implicit memory barrier in vmx_deliver_nested_posted_interrupt */
+ smp_mb__after_atomic();
+
+ if (old_nested_state & KVM_NESTED_PI_PENDING)
+ __vmx_complete_nested_posted_interrupt(vcpu);
if (nested_cpu_has_preemption_timer(vmcs12))
hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index e6dd37a3ac44..4a5adb1507ce 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4370,41 +4370,41 @@ void vmx_recalc_intercepts(struct kvm_vcpu *vcpu)
static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
int vector)
{
- int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
+ int old = atomic_read(&vcpu->arch.nested_guest_state);
+
+ while (true) {
+ if ((old & (KVM_NESTED_PI_ACTIVE | KVM_NESTED_PI_VECTOR_MASK)) !=
+ (KVM_NESTED_PI_ACTIVE | vector))
+ return -1;
+
+ WARN_ON_ONCE(!(old & KVM_NESTED_IN_GUEST));
+
+ if (atomic_try_cmpxchg(&vcpu->arch.nested_guest_state,
+ &old, old | KVM_NESTED_PI_PENDING))
+ break;
+ }
/*
- * DO NOT query the vCPU's vmcs12, as vmcs12 is dynamically allocated
- * and freed, and must not be accessed outside of vcpu->mutex. The
- * vCPU's cached PI NV is valid if and only if posted interrupts
- * enabled in its vmcs12, i.e. checking the vector also checks that
- * L1 has enabled posted interrupts for L2.
+ * If a posted intr is not recognized by hardware,
+ * we will accomplish it in the next vmentry.
*/
- if (is_guest_mode(vcpu) &&
- vector == (nested_state & KVM_NESTED_PI_VECTOR_MASK)) {
- /*
- * If a posted intr is not recognized by hardware,
- * we will accomplish it in the next vmentry.
- */
- atomic_or(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
- kvm_make_request(KVM_REQ_EVENT, vcpu);
+ kvm_make_request(KVM_REQ_EVENT, vcpu);
- /*
- * This pairs with the smp_mb_*() after setting vcpu->mode in
- * vcpu_enter_guest() to guarantee the vCPU sees the event
- * request if triggering a posted interrupt "fails" because
- * vcpu->mode != IN_GUEST_MODE. The extra barrier is needed as
- * the smb_wmb() in kvm_make_request() only ensures everything
- * done before making the request is visible when the request
- * is visible, it doesn't ensure ordering between the store to
- * vcpu->requests and the load from vcpu->mode.
- */
- smp_mb__after_atomic();
+ /*
+ * This pairs with the smp_mb_*() after setting vcpu->mode in
+ * vcpu_enter_guest() to guarantee the vCPU sees the event
+ * request if triggering a posted interrupt "fails" because
+ * vcpu->mode != IN_GUEST_MODE. The extra barrier is needed as
+ * the smb_wmb() in kvm_make_request() only ensures everything
+ * done before making the request is visible when the request
+ * is visible, it doesn't ensure ordering between the store to
+ * vcpu->requests and the load from vcpu->mode.
+ */
+ smp_mb__after_atomic();
- /* the PIR and ON have been set by L1. */
- kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_NESTED_VECTOR);
- return 0;
- }
- return -1;
+ /* the PIR and ON have been set by L1. */
+ kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_NESTED_VECTOR);
+ return 0;
}
/*
* Send interrupt to vcpu via posted interrupt way.
--
2.56.1
prev parent reply other threads:[~2026-11-23 17:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 17:55 [PATCH v3 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 1/5] KVM: nVMX: don't check PIR.ON when processing nested posted interrupts Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 3/5] KVM: x86: convert nested_guest_state into an atomic variable Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 4/5] KVM: x86: nVMX: move nested APICv state to nested_guest_state Maxim Levitsky
2026-11-23 17:55 ` Maxim Levitsky [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123175512.80438-6-mlevitsk@redhat.com \
--to=mlevitsk@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®