Skip to content

Commit 31f251d

Browse files
sean-jcbonzini
authored andcommitted
KVM: SVM: Signal AVIC doorbell iff vCPU is in guest mode
Signal the AVIC doorbell iff the vCPU is running in the guest. If the vCPU is not IN_GUEST_MODE, it's guaranteed to pick up any pending IRQs on the next VMRUN, which unconditionally processes the vIRR. Add comments to document the logic. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c3e8abf commit 31f251d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,22 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
672672
return -1;
673673

674674
kvm_lapic_set_irr(vec, vcpu->arch.apic);
675+
676+
/*
677+
* Pairs with the smp_mb_*() after setting vcpu->guest_mode in
678+
* vcpu_enter_guest() to ensure the write to the vIRR is ordered before
679+
* the read of guest_mode, which guarantees that either VMRUN will see
680+
* and process the new vIRR entry, or that the below code will signal
681+
* the doorbell if the vCPU is already running in the guest.
682+
*/
675683
smp_mb__after_atomic();
676684

677-
if (avic_vcpu_is_running(vcpu)) {
685+
/*
686+
* Signal the doorbell to tell hardware to inject the IRQ if the vCPU
687+
* is in the guest. If the vCPU is not in the guest, hardware will
688+
* automatically process AVIC interrupts at VMRUN.
689+
*/
690+
if (vcpu->mode == IN_GUEST_MODE) {
678691
int cpu = READ_ONCE(vcpu->cpu);
679692

680693
/*
@@ -688,8 +701,13 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
688701
if (cpu != get_cpu())
689702
wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu));
690703
put_cpu();
691-
} else
704+
} else {
705+
/*
706+
* Wake the vCPU if it was blocking. KVM will then detect the
707+
* pending IRQ when checking if the vCPU has a wake event.
708+
*/
692709
kvm_vcpu_wake_up(vcpu);
710+
}
693711

694712
return 0;
695713
}

0 commit comments

Comments
 (0)