Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

introduce __next_thread(), fix next_tid() vs exec() race

Patch series "introduce __next_thread(), change next_thread()".

After commit dce8f8ed1de1 ("document while_each_thread(), change
first_tid() to use for_each_thread()") + this series

1. We have only one lockless user of next_thread(), task_group_seq_get_next().
I think it should be changed too.

2. We have only one user of task_struct->thread_group, thread_group_empty().
The next patches will change thread_group_empty() and kill ->thread_group.


This patch (of 2):

next_tid(start) does:

rcu_read_lock();
if (pid_alive(start)) {
pos = next_thread(start);
if (thread_group_leader(pos))
pos = NULL;
else
get_task_struct(pos);

it should return pos = NULL when next_thread() wraps to the 1st thread
in the thread group, group leader, and the thread_group_leader() check
tries to detect this case.

But this can race with exec. To simplify, suppose we have a main thread
M and a single sub-thread T, next_tid(T) should return NULL.

Now suppose that T execs. If next_tid(T) is called after T changes the
leadership and before it does release_task() which removes the old leader
from list, then next_thread() returns M and thread_group_leader(M) = F.

Lockless use of next_thread() should be avoided. After this change only
task_group_seq_get_next() does this, and I believe it should be changed
as well.

Link: https://lkml.kernel.org/r/20230824143112.GA31208@redhat.com
Link: https://lkml.kernel.org/r/20230824143142.GA31222@redhat.com
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Oleg Nesterov and committed by
Andrew Morton
33a98138 00adf323

+13 -4
+2 -4
fs/proc/base.c
··· 3840 3840 struct task_struct *pos = NULL; 3841 3841 rcu_read_lock(); 3842 3842 if (pid_alive(start)) { 3843 - pos = next_thread(start); 3844 - if (thread_group_leader(pos)) 3845 - pos = NULL; 3846 - else 3843 + pos = __next_thread(start); 3844 + if (pos) 3847 3845 get_task_struct(pos); 3848 3846 } 3849 3847 rcu_read_unlock();
+11
include/linux/sched/signal.h
··· 715 715 return p1->signal == p2->signal; 716 716 } 717 717 718 + /* 719 + * returns NULL if p is the last thread in the thread group 720 + */ 721 + static inline struct task_struct *__next_thread(struct task_struct *p) 722 + { 723 + return list_next_or_null_rcu(&p->signal->thread_head, 724 + &p->thread_node, 725 + struct task_struct, 726 + thread_node); 727 + } 728 + 718 729 static inline struct task_struct *next_thread(const struct task_struct *p) 719 730 { 720 731 return list_entry_rcu(p->thread_group.next,