selftests/bpf: Add tp_btf CO-RE reloc test for modules
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / progs / test_core_reloc_module.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_core_read.h>
7 #include <bpf/bpf_tracing.h>
8
9 char _license[] SEC("license") = "GPL";
10
11 struct bpf_testmod_test_read_ctx {
12         /* field order is mixed up */
13         size_t len;
14         char *buf;
15         loff_t off;
16 } __attribute__((preserve_access_index));
17
18 struct {
19         char in[256];
20         char out[256];
21         bool skip;
22         uint64_t my_pid_tgid;
23 } data = {};
24
25 struct core_reloc_module_output {
26         long long len;
27         long long off;
28         int read_ctx_sz;
29         bool read_ctx_exists;
30         bool buf_exists;
31         bool len_exists;
32         bool off_exists;
33         /* we have test_progs[-flavor], so cut flavor part */
34         char comm[sizeof("test_progs")];
35         int comm_len;
36 };
37
38 SEC("raw_tp/bpf_testmod_test_read")
39 int BPF_PROG(test_core_module_probed,
40              struct task_struct *task,
41              struct bpf_testmod_test_read_ctx *read_ctx)
42 {
43         struct core_reloc_module_output *out = (void *)&data.out;
44         __u64 pid_tgid = bpf_get_current_pid_tgid();
45         __u32 real_tgid = (__u32)(pid_tgid >> 32);
46         __u32 real_pid = (__u32)pid_tgid;
47
48         if (data.my_pid_tgid != pid_tgid)
49                 return 0;
50
51         if (BPF_CORE_READ(task, pid) != real_pid || BPF_CORE_READ(task, tgid) != real_tgid)
52                 return 0;
53
54         out->len = BPF_CORE_READ(read_ctx, len);
55         out->off = BPF_CORE_READ(read_ctx, off);
56
57         out->read_ctx_sz = bpf_core_type_size(struct bpf_testmod_test_read_ctx);
58         out->read_ctx_exists = bpf_core_type_exists(struct bpf_testmod_test_read_ctx);
59         out->buf_exists = bpf_core_field_exists(read_ctx->buf);
60         out->off_exists = bpf_core_field_exists(read_ctx->off);
61         out->len_exists = bpf_core_field_exists(read_ctx->len);
62
63         out->comm_len = BPF_CORE_READ_STR_INTO(&out->comm, task, comm);
64
65         return 0;
66 }
67
68 SEC("tp_btf/bpf_testmod_test_read")
69 int BPF_PROG(test_core_module_direct,
70              struct task_struct *task,
71              struct bpf_testmod_test_read_ctx *read_ctx)
72 {
73         struct core_reloc_module_output *out = (void *)&data.out;
74         __u64 pid_tgid = bpf_get_current_pid_tgid();
75         __u32 real_tgid = (__u32)(pid_tgid >> 32);
76         __u32 real_pid = (__u32)pid_tgid;
77
78         if (data.my_pid_tgid != pid_tgid)
79                 return 0;
80
81         if (task->pid != real_pid || task->tgid != real_tgid)
82                 return 0;
83
84         out->len = read_ctx->len;
85         out->off = read_ctx->off;
86
87         out->read_ctx_sz = bpf_core_type_size(struct bpf_testmod_test_read_ctx);
88         out->read_ctx_exists = bpf_core_type_exists(struct bpf_testmod_test_read_ctx);
89         out->buf_exists = bpf_core_field_exists(read_ctx->buf);
90         out->off_exists = bpf_core_field_exists(read_ctx->off);
91         out->len_exists = bpf_core_field_exists(read_ctx->len);
92
93         out->comm_len = BPF_CORE_READ_STR_INTO(&out->comm, task, comm);
94
95         return 0;
96 }