Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
On Fri, Nov 30, 2012 at 10:38 AM, Frederic Weisbecker wrote: > 2012/11/28 Hakan Akkan : >> +static int check_drop_timer_duty(int cpu) >> +{ >> + int curr_handler, prev_handler, new_handler; >> + int nrepeat = -1; >> + bool drop_recheck; >> + >> +repeat: >> + WARN_ON_ONCE(++nrepeat > 1); >> + drop_recheck = false; >> + curr_handler = cpu; >> + new_handler = TICK_DO_TIMER_NONE; >> + >> +#ifdef CONFIG_CPUSETS_NO_HZ >> + if (atomic_read(&nr_cpus_user_nohz) > 0) { > > Note atomic_read() is not SMP ordered. If another CPU does > atomic_add() or atomic_add_return(), you may not see the new value as > expected with atomic_read(). Doing atomic_add_return(0, &atomic_thing) > returns the fully ordered result. That code doesn't necessarily try to achieve a prefect ordering. I wasn't very sure if we want the overhead of atomic_add_return(0, ...) in such a hot path so this part of the function is racy. Nevertheless, it makes sure that someone gets the duty. #ifdef CONFIG_CPUSETS_NO_HZ if (atomic_read(&nr_cpus_user_nohz) > 0) { The CPU who updated nr_cpus_user_nohz will certainly see the new value and claim the duty if someone else hasn't yet. Another regular CPU will eventually see the new value and assign itself to the duty. So we can tolerate some race here for the sake of avoiding heavier locks. > > You also need to do that to ensure full ordering against > tick_cpu_sched.user_nohz. > > On the current layout we have: > > (Write side) (Read side) > > ts->user_nohz = 1; > atomic_inc(&nr_cpus_user_nohz) > > if > (atomic_read(&nr_cpus_user_nohz)) > if > (per_cpu(tick_cpu_sched, curr_handler).user_nohz) > > > If you want to make sure that you see the expected value on user_nohz > from the read side, you need to correctly order the write and read > against nr_cpus_user_nohz. Again, perfect ordering is not critical here. In the worst case we miss that CPUs ts->user_nohz = 1 update and that adaptive nohz CPU will keep the duty for a short while until someone else takes it. Trying to avoid this would require more locking instructions like you suggest below. If we really want to fully isolate nohz CPUs and avoid "being stuck with jiffies update until someone else takes it away" situations, we can have a cpumask denoting idle and/or non-adaptive-nohz CPUs and dump the duty once we discover that we're "stuck". > > For this you can use atomic_add_return() which implies the full barrier: > > > (Write side) (Read side) > > ts->user_nohz = 1; > atomic_inc_return(&nr_cpus_user_nohz) > > if > (atomic_add_return(0, &nr_cpus_user_nohz)) > if > (per_cpu(tick_cpu_sched, curr_handler).user_nohz) > > > I have much more comments on this patch, I will come back on this soon. > > Thanks. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
2012/11/28 Hakan Akkan : > +static int check_drop_timer_duty(int cpu) > +{ > + int curr_handler, prev_handler, new_handler; > + int nrepeat = -1; > + bool drop_recheck; > + > +repeat: > + WARN_ON_ONCE(++nrepeat > 1); > + drop_recheck = false; > + curr_handler = cpu; > + new_handler = TICK_DO_TIMER_NONE; > + > +#ifdef CONFIG_CPUSETS_NO_HZ > + if (atomic_read(&nr_cpus_user_nohz) > 0) { Note atomic_read() is not SMP ordered. If another CPU does atomic_add() or atomic_add_return(), you may not see the new value as expected with atomic_read(). Doing atomic_add_return(0, &atomic_thing) returns the fully ordered result. You also need to do that to ensure full ordering against tick_cpu_sched.user_nohz. On the current layout we have: (Write side) (Read side) ts->user_nohz = 1; atomic_inc(&nr_cpus_user_nohz) if (atomic_read(&nr_cpus_user_nohz)) if (per_cpu(tick_cpu_sched, curr_handler).user_nohz) If you want to make sure that you see the expected value on user_nohz from the read side, you need to correctly order the write and read against nr_cpus_user_nohz. For this you can use atomic_add_return() which implies the full barrier: (Write side) (Read side) ts->user_nohz = 1; atomic_inc_return(&nr_cpus_user_nohz) if (atomic_add_return(0, &nr_cpus_user_nohz)) if (per_cpu(tick_cpu_sched, curr_handler).user_nohz) I have much more comments on this patch, I will come back on this soon. Thanks. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
(Please ignore the previous patch as it never really prevented the last idler from going longer than timekeeping_max_deferement sleeps.) An adaptive nohz (AHZ) CPU may not do do_timer() for a while despite being non-idle. When all other CPUs are idle, AHZ CPUs might be using stale jiffies values. To prevent this always keep a CPU with ticks if there is one or more AHZ CPUs. A new function, check_drop_timer_duty, handles the updates to tick_do_timer_cpu value and makes sure that the jiffies update is done when there are non-idle adaptive-nohz CPUs. Also added is a new field in struct tick_sched to indicate if CPU is ready to run something other than the idle task without ticks once it drops the do_timer() duty. This also facilitates the system-wide tick shut down when all CPUs, including AHZ CPUs, are idle. Signed-off-by: Hakan Akkan Cc: Frederic Weisbecker Cc: Thomas Gleixner Cc: Steven Rostedt Cc: Peter Zijlstra Cc: Ingo Molnar --- include/linux/tick.h |2 + kernel/time/tick-sched.c | 219 -- 2 files changed, 174 insertions(+), 47 deletions(-) diff --git a/include/linux/tick.h b/include/linux/tick.h index 93add37..0a65dfb 100644 --- a/include/linux/tick.h +++ b/include/linux/tick.h @@ -54,6 +54,7 @@ enum tick_saved_jiffies { * @iowait_sleeptime: Sum of the time slept in idle with sched tick stopped, with IO outstanding * @sleep_length: Duration of the current idle sleep * @do_timer_lst: CPU was the last one doing do_timer before going idle + * @user_nohz: CPU wants to switch to adaptive nohz mode */ struct tick_sched { struct hrtimer sched_timer; @@ -77,6 +78,7 @@ struct tick_sched { unsigned long next_jiffies; ktime_t idle_expires; int do_timer_last; + int user_nohz; }; extern void __init tick_init(void); diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index bdc8aeb..f9a85e0 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -172,6 +172,115 @@ update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_upda } +#ifdef CONFIG_CPUSETS_NO_HZ +/* + * This defines the number of CPUs currently in (or wanting to + * be in) adaptive nohz mode. Greater than 0 means at least + * one CPU is ready to shut down its tick for non-idle purposes. + */ +static atomic_t __read_mostly nr_cpus_user_nohz = ATOMIC_INIT(0); + +static inline int update_do_timer_cpu(int current_handler, + int new_handler) +{ + return cmpxchg(&tick_do_timer_cpu, current_handler, new_handler); +} +#else +static inline int update_do_timer_cpu(int current_handler, + int new_handler) +{ + int tmp = ACCESS_ONCE(tick_do_timer_cpu); + tick_do_timer_cpu = new_handler; + return tmp; +} +#endif + +/* + * check_drop_timer_duty: Check if this cpu can shut down + * ticks without worrying about who is going to handle + * timekeeping. The duty is dropped here as well if possible. + * When there are adaptive nohz cpus in the system ready to + * run user tasks without ticks, this function makes sure + * that timekeeping is handled by a cpu. A non-adaptive-nohz + * cpu, if any, will claim the duty as soon as it discovers + * that some adaptive-nohz cpu is stuck with it. + * + * Returns the new value of tick_do_timer_cpu. + */ +static int check_drop_timer_duty(int cpu) +{ + int curr_handler, prev_handler, new_handler; + int nrepeat = -1; + bool drop_recheck; + +repeat: + WARN_ON_ONCE(++nrepeat > 1); + drop_recheck = false; + curr_handler = cpu; + new_handler = TICK_DO_TIMER_NONE; + +#ifdef CONFIG_CPUSETS_NO_HZ + if (atomic_read(&nr_cpus_user_nohz) > 0) { + curr_handler = ACCESS_ONCE(tick_do_timer_cpu); + /* +* Keep the duty until someone takes it away. +* FIXME: Make nr_cpus_user_nohz an atomic cpumask +* to find an idle CPU to dump the duty at. +*/ + if (curr_handler == cpu) + return cpu; + /* +* This cpu will try to take the duty if 1) there is +* no handler or 2) current handler seems to be an +* adaptive-nohz cpu. We take the duty from others +* only if the we are idle or not part of an +* adaptive-nohz cpuset. +* Once we take the duty, the check above ensures that +* we stick with it. +*/ + if (unlikely(curr_handler == TICK_DO_TIMER_NONE) + || (per_cpu(tick_cpu_sched, curr_handler).user_nohz + && (is_idle_task(current) + || !cpuset_cpu_adaptive_
[PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
An adaptive nohz (AHZ) CPU may not do do_timer() for a while despite being non-idle. When all other CPUs are idle, AHZ CPUs might be using stale jiffies values. To prevent this always keep a CPU with ticks if there is one or more AHZ CPUs. A new function, check_drop_timer_duty, handles the updates to tick_do_timer_cpu value and makes sure that the jiffies update is done when there are non-idle adaptive-nohz CPUs. Also added is a new field in struct tick_sched to indicate if CPU is ready to run something other than the idle task without ticks once it drops the do_timer() duty. This also facilitates the system-wide tick shut down when all CPUs, including AHZ CPUs, are idle. Signed-off-by: Hakan Akkan Cc: Frederic Weisbecker Cc: Thomas Gleixner Cc: Steven Rostedt Cc: Peter Zijlstra Cc: Ingo Molnar --- include/linux/tick.h |2 + kernel/time/tick-sched.c | 235 -- 2 files changed, 188 insertions(+), 49 deletions(-) diff --git a/include/linux/tick.h b/include/linux/tick.h index 93add37..0a65dfb 100644 --- a/include/linux/tick.h +++ b/include/linux/tick.h @@ -54,6 +54,7 @@ enum tick_saved_jiffies { * @iowait_sleeptime: Sum of the time slept in idle with sched tick stopped, with IO outstanding * @sleep_length: Duration of the current idle sleep * @do_timer_lst: CPU was the last one doing do_timer before going idle + * @user_nohz: CPU wants to switch to adaptive nohz mode */ struct tick_sched { struct hrtimer sched_timer; @@ -77,6 +78,7 @@ struct tick_sched { unsigned long next_jiffies; ktime_t idle_expires; int do_timer_last; + int user_nohz; }; extern void __init tick_init(void); diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index bdc8aeb..3ff9dc5 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -172,6 +172,130 @@ update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_upda } +#ifdef CONFIG_CPUSETS_NO_HZ +/* + * This defines the number of CPUs currently in (or wanting to + * be in) adaptive nohz mode. Greater than 0 means at least + * one CPU is ready to shut down its tick for non-idle purposes. + */ +static atomic_t __read_mostly nr_cpus_user_nohz = ATOMIC_INIT(0); + +static inline int update_do_timer_cpu(int current_handler, + int new_handler) +{ + return cmpxchg(&tick_do_timer_cpu, current_handler, new_handler); +} +#else +static inline int update_do_timer_cpu(int current_handler, + int new_handler) +{ + int tmp = ACCESS_ONCE(tick_do_timer_cpu); + tick_do_timer_cpu = new_handler; + return tmp; +} +#endif + +/* + * check_drop_timer_duty: Check if this cpu can shut down + * ticks without worrying about who is going to handle + * timekeeping. The duty is dropped here as well if possible. + * When there are adaptive nohz cpus in the system ready to + * run user tasks without ticks, this function makes sure + * that timekeeping is handled by a cpu. A non-adaptive-nohz + * cpu, if any, will claim the duty as soon as it discovers + * that some adaptive-nohz cpu is stuck with it. + * + * Returns + * 0 if cpu has to keep ticks on for timekeeping, + * 1 if cpu drops the duty inside this function and can shut + *down its ticks, + * 2 if cpu did not have the duty anyway. + */ +static int check_drop_timer_duty(int cpu) +{ + int curr_handler, prev_handler, new_handler; + int nrepeat = -1; + bool drop_recheck; + +repeat: + WARN_ON_ONCE(++nrepeat > 1); + drop_recheck = false; + curr_handler = cpu; + new_handler = TICK_DO_TIMER_NONE; + +#ifdef CONFIG_CPUSETS_NO_HZ + if (atomic_read(&nr_cpus_user_nohz) > 0) { + curr_handler = ACCESS_ONCE(tick_do_timer_cpu); + /* +* Keep the duty until someone takes it away. +* FIXME: Make nr_cpus_user_nohz an atomic cpumask +* to find an idle CPU to dump the duty at. +*/ + if (curr_handler == cpu) + return 0; + /* +* This cpu will try to take the duty if 1) there is +* no handler or 2) current handler seems to be an +* adaptive-nohz cpu. We take the duty from others +* only if the we are idle or not part of an +* adaptive-nohz cpuset. +* Once we take the duty, the check above ensures that +* we stick with it. +*/ + if (unlikely(curr_handler == TICK_DO_TIMER_NONE) + || (per_cpu(tick_cpu_sched, curr_handler).user_nohz + && (is_idle_task(current) + || !cpuset_cpu_ada
Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
2012/11/20 Steven Rostedt : > On Mon, 2012-11-19 at 17:27 -0700, Hakan Akkan wrote: > >> > >> > I suggest to rather define a tunable timekeeping duty CPU affinity in >> > a cpumask file at /sys/devices/system/cpu/timekeeping and a toggle at >> > /sys/devices/system/cpu/cpuX/timekeeping (like the online file). This >> > way the user can decide whether adaptive nohz CPU can handle >> > timekeeping or this must be forced to other CPUs in order to enforce >> > isolation. >> >> Well, users want tickless CPUs because they don't want timekeeping >> (or any other kernel activity for that matter) to run in there. So, I believe >> having that "timekeeping affinity" stay in the regular CPUs is good enough. >> Please let me know how users could utilize these control files to do anything >> other than keeping the timekeeping out of adaptive nohz CPUs. > > I agree. If we already have some /sys cpumask that denotes which CPUs > will be adaptive NO_HZ (or simply isolated) then just keep the tick from > ever going on those CPUs. If all but one CPU is set for nohz, and that > one CPU goes idle, it should still be the one doing the tick. If you want isolation on your full dynticks CPU it's right. Now you could have lower requirements, a different policy that rather enforce energy saving. But I realize we can integrate such granularity later if users request it and take the behaviour you both describe as the default for now. So let's take that direction. Thanks. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
On Mon, 2012-11-19 at 17:27 -0700, Hakan Akkan wrote: > > > > I suggest to rather define a tunable timekeeping duty CPU affinity in > > a cpumask file at /sys/devices/system/cpu/timekeeping and a toggle at > > /sys/devices/system/cpu/cpuX/timekeeping (like the online file). This > > way the user can decide whether adaptive nohz CPU can handle > > timekeeping or this must be forced to other CPUs in order to enforce > > isolation. > > Well, users want tickless CPUs because they don't want timekeeping > (or any other kernel activity for that matter) to run in there. So, I believe > having that "timekeeping affinity" stay in the regular CPUs is good enough. > Please let me know how users could utilize these control files to do anything > other than keeping the timekeeping out of adaptive nohz CPUs. I agree. If we already have some /sys cpumask that denotes which CPUs will be adaptive NO_HZ (or simply isolated) then just keep the tick from ever going on those CPUs. If all but one CPU is set for nohz, and that one CPU goes idle, it should still be the one doing the tick. -- Steve -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
On Mon, Nov 19, 2012 at 7:46 AM, Frederic Weisbecker wrote: > > Hi Hakan, > > As I start to focus on timekeeing for full dynticks, I'm looking at > your patch. Sorry I haven't yet replied with a serious review until > now. But here is it, finally: > > 2012/6/17 Hakan Akkan : > > An adaptive nohz (AHZ) CPU may not do do_timer() for a while > > despite being non-idle. When all other CPUs are idle, AHZ > > CPUs might be using stale jiffies values. To prevent this > > always keep a CPU with ticks if there is one or more AHZ > > CPUs. > > > > The patch changes can_stop_{idle,adaptive}_tick functions > > and prevents either the last CPU who did the do_timer() duty > > or the AHZ CPU itself from stopping its sched timer if there > > is one or more AHZ CPUs in the system. This means AHZ CPUs > > might keep the ticks running for short periods until a > > non-AHZ CPU takes the charge away in > > tick_do_timer_check_handler() function. When a non-AHZ CPU > > takes the charge, it never gives it away so that AHZ CPUs > > can run tickless. > > > > Signed-off-by: Hakan Akkan > > CC: Frederic Weisbecker > > --- > > include/linux/cpuset.h |3 ++- > > kernel/cpuset.c |5 + > > kernel/time/tick-sched.c | 31 ++- > > 3 files changed, 37 insertions(+), 2 deletions(-) > > > > diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h > > index ccbc2fd..19aa448 100644 > > --- a/include/linux/cpuset.h > > +++ b/include/linux/cpuset.h > > @@ -266,11 +266,12 @@ static inline bool cpuset_adaptive_nohz(void) > > > > extern void cpuset_exit_nohz_interrupt(void *unused); > > extern void cpuset_nohz_flush_cputimes(void); > > +extern bool nohz_cpu_exist(void); > > #else > > static inline bool cpuset_cpu_adaptive_nohz(int cpu) { return false; } > > static inline bool cpuset_adaptive_nohz(void) { return false; } > > static inline void cpuset_nohz_flush_cputimes(void) { } > > - > > +static inline bool nohz_cpu_exist(void) { return false; } > > #endif /* CONFIG_CPUSETS_NO_HZ */ > > > > #endif /* _LINUX_CPUSET_H */ > > diff --git a/kernel/cpuset.c b/kernel/cpuset.c > > index 858217b..ccbaac9 100644 > > --- a/kernel/cpuset.c > > +++ b/kernel/cpuset.c > > @@ -1231,6 +1231,11 @@ DEFINE_PER_CPU(int, cpu_adaptive_nohz_ref); > > > > static cpumask_t nohz_cpuset_mask; > > > > +inline bool nohz_cpu_exist(void) > > +{ > > + return !cpumask_empty(&nohz_cpuset_mask); > > +} > > + > > static void flush_cputime_interrupt(void *unused) > > { > > trace_printk("IPI: flush cputime\n"); > > diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c > > index bdc8aeb..e60d541 100644 > > --- a/kernel/time/tick-sched.c > > +++ b/kernel/time/tick-sched.c > > @@ -409,6 +409,25 @@ out: > > return ret; > > } > > > > +static inline bool must_take_timer_duty(int cpu) > > +{ > > + int handler = tick_do_timer_cpu; > > + bool ret = false; > > + bool tick_needed = nohz_cpu_exist(); > > Note this is racy because this fetches the value of the > nohz_cpuset_mask without locking. > We may "see" there is no nohz cpusets whereas we just set some of them > as nohz and they > could even have shut down their tick already. Yes, this is racy indeed. Thanks for pointing out. > > > > + > > + /* > > +* A CPU will have to take the timer duty if there is an adaptive > > +* nohz CPU in the system. The last handler == cpu check ensures > > +* that the last cpu that did the do_timer() sticks with the duty. > > +* A normal (non nohz) cpu will take the charge from a nohz cpu in > > +* tick_do_timer_check_handler anyway. > > +*/ > > + if (tick_needed && (handler == TICK_DO_TIMER_NONE || handler == > > cpu)) > > + ret = true; > > This check is also racy due to the lack of locking. The previous > handler may have set TICK_DO_TIMER_NONE and gone to sleep. We have no > guarantee that the CPU can see that new value. It could believe there > is still a handler. This needs at least cmpxchg() to make the test and > set atomic. > > > + > > + return ret; > > +} > > + > > static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) > > { > > /* > > @@ -421,6 +440,9 @@ static bool can_stop_idle_tick(int cpu, struct > > tick_sched *ts) > > if (unlikely(!cpu_online(cpu))) { > > if (cpu == tick_do_timer_cpu) > > tick_do_timer_cpu = TICK_DO_TIMER_NONE; > > + } else if (must_take_timer_duty(cpu)) { > > + tick_do_timer_cpu = cpu; > > + return false; > > } > > > > if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) > > @@ -512,6 +534,13 @@ void tick_nohz_idle_enter(void) > > #ifdef CONFIG_CPUSETS_NO_HZ > > static bool can_stop_adaptive_tick(void) > > { > > + int cpu = smp_processor_id(); > > + > > + if (must_take_timer_duty(cpu)) { > > + tick_do_timer_cpu = cpu; > > +
Re: [PATCH] nohz/cpuset: Make a CPU stick with do_timer() duty in the presence of nohz cpusets
Hi Hakan, As I start to focus on timekeeing for full dynticks, I'm looking at your patch. Sorry I haven't yet replied with a serious review until now. But here is it, finally: 2012/6/17 Hakan Akkan : > An adaptive nohz (AHZ) CPU may not do do_timer() for a while > despite being non-idle. When all other CPUs are idle, AHZ > CPUs might be using stale jiffies values. To prevent this > always keep a CPU with ticks if there is one or more AHZ > CPUs. > > The patch changes can_stop_{idle,adaptive}_tick functions > and prevents either the last CPU who did the do_timer() duty > or the AHZ CPU itself from stopping its sched timer if there > is one or more AHZ CPUs in the system. This means AHZ CPUs > might keep the ticks running for short periods until a > non-AHZ CPU takes the charge away in > tick_do_timer_check_handler() function. When a non-AHZ CPU > takes the charge, it never gives it away so that AHZ CPUs > can run tickless. > > Signed-off-by: Hakan Akkan > CC: Frederic Weisbecker > --- > include/linux/cpuset.h |3 ++- > kernel/cpuset.c |5 + > kernel/time/tick-sched.c | 31 ++- > 3 files changed, 37 insertions(+), 2 deletions(-) > > diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h > index ccbc2fd..19aa448 100644 > --- a/include/linux/cpuset.h > +++ b/include/linux/cpuset.h > @@ -266,11 +266,12 @@ static inline bool cpuset_adaptive_nohz(void) > > extern void cpuset_exit_nohz_interrupt(void *unused); > extern void cpuset_nohz_flush_cputimes(void); > +extern bool nohz_cpu_exist(void); > #else > static inline bool cpuset_cpu_adaptive_nohz(int cpu) { return false; } > static inline bool cpuset_adaptive_nohz(void) { return false; } > static inline void cpuset_nohz_flush_cputimes(void) { } > - > +static inline bool nohz_cpu_exist(void) { return false; } > #endif /* CONFIG_CPUSETS_NO_HZ */ > > #endif /* _LINUX_CPUSET_H */ > diff --git a/kernel/cpuset.c b/kernel/cpuset.c > index 858217b..ccbaac9 100644 > --- a/kernel/cpuset.c > +++ b/kernel/cpuset.c > @@ -1231,6 +1231,11 @@ DEFINE_PER_CPU(int, cpu_adaptive_nohz_ref); > > static cpumask_t nohz_cpuset_mask; > > +inline bool nohz_cpu_exist(void) > +{ > + return !cpumask_empty(&nohz_cpuset_mask); > +} > + > static void flush_cputime_interrupt(void *unused) > { > trace_printk("IPI: flush cputime\n"); > diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c > index bdc8aeb..e60d541 100644 > --- a/kernel/time/tick-sched.c > +++ b/kernel/time/tick-sched.c > @@ -409,6 +409,25 @@ out: > return ret; > } > > +static inline bool must_take_timer_duty(int cpu) > +{ > + int handler = tick_do_timer_cpu; > + bool ret = false; > + bool tick_needed = nohz_cpu_exist(); Note this is racy because this fetches the value of the nohz_cpuset_mask without locking. We may "see" there is no nohz cpusets whereas we just set some of them as nohz and they could even have shut down their tick already. > + > + /* > +* A CPU will have to take the timer duty if there is an adaptive > +* nohz CPU in the system. The last handler == cpu check ensures > +* that the last cpu that did the do_timer() sticks with the duty. > +* A normal (non nohz) cpu will take the charge from a nohz cpu in > +* tick_do_timer_check_handler anyway. > +*/ > + if (tick_needed && (handler == TICK_DO_TIMER_NONE || handler == cpu)) > + ret = true; This check is also racy due to the lack of locking. The previous handler may have set TICK_DO_TIMER_NONE and gone to sleep. We have no guarantee that the CPU can see that new value. It could believe there is still a handler. This needs at least cmpxchg() to make the test and set atomic. > + > + return ret; > +} > + > static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) > { > /* > @@ -421,6 +440,9 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched > *ts) > if (unlikely(!cpu_online(cpu))) { > if (cpu == tick_do_timer_cpu) > tick_do_timer_cpu = TICK_DO_TIMER_NONE; > + } else if (must_take_timer_duty(cpu)) { > + tick_do_timer_cpu = cpu; > + return false; > } > > if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) > @@ -512,6 +534,13 @@ void tick_nohz_idle_enter(void) > #ifdef CONFIG_CPUSETS_NO_HZ > static bool can_stop_adaptive_tick(void) > { > + int cpu = smp_processor_id(); > + > + if (must_take_timer_duty(cpu)) { > + tick_do_timer_cpu = cpu; > + return false; One problem I see here is that you randomize the handler. It could be an adaptive nohz CPU or an idle CPU. It's a problem if the user wants CPU isolation. I suggest to rather define a tunable timekeeping duty CPU affinity in a cpumask file at /sys/devices/system/cpu/timekeeping and a toggle at /sys/devices/system/cpu/cpuX/timekeeping (like the online file