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
19 bpf_testmod_test_mod_kfunc(int i)
20 {
21 	*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
22 }
23 
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 
36 noinline ssize_t
37 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
38 		      struct bin_attribute *bin_attr,
39 		      char *buf, loff_t off, size_t len)
40 {
41 	struct bpf_testmod_test_read_ctx ctx = {
42 		.buf = buf,
43 		.off = off,
44 		.len = len,
45 	};
46 
47 	/* This is always true. Use the check to make sure the compiler
48 	 * doesn't remove bpf_testmod_loop_test.
49 	 */
50 	if (bpf_testmod_loop_test(101) > 100)
51 		trace_bpf_testmod_test_read(current, &ctx);
52 
53 	/* Magic number to enable writable tp */
54 	if (len == 64) {
55 		struct bpf_testmod_test_writable_ctx writable = {
56 			.val = 1024,
57 		};
58 		trace_bpf_testmod_test_writable_bare(&writable);
59 		if (writable.early_ret)
60 			return snprintf(buf, len, "%d\n", writable.val);
61 	}
62 
63 	return -EIO; /* always fail */
64 }
65 EXPORT_SYMBOL(bpf_testmod_test_read);
66 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
67 
68 noinline ssize_t
69 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
70 		      struct bin_attribute *bin_attr,
71 		      char *buf, loff_t off, size_t len)
72 {
73 	struct bpf_testmod_test_write_ctx ctx = {
74 		.buf = buf,
75 		.off = off,
76 		.len = len,
77 	};
78 
79 	trace_bpf_testmod_test_write_bare(current, &ctx);
80 
81 	return -EIO; /* always fail */
82 }
83 EXPORT_SYMBOL(bpf_testmod_test_write);
84 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
85 
86 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
87 	.attr = { .name = "bpf_testmod", .mode = 0666, },
88 	.read = bpf_testmod_test_read,
89 	.write = bpf_testmod_test_write,
90 };
91 
92 BTF_SET_START(bpf_testmod_kfunc_ids)
93 BTF_ID(func, bpf_testmod_test_mod_kfunc)
94 BTF_SET_END(bpf_testmod_kfunc_ids)
95 
96 static DEFINE_KFUNC_BTF_ID_SET(&bpf_testmod_kfunc_ids, bpf_testmod_kfunc_btf_set);
97 
98 static int bpf_testmod_init(void)
99 {
100 	int ret;
101 
102 	ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
103 	if (ret)
104 		return ret;
105 	register_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set);
106 	return 0;
107 }
108 
109 static void bpf_testmod_exit(void)
110 {
111 	unregister_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set);
112 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
113 }
114 
115 module_init(bpf_testmod_init);
116 module_exit(bpf_testmod_exit);
117 
118 MODULE_AUTHOR("Andrii Nakryiko");
119 MODULE_DESCRIPTION("BPF selftests module");
120 MODULE_LICENSE("Dual BSD/GPL");
121