Skip to contentSkip to conversation

Parameterize task iterators.

PATCH Superseded Kui-Feng Lee 3 participants 19 messages Aug 29, 2022 – Aug 31, 2022

Subsystems (6)
Kselftest BPF BPF selftests BPF core +2 more subsystems

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover Parameterize task iterators.

Allow creating an iterator that loops through resources of one task/thread.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested in only the resources of a specific task or process. Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

Major Changes:

- Add new parameters in bpf_iter_link_info to indicate to go through all tasks or to go through a specific task.

- Change the implementations of BPF iterators of vma, files, and tasks to allow going through only the resources of a specific task.

- Provide the arguments of parameterized task iterators in bpf_link_info.

Differences from v7:

- Travel the tasks of a process through task_group linked list instead of traveling through the whole namespace.

Differences from v6:

- Add part 5 to make bpftool show the value of parameters.

- Change of wording of show_fdinfo() to show pid or tid instead of always pid.

- Simplify error handling and naming of test cases.

Differences from v5:

- Use user-space tid/pid terminologies in bpf_iter_link_info and bpf_link_info.

- Fix reference count

- Merge all variants to one 'u32 pid' in internal structs. (bpf_iter_aux_info and bpf_iter_seq_task_common)

- Compare the result of get_uprobe_offset() with the implementation with the vma iterators.

- Implement show_fdinfo.

Differences from v4:

- Remove 'type' from bpf_iter_link_info and bpf_link_info.

v6: https://lore.kernel.org/bpf/[email protected]/
v5: https://lore.kernel.org/bpf/[email protected]/
v4: https://lore.kernel.org/bpf/[email protected]/
v3: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/

Kui-Feng Lee (5):
bpf: Parameterize task iterators.
bpf: Handle bpf_link_info for the parameterized task BPF iterators.
bpf: Handle show_fdinfo for the parameterized task BPF iterators selftests/bpf: Test parameterized task BPF iterators.
bpftool: Show parameters of BPF task iterators.

 include/linux/bpf.h                           |  25 ++
 include/uapi/linux/bpf.h                      |  10 +
 kernel/bpf/task_iter.c                        | 220 ++++++++++++--
 tools/bpf/bpftool/link.c                      |  19 ++
 tools/include/uapi/linux/bpf.h                |  10 +
 .../selftests/bpf/prog_tests/bpf_iter.c       | 282 ++++++++++++++++--
 .../selftests/bpf/prog_tests/btf_dump.c       |   2 +-
 .../selftests/bpf/progs/bpf_iter_task.c       |   9 +
 .../selftests/bpf/progs/bpf_iter_task_file.c  |   9 +-
 .../selftests/bpf/progs/bpf_iter_task_vma.c   |   7 +-
 .../selftests/bpf/progs/bpf_iter_vma_offset.c |  37 +++
 11 files changed, 584 insertions(+), 46 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v8 1/5 bpf: Parameterize task iterators.

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested in only the resources of a specific task or process. Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
 include/linux/bpf.h            |  25 +++++
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/task_iter.c         | 184 +++++++++++++++++++++++++++++----
 tools/include/uapi/linux/bpf.h |   6 ++
 4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +25 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user *pathname, int flags);
 	extern int bpf_iter_ ## target(args);			\
 	int __init bpf_iter_ ## target(args) { return 0; }
 
+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *	Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *	Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *	Iterate over resources of every task of a process / task group.
+ */
+enum bpf_iter_task_type {
+	BPF_TASK_ITER_ALL = 0,
+	BPF_TASK_ITER_TID,
+	BPF_TASK_ITER_TGID,
+};
+
 struct bpf_iter_aux_info {
 	/* for map_elem iter */
 	struct bpf_map *map;
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {
 		struct cgroup *start; /* starting cgroup */
 		enum bpf_cgroup_iter_order order;
 	} cgroup;
+	struct {
+		enum bpf_iter_task_type	type;
+		u32 pid;
+	} task;
 };
 
 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
 		__u32	cgroup_fd;
 		__u64	cgroup_id;
 	} cgroup;
+	/* Parameters of task iterators. */
+	struct {
+		__u32	tid;
+		__u32	pid;
+		__u32	pid_fd;
+	} task;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
kernel/bpf/task_iter.c +162 -22

302 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +6 -0
index f4ba82a1eace..40935278eede 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
 		__u32	cgroup_fd;
 		__u64	cgroup_id;
 	} cgroup;
+	/* Parameters of task iterators. */
+	struct {
+		__u32	tid;
+		__u32	pid;
+		__u32	pid_fd;
+	} task;
 };
 
 /* BPF syscall commands, see bpf(2) man-page for more details. */
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH bpf-next v8 1/5] bpf: Parameterize task iterators.

Quoted 96 lines · Kui-Feng Lee

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested in only the resources of a specific task or process. Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
  include/linux/bpf.h            |  25 +++++
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/task_iter.c         | 184 +++++++++++++++++++++++++++++----
  tools/include/uapi/linux/bpf.h |   6 ++
  4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +25 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user *pathname, int flags);
  	extern int bpf_iter_ ## target(args);			\
  	int __init bpf_iter_ ## target(args) { return 0; }
 
+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *	Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *	Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *	Iterate over resources of every task of a process / task group.
+ */
+enum bpf_iter_task_type {
+	BPF_TASK_ITER_ALL = 0,
+	BPF_TASK_ITER_TID,
+	BPF_TASK_ITER_TGID,
+};
+
  struct bpf_iter_aux_info {
  	/* for map_elem iter */
  	struct bpf_map *map;
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {
  		struct cgroup *start; /* starting cgroup */
  		enum bpf_cgroup_iter_order order;
  	} cgroup;
+	struct {
+		enum bpf_iter_task_type	type;
+		u32 pid;
+	} task;
  };
 
  typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
  		__u32	cgroup_fd;
  		__u64	cgroup_id;
  	} cgroup;
+	/* Parameters of task iterators. */
+	struct {
+		__u32	tid;
+		__u32	pid;
+		__u32	pid_fd;
+	} task;
  };
 
  /* BPF syscall commands, see bpf(2) man-page for more details. */
kernel/bpf/task_iter.c +3 -0
index 8c921799def4..0bc7277d1ee1 100644
@@ -12,6 +12,9 @@
 
  struct bpf_iter_seq_task_common {
  	struct pid_namespace *ns;
+	enum bpf_iter_task_type	type;
+	u32 pid;
+	u32 pid_visiting;
  };
 
  struct bpf_iter_seq_task_info {
@@ -22,18 +25,107 @@ struct bpf_iter_seq_task_info {
  	u32 tid;
  };
 

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 36

Quoted 10 lines
kernel/bpf/task_iter.c +9 -1
-static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
+static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
+						   u32 *tid,
+						   bool skip_if_dup_files)
+{
+	struct task_struct *task, *next_task;
+	struct pid *pid;
+	u32 saved_tid;
+
+	if (!*tid) {

Add a comment in the above to say that this is for the *very first* visit of tasks in the process.

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 39

Quoted 3 lines
kernel/bpf/task_iter.c +3 -0
+		pid = find_pid_ns(common->pid, common->ns);
+		if (pid)
+			task = get_pid_task(pid, PIDTYPE_TGID);

'task' is not initialized, so it is possible task could hold a garbase value here if !pid, right?

Also if indeed task is NULL, here, should we return NULL here first?

Quoted 11 lines
kernel/bpf/task_iter.c +11 -0
+
+		*tid = common->pid;
+		common->pid_visiting = common->pid;
+
+		return task;
+	}
+
+	/* The callers increase *tid by 1 once they want next task.
+	 * However, next_thread() doesn't return tasks in incremental
+	 * order of pids. We can not find next task by just finding a
+	 * task whose pid is greater or equal to *tid.  pid_visiting

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 60

Quoted 10 lines
kernel/bpf/task_iter.c +10 -0
+	 * remembers the pid value of the task returned last time. By
+	 * comparing pid_visiting and *tid, we known if the caller
+	 * wants the next task.
+	 */
+	if (*tid == common->pid_visiting) {
+		pid = find_pid_ns(common->pid_visiting, common->ns);
+		task = get_pid_task(pid, PIDTYPE_PID);
+
+		return task;
+	}

Do not understand the above code. Why we need it? Looks like the code below trying to get the *next_task* and will return NULL if wrap around happens(the tid again equals tgid), right?

Quoted 17 lines
kernel/bpf/task_iter.c +17 -0
+
+retry:
+	pid = find_pid_ns(common->pid_visiting, common->ns);
+	if (!pid)
+		return NULL;
+
+	task = get_pid_task(pid, PIDTYPE_PID);
+	if (!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);

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 87

Quoted 10 lines
kernel/bpf/task_iter.c +10 -0
+	if (*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;
+	}
+
+	get_task_struct(next_task);
+	common->pid_visiting = *tid;

We could do quite some redundant works here if the following condition is true. Basically, we get next_task and get a tid and release it, but in the next iteration, from tid, we try to get the task again.

Quoted 68 lines
kernel/bpf/task_iter.c +34 -4
+
+	if (skip_if_dup_files && task->files == task->group_leader->files)
+		goto retry;
+
+	return next_task;
+}
+
+static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
  					     u32 *tid,
  					     bool skip_if_dup_files)
  {
  	struct task_struct *task = NULL;
  	struct pid *pid;
 
+	if (common->type == BPF_TASK_ITER_TID) {
+		if (*tid && *tid != common->pid)
+			return NULL;
+		rcu_read_lock();
+		pid = find_pid_ns(common->pid, common->ns);
+		if (pid) {
+			task = get_pid_task(pid, PIDTYPE_TGID);
+			*tid = common->pid;
+		}
+		rcu_read_unlock();
+
+		return task;
+	}
+
+	if (common->type == BPF_TASK_ITER_TGID) {
+		rcu_read_lock();
+		task = task_group_seq_get_next(common, tid, skip_if_dup_files);
+		rcu_read_unlock();
+
+		return task;
+	}
+
  	rcu_read_lock();
  retry:
-	pid = find_ge_pid(*tid, ns);
+	pid = find_ge_pid(*tid, common->ns);
  	if (pid) {
-		*tid = pid_nr_ns(pid, ns);
+		*tid = pid_nr_ns(pid, common->ns);
  		task = get_pid_task(pid, PIDTYPE_PID);
  		if (!task) {
  			++*tid;
@@ -56,7 +148,7 @@ static void *task_seq_start(struct seq_file *seq, loff_t *pos)
  	struct bpf_iter_seq_task_info *info = seq->private;
  	struct task_struct *task;
 
-	task = task_seq_get_next(info->common.ns, &info->tid, false);
+	task = task_seq_get_next(&info->common, &info->tid, false);
  	if (!task)
  		return NULL;
 
@@ -73,7 +165,7 @@ static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  	++*pos;
  	++info->tid;
  	put_task_struct((struct task_struct *)v);
-	task = task_seq_get_next(info->common.ns, &info->tid, false);
+	task = task_seq_get_next(&info->common, &info->tid, false);
  	if (!task)
  		return NULL;
 
@@ -117,6 +209,45 @@ static void task_seq_stop(struct seq_file *seq, void *v)
  		put_task_struct((struct task_struct *)v);
  }
 

[...]

#3 lore raw

Re: [PATCH bpf-next v8 1/5] bpf: Parameterize task iterators.

Quoted 124 lines · Yonghong Song
Quoted 107 lines · Kui-Feng Lee

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested
in only the resources of a specific task or process.  Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
  include/linux/bpf.h            |  25 +++++
  include/uapi/linux/bpf.h       |   6 ++
  kernel/bpf/task_iter.c         | 184
+++++++++++++++++++++++++++++----
  tools/include/uapi/linux/bpf.h |   6 ++

  4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +0 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user
 *pathname, int flags);
         extern int bpf_iter_ ## target(args);                   \
         int __init bpf_iter_ ## target(args) { return 0; }


+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *     Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *     Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *     Iterate over resources of every task of a process / task group.
+ */

include/linux/bpf.h +6 -0
+enum bpf_iter_task_type {
+       BPF_TASK_ITER_ALL = 0,
+       BPF_TASK_ITER_TID,
+       BPF_TASK_ITER_TGID,
+};
+

  struct bpf_iter_aux_info {
        /* for map_elem iter */
        struct bpf_map *map;

include/linux/bpf.h +0 -0
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {

                struct cgroup *start; /* starting cgroup */ enum bpf_cgroup_iter_order order;
        } cgroup;
+       struct {
+               enum bpf_iter_task_type type;
+               u32 pid;
+       } task;
  };

  typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,

include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
                 __u32   cgroup_fd;
                 __u64   cgroup_id;
         } cgroup;
+       /* Parameters of task iterators. */
+       struct {
+               __u32   tid;
+               __u32   pid;
+               __u32   pid_fd;
+       } task;
   };


  /* BPF syscall commands, see bpf(2) man-page for more details. */

kernel/bpf/task_iter.c +0 -0
index 8c921799def4..0bc7277d1ee1 100644
@@ -12,6 +12,9 @@


  struct bpf_iter_seq_task_common {
        struct pid_namespace *ns;
+       enum bpf_iter_task_type type;
+       u32 pid;
+       u32 pid_visiting;
  };

  struct bpf_iter_seq_task_info {

kernel/bpf/task_iter.c +0 -0
@@ -22,18 +25,107 @@ struct bpf_iter_seq_task_info {
         u32 tid;
   };


-static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
+static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
+                                                  u32 *tid, +                                                  bool skip_if_dup_files)

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 36

Quoted 6 lines
kernel/bpf/task_iter.c +6 -0
+{
+       struct task_struct *task, *next_task;
+       struct pid *pid;
+       u32 saved_tid;
+
+       if (!*tid) {

Add a comment in the above to say that this is for the *very first* visit of tasks in the process.

Quoted 3 lines

+               pid = find_pid_ns(common->pid, common->ns); +               if (pid)
+                       task = get_pid_task(pid, PIDTYPE_TGID);

Quoted 5 lines

'task' is not initialized, so it is possible task could hold a garbase value here if !pid, right?

Also if indeed task is NULL, here, should we return NULL here first?

yes, it should return earlier.

Quoted 23 lines
Quoted 23 lines
kernel/bpf/task_iter.c +9 -0
+
+               *tid = common->pid;
+               common->pid_visiting = common->pid;
+
+               return task;
+       }
+
+       /* The callers increase *tid by 1 once they want next task.
+        * However, next_thread() doesn't return tasks in

incremental
+        * order of pids. We can not find next task by just finding a
+        * task whose pid is greater or equal to *tid. pid_visiting
+        * remembers the pid value of the task returned last time. By
+        * comparing pid_visiting and *tid, we known if the caller +        * wants the next task.
+        */
+       if (*tid == common->pid_visiting) {
+               pid = find_pid_ns(common->pid_visiting, common-

Quoted 1 line

ns);

Quoted 8 lines
Quoted 4 lines

+               task = get_pid_task(pid, PIDTYPE_PID); +
+               return task;
+       }

Do not understand the above code. Why we need it? Looks like the code below trying to get the *next_task* and will return NULL if wrap around happens(the tid again equals tgid), right?

The above code is to handle the case that the caller want to visit the same task again. For example, task_file_seq_get_next() will call this function several time to return the same task, and move to next task by increasing info->tid. The above code checks the value of *tid to return the same task if the value doesn't change.

Quoted 30 lines
Quoted 19 lines
kernel/bpf/task_iter.c +17 -0
+
+retry:
+       pid = find_pid_ns(common->pid_visiting, common->ns);
+       if (!pid)
+               return NULL;
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!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-
Quoted 1 line

ns);

Quoted 10 lines

+       if (*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;
+       }
+
+       get_task_struct(next_task);
+       common->pid_visiting = *tid;

Quoted 4 lines

We could do quite some redundant works here if the following condition is true. Basically, we get next_task and get a tid and release it, but in the next iteration, from tid, we try to get the task again.

Yes, I will move 'retry' and move next_task to task to avoid the redundant work.

Quoted 79 lines
Quoted 78 lines

+
+       if (skip_if_dup_files && task->files == task->group_leader-

Quoted 1 line

files)

+               goto retry;

kernel/bpf/task_iter.c +5 -0
+
+       return next_task;
+}
+
+static struct task_struct *task_seq_get_next(struct

bpf_iter_seq_task_common *common,
                                             u32 *tid, bool skip_if_dup_files)
  {
        struct task_struct *task = NULL;
        struct pid *pid;

+       if (common->type == BPF_TASK_ITER_TID) {
+               if (*tid && *tid != common->pid)
+                       return NULL;
+               rcu_read_lock();
+               pid = find_pid_ns(common->pid, common->ns); +               if (pid) {
+                       task = get_pid_task(pid, PIDTYPE_TGID); +                       *tid = common->pid;
+               }
+               rcu_read_unlock();

kernel/bpf/task_iter.c +7 -0
+
+               return task;
+       }
+
+       if (common->type == BPF_TASK_ITER_TGID) {
+               rcu_read_lock();
+               task = task_group_seq_get_next(common, tid,

skip_if_dup_files);
+               rcu_read_unlock();

kernel/bpf/task_iter.c +4 -0
+
+               return task;
+       }
+

        rcu_read_lock();
  retry:
-       pid = find_ge_pid(*tid, ns);
+       pid = find_ge_pid(*tid, common->ns); if (pid) {
-               *tid = pid_nr_ns(pid, ns);
+               *tid = pid_nr_ns(pid, common->ns); task = get_pid_task(pid, PIDTYPE_PID); if (!task) {
                        ++*tid;

kernel/bpf/task_iter.c +0 -0
@@ -56,7 +148,7 @@ static void *task_seq_start(struct seq_file
 *seq, loff_t *pos)
         struct bpf_iter_seq_task_info *info = seq->private;
         struct task_struct *task;


-       task = task_seq_get_next(info->common.ns, &info->tid, false);
+       task = task_seq_get_next(&info->common, &info->tid, false); if (!task)
                return NULL;

kernel/bpf/task_iter.c +1 -1
@@ -73,7 +165,7 @@ static void *task_seq_next(struct seq_file *seq,
 void *v, loff_t *pos)
         ++*pos;
         ++info->tid;
         put_task_struct((struct task_struct *)v);
-       task = task_seq_get_next(info->common.ns, &info->tid,
 false);
+       task = task_seq_get_next(&info->common, &info->tid, false);
         if (!task)

                return NULL;

kernel/bpf/task_iter.c +0 -0
@@ -117,6 +209,45 @@ static void task_seq_stop(struct seq_file
 *seq, void *v)
                 put_task_struct((struct task_struct *)v);
   }

[...]

#4 lore raw

Re: [PATCH bpf-next v8 1/5] bpf: Parameterize task iterators.

Quoted 165 lines · Kui-Feng Lee
Quoted 124 lines · Yonghong Song
Quoted 103 lines · Kui-Feng Lee

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested
in only the resources of a specific task or process.  Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
   include/linux/bpf.h            |  25 +++++
   include/uapi/linux/bpf.h       |   6 ++
   kernel/bpf/task_iter.c         | 184
+++++++++++++++++++++++++++++----
   tools/include/uapi/linux/bpf.h |   6 ++
   4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +14 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user
 *pathname, int flags);
         extern int bpf_iter_ ## target(args);                   \
         int __init bpf_iter_ ## target(args) { return 0; }
 
+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *     Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *     Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *     Iterate over resources of every task of a process / task

group.
+ */

include/linux/bpf.h +10 -0
+enum bpf_iter_task_type {
+       BPF_TASK_ITER_ALL = 0,
+       BPF_TASK_ITER_TID,
+       BPF_TASK_ITER_TGID,
+};
+
   struct bpf_iter_aux_info {
         /* for map_elem iter */
         struct bpf_map *map;
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {
                 struct cgroup *start; /* starting cgroup */
                 enum bpf_cgroup_iter_order order;
         } cgroup;
+       struct {
+               enum bpf_iter_task_type type;
+               u32 pid;
+       } task;
   };
 
   typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
                 __u32   cgroup_fd;
                 __u64   cgroup_id;
         } cgroup;
+       /* Parameters of task iterators. */
+       struct {
+               __u32   tid;
+               __u32   pid;
+               __u32   pid_fd;
+       } task;
   };
 
   /* BPF syscall commands, see bpf(2) man-page for more details. */
kernel/bpf/task_iter.c +4 -1
index 8c921799def4..0bc7277d1ee1 100644
@@ -12,6 +12,9 @@
 
   struct bpf_iter_seq_task_common {
         struct pid_namespace *ns;
+       enum bpf_iter_task_type type;
+       u32 pid;
+       u32 pid_visiting;
   };
 
   struct bpf_iter_seq_task_info {
@@ -22,18 +25,107 @@ struct bpf_iter_seq_task_info {
         u32 tid;
   };
 
-static struct task_struct *task_seq_get_next(struct pid_namespace
 *ns,
+static struct task_struct *task_group_seq_get_next(struct

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 39

Quoted 10 lines
kernel/bpf/task_iter.c +8 -0
 bpf_iter_seq_task_common *common,
+                                                  u32 *tid,
+                                                  bool
 skip_if_dup_files)
+{
+       struct task_struct *task, *next_task;
+       struct pid *pid;
+       u32 saved_tid;
+
+       if (!*tid) {

Add a comment in the above to say that this is for the *very first* visit of tasks in the process.

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 39

Quoted 3 lines
kernel/bpf/task_iter.c +3 -0
+               pid = find_pid_ns(common->pid, common->ns);
+               if (pid)
+                       task = get_pid_task(pid, PIDTYPE_TGID);
Quoted 5 lines

'task' is not initialized, so it is possible task could hold a garbase value here if !pid, right?

Also if indeed task is NULL, here, should we return NULL here first?

yes, it should return earlier.

Quoted 23 lines
Quoted 23 lines
kernel/bpf/task_iter.c +9 -0
+
+               *tid = common->pid;
+               common->pid_visiting = common->pid;
+
+               return task;
+       }
+
+       /* The callers increase *tid by 1 once they want next task.
+        * However, next_thread() doesn't return tasks in

incremental
+        * order of pids. We can not find next task by just finding a
+        * task whose pid is greater or equal to *tid. pid_visiting
+        * remembers the pid value of the task returned last time. By
+        * comparing pid_visiting and *tid, we known if the caller +        * wants the next task.
+        */
+       if (*tid == common->pid_visiting) {
+               pid = find_pid_ns(common->pid_visiting, common-

Quoted 1 line

ns);

Quoted 8 lines
Quoted 4 lines

+               task = get_pid_task(pid, PIDTYPE_PID); +
+               return task;
+       }

Do not understand the above code. Why we need it? Looks like the code below trying to get the *next_task* and will return NULL if wrap around happens(the tid again equals tgid), right?

Quoted 5 lines

The above code is to handle the case that the caller want to visit the same task again. For example, task_file_seq_get_next() will call this function several time to return the same task, and move to next task by increasing info->tid. The above code checks the value of *tid to return the same task if the value doesn't change.

Could you explain when task_file_seq_get_next() will call this function several times? IIUC, from the following code,

+static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
u32 *tid,
bool skip_if_dup_files)
{
struct task_struct *task = NULL;
struct pid *pid;

+ if (common->type == BPF_TASK_ITER_TID) {
+ if (*tid && *tid != common->pid)
+ return NULL;
+ rcu_read_lock();
+ pid = find_pid_ns(common->pid, common->ns);
+ if (pid) {
+ task = get_pid_task(pid, PIDTYPE_TGID);
+ *tid = common->pid;
+ }
+ rcu_read_unlock();
+
+ return task;
+ }
+
+ if (common->type == BPF_TASK_ITER_TGID) {
+ rcu_read_lock();
+ task = task_group_seq_get_next(common, tid, skip_if_dup_files);
+ rcu_read_unlock();
+
+ return task;
+ }
+

task_group_seq_get_next() is only called once per task_seq_get_next() for BPF_TASK_ITER_TGID.
Maybe I missed something?

Quoted 119 lines
Quoted 31 lines
Quoted 20 lines
kernel/bpf/task_iter.c +17 -0
+
+retry:
+       pid = find_pid_ns(common->pid_visiting, common->ns);
+       if (!pid)
+               return NULL;
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!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-
Quoted 1 line

ns);

Quoted 10 lines

+       if (*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;
+       }
+
+       get_task_struct(next_task);
+       common->pid_visiting = *tid;

Quoted 4 lines

We could do quite some redundant works here if the following condition is true. Basically, we get next_task and get a tid and release it, but in the next iteration, from tid, we try to get the task again.

Yes, I will move 'retry' and move next_task to task to avoid the redundant work.

Quoted 79 lines
Quoted 78 lines

+
+       if (skip_if_dup_files && task->files == task->group_leader-

Quoted 1 line

files)

+               goto retry;

kernel/bpf/task_iter.c +5 -0
+
+       return next_task;
+}
+
+static struct task_struct *task_seq_get_next(struct

bpf_iter_seq_task_common *common,
                                             u32 *tid, bool
skip_if_dup_files)
  {
        struct task_struct *task = NULL; struct pid *pid;

+       if (common->type == BPF_TASK_ITER_TID) {
+               if (*tid && *tid != common->pid)
+                       return NULL;
+               rcu_read_lock();
+               pid = find_pid_ns(common->pid, common->ns); +               if (pid) {
+                       task = get_pid_task(pid, PIDTYPE_TGID); +                       *tid = common->pid;
+               }
+               rcu_read_unlock();

kernel/bpf/task_iter.c +7 -0
+
+               return task;
+       }
+
+       if (common->type == BPF_TASK_ITER_TGID) {
+               rcu_read_lock();
+               task = task_group_seq_get_next(common, tid,

skip_if_dup_files);
+               rcu_read_unlock();

kernel/bpf/task_iter.c +7 -3
+
+               return task;
+       }
+
         rcu_read_lock();
   retry:
-       pid = find_ge_pid(*tid, ns);
+       pid = find_ge_pid(*tid, common->ns);
         if (pid) {
-               *tid = pid_nr_ns(pid, ns);
+               *tid = pid_nr_ns(pid, common->ns);
                 task = get_pid_task(pid, PIDTYPE_PID);
                 if (!task) {
                         ++*tid;
@@ -56,7 +148,7 @@ static void *task_seq_start(struct seq_file
 *seq, loff_t *pos)
         struct bpf_iter_seq_task_info *info = seq->private;
         struct task_struct *task;
 
-       task = task_seq_get_next(info->common.ns, &info->tid,
 false);
+       task = task_seq_get_next(&info->common, &info->tid, false);
         if (!task)

                return NULL;

kernel/bpf/task_iter.c +1 -1
@@ -73,7 +165,7 @@ static void *task_seq_next(struct seq_file *seq,
 void *v, loff_t *pos)
         ++*pos;
         ++info->tid;
         put_task_struct((struct task_struct *)v);
-       task = task_seq_get_next(info->common.ns, &info->tid,
 false);
+       task = task_seq_get_next(&info->common, &info->tid, false);
         if (!task)

                return NULL;

kernel/bpf/task_iter.c +0 -0
@@ -117,6 +209,45 @@ static void task_seq_stop(struct seq_file
 *seq, void *v)
                 put_task_struct((struct task_struct *)v);
   }
 

[...]

#5 lore raw

Re: [PATCH bpf-next v8 1/5] bpf: Parameterize task iterators.

Quoted 162 lines · Yonghong Song
Quoted 159 lines · Kui-Feng Lee
Quoted 118 lines · Yonghong Song
Quoted 97 lines · Kui-Feng Lee

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of files, vma, and tasks in the system, even though they were interested
in only the resources of a specific task or process.  Passing the additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
   include/linux/bpf.h            |  25 +++++
   include/uapi/linux/bpf.h       |   6 ++
   kernel/bpf/task_iter.c         | 184
+++++++++++++++++++++++++++++----
   tools/include/uapi/linux/bpf.h |   6 ++

   4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +14 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user
 *pathname, int flags);
          extern int bpf_iter_ ## target(args);                   \
          int __init bpf_iter_ ## target(args) { return 0; }
+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *     Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *     Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *     Iterate over resources of every task of a process / task

group.
+ */

include/linux/bpf.h +6 -0
+enum bpf_iter_task_type {
+       BPF_TASK_ITER_ALL = 0,
+       BPF_TASK_ITER_TID,
+       BPF_TASK_ITER_TGID,
+};
+

   struct bpf_iter_aux_info {
         /* for map_elem iter */
         struct bpf_map *map;

include/linux/bpf.h +0 -0
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {

                 struct cgroup *start; /* starting cgroup */ enum bpf_cgroup_iter_order order;
         } cgroup;
+       struct {
+               enum bpf_iter_task_type type;
+               u32 pid;
+       } task;
   };
   typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,

include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
                  __u32   cgroup_fd;
                  __u64   cgroup_id;
          } cgroup;
+       /* Parameters of task iterators. */
+       struct {
+               __u32   tid;
+               __u32   pid;
+               __u32   pid_fd;
+       } task;
    };
    /* BPF syscall commands, see bpf(2) man-page for more details. */
kernel/bpf/task_iter.c +4 -1
index 8c921799def4..0bc7277d1ee1 100644
@@ -12,6 +12,9 @@
    struct bpf_iter_seq_task_common {
          struct pid_namespace *ns;
+       enum bpf_iter_task_type type;
+       u32 pid;
+       u32 pid_visiting;
    };
    struct bpf_iter_seq_task_info {
@@ -22,18 +25,107 @@ struct bpf_iter_seq_task_info {
          u32 tid;
    };
-static struct task_struct *task_seq_get_next(struct pid_namespace
 *ns,
+static struct task_struct *task_group_seq_get_next(struct

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 38

Quoted 10 lines
kernel/bpf/task_iter.c +8 -0
 bpf_iter_seq_task_common *common,
+                                                  u32 *tid,
+                                                  bool
 skip_if_dup_files)
+{
+       struct task_struct *task, *next_task;
+       struct pid *pid;
+       u32 saved_tid;
+
+       if (!*tid) {

Add a comment in the above to say that this is for the *very first* visit of tasks in the process.

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 39

Quoted 3 lines
kernel/bpf/task_iter.c +3 -0
+               pid = find_pid_ns(common->pid, common->ns);
+               if (pid)
+                       task = get_pid_task(pid, PIDTYPE_TGID);
Quoted 5 lines

'task' is not initialized, so it is possible task could hold a garbase value here if !pid, right?

Also if indeed task is NULL, here, should we return NULL here first?

yes, it should return earlier.

Quoted 31 lines
Quoted 22 lines
kernel/bpf/task_iter.c +9 -0
+
+               *tid = common->pid;
+               common->pid_visiting = common->pid;
+
+               return task;
+       }
+
+       /* The callers increase *tid by 1 once they want next task.
+        * However, next_thread() doesn't return tasks in

incremental
+        * order of pids. We can not find next task by just finding a
+        * task whose pid is greater or equal to *tid. pid_visiting
+        * remembers the pid value of the task returned last time. By
+        * comparing pid_visiting and *tid, we known if the caller +        * wants the next task.
+        */
+       if (*tid == common->pid_visiting) {
+               pid = find_pid_ns(common->pid_visiting, common-

Quoted 5 lines
Quoted 1 line

ns);

+               task = get_pid_task(pid, PIDTYPE_PID); +
+               return task;
+       }

Do not understand the above code. Why we need it? Looks like the code below trying to get the *next_task* and will return NULL if wrap around happens(the tid again equals tgid), right?

Quoted 5 lines
Quoted 5 lines

The above code is to handle the case that the caller want to visit the same task again.  For example, task_file_seq_get_next() will call this function several time to return the same task, and move to next task by increasing info->tid.  The above code checks the value of *tid to return the same task if the value doesn't change.

Okay, I did a little more investigation. The above code is correct. But the comment does not clearly describe why. The real reason we need this is for subsequent iterations from task_seq_start(). Basically we have

   task_seq_next();
   // return to user space
   task_seq_start()
     task_group_seq_get_next()

In above task_group_seq_get_next() needs to retrieve the task which is the to-be-visited task during last task_seq_start/next/show/stop() run.

The comment can be as simple as below.

/* If the control returns to user space and comes back to
* the kernel again, *tid and common->pid_visiting should
* be the same for task_seq_start() to pick up the correct
* task.
*/

Quoted 41 lines

Could you explain when task_file_seq_get_next() will call this function several times? IIUC, from the following code,

+static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
                          u32 *tid,
                          bool skip_if_dup_files) {
     struct task_struct *task = NULL; struct pid *pid;

+    if (common->type == BPF_TASK_ITER_TID) { +        if (*tid && *tid != common->pid)
+            return NULL;
+        rcu_read_lock();
+        pid = find_pid_ns(common->pid, common->ns); +        if (pid) {
+            task = get_pid_task(pid, PIDTYPE_TGID); +            *tid = common->pid;
+        }
+        rcu_read_unlock();

kernel/bpf/task_iter.c +12 -0
+
+        return task;
+    }
+
+    if (common->type == BPF_TASK_ITER_TGID) {
+        rcu_read_lock();
+        task = task_group_seq_get_next(common, tid, skip_if_dup_files);
+        rcu_read_unlock();
+
+        return task;
+    }
+

task_group_seq_get_next() is only called once per task_seq_get_next() for BPF_TASK_ITER_TGID.
Maybe I missed something?

[...]

#6 lore raw

Re: [PATCH bpf-next v8 1/5] bpf: Parameterize task iterators.

In reply to Yonghong Song
Quoted 191 lines · Yonghong Song
Quoted 179 lines · Kui-Feng Lee
Quoted 134 lines · Yonghong Song
Quoted 116 lines · Kui-Feng Lee

Allow creating an iterator that loops through resources of one thread/process.

People could only create iterators to loop through all resources of
files, vma, and tasks in the system, even though they were interested
in only the resources of a specific task or process.  Passing the
additional parameters, people can now create an iterator to go through all resources or only the resources of a task.

---
   include/linux/bpf.h            |  25 +++++
   include/uapi/linux/bpf.h       |   6 ++
   kernel/bpf/task_iter.c         | 184
+++++++++++++++++++++++++++++----
   tools/include/uapi/linux/bpf.h |   6 ++

   4 files changed, 199 insertions(+), 22 deletions(-)
include/linux/bpf.h +0 -0
index 9c1674973e03..31ac2c1181f5 100644
@@ -1730,6 +1730,27 @@ int bpf_obj_get_user(const char __user
 *pathname, int flags);

         extern int bpf_iter_ ##
target(args);                   \
         int __init bpf_iter_ ## target(args) { return 0; }
+/*
+ * The task type of iterators.
+ *
+ * For BPF task iterators, they can be parameterized with various
+ * parameters to visit only some of tasks.
+ *
+ * BPF_TASK_ITER_ALL (default)
+ *     Iterate over resources of every task.
+ *
+ * BPF_TASK_ITER_TID
+ *     Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *     Iterate over resources of every task of a process / task
group.
+ */

include/linux/bpf.h +6 -0
+enum bpf_iter_task_type {
+       BPF_TASK_ITER_ALL = 0,
+       BPF_TASK_ITER_TID,
+       BPF_TASK_ITER_TGID,
+};
+

   struct bpf_iter_aux_info {
         /* for map_elem iter */
         struct bpf_map *map;

include/linux/bpf.h +0 -0
@@ -1739,6 +1760,10 @@ struct bpf_iter_aux_info {

                 struct cgroup *start; /* starting cgroup */ enum bpf_cgroup_iter_order order; } cgroup;
+       struct {
+               enum bpf_iter_task_type type;
+               u32 pid;
+       } task;
   };

   typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,

include/uapi/linux/bpf.h +6 -0
index 962960a98835..f212a19eda06 100644
@@ -110,6 +110,12 @@ union bpf_iter_link_info {
                  __u32   cgroup_fd;
                  __u64   cgroup_id;
          } cgroup;
+       /* Parameters of task iterators. */
+       struct {
+               __u32   tid;
+               __u32   pid;
+               __u32   pid_fd;
+       } task;
    };


   /* BPF syscall commands, see bpf(2) man-page for more details. */

kernel/bpf/task_iter.c +0 -0
index 8c921799def4..0bc7277d1ee1 100644
@@ -12,6 +12,9 @@


   struct bpf_iter_seq_task_common {
         struct pid_namespace *ns;
+       enum bpf_iter_task_type type;
+       u32 pid;
+       u32 pid_visiting;
   };

   struct bpf_iter_seq_task_info {

kernel/bpf/task_iter.c +0 -0
@@ -22,18 +25,107 @@ struct bpf_iter_seq_task_info {
          u32 tid;
    };


-static struct task_struct *task_seq_get_next(struct pid_namespace
*ns,
+static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
+                                                  u32 *tid, +                                                  bool skip_if_dup_files)

kernel/bpf/task_iter.c struct bpf_iter_seq_task_info { · line 36

Quoted 6 lines
kernel/bpf/task_iter.c +6 -0
+{
+       struct task_struct *task, *next_task;
+       struct pid *pid;
+       u32 saved_tid;
+
+       if (!*tid) {

Add a comment in the above to say that this is for the *very first*
visit of tasks in the process.

Quoted 3 lines

+               pid = find_pid_ns(common->pid, common->ns); +               if (pid)
+                       task = get_pid_task(pid, PIDTYPE_TGID);

Quoted 5 lines

'task' is not initialized, so it is possible task could hold a garbase value here if !pid, right?

Also if indeed task is NULL, here, should we return NULL here first?

yes, it should return earlier.

Quoted 27 lines
Quoted 27 lines
kernel/bpf/task_iter.c +8 -0
+
+               *tid = common->pid;
+               common->pid_visiting = common->pid;
+
+               return task;
+       }
+
+       /* The callers increase *tid by 1 once they want next

task.
+        * However, next_thread() doesn't return tasks in incremental
+        * order of pids. We can not find next task by just finding
a
+        * task whose pid is greater or equal to *tid. pid_visiting
+        * remembers the pid value of the task returned last time.
By
+        * comparing pid_visiting and *tid, we known if the caller
+        * wants the next task.
+        */
+       if (*tid == common->pid_visiting) {
+               pid = find_pid_ns(common->pid_visiting, common-

Quoted 1 line

ns);

Quoted 8 lines
Quoted 4 lines

+               task = get_pid_task(pid, PIDTYPE_PID); +
+               return task;
+       }

Do not understand the above code. Why we need it? Looks like the code below trying to get the *next_task* and will return NULL if wrap around happens(the tid again equals tgid), right?

Quoted 8 lines

The above code is to handle the case that the caller want to visit the
same task again.  For example, task_file_seq_get_next() will call this
function several time to return the same task, and move to next task by
increasing info->tid.  The above code checks the value of *tid to return the same task if the value doesn't change.

Quoted 3 lines

Could you explain when task_file_seq_get_next() will call this function
several times? IIUC, from the following code,

Let's assume that a userspace program reads an BPF file iterator in several times instead of reading the whole content in once.

bpf_seq_read() will be called several times. Every time returning from bpf_seq_read except the last one, info->tid will be the tid of the latest visited task.

bpf_seq_read() will call seq->op->start() (a.k.a task_file_seq_start() in this case). However, task_file_seq_start() will reset info->task, and call task_file_seq_get_next() to return the task which has tid giving by *tid. Since info->task is NULL and *tid is not 0, task_file_seq_get_next() will call task_seq_get_next() to retrieve the same task again.

Does that make sense?

Quoted 165 lines

+static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
                                             u32 *tid, bool skip_if_dup_files) {
        struct task_struct *task = NULL;
        struct pid *pid;

+       if (common->type == BPF_TASK_ITER_TID) {
+               if (*tid && *tid != common->pid)
+                       return NULL;
+               rcu_read_lock();
+               pid = find_pid_ns(common->pid, common->ns); +               if (pid) {
+                       task = get_pid_task(pid, PIDTYPE_TGID); +                       *tid = common->pid;
+               }
+               rcu_read_unlock();

kernel/bpf/task_iter.c +7 -0
+
+               return task;
+       }
+
+       if (common->type == BPF_TASK_ITER_TGID) {
+               rcu_read_lock();
+               task = task_group_seq_get_next(common, tid,

skip_if_dup_files);
+               rcu_read_unlock();

kernel/bpf/task_iter.c +4 -0
+
+               return task;
+       }
+

task_group_seq_get_next() is only called once per task_seq_get_next() for BPF_TASK_ITER_TGID.
Maybe I missed something?

Quoted 126 lines
Quoted 33 lines
Quoted 20 lines
kernel/bpf/task_iter.c +17 -0
+
+retry:
+       pid = find_pid_ns(common->pid_visiting, common->ns);
+       if (!pid)
+               return NULL;
+
+       task = get_pid_task(pid, PIDTYPE_PID);
+       if (!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-
Quoted 1 line

ns);

Quoted 12 lines

+       if (*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;
+       }
+
+       get_task_struct(next_task);
+       common->pid_visiting = *tid;

Quoted 5 lines

We could do quite some redundant works here if the following condition is true. Basically, we get next_task and get a tid and release it, but in the next iteration, from tid, we try to get
the task again.

Yes, I will move 'retry' and move next_task to task to avoid the redundant work.

Quoted 83 lines
Quoted 82 lines

+
+       if (skip_if_dup_files && task->files == task-

Quoted 2 lines

group_leader-
files)

+               goto retry;

kernel/bpf/task_iter.c +5 -0
+
+       return next_task;
+}
+
+static struct task_struct *task_seq_get_next(struct

bpf_iter_seq_task_common *common,
                                              u32 *tid, bool skip_if_dup_files)
   {
         struct task_struct *task = NULL;
         struct pid *pid;

+       if (common->type == BPF_TASK_ITER_TID) {
+               if (*tid && *tid != common->pid)
+                       return NULL;
+               rcu_read_lock();
+               pid = find_pid_ns(common->pid, common->ns); +               if (pid) {
+                       task = get_pid_task(pid, PIDTYPE_TGID); +                       *tid = common->pid;
+               }
+               rcu_read_unlock();

kernel/bpf/task_iter.c +7 -0
+
+               return task;
+       }
+
+       if (common->type == BPF_TASK_ITER_TGID) {
+               rcu_read_lock();
+               task = task_group_seq_get_next(common, tid,

skip_if_dup_files);
+               rcu_read_unlock();

kernel/bpf/task_iter.c +4 -0
+
+               return task;
+       }
+

         rcu_read_lock();
   retry:
-       pid = find_ge_pid(*tid, ns);
+       pid = find_ge_pid(*tid, common->ns); if (pid) {
-               *tid = pid_nr_ns(pid, ns);
+               *tid = pid_nr_ns(pid, common->ns); task = get_pid_task(pid, PIDTYPE_PID); if (!task) {
                         ++*tid;

kernel/bpf/task_iter.c +0 -0
@@ -56,7 +148,7 @@ static void *task_seq_start(struct seq_file
 *seq, loff_t *pos)
          struct bpf_iter_seq_task_info *info = seq->private;
          struct task_struct *task;


-       task = task_seq_get_next(info->common.ns, &info->tid, false);
+       task = task_seq_get_next(&info->common, &info->tid, false);
         if (!task)
                 return NULL;

kernel/bpf/task_iter.c +1 -1
@@ -73,7 +165,7 @@ static void *task_seq_next(struct seq_file
 *seq,
 void *v, loff_t *pos)
          ++*pos;
          ++info->tid;
          put_task_struct((struct task_struct *)v);
-       task = task_seq_get_next(info->common.ns, &info->tid,
 false);
+       task = task_seq_get_next(&info->common, &info->tid,

false);
         if (!task)
                 return NULL;

kernel/bpf/task_iter.c +0 -0
@@ -117,6 +209,45 @@ static void task_seq_stop(struct seq_file
 *seq, void *v)
                  put_task_struct((struct task_struct *)v);
    }

[...]

#7 lore raw

PATCH v8 2/5 bpf: Handle bpf_link_info for the parameterized task BPF iterators.

In reply to Kui-Feng Lee

Add new fields to bpf_link_info that users can query it through bpf_obj_get_info_by_fd().

---
 include/uapi/linux/bpf.h       |  4 ++++
 kernel/bpf/task_iter.c         | 18 ++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  4 ++++
 3 files changed, 26 insertions(+)
include/uapi/linux/bpf.h +4 -0
index f212a19eda06..088cbc8f6a0c 100644
@@ -6216,6 +6216,10 @@ struct bpf_link_info {
 					__u64 cgroup_id;
 					__u32 order;
 				} cgroup;
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
 			};
 		} iter;
 		struct  {
kernel/bpf/task_iter.c +18 -0
index 0bc7277d1ee1..5140117447e5 100644
@@ -668,6 +668,21 @@ static const struct bpf_iter_seq_info task_seq_info = {
 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_info),
 };
 
+static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
+{
+	switch (aux->task.type) {
+	case BPF_TASK_ITER_TID:
+		info->iter.task.tid = aux->task.pid;
+		break;
+	case BPF_TASK_ITER_TGID:
+		info->iter.task.pid = aux->task.pid;
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static struct bpf_iter_reg task_reg_info = {
 	.target			= "task",
 	.attach_target		= bpf_iter_attach_task,
@@ -678,6 +693,7 @@ static struct bpf_iter_reg task_reg_info = {
 		  PTR_TO_BTF_ID_OR_NULL },
 	},
 	.seq_info		= &task_seq_info,
+	.fill_link_info		= bpf_iter_fill_link_info,
 };
 
 static const struct bpf_iter_seq_info task_file_seq_info = {
@@ -699,6 +715,7 @@ static struct bpf_iter_reg task_file_reg_info = {
 		  PTR_TO_BTF_ID_OR_NULL },
 	},
 	.seq_info		= &task_file_seq_info,
+	.fill_link_info		= bpf_iter_fill_link_info,
 };
 
 static const struct bpf_iter_seq_info task_vma_seq_info = {
@@ -720,6 +737,7 @@ static struct bpf_iter_reg task_vma_reg_info = {
 		  PTR_TO_BTF_ID_OR_NULL },
 	},
 	.seq_info		= &task_vma_seq_info,
+	.fill_link_info		= bpf_iter_fill_link_info,
 };
 
 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
tools/include/uapi/linux/bpf.h +4 -0
index 40935278eede..864cf6a613d6 100644
@@ -6216,6 +6216,10 @@ struct bpf_link_info {
 					__u64 cgroup_id;
 					__u32 order;
 				} cgroup;
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
 			};
 		} iter;
 		struct  {
Signature · 1 line
2.30.2
#8 lore raw

PATCH v8 3/5 bpf: Handle show_fdinfo for the parameterized task BPF iterators

In reply to Kui-Feng Lee

Show information of iterators in the respective files under /proc/<pid>/fdinfo/.

For example, for a task file iterator with 1723 as the value of tid parameter, its fdinfo would look like the following lines.

    pos:    0
    flags:  02000000
    mnt_id: 14
    ino:    38
    link_type:      iter
    link_id:        51
    prog_tag:       a590ac96db22b825
    prog_id:        299
    target_name:    task_file
    task_type:      TID
    tid: 1723

This patch add the last three fields. task_type is the type of the task parameter. TID means the iterator visit only the thread specified by tid. The value of tid in the above example is 1723. For the case of PID task_type, it means the iterator visits only threads of a process and will show the pid value of the process instead of a tid.

---
 kernel/bpf/task_iter.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
kernel/bpf/task_iter.c +18 -0
index 5140117447e5..c10aeeffe1d6 100644
@@ -10,6 +10,12 @@
 #include <linux/btf_ids.h>
 #include "mmap_unlock_work.h"
 
+static const char * const iter_task_type_names[] = {
+	"ALL",
+	"TID",
+	"PID",
+};
+
 struct bpf_iter_seq_task_common {
 	struct pid_namespace *ns;
 	enum bpf_iter_task_type	type;
@@ -683,6 +689,15 @@ static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct b
 	return 0;
 }
 
+static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
+{
+	seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
+	if (aux->task.type == BPF_TASK_ITER_TID)
+		seq_printf(seq, "tid:\t%d\n", aux->task.pid);
+	else if (aux->task.type == BPF_TASK_ITER_TGID)
+		seq_printf(seq, "pid:\t%d\n", aux->task.pid);
+}
+
 static struct bpf_iter_reg task_reg_info = {
 	.target			= "task",
 	.attach_target		= bpf_iter_attach_task,
@@ -694,6 +709,7 @@ static struct bpf_iter_reg task_reg_info = {
 	},
 	.seq_info		= &task_seq_info,
 	.fill_link_info		= bpf_iter_fill_link_info,
+	.show_fdinfo		= bpf_iter_task_show_fdinfo,
 };
 
 static const struct bpf_iter_seq_info task_file_seq_info = {
@@ -716,6 +732,7 @@ static struct bpf_iter_reg task_file_reg_info = {
 	},
 	.seq_info		= &task_file_seq_info,
 	.fill_link_info		= bpf_iter_fill_link_info,
+	.show_fdinfo		= bpf_iter_task_show_fdinfo,
 };
 
 static const struct bpf_iter_seq_info task_vma_seq_info = {
@@ -738,6 +755,7 @@ static struct bpf_iter_reg task_vma_reg_info = {
 	},
 	.seq_info		= &task_vma_seq_info,
 	.fill_link_info		= bpf_iter_fill_link_info,
+	.show_fdinfo		= bpf_iter_task_show_fdinfo,
 };
 
 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
Signature · 1 line
2.30.2
author Acked-by Yonghong Song on v8 3/5
#9 lore raw

Re: [PATCH bpf-next v8 3/5] bpf: Handle show_fdinfo for the parameterized task BPF iterators

Show information of iterators in the respective files under /proc/<pid>/fdinfo/.

For example, for a task file iterator with 1723 as the value of tid parameter, its fdinfo would look like the following lines.

    pos:    0
    flags:  02000000
    mnt_id: 14
    ino:    38
    link_type:      iter
    link_id:        51
    prog_tag:       a590ac96db22b825
    prog_id:        299
    target_name:    task_file
    task_type:      TID
    tid: 1723

This patch add the last three fields. task_type is the type of the task parameter. TID means the iterator visit only the thread specified by tid. The value of tid in the above example is 1723. For the case of PID task_type, it means the iterator visits only threads of a process and will show the pid value of the process instead of a tid.

---
 kernel/bpf/task_iter.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
kernel/bpf/task_iter.c +6 -0
index 5140117447e5..c10aeeffe1d6 100644
@@ -10,6 +10,12 @@
 #include <linux/btf_ids.h>
 #include "mmap_unlock_work.h"
 
+static const char * const iter_task_type_names[] = {
+	"ALL",
+	"TID",
+	"PID",
+};
+
 struct bpf_iter_seq_task_common {
 	struct pid_namespace *ns;
 	enum bpf_iter_task_type	type;
@@ -683,6 +689,15 @@ static int bpf_iter_fill_link_info(const struct

bpf_iter_aux_info *aux, struct b
return 0;
}

+static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
+{
+ seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux-

Quoted 1 line

task.type]);

+ if (aux->task.type == BPF_TASK_ITER_TID)
+ seq_printf(seq, "tid:\t%d\n", aux->task.pid);
+ else if (aux->task.type == BPF_TASK_ITER_TGID)
+ seq_printf(seq, "pid:\t%d\n", aux->task.pid);
+}
+
static struct bpf_iter_reg task_reg_info = {
.target = "task",
.attach_target = bpf_iter_attach_task,
@@ -694,6 +709,7 @@ static struct bpf_iter_reg task_reg_info = {
},
.seq_info = &task_seq_info,
.fill_link_info = bpf_iter_fill_link_info,
+ .show_fdinfo = bpf_iter_task_show_fdinfo,
};

static const struct bpf_iter_seq_info task_file_seq_info = {
@@ -716,6 +732,7 @@ static struct bpf_iter_reg task_file_reg_info = {
},
.seq_info = &task_file_seq_info,
.fill_link_info = bpf_iter_fill_link_info,
+ .show_fdinfo = bpf_iter_task_show_fdinfo,
};

static const struct bpf_iter_seq_info task_vma_seq_info = {
@@ -738,6 +755,7 @@ static struct bpf_iter_reg task_vma_reg_info = {
},
.seq_info = &task_vma_seq_info,
.fill_link_info = bpf_iter_fill_link_info,
+ .show_fdinfo = bpf_iter_task_show_fdinfo,
};

BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,

Signature · 1 line
2.30.2
#10 lore raw

Re: [PATCH bpf-next v8 3/5] bpf: Handle show_fdinfo for the parameterized task BPF iterators

Quoted 26 lines · Kui-Feng Lee

Show information of iterators in the respective files under /proc/<pid>/fdinfo/.

For example, for a task file iterator with 1723 as the value of tid parameter, its fdinfo would look like the following lines.

    pos:    0
    flags:  02000000
    mnt_id: 14
    ino:    38
    link_type:      iter
    link_id:        51
    prog_tag:       a590ac96db22b825
    prog_id:        299
    target_name:    task_file
    task_type:      TID
    tid: 1723

This patch add the last three fields.  task_type is the type of the task parameter.  TID means the iterator visit only the thread specified by tid.  The value of tid in the above example is 1723. For
the case of PID task_type, it means the iterator visits only threads of a process and will show the pid value of the process instead of a tid.

Quoted 2 lines

I missed the acked-by here in the previous message.

Acked-by on v8 3/5
#11 lore raw

Re: [PATCH bpf-next v8 3/5] bpf: Handle show_fdinfo for the parameterized task BPF iterators

In reply to Kui-Feng Lee
Quoted 18 lines · Kui-Feng Lee

Show information of iterators in the respective files under /proc/<pid>/fdinfo/.

For example, for a task file iterator with 1723 as the value of tid parameter, its fdinfo would look like the following lines.

     pos:    0
     flags:  02000000
     mnt_id: 14
     ino:    38
     link_type:      iter
     link_id:        51
     prog_tag:       a590ac96db22b825
     prog_id:        299
     target_name:    task_file
     task_type:      TID
     tid: 1723
Quoted 8 lines

This patch add the last three fields. task_type is the type of the task parameter. TID means the iterator visit only the thread specified by tid. The value of tid in the above example is 1723. For the case of PID task_type, it means the iterator visits only threads of a process and will show the pid value of the process instead of a tid.

Ack with a nit below.

Quoted 23 lines
---
  kernel/bpf/task_iter.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
kernel/bpf/task_iter.c +6 -0
index 5140117447e5..c10aeeffe1d6 100644
@@ -10,6 +10,12 @@
  #include <linux/btf_ids.h>
  #include "mmap_unlock_work.h"
 
+static const char * const iter_task_type_names[] = {
+	"ALL",
+	"TID",
+	"PID",
+};
+
  struct bpf_iter_seq_task_common {
  	struct pid_namespace *ns;
  	enum bpf_iter_task_type	type;
@@ -683,6 +689,15 @@ static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct b
  	return 0;

kernel/bpf/task_iter.c static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct b · line 699

Quoted 10 lines
kernel/bpf/task_iter.c +8 -0
  }
 
+static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
+{
+	seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
+	if (aux->task.type == BPF_TASK_ITER_TID)
+		seq_printf(seq, "tid:\t%d\n", aux->task.pid);
+	else if (aux->task.type == BPF_TASK_ITER_TGID)
+		seq_printf(seq, "pid:\t%d\n", aux->task.pid);
+}

Let us use '%u' instead of '%d' for aux->type.pid since its type is u32.

Quoted 11 lines
kernel/bpf/task_iter.c +2 -0
+
  static struct bpf_iter_reg task_reg_info = {
  	.target			= "task",
  	.attach_target		= bpf_iter_attach_task,
@@ -694,6 +709,7 @@ static struct bpf_iter_reg task_reg_info = {
  	},
  	.seq_info		= &task_seq_info,
  	.fill_link_info		= bpf_iter_fill_link_info,
+	.show_fdinfo		= bpf_iter_task_show_fdinfo,
  };
 

[...]

#12 lore raw

PATCH v8 4/5 selftests/bpf: Test parameterized task BPF iterators.

In reply to Kui-Feng Lee

Test iterators of vma, files and tasks.

Ensure the API works appropriately to visit all tasks, tasks in a process, or a particular task.

---
 .../selftests/bpf/prog_tests/bpf_iter.c       | 282 ++++++++++++++++--
 .../selftests/bpf/prog_tests/btf_dump.c       |   2 +-
 .../selftests/bpf/progs/bpf_iter_task.c       |   9 +
 .../selftests/bpf/progs/bpf_iter_task_file.c  |   9 +-
 .../selftests/bpf/progs/bpf_iter_task_vma.c   |   7 +-
 .../selftests/bpf/progs/bpf_iter_vma_offset.c |  37 +++
 6 files changed, 322 insertions(+), 24 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +261 -21
index e89685bd587c..30c781c3a0a6 100644
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #include <test_progs.h>
+#include <unistd.h>
+#include <sys/syscall.h>
 #include "bpf_iter_ipv6_route.skel.h"
 #include "bpf_iter_netlink.skel.h"
 #include "bpf_iter_bpf_map.skel.h"
@@ -14,6 +16,7 @@
 #include "bpf_iter_udp4.skel.h"
 #include "bpf_iter_udp6.skel.h"
 #include "bpf_iter_unix.skel.h"
+#include "bpf_iter_vma_offset.skel.h"
 #include "bpf_iter_test_kern1.skel.h"
 #include "bpf_iter_test_kern2.skel.h"
 #include "bpf_iter_test_kern3.skel.h"
@@ -43,13 +46,13 @@ static void test_btf_id_or_null(void)
 	}
 }
 
-static void do_dummy_read(struct bpf_program *prog)
+static void do_dummy_read_opts(struct bpf_program *prog, struct bpf_iter_attach_opts *opts)
 {
 	struct bpf_link *link;
 	char buf[16] = {};
 	int iter_fd, len;
 
-	link = bpf_program__attach_iter(prog, NULL);
+	link = bpf_program__attach_iter(prog, opts);
 	if (!ASSERT_OK_PTR(link, "attach_iter"))
 		return;
 
@@ -68,6 +71,11 @@ static void do_dummy_read(struct bpf_program *prog)
 	bpf_link__destroy(link);
 }
 
+static void do_dummy_read(struct bpf_program *prog)
+{
+	do_dummy_read_opts(prog, NULL);
+}
+
 static void do_read_map_iter_fd(struct bpf_object_skeleton **skel, struct bpf_program *prog,
 				struct bpf_map *map)
 {
@@ -167,19 +175,140 @@ static void test_bpf_map(void)
 	bpf_iter_bpf_map__destroy(skel);
 }
 
-static void test_task(void)
+static int pidfd_open(pid_t pid, unsigned int flags)
+{
+	return syscall(SYS_pidfd_open, pid, flags);
+}
+
+static void check_bpf_link_info(const struct bpf_program *prog)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+	struct bpf_link_info info = {};
+	struct bpf_link *link;
+	__u32 info_len;
+	int err;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.tid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	link = bpf_program__attach_iter(prog, &opts);
+	if (!ASSERT_OK_PTR(link, "attach_iter"))
+		return;
+
+	info_len = sizeof(info);
+	err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &info, &info_len);
+	ASSERT_OK(err, "bpf_obj_get_info_by_fd");
+	ASSERT_EQ(info.iter.task.tid, getpid(), "check_task_tid");
+
+	bpf_link__destroy(link);
+}
+
+static pthread_mutex_t do_nothing_mutex;
+
+static void *do_nothing_wait(void *arg)
+{
+	pthread_mutex_lock(&do_nothing_mutex);
+	pthread_mutex_unlock(&do_nothing_mutex);
+
+	pthread_exit(arg);
+}
+
+static void test_task_common_nocheck(struct bpf_iter_attach_opts *opts,
+				     int *num_unknown, int *num_known)
 {
 	struct bpf_iter_task *skel;
+	pthread_t thread_id;
+	void *ret;
 
 	skel = bpf_iter_task__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "bpf_iter_task__open_and_load"))
 		return;
 
-	do_dummy_read(skel->progs.dump_task);
+	ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock");
+
+	ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
+		  "pthread_create");
+
+	skel->bss->tid = getpid();
+
+	do_dummy_read_opts(skel->progs.dump_task, opts);
+
+	*num_unknown = skel->bss->num_unknown_tid;
+	*num_known = skel->bss->num_known_tid;
+
+	ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
+	ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
+		     "pthread_join");
 
 	bpf_iter_task__destroy(skel);
 }
 
+static void test_task_common(struct bpf_iter_attach_opts *opts, int num_unknown, int num_known)
+{
+	int num_unknown_tid, num_known_tid;
+
+	test_task_common_nocheck(opts, &num_unknown_tid, &num_known_tid);
+	ASSERT_EQ(num_unknown_tid, num_unknown, "check_num_unknown_tid");
+	ASSERT_EQ(num_known_tid, num_known, "check_num_known_tid");
+}
+
+static void test_task_tid(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+	int num_unknown_tid, num_known_tid;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.tid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+	test_task_common(&opts, 0, 1);
+
+	linfo.task.tid = 0;
+	linfo.task.pid = getpid();
+	test_task_common(&opts, 1, 1);
+
+	test_task_common_nocheck(NULL, &num_unknown_tid, &num_known_tid);
+	ASSERT_GT(num_unknown_tid, 1, "check_num_unknown_tid");
+	ASSERT_EQ(num_known_tid, 1, "check_num_known_tid");
+}
+
+static void test_task_pid(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.pid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	test_task_common(&opts, 1, 1);
+}
+
+static void test_task_pidfd(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+	int pidfd;
+
+	pidfd = pidfd_open(getpid(), 0);
+	if (!ASSERT_GT(pidfd, 0, "pidfd_open"))
+		return;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.pid_fd = pidfd;
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	test_task_common(&opts, 1, 1);
+
+	close(pidfd);
+}
+
 static void test_task_sleepable(void)
 {
 	struct bpf_iter_task *skel;
@@ -212,14 +341,11 @@ static void test_task_stack(void)
 	bpf_iter_task_stack__destroy(skel);
 }
 
-static void *do_nothing(void *arg)
-{
-	pthread_exit(arg);
-}
-
 static void test_task_file(void)
 {
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
 	struct bpf_iter_task_file *skel;
+	union bpf_iter_link_info linfo;
 	pthread_t thread_id;
 	void *ret;
 
@@ -229,19 +355,36 @@ static void test_task_file(void)
 
 	skel->bss->tgid = getpid();
 
-	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing, NULL),
-		  "pthread_create"))
-		goto done;
+	ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock");
 
-	do_dummy_read(skel->progs.dump_task_file);
+	ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
+		  "pthread_create");
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.tid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
 
-	if (!ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
-		  "pthread_join"))
-		goto done;
+	do_dummy_read_opts(skel->progs.dump_task_file, &opts);
 
 	ASSERT_EQ(skel->bss->count, 0, "check_count");
+	ASSERT_EQ(skel->bss->unique_tgid_count, 1, "check_unique_tgid_count");
+
+	skel->bss->last_tgid = 0;
+	skel->bss->count = 0;
+	skel->bss->unique_tgid_count = 0;
+
+	do_dummy_read(skel->progs.dump_task_file);
+
+	ASSERT_EQ(skel->bss->count, 0, "check_count");
+	ASSERT_GT(skel->bss->unique_tgid_count, 1, "check_unique_tgid_count");
+
+	check_bpf_link_info(skel->progs.dump_task_file);
+
+	ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
+	ASSERT_OK(pthread_join(thread_id, &ret), "pthread_join");
+	ASSERT_NULL(ret, "pthread_join");
 
-done:
 	bpf_iter_task_file__destroy(skel);
 }
 
@@ -1249,7 +1392,7 @@ static void str_strip_first_line(char *str)
 	*dst = '\0';
 }
 
-static void test_task_vma(void)
+static void test_task_vma_common(struct bpf_iter_attach_opts *opts)
 {
 	int err, iter_fd = -1, proc_maps_fd = -1;
 	struct bpf_iter_task_vma *skel;
@@ -1261,13 +1404,14 @@ static void test_task_vma(void)
 		return;
 
 	skel->bss->pid = getpid();
+	skel->bss->one_task = opts ? 1 : 0;
 
 	err = bpf_iter_task_vma__load(skel);
 	if (!ASSERT_OK(err, "bpf_iter_task_vma__load"))
 		goto out;
 
 	skel->links.proc_maps = bpf_program__attach_iter(
-		skel->progs.proc_maps, NULL);
+		skel->progs.proc_maps, opts);
 
 	if (!ASSERT_OK_PTR(skel->links.proc_maps, "bpf_program__attach_iter")) {
 		skel->links.proc_maps = NULL;
@@ -1291,6 +1435,8 @@ static void test_task_vma(void)
 			goto out;
 		len += err;
 	}
+	if (opts)
+		ASSERT_EQ(skel->bss->one_task_error, 0, "unexpected task");
 
 	/* read CMP_BUFFER_SIZE (1kB) from /proc/pid/maps */
 	snprintf(maps_path, 64, "/proc/%u/maps", skel->bss->pid);
@@ -1306,6 +1452,9 @@ static void test_task_vma(void)
 	str_strip_first_line(proc_maps_output);
 
 	ASSERT_STREQ(task_vma_output, proc_maps_output, "compare_output");
+
+	check_bpf_link_info(skel->progs.proc_maps);
+
 out:
 	close(proc_maps_fd);
 	close(iter_fd);
@@ -1325,8 +1474,93 @@ void test_bpf_sockmap_map_iter_fd(void)
 	bpf_iter_sockmap__destroy(skel);
 }
 
+static void test_task_vma(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.tid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	test_task_vma_common(&opts);
+	test_task_vma_common(NULL);
+}
+
+/* uprobe attach point */
+static noinline int trigger_func(int arg)
+{
+	asm volatile ("");
+	return arg + 1;
+}
+
+static void test_task_vma_offset_common(struct bpf_iter_attach_opts *opts, bool one_proc)
+{
+	struct bpf_iter_vma_offset *skel;
+	struct bpf_link *link;
+	char buf[16] = {};
+	int iter_fd, len;
+	int pgsz, shift;
+
+	skel = bpf_iter_vma_offset__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "bpf_iter_vma_offset__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+	skel->bss->address = (uintptr_t)trigger_func;
+	for (pgsz = getpagesize(), shift = 0; pgsz; pgsz >>= 1, shift++)
+		;
+	skel->bss->page_shift = shift;
+
+	link = bpf_program__attach_iter(skel->progs.get_vma_offset, opts);
+	if (!ASSERT_OK_PTR(link, "attach_iter"))
+		return;
+
+	iter_fd = bpf_iter_create(bpf_link__fd(link));
+	if (!ASSERT_GT(iter_fd, 0, "create_iter"))
+		goto exit;
+
+	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
+		;
+	buf[15] = 0;
+	ASSERT_EQ(strcmp(buf, "OK\n"), 0, "strcmp");
+
+	ASSERT_EQ(skel->bss->offset, get_uprobe_offset(trigger_func), "offset");
+	if (one_proc)
+		ASSERT_EQ(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
+	else
+		ASSERT_GT(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
+
+	close(iter_fd);
+
+exit:
+	bpf_link__destroy(link);
+}
+
+static void test_task_vma_offset(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.pid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	test_task_vma_offset_common(&opts, true);
+
+	linfo.task.pid = 0;
+	linfo.task.tid = getpid();
+	test_task_vma_offset_common(&opts, true);
+
+	test_task_vma_offset_common(NULL, false);
+}
+
 void test_bpf_iter(void)
 {
+	ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init");
+
 	if (test__start_subtest("btf_id_or_null"))
 		test_btf_id_or_null();
 	if (test__start_subtest("ipv6_route"))
@@ -1335,8 +1569,12 @@ void test_bpf_iter(void)
 		test_netlink();
 	if (test__start_subtest("bpf_map"))
 		test_bpf_map();
-	if (test__start_subtest("task"))
-		test_task();
+	if (test__start_subtest("task_tid"))
+		test_task_tid();
+	if (test__start_subtest("task_pid"))
+		test_task_pid();
+	if (test__start_subtest("task_pidfd"))
+		test_task_pidfd();
 	if (test__start_subtest("task_sleepable"))
 		test_task_sleepable();
 	if (test__start_subtest("task_stack"))
@@ -1397,4 +1635,6 @@ void test_bpf_iter(void)
 		test_ksym_iter();
 	if (test__start_subtest("bpf_sockmap_map_iter_fd"))
 		test_bpf_sockmap_map_iter_fd();
+	if (test__start_subtest("vma_offset"))
+		test_task_vma_offset();
 }
tools/testing/selftests/bpf/prog_tests/btf_dump.c +1 -1
index 7b5bbe21b549..847acd48fe9f 100644
@@ -764,7 +764,7 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d,
 
 	/* union with nested struct */
 	TEST_BTF_DUMP_DATA(btf, d, "union", str, union bpf_iter_link_info, BTF_F_COMPACT,
-			   "(union bpf_iter_link_info){.map = (struct){.map_fd = (__u32)1,},.cgroup = (struct){.order = (enum bpf_cgroup_iter_order)BPF_CGROUP_ITER_SELF_ONLY,.cgroup_fd = (__u32)1,},}",
+			   "(union bpf_iter_link_info){.map = (struct){.map_fd = (__u32)1,},.cgroup = (struct){.order = (enum bpf_cgroup_iter_order)BPF_CGROUP_ITER_SELF_ONLY,.cgroup_fd = (__u32)1,},.task = (struct){.tid = (__u32)1,.pid = (__u32)1,},}",
 			   { .cgroup = { .order = 1, .cgroup_fd = 1, }});
 
 	/* struct skb with nested structs/unions; because type output is so
tools/testing/selftests/bpf/progs/bpf_iter_task.c +9 -0
index d22741272692..96131b9a1caa 100644
@@ -6,6 +6,10 @@
 
 char _license[] SEC("license") = "GPL";
 
+uint32_t tid = 0;
+int num_unknown_tid = 0;
+int num_known_tid = 0;
+
 SEC("iter/task")
 int dump_task(struct bpf_iter__task *ctx)
 {
@@ -18,6 +22,11 @@ int dump_task(struct bpf_iter__task *ctx)
 		return 0;
 	}
 
+	if (task->pid != tid)
+		num_unknown_tid++;
+	else
+		num_known_tid++;
+
 	if (ctx->meta->seq_num == 0)
 		BPF_SEQ_PRINTF(seq, "    tgid      gid\n");
 
tools/testing/selftests/bpf/progs/bpf_iter_task_file.c +8 -1
index 6e7b400888fe..b0255080662d 100644
@@ -7,14 +7,16 @@ char _license[] SEC("license") = "GPL";
 
 int count = 0;
 int tgid = 0;
+int last_tgid = 0;
+int unique_tgid_count = 0;
 
 SEC("iter/task_file")
 int dump_task_file(struct bpf_iter__task_file *ctx)
 {
 	struct seq_file *seq = ctx->meta->seq;
 	struct task_struct *task = ctx->task;
-	__u32 fd = ctx->fd;
 	struct file *file = ctx->file;
+	__u32 fd = ctx->fd;
 
 	if (task == (void *)0 || file == (void *)0)
 		return 0;
@@ -27,6 +29,11 @@ int dump_task_file(struct bpf_iter__task_file *ctx)
 	if (tgid == task->tgid && task->tgid != task->pid)
 		count++;
 
+	if (last_tgid != task->tgid) {
+		last_tgid = task->tgid;
+		unique_tgid_count++;
+	}
+
 	BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd,
 		       (long)file->f_op);
 	return 0;
tools/testing/selftests/bpf/progs/bpf_iter_task_vma.c +6 -1
index 4ea6a37d1345..dd923dc637d5 100644
@@ -20,6 +20,8 @@ char _license[] SEC("license") = "GPL";
 #define D_PATH_BUF_SIZE 1024
 char d_path_buf[D_PATH_BUF_SIZE] = {};
 __u32 pid = 0;
+__u32 one_task = 0;
+__u32 one_task_error = 0;
 
 SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
 {
@@ -33,8 +35,11 @@ SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
 		return 0;
 
 	file = vma->vm_file;
-	if (task->tgid != pid)
+	if (task->tgid != pid) {
+		if (one_task)
+			one_task_error = 1;
 		return 0;
+	}
 	perm_str[0] = (vma->vm_flags & VM_READ) ? 'r' : '-';
 	perm_str[1] = (vma->vm_flags & VM_WRITE) ? 'w' : '-';
 	perm_str[2] = (vma->vm_flags & VM_EXEC) ? 'x' : '-';
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +37 -0
new file mode 100644
index 000000000000..03db36da5da8
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+#include "bpf_iter.h"
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u32 unique_tgid_cnt = 0;
+uintptr_t address = 0;
+uintptr_t offset = 0;
+__u32 last_tgid = 0;
+__u32 pid = 0;
+__u32 page_shift = 0;
+
+SEC("iter/task_vma")
+int get_vma_offset(struct bpf_iter__task_vma *ctx)
+{
+	struct vm_area_struct *vma = ctx->vma;
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+
+	if (task == NULL || vma == NULL)
+		return 0;
+
+	if (last_tgid != task->tgid)
+		unique_tgid_cnt++;
+	last_tgid = task->tgid;
+
+	if (task->tgid != pid)
+		return 0;
+
+	if (vma->vm_start <= address && vma->vm_end > address) {
+		offset = address - vma->vm_start + (vma->vm_pgoff << 12);
+		BPF_SEQ_PRINTF(seq, "OK\n");
+	}
+	return 0;
+}
Signature · 1 line
2.30.2
#13 lore raw

Re: [PATCH bpf-next v8 4/5] selftests/bpf: Test parameterized task BPF iterators.

Quoted 16 lines · Kui-Feng Lee

Test iterators of vma, files and tasks.

Ensure the API works appropriately to visit all tasks, tasks in a process, or a particular task.

---
  .../selftests/bpf/prog_tests/bpf_iter.c       | 282 ++++++++++++++++--
  .../selftests/bpf/prog_tests/btf_dump.c       |   2 +-
  .../selftests/bpf/progs/bpf_iter_task.c       |   9 +
  .../selftests/bpf/progs/bpf_iter_task_file.c  |   9 +-
  .../selftests/bpf/progs/bpf_iter_task_vma.c   |   7 +-
  .../selftests/bpf/progs/bpf_iter_vma_offset.c |  37 +++
  6 files changed, 322 insertions(+), 24 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c

[...]

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +8 -0
+
+static void test_task_vma_offset_common(struct bpf_iter_attach_opts *opts, bool one_proc)
+{
+	struct bpf_iter_vma_offset *skel;
+	struct bpf_link *link;
+	char buf[16] = {};
+	int iter_fd, len;
+	int pgsz, shift;

tools/testing/selftests/bpf/prog_tests/bpf_iter.c void test_bpf_sockmap_map_iter_fd(void) · line 1514

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +10 -0
+
+	skel = bpf_iter_vma_offset__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "bpf_iter_vma_offset__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+	skel->bss->address = (uintptr_t)trigger_func;
+	for (pgsz = getpagesize(), shift = 0; pgsz; pgsz >>= 1, shift++)
+		;
+	skel->bss->page_shift = shift;

if getpagesize() is 4K, shift will be 13 which is incorrect.

Quoted 74 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +55 -2
+
+	link = bpf_program__attach_iter(skel->progs.get_vma_offset, opts);
+	if (!ASSERT_OK_PTR(link, "attach_iter"))
+		return;
+
+	iter_fd = bpf_iter_create(bpf_link__fd(link));
+	if (!ASSERT_GT(iter_fd, 0, "create_iter"))
+		goto exit;
+
+	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
+		;
+	buf[15] = 0;
+	ASSERT_EQ(strcmp(buf, "OK\n"), 0, "strcmp");
+
+	ASSERT_EQ(skel->bss->offset, get_uprobe_offset(trigger_func), "offset");
+	if (one_proc)
+		ASSERT_EQ(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
+	else
+		ASSERT_GT(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
+
+	close(iter_fd);
+
+exit:
+	bpf_link__destroy(link);
+}
+
+static void test_task_vma_offset(void)
+{
+	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
+	union bpf_iter_link_info linfo;
+
+	memset(&linfo, 0, sizeof(linfo));
+	linfo.task.pid = getpid();
+	opts.link_info = &linfo;
+	opts.link_info_len = sizeof(linfo);
+
+	test_task_vma_offset_common(&opts, true);
+
+	linfo.task.pid = 0;
+	linfo.task.tid = getpid();
+	test_task_vma_offset_common(&opts, true);
+
+	test_task_vma_offset_common(NULL, false);
+}
+
  void test_bpf_iter(void)
  {
+	ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init");
+
  	if (test__start_subtest("btf_id_or_null"))
  		test_btf_id_or_null();
  	if (test__start_subtest("ipv6_route"))
@@ -1335,8 +1569,12 @@ void test_bpf_iter(void)
  		test_netlink();
  	if (test__start_subtest("bpf_map"))
  		test_bpf_map();
-	if (test__start_subtest("task"))
-		test_task();
+	if (test__start_subtest("task_tid"))
+		test_task_tid();
+	if (test__start_subtest("task_pid"))
+		test_task_pid();
+	if (test__start_subtest("task_pidfd"))
+		test_task_pidfd();
  	if (test__start_subtest("task_sleepable"))
  		test_task_sleepable();
  	if (test__start_subtest("task_stack"))
@@ -1397,4 +1635,6 @@ void test_bpf_iter(void)
  		test_ksym_iter();
  	if (test__start_subtest("bpf_sockmap_map_iter_fd"))
  		test_bpf_sockmap_map_iter_fd();
+	if (test__start_subtest("vma_offset"))
+		test_task_vma_offset();
  }

[...]

Quoted 9 lines
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +3 -0
new file mode 100644
index 000000000000..03db36da5da8
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+#include "bpf_iter.h"

tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c line 13

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +10 -0
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u32 unique_tgid_cnt = 0;
+uintptr_t address = 0;
+uintptr_t offset = 0;
+__u32 last_tgid = 0;
+__u32 pid = 0;
+__u32 page_shift = 0;

page_shift is not used here.

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +10 -0
+
+SEC("iter/task_vma")
+int get_vma_offset(struct bpf_iter__task_vma *ctx)
+{
+	struct vm_area_struct *vma = ctx->vma;
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+
+	if (task == NULL || vma == NULL)
+		return 0;

tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c line 33

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +10 -0
+
+	if (last_tgid != task->tgid)
+		unique_tgid_cnt++;
+	last_tgid = task->tgid;
+
+	if (task->tgid != pid)
+		return 0;
+
+	if (vma->vm_start <= address && vma->vm_end > address) {
+		offset = address - vma->vm_start + (vma->vm_pgoff << 12);

'12' -> page_shift?

Quoted 4 lines
tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c new file +4 -0
+		BPF_SEQ_PRINTF(seq, "OK\n");
+	}
+	return 0;
+}
#14 lore raw

PATCH v8 5/5 bpftool: Show parameters of BPF task iterators.

In reply to Kui-Feng Lee

Show tid or pid of iterators if giving an argument of tid or pid

For example, the command bpftool link list may list following lines.

1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
33: iter  prog 225  target_name task_file  tid 1644
        pids test_progs(1644)

Link 33 is a task_file iterator with tid 1644. For now, only targets of task, task_file and task_vma may be with tid or pid to filter out tasks other than those belonging to a process (pid) or a thread (tid).

---
 tools/bpf/bpftool/link.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/bpf/bpftool/link.c +19 -0
index 7a20931c3250..88937036fae0 100644
@@ -83,6 +83,13 @@ static bool is_iter_map_target(const char *target_name)
 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
 }
 
+static bool is_iter_task_target(const char *target_name)
+{
+	return strcmp(target_name, "task") == 0 ||
+		strcmp(target_name, "task_file") == 0 ||
+		strcmp(target_name, "task_vma") == 0;
+}
+
 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
 {
 	const char *target_name = u64_to_ptr(info->iter.target_name);
@@ -91,6 +98,12 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
 
 	if (is_iter_map_target(target_name))
 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
+	else if (is_iter_task_target(target_name)) {
+		if (info->iter.task.tid)
+			jsonw_uint_field(wtr, "tid", info->iter.task.tid);
+		else if (info->iter.task.pid)
+			jsonw_uint_field(wtr, "pid", info->iter.task.pid);
+	}
 }
 
 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
@@ -208,6 +221,12 @@ static void show_iter_plain(struct bpf_link_info *info)
 
 	if (is_iter_map_target(target_name))
 		printf("map_id %u  ", info->iter.map.map_id);
+	else if (is_iter_task_target(target_name)) {
+		if (info->iter.task.tid)
+			printf("tid %u ", info->iter.task.tid);
+		else if (info->iter.task.pid)
+			printf("pid %u ", info->iter.task.pid);
+	}
 }
 
 static int show_link_close_plain(int fd, struct bpf_link_info *info)
Signature · 1 line
2.30.2
Acked-by on v8 5/5
#15 lore raw

Re: [PATCH bpf-next v8 5/5] bpftool: Show parameters of BPF task iterators.

Quoted 53 lines · Kui-Feng Lee

Show tid or pid of iterators if giving an argument of tid or pid

For example, the command bpftool link list may list following lines.

1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
33: iter  prog 225  target_name task_file  tid 1644
        pids test_progs(1644)

Link 33 is a task_file iterator with tid 1644. For now, only targets of task, task_file and task_vma may be with tid or pid to filter out tasks other than those belonging to a process (pid) or a thread (tid).

---
 tools/bpf/bpftool/link.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/bpf/bpftool/link.c +13 -0
index 7a20931c3250..88937036fae0 100644
@@ -83,6 +83,13 @@ static bool is_iter_map_target(const char *target_name)
 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
 }
 
+static bool is_iter_task_target(const char *target_name)
+{
+	return strcmp(target_name, "task") == 0 ||
+		strcmp(target_name, "task_file") == 0 ||
+		strcmp(target_name, "task_vma") == 0;
+}
+
 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
 {
 	const char *target_name = u64_to_ptr(info->iter.target_name);
@@ -91,6 +98,12 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
 
 	if (is_iter_map_target(target_name))
 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
+	else if (is_iter_task_target(target_name)) {
+		if (info->iter.task.tid)
+			jsonw_uint_field(wtr, "tid", info->iter.task.tid);
+		else if (info->iter.task.pid)
+			jsonw_uint_field(wtr, "pid", info->iter.task.pid);
+	}
 }
 
 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
@@ -208,6 +221,12 @@ static void show_iter_plain(struct bpf_link_info *info)
 
 	if (is_iter_map_target(target_name))

tools/bpf/bpftool/link.c static void show_iter_plain(struct bpf_link_info *info) · line 232

Quoted 10 lines
tools/bpf/bpftool/link.c +6 -0
 		printf("map_id %u  ", info->iter.map.map_id);
+	else if (is_iter_task_target(target_name)) {
+		if (info->iter.task.tid)
+			printf("tid %u ", info->iter.task.tid);
+		else if (info->iter.task.pid)
+			printf("pid %u ", info->iter.task.pid);
+	}
 }
 
 static int show_link_close_plain(int fd, struct bpf_link_info *info)

Looks good to me, although this patch may conflict with
https://lore.kernel.org/bpf/[email protected]/t/#u

#16 lore raw

Re: [PATCH bpf-next v8 5/5] bpftool: Show parameters of BPF task iterators.

Quoted 85 lines · Quentin Monnet

!-------------------------------------------------------------------| This Message Is From an External Sender

Quoted 2 lines

-------------------------------------------------------------------
!

Quoted 67 lines · Kui-Feng Lee

Show tid or pid of iterators if giving an argument of tid or pid

For example, the command bpftool link list may list following lines.

1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
33: iter  prog 225  target_name task_file  tid 1644 pids test_progs(1644)

Link 33 is a task_file iterator with tid 1644.  For now, only targets
of task, task_file and task_vma may be with tid or pid to filter out
tasks other than those belonging to a process (pid) or a thread (tid).

---
 tools/bpf/bpftool/link.c | 19 +++++++++++++++++++

 1 file changed, 19 insertions(+)
tools/bpf/bpftool/link.c +0 -0
index 7a20931c3250..88937036fae0 100644
@@ -83,6 +83,13 @@ static bool is_iter_map_target(const char
 *target_name)
                strcmp(target_name, "bpf_sk_storage_map") == 0;
  }

tools/bpf/bpftool/link.c +7 -0
+static bool is_iter_task_target(const char *target_name)
+{
+       return strcmp(target_name, "task") == 0 ||
+               strcmp(target_name, "task_file") == 0 ||
+               strcmp(target_name, "task_vma") == 0;
+}
+

 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
 {
        const char *target_name = u64_to_ptr(info-

Quoted 1 line

iter.target_name);

tools/bpf/bpftool/link.c +0 -0
@@ -91,6 +98,12 @@ static void show_iter_json(struct bpf_link_info
 *info, json_writer_t *wtr)


        if (is_iter_map_target(target_name)) jsonw_uint_field(wtr, "map_id", info-

Quoted 1 line

iter.map.map_id);

+       else if (is_iter_task_target(target_name)) { +               if (info->iter.task.tid)
+                       jsonw_uint_field(wtr, "tid", info-

Quoted 1 line

iter.task.tid);

+               else if (info->iter.task.pid)
+                       jsonw_uint_field(wtr, "pid", info-

Quoted 1 line

iter.task.pid);

+       }
 }

 static int get_prog_info(int prog_id, struct bpf_prog_info *info)

tools/bpf/bpftool/link.c +0 -0
@@ -208,6 +221,12 @@ static void show_iter_plain(struct
 bpf_link_info *info)


        if (is_iter_map_target(target_name)) printf("map_id %u  ", info->iter.map.map_id); +       else if (is_iter_task_target(target_name)) { +               if (info->iter.task.tid)

Quoted 8 lines

+                       printf("tid %u ", info->iter.task.tid); +               else if (info->iter.task.pid)
+                       printf("pid %u ", info->iter.task.pid); +       }
 }

 static int show_link_close_plain(int fd, struct bpf_link_info *info)

Quoted 2 lines

Looks good to me, although this patch may conflict with
https://lore.kernel.org/bpf/[email protected]/t/#u

Thanks! I will rebase the code.

Acked-by on v8 5/5
#17 lore raw

Re: [PATCH bpf-next v8 5/5] bpftool: Show parameters of BPF task iterators.

In reply to Kui-Feng Lee
Quoted 10 lines · Kui-Feng Lee

Show tid or pid of iterators if giving an argument of tid or pid

For example, the command bpftool link list may list following lines.

1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
33: iter  prog 225  target_name task_file  tid 1644
         pids test_progs(1644)
Quoted 5 lines

Link 33 is a task_file iterator with tid 1644. For now, only targets of task, task_file and task_vma may be with tid or pid to filter out tasks other than those belonging to a process (pid) or a thread (tid).

Please rebase on top of bpf-next as the bpf_iter cgroup support is just merged which will cause a conflict with this patch.

#18 lore raw

Re: [PATCH bpf-next v8 5/5] bpftool: Show parameters of BPF task iterators.

Quoted 20 lines · Yonghong Song
Quoted 17 lines · Kui-Feng Lee

Show tid or pid of iterators if giving an argument of tid or pid

For example, the command bpftool link list may list following lines.

1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
33: iter  prog 225  target_name task_file  tid 1644 pids test_progs(1644)

Link 33 is a task_file iterator with tid 1644.  For now, only targets
of task, task_file and task_vma may be with tid or pid to filter out
tasks other than those belonging to a process (pid) or a thread (tid).

Quoted 6 lines
Quoted 1 line

Please rebase on top of bpf-next as the bpf_iter cgroup support is just merged which will cause a conflict with this patch.

Tnank you! Just rebased the code.