Skip to content

Commit 2d16180

Browse files
oleg-nesterovAlexei Starovoitov
authored andcommitted
bpf: task_group_seq_get_next: use __next_thread() rather than next_thread()
Lockless use of next_thread() should be avoided, kernel/bpf/task_iter.c is the last user and the usage is wrong. task_group_seq_get_next() can return the group leader twice if it races with mt-thread exec which changes the group->leader's pid. Change the main loop to use __next_thread(), kill "next_tid == common->pid" check. __next_thread() can't loop forever, we can also change this code to retry if next_tid == 0. Signed-off-by: Oleg Nesterov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 16b3129 commit 2d16180

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

kernel/bpf/task_iter.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm
7070
return NULL;
7171

7272
retry:
73-
task = next_thread(task);
73+
task = __next_thread(task);
74+
if (!task)
75+
return NULL;
7476

7577
next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
76-
if (!next_tid || next_tid == common->pid) {
77-
/* Run out of tasks of a process. The tasks of a
78-
* thread_group are linked as circular linked list.
79-
*/
80-
return NULL;
81-
}
78+
if (!next_tid)
79+
goto retry;
8280

8381
if (skip_if_dup_files && task->files == task->group_leader->files)
8482
goto retry;

0 commit comments

Comments
 (0)