Skip to contentSkip to conversation

sched/topology: Add lock guard support

PATCH New Jemmy Wong 1 participant 2 messages Jun 13, 2025 Scheduler

lore
Share & tools
lore

Jemmy Wong

author
#0 lore raw

v2 cover sched/topology: Add lock guard support

Changes in v2:
- guard inside sched_init_domains instead of outside
- replace do{}while(0) with sched_global_validate func
- wrap commments at 80 characters

Changes in v1:
- Convert scoped_guard to guard
https://lore.kernel.org/all/[email protected]/

v0 link:
https://lore.kernel.org/all/[email protected]/

Jemmy Wong (1):
sched/topology: Add lock guard support

 include/linux/sched.h   |   9 ++--
 kernel/sched/core.c     |   2 -
 kernel/sched/debug.c    |  13 ++---
 kernel/sched/rt.c       |  47 +++++++++-------
 kernel/sched/topology.c | 117 ++++++++++++++++++----------------------
 5 files changed, 88 insertions(+), 100 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/1 sched/topology: Add lock guard support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement. No functional changes to the scheduler topology logic are introduced.

---
 include/linux/sched.h   |   9 ++--
 kernel/sched/core.c     |   2 -
 kernel/sched/debug.c    |  13 ++---
 kernel/sched/rt.c       |  47 +++++++++-------
 kernel/sched/topology.c | 117 ++++++++++++++++++----------------------
 5 files changed, 88 insertions(+), 100 deletions(-)
include/linux/sched.h +5 -4
index 4f78a64beb52..cbc3476eef0e 100644
@@ -46,6 +46,7 @@
 #include <linux/rv.h>
 #include <linux/uidgid_types.h>
 #include <linux/tracepoint-defs.h>
+#include <linux/mutex.h>
 #include <asm/kmap_size.h>
 
 /* task_struct member predeclarations (sorted alphabetically): */
@@ -398,11 +399,11 @@ enum uclamp_id {
 #ifdef CONFIG_SMP
 extern struct root_domain def_root_domain;
 extern struct mutex sched_domains_mutex;
-extern void sched_domains_mutex_lock(void);
-extern void sched_domains_mutex_unlock(void);
+DEFINE_LOCK_GUARD_0(sched_domains_mutex,
+	mutex_lock(&sched_domains_mutex),
+	mutex_unlock(&sched_domains_mutex))
 #else
-static inline void sched_domains_mutex_lock(void) { }
-static inline void sched_domains_mutex_unlock(void) { }
+DEFINE_LOCK_GUARD_0(sched_domains_mutex, ,)
 #endif
 
 struct sched_param {
kernel/sched/core.c +0 -2
index dce50fa57471..4a7fc81c0745 100644
@@ -8457,9 +8457,7 @@ void __init sched_init_smp(void)
 	 * CPU masks are stable and all blatant races in the below code cannot
 	 * happen.
 	 */
-	sched_domains_mutex_lock();
 	sched_init_domains(cpu_active_mask);
-	sched_domains_mutex_unlock();
 
 	/* Move init over to a non-isolated CPU */
 	if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_TYPE_DOMAIN)) < 0)
kernel/sched/debug.c +5 -8
index 9d71baf08075..1c00016fd54c 100644
@@ -293,8 +293,8 @@ static ssize_t sched_verbose_write(struct file *filp, const char __user *ubuf,
 	ssize_t result;
 	bool orig;
 
-	cpus_read_lock();
-	sched_domains_mutex_lock();
+	guard(cpus_read_lock)();
+	guard(sched_domains_mutex)();
 
 	orig = sched_debug_verbose;
 	result = debugfs_write_file_bool(filp, ubuf, cnt, ppos);
@@ -306,9 +306,6 @@ static ssize_t sched_verbose_write(struct file *filp, const char __user *ubuf,
 		sd_dentry = NULL;
 	}
 
-	sched_domains_mutex_unlock();
-	cpus_read_unlock();
-
 	return result;
 }
 #else
@@ -517,9 +514,9 @@ static __init int sched_init_debug(void)
 	debugfs_create_u32("migration_cost_ns", 0644, debugfs_sched, &sysctl_sched_migration_cost);
 	debugfs_create_u32("nr_migrate", 0644, debugfs_sched, &sysctl_sched_nr_migrate);
 
-	sched_domains_mutex_lock();
-	update_sched_domain_debugfs();
-	sched_domains_mutex_unlock();
+	scoped_guard(sched_domains_mutex) {
+		update_sched_domain_debugfs();
+	}
 #endif
 
 #ifdef CONFIG_NUMA_BALANCING
kernel/sched/rt.c +27 -20

69 lines not on this page · raw message

kernel/sched/topology.c +51 -66

215 lines not on this page · raw message

Signature · 1 line
2.43.0