Commit 3e124aa6 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

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/20231114163211.GA874@redhat.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 16b3129e ac8148d9
Loading
Loading
Loading
Loading
+11 −18
Original line number Diff line number Diff line
@@ -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;
@@ -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:
@@ -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;
}