From: "tip-bot2 for Xuewen Yan" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Xuewen Yan <xuewen.yan@unisoc.com>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
Christian Loehle <christian.loehle@arm.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: sched/core] sched/uclamp: Optimize sched_uclamp_used static key enabling
Date: Sat, 15 Mar 2025 09:37:01 -0000 [thread overview]
Message-ID: <174203142298.14745.10610106009885438181.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250219093747.2612-2-xuewen.yan@unisoc.com>
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 4bc45824149ea83a1e293c674e7f542b8127afb6
Gitweb: https://git.kernel.org/tip/4bc45824149ea83a1e293c674e7f542b8127afb6
Author: Xuewen Yan <xuewen.yan@unisoc.com>
AuthorDate: Wed, 19 Feb 2025 17:37:47 +08:00
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Sat, 15 Mar 2025 10:28:50 +01:00
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 <xuewen.yan@unisoc.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Christian Loehle <christian.loehle@arm.com>
Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org>
Link: https://lore.kernel.org/r/20250219093747.2612-2-xuewen.yan@unisoc.com
---
kernel/sched/core.c | 6 +++---
kernel/sched/sched.h | 14 ++++++++++++++
kernel/sched/syscalls.c | 2 +-
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 45daa41..03d7b63 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1941,12 +1941,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();
}
@@ -9294,7 +9294,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)();
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 8d42d3c..0212a0c 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3407,6 +3407,18 @@ static inline bool uclamp_is_used(void)
return static_branch_likely(&sched_uclamp_used);
}
+/*
+ * Enabling static branches would get the cpus_read_lock(),
+ * check whether uclamp_is_used before enable it to avoid always
+ * calling cpus_read_lock(). Because we never disable this
+ * static key once enable it.
+ */
+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 +3498,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)
{
diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 9f40348..c326de1 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -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;
}
next prev parent reply other threads:[~2025-03-15 9:37 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-19 9:37 [PATCH v2 1/2] sched/uclamp: Always using uclamp_is_used() Xuewen Yan
2025-02-19 9:37 ` [PATCH v2 2/2] sched/uclamp: Add uclamp_is_used() check before enable it Xuewen Yan
2025-02-19 15:59 ` Christian Loehle
2025-02-19 16:25 ` Vincent Guittot
2025-03-14 20:24 ` [tip: sched/core] " tip-bot2 for Xuewen Yan
2025-03-15 9:37 ` tip-bot2 for Xuewen Yan [this message]
2025-02-19 15:57 ` [PATCH v2 1/2] sched/uclamp: Always using uclamp_is_used() Christian Loehle
2025-03-14 20:24 ` [tip: sched/core] " tip-bot2 for Xuewen Yan
2025-03-15 9:37 ` [tip: sched/core] sched/uclamp: Use the uclamp_is_used() helper instead of open-coding it tip-bot2 for Xuewen Yan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=174203142298.14745.10610106009885438181.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=christian.loehle@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=vincent.guittot@linaro.org \
--cc=x86@kernel.org \
--cc=xuewen.yan@unisoc.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®