The patch titled Subject: document while_each_thread(), change first_tid() to use for_each_thread() has been added to the -mm mm-nonmm-unstable branch. Its filename is document-while_each_thread-change-first_tid-to-use-for_each_thread.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/document-while_each_thread-change-first_tid-to-use-for_each_thread.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Oleg Nesterov <oleg@xxxxxxxxxx> Subject: document while_each_thread(), change first_tid() to use for_each_thread() Date: Wed, 23 Aug 2023 19:08:06 +0200 Add the comment to explain that while_each_thread(g,t) is not rcu-safe unless g is stable (e.g. current). Even if g is a group leader and thus can't exit before t, t or another sub-thread can exec and remove g from the thread_group list. The only lockless user of while_each_thread() is first_tid() and it is fine in that it can't loop forever, yet for_each_thread() looks better and I am going to change while_each_thread/next_thread. Link: https://lkml.kernel.org/r/20230823170806.GA11724@xxxxxxxxxx Signed-off-by: Oleg Nesterov <oleg@xxxxxxxxxx> Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- fs/proc/base.c | 5 ++--- include/linux/sched/signal.h | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) --- a/fs/proc/base.c~document-while_each_thread-change-first_tid-to-use-for_each_thread +++ a/fs/proc/base.c @@ -3814,11 +3814,10 @@ static struct task_struct *first_tid(str /* If we haven't found our starting place yet start * with the leader and walk nr threads forward. */ - pos = task = task->group_leader; - do { + for_each_thread(task, pos) { if (!nr--) goto found; - } while_each_thread(task, pos); + }; fail: pos = NULL; goto out; --- a/include/linux/sched/signal.h~document-while_each_thread-change-first_tid-to-use-for_each_thread +++ a/include/linux/sched/signal.h @@ -648,6 +648,10 @@ extern void flush_itimer_signals(void); extern bool current_is_single_threaded(void); +/* + * Without tasklist/siglock it is only rcu-safe if g can't exit/exec, + * otherwise next_thread(t) will never reach g after list_del_rcu(g). + */ #define while_each_thread(g, t) \ while ((t = next_thread(t)) != g) _ Patches currently in -mm which might be from oleg@xxxxxxxxxx are document-while_each_thread-change-first_tid-to-use-for_each_thread.patch