xref: /linux/tools/testing/selftests/bpf/progs/iters_task_vma.c (revision 42422993cf28d456778ee9168d73758ec037cd51)
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 		barrier_var(seen);
34 
35 		vm_ranges[seen].vm_start = vma->vm_start;
36 		vm_ranges[seen].vm_end = vma->vm_end;
37 		seen++;
38 	}
39 
40 	vmas_seen = seen;
41 	return 0;
42 }
43 
44 char _license[] SEC("license") = "GPL";
45