xref: /linux-6.15/kernel/trace/fprobe.c (revision d24fa977)
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/slab.h>
14 #include <linux/sort.h>
15 
16 #include <asm/fprobe.h>
17 
18 #include "trace.h"
19 
20 #define FPROBE_IP_HASH_BITS 8
21 #define FPROBE_IP_TABLE_SIZE (1 << FPROBE_IP_HASH_BITS)
22 
23 #define FPROBE_HASH_BITS 6
24 #define FPROBE_TABLE_SIZE (1 << FPROBE_HASH_BITS)
25 
26 #define SIZE_IN_LONG(x) ((x + sizeof(long) - 1) >> (sizeof(long) == 8 ? 3 : 2))
27 
28 /*
29  * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still
30  *   exists. The key is the address of fprobe instance.
31  * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe
32  *   instance related to the funciton address. The key is the ftrace IP
33  *   address.
34  *
35  * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array[*].fp
36  * are set NULL and delete those from both hash tables (by hlist_del_rcu).
37  * After an RCU grace period, the fprobe_hlist itself will be released.
38  *
39  * fprobe_table and fprobe_ip_table can be accessed from either
40  *  - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held.
41  *  - RCU hlist traversal under disabling preempt
42  */
43 static struct hlist_head fprobe_table[FPROBE_TABLE_SIZE];
44 static struct hlist_head fprobe_ip_table[FPROBE_IP_TABLE_SIZE];
45 static DEFINE_MUTEX(fprobe_mutex);
46 
47 /*
48  * Find first fprobe in the hlist. It will be iterated twice in the entry
49  * probe, once for correcting the total required size, the second time is
50  * calling back the user handlers.
51  * Thus the hlist in the fprobe_table must be sorted and new probe needs to
52  * be added *before* the first fprobe.
53  */
54 static struct fprobe_hlist_node *find_first_fprobe_node(unsigned long ip)
55 {
56 	struct fprobe_hlist_node *node;
57 	struct hlist_head *head;
58 
59 	head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)];
60 	hlist_for_each_entry_rcu(node, head, hlist,
61 				 lockdep_is_held(&fprobe_mutex)) {
62 		if (node->addr == ip)
63 			return node;
64 	}
65 	return NULL;
66 }
67 NOKPROBE_SYMBOL(find_first_fprobe_node);
68 
69 /* Node insertion and deletion requires the fprobe_mutex */
70 static void insert_fprobe_node(struct fprobe_hlist_node *node)
71 {
72 	unsigned long ip = node->addr;
73 	struct fprobe_hlist_node *next;
74 	struct hlist_head *head;
75 
76 	lockdep_assert_held(&fprobe_mutex);
77 
78 	next = find_first_fprobe_node(ip);
79 	if (next) {
80 		hlist_add_before_rcu(&node->hlist, &next->hlist);
81 		return;
82 	}
83 	head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)];
84 	hlist_add_head_rcu(&node->hlist, head);
85 }
86 
87 /* Return true if there are synonims */
88 static bool delete_fprobe_node(struct fprobe_hlist_node *node)
89 {
90 	lockdep_assert_held(&fprobe_mutex);
91 
92 	WRITE_ONCE(node->fp, NULL);
93 	hlist_del_rcu(&node->hlist);
94 	return !!find_first_fprobe_node(node->addr);
95 }
96 
97 /* Check existence of the fprobe */
98 static bool is_fprobe_still_exist(struct fprobe *fp)
99 {
100 	struct hlist_head *head;
101 	struct fprobe_hlist *fph;
102 
103 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
104 	hlist_for_each_entry_rcu(fph, head, hlist,
105 				 lockdep_is_held(&fprobe_mutex)) {
106 		if (fph->fp == fp)
107 			return true;
108 	}
109 	return false;
110 }
111 NOKPROBE_SYMBOL(is_fprobe_still_exist);
112 
113 static int add_fprobe_hash(struct fprobe *fp)
114 {
115 	struct fprobe_hlist *fph = fp->hlist_array;
116 	struct hlist_head *head;
117 
118 	lockdep_assert_held(&fprobe_mutex);
119 
120 	if (WARN_ON_ONCE(!fph))
121 		return -EINVAL;
122 
123 	if (is_fprobe_still_exist(fp))
124 		return -EEXIST;
125 
126 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
127 	hlist_add_head_rcu(&fp->hlist_array->hlist, head);
128 	return 0;
129 }
130 
131 static int del_fprobe_hash(struct fprobe *fp)
132 {
133 	struct fprobe_hlist *fph = fp->hlist_array;
134 
135 	lockdep_assert_held(&fprobe_mutex);
136 
137 	if (WARN_ON_ONCE(!fph))
138 		return -EINVAL;
139 
140 	if (!is_fprobe_still_exist(fp))
141 		return -ENOENT;
142 
143 	fph->fp = NULL;
144 	hlist_del_rcu(&fph->hlist);
145 	return 0;
146 }
147 
148 #ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER
149 
150 /* The arch should encode fprobe_header info into one unsigned long */
151 #define FPROBE_HEADER_SIZE_IN_LONG	1
152 
153 static inline bool write_fprobe_header(unsigned long *stack,
154 					struct fprobe *fp, unsigned int size_words)
155 {
156 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD ||
157 			 !arch_fprobe_header_encodable(fp)))
158 		return false;
159 
160 	*stack = arch_encode_fprobe_header(fp, size_words);
161 	return true;
162 }
163 
164 static inline void read_fprobe_header(unsigned long *stack,
165 					struct fprobe **fp, unsigned int *size_words)
166 {
167 	*fp = arch_decode_fprobe_header_fp(*stack);
168 	*size_words = arch_decode_fprobe_header_size(*stack);
169 }
170 
171 #else
172 
173 /* Generic fprobe_header */
174 struct __fprobe_header {
175 	struct fprobe *fp;
176 	unsigned long size_words;
177 } __packed;
178 
179 #define FPROBE_HEADER_SIZE_IN_LONG	SIZE_IN_LONG(sizeof(struct __fprobe_header))
180 
181 static inline bool write_fprobe_header(unsigned long *stack,
182 					struct fprobe *fp, unsigned int size_words)
183 {
184 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
185 
186 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD))
187 		return false;
188 
189 	fph->fp = fp;
190 	fph->size_words = size_words;
191 	return true;
192 }
193 
194 static inline void read_fprobe_header(unsigned long *stack,
195 					struct fprobe **fp, unsigned int *size_words)
196 {
197 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
198 
199 	*fp = fph->fp;
200 	*size_words = fph->size_words;
201 }
202 
203 #endif
204 
205 /*
206  * fprobe shadow stack management:
207  * Since fprobe shares a single fgraph_ops, it needs to share the stack entry
208  * among the probes on the same function exit. Note that a new probe can be
209  * registered before a target function is returning, we can not use the hash
210  * table to find the corresponding probes. Thus the probe address is stored on
211  * the shadow stack with its entry data size.
212  *
213  */
214 static inline int __fprobe_handler(unsigned long ip, unsigned long parent_ip,
215 				   struct fprobe *fp, struct ftrace_regs *fregs,
216 				   void *data)
217 {
218 	if (!fp->entry_handler)
219 		return 0;
220 
221 	return fp->entry_handler(fp, ip, parent_ip, fregs, data);
222 }
223 
224 static inline int __fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
225 					  struct fprobe *fp, struct ftrace_regs *fregs,
226 					  void *data)
227 {
228 	int ret;
229 	/*
230 	 * This user handler is shared with other kprobes and is not expected to be
231 	 * called recursively. So if any other kprobe handler is running, this will
232 	 * exit as kprobe does. See the section 'Share the callbacks with kprobes'
233 	 * in Documentation/trace/fprobe.rst for more information.
234 	 */
235 	if (unlikely(kprobe_running())) {
236 		fp->nmissed++;
237 		return 0;
238 	}
239 
240 	kprobe_busy_begin();
241 	ret = __fprobe_handler(ip, parent_ip, fp, fregs, data);
242 	kprobe_busy_end();
243 	return ret;
244 }
245 
246 static int fprobe_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops,
247 			struct ftrace_regs *fregs)
248 {
249 	struct fprobe_hlist_node *node, *first;
250 	unsigned long *fgraph_data = NULL;
251 	unsigned long func = trace->func;
252 	unsigned long ret_ip;
253 	int reserved_words;
254 	struct fprobe *fp;
255 	int used, ret;
256 
257 	if (WARN_ON_ONCE(!fregs))
258 		return 0;
259 
260 	first = node = find_first_fprobe_node(func);
261 	if (unlikely(!first))
262 		return 0;
263 
264 	reserved_words = 0;
265 	hlist_for_each_entry_from_rcu(node, hlist) {
266 		if (node->addr != func)
267 			break;
268 		fp = READ_ONCE(node->fp);
269 		if (!fp || !fp->exit_handler)
270 			continue;
271 		/*
272 		 * Since fprobe can be enabled until the next loop, we ignore the
273 		 * fprobe's disabled flag in this loop.
274 		 */
275 		reserved_words +=
276 			FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size);
277 	}
278 	node = first;
279 	if (reserved_words) {
280 		fgraph_data = fgraph_reserve_data(gops->idx, reserved_words * sizeof(long));
281 		if (unlikely(!fgraph_data)) {
282 			hlist_for_each_entry_from_rcu(node, hlist) {
283 				if (node->addr != func)
284 					break;
285 				fp = READ_ONCE(node->fp);
286 				if (fp && !fprobe_disabled(fp))
287 					fp->nmissed++;
288 			}
289 			return 0;
290 		}
291 	}
292 
293 	/*
294 	 * TODO: recursion detection has been done in the fgraph. Thus we need
295 	 * to add a callback to increment missed counter.
296 	 */
297 	ret_ip = ftrace_regs_get_return_address(fregs);
298 	used = 0;
299 	hlist_for_each_entry_from_rcu(node, hlist) {
300 		int data_size;
301 		void *data;
302 
303 		if (node->addr != func)
304 			break;
305 		fp = READ_ONCE(node->fp);
306 		if (!fp || fprobe_disabled(fp))
307 			continue;
308 
309 		data_size = fp->entry_data_size;
310 		if (data_size && fp->exit_handler)
311 			data = fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG;
312 		else
313 			data = NULL;
314 
315 		if (fprobe_shared_with_kprobes(fp))
316 			ret = __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data);
317 		else
318 			ret = __fprobe_handler(func, ret_ip, fp, fregs, data);
319 
320 		/* If entry_handler returns !0, nmissed is not counted but skips exit_handler. */
321 		if (!ret && fp->exit_handler) {
322 			int size_words = SIZE_IN_LONG(data_size);
323 
324 			if (write_fprobe_header(&fgraph_data[used], fp, size_words))
325 				used += FPROBE_HEADER_SIZE_IN_LONG + size_words;
326 		}
327 	}
328 	if (used < reserved_words)
329 		memset(fgraph_data + used, 0, reserved_words - used);
330 
331 	/* If any exit_handler is set, data must be used. */
332 	return used != 0;
333 }
334 NOKPROBE_SYMBOL(fprobe_entry);
335 
336 static void fprobe_return(struct ftrace_graph_ret *trace,
337 			  struct fgraph_ops *gops,
338 			  struct ftrace_regs *fregs)
339 {
340 	unsigned long *fgraph_data = NULL;
341 	unsigned long ret_ip;
342 	struct fprobe *fp;
343 	int size, curr;
344 	int size_words;
345 
346 	fgraph_data = (unsigned long *)fgraph_retrieve_data(gops->idx, &size);
347 	if (WARN_ON_ONCE(!fgraph_data))
348 		return;
349 	size_words = SIZE_IN_LONG(size);
350 	ret_ip = ftrace_regs_get_instruction_pointer(fregs);
351 
352 	preempt_disable();
353 
354 	curr = 0;
355 	while (size_words > curr) {
356 		read_fprobe_header(&fgraph_data[curr], &fp, &size);
357 		if (!fp)
358 			break;
359 		curr += FPROBE_HEADER_SIZE_IN_LONG;
360 		if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) {
361 			if (WARN_ON_ONCE(curr + size > size_words))
362 				break;
363 			fp->exit_handler(fp, trace->func, ret_ip, fregs,
364 					 size ? fgraph_data + curr : NULL);
365 		}
366 		curr += size;
367 	}
368 	preempt_enable();
369 }
370 NOKPROBE_SYMBOL(fprobe_return);
371 
372 static struct fgraph_ops fprobe_graph_ops = {
373 	.entryfunc	= fprobe_entry,
374 	.retfunc	= fprobe_return,
375 };
376 static int fprobe_graph_active;
377 
378 /* Add @addrs to the ftrace filter and register fgraph if needed. */
379 static int fprobe_graph_add_ips(unsigned long *addrs, int num)
380 {
381 	int ret;
382 
383 	lockdep_assert_held(&fprobe_mutex);
384 
385 	ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0);
386 	if (ret)
387 		return ret;
388 
389 	if (!fprobe_graph_active) {
390 		ret = register_ftrace_graph(&fprobe_graph_ops);
391 		if (WARN_ON_ONCE(ret)) {
392 			ftrace_free_filter(&fprobe_graph_ops.ops);
393 			return ret;
394 		}
395 	}
396 	fprobe_graph_active++;
397 	return 0;
398 }
399 
400 /* Remove @addrs from the ftrace filter and unregister fgraph if possible. */
401 static void fprobe_graph_remove_ips(unsigned long *addrs, int num)
402 {
403 	lockdep_assert_held(&fprobe_mutex);
404 
405 	fprobe_graph_active--;
406 	/* Q: should we unregister it ? */
407 	if (!fprobe_graph_active)
408 		unregister_ftrace_graph(&fprobe_graph_ops);
409 
410 	if (num)
411 		ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
412 }
413 
414 static int symbols_cmp(const void *a, const void *b)
415 {
416 	const char **str_a = (const char **) a;
417 	const char **str_b = (const char **) b;
418 
419 	return strcmp(*str_a, *str_b);
420 }
421 
422 /* Convert ftrace location address from symbols */
423 static unsigned long *get_ftrace_locations(const char **syms, int num)
424 {
425 	unsigned long *addrs;
426 
427 	/* Convert symbols to symbol address */
428 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
429 	if (!addrs)
430 		return ERR_PTR(-ENOMEM);
431 
432 	/* ftrace_lookup_symbols expects sorted symbols */
433 	sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
434 
435 	if (!ftrace_lookup_symbols(syms, num, addrs))
436 		return addrs;
437 
438 	kfree(addrs);
439 	return ERR_PTR(-ENOENT);
440 }
441 
442 struct filter_match_data {
443 	const char *filter;
444 	const char *notfilter;
445 	size_t index;
446 	size_t size;
447 	unsigned long *addrs;
448 	struct module **mods;
449 };
450 
451 static int filter_match_callback(void *data, const char *name, unsigned long addr)
452 {
453 	struct filter_match_data *match = data;
454 
455 	if (!glob_match(match->filter, name) ||
456 	    (match->notfilter && glob_match(match->notfilter, name)))
457 		return 0;
458 
459 	if (!ftrace_location(addr))
460 		return 0;
461 
462 	if (match->addrs) {
463 		struct module *mod = __module_text_address(addr);
464 
465 		if (mod && !try_module_get(mod))
466 			return 0;
467 
468 		match->mods[match->index] = mod;
469 		match->addrs[match->index] = addr;
470 	}
471 	match->index++;
472 	return match->index == match->size;
473 }
474 
475 /*
476  * Make IP list from the filter/no-filter glob patterns.
477  * Return the number of matched symbols, or errno.
478  * If @addrs == NULL, this just counts the number of matched symbols. If @addrs
479  * is passed with an array, we need to pass the an @mods array of the same size
480  * to increment the module refcount for each symbol.
481  * This means we also need to call `module_put` for each element of @mods after
482  * using the @addrs.
483  */
484 static int get_ips_from_filter(const char *filter, const char *notfilter,
485 			       unsigned long *addrs, struct module **mods,
486 			       size_t size)
487 {
488 	struct filter_match_data match = { .filter = filter, .notfilter = notfilter,
489 		.index = 0, .size = size, .addrs = addrs, .mods = mods};
490 	int ret;
491 
492 	if (addrs && !mods)
493 		return -EINVAL;
494 
495 	ret = kallsyms_on_each_symbol(filter_match_callback, &match);
496 	if (ret < 0)
497 		return ret;
498 	if (IS_ENABLED(CONFIG_MODULES)) {
499 		ret = module_kallsyms_on_each_symbol(NULL, filter_match_callback, &match);
500 		if (ret < 0)
501 			return ret;
502 	}
503 
504 	return match.index ?: -ENOENT;
505 }
506 
507 static void fprobe_fail_cleanup(struct fprobe *fp)
508 {
509 	kfree(fp->hlist_array);
510 	fp->hlist_array = NULL;
511 }
512 
513 /* Initialize the fprobe data structure. */
514 static int fprobe_init(struct fprobe *fp, unsigned long *addrs, int num)
515 {
516 	struct fprobe_hlist *hlist_array;
517 	unsigned long addr;
518 	int size, i;
519 
520 	if (!fp || !addrs || num <= 0)
521 		return -EINVAL;
522 
523 	size = ALIGN(fp->entry_data_size, sizeof(long));
524 	if (size > MAX_FPROBE_DATA_SIZE)
525 		return -E2BIG;
526 	fp->entry_data_size = size;
527 
528 	hlist_array = kzalloc(struct_size(hlist_array, array, num), GFP_KERNEL);
529 	if (!hlist_array)
530 		return -ENOMEM;
531 
532 	fp->nmissed = 0;
533 
534 	hlist_array->size = num;
535 	fp->hlist_array = hlist_array;
536 	hlist_array->fp = fp;
537 	for (i = 0; i < num; i++) {
538 		hlist_array->array[i].fp = fp;
539 		addr = ftrace_location(addrs[i]);
540 		if (!addr) {
541 			fprobe_fail_cleanup(fp);
542 			return -ENOENT;
543 		}
544 		hlist_array->array[i].addr = addr;
545 	}
546 	return 0;
547 }
548 
549 #define FPROBE_IPS_MAX	INT_MAX
550 
551 /**
552  * register_fprobe() - Register fprobe to ftrace by pattern.
553  * @fp: A fprobe data structure to be registered.
554  * @filter: A wildcard pattern of probed symbols.
555  * @notfilter: A wildcard pattern of NOT probed symbols.
556  *
557  * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
558  * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
559  *
560  * Return 0 if @fp is registered successfully, -errno if not.
561  */
562 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
563 {
564 	unsigned long *addrs __free(kfree) = NULL;
565 	struct module **mods __free(kfree) = NULL;
566 	int ret, num;
567 
568 	if (!fp || !filter)
569 		return -EINVAL;
570 
571 	num = get_ips_from_filter(filter, notfilter, NULL, NULL, FPROBE_IPS_MAX);
572 	if (num < 0)
573 		return num;
574 
575 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
576 	if (!addrs)
577 		return -ENOMEM;
578 
579 	mods = kcalloc(num, sizeof(*mods), GFP_KERNEL);
580 	if (!mods)
581 		return -ENOMEM;
582 
583 	ret = get_ips_from_filter(filter, notfilter, addrs, mods, num);
584 	if (ret < 0)
585 		return ret;
586 
587 	ret = register_fprobe_ips(fp, addrs, ret);
588 
589 	for (int i = 0; i < num; i++) {
590 		if (mods[i])
591 			module_put(mods[i]);
592 	}
593 	return ret;
594 }
595 EXPORT_SYMBOL_GPL(register_fprobe);
596 
597 /**
598  * register_fprobe_ips() - Register fprobe to ftrace by address.
599  * @fp: A fprobe data structure to be registered.
600  * @addrs: An array of target function address.
601  * @num: The number of entries of @addrs.
602  *
603  * Register @fp to ftrace for enabling the probe on the address given by @addrs.
604  * The @addrs must be the addresses of ftrace location address, which may be
605  * the symbol address + arch-dependent offset.
606  * If you unsure what this mean, please use other registration functions.
607  *
608  * Return 0 if @fp is registered successfully, -errno if not.
609  */
610 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
611 {
612 	struct fprobe_hlist *hlist_array;
613 	int ret, i;
614 
615 	ret = fprobe_init(fp, addrs, num);
616 	if (ret)
617 		return ret;
618 
619 	mutex_lock(&fprobe_mutex);
620 
621 	hlist_array = fp->hlist_array;
622 	ret = fprobe_graph_add_ips(addrs, num);
623 	if (!ret) {
624 		add_fprobe_hash(fp);
625 		for (i = 0; i < hlist_array->size; i++)
626 			insert_fprobe_node(&hlist_array->array[i]);
627 	}
628 	mutex_unlock(&fprobe_mutex);
629 
630 	if (ret)
631 		fprobe_fail_cleanup(fp);
632 
633 	return ret;
634 }
635 EXPORT_SYMBOL_GPL(register_fprobe_ips);
636 
637 /**
638  * register_fprobe_syms() - Register fprobe to ftrace by symbols.
639  * @fp: A fprobe data structure to be registered.
640  * @syms: An array of target symbols.
641  * @num: The number of entries of @syms.
642  *
643  * Register @fp to the symbols given by @syms array. This will be useful if
644  * you are sure the symbols exist in the kernel.
645  *
646  * Return 0 if @fp is registered successfully, -errno if not.
647  */
648 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
649 {
650 	unsigned long *addrs;
651 	int ret;
652 
653 	if (!fp || !syms || num <= 0)
654 		return -EINVAL;
655 
656 	addrs = get_ftrace_locations(syms, num);
657 	if (IS_ERR(addrs))
658 		return PTR_ERR(addrs);
659 
660 	ret = register_fprobe_ips(fp, addrs, num);
661 
662 	kfree(addrs);
663 
664 	return ret;
665 }
666 EXPORT_SYMBOL_GPL(register_fprobe_syms);
667 
668 bool fprobe_is_registered(struct fprobe *fp)
669 {
670 	if (!fp || !fp->hlist_array)
671 		return false;
672 	return true;
673 }
674 
675 /**
676  * unregister_fprobe() - Unregister fprobe.
677  * @fp: A fprobe data structure to be unregistered.
678  *
679  * Unregister fprobe (and remove ftrace hooks from the function entries).
680  *
681  * Return 0 if @fp is unregistered successfully, -errno if not.
682  */
683 int unregister_fprobe(struct fprobe *fp)
684 {
685 	struct fprobe_hlist *hlist_array;
686 	unsigned long *addrs = NULL;
687 	int ret = 0, i, count;
688 
689 	mutex_lock(&fprobe_mutex);
690 	if (!fp || !is_fprobe_still_exist(fp)) {
691 		ret = -EINVAL;
692 		goto out;
693 	}
694 
695 	hlist_array = fp->hlist_array;
696 	addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL);
697 	if (!addrs) {
698 		ret = -ENOMEM;	/* TODO: Fallback to one-by-one loop */
699 		goto out;
700 	}
701 
702 	/* Remove non-synonim ips from table and hash */
703 	count = 0;
704 	for (i = 0; i < hlist_array->size; i++) {
705 		if (!delete_fprobe_node(&hlist_array->array[i]))
706 			addrs[count++] = hlist_array->array[i].addr;
707 	}
708 	del_fprobe_hash(fp);
709 
710 	fprobe_graph_remove_ips(addrs, count);
711 
712 	kfree_rcu(hlist_array, rcu);
713 	fp->hlist_array = NULL;
714 
715 out:
716 	mutex_unlock(&fprobe_mutex);
717 
718 	kfree(addrs);
719 	return ret;
720 }
721 EXPORT_SYMBOL_GPL(unregister_fprobe);
722