selftests/bpf: Add fentry/fexit/fmod_ret selftest for kernel module
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / progs / test_module_attach.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_tracing.h>
7 #include <bpf/bpf_core_read.h>
8 #include "../bpf_testmod/bpf_testmod.h"
9
10 __u32 raw_tp_read_sz = 0;
11
12 SEC("raw_tp/bpf_testmod_test_read")
13 int BPF_PROG(handle_raw_tp,
14              struct task_struct *task, struct bpf_testmod_test_read_ctx *read_ctx)
15 {
16         raw_tp_read_sz = BPF_CORE_READ(read_ctx, len);
17         return 0;
18 }
19
20 __u32 tp_btf_read_sz = 0;
21
22 SEC("tp_btf/bpf_testmod_test_read")
23 int BPF_PROG(handle_tp_btf,
24              struct task_struct *task, struct bpf_testmod_test_read_ctx *read_ctx)
25 {
26         tp_btf_read_sz = read_ctx->len;
27         return 0;
28 }
29
30 __u32 fentry_read_sz = 0;
31
32 SEC("fentry/bpf_testmod_test_read")
33 int BPF_PROG(handle_fentry,
34              struct file *file, struct kobject *kobj,
35              struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
36 {
37         fentry_read_sz = len;
38         return 0;
39 }
40
41 __u32 fexit_read_sz = 0;
42 int fexit_ret = 0;
43
44 SEC("fexit/bpf_testmod_test_read")
45 int BPF_PROG(handle_fexit,
46              struct file *file, struct kobject *kobj,
47              struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len,
48              int ret)
49 {
50         fexit_read_sz = len;
51         fexit_ret = ret;
52         return 0;
53 }
54
55 __u32 fmod_ret_read_sz = 0;
56
57 SEC("fmod_ret/bpf_testmod_test_read")
58 int BPF_PROG(handle_fmod_ret,
59              struct file *file, struct kobject *kobj,
60              struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
61 {
62         fmod_ret_read_sz = len;
63         return 0; /* don't override the exit code */
64 }
65
66 char _license[] SEC("license") = "GPL";