1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fprobe - Simple ftrace probe wrapper for function entry.
4 */
5 #define pr_fmt(fmt) "fprobe: " fmt
6
7 #include <linux/err.h>
8 #include <linux/fprobe.h>
9 #include <linux/kallsyms.h>
10 #include <linux/kprobes.h>
11 #include <linux/rethook.h>
12 #include <linux/slab.h>
13 #include <linux/sort.h>
14
15 #include "trace.h"
16
17 struct fprobe_rethook_node {
18 struct rethook_node node;
19 unsigned long entry_ip;
20 char data[];
21 };
22
fprobe_handler(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * ops,struct ftrace_regs * fregs)23 static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
24 struct ftrace_ops *ops, struct ftrace_regs *fregs)
25 {
26 struct fprobe_rethook_node *fpr;
27 struct rethook_node *rh = NULL;
28 struct fprobe *fp;
29 void *entry_data = NULL;
30 int bit;
31
32 fp = container_of(ops, struct fprobe, ops);
33 if (fprobe_disabled(fp))
34 return;
35
36 bit = ftrace_test_recursion_trylock(ip, parent_ip);
37 if (bit < 0) {
38 fp->nmissed++;
39 return;
40 }
41
42 if (fp->exit_handler) {
43 rh = rethook_try_get(fp->rethook);
44 if (!rh) {
45 fp->nmissed++;
46 goto out;
47 }
48 fpr = container_of(rh, struct fprobe_rethook_node, node);
49 fpr->entry_ip = ip;
50 if (fp->entry_data_size)
51 entry_data = fpr->data;
52 }
53
54 if (fp->entry_handler)
55 fp->entry_handler(fp, ip, ftrace_get_regs(fregs), entry_data);
56
57 if (rh)
58 rethook_hook(rh, ftrace_get_regs(fregs), true);
59
60 out:
61 ftrace_test_recursion_unlock(bit);
62 }
63 NOKPROBE_SYMBOL(fprobe_handler);
64
fprobe_kprobe_handler(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * ops,struct ftrace_regs * fregs)65 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
66 struct ftrace_ops *ops, struct ftrace_regs *fregs)
67 {
68 struct fprobe *fp = container_of(ops, struct fprobe, ops);
69
70 if (unlikely(kprobe_running())) {
71 fp->nmissed++;
72 return;
73 }
74 kprobe_busy_begin();
75 fprobe_handler(ip, parent_ip, ops, fregs);
76 kprobe_busy_end();
77 }
78
fprobe_exit_handler(struct rethook_node * rh,void * data,struct pt_regs * regs)79 static void fprobe_exit_handler(struct rethook_node *rh, void *data,
80 struct pt_regs *regs)
81 {
82 struct fprobe *fp = (struct fprobe *)data;
83 struct fprobe_rethook_node *fpr;
84
85 if (!fp || fprobe_disabled(fp))
86 return;
87
88 fpr = container_of(rh, struct fprobe_rethook_node, node);
89
90 fp->exit_handler(fp, fpr->entry_ip, regs,
91 fp->entry_data_size ? (void *)fpr->data : NULL);
92 }
93 NOKPROBE_SYMBOL(fprobe_exit_handler);
94
symbols_cmp(const void * a,const void * b)95 static int symbols_cmp(const void *a, const void *b)
96 {
97 const char **str_a = (const char **) a;
98 const char **str_b = (const char **) b;
99
100 return strcmp(*str_a, *str_b);
101 }
102
103 /* Convert ftrace location address from symbols */
get_ftrace_locations(const char ** syms,int num)104 static unsigned long *get_ftrace_locations(const char **syms, int num)
105 {
106 unsigned long *addrs;
107
108 /* Convert symbols to symbol address */
109 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
110 if (!addrs)
111 return ERR_PTR(-ENOMEM);
112
113 /* ftrace_lookup_symbols expects sorted symbols */
114 sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
115
116 if (!ftrace_lookup_symbols(syms, num, addrs))
117 return addrs;
118
119 kfree(addrs);
120 return ERR_PTR(-ENOENT);
121 }
122
fprobe_init(struct fprobe * fp)123 static void fprobe_init(struct fprobe *fp)
124 {
125 fp->nmissed = 0;
126 if (fprobe_shared_with_kprobes(fp))
127 fp->ops.func = fprobe_kprobe_handler;
128 else
129 fp->ops.func = fprobe_handler;
130 fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
131 }
132
fprobe_init_rethook(struct fprobe * fp,int num)133 static int fprobe_init_rethook(struct fprobe *fp, int num)
134 {
135 int i, size;
136
137 if (num <= 0)
138 return -EINVAL;
139
140 if (!fp->exit_handler) {
141 fp->rethook = NULL;
142 return 0;
143 }
144
145 /* Initialize rethook if needed */
146 if (fp->nr_maxactive)
147 size = fp->nr_maxactive;
148 else
149 size = num * num_possible_cpus() * 2;
150 if (size <= 0)
151 return -EINVAL;
152
153 fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler);
154 if (!fp->rethook)
155 return -ENOMEM;
156 for (i = 0; i < size; i++) {
157 struct fprobe_rethook_node *node;
158
159 node = kzalloc(sizeof(*node) + fp->entry_data_size, GFP_KERNEL);
160 if (!node) {
161 rethook_free(fp->rethook);
162 fp->rethook = NULL;
163 return -ENOMEM;
164 }
165 rethook_add_node(fp->rethook, &node->node);
166 }
167 return 0;
168 }
169
fprobe_fail_cleanup(struct fprobe * fp)170 static void fprobe_fail_cleanup(struct fprobe *fp)
171 {
172 if (fp->rethook) {
173 /* Don't need to cleanup rethook->handler because this is not used. */
174 rethook_free(fp->rethook);
175 fp->rethook = NULL;
176 }
177 ftrace_free_filter(&fp->ops);
178 }
179
180 /**
181 * register_fprobe() - Register fprobe to ftrace by pattern.
182 * @fp: A fprobe data structure to be registered.
183 * @filter: A wildcard pattern of probed symbols.
184 * @notfilter: A wildcard pattern of NOT probed symbols.
185 *
186 * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
187 * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
188 *
189 * Return 0 if @fp is registered successfully, -errno if not.
190 */
register_fprobe(struct fprobe * fp,const char * filter,const char * notfilter)191 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
192 {
193 struct ftrace_hash *hash;
194 unsigned char *str;
195 int ret, len;
196
197 if (!fp || !filter)
198 return -EINVAL;
199
200 fprobe_init(fp);
201
202 len = strlen(filter);
203 str = kstrdup(filter, GFP_KERNEL);
204 ret = ftrace_set_filter(&fp->ops, str, len, 0);
205 kfree(str);
206 if (ret)
207 return ret;
208
209 if (notfilter) {
210 len = strlen(notfilter);
211 str = kstrdup(notfilter, GFP_KERNEL);
212 ret = ftrace_set_notrace(&fp->ops, str, len, 0);
213 kfree(str);
214 if (ret)
215 goto out;
216 }
217
218 /* TODO:
219 * correctly calculate the total number of filtered symbols
220 * from both filter and notfilter.
221 */
222 hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
223 if (WARN_ON_ONCE(!hash))
224 goto out;
225
226 ret = fprobe_init_rethook(fp, (int)hash->count);
227 if (!ret)
228 ret = register_ftrace_function(&fp->ops);
229
230 out:
231 if (ret)
232 fprobe_fail_cleanup(fp);
233 return ret;
234 }
235 EXPORT_SYMBOL_GPL(register_fprobe);
236
237 /**
238 * register_fprobe_ips() - Register fprobe to ftrace by address.
239 * @fp: A fprobe data structure to be registered.
240 * @addrs: An array of target ftrace location addresses.
241 * @num: The number of entries of @addrs.
242 *
243 * Register @fp to ftrace for enabling the probe on the address given by @addrs.
244 * The @addrs must be the addresses of ftrace location address, which may be
245 * the symbol address + arch-dependent offset.
246 * If you unsure what this mean, please use other registration functions.
247 *
248 * Return 0 if @fp is registered successfully, -errno if not.
249 */
register_fprobe_ips(struct fprobe * fp,unsigned long * addrs,int num)250 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
251 {
252 int ret;
253
254 if (!fp || !addrs || num <= 0)
255 return -EINVAL;
256
257 fprobe_init(fp);
258
259 ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
260 if (ret)
261 return ret;
262
263 ret = fprobe_init_rethook(fp, num);
264 if (!ret)
265 ret = register_ftrace_function(&fp->ops);
266
267 if (ret)
268 fprobe_fail_cleanup(fp);
269 return ret;
270 }
271 EXPORT_SYMBOL_GPL(register_fprobe_ips);
272
273 /**
274 * register_fprobe_syms() - Register fprobe to ftrace by symbols.
275 * @fp: A fprobe data structure to be registered.
276 * @syms: An array of target symbols.
277 * @num: The number of entries of @syms.
278 *
279 * Register @fp to the symbols given by @syms array. This will be useful if
280 * you are sure the symbols exist in the kernel.
281 *
282 * Return 0 if @fp is registered successfully, -errno if not.
283 */
register_fprobe_syms(struct fprobe * fp,const char ** syms,int num)284 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
285 {
286 unsigned long *addrs;
287 int ret;
288
289 if (!fp || !syms || num <= 0)
290 return -EINVAL;
291
292 addrs = get_ftrace_locations(syms, num);
293 if (IS_ERR(addrs))
294 return PTR_ERR(addrs);
295
296 ret = register_fprobe_ips(fp, addrs, num);
297
298 kfree(addrs);
299
300 return ret;
301 }
302 EXPORT_SYMBOL_GPL(register_fprobe_syms);
303
304 /**
305 * unregister_fprobe() - Unregister fprobe from ftrace
306 * @fp: A fprobe data structure to be unregistered.
307 *
308 * Unregister fprobe (and remove ftrace hooks from the function entries).
309 *
310 * Return 0 if @fp is unregistered successfully, -errno if not.
311 */
unregister_fprobe(struct fprobe * fp)312 int unregister_fprobe(struct fprobe *fp)
313 {
314 int ret;
315
316 if (!fp || (fp->ops.saved_func != fprobe_handler &&
317 fp->ops.saved_func != fprobe_kprobe_handler))
318 return -EINVAL;
319
320 if (fp->rethook)
321 rethook_stop(fp->rethook);
322
323 ret = unregister_ftrace_function(&fp->ops);
324 if (ret < 0)
325 return ret;
326
327 if (fp->rethook)
328 rethook_free(fp->rethook);
329
330 ftrace_free_filter(&fp->ops);
331
332 return ret;
333 }
334 EXPORT_SYMBOL_GPL(unregister_fprobe);
335