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, 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);
103 if (unlikely(kprobe_running())) {
105 goto recursion_unlock;
109 __fprobe_handler(ip, parent_ip, ops, fregs);
113 ftrace_test_recursion_unlock(bit);
116 static void fprobe_exit_handler(struct rethook_node *rh, void *data,
117 struct pt_regs *regs)
119 struct fprobe *fp = (struct fprobe *)data;
120 struct fprobe_rethook_node *fpr;
123 if (!fp || fprobe_disabled(fp))
126 fpr = container_of(rh, struct fprobe_rethook_node, node);
129 * we need to assure no calls to traceable functions in-between the
130 * end of fprobe_handler and the beginning of fprobe_exit_handler.
132 bit = ftrace_test_recursion_trylock(fpr->entry_ip, fpr->entry_parent_ip);
138 fp->exit_handler(fp, fpr->entry_ip, regs,
139 fp->entry_data_size ? (void *)fpr->data : NULL);
140 ftrace_test_recursion_unlock(bit);
142 NOKPROBE_SYMBOL(fprobe_exit_handler);
144 static int symbols_cmp(const void *a, const void *b)
146 const char **str_a = (const char **) a;
147 const char **str_b = (const char **) b;
149 return strcmp(*str_a, *str_b);
152 /* Convert ftrace location address from symbols */
153 static unsigned long *get_ftrace_locations(const char **syms, int num)
155 unsigned long *addrs;
157 /* Convert symbols to symbol address */
158 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
160 return ERR_PTR(-ENOMEM);
162 /* ftrace_lookup_symbols expects sorted symbols */
163 sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
165 if (!ftrace_lookup_symbols(syms, num, addrs))
169 return ERR_PTR(-ENOENT);
172 static void fprobe_init(struct fprobe *fp)
175 if (fprobe_shared_with_kprobes(fp))
176 fp->ops.func = fprobe_kprobe_handler;
178 fp->ops.func = fprobe_handler;
179 fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
182 static int fprobe_init_rethook(struct fprobe *fp, int num)
189 if (!fp->exit_handler) {
194 /* Initialize rethook if needed */
195 if (fp->nr_maxactive)
196 size = fp->nr_maxactive;
198 size = num * num_possible_cpus() * 2;
202 fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler);
205 for (i = 0; i < size; i++) {
206 struct fprobe_rethook_node *node;
208 node = kzalloc(sizeof(*node) + fp->entry_data_size, GFP_KERNEL);
210 rethook_free(fp->rethook);
214 rethook_add_node(fp->rethook, &node->node);
219 static void fprobe_fail_cleanup(struct fprobe *fp)
222 /* Don't need to cleanup rethook->handler because this is not used. */
223 rethook_free(fp->rethook);
226 ftrace_free_filter(&fp->ops);
230 * register_fprobe() - Register fprobe to ftrace by pattern.
231 * @fp: A fprobe data structure to be registered.
232 * @filter: A wildcard pattern of probed symbols.
233 * @notfilter: A wildcard pattern of NOT probed symbols.
235 * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
236 * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
238 * Return 0 if @fp is registered successfully, -errno if not.
240 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
242 struct ftrace_hash *hash;
251 len = strlen(filter);
252 str = kstrdup(filter, GFP_KERNEL);
253 ret = ftrace_set_filter(&fp->ops, str, len, 0);
259 len = strlen(notfilter);
260 str = kstrdup(notfilter, GFP_KERNEL);
261 ret = ftrace_set_notrace(&fp->ops, str, len, 0);
268 * correctly calculate the total number of filtered symbols
269 * from both filter and notfilter.
271 hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
272 if (WARN_ON_ONCE(!hash))
275 ret = fprobe_init_rethook(fp, (int)hash->count);
277 ret = register_ftrace_function(&fp->ops);
281 fprobe_fail_cleanup(fp);
284 EXPORT_SYMBOL_GPL(register_fprobe);
287 * register_fprobe_ips() - Register fprobe to ftrace by address.
288 * @fp: A fprobe data structure to be registered.
289 * @addrs: An array of target ftrace location addresses.
290 * @num: The number of entries of @addrs.
292 * Register @fp to ftrace for enabling the probe on the address given by @addrs.
293 * The @addrs must be the addresses of ftrace location address, which may be
294 * the symbol address + arch-dependent offset.
295 * If you unsure what this mean, please use other registration functions.
297 * Return 0 if @fp is registered successfully, -errno if not.
299 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
303 if (!fp || !addrs || num <= 0)
308 ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
312 ret = fprobe_init_rethook(fp, num);
314 ret = register_ftrace_function(&fp->ops);
317 fprobe_fail_cleanup(fp);
320 EXPORT_SYMBOL_GPL(register_fprobe_ips);
323 * register_fprobe_syms() - Register fprobe to ftrace by symbols.
324 * @fp: A fprobe data structure to be registered.
325 * @syms: An array of target symbols.
326 * @num: The number of entries of @syms.
328 * Register @fp to the symbols given by @syms array. This will be useful if
329 * you are sure the symbols exist in the kernel.
331 * Return 0 if @fp is registered successfully, -errno if not.
333 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
335 unsigned long *addrs;
338 if (!fp || !syms || num <= 0)
341 addrs = get_ftrace_locations(syms, num);
343 return PTR_ERR(addrs);
345 ret = register_fprobe_ips(fp, addrs, num);
351 EXPORT_SYMBOL_GPL(register_fprobe_syms);
354 * unregister_fprobe() - Unregister fprobe from ftrace
355 * @fp: A fprobe data structure to be unregistered.
357 * Unregister fprobe (and remove ftrace hooks from the function entries).
359 * Return 0 if @fp is unregistered successfully, -errno if not.
361 int unregister_fprobe(struct fprobe *fp)
365 if (!fp || (fp->ops.saved_func != fprobe_handler &&
366 fp->ops.saved_func != fprobe_kprobe_handler))
370 rethook_stop(fp->rethook);
372 ret = unregister_ftrace_function(&fp->ops);
377 rethook_free(fp->rethook);
379 ftrace_free_filter(&fp->ops);
383 EXPORT_SYMBOL_GPL(unregister_fprobe);