xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #include "bpf_iter.h"
4 #include <bpf/bpf_helpers.h>
5 
6 char _license[] SEC("license") = "GPL";
7 
8 __u32 unique_tgid_cnt = 0;
9 uintptr_t address = 0;
10 uintptr_t offset = 0;
11 __u32 last_tgid = 0;
12 __u32 pid = 0;
13 __u32 page_shift = 0;
14 
15 SEC("iter/task_vma")
16 int get_vma_offset(struct bpf_iter__task_vma *ctx)
17 {
18 	struct vm_area_struct *vma = ctx->vma;
19 	struct seq_file *seq = ctx->meta->seq;
20 	struct task_struct *task = ctx->task;
21 
22 	if (task == NULL || vma == NULL)
23 		return 0;
24 
25 	if (last_tgid != task->tgid)
26 		unique_tgid_cnt++;
27 	last_tgid = task->tgid;
28 
29 	if (task->tgid != pid)
30 		return 0;
31 
32 	if (vma->vm_start <= address && vma->vm_end > address) {
33 		offset = address - vma->vm_start + (vma->vm_pgoff << page_shift);
34 		BPF_SEQ_PRINTF(seq, "OK\n");
35 	}
36 	return 0;
37 }
38