Linux Power Management development
 help / color / mirror / Atom feed
From: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
To: "Rafael J. Wysocki (Intel)" <rafael@kernel.org>,
	Avi Weiss <thnkslprpt@gmail.com>
Cc: viresh.kumar@linaro.org, mingo@redhat.com, peterz@infradead.org,
	juri.lelli@redhat.com, vincent.guittot@linaro.org,
	zhanxusheng1024@gmail.com, linux-pm@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Subject: Re: [PATCH v2] cpufreq: schedutil: Skip busy-CPU hold when NOHZ is inactive
Date: Mon, 16 Nov 2026 17:44:51 +0800	[thread overview]
Message-ID: <44ed43c9-a91f-445a-97d0-ebe538888cf5@oss.qualcomm.com> (raw)
In-Reply-To: <CAJZ5v0g-oDagp2GrZyhLmT-BYJqcm-D4s1eTrXCxB-fj5BNixw@mail.gmail.com>

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

  parent reply	other threads:[~2026-11-16  9:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-16 10:38 ` Christian Loehle
2026-11-16 10:52 ` Vincent Guittot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44ed43c9-a91f-445a-97d0-ebe538888cf5@oss.qualcomm.com \
    --to=zhongqiu.han@oss.qualcomm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=thnkslprpt@gmail.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    --cc=zhanxusheng1024@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox