Skip to contentSkip to conversation

bpf: fix bpf_iter's task iterator logic

PATCH Superseded Andrii Nakryiko 4 participants 4 messages May 13, 2020 – May 14, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Cc Yonghong Song <[email protected]>
Fixes eaaacd23910f ("bpf: Add task and task/file iterator targets")
---
 kernel/bpf/task_iter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/task_iter.c +7 -1
index a9b7264dda08..e1836def6738 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrii Nakryiko

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.

Cc Yonghong Song <[email protected]>
Fixes eaaacd23910f ("bpf: Add task and task/file iterator targets")

Thanks for the fix. We did this retry logic for bpf_map which is idr based logic too. But forgot to check for task which has the same issue.

Quoted 26 lines
---
  kernel/bpf/task_iter.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/task_iter.c +7 -1
index a9b7264dda08..e1836def6738 100644
@@ -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;
#2 lore raw
In reply to Andrii Nakryiko
Quoted 20 lines · Andrii Nakryiko

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.

Cc Yonghong Song <[email protected]>
Fixes eaaacd23910f ("bpf: Add task and task/file iterator targets")
---
 kernel/bpf/task_iter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/task_iter.c +0 -0
index a9b7264dda08..e1836def6738 100644

kernel/bpf/task_iter.c static struct task_struct *task_seq_get_next(struct pid_namespace *ns, · line 35

Quoted 10 lines
kernel/bpf/task_iter.c +4 -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++;

../kernel/bpf/task_iter.c: In function ‘task_seq_get_next’:
../kernel/bpf/task_iter.c:35:4: warning: value computed is not used [-Wunused-value]

   35 |    *tid++;
      |    ^~~~~~
#3 lore raw
Quoted 36 lines · Alexei Starovoitov
Quoted 20 lines · Andrii Nakryiko

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.

Cc Yonghong Song <[email protected]>
Fixes eaaacd23910f ("bpf: Add task and task/file iterator targets")
---
 kernel/bpf/task_iter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/bpf/task_iter.c +0 -0
index a9b7264dda08..e1836def6738 100644

kernel/bpf/task_iter.c static struct task_struct *task_seq_get_next(struct pid_namespace *ns, · line 35

Quoted 10 lines
kernel/bpf/task_iter.c +4 -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++;

../kernel/bpf/task_iter.c: In function ‘task_seq_get_next’:
../kernel/bpf/task_iter.c:35:4: warning: value computed is not used [-Wunused-value]

Quoted 2 lines
   35 |    *tid++;
      |    ^~~~~~

welp... thanks, fixing to prefix form