Merge branch 'io_uring-zerocopy-send' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / bpf_testmod / bpf_testmod.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <linux/btf.h>
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"
12
13 #define CREATE_TRACE_POINTS
14 #include "bpf_testmod-events.h"
15
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);
19
20 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
21
22 noinline void
23 bpf_testmod_test_mod_kfunc(int i)
24 {
25         *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
26 }
27
28 struct bpf_testmod_btf_type_tag_1 {
29         int a;
30 };
31
32 struct bpf_testmod_btf_type_tag_2 {
33         struct bpf_testmod_btf_type_tag_1 __user *p;
34 };
35
36 struct bpf_testmod_btf_type_tag_3 {
37         struct bpf_testmod_btf_type_tag_1 __percpu *p;
38 };
39
40 noinline int
41 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
42         BTF_TYPE_EMIT(func_proto_typedef);
43         BTF_TYPE_EMIT(func_proto_typedef_nested1);
44         BTF_TYPE_EMIT(func_proto_typedef_nested2);
45         return arg->a;
46 }
47
48 noinline int
49 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
50         return arg->p->a;
51 }
52
53 noinline int
54 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
55         return arg->a;
56 }
57
58 noinline int
59 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
60         return arg->p->a;
61 }
62
63 noinline int bpf_testmod_loop_test(int n)
64 {
65         int i, sum = 0;
66
67         /* the primary goal of this test is to test LBR. Create a lot of
68          * branches in the function, so we can catch it easily.
69          */
70         for (i = 0; i < n; i++)
71                 sum += i;
72         return sum;
73 }
74
75 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
76 {
77         static struct file f = {};
78
79         switch (arg) {
80         case 1: return (void *)EINVAL;          /* user addr */
81         case 2: return (void *)0xcafe4a11;      /* user addr */
82         case 3: return (void *)-EINVAL;         /* canonical, but invalid */
83         case 4: return (void *)(1ull << 60);    /* non-canonical and invalid */
84         case 5: return (void *)~(1ull << 30);   /* trigger extable */
85         case 6: return &f;                      /* valid addr */
86         case 7: return (void *)((long)&f | 1);  /* kernel tricks */
87         default: return NULL;
88         }
89 }
90
91 noinline ssize_t
92 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
93                       struct bin_attribute *bin_attr,
94                       char *buf, loff_t off, size_t len)
95 {
96         struct bpf_testmod_test_read_ctx ctx = {
97                 .buf = buf,
98                 .off = off,
99                 .len = len,
100         };
101         int i = 1;
102
103         while (bpf_testmod_return_ptr(i))
104                 i++;
105
106         /* This is always true. Use the check to make sure the compiler
107          * doesn't remove bpf_testmod_loop_test.
108          */
109         if (bpf_testmod_loop_test(101) > 100)
110                 trace_bpf_testmod_test_read(current, &ctx);
111
112         /* Magic number to enable writable tp */
113         if (len == 64) {
114                 struct bpf_testmod_test_writable_ctx writable = {
115                         .val = 1024,
116                 };
117                 trace_bpf_testmod_test_writable_bare(&writable);
118                 if (writable.early_ret)
119                         return snprintf(buf, len, "%d\n", writable.val);
120         }
121
122         return -EIO; /* always fail */
123 }
124 EXPORT_SYMBOL(bpf_testmod_test_read);
125 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
126
127 noinline ssize_t
128 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
129                       struct bin_attribute *bin_attr,
130                       char *buf, loff_t off, size_t len)
131 {
132         struct bpf_testmod_test_write_ctx ctx = {
133                 .buf = buf,
134                 .off = off,
135                 .len = len,
136         };
137
138         trace_bpf_testmod_test_write_bare(current, &ctx);
139
140         return -EIO; /* always fail */
141 }
142 EXPORT_SYMBOL(bpf_testmod_test_write);
143 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
144
145 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
146         .attr = { .name = "bpf_testmod", .mode = 0666, },
147         .read = bpf_testmod_test_read,
148         .write = bpf_testmod_test_write,
149 };
150
151 BTF_SET_START(bpf_testmod_check_kfunc_ids)
152 BTF_ID(func, bpf_testmod_test_mod_kfunc)
153 BTF_SET_END(bpf_testmod_check_kfunc_ids)
154
155 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
156         .owner     = THIS_MODULE,
157         .check_set = &bpf_testmod_check_kfunc_ids,
158 };
159
160 extern int bpf_fentry_test1(int a);
161
162 static int bpf_testmod_init(void)
163 {
164         int ret;
165
166         ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
167         if (ret < 0)
168                 return ret;
169         if (bpf_fentry_test1(0) < 0)
170                 return -EINVAL;
171         return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
172 }
173
174 static void bpf_testmod_exit(void)
175 {
176         return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
177 }
178
179 module_init(bpf_testmod_init);
180 module_exit(bpf_testmod_exit);
181
182 MODULE_AUTHOR("Andrii Nakryiko");
183 MODULE_DESCRIPTION("BPF selftests module");
184 MODULE_LICENSE("Dual BSD/GPL");