Skip to contentSkip to conversation

mm: introduce for_each_process_rcu and for_each_thread_rcu

PATCH In review Ye Liu 14 participants 39 messages Sep 4 – Sep 11

Subsystems (19)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

cover mm: introduce for_each_process_rcu and for_each_thread_rcu

From: Ye Liu <[email protected]>

Introduce for_each_process_rcu(), for_each_thread_rcu() and for_each_process_thread_rcu() 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.

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 [1].

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

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

 fs/proc/base.c               |  4 +---
 fs/resctrl/rdtgroup.c        |  8 ++------
 include/linux/sched/signal.h | 19 +++++++++++++++++++
 kernel/cpu.c                 |  4 +---
 kernel/freezer.c             |  4 +---
 kernel/hung_task.c           |  7 ++-----
 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, 46 insertions(+), 79 deletions(-)

--
2.25.1

author
#1 lore raw

PATCH 1/8 mm: introduce for_each_process_rcu and for_each_thread_rcu

From: Ye Liu <[email protected]>

Introduce for_each_process_rcu(), for_each_thread_rcu() and for_each_process_thread_rcu() 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 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/sched/signal.h +19 -0
index 584ae88b435e..fe0c41dc99a1 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,24 @@ extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 
+/*
+ * RCU-internal variants: automatically acquire and release the RCU read
+ * lock around the iteration.  Equivalent to scoped_guard(rcu) combined
+ * with the respective non-_rcu variant.
+ */
+#define for_each_process_rcu(p) \
+	scoped_guard(rcu) \
+	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+
+#define for_each_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)
+
+/* Careful: this is a double loop, 'break' won't work as expected. */
+#define for_each_process_thread_rcu(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
#11 lore raw

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

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_rcu() and for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

No functional change.

---
 mm/oom_kill.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
mm/oom_kill.c +5 -15
index 5f372f6e26fa..8dd025965810 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_rcu(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_rcu(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_rcu(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_rcu(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_rcu(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
#14 lore raw

PATCH 3/8 mm/ksm: convert process iterator to for_each_process_rcu

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

No functional change.

---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -3
index 49d48d1e0998..fa5e4c47fb1b 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_rcu(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
#17 lore raw

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

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

No functional change.

---
 mm/memory-failure.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
mm/memory-failure.c +4 -12
index a8b03e2920ba..63a2ff08ee33 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_rcu(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_rcu(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_rcu(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_rcu(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
#21 lore raw

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

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_*_rcu() macros.

No functional change.

---
 kernel/cpu.c             | 4 +---
 kernel/freezer.c         | 4 +---
 kernel/hung_task.c       | 7 ++-----
 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, 11 insertions(+), 30 deletions(-)
kernel/cpu.c +1 -3
index b3c8553d7bd6..bc70fd21561a 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_rcu(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..a27b2382fda9 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_rcu(p, t) {
 		__thaw_task(t);
 	}
-	rcu_read_unlock();
 }
 
 /**
kernel/hung_task.c +2 -5
index 6fcc94ce4ca9..76871f78497f 100644
@@ -315,8 +315,7 @@ 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_rcu(g, t) {
 		if (!max_count--)
 			goto unlock;
 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
@@ -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();
-
+unlock:
 	if (!this_round_count)
 		return;
 
kernel/locking/lockdep.c +1 -3
index c56a7f91d72e..9c3dce0f0c4b 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_rcu(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..c48a84a0da82 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_rcu(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..a41cdd2ff02f 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_rcu(g, p)
 		uclamp_update_util_min_rt_default(p);
 }
 
kernel/sched/debug.c +1 -3
index 72236db67983..8f37b1a7adfe 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_rcu(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..e522433ec8a5 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_rcu(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..61fe2d4727ce 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_rcu(g, t) {
 		atomic_long_andnot(BIT(bit),
 				   &t->unwind_info.unwind_mask);
 		if (t->unwind_info.cache)
Signature · 1 line
2.25.1
author
#25 lore raw

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

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_*_rcu() macros.

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..da36ba73dc17 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_rcu(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..3f96d21b84ab 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_rcu(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_rcu(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
#34 lore raw

PATCH 7/8 lib: convert process iterator to for_each_process_rcu

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_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..0682dc87bcdf 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_rcu(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
#36 lore raw

PATCH 8/8 security/landlock: convert thread iterator to for_each_thread_rcu

In reply to Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

No functional change.

---
 security/landlock/tsync.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
security/landlock/tsync.c +2 -6
index 0b71e158c3f5..f1c08aae179b 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_rcu(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_rcu(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
Signature · 1 line
2.25.1
#22 lore raw

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

In reply to Ye Liu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Mixing goto-based error cleanup with guard() scopes in the same function…

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

commit f2b0046bb431ebefb3d059e22788f2810f0e6684
Author: Ye Liu <[email protected]>
kernel: convert process/thread iterators to for_each_*_rcu

This commit converts multiple manual rcu_read_lock() and iterator blocks across the kernel to use the new for_each_*_rcu() macros.

Quoted 4 lines
kernel/trace/fgraph.c +0 -0
index 40d373d65f9b9..e522433ec8a5a 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_rcu(g, t) {
 		if (start == end) {
 			ret = -EAGAIN;
-			goto unlock;
+			goto free;

[Severity: Low]
Does this change violate the cleanup subsystem guidelines by mixing paradigms?

The for_each_process_thread_rcu() macro expands to include a scoped_guard(rcu). However, alloc_retstack_tasklist() still uses a goto free statement to manually clean up the ret_stack_list array allocations upon error.

According to the kernel guidelines in include/linux/cleanup.h, the expectation is that a given function either converts all resources requiring a goto cleanup to scope-based cleanup, or converts none of them. Mixing goto-based error handling with guard() or scoped_guard() scopes in the same function should be avoided.

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=5
#26 lore raw

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

In reply to Ye Liu

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

Quoted 10 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;

Hmm... I am not aware of for_each_process_rcu(), but looking at this change I guess it includes something like scope_guard(rcu) ?

Perhaps makes sense, but the naming looks sligthly confusing to me. I mean, to for_each_process_rcu() looks like (say) list_for_each_entry_rcu() where _rcu has another meaning...

Oleg.

#27 lore raw

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

In reply to Ye Liu

Please cc everybody on every patch in the series :) it makes it incredible hard for me to see context otherwise.

I can already see a comment I'd like to leave on another patch in the series but it's a total pain for me go retrieve that to do it.

And I worry about acking one bit only to find out a horrible flaw in another part :P

Can you please make sure to do that on any respin?...

Quoted 5 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_*_rcu() macros.

Probably worth mentioning that they hold the RCU lock in a scoped guard over the operation.

Quoted 4 lines

No functional change.

In general it looks reasonable to me, but I wonder if you're correctly including linux/cleanup.h to have the scope guard available...

Anyway can check that on respin with the right cc ;)

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

Quoted 10 lines
---
 fs/proc/base.c        | 4 +---
 fs/resctrl/rdtgroup.c | 8 ++------
 2 files changed, 3 insertions(+), 9 deletions(-)
fs/proc/base.c +0 -0
index 6a39de424f62..da36ba73dc17 100644
@@ -1160,8 +1160,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)

Side-note - I wonder if this really belongs in mm/oom_kill.c? Seems really odd to have it here.

Quoted 55 lines
fs/proc/base.c +1 -3
 	if (mm) {
 		struct task_struct *p;
 
-		rcu_read_lock();
-		for_each_process(p) {
+		for_each_process_rcu(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..3f96d21b84ab 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_rcu(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_rcu(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

maintainer Acked-by on v1 1/8
#2 lore raw
In reply to Ye Liu

You want to CC Oleg at least. Now added

Also prefixing this patch with mm is confusing as this has nothing to do with mm.

Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Introduce for_each_process_rcu(), for_each_thread_rcu() and for_each_process_thread_rcu() 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.

Apparat from potential naming changes this makes a lot of sense to me. Many users really need RCU in the scope of iteration and this simplifies the code and it makes less prone to bugs.

I do not really care much about the naming TBH

Quoted 44 lines
---
 include/linux/sched/signal.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/sched/signal.h +19 -0
index 584ae88b435e..fe0c41dc99a1 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,24 @@ extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 
+/*
+ * RCU-internal variants: automatically acquire and release the RCU read
+ * lock around the iteration.  Equivalent to scoped_guard(rcu) combined
+ * with the respective non-_rcu variant.
+ */
+#define for_each_process_rcu(p) \
+	scoped_guard(rcu) \
+	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+
+#define for_each_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)
+
+/* Careful: this is a double loop, 'break' won't work as expected. */
+#define for_each_process_thread_rcu(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
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 2/8
#12 lore raw

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

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_rcu() and for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

No functional change.

Quoted 93 lines
---
 mm/oom_kill.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
mm/oom_kill.c +5 -15
index 5f372f6e26fa..8dd025965810 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_rcu(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_rcu(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_rcu(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_rcu(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_rcu(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
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 3/8
#15 lore raw

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

In reply to Ye Liu
Quoted 8 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_rcu().

No functional change.

Quoted 29 lines
---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -3
index 49d48d1e0998..fa5e4c47fb1b 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_rcu(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
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 4/8
#18 lore raw

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

In reply to Ye Liu
Quoted 9 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_rcu().

No functional change.

Quoted 84 lines
---
 mm/memory-failure.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
mm/memory-failure.c +4 -12
index a8b03e2920ba..63a2ff08ee33 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_rcu(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_rcu(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_rcu(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_rcu(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
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 5/8
#23 lore raw

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

In reply to Ye Liu
Quoted 10 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_*_rcu() macros.

No functional change.

Quoted 198 lines
---
 kernel/cpu.c             | 4 +---
 kernel/freezer.c         | 4 +---
 kernel/hung_task.c       | 7 ++-----
 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, 11 insertions(+), 30 deletions(-)
kernel/cpu.c +1 -3
index b3c8553d7bd6..bc70fd21561a 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_rcu(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..a27b2382fda9 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_rcu(p, t) {
 		__thaw_task(t);
 	}
-	rcu_read_unlock();
 }
 
 /**
kernel/hung_task.c +2 -5
index 6fcc94ce4ca9..76871f78497f 100644
@@ -315,8 +315,7 @@ 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_rcu(g, t) {
 		if (!max_count--)
 			goto unlock;
 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
@@ -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();
-
+unlock:
 	if (!this_round_count)
 		return;
 
kernel/locking/lockdep.c +1 -3
index c56a7f91d72e..9c3dce0f0c4b 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_rcu(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..c48a84a0da82 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_rcu(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..a41cdd2ff02f 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_rcu(g, p)
 		uclamp_update_util_min_rt_default(p);
 }
 
kernel/sched/debug.c +1 -3
index 72236db67983..8f37b1a7adfe 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_rcu(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..e522433ec8a5 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_rcu(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..61fe2d4727ce 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_rcu(g, t) {
 		atomic_long_andnot(BIT(bit),
 				   &t->unwind_info.unwind_mask);
 		if (t->unwind_info.cache)
Signature · 1 line
2.25.1
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 6/8
#28 lore raw

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

In reply to Ye Liu
Quoted 9 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_*_rcu() macros.

No functional change.

Quoted 67 lines
---
 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..da36ba73dc17 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_rcu(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..3f96d21b84ab 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_rcu(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_rcu(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
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer Acked-by on v1 7/8
#35 lore raw

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

In reply to Ye Liu
Quoted 27 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_rcu().

No functional change.

---
 lib/is_single_threaded.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/is_single_threaded.c +1 -2
index 8c98b20bfc41..0682dc87bcdf 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_rcu(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 50

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

This can be slightly simplified as below. Anyway

---

lib/is_single_threaded.c +3 -10
index 8c98b20bfc41..4f7620481066 100644
@@ -17,7 +17,6 @@ bool current_is_single_threaded(void)
 	struct task_struct *task = current;
 	struct mm_struct *mm = task->mm;
 	struct task_struct *p, *t;
-	bool ret;
 
 	if (atomic_read(&task->signal->live) != 1)
 		return false;
@@ -25,9 +24,7 @@ bool current_is_single_threaded(void)
 	if (atomic_read(&mm->mm_users) == 1)
 		return true;
 
-	ret = false;
-	rcu_read_lock();
-	for_each_process(p) {
+	for_each_process_rcu(p) {
 		if (unlikely(p->flags & PF_KTHREAD))
 			continue;
 		if (unlikely(p == task->group_leader))
@@ -35,7 +32,7 @@ bool current_is_single_threaded(void)
 
 		for_each_thread(p, t) {
 			if (unlikely(t->mm == mm))
-				goto found;
+				return false
 			if (likely(t->mm))
 				break;
 			/*
@@ -46,9 +43,5 @@ bool current_is_single_threaded(void)
 			smp_rmb();
 		}
 	}
-	ret = true;
-found:
-	rcu_read_unlock();
-
-	return ret;
+	return true;
 }
Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by on v1 8/8
#37 lore raw

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

In reply to Ye Liu
Quoted 28 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

No functional change.

---
 security/landlock/tsync.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
security/landlock/tsync.c +1 -3
index 0b71e158c3f5..f1c08aae179b 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_rcu(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;

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

Quoted 10 lines
security/landlock/tsync.c +1 -3
@@ -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_rcu(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
Quoted 4 lines
Signature · 1 line
2.25.1
#24 lore raw

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

In reply to Ye Liu
Quoted 5 lines · Ye Liu
kernel/hung_task.c +0 -0
index 6fcc94ce4ca9..76871f78497f 100644

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

Quoted 9 lines
kernel/hung_task.c +1 -3
@@ -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();
-
+unlock:
 	if (!this_round_count)
 		return;

The label name "unlock" is misleading now. There is no further unlock happening after it any more.

–Günther

Reviewed-by on v1 8/8
#38 lore raw

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

In reply to Ye Liu
Quoted 41 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace guard(rcu)() + for_each_thread() with for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

No functional change.

---
 security/landlock/tsync.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
security/landlock/tsync.c +2 -6
index 0b71e158c3f5..f1c08aae179b 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_rcu(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_rcu(caller, thread) {
 		/* Skip current, since it is initiating the sync. */
 		if (thread == caller)
 			continue;
Signature · 1 line
2.25.1

Looks good, provided that the for_each_thread_rcu() macro gets accepted.

Although, I find that in the Landlock case, it does not provide a very strong advantage over the explicit "guard(rcu)();", and I find it normally preferrable to use orthogonal APIs.

–Günther

maintainer
#3 lore raw
In reply to Ye Liu
Quoted 33 lines · On Fri, 4 Sep 2026 16:29:53 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Introduce for_each_process_rcu(), for_each_thread_rcu() and for_each_process_thread_rcu() 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 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/sched/signal.h +4 -0
index 584ae88b435e..fe0c41dc99a1 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,24 @@ extern bool current_is_single_threaded(void);
 #define for_each_process_thread(p, t)	\
 	for_each_process(p) for_each_thread(p, t)
 
+/*
+ * RCU-internal variants: automatically acquire and release the RCU read
+ * lock around the iteration.  Equivalent to scoped_guard(rcu) combined

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

Quoted 10 lines
include/linux/sched/signal.h +10 -0
+ * with the respective non-_rcu variant.
+ */
+#define for_each_process_rcu(p) \
+	scoped_guard(rcu) \
+	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
+
+#define for_each_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)
+

I would rename it to: *_rculock()

as there are already *_rcu() list iterators that are used to be done within an rcu locked region. Using just *_rcu() would make it confusing to know if the rcu lock was taken or if the loop is expected to be locked within RCU.

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

Quoted 4 lines
include/linux/sched/signal.h +4 -0
+/* Careful: this is a double loop, 'break' won't work as expected. */
+#define for_each_process_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	for_each_process(p) for_each_thread(p, t)

I would not do this one. It is prone to bugs. As it is a double loop, the way to "break" is to do a goto. That will be bug to have a goto jump out of a scoped_guard().

-- Steve

Quoted 4 lines
include/linux/sched/signal.h +1 -0
+
 typedef int (*proc_visitor)(struct task_struct *p, void *data);
 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
 
maintainer
#4 lore raw
Quoted 9 lines · Steven Rostedt

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

Quoted 4 lines · On Fri, 4 Sep 2026 16:29:53 +0800 Ye Liu <[email protected]> wrote:
include/linux/sched/signal.h +4 -0
+#define for_each_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	__for_each_thread((p)->signal, t)
+

I would rename it to: *_rculock()

Quoted 3 lines

as there are already *_rcu() list iterators that are used to be done within an rcu locked region. Using just *_rcu() would make it confusing to know if the rcu lock was taken or if the loop is expected to be locked within RCU.

Ack.

Quoted 8 lines

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

Quoted 4 lines
include/linux/sched/signal.h +4 -0
+/* Careful: this is a double loop, 'break' won't work as expected. */
+#define for_each_process_thread_rcu(p, t) \
+	scoped_guard(rcu) \
+	for_each_process(p) for_each_thread(p, t)

I would not do this one. It is prone to bugs. As it is a double loop, the way to "break" is to do a goto. That will be bug to have a goto jump out of a scoped_guard().

No. Scopes can be left by any valid termination mechanism.

The only problematic case of leaving a scoped_guard() with goto is when the goto is actually implemented as an ASM goto. See the comment above arch_unsafe_get_user() in linux/uaccess.h.

But I agree that requiring a goto to break out of the nested loop is nasty especially when it's not well documented. The copied original comment is pretty useless.

Thanks,

tglx

maintainer
#5 lore raw
Quoted 5 lines · On Fri, 04 Sep 2026 23:17:38 +0200 Thomas Gleixner <[email protected]> wrote:

No. Scopes can be left by any valid termination mechanism.

The only problematic case of leaving a scoped_guard() with goto is when the goto is actually implemented as an ASM goto. See the comment above arch_unsafe_get_user() in linux/uaccess.h.

Oh OK. I was under the impression that goto's could cause undefined behavior with guards. Or is that just when a goto jumps over one? Or is that OK too?

-- Steve

Reviewed-by on v1 2/8
#13 lore raw

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

In reply to Ye Liu
Quoted 6 lines · On Fri, 4 Sep 2026 16:29:54 +0800 Ye Liu <[email protected]> wrote:

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_rcu() and for_each_thread_rcu(), so that the RCU read-side critical section is scoped to the loop body.

Looks clean to me :)

Quoted 4 lines

No functional change.

Thanks,
SJ

[...]

Reviewed-by on v1 3/8
#16 lore raw

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

In reply to Ye Liu
Quoted 6 lines · On Fri, 4 Sep 2026 16:29:55 +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 mm/ksm.c with for_each_process_rcu().

No functional change.

Looks clean to me, thank you.

Quoted 2 lines

Thanks,
SJ

[...]

#19 lore raw

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

In reply to Ye Liu
Quoted 36 lines · On Fri, 4 Sep 2026 16:29:56 +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_rcu().

No functional change.

---
 mm/memory-failure.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
mm/memory-failure.c +1 -3
index a8b03e2920ba..63a2ff08ee33 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_rcu(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);
 }
 

mm/memory-failure.c static void collect_procs_file(const struct folio *folio, · line 593

Quoted 9 lines
mm/memory-failure.c +1 -2
@@ -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_rcu(tsk) {
 		struct task_struct *t = task_early_kill(tsk, force_early);
 		unsigned long addr;

This moves pgoff assignment out of the RCU critical section. Is this fine and making no real functional change? If it is fine and no functiona change, could we clarify the reasons on the commit message?

Thanks,
SJ

[...]

maintainer
#6 lore raw
In reply to Steven Rostedt
Quoted 5 lines · Steven Rostedt
Quoted 2 lines · On Fri, 04 Sep 2026 23:17:38 +0200 Thomas Gleixner <[email protected]> wrote:

No. Scopes can be left by any valid termination mechanism.

Quoted 7 lines
Quoted 3 lines

The only problematic case of leaving a scoped_guard() with goto is when the goto is actually implemented as an ASM goto. See the comment above arch_unsafe_get_user() in linux/uaccess.h.

Oh OK. I was under the impression that goto's could cause undefined behavior with guards. Or is that just when a goto jumps over one? Or is that OK too?

Jump where ever you want. The keyword is 'scope'.

The normal visibility rules of variables in C scopes apply. So if the scope for which a variable is defined is left and the variable is defined with __attribute__((__cleanup__(cleanup_func))) then the compiler inserts a call to 'cleanup_func()'.

The problem with ASM GOTO is that the 'goto' is not visible to the C compiler because it is burried in the asm inline. Clang detects it at some later point and fails the build. GCC simply emits buggy code or at least used to. Haven't checked whether that's still the case.

So we worked around that by letting the ASM goto jump to a local label within the scope and use goto 'outofscope' from there. That makes it observe that the scope is left and the cleanup call is inserted at the right place. See 3eb6660f26d1 ("uaccess: Provide ASM GOTO safe wrappers for unsafe_*_user()")

Thanks,

tglx

maintainer Acked-by on v1 4/8
#20 lore raw

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

In reply to Ye Liu
Quoted 9 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_rcu().

No functional change.

With SJ's comment addressed:

Thanks.
.

maintainer
#7 lore raw
In reply to Thomas Gleixner
Quoted 26 lines · Thomas Gleixner
Quoted 5 lines · Steven Rostedt
Quoted 2 lines · On Fri, 04 Sep 2026 23:17:38 +0200 Thomas Gleixner <[email protected]> wrote:

No. Scopes can be left by any valid termination mechanism.

Quoted 7 lines
Quoted 3 lines

The only problematic case of leaving a scoped_guard() with goto is when the goto is actually implemented as an ASM goto. See the comment above arch_unsafe_get_user() in linux/uaccess.h.

Oh OK. I was under the impression that goto's could cause undefined behavior with guards. Or is that just when a goto jumps over one? Or is that OK too?

Jump where ever you want. The keyword is 'scope'.

The normal visibility rules of variables in C scopes apply. So if the scope for which a variable is defined is left and the variable is defined with __attribute__((__cleanup__(cleanup_func))) then the compiler inserts a call to 'cleanup_func()'.

The problem with ASM GOTO is that the 'goto' is not visible to the C compiler because it is burried in the asm inline. Clang detects it at some later point and fails the build. GCC simply emits buggy code or at least used to. Haven't checked whether that's still the case.

Quoted 5 lines

So we worked around that by letting the ASM goto jump to a local label within the scope and use goto 'outofscope' from there. That makes it observe that the scope is left and the cleanup call is inserted at the right place. See 3eb6660f26d1 ("uaccess: Provide ASM GOTO safe wrappers for unsafe_*_user()")

Right, so GCC will accept and miscompile jumps into a scope. Clang will warn/error about this, so this should never happen.

Aside from the ASM GOTO issue, there is also an issue with computed goto, those don't work either.

maintainer
#10 lore raw
In reply to Thomas Gleixner
Quoted 6 lines · On Sat, 05 Sep 2026 09:27:33 +0200 Thomas Gleixner <[email protected]> wrote:

Jump where ever you want. The keyword is 'scope'.

The normal visibility rules of variables in C scopes apply. So if the scope for which a variable is defined is left and the variable is defined with __attribute__((__cleanup__(cleanup_func))) then the compiler inserts a call to 'cleanup_func()'.

I guess I was just confused by the comment in cleanup.h:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/cleanup.h#n147

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

Hence, I was thinking that scoped guards had some issues with gotos. Specifically the "goto statement can jump between scopes" I thought it was more than just a guideline and that it could actually cause issues with the compiled code.

-- Steve

#29 lore raw

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

In reply to Ye Liu
Quoted 13 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;
 

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

Quoted 7 lines
fs/proc/base.c +0 -1
@@ -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:

What's going on here?
Now it's RCU unlock after mmdrop().

These scoped guards kind of suck unless you allow infinite line length.

A

#30 lore raw

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

Quoted 13 lines · Alexey Dobriyan
Quoted 12 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;
 
Quoted 11 lines

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

Quoted 8 lines
fs/proc/base.c +0 -1
@@ -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:

What's going on here?
Now it's RCU unlock after mmdrop().

No it's not, scoped_guard() is scoped to the for_each_process_rcu() block which ends before mmdrop(), i.e. the exact same scope as before.

That closing '}' is attached to the if (mm).

Quoted 4 lines

These scoped guards kind of suck unless you allow infinite line length.

A

--
Cheers, Lorenzo

#33 lore raw

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

In reply to Alexey Dobriyan
Quoted 14 lines · Alexey Dobriyan
Quoted 12 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;
 
Quoted 11 lines

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

Quoted 8 lines
fs/proc/base.c +0 -1
@@ -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:

What's going on here?
Now it's RCU unlock after mmdrop().

Confused... Why do you think so?

I think mmdrop() is called after rcu_read_unlock(), with or without this change.

No?

Oleg.

#31 lore raw

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

In reply to Lorenzo Stoakes (ARM)
Quoted 26 lines · Lorenzo Stoakes (ARM)
Quoted 13 lines · Alexey Dobriyan
Quoted 12 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;
 
Quoted 11 lines

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

Quoted 8 lines
fs/proc/base.c +0 -1
@@ -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:

What's going on here?
Now it's RCU unlock after mmdrop().

Quoted 4 lines

No it's not, scoped_guard() is scoped to the for_each_process_rcu() block which ends before mmdrop(), i.e. the exact same scope as before.

That closing '}' is attached to the if (mm).

OK.

I think naming (and macro) are misguided:
* _rcu means "use under RCU section opened elsewhere", this one is "I open and close RCU section myself".

* not every 2 lines should be combined into new interface.

#32 lore raw

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

Quoted 30 lines · Alexey Dobriyan
Quoted 29 lines · Lorenzo Stoakes (ARM)
Quoted 13 lines · Alexey Dobriyan
Quoted 12 lines · Ye Liu
fs/proc/base.c +1 -2
@@ -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_rcu(p) {
 			if (same_thread_group(task, p))
 				continue;
 
Quoted 11 lines

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

Quoted 8 lines
fs/proc/base.c +0 -1
@@ -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:

What's going on here?
Now it's RCU unlock after mmdrop().

No it's not, scoped_guard() is scoped to the for_each_process_rcu() block which ends before mmdrop(), i.e. the exact same scope as before.

Quoted 7 lines
Quoted 1 line

That closing '}' is attached to the if (mm).

OK.

I think naming (and macro) are misguided:
* _rcu means "use under RCU section opened elsewhere", this one is "I open and close RCU section myself".

You're replying to a v1 of a series that has a v2 which changes the naming.

Quoted 2 lines

* not every 2 lines should be combined into new interface.

Nobody is suggesting every 2 lines should be combined into a new interface.

They're suggesting that explicitly tying lock lifetime to block scope is a really good idea.

And having debugged lots of locking issues, I agree.

Anyway the v2 is at:

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

--
Cheers, Lorenzo

maintainer
#8 lore raw
In reply to Peter Zijlstra
Quoted 5 lines · On Tue, 8 Sep 2026 10:38:17 +0200 Peter Zijlstra <[email protected]> wrote:

Right, so GCC will accept and miscompile jumps into a scope. Clang will warn/error about this, so this should never happen.

Aside from the ASM GOTO issue, there is also an issue with computed goto, those don't work either.

So should we have Sashiko stop complaining about it?

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

By replacing the manual RCU lock with for_each_process_thread_rculock() (which uses scoped_guard(rcu)), the function now relies on scope-based cleanup for the RCU lock, but still jumps to a manual "goto free" label for the memory cleanup below.

The cleanup guidelines state that for a given routine, we should convert all resources that need a "goto" cleanup to scope-based cleanup, or convert none of them, avoiding mixed usage in the same function.

https://lore.kernel.org/linux-trace-kernel/[email protected]/

-- Steve

#9 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 5 lines · On Tue, 8 Sep 2026 10:38:17 +0200 Peter Zijlstra <[email protected]> wrote:

Right, so GCC will accept and miscompile jumps into a scope. Clang will warn/error about this, so this should never happen.

Aside from the ASM GOTO issue, there is also an issue with computed goto, those don't work either.

So should we have Sashiko stop complaining about it?

Yes please. I don't like it that Sashiko complains about regular goto statements inside scope-based cleanup code.

Thanks,

Bart.