Skip to content

Commit 3e124aa

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-kernel-bpf-task_iter-c-don-t-abuse-next_thread'
Oleg Nesterov says: ==================== bpf: kernel/bpf/task_iter.c: don't abuse next_thread() Compile tested. Every lockless usage of next_thread() was wrong, bpf/task_iter.c is the last user and is no exception. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 16b3129 + ac8148d commit 3e124aa

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

kernel/bpf/task_iter.c

Lines changed: 11 additions & 18 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;
@@ -980,7 +978,6 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
980978
BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
981979
__alignof__(struct bpf_iter_task));
982980

983-
kit->task = kit->pos = NULL;
984981
switch (flags) {
985982
case BPF_TASK_ITER_ALL_THREADS:
986983
case BPF_TASK_ITER_ALL_PROCS:
@@ -1017,20 +1014,16 @@ __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
10171014
if (flags == BPF_TASK_ITER_ALL_PROCS)
10181015
goto get_next_task;
10191016

1020-
kit->pos = next_thread(kit->pos);
1021-
if (kit->pos == kit->task) {
1022-
if (flags == BPF_TASK_ITER_PROC_THREADS) {
1023-
kit->pos = NULL;
1024-
return pos;
1025-
}
1026-
} else
1017+
kit->pos = __next_thread(kit->pos);
1018+
if (kit->pos || flags == BPF_TASK_ITER_PROC_THREADS)
10271019
return pos;
10281020

10291021
get_next_task:
1030-
kit->pos = next_task(kit->pos);
1031-
kit->task = kit->pos;
1032-
if (kit->pos == &init_task)
1022+
kit->task = next_task(kit->task);
1023+
if (kit->task == &init_task)
10331024
kit->pos = NULL;
1025+
else
1026+
kit->pos = kit->task;
10341027

10351028
return pos;
10361029
}

0 commit comments

Comments
 (0)