xref: /linux/tools/testing/selftests/bpf/progs/test_module_attach.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 fentry_explicit_read_sz = 0;
77 
78 SEC("fentry/bpf_testmod:bpf_testmod_test_read")
79 int BPF_PROG(handle_fentry_explicit,
80 	     struct file *file, struct kobject *kobj,
81 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
82 {
83 	fentry_explicit_read_sz = len;
84 	return 0;
85 }
86 
87 
88 __u32 fentry_explicit_manual_read_sz = 0;
89 
90 SEC("fentry")
91 int BPF_PROG(handle_fentry_explicit_manual,
92 	     struct file *file, struct kobject *kobj,
93 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
94 {
95 	fentry_explicit_manual_read_sz = len;
96 	return 0;
97 }
98 
99 __u32 fexit_read_sz = 0;
100 int fexit_ret = 0;
101 
102 SEC("fexit/bpf_testmod_test_read")
103 int BPF_PROG(handle_fexit,
104 	     struct file *file, struct kobject *kobj,
105 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len,
106 	     int ret)
107 {
108 	fexit_read_sz = len;
109 	fexit_ret = ret;
110 	return 0;
111 }
112 
113 SEC("fexit/bpf_testmod_return_ptr")
114 int BPF_PROG(handle_fexit_ret, int arg, struct file *ret)
115 {
116 	long buf = 0;
117 
118 	bpf_probe_read_kernel(&buf, 8, ret);
119 	bpf_probe_read_kernel(&buf, 8, (char *)ret + 256);
120 	*(volatile long long *)ret;
121 	*(volatile int *)&ret->f_mode;
122 	return 0;
123 }
124 
125 __u32 fmod_ret_read_sz = 0;
126 
127 SEC("fmod_ret/bpf_testmod_test_read")
128 int BPF_PROG(handle_fmod_ret,
129 	     struct file *file, struct kobject *kobj,
130 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
131 {
132 	fmod_ret_read_sz = len;
133 	return 0; /* don't override the exit code */
134 }
135 
136 SEC("kprobe.multi/bpf_testmod_test_read")
137 int BPF_PROG(kprobe_multi)
138 {
139 	return 0;
140 }
141 
142 char _license[] SEC("license") = "GPL";
143