xref: /linux-6.15/kernel/trace/fprobe.c (revision 9dda18a3)
1cad9931fSMasami Hiramatsu // SPDX-License-Identifier: GPL-2.0
2cad9931fSMasami Hiramatsu /*
3cad9931fSMasami Hiramatsu  * fprobe - Simple ftrace probe wrapper for function entry.
4cad9931fSMasami Hiramatsu  */
5cad9931fSMasami Hiramatsu #define pr_fmt(fmt) "fprobe: " fmt
6cad9931fSMasami Hiramatsu 
7cad9931fSMasami Hiramatsu #include <linux/err.h>
8cad9931fSMasami Hiramatsu #include <linux/fprobe.h>
9cad9931fSMasami Hiramatsu #include <linux/kallsyms.h>
10cad9931fSMasami Hiramatsu #include <linux/kprobes.h>
114346ba16SMasami Hiramatsu (Google) #include <linux/list.h>
124346ba16SMasami Hiramatsu (Google) #include <linux/mutex.h>
13cad9931fSMasami Hiramatsu #include <linux/slab.h>
14cad9931fSMasami Hiramatsu #include <linux/sort.h>
15cad9931fSMasami Hiramatsu 
16b5fa903bSMasami Hiramatsu (Google) #include <asm/fprobe.h>
17b5fa903bSMasami Hiramatsu (Google) 
185b0ab789SMasami Hiramatsu #include "trace.h"
195b0ab789SMasami Hiramatsu 
204346ba16SMasami Hiramatsu (Google) #define FPROBE_IP_HASH_BITS 8
214346ba16SMasami Hiramatsu (Google) #define FPROBE_IP_TABLE_SIZE (1 << FPROBE_IP_HASH_BITS)
225b0ab789SMasami Hiramatsu 
234346ba16SMasami Hiramatsu (Google) #define FPROBE_HASH_BITS 6
244346ba16SMasami Hiramatsu (Google) #define FPROBE_TABLE_SIZE (1 << FPROBE_HASH_BITS)
25cad9931fSMasami Hiramatsu 
264346ba16SMasami Hiramatsu (Google) #define SIZE_IN_LONG(x) ((x + sizeof(long) - 1) >> (sizeof(long) == 8 ? 3 : 2))
27cad9931fSMasami Hiramatsu 
284346ba16SMasami Hiramatsu (Google) /*
294346ba16SMasami Hiramatsu (Google)  * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still
304346ba16SMasami Hiramatsu (Google)  *   exists. The key is the address of fprobe instance.
314346ba16SMasami Hiramatsu (Google)  * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe
324346ba16SMasami Hiramatsu (Google)  *   instance related to the funciton address. The key is the ftrace IP
334346ba16SMasami Hiramatsu (Google)  *   address.
344346ba16SMasami Hiramatsu (Google)  *
354346ba16SMasami Hiramatsu (Google)  * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array[*].fp
364346ba16SMasami Hiramatsu (Google)  * are set NULL and delete those from both hash tables (by hlist_del_rcu).
374346ba16SMasami Hiramatsu (Google)  * After an RCU grace period, the fprobe_hlist itself will be released.
384346ba16SMasami Hiramatsu (Google)  *
394346ba16SMasami Hiramatsu (Google)  * fprobe_table and fprobe_ip_table can be accessed from either
404346ba16SMasami Hiramatsu (Google)  *  - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held.
414346ba16SMasami Hiramatsu (Google)  *  - RCU hlist traversal under disabling preempt
423cc4e2c5SZe Gao  */
434346ba16SMasami Hiramatsu (Google) static struct hlist_head fprobe_table[FPROBE_TABLE_SIZE];
444346ba16SMasami Hiramatsu (Google) static struct hlist_head fprobe_ip_table[FPROBE_IP_TABLE_SIZE];
454346ba16SMasami Hiramatsu (Google) static DEFINE_MUTEX(fprobe_mutex);
463cc4e2c5SZe Gao 
474346ba16SMasami Hiramatsu (Google) /*
484346ba16SMasami Hiramatsu (Google)  * Find first fprobe in the hlist. It will be iterated twice in the entry
494346ba16SMasami Hiramatsu (Google)  * probe, once for correcting the total required size, the second time is
504346ba16SMasami Hiramatsu (Google)  * calling back the user handlers.
514346ba16SMasami Hiramatsu (Google)  * Thus the hlist in the fprobe_table must be sorted and new probe needs to
524346ba16SMasami Hiramatsu (Google)  * be added *before* the first fprobe.
533cc4e2c5SZe Gao  */
find_first_fprobe_node(unsigned long ip)544346ba16SMasami Hiramatsu (Google) static struct fprobe_hlist_node *find_first_fprobe_node(unsigned long ip)
554346ba16SMasami Hiramatsu (Google) {
564346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist_node *node;
574346ba16SMasami Hiramatsu (Google) 	struct hlist_head *head;
584346ba16SMasami Hiramatsu (Google) 
594346ba16SMasami Hiramatsu (Google) 	head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)];
604346ba16SMasami Hiramatsu (Google) 	hlist_for_each_entry_rcu(node, head, hlist,
614346ba16SMasami Hiramatsu (Google) 				 lockdep_is_held(&fprobe_mutex)) {
624346ba16SMasami Hiramatsu (Google) 		if (node->addr == ip)
634346ba16SMasami Hiramatsu (Google) 			return node;
644346ba16SMasami Hiramatsu (Google) 	}
654346ba16SMasami Hiramatsu (Google) 	return NULL;
664346ba16SMasami Hiramatsu (Google) }
674346ba16SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(find_first_fprobe_node);
684346ba16SMasami Hiramatsu (Google) 
694346ba16SMasami Hiramatsu (Google) /* Node insertion and deletion requires the fprobe_mutex */
insert_fprobe_node(struct fprobe_hlist_node * node)704346ba16SMasami Hiramatsu (Google) static void insert_fprobe_node(struct fprobe_hlist_node *node)
714346ba16SMasami Hiramatsu (Google) {
724346ba16SMasami Hiramatsu (Google) 	unsigned long ip = node->addr;
734346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist_node *next;
744346ba16SMasami Hiramatsu (Google) 	struct hlist_head *head;
754346ba16SMasami Hiramatsu (Google) 
764346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
774346ba16SMasami Hiramatsu (Google) 
784346ba16SMasami Hiramatsu (Google) 	next = find_first_fprobe_node(ip);
794346ba16SMasami Hiramatsu (Google) 	if (next) {
804346ba16SMasami Hiramatsu (Google) 		hlist_add_before_rcu(&node->hlist, &next->hlist);
813cc4e2c5SZe Gao 		return;
823cc4e2c5SZe Gao 	}
834346ba16SMasami Hiramatsu (Google) 	head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)];
844346ba16SMasami Hiramatsu (Google) 	hlist_add_head_rcu(&node->hlist, head);
854346ba16SMasami Hiramatsu (Google) }
86ab51e15dSMasami Hiramatsu 
874346ba16SMasami Hiramatsu (Google) /* Return true if there are synonims */
delete_fprobe_node(struct fprobe_hlist_node * node)884346ba16SMasami Hiramatsu (Google) static bool delete_fprobe_node(struct fprobe_hlist_node *node)
894346ba16SMasami Hiramatsu (Google) {
904346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
914346ba16SMasami Hiramatsu (Google) 
92a3dc2983SMasami Hiramatsu (Google) 	/* Avoid double deleting */
93a3dc2983SMasami Hiramatsu (Google) 	if (READ_ONCE(node->fp) != NULL) {
944346ba16SMasami Hiramatsu (Google) 		WRITE_ONCE(node->fp, NULL);
954346ba16SMasami Hiramatsu (Google) 		hlist_del_rcu(&node->hlist);
96a3dc2983SMasami Hiramatsu (Google) 	}
974346ba16SMasami Hiramatsu (Google) 	return !!find_first_fprobe_node(node->addr);
984346ba16SMasami Hiramatsu (Google) }
994346ba16SMasami Hiramatsu (Google) 
1004346ba16SMasami Hiramatsu (Google) /* Check existence of the fprobe */
is_fprobe_still_exist(struct fprobe * fp)1014346ba16SMasami Hiramatsu (Google) static bool is_fprobe_still_exist(struct fprobe *fp)
1024346ba16SMasami Hiramatsu (Google) {
1034346ba16SMasami Hiramatsu (Google) 	struct hlist_head *head;
1044346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *fph;
1054346ba16SMasami Hiramatsu (Google) 
1064346ba16SMasami Hiramatsu (Google) 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
1074346ba16SMasami Hiramatsu (Google) 	hlist_for_each_entry_rcu(fph, head, hlist,
1084346ba16SMasami Hiramatsu (Google) 				 lockdep_is_held(&fprobe_mutex)) {
1094346ba16SMasami Hiramatsu (Google) 		if (fph->fp == fp)
1104346ba16SMasami Hiramatsu (Google) 			return true;
1114346ba16SMasami Hiramatsu (Google) 	}
1124346ba16SMasami Hiramatsu (Google) 	return false;
1134346ba16SMasami Hiramatsu (Google) }
1144346ba16SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(is_fprobe_still_exist);
1154346ba16SMasami Hiramatsu (Google) 
add_fprobe_hash(struct fprobe * fp)1164346ba16SMasami Hiramatsu (Google) static int add_fprobe_hash(struct fprobe *fp)
1174346ba16SMasami Hiramatsu (Google) {
1184346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *fph = fp->hlist_array;
1194346ba16SMasami Hiramatsu (Google) 	struct hlist_head *head;
1204346ba16SMasami Hiramatsu (Google) 
1214346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
1224346ba16SMasami Hiramatsu (Google) 
1234346ba16SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!fph))
1244346ba16SMasami Hiramatsu (Google) 		return -EINVAL;
1254346ba16SMasami Hiramatsu (Google) 
1264346ba16SMasami Hiramatsu (Google) 	if (is_fprobe_still_exist(fp))
1274346ba16SMasami Hiramatsu (Google) 		return -EEXIST;
1284346ba16SMasami Hiramatsu (Google) 
1294346ba16SMasami Hiramatsu (Google) 	head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)];
1304346ba16SMasami Hiramatsu (Google) 	hlist_add_head_rcu(&fp->hlist_array->hlist, head);
1314346ba16SMasami Hiramatsu (Google) 	return 0;
1324346ba16SMasami Hiramatsu (Google) }
1334346ba16SMasami Hiramatsu (Google) 
del_fprobe_hash(struct fprobe * fp)1344346ba16SMasami Hiramatsu (Google) static int del_fprobe_hash(struct fprobe *fp)
1354346ba16SMasami Hiramatsu (Google) {
1364346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *fph = fp->hlist_array;
1374346ba16SMasami Hiramatsu (Google) 
1384346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
1394346ba16SMasami Hiramatsu (Google) 
1404346ba16SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!fph))
1414346ba16SMasami Hiramatsu (Google) 		return -EINVAL;
1424346ba16SMasami Hiramatsu (Google) 
1434346ba16SMasami Hiramatsu (Google) 	if (!is_fprobe_still_exist(fp))
1444346ba16SMasami Hiramatsu (Google) 		return -ENOENT;
1454346ba16SMasami Hiramatsu (Google) 
1464346ba16SMasami Hiramatsu (Google) 	fph->fp = NULL;
1474346ba16SMasami Hiramatsu (Google) 	hlist_del_rcu(&fph->hlist);
1484346ba16SMasami Hiramatsu (Google) 	return 0;
1494346ba16SMasami Hiramatsu (Google) }
1504346ba16SMasami Hiramatsu (Google) 
151b5fa903bSMasami Hiramatsu (Google) #ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER
152b5fa903bSMasami Hiramatsu (Google) 
153b5fa903bSMasami Hiramatsu (Google) /* The arch should encode fprobe_header info into one unsigned long */
154b5fa903bSMasami Hiramatsu (Google) #define FPROBE_HEADER_SIZE_IN_LONG	1
155b5fa903bSMasami Hiramatsu (Google) 
write_fprobe_header(unsigned long * stack,struct fprobe * fp,unsigned int size_words)156b5fa903bSMasami Hiramatsu (Google) static inline bool write_fprobe_header(unsigned long *stack,
157b5fa903bSMasami Hiramatsu (Google) 					struct fprobe *fp, unsigned int size_words)
158b5fa903bSMasami Hiramatsu (Google) {
159b5fa903bSMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD ||
160b5fa903bSMasami Hiramatsu (Google) 			 !arch_fprobe_header_encodable(fp)))
161b5fa903bSMasami Hiramatsu (Google) 		return false;
162b5fa903bSMasami Hiramatsu (Google) 
163b5fa903bSMasami Hiramatsu (Google) 	*stack = arch_encode_fprobe_header(fp, size_words);
164b5fa903bSMasami Hiramatsu (Google) 	return true;
165b5fa903bSMasami Hiramatsu (Google) }
166b5fa903bSMasami Hiramatsu (Google) 
read_fprobe_header(unsigned long * stack,struct fprobe ** fp,unsigned int * size_words)167b5fa903bSMasami Hiramatsu (Google) static inline void read_fprobe_header(unsigned long *stack,
168b5fa903bSMasami Hiramatsu (Google) 					struct fprobe **fp, unsigned int *size_words)
169b5fa903bSMasami Hiramatsu (Google) {
170b5fa903bSMasami Hiramatsu (Google) 	*fp = arch_decode_fprobe_header_fp(*stack);
171b5fa903bSMasami Hiramatsu (Google) 	*size_words = arch_decode_fprobe_header_size(*stack);
172b5fa903bSMasami Hiramatsu (Google) }
173b5fa903bSMasami Hiramatsu (Google) 
174b5fa903bSMasami Hiramatsu (Google) #else
175b5fa903bSMasami Hiramatsu (Google) 
1764346ba16SMasami Hiramatsu (Google) /* Generic fprobe_header */
1774346ba16SMasami Hiramatsu (Google) struct __fprobe_header {
1784346ba16SMasami Hiramatsu (Google) 	struct fprobe *fp;
1794346ba16SMasami Hiramatsu (Google) 	unsigned long size_words;
1804346ba16SMasami Hiramatsu (Google) } __packed;
1814346ba16SMasami Hiramatsu (Google) 
1824346ba16SMasami Hiramatsu (Google) #define FPROBE_HEADER_SIZE_IN_LONG	SIZE_IN_LONG(sizeof(struct __fprobe_header))
1834346ba16SMasami Hiramatsu (Google) 
write_fprobe_header(unsigned long * stack,struct fprobe * fp,unsigned int size_words)1844346ba16SMasami Hiramatsu (Google) static inline bool write_fprobe_header(unsigned long *stack,
1854346ba16SMasami Hiramatsu (Google) 					struct fprobe *fp, unsigned int size_words)
1864346ba16SMasami Hiramatsu (Google) {
1874346ba16SMasami Hiramatsu (Google) 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
1884346ba16SMasami Hiramatsu (Google) 
1894346ba16SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD))
1904346ba16SMasami Hiramatsu (Google) 		return false;
1914346ba16SMasami Hiramatsu (Google) 
1924346ba16SMasami Hiramatsu (Google) 	fph->fp = fp;
1934346ba16SMasami Hiramatsu (Google) 	fph->size_words = size_words;
1944346ba16SMasami Hiramatsu (Google) 	return true;
1954346ba16SMasami Hiramatsu (Google) }
1964346ba16SMasami Hiramatsu (Google) 
read_fprobe_header(unsigned long * stack,struct fprobe ** fp,unsigned int * size_words)1974346ba16SMasami Hiramatsu (Google) static inline void read_fprobe_header(unsigned long *stack,
1984346ba16SMasami Hiramatsu (Google) 					struct fprobe **fp, unsigned int *size_words)
1994346ba16SMasami Hiramatsu (Google) {
2004346ba16SMasami Hiramatsu (Google) 	struct __fprobe_header *fph = (struct __fprobe_header *)stack;
2014346ba16SMasami Hiramatsu (Google) 
2024346ba16SMasami Hiramatsu (Google) 	*fp = fph->fp;
2034346ba16SMasami Hiramatsu (Google) 	*size_words = fph->size_words;
2044346ba16SMasami Hiramatsu (Google) }
2054346ba16SMasami Hiramatsu (Google) 
206b5fa903bSMasami Hiramatsu (Google) #endif
207b5fa903bSMasami Hiramatsu (Google) 
2084346ba16SMasami Hiramatsu (Google) /*
2094346ba16SMasami Hiramatsu (Google)  * fprobe shadow stack management:
2104346ba16SMasami Hiramatsu (Google)  * Since fprobe shares a single fgraph_ops, it needs to share the stack entry
2114346ba16SMasami Hiramatsu (Google)  * among the probes on the same function exit. Note that a new probe can be
2124346ba16SMasami Hiramatsu (Google)  * registered before a target function is returning, we can not use the hash
2134346ba16SMasami Hiramatsu (Google)  * table to find the corresponding probes. Thus the probe address is stored on
2144346ba16SMasami Hiramatsu (Google)  * the shadow stack with its entry data size.
2154346ba16SMasami Hiramatsu (Google)  *
2164346ba16SMasami Hiramatsu (Google)  */
__fprobe_handler(unsigned long ip,unsigned long parent_ip,struct fprobe * fp,struct ftrace_regs * fregs,void * data)2174346ba16SMasami Hiramatsu (Google) static inline int __fprobe_handler(unsigned long ip, unsigned long parent_ip,
2184346ba16SMasami Hiramatsu (Google) 				   struct fprobe *fp, struct ftrace_regs *fregs,
2194346ba16SMasami Hiramatsu (Google) 				   void *data)
2204346ba16SMasami Hiramatsu (Google) {
2214346ba16SMasami Hiramatsu (Google) 	if (!fp->entry_handler)
2224346ba16SMasami Hiramatsu (Google) 		return 0;
2234346ba16SMasami Hiramatsu (Google) 
2244346ba16SMasami Hiramatsu (Google) 	return fp->entry_handler(fp, ip, parent_ip, fregs, data);
2254346ba16SMasami Hiramatsu (Google) }
2264346ba16SMasami Hiramatsu (Google) 
__fprobe_kprobe_handler(unsigned long ip,unsigned long parent_ip,struct fprobe * fp,struct ftrace_regs * fregs,void * data)2274346ba16SMasami Hiramatsu (Google) static inline int __fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
2284346ba16SMasami Hiramatsu (Google) 					  struct fprobe *fp, struct ftrace_regs *fregs,
2294346ba16SMasami Hiramatsu (Google) 					  void *data)
2304346ba16SMasami Hiramatsu (Google) {
2314346ba16SMasami Hiramatsu (Google) 	int ret;
232d5f28bb1SMasami Hiramatsu (Google) 	/*
233d5f28bb1SMasami Hiramatsu (Google) 	 * This user handler is shared with other kprobes and is not expected to be
234d5f28bb1SMasami Hiramatsu (Google) 	 * called recursively. So if any other kprobe handler is running, this will
235d5f28bb1SMasami Hiramatsu (Google) 	 * exit as kprobe does. See the section 'Share the callbacks with kprobes'
236d5f28bb1SMasami Hiramatsu (Google) 	 * in Documentation/trace/fprobe.rst for more information.
237d5f28bb1SMasami Hiramatsu (Google) 	 */
238ab51e15dSMasami Hiramatsu 	if (unlikely(kprobe_running())) {
239ab51e15dSMasami Hiramatsu 		fp->nmissed++;
2404346ba16SMasami Hiramatsu (Google) 		return 0;
241ab51e15dSMasami Hiramatsu 	}
2423cc4e2c5SZe Gao 
243ab51e15dSMasami Hiramatsu 	kprobe_busy_begin();
2444346ba16SMasami Hiramatsu (Google) 	ret = __fprobe_handler(ip, parent_ip, fp, fregs, data);
245ab51e15dSMasami Hiramatsu 	kprobe_busy_end();
2464346ba16SMasami Hiramatsu (Google) 	return ret;
247ab51e15dSMasami Hiramatsu }
248ab51e15dSMasami Hiramatsu 
fprobe_entry(struct ftrace_graph_ent * trace,struct fgraph_ops * gops,struct ftrace_regs * fregs)2494346ba16SMasami Hiramatsu (Google) static int fprobe_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops,
2504346ba16SMasami Hiramatsu (Google) 			struct ftrace_regs *fregs)
2515b0ab789SMasami Hiramatsu {
2524346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist_node *node, *first;
2534346ba16SMasami Hiramatsu (Google) 	unsigned long *fgraph_data = NULL;
2544346ba16SMasami Hiramatsu (Google) 	unsigned long func = trace->func;
2554346ba16SMasami Hiramatsu (Google) 	unsigned long ret_ip;
2564346ba16SMasami Hiramatsu (Google) 	int reserved_words;
2574346ba16SMasami Hiramatsu (Google) 	struct fprobe *fp;
2584346ba16SMasami Hiramatsu (Google) 	int used, ret;
2595b0ab789SMasami Hiramatsu 
2604346ba16SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!fregs))
2614346ba16SMasami Hiramatsu (Google) 		return 0;
2625b0ab789SMasami Hiramatsu 
2634346ba16SMasami Hiramatsu (Google) 	first = node = find_first_fprobe_node(func);
2644346ba16SMasami Hiramatsu (Google) 	if (unlikely(!first))
2654346ba16SMasami Hiramatsu (Google) 		return 0;
2664346ba16SMasami Hiramatsu (Google) 
2674346ba16SMasami Hiramatsu (Google) 	reserved_words = 0;
2684346ba16SMasami Hiramatsu (Google) 	hlist_for_each_entry_from_rcu(node, hlist) {
2694346ba16SMasami Hiramatsu (Google) 		if (node->addr != func)
2704346ba16SMasami Hiramatsu (Google) 			break;
2714346ba16SMasami Hiramatsu (Google) 		fp = READ_ONCE(node->fp);
2724346ba16SMasami Hiramatsu (Google) 		if (!fp || !fp->exit_handler)
2734346ba16SMasami Hiramatsu (Google) 			continue;
2744346ba16SMasami Hiramatsu (Google) 		/*
2754346ba16SMasami Hiramatsu (Google) 		 * Since fprobe can be enabled until the next loop, we ignore the
2764346ba16SMasami Hiramatsu (Google) 		 * fprobe's disabled flag in this loop.
2774346ba16SMasami Hiramatsu (Google) 		 */
2784346ba16SMasami Hiramatsu (Google) 		reserved_words +=
2794346ba16SMasami Hiramatsu (Google) 			FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size);
2804346ba16SMasami Hiramatsu (Google) 	}
2814346ba16SMasami Hiramatsu (Google) 	node = first;
2824346ba16SMasami Hiramatsu (Google) 	if (reserved_words) {
2834346ba16SMasami Hiramatsu (Google) 		fgraph_data = fgraph_reserve_data(gops->idx, reserved_words * sizeof(long));
2844346ba16SMasami Hiramatsu (Google) 		if (unlikely(!fgraph_data)) {
2854346ba16SMasami Hiramatsu (Google) 			hlist_for_each_entry_from_rcu(node, hlist) {
2864346ba16SMasami Hiramatsu (Google) 				if (node->addr != func)
2874346ba16SMasami Hiramatsu (Google) 					break;
2884346ba16SMasami Hiramatsu (Google) 				fp = READ_ONCE(node->fp);
2894346ba16SMasami Hiramatsu (Google) 				if (fp && !fprobe_disabled(fp))
2904346ba16SMasami Hiramatsu (Google) 					fp->nmissed++;
2914346ba16SMasami Hiramatsu (Google) 			}
2924346ba16SMasami Hiramatsu (Google) 			return 0;
2934346ba16SMasami Hiramatsu (Google) 		}
2944346ba16SMasami Hiramatsu (Google) 	}
2955b0ab789SMasami Hiramatsu 
29627527410SZe Gao 	/*
2974346ba16SMasami Hiramatsu (Google) 	 * TODO: recursion detection has been done in the fgraph. Thus we need
2984346ba16SMasami Hiramatsu (Google) 	 * to add a callback to increment missed counter.
29927527410SZe Gao 	 */
3004346ba16SMasami Hiramatsu (Google) 	ret_ip = ftrace_regs_get_return_address(fregs);
3014346ba16SMasami Hiramatsu (Google) 	used = 0;
3024346ba16SMasami Hiramatsu (Google) 	hlist_for_each_entry_from_rcu(node, hlist) {
3034346ba16SMasami Hiramatsu (Google) 		int data_size;
3044346ba16SMasami Hiramatsu (Google) 		void *data;
3054346ba16SMasami Hiramatsu (Google) 
3064346ba16SMasami Hiramatsu (Google) 		if (node->addr != func)
3074346ba16SMasami Hiramatsu (Google) 			break;
3084346ba16SMasami Hiramatsu (Google) 		fp = READ_ONCE(node->fp);
3094346ba16SMasami Hiramatsu (Google) 		if (!fp || fprobe_disabled(fp))
3104346ba16SMasami Hiramatsu (Google) 			continue;
3114346ba16SMasami Hiramatsu (Google) 
3124346ba16SMasami Hiramatsu (Google) 		data_size = fp->entry_data_size;
3134346ba16SMasami Hiramatsu (Google) 		if (data_size && fp->exit_handler)
3144346ba16SMasami Hiramatsu (Google) 			data = fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG;
3154346ba16SMasami Hiramatsu (Google) 		else
3164346ba16SMasami Hiramatsu (Google) 			data = NULL;
3174346ba16SMasami Hiramatsu (Google) 
3184346ba16SMasami Hiramatsu (Google) 		if (fprobe_shared_with_kprobes(fp))
3194346ba16SMasami Hiramatsu (Google) 			ret = __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data);
3204346ba16SMasami Hiramatsu (Google) 		else
3214346ba16SMasami Hiramatsu (Google) 			ret = __fprobe_handler(func, ret_ip, fp, fregs, data);
3224346ba16SMasami Hiramatsu (Google) 
3234346ba16SMasami Hiramatsu (Google) 		/* If entry_handler returns !0, nmissed is not counted but skips exit_handler. */
3244346ba16SMasami Hiramatsu (Google) 		if (!ret && fp->exit_handler) {
3254346ba16SMasami Hiramatsu (Google) 			int size_words = SIZE_IN_LONG(data_size);
3264346ba16SMasami Hiramatsu (Google) 
3274346ba16SMasami Hiramatsu (Google) 			if (write_fprobe_header(&fgraph_data[used], fp, size_words))
3284346ba16SMasami Hiramatsu (Google) 				used += FPROBE_HEADER_SIZE_IN_LONG + size_words;
3294346ba16SMasami Hiramatsu (Google) 		}
3304346ba16SMasami Hiramatsu (Google) 	}
3314346ba16SMasami Hiramatsu (Google) 	if (used < reserved_words)
3324346ba16SMasami Hiramatsu (Google) 		memset(fgraph_data + used, 0, reserved_words - used);
3334346ba16SMasami Hiramatsu (Google) 
3344346ba16SMasami Hiramatsu (Google) 	/* If any exit_handler is set, data must be used. */
3354346ba16SMasami Hiramatsu (Google) 	return used != 0;
3364346ba16SMasami Hiramatsu (Google) }
3374346ba16SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fprobe_entry);
3384346ba16SMasami Hiramatsu (Google) 
fprobe_return(struct ftrace_graph_ret * trace,struct fgraph_ops * gops,struct ftrace_regs * fregs)3394346ba16SMasami Hiramatsu (Google) static void fprobe_return(struct ftrace_graph_ret *trace,
3404346ba16SMasami Hiramatsu (Google) 			  struct fgraph_ops *gops,
3414346ba16SMasami Hiramatsu (Google) 			  struct ftrace_regs *fregs)
3424346ba16SMasami Hiramatsu (Google) {
3434346ba16SMasami Hiramatsu (Google) 	unsigned long *fgraph_data = NULL;
3444346ba16SMasami Hiramatsu (Google) 	unsigned long ret_ip;
3454346ba16SMasami Hiramatsu (Google) 	struct fprobe *fp;
3464346ba16SMasami Hiramatsu (Google) 	int size, curr;
3474346ba16SMasami Hiramatsu (Google) 	int size_words;
3484346ba16SMasami Hiramatsu (Google) 
3494346ba16SMasami Hiramatsu (Google) 	fgraph_data = (unsigned long *)fgraph_retrieve_data(gops->idx, &size);
3504346ba16SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!fgraph_data))
3514346ba16SMasami Hiramatsu (Google) 		return;
3524346ba16SMasami Hiramatsu (Google) 	size_words = SIZE_IN_LONG(size);
3534346ba16SMasami Hiramatsu (Google) 	ret_ip = ftrace_regs_get_instruction_pointer(fregs);
3544346ba16SMasami Hiramatsu (Google) 
3554346ba16SMasami Hiramatsu (Google) 	preempt_disable();
3564346ba16SMasami Hiramatsu (Google) 
3574346ba16SMasami Hiramatsu (Google) 	curr = 0;
3584346ba16SMasami Hiramatsu (Google) 	while (size_words > curr) {
3594346ba16SMasami Hiramatsu (Google) 		read_fprobe_header(&fgraph_data[curr], &fp, &size);
3604346ba16SMasami Hiramatsu (Google) 		if (!fp)
3614346ba16SMasami Hiramatsu (Google) 			break;
3624346ba16SMasami Hiramatsu (Google) 		curr += FPROBE_HEADER_SIZE_IN_LONG;
3634346ba16SMasami Hiramatsu (Google) 		if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) {
3644346ba16SMasami Hiramatsu (Google) 			if (WARN_ON_ONCE(curr + size > size_words))
3654346ba16SMasami Hiramatsu (Google) 				break;
3664346ba16SMasami Hiramatsu (Google) 			fp->exit_handler(fp, trace->func, ret_ip, fregs,
3674346ba16SMasami Hiramatsu (Google) 					 size ? fgraph_data + curr : NULL);
3684346ba16SMasami Hiramatsu (Google) 		}
3694346ba16SMasami Hiramatsu (Google) 		curr += size;
3704346ba16SMasami Hiramatsu (Google) 	}
3714346ba16SMasami Hiramatsu (Google) 	preempt_enable();
3724346ba16SMasami Hiramatsu (Google) }
3734346ba16SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fprobe_return);
3744346ba16SMasami Hiramatsu (Google) 
3754346ba16SMasami Hiramatsu (Google) static struct fgraph_ops fprobe_graph_ops = {
3764346ba16SMasami Hiramatsu (Google) 	.entryfunc	= fprobe_entry,
3774346ba16SMasami Hiramatsu (Google) 	.retfunc	= fprobe_return,
3784346ba16SMasami Hiramatsu (Google) };
3794346ba16SMasami Hiramatsu (Google) static int fprobe_graph_active;
3804346ba16SMasami Hiramatsu (Google) 
3814346ba16SMasami Hiramatsu (Google) /* Add @addrs to the ftrace filter and register fgraph if needed. */
fprobe_graph_add_ips(unsigned long * addrs,int num)3824346ba16SMasami Hiramatsu (Google) static int fprobe_graph_add_ips(unsigned long *addrs, int num)
3834346ba16SMasami Hiramatsu (Google) {
3844346ba16SMasami Hiramatsu (Google) 	int ret;
3854346ba16SMasami Hiramatsu (Google) 
3864346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
3874346ba16SMasami Hiramatsu (Google) 
3884346ba16SMasami Hiramatsu (Google) 	ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0);
3894346ba16SMasami Hiramatsu (Google) 	if (ret)
3904346ba16SMasami Hiramatsu (Google) 		return ret;
3914346ba16SMasami Hiramatsu (Google) 
3924346ba16SMasami Hiramatsu (Google) 	if (!fprobe_graph_active) {
3934346ba16SMasami Hiramatsu (Google) 		ret = register_ftrace_graph(&fprobe_graph_ops);
3944346ba16SMasami Hiramatsu (Google) 		if (WARN_ON_ONCE(ret)) {
3954346ba16SMasami Hiramatsu (Google) 			ftrace_free_filter(&fprobe_graph_ops.ops);
3964346ba16SMasami Hiramatsu (Google) 			return ret;
3974346ba16SMasami Hiramatsu (Google) 		}
3984346ba16SMasami Hiramatsu (Google) 	}
3994346ba16SMasami Hiramatsu (Google) 	fprobe_graph_active++;
4004346ba16SMasami Hiramatsu (Google) 	return 0;
4014346ba16SMasami Hiramatsu (Google) }
4024346ba16SMasami Hiramatsu (Google) 
4034346ba16SMasami Hiramatsu (Google) /* Remove @addrs from the ftrace filter and unregister fgraph if possible. */
fprobe_graph_remove_ips(unsigned long * addrs,int num)4044346ba16SMasami Hiramatsu (Google) static void fprobe_graph_remove_ips(unsigned long *addrs, int num)
4054346ba16SMasami Hiramatsu (Google) {
4064346ba16SMasami Hiramatsu (Google) 	lockdep_assert_held(&fprobe_mutex);
4074346ba16SMasami Hiramatsu (Google) 
4084346ba16SMasami Hiramatsu (Google) 	fprobe_graph_active--;
4094346ba16SMasami Hiramatsu (Google) 	/* Q: should we unregister it ? */
410ded91406SSteven Rostedt 	if (!fprobe_graph_active)
4114346ba16SMasami Hiramatsu (Google) 		unregister_ftrace_graph(&fprobe_graph_ops);
41227527410SZe Gao 
413ca26554aSSteven Rostedt 	if (num)
4144346ba16SMasami Hiramatsu (Google) 		ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
4155b0ab789SMasami Hiramatsu }
4165b0ab789SMasami Hiramatsu 
417a3dc2983SMasami Hiramatsu (Google) #ifdef CONFIG_MODULES
418a3dc2983SMasami Hiramatsu (Google) 
419a3dc2983SMasami Hiramatsu (Google) #define FPROBE_IPS_BATCH_INIT 8
420a3dc2983SMasami Hiramatsu (Google) /* instruction pointer address list */
421a3dc2983SMasami Hiramatsu (Google) struct fprobe_addr_list {
422a3dc2983SMasami Hiramatsu (Google) 	int index;
423a3dc2983SMasami Hiramatsu (Google) 	int size;
424a3dc2983SMasami Hiramatsu (Google) 	unsigned long *addrs;
425a3dc2983SMasami Hiramatsu (Google) };
426a3dc2983SMasami Hiramatsu (Google) 
fprobe_addr_list_add(struct fprobe_addr_list * alist,unsigned long addr)427a3dc2983SMasami Hiramatsu (Google) static int fprobe_addr_list_add(struct fprobe_addr_list *alist, unsigned long addr)
428a3dc2983SMasami Hiramatsu (Google) {
429a3dc2983SMasami Hiramatsu (Google) 	unsigned long *addrs;
430a3dc2983SMasami Hiramatsu (Google) 
431a3dc2983SMasami Hiramatsu (Google) 	if (alist->index >= alist->size)
432a3dc2983SMasami Hiramatsu (Google) 		return -ENOMEM;
433a3dc2983SMasami Hiramatsu (Google) 
434a3dc2983SMasami Hiramatsu (Google) 	alist->addrs[alist->index++] = addr;
435a3dc2983SMasami Hiramatsu (Google) 	if (alist->index < alist->size)
436a3dc2983SMasami Hiramatsu (Google) 		return 0;
437a3dc2983SMasami Hiramatsu (Google) 
438a3dc2983SMasami Hiramatsu (Google) 	/* Expand the address list */
439a3dc2983SMasami Hiramatsu (Google) 	addrs = kcalloc(alist->size * 2, sizeof(*addrs), GFP_KERNEL);
440a3dc2983SMasami Hiramatsu (Google) 	if (!addrs)
441a3dc2983SMasami Hiramatsu (Google) 		return -ENOMEM;
442a3dc2983SMasami Hiramatsu (Google) 
443a3dc2983SMasami Hiramatsu (Google) 	memcpy(addrs, alist->addrs, alist->size * sizeof(*addrs));
444a3dc2983SMasami Hiramatsu (Google) 	alist->size *= 2;
445a3dc2983SMasami Hiramatsu (Google) 	kfree(alist->addrs);
446a3dc2983SMasami Hiramatsu (Google) 	alist->addrs = addrs;
447a3dc2983SMasami Hiramatsu (Google) 
448a3dc2983SMasami Hiramatsu (Google) 	return 0;
449a3dc2983SMasami Hiramatsu (Google) }
450a3dc2983SMasami Hiramatsu (Google) 
fprobe_remove_node_in_module(struct module * mod,struct hlist_head * head,struct fprobe_addr_list * alist)451a3dc2983SMasami Hiramatsu (Google) static void fprobe_remove_node_in_module(struct module *mod, struct hlist_head *head,
452a3dc2983SMasami Hiramatsu (Google) 					struct fprobe_addr_list *alist)
453a3dc2983SMasami Hiramatsu (Google) {
454a3dc2983SMasami Hiramatsu (Google) 	struct fprobe_hlist_node *node;
455a3dc2983SMasami Hiramatsu (Google) 	int ret = 0;
456a3dc2983SMasami Hiramatsu (Google) 
457*9dda18a3SBreno Leitao 	hlist_for_each_entry_rcu(node, head, hlist,
458*9dda18a3SBreno Leitao 				 lockdep_is_held(&fprobe_mutex)) {
459a3dc2983SMasami Hiramatsu (Google) 		if (!within_module(node->addr, mod))
460a3dc2983SMasami Hiramatsu (Google) 			continue;
461a3dc2983SMasami Hiramatsu (Google) 		if (delete_fprobe_node(node))
462a3dc2983SMasami Hiramatsu (Google) 			continue;
463a3dc2983SMasami Hiramatsu (Google) 		/*
464a3dc2983SMasami Hiramatsu (Google) 		 * If failed to update alist, just continue to update hlist.
465a3dc2983SMasami Hiramatsu (Google) 		 * Therefore, at list user handler will not hit anymore.
466a3dc2983SMasami Hiramatsu (Google) 		 */
467a3dc2983SMasami Hiramatsu (Google) 		if (!ret)
468a3dc2983SMasami Hiramatsu (Google) 			ret = fprobe_addr_list_add(alist, node->addr);
469a3dc2983SMasami Hiramatsu (Google) 	}
470a3dc2983SMasami Hiramatsu (Google) }
471a3dc2983SMasami Hiramatsu (Google) 
472a3dc2983SMasami Hiramatsu (Google) /* Handle module unloading to manage fprobe_ip_table. */
fprobe_module_callback(struct notifier_block * nb,unsigned long val,void * data)473a3dc2983SMasami Hiramatsu (Google) static int fprobe_module_callback(struct notifier_block *nb,
474a3dc2983SMasami Hiramatsu (Google) 				  unsigned long val, void *data)
475a3dc2983SMasami Hiramatsu (Google) {
476a3dc2983SMasami Hiramatsu (Google) 	struct fprobe_addr_list alist = {.size = FPROBE_IPS_BATCH_INIT};
477a3dc2983SMasami Hiramatsu (Google) 	struct module *mod = data;
478a3dc2983SMasami Hiramatsu (Google) 	int i;
479a3dc2983SMasami Hiramatsu (Google) 
480a3dc2983SMasami Hiramatsu (Google) 	if (val != MODULE_STATE_GOING)
481a3dc2983SMasami Hiramatsu (Google) 		return NOTIFY_DONE;
482a3dc2983SMasami Hiramatsu (Google) 
483a3dc2983SMasami Hiramatsu (Google) 	alist.addrs = kcalloc(alist.size, sizeof(*alist.addrs), GFP_KERNEL);
484a3dc2983SMasami Hiramatsu (Google) 	/* If failed to alloc memory, we can not remove ips from hash. */
485a3dc2983SMasami Hiramatsu (Google) 	if (!alist.addrs)
486a3dc2983SMasami Hiramatsu (Google) 		return NOTIFY_DONE;
487a3dc2983SMasami Hiramatsu (Google) 
488a3dc2983SMasami Hiramatsu (Google) 	mutex_lock(&fprobe_mutex);
489a3dc2983SMasami Hiramatsu (Google) 	for (i = 0; i < FPROBE_IP_TABLE_SIZE; i++)
490a3dc2983SMasami Hiramatsu (Google) 		fprobe_remove_node_in_module(mod, &fprobe_ip_table[i], &alist);
491a3dc2983SMasami Hiramatsu (Google) 
492a3dc2983SMasami Hiramatsu (Google) 	if (alist.index < alist.size && alist.index > 0)
493a3dc2983SMasami Hiramatsu (Google) 		ftrace_set_filter_ips(&fprobe_graph_ops.ops,
494a3dc2983SMasami Hiramatsu (Google) 				      alist.addrs, alist.index, 1, 0);
495a3dc2983SMasami Hiramatsu (Google) 	mutex_unlock(&fprobe_mutex);
496a3dc2983SMasami Hiramatsu (Google) 
497a3dc2983SMasami Hiramatsu (Google) 	kfree(alist.addrs);
498a3dc2983SMasami Hiramatsu (Google) 
499a3dc2983SMasami Hiramatsu (Google) 	return NOTIFY_DONE;
500a3dc2983SMasami Hiramatsu (Google) }
501a3dc2983SMasami Hiramatsu (Google) 
502a3dc2983SMasami Hiramatsu (Google) static struct notifier_block fprobe_module_nb = {
503a3dc2983SMasami Hiramatsu (Google) 	.notifier_call = fprobe_module_callback,
504a3dc2983SMasami Hiramatsu (Google) 	.priority = 0,
505a3dc2983SMasami Hiramatsu (Google) };
506a3dc2983SMasami Hiramatsu (Google) 
init_fprobe_module(void)507a3dc2983SMasami Hiramatsu (Google) static int __init init_fprobe_module(void)
508a3dc2983SMasami Hiramatsu (Google) {
509a3dc2983SMasami Hiramatsu (Google) 	return register_module_notifier(&fprobe_module_nb);
510a3dc2983SMasami Hiramatsu (Google) }
511a3dc2983SMasami Hiramatsu (Google) early_initcall(init_fprobe_module);
512a3dc2983SMasami Hiramatsu (Google) #endif
513a3dc2983SMasami Hiramatsu (Google) 
symbols_cmp(const void * a,const void * b)5148be92533SJiri Olsa static int symbols_cmp(const void *a, const void *b)
5158be92533SJiri Olsa {
5168be92533SJiri Olsa 	const char **str_a = (const char **) a;
5178be92533SJiri Olsa 	const char **str_b = (const char **) b;
5188be92533SJiri Olsa 
5198be92533SJiri Olsa 	return strcmp(*str_a, *str_b);
5208be92533SJiri Olsa }
5218be92533SJiri Olsa 
522cad9931fSMasami Hiramatsu /* Convert ftrace location address from symbols */
get_ftrace_locations(const char ** syms,int num)523cad9931fSMasami Hiramatsu static unsigned long *get_ftrace_locations(const char **syms, int num)
524cad9931fSMasami Hiramatsu {
525cad9931fSMasami Hiramatsu 	unsigned long *addrs;
526cad9931fSMasami Hiramatsu 
527cad9931fSMasami Hiramatsu 	/* Convert symbols to symbol address */
528cad9931fSMasami Hiramatsu 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
529cad9931fSMasami Hiramatsu 	if (!addrs)
530cad9931fSMasami Hiramatsu 		return ERR_PTR(-ENOMEM);
531cad9931fSMasami Hiramatsu 
5328be92533SJiri Olsa 	/* ftrace_lookup_symbols expects sorted symbols */
5338be92533SJiri Olsa 	sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
534cad9931fSMasami Hiramatsu 
5358be92533SJiri Olsa 	if (!ftrace_lookup_symbols(syms, num, addrs))
536cad9931fSMasami Hiramatsu 		return addrs;
537cad9931fSMasami Hiramatsu 
538cad9931fSMasami Hiramatsu 	kfree(addrs);
539cad9931fSMasami Hiramatsu 	return ERR_PTR(-ENOENT);
540cad9931fSMasami Hiramatsu }
541cad9931fSMasami Hiramatsu 
5424346ba16SMasami Hiramatsu (Google) struct filter_match_data {
5434346ba16SMasami Hiramatsu (Google) 	const char *filter;
5444346ba16SMasami Hiramatsu (Google) 	const char *notfilter;
5454346ba16SMasami Hiramatsu (Google) 	size_t index;
5464346ba16SMasami Hiramatsu (Google) 	size_t size;
5474346ba16SMasami Hiramatsu (Google) 	unsigned long *addrs;
548d24fa977SMasami Hiramatsu (Google) 	struct module **mods;
5494346ba16SMasami Hiramatsu (Google) };
55046bc0823SMasami Hiramatsu (Google) 
filter_match_callback(void * data,const char * name,unsigned long addr)5514346ba16SMasami Hiramatsu (Google) static int filter_match_callback(void *data, const char *name, unsigned long addr)
5524346ba16SMasami Hiramatsu (Google) {
5534346ba16SMasami Hiramatsu (Google) 	struct filter_match_data *match = data;
5544346ba16SMasami Hiramatsu (Google) 
5554346ba16SMasami Hiramatsu (Google) 	if (!glob_match(match->filter, name) ||
5564346ba16SMasami Hiramatsu (Google) 	    (match->notfilter && glob_match(match->notfilter, name)))
5574346ba16SMasami Hiramatsu (Google) 		return 0;
5584346ba16SMasami Hiramatsu (Google) 
5594346ba16SMasami Hiramatsu (Google) 	if (!ftrace_location(addr))
5604346ba16SMasami Hiramatsu (Google) 		return 0;
5614346ba16SMasami Hiramatsu (Google) 
562d24fa977SMasami Hiramatsu (Google) 	if (match->addrs) {
563d24fa977SMasami Hiramatsu (Google) 		struct module *mod = __module_text_address(addr);
5644346ba16SMasami Hiramatsu (Google) 
565d24fa977SMasami Hiramatsu (Google) 		if (mod && !try_module_get(mod))
566d24fa977SMasami Hiramatsu (Google) 			return 0;
567d24fa977SMasami Hiramatsu (Google) 
568d24fa977SMasami Hiramatsu (Google) 		match->mods[match->index] = mod;
569d24fa977SMasami Hiramatsu (Google) 		match->addrs[match->index] = addr;
570d24fa977SMasami Hiramatsu (Google) 	}
5714346ba16SMasami Hiramatsu (Google) 	match->index++;
5724346ba16SMasami Hiramatsu (Google) 	return match->index == match->size;
573cad9931fSMasami Hiramatsu }
574cad9931fSMasami Hiramatsu 
5754346ba16SMasami Hiramatsu (Google) /*
5764346ba16SMasami Hiramatsu (Google)  * Make IP list from the filter/no-filter glob patterns.
577d24fa977SMasami Hiramatsu (Google)  * Return the number of matched symbols, or errno.
578d24fa977SMasami Hiramatsu (Google)  * If @addrs == NULL, this just counts the number of matched symbols. If @addrs
579d24fa977SMasami Hiramatsu (Google)  * is passed with an array, we need to pass the an @mods array of the same size
580d24fa977SMasami Hiramatsu (Google)  * to increment the module refcount for each symbol.
581d24fa977SMasami Hiramatsu (Google)  * This means we also need to call `module_put` for each element of @mods after
582d24fa977SMasami Hiramatsu (Google)  * using the @addrs.
5834346ba16SMasami Hiramatsu (Google)  */
get_ips_from_filter(const char * filter,const char * notfilter,unsigned long * addrs,struct module ** mods,size_t size)584d24fa977SMasami Hiramatsu (Google) static int get_ips_from_filter(const char *filter, const char *notfilter,
585d24fa977SMasami Hiramatsu (Google) 			       unsigned long *addrs, struct module **mods,
586d24fa977SMasami Hiramatsu (Google) 			       size_t size)
5875b0ab789SMasami Hiramatsu {
5884346ba16SMasami Hiramatsu (Google) 	struct filter_match_data match = { .filter = filter, .notfilter = notfilter,
589d24fa977SMasami Hiramatsu (Google) 		.index = 0, .size = size, .addrs = addrs, .mods = mods};
5904346ba16SMasami Hiramatsu (Google) 	int ret;
5915b0ab789SMasami Hiramatsu 
592d24fa977SMasami Hiramatsu (Google) 	if (addrs && !mods)
593d24fa977SMasami Hiramatsu (Google) 		return -EINVAL;
594d24fa977SMasami Hiramatsu (Google) 
5954346ba16SMasami Hiramatsu (Google) 	ret = kallsyms_on_each_symbol(filter_match_callback, &match);
5964346ba16SMasami Hiramatsu (Google) 	if (ret < 0)
5974346ba16SMasami Hiramatsu (Google) 		return ret;
598d24fa977SMasami Hiramatsu (Google) 	if (IS_ENABLED(CONFIG_MODULES)) {
5994346ba16SMasami Hiramatsu (Google) 		ret = module_kallsyms_on_each_symbol(NULL, filter_match_callback, &match);
6004346ba16SMasami Hiramatsu (Google) 		if (ret < 0)
6014346ba16SMasami Hiramatsu (Google) 			return ret;
602d24fa977SMasami Hiramatsu (Google) 	}
6035b0ab789SMasami Hiramatsu 
6044346ba16SMasami Hiramatsu (Google) 	return match.index ?: -ENOENT;
6055b0ab789SMasami Hiramatsu }
6065b0ab789SMasami Hiramatsu 
fprobe_fail_cleanup(struct fprobe * fp)6075b0ab789SMasami Hiramatsu static void fprobe_fail_cleanup(struct fprobe *fp)
6085b0ab789SMasami Hiramatsu {
6094346ba16SMasami Hiramatsu (Google) 	kfree(fp->hlist_array);
6104346ba16SMasami Hiramatsu (Google) 	fp->hlist_array = NULL;
6115b0ab789SMasami Hiramatsu }
6124346ba16SMasami Hiramatsu (Google) 
6134346ba16SMasami Hiramatsu (Google) /* Initialize the fprobe data structure. */
fprobe_init(struct fprobe * fp,unsigned long * addrs,int num)6144346ba16SMasami Hiramatsu (Google) static int fprobe_init(struct fprobe *fp, unsigned long *addrs, int num)
6154346ba16SMasami Hiramatsu (Google) {
6164346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *hlist_array;
6174346ba16SMasami Hiramatsu (Google) 	unsigned long addr;
6184346ba16SMasami Hiramatsu (Google) 	int size, i;
6194346ba16SMasami Hiramatsu (Google) 
6204346ba16SMasami Hiramatsu (Google) 	if (!fp || !addrs || num <= 0)
6214346ba16SMasami Hiramatsu (Google) 		return -EINVAL;
6224346ba16SMasami Hiramatsu (Google) 
6234346ba16SMasami Hiramatsu (Google) 	size = ALIGN(fp->entry_data_size, sizeof(long));
6244346ba16SMasami Hiramatsu (Google) 	if (size > MAX_FPROBE_DATA_SIZE)
6254346ba16SMasami Hiramatsu (Google) 		return -E2BIG;
6264346ba16SMasami Hiramatsu (Google) 	fp->entry_data_size = size;
6274346ba16SMasami Hiramatsu (Google) 
6284346ba16SMasami Hiramatsu (Google) 	hlist_array = kzalloc(struct_size(hlist_array, array, num), GFP_KERNEL);
6294346ba16SMasami Hiramatsu (Google) 	if (!hlist_array)
6304346ba16SMasami Hiramatsu (Google) 		return -ENOMEM;
6314346ba16SMasami Hiramatsu (Google) 
6324346ba16SMasami Hiramatsu (Google) 	fp->nmissed = 0;
6334346ba16SMasami Hiramatsu (Google) 
6344346ba16SMasami Hiramatsu (Google) 	hlist_array->size = num;
6354346ba16SMasami Hiramatsu (Google) 	fp->hlist_array = hlist_array;
6364346ba16SMasami Hiramatsu (Google) 	hlist_array->fp = fp;
6374346ba16SMasami Hiramatsu (Google) 	for (i = 0; i < num; i++) {
6384346ba16SMasami Hiramatsu (Google) 		hlist_array->array[i].fp = fp;
6394346ba16SMasami Hiramatsu (Google) 		addr = ftrace_location(addrs[i]);
6404346ba16SMasami Hiramatsu (Google) 		if (!addr) {
6414346ba16SMasami Hiramatsu (Google) 			fprobe_fail_cleanup(fp);
6424346ba16SMasami Hiramatsu (Google) 			return -ENOENT;
6435b0ab789SMasami Hiramatsu 		}
6444346ba16SMasami Hiramatsu (Google) 		hlist_array->array[i].addr = addr;
6454346ba16SMasami Hiramatsu (Google) 	}
6464346ba16SMasami Hiramatsu (Google) 	return 0;
6474346ba16SMasami Hiramatsu (Google) }
6484346ba16SMasami Hiramatsu (Google) 
6494346ba16SMasami Hiramatsu (Google) #define FPROBE_IPS_MAX	INT_MAX
6505b0ab789SMasami Hiramatsu 
651cad9931fSMasami Hiramatsu /**
652cad9931fSMasami Hiramatsu  * register_fprobe() - Register fprobe to ftrace by pattern.
653cad9931fSMasami Hiramatsu  * @fp: A fprobe data structure to be registered.
654cad9931fSMasami Hiramatsu  * @filter: A wildcard pattern of probed symbols.
655cad9931fSMasami Hiramatsu  * @notfilter: A wildcard pattern of NOT probed symbols.
656cad9931fSMasami Hiramatsu  *
657cad9931fSMasami Hiramatsu  * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
658cad9931fSMasami Hiramatsu  * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
659cad9931fSMasami Hiramatsu  *
660cad9931fSMasami Hiramatsu  * Return 0 if @fp is registered successfully, -errno if not.
661cad9931fSMasami Hiramatsu  */
register_fprobe(struct fprobe * fp,const char * filter,const char * notfilter)662cad9931fSMasami Hiramatsu int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
663cad9931fSMasami Hiramatsu {
664d24fa977SMasami Hiramatsu (Google) 	unsigned long *addrs __free(kfree) = NULL;
665d24fa977SMasami Hiramatsu (Google) 	struct module **mods __free(kfree) = NULL;
666d24fa977SMasami Hiramatsu (Google) 	int ret, num;
667cad9931fSMasami Hiramatsu 
668cad9931fSMasami Hiramatsu 	if (!fp || !filter)
669cad9931fSMasami Hiramatsu 		return -EINVAL;
670cad9931fSMasami Hiramatsu 
671d24fa977SMasami Hiramatsu (Google) 	num = get_ips_from_filter(filter, notfilter, NULL, NULL, FPROBE_IPS_MAX);
672d24fa977SMasami Hiramatsu (Google) 	if (num < 0)
673d24fa977SMasami Hiramatsu (Google) 		return num;
674d24fa977SMasami Hiramatsu (Google) 
675d24fa977SMasami Hiramatsu (Google) 	addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
676d24fa977SMasami Hiramatsu (Google) 	if (!addrs)
677d24fa977SMasami Hiramatsu (Google) 		return -ENOMEM;
678d24fa977SMasami Hiramatsu (Google) 
679d24fa977SMasami Hiramatsu (Google) 	mods = kcalloc(num, sizeof(*mods), GFP_KERNEL);
680d24fa977SMasami Hiramatsu (Google) 	if (!mods)
681d24fa977SMasami Hiramatsu (Google) 		return -ENOMEM;
682d24fa977SMasami Hiramatsu (Google) 
683d24fa977SMasami Hiramatsu (Google) 	ret = get_ips_from_filter(filter, notfilter, addrs, mods, num);
6844346ba16SMasami Hiramatsu (Google) 	if (ret < 0)
685cad9931fSMasami Hiramatsu 		return ret;
686cad9931fSMasami Hiramatsu 
6874346ba16SMasami Hiramatsu (Google) 	ret = register_fprobe_ips(fp, addrs, ret);
688cad9931fSMasami Hiramatsu 
689d24fa977SMasami Hiramatsu (Google) 	for (int i = 0; i < num; i++) {
690d24fa977SMasami Hiramatsu (Google) 		if (mods[i])
691d24fa977SMasami Hiramatsu (Google) 			module_put(mods[i]);
692d24fa977SMasami Hiramatsu (Google) 	}
693cad9931fSMasami Hiramatsu 	return ret;
694cad9931fSMasami Hiramatsu }
695cad9931fSMasami Hiramatsu EXPORT_SYMBOL_GPL(register_fprobe);
696cad9931fSMasami Hiramatsu 
697cad9931fSMasami Hiramatsu /**
698cad9931fSMasami Hiramatsu  * register_fprobe_ips() - Register fprobe to ftrace by address.
699cad9931fSMasami Hiramatsu  * @fp: A fprobe data structure to be registered.
7004346ba16SMasami Hiramatsu (Google)  * @addrs: An array of target function address.
701cad9931fSMasami Hiramatsu  * @num: The number of entries of @addrs.
702cad9931fSMasami Hiramatsu  *
703cad9931fSMasami Hiramatsu  * Register @fp to ftrace for enabling the probe on the address given by @addrs.
704cad9931fSMasami Hiramatsu  * The @addrs must be the addresses of ftrace location address, which may be
705cad9931fSMasami Hiramatsu  * the symbol address + arch-dependent offset.
706cad9931fSMasami Hiramatsu  * If you unsure what this mean, please use other registration functions.
707cad9931fSMasami Hiramatsu  *
708cad9931fSMasami Hiramatsu  * Return 0 if @fp is registered successfully, -errno if not.
709cad9931fSMasami Hiramatsu  */
register_fprobe_ips(struct fprobe * fp,unsigned long * addrs,int num)710cad9931fSMasami Hiramatsu int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
711cad9931fSMasami Hiramatsu {
7124346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *hlist_array;
7134346ba16SMasami Hiramatsu (Google) 	int ret, i;
714cad9931fSMasami Hiramatsu 
7154346ba16SMasami Hiramatsu (Google) 	ret = fprobe_init(fp, addrs, num);
7165b0ab789SMasami Hiramatsu 	if (ret)
7175b0ab789SMasami Hiramatsu 		return ret;
7185b0ab789SMasami Hiramatsu 
7194346ba16SMasami Hiramatsu (Google) 	mutex_lock(&fprobe_mutex);
7204346ba16SMasami Hiramatsu (Google) 
7214346ba16SMasami Hiramatsu (Google) 	hlist_array = fp->hlist_array;
7224346ba16SMasami Hiramatsu (Google) 	ret = fprobe_graph_add_ips(addrs, num);
7234346ba16SMasami Hiramatsu (Google) 	if (!ret) {
7244346ba16SMasami Hiramatsu (Google) 		add_fprobe_hash(fp);
7254346ba16SMasami Hiramatsu (Google) 		for (i = 0; i < hlist_array->size; i++)
7264346ba16SMasami Hiramatsu (Google) 			insert_fprobe_node(&hlist_array->array[i]);
7274346ba16SMasami Hiramatsu (Google) 	}
7284346ba16SMasami Hiramatsu (Google) 	mutex_unlock(&fprobe_mutex);
729cad9931fSMasami Hiramatsu 
730cad9931fSMasami Hiramatsu 	if (ret)
7315b0ab789SMasami Hiramatsu 		fprobe_fail_cleanup(fp);
7324346ba16SMasami Hiramatsu (Google) 
733cad9931fSMasami Hiramatsu 	return ret;
734cad9931fSMasami Hiramatsu }
735cad9931fSMasami Hiramatsu EXPORT_SYMBOL_GPL(register_fprobe_ips);
736cad9931fSMasami Hiramatsu 
737cad9931fSMasami Hiramatsu /**
738cad9931fSMasami Hiramatsu  * register_fprobe_syms() - Register fprobe to ftrace by symbols.
739cad9931fSMasami Hiramatsu  * @fp: A fprobe data structure to be registered.
740cad9931fSMasami Hiramatsu  * @syms: An array of target symbols.
741cad9931fSMasami Hiramatsu  * @num: The number of entries of @syms.
742cad9931fSMasami Hiramatsu  *
743cad9931fSMasami Hiramatsu  * Register @fp to the symbols given by @syms array. This will be useful if
744cad9931fSMasami Hiramatsu  * you are sure the symbols exist in the kernel.
745cad9931fSMasami Hiramatsu  *
746cad9931fSMasami Hiramatsu  * Return 0 if @fp is registered successfully, -errno if not.
747cad9931fSMasami Hiramatsu  */
register_fprobe_syms(struct fprobe * fp,const char ** syms,int num)748cad9931fSMasami Hiramatsu int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
749cad9931fSMasami Hiramatsu {
750cad9931fSMasami Hiramatsu 	unsigned long *addrs;
751cad9931fSMasami Hiramatsu 	int ret;
752cad9931fSMasami Hiramatsu 
753cad9931fSMasami Hiramatsu 	if (!fp || !syms || num <= 0)
754cad9931fSMasami Hiramatsu 		return -EINVAL;
755cad9931fSMasami Hiramatsu 
756cad9931fSMasami Hiramatsu 	addrs = get_ftrace_locations(syms, num);
757cad9931fSMasami Hiramatsu 	if (IS_ERR(addrs))
758cad9931fSMasami Hiramatsu 		return PTR_ERR(addrs);
759cad9931fSMasami Hiramatsu 
760cad9931fSMasami Hiramatsu 	ret = register_fprobe_ips(fp, addrs, num);
761cad9931fSMasami Hiramatsu 
762cad9931fSMasami Hiramatsu 	kfree(addrs);
763cad9931fSMasami Hiramatsu 
764cad9931fSMasami Hiramatsu 	return ret;
765cad9931fSMasami Hiramatsu }
766cad9931fSMasami Hiramatsu EXPORT_SYMBOL_GPL(register_fprobe_syms);
767cad9931fSMasami Hiramatsu 
fprobe_is_registered(struct fprobe * fp)768334e5519SMasami Hiramatsu (Google) bool fprobe_is_registered(struct fprobe *fp)
769334e5519SMasami Hiramatsu (Google) {
7704346ba16SMasami Hiramatsu (Google) 	if (!fp || !fp->hlist_array)
771334e5519SMasami Hiramatsu (Google) 		return false;
772334e5519SMasami Hiramatsu (Google) 	return true;
773334e5519SMasami Hiramatsu (Google) }
774334e5519SMasami Hiramatsu (Google) 
775cad9931fSMasami Hiramatsu /**
7764346ba16SMasami Hiramatsu (Google)  * unregister_fprobe() - Unregister fprobe.
777cad9931fSMasami Hiramatsu  * @fp: A fprobe data structure to be unregistered.
778cad9931fSMasami Hiramatsu  *
779cad9931fSMasami Hiramatsu  * Unregister fprobe (and remove ftrace hooks from the function entries).
780cad9931fSMasami Hiramatsu  *
781cad9931fSMasami Hiramatsu  * Return 0 if @fp is unregistered successfully, -errno if not.
782cad9931fSMasami Hiramatsu  */
unregister_fprobe(struct fprobe * fp)783cad9931fSMasami Hiramatsu int unregister_fprobe(struct fprobe *fp)
784cad9931fSMasami Hiramatsu {
7854346ba16SMasami Hiramatsu (Google) 	struct fprobe_hlist *hlist_array;
7864346ba16SMasami Hiramatsu (Google) 	unsigned long *addrs = NULL;
7874346ba16SMasami Hiramatsu (Google) 	int ret = 0, i, count;
788cad9931fSMasami Hiramatsu 
7894346ba16SMasami Hiramatsu (Google) 	mutex_lock(&fprobe_mutex);
7904346ba16SMasami Hiramatsu (Google) 	if (!fp || !is_fprobe_still_exist(fp)) {
7914346ba16SMasami Hiramatsu (Google) 		ret = -EINVAL;
7924346ba16SMasami Hiramatsu (Google) 		goto out;
7934346ba16SMasami Hiramatsu (Google) 	}
794cad9931fSMasami Hiramatsu 
7954346ba16SMasami Hiramatsu (Google) 	hlist_array = fp->hlist_array;
7964346ba16SMasami Hiramatsu (Google) 	addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL);
7974346ba16SMasami Hiramatsu (Google) 	if (!addrs) {
7984346ba16SMasami Hiramatsu (Google) 		ret = -ENOMEM;	/* TODO: Fallback to one-by-one loop */
7994346ba16SMasami Hiramatsu (Google) 		goto out;
8004346ba16SMasami Hiramatsu (Google) 	}
801cad9931fSMasami Hiramatsu 
8024346ba16SMasami Hiramatsu (Google) 	/* Remove non-synonim ips from table and hash */
8034346ba16SMasami Hiramatsu (Google) 	count = 0;
8044346ba16SMasami Hiramatsu (Google) 	for (i = 0; i < hlist_array->size; i++) {
8054346ba16SMasami Hiramatsu (Google) 		if (!delete_fprobe_node(&hlist_array->array[i]))
8064346ba16SMasami Hiramatsu (Google) 			addrs[count++] = hlist_array->array[i].addr;
8074346ba16SMasami Hiramatsu (Google) 	}
8084346ba16SMasami Hiramatsu (Google) 	del_fprobe_hash(fp);
8095b0ab789SMasami Hiramatsu 
8104346ba16SMasami Hiramatsu (Google) 	fprobe_graph_remove_ips(addrs, count);
8115f810187SJiri Olsa 
8124346ba16SMasami Hiramatsu (Google) 	kfree_rcu(hlist_array, rcu);
8134346ba16SMasami Hiramatsu (Google) 	fp->hlist_array = NULL;
814cad9931fSMasami Hiramatsu 
8154346ba16SMasami Hiramatsu (Google) out:
8164346ba16SMasami Hiramatsu (Google) 	mutex_unlock(&fprobe_mutex);
8174346ba16SMasami Hiramatsu (Google) 
8184346ba16SMasami Hiramatsu (Google) 	kfree(addrs);
819cad9931fSMasami Hiramatsu 	return ret;
820cad9931fSMasami Hiramatsu }
821cad9931fSMasami Hiramatsu EXPORT_SYMBOL_GPL(unregister_fprobe);
822