Skip to content

RFC sched: Add more Scope-based Resource Management Support

Jemmy Wong lkml 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v0 RFC Latest0 of 13 patches 0 repliesNew

Patches in v0 No applies recorded yet

None of the 13 patches the cover letter announces are in this archive.

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v0 yet.

Read the v0 thread

Cover letter [PATCH v0 00/13] [RFC] sched: Add more Scope-based Resource Management Support

From 0dc6cbb0bd06fe2c29999b7c6b3c2206b612d1fa Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Thu, 19 Jun 2025 03:28:50 +0800
Subject: [PATCH v0 00/13] [RFC] sched: Add more Scope-based Resource Management Support

Hi,

I greatly admire the Scope-based Resource Management infrastructure as it elegantly aligns with the Resource Acquisition Is Initialization (RAII) programming idiom, improving code safety and maintainability.

I am interested in driving a comprehensive conversion of traditional manual lock/unlock patterns to use guard/scoped_guard, starting with the sched module.

Before proceeding, I’d like to confirm if you believe this effort is valuable and whether you’d support such a conversion.

Best,
Jemmy

Jemmy Wong (13):
sched/completion: Scope-based Resource Management Support sched/dealine: Scope-based Resource Management Support sched/psi: Scope-based Resource Management Support sched/cpuacct: Scope-based Resource Management Support sched/syscalls: Scope-based Resource Management Support sched/core_sched: Scope-based Resource Management Support sched/cpudeadline: Scope-based Resource Management Support sched/cpufreq_schedutil: Scope-based Resource Management Support sched/cputime: Scope-based Resource Management Support sched/stats: Scope-based Resource Management Support sched/wait: Scope-based Resource Management Support sched/swait: Scope-based Resource Management Support sched/ext_idle: Scope-based Resource Management Support

 include/linux/completion.h       |   4 ++
 include/linux/rcupdate.h         |   4 ++
 kernel/sched/completion.c        |  32 +++------
 kernel/sched/core_sched.c        |  30 ++++----
 kernel/sched/cpuacct.c           |  12 +---
 kernel/sched/cpudeadline.c       |   9 +--
 kernel/sched/cpufreq_schedutil.c |  41 +++++------
 kernel/sched/cputime.c           |  39 +++++------
 kernel/sched/deadline.c          | 114 +++++++++++++------------------
 kernel/sched/ext_idle.c          |  29 ++++----
 kernel/sched/psi.c               |  71 ++++++-------------
 kernel/sched/sched.h             |  14 +---
 kernel/sched/stats.c             |   3 +-
 kernel/sched/swait.c             |  29 +++-----
 kernel/sched/syscalls.c          |   4 +-
 kernel/sched/wait.c              |  43 +++---------
 16 files changed, 171 insertions(+), 307 deletions(-)

--
2.43.0

From 44681a9c8dea41c8b765158e850ef08d7e6236ef Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:25:30 +0800
Subject: [PATCH v0 01/13] sched/completion: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 include/linux/completion.h |  4 ++++
 kernel/sched/completion.c  | 32 ++++++++++----------------------
 2 files changed, 14 insertions(+), 22 deletions(-)
include/linux/completion.h +4 -0
index fb2915676574..fcd987b56fa0 100644
@@ -32,6 +32,10 @@ struct completion {
 static inline void complete_acquire(struct completion *x) {}
 static inline void complete_release(struct completion *x) {}
 
+DEFINE_LOCK_GUARD_1(complete, struct completion,
+	complete_acquire(_T->lock),
+	complete_release(_T->lock))
+
 #define COMPLETION_INITIALIZER(work) \
 	{ 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
 
kernel/sched/completion.c +10 -22
index 3561ab533dd4..9d4efdce9d23 100644
@@ -15,14 +15,11 @@
 
 static void complete_with_flags(struct completion *x, int wake_flags)
 {
-	unsigned long flags;
-
-	raw_spin_lock_irqsave(&x->wait.lock, flags);
+	guard(raw_spinlock_irqsave)(&x->wait.lock);
 
 	if (x->done != UINT_MAX)
 		x->done++;
 	swake_up_locked(&x->wait, wake_flags);
-	raw_spin_unlock_irqrestore(&x->wait.lock, flags);
 }
 
 void complete_on_current_cpu(struct completion *x)
@@ -66,14 +63,11 @@ EXPORT_SYMBOL(complete);
  */
 void complete_all(struct completion *x)
 {
-	unsigned long flags;
-
 	lockdep_assert_RT_in_threaded_ctx();
 
-	raw_spin_lock_irqsave(&x->wait.lock, flags);
+	guard(raw_spinlock_irqsave)(&x->wait.lock);
 	x->done = UINT_MAX;
 	swake_up_all_locked(&x->wait);
-	raw_spin_unlock_irqrestore(&x->wait.lock, flags);
 }
 EXPORT_SYMBOL(complete_all);
 
@@ -110,13 +104,10 @@ __wait_for_common(struct completion *x,
 {
 	might_sleep();
 
-	complete_acquire(x);
-
-	raw_spin_lock_irq(&x->wait.lock);
-	timeout = do_wait_for_common(x, action, timeout, state);
-	raw_spin_unlock_irq(&x->wait.lock);
-
-	complete_release(x);
+	guard(complete)(x);
+	scoped_guard(raw_spinlock_irq, &x->wait.lock) {
+		timeout = do_wait_for_common(x, action, timeout, state);
+	}
 
 	return timeout;
 }
@@ -303,7 +294,6 @@ EXPORT_SYMBOL(wait_for_completion_killable_timeout);
  */
 bool try_wait_for_completion(struct completion *x)
 {
-	unsigned long flags;
 	bool ret = true;
 
 	/*
@@ -315,12 +305,12 @@ bool try_wait_for_completion(struct completion *x)
 	if (!READ_ONCE(x->done))
 		return false;
 
-	raw_spin_lock_irqsave(&x->wait.lock, flags);
+	guard(raw_spinlock_irqsave)(&x->wait.lock);
 	if (!x->done)
 		ret = false;
 	else if (x->done != UINT_MAX)
 		x->done--;
-	raw_spin_unlock_irqrestore(&x->wait.lock, flags);
+
 	return ret;
 }
 EXPORT_SYMBOL(try_wait_for_completion);
@@ -336,8 +326,6 @@ EXPORT_SYMBOL(try_wait_for_completion);
  */
 bool completion_done(struct completion *x)
 {
-	unsigned long flags;
-
 	if (!READ_ONCE(x->done))
 		return false;
 
@@ -346,8 +334,8 @@ bool completion_done(struct completion *x)
 	 * otherwise we can end up freeing the completion before complete()
 	 * is done referencing it.
 	 */
-	raw_spin_lock_irqsave(&x->wait.lock, flags);
-	raw_spin_unlock_irqrestore(&x->wait.lock, flags);
+	guard(raw_spinlock_irqsave)(&x->wait.lock);
+
 	return true;
 }
 EXPORT_SYMBOL(completion_done);

--
2.43.0

From 35cdf70828f8b9f9eee819c7b19846e67fe12100 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:26:00 +0800
Subject: [PATCH v0 02/13] sched/dealine: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 include/linux/rcupdate.h |   4 ++
 kernel/sched/deadline.c  | 114 ++++++++++++++++-----------------------
 2 files changed, 50 insertions(+), 68 deletions(-)
include/linux/rcupdate.h +4 -0
index 120536f4c6eb..b1aacdb89886 100644
@@ -1166,4 +1166,8 @@ DEFINE_LOCK_GUARD_0(rcu,
 	} while (0),
 	rcu_read_unlock())
 
+DEFINE_LOCK_GUARD_0(rcu_read_lock_sched,
+	rcu_read_lock_sched(),
+	rcu_read_unlock_sched())
+
 #endif /* __LINUX_RCUPDATE_H */
kernel/sched/deadline.c +46 -68

290 lines not on this page · raw message

--
2.43.0

From 314d093c8e6cc7e761092e1b5f038ed587e19a04 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:26:52 +0800
Subject: [PATCH v0 03/13] sched/psi: Scope-based Resource Management Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/psi.c   | 71 +++++++++++++-------------------------------
 kernel/sched/sched.h | 14 ++-------
 2 files changed, 23 insertions(+), 62 deletions(-)
kernel/sched/psi.c +20 -51

232 lines not on this page · raw message

kernel/sched/sched.h +3 -11
index 475bb5998295..c2929f6be25f 100644
@@ -1893,17 +1893,9 @@ DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
 		    rq_unlock_irqrestore(_T->lock, &_T->rf),
 		    struct rq_flags rf)
 
-static inline struct rq *this_rq_lock_irq(struct rq_flags *rf)
-	__acquires(rq->lock)
-{
-	struct rq *rq;
-
-	local_irq_disable();
-	rq = this_rq();
-	rq_lock(rq, rf);
-
-	return rq;
-}
+DEFINE_LOCK_GUARD_1(raw_spin_rq_lock_irq, struct rq,
+		    raw_spin_rq_lock_irq(_T->lock),
+		    raw_spin_rq_unlock_irq(_T->lock))
 
 #ifdef CONFIG_NUMA
 

--
2.43.0

From 409c82ef25583f35ec53d0f574484d3c3c4fb5f1 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:27:13 +0800
Subject: [PATCH v0 04/13] sched/cpuacct: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/cpuacct.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
kernel/sched/cpuacct.c +2 -10
index 0de9dda09949..c6ba078ae4f6 100644
@@ -109,7 +109,7 @@ static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
 	/*
 	 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
 	 */
-	raw_spin_rq_lock_irq(cpu_rq(cpu));
+	guard(raw_spin_rq_lock_irq)(cpu_rq(cpu));
 #endif
 
 	switch (index) {
@@ -125,10 +125,6 @@ static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
 		break;
 	}
 
-#ifndef CONFIG_64BIT
-	raw_spin_rq_unlock_irq(cpu_rq(cpu));
-#endif
-
 	return data;
 }
 
@@ -145,16 +141,12 @@ static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu)
 	/*
 	 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
 	 */
-	raw_spin_rq_lock_irq(cpu_rq(cpu));
+	guard(raw_spin_rq_lock_irq)(cpu_rq(cpu));
 #endif
 	*cpuusage = 0;
 	cpustat[CPUTIME_USER] = cpustat[CPUTIME_NICE] = 0;
 	cpustat[CPUTIME_SYSTEM] = cpustat[CPUTIME_IRQ] = 0;
 	cpustat[CPUTIME_SOFTIRQ] = 0;
-
-#ifndef CONFIG_64BIT
-	raw_spin_rq_unlock_irq(cpu_rq(cpu));
-#endif
 }
 
 /* Return total CPU usage (in nanoseconds) of a group */

--
2.43.0

From 903c2521f9a1a8020033614c7f6733c8cafc1968 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:27:31 +0800
Subject: [PATCH v0 05/13] sched/syscalls: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/syscalls.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/syscalls.c +2 -2
index 547c1f05b667..cc677f5845c2 100644
@@ -1353,9 +1353,9 @@ SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
 static void do_sched_yield(void)
 {
 	struct rq_flags rf;
-	struct rq *rq;
+	struct rq *rq = this_rq();
 
-	rq = this_rq_lock_irq(&rf);
+	rq_lock_irq(rq, &rf);
 
 	schedstat_inc(rq->yld_count);
 	current->sched_class->yield_task(rq);

--
2.43.0

From b9859f2d49c79f25702406e28504aac8603050dc Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:27:59 +0800
Subject: [PATCH v0 06/13] sched/core_sched: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/core_sched.c | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)
kernel/sched/core_sched.c +12 -18
index c4606ca89210..4dc6fdc8bae0 100644
@@ -54,11 +54,10 @@ static unsigned long sched_core_update_cookie(struct task_struct *p,
 					      unsigned long cookie)
 {
 	unsigned long old_cookie;
-	struct rq_flags rf;
 	struct rq *rq;
 
-	rq = task_rq_lock(p, &rf);
-
+	CLASS(task_rq_lock, guard)(p);
+	rq = guard.rq;
 	/*
 	 * Since creating a cookie implies sched_core_get(), and we cannot set
 	 * a cookie until after we've created it, similarly, we cannot destroy
@@ -91,18 +90,15 @@ static unsigned long sched_core_update_cookie(struct task_struct *p,
 	if (task_on_cpu(rq, p))
 		resched_curr(rq);
 
-	task_rq_unlock(rq, p, &rf);
-
 	return old_cookie;
 }
 
 static unsigned long sched_core_clone_cookie(struct task_struct *p)
 {
-	unsigned long cookie, flags;
+	unsigned long cookie;
 
-	raw_spin_lock_irqsave(&p->pi_lock, flags);
+	guard(raw_spinlock_irqsave)(&p->pi_lock);
 	cookie = sched_core_get_cookie(p->core_cookie);
-	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
 
 	return cookie;
 }
@@ -145,18 +141,16 @@ int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type,
 	    (cmd != PR_SCHED_CORE_GET && uaddr))
 		return -EINVAL;
 
-	rcu_read_lock();
-	if (pid == 0) {
-		task = current;
-	} else {
-		task = find_task_by_vpid(pid);
-		if (!task) {
-			rcu_read_unlock();
-			return -ESRCH;
+	scoped_guard(rcu) {
+		if (pid == 0) {
+			task = current;
+		} else {
+			task = find_task_by_vpid(pid);
+			if (!task)
+				return -ESRCH;
 		}
+		get_task_struct(task);
 	}
-	get_task_struct(task);
-	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified

--
2.43.0

From bd17d15ad082a4288e45be1679f52cba691a8e0d Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:28:15 +0800
Subject: [PATCH v0 07/13] sched/cpudeadline: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/cpudeadline.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/sched/cpudeadline.c +2 -7
index 95baa12a1029..7959c9383e72 100644
@@ -173,11 +173,10 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p,
 void cpudl_clear(struct cpudl *cp, int cpu)
 {
 	int old_idx, new_cpu;
-	unsigned long flags;
 
 	WARN_ON(!cpu_present(cpu));
 
-	raw_spin_lock_irqsave(&cp->lock, flags);
+	guard(raw_spinlock_irqsave)(&cp->lock);
 
 	old_idx = cp->elements[cpu].idx;
 	if (old_idx == IDX_INVALID) {
@@ -197,7 +196,6 @@ void cpudl_clear(struct cpudl *cp, int cpu)
 
 		cpumask_set_cpu(cpu, cp->free_cpus);
 	}
-	raw_spin_unlock_irqrestore(&cp->lock, flags);
 }
 
 /*
@@ -213,11 +211,10 @@ void cpudl_clear(struct cpudl *cp, int cpu)
 void cpudl_set(struct cpudl *cp, int cpu, u64 dl)
 {
 	int old_idx;
-	unsigned long flags;
 
 	WARN_ON(!cpu_present(cpu));
 
-	raw_spin_lock_irqsave(&cp->lock, flags);
+	guard(raw_spinlock_irqsave)(&cp->lock);
 
 	old_idx = cp->elements[cpu].idx;
 	if (old_idx == IDX_INVALID) {
@@ -232,8 +229,6 @@ void cpudl_set(struct cpudl *cp, int cpu, u64 dl)
 		cp->elements[old_idx].dl = dl;
 		cpudl_heapify(cp, old_idx);
 	}
-
-	raw_spin_unlock_irqrestore(&cp->lock, flags);
 }
 
 /*

--
2.43.0

From 072805c802590a9e0e76cd6a0cc214fe10d7ba93 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:28:34 +0800
Subject: [PATCH v0 08/13] sched/cpufreq_schedutil: Scope-based Resource
Management Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/cpufreq_schedutil.c | 41 +++++++++++++-------------------
 1 file changed, 17 insertions(+), 24 deletions(-)
kernel/sched/cpufreq_schedutil.c +17 -24

92 lines not on this page · raw message

--
2.43.0

From 507782856b661027f8b25fe77fdd77986857295a Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:28:47 +0800
Subject: [PATCH v0 09/13] sched/cputime: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/cputime.c | 39 +++++++++++++++++----------------------
 1 file changed, 17 insertions(+), 22 deletions(-)
kernel/sched/cputime.c +17 -22
index 6dab4854c6c0..1bff83476183 100644
@@ -292,12 +292,10 @@ static inline u64 read_sum_exec_runtime(struct task_struct *t)
 static u64 read_sum_exec_runtime(struct task_struct *t)
 {
 	u64 ns;
-	struct rq_flags rf;
 	struct rq *rq;
 
-	rq = task_rq_lock(t, &rf);
+	guard(task_rq_lock)(t);
 	ns = t->se.sum_exec_runtime;
-	task_rq_unlock(rq, t, &rf);
 
 	return ns;
 }
@@ -326,7 +324,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
 	if (same_thread_group(current, tsk))
 		(void) task_sched_runtime(current);
 
-	rcu_read_lock();
+	guard(rcu)();
 	/* Attempt a lockless read on the first round. */
 	nextseq = 0;
 	do {
@@ -346,7 +344,6 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
 		nextseq = 1;
 	} while (need_seqretry(&sig->stats_lock, seq));
 	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
-	rcu_read_unlock();
 }
 
 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
@@ -984,15 +981,14 @@ u64 kcpustat_field(struct kernel_cpustat *kcpustat,
 	for (;;) {
 		struct task_struct *curr;
 
-		rcu_read_lock();
-		curr = rcu_dereference(rq->curr);
-		if (WARN_ON_ONCE(!curr)) {
-			rcu_read_unlock();
-			return cpustat[usage];
-		}
+		scoped_guard(rcu) {
+			curr = rcu_dereference(rq->curr);
+			if (WARN_ON_ONCE(!curr))
+				return cpustat[usage];
 
-		err = kcpustat_field_vtime(cpustat, curr, usage, cpu, &val);
-		rcu_read_unlock();
+			err = kcpustat_field_vtime(cpustat, curr,
+				usage, cpu, &val);
+		}
 
 		if (!err)
 			return val;
@@ -1071,16 +1067,15 @@ void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
 	for (;;) {
 		struct task_struct *curr;
 
-		rcu_read_lock();
-		curr = rcu_dereference(rq->curr);
-		if (WARN_ON_ONCE(!curr)) {
-			rcu_read_unlock();
-			*dst = *src;
-			return;
-		}
+		scoped_guard(rcu) {
+			curr = rcu_dereference(rq->curr);
+			if (WARN_ON_ONCE(!curr)) {
+				*dst = *src;
+				return;
+			}
 
-		err = kcpustat_cpu_fetch_vtime(dst, src, curr, cpu);
-		rcu_read_unlock();
+			err = kcpustat_cpu_fetch_vtime(dst, src, curr, cpu);
+		}
 
 		if (!err)
 			return;

--
2.43.0

From ee28e9e123bc4557008a38652d78de5019c41be0 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:29:04 +0800
Subject: [PATCH v0 10/13] sched/stats: Scope-based Resource Management Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/stats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/stats.c +1 -2
index 4346fd81c31f..1497a244eb40 100644
@@ -134,7 +134,7 @@ static int show_schedstat(struct seq_file *seq, void *v)
 
 #ifdef CONFIG_SMP
 		/* domain-specific stats */
-		rcu_read_lock();
+		guard(rcu)();
 		for_each_domain(cpu, sd) {
 			enum cpu_idle_type itype;
 
@@ -162,7 +162,6 @@ static int show_schedstat(struct seq_file *seq, void *v)
 			    sd->ttwu_wake_remote, sd->ttwu_move_affine,
 			    sd->ttwu_move_balance);
 		}
-		rcu_read_unlock();
 #endif
 	}
 	return 0;

--
2.43.0

From 1cea7c63e8d44b4ec5c8a3e7d7911200a4458047 Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:29:20 +0800
Subject: [PATCH v0 11/13] sched/wait: Scope-based Resource Management Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/wait.c | 43 ++++++++++---------------------------------
 1 file changed, 10 insertions(+), 33 deletions(-)
kernel/sched/wait.c +10 -33

132 lines not on this page · raw message

--
2.43.0

From c70921420ddedfa08fed87d08bcb7130e63dcd7d Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:29:35 +0800
Subject: [PATCH v0 12/13] sched/swait: Scope-based Resource Management Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/swait.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
kernel/sched/swait.c +9 -20
index 72505cd3b60a..cf4eda166cf0 100644
@@ -46,11 +46,9 @@ void swake_up_all_locked(struct swait_queue_head *q)
 
 void swake_up_one(struct swait_queue_head *q)
 {
-	unsigned long flags;
 
-	raw_spin_lock_irqsave(&q->lock, flags);
+	guard(raw_spinlock_irqsave)(&q->lock);
 	swake_up_locked(q, 0);
-	raw_spin_unlock_irqrestore(&q->lock, flags);
 }
 EXPORT_SYMBOL(swake_up_one);
 
@@ -63,9 +61,12 @@ void swake_up_all(struct swait_queue_head *q)
 	struct swait_queue *curr;
 	LIST_HEAD(tmp);
 
-	raw_spin_lock_irq(&q->lock);
-	list_splice_init(&q->task_list, &tmp);
+	scoped_guard(raw_spinlock_irq, &q->lock) {
+		list_splice_init(&q->task_list, &tmp);
+	}
 	while (!list_empty(&tmp)) {
+		guard(raw_spinlock_irq)(&q->lock);
+
 		curr = list_first_entry(&tmp, typeof(*curr), task_list);
 
 		wake_up_state(curr->task, TASK_NORMAL);
@@ -73,11 +74,7 @@ void swake_up_all(struct swait_queue_head *q)
 
 		if (list_empty(&tmp))
 			break;
-
-		raw_spin_unlock_irq(&q->lock);
-		raw_spin_lock_irq(&q->lock);
 	}
-	raw_spin_unlock_irq(&q->lock);
 }
 EXPORT_SYMBOL(swake_up_all);
 
@@ -90,21 +87,17 @@ void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
 
 void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
 {
-	unsigned long flags;
-
-	raw_spin_lock_irqsave(&q->lock, flags);
+	guard(raw_spinlock_irqsave)(&q->lock);
 	__prepare_to_swait(q, wait);
 	set_current_state(state);
-	raw_spin_unlock_irqrestore(&q->lock, flags);
 }
 EXPORT_SYMBOL(prepare_to_swait_exclusive);
 
 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
 {
-	unsigned long flags;
 	long ret = 0;
 
-	raw_spin_lock_irqsave(&q->lock, flags);
+	guard(raw_spinlock_irqsave)(&q->lock);
 	if (signal_pending_state(state, current)) {
 		/*
 		 * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
@@ -116,7 +109,6 @@ long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait
 		__prepare_to_swait(q, wait);
 		set_current_state(state);
 	}
-	raw_spin_unlock_irqrestore(&q->lock, flags);
 
 	return ret;
 }
@@ -131,14 +123,11 @@ void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
 
 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
 {
-	unsigned long flags;
-
 	__set_current_state(TASK_RUNNING);
 
 	if (!list_empty_careful(&wait->task_list)) {
-		raw_spin_lock_irqsave(&q->lock, flags);
+		guard(raw_spinlock_irqsave)(&q->lock);
 		list_del_init(&wait->task_list);
-		raw_spin_unlock_irqrestore(&q->lock, flags);
 	}
 }
 EXPORT_SYMBOL(finish_swait);

--
2.43.0

From 0dc6cbb0bd06fe2c29999b7c6b3c2206b612d1fa Mon Sep 17 00:00:00 2001
From: Jemmy Wong <[email protected]>
Date: Sat, 14 Jun 2025 15:30:26 +0800
Subject: [PATCH v0 13/13] sched/ext_idle: Scope-based Resource Management
Support

This change replaces manual lock acquisition and release with lock guards to improve code robustness and reduce the risk of lock mismanagement.

---
 kernel/sched/ext_idle.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)
kernel/sched/ext_idle.c +12 -17
index 6d29d3cbc670..0d280ab06b6d 100644
@@ -458,7 +458,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	bool is_prev_allowed;
 	s32 cpu;
 
-	preempt_disable();
+	guard(preempt)();
 
 	/*
 	 * Check whether @prev_cpu is still within the allowed set. If not,
@@ -485,7 +485,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	/*
 	 * This is necessary to protect llc_cpus.
 	 */
-	rcu_read_lock();
+	guard(rcu)();
 
 	/*
 	 * Determine the subset of CPUs that the task can use in its
@@ -528,7 +528,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		if (is_prev_allowed && cpus_share_cache(cpu, prev_cpu) &&
 		    scx_idle_test_and_clear_cpu(prev_cpu)) {
 			cpu = prev_cpu;
-			goto out_unlock;
+			return cpu;
 		}
 
 		/*
@@ -550,7 +550,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		    (!(flags & SCX_PICK_IDLE_IN_NODE) || (waker_node == node)) &&
 		    !cpumask_empty(idle_cpumask(waker_node)->cpu)) {
 			if (cpumask_test_cpu(cpu, allowed))
-				goto out_unlock;
+				return cpu;
 		}
 	}
 
@@ -566,7 +566,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		    cpumask_test_cpu(prev_cpu, idle_cpumask(node)->smt) &&
 		    scx_idle_test_and_clear_cpu(prev_cpu)) {
 			cpu = prev_cpu;
-			goto out_unlock;
+			return cpu;
 		}
 
 		/*
@@ -575,7 +575,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		if (llc_cpus) {
 			cpu = pick_idle_cpu_in_node(llc_cpus, node, SCX_PICK_IDLE_CORE);
 			if (cpu >= 0)
-				goto out_unlock;
+				return cpu;
 		}
 
 		/*
@@ -584,7 +584,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		if (numa_cpus) {
 			cpu = pick_idle_cpu_in_node(numa_cpus, node, SCX_PICK_IDLE_CORE);
 			if (cpu >= 0)
-				goto out_unlock;
+				return cpu;
 		}
 
 		/*
@@ -597,7 +597,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		 */
 		cpu = scx_pick_idle_cpu(allowed, node, flags | SCX_PICK_IDLE_CORE);
 		if (cpu >= 0)
-			goto out_unlock;
+			return cpu;
 
 		/*
 		 * Give up if we're strictly looking for a full-idle SMT
@@ -605,7 +605,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 		 */
 		if (flags & SCX_PICK_IDLE_CORE) {
 			cpu = -EBUSY;
-			goto out_unlock;
+			return cpu;
 		}
 	}
 
@@ -614,7 +614,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	 */
 	if (is_prev_allowed && scx_idle_test_and_clear_cpu(prev_cpu)) {
 		cpu = prev_cpu;
-		goto out_unlock;
+		return cpu;
 	}
 
 	/*
@@ -623,7 +623,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	if (llc_cpus) {
 		cpu = pick_idle_cpu_in_node(llc_cpus, node, 0);
 		if (cpu >= 0)
-			goto out_unlock;
+			return cpu;
 	}
 
 	/*
@@ -632,7 +632,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	if (numa_cpus) {
 		cpu = pick_idle_cpu_in_node(numa_cpus, node, 0);
 		if (cpu >= 0)
-			goto out_unlock;
+			return cpu;
 	}
 
 	/*
@@ -645,11 +645,6 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	 */
 	cpu = scx_pick_idle_cpu(allowed, node, flags);
 
-out_unlock:
-	rcu_read_unlock();
-out_enable:
-	preempt_enable();
-
 	return cpu;
 }
 

--
2.43.0