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