* [PATCH v9 0/3] Fix uclamp race against non atomic static key enable
@ 2026-11-29 18:40 Qais Yousef
2026-11-29 18:40 ` [PATCH v9 1/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Qais Yousef @ 2026-11-29 18:40 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
More sashiko races addressed..
Changes since v8:
- Hopefully fix the last race reported by sashiko in
uclamp_update_active() for delayed and throttled tasks.
Changes since v7:
- Reorder the series so the fair bandwidth control fix is before
the static key fix.
- Address Sashiko's comments about potentially missing cpufreq
updates and not handling delayed tasks properly.
- Handle Christian's point about the WARN_ON_ONCE() if
uclamp_update_active() is called when a sleeping task has
imbalanced .active set due to static key race condition.
- Re-introduced the Fixes tag.
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: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c
sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
kernel/sched/core.c | 68 ++++++++++++++++++++----------------------
kernel/sched/ext/ext.c | 6 ++--
kernel/sched/fair.c | 16 +++++-----
kernel/sched/idle.c | 19 ++++++++++++
kernel/sched/rt.c | 10 ++++---
kernel/sched/sched.h | 22 +++++++++++---
6 files changed, 86 insertions(+), 55 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v9 1/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
2026-11-29 18:40 [PATCH v9 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
@ 2026-11-29 18:40 ` Qais Yousef
2026-11-29 18:40 ` [PATCH v9 2/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
2026-11-29 18:40 ` [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
2 siblings, 0 replies; 5+ messages in thread
From: Qais Yousef @ 2026-11-29 18:40 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>
Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org>
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 ea89471d4743c..0062df1fdaaba 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
@@ -2150,7 +2150,7 @@ static void __init init_uclamp_rq(struct rq *rq)
};
}
- 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 32f76551a0c5e..7b7291da1fde4 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3853,6 +3853,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] 5+ messages in thread
* [PATCH v9 2/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c
2026-11-29 18:40 [PATCH v9 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-29 18:40 ` [PATCH v9 1/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
@ 2026-11-29 18:40 ` Qais Yousef
2026-11-29 18:40 ` [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
2 siblings, 0 replies; 5+ messages in thread
From: Qais Yousef @ 2026-11-29 18:40 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 bandwidth control can dequeue tasks without going through core
leading to partial dequeue and corrupt rq uclamp state. Fix the problem
by moving uclamp management to each class enqueue/dequeue path so that
the accounting is handled by the class like nr_running.
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 | 12 ++++++------
kernel/sched/rt.c | 10 ++++++----
kernel/sched/sched.h | 9 +++++----
5 files changed, 28 insertions(+), 42 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 0062df1fdaaba..770fa669e8494 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);
}
@@ -2179,8 +2166,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) { }
@@ -2216,11 +2201,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 can enqueue tasks without going through core due to features
+ * like CFS bandwidth throttling.
*/
- uclamp_rq_inc(rq, p, flags);
p->sched_class->enqueue_task(rq, p, flags);
@@ -2250,10 +2234,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 can dequeue tasks without going through core due to features
+ * like CFS bandwidth throttling or delayed dequeue.
*/
- 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..ded8c9bce0822 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);
@@ -2613,6 +2614,7 @@ static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_
return true;
}
+ uclamp_rq_dec(rq, p);
ops_dequeue(rq, p, deq_flags);
/*
@@ -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..847ddee276e3a 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -8223,8 +8223,10 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
* Let's add the task's estimated utilization to the cfs_rq's
* estimated utilization, before we update schedutil.
*/
- if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED))
+ if (!p->se.sched_delayed || (flags & ENQUEUE_DELAYED)) {
util_est_enqueue(cfs_rq, p);
+ uclamp_rq_inc(rq, p);
+ }
update_curr_eevdf(cfs_rq);
@@ -8423,8 +8425,10 @@ static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
return true;
}
- if (!p->se.sched_delayed)
+ if (!p->se.sched_delayed) {
util_est_dequeue(&rq->cfs, p);
+ uclamp_rq_dec(rq, p);
+ }
if (!__dequeue_task(rq, p, flags))
return false;
@@ -15962,10 +15966,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 7b7291da1fde4..b5ccb60ebe8ab 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
@@ -3815,6 +3811,8 @@ 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 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'
@@ -3923,6 +3921,9 @@ uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
return SCHED_CAPACITY_SCALE;
}
+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; }
static inline bool uclamp_is_used(void)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
2026-11-29 18:40 [PATCH v9 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-29 18:40 ` [PATCH v9 1/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
2026-11-29 18:40 ` [PATCH v9 2/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
@ 2026-11-29 18:40 ` Qais Yousef
2026-11-29 23:03 ` kernel test robot
2 siblings, 1 reply; 5+ messages in thread
From: Qais Yousef @ 2026-11-29 18:40 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.
Tasks can still end up with an enqueue without a dequeue and with this
patch can trigger WARN_ON_ONCE(!bucket->tasks) in uclamp_rq_dec_id().
This can happen if uclamp_update_active() is called for a sleeping,
delayed or throttled task with a stale uclamp[].active flag. Fix this by
skipping the reinc for such tasks and resetting their active flag.
[1] https://lore.kernel.org/lkml/CANDhNCqyXsZ6G77z+9s=rTO6BZifRG9+hVxMTah+tAv6pB6h3w@mail.gmail.com/
Reported-by: Wei Wang <wvw@google.com>
Fixes: 46609ce22703 ("sched/uclamp: Protect uclamp fast path code with static key")
Signed-off-by: Qais Yousef <qyousef@layalina.io>
---
kernel/sched/core.c | 21 +++++++++++++++++----
kernel/sched/fair.c | 4 ++--
kernel/sched/idle.c | 19 +++++++++++++++++++
kernel/sched/sched.h | 3 +++
4 files changed, 41 insertions(+), 6 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 770fa669e8494..ab6de35b89045 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1971,10 +1971,24 @@ uclamp_update_active(struct task_struct *p)
* If the task is not yet RUNNABLE and its task_struct is not
* affecting a valid clamp bucket, the next time it's enqueued,
* it will already see the updated clamp bucket value.
+ *
+ * Skip tasks that are not queued on rq, or have already been
+ * decremented by delayed dequeue or CFS bandwidth throttling; if a
+ * task missed uclamp_rq_dec() during the static-key enabling race,
+ * clear its stale active flag instead of decrementing a potentially
+ * reset rq.
*/
+ if (!task_on_rq_queued(p) || p->se.sched_delayed ||
+ task_is_throttled(p)) {
+ for_each_clamp_id(clamp_id)
+ p->uclamp[clamp_id].active = false;
+ goto unlock;
+ }
+
for_each_clamp_id(clamp_id)
uclamp_rq_reinc_id(rq, p, clamp_id);
+unlock:
task_rq_unlock(rq, p, &rf);
}
@@ -2126,15 +2140,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));
}
uclamp_rq_set_idle(rq);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 847ddee276e3a..479707e252317 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -6932,7 +6932,7 @@ static inline int lb_throttled_hierarchy(struct task_struct *p, int dst_cpu)
return throttled_hierarchy(tg_cfs_rq(task_group(p), dst_cpu));
}
-static inline bool task_is_throttled(struct task_struct *p)
+bool task_is_throttled(struct task_struct *p)
{
return cfs_bandwidth_used() && p->throttled;
}
@@ -7945,7 +7945,7 @@ static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
static inline void sync_throttle(struct task_group *tg, int cpu) {}
static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
static void task_throttle_setup_work(struct task_struct *p) {}
-static bool task_is_throttled(struct task_struct *p) { return false; }
+bool task_is_throttled(struct task_struct *p) { return false; }
static void dequeue_throttled_task(struct task_struct *p, int flags) {}
static bool enqueue_throttled_task(struct task_struct *p) { return false; }
static void record_throttle_clock(struct cfs_rq *cfs_rq) {}
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 b5ccb60ebe8ab..40a39f0a2731b 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -580,6 +580,7 @@ extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
extern bool cfs_task_bw_constrained(struct task_struct *p);
+extern bool task_is_throttled(struct task_struct *p);
extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
struct sched_rt_entity *rt_se, int cpu,
@@ -3811,6 +3812,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);
void uclamp_rq_inc(struct rq *rq, struct task_struct *p);
void uclamp_rq_dec(struct rq *rq, struct task_struct *p);
@@ -3921,6 +3923,7 @@ 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 void uclamp_rq_inc(struct rq *rq, struct task_struct *p) {}
static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) {}
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
2026-11-29 18:40 ` [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
@ 2026-11-29 23:03 ` kernel test robot
0 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2026-11-29 23:03 UTC (permalink / raw)
To: Qais Yousef, Ingo Molnar, Vincent Guittot
Cc: llvm, oe-kbuild-all, Christian Loehle, Dietmar Eggemann,
Steven Rostedt, John Stultz, linux-kernel, Kayra Cizmeci,
Qais Yousef, Wei Wang
Hi Qais,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/sched/core]
[also build test WARNING on tip/master peterz-queue/sched/core linus/master v7.4-rc4 next-20261127]
[cannot apply to tip/auto-latest]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Qais-Yousef/sched-uclamp-Use-wrappers-instead-of-open-coding-UCLAMP_FLAG_IDLE/20261129-184050
base: tip/sched/core
patch link: https://lore.kernel.org/r/20261129184052.15594-4-qyousef%40layalina.io
patch subject: [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
config: s390-allnoconfig (https://download.01.org/0day-ci/archive/20261130/20261130065142.jL2yjqPL-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 04597ff5c4eda9d66da39a536aa26a7bab2fb2dd)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261130/20261130065142.jL2yjqPL-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261130065142.jL2yjqPL-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> kernel/sched/fair.c:7948:6: warning: no previous prototype for function 'task_is_throttled' [-Wmissing-prototypes]
7948 | bool task_is_throttled(struct task_struct *p) { return false; }
| ^
kernel/sched/fair.c:7948:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
7948 | bool task_is_throttled(struct task_struct *p) { return false; }
| ^
| static
1 warning generated.
vim +/task_is_throttled +7948 kernel/sched/fair.c
7942
7943 static bool account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) { return false; }
7944 static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
7945 static inline void sync_throttle(struct task_group *tg, int cpu) {}
7946 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
7947 static void task_throttle_setup_work(struct task_struct *p) {}
> 7948 bool task_is_throttled(struct task_struct *p) { return false; }
7949 static void dequeue_throttled_task(struct task_struct *p, int flags) {}
7950 static bool enqueue_throttled_task(struct task_struct *p) { return false; }
7951 static void record_throttle_clock(struct cfs_rq *cfs_rq) {}
7952
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-29 23:03 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 18:40 [PATCH v9 0/3] Fix uclamp race against non atomic static key enable Qais Yousef
2026-11-29 18:40 ` [PATCH v9 1/3] sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE Qais Yousef
2026-11-29 18:40 ` [PATCH v9 2/3] sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c Qais Yousef
2026-11-29 18:40 ` [PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq Qais Yousef
2026-11-29 23:03 ` kernel test robot
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®