* [PATCH v7 0/3] Fix uclamp race against non atomic static key enable
@ 2026-11-26 1:28 Qais Yousef
2026-11-26 1:28 ` [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Qais Yousef @ 2026-11-26 1:28 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Vincent Guittot
Cc: Christian Loehle, Dietmar Eggemann, Steven Rostedt, John Stultz,
linux-kernel, Kayra Cizmeci, Qais Yousef
Hopefully the last version. There was a debate on whether to drop Fixes
tag or not. And Christian seems to be looking at alternative proposal.
Fixes tag for patch one for the maintainers if they chose to keep it but
NACK the LTS backport:
Fixes: 46609ce227039 ("sched/uclamp: Protect uclamp fast path code with static key")
Changes since v6:
- Use uclamp_rq_set() in init_uclamp_rq() to preserve
WRITE_ONCE() annotation
Changes since v5:
- Drop patch 3 to partial revert subtracting rq->nr_running for
RT throttling.
- Drop fixes tag from patch 1 as the series now is partially
fixed and not suitable for LTS.
Changes since v4:
- Fix problems in fair/RT throttling logic that were exposed by
the original fix.
Changes since v3:
- Fix Sashiko report about force_idle paths where
rq->nr_running is not 0 highlighted by Christian
- Update patch 1 commit message to reflect jump_label call
sites on the same CPU might not be updated atomically too
Changes since v2:
- Use uclamp_rq_is_idle() instead of scanning buckets to detect
imbalance
- Do some cleanup on accessing the flag as an additional patch
Changes since v1:
- Fix kernel test robot report due to missing static inline in dummy
definition of init_uclamp_rq()
Tested with the patch below to force imbalance via sched_feat. I could see the
warning appearing and frequencies spiking during imbalance. Correct behavior is
restored when sched_feat is disabled where the idle entry reset fixes things
up.
I had a mix of uclamp_max mainly used in the system via schedqosd.
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index a2035d66270b9..14f9f1e419e02 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1889,6 +1889,11 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
for_each_clamp_id(clamp_id)
uclamp_rq_inc_id(rq, p, clamp_id);
+ if (sched_feat(UCLAMP_IMBALANCED)) {
+ for_each_clamp_id(clamp_id)
+ uclamp_rq_inc_id(rq, p, clamp_id);
+ }
+
/* Reset clamp idle holding when there is one RUNNABLE task */
if (uclamp_rq_is_idle(rq))
uclamp_rq_clear_idle(rq);
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index 8f0dee8fc4755..b59e532500fd9 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -142,3 +142,5 @@ SCHED_FEAT(LATENCY_WARN, false)
*/
SCHED_FEAT(NI_RANDOM, true)
SCHED_FEAT(NI_RATE, true)
+
+SCHED_FEAT(UCLAMP_IMBALANCED, false)
diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index 84301fc49e699..dbc3f5e1a7e01 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -519,7 +519,7 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next, enum snt
* are still queued.
*/
if (uclamp_is_used() &&
- !rq->nr_running && unlikely(!uclamp_rq_is_idle(rq)))
+ !rq->nr_running && unlikely(WARN_ON(!uclamp_rq_is_idle(rq))))
init_uclamp_rq(rq);
}
Qais Yousef (3):
sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c
kernel/sched/core.c | 54 +++++++++++++++---------------------------
kernel/sched/ext/ext.c | 6 ++---
kernel/sched/fair.c | 6 ++---
kernel/sched/idle.c | 19 +++++++++++++++
kernel/sched/rt.c | 10 ++++----
kernel/sched/sched.h | 21 ++++++++++++----
6 files changed, 65 insertions(+), 51 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
2026-11-26 1:28 [PATCH v7 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
@ 2026-11-26 1:28 ` Qais Yousef
2026-11-26 10:05 ` Christian Loehle
2026-11-26 1:28 ` [PATCH v7 2/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
2026-11-26 1:28 ` [PATCH v7 3/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
2 siblings, 1 reply; 6+ messages in thread
From: Qais Yousef @ 2026-11-26 1:28 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Vincent Guittot
Cc: Christian Loehle, Dietmar Eggemann, Steven Rostedt, John Stultz,
linux-kernel, Kayra Cizmeci, Qais Yousef, Wei Wang
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.
The reported case seems to happen due to callsites on the same CPU not
seeing the flipped state.
Update init_uclamp_rq() so that we do uclamp_rq_set() to ensure
WRITE_ONCE() is used to update the value field and keep KCSAN happy.
Plugging the race is hard as discussed in [1]. Take it for granted and
do correction actions on going to idle to guarantee this state is
transient for a short period after uclamp_sched_used is enabled.
[1] https://lore.kernel.org/lkml/CANDhNCqyXsZ6G77z+9s=rTO6BZifRG9+hVxMTah+tAv6pB6h3w@mail.gmail.com/
Reported-by: Wei Wang <wvw@google.com>
Signed-off-by: Qais Yousef <qyousef@layalina.io>
---
kernel/sched/core.c | 7 +++----
kernel/sched/idle.c | 19 +++++++++++++++++++
kernel/sched/sched.h | 3 +++
3 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ea89471d4743c..23009278cc9a4 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2139,15 +2139,14 @@ 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;
for_each_clamp_id(clamp_id) {
- uc_rq[clamp_id] = (struct uclamp_rq) {
- .value = uclamp_none(clamp_id)
- };
+ memset(uc_rq[clamp_id].bucket, 0, sizeof(uc_rq[clamp_id].bucket));
+ uclamp_rq_set(rq, clamp_id, uclamp_none(clamp_id));
}
rq->uclamp_flags = UCLAMP_FLAG_IDLE;
diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index 451b4846219d5..c3882e3fbdf78 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -506,6 +506,25 @@ 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.
+ *
+ * The reset will cause the uclamp_max from uclamp_idle_value() to be
+ * lost. Since we are imbalanced, hard to know what is right. So just
+ * cleanup. This should be a transient error state once we recover
+ * everything should work as expected.
+ *
+ * We need to be careful of some paths that can force idle with tasks
+ * are still queued.
+ */
+ if (uclamp_is_used() &&
+ !rq->nr_running && unlikely(!uclamp_rq_is_idle(rq)))
+ 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 32f76551a0c5e..3cbaea9e29406 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3815,6 +3815,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'
@@ -3913,6 +3914,8 @@ uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
return SCHED_CAPACITY_SCALE;
}
+static inline 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)
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v7 2/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
2026-11-26 1:28 [PATCH v7 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-26 1:28 ` [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
@ 2026-11-26 1:28 ` Qais Yousef
2026-11-26 1:28 ` [PATCH v7 3/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
2 siblings, 0 replies; 6+ messages in thread
From: Qais Yousef @ 2026-11-26 1:28 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Vincent Guittot
Cc: Christian Loehle, Dietmar Eggemann, Steven Rostedt, John Stultz,
linux-kernel, Kayra Cizmeci, Qais Yousef
We already have uclamp_rq_is_idle(), but it wasn't used everywhere.
Replace open coding with the function and add new wrapper to set and
clear the flag which enhances readability.
Reviewed-by: Kayra Cizmeci <kayracizmeci@gmail.com>
Signed-off-by: Qais Yousef <qyousef@layalina.io>
---
kernel/sched/core.c | 14 +++++++-------
kernel/sched/sched.h | 10 ++++++++++
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 23009278cc9a4..04c396435413a 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1654,7 +1654,7 @@ uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id,
* max-clamp.
*/
if (clamp_id == UCLAMP_MAX) {
- rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
+ uclamp_rq_set_idle(rq);
return clamp_value;
}
@@ -1665,7 +1665,7 @@ static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
unsigned int clamp_value)
{
/* Reset max-clamp retention only on idle exit */
- if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
+ if (!uclamp_rq_is_idle(rq))
return;
uclamp_rq_set(rq, clamp_id, clamp_value);
@@ -1918,8 +1918,8 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
uclamp_rq_inc_id(rq, p, clamp_id);
/* Reset clamp idle holding when there is one RUNNABLE task */
- if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
- rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
+ if (uclamp_rq_is_idle(rq))
+ uclamp_rq_clear_idle(rq);
}
static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
@@ -1958,8 +1958,8 @@ static inline void uclamp_rq_reinc_id(struct rq *rq, struct task_struct *p,
* Make sure to clear the idle flag if we've transiently reached 0
* active tasks on rq.
*/
- if (clamp_id == UCLAMP_MAX && (rq->uclamp_flags & UCLAMP_FLAG_IDLE))
- rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
+ if (clamp_id == UCLAMP_MAX && uclamp_rq_is_idle(rq))
+ uclamp_rq_clear_idle(rq);
}
static inline void
@@ -2149,7 +2149,7 @@ void init_uclamp_rq(struct rq *rq)
uclamp_rq_set(rq, clamp_id, uclamp_none(clamp_id));
}
- rq->uclamp_flags = UCLAMP_FLAG_IDLE;
+ uclamp_rq_set_idle(rq);
}
static void __init init_uclamp(void)
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 3cbaea9e29406..8fcac9b6be9cd 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3854,6 +3854,16 @@ static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
WRITE_ONCE(rq->uclamp[clamp_id].value, value);
}
+static inline void uclamp_rq_set_idle(struct rq *rq)
+{
+ rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
+}
+
+static inline void uclamp_rq_clear_idle(struct rq *rq)
+{
+ rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
+}
+
static inline bool uclamp_rq_is_idle(struct rq *rq)
{
return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v7 3/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c
2026-11-26 1:28 [PATCH v7 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-26 1:28 ` [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
2026-11-26 1:28 ` [PATCH v7 2/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
@ 2026-11-26 1:28 ` Qais Yousef
2 siblings, 0 replies; 6+ messages in thread
From: Qais Yousef @ 2026-11-26 1:28 UTC (permalink / raw)
To: Ingo Molnar, Peter Zijlstra, Vincent Guittot
Cc: Christian Loehle, Dietmar Eggemann, Steven Rostedt, John Stultz,
linux-kernel, Kayra Cizmeci, Qais Yousef
Fair and RT throttling can dequeue tasks without going through core
leading to partial dequeue and corrupt rq uclamp state. Fix the problem
by pairing uclamp_rq_{inc, dec}() with {add, sub}_nr_running() so that
each class that cares about uclamp updates the rq state along with the
nr_running. For RT use {inc, dec}_tasks() when a task is added/removed
since managing nr_running is more complicated.
Since RT group throttling doesn't actually dequeue the task but only the
group but lies about nr_running, it is a more complicated fix and there
are proposals to mark it as broken, so this is not handled and left
alone.
Remove the sched_delayed guards from uclamp_rq_{inc, dec}() since we now
react at the appropriate time to do the operations and no longer need to
care about this detail. With this gone, the flags parameter can be
dropped too.
Signed-off-by: Qais Yousef <qyousef@layalina.io>
---
kernel/sched/core.c | 33 +++++++++------------------------
kernel/sched/ext/ext.c | 6 ++----
kernel/sched/fair.c | 6 ++----
kernel/sched/rt.c | 10 ++++++----
kernel/sched/sched.h | 8 ++++----
5 files changed, 23 insertions(+), 40 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 04c396435413a..8994e27f8582d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1894,7 +1894,7 @@ static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
}
}
-static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags)
+void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
{
enum uclamp_id clamp_id;
@@ -1907,13 +1907,6 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
if (!uclamp_is_used())
return;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
-
- /* Only inc the delayed task which being woken up. */
- if (p->se.sched_delayed && !(flags & ENQUEUE_DELAYED))
- return;
-
for_each_clamp_id(clamp_id)
uclamp_rq_inc_id(rq, p, clamp_id);
@@ -1922,7 +1915,7 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
uclamp_rq_clear_idle(rq);
}
-static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
+void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
{
enum uclamp_id clamp_id;
@@ -1935,12 +1928,6 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
if (!uclamp_is_used())
return;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
-
- if (p->se.sched_delayed)
- return;
-
for_each_clamp_id(clamp_id)
uclamp_rq_dec_id(rq, p, clamp_id);
}
@@ -2178,8 +2165,6 @@ static void __init init_uclamp(void)
}
#else /* !CONFIG_UCLAMP_TASK: */
-static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags) { }
-static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
static inline void uclamp_fork(struct task_struct *p) { }
static inline void uclamp_post_fork(struct task_struct *p) { }
static inline void init_uclamp(void) { }
@@ -2215,11 +2200,10 @@ void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
update_rq_clock(rq);
/*
- * Can be before ->enqueue_task() because uclamp considers the
- * ENQUEUE_DELAYED task before its ->sched_delayed gets cleared
- * in ->enqueue_task().
+ * Need to be careful what operations are done here, some classes like
+ * fair and RT can enqueue tasks without going through core due to some
+ * features like throttling.
*/
- uclamp_rq_inc(rq, p, flags);
p->sched_class->enqueue_task(rq, p, flags);
@@ -2249,10 +2233,11 @@ inline bool dequeue_task(struct rq *rq, struct task_struct *p, int flags)
psi_dequeue(p, flags);
/*
- * Must be before ->dequeue_task() because ->dequeue_task() can 'fail'
- * and mark the task ->sched_delayed.
+ * Need to be careful what operations are done here, some classes like
+ * fair and RT can dequeue tasks without going through core due to some
+ * features like throttling.
*/
- uclamp_rq_dec(rq, p);
+
return p->sched_class->dequeue_task(rq, p, flags);
}
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index a9e36b5bb0386..1dc35dbd42df1 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -2496,6 +2496,7 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_
p->scx.flags |= SCX_TASK_QUEUED;
rq->scx.nr_running++;
add_nr_running(rq, 1);
+ uclamp_rq_inc(rq, p);
if (SCX_HAS_OP(sch, runnable) && !task_on_rq_migrating(p))
SCX_CALL_OP_TASK(sch, runnable, rq, p, enq_flags);
@@ -2647,6 +2648,7 @@ static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_
p->scx.flags &= ~SCX_TASK_QUEUED;
rq->scx.nr_running--;
sub_nr_running(rq, 1);
+ uclamp_rq_dec(rq, p);
scx_dispatch_dequeue(rq, p);
p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK;
@@ -5922,10 +5924,6 @@ DEFINE_SCHED_CLASS(ext) = {
.prio_changed = prio_changed_scx,
.update_curr = update_curr_scx,
-
-#ifdef CONFIG_UCLAMP_TASK
- .uclamp_enabled = 1,
-#endif
};
s32 scx_init_dsq(struct scx_dispatch_q *dsq, u64 dsq_id, struct scx_sched *sch)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 731d78155e654..dde12d9e1af4d 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8266,6 +8266,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
/* At this point se is NULL and we are at root level*/
add_nr_running(rq, 1);
+ uclamp_rq_inc(rq, p);
/*
* Since new tasks are assigned an initial util_avg equal to
@@ -8385,6 +8386,7 @@ static bool __dequeue_task(struct rq *rq, struct task_struct *p, int flags)
__dequeue_entity(cfs_rq, se);
sub_nr_running(rq, 1);
+ uclamp_rq_dec(rq, p);
/* balance early to pull high priority tasks */
if (unlikely(!was_sched_idle && sched_idle_rq(rq)))
@@ -15962,10 +15964,6 @@ DEFINE_SCHED_CLASS(fair) = {
#ifdef CONFIG_SCHED_CORE
.task_is_throttled = task_is_throttled_fair,
#endif
-
-#ifdef CONFIG_UCLAMP_TASK
- .uclamp_enabled = 1,
-#endif
};
#define SCHED_DEBUG_MAX_ITER 4096
diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 81a71aa5f094a..f353add1cc854 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -1189,6 +1189,9 @@ void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
inc_rt_prio(rt_rq, prio);
inc_rt_group(rt_se, rt_rq);
+
+ if (rt_entity_is_task(rt_se))
+ uclamp_rq_inc(rq_of_rt_rq(rt_rq), rt_task_of(rt_se));
}
static inline
@@ -1201,6 +1204,9 @@ void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
dec_rt_prio(rt_rq, rt_se_prio(rt_se));
dec_rt_group(rt_se, rt_rq);
+
+ if (rt_entity_is_task(rt_se))
+ uclamp_rq_dec(rq_of_rt_rq(rt_rq), rt_task_of(rt_se));
}
/*
@@ -2644,10 +2650,6 @@ DEFINE_SCHED_CLASS(rt) = {
#ifdef CONFIG_SCHED_CORE
.task_is_throttled = task_is_throttled_rt,
#endif
-
-#ifdef CONFIG_UCLAMP_TASK
- .uclamp_enabled = 1,
-#endif
};
#ifdef CONFIG_RT_GROUP_SCHED
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 8fcac9b6be9cd..f6ff4e78fce0e 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2729,10 +2729,6 @@ enum snt_e {
struct sched_class {
-#ifdef CONFIG_UCLAMP_TASK
- int uclamp_enabled;
-#endif
-
/*
* move_queued_task/activate_task/enqueue_task: rq->lock
* ttwu_do_activate/activate_task/enqueue_task: rq->lock
@@ -3816,6 +3812,8 @@ static inline unsigned long cpu_util_rt(struct rq *rq)
unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
void init_uclamp_rq(struct rq *rq);
+void uclamp_rq_inc(struct rq *rq, struct task_struct *p);
+void uclamp_rq_dec(struct rq *rq, struct task_struct *p);
/*
* When uclamp is compiled in, the aggregation at rq level is 'turned off'
@@ -3925,6 +3923,8 @@ uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
}
static inline void init_uclamp_rq(struct rq *rq) {}
+static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) {}
+static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) {}
static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
2026-11-26 1:28 ` [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
@ 2026-11-26 10:05 ` Christian Loehle
2026-11-26 11:48 ` Qais Yousef
0 siblings, 1 reply; 6+ messages in thread
From: Christian Loehle @ 2026-11-26 10:05 UTC (permalink / raw)
To: Qais Yousef, Ingo Molnar, Peter Zijlstra, Vincent Guittot
Cc: Dietmar Eggemann, Steven Rostedt, John Stultz, linux-kernel,
Kayra Cizmeci, Wei Wang
On 11/26/26 01:28, 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.
>
> The reported case seems to happen due to callsites on the same CPU not
> seeing the flipped state.
>
> Update init_uclamp_rq() so that we do uclamp_rq_set() to ensure
> WRITE_ONCE() is used to update the value field and keep KCSAN happy.
>
> Plugging the race is hard as discussed in [1]. Take it for granted and
> do correction actions on going to idle to guarantee this state is
> transient for a short period after uclamp_sched_used is enabled.
>
> [1] https://lore.kernel.org/lkml/CANDhNCqyXsZ6G77z+9s=rTO6BZifRG9+hVxMTah+tAv6pB6h3w@mail.gmail.com/
>
> Reported-by: Wei Wang <wvw@google.com>
> Signed-off-by: Qais Yousef <qyousef@layalina.io>
> ---
> kernel/sched/core.c | 7 +++----
> kernel/sched/idle.c | 19 +++++++++++++++++++
> kernel/sched/sched.h | 3 +++
> 3 files changed, 25 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index ea89471d4743c..23009278cc9a4 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2139,15 +2139,14 @@ 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;
>
> for_each_clamp_id(clamp_id) {
> - uc_rq[clamp_id] = (struct uclamp_rq) {
> - .value = uclamp_none(clamp_id)
> - };
> + memset(uc_rq[clamp_id].bucket, 0, sizeof(uc_rq[clamp_id].bucket));
nit: indent
> + uclamp_rq_set(rq, clamp_id, uclamp_none(clamp_id));
So I did reproduce WARN_ON_ONCE(!bucket->tasks) on CONFIG_UCLAMP_TASK_GROUP=y, which
relies on .active being in sync with bucket->active.
FWIW I don't see a good way of mitigating the missed decrement for .active,
but if somebody else sees it please shout.
I'll post my proposal in a minute
> }
>
> rq->uclamp_flags = UCLAMP_FLAG_IDLE;
> diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
> index 451b4846219d5..c3882e3fbdf78 100644
> --- a/kernel/sched/idle.c
> +++ b/kernel/sched/idle.c
> @@ -506,6 +506,25 @@ 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.
> + *
> + * The reset will cause the uclamp_max from uclamp_idle_value() to be
> + * lost. Since we are imbalanced, hard to know what is right. So just
> + * cleanup. This should be a transient error state once we recover
> + * everything should work as expected.
> + *
> + * We need to be careful of some paths that can force idle with tasks
> + * are still queued.
> + */
> + if (uclamp_is_used() &&
> + !rq->nr_running && unlikely(!uclamp_rq_is_idle(rq)))
> + 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 32f76551a0c5e..3cbaea9e29406 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -3815,6 +3815,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'
> @@ -3913,6 +3914,8 @@ uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
> return SCHED_CAPACITY_SCALE;
> }
>
> +static inline 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)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
2026-11-26 10:05 ` Christian Loehle
@ 2026-11-26 11:48 ` Qais Yousef
0 siblings, 0 replies; 6+ messages in thread
From: Qais Yousef @ 2026-11-26 11:48 UTC (permalink / raw)
To: Christian Loehle
Cc: Ingo Molnar, Peter Zijlstra, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, John Stultz, linux-kernel, Kayra Cizmeci,
Wei Wang
> > -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;
> >
> > for_each_clamp_id(clamp_id) {
> > - uc_rq[clamp_id] = (struct uclamp_rq) {
> > - .value = uclamp_none(clamp_id)
> > - };
> > + memset(uc_rq[clamp_id].bucket, 0, sizeof(uc_rq[clamp_id].bucket));
>
> nit: indent
Hmm I thought I fixed that before posting.
>
> > + uclamp_rq_set(rq, clamp_id, uclamp_none(clamp_id));
>
> So I did reproduce WARN_ON_ONCE(!bucket->tasks) on CONFIG_UCLAMP_TASK_GROUP=y, which
> relies on .active being in sync with bucket->active.
More details will make following your thoughts easier.
> FWIW I don't see a good way of mitigating the missed decrement for .active,
> but if somebody else sees it please shout.
> I'll post my proposal in a minute
Please send it on top of patch 2 and 3 at least as they are good clean ups that
I'd like them in still. I think the two approaches are not mutually exclusive
and the maintainers can pick both this patch and your other proposal
potentially.
Given that this imbalance can only (if at all) happen once after boot I don't
think that WARN_ON_ONCE() is consequential assuming we fail safe - which
I think we do but since you weren't specific about the path and I didn't go and
try to untangle it, I could have missed something.
FWIW, I think since there are no correctness issue but inconvenient _expected_
harmless warning, we are good. It could just make debugging harder if this
warning is due to the race or a true bug. All the sensible choices have some
imperfections..
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-26 11:48 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 1:28 [PATCH v7 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-26 1:28 ` [PATCH v7 1/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
2026-11-26 10:05 ` Christian Loehle
2026-11-26 11:48 ` Qais Yousef
2026-11-26 1:28 ` [PATCH v7 2/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
2026-11-26 1:28 ` [PATCH v7 3/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
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®