bpf: fix bpf_iter's task iterator logic
Subsystems (4)
PATCH v2 1/1 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.
--- kernel/bpf/task_iter.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/task_iter.c +7 -1
@@ -27,9 +27,15 @@ static struct task_struct *task_seq_get_next(struct pid_namespace *ns,struct pid *pid;rcu_read_lock (); +retry: pid =idr_get_next (&ns->idr, tid); -if (pid) +if (pid) { task =get_pid_task (pid,PIDTYPE_PID ); +if (!task) { + ++*tid; + goto retry; + } + }rcu_read_unlock ();return task;
Signature · 1 line
2.24.1
Alexei Starovoitov
AppliedQuoted 8 lines · Andrii Nakryiko
task_seq_get_nextmight stop prematurely ifget_pid_task()fails to gettask_struct. Failure to do so doesn't mean that there are no more tasks with higher pids. Procfs's iteration algorithm (seenext_tgidinfs/proc/base.c) does a retry in such case. After this fix, instead of stopping prematurely after about 300 tasks on my server,bpf_iterprogram now returns >4000, which sounds much closer to reality.
Quoted 4 lines
Cc Yonghong Song <[email protected]>Fixes eaaacd23910f ("bpf: Add task and task/file iterator targets")Acked-by Yonghong Song <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>
Applied. Thanks