Skip to contentSkip to conversation

sched/uclamp: Always using uclamp_is_used()

PATCH In review Xuewen Yan 3 participants 5 messages Feb 20, 2025 – Feb 25, 2025

Subsystems (2)
lore
Share & tools
lore

Xuewen Yan

author
#0 lore raw

PATCH v3 1/2 sched/uclamp: Always using uclamp_is_used()

Now, we have the uclamp_is_used() func to judge the uclamp enabled, so replace the static_branch_unlikely(&sched_uclamp_used) with it.

---
V3:
- no change.
---
V2:
- Correct the spelling mistakes in the commit message (Hongyan)
- Add Reviewed-by
---

---
 kernel/sched/core.c  |  4 ++--
 kernel/sched/sched.h | 28 ++++++++++++++--------------
 2 files changed, 16 insertions(+), 16 deletions(-)
kernel/sched/core.c +2 -2
index 9aecd914ac69..38a7192bfc19 100644
@@ -1757,7 +1757,7 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
 	 * The condition is constructed such that a NOP is generated when
 	 * sched_uclamp_used is disabled.
 	 */
-	if (!static_branch_unlikely(&sched_uclamp_used))
+	if (!uclamp_is_used())
 		return;
 
 	if (unlikely(!p->sched_class->uclamp_enabled))
@@ -1784,7 +1784,7 @@ static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
 	 * The condition is constructed such that a NOP is generated when
 	 * sched_uclamp_used is disabled.
 	 */
-	if (!static_branch_unlikely(&sched_uclamp_used))
+	if (!uclamp_is_used())
 		return;
 
 	if (unlikely(!p->sched_class->uclamp_enabled))
kernel/sched/sched.h +14 -14
index b93c8c3dc05a..3624fdce5536 100644
@@ -3394,6 +3394,19 @@ static inline bool update_other_load_avgs(struct rq *rq) { return false; }
 
 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
 
+/*
+ * When uclamp is compiled in, the aggregation at rq level is 'turned off'
+ * by default in the fast path and only gets turned on once userspace performs
+ * an operation that requires it.
+ *
+ * Returns true if userspace opted-in to use uclamp and aggregation at rq level
+ * hence is active.
+ */
+static inline bool uclamp_is_used(void)
+{
+	return static_branch_likely(&sched_uclamp_used);
+}
+
 static inline unsigned long uclamp_rq_get(struct rq *rq,
 					  enum uclamp_id clamp_id)
 {
@@ -3417,7 +3430,7 @@ static inline bool uclamp_rq_is_capped(struct rq *rq)
 	unsigned long rq_util;
 	unsigned long max_util;
 
-	if (!static_branch_likely(&sched_uclamp_used))
+	if (!uclamp_is_used())
 		return false;
 
 	rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
@@ -3426,19 +3439,6 @@ static inline bool uclamp_rq_is_capped(struct rq *rq)
 	return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
 }
 
-/*
- * When uclamp is compiled in, the aggregation at rq level is 'turned off'
- * by default in the fast path and only gets turned on once userspace performs
- * an operation that requires it.
- *
- * Returns true if userspace opted-in to use uclamp and aggregation at rq level
- * hence is active.
- */
-static inline bool uclamp_is_used(void)
-{
-	return static_branch_likely(&sched_uclamp_used);
-}
-
 #define for_each_clamp_id(clamp_id) \
 	for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
 
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 2/2 sched/uclamp: Add uclamp_is_used() check before enable it

Because the static_branch_enable() would get the cpus_read_lock(), and sometimes users may frequently set the uclamp value of tasks, and this operation would call the static_branch_enable() frequently, so add the uclamp_is_used() check to prevent calling the cpus_read_lock() frequently.
And to make the code more concise, add a helper function to encapsulate this and use it everywhere we enable sched_uclamp_used.

---
V3:
- update comment for helper function (Vincent)
- add Reviewed-by
---
V2:
- change some commit message;
- Add a helper function and use it everywhere when enable uclamp (Vincent)
---

---
 kernel/sched/core.c     |  6 +++---
 kernel/sched/sched.h    | 13 +++++++++++++
 kernel/sched/syscalls.c |  2 +-
 3 files changed, 17 insertions(+), 4 deletions(-)
kernel/sched/core.c +3 -3
index 38a7192bfc19..0466a2f61b99 100644
@@ -1942,12 +1942,12 @@ static int sysctl_sched_uclamp_handler(const struct ctl_table *table, int write,
 	}
 
 	if (update_root_tg) {
-		static_branch_enable(&sched_uclamp_used);
+		sched_uclamp_enable();
 		uclamp_update_root_tg();
 	}
 
 	if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) {
-		static_branch_enable(&sched_uclamp_used);
+		sched_uclamp_enable();
 		uclamp_sync_util_min_rt_default();
 	}
 
@@ -9295,7 +9295,7 @@ static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
 	if (req.ret)
 		return req.ret;
 
-	static_branch_enable(&sched_uclamp_used);
+	sched_uclamp_enable();
 
 	guard(mutex)(&uclamp_mutex);
 	guard(rcu)();
kernel/sched/sched.h +13 -0
index 3624fdce5536..98fe2c8f6961 100644
@@ -3407,6 +3407,17 @@ static inline bool uclamp_is_used(void)
 	return static_branch_likely(&sched_uclamp_used);
 }
 
+/*
+ * Enabling static branches would get the cpus_read_lock(),
+ * check uclamp_is_used before enabling it. There is no race
+ * issue because we never disable this static key once enabled.
+ */
+static inline void sched_uclamp_enable(void)
+{
+	if (!uclamp_is_used())
+		static_branch_enable(&sched_uclamp_used);
+}
+
 static inline unsigned long uclamp_rq_get(struct rq *rq,
 					  enum uclamp_id clamp_id)
 {
@@ -3486,6 +3497,8 @@ static inline bool uclamp_is_used(void)
 	return false;
 }
 
+static inline void sched_uclamp_enable(void) {}
+
 static inline unsigned long
 uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
 {
kernel/sched/syscalls.c +1 -1
index 456d339be98f..9100a77e9d79 100644
@@ -368,7 +368,7 @@ static int uclamp_validate(struct task_struct *p,
 	 * blocking operation which obviously cannot be done while holding
 	 * scheduler locks.
 	 */
-	static_branch_enable(&sched_uclamp_used);
+	sched_uclamp_enable();
 
 	return 0;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v3 2/2] sched/uclamp: Add uclamp_is_used() check before enable it

Quoted 12 lines · Xuewen Yan

Because the static_branch_enable() would get the cpus_read_lock(), and sometimes users may frequently set the uclamp value of tasks, and this operation would call the static_branch_enable() frequently, so add the uclamp_is_used() check to prevent calling the cpus_read_lock() frequently.
And to make the code more concise, add a helper function to encapsulate this and use it everywhere we enable sched_uclamp_used.

---

[...]

Review

Quoted 11 lines
(quoted diff) +11 -0
+/*
+ * Enabling static branches would get the cpus_read_lock(),
+ * check uclamp_is_used before enabling it. There is no race
+ * issue because we never disable this static key once enabled.
+ */
+static inline void sched_uclamp_enable(void)
+{
+	if (!uclamp_is_used())
+		static_branch_enable(&sched_uclamp_used);
+}
+

As I indicated in [1] I think the pattern of repeatedly enable is actually sane and what we probably should be doing is modify the static_key_enable() logic to do the early bail out logic outside of the lock. I had this code this way FWIW initially and Peter asked for it to be called unconditionally instead.

I think repeated calls to static_key_enable/disable() should be made cheap and I don't see a side effect of _replicating_ the early bail out logic outside of the lock so that if we have already enabled/disabled we just return immediately without any side effect (holding the lock in this case). I agree the hotplug lock is ugly and if we can avoid touching it when we don't really need to that would be better.

--->8---

kernel/jump_label.c +14 -0
index d9c822bbffb8..17583c98c447 100644
@@ -214,6 +214,13 @@ EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
 
 void static_key_enable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }
+
        cpus_read_lock();
        static_key_enable_cpuslocked(key);
        cpus_read_unlock();
@@ -239,6 +246,13 @@ EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
 
 void static_key_disable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }
+
        cpus_read_lock();
        static_key_disable_cpuslocked(key);
        cpus_read_unlock();

--->8---

[1] https://lore.kernel.org/all/20250222233627.3yx55ks5lnq3elby@airbuntu/

#3 lore raw

Re: [PATCH v3 2/2] sched/uclamp: Add uclamp_is_used() check before enable it

Quoted 34 lines · Qais Yousef
Quoted 12 lines · Xuewen Yan

Because the static_branch_enable() would get the cpus_read_lock(), and sometimes users may frequently set the uclamp value of tasks, and this operation would call the static_branch_enable() frequently, so add the uclamp_is_used() check to prevent calling the cpus_read_lock() frequently.
And to make the code more concise, add a helper function to encapsulate this and use it everywhere we enable sched_uclamp_used.

---

[...]

Review

Quoted 11 lines
(quoted diff) +11 -0
+/*
+ * Enabling static branches would get the cpus_read_lock(),
+ * check uclamp_is_used before enabling it. There is no race
+ * issue because we never disable this static key once enabled.
+ */
+static inline void sched_uclamp_enable(void)
+{
+     if (!uclamp_is_used())
+             static_branch_enable(&sched_uclamp_used);
+}
+

As I indicated in [1] I think the pattern of repeatedly enable is actually sane and what we probably should be doing is modify the static_key_enable() logic to do the early bail out logic outside of the lock. I had this code this way FWIW initially and Peter asked for it to be called unconditionally instead.

Quoted 4 lines

I think repeated calls to static_key_enable/disable() should be made cheap and I don't see a side effect of _replicating_ the early bail out logic outside of the lock so that if we have already enabled/disabled we just return immediately without any side effect (holding the lock in this case). I agree the hotplug

Because of the jump_lable_lock(), early bailout could indeed be a good idea.

Quoted 24 lines

lock is ugly and if we can avoid touching it when we don't really need to that would be better.

--->8---

kernel/jump_label.c +7 -0
index d9c822bbffb8..17583c98c447 100644
@@ -214,6 +214,13 @@ EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
 
 void static_key_enable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }
+
        cpus_read_lock();
        static_key_enable_cpuslocked(key);
        cpus_read_unlock();

kernel/jump_label.c EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked); · line 254

Quoted 9 lines
kernel/jump_label.c +6 -0
@@ -239,6 +246,13 @@ EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
 
 void static_key_disable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }

Maybe here should be just check whether == 0, because when enabling the static key, the enable may occur to be -1.

+ if (atomic_read(&key->enabled) == 0)
+ return;

Quoted 8 lines
kernel/jump_label.c +1 -1
+
        cpus_read_lock();
        static_key_disable_cpuslocked(key);
        cpus_read_unlock();
 
--->8---

[1] https://lore.kernel.org/all/20250222233627.3yx55ks5lnq3elby@airbuntu/

BR

#4 lore raw

Re: [PATCH v3 2/2] sched/uclamp: Add uclamp_is_used() check before enable it

Quoted 35 lines · Xuewen Yan
Quoted 34 lines · Qais Yousef
Quoted 12 lines · Xuewen Yan

Because the static_branch_enable() would get the cpus_read_lock(), and sometimes users may frequently set the uclamp value of tasks, and this operation would call the static_branch_enable() frequently, so add the uclamp_is_used() check to prevent calling the cpus_read_lock() frequently.
And to make the code more concise, add a helper function to encapsulate this and use it everywhere we enable sched_uclamp_used.

---

[...]

Review

Quoted 11 lines
(quoted diff) +11 -0
+/*
+ * Enabling static branches would get the cpus_read_lock(),
+ * check uclamp_is_used before enabling it. There is no race
+ * issue because we never disable this static key once enabled.
+ */
+static inline void sched_uclamp_enable(void)
+{
+     if (!uclamp_is_used())
+             static_branch_enable(&sched_uclamp_used);
+}
+

As I indicated in [1] I think the pattern of repeatedly enable is actually sane and what we probably should be doing is modify the static_key_enable() logic to do the early bail out logic outside of the lock. I had this code this way FWIW initially and Peter asked for it to be called unconditionally instead.

Quoted 6 lines
Quoted 4 lines

I think repeated calls to static_key_enable/disable() should be made cheap and I don't see a side effect of _replicating_ the early bail out logic outside of the lock so that if we have already enabled/disabled we just return immediately without any side effect (holding the lock in this case). I agree the hotplug

Because of the jump_lable_lock(), early bailout could indeed be a good idea.

Yeah AFAICS the current code already handles the double enable, but it's embedded within the cpus_read_lock(). I don't see why they must be inside. Though I think we can't just move the code, we must replicate it outside the lock in case the two enables race. I don't think the atomic_t will protect and force one of them only to proceed. The lock will be stronger synchronization to serialize, but one of them will end up doing the actual enable and the other one will bail out after holding the lock. All future redundant enables will bail out before holding the lock safely AFAICT.

Quoted 26 lines
Quoted 26 lines

lock is ugly and if we can avoid touching it when we don't really need to that would be better.

--->8---

kernel/jump_label.c +7 -0
index d9c822bbffb8..17583c98c447 100644
@@ -214,6 +214,13 @@ EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
 
 void static_key_enable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }
+
        cpus_read_lock();
        static_key_enable_cpuslocked(key);
        cpus_read_unlock();
@@ -239,6 +246,13 @@ EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
 
Quoted 11 lines

kernel/jump_label.c EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked); · line 254

Quoted 8 lines
kernel/jump_label.c +6 -0
 void static_key_disable(struct static_key *key)
 {
+       STATIC_KEY_CHECK_USE(key);
+
+       if (atomic_read(&key->enabled) > 0) {
+               WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
+               return;
+       }

Maybe here should be just check whether == 0, because when enabling the static key, the enable may occur to be -1.

If it is -1 then the code is already being patched to enable the key and this call to enable is redundant and can be ignored, no? ie: if we check for == 0 then we'll hold the lock to bail out anyway.

There's no need for the warning though as this can trigger incorrectly, yes.

Quoted 14 lines
(quoted diff) +2 -0
+       if (atomic_read(&key->enabled) == 0)
+               return;
Quoted 9 lines
kernel/jump_label.c +1 -1
+
        cpus_read_lock();
        static_key_disable_cpuslocked(key);
        cpus_read_unlock();
 
--->8---

[1] https://lore.kernel.org/all/20250222233627.3yx55ks5lnq3elby@airbuntu/

BR