Skip to contentSkip to conversation

sched: introduce for_each_process_rculock and for_each_thread_rculock

PATCH In review Ye Liu 4 participants 36 messages Sep 21 – Oct 12

Subsystems (19)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

v4 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.

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

---
Changes since v3 [4]:
- Fix patch 5 (cpu/hotplug): title and commit message incorrectly said "thread iterator" and "for_each_thread_rculock" while the code converts for_each_process() to for_each_process_rculock().

Note: Patches 4, 11, and 13 received automated reviews from sashiko-bot in the v3 thread [4] that were verified as false positives:

- Patch 4: claimed the folio lock is dropped before collect_procs(),

    leading to a NULL deref on folio->mapping; the lock is held in all
    call paths.

- Patch 11: claimed mixing goto/scope-based cleanup in alloc_retstack_tasklist(); the goto free label handles memory cleanup, not lock management.
- Patch 13: claimed mixing goto/scope-based cleanup in __set_oom_adj(); goto err_unlock manages oom_adj_mutex while scoped_guard(rcu) manages the RCU read lock — different resources.

Changes since v2 [3]:
- Split the kernel/ patch into per-subsystem patches, as the combined patch could not be applied (Peter Zijlstra, Steven Rostedt).
- Move changelog below the --- separator so it is excluded from the commit message (Lorenzo Stoakes).
- Indent loop body in macro definitions to show scoped_guard scope (Lorenzo Stoakes).

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).

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

Ye Liu (15):
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
cpu/hotplug: convert process iterator to for_each_process_rculock
freezer: convert thread iterator to for_each_thread_rculock
hung_task: convert process/thread iterators to for_each_*_rculock locking/lockdep: convert process/thread iterators to
for_each_*_rculock
rcu: convert process/thread iterator to
for_each_process_thread_rculock
sched: convert process/thread iterators to for_each_*_rculock tracing/fgraph: convert process/thread iterator to
for_each_process_thread_rculock
unwind: convert process/thread iterator to
for_each_process_thread_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 | 26 ++++++++++++++++++++++++++
 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, 55 insertions(+), 81 deletions(-)

--
2.25.1

author
#1 lore raw

PATCH v4 1/15 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.

---
 include/linux/sched/signal.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
include/linux/sched/signal.h +26 -0
index 584ae88b435e..ea23c7e9db33 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,31 @@ 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
author
#3 lore raw

PATCH v4 2/15 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.

---
 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
author
#6 lore raw

PATCH v4 3/15 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.

---
 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
author
#9 lore raw

PATCH v4 4/15 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.

---
 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
author
#11 lore raw

PATCH v4 5/15 cpu/hotplug: 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 kernel/cpu.c with for_each_process_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/cpu.c | 4 +---
 1 file changed, 1 insertion(+), 3 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. */
Signature · 1 line
2.25.1
author
#13 lore raw

PATCH v4 6/15 freezer: convert thread iterator to for_each_thread_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_thread() loop in kernel/freezer.c with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/freezer.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
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();
 }
 
 /**
Signature · 1 line
2.25.1
author
#15 lore raw

PATCH v4 7/15 hung_task: 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() pair combined with for_each_process_thread() loop in kernel/hung_task.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/hung_task.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
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;
 
Signature · 1 line
2.25.1
author
#17 lore raw

PATCH v4 8/15 locking/lockdep: 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() pair combined with for_each_process_thread() loop in kernel/locking/lockdep.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/locking/lockdep.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
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");
Signature · 1 line
2.25.1
author
#19 lore raw

PATCH v4 9/15 rcu: convert process/thread iterator to for_each_process_thread_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_thread() loop in kernel/rcu/update.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/rcu/update.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
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)
Signature · 1 line
2.25.1
author
#22 lore raw

PATCH v4 10/15 sched: 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_thread() loops in kernel/sched/ with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/sched/core.c  | 3 +--
 kernel/sched/debug.c | 4 +---
 2 files changed, 2 insertions(+), 5 deletions(-)
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)
Signature · 1 line
2.25.1
author
#24 lore raw

PATCH v4 11/15 tracing/fgraph: convert process/thread iterator to for_each_process_thread_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_thread() loop in kernel/trace/fgraph.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/trace/fgraph.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
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]);
Signature · 1 line
2.25.1
author
#26 lore raw

PATCH v4 12/15 unwind: convert process/thread iterator to for_each_process_thread_rculock

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() combined with for_each_process_thread() loop in kernel/unwind/deferred.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

---
 kernel/unwind/deferred.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
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
author
#28 lore raw

PATCH v4 13/15 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.

---
 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
author
#30 lore raw

PATCH v4 14/15 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.

---
 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
author
#33 lore raw

PATCH v4 15/15 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.

---
 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
#2 lore raw
In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v4 02/15] mm/oom_kill: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v4 03/15] mm/ksm: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw

Re: [PATCH v4 04/15] mm/memory-failure: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

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 6 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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

Re: [PATCH v4 05/15] cpu/hotplug: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

From: Ye Liu <[email protected]>

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

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#14 lore raw

Re: [PATCH v4 06/15] freezer: convert thread iterator to for_each_thread_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_thread() loop in kernel/freezer.c with for_each_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#16 lore raw

Re: [PATCH v4 07/15] hung_task: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process_thread() loop in kernel/hung_task.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#18 lore raw

Re: [PATCH v4 08/15] locking/lockdep: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process_thread() loop in kernel/locking/lockdep.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#20 lore raw

Re: [PATCH v4 09/15] rcu: convert process/thread iterator to for_each_process_thread_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() pair combined with for_each_process_thread() loop in kernel/rcu/update.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#23 lore raw

Re: [PATCH v4 10/15] sched: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

From: Ye Liu <[email protected]>

Replace the manual rcu_read_lock()/rcu_read_unlock() and guard(rcu) pairs combined with for_each_process_thread() loops in kernel/sched/ with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#25 lore raw

Re: [PATCH v4 11/15] tracing/fgraph: convert process/thread iterator to for_each_process_thread_rculock

In reply to Ye Liu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] trace/fgraph: mixed goto-based error handling with scope-based cleanup --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] trace/fgraph: mixed goto-based error handling with scope-based cleanup --

commit 6422a16035934ad7c858389b51cb5a7d9fecc15f
Author: Ye Liu <[email protected]>
tracing/fgraph: convert process/thread iterator to for_each_process_thread_rculock

This commit converts the manual rcu_read_lock() and rcu_read_unlock() pair around the for_each_process_thread() loop in alloc_retstack_tasklist() to use the for_each_process_thread_rculock() macro. This scopes the RCU read lock to the loop body using a scoped guard, and removes the manual unlock label in the error handling path.

Quoted 5 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 10 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]
This doesn't cause a direct runtime bug here, but does this change mix goto-based cleanup with scope-based cleanup?

The for_each_process_thread_rculock() macro initializes a scope-based RCU guard via scoped_guard(rcu). By using goto free; to jump out of the scoped guard to a manual cleanup label, this explicitly violates the strict kernel cleanup rule in include/linux/cleanup.h meant to prevent double-free and resource leak bugs. That rule states that goto-based cleanup and scope-based cleanup must never be mixed in the same function.

While jumping out of scoped_guard() here triggers the RCU unlock safely before executing the manual free label, should alloc_retstack_tasklist() be fully converted to use scope-based cleanup for the ret_stack_list allocations as well, or should it stick to manual RCU locks to avoid mixing paradigms?

Quoted 3 lines
kernel/trace/fgraph.c +0 -0
 		}
 
 		if (t->ret_stack == NULL) {

[ ... ]

Quoted 10 lines
kernel/trace/fgraph.c +0 -3
@@ -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=11
#27 lore raw

Re: [PATCH v4 12/15] unwind: convert process/thread iterator to for_each_process_thread_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

From: Ye Liu <[email protected]>

Replace guard(rcu)() combined with for_each_process_thread() loop in kernel/unwind/deferred.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

No functional change.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#29 lore raw

Re: [PATCH v4 13/15] fs: convert process/thread iterators to for_each_*_rculock

In reply to Ye Liu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] fs: mixing goto and scoped_guard in __set_oom_adj violates cleanup rules --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] fs: mixing goto and scoped_guard in __set_oom_adj violates cleanup rules --

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

fs: convert process/thread iterators to for_each_*_rculock

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).

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

Quoted 11 lines
fs/proc/base.c +1 -2
index 58be389424605..2c47d838a1c76 100644
@@ -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 introducing for_each_process_rculock() here violate the rule against mixing scope-based cleanup and goto error handling in the same function?

This macro expands to scoped_guard(rcu), introducing scope-based cleanup into __set_oom_adj(). However, this function already uses manual goto-based error handling:

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

The subsystem guidelines require converting all resources in a function to scope-based cleanup, or none at all, explicitly prohibiting mixing both in the same function.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#31 lore raw

Re: [PATCH v4 14/15] lib: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#34 lore raw

Re: [PATCH v4 15/15] security/landlock: convert thread iterator to for_each_thread_rculock

In reply to Ye Liu
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
maintainer Reviewed-by on v4 9/15
#21 lore raw

Re: [PATCH v4 09/15] rcu: convert process/thread iterator to for_each_process_thread_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() pair combined with for_each_process_thread() loop in kernel/rcu/update.c with for_each_process_thread_rculock(), which scopes the RCU read lock to the loop body via scoped_guard(rcu).

Quoted 7 lines

No functional change.

Quoted 28 lines
---
 kernel/rcu/update.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
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)
Signature · 1 line
2.25.1
Reviewed-by on v4 2/15
#5 lore raw

Re: [PATCH v4 02/15] mm/oom_kill: convert process/thread iterators to for_each_*_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() 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.

Quoted 8 lines
---
 mm/oom_kill.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
Reviewed-by on v4 3/15
#8 lore raw

Re: [PATCH v4 03/15] mm/ksm: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 14 lines · Ye Liu
mm/ksm.c +1 -2
@@ -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;

mm/ksm.c void collect_procs_ksm(const struct folio *folio, const struct page *page, · line 3301

Quoted 4 lines
mm/ksm.c +0 -1
@@ -3298,7 +3297,6 @@ void collect_procs_ksm(const struct folio *folio, const struct page *page,
 				}
 			}
 		}
-		rcu_read_unlock();
Reviewed-by on v4 14/15
#32 lore raw

Re: [PATCH v4 14/15] lib: convert process iterator to for_each_process_rculock

In reply to Ye Liu
Quoted 16 lines · Ye Liu
lib/is_single_threaded.c +1 -2
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))

lib/is_single_threaded.c bool current_is_single_threaded(void) · line 52

Quoted 5 lines
lib/is_single_threaded.c +0 -2
@@ -48,7 +47,5 @@ bool current_is_single_threaded(void)
 	}
 	ret = true;
 found:
-	rcu_read_unlock();
-

but I think that the "found" label and "int ret" can now be removed, the for_each_thread() loop can just do

	...
	if (unlikely(t->mm == mm))
		return false;

and current_is_single_threaded() can simply return true after the main for_each_process_rculock() loop.

But again, I agree with this patch, it looks simple and "obviously correct". Even if I am right, the simplifications above need a separate change.

Oleg.

author
#35 lore raw
In reply to Ye Liu

Hi, Peter

Friendly ping on this series, posted three weeks ago. You previously requested splitting the combined kernel/ conversion into per-subsystem patches, which has been done in v4.
No rush — just want to make sure this is on your radar.

Thanks,
Ye Liu

Quoted 108 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.

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.

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

---
Changes since v3 [4]:
- Fix patch 5 (cpu/hotplug): title and commit message incorrectly said "thread iterator" and "for_each_thread_rculock" while the code converts for_each_process() to for_each_process_rculock().

Note: Patches 4, 11, and 13 received automated reviews from sashiko-bot in the v3 thread [4] that were verified as false positives:

- Patch 4: claimed the folio lock is dropped before collect_procs(),

    leading to a NULL deref on folio->mapping; the lock is held in all
    call paths.

- Patch 11: claimed mixing goto/scope-based cleanup in alloc_retstack_tasklist(); the goto free label handles memory cleanup, not lock management.
- Patch 13: claimed mixing goto/scope-based cleanup in __set_oom_adj(); goto err_unlock manages oom_adj_mutex while scoped_guard(rcu) manages the RCU read lock — different resources.

Changes since v2 [3]:
- Split the kernel/ patch into per-subsystem patches, as the combined patch could not be applied (Peter Zijlstra, Steven Rostedt).
- Move changelog below the --- separator so it is excluded from the commit message (Lorenzo Stoakes).
- Indent loop body in macro definitions to show scoped_guard scope (Lorenzo Stoakes).

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).

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

Ye Liu (15):
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
cpu/hotplug: convert process iterator to for_each_process_rculock
freezer: convert thread iterator to for_each_thread_rculock
hung_task: convert process/thread iterators to for_each_*_rculock locking/lockdep: convert process/thread iterators to
for_each_*_rculock
rcu: convert process/thread iterator to
for_each_process_thread_rculock
sched: convert process/thread iterators to for_each_*_rculock tracing/fgraph: convert process/thread iterator to
for_each_process_thread_rculock
unwind: convert process/thread iterator to
for_each_process_thread_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 | 26 ++++++++++++++++++++++++++
 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, 55 insertions(+), 81 deletions(-)

--
2.25.1

Signature · 2 lines
Thanks,
Ye Liu