Re: [PATCH v6 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq

From: Christian Loehle

Date: Wed Nov 25 2026 - 14:10:13 EST


On 11/25/26 17:15, Vincent Guittot wrote:
> On Wed, 25 Nov 2026 at 10:13, Christian Loehle <christian.loehle@xxxxxxx> wrote:
>>
>> On 11/23/26 18:15, Christian Loehle wrote:
>>> On 11/23/26 17:23, Vincent Guittot wrote:
>>>> On Mon, 23 Nov 2026 at 15:38, Christian Loehle <christian.loehle@xxxxxxx> wrote:
>>>>>
>>>>> On 11/21/26 04:15, Qais Yousef wrote:
>>>>>> Static keys are not guaranteed to be enabled atomically across CPUs. Or
>>>>>> across callsites on the same CPUs. Which means when sched_uclamp_used is
>>>>>> enabled, some CPUs/callsites will return true for uclamp_is_used() while
>>>>>> others won't, leading to asymmetric uclamp_rq_{inc, dec}_id. If we
>>>>>> decrement without increment this is safe as documented in the code. But
>>>>>> an increment without a decrement is a problem and will leave the
>>>>>> uclamp_rq in a corrupt state.
>>>>>
>>>>> So if we only need decrement to work then we can rely on
>>>>> p's uclamp_se.active and decrement regardless of uclamp_is_used() and call it
>>>>> a day?
>>>>
>>>> The static key prevents overhead for those who don't use uclamp. It's
>>>> a default rule for features like numa, cache aware, eas, uclamp etc to
>>>> not impact those who don't use it.
>>>> I'm afraid that the executing uclamp_rq_dec up to
>>>> p->uclamp[clamp_id].active will not be free of overhead
>>>
>>> I get why the static branch is there, but as long as p->uclamp[clamp_id].active
>>> never changes from 0, like when uclamp is never used, the check should be virtually
>>> free on any half-decent branch predictor.
>>> Also in any case the enqueue path ought to be the expensive part for uclamp.
>>>
>>> I can come back with some numbers though, including the original netperf that
>>> introduced the static key.
>>
>> So I compared mainline, my dequeue-only fix and a version where uclamp_is_used()
>> always returns true (i.e. no static branch optimisation at all).
>> A MeteorLake, Xeon and Orion, all compared to mainline as a base:
>
> I did a quick test on my favorite dragon rb5 with perf sched pipe -T ,
> perf cpufreq gov and only WFI:
> - on big, the diff is -0.7% (+/- 0.30%)
> - on medium it's -1.85% (+/- 0.20%)
> - and on little it's -6.53% (+/- 0.15%)
>

Looks in line with my results on Orion I'd say,
FWIW here's the diff I'm proposing on top, proper will follow once I've completed the
full results, but if you wanna have a go already and (hopefully) convince yourself:

----------->8------------

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 3c141aaae6a7..850088eeda5f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1900,8 +1900,12 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
/*
* Static-key updates are not atomic across callsites. An enqueue may
* have accounted this task before this callsite observes the key enabled.
- * Let uclamp_rq_dec_id() use uc_se->active to match that accounting.
+ * Check the task's accounting state instead. Both clamps are updated
+ * under the rq lock, so either active flag is sufficient here.
*/
+ if (!p->uclamp[UCLAMP_MIN].active)
+ return;
+
if (unlikely(!p->sched_class->uclamp_enabled))
return;

(END)