xref: /linux/tools/testing/selftests/bpf/progs/test_module_attach.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include <bpf/bpf_core_read.h>
8 #include "../bpf_testmod/bpf_testmod.h"
9 
10 __u32 raw_tp_read_sz = 0;
11 
12 SEC("raw_tp/bpf_testmod_test_read")
13 int BPF_PROG(handle_raw_tp,
14 	     struct task_struct *task, struct bpf_testmod_test_read_ctx *read_ctx)
15 {
16 	raw_tp_read_sz = BPF_CORE_READ(read_ctx, len);
17 	return 0;
18 }
19 
20 __u32 raw_tp_bare_write_sz = 0;
21 
22 SEC("raw_tp/bpf_testmod_test_write_bare")
23 int BPF_PROG(handle_raw_tp_bare,
24 	     struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx)
25 {
26 	raw_tp_bare_write_sz = BPF_CORE_READ(write_ctx, len);
27 	return 0;
28 }
29 
30 int raw_tp_writable_bare_in_val = 0;
31 int raw_tp_writable_bare_early_ret = 0;
32 int raw_tp_writable_bare_out_val = 0;
33 
34 SEC("raw_tp.w/bpf_testmod_test_writable_bare")
35 int BPF_PROG(handle_raw_tp_writable_bare,
36 	     struct bpf_testmod_test_writable_ctx *writable)
37 {
38 	raw_tp_writable_bare_in_val = writable->val;
39 	writable->early_ret = raw_tp_writable_bare_early_ret;
40 	writable->val = raw_tp_writable_bare_out_val;
41 	return 0;
42 }
43 
44 __u32 tp_btf_read_sz = 0;
45 
46 SEC("tp_btf/bpf_testmod_test_read")
47 int BPF_PROG(handle_tp_btf,
48 	     struct task_struct *task, struct bpf_testmod_test_read_ctx *read_ctx)
49 {
50 	tp_btf_read_sz = read_ctx->len;
51 	return 0;
52 }
53 
54 __u32 fentry_read_sz = 0;
55 
56 SEC("fentry/bpf_testmod_test_read")
57 int BPF_PROG(handle_fentry,
58 	     struct file *file, struct kobject *kobj,
59 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
60 {
61 	fentry_read_sz = len;
62 	return 0;
63 }
64 
65 __u32 fentry_manual_read_sz = 0;
66 
67 SEC("fentry")
68 int BPF_PROG(handle_fentry_manual,
69 	     struct file *file, struct kobject *kobj,
70 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
71 {
72 	fentry_manual_read_sz = len;
73 	return 0;
74 }
75 
76 __u32 fexit_read_sz = 0;
77 int fexit_ret = 0;
78 
79 SEC("fexit/bpf_testmod_test_read")
80 int BPF_PROG(handle_fexit,
81 	     struct file *file, struct kobject *kobj,
82 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len,
83 	     int ret)
84 {
85 	fexit_read_sz = len;
86 	fexit_ret = ret;
87 	return 0;
88 }
89 
90 SEC("fexit/bpf_testmod_return_ptr")
91 int BPF_PROG(handle_fexit_ret, int arg, struct file *ret)
92 {
93 	long buf = 0;
94 
95 	bpf_probe_read_kernel(&buf, 8, ret);
96 	bpf_probe_read_kernel(&buf, 8, (char *)ret + 256);
97 	*(volatile long long *)ret;
98 	*(volatile int *)&ret->f_mode;
99 	return 0;
100 }
101 
102 __u32 fmod_ret_read_sz = 0;
103 
104 SEC("fmod_ret/bpf_testmod_test_read")
105 int BPF_PROG(handle_fmod_ret,
106 	     struct file *file, struct kobject *kobj,
107 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
108 {
109 	fmod_ret_read_sz = len;
110 	return 0; /* don't override the exit code */
111 }
112 
113 SEC("kprobe.multi/bpf_testmod_test_read")
114 int BPF_PROG(kprobe_multi)
115 {
116 	return 0;
117 }
118 
119 char _license[] SEC("license") = "GPL";
120