Skip to contentSkip to conversation

sched: introduce for_each_process_rculock and for_each_thread_rculock

PATCH Superseded Ye Liu 10 participants 49 messages Sep 7 – Sep 9

Subsystems (19)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ye Liu

author
#0 lore raw

v2 cover sched: introduce for_each_process_rculock and for_each_thread_rculock

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that combine the existing iteration macros with scoped_guard(rcu), so that the RCU read lock is automatically acquired before iteration and released when the loop exits — including via break, goto, or return.

The rest of the series converts manual rcu_read_lock()/ rcu_read_unlock() and guard(rcu)() pairs across mm/, kernel/, fs/, lib/ and security/ to use the new macros.

Changes since v1 [1]:
- Rename macros from *_rcu to *_rculock, as suggested by Steven Rostedt and acked by Thomas Gleixner, to avoid confusion with existing *_rcu() list iterators that expect the caller to hold the RCU read lock.
- Improve the comment on for_each_process_thread_rculock() to document that 'break' only exits the inner loop and 'goto' is needed to exit both loops (Thomas Gleixner).
- Rename the stale 'unlock:' label to 'out:' in hung_task.c, as noted by Günther Noack.
- Clarify in patch 4 that page_pgoff() is safe outside the RCU read-side critical section (SJ Park).
- CC all relevant maintainers on every patch (Lorenzo Stoakes).
- Drop the mm: prefix from patch 1, as the macros are in include/linux/sched/signal.h (Michal Hocko).

Patch 1 may trigger checkpatch "Macros with complex values should be enclosed in parentheses" errors. These are false positives — the scoped_guard() pattern is a control-flow construct, not a multi- statement macro, and the same idiom is used elsewhere in the kernel.

Suggested by Michal Hocko for the oom_kill path [2].

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Ye Liu (8):
sched: introduce for_each_process_rculock and for_each_thread_rculock mm/oom_kill: convert process/thread iterators to for_each_*_rculock mm/ksm: convert process iterator to for_each_process_rculock mm/memory-failure: convert process iterator to for_each_process_rculock
kernel: convert process/thread iterators to for_each_*_rculock
fs: convert process/thread iterators to for_each_*_rculock
lib: convert process iterator to for_each_process_rculock security/landlock: convert thread iterator to for_each_thread_rculock

 fs/proc/base.c               |  4 +---
 fs/resctrl/rdtgroup.c        |  8 ++------
 include/linux/sched/signal.h | 25 +++++++++++++++++++++++++
 kernel/cpu.c                 |  4 +---
 kernel/freezer.c             |  4 +---
 kernel/hung_task.c           | 11 ++++-------
 kernel/locking/lockdep.c     |  4 +---
 kernel/rcu/update.c          |  4 +---
 kernel/sched/core.c          |  3 +--
 kernel/sched/debug.c         |  4 +---
 kernel/trace/fgraph.c        |  8 ++------
 kernel/unwind/deferred.c     |  3 +--
 lib/is_single_threaded.c     |  5 +----
 mm/ksm.c                     |  4 +---
 mm/memory-failure.c          | 16 ++++------------
 mm/oom_kill.c                | 20 +++++---------------
 security/landlock/tsync.c    |  8 ++------
 17 files changed, 54 insertions(+), 81 deletions(-)

--
2.25.1

author
#1 lore raw

PATCH v2 1/8 sched: introduce for_each_process_rculock and for_each_thread_rculock

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair.

---
Changes in v2:
- Rename macros from *_rcu to *_rculock (Steven Rostedt, Thomas Gleixner)
- Improve comment on for_each_process_thread_rculock() double loop
- Change prefix from mm: to sched: (Michal Hocko)

 include/linux/sched/signal.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/sched/signal.h +25 -0
index 584ae88b435e..110ea9f131f3 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_SCHED_SIGNAL_H
 #define _LINUX_SCHED_SIGNAL_H
 
+#include <linux/cleanup.h>
 #include <linux/rculist.h>
 #include <linux/signal.h>
 #include <linux/sched.h>
@@ -663,6 +664,30 @@ extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 
+/*
+ * Variants of for_each_process(), for_each_thread() and
+ * for_each_process_thread() that automatically acquire and release the
+ * RCU read lock via scoped_guard(rcu).  The lock is held for the
+ * duration of the loop and released on exit, including via break,
+ * goto, or return.
+ */
+#define for_each_process_rculock(p) \
+	scoped_guard(rcu) \
+	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+
+#define for_each_thread_rculock(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)
+
+/*
+ * Double loop: 'break' only exits the inner for_each_thread() loop.
+ * Use 'goto' to exit both loops; the RCU read lock is released
+ * automatically when the scoped_guard scope is left.
+ */
+#define for_each_process_thread_rculock(p, t) \
+	scoped_guard(rcu) \
+	for_each_process(p) for_each_thread(p, t)
+
 typedef int (*proc_visitor)(struct task_struct *p, void *data);
 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
 
Signature · 1 line
2.25.1
Reviewed-by on v2 1/8
#2 lore raw
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair.

Some nits below, with those addressed, LGTM so:

Quoted 24 lines

---
Changes in v2:
- Rename macros from *_rcu to *_rculock (Steven Rostedt, Thomas Gleixner)
- Improve comment on for_each_process_thread_rculock() double loop
- Change prefix from mm: to sched: (Michal Hocko)

 include/linux/sched/signal.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/sched/signal.h +1 -0
index 584ae88b435e..110ea9f131f3 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_SCHED_SIGNAL_H
 #define _LINUX_SCHED_SIGNAL_H
 
+#include <linux/cleanup.h>
 #include <linux/rculist.h>
 #include <linux/signal.h>
 #include <linux/sched.h>
@@ -663,6 +664,30 @@ extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 

include/linux/sched/signal.h extern bool current_is_single_threaded(void); · line 676

Quoted 10 lines
include/linux/sched/signal.h +10 -0
+/*
+ * Variants of for_each_process(), for_each_thread() and
+ * for_each_process_thread() that automatically acquire and release the
+ * RCU read lock via scoped_guard(rcu).  The lock is held for the
+ * duration of the loop and released on exit, including via break,
+ * goto, or return.
+ */
+#define for_each_process_rculock(p) \
+	scoped_guard(rcu) \
+	for (p = &init_task ; (p = next_task(p)) != &init_task ; )

NIT: Can you indent the for loop by a tab? Since the for is in scoped_guard() scope and that's not made clear.

include/linux/sched/signal.h extern bool current_is_single_threaded(void); · line 680

Quoted 4 lines
include/linux/sched/signal.h +4 -0
+
+#define for_each_thread_rculock(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)

Same comment as above.

include/linux/sched/signal.h extern bool current_is_single_threaded(void); · line 689

Quoted 9 lines
include/linux/sched/signal.h +9 -0
+
+/*
+ * Double loop: 'break' only exits the inner for_each_thread() loop.
+ * Use 'goto' to exit both loops; the RCU read lock is released
+ * automatically when the scoped_guard scope is left.
+ */
+#define for_each_process_thread_rculock(p, t) \
+	scoped_guard(rcu) \
+	for_each_process(p) for_each_thread(p, t)

Yeah this is even worse for clarity :) I know the existing defines do itg this way but it's kinda confusing.

I know it's a bit fugly, but it's MUCH clearer as:

	#define for_each_process_thread_rculock(p, t) \
		scoped_guard(rcu) \
			for_each_process(p) \
				for_each_thread(p, t)
Quoted 7 lines
include/linux/sched/signal.h +1 -1
+
 typedef int (*proc_visitor)(struct task_struct *p, void *data);
 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
 
--

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 1/8
#3 lore raw
In reply to Ye Liu
Quoted 10 lines · Ye Liu

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair.

Thanks, I personally like the new _rculock suffix more ;)

I see the comments about indentation from Lorenzo, I am fine either way.

Reviewed-by on v2 1/8
#4 lore raw
In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair.

Lorenzo already picked up the nits, this is really nice otherwise.

Reviewed-by on v2 1/8
#5 lore raw
In reply to Ye Liu
Quoted 8 lines · On Mon, 7 Sep 2026 16:13:27 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair.

Looks good to me.

Quoted 3 lines

Assuming Lorenzo's indentation change requests are accepted,

Thanks,
SJ

[...]

author
#6 lore raw

PATCH v2 2/8 mm/oom_kill: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs wrapping for_each_process()/for_each_thread() loops with for_each_process_rculock() and for_each_thread_rculock(), which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock

 mm/oom_kill.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
mm/oom_kill.c +5 -15
index 5f372f6e26fa..ef2e1c423c1d 100644
@@ -94,8 +94,7 @@ static bool oom_cpuset_eligible(struct task_struct *start,
 	bool ret = false;
 	const nodemask_t *mask = oc->nodemask;
 
-	rcu_read_lock();
-	for_each_thread(start, tsk) {
+	for_each_thread_rculock(start, tsk) {
 		if (mask) {
 			/*
 			 * If this is a mempolicy constrained oom, tsk's
@@ -114,7 +113,6 @@ static bool oom_cpuset_eligible(struct task_struct *start,
 		if (ret)
 			break;
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -368,11 +366,9 @@ static void select_bad_process(struct oom_control *oc)
 	else {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p)
+		for_each_process_rculock(p)
 			if (oom_evaluate_task(p, oc))
 				break;
-		rcu_read_unlock();
 	}
 }
 
@@ -430,14 +426,12 @@ static void dump_tasks(struct oom_control *oc)
 		struct task_struct *p;
 		int i = 0;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rculock(p) {
 			/* Avoid potential softlockup warning */
 			if ((++i & 1023) == 0)
 				touch_softlockup_watchdog();
 			dump_task(p, oc);
 		}
-		rcu_read_unlock();
 	}
 }
 
@@ -894,8 +888,7 @@ static bool task_will_free_mem(struct task_struct *task)
 	 * are dying as well to make sure that a) nobody pins its mm and
 	 * b) the task is also reapable by the oom reaper.
 	 */
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
 		if (same_thread_group(task, p))
@@ -904,7 +897,6 @@ static bool task_will_free_mem(struct task_struct *task)
 		if (!ret)
 			break;
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -960,8 +952,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 	 * That thread will now get access to memory reserves since it has a
 	 * pending fatal signal.
 	 */
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
 		if (same_thread_group(p, victim))
@@ -982,7 +973,6 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 			continue;
 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
 	}
-	rcu_read_unlock();
 
 	if (can_oom_reap)
 		queue_oom_reaper(victim);
Signature · 1 line
2.25.1
Reviewed-by on v2 2/8
#7 lore raw

Re: [PATCH v2 2/8] mm/oom_kill: convert process/thread iterators to for_each_*_rculock

Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs wrapping for_each_process()/for_each_thread() loops with for_each_process_rculock() and for_each_thread_rculock(), which scope the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 5 lines

No functional change.

LGTM so:

Quoted 94 lines

---
Changes in v2:
- Rename *_rcu to *_rculock

 mm/oom_kill.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
mm/oom_kill.c +5 -16
index 5f372f6e26fa..ef2e1c423c1d 100644
@@ -94,8 +94,7 @@ static bool oom_cpuset_eligible(struct task_struct *start,
 	bool ret = false;
 	const nodemask_t *mask = oc->nodemask;
 
-	rcu_read_lock();
-	for_each_thread(start, tsk) {
+	for_each_thread_rculock(start, tsk) {
 		if (mask) {
 			/*
 			 * If this is a mempolicy constrained oom, tsk's
@@ -114,7 +113,6 @@ static bool oom_cpuset_eligible(struct task_struct *start,
 		if (ret)
 			break;
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -368,11 +366,9 @@ static void select_bad_process(struct oom_control *oc)
 	else {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p)
+		for_each_process_rculock(p)
 			if (oom_evaluate_task(p, oc))
 				break;
-		rcu_read_unlock();
 	}
 }
 
@@ -430,14 +426,12 @@ static void dump_tasks(struct oom_control *oc)
 		struct task_struct *p;
 		int i = 0;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rculock(p) {
 			/* Avoid potential softlockup warning */
 			if ((++i & 1023) == 0)
 				touch_softlockup_watchdog();
 			dump_task(p, oc);
 		}
-		rcu_read_unlock();
 	}
 }
 
@@ -894,8 +888,7 @@ static bool task_will_free_mem(struct task_struct *task)
 	 * are dying as well to make sure that a) nobody pins its mm and
 	 * b) the task is also reapable by the oom reaper.
 	 */
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
 		if (same_thread_group(task, p))
@@ -904,7 +897,6 @@ static bool task_will_free_mem(struct task_struct *task)
 		if (!ret)
 			break;
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -960,8 +952,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 	 * That thread will now get access to memory reserves since it has a
 	 * pending fatal signal.
 	 */
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (!process_shares_mm(p, mm))
 			continue;
 		if (same_thread_group(p, victim))
@@ -982,7 +973,6 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 			continue;
 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
 	}
-	rcu_read_unlock();
 
 	if (can_oom_reap)
 		queue_oom_reaper(victim);
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 2/8
#8 lore raw

Re: [PATCH v2 2/8] mm/oom_kill: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs wrapping for_each_process()/for_each_thread() loops with for_each_process_rculock() and for_each_thread_rculock(), which scope the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 5 lines

No functional change.

author
#9 lore raw

PATCH v2 3/8 mm/ksm: convert process iterator to for_each_process_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in mm/ksm.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock

 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -3
index 49d48d1e0998..69d30e80a090 100644
@@ -3280,8 +3280,7 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
 		struct anon_vma *av = rmap_item->anon_vma;
 
 		anon_vma_lock_read(av);
-		rcu_read_lock();
-		for_each_process(tsk) {
+		for_each_process_rculock(tsk) {
 			struct anon_vma_chain *vmac;
 			const unsigned long addr = rmap_item->address & PAGE_MASK;
 			const unsigned long index = rmap_item->linear_page_index;
@@ -3298,7 +3297,6 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
 				}
 			}
 		}
-		rcu_read_unlock();
 		anon_vma_unlock_read(av);
 	}
 }
Signature · 1 line
2.25.1
Reviewed-by on v2 3/8
#10 lore raw

Re: [PATCH v2 3/8] mm/ksm: convert process iterator to for_each_process_rculock

Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in mm/ksm.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

LGTM so:

Quoted 29 lines

---
Changes in v2:
- Rename *_rcu to *_rculock

 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -4
index 49d48d1e0998..69d30e80a090 100644
@@ -3280,8 +3280,7 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
 		struct anon_vma *av = rmap_item->anon_vma;
 
 		anon_vma_lock_read(av);
-		rcu_read_lock();
-		for_each_process(tsk) {
+		for_each_process_rculock(tsk) {
 			struct anon_vma_chain *vmac;
 			const unsigned long addr = rmap_item->address & PAGE_MASK;
 			const unsigned long index = rmap_item->linear_page_index;
@@ -3298,7 +3297,6 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
 				}
 			}
 		}
-		rcu_read_unlock();
 		anon_vma_unlock_read(av);
 	}
 }
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 3/8
#11 lore raw

Re: [PATCH v2 3/8] mm/ksm: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in mm/ksm.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

maintainer Acked-by on v2 3/8
#12 lore raw

Re: [PATCH v2 3/8] mm/ksm: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 6 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in mm/ksm.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 6 lines

No functional change.

---

Signature · 3 lines
Cheers,

David
author
#13 lore raw

PATCH v2 4/8 mm/memory-failure: convert process iterator to for_each_process_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() loop in mm/memory-failure.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

In collect_procs_file(), the page_pgoff() call now falls outside the RCU read-side critical section. This is safe because page_pgoff() only reads folio->index and does not traverse any RCU-protected structures.

---
Changes in v2:
- Rename *_rcu to *_rculock
- Clarify page_pgoff() safety outside RCU lock (SJ Park)

 mm/memory-failure.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
mm/memory-failure.c +4 -12
index a8b03e2920ba..7d93deb1f7b3 100644
@@ -555,8 +555,7 @@ static void collect_procs_anon(const struct folio *folio,
 		return;
 
 	pgoff = page_pgoff(folio, page);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct vm_area_struct *vma;
 		struct anon_vma_chain *vmac;
 		struct task_struct *t = task_early_kill(tsk, force_early);
@@ -572,7 +571,6 @@ static void collect_procs_anon(const struct folio *folio,
 			add_to_kill_anon_file(t, page, vma, to_kill, addr);
 		}
 	}
-	rcu_read_unlock();
 	anon_vma_unlock_read(av);
 }
 
@@ -589,9 +587,8 @@ static void collect_procs_file(const struct folio *folio,
 	pgoff_t pgoff;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
 	pgoff = page_pgoff(folio, page);
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = task_early_kill(tsk, force_early);
 		unsigned long addr;
 
@@ -611,7 +608,6 @@ static void collect_procs_file(const struct folio *folio,
 			add_to_kill_anon_file(t, page, vma, to_kill, addr);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 
@@ -635,8 +631,7 @@ static void collect_procs_fsdax(const struct page *page,
 	struct task_struct *tsk;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = tsk;
 
 		/*
@@ -653,7 +648,6 @@ static void collect_procs_fsdax(const struct page *page,
 				add_to_kill_fsdax(t, page, vma, to_kill, pgoff);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 #endif /* CONFIG_FS_DAX */
@@ -2288,8 +2282,7 @@ static void collect_procs_pfn(struct pfn_address_space *pfn_space,
 	struct address_space *mapping = pfn_space->mapping;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = tsk;
 
 		t = task_early_kill(tsk, true);
@@ -2303,7 +2296,6 @@ static void collect_procs_pfn(struct pfn_address_space *pfn_space,
 				add_to_kill_pgoff(t, vma, to_kill, pgoff);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v2 4/8
#14 lore raw

Re: [PATCH v2 4/8] mm/memory-failure: convert process iterator to for_each_process_rculock

Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() loop in mm/memory-failure.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 8 lines

In collect_procs_file(), the page_pgoff() call now falls outside the RCU read-side critical section. This is safe because page_pgoff() only reads folio->index and does not traverse any RCU-protected structures.

LGTM so:

Quoted 85 lines

---
Changes in v2:
- Rename *_rcu to *_rculock
- Clarify page_pgoff() safety outside RCU lock (SJ Park)

 mm/memory-failure.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
mm/memory-failure.c +4 -13
index a8b03e2920ba..7d93deb1f7b3 100644
@@ -555,8 +555,7 @@ static void collect_procs_anon(const struct folio *folio,
 		return;
 
 	pgoff = page_pgoff(folio, page);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct vm_area_struct *vma;
 		struct anon_vma_chain *vmac;
 		struct task_struct *t = task_early_kill(tsk, force_early);
@@ -572,7 +571,6 @@ static void collect_procs_anon(const struct folio *folio,
 			add_to_kill_anon_file(t, page, vma, to_kill, addr);
 		}
 	}
-	rcu_read_unlock();
 	anon_vma_unlock_read(av);
 }
 
@@ -589,9 +587,8 @@ static void collect_procs_file(const struct folio *folio,
 	pgoff_t pgoff;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
 	pgoff = page_pgoff(folio, page);
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = task_early_kill(tsk, force_early);
 		unsigned long addr;
 
@@ -611,7 +608,6 @@ static void collect_procs_file(const struct folio *folio,
 			add_to_kill_anon_file(t, page, vma, to_kill, addr);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 
@@ -635,8 +631,7 @@ static void collect_procs_fsdax(const struct page *page,
 	struct task_struct *tsk;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = tsk;
 
 		/*
@@ -653,7 +648,6 @@ static void collect_procs_fsdax(const struct page *page,
 				add_to_kill_fsdax(t, page, vma, to_kill, pgoff);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 #endif /* CONFIG_FS_DAX */
@@ -2288,8 +2282,7 @@ static void collect_procs_pfn(struct pfn_address_space *pfn_space,
 	struct address_space *mapping = pfn_space->mapping;
 
 	i_mmap_lock_read(mapping);
-	rcu_read_lock();
-	for_each_process(tsk) {
+	for_each_process_rculock(tsk) {
 		struct task_struct *t = tsk;
 
 		t = task_early_kill(tsk, true);
@@ -2303,7 +2296,6 @@ static void collect_procs_pfn(struct pfn_address_space *pfn_space,
 				add_to_kill_pgoff(t, vma, to_kill, pgoff);
 		}
 	}
-	rcu_read_unlock();
 	i_mmap_unlock_read(mapping);
 }
 
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 4/8
#15 lore raw

Re: [PATCH v2 4/8] mm/memory-failure: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() loop in mm/memory-failure.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 8 lines

In collect_procs_file(), the page_pgoff() call now falls outside the RCU read-side critical section. This is safe because page_pgoff() only reads folio->index and does not traverse any RCU-protected structures.

Reviewed-by on v2 4/8
#16 lore raw

Re: [PATCH v2 4/8] mm/memory-failure: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 11 lines · On Mon, 7 Sep 2026 16:13:30 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() loop in mm/memory-failure.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

In collect_procs_file(), the page_pgoff() call now falls outside the RCU read-side critical section. This is safe because page_pgoff() only reads folio->index and does not traverse any RCU-protected structures.

Thank you for clarifying :)

Quoted 4 lines

Thanks,
SJ

[...]

author
#17 lore raw

PATCH v2 5/8 kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock
- Rename stale 'unlock:' label to 'out:' in hung_task.c (Günther Noack)

 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
 9 files changed, 13 insertions(+), 32 deletions(-)
kernel/cpu.c +1 -3
index b3c8553d7bd6..00638d2abc0f 100644
@@ -1254,8 +1254,7 @@ void clear_tasks_mm_cpumask(int cpu)
 	 * full-fledged tasklist_lock.
 	 */
 	WARN_ON(cpu_online(cpu));
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		struct task_struct *t;
 
 		/*
@@ -1268,7 +1267,6 @@ void clear_tasks_mm_cpumask(int cpu)
 		arch_clear_mm_cpumask_cpu(cpu, t->mm);
 		task_unlock(t);
 	}
-	rcu_read_unlock();
 }
 
 /* Take this CPU down. */
kernel/freezer.c +1 -3
index a76bf957fb32..7a870f71dcf6 100644
@@ -217,11 +217,9 @@ void thaw_process(struct task_struct *p)
 {
 	struct task_struct *t;
 
-	rcu_read_lock();
-	for_each_thread(p, t) {
+	for_each_thread_rculock(p, t) {
 		__thaw_task(t);
 	}
-	rcu_read_unlock();
 }
 
 /**
kernel/hung_task.c +4 -7
index 6fcc94ce4ca9..73a5ad3be9a8 100644
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;
 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
 			if (!rcu_lock_break(g, t))
-				goto unlock;
+				goto out;
 			last_break = jiffies;
 		}
 
@@ -337,9 +336,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 			hung_task_info(t, timeout, this_round_count);
 		}
 	}
- unlock:
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;
 
kernel/locking/lockdep.c +1 -3
index c56a7f91d72e..ea218dc75ad5 100644
@@ -6834,15 +6834,13 @@ void debug_show_all_locks(void)
 	}
 	pr_warn("\nShowing all locks held in the system:\n");
 
-	rcu_read_lock();
-	for_each_process_thread(g, p) {
+	for_each_process_thread_rculock(g, p) {
 		if (!p->lockdep_depth)
 			continue;
 		lockdep_print_held_locks(p);
 		touch_nmi_watchdog();
 		touch_all_softlockup_watchdogs();
 	}
-	rcu_read_unlock();
 
 	pr_warn("\n");
 	pr_warn("=============================================\n\n");
kernel/rcu/update.c +1 -3
index 2a778b8ab4ad..d8e0b5896db9 100644
@@ -548,15 +548,13 @@ void synchronize_rcu_trivial_preempt(void)
 	struct task_struct *t;
 
 	smp_mb(); // Order prior accesses before grace-period start.
-	rcu_read_lock(); // Protect task list.
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (t == current)
 			continue;  // Don't deadlock on ourselves!
 		// Order later rcu_read_lock() on other tasks after QS.
 		while (smp_load_acquire(&t->rcu_trivial_preempt_nesting))
 			continue;
 	}
-	rcu_read_unlock();
 }
 EXPORT_SYMBOL_GPL(synchronize_rcu_trivial_preempt);
 #endif // #if IS_ENABLED(CONFIG_TRIVIAL_PREEMPT_RCU)
kernel/sched/core.c +1 -2
index 0697ed0f1c3d..fd5bdd2cb798 100644
@@ -2005,8 +2005,7 @@ static void uclamp_sync_util_min_rt_default(void)
 	smp_mb__after_spinlock();
 	read_unlock(&tasklist_lock);
 
-	guard(rcu)();
-	for_each_process_thread(g, p)
+	for_each_process_thread_rculock(g, p)
 		uclamp_update_util_min_rt_default(p);
 }
 
kernel/sched/debug.c +1 -3
index 72236db67983..cdae165b98f7 100644
@@ -1029,14 +1029,12 @@ static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
 #endif
 		   "\n");
 
-	rcu_read_lock();
-	for_each_process_thread(g, p) {
+	for_each_process_thread_rculock(g, p) {
 		if (task_cpu(p) != rq_cpu)
 			continue;
 
 		print_task(m, rq, p);
 	}
-	rcu_read_unlock();
 }
 
 void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
kernel/trace/fgraph.c +2 -6
index 40d373d65f9b..f797ce119223 100644
@@ -1057,11 +1057,10 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 		}
 	}
 
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (start == end) {
 			ret = -EAGAIN;
-			goto unlock;
+			goto free;
 		}
 
 		if (t->ret_stack == NULL) {
@@ -1074,9 +1073,6 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 			t->ret_stack = ret_stack_list[start++];
 		}
 	}
-
-unlock:
-	rcu_read_unlock();
 free:
 	for (i = start; i < end; i++)
 		kmem_cache_free(fgraph_stack_cachep, ret_stack_list[i]);
kernel/unwind/deferred.c +1 -2
index 5bea47314254..ecfe1336095e 100644
@@ -319,9 +319,8 @@ void unwind_deferred_cancel(struct unwind_work *work)
 
 	synchronize_srcu(&unwind_srcu);
 
-	guard(rcu)();
 	/* Clear this bit from all threads */
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		atomic_long_andnot(BIT(bit),
 				   &t->unwind_info.unwind_mask);
 		if (t->unwind_info.cache)
Signature · 1 line
2.25.1
#18 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Mixing goto-based cleanup labels with scope-based guard() declarations in the same function. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Mixing goto-based cleanup labels with scope-based guard() declarations in the same function. --

commit e4ac0b901a628fa09984a6972d98830031479b53
Author: Ye Liu <[email protected]>

kernel: convert process/thread iterators to for_each_*_rculock

This commit replaces manual rcu_read_lock() and rcu_read_unlock() pairs combined with process/thread iterators across the kernel with the for_each_*_rculock() macros. These macros use scoped_guard(rcu) to handle the RCU lock duration.

Quoted 4 lines
kernel/trace/fgraph.c +0 -0
index 40d373d65f9b9..f797ce1192233 100644

[ ... ]

kernel/trace/fgraph.c static int alloc_retstack_tasklist(unsigned long **ret_stack_list) · line 1063

Quoted 11 lines
kernel/trace/fgraph.c +2 -3
@@ -1057,11 +1057,10 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 		}
 	}
 
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (start == end) {
 			ret = -EAGAIN;
-			goto unlock;
+			goto free;

[Severity: Low]
Does jumping to a manual cleanup label from within the scoped_guard(rcu) loop introduced by for_each_process_thread_rculock() violate the cleanup subsystem guidelines?

According to include/linux/cleanup.h, goto and cleanup helpers should never be mixed in the same function to avoid confusing ownership semantics. Since alloc_retstack_tasklist() still uses a goto-based cleanup for ret_stack_list, should the allocation here be converted to use scope-based cleanup to avoid mixing these paradigms?

Quoted 13 lines
kernel/trace/fgraph.c +0 -3
 		}
 
 		if (t->ret_stack == NULL) {
@@ -1074,9 +1073,6 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 			t->ret_stack = ret_stack_list[start++];
 		}
 	}
-
-unlock:
-	rcu_read_unlock();
 free:
 	for (i = start; i < end; i++)
 		kmem_cache_free(fgraph_stack_cachep, ret_stack_list[i]);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Reviewed-by on v2 5/8
#19 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 4 lines

No functional change.

LGTM so:

Quoted 206 lines

---
Changes in v2:
- Rename *_rcu to *_rculock
- Rename stale 'unlock:' label to 'out:' in hung_task.c (Günther Noack)

 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
 9 files changed, 13 insertions(+), 32 deletions(-)
kernel/cpu.c +1 -3
index b3c8553d7bd6..00638d2abc0f 100644
@@ -1254,8 +1254,7 @@ void clear_tasks_mm_cpumask(int cpu)
 	 * full-fledged tasklist_lock.
 	 */
 	WARN_ON(cpu_online(cpu));
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		struct task_struct *t;
 
 		/*
@@ -1268,7 +1267,6 @@ void clear_tasks_mm_cpumask(int cpu)
 		arch_clear_mm_cpumask_cpu(cpu, t->mm);
 		task_unlock(t);
 	}
-	rcu_read_unlock();
 }
 
 /* Take this CPU down. */
kernel/freezer.c +1 -3
index a76bf957fb32..7a870f71dcf6 100644
@@ -217,11 +217,9 @@ void thaw_process(struct task_struct *p)
 {
 	struct task_struct *t;
 
-	rcu_read_lock();
-	for_each_thread(p, t) {
+	for_each_thread_rculock(p, t) {
 		__thaw_task(t);
 	}
-	rcu_read_unlock();
 }
 
 /**
kernel/hung_task.c +4 -7
index 6fcc94ce4ca9..73a5ad3be9a8 100644
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;
 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
 			if (!rcu_lock_break(g, t))
-				goto unlock;
+				goto out;
 			last_break = jiffies;
 		}
 
@@ -337,9 +336,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 			hung_task_info(t, timeout, this_round_count);
 		}
 	}
- unlock:
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;
 
kernel/locking/lockdep.c +1 -3
index c56a7f91d72e..ea218dc75ad5 100644
@@ -6834,15 +6834,13 @@ void debug_show_all_locks(void)
 	}
 	pr_warn("\nShowing all locks held in the system:\n");
 
-	rcu_read_lock();
-	for_each_process_thread(g, p) {
+	for_each_process_thread_rculock(g, p) {
 		if (!p->lockdep_depth)
 			continue;
 		lockdep_print_held_locks(p);
 		touch_nmi_watchdog();
 		touch_all_softlockup_watchdogs();
 	}
-	rcu_read_unlock();
 
 	pr_warn("\n");
 	pr_warn("=============================================\n\n");
kernel/rcu/update.c +1 -3
index 2a778b8ab4ad..d8e0b5896db9 100644
@@ -548,15 +548,13 @@ void synchronize_rcu_trivial_preempt(void)
 	struct task_struct *t;
 
 	smp_mb(); // Order prior accesses before grace-period start.
-	rcu_read_lock(); // Protect task list.
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (t == current)
 			continue;  // Don't deadlock on ourselves!
 		// Order later rcu_read_lock() on other tasks after QS.
 		while (smp_load_acquire(&t->rcu_trivial_preempt_nesting))
 			continue;
 	}
-	rcu_read_unlock();
 }
 EXPORT_SYMBOL_GPL(synchronize_rcu_trivial_preempt);
 #endif // #if IS_ENABLED(CONFIG_TRIVIAL_PREEMPT_RCU)
kernel/sched/core.c +1 -2
index 0697ed0f1c3d..fd5bdd2cb798 100644
@@ -2005,8 +2005,7 @@ static void uclamp_sync_util_min_rt_default(void)
 	smp_mb__after_spinlock();
 	read_unlock(&tasklist_lock);
 
-	guard(rcu)();
-	for_each_process_thread(g, p)
+	for_each_process_thread_rculock(g, p)
 		uclamp_update_util_min_rt_default(p);
 }
 
kernel/sched/debug.c +1 -3
index 72236db67983..cdae165b98f7 100644
@@ -1029,14 +1029,12 @@ static void print_rq(struct seq_file *m, struct rq *rq, int rq_cpu)
 #endif
 		   "\n");
 
-	rcu_read_lock();
-	for_each_process_thread(g, p) {
+	for_each_process_thread_rculock(g, p) {
 		if (task_cpu(p) != rq_cpu)
 			continue;
 
 		print_task(m, rq, p);
 	}
-	rcu_read_unlock();
 }
 
 void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
kernel/trace/fgraph.c +2 -6
index 40d373d65f9b..f797ce119223 100644
@@ -1057,11 +1057,10 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 		}
 	}
 
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (start == end) {
 			ret = -EAGAIN;
-			goto unlock;
+			goto free;
 		}
 
 		if (t->ret_stack == NULL) {
@@ -1074,9 +1073,6 @@ static int alloc_retstack_tasklist(unsigned long **ret_stack_list)
 			t->ret_stack = ret_stack_list[start++];
 		}
 	}
-
-unlock:
-	rcu_read_unlock();
 free:
 	for (i = start; i < end; i++)
 		kmem_cache_free(fgraph_stack_cachep, ret_stack_list[i]);
kernel/unwind/deferred.c +1 -3
index 5bea47314254..ecfe1336095e 100644
@@ -319,9 +319,8 @@ void unwind_deferred_cancel(struct unwind_work *work)
 
 	synchronize_srcu(&unwind_srcu);
 
-	guard(rcu)();
 	/* Clear this bit from all threads */
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		atomic_long_andnot(BIT(bit),
 				   &t->unwind_info.unwind_mask);
 		if (t->unwind_info.cache)
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

#20 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 4 lines

No functional change.

Question below

Quoted 14 lines
kernel/hung_task.c +2 -3
index 6fcc94ce4ca9..73a5ad3be9a8 100644
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;

... snip ...

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 341

Quoted 7 lines

- unlock:

kernel/hung_task.c +1 -2
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;

The sunken rcu_read_lock()/unlock() + scoped_guard usage makes this goto at a minimum very confusing, if not actually broken.

Are we sure this isn't broken as-written? Generally we don't mix cleanup and goto for exactly this reason.

~Gregory

#21 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 15 lines · Gregory Price
Quoted 12 lines · Ye Liu
kernel/hung_task.c +2 -3
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;

... snip ...

Quoted 10 lines

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 341

Quoted 7 lines

- unlock:

kernel/hung_task.c +1 -2
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;

The sunken rcu_read_lock()/unlock() + scoped_guard usage makes this goto at a minimum very confusing, if not actually broken.

I am afraid I misunderstood you concerns...

This patch renames the "unlock:" label to "out:", and to me this makes sense.

Unfortunately for_each_process_thread() and the new helper can't use "break".

Oleg.

#22 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 27 lines · Oleg Nesterov
Quoted 15 lines · Gregory Price
Quoted 12 lines · Ye Liu
kernel/hung_task.c +2 -3
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;

... snip ...

Quoted 10 lines

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 341

Quoted 7 lines

- unlock:

kernel/hung_task.c +1 -2
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;

The sunken rcu_read_lock()/unlock() + scoped_guard usage makes this goto at a minimum very confusing, if not actually broken.

Quoted 2 lines

I am afraid I misunderstood you concerns...

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

On the surface it looks like it would skip the unlock entirely.

That is what i mean by "at a minimum very confusing, if not broken" and why it's generally discouraged to mix cleanup.h and goto.

~Gregory

#23 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 29 lines · Gregory Price
Quoted 27 lines · Oleg Nesterov
Quoted 25 lines · Gregory Price
Quoted 12 lines · Ye Liu
kernel/hung_task.c +2 -3
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;

... snip ...

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 341

Quoted 7 lines

- unlock:

kernel/hung_task.c +1 -2
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;

The sunken rcu_read_lock()/unlock() + scoped_guard usage makes this goto at a minimum very confusing, if not actually broken.

Quoted 5 lines
Quoted 2 lines

I am afraid I misunderstood you concerns...

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Hmm, why? I didn't know...

Oleg.

#24 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 10 lines · Oleg Nesterov
Quoted 3 lines

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Hmm, why? I didn't know...

one of the reasons to use cleanup.h it is to eliminate the "goto for exit cleanup" mechanism in the first place (says so in the first paragraph of cleanup.h).

Later in the header is explicit says don't mix:

* Lastly, given that the benefit of cleanup helpers is removal of
* "goto", and that the "goto" statement can jump between scopes, the
* expectation is that usage of "goto" and cleanup helpers is never
* mixed in the same function. I.e. for a given routine, convert all
* resources that need a "goto" cleanup to scope-based cleanup, or
* convert none of them.

This particular patch only does a partial conversion - so it's a bit of an anti-pattern.

Reading up on scoped_guard a bit more it seems you're right it handles the issue to jump-out, but i can see why cleanup.h says "don't do that" because it's confusing to look at / hard to reason about.

~Gregory

#25 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 26 lines · Gregory Price
Quoted 10 lines · Oleg Nesterov
Quoted 3 lines

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Hmm, why? I didn't know...

one of the reasons to use cleanup.h it is to eliminate the "goto for exit cleanup" mechanism in the first place (says so in the first paragraph of cleanup.h).

Later in the header is explicit says don't mix:

* Lastly, given that the benefit of cleanup helpers is removal of
* "goto", and that the "goto" statement can jump between scopes, the
* expectation is that usage of "goto" and cleanup helpers is never
* mixed in the same function. I.e. for a given routine, convert all
* resources that need a "goto" cleanup to scope-based cleanup, or
* convert none of them.

Quoted 2 lines

This particular patch only does a partial conversion - so it's a bit of an anti-pattern.

Ah, thanks, now I see what you meant.

Perhaps in this sense this particular change is "anti-pattern", even if "goto out" jumps out of the scope...

To me the code looks better with this change, but of course this is subjective.

Oleg.

#26 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Oleg Nesterov
Quoted 8 lines · Oleg Nesterov
Quoted 2 lines

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Hmm, why? I didn't know...

Apparently older GCC would allow subtle bugs with cleanup + goto to slip by but clang has always caught those bugs and refused to compile so as long as clang builds are happy, things should be good.

Here is an older explanation from Peter:
https://lore.kernel.org/lkml/[email protected]/

Signature · 2 lines
Thanks and Regards,
Prateek
#27 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 9 lines · K Prateek Nayak
Quoted 7 lines · Oleg Nesterov
Quoted 2 lines

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Quoted 8 lines
Quoted 1 line

Hmm, why? I didn't know...

Apparently older GCC would allow subtle bugs with cleanup + goto to slip by but clang has always caught those bugs and refused to compile so as long as clang builds are happy, things should be good.

Here is an older explanation from Peter:
https://lore.kernel.org/lkml/[email protected]/

Ah, but in this case the code is buggy, "goto unregister" jumps into the scope of __free.

Anyway, I agree that mixing cleanup and goto can make the code confusing or buggy, but this particular case still looks fine to me...

Oleg.

#28 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 19 lines · Oleg Nesterov
Quoted 9 lines · K Prateek Nayak
Quoted 7 lines · Oleg Nesterov
Quoted 2 lines

This might be my - admittedly shallow - understanding of scoped_guard, but does scoped guard handle goto correctly?

Yes, it does,

Quoted 1 line

and why it's generally discouraged to mix cleanup.h and goto.

Quoted 8 lines
Quoted 1 line

Hmm, why? I didn't know...

Apparently older GCC would allow subtle bugs with cleanup + goto to slip by but clang has always caught those bugs and refused to compile so as long as clang builds are happy, things should be good.

Here is an older explanation from Peter:
https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines

Ah, but in this case the code is buggy, "goto unregister" jumps into the scope of __free.

Anyway, I agree that mixing cleanup and goto can make the code confusing or buggy, but this particular case still looks fine to me...

Seems jump-out is ok, thanks for the confirmation Peter.

I do agree the general improvement is nice, but maybe worth considering taking cleanup.h all the way if we're going to use it - that's all.

The improvement is clearly welcome.

~Gregory

maintainer
#29 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Gregory Price
Quoted 31 lines · Gregory Price
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 4 lines

No functional change.

Question below

Quoted 14 lines
kernel/hung_task.c +2 -3
index 6fcc94ce4ca9..73a5ad3be9a8 100644
@@ -315,13 +315,12 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 		return;
 
 	this_round_count = 0;
-	rcu_read_lock();
-	for_each_process_thread(g, t) {
+	for_each_process_thread_rculock(g, t) {
 		if (!max_count--)
-			goto unlock;
+			goto out;

... snip ...

Quoted 10 lines

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 341

Quoted 7 lines

- unlock:

kernel/hung_task.c +1 -2
-	rcu_read_unlock();
-
+out:
 	if (!this_round_count)
 		return;

The sunken rcu_read_lock()/unlock() + scoped_guard usage makes this goto at a minimum very confusing, if not actually broken.

You can use goto to exit a scope, that's fine. Using goto to enter a scope is not (clang will error on that).

Just don't use computed goto, that goes sideways real quick.

Reviewed-by on v2 5/8
#30 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 9 lines · On Mon, 7 Sep 2026 16:13:31 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Looks good and correct to me.

Quoted 3 lines

Thanks,
SJ

[...]

Reviewed-by on v2 5/8
#31 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 17 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock
- Rename stale 'unlock:' label to 'out:' in hung_task.c (Günther Noack)

Quoted 9 lines
 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
maintainer
#32 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 16 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process(), for_each_thread() and for_each_process_thread() loops across kernel/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock
- Rename stale 'unlock:' label to 'out:' in hung_task.c (Günther Noack)

Quoted 10 lines
 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
 9 files changed, 13 insertions(+), 32 deletions(-)

This is not something that can be applied.

maintainer
#33 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 11 lines · On Tue, 8 Sep 2026 09:42:28 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 10 lines
 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
 9 files changed, 13 insertions(+), 32 deletions(-)  
Quoted 1 line

This is not something that can be applied.

I believe you mean to break it up into individual subsystems. kernel/ is not its own subsystem, it contains many different ones.

-- Steve

maintainer
#34 lore raw

Re: [PATCH v2 5/8] kernel: convert process/thread iterators to for_each_*_rculock

Quoted 14 lines · Steven Rostedt
Quoted 11 lines · On Tue, 8 Sep 2026 09:42:28 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 10 lines
 kernel/cpu.c             |  4 +---
 kernel/freezer.c         |  4 +---
 kernel/hung_task.c       | 11 ++++-------
 kernel/locking/lockdep.c |  4 +---
 kernel/rcu/update.c      |  4 +---
 kernel/sched/core.c      |  3 +--
 kernel/sched/debug.c     |  4 +---
 kernel/trace/fgraph.c    |  8 ++------
 kernel/unwind/deferred.c |  3 +--
 9 files changed, 13 insertions(+), 32 deletions(-)  
Quoted 4 lines
Quoted 1 line

This is not something that can be applied.

I believe you mean to break it up into individual subsystems. kernel/ is not its own subsystem, it contains many different ones.

Just so.

author
#35 lore raw

PATCH v2 6/8 fs: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() and for_each_process_thread() loops in fs/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock

 fs/proc/base.c        | 4 +---
 fs/resctrl/rdtgroup.c | 8 ++------
 2 files changed, 3 insertions(+), 9 deletions(-)
fs/proc/base.c +1 -3
index 6a39de424f62..3de4f4557e4c 100644
@@ -1160,8 +1160,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	if (mm) {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rculock(p) {
 			if (same_thread_group(task, p))
 				continue;
 
@@ -1177,7 +1176,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 			}
 			task_unlock(p);
 		}
-		rcu_read_unlock();
 		mmdrop(mm);
 	}
 err_unlock:
fs/resctrl/rdtgroup.c +2 -6
index 5dcbb0a964e8..b14df8b23f1a 100644
@@ -709,14 +709,12 @@ int rdtgroup_tasks_assigned(struct rdtgroup *r)
 
 	lockdep_assert_held(&rdtgroup_mutex);
 
-	rcu_read_lock();
-	for_each_process_thread(p, t) {
+	for_each_process_thread_rculock(p, t) {
 		if (is_closid_match(t, r) || is_rmid_match(t, r)) {
 			ret = 1;
 			break;
 		}
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -826,15 +824,13 @@ static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
 	struct task_struct *p, *t;
 	pid_t pid;
 
-	rcu_read_lock();
-	for_each_process_thread(p, t) {
+	for_each_process_thread_rculock(p, t) {
 		if (is_closid_match(t, r) || is_rmid_match(t, r)) {
 			pid = task_pid_vnr(t);
 			if (pid)
 				seq_printf(s, "%d\n", pid);
 		}
 	}
-	rcu_read_unlock();
 }
 
 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
Signature · 1 line
2.25.1
#36 lore raw

Re: [PATCH v2 6/8] fs: convert process/thread iterators to for_each_*_rculock

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Mixing scope-based cleanup (scoped_guard) with goto-based error handling in the same function. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Mixing scope-based cleanup (scoped_guard) with goto-based error handling in the same function. --

commit e0076a589e993892daf78cfc52036d64c5fb68e6
Author: Ye Liu <[email protected]>
fs: convert process/thread iterators to for_each_*_rculock

This commit replaces manual rcu_read_lock() and rcu_read_unlock() pairs combined with process iterators in the fs/ directory with for_each_*_rculock() macros. These new macros scope the RCU read lock to the loop body using scoped_guard(rcu).

Quoted 4 lines
fs/proc/base.c +0 -0
index 58be389424605..2c47d838a1c76 100644

[ ... ]

fs/proc/base.c static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) · line 1164

Quoted 7 lines
fs/proc/base.c +1 -2
@@ -1161,8 +1161,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	if (mm) {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rculock(p) {

[Severity: Low]
Does this code mix scope-based cleanup with goto-based error handling?

Looking at __set_oom_adj(), the function still uses goto err_unlock; for error paths earlier in the function:

fs/proc/base.c:__set_oom_adj() {
    ...
    if (legacy) {
        if (oom_adj < task->signal->oom_score_adj &&
                !capable(CAP_SYS_RESOURCE)) {
            err = -EACCES;
            goto err_unlock;
        }
    ...
}

The newly introduced for_each_process_rculock() macro expands to use scoped_guard(rcu). The cleanup subsystem guidelines explicitly state that a function should either convert all resources that need cleanup to scope-based cleanup, or convert none of them, to avoid confusing ownership semantics.

Could this function be fully converted, or should the rcu_read_lock() remain manual here until the rest of the error handling is updated?

Quoted 2 lines
fs/proc/base.c +0 -0
 			if (same_thread_group(task, p))
 				continue;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Reviewed-by on v2 6/8
#37 lore raw

Re: [PATCH v2 6/8] fs: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() and for_each_process_thread() loops in fs/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

LGTM so:

Quoted 67 lines

---
Changes in v2:
- Rename *_rcu to *_rculock

 fs/proc/base.c        | 4 +---
 fs/resctrl/rdtgroup.c | 8 ++------
 2 files changed, 3 insertions(+), 9 deletions(-)
fs/proc/base.c +1 -3
index 6a39de424f62..3de4f4557e4c 100644
@@ -1160,8 +1160,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	if (mm) {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rculock(p) {
 			if (same_thread_group(task, p))
 				continue;
 
@@ -1177,7 +1176,6 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 			}
 			task_unlock(p);
 		}
-		rcu_read_unlock();
 		mmdrop(mm);
 	}
 err_unlock:
fs/resctrl/rdtgroup.c +2 -7
index 5dcbb0a964e8..b14df8b23f1a 100644
@@ -709,14 +709,12 @@ int rdtgroup_tasks_assigned(struct rdtgroup *r)
 
 	lockdep_assert_held(&rdtgroup_mutex);
 
-	rcu_read_lock();
-	for_each_process_thread(p, t) {
+	for_each_process_thread_rculock(p, t) {
 		if (is_closid_match(t, r) || is_rmid_match(t, r)) {
 			ret = 1;
 			break;
 		}
 	}
-	rcu_read_unlock();
 
 	return ret;
 }
@@ -826,15 +824,13 @@ static void show_rdt_tasks(struct rdtgroup *r, struct seq_file *s)
 	struct task_struct *p, *t;
 	pid_t pid;
 
-	rcu_read_lock();
-	for_each_process_thread(p, t) {
+	for_each_process_thread_rculock(p, t) {
 		if (is_closid_match(t, r) || is_rmid_match(t, r)) {
 			pid = task_pid_vnr(t);
 			if (pid)
 				seq_printf(s, "%d\n", pid);
 		}
 	}
-	rcu_read_unlock();
 }
 
 static int rdtgroup_tasks_show(struct kernfs_open_file *of,
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 6/8
#38 lore raw

Re: [PATCH v2 6/8] fs: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() and for_each_process_thread() loops in fs/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Reviewed-by on v2 6/8
#39 lore raw

Re: [PATCH v2 6/8] fs: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Quoted 8 lines · On Mon, 7 Sep 2026 16:13:32 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pairs combined with for_each_process() and for_each_process_thread() loops in fs/ with the for_each_*_rculock() macros, which scope the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Looks good and correct to me.

Quoted 3 lines

Thanks,
SJ

[...]

author
#40 lore raw

PATCH v2 7/8 lib: convert process iterator to for_each_process_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in lib/ with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock

 lib/is_single_threaded.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/is_single_threaded.c +1 -4
index 8c98b20bfc41..4e7fe85454ab 100644
@@ -26,8 +26,7 @@ bool current_is_single_threaded(void)
 		return true;
 
 	ret = false;
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (unlikely(p->flags & PF_KTHREAD))
 			continue;
 		if (unlikely(p == task->group_leader))
@@ -48,7 +47,5 @@ bool current_is_single_threaded(void)
 	}
 	ret = true;
 found:
-	rcu_read_unlock();
-
 	return ret;
 }
Signature · 1 line
2.25.1
Reviewed-by on v2 7/8
#41 lore raw

Re: [PATCH v2 7/8] lib: convert process iterator to for_each_process_rculock

Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in lib/ with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

LGTM so:

Quoted 29 lines

---
Changes in v2:
- Rename *_rcu to *_rculock

 lib/is_single_threaded.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/is_single_threaded.c +1 -5
index 8c98b20bfc41..4e7fe85454ab 100644
@@ -26,8 +26,7 @@ bool current_is_single_threaded(void)
 		return true;
 
 	ret = false;
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rculock(p) {
 		if (unlikely(p->flags & PF_KTHREAD))
 			continue;
 		if (unlikely(p == task->group_leader))
@@ -48,7 +47,5 @@ bool current_is_single_threaded(void)
 	}
 	ret = true;
 found:
-	rcu_read_unlock();
-
 	return ret;
 }
--
Quoted 2 lines

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 7/8
#42 lore raw

Re: [PATCH v2 7/8] lib: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in lib/ with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Reviewed-by on v2 7/8
#43 lore raw

Re: [PATCH v2 7/8] lib: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 7 lines · On Mon, 7 Sep 2026 16:13:33 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process() loop in lib/ with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Looks good and correct to me.

Quoted 3 lines

Thanks,
SJ

[...]

author
#44 lore raw

PATCH v2 8/8 security/landlock: convert thread iterator to for_each_thread_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
Changes in v2:
- Rename *_rcu to *_rculock

 security/landlock/tsync.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
security/landlock/tsync.c +2 -6
index 0b71e158c3f5..a578af640aad 100644
@@ -335,9 +335,7 @@ static size_t count_additional_threads(const struct tsync_works *works)
 
 	caller = current;
 
-	guard(rcu)();
-
-	for_each_thread(caller, thread) {
+	for_each_thread_rculock(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
@@ -376,9 +374,7 @@ static bool schedule_task_work(struct tsync_works *works,
 
 	caller = current;
 
-	guard(rcu)();
-
-	for_each_thread(caller, thread) {
+	for_each_thread_rculock(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
Signature · 1 line
2.25.1
Reviewed-by on v2 8/8
#45 lore raw

Re: [PATCH v2 8/8] security/landlock: convert thread iterator to for_each_thread_rculock

Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

LGTM so:

Quoted 11 lines

---
Changes in v2:
- Rename *_rcu to *_rculock

 security/landlock/tsync.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
security/landlock/tsync.c +0 -0
index 0b71e158c3f5..a578af640aad 100644

security/landlock/tsync.c static size_t count_additional_threads(const struct tsync_works *works) · line 338

Quoted 4 lines
security/landlock/tsync.c +0 -1
@@ -335,9 +335,7 @@ static size_t count_additional_threads(const struct tsync_works *works)
 
 	caller = current;
 
-	guard(rcu)();

OK looked at the file and confirmed the function exits just after the below section so fine :)

security/landlock/tsync.c static bool schedule_task_work(struct tsync_works *works, · line 379

Quoted 11 lines
security/landlock/tsync.c +1 -3
-
-	for_each_thread(caller, thread) {
+	for_each_thread_rculock(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
@@ -376,9 +374,7 @@ static bool schedule_task_work(struct tsync_works *works,
 
 	caller = current;
 
-	guard(rcu)();

Similar here.

Quoted 9 lines
security/landlock/tsync.c +1 -3
-
-	for_each_thread(caller, thread) {
+	for_each_thread_rculock(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
--

2.25.1

--
Cheers, Lorenzo

Reviewed-by on v2 8/8
#46 lore raw

Re: [PATCH v2 8/8] security/landlock: convert thread iterator to for_each_thread_rculock

In reply to Ye Liu
Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Reviewed-by on v2 8/8
#47 lore raw

Re: [PATCH v2 8/8] security/landlock: convert thread iterator to for_each_thread_rculock

In reply to Ye Liu
Quoted 10 lines · On Mon, 7 Sep 2026 16:13:34 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Thanks,
SJ

[...]

#48 lore raw
In reply to Ye Liu
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that combine the existing iteration macros with scoped_guard(rcu), so that the RCU read lock is automatically acquired before iteration and released when the loop exits — including via break, goto, or return.

Quoted 4 lines

The rest of the series converts manual rcu_read_lock()/ rcu_read_unlock() and guard(rcu)() pairs across mm/, kernel/, fs/, lib/ and security/ to use the new macros.

Andrew - just a note (assuming Ye Liu is in agreement) I think all of the below should be excluded from the cover letter that goes into the commit message, including the checkpatch thing.

Quoted 16 lines

Changes since v1 [1]:
- Rename macros from *_rcu to *_rculock, as suggested by Steven Rostedt and acked by Thomas Gleixner, to avoid confusion with existing *_rcu() list iterators that expect the caller to hold the RCU read lock.
- Improve the comment on for_each_process_thread_rculock() to document that 'break' only exits the inner loop and 'goto' is needed to exit both loops (Thomas Gleixner).
- Rename the stale 'unlock:' label to 'out:' in hung_task.c, as noted by Günther Noack.
- Clarify in patch 4 that page_pgoff() is safe outside the RCU read-side critical section (SJ Park).
- CC all relevant maintainers on every patch (Lorenzo Stoakes).
- Drop the mm: prefix from patch 1, as the macros are in include/linux/sched/signal.h (Michal Hocko).

Quoted 4 lines

Patch 1 may trigger checkpatch "Macros with complex values should be enclosed in parentheses" errors. These are false positives — the scoped_guard() pattern is a control-flow construct, not a multi- statement macro, and the same idiom is used elsewhere in the kernel.

While it's useful as a note to reviewers, in general checkpatch is taken with a pinch of salt - it's a master of false positives so usually no need to say this :)

Quoted 35 lines

Suggested by Michal Hocko for the oom_kill path [2].

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Ye Liu (8):
sched: introduce for_each_process_rculock and for_each_thread_rculock mm/oom_kill: convert process/thread iterators to for_each_*_rculock mm/ksm: convert process iterator to for_each_process_rculock mm/memory-failure: convert process iterator to for_each_process_rculock
kernel: convert process/thread iterators to for_each_*_rculock
fs: convert process/thread iterators to for_each_*_rculock
lib: convert process iterator to for_each_process_rculock security/landlock: convert thread iterator to for_each_thread_rculock

 fs/proc/base.c               |  4 +---
 fs/resctrl/rdtgroup.c        |  8 ++------
 include/linux/sched/signal.h | 25 +++++++++++++++++++++++++
 kernel/cpu.c                 |  4 +---
 kernel/freezer.c             |  4 +---
 kernel/hung_task.c           | 11 ++++-------
 kernel/locking/lockdep.c     |  4 +---
 kernel/rcu/update.c          |  4 +---
 kernel/sched/core.c          |  3 +--
 kernel/sched/debug.c         |  4 +---
 kernel/trace/fgraph.c        |  8 ++------
 kernel/unwind/deferred.c     |  3 +--
 lib/is_single_threaded.c     |  5 +----
 mm/ksm.c                     |  4 +---
 mm/memory-failure.c          | 16 ++++------------
 mm/oom_kill.c                | 20 +++++---------------
 security/landlock/tsync.c    |  8 ++------
 17 files changed, 54 insertions(+), 81 deletions(-)
Quoted 4 lines

--
2.25.1

--
Cheers, Lorenzo