From: Christian Loehle <christian.loehle@arm.com>
To: Qais Yousef <qyousef@layalina.io>, Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
John Stultz <jstultz@google.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Qais Yousef <qyousef@google.com>,
LKML <linux-kernel@vger.kernel.org>,
Hongyan Xia <hongyan.xia2@arm.com>,
Ingo Molnar <mingo@kernel.org>, Wei Wang <wvw@google.com>,
Patrick Bellasi <patrick.bellasi@arm.com>,
Dietmar Eggemann <dietmar.eggemann@arm.com>
Subject: Re: Uclamp races due to non-atomic static_branch_likely updates
Date: Thu, 22 Oct 2026 14:18:19 +0100 [thread overview]
Message-ID: <005ed941-2737-413c-8436-a9de227d01ca@arm.com> (raw)
In-Reply-To: <20261021140222.tazz6aw4j5pvqwox@airbuntu>
On 10/21/26 15:02, Qais Yousef wrote:
> On 10/21/26 15:29, Peter Zijlstra wrote:
>> On Wed, Oct 21, 2026 at 02:20:58PM +0100, Qais Yousef wrote:
>>
>>>> Also note that a simple global boolean has the exact same problem.
>>>> Adding runtime conditionals isn't going to fix this.
>>>
>>> I have something in my memory that could be complete garbage that tells me all
>>> cpus should see the new update after a grace period, did my brain invent this?
>>
>> This is true; rcu_sync() implies a memory barrier on all CPUs, and
>> therefore all CPUs must observe the prior state upon completion.
>>
>> The thing is that a grace period is of 'arbitrary length' and during
>> that you can still have some CPUs seeing your variable set while others
>> will not and you get into the very same problem.
>
> Maybe this arbitrary length what contributes to the size of the race window
> otherwise I struggle to see this being a problem practically.
>
>>
>>> If not invented, can we do synchronize_rcu() once on any cpu seeing the code
>>> enabled for the first time to ensure the rest of the cpus has seen the newly
>>> enabled path?
>>>
>>> eg (not even compile tested):
>>>
>>> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
>>> index 7d2ec527b8a2..a6093ec88103 100644
>>> --- a/kernel/sched/sched.h
>>> +++ b/kernel/sched/sched.h
>>> @@ -3715,7 +3715,16 @@ unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
>>> */
>>> static inline bool uclamp_is_used(void)
>>> {
>>> - return static_branch_likely(&sched_uclamp_used);
>>> + if (static_branch_likely(&sched_uclamp_used)) {
>>> + static bool do_once = true;
>>> + if (unlikely(do_once)) {
>>> + do_once = false;
>>> + synchronize_rcu();
>>> + }
>>> + return true;
>>> + }
>>> +
>>> + return false;
>>> }
>>>
>>> /*
>>
>> This cannot work; two immediate problems:
>>
>> - the do_once needs re-evaluating for every switch
>
> I think uclamp can only be turned on, but yeah for a generic solution this is
> a problem and the do_once will need to be reset on switch off.
>
>> - you cannot do rcu_sync while holding rq->lock ;-)
>
> Uh-oh, right :-)
>
>>
>>> The other alternative is to just accept this race can happen and do some sanity
>>> check and reset/cleanup on going to idle - which should be pretty cheap and
>>> straightforward.
>>
>> This seems the sanest option.
>
> I think the below should do
>
> -->8--
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index d7c3b43158de..c6780358a797 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2111,7 +2111,7 @@ static void uclamp_post_fork(struct task_struct *p)
> uclamp_update_util_min_rt_default(p);
> }
>
> -static void __init init_uclamp_rq(struct rq *rq)
> +void init_uclamp_rq(struct rq *rq)
> {
> enum uclamp_id clamp_id;
> struct uclamp_rq *uc_rq = rq->uclamp;
> diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
> index 76f3c84ca684..64ea91b743bb 100644
> --- a/kernel/sched/idle.c
> +++ b/kernel/sched/idle.c
> @@ -502,6 +502,14 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next, enum snt
> * lost_idle_time of clock_pelt
> */
> update_idle_rq_clock_pelt(rq);
> + /*
> + * Due to a race condition when enabling sched_uclamp_used static key,
> + * we can end up with imbalance where uclamp_rq_inc_id() has happened
> + * on a CPU without uclamp_rq_dec_id() on another CPU that hasn't seen
> + * the static key flipped. The race condition is unlikely, but ensure
> + * the uclamp_rq() state is set properly on each entry to idle.
> + */
> + init_uclamp_rq(rq);
> }
>
> struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf)
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 7d2ec527b8a2..6d7be3e3b36b 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -3704,6 +3704,7 @@ static inline unsigned long cpu_util_rt(struct rq *rq)
> #ifdef CONFIG_UCLAMP_TASK
>
> unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
> +void init_uclamp_rq(struct rq *rq);
>
> /*
> * When uclamp is compiled in, the aggregation at rq level is 'turned off'
> @@ -3802,6 +3803,8 @@ uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
> return SCHED_CAPACITY_SCALE;
> }
>
> +void init_uclamp_rq(struct rq *rq) {}
> +
> static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
>
> static inline bool uclamp_is_used(void)
So I guess the clear-when-entering-idle taper is sufficient for UCLAMP_MIN.
UCLAMP_MAX (where on a sufficiently low setting we may just never have that
idle entry to clear) we expect to be fine because it's max-aggregated, i.e.
a second task would let us escape the low stale UCLAMP_MAX on the rq?
I guess we could reevaluate the static branch at every tick too if we really
care?
next prev parent reply other threads:[~2026-10-22 13:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 18:53 John Stultz
2026-10-20 19:29 ` Steven Rostedt
2026-10-21 11:38 ` Peter Zijlstra
2026-10-21 13:20 ` Qais Yousef
2026-10-21 13:29 ` Peter Zijlstra
2026-10-21 14:02 ` Qais Yousef
2026-10-22 13:18 ` Christian Loehle [this message]
2026-10-21 13:21 ` Steven Rostedt
2026-10-21 13:34 ` Peter Zijlstra
2026-10-21 13:59 ` Steven Rostedt
2026-10-21 17:56 ` John Stultz
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=005ed941-2737-413c-8436-a9de227d01ca@arm.com \
--to=christian.loehle@arm.com \
--cc=dietmar.eggemann@arm.com \
--cc=hongyan.xia2@arm.com \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=patrick.bellasi@arm.com \
--cc=peterz@infradead.org \
--cc=qyousef@google.com \
--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®