On Fri, 20 Nov 2015, Peter Zijlstra wrote:
> On Fri, Nov 20, 2015 at 11:58:12AM +0100, Thomas Gleixner wrote:
> > That's not what I meant. If you don't want to control all that from
> > the scheduler than you are back to that thread which "runs" at RT
> > priority and does
> > 
> >      if (machine_on_fire) {
> >             defer_timer_interrupt(5ms);
> >             end = now + 5ms:
> >             while (now < end)
> >                   mwait();
> >          }
> > 
> > That's what the existing code does, but the above does not longer
> > claim it's idle and confuses the hell out of nohz and whatever.  It's
> > just a "runaway" RT task which "hogs" the CPU for 5ms and makes the
> > next timer interrupt firing late.
> 
> Right; so the naive way of implementing that is by simply programing the
> timer hardware 5ms in the future and leaving it at that.
> 
> The problem with that would be a device interrupt happening and mucking
> with timers, this would result in the timer hardware being reprogrammed
> to a 'sane' value. I see two solutions for that:
> 
>  - add another check in tick_program_event(); or,
> 
>  - muck about with the evtdev pointer, such that we (temporarily) neuter
>    its clock_event_device::set_next_*() methods.
> 
> The later is fugly, but avoids any runtime overhead.

Yes, it's fugly, but it does not touch any of the states.
 
> This all makes the idle-injection muck hard depend on hres_active, but I
> think that's a sane constraint anyway.

It makes it actually depend on NO_HZ || HRES. It comes with a few
other restrictions as well: I'm not going to support that for TSCs
which stop in C3. It's ugly enough already and I really don't want to
muck with the broadcast device.

One other thing is that the caller has to ensure that the unblock()
happens in a timely manner. I really would recommend to just disable
preemption around that machine on fire wait loop and screw the RT
tasks. We screw them anyway when they depend on a timer.

Untested patch below.

Thanks,

        tglx

8<----------------
Subject: tick/nohz: Allow blocking the tick to prevent fire
From: Thomas Gleixner <[email protected]>
Date: Fri, 20 Nov 2015 14:28:17 +0100

Signed-off-by: Thomas Gleixner <[email protected]>
---
 include/linux/tick.h     |    5 ++
 kernel/time/Kconfig      |    5 ++
 kernel/time/tick-sched.c |   99 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/time/tick-sched.h |    7 ++-
 4 files changed, 114 insertions(+), 2 deletions(-)

Index: tip/include/linux/tick.h
===================================================================
--- tip.orig/include/linux/tick.h
+++ tip/include/linux/tick.h
@@ -203,4 +203,9 @@ static inline void tick_nohz_task_switch
                __tick_nohz_task_switch();
 }
 
+#ifdef CONFIG_TICK_THROTTLING
+int tick_nohz_block_tick(u64 delay);
+void tick_nohz_unblock_tick(void);
+#endif
+
 #endif
Index: tip/kernel/time/Kconfig
===================================================================
--- tip.orig/kernel/time/Kconfig
+++ tip/kernel/time/Kconfig
@@ -60,6 +60,11 @@ menu "Timers subsystem"
 config TICK_ONESHOT
        bool
 
+# Special functions for tick throttling to avoid fire extinguishers
+config TICK_THROTTLING
+       bool
+       depends on TICK_ONESHOT
+
 config NO_HZ_COMMON
        bool
        depends on !ARCH_USES_GETTIMEOFFSET && GENERIC_CLOCKEVENTS
Index: tip/kernel/time/tick-sched.c
===================================================================
--- tip.orig/kernel/time/tick-sched.c
+++ tip/kernel/time/tick-sched.c
@@ -1119,6 +1119,105 @@ void tick_setup_sched_timer(void)
 }
 #endif /* HIGH_RES_TIMERS */
 
+#ifdef CONFIG_TICK_THROTTLING
+/*
+ * An interrupt might have been pending already, when we programmed
+ * the throttler time. Nothing to do here. The device is armed and the
+ * interrupt will fire again. If this is the real wakeup event then
+ * the unblock call will retrigger it.
+ */
+static void tick_throttling_handler(struct clock_event_device *dev)
+{
+}
+
+static int tick_throttling_noop(unsigned long evt,
+                               struct clock_event_device *d)
+{
+       return 0;
+}
+
+static struct clock_event_device tick_throttler = {
+       .name           = "throttler",
+       .features       = CLOCK_EVT_FEAT_ONESHOT,
+       .event_handler  = tick_throttling_handler,
+       .set_next_event = tick_throttling_noop,
+       .mult           = 1,
+       .shift          = 0,
+       .irq            = -1,
+};
+
+/**
+ * tick_nohz_block_tick - Force block the tick to prevent fire
+ * @delay:     Defer the tick for X nano seconds
+ *
+ * This is a special interface for thermal emergencies. Do not use
+ * otherwise!  Make sure to call tick_nohz_block_tick() right after
+ * the delay ends to undo the damage.
+ */
+int tick_nohz_block_tick(u64 delay)
+{
+       struct tick_device *td;
+       unsigned long flags;
+       int ret = -EBUSY;
+       ktime_t until;
+
+       if (!tick_nohz_active)
+               return -EBUSY;
+
+       local_irq_save(flags);
+       td = this_cpu_ptr(&tick_cpu_device);
+
+       /* No way to do that with broadcast */
+       if (td->evtdev->features & CLOCK_EVT_FEAT_C3STOP)
+               goto out;
+
+       /* Yes, I do not trust people! */
+       if (WARN_ON_ONCE(td->evtdev == &tick_throttler))
+               goto out;
+
+       if (delay > td->evtdev->max_delta_ticks) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       until = ktime_add_ns(ktime_get(), delay);
+       if (!tick_program_event(until, 0)) {
+               td->real_evtdev = td->evtdev;
+               td->evtdev = &tick_throttler;
+               ret = 0;
+       }
+out:
+       local_irq_restore(flags);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(tick_nohz_block_tick);
+
+/**
+ * tick_nohz_force_unblock_tick - Undo the force blocking of the tick
+ *
+ * Pairs with tick_nohz_block_tick(). Can be called unconditionally
+ * even if the tick was not blocked by tick_nohz_block_tick().
+ */
+void tick_nohz_unblock_tick(void)
+{
+       struct tick_device *td;
+       unsigned long flags;
+
+       if (!tick_nohz_active)
+               return;
+
+       local_irq_save(flags);
+       td = this_cpu_ptr(&tick_cpu_device);
+       if (td->evtdev == &tick_throttler) {
+               td->evtdev = td->real_evtdev;
+               /* Force a timer interrupt now */
+               tick_program_event(ktime_get(), 1);
+       }
+       local_irq_restore(flags);
+}
+EXPORT_SYMBOL_GPL(tick_nohz_unblock_tick);
+#endif /* TICK_THROTTLING */
+
 #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
 void tick_cancel_sched_timer(int cpu)
 {
Index: tip/kernel/time/tick-sched.h
===================================================================
--- tip.orig/kernel/time/tick-sched.h
+++ tip/kernel/time/tick-sched.h
@@ -9,8 +9,11 @@ enum tick_device_mode {
 };
 
 struct tick_device {
-       struct clock_event_device *evtdev;
-       enum tick_device_mode mode;
+       struct clock_event_device       *evtdev;
+       enum tick_device_mode           mode;
+#ifdef CONFIG_TICK_THROTTLING
+       struct clock_event_device       *real_evtdev;
+#endif
 };
 
 enum tick_nohz_mode {




--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to