]> nv-tegra.nvidia Code Review - linux-3.10.git/blobdiff - kernel/time/tick-broadcast.c
Merge branch 'timers-nohz-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-3.10.git] / kernel / time / tick-broadcast.c
index 005c0ca81a32bced4b65a491a6964f2182e30be2..206bbfb34e091d21f5928a846f4e1a53ab984663 100644 (file)
@@ -66,15 +66,30 @@ static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  */
 int tick_check_broadcast_device(struct clock_event_device *dev)
 {
-       if ((tick_broadcast_device.evtdev &&
+       struct clock_event_device *cur = tick_broadcast_device.evtdev;
+
+       if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
+           (tick_broadcast_device.evtdev &&
             tick_broadcast_device.evtdev->rating >= dev->rating) ||
             (dev->features & CLOCK_EVT_FEAT_C3STOP))
                return 0;
 
        clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
+       if (cur)
+               cur->event_handler = clockevents_handle_noop;
        tick_broadcast_device.evtdev = dev;
        if (!cpumask_empty(tick_broadcast_mask))
                tick_broadcast_start_periodic(dev);
+       /*
+        * Inform all cpus about this. We might be in a situation
+        * where we did not switch to oneshot mode because the per cpu
+        * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
+        * of a oneshot capable broadcast device. Without that
+        * notification the systems stays stuck in periodic mode
+        * forever.
+        */
+       if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
+               tick_clock_notify();
        return 1;
 }
 
@@ -393,6 +408,7 @@ int tick_resume_broadcast(void)
 
 static cpumask_var_t tick_broadcast_oneshot_mask;
 static cpumask_var_t tick_broadcast_pending_mask;
+static cpumask_var_t tick_broadcast_force_mask;
 
 /*
  * Exposed for debugging: see timer_list.c
@@ -402,6 +418,18 @@ struct cpumask *tick_get_broadcast_oneshot_mask(void)
        return tick_broadcast_oneshot_mask;
 }
 
+/*
+ * Called before going idle with interrupts disabled. Checks whether a
+ * broadcast event from the other core is about to happen. We detected
+ * that in tick_broadcast_oneshot_control(). The callsite can use this
+ * to avoid a deep idle transition as we are about to get the
+ * broadcast IPI right away.
+ */
+int tick_check_broadcast_expired(void)
+{
+       return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
+}
+
 /*
  * Set broadcast interrupt affinity
  */
@@ -483,6 +511,10 @@ again:
                }
        }
 
+       /* Take care of enforced broadcast requests */
+       cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
+       cpumask_clear(tick_broadcast_force_mask);
+
        /*
         * Wakeup the cpus which have an expired event.
         */
@@ -518,6 +550,7 @@ void tick_broadcast_oneshot_control(unsigned long reason)
        struct clock_event_device *bc, *dev;
        struct tick_device *td;
        unsigned long flags;
+       ktime_t now;
        int cpu;
 
        /*
@@ -545,7 +578,16 @@ void tick_broadcast_oneshot_control(unsigned long reason)
                WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
                if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
                        clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
-                       if (dev->next_event.tv64 < bc->next_event.tv64)
+                       /*
+                        * We only reprogram the broadcast timer if we
+                        * did not mark ourself in the force mask and
+                        * if the cpu local event is earlier than the
+                        * broadcast event. If the current CPU is in
+                        * the force mask, then we are going to be
+                        * woken by the IPI right away.
+                        */
+                       if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
+                           dev->next_event.tv64 < bc->next_event.tv64)
                                tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
                }
        } else {
@@ -566,6 +608,47 @@ void tick_broadcast_oneshot_control(unsigned long reason)
                                       tick_broadcast_pending_mask))
                                goto out;
 
+                       /*
+                        * If the pending bit is not set, then we are
+                        * either the CPU handling the broadcast
+                        * interrupt or we got woken by something else.
+                        *
+                        * We are not longer in the broadcast mask, so
+                        * if the cpu local expiry time is already
+                        * reached, we would reprogram the cpu local
+                        * timer with an already expired event.
+                        *
+                        * This can lead to a ping-pong when we return
+                        * to idle and therefor rearm the broadcast
+                        * timer before the cpu local timer was able
+                        * to fire. This happens because the forced
+                        * reprogramming makes sure that the event
+                        * will happen in the future and depending on
+                        * the min_delta setting this might be far
+                        * enough out that the ping-pong starts.
+                        *
+                        * If the cpu local next_event has expired
+                        * then we know that the broadcast timer
+                        * next_event has expired as well and
+                        * broadcast is about to be handled. So we
+                        * avoid reprogramming and enforce that the
+                        * broadcast handler, which did not run yet,
+                        * will invoke the cpu local handler.
+                        *
+                        * We cannot call the handler directly from
+                        * here, because we might be in a NOHZ phase
+                        * and we did not go through the irq_enter()
+                        * nohz fixups.
+                        */
+                       now = ktime_get();
+                       if (dev->next_event.tv64 <= now.tv64) {
+                               cpumask_set_cpu(cpu, tick_broadcast_force_mask);
+                               goto out;
+                       }
+                       /*
+                        * We got woken by something else. Reprogram
+                        * the cpu local timer device.
+                        */
                        tick_program_event(dev->next_event, 1);
                }
        }
@@ -610,7 +693,8 @@ void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
                bc->event_handler = tick_handle_oneshot_broadcast;
 
                /* Take the do_timer update */
-               tick_do_timer_cpu = cpu;
+               if (!tick_nohz_full_cpu(cpu))
+                       tick_do_timer_cpu = cpu;
 
                /*
                 * We must be careful here. There might be other CPUs
@@ -707,5 +791,6 @@ void __init tick_broadcast_init(void)
 #ifdef CONFIG_TICK_ONESHOT
        alloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
        alloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
+       alloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
 #endif
 }