On Tue, Oct 10, 2023 at 12:00 PM Dave Marchevsky <davemarchevsky@xxxxxx> wrote: > > The open-coded task_vma iter added earlier in this series allows for > natural iteration over a task's vmas using existing open-coded iter > infrastructure, specifically bpf_for_each. > > This patch adds a test demonstrating this pattern and validating > correctness. The vma->vm_start and vma->vm_end addresses of the first > 1000 vmas are recorded and compared to /proc/PID/maps output. As > expected, both see the same vmas and addresses - with the exception of > the [vsyscall] vma - which is explained in a comment in the prog_tests > program. > > Signed-off-by: Dave Marchevsky <davemarchevsky@xxxxxx> > --- > .../testing/selftests/bpf/bpf_experimental.h | 8 +++ > .../testing/selftests/bpf/prog_tests/iters.c | 58 +++++++++++++++++++ > .../selftests/bpf/progs/iters_task_vma.c | 46 +++++++++++++++ > 3 files changed, 112 insertions(+) > create mode 100644 tools/testing/selftests/bpf/progs/iters_task_vma.c > LGTM, I was going to apply, but CI is angry at you ([0]), please take a look. Minor nits below, don't matter much. [0] https://github.com/kernel-patches/bpf/actions/runs/6489676263/job/17624379177 [...] > diff --git a/tools/testing/selftests/bpf/progs/iters_task_vma.c b/tools/testing/selftests/bpf/progs/iters_task_vma.c > new file mode 100644 > index 000000000000..e3759e425420 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/iters_task_vma.c > @@ -0,0 +1,46 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ > + > +#include <limits.h> > +#include <linux/errno.h> do you need these two includes? > +#include "vmlinux.h" > +#include "bpf_experimental.h" > +#include <bpf/bpf_helpers.h> > +#include "bpf_misc.h" > + > +pid_t target_pid = 0; > +unsigned int vmas_seen = 0; > + > +struct { > + __u64 vm_start; > + __u64 vm_end; > +} vm_ranges[1000]; > + > +SEC("raw_tp/sys_enter") > +int iter_task_vma_for_each(const void *ctx) > +{ > + struct task_struct *task = bpf_get_current_task_btf(); > + struct vm_area_struct *vma; > + unsigned int seen = 0; > + > + if (task->pid != target_pid) > + return 0; > + > + if (vmas_seen) > + return 0; > + > + bpf_for_each(task_vma, vma, task, 0) { > + if (seen >= 1000) > + break; > + > + vm_ranges[seen].vm_start = vma->vm_start; > + vm_ranges[seen].vm_end = vma->vm_end; > + seen++; > + } > + > + if (!vmas_seen) > + vmas_seen = seen; you checked if (vmas_seen) above, so this check is probably unnecessary, and presumably we'll arrive at the same seen even if we run this many times for the same PID > + return 0; > +} > + > +char _license[] SEC("license") = "GPL"; > -- > 2.34.1 >