1 // SPDX-License-Identifier: GPL-2.0
3 * fprobe - Simple ftrace probe wrapper for function entry.
5 #define pr_fmt(fmt) "fprobe: " fmt
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>
17 struct fprobe_rethook_node {
18 struct rethook_node node;
19 unsigned long entry_ip;
20 unsigned long entry_parent_ip;
24 static inline void __fprobe_handler(unsigned long ip, unsigned long parent_ip,
25 struct ftrace_ops *ops, struct ftrace_regs *fregs)
27 struct fprobe_rethook_node *fpr;
28 struct rethook_node *rh = NULL;
30 void *entry_data = NULL;
33 fp = container_of(ops, struct fprobe, ops);
35 if (fp->exit_handler) {
36 rh = rethook_try_get(fp->rethook);
41 fpr = container_of(rh, struct fprobe_rethook_node, node);
43 fpr->entry_parent_ip = parent_ip;
44 if (fp->entry_data_size)
45 entry_data = fpr->data;
48 if (fp->entry_handler)
49 ret = fp->entry_handler(fp, ip, parent_ip, ftrace_get_regs(fregs), entry_data);
51 /* If entry_handler returns !0, nmissed is not counted. */
56 rethook_hook(rh, ftrace_get_regs(fregs), true);
60 static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
61 struct ftrace_ops *ops, struct ftrace_regs *fregs)
66 fp = container_of(ops, struct fprobe, ops);
67 if (fprobe_disabled(fp))
70 /* recursion detection has to go before any traceable function and
71 * all functions before this point should be marked as notrace
73 bit = ftrace_test_recursion_trylock(ip, parent_ip);
78 __fprobe_handler(ip, parent_ip, ops, fregs);
79 ftrace_test_recursion_unlock(bit);
82 NOKPROBE_SYMBOL(fprobe_handler);
84 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
85 struct ftrace_ops *ops, struct ftrace_regs *fregs)
90 fp = container_of(ops, struct fprobe, ops);
91 if (fprobe_disabled(fp))
94 /* recursion detection has to go before any traceable function and
95 * all functions called before this point should be marked as notrace
97 bit = ftrace_test_recursion_trylock(ip, parent_ip);
104 * This user handler is shared with other kprobes and is not expected to be
105 * called recursively. So if any other kprobe handler is running, this will
106 * exit as kprobe does. See the section 'Share the callbacks with kprobes'
107 * in Documentation/trace/fprobe.rst for more information.
109 if (unlikely(kprobe_running())) {
111 goto recursion_unlock;
115 __fprobe_handler(ip, parent_ip, ops, fregs);
119 ftrace_test_recursion_unlock(bit);
122 static void fprobe_exit_handler(struct rethook_node *rh, void *data,
123 unsigned long ret_ip, struct pt_regs *regs)
125 struct fprobe *fp = (struct fprobe *)data;
126 struct fprobe_rethook_node *fpr;
129 if (!fp || fprobe_disabled(fp))
132 fpr = container_of(rh, struct fprobe_rethook_node, node);
135 * we need to assure no calls to traceable functions in-between the
136 * end of fprobe_handler and the beginning of fprobe_exit_handler.
138 bit = ftrace_test_recursion_trylock(fpr->entry_ip, fpr->entry_parent_ip);
144 fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs,
145 fp->entry_data_size ? (void *)fpr->data : NULL);
146 ftrace_test_recursion_unlock(bit);
148 NOKPROBE_SYMBOL(fprobe_exit_handler);
150 static int symbols_cmp(const void *a, const void *b)
152 const char **str_a = (const char **) a;
153 const char **str_b = (const char **) b;
155 return strcmp(*str_a, *str_b);
158 /* Convert ftrace location address from symbols */
159 static unsigned long *get_ftrace_locations(const char **syms, int num)
161 unsigned long *addrs;
163 /* Convert symbols to symbol address */
164 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
166 return ERR_PTR(-ENOMEM);
168 /* ftrace_lookup_symbols expects sorted symbols */
169 sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
171 if (!ftrace_lookup_symbols(syms, num, addrs))
175 return ERR_PTR(-ENOENT);
178 static void fprobe_init(struct fprobe *fp)
181 if (fprobe_shared_with_kprobes(fp))
182 fp->ops.func = fprobe_kprobe_handler;
184 fp->ops.func = fprobe_handler;
185 fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
188 static int fprobe_init_rethook(struct fprobe *fp, int num)
195 if (!fp->exit_handler) {
200 /* Initialize rethook if needed */
201 if (fp->nr_maxactive)
202 size = fp->nr_maxactive;
204 size = num * num_possible_cpus() * 2;
208 /* Initialize rethook */
209 fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler,
210 sizeof(struct fprobe_rethook_node), size);
211 if (IS_ERR(fp->rethook))
212 return PTR_ERR(fp->rethook);
217 static void fprobe_fail_cleanup(struct fprobe *fp)
219 if (!IS_ERR_OR_NULL(fp->rethook)) {
220 /* Don't need to cleanup rethook->handler because this is not used. */
221 rethook_free(fp->rethook);
224 ftrace_free_filter(&fp->ops);
228 * register_fprobe() - Register fprobe to ftrace by pattern.
229 * @fp: A fprobe data structure to be registered.
230 * @filter: A wildcard pattern of probed symbols.
231 * @notfilter: A wildcard pattern of NOT probed symbols.
233 * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
234 * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
236 * Return 0 if @fp is registered successfully, -errno if not.
238 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
240 struct ftrace_hash *hash;
249 len = strlen(filter);
250 str = kstrdup(filter, GFP_KERNEL);
251 ret = ftrace_set_filter(&fp->ops, str, len, 0);
257 len = strlen(notfilter);
258 str = kstrdup(notfilter, GFP_KERNEL);
259 ret = ftrace_set_notrace(&fp->ops, str, len, 0);
266 * correctly calculate the total number of filtered symbols
267 * from both filter and notfilter.
269 hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
270 if (WARN_ON_ONCE(!hash))
273 ret = fprobe_init_rethook(fp, (int)hash->count);
275 ret = register_ftrace_function(&fp->ops);
279 fprobe_fail_cleanup(fp);
282 EXPORT_SYMBOL_GPL(register_fprobe);
285 * register_fprobe_ips() - Register fprobe to ftrace by address.
286 * @fp: A fprobe data structure to be registered.
287 * @addrs: An array of target ftrace location addresses.
288 * @num: The number of entries of @addrs.
290 * Register @fp to ftrace for enabling the probe on the address given by @addrs.
291 * The @addrs must be the addresses of ftrace location address, which may be
292 * the symbol address + arch-dependent offset.
293 * If you unsure what this mean, please use other registration functions.
295 * Return 0 if @fp is registered successfully, -errno if not.
297 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
301 if (!fp || !addrs || num <= 0)
306 ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
310 ret = fprobe_init_rethook(fp, num);
312 ret = register_ftrace_function(&fp->ops);
315 fprobe_fail_cleanup(fp);
318 EXPORT_SYMBOL_GPL(register_fprobe_ips);
321 * register_fprobe_syms() - Register fprobe to ftrace by symbols.
322 * @fp: A fprobe data structure to be registered.
323 * @syms: An array of target symbols.
324 * @num: The number of entries of @syms.
326 * Register @fp to the symbols given by @syms array. This will be useful if
327 * you are sure the symbols exist in the kernel.
329 * Return 0 if @fp is registered successfully, -errno if not.
331 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
333 unsigned long *addrs;
336 if (!fp || !syms || num <= 0)
339 addrs = get_ftrace_locations(syms, num);
341 return PTR_ERR(addrs);
343 ret = register_fprobe_ips(fp, addrs, num);
349 EXPORT_SYMBOL_GPL(register_fprobe_syms);
351 bool fprobe_is_registered(struct fprobe *fp)
353 if (!fp || (fp->ops.saved_func != fprobe_handler &&
354 fp->ops.saved_func != fprobe_kprobe_handler))
360 * unregister_fprobe() - Unregister fprobe from ftrace
361 * @fp: A fprobe data structure to be unregistered.
363 * Unregister fprobe (and remove ftrace hooks from the function entries).
365 * Return 0 if @fp is unregistered successfully, -errno if not.
367 int unregister_fprobe(struct fprobe *fp)
371 if (!fprobe_is_registered(fp))
374 if (!IS_ERR_OR_NULL(fp->rethook))
375 rethook_stop(fp->rethook);
377 ret = unregister_ftrace_function(&fp->ops);
381 if (!IS_ERR_OR_NULL(fp->rethook))
382 rethook_free(fp->rethook);
384 ftrace_free_filter(&fp->ops);
388 EXPORT_SYMBOL_GPL(unregister_fprobe);