Skip to content

Commit

Permalink
Merge branch 'bpf-kernel-bpf-task_iter-c-don-t-abuse-next_thread'
Browse files Browse the repository at this point in the history
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/20231114163211.GA874@redhat.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Alexei Starovoitov committed Nov 19, 2023
2 parents 16b3129 + ac8148d commit 3e124aa
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions kernel/bpf/task_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm
return NULL;

retry:
task = next_thread(task);
task = __next_thread(task);
if (!task)
return NULL;

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

if (skip_if_dup_files && task->files == task->group_leader->files)
goto retry;
Expand Down Expand Up @@ -980,7 +978,6 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
__alignof__(struct bpf_iter_task));

kit->task = kit->pos = NULL;
switch (flags) {
case BPF_TASK_ITER_ALL_THREADS:
case BPF_TASK_ITER_ALL_PROCS:
Expand Down Expand Up @@ -1017,20 +1014,16 @@ __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
if (flags == BPF_TASK_ITER_ALL_PROCS)
goto get_next_task;

kit->pos = next_thread(kit->pos);
if (kit->pos == kit->task) {
if (flags == BPF_TASK_ITER_PROC_THREADS) {
kit->pos = NULL;
return pos;
}
} else
kit->pos = __next_thread(kit->pos);
if (kit->pos || flags == BPF_TASK_ITER_PROC_THREADS)
return pos;

get_next_task:
kit->pos = next_task(kit->pos);
kit->task = kit->pos;
if (kit->pos == &init_task)
kit->task = next_task(kit->task);
if (kit->task == &init_task)
kit->pos = NULL;
else
kit->pos = kit->task;

return pos;
}
Expand Down

0 comments on commit 3e124aa

Please sign in to comment.