Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
@ 2026-11-08 14:50 Avi Weiss
  2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Avi Weiss @ 2026-11-08 14:50 UTC (permalink / raw)
  To: rafael, viresh.kumar
  Cc: mingo, peterz, juri.lelli, vincent.guittot, zhanxusheng1024,
	zhongqiu.han, linux-pm, linux-kernel

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.

Fixes: b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")
Cc: stable@vger.kernel.org
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---
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(+)

diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
index 14405a614991..50249cc1a594 100644
--- a/kernel/sched/cpufreq_schedutil.c
+++ b/kernel/sched/cpufreq_schedutil.c
@@ -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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
  2026-11-08 14:50 [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive Avi Weiss
@ 2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
  2026-11-16  9:27   ` Zhan Xusheng
  2026-11-16  9:44   ` Zhongqiu Han
  2026-11-16 10:38 ` Christian Loehle
  2026-11-16 10:52 ` Vincent Guittot
  2 siblings, 2 replies; 6+ messages in thread
From: Rafael J. Wysocki (Intel) @ 2026-11-13 18:21 UTC (permalink / raw)
  To: Avi Weiss
  Cc: rafael, viresh.kumar, mingo, peterz, juri.lelli, vincent.guittot,
	zhanxusheng1024, zhongqiu.han, linux-pm, linux-kernel

On Sun, Nov 8, 2026 at 3:50 PM Avi Weiss <thnkslprpt@gmail.com> wrote:
>
> 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.
>
> Fixes: b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")
> Cc: stable@vger.kernel.org
> Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>

I'd love to see some feedback on this from the people on the CC.

> ---
> 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(+)
>
> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
> index 14405a614991..50249cc1a594 100644
> --- a/kernel/sched/cpufreq_schedutil.c
> +++ b/kernel/sched/cpufreq_schedutil.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
  2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
@ 2026-11-16  9:27   ` Zhan Xusheng
  2026-11-16  9:44   ` Zhongqiu Han
  1 sibling, 0 replies; 6+ messages in thread
From: Zhan Xusheng @ 2026-11-16  9:27 UTC (permalink / raw)
  To: rafael, thnkslprpt
  Cc: viresh.kumar, mingo, peterz, juri.lelli, vincent.guittot,
	zhongqiu.han, linux-pm, linux-kernel, zhanxusheng

Hi Rafael,

On Fri, Nov 13, 2026 at 07:21:00PM +0100, Rafael J. Wysocki wrote:
> 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.

Reviewed-by: Zhan Xusheng <zhanxusheng@xiaomi.com>

Thanks,
Zhan Xusheng

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
  2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
  2026-11-16  9:27   ` Zhan Xusheng
@ 2026-11-16  9:44   ` Zhongqiu Han
  1 sibling, 0 replies; 6+ messages in thread
From: Zhongqiu Han @ 2026-11-16  9:44 UTC (permalink / raw)
  To: Rafael J. Wysocki (Intel), Avi Weiss
  Cc: viresh.kumar, mingo, peterz, juri.lelli, vincent.guittot,
	zhanxusheng1024, linux-pm, linux-kernel, Zhongqiu Han

On 11/14/2026 2:21 AM, Rafael J. Wysocki (Intel) wrote:
> On Sun, Nov 8, 2026 at 3:50 PM Avi Weiss <thnkslprpt@gmail.com> wrote:
>>
>> 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.
>>
>> Fixes: b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
> 
> I'd love to see some feedback on this from the people on the CC.
> 
>> ---
>> v2:
>> - use tick_nohz_is_active() instead of tick_nohz_enabled to 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.

Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>


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/78af4d6d-7465-4980-9d8b-e832db76a08e@oss.qualcomm.com/

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):

diff --git a/include/linux/tick.h b/include/linux/tick.h
index b121c5d53e30..4733344f695a 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -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; }
diff --git a/kernel/sched/cpufreq_schedutil.c 
b/kernel/sched/cpufreq_schedutil.c
index 14405a614991..5285074ee2f1 100644
--- a/kernel/sched/cpufreq_schedutil.c
+++ b/kernel/sched/cpufreq_schedutil.c
@@ -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
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index c6d86e4da8a9..878aca904f5c 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -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.

>>
>>   kernel/sched/cpufreq_schedutil.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
>> index 14405a614991..50249cc1a594 100644
>> --- a/kernel/sched/cpufreq_schedutil.c
>> +++ b/kernel/sched/cpufreq_schedutil.c
>> @@ -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
>>


-- 
Thx and BRs,
Zhongqiu Han

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
  2026-11-08 14:50 [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive Avi Weiss
  2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
@ 2026-11-16 10:38 ` Christian Loehle
  2026-11-16 10:52 ` Vincent Guittot
  2 siblings, 0 replies; 6+ messages in thread
From: Christian Loehle @ 2026-11-16 10:38 UTC (permalink / raw)
  To: Avi Weiss, rafael, viresh.kumar
  Cc: mingo, peterz, juri.lelli, vincent.guittot, zhanxusheng1024,
	zhongqiu.han, linux-pm, linux-kernel

On 11/8/26 14:50, Avi Weiss wrote:
> 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.
> 
> Fixes: b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")
> Cc: stable@vger.kernel.org
> Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>

Reviewed-by: Christian Loehle <christian.loehle@arm.com>

> ---
> 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(+)
> 
> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
> index 14405a614991..50249cc1a594 100644
> --- a/kernel/sched/cpufreq_schedutil.c
> +++ b/kernel/sched/cpufreq_schedutil.c
> @@ -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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
  2026-11-08 14:50 [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive Avi Weiss
  2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
  2026-11-16 10:38 ` Christian Loehle
@ 2026-11-16 10:52 ` Vincent Guittot
  2 siblings, 0 replies; 6+ messages in thread
From: Vincent Guittot @ 2026-11-16 10:52 UTC (permalink / raw)
  To: Avi Weiss
  Cc: rafael, viresh.kumar, mingo, peterz, juri.lelli, zhanxusheng1024,
	zhongqiu.han, linux-pm, linux-kernel

On Sun, 8 Nov 2026 at 15:50, Avi Weiss <thnkslprpt@gmail.com> wrote:
>
> 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.
>
> Fixes: b7eaf1aab9f8 ("cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely")
> Cc: stable@vger.kernel.org
> Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>

Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org>

> ---
> 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(+)
>
> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c
> index 14405a614991..50249cc1a594 100644
> --- a/kernel/sched/cpufreq_schedutil.c
> +++ b/kernel/sched/cpufreq_schedutil.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-16 10:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-08 14:50 [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive Avi Weiss
2026-11-13 18:21 ` Rafael J. Wysocki (Intel)
2026-11-16  9:27   ` Zhan Xusheng
2026-11-16  9:44   ` Zhongqiu Han
2026-11-16 10:38 ` Christian Loehle
2026-11-16 10:52 ` Vincent Guittot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox