[PATCH -next 4/4] cpuset: replace cpuset_lock() with guard_cpuset()

From: Chen Ridong
Date: Fri Aug 08 2025 - 05:40:32 EST


From: Chen Ridong <chenridong@xxxxxxxxxx>

The guard_cpuset() is introduced, we can replace all cpuset_lock()
usage with it and remove the cpuset_lock().

Signed-off-by: Chen Ridong <chenridong@xxxxxxxxxx>
---
include/linux/cpuset.h | 2 --
kernel/cgroup/cpuset.c | 10 ----------
kernel/sched/syscalls.c | 15 +++++----------
3 files changed, 5 insertions(+), 22 deletions(-)

diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h
index 6153de28acf0..1baf12f4be19 100644
--- a/include/linux/cpuset.h
+++ b/include/linux/cpuset.h
@@ -72,8 +72,6 @@ extern void cpuset_force_rebuild(void);
extern void cpuset_update_active_cpus(void);
extern void inc_dl_tasks_cs(struct task_struct *task);
extern void dec_dl_tasks_cs(struct task_struct *task);
-extern void cpuset_lock(void);
-extern void cpuset_unlock(void);
extern void guard_cpuset(void);
extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
extern bool cpuset_cpus_allowed_fallback(struct task_struct *p);
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 110d2b93ff96..04ed73d0887e 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -250,16 +250,6 @@ static struct cpuset top_cpuset = {

static DEFINE_MUTEX(cpuset_mutex);

-void cpuset_lock(void)
-{
- mutex_lock(&cpuset_mutex);
-}
-
-void cpuset_unlock(void)
-{
- mutex_unlock(&cpuset_mutex);
-}
-
void guard_cpuset(void)
{
guard(mutex)(&cpuset_mutex);
diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 77ae87f36e84..954f6e9af41b 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -577,8 +577,10 @@ int __sched_setscheduler(struct task_struct *p,
* information.
*/
if (dl_policy(policy) || dl_policy(p->policy)) {
- cpuset_locked = true;
- cpuset_lock();
+ if (!cpuset_locked) {
+ guard_cpuset();
+ cpuset_locked = true;
+ }
}

/*
@@ -660,8 +662,6 @@ int __sched_setscheduler(struct task_struct *p,
if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
policy = oldpolicy = -1;
task_rq_unlock(rq, p, &rf);
- if (cpuset_locked)
- cpuset_unlock();
goto recheck;
}

@@ -733,11 +733,8 @@ int __sched_setscheduler(struct task_struct *p,
head = splice_balance_callbacks(rq);
task_rq_unlock(rq, p, &rf);

- if (pi) {
- if (cpuset_locked)
- cpuset_unlock();
+ if (pi)
rt_mutex_adjust_pi(p);
- }

/* Run balance callbacks after we've adjusted the PI chain: */
balance_callbacks(rq, head);
@@ -747,8 +744,6 @@ int __sched_setscheduler(struct task_struct *p,

unlock:
task_rq_unlock(rq, p, &rf);
- if (cpuset_locked)
- cpuset_unlock();
return retval;
}

--
2.34.1