Skip to content

Commit e0e1a7a

Browse files
davemarchevskyanakryiko
authored andcommitted
selftests/bpf: Add tests for open-coded task_vma iter
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@fb.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20231013204426.1074286-5-davemarchevsky@fb.com
1 parent 4ac4546 commit e0e1a7a

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ extern void *bpf_percpu_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
159159
*/
160160
extern void bpf_percpu_obj_drop_impl(void *kptr, void *meta) __ksym;
161161

162+
struct bpf_iter_task_vma;
163+
164+
extern int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
165+
struct task_struct *task,
166+
unsigned long addr) __ksym;
167+
extern struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it) __ksym;
168+
extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
169+
162170
/* Convenience macro to wrap over bpf_obj_drop_impl */
163171
#define bpf_percpu_obj_drop(kptr) bpf_percpu_obj_drop_impl(kptr, NULL)
164172

tools/testing/selftests/bpf/prog_tests/iters.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "iters_looping.skel.h"
99
#include "iters_num.skel.h"
1010
#include "iters_testmod_seq.skel.h"
11+
#include "iters_task_vma.skel.h"
1112

1213
static void subtest_num_iters(void)
1314
{
@@ -90,6 +91,61 @@ static void subtest_testmod_seq_iters(void)
9091
iters_testmod_seq__destroy(skel);
9192
}
9293

94+
static void subtest_task_vma_iters(void)
95+
{
96+
unsigned long start, end, bpf_iter_start, bpf_iter_end;
97+
struct iters_task_vma *skel;
98+
char rest_of_line[1000];
99+
unsigned int seen;
100+
FILE *f = NULL;
101+
int err;
102+
103+
skel = iters_task_vma__open_and_load();
104+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
105+
return;
106+
107+
skel->bss->target_pid = getpid();
108+
109+
err = iters_task_vma__attach(skel);
110+
if (!ASSERT_OK(err, "skel_attach"))
111+
goto cleanup;
112+
113+
getpgid(skel->bss->target_pid);
114+
iters_task_vma__detach(skel);
115+
116+
if (!ASSERT_GT(skel->bss->vmas_seen, 0, "vmas_seen_gt_zero"))
117+
goto cleanup;
118+
119+
f = fopen("/proc/self/maps", "r");
120+
if (!ASSERT_OK_PTR(f, "proc_maps_fopen"))
121+
goto cleanup;
122+
123+
seen = 0;
124+
while (fscanf(f, "%lx-%lx %[^\n]\n", &start, &end, rest_of_line) == 3) {
125+
/* [vsyscall] vma isn't _really_ part of task->mm vmas.
126+
* /proc/PID/maps returns it when out of vmas - see get_gate_vma
127+
* calls in fs/proc/task_mmu.c
128+
*/
129+
if (strstr(rest_of_line, "[vsyscall]"))
130+
continue;
131+
132+
bpf_iter_start = skel->bss->vm_ranges[seen].vm_start;
133+
bpf_iter_end = skel->bss->vm_ranges[seen].vm_end;
134+
135+
ASSERT_EQ(bpf_iter_start, start, "vma->vm_start match");
136+
ASSERT_EQ(bpf_iter_end, end, "vma->vm_end match");
137+
seen++;
138+
}
139+
140+
if (!ASSERT_EQ(skel->bss->vmas_seen, seen, "vmas_seen_eq"))
141+
goto cleanup;
142+
143+
cleanup:
144+
if (f)
145+
fclose(f);
146+
iters_task_vma__destroy(skel);
147+
}
148+
93149
void test_iters(void)
94150
{
95151
RUN_TESTS(iters_state_safety);
@@ -103,4 +159,6 @@ void test_iters(void)
103159
subtest_num_iters();
104160
if (test__start_subtest("testmod_seq"))
105161
subtest_testmod_seq_iters();
162+
if (test__start_subtest("task_vma"))
163+
subtest_task_vma_iters();
106164
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include "bpf_experimental.h"
6+
#include <bpf/bpf_helpers.h>
7+
#include "bpf_misc.h"
8+
9+
pid_t target_pid = 0;
10+
unsigned int vmas_seen = 0;
11+
12+
struct {
13+
__u64 vm_start;
14+
__u64 vm_end;
15+
} vm_ranges[1000];
16+
17+
SEC("raw_tp/sys_enter")
18+
int iter_task_vma_for_each(const void *ctx)
19+
{
20+
struct task_struct *task = bpf_get_current_task_btf();
21+
struct vm_area_struct *vma;
22+
unsigned int seen = 0;
23+
24+
if (task->pid != target_pid)
25+
return 0;
26+
27+
if (vmas_seen)
28+
return 0;
29+
30+
bpf_for_each(task_vma, vma, task, 0) {
31+
if (seen >= 1000)
32+
break;
33+
34+
vm_ranges[seen].vm_start = vma->vm_start;
35+
vm_ranges[seen].vm_end = vma->vm_end;
36+
seen++;
37+
}
38+
39+
vmas_seen = seen;
40+
return 0;
41+
}
42+
43+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)