cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
Subsystems (3)
PATCH v2 1/1 cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
sugov_hold_freq() uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Skip the busy-CPU hold when tick_nohz_is_active() is false. This also handles systems where NOHZ was enabled but never activated.
---
v2:
- use tick_nohz_is_active() instead of tick_nohz_enabled to also handle NOHZ enabled but never activated, as suggested by Zhan Xusheng
kernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/cpufreq_schedutil.c +3 -0
@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature.
base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd
Signature · 1 line
2.43.0
Rafael J. Wysocki (Intel)
maintainerQuoted 7 lines · Avi Weiss
sugov_hold_freq()uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Quoted 6 lines
Skip the busy-CPU hold when
tick_nohz_is_active()is false. This also handles systems where NOHZ was enabled but never activated.Fixes b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")Signed-off-by Avi Weiss <[email protected]>
I'd love to see some feedback on this from the people on the CC.
Quoted 27 lines
---
v2:
- usetick_nohz_is_active()instead oftick_nohz_enabledto also handle NOHZ enabled but never activated, as suggested by Zhan Xushengkernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/cpufreq_schedutil.c +3
-0@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature. base-commit:
fa4ad798f6f681031775266044ba1b0cf3c670bd--
2.43.0
Zhan Xusheng
Reviewed-byHi Rafael,
Quoted 1 line · Rafael J. Wysocki
I'd love to see some feedback on this from the people on the CC.
idle_calls only advances in nohz mode; with nohz=off it stays at 0 on all CPUs, so the hold never releases.
Thanks,
Zhan Xusheng
Zhongqiu Han
Reviewed-byQuoted 8 lines · Rafael J. Wysocki (Intel)
Quoted 7 lines · Avi Weiss
sugov_hold_freq()uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Quoted 13 lines
Quoted 6 lines
Skip the busy-CPU hold when
tick_nohz_is_active()is false. This also handles systems where NOHZ was enabled but never activated.Fixes b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")Signed-off-by Avi Weiss <[email protected]>I'd love to see some feedback on this from the people on the CC.
Quoted 4 lines
---
v2:
- usetick_nohz_is_active()instead oftick_nohz_enabledto also handle NOHZ enabled but never activated, as suggested by Zhan Xusheng
v2 is small and only affects the NOHZ-inactive case, so I think it is correct for stable if needed.
However, the hold can still get stuck in several other cases, described below, and fixing them needs a different way to detect idle periods.
I would appreciate it if the scheduler maintainers could also take a look, since part of what I describe below touches scheduler internals (rq->idle->nivcsw). Please feel free to correct me if there is any misunderstanding.
As I mentioned on V1:
https://lore.kernel.org/all/[email protected]/
This still leaves other cases in which the CPU does go idle, but idle_calls never moves, so sugov_hold_freq() keeps holding the frequency. In all of them NOHZ is active (except the first one), so the new tick_nohz_is_active() check does not help.
1. nohz=off (or NOHZ never activated): b7eaf1aab9f8 (v4.12, 2017-03) idle_calls is only incremented if can_stop_idle_tick() succeeds, and it always fails when NOHZ is not active. This is the case current v2 fixes.
2. nohz_full timekeeper CPU: b7eaf1aab9f8 (v4.12, 2017-03) Same as above: the timekeeper CPU must keep its tick, so can_stop_idle_tick() fails.
3. idle=poll: 2aaf709a518d (v4.17, 2018-03-15)
The idle=poll path restarts rather than stops the tick, so idle_calls is never incremented.
4. CPU latency QoS limit, short idle: 554c8aa8ecad (v4.17, 2018-04-03) With a cpuidle governor, idle_calls only moves when the governor asks to stop the tick (or it is stopped already). So:
- with a QoS limit that allows only POLL (or, with teo, one below 7.5 us that leaves only shallow states), the tick is always retained;
- idle periods predicted to be shorter than the tick are not counted, so the hold is kept across them, and never released if all idle periods are that short (e.g. a periodic timer below TICK_NSEC). b7eaf1aab9f8 counted them.
A tick-stop counter ties the hold to the tick policy. Two signals that do not are rq->idle->nivcsw and rq->clock_idle.
- rq->idle->nivcsw: the idle task is always TASK_RUNNING, so when it is switched out at the end of an idle period, __schedule() accounts it in nivcsw. That is once per idle period, whatever the idle
state,
the tick state or the QoS request.
- rq->clock_idle: written when the CPU switches to the idle task, and
on every rq clock update while rq->curr is the idle task.
nivcsw seems to be the better fit: clock_idle is a PELT-private timestamp, updated on every rq clock update while the CPU is idle, including at wakeup. If, e.g., an interrupt wakes up two tasks on an idle CPU and the update for the second one is rate limited, clock_idle changes after the last update in that idle period, so the next update releases the hold although no new idle period has started.
The patch (RFC, appended below):
include/linux/tick.h +0 -1
@@ -137,7 +137,6 @@ extern void tick_nohz_irq_exit(void);extern bool tick_nohz_idle_got_tick (void );extern ktime_t tick_nohz_get_next_hrtimer (void );extern ktime_t tick_nohz_get_sleep_length (ktime_t *delta_next); -extern unsigned long tick_nohz_get_idle_calls_cpu (int cpu);#else /* !CONFIG_NO_HZ_COMMON */ #define tick_nohz_enabled (0)static inline bool tick_nohz_is_active (void ) {return false; }
kernel/sched/cpufreq_schedutil.c +17 -14
@@ -53,9 +53,7 @@ struct sugov_cpu {unsigned long bw_max;/* The field below is for single-CPU policies only: */ -#ifdef CONFIG_NO_HZ_COMMON -unsigned long saved_idle_calls; -#endif +unsigned long saved_idle_entries; }; static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -379,10 +377,11 @@ static unsigned long sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,return (sg_cpu->iowait_boost * max_cap) >>SCHED_CAPACITY_SHIFT ; } -#ifdef CONFIG_NO_HZ_COMMON static bool sugov_hold_freq (struct sugov_cpu *sg_cpu) { -unsigned long idle_calls; +struct rq *rq =cpu_rq (sg_cpu->cpu); +unsigned long idle_entries; +bool idle;bool ret;/*
@@ -394,22 +393,26 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)return false;/* if capped by uclamp_max, always update to be in compliance */ -if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu))) +if (uclamp_rq_is_capped (rq))return false;/* -* Maintain the frequency if the CPU has not been idle recently, as -* reduction is likely to be premature. +* Maintain the frequency if the CPU has not been idle since the +* previous update, as reduction is likely to be premature. +* +* The idle task is switched out once at the end of every idle period, +* so its nivcsw, plus one while it is running, counts idle periods +* including the current one, regardless of whether or not the tick has +* been stopped. The caller holds the rq lock, which serializes this +* with the updates of rq->curr and nivcsw. */ - idle_calls =tick_nohz_get_idle_calls_cpu (sg_cpu->cpu); - ret = idle_calls == sg_cpu->saved_idle_calls; + idle = rq->curr == rq->idle; + idle_entries = rq->idle->nivcsw + idle; + ret = !idle && idle_entries == sg_cpu->saved_idle_entries; - sg_cpu->saved_idle_calls = idle_calls; + sg_cpu->saved_idle_entries = idle_entries;return ret; } -#else /* !CONFIG_NO_HZ_COMMON: */ -static inline bool sugov_hold_freq (struct sugov_cpu *sg_cpu) {return false; } -#endif /* !CONFIG_NO_HZ_COMMON */ /* * Make sugov_should_update_freq() ignore the rate limit when DL
kernel/time/tick-sched.c +0 -16
@@ -1329,22 +1329,6 @@ ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)return ktime_sub (next_event, now); } -/** -* tick_nohz_get_idle_calls_cpu - return the current idle calls counter value -* for a particular CPU. -* @cpu: target CPU number -* -* Called from the schedutil frequency scaling governor in scheduler context. -* -* Return: the current idle calls counter value for @cpu -*/ -unsigned long tick_nohz_get_idle_calls_cpu (int cpu) -{ -struct tick_sched *ts =tick_get_tick_sched (cpu); - -return ts->idle_calls; -} -static void tick_nohz_dyntick_stop (struct tick_sched *ts,ktime_t now) {tick_nohz_forward_last_tick (ts, now);
Two other behaviour changes, besides short idle:
- CONFIG_HZ_PERIODIC kernels: the hold was compiled out there (no NOHZ counter).
- idle injection (play_idle_precise(), drivers/powercap/idle_inject.c):
it runs in an RT kthread, not the idle task, so injected idle no longer releases the hold.
I have been testing this in QEMU with fake cpufreq and cpuidle drivers as no real hardware with per-CPU cpufreq policies yet.
Quoted 23 lines
Quoted 23 lines
kernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/cpufreq_schedutil.c +3
-0@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature. base-commit:
fa4ad798f6f681031775266044ba1b0cf3c670bd--
2.43.0
Signature · 2 lines
Thx and BRs, Zhongqiu Han
Quoted 6 lines · Avi Weiss
sugov_hold_freq()uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Quoted 6 lines
Skip the busy-CPU hold when
tick_nohz_is_active()is false. This also handles systems where NOHZ was enabled but never activated.Fixes b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")Signed-off-by Avi Weiss <[email protected]>
Quoted 24 lines
---
v2:
- usetick_nohz_is_active()instead oftick_nohz_enabledto also handle NOHZ enabled but never activated, as suggested by Zhan Xushengkernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/cpufreq_schedutil.c +3
-0@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature. base-commit:
fa4ad798f6f681031775266044ba1b0cf3c670bd
Quoted 7 lines · Avi Weiss
sugov_hold_freq()uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Quoted 6 lines
Skip the busy-CPU hold when
tick_nohz_is_active()is false. This also handles systems where NOHZ was enabled but never activated.Fixes b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")Signed-off-by Avi Weiss <[email protected]>
Quoted 27 lines
---
v2:
- usetick_nohz_is_active()instead oftick_nohz_enabledto also handle NOHZ enabled but never activated, as suggested by Zhan Xushengkernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/cpufreq_schedutil.c +3
-0@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature. base-commit:
fa4ad798f6f681031775266044ba1b0cf3c670bd--
2.43.0
Quoted 9 lines · Vincent Guittot
Quoted 7 lines · Avi Weiss
sugov_hold_freq()uses the NOHZ idle-call counter to determine whether a CPU has been idle recently. When NOHZ is disabled or fails to activate, the counter does not advance, causing schedutil to treat the CPU as continuously busy and potentially prevent frequency reductions.
Quoted 8 lines
Quoted 6 lines
Skip the busy-CPU hold when
tick_nohz_is_active()is false. This also handles systems where NOHZ was enabled but never activated.Fixes b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")Signed-off-by Avi Weiss <[email protected]>Reviewed-by Vincent Guittot <[email protected]>
Applied as 7.5 material, thanks!
Quoted 27 lines
Quoted 27 lines
---
v2:
- usetick_nohz_is_active()instead oftick_nohz_enabledto also handle NOHZ enabled but never activated, as suggested by Zhan Xushengkernel/sched/cpufreq_schedutil.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/cpufreq_schedutil.c +3
-0@@ -397,6 +397,9 @@ static bool sugov_hold_freq(struct sugov_cpu *sg_cpu)if (uclamp_rq_is_capped (cpu_rq (sg_cpu->cpu)))return false; +if (!tick_nohz_is_active ()) +return false; +/* * Maintain the frequency if the CPU has not been idle recently, as * reduction is likely to be premature. base-commit:
fa4ad798f6f681031775266044ba1b0cf3c670bd--
2.43.0