Skip to content

Commit dce8f8e

Browse files
oleg-nesterovakpm00
authored andcommitted
document while_each_thread(), change first_tid() to use for_each_thread()
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@redhat.com Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ed1af26 commit dce8f8e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

fs/proc/base.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,11 +3813,10 @@ static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
38133813
/* If we haven't found our starting place yet start
38143814
* with the leader and walk nr threads forward.
38153815
*/
3816-
pos = task = task->group_leader;
3817-
do {
3816+
for_each_thread(task, pos) {
38183817
if (!nr--)
38193818
goto found;
3820-
} while_each_thread(task, pos);
3819+
};
38213820
fail:
38223821
pos = NULL;
38233822
goto out;

include/linux/sched/signal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ extern void flush_itimer_signals(void);
648648

649649
extern bool current_is_single_threaded(void);
650650

651+
/*
652+
* Without tasklist/siglock it is only rcu-safe if g can't exit/exec,
653+
* otherwise next_thread(t) will never reach g after list_del_rcu(g).
654+
*/
651655
#define while_each_thread(g, t) \
652656
while ((t = next_thread(t)) != g)
653657

0 commit comments

Comments
 (0)