]> xenbits.xensource.com Git - xen.git/commitdiff
timers: Track inactive timers and migrate them on cpu offline.
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 2 Jun 2010 12:13:11 +0000 (13:13 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 2 Jun 2010 12:13:11 +0000 (13:13 +0100)
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
xen/arch/x86/irq.c
xen/common/domain.c
xen/common/schedule.c
xen/common/timer.c
xen/include/xen/timer.h

index a2e3e5d0ecc782399550e83132222dc8b9d19766..c4ca46c329c3cf9a7c21bbf2f6caf98ccb9e2978 100644 (file)
@@ -46,8 +46,6 @@ static DECLARE_BITMAP(used_vectors, NR_VECTORS);
 
 struct irq_cfg __read_mostly *irq_cfg = NULL;
 
-static struct timer *__read_mostly irq_guest_eoi_timer;
-
 static DEFINE_SPINLOCK(vector_lock);
 
 DEFINE_PER_CPU(vector_irq_t, vector_irq);
@@ -275,18 +273,15 @@ int init_irq_data(void)
     irq_desc = xmalloc_array(struct irq_desc, nr_irqs);
     irq_cfg = xmalloc_array(struct irq_cfg, nr_irqs);
     irq_status = xmalloc_array(int, nr_irqs);
-    irq_guest_eoi_timer = xmalloc_array(struct timer, nr_irqs);
     irq_vector = xmalloc_array(u8, nr_irqs_gsi);
     
-    if (!irq_desc || !irq_cfg || !irq_status ||! irq_vector ||
-        !irq_guest_eoi_timer)
+    if ( !irq_desc || !irq_cfg || !irq_status ||! irq_vector )
         return -ENOMEM;
 
     memset(irq_desc, 0,  nr_irqs * sizeof(*irq_desc));
     memset(irq_cfg, 0,  nr_irqs * sizeof(*irq_cfg));
     memset(irq_status, 0,  nr_irqs * sizeof(*irq_status));
     memset(irq_vector, 0, nr_irqs_gsi * sizeof(*irq_vector));
-    memset(irq_guest_eoi_timer, 0, nr_irqs * sizeof(*irq_guest_eoi_timer));
     
     for (irq = 0; irq < nr_irqs; irq++) {
         desc = irq_to_desc(irq);
@@ -741,6 +736,7 @@ typedef struct {
 #define ACKTYPE_UNMASK 1     /* Unmask PIC hardware (from any CPU)   */
 #define ACKTYPE_EOI    2     /* EOI on the CPU that was interrupted  */
     cpumask_t cpu_eoi_map;   /* CPUs that need to EOI this interrupt */
+    struct timer eoi_timer;
     struct domain *guest[IRQ_MAX_GUESTS];
 } irq_guest_action_t;
 
@@ -850,7 +846,7 @@ static void __do_IRQ_guest(int irq)
 
     if ( already_pending == action->nr_guests )
     {
-        stop_timer(&irq_guest_eoi_timer[irq]);
+        stop_timer(&action->eoi_timer);
         desc->handler->disable(irq);
         desc->status |= IRQ_GUEST_EOI_PENDING;
         for ( i = 0; i < already_pending; ++i )
@@ -866,9 +862,8 @@ static void __do_IRQ_guest(int irq)
              * - skip the timer setup below.
              */
         }
-        init_timer(&irq_guest_eoi_timer[irq],
-                   irq_guest_eoi_timer_fn, desc, smp_processor_id());
-        set_timer(&irq_guest_eoi_timer[irq], NOW() + MILLISECS(1));
+        migrate_timer(&action->eoi_timer, smp_processor_id());
+        set_timer(&action->eoi_timer, NOW() + MILLISECS(1));
     }
 }
 
@@ -979,7 +974,7 @@ static void __pirq_guest_eoi(struct domain *d, int pirq)
     if ( action->ack_type == ACKTYPE_NONE )
     {
         ASSERT(!test_bit(pirq, d->pirq_mask));
-        stop_timer(&irq_guest_eoi_timer[irq]);
+        stop_timer(&action->eoi_timer);
         _irq_guest_eoi(desc);
     }
 
@@ -1163,6 +1158,7 @@ int pirq_guest_bind(struct vcpu *v, int pirq, int will_share)
         action->shareable   = will_share;
         action->ack_type    = pirq_acktype(v->domain, pirq);
         cpus_clear(action->cpu_eoi_map);
+        init_timer(&action->eoi_timer, irq_guest_eoi_timer_fn, desc, 0);
 
         desc->depth = 0;
         desc->status |= IRQ_GUEST;
@@ -1267,7 +1263,7 @@ static irq_guest_action_t *__pirq_guest_unbind(
         }
         break;
     case ACKTYPE_NONE:
-        stop_timer(&irq_guest_eoi_timer[irq]);
+        stop_timer(&action->eoi_timer);
         _irq_guest_eoi(desc);
         break;
     }
@@ -1309,7 +1305,7 @@ static irq_guest_action_t *__pirq_guest_unbind(
     desc->action = NULL;
     desc->status &= ~IRQ_GUEST;
     desc->status &= ~IRQ_INPROGRESS;
-    kill_timer(&irq_guest_eoi_timer[irq]);
+    kill_timer(&action->eoi_timer);
     desc->handler->shutdown(irq);
 
     /* Caller frees the old guest descriptor block. */
index 32f761f2f3a7abc1fa3642907363054fb6b2c77e..f8d8a25346da2be82dc5521eb25903c676fe87a6 100644 (file)
@@ -847,12 +847,7 @@ long do_vcpu_op(int cmd, int vcpuid, XEN_GUEST_HANDLE(void) arg)
              (set.timeout_abs_ns < NOW()) )
             return -ETIME;
 
-        if ( v->singleshot_timer.cpu != smp_processor_id() )
-        {
-            stop_timer(&v->singleshot_timer);
-            v->singleshot_timer.cpu = smp_processor_id();
-        }
-
+        migrate_timer(&v->singleshot_timer, smp_processor_id());
         set_timer(&v->singleshot_timer, set.timeout_abs_ns);
 
         break;
index 60e630dc49931aa5a90498ed2d8f4996c0f337fd..01651b3d3b6ef0f0ac28d82286ed67e6852aa1d0 100644 (file)
@@ -475,18 +475,6 @@ int cpu_disable_scheduler(unsigned int cpu)
                 cpus_setall(v->cpu_affinity);
             }
 
-            /*
-             * Migrate single-shot timers to CPU0. A new cpu will automatically
-             * be chosen when the timer is next re-set.
-             */
-            if ( v->singleshot_timer.cpu == cpu )
-            {
-                int cpu_mig = first_cpu(c->cpu_valid);
-                if ( cpu_mig == cpu )
-                    cpu_mig = next_cpu(cpu_mig, c->cpu_valid);
-                migrate_timer(&v->singleshot_timer, cpu_mig);
-            }
-
             if ( v->processor == cpu )
             {
                 set_bit(_VPF_migrating, &v->pause_flags);
@@ -804,12 +792,7 @@ long do_set_timer_op(s_time_t timeout)
     }
     else
     {
-        if ( v->singleshot_timer.cpu != smp_processor_id() )
-        {
-            stop_timer(&v->singleshot_timer);
-            v->singleshot_timer.cpu = smp_processor_id();
-        }
-
+        migrate_timer(&v->singleshot_timer, smp_processor_id());
         set_timer(&v->singleshot_timer, timeout);
     }
 
@@ -890,8 +873,6 @@ static void vcpu_periodic_timer_work(struct vcpu *v)
     s_time_t now = NOW();
     uint64_t periodic_next_event;
 
-    ASSERT(!active_timer(&v->periodic_timer));
-
     if ( v->periodic_period == 0 )
         return;
 
@@ -904,7 +885,7 @@ static void vcpu_periodic_timer_work(struct vcpu *v)
         periodic_next_event = now + v->periodic_period;
     }
 
-    v->periodic_timer.cpu = smp_processor_id();
+    migrate_timer(&v->periodic_timer, smp_processor_id());
     set_timer(&v->periodic_timer, periodic_next_event);
 }
 
index 68b56a6805ba29c4f6f3bb7e54dc734e2f92e5bc..deb3a1ac11045a9135ed005e5a03bc9f760c6b7c 100644 (file)
@@ -35,6 +35,7 @@ struct timers {
     struct timer **heap;
     struct timer  *list;
     struct timer  *running;
+    struct list_head inactive;
 } __cacheline_aligned;
 
 static DEFINE_PER_CPU(struct timers, timers);
@@ -169,8 +170,9 @@ static int add_to_list(struct timer **pprev, struct timer *t)
  * TIMER OPERATIONS.
  */
 
-static int remove_entry(struct timers *timers, struct timer *t)
+static int remove_entry(struct timer *t)
 {
+    struct timers *timers = &per_cpu(timers, t->cpu);
     int rc;
 
     switch ( t->status )
@@ -186,15 +188,16 @@ static int remove_entry(struct timers *timers, struct timer *t)
         BUG();
     }
 
-    t->status = TIMER_STATUS_inactive;
+    t->status = TIMER_STATUS_invalid;
     return rc;
 }
 
-static int add_entry(struct timers *timers, struct timer *t)
+static int add_entry(struct timer *t)
 {
+    struct timers *timers = &per_cpu(timers, t->cpu);
     int rc;
 
-    ASSERT(t->status == TIMER_STATUS_inactive);
+    ASSERT(t->status == TIMER_STATUS_invalid);
 
     /* Try to add to heap. t->heap_offset indicates whether we succeed. */
     t->heap_offset = 0;
@@ -209,18 +212,23 @@ static int add_entry(struct timers *timers, struct timer *t)
     return add_to_list(&timers->list, t);
 }
 
-static inline void __add_timer(struct timer *timer)
+static inline void activate_timer(struct timer *timer)
 {
-    int cpu = timer->cpu;
-    if ( add_entry(&per_cpu(timers, cpu), timer) )
-        cpu_raise_softirq(cpu, TIMER_SOFTIRQ);
+    ASSERT(timer->status == TIMER_STATUS_inactive);
+    timer->status = TIMER_STATUS_invalid;
+    list_del(&timer->inactive);
+
+    if ( add_entry(timer) )
+        cpu_raise_softirq(timer->cpu, TIMER_SOFTIRQ);
 }
 
-static inline void __stop_timer(struct timer *timer)
+static inline void deactivate_timer(struct timer *timer)
 {
-    int cpu = timer->cpu;
-    if ( remove_entry(&per_cpu(timers, cpu), timer) )
-        cpu_raise_softirq(cpu, TIMER_SOFTIRQ);
+    if ( remove_entry(timer) )
+        cpu_raise_softirq(timer->cpu, TIMER_SOFTIRQ);
+
+    timer->status = TIMER_STATUS_inactive;
+    list_add(&timer->inactive, &per_cpu(timers, timer->cpu).inactive);
 }
 
 static inline void timer_lock(struct timer *timer)
@@ -253,6 +261,32 @@ static inline void timer_unlock(struct timer *timer)
     do { timer_unlock(t); local_irq_restore(flags); } while ( 0 )
 
 
+static bool_t active_timer(struct timer *timer)
+{
+    ASSERT(timer->status >= TIMER_STATUS_inactive);
+    ASSERT(timer->status <= TIMER_STATUS_in_list);
+    return (timer->status >= TIMER_STATUS_in_heap);
+}
+
+
+void init_timer(
+    struct timer *timer,
+    void        (*function)(void *),
+    void         *data,
+    unsigned int  cpu)
+{
+    unsigned long flags;
+    memset(timer, 0, sizeof(*timer));
+    timer->function = function;
+    timer->data = data;
+    timer->cpu = cpu;
+    timer->status = TIMER_STATUS_inactive;
+    timer_lock_irqsave(timer, flags);
+    list_add(&timer->inactive, &per_cpu(timers, cpu).inactive);
+    timer_unlock_irqrestore(timer, flags);
+}
+
+
 void set_timer(struct timer *timer, s_time_t expires)
 {
     unsigned long flags;
@@ -260,13 +294,13 @@ void set_timer(struct timer *timer, s_time_t expires)
     timer_lock_irqsave(timer, flags);
 
     if ( active_timer(timer) )
-        __stop_timer(timer);
+        deactivate_timer(timer);
 
     timer->expires = expires;
     timer->expires_end = expires + timer_slop;
 
     if ( likely(timer->status != TIMER_STATUS_killed) )
-        __add_timer(timer);
+        activate_timer(timer);
 
     timer_unlock_irqrestore(timer, flags);
 }
@@ -279,7 +313,7 @@ void stop_timer(struct timer *timer)
     timer_lock_irqsave(timer, flags);
 
     if ( active_timer(timer) )
-        __stop_timer(timer);
+        deactivate_timer(timer);
 
     timer_unlock_irqrestore(timer, flags);
 }
@@ -287,7 +321,8 @@ void stop_timer(struct timer *timer)
 
 void migrate_timer(struct timer *timer, unsigned int new_cpu)
 {
-    int           old_cpu;
+    int old_cpu;
+    bool_t active;
     unsigned long flags;
 
     for ( ; ; )
@@ -313,16 +348,16 @@ void migrate_timer(struct timer *timer, unsigned int new_cpu)
         spin_unlock_irqrestore(&per_cpu(timers, new_cpu).lock, flags);
     }
 
-    if ( active_timer(timer) )
-    {
-        __stop_timer(timer);
-        timer->cpu = new_cpu;
-        __add_timer(timer);
-    }
-    else
-    {
-        timer->cpu = new_cpu;
-    }
+    active = active_timer(timer);
+    if ( active )
+        deactivate_timer(timer);
+
+    list_del(&timer->inactive);
+    timer->cpu = new_cpu;
+    list_add(&timer->inactive, &per_cpu(timers, new_cpu).inactive);
+
+    if ( active )
+        activate_timer(timer);
 
     spin_unlock(&per_cpu(timers, old_cpu).lock);
     spin_unlock_irqrestore(&per_cpu(timers, new_cpu).lock, flags);
@@ -339,7 +374,9 @@ void kill_timer(struct timer *timer)
     timer_lock_irqsave(timer, flags);
 
     if ( active_timer(timer) )
-        __stop_timer(timer);
+        deactivate_timer(timer);
+
+    list_del(&timer->inactive);
     timer->status = TIMER_STATUS_killed;
 
     timer_unlock_irqrestore(timer, flags);
@@ -355,6 +392,9 @@ static void execute_timer(struct timers *ts, struct timer *t)
     void (*fn)(void *) = t->function;
     void *data = t->data;
 
+    t->status = TIMER_STATUS_inactive;
+    list_add(&t->inactive, &ts->inactive);
+
     ts->running = t;
     spin_unlock_irq(&ts->lock);
     (*fn)(data);
@@ -401,7 +441,6 @@ static void timer_softirq_action(void)
             ((t = heap[1])->expires < now) )
     {
         remove_from_heap(heap, t);
-        t->status = TIMER_STATUS_inactive;
         execute_timer(ts, t);
     }
 
@@ -409,7 +448,6 @@ static void timer_softirq_action(void)
     while ( ((t = ts->list) != NULL) && (t->expires < now) )
     {
         ts->list = t->list_next;
-        t->status = TIMER_STATUS_inactive;
         execute_timer(ts, t);
     }
 
@@ -419,8 +457,8 @@ static void timer_softirq_action(void)
     while ( unlikely((t = next) != NULL) )
     {
         next = t->list_next;
-        t->status = TIMER_STATUS_inactive;
-        add_entry(ts, t);
+        t->status = TIMER_STATUS_invalid;
+        add_entry(t);
     }
 
     ts->overflow = (ts->list != NULL);
@@ -446,7 +484,7 @@ static void timer_softirq_action(void)
         while ( (GET_HEAP_SIZE(heap) != 0) &&
                 ((t = heap[1])->expires <= end) )
         {
-            remove_entry(ts, t);
+            remove_entry(t);
 
             t->status = TIMER_STATUS_in_list;
             t->list_next = NULL;
@@ -529,13 +567,23 @@ static void migrate_timers_from_cpu(unsigned int cpu)
 
     while ( (t = GET_HEAP_SIZE(ts->heap) ? ts->heap[1] : ts->list) != NULL )
     {
-        remove_entry(ts, t);
+        remove_entry(t);
         t->cpu = 0;
-        __add_timer(t);
+        add_entry(t);
+    }
+
+    while ( !list_empty(&ts->inactive) )
+    {
+        t = list_entry(ts->inactive.next, struct timer, inactive);
+        list_del(&t->inactive);
+        t->cpu = 0;
+        list_add(&t->inactive, &per_cpu(timers, 0).inactive);
     }
 
     spin_unlock(&ts->lock);
     spin_unlock_irq(&per_cpu(timers, 0).lock);
+
+    cpu_raise_softirq(0, TIMER_SOFTIRQ);
 }
 
 static struct timer *dummy_heap;
@@ -549,6 +597,7 @@ static int cpu_callback(
     switch ( action )
     {
     case CPU_UP_PREPARE:
+        INIT_LIST_HEAD(&ts->inactive);
         spin_lock_init(&ts->lock);
         ts->heap = &dummy_heap;
         break;
index f5340d07b75d46f7e0d283bd863a74598a70f528..53b3c3cc17e560d2156adbd81965655245147c1c 100644 (file)
@@ -11,6 +11,7 @@
 #include <xen/spinlock.h>
 #include <xen/time.h>
 #include <xen/string.h>
+#include <xen/list.h>
 
 struct timer {
     /* System time expiry value (nanoseconds since boot). */
@@ -19,10 +20,12 @@ struct timer {
 
     /* Position in active-timer data structure. */
     union {
-        /* Timer-heap offset. */
+        /* Timer-heap offset (TIMER_STATUS_in_heap). */
         unsigned int heap_offset;
-        /* Linked list. */
+        /* Linked list (TIMER_STATUS_in_list). */
         struct timer *list_next;
+        /* Linked list of inactive timers (TIMER_STATUS_inactive). */
+        struct list_head inactive;
     };
 
     /* On expiry, '(*function)(data)' will be executed in softirq context. */
@@ -33,10 +36,11 @@ struct timer {
     uint16_t cpu;
 
     /* Timer status. */
-#define TIMER_STATUS_inactive 0 /* Not in use; can be activated.    */
-#define TIMER_STATUS_killed   1 /* Not in use; canot be activated.  */
-#define TIMER_STATUS_in_heap  2 /* In use; on timer heap.           */
-#define TIMER_STATUS_in_list  3 /* In use; on overflow linked list. */
+#define TIMER_STATUS_invalid  0 /* Should never see this.           */
+#define TIMER_STATUS_inactive 1 /* Not in use; can be activated.    */
+#define TIMER_STATUS_killed   2 /* Not in use; cannot be activated. */
+#define TIMER_STATUS_in_heap  3 /* In use; on timer heap.           */
+#define TIMER_STATUS_in_list  4 /* In use; on overflow linked list. */
     uint8_t status;
 };
 
@@ -44,68 +48,39 @@ struct timer {
  * All functions below can be called for any CPU from any CPU in any context.
  */
 
-/*
- * Returns TRUE if the given timer is on a timer list.
- * The timer must *previously* have been initialised by init_timer(), or its
- * structure initialised to all-zeroes.
- */
-static inline int active_timer(struct timer *timer)
-{
-    return (timer->status >= TIMER_STATUS_in_heap);
-}
-
 /*
  * Initialise a timer structure with an initial callback CPU, callback
- * function and callback data pointer. This function may be called at any
- * time (and multiple times) on an inactive timer. It must *never* execute
- * concurrently with any other operation on the same timer.
+ * function and callback data pointer. This function must only be called on
+ * a brand new timer, or a killed timer. It must *never* execute concurrently
+ * with any other operation on the same timer.
  */
-static inline void init_timer(
+void init_timer(
     struct timer *timer,
-    void           (*function)(void *),
-    void            *data,
-    unsigned int     cpu)
-{
-    memset(timer, 0, sizeof(*timer));
-    timer->function = function;
-    timer->data     = data;
-    timer->cpu      = cpu;
-}
+    void        (*function)(void *),
+    void         *data,
+    unsigned int  cpu);
 
-/*
- * Set the expiry time and activate a timer. The timer must *previously* have
- * been initialised by init_timer() (so that callback details are known).
- */
-extern void set_timer(struct timer *timer, s_time_t expires);
+/* Set the expiry time and activate a timer. */
+void set_timer(struct timer *timer, s_time_t expires);
 
 /*
  * Deactivate a timer This function has no effect if the timer is not currently
  * active.
- * The timer must *previously* have been initialised by init_timer(), or its
- * structure initialised to all zeroes.
  */
-extern void stop_timer(struct timer *timer);
+void stop_timer(struct timer *timer);
 
-/*
- * Migrate a timer to a different CPU. The timer may be currently active.
- * The timer must *previously* have been initialised by init_timer(), or its
- * structure initialised to all zeroes.
- */
-extern void migrate_timer(struct timer *timer, unsigned int new_cpu);
+/* Migrate a timer to a different CPU. The timer may be currently active. */
+void migrate_timer(struct timer *timer, unsigned int new_cpu);
 
 /*
  * Deactivate a timer and prevent it from being re-set (future calls to
  * set_timer will silently fail). When this function returns it is guaranteed
  * that the timer callback handler is not running on any CPU.
- * The timer must *previously* have been initialised by init_timer(), or its
- * structure initialised to all zeroes.
  */
-extern void kill_timer(struct timer *timer);
+void kill_timer(struct timer *timer);
 
-/*
- * Bootstrap initialisation. Must be called before any other timer function.
- */
-extern void timer_init(void);
+/* Bootstrap initialisation. Must be called before any other timer function. */
+void timer_init(void);
 
 /*
  * Next timer deadline for each CPU.
@@ -115,10 +90,10 @@ DECLARE_PER_CPU(s_time_t, timer_deadline_start);
 DECLARE_PER_CPU(s_time_t, timer_deadline_end);
 
 /* Arch-defined function to reprogram timer hardware for new deadline. */
-extern int reprogram_timer(s_time_t timeout);
+int reprogram_timer(s_time_t timeout);
 
-/* calculate the aligned first tick time for a given periodic timer */ 
-extern s_time_t align_timer(s_time_t firsttick, uint64_t period);
+/* Calculate the aligned first tick time for a given periodic timer. */
+s_time_t align_timer(s_time_t firsttick, uint64_t period);
 
 #endif /* _TIMER_H_ */