xref: /linux/kernel/trace/fprobe.c (revision 76d0de5729c0569c4071e7f21fcab394e502f03a)
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 
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 
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 
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 
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 */
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 
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 
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 	size = num * num_possible_cpus() * 2;
147 	if (size < 0)
148 		return -E2BIG;
149 
150 	fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler);
151 	if (!fp->rethook)
152 		return -ENOMEM;
153 	for (i = 0; i < size; i++) {
154 		struct fprobe_rethook_node *node;
155 
156 		node = kzalloc(sizeof(*node) + fp->entry_data_size, GFP_KERNEL);
157 		if (!node) {
158 			rethook_free(fp->rethook);
159 			fp->rethook = NULL;
160 			return -ENOMEM;
161 		}
162 		rethook_add_node(fp->rethook, &node->node);
163 	}
164 	return 0;
165 }
166 
167 static void fprobe_fail_cleanup(struct fprobe *fp)
168 {
169 	if (fp->rethook) {
170 		/* Don't need to cleanup rethook->handler because this is not used. */
171 		rethook_free(fp->rethook);
172 		fp->rethook = NULL;
173 	}
174 	ftrace_free_filter(&fp->ops);
175 }
176 
177 /**
178  * register_fprobe() - Register fprobe to ftrace by pattern.
179  * @fp: A fprobe data structure to be registered.
180  * @filter: A wildcard pattern of probed symbols.
181  * @notfilter: A wildcard pattern of NOT probed symbols.
182  *
183  * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
184  * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
185  *
186  * Return 0 if @fp is registered successfully, -errno if not.
187  */
188 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
189 {
190 	struct ftrace_hash *hash;
191 	unsigned char *str;
192 	int ret, len;
193 
194 	if (!fp || !filter)
195 		return -EINVAL;
196 
197 	fprobe_init(fp);
198 
199 	len = strlen(filter);
200 	str = kstrdup(filter, GFP_KERNEL);
201 	ret = ftrace_set_filter(&fp->ops, str, len, 0);
202 	kfree(str);
203 	if (ret)
204 		return ret;
205 
206 	if (notfilter) {
207 		len = strlen(notfilter);
208 		str = kstrdup(notfilter, GFP_KERNEL);
209 		ret = ftrace_set_notrace(&fp->ops, str, len, 0);
210 		kfree(str);
211 		if (ret)
212 			goto out;
213 	}
214 
215 	/* TODO:
216 	 * correctly calculate the total number of filtered symbols
217 	 * from both filter and notfilter.
218 	 */
219 	hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
220 	if (WARN_ON_ONCE(!hash))
221 		goto out;
222 
223 	ret = fprobe_init_rethook(fp, (int)hash->count);
224 	if (!ret)
225 		ret = register_ftrace_function(&fp->ops);
226 
227 out:
228 	if (ret)
229 		fprobe_fail_cleanup(fp);
230 	return ret;
231 }
232 EXPORT_SYMBOL_GPL(register_fprobe);
233 
234 /**
235  * register_fprobe_ips() - Register fprobe to ftrace by address.
236  * @fp: A fprobe data structure to be registered.
237  * @addrs: An array of target ftrace location addresses.
238  * @num: The number of entries of @addrs.
239  *
240  * Register @fp to ftrace for enabling the probe on the address given by @addrs.
241  * The @addrs must be the addresses of ftrace location address, which may be
242  * the symbol address + arch-dependent offset.
243  * If you unsure what this mean, please use other registration functions.
244  *
245  * Return 0 if @fp is registered successfully, -errno if not.
246  */
247 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
248 {
249 	int ret;
250 
251 	if (!fp || !addrs || num <= 0)
252 		return -EINVAL;
253 
254 	fprobe_init(fp);
255 
256 	ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
257 	if (ret)
258 		return ret;
259 
260 	ret = fprobe_init_rethook(fp, num);
261 	if (!ret)
262 		ret = register_ftrace_function(&fp->ops);
263 
264 	if (ret)
265 		fprobe_fail_cleanup(fp);
266 	return ret;
267 }
268 EXPORT_SYMBOL_GPL(register_fprobe_ips);
269 
270 /**
271  * register_fprobe_syms() - Register fprobe to ftrace by symbols.
272  * @fp: A fprobe data structure to be registered.
273  * @syms: An array of target symbols.
274  * @num: The number of entries of @syms.
275  *
276  * Register @fp to the symbols given by @syms array. This will be useful if
277  * you are sure the symbols exist in the kernel.
278  *
279  * Return 0 if @fp is registered successfully, -errno if not.
280  */
281 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
282 {
283 	unsigned long *addrs;
284 	int ret;
285 
286 	if (!fp || !syms || num <= 0)
287 		return -EINVAL;
288 
289 	addrs = get_ftrace_locations(syms, num);
290 	if (IS_ERR(addrs))
291 		return PTR_ERR(addrs);
292 
293 	ret = register_fprobe_ips(fp, addrs, num);
294 
295 	kfree(addrs);
296 
297 	return ret;
298 }
299 EXPORT_SYMBOL_GPL(register_fprobe_syms);
300 
301 /**
302  * unregister_fprobe() - Unregister fprobe from ftrace
303  * @fp: A fprobe data structure to be unregistered.
304  *
305  * Unregister fprobe (and remove ftrace hooks from the function entries).
306  *
307  * Return 0 if @fp is unregistered successfully, -errno if not.
308  */
309 int unregister_fprobe(struct fprobe *fp)
310 {
311 	int ret;
312 
313 	if (!fp || (fp->ops.saved_func != fprobe_handler &&
314 		    fp->ops.saved_func != fprobe_kprobe_handler))
315 		return -EINVAL;
316 
317 	/*
318 	 * rethook_free() starts disabling the rethook, but the rethook handlers
319 	 * may be running on other processors at this point. To make sure that all
320 	 * current running handlers are finished, call unregister_ftrace_function()
321 	 * after this.
322 	 */
323 	if (fp->rethook)
324 		rethook_free(fp->rethook);
325 
326 	ret = unregister_ftrace_function(&fp->ops);
327 	if (ret < 0)
328 		return ret;
329 
330 	ftrace_free_filter(&fp->ops);
331 
332 	return ret;
333 }
334 EXPORT_SYMBOL_GPL(unregister_fprobe);
335