[PATCH 3/4] KVM: x86: change PIT discard tick policy

From: Radim KrÄmÃÅ
Date: Wed Feb 03 2016 - 11:24:41 EST


Discard policy uses ack_notifiers to prevent injection of PIT interrupts
before EOI from the last one.

This patch changes the policy to always try to deliver the interrupt,
which makes a difference when its vector is in ISR.
Old implementation would drop the interrupt, but proposed one injects to
IRR, like real hardware would.

The old policy breaks legacy NMI watchdogs, where PIT is used through
virtual wire (LVT0): PIT never sends an interrupt before receiving EOI,
thus a guest deadlock with disabled interrupts will stop NMIs.

Note that NMI doesn't do EOI, so PIT also had to send a normal interrupt
through IOAPIC. (KVM's PIT is deeply rotten and luckily not used much
in modern systems.)

Even though there is a chance of regressions, I think we can fix the
LVT0 NMI bug without introducing a new tick policy.

Reported-by: Yuki Shibuya <shibuya.yk@xxxxxxxxxxxxxx>
Signed-off-by: Radim KrÄmÃÅ <rkrcmar@xxxxxxxxxx>
---
arch/x86/kvm/i8254.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
index a137eb381012..fc554fbf71a7 100644
--- a/arch/x86/kvm/i8254.c
+++ b/arch/x86/kvm/i8254.c
@@ -237,6 +237,9 @@ static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian)
struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state,
irq_ack_notifier);

+ if (!ps->reinject)
+ return;
+
atomic_set(&ps->irq_ack, 1);
if (atomic_add_unless(&ps->pending, -1, 0))
/* in this case, we had multiple outstanding pit interrupts
@@ -274,7 +277,7 @@ static void pit_do_work(struct kthread_work *work)
int i;
struct kvm_kpit_state *ps = &pit->pit_state;

- if (!atomic_xchg(&ps->irq_ack, 0))
+ if (ps->reinject && !atomic_xchg(&ps->irq_ack, 0))
return;

kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false);
@@ -299,10 +302,10 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
struct kvm_kpit_state *ps = container_of(data, struct kvm_kpit_state, timer);
struct kvm_pit *pt = ps->kvm->arch.vpit;

- if (ps->reinject || !atomic_read(&ps->pending)) {
+ if (ps->reinject)
atomic_inc(&ps->pending);
- queue_kthread_work(&pt->worker, &pt->expired);
- }
+
+ queue_kthread_work(&pt->worker, &pt->expired);

if (ps->is_periodic) {
hrtimer_add_expires_ns(&ps->timer, ps->period);
--
2.7.0