bpf: task_group_seq_get_next: misc cleanups
Subsystems (2)
v2 cover bpf: task_group_seq_get_next: misc cleanups
Yonghong,
I am resending 1-5 of 6 as you suggested with your acks included.
The next (final) patch will change this code to use __next_thread when
https://lore.kernel.org/all/[email protected]/
is merged.
Oleg.
PATCH v2 1/5 bpf: task_group_seq_get_next: cleanup the usage of next_thread()
1. find_pid_ns() + get_pid_task() under rcu_read_lock() guarantees that we can safely iterate the task->thread_group list. Even if this task exits right after get_pid_task() (or goto retry) and pid_alive() returns 0.
Kill the unnecessary pid_alive() check.
2. next_thread() simply can't return NULL, kill the bogus "if (!next_task)" check.
--- kernel/bpf/task_iter.c | 7 ------- 1 file changed, 7 deletions(-)
kernel/bpf/task_iter.c +0 -7
@@ -75,15 +75,8 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_commreturn NULL ; retry: -if (!pid_alive (task)) { -put_task_struct (task); -return NULL ; - } - next_task =next_thread (task);put_task_struct (task); -if (!next_task) -return NULL ; saved_tid = *tid; *tid =__task_pid_nr_ns (next_task,PIDTYPE_PID , common->ns);
Signature · 1 line
2.25.1.362.g51ebf55
PATCH v2 2/5 bpf: task_group_seq_get_next: cleanup the usage of get/put_task_struct
In reply to Oleg Nesterovget_pid_task() makes no sense, the code does put_task_struct() soon after. Use find_task_by_pid_ns() instead of find_pid_ns + get_pid_task and kill put_task_struct(), this allows to do get_task_struct() only once before return.
While at it, kill the unnecessary "if (!pid)" check in the "if (!*tid)" block, this matches the next usage of find_pid_ns() + get_pid_task() in this function.
--- kernel/bpf/task_iter.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
kernel/bpf/task_iter.c +2 -10
@@ -42,9 +42,6 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_commif (!*tid) {/* The first time, the iterator calls this function. */ pid =find_pid_ns (common->pid, common->ns); -if (!pid) -return NULL ; - task =get_pid_task (pid,PIDTYPE_TGID );if (!task)return NULL ;
@@ -66,17 +63,12 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_commreturn task; } - pid =find_pid_ns (common->pid_visiting, common->ns); -if (!pid) -return NULL ; - - task =get_pid_task (pid,PIDTYPE_PID ); + task =find_task_by_pid_ns (common->pid_visiting, common->ns);if (!task)return NULL ; retry: next_task =next_thread (task); -put_task_struct (task); saved_tid = *tid; *tid =__task_pid_nr_ns (next_task,PIDTYPE_PID , common->ns);
@@ -88,7 +80,6 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_commreturn NULL ; } -get_task_struct (next_task); common->pid_visiting = *tid;if (skip_if_dup_files && task->files == task->group_leader->files) {
@@ -96,6 +87,7 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm goto retry; } +get_task_struct (next_task);return next_task; }
Signature · 1 line
2.25.1.362.g51ebf55
PATCH v2 3/5 bpf: task_group_seq_get_next: fix the skip_if_dup_files check
In reply to Oleg NesterovUnless I am notally confused it is wrong. We are going to return or skip next_task so we need to check next_task-files, not task->files.
--- kernel/bpf/task_iter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/task_iter.c +1 -1
@@ -82,7 +82,7 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm common->pid_visiting = *tid; -if (skip_if_dup_files && task->files == task->group_leader->files) { +if (skip_if_dup_files && next_task->files == next_task->group_leader->files) { task = next_task; goto retry; }
Signature · 1 line
2.25.1.362.g51ebf55
PATCH v2 4/5 bpf: task_group_seq_get_next: kill next_task
In reply to Oleg NesterovIt only adds the unnecessary confusion and compicates the "retry" code.
--- kernel/bpf/task_iter.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
kernel/bpf/task_iter.c +6 -8
@@ -35,7 +35,7 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm u32 *tid, bool skip_if_dup_files) { -struct task_struct *task, *next_task; +struct task_struct *task;struct pid *pid;u32 saved_tid;
@@ -68,10 +68,10 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_commreturn NULL ; retry: - next_task =next_thread (task); + task =next_thread (task); saved_tid = *tid; - *tid =__task_pid_nr_ns (next_task,PIDTYPE_PID , common->ns); + *tid =__task_pid_nr_ns (task,PIDTYPE_PID , common->ns);if (!*tid || *tid == common->pid) {/* Run out of tasks of a process. The tasks of a * thread_group are linked as circular linked list.
@@ -82,13 +82,11 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm common->pid_visiting = *tid; -if (skip_if_dup_files && next_task->files == next_task->group_leader->files) { - task = next_task; +if (skip_if_dup_files && task->files == task->group_leader->files) goto retry; - } -get_task_struct (next_task); -return next_task; +get_task_struct (task); +return task; } static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
Signature · 1 line
2.25.1.362.g51ebf55
PATCH v2 5/5 bpf: task_group_seq_get_next: simplify the "next tid" logic
In reply to Oleg NesterovKill saved_tid. It looks ugly to update *tid and then restore the previous value if __task_pid_nr_ns() returns 0. Change this code to update *tid and common->pid_visiting once before return.
--- kernel/bpf/task_iter.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
kernel/bpf/task_iter.c +4 -7
@@ -37,7 +37,7 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm {struct task_struct *task;struct pid *pid; -u32 saved_tid; +u32 next_tid;if (!*tid) {/* The first time, the iterator calls this function. */
@@ -70,21 +70,18 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm retry: task =next_thread (task); - saved_tid = *tid; - *tid =__task_pid_nr_ns (task,PIDTYPE_PID , common->ns); -if (!*tid || *tid == common->pid) { + 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. */ - *tid = saved_tid;return NULL ; } - common->pid_visiting = *tid; -if (skip_if_dup_files && task->files == task->group_leader->files) goto retry; + *tid = common->pid_visiting = next_tid;get_task_struct (task);return task; }
Signature · 1 line
2.25.1.362.g51ebf55
sorry for noise, forgot to mention...
this (hopefully simple) series was only compile-tested.
Quoted 12 lines · Oleg Nesterov
Yonghong,
I am resending 1-5 of 6 as you suggested with your acks included.
The next (final) patch will change this code to use
__next_threadwhenhttps://lore.kernel.org/all/[email protected]/is merged.
Oleg.
Quoted 16 lines · Oleg Nesterov
sorry for noise, forgot to mention...
this (hopefully simple) series was only compile-tested.
Quoted 10 lines · Oleg Nesterov
Yonghong,
I am resending 1-5 of 6 as you suggested with your acks included.
The next (final) patch will change this code to use
__next_threadwhenhttps://lore.kernel.org/all/[email protected]/is merged.
The patch set looks fine.
What is the next step?
Should we merge it now in bpf-next and then during the next merge window you'll follow up with __next_thread clean up?
Quoted 5 lines · Alexei Starovoitov
Quoted 3 lines · Oleg Nesterov
Quoted 3 lines
The next (final) patch will change this code to use
__next_threadwhen
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
https://lore.kernel.org/all/[email protected]/is merged.
The patch set looks fine.
What is the next step?
Should we merge it now in bpf-next and then during the next merge window you'll follow up with__next_threadclean up?
Thanks, works for me.
Oleg.
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Tue, 5 Sep 2023 17:46:12 +0200 you wrote:
Yonghong,
I am resending 1-5 of 6 as you suggested with your acks included.
The next (final) patch will change this code to use
__next_threadwhenhttps://lore.kernel.org/all/[email protected]/[...]
Here is the summary with links:
- [bpf-next,v2,1/5] bpf: task_group_seq_get_next: cleanup the usage of next_thread()
https://git.kernel.org/bpf/bpf-next/c/f9aedd66c46b - [bpf-next,v2,2/5] bpf: task_group_seq_get_next: cleanup the usage of get/put_task_struct https://git.kernel.org/bpf/bpf-next/c/ad98e2e5f84f - [bpf-next,v2,3/5] bpf: task_group_seq_get_next: fix the skip_if_dup_files check https://git.kernel.org/bpf/bpf-next/c/c12e785e8648 - [bpf-next,v2,4/5] bpf: task_group_seq_get_next: kill next_task https://git.kernel.org/bpf/bpf-next/c/c40a3b44f7c5 - [bpf-next,v2,5/5] bpf: task_group_seq_get_next: simplify the "next tid" logic https://git.kernel.org/bpf/bpf-next/c/1e48f069c90f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html