Re: [PATCH v2] sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
From: Qais Yousef
Date: Tue Oct 27 2026 - 22:11:41 EST
On 10/26/26 11:59, Peter Zijlstra wrote:
On Mon, Oct 26, 2026 at 11:24:31AM +0000, Qais Yousef wrote:
On 10/26/26 10:12, Peter Zijlstra wrote:
+static inline bool __uclamp_is_imbalanced(struct rq *rq, enum uclamp_id clamp_id)
+{
+ struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket;
+ int bucket_id = UCLAMP_BUCKETS - 1;
+
+ /*
+ * On idle all bucket.tasks must be 0, or we have hit imbalance
+ * problem.
+ */
+ for ( ; bucket_id >= 0; bucket_id--)
+ if (bucket[bucket_id].tasks)
+ return true;
+
+ return false;
+}
+
+static inline bool uclamp_is_imbalanced(struct rq *rq)
+{
+ enum uclamp_id clamp_id;
+
+ for_each_clamp_id(clamp_id)
+ if(__uclamp_is_imbalanced(rq, clamp_id))
+ return true;
+
+ return false;
+}
Anyway; I was thinking, since we have a hole in struct uclamp_rq; and
also uclamp_flags. Can we fold uclamp_flags into the hold, and also use
UCLAMP_FLAG_IDLE to detect if we need this bucket scan at all?
Actually maybe we can just check if the flag is not set then this means we
have a corruption without scanning the buckets. I didn't like the scan, but
seemed safer for a backport. Let me try that and update.
I can fold the uclamp_flags into buckets, I don't think this will grow and I
do have a path to help us get rid off uclamp_rq (hopefully) in the future.
Okay to do this as a separate follow up patch? Since this will go to LTS, I
think safer keep it minimalist and do the folding bit in a separate patch.
Sure; I just noticed these separate things while trying to re acquaint
myself with uclamp ;-)
I posted new version along with some clean up that makes this conversion slightly easier, but didn't go all the way in as the flag being per uclamp_id didn't make sense and if I moved it to uclamp_rq or uclamp_bucket it meant we'll need to set and clear the two IDs which didn't feel much better. We can discuss on the new series.
Turns out no need to scan too and can just check if the flag is not set to detect the imbalance.