xref: /linux/kernel/trace/fprobe.c (revision e0a384434ae1bdfb03954c46c464e3dbd3223ad6)
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/list.h>
12 #include <linux/mutex.h>
13 #include <linux/rhashtable.h>
14 #include <linux/slab.h>
15 #include <linux/sort.h>
16 
17 #include <asm/fprobe.h>
18 
19 #include "trace.h"
20 
21 #define FPROBE_IP_HASH_BITS 8
22 #define FPROBE_IP_TABLE_SIZE (1 << FPROBE_IP_HASH_BITS)
23 
24 #define FPROBE_HASH_BITS 6
25 #define FPROBE_TABLE_SIZE (1 << FPROBE_HASH_BITS)
26 
27 #define SIZE_IN_LONG(x) ((x + sizeof(long) - 1) >> (sizeof(long) == 8 ? 3 : 2))
28 
29 /*
30  * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still
31  *   exists. The key is the address of fprobe instance.
32  * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe
33  *   instance related to the function address. The key is the ftrace IP
34  *   address.
35  *
36  * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array[*].fp
37  * are set NULL and delete those from both hash tables (by hlist_del_rcu).
38  * After an RCU grace period, the fprobe_hlist itself will be released.
39  *
40  * fprobe_table and fprobe_ip_table can be accessed from either
41  *  - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held.
42  *  - RCU hlist traversal under disabling preempt
43  */
44 static struct hlist_head fprobe_table[FPROBE_TABLE_SIZE];
45 static struct rhltable fprobe_ip_table;
46 static DEFINE_MUTEX(fprobe_mutex);
47 static struct fgraph_ops fprobe_graph_ops;
48 
49 static u32 fprobe_node_hashfn(const void *data, u32 len, u32 seed)
50 {
51 	return hash_ptr(*(unsigned long **)data, 32);
52 }
53 
54 static int fprobe_node_cmp(struct rhashtable_compare_arg *arg,
55 			   const void *ptr)
56 {
57 	unsigned long key = *(unsigned long *)arg->key;
58 	const struct fprobe_hlist_node *n = ptr;
59 
60 	return n->addr != key;
61 }
62 
63 static u32 fprobe_node_obj_hashfn(const void *data, u32 len, u32 seed)
64 {
65 	const struct fprobe_hlist_node *n = data;
66 
67 	return hash_ptr((void *)n->addr, 32);
68 }
69 
70 static const struct rhashtable_params fprobe_rht_params = {
71 	.head_offset		= offsetof(struct fprobe_hlist_node, hlist),
72 	.key_offset		= offsetof(struct fprobe_hlist_node, addr),
73 	.key_len		= sizeof_field(struct fprobe_hlist_node, addr),
74 	.hashfn			= fprobe_node_hashfn,
75 	.obj_hashfn		= fprobe_node_obj_hashfn,
76 	.obj_cmpfn		= fprobe_node_cmp,
77 	.automatic_shrinking	= true,
78 };
79 
80 /* Node insertion and deletion requires the fprobe_mutex */
81 static int insert_fprobe_node(struct fprobe_hlist_node *node)
82 {
83 	lockdep_assert_held(&fprobe_mutex);
84 
85 	return rhltable_insert(&fprobe_ip_table, &node->hlist, fprobe_rht_params);
86 }
87 
88 /* Return true if there are synonims */
89 static bool delete_fprobe_node(struct fprobe_hlist_node *node)
90 {
91 	lockdep_assert_held(&fprobe_mutex);
92 	bool ret;
93 
94 	/* Avoid double deleting */
95 	if (READ_ONCE(node->fp) != NULL) {
96 		WRITE_ONCE(node->fp, NULL);
97 		rhltable_remove(&fprobe_ip_table, &node->hlist,
98 				fprobe_rht_params);
99 	}
100 
101 	rcu_read_lock();
102 	ret = !!rhltable_lookup(&fprobe_ip_table, &node->addr,
103 				fprobe_rht_params);
104 	rcu_read_unlock();
105 
106 	return ret;
107 }
108 
109 /* Check existence of the fprobe */
110 static bool is_fprobe_still_exist(struct fprobe *fp)
111 {
112 	struct hlist_head *head;
113 	struct fprobe_hlist *fph;
114 
115 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
116 	hlist_for_each_entry_rcu(fph, head, hlist,
117 				 lockdep_is_held(&fprobe_mutex)) {
118 		if (fph->fp == fp)
119 			return true;
120 	}
121 	return false;
122 }
123 NOKPROBE_SYMBOL(is_fprobe_still_exist);
124 
125 static int add_fprobe_hash(struct fprobe *fp)
126 {
127 	struct fprobe_hlist *fph = fp->hlist_array;
128 	struct hlist_head *head;
129 
130 	lockdep_assert_held(&fprobe_mutex);
131 
132 	if (WARN_ON_ONCE(!fph))
133 		return -EINVAL;
134 
135 	if (is_fprobe_still_exist(fp))
136 		return -EEXIST;
137 
138 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
139 	hlist_add_head_rcu(&fp->hlist_array->hlist, head);
140 	return 0;
141 }
142 
143 static int del_fprobe_hash(struct fprobe *fp)
144 {
145 	struct fprobe_hlist *fph = fp->hlist_array;
146 
147 	lockdep_assert_held(&fprobe_mutex);
148 
149 	if (WARN_ON_ONCE(!fph))
150 		return -EINVAL;
151 
152 	if (!is_fprobe_still_exist(fp))
153 		return -ENOENT;
154 
155 	fph->fp = NULL;
156 	hlist_del_rcu(&fph->hlist);
157 	return 0;
158 }
159 
160 #ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER
161 
162 /* The arch should encode fprobe_header info into one unsigned long */
163 #define FPROBE_HEADER_SIZE_IN_LONG	1
164 
165 static inline bool write_fprobe_header(unsigned long *stack,
166 					struct fprobe *fp, unsigned int size_words)
167 {
168 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD ||
169 			 !arch_fprobe_header_encodable(fp)))
170 		return false;
171 
172 	*stack = arch_encode_fprobe_header(fp, size_words);
173 	return true;
174 }
175 
176 static inline void read_fprobe_header(unsigned long *stack,
177 					struct fprobe **fp, unsigned int *size_words)
178 {
179 	*fp = arch_decode_fprobe_header_fp(*stack);
180 	*size_words = arch_decode_fprobe_header_size(*stack);
181 }
182 
183 #else
184 
185 /* Generic fprobe_header */
186 struct __fprobe_header {
187 	struct fprobe *fp;
188 	unsigned long size_words;
189 } __packed;
190 
191 #define FPROBE_HEADER_SIZE_IN_LONG	SIZE_IN_LONG(sizeof(struct __fprobe_header))
192 
193 static inline bool write_fprobe_header(unsigned long *stack,
194 					struct fprobe *fp, unsigned int size_words)
195 {
196 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
197 
198 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD))
199 		return false;
200 
201 	fph->fp = fp;
202 	fph->size_words = size_words;
203 	return true;
204 }
205 
206 static inline void read_fprobe_header(unsigned long *stack,
207 					struct fprobe **fp, unsigned int *size_words)
208 {
209 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
210 
211 	*fp = fph->fp;
212 	*size_words = fph->size_words;
213 }
214 
215 #endif
216 
217 /*
218  * fprobe shadow stack management:
219  * Since fprobe shares a single fgraph_ops, it needs to share the stack entry
220  * among the probes on the same function exit. Note that a new probe can be
221  * registered before a target function is returning, we can not use the hash
222  * table to find the corresponding probes. Thus the probe address is stored on
223  * the shadow stack with its entry data size.
224  *
225  */
226 static inline int __fprobe_handler(unsigned long ip, unsigned long parent_ip,
227 				   struct fprobe *fp, struct ftrace_regs *fregs,
228 				   void *data)
229 {
230 	if (!fp->entry_handler)
231 		return 0;
232 
233 	return fp->entry_handler(fp, ip, parent_ip, fregs, data);
234 }
235 
236 static inline int __fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
237 					  struct fprobe *fp, struct ftrace_regs *fregs,
238 					  void *data)
239 {
240 	int ret;
241 	/*
242 	 * This user handler is shared with other kprobes and is not expected to be
243 	 * called recursively. So if any other kprobe handler is running, this will
244 	 * exit as kprobe does. See the section 'Share the callbacks with kprobes'
245 	 * in Documentation/trace/fprobe.rst for more information.
246 	 */
247 	if (unlikely(kprobe_running())) {
248 		fp->nmissed++;
249 		return 0;
250 	}
251 
252 	kprobe_busy_begin();
253 	ret = __fprobe_handler(ip, parent_ip, fp, fregs, data);
254 	kprobe_busy_end();
255 	return ret;
256 }
257 
258 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_ARGS) || defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS)
259 /* ftrace_ops callback, this processes fprobes which have only entry_handler. */
260 static void fprobe_ftrace_entry(unsigned long ip, unsigned long parent_ip,
261 	struct ftrace_ops *ops, struct ftrace_regs *fregs)
262 {
263 	struct fprobe_hlist_node *node;
264 	struct rhlist_head *head, *pos;
265 	struct fprobe *fp;
266 	int bit;
267 
268 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
269 	if (bit < 0)
270 		return;
271 
272 	/*
273 	 * ftrace_test_recursion_trylock() disables preemption, but
274 	 * rhltable_lookup() checks whether rcu_read_lcok is held.
275 	 * So we take rcu_read_lock() here.
276 	 */
277 	rcu_read_lock();
278 	head = rhltable_lookup(&fprobe_ip_table, &ip, fprobe_rht_params);
279 
280 	rhl_for_each_entry_rcu(node, pos, head, hlist) {
281 		if (node->addr != ip)
282 			break;
283 		fp = READ_ONCE(node->fp);
284 		if (unlikely(!fp || fprobe_disabled(fp) || fp->exit_handler))
285 			continue;
286 
287 		if (fprobe_shared_with_kprobes(fp))
288 			__fprobe_kprobe_handler(ip, parent_ip, fp, fregs, NULL);
289 		else
290 			__fprobe_handler(ip, parent_ip, fp, fregs, NULL);
291 	}
292 	rcu_read_unlock();
293 	ftrace_test_recursion_unlock(bit);
294 }
295 NOKPROBE_SYMBOL(fprobe_ftrace_entry);
296 
297 static struct ftrace_ops fprobe_ftrace_ops = {
298 	.func	= fprobe_ftrace_entry,
299 	.flags	= FTRACE_OPS_FL_SAVE_ARGS,
300 };
301 static int fprobe_ftrace_active;
302 
303 static int fprobe_ftrace_add_ips(unsigned long *addrs, int num)
304 {
305 	int ret;
306 
307 	lockdep_assert_held(&fprobe_mutex);
308 
309 	ret = ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 0, 0);
310 	if (ret)
311 		return ret;
312 
313 	if (!fprobe_ftrace_active) {
314 		ret = register_ftrace_function(&fprobe_ftrace_ops);
315 		if (ret) {
316 			ftrace_free_filter(&fprobe_ftrace_ops);
317 			return ret;
318 		}
319 	}
320 	fprobe_ftrace_active++;
321 	return 0;
322 }
323 
324 static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num)
325 {
326 	lockdep_assert_held(&fprobe_mutex);
327 
328 	fprobe_ftrace_active--;
329 	if (!fprobe_ftrace_active)
330 		unregister_ftrace_function(&fprobe_ftrace_ops);
331 	if (num)
332 		ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 1, 0);
333 }
334 
335 static bool fprobe_is_ftrace(struct fprobe *fp)
336 {
337 	return !fp->exit_handler;
338 }
339 
340 #ifdef CONFIG_MODULES
341 static void fprobe_set_ips(unsigned long *ips, unsigned int cnt, int remove,
342 			   int reset)
343 {
344 	ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, remove, reset);
345 	ftrace_set_filter_ips(&fprobe_ftrace_ops, ips, cnt, remove, reset);
346 }
347 #endif
348 #else
349 static int fprobe_ftrace_add_ips(unsigned long *addrs, int num)
350 {
351 	return -ENOENT;
352 }
353 
354 static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num)
355 {
356 }
357 
358 static bool fprobe_is_ftrace(struct fprobe *fp)
359 {
360 	return false;
361 }
362 
363 #ifdef CONFIG_MODULES
364 static void fprobe_set_ips(unsigned long *ips, unsigned int cnt, int remove,
365 			   int reset)
366 {
367 	ftrace_set_filter_ips(&fprobe_graph_ops.ops, ips, cnt, remove, reset);
368 }
369 #endif
370 #endif /* !CONFIG_DYNAMIC_FTRACE_WITH_ARGS && !CONFIG_DYNAMIC_FTRACE_WITH_REGS */
371 
372 /* fgraph_ops callback, this processes fprobes which have exit_handler. */
373 static int fprobe_fgraph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops,
374 			       struct ftrace_regs *fregs)
375 {
376 	unsigned long *fgraph_data = NULL;
377 	unsigned long func = trace->func;
378 	struct fprobe_hlist_node *node;
379 	struct rhlist_head *head, *pos;
380 	unsigned long ret_ip;
381 	int reserved_words;
382 	struct fprobe *fp;
383 	int used, ret;
384 
385 	if (WARN_ON_ONCE(!fregs))
386 		return 0;
387 
388 	guard(rcu)();
389 	head = rhltable_lookup(&fprobe_ip_table, &func, fprobe_rht_params);
390 	reserved_words = 0;
391 	rhl_for_each_entry_rcu(node, pos, head, hlist) {
392 		if (node->addr != func)
393 			continue;
394 		fp = READ_ONCE(node->fp);
395 		if (!fp || !fp->exit_handler)
396 			continue;
397 		/*
398 		 * Since fprobe can be enabled until the next loop, we ignore the
399 		 * fprobe's disabled flag in this loop.
400 		 */
401 		reserved_words +=
402 			FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size);
403 	}
404 	if (reserved_words) {
405 		fgraph_data = fgraph_reserve_data(gops->idx, reserved_words * sizeof(long));
406 		if (unlikely(!fgraph_data)) {
407 			rhl_for_each_entry_rcu(node, pos, head, hlist) {
408 				if (node->addr != func)
409 					continue;
410 				fp = READ_ONCE(node->fp);
411 				if (fp && !fprobe_disabled(fp) && !fprobe_is_ftrace(fp))
412 					fp->nmissed++;
413 			}
414 			return 0;
415 		}
416 	}
417 
418 	/*
419 	 * TODO: recursion detection has been done in the fgraph. Thus we need
420 	 * to add a callback to increment missed counter.
421 	 */
422 	ret_ip = ftrace_regs_get_return_address(fregs);
423 	used = 0;
424 	rhl_for_each_entry_rcu(node, pos, head, hlist) {
425 		int data_size;
426 		void *data;
427 
428 		if (node->addr != func)
429 			continue;
430 		fp = READ_ONCE(node->fp);
431 		if (unlikely(!fp || fprobe_disabled(fp) || fprobe_is_ftrace(fp)))
432 			continue;
433 
434 		data_size = fp->entry_data_size;
435 		if (data_size && fp->exit_handler)
436 			data = fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG;
437 		else
438 			data = NULL;
439 
440 		if (fprobe_shared_with_kprobes(fp))
441 			ret = __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data);
442 		else
443 			ret = __fprobe_handler(func, ret_ip, fp, fregs, data);
444 
445 		/* If entry_handler returns !0, nmissed is not counted but skips exit_handler. */
446 		if (!ret && fp->exit_handler) {
447 			int size_words = SIZE_IN_LONG(data_size);
448 
449 			if (write_fprobe_header(&fgraph_data[used], fp, size_words))
450 				used += FPROBE_HEADER_SIZE_IN_LONG + size_words;
451 		}
452 	}
453 
454 	/* If any exit_handler is set, data must be used. */
455 	return used != 0;
456 }
457 NOKPROBE_SYMBOL(fprobe_fgraph_entry);
458 
459 static void fprobe_return(struct ftrace_graph_ret *trace,
460 			  struct fgraph_ops *gops,
461 			  struct ftrace_regs *fregs)
462 {
463 	unsigned long *fgraph_data = NULL;
464 	unsigned long ret_ip;
465 	struct fprobe *fp;
466 	int size, curr;
467 	int size_words;
468 
469 	fgraph_data = (unsigned long *)fgraph_retrieve_data(gops->idx, &size);
470 	if (WARN_ON_ONCE(!fgraph_data))
471 		return;
472 	size_words = SIZE_IN_LONG(size);
473 	ret_ip = ftrace_regs_get_instruction_pointer(fregs);
474 
475 	preempt_disable_notrace();
476 
477 	curr = 0;
478 	while (size_words > curr) {
479 		read_fprobe_header(&fgraph_data[curr], &fp, &size);
480 		if (!fp)
481 			break;
482 		curr += FPROBE_HEADER_SIZE_IN_LONG;
483 		if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) {
484 			if (WARN_ON_ONCE(curr + size > size_words))
485 				break;
486 			fp->exit_handler(fp, trace->func, ret_ip, fregs,
487 					 size ? fgraph_data + curr : NULL);
488 		}
489 		curr += size;
490 	}
491 	preempt_enable_notrace();
492 }
493 NOKPROBE_SYMBOL(fprobe_return);
494 
495 static struct fgraph_ops fprobe_graph_ops = {
496 	.entryfunc	= fprobe_fgraph_entry,
497 	.retfunc	= fprobe_return,
498 };
499 static int fprobe_graph_active;
500 
501 /* Add @addrs to the ftrace filter and register fgraph if needed. */
502 static int fprobe_graph_add_ips(unsigned long *addrs, int num)
503 {
504 	int ret;
505 
506 	lockdep_assert_held(&fprobe_mutex);
507 
508 	ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0);
509 	if (ret)
510 		return ret;
511 
512 	if (!fprobe_graph_active) {
513 		ret = register_ftrace_graph(&fprobe_graph_ops);
514 		if (WARN_ON_ONCE(ret)) {
515 			ftrace_free_filter(&fprobe_graph_ops.ops);
516 			return ret;
517 		}
518 	}
519 	fprobe_graph_active++;
520 	return 0;
521 }
522 
523 /* Remove @addrs from the ftrace filter and unregister fgraph if possible. */
524 static void fprobe_graph_remove_ips(unsigned long *addrs, int num)
525 {
526 	lockdep_assert_held(&fprobe_mutex);
527 
528 	fprobe_graph_active--;
529 	/* Q: should we unregister it ? */
530 	if (!fprobe_graph_active)
531 		unregister_ftrace_graph(&fprobe_graph_ops);
532 
533 	if (num)
534 		ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
535 }
536 
537 #ifdef CONFIG_MODULES
538 
539 #define FPROBE_IPS_BATCH_INIT 8
540 /* instruction pointer address list */
541 struct fprobe_addr_list {
542 	int index;
543 	int size;
544 	unsigned long *addrs;
545 };
546 
547 static int fprobe_addr_list_add(struct fprobe_addr_list *alist, unsigned long addr)
548 {
549 	unsigned long *addrs;
550 
551 	/* Previously we failed to expand the list. */
552 	if (alist->index == alist->size)
553 		return -ENOSPC;
554 
555 	alist->addrs[alist->index++] = addr;
556 	if (alist->index < alist->size)
557 		return 0;
558 
559 	/* Expand the address list */
560 	addrs = kcalloc(alist->size * 2, sizeof(*addrs), GFP_KERNEL);
561 	if (!addrs)
562 		return -ENOMEM;
563 
564 	memcpy(addrs, alist->addrs, alist->size * sizeof(*addrs));
565 	alist->size *= 2;
566 	kfree(alist->addrs);
567 	alist->addrs = addrs;
568 
569 	return 0;
570 }
571 
572 static void fprobe_remove_node_in_module(struct module *mod, struct fprobe_hlist_node *node,
573 					 struct fprobe_addr_list *alist)
574 {
575 	if (!within_module(node->addr, mod))
576 		return;
577 	if (delete_fprobe_node(node))
578 		return;
579 	/*
580 	 * If failed to update alist, just continue to update hlist.
581 	 * Therefore, at list user handler will not hit anymore.
582 	 */
583 	fprobe_addr_list_add(alist, node->addr);
584 }
585 
586 /* Handle module unloading to manage fprobe_ip_table. */
587 static int fprobe_module_callback(struct notifier_block *nb,
588 				  unsigned long val, void *data)
589 {
590 	struct fprobe_addr_list alist = {.size = FPROBE_IPS_BATCH_INIT};
591 	struct fprobe_hlist_node *node;
592 	struct rhashtable_iter iter;
593 	struct module *mod = data;
594 
595 	if (val != MODULE_STATE_GOING)
596 		return NOTIFY_DONE;
597 
598 	alist.addrs = kcalloc(alist.size, sizeof(*alist.addrs), GFP_KERNEL);
599 	/* If failed to alloc memory, we can not remove ips from hash. */
600 	if (!alist.addrs)
601 		return NOTIFY_DONE;
602 
603 	mutex_lock(&fprobe_mutex);
604 	rhltable_walk_enter(&fprobe_ip_table, &iter);
605 	do {
606 		rhashtable_walk_start(&iter);
607 
608 		while ((node = rhashtable_walk_next(&iter)) && !IS_ERR(node))
609 			fprobe_remove_node_in_module(mod, node, &alist);
610 
611 		rhashtable_walk_stop(&iter);
612 	} while (node == ERR_PTR(-EAGAIN));
613 	rhashtable_walk_exit(&iter);
614 
615 	if (alist.index > 0)
616 		fprobe_set_ips(alist.addrs, alist.index, 1, 0);
617 	mutex_unlock(&fprobe_mutex);
618 
619 	kfree(alist.addrs);
620 
621 	return NOTIFY_DONE;
622 }
623 
624 static struct notifier_block fprobe_module_nb = {
625 	.notifier_call = fprobe_module_callback,
626 	.priority = 0,
627 };
628 
629 static int __init init_fprobe_module(void)
630 {
631 	return register_module_notifier(&fprobe_module_nb);
632 }
633 early_initcall(init_fprobe_module);
634 #endif
635 
636 static int symbols_cmp(const void *a, const void *b)
637 {
638 	const char **str_a = (const char **) a;
639 	const char **str_b = (const char **) b;
640 
641 	return strcmp(*str_a, *str_b);
642 }
643 
644 /* Convert ftrace location address from symbols */
645 static unsigned long *get_ftrace_locations(const char **syms, int num)
646 {
647 	unsigned long *addrs;
648 
649 	/* Convert symbols to symbol address */
650 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
651 	if (!addrs)
652 		return ERR_PTR(-ENOMEM);
653 
654 	/* ftrace_lookup_symbols expects sorted symbols */
655 	sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
656 
657 	if (!ftrace_lookup_symbols(syms, num, addrs))
658 		return addrs;
659 
660 	kfree(addrs);
661 	return ERR_PTR(-ENOENT);
662 }
663 
664 struct filter_match_data {
665 	const char *filter;
666 	const char *notfilter;
667 	size_t index;
668 	size_t size;
669 	unsigned long *addrs;
670 	struct module **mods;
671 };
672 
673 static int filter_match_callback(void *data, const char *name, unsigned long addr)
674 {
675 	struct filter_match_data *match = data;
676 
677 	if (!glob_match(match->filter, name) ||
678 	    (match->notfilter && glob_match(match->notfilter, name)))
679 		return 0;
680 
681 	if (!ftrace_location(addr))
682 		return 0;
683 
684 	if (match->addrs) {
685 		struct module *mod = __module_text_address(addr);
686 
687 		if (mod && !try_module_get(mod))
688 			return 0;
689 
690 		match->mods[match->index] = mod;
691 		match->addrs[match->index] = addr;
692 	}
693 	match->index++;
694 	return match->index == match->size;
695 }
696 
697 /*
698  * Make IP list from the filter/no-filter glob patterns.
699  * Return the number of matched symbols, or errno.
700  * If @addrs == NULL, this just counts the number of matched symbols. If @addrs
701  * is passed with an array, we need to pass the an @mods array of the same size
702  * to increment the module refcount for each symbol.
703  * This means we also need to call `module_put` for each element of @mods after
704  * using the @addrs.
705  */
706 static int get_ips_from_filter(const char *filter, const char *notfilter,
707 			       unsigned long *addrs, struct module **mods,
708 			       size_t size)
709 {
710 	struct filter_match_data match = { .filter = filter, .notfilter = notfilter,
711 		.index = 0, .size = size, .addrs = addrs, .mods = mods};
712 	int ret;
713 
714 	if (addrs && !mods)
715 		return -EINVAL;
716 
717 	ret = kallsyms_on_each_symbol(filter_match_callback, &match);
718 	if (ret < 0)
719 		return ret;
720 	if (IS_ENABLED(CONFIG_MODULES)) {
721 		ret = module_kallsyms_on_each_symbol(NULL, filter_match_callback, &match);
722 		if (ret < 0)
723 			return ret;
724 	}
725 
726 	return match.index ?: -ENOENT;
727 }
728 
729 static void fprobe_fail_cleanup(struct fprobe *fp)
730 {
731 	kfree(fp->hlist_array);
732 	fp->hlist_array = NULL;
733 }
734 
735 /* Initialize the fprobe data structure. */
736 static int fprobe_init(struct fprobe *fp, unsigned long *addrs, int num)
737 {
738 	struct fprobe_hlist *hlist_array;
739 	unsigned long addr;
740 	int size, i;
741 
742 	if (!fp || !addrs || num <= 0)
743 		return -EINVAL;
744 
745 	size = ALIGN(fp->entry_data_size, sizeof(long));
746 	if (size > MAX_FPROBE_DATA_SIZE)
747 		return -E2BIG;
748 	fp->entry_data_size = size;
749 
750 	hlist_array = kzalloc_flex(*hlist_array, array, num);
751 	if (!hlist_array)
752 		return -ENOMEM;
753 
754 	fp->nmissed = 0;
755 
756 	hlist_array->size = num;
757 	fp->hlist_array = hlist_array;
758 	hlist_array->fp = fp;
759 	for (i = 0; i < num; i++) {
760 		hlist_array->array[i].fp = fp;
761 		addr = ftrace_location(addrs[i]);
762 		if (!addr) {
763 			fprobe_fail_cleanup(fp);
764 			return -ENOENT;
765 		}
766 		hlist_array->array[i].addr = addr;
767 	}
768 	return 0;
769 }
770 
771 #define FPROBE_IPS_MAX	INT_MAX
772 
773 int fprobe_count_ips_from_filter(const char *filter, const char *notfilter)
774 {
775 	return get_ips_from_filter(filter, notfilter, NULL, NULL, FPROBE_IPS_MAX);
776 }
777 
778 /**
779  * register_fprobe() - Register fprobe to ftrace by pattern.
780  * @fp: A fprobe data structure to be registered.
781  * @filter: A wildcard pattern of probed symbols.
782  * @notfilter: A wildcard pattern of NOT probed symbols.
783  *
784  * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
785  * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
786  *
787  * Return 0 if @fp is registered successfully, -errno if not.
788  */
789 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
790 {
791 	unsigned long *addrs __free(kfree) = NULL;
792 	struct module **mods __free(kfree) = NULL;
793 	int ret, num;
794 
795 	if (!fp || !filter)
796 		return -EINVAL;
797 
798 	num = get_ips_from_filter(filter, notfilter, NULL, NULL, FPROBE_IPS_MAX);
799 	if (num < 0)
800 		return num;
801 
802 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
803 	if (!addrs)
804 		return -ENOMEM;
805 
806 	mods = kzalloc_objs(*mods, num);
807 	if (!mods)
808 		return -ENOMEM;
809 
810 	ret = get_ips_from_filter(filter, notfilter, addrs, mods, num);
811 	if (ret < 0)
812 		return ret;
813 
814 	ret = register_fprobe_ips(fp, addrs, ret);
815 
816 	for (int i = 0; i < num; i++) {
817 		if (mods[i])
818 			module_put(mods[i]);
819 	}
820 	return ret;
821 }
822 EXPORT_SYMBOL_GPL(register_fprobe);
823 
824 /**
825  * register_fprobe_ips() - Register fprobe to ftrace by address.
826  * @fp: A fprobe data structure to be registered.
827  * @addrs: An array of target function address.
828  * @num: The number of entries of @addrs.
829  *
830  * Register @fp to ftrace for enabling the probe on the address given by @addrs.
831  * The @addrs must be the addresses of ftrace location address, which may be
832  * the symbol address + arch-dependent offset.
833  * If you unsure what this mean, please use other registration functions.
834  *
835  * Return 0 if @fp is registered successfully, -errno if not.
836  */
837 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
838 {
839 	struct fprobe_hlist *hlist_array;
840 	int ret, i;
841 
842 	ret = fprobe_init(fp, addrs, num);
843 	if (ret)
844 		return ret;
845 
846 	mutex_lock(&fprobe_mutex);
847 
848 	hlist_array = fp->hlist_array;
849 	if (fprobe_is_ftrace(fp))
850 		ret = fprobe_ftrace_add_ips(addrs, num);
851 	else
852 		ret = fprobe_graph_add_ips(addrs, num);
853 
854 	if (!ret) {
855 		add_fprobe_hash(fp);
856 		for (i = 0; i < hlist_array->size; i++) {
857 			ret = insert_fprobe_node(&hlist_array->array[i]);
858 			if (ret)
859 				break;
860 		}
861 		/* fallback on insert error */
862 		if (ret) {
863 			for (i--; i >= 0; i--)
864 				delete_fprobe_node(&hlist_array->array[i]);
865 		}
866 	}
867 	mutex_unlock(&fprobe_mutex);
868 
869 	if (ret)
870 		fprobe_fail_cleanup(fp);
871 
872 	return ret;
873 }
874 EXPORT_SYMBOL_GPL(register_fprobe_ips);
875 
876 /**
877  * register_fprobe_syms() - Register fprobe to ftrace by symbols.
878  * @fp: A fprobe data structure to be registered.
879  * @syms: An array of target symbols.
880  * @num: The number of entries of @syms.
881  *
882  * Register @fp to the symbols given by @syms array. This will be useful if
883  * you are sure the symbols exist in the kernel.
884  *
885  * Return 0 if @fp is registered successfully, -errno if not.
886  */
887 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
888 {
889 	unsigned long *addrs;
890 	int ret;
891 
892 	if (!fp || !syms || num <= 0)
893 		return -EINVAL;
894 
895 	addrs = get_ftrace_locations(syms, num);
896 	if (IS_ERR(addrs))
897 		return PTR_ERR(addrs);
898 
899 	ret = register_fprobe_ips(fp, addrs, num);
900 
901 	kfree(addrs);
902 
903 	return ret;
904 }
905 EXPORT_SYMBOL_GPL(register_fprobe_syms);
906 
907 bool fprobe_is_registered(struct fprobe *fp)
908 {
909 	if (!fp || !fp->hlist_array)
910 		return false;
911 	return true;
912 }
913 
914 /**
915  * unregister_fprobe() - Unregister fprobe.
916  * @fp: A fprobe data structure to be unregistered.
917  *
918  * Unregister fprobe (and remove ftrace hooks from the function entries).
919  *
920  * Return 0 if @fp is unregistered successfully, -errno if not.
921  */
922 int unregister_fprobe(struct fprobe *fp)
923 {
924 	struct fprobe_hlist *hlist_array;
925 	unsigned long *addrs = NULL;
926 	int ret = 0, i, count;
927 
928 	mutex_lock(&fprobe_mutex);
929 	if (!fp || !is_fprobe_still_exist(fp)) {
930 		ret = -EINVAL;
931 		goto out;
932 	}
933 
934 	hlist_array = fp->hlist_array;
935 	addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL);
936 	if (!addrs) {
937 		ret = -ENOMEM;	/* TODO: Fallback to one-by-one loop */
938 		goto out;
939 	}
940 
941 	/* Remove non-synonim ips from table and hash */
942 	count = 0;
943 	for (i = 0; i < hlist_array->size; i++) {
944 		if (!delete_fprobe_node(&hlist_array->array[i]))
945 			addrs[count++] = hlist_array->array[i].addr;
946 	}
947 	del_fprobe_hash(fp);
948 
949 	if (fprobe_is_ftrace(fp))
950 		fprobe_ftrace_remove_ips(addrs, count);
951 	else
952 		fprobe_graph_remove_ips(addrs, count);
953 
954 	kfree_rcu(hlist_array, rcu);
955 	fp->hlist_array = NULL;
956 
957 out:
958 	mutex_unlock(&fprobe_mutex);
959 
960 	kfree(addrs);
961 	return ret;
962 }
963 EXPORT_SYMBOL_GPL(unregister_fprobe);
964 
965 static int __init fprobe_initcall(void)
966 {
967 	rhltable_init(&fprobe_ip_table, &fprobe_rht_params);
968 	return 0;
969 }
970 core_initcall(fprobe_initcall);
971