1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Simple ftrace probe wrapper */
3 #ifndef _LINUX_FPROBE_H
4 #define _LINUX_FPROBE_H
5 
6 #include <linux/compiler.h>
7 #include <linux/ftrace.h>
8 #include <linux/rcupdate.h>
9 #include <linux/refcount.h>
10 #include <linux/slab.h>
11 
12 struct fprobe;
13 typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigned long entry_ip,
14 			       unsigned long ret_ip, struct ftrace_regs *regs,
15 			       void *entry_data);
16 
17 typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsigned long entry_ip,
18 			       unsigned long ret_ip, struct ftrace_regs *regs,
19 			       void *entry_data);
20 
21 /**
22  * struct fprobe_hlist_node - address based hash list node for fprobe.
23  *
24  * @hlist: The hlist node for address search hash table.
25  * @addr: One of the probing address of @fp.
26  * @fp: The fprobe which owns this.
27  */
28 struct fprobe_hlist_node {
29 	struct hlist_node	hlist;
30 	unsigned long		addr;
31 	struct fprobe		*fp;
32 };
33 
34 /**
35  * struct fprobe_hlist - hash list nodes for fprobe.
36  *
37  * @hlist: The hlist node for existence checking hash table.
38  * @rcu: rcu_head for RCU deferred release.
39  * @fp: The fprobe which owns this fprobe_hlist.
40  * @size: The size of @array.
41  * @array: The fprobe_hlist_node for each address to probe.
42  */
43 struct fprobe_hlist {
44 	struct hlist_node		hlist;
45 	struct rcu_head			rcu;
46 	struct fprobe			*fp;
47 	int				size;
48 	struct fprobe_hlist_node	array[] __counted_by(size);
49 };
50 
51 /**
52  * struct fprobe - ftrace based probe.
53  *
54  * @nmissed: The counter for missing events.
55  * @flags: The status flag.
56  * @entry_data_size: The private data storage size.
57  * @entry_handler: The callback function for function entry.
58  * @exit_handler: The callback function for function exit.
59  * @hlist_array: The fprobe_hlist for fprobe search from IP hash table.
60  */
61 struct fprobe {
62 	unsigned long		nmissed;
63 	unsigned int		flags;
64 	size_t			entry_data_size;
65 
66 	fprobe_entry_cb entry_handler;
67 	fprobe_exit_cb  exit_handler;
68 
69 	struct fprobe_hlist	*hlist_array;
70 };
71 
72 /* This fprobe is soft-disabled. */
73 #define FPROBE_FL_DISABLED	1
74 
75 /*
76  * This fprobe handler will be shared with kprobes.
77  * This flag must be set before registering.
78  */
79 #define FPROBE_FL_KPROBE_SHARED	2
80 
fprobe_disabled(struct fprobe * fp)81 static inline bool fprobe_disabled(struct fprobe *fp)
82 {
83 	return (fp) ? fp->flags & FPROBE_FL_DISABLED : false;
84 }
85 
fprobe_shared_with_kprobes(struct fprobe * fp)86 static inline bool fprobe_shared_with_kprobes(struct fprobe *fp)
87 {
88 	return (fp) ? fp->flags & FPROBE_FL_KPROBE_SHARED : false;
89 }
90 
91 #ifdef CONFIG_FPROBE
92 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter);
93 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num);
94 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num);
95 int unregister_fprobe(struct fprobe *fp);
96 bool fprobe_is_registered(struct fprobe *fp);
97 #else
register_fprobe(struct fprobe * fp,const char * filter,const char * notfilter)98 static inline int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
99 {
100 	return -EOPNOTSUPP;
101 }
register_fprobe_ips(struct fprobe * fp,unsigned long * addrs,int num)102 static inline int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
103 {
104 	return -EOPNOTSUPP;
105 }
register_fprobe_syms(struct fprobe * fp,const char ** syms,int num)106 static inline int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
107 {
108 	return -EOPNOTSUPP;
109 }
unregister_fprobe(struct fprobe * fp)110 static inline int unregister_fprobe(struct fprobe *fp)
111 {
112 	return -EOPNOTSUPP;
113 }
fprobe_is_registered(struct fprobe * fp)114 static inline bool fprobe_is_registered(struct fprobe *fp)
115 {
116 	return false;
117 }
118 #endif
119 
120 /**
121  * disable_fprobe() - Disable fprobe
122  * @fp: The fprobe to be disabled.
123  *
124  * This will soft-disable @fp. Note that this doesn't remove the ftrace
125  * hooks from the function entry.
126  */
disable_fprobe(struct fprobe * fp)127 static inline void disable_fprobe(struct fprobe *fp)
128 {
129 	if (fp)
130 		fp->flags |= FPROBE_FL_DISABLED;
131 }
132 
133 /**
134  * enable_fprobe() - Enable fprobe
135  * @fp: The fprobe to be enabled.
136  *
137  * This will soft-enable @fp.
138  */
enable_fprobe(struct fprobe * fp)139 static inline void enable_fprobe(struct fprobe *fp)
140 {
141 	if (fp)
142 		fp->flags &= ~FPROBE_FL_DISABLED;
143 }
144 
145 /* The entry data size is 4 bits (=16) * sizeof(long) in maximum */
146 #define FPROBE_DATA_SIZE_BITS		4
147 #define MAX_FPROBE_DATA_SIZE_WORD	((1L << FPROBE_DATA_SIZE_BITS) - 1)
148 #define MAX_FPROBE_DATA_SIZE		(MAX_FPROBE_DATA_SIZE_WORD * sizeof(long))
149 
150 #endif
151