[PATCH v9 3/3] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
From: Qais Yousef
Date: Sun Nov 29 2026 - 13:41:13 EST
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@xxxxxxxxxxxxxx/
Reported-by: Wei Wang <wvw@xxxxxxxxxx>
Fixes: 46609ce22703 ("sched/uclamp: Protect uclamp fast path code with static key")
Signed-off-by: Qais Yousef <qyousef@xxxxxxxxxxx>
---
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