Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760049AbbKTSyj (ORCPT ); Fri, 20 Nov 2015 13:54:39 -0500 Received: from www.linutronix.de ([62.245.132.108]:52719 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751471AbbKTSyh (ORCPT ); Fri, 20 Nov 2015 13:54:37 -0500 Date: Fri, 20 Nov 2015 19:53:45 +0100 (CET) From: Thomas Gleixner To: Peter Zijlstra cc: Morten Rasmussen , Arjan van de Ven , Jacob Pan , Ingo Molnar , John Stultz , LKML , Srinivas Pandruvada , Len Brown , Rafael Wysocki , Eduardo Valentin , Paul Turner Subject: Re: [PATCH 3/4] sched: introduce synchronized idle injection In-Reply-To: <20151120125441.GD17308@twins.programming.kicks-ass.net> Message-ID: References: <1447444387-23525-1-git-send-email-jacob.jun.pan@linux.intel.com> <1447444387-23525-4-git-send-email-jacob.jun.pan@linux.intel.com> <20151118154457.GD30184@e105550-lin.cambridge.arm.com> <564C9E93.8030901@linux.intel.com> <20151119172405.GA798@e105550-lin.cambridge.arm.com> <20151119200906.GS3816@twins.programming.kicks-ass.net> <20151120102014.GC17308@twins.programming.kicks-ass.net> <20151120125441.GD17308@twins.programming.kicks-ass.net> User-Agent: Alpine 2.11 (DEB 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001,URIBL_BLOCKED=0.001 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 6892 Lines: 233 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 Date: Fri, 20 Nov 2015 14:28:17 +0100 Signed-off-by: Thomas Gleixner --- 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 majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/