* [PATCH v3 0/5] KVM: nVMX: fix nested APICv emulation for windows guests
@ 2026-11-23 17:55 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
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
Fix two problems that were recently discovered that cause nested APICv
to malfunction on windows with hyperv enabled.
The first problem is that unlike KVM, windows doesn't set PIR.ON when
sending posted interrupts. X86 spec actually allows this.
The second problem is that unlilke KVM, which mostly ignores them,
windows depends on accurate delivery of the posted notification
interrupts, and currently it is possible for a posted notification
to get lost it it arrives at the same time as the nested VM exit.
V2: to avoid spurious notification in L1, process the posted
notification in L2, just before VM exit.
V3: rework the patch series following the pseudocode that Paolo suggested
(Thanks!!)
Best regards,
Maxim Levitsky
Maxim Levitsky (5):
KVM: nVMX: don't check PIR.ON when processing nested posted interrupts
KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode
KVM: x86: convert nested_guest_state into an atomic variable
KVM: x86: nVMX: move nested APICv state to nested_guest_state
KVM: x86: nVMX: fix races in delivery of nested posted interrupts
arch/x86/include/asm/kvm_host.h | 22 +++++++----
arch/x86/kvm/regs.h | 9 +++--
arch/x86/kvm/smm.c | 8 ++--
arch/x86/kvm/smm.h | 2 +-
arch/x86/kvm/vmx/nested.c | 67 ++++++++++++++++++++++-----------
arch/x86/kvm/vmx/vmx.c | 61 +++++++++++++++---------------
arch/x86/kvm/vmx/vmx.h | 2 -
arch/x86/kvm/x86.c | 11 +++---
8 files changed, 107 insertions(+), 75 deletions(-)
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 1/5] KVM: nVMX: don't check PIR.ON when processing nested posted interrupts
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 ` 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
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
Despite the suggestion to test and set the PIR.ON and avoid sending
a posted interrupt if it is already set, there is no requirement
for this bit to be set for posted interrupt processing to happen
when the target CPU receives the posted notification vector.
See section 30.6 POSTED-INTERRUPT PROCESSING:
"The processor clears the outstanding-notification bit in
the posted-interrupt descriptor. This is done atomically so as to leave
the remainder of the descriptor unmodified
(e.g., with a locked AND operation)."
Apparently Windows' implementation of APICv does exactly this:
it sets bits in PIR, without bothering to also set the PIR.ON.
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/kvm/vmx/nested.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 151873407abd..e336dfb8d6b9 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -4041,8 +4041,16 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
vmx->nested.pi_pending = false;
- if (!pi_test_and_clear_on(vmx->nested.pi_desc))
- return 0;
+ /*
+ * Don't test the value of PID.ON.
+ * It is valid to trigger a posted interrupt without setting this bit
+ */
+ pi_clear_on(vmx->nested.pi_desc);
+
+ /* Ensure that guests sees the change to PIR.ON before KVM harvest
+ * the PIR
+ */
+ smp_mb__after_atomic();
max_irr = pi_find_highest_vector(vmx->nested.pi_desc);
if (max_irr > 0) {
@@ -4209,8 +4217,7 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection)
if ((max_irr & 0xf0) > (vppr & 0xf0))
return true;
- if (vmx->nested.pi_pending && vmx->nested.pi_desc &&
- pi_test_on(vmx->nested.pi_desc)) {
+ if (vmx->nested.pi_pending && vmx->nested.pi_desc) {
max_irr = pi_find_highest_vector(vmx->nested.pi_desc);
if (max_irr > 0 && (max_irr & 0xf0) > (vppr & 0xf0))
return true;
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode
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 ` Maxim Levitsky
2026-11-24 2:59 ` kernel test robot
2026-11-23 17:55 ` [PATCH v3 3/5] KVM: x86: convert nested_guest_state into an atomic variable Maxim Levitsky
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
Split vcpu->arch.hflags into nested_guest_mode and smm_mode to prepare
for converting nested_guest_mode into an atomic variable that will contain
nested guest state that is exposed locklessly for other vCPUs.
No functional change expected.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/include/asm/kvm_host.h | 18 +++++++++++-------
arch/x86/kvm/regs.h | 6 +++---
arch/x86/kvm/smm.c | 8 ++++----
arch/x86/kvm/smm.h | 2 +-
arch/x86/kvm/x86.c | 11 ++++++-----
5 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ab30f418e526..d891dc678e9b 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -738,7 +738,6 @@ struct kvm_vcpu_arch {
unsigned long cr8;
u32 host_pkru;
u32 pkru;
- u32 hflags;
u64 efer;
u64 host_debugctl;
u64 apic_base;
@@ -758,6 +757,16 @@ struct kvm_vcpu_arch {
u64 arch_capabilities;
u64 perf_capabilities;
+#define KVM_NESTED_IN_GUEST BIT(8)
+ /* Non zero when the guest is executing another guest */
+ u16 nested_guest_state;
+
+#ifdef CONFIG_KVM_SMM
+#define SMM_MASK BIT(0)
+#define SMM_INSIDE_NMI_MASK BIT(1)
+ /* Non zero when the guest is in SMM*/
+ u16 smm_mode;
+#endif
/*
* Paging state of the vcpu
*
@@ -1875,16 +1884,11 @@ static inline unsigned long read_msr(unsigned long msr)
}
#endif
-#define HF_GUEST_MASK (1 << 0) /* VCPU is in guest-mode */
-
#ifdef CONFIG_KVM_SMM
-#define HF_SMM_MASK (1 << 1)
-#define HF_SMM_INSIDE_NMI_MASK (1 << 2)
-
# define KVM_MAX_NR_ADDRESS_SPACES 2
/* SMM is currently unsupported for guests with private memory. */
# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2)
-# define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
+# define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.smm_mode & SMM_MASK ? 1 : 0)
# define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm)
#else
# define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0)
diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
index 447f0ec3e63e..6114a099d177 100644
--- a/arch/x86/kvm/regs.h
+++ b/arch/x86/kvm/regs.h
@@ -457,13 +457,13 @@ static inline unsigned long kvm_get_effective_dr7(struct kvm_vcpu *vcpu)
static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.hflags |= HF_GUEST_MASK;
+ vcpu->arch.nested_guest_state = KVM_NESTED_IN_GUEST;
vcpu->stat.guest_mode = 1;
}
static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.hflags &= ~HF_GUEST_MASK;
+ vcpu->arch.nested_guest_state = 0;
if (vcpu->arch.load_eoi_exitmap_pending) {
vcpu->arch.load_eoi_exitmap_pending = false;
@@ -475,7 +475,7 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.hflags & HF_GUEST_MASK;
+ return vcpu->arch.nested_guest_state != 0;
}
static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c
index 656a38dad7e7..60af64a6209f 100644
--- a/arch/x86/kvm/smm.c
+++ b/arch/x86/kvm/smm.c
@@ -114,9 +114,9 @@ void kvm_smm_changed(struct kvm_vcpu *vcpu, bool entering_smm)
trace_kvm_smm_transition(vcpu->vcpu_id, vcpu->arch.smbase, entering_smm);
if (entering_smm) {
- vcpu->arch.hflags |= HF_SMM_MASK;
+ vcpu->arch.smm_mode |= SMM_MASK;
} else {
- vcpu->arch.hflags &= ~(HF_SMM_MASK | HF_SMM_INSIDE_NMI_MASK);
+ vcpu->arch.smm_mode &= ~(SMM_MASK | SMM_INSIDE_NMI_MASK);
/* Process a latched INIT or SMI, if any. */
kvm_make_request(KVM_REQ_EVENT, vcpu);
@@ -311,7 +311,7 @@ void enter_smm(struct kvm_vcpu *vcpu)
goto error;
if (kvm_x86_call(get_nmi_mask)(vcpu))
- vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode |= SMM_INSIDE_NMI_MASK;
else
kvm_x86_call(set_nmi_mask)(vcpu, true);
@@ -584,7 +584,7 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)
if (ret < 0)
return X86EMUL_UNHANDLEABLE;
- if ((vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK) == 0)
+ if ((vcpu->arch.smm_mode & SMM_INSIDE_NMI_MASK) == 0)
kvm_x86_call(set_nmi_mask)(vcpu, false);
kvm_smm_changed(vcpu, false);
diff --git a/arch/x86/kvm/smm.h b/arch/x86/kvm/smm.h
index db3c88f16138..323c03827d28 100644
--- a/arch/x86/kvm/smm.h
+++ b/arch/x86/kvm/smm.h
@@ -151,7 +151,7 @@ static inline int kvm_inject_smi(struct kvm_vcpu *vcpu)
static inline bool is_smm(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.hflags & HF_SMM_MASK;
+ return vcpu->arch.smm_mode & SMM_MASK;
}
void kvm_smm_changed(struct kvm_vcpu *vcpu, bool in_smm);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 79468ddfe473..821411291f48 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3013,7 +3013,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
events->smi.smm = is_smm(vcpu);
events->smi.pending = vcpu->arch.smi_pending;
events->smi.smm_inside_nmi =
- !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
+ !!(vcpu->arch.smm_mode & SMM_INSIDE_NMI_MASK);
#endif
events->smi.latched_init = kvm_lapic_latched_init(vcpu);
@@ -3099,7 +3099,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
#ifdef CONFIG_KVM_SMM
- if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
+ if (!!(vcpu->arch.smm_mode & SMM_MASK) != events->smi.smm) {
kvm_leave_nested(vcpu);
kvm_smm_changed(vcpu, events->smi.smm);
}
@@ -3108,9 +3108,9 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
if (events->smi.smm) {
if (events->smi.smm_inside_nmi)
- vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode |= SMM_INSIDE_NMI_MASK;
else
- vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
+ vcpu->arch.smm_mode &= ~SMM_INSIDE_NMI_MASK;
}
#else
@@ -9554,7 +9554,8 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
kvm_lapic_reset(vcpu, init_event);
WARN_ON_ONCE(is_guest_mode(vcpu) || is_smm(vcpu));
- vcpu->arch.hflags = 0;
+ vcpu->arch.smm_mode = 0;
+ vcpu->arch.nested_guest_state = 0;
vcpu->arch.smi_pending = 0;
vcpu->arch.smi_count = 0;
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 3/5] KVM: x86: convert nested_guest_state into an atomic variable
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 ` 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 ` [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky
4 siblings, 0 replies; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
Convert nested_guest_state into an atomic variable, to prepare
to fix nested APICv delivery races.
No functional change expected.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/include/asm/kvm_host.h | 2 +-
arch/x86/kvm/regs.h | 6 +++---
arch/x86/kvm/x86.c | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index d891dc678e9b..562042d5589b 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -759,7 +759,7 @@ struct kvm_vcpu_arch {
#define KVM_NESTED_IN_GUEST BIT(8)
/* Non zero when the guest is executing another guest */
- u16 nested_guest_state;
+ atomic_t nested_guest_state;
#ifdef CONFIG_KVM_SMM
#define SMM_MASK BIT(0)
diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
index 6114a099d177..58950f7d4713 100644
--- a/arch/x86/kvm/regs.h
+++ b/arch/x86/kvm/regs.h
@@ -457,13 +457,13 @@ static inline unsigned long kvm_get_effective_dr7(struct kvm_vcpu *vcpu)
static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.nested_guest_state = KVM_NESTED_IN_GUEST;
+ atomic_set(&vcpu->arch.nested_guest_state, KVM_NESTED_IN_GUEST);
vcpu->stat.guest_mode = 1;
}
static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
{
- vcpu->arch.nested_guest_state = 0;
+ atomic_set(&vcpu->arch.nested_guest_state, 0);
if (vcpu->arch.load_eoi_exitmap_pending) {
vcpu->arch.load_eoi_exitmap_pending = false;
@@ -475,7 +475,7 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
{
- return vcpu->arch.nested_guest_state != 0;
+ return atomic_read(&vcpu->arch.nested_guest_state) != 0;
}
static inline unsigned long kvm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 821411291f48..a28d263e936a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9555,7 +9555,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
WARN_ON_ONCE(is_guest_mode(vcpu) || is_smm(vcpu));
vcpu->arch.smm_mode = 0;
- vcpu->arch.nested_guest_state = 0;
+ atomic_set(&vcpu->arch.nested_guest_state, 0);
vcpu->arch.smi_pending = 0;
vcpu->arch.smi_count = 0;
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 4/5] KVM: x86: nVMX: move nested APICv state to nested_guest_state
2026-11-23 17:55 [PATCH v3 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
` (2 preceding siblings ...)
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 ` Maxim Levitsky
2026-11-23 17:55 ` [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky
4 siblings, 0 replies; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
Replace vmx->nested.pi_pending,pi_active and posted_intr_nv with
KVM_NESTED_PI_VECTOR_MASK,KVM_NESTED_PI_ACTIVE,KVM_NESTED_PI_PENDING
flags of nested_guest_state.
Since the nested state is cleared as a whole to 0, on each nested VM exit,
explicit clearing of vmx->nested.posted_intr_nv and vmx->nested.pi_pending
was dropped.
Also, since nested entry sets nested_guest_state to KVM_NESTED_IN_GUEST,
the publishing of KVM_NESTED_PI_VECTOR_MASK,KVM_NESTED_PI_ACTIVE is delayed
to happen after enter_guest_mode().
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/include/asm/kvm_host.h | 6 +++++-
arch/x86/kvm/vmx/nested.c | 34 +++++++++++++++++++--------------
arch/x86/kvm/vmx/vmx.c | 7 +++----
arch/x86/kvm/vmx/vmx.h | 2 --
4 files changed, 28 insertions(+), 21 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 562042d5589b..f7f1b7611a73 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -757,7 +757,11 @@ struct kvm_vcpu_arch {
u64 arch_capabilities;
u64 perf_capabilities;
-#define KVM_NESTED_IN_GUEST BIT(8)
+#define KVM_NESTED_PI_VECTOR_MASK 0xFF
+#define KVM_NESTED_IN_GUEST BIT(8)
+#define KVM_NESTED_PI_ACTIVE BIT(9)
+#define KVM_NESTED_PI_PENDING BIT(10)
+
/* Non zero when the guest is executing another guest */
atomic_t nested_guest_state;
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index e336dfb8d6b9..8d242d46a245 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -351,7 +351,6 @@ static void free_nested(struct kvm_vcpu *vcpu)
vmx->nested.smm.vmxon = false;
vmx->nested.vmxon_ptr = INVALID_GPA;
free_vpid(vmx->nested.vpid02);
- vmx->nested.posted_intr_nv = -1;
vmx->nested.current_vmptr = INVALID_GPA;
if (enable_shadow_vmcs) {
vmx_disable_shadow_vmcs(vmx);
@@ -2445,13 +2444,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
~PIN_BASED_VMX_PREEMPTION_TIMER);
/* Posted interrupts setting is only taken from vmcs12. */
- vmx->nested.pi_pending = false;
- if (nested_cpu_has_posted_intr(vmcs12)) {
- vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
- } else {
- vmx->nested.posted_intr_nv = -1;
+ if (!nested_cpu_has_posted_intr(vmcs12))
exec_control &= ~PIN_BASED_POSTED_INTR;
- }
+
pin_controls_set(vmx, exec_control);
/*
@@ -3720,6 +3715,10 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
}
+ /* Publish nested posted interrupt state */
+ if (nested_cpu_has_posted_intr(vmcs12))
+ atomic_or(KVM_NESTED_PI_ACTIVE | vmcs12->posted_intr_nv,
+ &vcpu->arch.nested_guest_state);
/*
* Re-evaluate pending events if L1 had a pending IRQ/NMI/INIT/SIPI
* when it executed VMLAUNCH/VMRESUME, as entering non-root mode can
@@ -4033,13 +4032,15 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
void *vapic_page;
u16 status;
- if (!vmx->nested.pi_pending)
+ 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;
- vmx->nested.pi_pending = false;
+ atomic_andnot(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
/*
* Don't test the value of PID.ON.
@@ -4191,6 +4192,8 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection)
void *vapic = vmx->nested.virtual_apic_map.hva;
int max_irr, vppr;
+ int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
+
if (nested_vmx_preemption_timer_pending(vcpu) ||
vmx->nested.mtf_pending)
return true;
@@ -4217,7 +4220,7 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection)
if ((max_irr & 0xf0) > (vppr & 0xf0))
return true;
- if (vmx->nested.pi_pending && vmx->nested.pi_desc) {
+ if ((nested_state & KVM_NESTED_PI_PENDING) && vmx->nested.pi_desc) {
max_irr = pi_find_highest_vector(vmx->nested.pi_desc);
if (max_irr > 0 && (max_irr & 0xf0) > (vppr & 0xf0))
return true;
@@ -4448,7 +4451,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
}
if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
- int irq;
+ int irq, nested_state;
if (!nested_exit_on_intr(vcpu)) {
if (block_nested_events)
@@ -4481,6 +4484,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
goto no_vmexit;
}
+ nested_state = atomic_read(&vcpu->arch.nested_guest_state);
+
/*
* If the IRQ is L2's PI notification vector, process posted
* interrupts for L2 instead of injecting VM-Exit, as the
@@ -4489,7 +4494,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
* through the local APIC trigger posted interrupt processing,
* and enabling posted interrupts requires ACK-on-exit.
*/
- if (irq == vmx->nested.posted_intr_nv) {
+ if ((nested_state & KVM_NESTED_PI_ACTIVE) &&
+ ((nested_state & KVM_NESTED_PI_VECTOR_MASK) == irq)) {
/*
* Nested posted interrupts are delivered via RVI, i.e.
* aren't injected by KVM, and so can be queued even if
@@ -4498,7 +4504,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
if (block_non_injected_events)
return -EBUSY;
- vmx->nested.pi_pending = true;
+ atomic_or(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
+
kvm_apic_clear_irr(vcpu, irq);
goto no_vmexit;
}
@@ -5587,7 +5594,6 @@ static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
copy_shadow_to_vmcs12(vmx);
vmx_disable_shadow_vmcs(vmx);
}
- vmx->nested.posted_intr_nv = -1;
/* Flush VMCS12 to guest memory */
kvm_vcpu_write_guest_page(vcpu,
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 612ab07d4100..e6dd37a3ac44 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4370,7 +4370,7 @@ void vmx_recalc_intercepts(struct kvm_vcpu *vcpu)
static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
int vector)
{
- struct vcpu_vmx *vmx = to_vmx(vcpu);
+ int nested_state = atomic_read(&vcpu->arch.nested_guest_state);
/*
* DO NOT query the vCPU's vmcs12, as vmcs12 is dynamically allocated
@@ -4380,12 +4380,12 @@ static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
* L1 has enabled posted interrupts for L2.
*/
if (is_guest_mode(vcpu) &&
- vector == vmx->nested.posted_intr_nv) {
+ 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.
*/
- vmx->nested.pi_pending = true;
+ atomic_or(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state);
kvm_make_request(KVM_REQ_EVENT, vcpu);
/*
@@ -5019,7 +5019,6 @@ static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu)
vcpu_setup_sgx_lepubkeyhash(vcpu);
- vmx->nested.posted_intr_nv = -1;
vmx->nested.vmxon_ptr = INVALID_GPA;
vmx->nested.current_vmptr = INVALID_GPA;
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index dc8517f15bc4..0632cd8908b3 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -151,8 +151,6 @@ struct nested_vmx {
struct kvm_host_map pi_desc_map;
struct pi_desc *pi_desc;
- bool pi_pending;
- u16 posted_intr_nv;
struct hrtimer preemption_timer;
u64 preemption_timer_deadline;
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts
2026-11-23 17:55 [PATCH v3 0/5] KVM: nVMX: fix nested APICv emulation for windows guests Maxim Levitsky
` (3 preceding siblings ...)
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
4 siblings, 0 replies; 7+ messages in thread
From: Maxim Levitsky @ 2026-11-23 17:55 UTC (permalink / raw)
To: kvm; +Cc: linux-kernel, Paolo Bonzini, x86, Sean Christopherson, Maxim Levitsky
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode
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-24 2:59 ` kernel test robot
0 siblings, 0 replies; 7+ messages in thread
From: kernel test robot @ 2026-11-24 2:59 UTC (permalink / raw)
To: Maxim Levitsky, kvm
Cc: oe-kbuild-all, linux-kernel, Paolo Bonzini, x86,
Sean Christopherson, Maxim Levitsky
Hi Maxim,
kernel test robot noticed the following build errors:
[auto build test ERROR on kvm/queue]
[also build test ERROR on kvm/next mst-vhost/linux-next linus/master v7.4-rc4]
[cannot apply to tip/master kvm/linux-next tip/auto-latest next-20261123]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Maxim-Levitsky/KVM-nVMX-don-t-check-PIR-ON-when-processing-nested-posted-interrupts/20261123-125508
base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link: https://lore.kernel.org/r/20261123175512.80438-3-mlevitsk%40redhat.com
patch subject: [PATCH v3 2/5] KVM: x86: split vcpu->arch.hflags into nested_guest_mode and smm_mode
config: x86_64-buildonly-randconfig-1001-20261124 (https://download.01.org/0day-ci/archive/20261124/20261124105040.GgfsW2nq-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261124/20261124105040.GgfsW2nq-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261124105040.GgfsW2nq-lkp@intel.com/
All errors (new ones prefixed by >>):
arch/x86/kvm/x86.c: In function 'kvm_vcpu_reset':
>> arch/x86/kvm/x86.c:9652:19: error: 'struct kvm_vcpu_arch' has no member named 'smm_mode'
9652 | vcpu->arch.smm_mode = 0;
| ^
vim +9652 arch/x86/kvm/x86.c
9623
9624 void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
9625 {
9626 struct kvm_cpuid_entry2 *cpuid_0x1;
9627 unsigned long old_cr0 = kvm_read_cr0(vcpu);
9628 unsigned long new_cr0;
9629
9630 /*
9631 * Several of the "set" flows, e.g. ->set_cr0(), read other registers
9632 * to handle side effects. RESET emulation hits those flows and relies
9633 * on emulated/virtualized registers, including those that are loaded
9634 * into hardware, to be zeroed at vCPU creation. Use CRs as a sentinel
9635 * to detect improper or missing initialization.
9636 */
9637 WARN_ON_ONCE(!init_event &&
9638 (old_cr0 || kvm_read_cr3(vcpu) || kvm_read_cr4(vcpu)));
9639
9640 /*
9641 * SVM doesn't unconditionally VM-Exit on INIT and SHUTDOWN, thus it's
9642 * possible to INIT the vCPU while L2 is active. Force the vCPU back
9643 * into L1 as EFER.SVME is cleared on INIT (along with all other EFER
9644 * bits), i.e. virtualization is disabled.
9645 */
9646 if (is_guest_mode(vcpu))
9647 kvm_leave_nested(vcpu);
9648
9649 kvm_lapic_reset(vcpu, init_event);
9650
9651 WARN_ON_ONCE(is_guest_mode(vcpu) || is_smm(vcpu));
> 9652 vcpu->arch.smm_mode = 0;
9653 vcpu->arch.nested_guest_state = 0;
9654
9655 vcpu->arch.smi_pending = 0;
9656 vcpu->arch.smi_count = 0;
9657 atomic_set(&vcpu->arch.nmi_queued, 0);
9658 vcpu->arch.nmi_pending = 0;
9659 vcpu->arch.nmi_injected = false;
9660 kvm_clear_interrupt_queue(vcpu);
9661 kvm_clear_exception_queue(vcpu);
9662
9663 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
9664 kvm_update_dr0123(vcpu);
9665 vcpu->arch.dr6 = DR6_ACTIVE_LOW;
9666 vcpu->arch.dr7 = DR7_FIXED_1;
9667 kvm_update_dr7(vcpu);
9668
9669 vcpu->arch.cr2 = 0;
9670
9671 kvm_make_request(KVM_REQ_EVENT, vcpu);
9672 vcpu->arch.apf.msr_en_val = 0;
9673 vcpu->arch.apf.msr_int_val = 0;
9674 vcpu->arch.st.msr_val = 0;
9675
9676 kvmclock_reset(vcpu);
9677
9678 kvm_clear_async_pf_completion_queue(vcpu);
9679 kvm_async_pf_hash_reset(vcpu);
9680 vcpu->arch.apf.halted = false;
9681
9682 kvm_xstate_reset(vcpu, init_event);
9683
9684 if (!init_event) {
9685 vcpu->arch.smbase = 0x30000;
9686
9687 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9688
9689 vcpu->arch.msr_misc_features_enables = 0;
9690 vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL |
9691 MSR_IA32_MISC_ENABLE_BTS_UNAVAIL;
9692
9693 __kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP);
9694 kvm_msr_write(vcpu, MSR_IA32_XSS, 0);
9695 }
9696
9697 /* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */
9698 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9699 kvm_register_mark_dirty(vcpu, VCPU_REGS_RSP);
9700
9701 /*
9702 * Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon)
9703 * if no CPUID match is found. Note, it's impossible to get a match at
9704 * RESET since KVM emulates RESET before exposing the vCPU to userspace,
9705 * i.e. it's impossible for kvm_find_cpuid_entry() to find a valid entry
9706 * on RESET. But, go through the motions in case that's ever remedied.
9707 */
9708 cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1);
9709 kvm_edx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600);
9710
9711 kvm_x86_call(vcpu_reset)(vcpu, init_event);
9712
9713 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
9714 kvm_rip_write(vcpu, 0xfff0);
9715
9716 vcpu->arch.cr3 = 0;
9717 kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
9718
9719 /*
9720 * CR0.CD/NW are set on RESET, preserved on INIT. Note, some versions
9721 * of Intel's SDM list CD/NW as being set on INIT, but they contradict
9722 * (or qualify) that with a footnote stating that CD/NW are preserved.
9723 */
9724 new_cr0 = X86_CR0_ET;
9725 if (init_event)
9726 new_cr0 |= (old_cr0 & (X86_CR0_NW | X86_CR0_CD));
9727 else
9728 new_cr0 |= X86_CR0_NW | X86_CR0_CD;
9729
9730 kvm_x86_call(set_cr0)(vcpu, new_cr0);
9731 kvm_x86_call(set_cr4)(vcpu, 0);
9732 kvm_x86_call(set_efer)(vcpu, 0);
9733 kvm_x86_call(update_exception_bitmap)(vcpu);
9734
9735 /*
9736 * Unconditionally reset the MMU, even though super strictly speaking,
9737 * a MMU reset is only necessary if and only one of the page role bits
9738 * is changing. All of the CR0/CR4/EFER bits effectively get covered
9739 * by checking CR0.PG=1, as CR0.WP and the paging-related bits in CR4
9740 * and EFER are irrelevant if CR0.PG was '0'. Subtly, if the vCPU was
9741 * in guest mode (L2) or SMM (although emulating INIT in SMM _should_
9742 * be impossible), an MMU reset is also necessary as "guest mode" and
9743 * "smm" are both encoded in the shadow page role. As a result, the
9744 * only scenarios where an MMU reset is *not* needed is if the vCPU is
9745 * in Real Mode or Unpaged Protected Mode, and micro-optimizing those
9746 * paths are not worth the risk of running with a stale MMU context.
9747 */
9748 kvm_mmu_reset_context(vcpu);
9749
9750 /*
9751 * Intel's SDM states that all TLB entries are flushed on INIT "as with
9752 * a hardware reset", and AMD's APM states the TLBs are flushed on
9753 * "External initialization of the processor". Flush the guest TLB for
9754 * both RESET and INIT, regardless of vendor, even though both the SDM
9755 * and APM contradict the above statements in other tables. There is
9756 * no meaningful benefit in relying on the guest to flush the TLB, and
9757 * a spurious TLB flush is benign and likely negligible from a
9758 * performance perspective (especially for RESET).
9759 */
9760 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
9761 }
9762 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_reset);
9763
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-24 3:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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-24 2:59 ` kernel test robot
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 ` [PATCH v3 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts Maxim Levitsky
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®