1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include <linux/error-injection.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/percpu-defs.h>
7 #include <linux/sysfs.h>
8 #include <linux/tracepoint.h>
9 #include "bpf_testmod.h"
10 
11 #define CREATE_TRACE_POINTS
12 #include "bpf_testmod-events.h"
13 
14 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
15 
16 noinline ssize_t
17 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
18 		      struct bin_attribute *bin_attr,
19 		      char *buf, loff_t off, size_t len)
20 {
21 	struct bpf_testmod_test_read_ctx ctx = {
22 		.buf = buf,
23 		.off = off,
24 		.len = len,
25 	};
26 
27 	trace_bpf_testmod_test_read(current, &ctx);
28 
29 	return -EIO; /* always fail */
30 }
31 EXPORT_SYMBOL(bpf_testmod_test_read);
32 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
33 
34 noinline ssize_t
35 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
36 		      struct bin_attribute *bin_attr,
37 		      char *buf, loff_t off, size_t len)
38 {
39 	struct bpf_testmod_test_write_ctx ctx = {
40 		.buf = buf,
41 		.off = off,
42 		.len = len,
43 	};
44 
45 	trace_bpf_testmod_test_write_bare(current, &ctx);
46 
47 	return -EIO; /* always fail */
48 }
49 EXPORT_SYMBOL(bpf_testmod_test_write);
50 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
51 
52 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
53 	.attr = { .name = "bpf_testmod", .mode = 0666, },
54 	.read = bpf_testmod_test_read,
55 	.write = bpf_testmod_test_write,
56 };
57 
58 static int bpf_testmod_init(void)
59 {
60 	return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
61 }
62 
63 static void bpf_testmod_exit(void)
64 {
65 	return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
66 }
67 
68 module_init(bpf_testmod_init);
69 module_exit(bpf_testmod_exit);
70 
71 MODULE_AUTHOR("Andrii Nakryiko");
72 MODULE_DESCRIPTION("BPF selftests module");
73 MODULE_LICENSE("Dual BSD/GPL");
74 
75