xref: /linux/tools/testing/selftests/bpf/progs/iters_task_vma.c (revision 119ff04864a24470b1e531bb53e5c141aa8fefb0)
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 (bpf_cmp_unlikely(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";
44