Skip to content

Commit c70f34a

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Fix bpf_iter's task iterator logic
task_seq_get_next might stop prematurely if get_pid_task() fails to get task_struct. Failure to do so doesn't mean that there are no more tasks with higher pids. Procfs's iteration algorithm (see next_tgid in fs/proc/base.c) does a retry in such case. After this fix, instead of stopping prematurely after about 300 tasks on my server, bpf_iter program now returns >4000, which sounds much closer to reality. Fixes: eaaacd2 ("bpf: Add task and task/file iterator targets") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0645f7e commit c70f34a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

kernel/bpf/task_iter.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
2727
struct pid *pid;
2828

2929
rcu_read_lock();
30+
retry:
3031
pid = idr_get_next(&ns->idr, tid);
31-
if (pid)
32+
if (pid) {
3233
task = get_pid_task(pid, PIDTYPE_PID);
34+
if (!task) {
35+
++*tid;
36+
goto retry;
37+
}
38+
}
3339
rcu_read_unlock();
3440

3541
return task;

0 commit comments

Comments
 (0)