Skip to content

Commit 4bc4582

Browse files
Xuewen YanIngo Molnar
authored andcommitted
sched/uclamp: Optimize sched_uclamp_used static key enabling
Repeat calls of static_branch_enable() to an already enabled static key introduce overhead, because it calls cpus_read_lock(). Users may frequently set the uclamp value of tasks, triggering the repeat enabling of the sched_uclamp_used static key. Optimize this and avoid repeat calls to static_branch_enable() by checking whether it's enabled already. [ mingo: Rewrote the changelog for legibility ] Signed-off-by: Xuewen Yan <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Christian Loehle <[email protected]> Reviewed-by: Vincent Guittot <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5fca5a4 commit 4bc4582

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

kernel/sched/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,12 +1941,12 @@ static int sysctl_sched_uclamp_handler(const struct ctl_table *table, int write,
19411941
}
19421942

19431943
if (update_root_tg) {
1944-
static_branch_enable(&sched_uclamp_used);
1944+
sched_uclamp_enable();
19451945
uclamp_update_root_tg();
19461946
}
19471947

19481948
if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) {
1949-
static_branch_enable(&sched_uclamp_used);
1949+
sched_uclamp_enable();
19501950
uclamp_sync_util_min_rt_default();
19511951
}
19521952

@@ -9294,7 +9294,7 @@ static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
92949294
if (req.ret)
92959295
return req.ret;
92969296

9297-
static_branch_enable(&sched_uclamp_used);
9297+
sched_uclamp_enable();
92989298

92999299
guard(mutex)(&uclamp_mutex);
93009300
guard(rcu)();

kernel/sched/sched.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,6 +3407,18 @@ static inline bool uclamp_is_used(void)
34073407
return static_branch_likely(&sched_uclamp_used);
34083408
}
34093409

3410+
/*
3411+
* Enabling static branches would get the cpus_read_lock(),
3412+
* check whether uclamp_is_used before enable it to avoid always
3413+
* calling cpus_read_lock(). Because we never disable this
3414+
* static key once enable it.
3415+
*/
3416+
static inline void sched_uclamp_enable(void)
3417+
{
3418+
if (!uclamp_is_used())
3419+
static_branch_enable(&sched_uclamp_used);
3420+
}
3421+
34103422
static inline unsigned long uclamp_rq_get(struct rq *rq,
34113423
enum uclamp_id clamp_id)
34123424
{
@@ -3486,6 +3498,8 @@ static inline bool uclamp_is_used(void)
34863498
return false;
34873499
}
34883500

3501+
static inline void sched_uclamp_enable(void) {}
3502+
34893503
static inline unsigned long
34903504
uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
34913505
{

kernel/sched/syscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static int uclamp_validate(struct task_struct *p,
368368
* blocking operation which obviously cannot be done while holding
369369
* scheduler locks.
370370
*/
371-
static_branch_enable(&sched_uclamp_used);
371+
sched_uclamp_enable();
372372

373373
return 0;
374374
}

0 commit comments

Comments
 (0)