Commit 1b9778da authored by Eddie Dong's avatar Eddie Dong Committed by Avi Kivity

KVM: Keep track of missed timer irq injections

APIC timer IRQ is set every time when a certain period
expires at host time, but the guest may be descheduled
at that time and thus the irq be overwritten by later fire.
This patch keep track of firing irq numbers and decrease
only when the IRQ is injected to guest or buffered in
APIC.
Signed-off-by: default avatarYaozu (Eddie) Dong <Eddie.Dong@intel.com>
Signed-off-by: default avatarQing He <qing.he@intel.com>
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent 6e5d865c
...@@ -78,3 +78,16 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu) ...@@ -78,3 +78,16 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0); smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);
} }
void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
{
kvm_inject_apic_timer_irqs(vcpu);
/* TODO: PIT, RTC etc. */
}
EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
{
kvm_apic_timer_intr_post(vcpu, vec);
/* TODO: PIT, RTC etc. */
}
EXPORT_SYMBOL_GPL(kvm_timer_intr_post);
...@@ -154,5 +154,9 @@ int kvm_ioapic_init(struct kvm *kvm); ...@@ -154,5 +154,9 @@ int kvm_ioapic_init(struct kvm *kvm);
void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level); void kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level);
int kvm_lapic_enabled(struct kvm_vcpu *vcpu); int kvm_lapic_enabled(struct kvm_vcpu *vcpu);
int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec);
void kvm_timer_intr_post(struct kvm_vcpu *vcpu, int vec);
void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
#endif #endif
...@@ -283,6 +283,8 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_init); ...@@ -283,6 +283,8 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_init);
void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
{ {
kvm_mmu_destroy(vcpu); kvm_mmu_destroy(vcpu);
if (vcpu->apic)
hrtimer_cancel(&vcpu->apic->timer.dev);
kvm_free_apic(vcpu->apic); kvm_free_apic(vcpu->apic);
free_page((unsigned long)vcpu->pio_data); free_page((unsigned long)vcpu->pio_data);
free_page((unsigned long)vcpu->run); free_page((unsigned long)vcpu->run);
......
...@@ -313,6 +313,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, ...@@ -313,6 +313,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
int vector, int level, int trig_mode) int vector, int level, int trig_mode)
{ {
int result = 0; int result = 0;
int orig_irr;
switch (delivery_mode) { switch (delivery_mode) {
case APIC_DM_FIXED: case APIC_DM_FIXED:
...@@ -321,7 +322,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, ...@@ -321,7 +322,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
if (unlikely(!apic_enabled(apic))) if (unlikely(!apic_enabled(apic)))
break; break;
if (apic_test_and_set_irr(vector, apic) && trig_mode) { orig_irr = apic_test_and_set_irr(vector, apic);
if (orig_irr && trig_mode) {
apic_debug("level trig mode repeatedly for vector %d", apic_debug("level trig mode repeatedly for vector %d",
vector); vector);
break; break;
...@@ -335,7 +337,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, ...@@ -335,7 +337,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
kvm_vcpu_kick(apic->vcpu); kvm_vcpu_kick(apic->vcpu);
result = 1; result = (orig_irr == 0);
break; break;
case APIC_DM_REMRD: case APIC_DM_REMRD:
...@@ -831,38 +833,33 @@ EXPORT_SYMBOL_GPL(kvm_lapic_enabled); ...@@ -831,38 +833,33 @@ EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
* timer interface * timer interface
*---------------------------------------------------------------------- *----------------------------------------------------------------------
*/ */
/* TODO: make sure __apic_timer_fn runs in current pCPU */
static int __apic_timer_fn(struct kvm_lapic *apic) static int __apic_timer_fn(struct kvm_lapic *apic)
{ {
u32 vector;
int result = 0; int result = 0;
wait_queue_head_t *q = &apic->vcpu->wq;
if (unlikely(!apic_enabled(apic) ||
!apic_lvt_enabled(apic, APIC_LVTT))) {
apic_debug("%s: time interrupt although apic is down\n",
__FUNCTION__);
return 0;
}
vector = apic_lvt_vector(apic, APIC_LVTT);
apic->timer.last_update = apic->timer.dev.expires;
atomic_inc(&apic->timer.pending); atomic_inc(&apic->timer.pending);
__apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0); if (waitqueue_active(q))
wake_up_interruptible(q);
if (apic_lvtt_period(apic)) { if (apic_lvtt_period(apic)) {
u32 offset;
u32 tmict = apic_get_reg(apic, APIC_TMICT);
offset = APIC_BUS_CYCLE_NS * apic->timer.divide_count * tmict;
result = 1; result = 1;
apic->timer.dev.expires = ktime_add_ns( apic->timer.dev.expires = ktime_add_ns(
apic->timer.dev.expires, apic->timer.dev.expires,
apic->timer.period); apic->timer.period);
} }
return result; return result;
} }
static int __inject_apic_timer_irq(struct kvm_lapic *apic)
{
int vector;
vector = apic_lvt_vector(apic, APIC_LVTT);
return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
}
static enum hrtimer_restart apic_timer_fn(struct hrtimer *data) static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
{ {
struct kvm_lapic *apic; struct kvm_lapic *apic;
...@@ -935,6 +932,27 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu) ...@@ -935,6 +932,27 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
return highest_irr; return highest_irr;
} }
void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
{
struct kvm_lapic *apic = vcpu->apic;
if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
atomic_read(&apic->timer.pending) > 0) {
if (__inject_apic_timer_irq(apic))
atomic_dec(&apic->timer.pending);
}
}
void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
{
struct kvm_lapic *apic = vcpu->apic;
if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
apic->timer.last_update = ktime_add_ns(
apic->timer.last_update,
apic->timer.period);
}
int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu) int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
{ {
int vector = kvm_apic_has_interrupt(vcpu); int vector = kvm_apic_has_interrupt(vcpu);
......
...@@ -1331,7 +1331,9 @@ static void svm_intr_assist(struct vcpu_svm *svm) ...@@ -1331,7 +1331,9 @@ static void svm_intr_assist(struct vcpu_svm *svm)
{ {
struct vmcb *vmcb = svm->vmcb; struct vmcb *vmcb = svm->vmcb;
int intr_vector = -1; int intr_vector = -1;
struct kvm_vcpu *vcpu = &svm->vcpu;
kvm_inject_pending_timer_irqs(vcpu);
if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) && if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) { ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
intr_vector = vmcb->control.exit_int_info & intr_vector = vmcb->control.exit_int_info &
...@@ -1344,7 +1346,7 @@ static void svm_intr_assist(struct vcpu_svm *svm) ...@@ -1344,7 +1346,7 @@ static void svm_intr_assist(struct vcpu_svm *svm)
if (vmcb->control.int_ctl & V_IRQ_MASK) if (vmcb->control.int_ctl & V_IRQ_MASK)
return; return;
if (!kvm_cpu_has_interrupt(&svm->vcpu)) if (!kvm_cpu_has_interrupt(vcpu))
return; return;
if (!(vmcb->save.rflags & X86_EFLAGS_IF) || if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
...@@ -1356,8 +1358,9 @@ static void svm_intr_assist(struct vcpu_svm *svm) ...@@ -1356,8 +1358,9 @@ static void svm_intr_assist(struct vcpu_svm *svm)
return; return;
} }
/* Okay, we can deliver the interrupt: grab it and update PIC state. */ /* Okay, we can deliver the interrupt: grab it and update PIC state. */
intr_vector = kvm_cpu_get_interrupt(&svm->vcpu); intr_vector = kvm_cpu_get_interrupt(vcpu);
svm_inject_irq(svm, intr_vector); svm_inject_irq(svm, intr_vector);
kvm_timer_intr_post(vcpu, intr_vector);
} }
static void kvm_reput_irq(struct vcpu_svm *svm) static void kvm_reput_irq(struct vcpu_svm *svm)
......
...@@ -2151,7 +2151,9 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu) ...@@ -2151,7 +2151,9 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu)
{ {
u32 idtv_info_field, intr_info_field; u32 idtv_info_field, intr_info_field;
int has_ext_irq, interrupt_window_open; int has_ext_irq, interrupt_window_open;
int vector;
kvm_inject_pending_timer_irqs(vcpu);
update_tpr_threshold(vcpu); update_tpr_threshold(vcpu);
has_ext_irq = kvm_cpu_has_interrupt(vcpu); has_ext_irq = kvm_cpu_has_interrupt(vcpu);
...@@ -2183,9 +2185,11 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu) ...@@ -2183,9 +2185,11 @@ static void vmx_intr_assist(struct kvm_vcpu *vcpu)
interrupt_window_open = interrupt_window_open =
((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0); (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
if (interrupt_window_open) if (interrupt_window_open) {
vmx_inject_irq(vcpu, kvm_cpu_get_interrupt(vcpu)); vector = kvm_cpu_get_interrupt(vcpu);
else vmx_inject_irq(vcpu, vector);
kvm_timer_intr_post(vcpu, vector);
} else
enable_irq_window(vcpu); enable_irq_window(vcpu);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment