* [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue
@ 2026-11-27 8:16 Christian Loehle
0 siblings, 0 replies; 4+ messages in thread
From: Christian Loehle @ 2026-11-27 8:16 UTC (permalink / raw)
To: mingo, peterz, vincent.guittot
Cc: qyousef, christian.loehle, dietmar.eggemann, rostedt, jstultz,
kayracizmeci, wvw, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue
2026-11-27 11:13 [PATCHv2 0/2] sched/uclamp: Fix dequeue accounting across static-key updates Christian Loehle
@ 2026-11-27 11:13 ` Christian Loehle
0 siblings, 0 replies; 4+ messages in thread
From: Christian Loehle @ 2026-11-27 11:13 UTC (permalink / raw)
To: mingo, peterz, vincent.guittot
Cc: qyousef, christian.loehle, dietmar.eggemann, rostedt, jstultz,
kayracizmeci, wvw, linux-kernel
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.
Drop the redundant active check in uclamp_rq_dec_id(), now done by callers.
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")
Reviewed-by: Kayra Cizmeci <kayracizmeci@gmail.com>
Signed-off-by: Christian Loehle <christian.loehle@arm.com>
---
kernel/sched/core.c | 39 ++++++---------------------------------
1 file changed, 6 insertions(+), 33 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index c9890ed35df7..fadad7d3f3a2 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.
@@ -1830,32 +1829,6 @@ static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
lockdep_assert_rq_held(rq);
- /*
- * If sched_uclamp_used was enabled after task @p was enqueued,
- * we could end up with unbalanced call to uclamp_rq_dec_id().
- *
- * In this case the uc_se->active flag should be false since no uclamp
- * accounting was performed at enqueue time and we can just return
- * here.
- *
- * Need to be careful of the following enqueue/dequeue ordering
- * problem too
- *
- * enqueue(taskA)
- * // sched_uclamp_used gets enabled
- * enqueue(taskB)
- * dequeue(taskA)
- * // Must not decrement bucket->tasks here
- * dequeue(taskB)
- *
- * where we could end up with stale data in uc_se and
- * bucket[uc_se->bucket_id].
- *
- * The following check here eliminates the possibility of such race.
- */
- if (unlikely(!uc_se->active))
- return;
-
bucket = &uc_rq->bucket[uc_se->bucket_id];
WARN_ON_ONCE(!bucket->tasks);
@@ -1918,12 +1891,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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue
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
0 siblings, 0 replies; 4+ messages in thread
From: Kayra Cizmeci @ 2026-11-27 10:18 UTC (permalink / raw)
To: Christian Loehle
Cc: mingo, peterz, vincent.guittot, qyousef, dietmar.eggemann,
rostedt, jstultz, wvw, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue
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 ` Christian Loehle
2026-11-27 10:18 ` Kayra Cizmeci
0 siblings, 1 reply; 4+ messages in thread
From: Christian Loehle @ 2026-11-27 8:35 UTC (permalink / raw)
To: mingo, peterz, vincent.guittot
Cc: qyousef, christian.loehle, dietmar.eggemann, rostedt, jstultz,
kayracizmeci, wvw, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-27 11:13 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 8:16 [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue Christian Loehle
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
2026-11-27 11:13 [PATCHv2 0/2] sched/uclamp: Fix dequeue accounting across static-key updates Christian Loehle
2026-11-27 11:13 ` [PATCH 1/2] sched/uclamp: Always account active clamps on dequeue Christian Loehle
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®