Skip to contentSkip to conversation

Parameterize task iterators.

PATCH Superseded Kui-Feng Lee 3 participants 17 messages Aug 19, 2022 – Aug 27, 2022

Subsystems (5)
Kselftest BPF BPF selftests BPF core +1 more subsystems

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v6 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 addintional 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 v5:

- Use user-speace tid/pid terminologies in bpf_iter_link_info *** BLURB HERE *** 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.

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 (4):
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.

 include/linux/bpf.h                           |  25 ++
 include/uapi/linux/bpf.h                      |  12 +
 kernel/bpf/task_iter.c                        | 142 +++++++--
 tools/include/uapi/linux/bpf.h                |  12 +
 .../selftests/bpf/prog_tests/bpf_iter.c       | 284 +++++++++++++++++-
 .../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   |   6 +-
 .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++
 10 files changed, 493 insertions(+), 43 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v6 1/4 bpf: 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.

---
 include/linux/bpf.h            |  25 +++++++
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/task_iter.c         | 116 ++++++++++++++++++++++++++-------
 tools/include/uapi/linux/bpf.h |   6 ++
 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +25 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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 reosurces of evevry 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 {
 	struct bpf_map *map;
+	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 934a2a8beb87..778fbf11aa00 100644
@@ -91,6 +91,12 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	/* 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 +92 -24

238 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +6 -0
index 1d6085e15fc8..7a0268749a48 100644
@@ -91,6 +91,12 @@ union bpf_iter_link_info {
 	struct {
 		__u32	map_fd;
 	} map;
+	/* 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
Acked-by on v6 1/4
#2 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

Quoted 1 line · Kui-Feng Lee

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

one thread/process.

Quoted 8 lines

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.

#3 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

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

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.

---
 include/linux/bpf.h            |  25 +++++++
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/task_iter.c         | 116 ++++++++++++++++++++++++++-------
 tools/include/uapi/linux/bpf.h |   6 ++
 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +4 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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

include/linux/bpf.h int bpf_obj_get_user(const char __user *pathname, int flags); · line 1745

Quoted 10 lines
include/linux/bpf.h +10 -0
+ * 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 reosurces of evevry task of a process / task group.

typos: resources, every

Quoted 7 lines
include/linux/bpf.h +7 -0
+ */
+enum bpf_iter_task_type {
+       BPF_TASK_ITER_ALL = 0,
+       BPF_TASK_ITER_TID,
+       BPF_TASK_ITER_TGID,
+};
+

[...]

Quoted 7 lines
        rcu_read_lock();

retry:

include/linux/bpf.h +2 -2
-       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);

include/linux/bpf.h

Quoted 10 lines
include/linux/bpf.h +4 -2
                task = get_pid_task(pid, PIDTYPE_PID);
                if (!task) {
                        ++*tid;
                        goto retry;
-               } else if (skip_if_dup_files && !thread_group_leader(task) &&
-                          task->files == task->group_leader->files) {
+               } else if ((skip_if_dup_files && !thread_group_leader(task) &&
+                           task->files == task->group_leader->files) ||
+                          (common->type == BPF_TASK_ITER_TGID &&
+                           __task_pid_nr_ns(task, PIDTYPE_TGID, common->ns) != common->pid)) {

it gets super hard to follow this logic, would a simple helper function to calculate this condition (and maybe some comments to explain the logic behind these checks?) make it a bit more readable?

Quoted 23 lines
                        put_task_struct(task);
                        task = NULL;
                        ++*tid;
include/linux/bpf.h +2 -2
@@ -56,7 +73,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 +90,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 +134,48 @@ static void task_seq_stop(struct seq_file *seq, void *v)
                put_task_struct((struct task_struct *)v);

include/linux/bpf.h static void task_seq_stop(struct seq_file *seq, void *v) · line 144

Quoted 10 lines
include/linux/bpf.h +8 -0
 }
 
+static int bpf_iter_attach_task(struct bpf_prog *prog,
+                               union bpf_iter_link_info *linfo,
+                               struct bpf_iter_aux_info *aux)
+{
+       unsigned int flags;
+       struct pid_namespace *ns;
+       struct pid *pid;
+       pid_t tgid;

it seems it would be simpler to first check that at most one of tid/pid/pid_fd is set instead of making sure that aux->task.type wasn't already set.

How about

if (!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd > 1)

    return -EINVAL;

?

Quoted 34 lines
include/linux/bpf.h +34 -0
+
+       aux->task.type = BPF_TASK_ITER_ALL;
+       if (linfo->task.tid != 0) {
+               aux->task.type = BPF_TASK_ITER_TID;
+               aux->task.pid = linfo->task.tid;
+       }
+       if (linfo->task.pid != 0) {
+               if (aux->task.type != BPF_TASK_ITER_ALL)
+                       return -EINVAL;
+
+               aux->task.type = BPF_TASK_ITER_TGID;
+               aux->task.pid = linfo->task.pid;
+       }
+       if (linfo->task.pid_fd != 0) {
+               if (aux->task.type != BPF_TASK_ITER_ALL)
+                       return -EINVAL;
+
+               aux->task.type = BPF_TASK_ITER_TGID;
+               ns = task_active_pid_ns(current);
+               if (IS_ERR(ns))
+                       return PTR_ERR(ns);
+
+               pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
+               if (IS_ERR(pid))
+                       return PTR_ERR(pid);
+
+               tgid = pid_nr_ns(pid, ns);
+               aux->task.pid = tgid;
+               put_pid(pid);
+       }
+
+       return 0;
+}
+

[...]

#4 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

Quoted 76 lines · Andrii Nakryiko
Quoted 43 lines · Kui-Feng Lee

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.

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

 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +14 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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 reosurces of evevry task of a process / task
Quoted 1 line

group.

typos: resources, every

Quoted 7 lines

+ */

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

[...]

Quoted 18 lines

        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;
                        goto retry;
-               } else if (skip_if_dup_files && !thread_group_leader(task) &&
-                          task->files == task->group_leader-

Quoted 1 line

files) {

+               } else if ((skip_if_dup_files && !thread_group_leader(task) &&
+                           task->files == task->group_leader-

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

files) ||

+                          (common->type == BPF_TASK_ITER_TGID && +                           __task_pid_nr_ns(task, PIDTYPE_TGID, common->ns) != common->pid)) {

it gets super hard to follow this logic, would a simple helper function to calculate this condition (and maybe some comments to explain the logic behind these checks?) make it a bit more readable?

!matched_task(task, common, skip_if_dup_file)?

bool matched_task(struct task_struct *task,

                  struct bpf_iter_seq_task_common *common,
                  bool skip_if_dup_file) {
        /* Should not have the same 'files' if skip_if_dup_file is true
*/
        bool diff_files_if =
                !skip_if_dup_file ||
                (thread_group_leader(task) &&
                task->file != task->gorup_leader->fies);
        /* Should have the given tgid if the type is BPF_TASK_ITER_TGI
*/
        bool have_tgid_if =
                common->type != BPF_TASK_ITER_TGID ||
                __task_pid_nr_ns(task, PIDTYPE_TGID,
                common->ns) == common->pid;
        return diff_files_if && have_tgid_if;
}

How about this?

Quoted 44 lines
Quoted 29 lines

                        put_task_struct(task);
                        task = NULL;
                        ++*tid;

include/linux/bpf.h +1 -1
@@ -56,7 +73,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;

include/linux/bpf.h +1 -1
@@ -73,7 +90,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;

include/linux/bpf.h +0 -0
@@ -117,6 +134,48 @@ static void task_seq_stop(struct seq_file
 *seq, void *v)
                 put_task_struct((struct task_struct *)v);

include/linux/bpf.h static void task_seq_stop(struct seq_file · line 145

Quoted 10 lines
include/linux/bpf.h +8 -0
  }
 
+static int bpf_iter_attach_task(struct bpf_prog *prog,
+                               union bpf_iter_link_info *linfo,
+                               struct bpf_iter_aux_info *aux)
+{
+       unsigned int flags;
+       struct pid_namespace *ns;
+       struct pid *pid;
+       pid_t tgid;

it seems it would be simpler to first check that at most one of tid/pid/pid_fd is set instead of making sure that aux->task.type wasn't already set.

Quoted 6 lines

How about

if (!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd > 1) return -EINVAL;

?

Agree

Quoted 37 lines
Quoted 35 lines
include/linux/bpf.h +34 -0
+
+       aux->task.type = BPF_TASK_ITER_ALL;
+       if (linfo->task.tid != 0) {
+               aux->task.type = BPF_TASK_ITER_TID;
+               aux->task.pid = linfo->task.tid;
+       }
+       if (linfo->task.pid != 0) {
+               if (aux->task.type != BPF_TASK_ITER_ALL)
+                       return -EINVAL;
+
+               aux->task.type = BPF_TASK_ITER_TGID;
+               aux->task.pid = linfo->task.pid;
+       }
+       if (linfo->task.pid_fd != 0) {
+               if (aux->task.type != BPF_TASK_ITER_ALL)
+                       return -EINVAL;
+
+               aux->task.type = BPF_TASK_ITER_TGID;
+               ns = task_active_pid_ns(current);
+               if (IS_ERR(ns))
+                       return PTR_ERR(ns);
+
+               pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
+               if (IS_ERR(pid))
+                       return PTR_ERR(pid);
+
+               tgid = pid_nr_ns(pid, ns);
+               aux->task.pid = tgid;
+               put_pid(pid);
+       }
+
+       return 0;
+}
+

[...]

#5 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

Quoted 107 lines · Kui-Feng Lee
Quoted 76 lines · Andrii Nakryiko
Quoted 43 lines · Kui-Feng Lee

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.

---
 include/linux/bpf.h            |  25 +++++++
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/task_iter.c         | 116 ++++++++++++++++++++++++++---
----
 tools/include/uapi/linux/bpf.h |   6 ++
 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +14 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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 reosurces of evevry task of a process / task
Quoted 1 line

group.

typos: resources, every

Quoted 7 lines

+ */

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

[...]

Quoted 18 lines
        rcu_read_lock();

retry:

include/linux/bpf.h +2 -3
-       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;
                        goto retry;
-               } else if (skip_if_dup_files &&

!thread_group_leader(task) &&
- task->files == task->group_leader-

Quoted 1 line

files) {

+ } else if ((skip_if_dup_files &&
!thread_group_leader(task) &&
+ task->files == task->group_leader-

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

files) ||

include/linux/bpf.h +2 -0
+                          (common->type == BPF_TASK_ITER_TGID &&
+                           __task_pid_nr_ns(task, PIDTYPE_TGID,

common->ns) != common->pid)) {

it gets super hard to follow this logic, would a simple helper function to calculate this condition (and maybe some comments to explain the logic behind these checks?) make it a bit more readable?

!matched_task(task, common, skip_if_dup_file)?

bool matched_task(struct task_struct *task,

                  struct bpf_iter_seq_task_common *common,
                  bool skip_if_dup_file) {
        /* Should not have the same 'files' if skip_if_dup_file is true
*/
        bool diff_files_if =
                !skip_if_dup_file ||
                (thread_group_leader(task) &&
                task->file != task->gorup_leader->fies);
        /* Should have the given tgid if the type is BPF_TASK_ITER_TGI
*/
        bool have_tgid_if =
                common->type != BPF_TASK_ITER_TGID ||
                __task_pid_nr_ns(task, PIDTYPE_TGID,
                common->ns) == common->pid;
        return diff_files_if && have_tgid_if;
}
Quoted 2 lines

How about this?

Hm... "matched_task" doesn't mean much, tbh, so not really. I wanted to suggest having a separate helper just for your TGID check and call it something more meaningful like "task_belongs_to_tgid". Can't come up with a good name for existing dup_file check, so I'd probably keep it as is. But also seems like there is same_thread_group() helper in include/linux/sched/signal.h, so let's look if we can use it, it seems like it's just comparing signal pointers (probably quite faster than what you are doing right now).

But looking at this some more made me realize that even if we specify pid (tgid in kernel terms) we are still going to iterate through all the tasks, essentially. Is that right? So TGID mode isn't great for speeding up, we should point out to users that if they want to iterate files of the process, they probably want to use TID mode and set tid to pid to use the early termination condition in TID.

It wasn't obvious to me until I re-read this patch like 3 times and wrote three different replies here :)

But then I also went looking at what procfs doing for /proc/<pid/task/* dirs. It does seem like there are faster ways to iterate all threads of a process. See next_tid() which uses next_thread(), etc. Can you please check those and see if we can have faster in-process iteration?

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
                        put_task_struct(task);
                        task = NULL;
                        ++*tid;
include/linux/bpf.h +1 -1
@@ -56,7 +73,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;

[...]

#6 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

Quoted 141 lines · Andrii Nakryiko
Quoted 119 lines · Kui-Feng Lee
Quoted 90 lines · Andrii Nakryiko
Quoted 42 lines · Kui-Feng Lee

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.

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

 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +4 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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.

Quoted 8 lines

+ *
+ * BPF_TASK_ITER_TID
+ *     Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ *     Iterate over reosurces of evevry task of a process / task
group.

typos: resources, every

Quoted 7 lines

+ */

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

[...]

Quoted 18 lines

        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;
                        goto retry;
-               } else if (skip_if_dup_files && !thread_group_leader(task) &&
-                          task->files == task->group_leader-

Quoted 1 line

files) {

+               } else if ((skip_if_dup_files && !thread_group_leader(task) &&
+                           task->files == task->group_leader-

Quoted 6 lines
Quoted 1 line

files) ||

+                          (common->type == BPF_TASK_ITER_TGID &&
+                           __task_pid_nr_ns(task, PIDTYPE_TGID,
common->ns) != common->pid)) {

Quoted 4 lines

it gets super hard to follow this logic, would a simple helper function to calculate this condition (and maybe some comments to explain the logic behind these checks?) make it a bit more readable?

!matched_task(task, common, skip_if_dup_file)?

bool matched_task(struct task_struct *task,
                  struct bpf_iter_seq_task_common *common, bool skip_if_dup_file) {
        /* Should not have the same 'files' if skip_if_dup_file is true
*/
        bool diff_files_if =
                !skip_if_dup_file || (thread_group_leader(task) && task->file != task->gorup_leader->fies); /* Should have the given tgid if the type is BPF_TASK_ITER_TGI
*/
        bool have_tgid_if =
                common->type != BPF_TASK_ITER_TGID || __task_pid_nr_ns(task, PIDTYPE_TGID, common->ns) == common->pid;
        return diff_files_if && have_tgid_if;
}

Quoted 2 lines

How about this?

Hm... "matched_task" doesn't mean much, tbh, so not really. I wanted to suggest having a separate helper just for your TGID check and call it something more meaningful like "task_belongs_to_tgid". Can't come up with a good name for existing dup_file check, so I'd probably keep it as is. But also seems like there is same_thread_group() helper in include/linux/sched/signal.h, so let's look if we can use it, it seems
like it's just comparing signal pointers (probably quite faster than what you are doing right now).

But looking at this some more made me realize that even if we specify pid (tgid in kernel terms) we are still going to iterate through all the tasks, essentially. Is that right? So TGID mode isn't great for speeding up, we should point out to users that if they want to iterate
files of the process, they probably want to use TID mode and set tid to pid to use the early termination condition in TID.

Quoted 9 lines

It wasn't obvious to me until I re-read this patch like 3 times and wrote three different replies here :)

But then I also went looking at what procfs doing for /proc/<pid/task/* dirs. It does seem like there are faster ways to iterate all threads of a process. See next_tid() which uses next_thread(), etc. Can you please check those and see if we can have faster in-process iteration?

I haven't notice this message until now.
It looks like very promise. I will add it to next version.

Quoted 20 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

                        put_task_struct(task);
                        task = NULL;
                        ++*tid;

include/linux/bpf.h +1 -1
@@ -56,7 +73,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;

[...]

#7 lore raw

Re: [PATCH bpf-next v6 1/4] bpf: Parameterize task iterators.

Quoted 153 lines · Kui-Feng Lee
Quoted 141 lines · Andrii Nakryiko
Quoted 119 lines · Kui-Feng Lee
Quoted 90 lines · Andrii Nakryiko
Quoted 42 lines · Kui-Feng Lee

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.

---
 include/linux/bpf.h            |  25 +++++++
 include/uapi/linux/bpf.h       |   6 ++
 kernel/bpf/task_iter.c         | 116
++++++++++++++++++++++++++---
----
 tools/include/uapi/linux/bpf.h |   6 ++
 4 files changed, 129 insertions(+), 24 deletions(-)
include/linux/bpf.h +4 -0
index 39bd36359c1e..59712dd917d8 100644
@@ -1729,8 +1729,33 @@ 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.

Quoted 8 lines

+ *
+ * BPF_TASK_ITER_TID
+ * Iterate over resources of a task/tid.
+ *
+ * BPF_TASK_ITER_TGID
+ * Iterate over reosurces of evevry task of a process /
task
group.

typos: resources, every

Quoted 7 lines

+ */

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

[...]

Quoted 18 lines
        rcu_read_lock();

retry:

include/linux/bpf.h +2 -3
-       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;
                        goto retry;
-               } else if (skip_if_dup_files &&

!thread_group_leader(task) &&
- task->files == task->group_leader-

Quoted 1 line

files) {

+ } else if ((skip_if_dup_files &&
!thread_group_leader(task) &&
+ task->files == task->group_leader-

Quoted 6 lines
Quoted 1 line

files) ||

+ (common->type == BPF_TASK_ITER_TGID
&&
+ __task_pid_nr_ns(task,
PIDTYPE_TGID,
common->ns) != common->pid)) {

Quoted 4 lines

it gets super hard to follow this logic, would a simple helper function to calculate this condition (and maybe some comments to explain the logic behind these checks?) make it a bit more readable?

!matched_task(task, common, skip_if_dup_file)?

bool matched_task(struct task_struct *task,

                  struct bpf_iter_seq_task_common *common,
                  bool skip_if_dup_file) {
        /* Should not have the same 'files' if skip_if_dup_file is
true
*/
        bool diff_files_if =
                !skip_if_dup_file ||
                (thread_group_leader(task) &&
                task->file != task->gorup_leader->fies);
        /* Should have the given tgid if the type is
BPF_TASK_ITER_TGI
*/
        bool have_tgid_if =
                common->type != BPF_TASK_ITER_TGID ||
                __task_pid_nr_ns(task, PIDTYPE_TGID,
                common->ns) == common->pid;
        return diff_files_if && have_tgid_if;
}
Quoted 2 lines

How about this?

Hm... "matched_task" doesn't mean much, tbh, so not really. I wanted to suggest having a separate helper just for your TGID check and call it something more meaningful like "task_belongs_to_tgid". Can't come up with a good name for existing dup_file check, so I'd probably keep it as is. But also seems like there is same_thread_group() helper in include/linux/sched/signal.h, so let's look if we can use it, it seems
like it's just comparing signal pointers (probably quite faster than what you are doing right now).

But looking at this some more made me realize that even if we specify pid (tgid in kernel terms) we are still going to iterate through all the tasks, essentially. Is that right? So TGID mode isn't great for speeding up, we should point out to users that if they want to iterate
files of the process, they probably want to use TID mode and set tid to pid to use the early termination condition in TID.

Quoted 9 lines

It wasn't obvious to me until I re-read this patch like 3 times and wrote three different replies here :)

But then I also went looking at what procfs doing for /proc/<pid/task/* dirs. It does seem like there are faster ways to iterate all threads of a process. See next_tid() which uses next_thread(), etc. Can you please check those and see if we can have faster in-process iteration?

Quoted 2 lines

I haven't notice this message until now.
It looks like very promise. I will add it to next version.

No worries. Thanks! Hopefully there is a way to make efficient per-process file and vma iteration.

Quoted 22 lines
Quoted 21 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines


                        put_task_struct(task);
                        task = NULL;
                        ++*tid;
include/linux/bpf.h +1 -1
@@ -56,7 +73,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;

[...]

#8 lore raw

PATCH v6 2/4 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       |  6 ++++++
 kernel/bpf/task_iter.c         | 18 ++++++++++++++++++
 tools/include/uapi/linux/bpf.h |  6 ++++++
 3 files changed, 30 insertions(+)
include/uapi/linux/bpf.h +6 -0
index 778fbf11aa00..6647e052dd00 100644
@@ -6170,6 +6170,12 @@ struct bpf_link_info {
 					__u32 map_id;
 				} map;
 			};
+			union {
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
+			};
 		} iter;
 		struct  {
 			__u32 netns_ino;
kernel/bpf/task_iter.c +18 -0
index 2f5fc6602917..927b3a1cf354 100644
@@ -596,6 +596,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,
@@ -606,6 +621,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 = {
@@ -627,6 +643,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 = {
@@ -648,6 +665,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 +6 -0
index 7a0268749a48..177722c5dd62 100644
@@ -6170,6 +6170,12 @@ struct bpf_link_info {
 					__u32 map_id;
 				} map;
 			};
+			union {
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
+			};
 		} iter;
 		struct  {
 			__u32 netns_ino;
Signature · 1 line
2.30.2
Acked-by on v6 2/4
#9 lore raw

Re: [PATCH bpf-next v6 2/4] bpf: Handle bpf_link_info for the parameterized task BPF iterators.

Quoted 4 lines · Kui-Feng Lee

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

But we are missing bpftool implementation which actually dumps the tid/pid here. So you need another bpftool patch to do this. See file tools/bpf/bpftool/link.c to see how map_id is printed.

Quoted 90 lines
---
  include/uapi/linux/bpf.h       |  6 ++++++
  kernel/bpf/task_iter.c         | 18 ++++++++++++++++++
  tools/include/uapi/linux/bpf.h |  6 ++++++
  3 files changed, 30 insertions(+)
include/uapi/linux/bpf.h +6 -0
index 778fbf11aa00..6647e052dd00 100644
@@ -6170,6 +6170,12 @@ struct bpf_link_info {
  					__u32 map_id;
  				} map;
  			};
+			union {
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
+			};
  		} iter;
  		struct  {
  			__u32 netns_ino;
kernel/bpf/task_iter.c +18 -0
index 2f5fc6602917..927b3a1cf354 100644
@@ -596,6 +596,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,
@@ -606,6 +621,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 = {
@@ -627,6 +643,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 = {
@@ -648,6 +665,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 +6 -0
index 7a0268749a48..177722c5dd62 100644
@@ -6170,6 +6170,12 @@ struct bpf_link_info {
  					__u32 map_id;
  				} map;
  			};
+			union {
+				struct {
+					__u32 tid;
+					__u32 pid;
+				} task;
+			};
  		} iter;
  		struct  {
  			__u32 netns_ino;
#10 lore raw

PATCH v6 3/4 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/.

---
 kernel/bpf/task_iter.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/bpf/task_iter.c +8 -0
index 927b3a1cf354..5303eddb264b 100644
@@ -611,6 +611,11 @@ 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%d\npid:\t%d\n", aux->task.type, aux->task.pid);
+}
+
 static struct bpf_iter_reg task_reg_info = {
 	.target			= "task",
 	.attach_target		= bpf_iter_attach_task,
@@ -622,6 +627,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 = {
@@ -644,6 +650,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 = {
@@ -666,6 +673,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
#11 lore raw

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

Quoted 2 lines · Kui-Feng Lee

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

Please show more information about what are dumped in the commit message.

Quoted 11 lines
---
  kernel/bpf/task_iter.c | 8 ++++++++
  1 file changed, 8 insertions(+)
kernel/bpf/task_iter.c +0 -0
index 927b3a1cf354..5303eddb264b 100644

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

Quoted 6 lines
kernel/bpf/task_iter.c +3 -0
@@ -611,6 +611,11 @@ 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%d\npid:\t%d\n", aux->task.type, aux->task.pid);

It would be good if we can print either 'tid: <number>' or 'pid: <number>' instead of just 'pid: <number>' in all cases. Also you don't need to print pid if it is 0 (to traverse all tasks).

We should use a string instead of an int for aux->task.type so user doesn't need to look at kernel source which they may not have.

Quoted 29 lines
kernel/bpf/task_iter.c +5 -0
+}
+
  static struct bpf_iter_reg task_reg_info = {
  	.target			= "task",
  	.attach_target		= bpf_iter_attach_task,
@@ -622,6 +627,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 = {
@@ -644,6 +650,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 = {
@@ -666,6 +673,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,
#12 lore raw

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

In reply to Kui-Feng Lee

Test iterators of vma, files, and tasks of 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       | 284 +++++++++++++++++-
 .../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   |   6 +-
 .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++
 6 files changed, 326 insertions(+), 19 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +268 -16
index e89685bd587c..c1ef7ffa6a43 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #include <test_progs.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 +15,7 @@
 #include "bpf_iter_udp4.skel.h"
 #include "bpf_iter_udp6.skel.h"
 #include "bpf_iter_unix.skel.h"
+#include "bpf_iter_uprobe_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 +45,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 +70,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 +174,149 @@ 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)
+{
+	DECLARE_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;
+	bool locked = false;
+	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);
+	if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+		goto done;
+	locked = true;
+
+	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
+		  "pthread_create"))
+		goto done;
+
+
+	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");
+	locked = false;
+	ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
+		     "pthread_join");
 
+done:
+	if (locked)
+		ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
 	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(void)
+{
+	DECLARE_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_tgid(void)
+{
+	DECLARE_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)
+{
+	DECLARE_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,15 +349,13 @@ 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)
 {
+	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
 	struct bpf_iter_task_file *skel;
+	union bpf_iter_link_info linfo;
 	pthread_t thread_id;
+	bool locked = false;
 	void *ret;
 
 	skel = bpf_iter_task_file__open_and_load();
@@ -229,19 +364,43 @@ static void test_task_file(void)
 
 	skel->bss->tgid = getpid();
 
-	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing, NULL),
+	if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+		goto done;
+	locked = true;
+
+	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
 		  "pthread_create"))
 		goto done;
 
-	do_dummy_read(skel->progs.dump_task_file);
+	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");
+	locked = false;
+	ASSERT_OK(pthread_join(thread_id, &ret), "pthread_join");
+	ASSERT_NULL(ret, "phtread_join");
 
 done:
+	if (locked)
+		ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
 	bpf_iter_task_file__destroy(skel);
 }
 
@@ -1249,7 +1408,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 +1420,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;
@@ -1306,6 +1466,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 +1488,91 @@ void test_bpf_sockmap_map_iter_fd(void)
 	bpf_iter_sockmap__destroy(skel);
 }
 
+static void test_task_vma(void)
+{
+	DECLARE_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_uprobe_offset_common(struct bpf_iter_attach_opts *opts, bool one_proc)
+{
+	struct bpf_iter_uprobe_offset *skel;
+	struct bpf_link *link;
+	char buf[16] = {};
+	int iter_fd, len;
+
+	skel = bpf_iter_uprobe_offset__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "bpf_iter_uprobe_offset__open_and_load"))
+		return;
+
+	skel->bss->pid = getpid();
+	skel->bss->address = (uintptr_t)trigger_func;
+
+	link = bpf_program__attach_iter(skel->progs.get_uprobe_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)
+		;
+	CHECK(len < 0, "read", "read failed: %s\n", strerror(errno));
+	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_uprobe_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_uprobe_offset_common(&opts, true);
+
+	linfo.task.pid = 0;
+	linfo.task.tid = getpid();
+	test_task_uprobe_offset_common(&opts, true);
+
+	test_task_uprobe_offset_common(NULL, false);
+}
+
 void test_bpf_iter(void)
 {
+	if (!ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init"))
+		return;
+
 	if (test__start_subtest("btf_id_or_null"))
 		test_btf_id_or_null();
 	if (test__start_subtest("ipv6_route"))
@@ -1337,6 +1583,10 @@ void test_bpf_iter(void)
 		test_bpf_map();
 	if (test__start_subtest("task"))
 		test_task();
+	if (test__start_subtest("task_tgid"))
+		test_task_tgid();
+	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 +1647,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("uprobe_offset"))
+		test_task_uprobe_offset();
 }
tools/testing/selftests/bpf/prog_tests/btf_dump.c +1 -1
index 5fce7008d1ff..32c34ce9cbeb 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,},}",
+			   "(union bpf_iter_link_info){.map = (struct){.map_fd = (__u32)1,},.task = (struct){.tid = (__u32)1,},}",
 			   { .map = { .map_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 +5 -1
index 4ea6a37d1345..44f4a31c2ddd 100644
@@ -20,6 +20,7 @@ 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;
 
 SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
 {
@@ -33,8 +34,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)
+			BPF_SEQ_PRINTF(seq, "unexpected task (%d != %d)", task->tgid, pid);
 		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_uprobe_offset.c new file +35 -0
new file mode 100644
index 000000000000..825ca86678bd
@@ -0,0 +1,35 @@
+// 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;
+
+SEC("iter/task_vma") int get_uprobe_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 v6 4/4] selftests/bpf: Test parameterized task BPF iterators.

Quoted 16 lines · Kui-Feng Lee

Test iterators of vma, files, and tasks of 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       | 284 +++++++++++++++++-
  .../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   |   6 +-
  .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++
  6 files changed, 326 insertions(+), 19 deletions(-)

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

[...]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +7 -0
+
+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);

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_bpf_map(void) · line 223

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +8 -0
+
+	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;
+	bool locked = false;

We can have a more 'kernel'-way implementation than using 'locked'.
if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex),
"pthread_mutex_lock"))

		goto done;
	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, 
NULL), "pthread_create"))
		goto unlock;

	...
unlock:
	ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
done:
	bpf_iter_task__destroy(skel);
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +24 -1
+	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);
+	if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+		goto done;
+	locked = true;
+
+	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
+		  "pthread_create"))
+		goto done;
+
+
+	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");
+	locked = false;
+	ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
+		     "pthread_join");
 
+done:
+	if (locked)
+		ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
  	bpf_iter_task__destroy(skel);
  }
 

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_bpf_map(void) · line 266

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +10 -0
+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(void)

test_task_tid?

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +11 -0
+{
+	DECLARE_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);
+

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_bpf_map(void) · line 287

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +10 -0
+	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_tgid(void)

test_task_pid?

Quoted 41 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +32 -2
+{
+	DECLARE_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)
+{
+	DECLARE_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,15 +349,13 @@ static void test_task_stack(void)
  	bpf_iter_task_stack__destroy(skel);
  }
 
-static void *do_nothing(void *arg)
-{

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_task_stack(void) · line 358

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +3 -3
-	pthread_exit(arg);
-}
-
  static void test_task_file(void)
  {
+	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
  	struct bpf_iter_task_file *skel;
+	union bpf_iter_link_info linfo;
  	pthread_t thread_id;
+	bool locked = false;

similar to the above, 'locked' variable can be removed by implementing an alternative approach.

Quoted 53 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +29 -5
  	void *ret;
 
  	skel = bpf_iter_task_file__open_and_load();
@@ -229,19 +364,43 @@ static void test_task_file(void)
 
  	skel->bss->tgid = getpid();
 
-	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing, NULL),
+	if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+		goto done;
+	locked = true;
+
+	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
  		  "pthread_create"))
  		goto done;
 
-	do_dummy_read(skel->progs.dump_task_file);
+	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");
+	locked = false;
+	ASSERT_OK(pthread_join(thread_id, &ret), "pthread_join");
+	ASSERT_NULL(ret, "phtread_join");
 
  done:
+	if (locked)
+		ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
  	bpf_iter_task_file__destroy(skel);
  }
 

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void str_strip_first_line(char *str) · line 1410

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +0 -0
@@ -1249,7 +1408,7 @@ static void str_strip_first_line(char *str)
  	*dst = '\0';
  }
 

[...

Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +8 -0
+
  void test_bpf_iter(void)
  {
+	if (!ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init"))
+		return;
+
  	if (test__start_subtest("btf_id_or_null"))
  		test_btf_id_or_null();
  	if (test__start_subtest("ipv6_route"))
@@ -1337,6 +1583,10 @@ void test_bpf_iter(void)
  		test_bpf_map();
  	if (test__start_subtest("task"))
  		test_task();
+	if (test__start_subtest("task_tgid"))
+		test_task_tgid();
+	if (test__start_subtest("task_pidfd"))
+		test_task_pidfd();
  	if (test__start_subtest("task_sleepable"))
  		test_task_sleepable();
  	if (test__start_subtest("task_stack"))

tools/testing/selftests/bpf/prog_tests/bpf_iter.c void test_bpf_iter(void) · line 1651

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +2 -0
@@ -1397,4 +1647,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("uprobe_offset"))
+		test_task_uprobe_offset();

uprobe_offset -> vma_offset? See below.

Quoted 88 lines
(quoted diff) +0 -0
  }
tools/testing/selftests/bpf/prog_tests/btf_dump.c +1 -1
index 5fce7008d1ff..32c34ce9cbeb 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,},}",
+			   "(union bpf_iter_link_info){.map = (struct){.map_fd = (__u32)1,},.task = (struct){.tid = (__u32)1,},}",
  			   { .map = { .map_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 +1 -0
index 4ea6a37d1345..44f4a31c2ddd 100644
@@ -20,6 +20,7 @@ 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;
 
  SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx)
  {

tools/testing/selftests/bpf/progs/bpf_iter_task_vma.c SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx) · line 39

Quoted 7 lines
tools/testing/selftests/bpf/progs/bpf_iter_task_vma.c +3 -1
@@ -33,8 +34,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)
+			BPF_SEQ_PRINTF(seq, "unexpected task (%d != %d)", task->tgid, pid);

Let us change this to an error code like
__u32 one_task_error = 0;
...

if (task->tgid != pid) {
	if (one_task)
		one_task_error = 1;
	return 0;
}

In bpf_iter.c, we can assert one_task_error must be 0?

tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_task_vma.c +1 -0
  		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_uprobe_offset.c new file +0 -0
new file mode 100644
index 000000000000..825ca86678bd

Maybe change file name to bpf_iter_vma_offset so we know the test is related 'iter/task_vma'? the offset can be used by uprobe, but it can be used for other purposes, e.g., symbolization.

Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +4 -0
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+#include "bpf_iter.h"
+#include <bpf/bpf_helpers.h>

tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c line 14

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +10 -0
+
+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;
+
+SEC("iter/task_vma") int get_uprobe_offset(struct bpf_iter__task_vma *ctx)

get_vma_offset?

Quoted 21 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +21 -0
+{
+	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;
+}
#14 lore raw

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

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

Test iterators of vma, files, and tasks of 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       | 284 +++++++++++++++++-
 .../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   |   6 +-
 .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++
 6 files changed, 326 insertions(+), 19 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c

[...]

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_bpf_map(void) · line 238

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +9 -1
-       do_dummy_read(skel->progs.dump_task);
+       if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+               goto done;
+       locked = true;
+
+       if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
+                 "pthread_create"))
+               goto done;
+
+

extra empty line

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +4 -0
+       skel->bss->tid = getpid();
+
+       do_dummy_read_opts(skel->progs.dump_task, opts);
+

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_bpf_map(void) · line 252

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +9 -0
+       *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");
+       locked = false;
+       ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
+                    "pthread_join");
 
+done:
+       if (locked)

it's a bit of an overkill to expect and handle that pthread_mutex_lock() might fail, I'd remove those asserts and locked flag, just assume that lock works (if it's not, it's either test bug and would be caught early, or something is very broken in the system anyway)

Quoted 4 lines
+               ASSERT_OK(pthread_mutex_unlock(&do_nothing_mutex), "pthread_mutex_unlock");
        bpf_iter_task__destroy(skel);

}

[...]

Quoted 5 lines

static void test_task_sleepable(void)

 {
        struct bpf_iter_task *skel;
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +0 -0
@@ -212,15 +349,13 @@ static void test_task_stack(void)
        bpf_iter_task_stack__destroy(skel);

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_task_stack(void) · line 354

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +1 -5
 }
 
-static void *do_nothing(void *arg)
-{
-       pthread_exit(arg);
-}
-
 static void test_task_file(void)
 {
+       DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);

DECLARE_LIBBPF_OPTS is discouraged, best to use shorter LIBBPF_OPTS

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +2 -0
        struct bpf_iter_task_file *skel;
+       union bpf_iter_link_info linfo;
        pthread_t thread_id;
+       bool locked = false;
        void *ret;
 
        skel = bpf_iter_task_file__open_and_load();

tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_task_file(void) · line 369

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +3 -1
@@ -229,19 +364,43 @@ static void test_task_file(void)
 
        skel->bss->tgid = getpid();
 
-       if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing, NULL),
+       if (!ASSERT_OK(pthread_mutex_lock(&do_nothing_mutex), "pthread_mutex_lock"))
+               goto done;
+       locked = true;

same about failing mutex_lock, it shouldn't and it's fair to expect that it won't

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +6 -1
+
+       if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
                  "pthread_create"))
                goto done;
 
-       do_dummy_read(skel->progs.dump_task_file);
+       memset(&linfo, 0, sizeof(linfo));
+       linfo.task.tid = getpid();
+       opts.link_info = &linfo;
+       opts.link_info_len = sizeof(linfo);

[...]

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

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +11 -0
+       link = bpf_program__attach_iter(skel->progs.get_uprobe_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)
+               ;
+       CHECK(len < 0, "read", "read failed: %s\n", strerror(errno));

no checks, please

Quoted 29 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +29 -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_uprobe_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_uprobe_offset_common(&opts, true);
+
+       linfo.task.pid = 0;
+       linfo.task.tid = getpid();

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +8 -0
+       test_task_uprobe_offset_common(&opts, true);
+
+       test_task_uprobe_offset_common(NULL, false);
+}
+
 void test_bpf_iter(void)
 {
+       if (!ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init"))
+               return;
+

ditto, too paranoid, IMO

Quoted 3 lines
        if (test__start_subtest("btf_id_or_null"))
                test_btf_id_or_null();
        if (test__start_subtest("ipv6_route"))

[...]

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +4 -0
new file mode 100644
index 000000000000..825ca86678bd
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
+#include "bpf_iter.h"
+#include <bpf/bpf_helpers.h>

tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c line 14

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +10 -0
+
+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;
+
+SEC("iter/task_vma") int get_uprobe_offset(struct bpf_iter__task_vma *ctx)

please keep SEC() on separate line

Quoted 7 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +7 -0
+{
+       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_uprobe_offset.c line 31

Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_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);

it's best not to assume page_size is 4K, you can pass actual value through global variable from user-space (we've previously fixed a bunch of tests with fixed page_size assumption as they break some platforms, let's not regress that)

Quoted 7 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +4 -1
+               BPF_SEQ_PRINTF(seq, "OK\n");
+       }
+       return 0;
+}
--

2.30.2

#15 lore raw

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

Quoted 20 lines · Andrii Nakryiko
Quoted 18 lines · Kui-Feng Lee

Test iterators of vma, files, and tasks of 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       | 284
+++++++++++++++++-
 .../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   |   6 +- .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++

 6 files changed, 326 insertions(+), 19 deletions(-)

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

[...]

Quoted 9 lines
Quoted 7 lines

+
 void test_bpf_iter(void)
 {
+       if (!ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init"))
+               return;
+

ditto, too paranoid, IMO

Right, for test cases, we can ease checks.
I would like to have something like

    INFALLIBLE(pthread_mutex_init(......)...);

It will crash at the first point.

Quoted 48 lines
Quoted 4 lines

        if (test__start_subtest("btf_id_or_null")) test_btf_id_or_null();
        if (test__start_subtest("ipv6_route"))

[...]

Quoted 22 lines

diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c b/tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file mode 100644
index 000000000000..825ca86678bd

tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +14 -0
@@ -0,0 +1,35 @@
+// 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;
+
+SEC("iter/task_vma") int get_uprobe_offset(struct
Quoted 1 line

bpf_iter__task_vma *ctx)

please keep SEC() on separate line

Quoted 15 lines
tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c new file +15 -0
+{
+       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;
+
Quoted 8 lines
Quoted 3 lines

+       if (vma->vm_start <= address && vma->vm_end > address) { +               offset = address - vma->vm_start + (vma->vm_pgoff << 12);

it's best not to assume page_size is 4K, you can pass actual value through global variable from user-space (we've previously fixed a bunch of tests with fixed page_size assumption as they break some platforms, let's not regress that)

A getpagesize() helper would help :)

Quoted 8 lines
Quoted 8 lines

+               BPF_SEQ_PRINTF(seq, "OK\n");
+       }
+       return 0;
+}
--
2.30.2

#16 lore raw
In reply to Kui-Feng Lee
Quoted 17 lines · Kui-Feng Lee

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

Quoted 6 lines

- Provide the arguments of parameterized task iterators in bpf_link_info.

Differences from v5:

- Use user-speace tid/pid terminologies in bpf_iter_link_info *** BLURB HERE ***

BLURB HERE :)

Quoted 7 lines

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)

Quoted 9 lines

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

We normally carry over entire change history across revisions, so we don't have to jump through multiple links to see what happened earlier. Please consider preserving all the changes in one place for next revision.

Quoted 28 lines

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 (4):
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.

 include/linux/bpf.h                           |  25 ++
 include/uapi/linux/bpf.h                      |  12 +
 kernel/bpf/task_iter.c                        | 142 +++++++--
 tools/include/uapi/linux/bpf.h                |  12 +
 .../selftests/bpf/prog_tests/bpf_iter.c       | 284 +++++++++++++++++-
 .../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   |   6 +-
 .../bpf/progs/bpf_iter_uprobe_offset.c        |  35 +++
 10 files changed, 493 insertions(+), 43 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_uprobe_offset.c

--
2.30.2