Conceptually, the i8254 is hooked to a PIC or IOAPIC.  Therefore,
this patch removes most references to "vcpu" in i8254.c.
There are two exceptions to this:

1)  In pit_timer_fn, we still have to kick the BSP to wake it out
of idle.  This will be changed in a later patch.

2)  In __kvm_migrate_pit_timer, we have to migrate the PIT
around with the BSP, since hrtimers work on a per-CPU basis.
I've added a comment here to clarify why this is needed.

Signed-off-by: Chris Lalancette <clala...@redhat.com>
---
:100644 100644 fab7440... d5c08fa... M  arch/x86/kvm/i8254.c
:100644 100644 d4c1c7f... d7bc40b... M  arch/x86/kvm/i8254.h
:100644 100644 96dfbb6... ab3a56e... M  arch/x86/kvm/irq.c
:100644 100644 c025a23... e16b968... M  arch/x86/kvm/irq.h
:100644 100644 55c7524... ba39e25... M  arch/x86/kvm/kvm_timer.h
 arch/x86/kvm/i8254.c     |   50 ++++++++++++++++++++++++++++++++++++++-------
 arch/x86/kvm/i8254.h     |    4 ++-
 arch/x86/kvm/irq.c       |    4 +-
 arch/x86/kvm/irq.h       |    2 -
 arch/x86/kvm/kvm_timer.h |    3 ++
 5 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c
index fab7440..d5c08fa 100644
--- a/arch/x86/kvm/i8254.c
+++ b/arch/x86/kvm/i8254.c
@@ -33,6 +33,7 @@
 
 #include "irq.h"
 #include "i8254.h"
+#include "kvm_timer.h"
 
 #ifndef CONFIG_X86_64
 #define mod_64(x, y) ((x) - (y) * div64_u64(x, y))
@@ -227,12 +228,13 @@ static void pit_latch_status(struct kvm *kvm, int channel)
        }
 }
 
-int pit_has_pending_timer(struct kvm_vcpu *vcpu)
+int pit_has_pending_timer(struct kvm *kvm)
 {
-       struct kvm_pit *pit = vcpu->kvm->arch.vpit;
+       struct kvm_pit *pit = kvm->arch.vpit;
 
-       if (pit && kvm_vcpu_is_bsp(vcpu) && pit->pit_state.irq_ack)
+       if (pit && pit->pit_state.irq_ack)
                return atomic_read(&pit->pit_state.pit_timer.pending);
+
        return 0;
 }
 
@@ -252,6 +254,13 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu)
        struct kvm_pit *pit = vcpu->kvm->arch.vpit;
        struct hrtimer *timer;
 
+       /*
+        * technically, the PIT isn't hooked to a particular VCPU;
+        * the logical structure is PIT -> [IOA]PIC -> CPU[s].  However,
+        * hrtimers expire on a per-cpu basis, and since we initially
+        * created the hrtimer during BSP creation, we move it around
+        * with the BSP.
+        */
        if (!kvm_vcpu_is_bsp(vcpu) || !pit)
                return;
 
@@ -277,6 +286,33 @@ static struct kvm_timer_ops kpit_ops = {
        .is_periodic = kpit_is_periodic,
 };
 
+static enum hrtimer_restart pit_timer_fn(struct hrtimer *data)
+{
+       struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
+       int restart_timer = 0;
+
+       /*
+        * There is a race window between reading and incrementing, but we do
+        * not care about potentially losing timer events in the !reinject
+        * case anyway.
+        */
+       if (ktimer->reinject || !atomic_read(&ktimer->pending))
+               atomic_inc(&ktimer->pending);
+
+       if (waitqueue_active(&ktimer->kvm->bsp_vcpu->wq))
+               wake_up_interruptible(&ktimer->kvm->bsp_vcpu->wq);
+
+       if (ktimer->t_ops->is_periodic(ktimer)) {
+               hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
+               restart_timer = 1;
+       }
+
+       if (restart_timer)
+               return HRTIMER_RESTART;
+       else
+               return HRTIMER_NORESTART;
+}
+
 static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period)
 {
        struct kvm_timer *pt = &ps->pit_timer;
@@ -291,10 +327,9 @@ static void create_pit_timer(struct kvm_kpit_state *ps, 
u32 val, int is_period)
        pt->period = interval;
        ps->is_periodic = is_period;
 
-       pt->timer.function = kvm_timer_fn;
+       pt->timer.function = pit_timer_fn;
        pt->t_ops = &kpit_ops;
        pt->kvm = ps->pit->kvm;
-       pt->vcpu = pt->kvm->bsp_vcpu;
 
        atomic_set(&pt->pending, 0);
        ps->irq_ack = 1;
@@ -705,10 +740,9 @@ static void __inject_pit_timer_intr(struct kvm *kvm)
                        kvm_apic_nmi_wd_deliver(vcpu);
 }
 
-void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)
+void kvm_inject_pit_timer_irqs(struct kvm *kvm)
 {
-       struct kvm_pit *pit = vcpu->kvm->arch.vpit;
-       struct kvm *kvm = vcpu->kvm;
+       struct kvm_pit *pit = kvm->arch.vpit;
        struct kvm_kpit_state *ps;
 
        if (pit) {
diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h
index d4c1c7f..d7bc40b 100644
--- a/arch/x86/kvm/i8254.h
+++ b/arch/x86/kvm/i8254.h
@@ -49,10 +49,12 @@ struct kvm_pit {
 #define KVM_MAX_PIT_INTR_INTERVAL   HZ / 100
 #define KVM_PIT_CHANNEL_MASK       0x3
 
-void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu);
+int pit_has_pending_timer(struct kvm *kvm);
+void kvm_inject_pit_timer_irqs(struct kvm *kvm);
 void kvm_pit_load_count(struct kvm *kvm, int channel, u32 val, int 
hpet_legacy_start);
 struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags);
 void kvm_free_pit(struct kvm *kvm);
 void kvm_pit_reset(struct kvm_pit *pit);
+void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu);
 
 #endif
diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index 96dfbb6..ab3a56e 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -34,7 +34,7 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        int ret;
 
-       ret = pit_has_pending_timer(vcpu);
+       ret = pit_has_pending_timer(vcpu->kvm);
        ret |= apic_has_pending_timer(vcpu);
 
        return ret;
@@ -89,7 +89,7 @@ EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
 {
        kvm_inject_apic_timer_irqs(vcpu);
-       kvm_inject_pit_timer_irqs(vcpu);
+       kvm_inject_pit_timer_irqs(vcpu->kvm);
        /* TODO: PIT, RTC etc. */
 }
 EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
index c025a23..e16b968 100644
--- a/arch/x86/kvm/irq.h
+++ b/arch/x86/kvm/irq.h
@@ -95,10 +95,8 @@ void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu);
 void kvm_apic_nmi_wd_deliver(struct kvm_vcpu *vcpu);
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu);
-void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu);
 void __kvm_migrate_timers(struct kvm_vcpu *vcpu);
 
-int pit_has_pending_timer(struct kvm_vcpu *vcpu);
 int apic_has_pending_timer(struct kvm_vcpu *vcpu);
 
 #endif
diff --git a/arch/x86/kvm/kvm_timer.h b/arch/x86/kvm/kvm_timer.h
index 55c7524..ba39e25 100644
--- a/arch/x86/kvm/kvm_timer.h
+++ b/arch/x86/kvm/kvm_timer.h
@@ -1,3 +1,5 @@
+#ifndef __KVM_TIMER_H
+#define __KVM_TIMER_H
 
 struct kvm_timer {
        struct hrtimer timer;
@@ -16,3 +18,4 @@ struct kvm_timer_ops {
 
 enum hrtimer_restart kvm_timer_fn(struct hrtimer *data);
 
+#endif
-- 
1.6.0.6

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to