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
6char _license[] SEC("license") = "GPL";
7
8__u32 unique_tgid_cnt = 0;
9uintptr_t address = 0;
10uintptr_t offset = 0;
11__u32 last_tgid = 0;
12__u32 pid = 0;
13__u32 page_shift = 0;
14
15SEC("iter/task_vma")
16int 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

source code of linux/tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c