Skip to content

Commit 66fa226

Browse files
Maxim Levitskybonzini
authored andcommitted
KVM: SVM: fix race between interrupt delivery and AVIC inhibition
If svm_deliver_avic_intr is called just after the target vcpu's AVIC got inhibited, it might read a stale value of vcpu->arch.apicv_active which can lead to the target vCPU not noticing the interrupt. To fix this use load-acquire/store-release so that, if the target vCPU is IN_GUEST_MODE, we're guaranteed to see a previous disabling of the AVIC. If AVIC has been disabled in the meanwhile, proceed with the KVM_REQ_EVENT-based delivery. Incomplete IPI vmexit has the same races as svm_deliver_avic_intr, and in fact it can be handled in exactly the same way; the only difference lies in who has set IRR, whether svm_deliver_interrupt or the processor. Therefore, svm_complete_interrupt_delivery can be used to fix incomplete IPI vmexits as well. Co-developed-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 3081117 commit 66fa226

File tree

4 files changed

+55
-49
lines changed

4 files changed

+55
-49
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
269269
return 0;
270270
}
271271

272-
static void avic_ring_doorbell(struct kvm_vcpu *vcpu)
272+
void avic_ring_doorbell(struct kvm_vcpu *vcpu)
273273
{
274274
/*
275275
* Note, the vCPU could get migrated to a different pCPU at any point,
@@ -300,8 +300,13 @@ static void avic_kick_target_vcpus(struct kvm *kvm, struct kvm_lapic *source,
300300
kvm_for_each_vcpu(i, vcpu, kvm) {
301301
if (kvm_apic_match_dest(vcpu, source, icrl & APIC_SHORT_MASK,
302302
GET_APIC_DEST_FIELD(icrh),
303-
icrl & APIC_DEST_MASK))
304-
kvm_vcpu_wake_up(vcpu);
303+
icrl & APIC_DEST_MASK)) {
304+
vcpu->arch.apic->irr_pending = true;
305+
svm_complete_interrupt_delivery(vcpu,
306+
icrl & APIC_MODE_MASK,
307+
icrl & APIC_INT_LEVELTRIG,
308+
icrl & APIC_VECTOR_MASK);
309+
}
305310
}
306311
}
307312

@@ -663,43 +668,6 @@ void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
663668
return;
664669
}
665670

666-
int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
667-
{
668-
if (!vcpu->arch.apicv_active)
669-
return -1;
670-
671-
/*
672-
* Pairs with the smp_mb_*() after setting vcpu->guest_mode in
673-
* vcpu_enter_guest() to ensure the write to the vIRR is ordered before
674-
* the read of guest_mode, which guarantees that either VMRUN will see
675-
* and process the new vIRR entry, or that the below code will signal
676-
* the doorbell if the vCPU is already running in the guest.
677-
*/
678-
smp_mb__after_atomic();
679-
680-
/*
681-
* Signal the doorbell to tell hardware to inject the IRQ if the vCPU
682-
* is in the guest. If the vCPU is not in the guest, hardware will
683-
* automatically process AVIC interrupts at VMRUN.
684-
*/
685-
if (vcpu->mode == IN_GUEST_MODE) {
686-
/*
687-
* Signal the doorbell to tell hardware to inject the IRQ. If
688-
* the vCPU exits the guest before the doorbell chimes, hardware
689-
* will automatically process AVIC interrupts at the next VMRUN.
690-
*/
691-
avic_ring_doorbell(vcpu);
692-
} else {
693-
/*
694-
* Wake the vCPU if it was blocking. KVM will then detect the
695-
* pending IRQ when checking if the vCPU has a wake event.
696-
*/
697-
kvm_vcpu_wake_up(vcpu);
698-
}
699-
700-
return 0;
701-
}
702-
703671
bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
704672
{
705673
return false;

arch/x86/kvm/svm/svm.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,21 +3299,55 @@ static void svm_set_irq(struct kvm_vcpu *vcpu)
32993299
SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
33003300
}
33013301

3302-
static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
3303-
int trig_mode, int vector)
3302+
void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
3303+
int trig_mode, int vector)
33043304
{
3305-
struct kvm_vcpu *vcpu = apic->vcpu;
3305+
/*
3306+
* vcpu->arch.apicv_active must be read after vcpu->mode.
3307+
* Pairs with smp_store_release in vcpu_enter_guest.
3308+
*/
3309+
bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE);
33063310

3307-
kvm_lapic_set_irr(vector, apic);
3308-
if (svm_deliver_avic_intr(vcpu, vector)) {
3311+
if (!READ_ONCE(vcpu->arch.apicv_active)) {
3312+
/* Process the interrupt via inject_pending_event */
33093313
kvm_make_request(KVM_REQ_EVENT, vcpu);
33103314
kvm_vcpu_kick(vcpu);
3315+
return;
3316+
}
3317+
3318+
trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, trig_mode, vector);
3319+
if (in_guest_mode) {
3320+
/*
3321+
* Signal the doorbell to tell hardware to inject the IRQ. If
3322+
* the vCPU exits the guest before the doorbell chimes, hardware
3323+
* will automatically process AVIC interrupts at the next VMRUN.
3324+
*/
3325+
avic_ring_doorbell(vcpu);
33113326
} else {
3312-
trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode,
3313-
trig_mode, vector);
3327+
/*
3328+
* Wake the vCPU if it was blocking. KVM will then detect the
3329+
* pending IRQ when checking if the vCPU has a wake event.
3330+
*/
3331+
kvm_vcpu_wake_up(vcpu);
33143332
}
33153333
}
33163334

3335+
static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
3336+
int trig_mode, int vector)
3337+
{
3338+
kvm_lapic_set_irr(vector, apic);
3339+
3340+
/*
3341+
* Pairs with the smp_mb_*() after setting vcpu->guest_mode in
3342+
* vcpu_enter_guest() to ensure the write to the vIRR is ordered before
3343+
* the read of guest_mode. This guarantees that either VMRUN will see
3344+
* and process the new vIRR entry, or that svm_complete_interrupt_delivery
3345+
* will signal the doorbell if the CPU has already entered the guest.
3346+
*/
3347+
smp_mb__after_atomic();
3348+
svm_complete_interrupt_delivery(apic->vcpu, delivery_mode, trig_mode, vector);
3349+
}
3350+
33173351
static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
33183352
{
33193353
struct vcpu_svm *svm = to_svm(vcpu);

arch/x86/kvm/svm/svm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ void svm_set_gif(struct vcpu_svm *svm, bool value);
489489
int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code);
490490
void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
491491
int read, int write);
492+
void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
493+
int trig_mode, int vec);
492494

493495
/* nested.c */
494496

@@ -572,12 +574,12 @@ bool svm_check_apicv_inhibit_reasons(ulong bit);
572574
void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
573575
void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr);
574576
void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr);
575-
int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec);
576577
bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu);
577578
int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
578579
uint32_t guest_irq, bool set);
579580
void avic_vcpu_blocking(struct kvm_vcpu *vcpu);
580581
void avic_vcpu_unblocking(struct kvm_vcpu *vcpu);
582+
void avic_ring_doorbell(struct kvm_vcpu *vcpu);
581583

582584
/* sev.c */
583585

arch/x86/kvm/x86.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9983,7 +9983,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
99839983
* result in virtual interrupt delivery.
99849984
*/
99859985
local_irq_disable();
9986-
vcpu->mode = IN_GUEST_MODE;
9986+
9987+
/* Store vcpu->apicv_active before vcpu->mode. */
9988+
smp_store_release(&vcpu->mode, IN_GUEST_MODE);
99879989

99889990
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
99899991

0 commit comments

Comments
 (0)