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;
23 bpf_testmod_test_mod_kfunc(int i)
25 *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
28 struct bpf_testmod_btf_type_tag_1 {
32 struct bpf_testmod_btf_type_tag_2 {
33 struct bpf_testmod_btf_type_tag_1 __user *p;
36 struct bpf_testmod_btf_type_tag_3 {
37 struct bpf_testmod_btf_type_tag_1 __percpu *p;
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);
49 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
54 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
59 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
63 noinline int bpf_testmod_loop_test(int n)
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.
70 for (i = 0; i < n; i++)
75 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
77 static struct file f = {};
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 */
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)
96 struct bpf_testmod_test_read_ctx ctx = {
103 while (bpf_testmod_return_ptr(i))
106 /* This is always true. Use the check to make sure the compiler
107 * doesn't remove bpf_testmod_loop_test.
109 if (bpf_testmod_loop_test(101) > 100)
110 trace_bpf_testmod_test_read(current, &ctx);
112 /* Magic number to enable writable tp */
114 struct bpf_testmod_test_writable_ctx writable = {
117 trace_bpf_testmod_test_writable_bare(&writable);
118 if (writable.early_ret)
119 return snprintf(buf, len, "%d\n", writable.val);
122 return -EIO; /* always fail */
124 EXPORT_SYMBOL(bpf_testmod_test_read);
125 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
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)
132 struct bpf_testmod_test_write_ctx ctx = {
138 trace_bpf_testmod_test_write_bare(current, &ctx);
140 return -EIO; /* always fail */
142 EXPORT_SYMBOL(bpf_testmod_test_write);
143 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
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,
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)
155 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
156 .owner = THIS_MODULE,
157 .check_set = &bpf_testmod_check_kfunc_ids,
160 extern int bpf_fentry_test1(int a);
162 static int bpf_testmod_init(void)
166 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
169 if (bpf_fentry_test1(0) < 0)
171 return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
174 static void bpf_testmod_exit(void)
176 return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
179 module_init(bpf_testmod_init);
180 module_exit(bpf_testmod_exit);
182 MODULE_AUTHOR("Andrii Nakryiko");
183 MODULE_DESCRIPTION("BPF selftests module");
184 MODULE_LICENSE("Dual BSD/GPL");