diff mbox series

[3/3,v4,bpf-next] bpf: optimize task iteration

Message ID 20201218185032.2464558-4-jonathan.lemon@gmail.com
State New
Headers show
Series bpf: increment and use correct thread iterator | expand

Commit Message

Jonathan Lemon Dec. 18, 2020, 6:50 p.m. UTC
From: Jonathan Lemon <bsd@fb.com>

Only obtain the task reference count at the end of the RCU section
instead of repeatedly obtaining/releasing it when iterating though
a thread group.

Jump to the correct branch when it is known that the task is NULL.

Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
---
 kernel/bpf/task_iter.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Andrii Nakryiko Dec. 18, 2020, 9:03 p.m. UTC | #1
On Fri, Dec 18, 2020 at 12:47 PM Jonathan Lemon
<jonathan.lemon@gmail.com> wrote:
>
> From: Jonathan Lemon <bsd@fb.com>
>
> Only obtain the task reference count at the end of the RCU section
> instead of repeatedly obtaining/releasing it when iterating though
> a thread group.
>
> Jump to the correct branch when it is known that the task is NULL.
>
> Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  kernel/bpf/task_iter.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
> index dc4007f1843b..598a8d7da5bf 100644
> --- a/kernel/bpf/task_iter.c
> +++ b/kernel/bpf/task_iter.c
> @@ -33,7 +33,7 @@ static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
>         pid = find_ge_pid(*tid, ns);
>         if (pid) {
>                 *tid = pid_nr_ns(pid, ns);
> -               task = get_pid_task(pid, PIDTYPE_PID);
> +               task = pid_task(pid, PIDTYPE_PID);
>                 if (!task) {
>                         ++*tid;
>                         goto retry;
> @@ -44,6 +44,7 @@ static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
>                         ++*tid;
>                         goto retry;
>                 }
> +               get_task_struct(task);
>         }
>         rcu_read_unlock();
>
> @@ -148,12 +149,12 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
>          * it held a reference to the task/files_struct/file.
>          * Otherwise, it does not hold any reference.
>          */
> -again:
>         if (info->task) {
>                 curr_task = info->task;
>                 curr_files = info->files;
>                 curr_fd = info->fd;
>         } else {
> +again:
>                 curr_task = task_seq_get_next(ns, &curr_tid, true);
>                 if (!curr_task) {
>                         info->task = NULL;
> --
> 2.24.1
>
diff mbox series

Patch

diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index dc4007f1843b..598a8d7da5bf 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -33,7 +33,7 @@  static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
 	pid = find_ge_pid(*tid, ns);
 	if (pid) {
 		*tid = pid_nr_ns(pid, ns);
-		task = get_pid_task(pid, PIDTYPE_PID);
+		task = pid_task(pid, PIDTYPE_PID);
 		if (!task) {
 			++*tid;
 			goto retry;
@@ -44,6 +44,7 @@  static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
 			++*tid;
 			goto retry;
 		}
+		get_task_struct(task);
 	}
 	rcu_read_unlock();
 
@@ -148,12 +149,12 @@  task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
 	 * it held a reference to the task/files_struct/file.
 	 * Otherwise, it does not hold any reference.
 	 */
-again:
 	if (info->task) {
 		curr_task = info->task;
 		curr_files = info->files;
 		curr_fd = info->fd;
 	} else {
+again:
 		curr_task = task_seq_get_next(ns, &curr_tid, true);
 		if (!curr_task) {
 			info->task = NULL;