mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kayra Cizmeci <kayracizmeci@gmail.com>
To: Christian Loehle <christian.loehle@arm.com>
Cc: mingo@kernel.org, peterz@infradead.org,
	vincent.guittot@linaro.org,  qyousef@layalina.io,
	dietmar.eggemann@arm.com, rostedt@goodmis.org,
	 jstultz@google.com, wvw@google.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue
Date: Fri, 27 Nov 2026 13:18:42 +0300	[thread overview]
Message-ID: <awlRErm6iR2QsWUX@scheduler.schedules> (raw)
In-Reply-To: <20261127083542.3172843-2-christian.loehle@arm.com>

Hi Christian,

On Fri, Nov 27, 2026 at 08:35:41AM +0000, Christian Loehle wrote:
> Enabling sched_uclamp_used does not update all static-key callsites
> atomically. An enqueue can observe the key enabled and increment a
> task's uclamp buckets while a subsequent dequeue still observes it
> disabled and skips the decrement. This leaves stale bucket references
> and active bits, which can keep the runqueue clamps elevated after the
> task sleeps or migrates.
> 
> Replace the static-key check in uclamp_rq_dec() with a check of the
> task's effective UCLAMP_MIN active flag. This records whether the task
> contributed to the runqueue accounting, so an accounted task releases
> its references regardless of static-key update progress.
> 
> Enqueue and dequeue update both effective clamps under rq->lock.
> Updates to an already active clamp complete their decrement and
> increment under the same lock, so dequeue cannot observe their
> temporary inactive state. At entry to uclamp_rq_dec(), either active
> flag therefore suffices to determine whether the task has accounting
> to release.
> 
> Keep the static key on enqueue, so unused uclamp still skips enqueue
> accounting. On dequeue, the early active check returns before the
> scheduling-class and delayed-task checks or either per-ID helper call.
> Limiting the change to dequeue retains the enqueue benefit of the
> static key and reduces the unused dequeue path to an active-state
> check.
> 
> Reported-by: Wei Wang <wvw@google.com>
> Closes: https://lore.kernel.org/all/CANDhNCqyXsZ6G77z+9s=rTO6BZifRG9+hVxMTah+tAv6pB6h3w@mail.gmail.com/
> Suggested-by: Wei Wang <wvw@google.com>
> Fixes: 46609ce22703 ("sched/uclamp: Protect uclamp fast path code with static key")
> Signed-off-by: Christian Loehle <christian.loehle@arm.com>
> ---
>  kernel/sched/core.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index c9890ed35df7..05b092147c6f 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -1617,8 +1617,7 @@ static struct uclamp_se uclamp_default[UCLAMP_CNT];
>  
>  /*
>   * This static key is used to reduce the uclamp overhead in the fast path. It
> - * primarily disables the call to uclamp_rq_{inc, dec}() in
> - * enqueue/dequeue_task().
> + * disables uclamp_rq_inc() accounting in enqueue_task().
>   *
>   * This allows users to continue to enable uclamp in their kernel config with
>   * minimum uclamp overhead in the fast path.
> @@ -1918,12 +1917,12 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
>  	enum uclamp_id clamp_id;
>  
>  	/*
> -	 * Avoid any overhead until uclamp is actually used by the userspace.
> -	 *
> -	 * The condition is constructed such that a NOP is generated when
> -	 * sched_uclamp_used is disabled.
> +	 * Static-key updates are not atomic across callsites. An enqueue may
> +	 * have accounted this task before this callsite observes the key enabled.
> +	 * Check the task's accounting state instead. Both clamps are updated
> +	 * under the rq lock, so either active flag is sufficient here.
>  	 */
> -	if (!uclamp_is_used())
> +	if (!p->uclamp[UCLAMP_MIN].active)
>  		return;
>  
>  	if (unlikely(!p->sched_class->uclamp_enabled))
> -- 
> 2.34.1
> 

Yeah well...

Hope I was following this issue from the start. Now reviewing is a bit
messy.

Although, I can't really see any *cleaner* way to fix this. And the code
looks correct. Since this active thing gets set in uclamp_rq_inc_id(),
when we increase the bucket thingie.

BTW, a little nit: right now uclamp_rq_dec_id() checks active too, but
with above that is always true since we check it before we enter.
The only other call site uclamp_rq_reinc_id() checks it too.
So removing it would be sweet. Just a nit tho :-).

Hope I'm not missing something.

Reviewed-by: Kayra Cizmeci <kayracizmeci@gmail.com>


Cheers,

Kayra

  reply	other threads:[~2026-11-27 10:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  8:35 [RESEND][PATCH 0/2] sched/uclamp: Fix dequeue accounting across static-key updates Christian Loehle
2026-11-27  8:35 ` [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue Christian Loehle
2026-11-27 10:18   ` Kayra Cizmeci [this message]
2026-11-27  8:35 ` [PATCH 2/2] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Christian Loehle
2026-11-27  9:40 ` [RESEND][PATCH 0/2] sched/uclamp: Fix dequeue accounting across static-key updates Peter Zijlstra
2026-11-27  9:47   ` Christian Loehle
2026-11-27  9:49 ` Vincent Guittot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-27 11:13 [PATCHv2 " Christian Loehle
2026-11-27 11:13 ` [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue Christian Loehle
2026-11-27  8:16 Christian Loehle

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=awlRErm6iR2QsWUX@scheduler.schedules \
    --to=kayracizmeci@gmail.com \
    --cc=christian.loehle@arm.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=qyousef@layalina.io \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=wvw@google.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

all inboxes | Powered by JetHome®