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;
22 static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
23 struct ftrace_ops *ops, struct ftrace_regs *fregs)
25 struct fprobe_rethook_node *fpr;
26 struct rethook_node *rh;
30 fp = container_of(ops, struct fprobe, ops);
31 if (fprobe_disabled(fp))
34 bit = ftrace_test_recursion_trylock(ip, parent_ip);
40 if (fp->entry_handler)
41 fp->entry_handler(fp, ip, ftrace_get_regs(fregs));
43 if (fp->exit_handler) {
44 rh = rethook_try_get(fp->rethook);
49 fpr = container_of(rh, struct fprobe_rethook_node, node);
51 rethook_hook(rh, ftrace_get_regs(fregs), true);
55 ftrace_test_recursion_unlock(bit);
57 NOKPROBE_SYMBOL(fprobe_handler);
59 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
60 struct ftrace_ops *ops, struct ftrace_regs *fregs)
62 struct fprobe *fp = container_of(ops, struct fprobe, ops);
64 if (unlikely(kprobe_running())) {
69 fprobe_handler(ip, parent_ip, ops, fregs);
73 static void fprobe_exit_handler(struct rethook_node *rh, void *data,
76 struct fprobe *fp = (struct fprobe *)data;
77 struct fprobe_rethook_node *fpr;
79 if (!fp || fprobe_disabled(fp))
82 fpr = container_of(rh, struct fprobe_rethook_node, node);
84 fp->exit_handler(fp, fpr->entry_ip, regs);
86 NOKPROBE_SYMBOL(fprobe_exit_handler);
88 static int symbols_cmp(const void *a, const void *b)
90 const char **str_a = (const char **) a;
91 const char **str_b = (const char **) b;
93 return strcmp(*str_a, *str_b);
96 /* Convert ftrace location address from symbols */
97 static unsigned long *get_ftrace_locations(const char **syms, int num)
101 /* Convert symbols to symbol address */
102 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
104 return ERR_PTR(-ENOMEM);
106 /* ftrace_lookup_symbols expects sorted symbols */
107 sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
109 if (!ftrace_lookup_symbols(syms, num, addrs))
113 return ERR_PTR(-ENOENT);
116 static void fprobe_init(struct fprobe *fp)
119 if (fprobe_shared_with_kprobes(fp))
120 fp->ops.func = fprobe_kprobe_handler;
122 fp->ops.func = fprobe_handler;
123 fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
126 static int fprobe_init_rethook(struct fprobe *fp, int num)
133 if (!fp->exit_handler) {
138 /* Initialize rethook if needed */
139 size = num * num_possible_cpus() * 2;
143 fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler);
146 for (i = 0; i < size; i++) {
147 struct fprobe_rethook_node *node;
149 node = kzalloc(sizeof(*node), GFP_KERNEL);
151 rethook_free(fp->rethook);
155 rethook_add_node(fp->rethook, &node->node);
160 static void fprobe_fail_cleanup(struct fprobe *fp)
163 /* Don't need to cleanup rethook->handler because this is not used. */
164 rethook_free(fp->rethook);
167 ftrace_free_filter(&fp->ops);
171 * register_fprobe() - Register fprobe to ftrace by pattern.
172 * @fp: A fprobe data structure to be registered.
173 * @filter: A wildcard pattern of probed symbols.
174 * @notfilter: A wildcard pattern of NOT probed symbols.
176 * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
177 * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
179 * Return 0 if @fp is registered successfully, -errno if not.
181 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
183 struct ftrace_hash *hash;
192 len = strlen(filter);
193 str = kstrdup(filter, GFP_KERNEL);
194 ret = ftrace_set_filter(&fp->ops, str, len, 0);
200 len = strlen(notfilter);
201 str = kstrdup(notfilter, GFP_KERNEL);
202 ret = ftrace_set_notrace(&fp->ops, str, len, 0);
209 * correctly calculate the total number of filtered symbols
210 * from both filter and notfilter.
212 hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
213 if (WARN_ON_ONCE(!hash))
216 ret = fprobe_init_rethook(fp, (int)hash->count);
218 ret = register_ftrace_function(&fp->ops);
222 fprobe_fail_cleanup(fp);
225 EXPORT_SYMBOL_GPL(register_fprobe);
228 * register_fprobe_ips() - Register fprobe to ftrace by address.
229 * @fp: A fprobe data structure to be registered.
230 * @addrs: An array of target ftrace location addresses.
231 * @num: The number of entries of @addrs.
233 * Register @fp to ftrace for enabling the probe on the address given by @addrs.
234 * The @addrs must be the addresses of ftrace location address, which may be
235 * the symbol address + arch-dependent offset.
236 * If you unsure what this mean, please use other registration functions.
238 * Return 0 if @fp is registered successfully, -errno if not.
240 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
244 if (!fp || !addrs || num <= 0)
249 ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
253 ret = fprobe_init_rethook(fp, num);
255 ret = register_ftrace_function(&fp->ops);
258 fprobe_fail_cleanup(fp);
261 EXPORT_SYMBOL_GPL(register_fprobe_ips);
264 * register_fprobe_syms() - Register fprobe to ftrace by symbols.
265 * @fp: A fprobe data structure to be registered.
266 * @syms: An array of target symbols.
267 * @num: The number of entries of @syms.
269 * Register @fp to the symbols given by @syms array. This will be useful if
270 * you are sure the symbols exist in the kernel.
272 * Return 0 if @fp is registered successfully, -errno if not.
274 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
276 unsigned long *addrs;
279 if (!fp || !syms || num <= 0)
282 addrs = get_ftrace_locations(syms, num);
284 return PTR_ERR(addrs);
286 ret = register_fprobe_ips(fp, addrs, num);
292 EXPORT_SYMBOL_GPL(register_fprobe_syms);
295 * unregister_fprobe() - Unregister fprobe from ftrace
296 * @fp: A fprobe data structure to be unregistered.
298 * Unregister fprobe (and remove ftrace hooks from the function entries).
300 * Return 0 if @fp is unregistered successfully, -errno if not.
302 int unregister_fprobe(struct fprobe *fp)
306 if (!fp || (fp->ops.saved_func != fprobe_handler &&
307 fp->ops.saved_func != fprobe_kprobe_handler))
311 * rethook_free() starts disabling the rethook, but the rethook handlers
312 * may be running on other processors at this point. To make sure that all
313 * current running handlers are finished, call unregister_ftrace_function()
317 rethook_free(fp->rethook);
319 ret = unregister_ftrace_function(&fp->ops);
323 ftrace_free_filter(&fp->ops);
327 EXPORT_SYMBOL_GPL(unregister_fprobe);