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 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
17
18 noinline void
bpf_testmod_test_mod_kfunc(int i)19 bpf_testmod_test_mod_kfunc(int i)
20 {
21 *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
22 }
23
bpf_testmod_loop_test(int n)24 noinline int bpf_testmod_loop_test(int n)
25 {
26 int i, sum = 0;
27
28 /* the primary goal of this test is to test LBR. Create a lot of
29 * branches in the function, so we can catch it easily.
30 */
31 for (i = 0; i < n; i++)
32 sum += i;
33 return sum;
34 }
35
bpf_testmod_return_ptr(int arg)36 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
37 {
38 static struct file f = {};
39
40 switch (arg) {
41 case 1: return (void *)EINVAL; /* user addr */
42 case 2: return (void *)0xcafe4a11; /* user addr */
43 case 3: return (void *)-EINVAL; /* canonical, but invalid */
44 case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
45 case 5: return (void *)~(1ull << 30); /* trigger extable */
46 case 6: return &f; /* valid addr */
47 case 7: return (void *)((long)&f | 1); /* kernel tricks */
48 default: return NULL;
49 }
50 }
51
52 noinline ssize_t
bpf_testmod_test_read(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)53 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
54 struct bin_attribute *bin_attr,
55 char *buf, loff_t off, size_t len)
56 {
57 struct bpf_testmod_test_read_ctx ctx = {
58 .buf = buf,
59 .off = off,
60 .len = len,
61 };
62 int i = 1;
63
64 while (bpf_testmod_return_ptr(i))
65 i++;
66
67 /* This is always true. Use the check to make sure the compiler
68 * doesn't remove bpf_testmod_loop_test.
69 */
70 if (bpf_testmod_loop_test(101) > 100)
71 trace_bpf_testmod_test_read(current, &ctx);
72
73 /* Magic number to enable writable tp */
74 if (len == 64) {
75 struct bpf_testmod_test_writable_ctx writable = {
76 .val = 1024,
77 };
78 trace_bpf_testmod_test_writable_bare(&writable);
79 if (writable.early_ret)
80 return snprintf(buf, len, "%d\n", writable.val);
81 }
82
83 return -EIO; /* always fail */
84 }
85 EXPORT_SYMBOL(bpf_testmod_test_read);
86 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
87
88 noinline ssize_t
bpf_testmod_test_write(struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)89 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
90 struct bin_attribute *bin_attr,
91 char *buf, loff_t off, size_t len)
92 {
93 struct bpf_testmod_test_write_ctx ctx = {
94 .buf = buf,
95 .off = off,
96 .len = len,
97 };
98
99 trace_bpf_testmod_test_write_bare(current, &ctx);
100
101 return -EIO; /* always fail */
102 }
103 EXPORT_SYMBOL(bpf_testmod_test_write);
104 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
105
106 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
107 .attr = { .name = "bpf_testmod", .mode = 0666, },
108 .read = bpf_testmod_test_read,
109 .write = bpf_testmod_test_write,
110 };
111
112 BTF_SET_START(bpf_testmod_kfunc_ids)
113 BTF_ID(func, bpf_testmod_test_mod_kfunc)
114 BTF_SET_END(bpf_testmod_kfunc_ids)
115
116 static DEFINE_KFUNC_BTF_ID_SET(&bpf_testmod_kfunc_ids, bpf_testmod_kfunc_btf_set);
117
bpf_testmod_init(void)118 static int bpf_testmod_init(void)
119 {
120 int ret;
121
122 ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
123 if (ret)
124 return ret;
125 register_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set);
126 return 0;
127 }
128
bpf_testmod_exit(void)129 static void bpf_testmod_exit(void)
130 {
131 unregister_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set);
132 return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
133 }
134
135 module_init(bpf_testmod_init);
136 module_exit(bpf_testmod_exit);
137
138 MODULE_AUTHOR("Andrii Nakryiko");
139 MODULE_DESCRIPTION("BPF selftests module");
140 MODULE_LICENSE("Dual BSD/GPL");
141