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