Skip to content

Commit 56cc240

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: nVMX: fix "acknowledge interrupt on exit" when APICv is in use
After commit 77b0f5d (KVM: nVMX: Ack and write vector info to intr_info if L1 asks us to), "Acknowledge interrupt on exit" behavior can be emulated. To do so, KVM will ask the APIC for the interrupt vector if during a nested vmexit if VM_EXIT_ACK_INTR_ON_EXIT is set. With APICv, kvm_get_apic_interrupt would return -1 and give the following WARNING: Call Trace: [<ffffffff81493563>] dump_stack+0x49/0x5e [<ffffffff8103f0eb>] warn_slowpath_common+0x7c/0x96 [<ffffffffa059709a>] ? nested_vmx_vmexit+0xa4/0x233 [kvm_intel] [<ffffffff8103f11a>] warn_slowpath_null+0x15/0x17 [<ffffffffa059709a>] nested_vmx_vmexit+0xa4/0x233 [kvm_intel] [<ffffffffa0594295>] ? nested_vmx_exit_handled+0x6a/0x39e [kvm_intel] [<ffffffffa0537931>] ? kvm_apic_has_interrupt+0x80/0xd5 [kvm] [<ffffffffa05972ec>] vmx_check_nested_events+0xc3/0xd3 [kvm_intel] [<ffffffffa051ebe9>] inject_pending_event+0xd0/0x16e [kvm] [<ffffffffa051efa0>] vcpu_enter_guest+0x319/0x704 [kvm] To fix this, we cannot rely on the processor's virtual interrupt delivery, because "acknowledge interrupt on exit" must only update the virtual ISR/PPR/IRR registers (and SVI, which is just a cache of the virtual ISR) but it should not deliver the interrupt through the IDT. Thus, KVM has to deliver the interrupt "by hand", similar to the treatment of EOI in commit fc57ac2 (KVM: lapic: sync highest ISR to hardware apic on EOI, 2014-05-14). The patch modifies kvm_cpu_get_interrupt to always acknowledge an interrupt; there are only two callers, and the other is not affected because it is never reached with kvm_apic_vid_enabled() == true. Then it modifies apic_set_isr and apic_clear_irr to update SVI and RVI in addition to the registers. Suggested-by: Paolo Bonzini <pbonzini@redhat.com> Suggested-by: "Zhang, Yang Z" <yang.z.zhang@intel.com> Tested-by: Liu, RongrongX <rongrongx.liu@intel.com> Tested-by: Felipe Reyes <freyes@suse.com> Fixes: 77b0f5d Cc: stable@vger.kernel.org Signed-off-by: Wanpeng Li <wanpeng.li@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent f3380ca commit 56cc240

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

‎arch/x86/kvm/irq.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
108108

109109
vector = kvm_cpu_get_extint(v);
110110

111-
if (kvm_apic_vid_enabled(v->kvm) || vector != -1)
111+
if (vector != -1)
112112
return vector; /* PIC */
113113

114114
return kvm_get_apic_interrupt(v); /* APIC */

‎arch/x86/kvm/lapic.c‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,25 +352,46 @@ static inline int apic_find_highest_irr(struct kvm_lapic *apic)
352352

353353
static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
354354
{
355-
apic->irr_pending = false;
355+
struct kvm_vcpu *vcpu;
356+
357+
vcpu = apic->vcpu;
358+
356359
apic_clear_vector(vec, apic->regs + APIC_IRR);
357-
if (apic_search_irr(apic) != -1)
358-
apic->irr_pending = true;
360+
if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
361+
/* try to update RVI */
362+
kvm_make_request(KVM_REQ_EVENT, vcpu);
363+
else {
364+
vec = apic_search_irr(apic);
365+
apic->irr_pending = (vec != -1);
366+
}
359367
}
360368

361369
static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
362370
{
363-
/* Note that we never get here with APIC virtualization enabled. */
371+
struct kvm_vcpu *vcpu;
372+
373+
if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
374+
return;
375+
376+
vcpu = apic->vcpu;
364377

365-
if (!__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
366-
++apic->isr_count;
367-
BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
368378
/*
369-
* ISR (in service register) bit is set when injecting an interrupt.
370-
* The highest vector is injected. Thus the latest bit set matches
371-
* the highest bit in ISR.
379+
* With APIC virtualization enabled, all caching is disabled
380+
* because the processor can modify ISR under the hood. Instead
381+
* just set SVI.
372382
*/
373-
apic->highest_isr_cache = vec;
383+
if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
384+
kvm_x86_ops->hwapic_isr_update(vcpu->kvm, vec);
385+
else {
386+
++apic->isr_count;
387+
BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
388+
/*
389+
* ISR (in service register) bit is set when injecting an interrupt.
390+
* The highest vector is injected. Thus the latest bit set matches
391+
* the highest bit in ISR.
392+
*/
393+
apic->highest_isr_cache = vec;
394+
}
374395
}
375396

376397
static inline int apic_find_highest_isr(struct kvm_lapic *apic)
@@ -1627,11 +1648,16 @@ int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
16271648
int vector = kvm_apic_has_interrupt(vcpu);
16281649
struct kvm_lapic *apic = vcpu->arch.apic;
16291650

1630-
/* Note that we never get here with APIC virtualization enabled. */
1631-
16321651
if (vector == -1)
16331652
return -1;
16341653

1654+
/*
1655+
* We get here even with APIC virtualization enabled, if doing
1656+
* nested virtualization and L1 runs with the "acknowledge interrupt
1657+
* on exit" mode. Then we cannot inject the interrupt via RVI,
1658+
* because the process would deliver it through the IDT.
1659+
*/
1660+
16351661
apic_set_isr(vector, apic);
16361662
apic_update_ppr(apic);
16371663
apic_clear_irr(vector, apic);

0 commit comments

Comments
 (0)