xref: /linux/tools/testing/selftests/bpf/progs/test_module_attach.c (revision f5ad4101009e7f5f5984ffea6923d4fcd470932a)
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 "../test_kmods/bpf_testmod.h"
9 
10 __u32 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 	sz = BPF_CORE_READ(read_ctx, len);
17 	return 0;
18 }
19 
20 SEC("?raw_tp/bpf_testmod_test_write_bare_tp")
21 int BPF_PROG(handle_raw_tp_bare,
22 	     struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx)
23 {
24 	sz = BPF_CORE_READ(write_ctx, len);
25 	return 0;
26 }
27 
28 int raw_tp_writable_bare_in_val = 0;
29 int raw_tp_writable_bare_early_ret = 0;
30 int raw_tp_writable_bare_out_val = 0;
31 
32 SEC("?raw_tp.w/bpf_testmod_test_writable_bare_tp")
33 int BPF_PROG(handle_raw_tp_writable_bare,
34 	     struct bpf_testmod_test_writable_ctx *writable)
35 {
36 	raw_tp_writable_bare_in_val = writable->val;
37 	writable->early_ret = raw_tp_writable_bare_early_ret;
38 	writable->val = raw_tp_writable_bare_out_val;
39 	return 0;
40 }
41 
42 SEC("?tp_btf/bpf_testmod_test_read")
43 int BPF_PROG(handle_tp_btf,
44 	     struct task_struct *task, struct bpf_testmod_test_read_ctx *read_ctx)
45 {
46 	sz = read_ctx->len;
47 	return 0;
48 }
49 
50 SEC("?fentry/bpf_testmod_test_read")
51 int BPF_PROG(handle_fentry,
52 	     struct file *file, struct kobject *kobj,
53 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
54 {
55 	sz = len;
56 	return 0;
57 }
58 
59 SEC("?fentry")
60 int BPF_PROG(handle_fentry_manual,
61 	     struct file *file, struct kobject *kobj,
62 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
63 {
64 	sz = len;
65 	return 0;
66 }
67 
68 SEC("?fentry/bpf_testmod:bpf_testmod_test_read")
69 int BPF_PROG(handle_fentry_explicit,
70 	     struct file *file, struct kobject *kobj,
71 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
72 {
73 	sz = len;
74 	return 0;
75 }
76 
77 
78 SEC("?fentry")
79 int BPF_PROG(handle_fentry_explicit_manual,
80 	     struct file *file, struct kobject *kobj,
81 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
82 {
83 	sz = len;
84 	return 0;
85 }
86 
87 int retval = 0;
88 
89 SEC("?fexit/bpf_testmod_test_read")
90 int BPF_PROG(handle_fexit,
91 	     struct file *file, struct kobject *kobj,
92 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len,
93 	     int ret)
94 {
95 	sz = len;
96 	retval = ret;
97 	return 0;
98 }
99 
100 SEC("?fexit/bpf_testmod_return_ptr")
101 int BPF_PROG(handle_fexit_ret, int arg, struct file *ret)
102 {
103 	long buf = 0;
104 
105 	bpf_probe_read_kernel(&buf, 8, ret);
106 	bpf_probe_read_kernel(&buf, 8, (char *)ret + 256);
107 	*(volatile int *)ret;
108 	*(volatile int *)&ret->f_mode;
109 	return 0;
110 }
111 
112 SEC("?fmod_ret/bpf_testmod_test_read")
113 int BPF_PROG(handle_fmod_ret,
114 	     struct file *file, struct kobject *kobj,
115 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
116 {
117 	sz = len;
118 	return 0; /* don't override the exit code */
119 }
120 
121 SEC("?kprobe.multi/bpf_testmod_test_read")
122 int BPF_PROG(kprobe_multi)
123 {
124 	return 0;
125 }
126 
127 char _license[] SEC("license") = "GPL";
128