KVM: nVMX: fix nested APICv emulation for windows guests
v4 cover KVM: nVMX: fix nested APICv emulation for windows guests
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!!)
V4: random fixes, including sashiko feedback.
Best regards,
Maxim Levitsky
Maxim Levitsky (5):
KVM: nVMX: don't check PID.ON when processing nested posted interrupts
KVM: x86: split vcpu->arch.hflags into nested_guest_state 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 | 21 +++++--- arch/x86/kvm/regs.h | 10 ++-- arch/x86/kvm/smm.c | 8 +-- arch/x86/kvm/smm.h | 2 +- arch/x86/kvm/vmx/nested.c | 93 +++++++++++++++++++++++---------- arch/x86/kvm/vmx/vmx.c | 61 +++++++++++---------- arch/x86/kvm/vmx/vmx.h | 2 - arch/x86/kvm/x86.c | 13 +++-- 8 files changed, 127 insertions(+), 83 deletions(-)
Signature · 1 line
2.56.1
PATCH v4 1/5 KVM: nVMX: don't check PID.ON when processing nested posted interrupts
Despite the suggestion to test and set the PID.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 PID.ON.
--- arch/x86/kvm/vmx/nested.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +11 -4
@@ -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 guest sees the change to PID.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;
Signature · 1 line
2.56.1
PATCH v4 2/5 KVM: x86: split vcpu->arch.hflags into nested_guest_state and smm_mode
In reply to Maxim LevitskySplit 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.
--- 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 | 13 ++++++++----- 5 files changed, 27 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +11 -7
@@ -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 a nested 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)
arch/x86/kvm/regs.h +3 -3
@@ -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 &KVM_NESTED_IN_GUEST ; }static inline unsigned long kvm_get_segment_base (struct kvm_vcpu *vcpu,int seg)
arch/x86/kvm/smm.c +4 -4
@@ -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);
arch/x86/kvm/smm.h +1 -1
@@ -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);
arch/x86/kvm/x86.c +8 -5
@@ -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,10 @@ 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 ; +#ifdef CONFIG_KVM_SMM + vcpu->arch.smm_mode =0 ; +#endif + vcpu->arch.nested_guest_state =0 ; vcpu->arch.smi_pending =0 ; vcpu->arch.smi_count =0 ;
Signature · 1 line
2.56.1
PATCH v4 3/5 KVM: x86: convert nested_guest_state into an atomic variable
In reply to Maxim LevitskyConvert nested_guest_state into an atomic variable, to prepare to fix nested APICv delivery races.
No functional change expected.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/regs.h | 7 ++++--- arch/x86/kvm/x86.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -759,7 +759,7 @@ struct kvm_vcpu_arch {#define KVM_NESTED_IN_GUEST BIT(8)/* Non zero when the guest is executing a nested guest */ -u16 nested_guest_state; +atomic_t nested_guest_state;#ifdef CONFIG_KVM_SMM #define SMM_MASK BIT(0)
arch/x86/kvm/regs.h +4 -3
@@ -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,8 @@ 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 &KVM_NESTED_IN_GUEST ; +return atomic_read (&vcpu->arch.nested_guest_state) & +KVM_NESTED_IN_GUEST ; }static inline unsigned long kvm_get_segment_base (struct kvm_vcpu *vcpu,int seg)
arch/x86/kvm/x86.c +1 -1
@@ -9557,7 +9557,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)#ifdef CONFIG_KVM_SMM vcpu->arch.smm_mode =0 ;#endif - vcpu->arch.nested_guest_state =0 ; +atomic_set (&vcpu->arch.nested_guest_state,0 ); vcpu->arch.smi_pending =0 ; vcpu->arch.smi_count =0 ;
Signature · 1 line
2.56.1
PATCH v4 4/5 KVM: x86: nVMX: move nested APICv state to nested_guest_state
In reply to Maxim LevitskyReplace 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().
--- arch/x86/include/asm/kvm_host.h | 5 ++++- arch/x86/kvm/vmx/nested.c | 33 +++++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.c | 7 +++---- arch/x86/kvm/vmx/vmx.h | 2 -- 4 files changed, 26 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -1
@@ -757,7 +757,10 @@ 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 a nested guest */ atomic_t nested_guest_state;
arch/x86/kvm/vmx/nested.c +19 -14
@@ -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,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu); } +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 +4031,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 +4191,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 +4219,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 +4450,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 +4483,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 +4493,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 +4503,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 +5593,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,
arch/x86/kvm/vmx/vmx.c +3 -4
@@ -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 ;
arch/x86/kvm/vmx/vmx.h +0 -2
@@ -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;
Signature · 1 line
2.56.1
PATCH v4 5/5 KVM: x86: nVMX: fix races in delivery of nested posted interrupts
In reply to Maxim LevitskyIf 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.
Also delay publish to of the nested posted interrupt state to KVM_REQ_GET_NESTED_STATE_PAGES to avoid dealing with a situation where vm exit causes a L2->L2 vm exit before nested APICv pages were mapped.
--- arch/x86/kvm/regs.h | 5 ++-- arch/x86/kvm/vmx/nested.c | 63 ++++++++++++++++++++++++++------------- arch/x86/kvm/vmx/vmx.c | 60 ++++++++++++++++++------------------- 3 files changed, 76 insertions(+), 52 deletions(-)
arch/x86/kvm/regs.h +3 -2
@@ -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)
arch/x86/kvm/vmx/nested.c +43 -20
137 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +30 -30
71 lines not on this page · raw message
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v4 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] KVM: nVMX: delayed KVM_NESTED_PI_ACTIVE misdelivers PI to halted/restored L2 - [High] KVM: nVMX…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] KVM: nVMX: delayed KVM_NESTED_PI_ACTIVE misdelivers PI to halted/restored L2
- [High] KVM: nVMX: gating KVM_NESTED_PI_ACTIVE on pi_desc breaks deferred error exit
- [Medium] KVM: nVMX: __nested_vmx_vmexit() processes nested PI on failed VM-Entry
--
commit aef7be5a3f3ba4f7dee7a4841c7b946e8a11d349
Author: Maxim Levitsky <[email protected]>
KVM: x86: nVMX: fix races in delivery of nested posted interrupts
This commit serializes nested posted interrupt delivery against nested VM exit using atomic operations on vcpu->arch.nested_guest_state and delays enabling nested posted interrupts until vmcs12 pages are mapped.
Quoted 53 lines
arch/x86/kvm/vmx/nested.c +17
-3@@ -3440,7 +3440,18 @@ static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) }#endif -static bool nested_get_vmcs12_pages (struct kvm_vcpu *vcpu) +static void nested_vmx_enable_posted_interrupts (struct kvm_vcpu *vcpu) +{ +struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); + +WARN_ON_ONCE (vmcs12->posted_intr_nv & ~KVM_NESTED_PI_VECTOR_MASK ); + +atomic_or (KVM_NESTED_PI_ACTIVE | + (vmcs12->posted_intr_nv &KVM_NESTED_PI_VECTOR_MASK ), + &vcpu->arch.nested_guest_state); +} + +static bool nested_get_vmcs12_pages (struct kvm_vcpu *vcpu,bool from_vmentry) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -3505,6 +3516,9 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) offset_in_page(vmcs12->posted_intr_desc_addr));vmcs_write64 (POSTED_INTR_DESC_ADDR ,pfn_to_hpa (map->pfn) +offset_in_page (vmcs12->posted_intr_desc_addr)); + +if (!from_vmentry) +nested_vmx_enable_posted_interrupts (vcpu); }else {/* * Defer the KVM_INTERNAL_EXIT until KVM tries to @@ -3540,7 +3554,7 @@ static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) }#endif -if (is_guest_mode (vcpu) && !nested_get_vmcs12_pages (vcpu)) +if (is_guest_mode (vcpu) && !nested_get_vmcs12_pages (vcpu, false))return false;return true;@@ -3671,7 +3685,7 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,prepare_vmcs02_early (vmx, &vmx->vmcs01, vmcs12);if (from_vmentry) { -if (unlikely (!nested_get_vmcs12_pages (vcpu))) { +if (unlikely (!nested_get_vmcs12_pages (vcpu, true))) {vmx_switch_vmcs (vcpu, &vmx->vmcs01);return NVMX_VMENTRY_KVM_INTERNAL_ERROR ; }
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3720
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +3
-0@@ -3701,6 +3715,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, vmcs12->exit_qualification = failed_index; goto vmentry_fail_vmexit_guest_mode; } + +if (nested_cpu_has_posted_intr (vmcs12) && vmx->nested.pi_desc) +nested_vmx_enable_posted_interrupts (vcpu);
[Severity: High]
Does gating nested_vmx_enable_posted_interrupts() on mapping vmx->nested.pi_desc break the deferred KVM_EXIT_INTERNAL_ERROR when posted_intr_desc_addr cannot be mapped?
When kvm_vcpu_map() fails in nested_get_vmcs12_pages(), KVM sets vmx->nested.pi_desc = NULL to defer the internal error exit until __vmx_complete_nested_posted_interrupt() tries to access the descriptor. Because KVM_NESTED_PI_ACTIVE is now left unset when pi_desc is NULL, neither vmx_deliver_nested_posted_interrupt() nor vmx_check_nested_events() recognizes vmcs12->posted_intr_nv as L2's posted interrupt notification vector:
arch/x86/kvm/vmx/nested.c:vmx_check_nested_events() {
...
if ((nested_state & KVM_NESTED_PI_ACTIVE) &&
((nested_state & KVM_NESTED_PI_VECTOR_MASK) == irq)) {
vmx_complete_nested_posted_interrupt(vcpu);
goto no_vmexit;
}
nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT,
INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq, 0);
...
}Instead of setting KVM_NESTED_PI_PENDING and exiting to userspace with KVM_EXIT_INTERNAL_ERROR via __vmx_complete_nested_posted_interrupt(), KVM injects a bogus EXIT_REASON_EXTERNAL_INTERRUPT VM-Exit into L1, and the !vmx->nested.pi_desc check in __vmx_complete_nested_posted_interrupt() becomes unreachable.
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3717
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +0
-3}else {/* * The MMU is not initialized to point at the right entities yet and @@ -3712,9 +3729,6 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu); } -if (nested_cpu_has_posted_intr (vmcs12)) -atomic_or (KVM_NESTED_PI_ACTIVE | vmcs12->posted_intr_nv, - &vcpu->arch.nested_guest_state);
[Severity: High]
Does deferring nested_vmx_enable_posted_interrupts() until KVM_REQ_GET_NESTED_STATE_PAGES misdeliver nested posted interrupts when L2 is restored in KVM_MP_STATE_HALTED?
After nested_vmx_enter_non_root_mode(vcpu, false) during KVM_SET_NESTED_STATE (or vmx_leave_smm()), a halted L2 vCPU enters vcpu_block() rather than vcpu_enter_guest(), so KVM_REQ_GET_NESTED_STATE_PAGES is not serviced while blocked. When another vCPU sends vmcs12->posted_intr_nv to wake L2, vmx_deliver_nested_posted_interrupt() sees KVM_NESTED_PI_ACTIVE == 0 and queues the vector into L1's LAPIC IRR. vcpu_block() then runs kvm_check_nested_events() -> vmx_check_nested_events() while KVM_NESTED_PI_ACTIVE is still unset, failing to morph the notification vector and instead reflecting a spurious EXIT_REASON_EXTERNAL_INTERRUPT VM-Exit to L1.
Quoted 34 lines
arch/x86/kvm/vmx/nested.c +3
-10@@ -4025,22 +4039,15 @@ 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); +return -ENXIO ;/* * Don't test the value of PID.ON. @@ -4057,7 +4064,7 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)if (max_irr >0 ) { vapic_page = vmx->nested.virtual_apic_map.hva;if (!vapic_page) - goto mmio_needed; +return -ENXIO ;__kvm_apic_update_irr (vmx->nested.pi_desc->pir, vapic_page, &max_irr);
[ ... ]
arch/x86/kvm/vmx/nested.c void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, · line 5147
Quoted 9 lines
arch/x86/kvm/vmx/nested.c +4
-1@@ -5121,7 +5141,10 @@ 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); + +if (old_nested_state &KVM_NESTED_PI_PENDING ) +__vmx_complete_nested_posted_interrupt (vcpu);
[Severity: Medium]
Can calling __vmx_complete_nested_posted_interrupt() unconditionally in __nested_vmx_vmexit() lose posted interrupts when vmx->fail is set or virtual_apic_map is unmapped?
When a hardware VM-Entry of vmcs02 fails (vmx->fail == 1), __nested_vmx_vmexit() skips sync_vmcs02_to_vmcs12(). Running __vmx_complete_nested_posted_interrupt() before checking !vmx->fail clears PID.ON and harvests PID.PIR into the virtual APIC page, but writes the updated GUEST_INTR_STATUS.RVI to vmcs02 where it is discarded without being synced to vmcs12.
Also, if virtual_apic_page_addr could not be mapped (which forces a vmcs02 VM-Entry failure while leaving pi_desc mapped), __vmx_complete_nested_posted_interrupt() clears PID.ON in guest memory before checking !vapic_page and returns -ENXIO without harvesting PIR or marking pi_desc_map dirty, while __nested_vmx_vmexit() ignores the return value.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v4 5/5] KVM: x86: nVMX: fix races in delivery of nested posted interrupts
Quoted 75 lines · On Wed, 2026-11-25 at 05:52 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] KVM: nVMX: delayedKVM_NESTED_PI_ACTIVEmisdelivers PI to halted/restored L2
- [High] KVM: nVMX: gatingKVM_NESTED_PI_ACTIVEonpi_descbreaks deferred error exit
- [Medium] KVM: nVMX:__nested_vmx_vmexit()processes nested PI on failed VM-Entry
--commit
aef7be5a3f3ba4f7dee7a4841c7b946e8a11d349
Author: Maxim Levitsky <[email protected]>KVM: x86: nVMX: fix races in delivery of nested posted interrupts
This commit serializes nested posted interrupt delivery against nested VM exit using atomic operations on
vcpu->arch.nested_guest_stateand delays enabling nested posted interrupts until vmcs12 pages are mapped.Quoted 56 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -3440,7 +3440,18 @@ static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) } #endifarch/x86/kvm/vmx/nested.c +12
-1-static bool nested_get_vmcs12_pages (struct kvm_vcpu *vcpu) +static void nested_vmx_enable_posted_interrupts (struct kvm_vcpu *vcpu) +{ +struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); + +WARN_ON_ONCE (vmcs12->posted_intr_nv & ~KVM_NESTED_PI_VECTOR_MASK ); + +atomic_or (KVM_NESTED_PI_ACTIVE | + (vmcs12->posted_intr_nv &KVM_NESTED_PI_VECTOR_MASK ), + &vcpu->arch.nested_guest_state); +} + +static bool nested_get_vmcs12_pages (struct kvm_vcpu *vcpu,bool from_vmentry){
struct vmcs12 *vmcs12 =get_vmcs12(vcpu);
structvcpu_vmx*vmx =to_vmx(vcpu);arch/x86/kvm/vmx/nested.c +3
-0@@ -3505,6 +3516,9 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) offset_in_page(vmcs12->posted_intr_desc_addr)); vmcs_write64(POSTED_INTR_DESC_ADDR, pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr)); + +if (!from_vmentry) +nested_vmx_enable_posted_interrupts (vcpu); } else { /** Defer the KVM_INTERNAL_EXIT until KVM tries to @@ -3540,7 +3554,7 @@ static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) } #endifarch/x86/kvm/vmx/nested.c +1
-1-if (is_guest_mode (vcpu) && !nested_get_vmcs12_pages (vcpu)) +if (is_guest_mode (vcpu) && !nested_get_vmcs12_pages (vcpu, false))return false;
return true;arch/x86/kvm/vmx/nested.c +0
-0@@ -3671,7 +3685,7 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12);
if (from_vmentry) {arch/x86/kvm/vmx/nested.c +1
-1-if (unlikely (!nested_get_vmcs12_pages (vcpu))) { +if (unlikely (!nested_get_vmcs12_pages (vcpu, true))) {
vmx_switch_vmcs(vcpu, &vmx->vmcs01); returnNVMX_VMENTRY_KVM_INTERNAL_ERROR; }arch/x86/kvm/vmx/nested.c +0
-0@@ -3701,6 +3715,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, vmcs12->exit_qualification = failed_index; goto vmentry_fail_vmexit_guest_mode; }
Quoted 8 lines
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3723
Quoted 3 lines
arch/x86/kvm/vmx/nested.c +3
-0+ +if (nested_cpu_has_posted_intr (vmcs12) && vmx->nested.pi_desc) +nested_vmx_enable_posted_interrupts (vcpu);[Severity: High]
Does gatingnested_vmx_enable_posted_interrupts()on mappingvmx->nested.pi_descbreak the deferredKVM_EXIT_INTERNAL_ERRORwhenposted_intr_desc_addrcannot be mapped?
Yes, I made this check to be consistent with KVM_GET_NESTED_STATE, but this was wrong, KVM_NESTED_PI_ACTIVE carries more weight that I thought.
To still be consistent, I'll activate PI regardless of mapping failure in both nested_vmx_enter_non_root_mode and in nested_get_vmcs12_pages.
Quoted 18 lines
When
kvm_vcpu_map()fails innested_get_vmcs12_pages(), KVM setsvmx->nested.pi_desc= NULL to defer the internal error exit until__vmx_complete_nested_posted_interrupt()tries to access the descriptor. BecauseKVM_NESTED_PI_ACTIVEis now left unset whenpi_descis NULL, neithervmx_deliver_nested_posted_interrupt()norvmx_check_nested_events()recognizesvmcs12->posted_intr_nvas L2's posted interrupt notification vector:arch/x86/kvm/vmx/nested.c:vmx_check_nested_events() {
...if ((nested_state & KVM_NESTED_PI_ACTIVE) && ((nested_state & KVM_NESTED_PI_VECTOR_MASK) == irq)) { vmx_complete_nested_posted_interrupt(vcpu); goto no_vmexit; } nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq, 0);
Quoted 8 lines
...
}Instead of setting
KVM_NESTED_PI_PENDINGand exiting to userspace withKVM_EXIT_INTERNAL_ERRORvia__vmx_complete_nested_posted_interrupt(), KVM injects a bogusEXIT_REASON_EXTERNAL_INTERRUPTVM-Exit into L1, and the !vmx->nested.pi_desc check in__vmx_complete_nested_posted_interrupt()becomes unreachable.
This should be fixed.
Quoted 88 lines
arch/x86/kvm/vmx/nested.c enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, · line 3720
Quoted 11 lines
arch/x86/kvm/vmx/nested.c +0
-0} else { /** The MMU is not initialized to point at the right entities yet and @@ -3712,9 +3729,6 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); }arch/x86/kvm/vmx/nested.c +0
-3-if (nested_cpu_has_posted_intr (vmcs12)) -atomic_or (KVM_NESTED_PI_ACTIVE | vmcs12->posted_intr_nv, - &vcpu->arch.nested_guest_state);[Severity: High]
Does deferringnested_vmx_enable_posted_interrupts()untilKVM_REQ_GET_NESTED_STATE_PAGESmisdeliver nested posted interrupts when L2 is restored inKVM_MP_STATE_HALTED?After
nested_vmx_enter_non_root_mode(vcpu, false)duringKVM_SET_NESTED_STATE(orvmx_leave_smm()), a halted L2 vCPU entersvcpu_block()rather thanvcpu_enter_guest(), soKVM_REQ_GET_NESTED_STATE_PAGESis not serviced while blocked. When another vCPU sendsvmcs12->posted_intr_nvto wake L2,vmx_deliver_nested_posted_interrupt()seesKVM_NESTED_PI_ACTIVE== 0 and queues the vector into L1's LAPIC IRR.vcpu_block()then runskvm_check_nested_events()->vmx_check_nested_events()whileKVM_NESTED_PI_ACTIVEis still unset, failing to morph the notification vector and instead reflecting a spuriousEXIT_REASON_EXTERNAL_INTERRUPTVM-Exit to L1.Quoted 34 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -4025,22 +4039,15 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, } }arch/x86/kvm/vmx/nested.c +1
-1-static int vmx_complete_nested_posted_interrupt (struct kvm_vcpu *vcpu) +static int __vmx_complete_nested_posted_interrupt (struct kvm_vcpu *vcpu){
structvcpu_vmx*vmx =to_vmx(vcpu);
intmax_irr;
void*vapic_page;
u16 status;arch/x86/kvm/vmx/nested.c +0
-5-int nested_state =atomic_read (&vcpu->arch.nested_guest_state); - -if (!(nested_state &KVM_NESTED_PI_PENDING )) -return 0 ; -if (!vmx->nested.pi_desc)
arch/x86/kvm/vmx/nested.c +1
-3- goto mmio_needed; - -atomic_andnot (KVM_NESTED_PI_PENDING , &vcpu->arch.nested_guest_state); +return -ENXIO ;
/*
* Don't test the value of PID.ON.arch/x86/kvm/vmx/nested.c +1
-1@@ -4057,7 +4064,7 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) if (max_irr > 0) { vapic_page = vmx->nested.virtual_apic_map.hva; if (!vapic_page) - goto mmio_needed; +return -ENXIO ;
__kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page, &max_irr);[ ... ]
arch/x86/kvm/vmx/nested.c void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, · line 5146
Quoted 9 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -5121,7 +5141,10 @@ void __nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, if (enable_ept && is_pae_paging(vcpu)) vmx_ept_load_pdptrs(vcpu);arch/x86/kvm/vmx/nested.c +4
-1-leave_guest_mode (vcpu); + old_nested_state =leave_guest_mode (vcpu); + +if (old_nested_state &KVM_NESTED_PI_PENDING ) +__vmx_complete_nested_posted_interrupt (vcpu);[Severity: Medium]
Can calling__vmx_complete_nested_posted_interrupt()unconditionally in__nested_vmx_vmexit()lose posted interrupts whenvmx->failis set orvirtual_apic_mapis unmapped?When a hardware VM-Entry of vmcs02 fails (
vmx->fail== 1),__nested_vmx_vmexit()skips sync_vmcs02_to_vmcs12(). Running__vmx_complete_nested_posted_interrupt()before checking !vmx->fail clears PID.ON and harvests PID.PIR into the virtual APIC page, but writes the updated GUEST_INTR_STATUS.RVI to vmcs02 where it is discarded without being synced to vmcs12.
Quoted 6 lines
Also, if
virtual_apic_page_addrcould not be mapped (which forces a vmcs02 VM-Entry failure while leavingpi_descmapped),__vmx_complete_nested_posted_interrupt()clears PID.ON in guest memory before checking !vapic_page and returns-ENXIOwithout harvesting PIR or markingpi_desc_mapdirty, while__nested_vmx_vmexit()ignores the return value.
While not optimal, this is not a issue, as posted interrupt is not really lost, and since it only happens once per migration, I don't think that this is a problem.
Best regards,
Maxim Levitsky