1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
4 #include <linux/btf_ids.h>
5 #include <linux/error-injection.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/percpu-defs.h>
9 #include <linux/sysfs.h>
10 #include <linux/tracepoint.h>
11 #include "bpf_testmod.h"
13 #define CREATE_TRACE_POINTS
14 #include "bpf_testmod-events.h"
16 typedef int (*func_proto_typedef)(long);
17 typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
18 typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
20 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
21 long bpf_testmod_test_struct_arg_result;
23 struct bpf_testmod_struct_arg_1 {
26 struct bpf_testmod_struct_arg_2 {
32 bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
33 bpf_testmod_test_struct_arg_result = a.a + a.b + b + c;
34 return bpf_testmod_test_struct_arg_result;
38 bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
39 bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
40 return bpf_testmod_test_struct_arg_result;
44 bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
45 bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
46 return bpf_testmod_test_struct_arg_result;
50 bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
51 int c, int d, struct bpf_testmod_struct_arg_2 e) {
52 bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
53 return bpf_testmod_test_struct_arg_result;
57 bpf_testmod_test_struct_arg_5(void) {
58 bpf_testmod_test_struct_arg_result = 1;
59 return bpf_testmod_test_struct_arg_result;
63 bpf_testmod_test_mod_kfunc(int i)
65 *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
68 struct bpf_testmod_btf_type_tag_1 {
72 struct bpf_testmod_btf_type_tag_2 {
73 struct bpf_testmod_btf_type_tag_1 __user *p;
76 struct bpf_testmod_btf_type_tag_3 {
77 struct bpf_testmod_btf_type_tag_1 __percpu *p;
81 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
82 BTF_TYPE_EMIT(func_proto_typedef);
83 BTF_TYPE_EMIT(func_proto_typedef_nested1);
84 BTF_TYPE_EMIT(func_proto_typedef_nested2);
89 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
94 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
99 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
103 noinline int bpf_testmod_loop_test(int n)
107 /* the primary goal of this test is to test LBR. Create a lot of
108 * branches in the function, so we can catch it easily.
110 for (i = 0; i < n; i++)
115 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
117 static struct file f = {};
120 case 1: return (void *)EINVAL; /* user addr */
121 case 2: return (void *)0xcafe4a11; /* user addr */
122 case 3: return (void *)-EINVAL; /* canonical, but invalid */
123 case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
124 case 5: return (void *)~(1ull << 30); /* trigger extable */
125 case 6: return &f; /* valid addr */
126 case 7: return (void *)((long)&f | 1); /* kernel tricks */
127 default: return NULL;
131 noinline int bpf_testmod_fentry_test1(int a)
136 noinline int bpf_testmod_fentry_test2(int a, u64 b)
141 noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
146 int bpf_testmod_fentry_ok;
149 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
150 struct bin_attribute *bin_attr,
151 char *buf, loff_t off, size_t len)
153 struct bpf_testmod_test_read_ctx ctx = {
158 struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
159 struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
162 while (bpf_testmod_return_ptr(i))
165 (void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
166 (void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
167 (void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
168 (void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
169 (void)bpf_testmod_test_struct_arg_5();
171 /* This is always true. Use the check to make sure the compiler
172 * doesn't remove bpf_testmod_loop_test.
174 if (bpf_testmod_loop_test(101) > 100)
175 trace_bpf_testmod_test_read(current, &ctx);
177 /* Magic number to enable writable tp */
179 struct bpf_testmod_test_writable_ctx writable = {
182 trace_bpf_testmod_test_writable_bare(&writable);
183 if (writable.early_ret)
184 return snprintf(buf, len, "%d\n", writable.val);
187 if (bpf_testmod_fentry_test1(1) != 2 ||
188 bpf_testmod_fentry_test2(2, 3) != 5 ||
189 bpf_testmod_fentry_test3(4, 5, 6) != 15)
192 bpf_testmod_fentry_ok = 1;
194 return -EIO; /* always fail */
196 EXPORT_SYMBOL(bpf_testmod_test_read);
197 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
200 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
201 struct bin_attribute *bin_attr,
202 char *buf, loff_t off, size_t len)
204 struct bpf_testmod_test_write_ctx ctx = {
210 trace_bpf_testmod_test_write_bare(current, &ctx);
212 return -EIO; /* always fail */
214 EXPORT_SYMBOL(bpf_testmod_test_write);
215 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
217 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
218 .attr = { .name = "bpf_testmod", .mode = 0666, },
219 .read = bpf_testmod_test_read,
220 .write = bpf_testmod_test_write,
223 BTF_SET8_START(bpf_testmod_check_kfunc_ids)
224 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
225 BTF_SET8_END(bpf_testmod_check_kfunc_ids)
227 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
228 .owner = THIS_MODULE,
229 .set = &bpf_testmod_check_kfunc_ids,
232 extern int bpf_fentry_test1(int a);
234 static int bpf_testmod_init(void)
238 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
241 if (bpf_fentry_test1(0) < 0)
243 return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
246 static void bpf_testmod_exit(void)
248 return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
251 module_init(bpf_testmod_init);
252 module_exit(bpf_testmod_exit);
254 MODULE_AUTHOR("Andrii Nakryiko");
255 MODULE_DESCRIPTION("BPF selftests module");
256 MODULE_LICENSE("Dual BSD/GPL");