Skip to content

Commit cad9931

Browse files
mhiramatAlexei Starovoitov
authored andcommitted
fprobe: Add ftrace based probe APIs
The fprobe is a wrapper API for ftrace function tracer. Unlike kprobes, this probes only supports the function entry, but this can probe multiple functions by one fprobe. The usage is similar, user will set their callback to fprobe::entry_handler and call register_fprobe*() with probed functions. There are 3 registration interfaces, - register_fprobe() takes filtering patterns of the functin names. - register_fprobe_ips() takes an array of ftrace-location addresses. - register_fprobe_syms() takes an array of function names. The registered fprobes can be unregistered with unregister_fprobe(). e.g. struct fprobe fp = { .entry_handler = user_handler }; const char *targets[] = { "func1", "func2", "func3"}; ... ret = register_fprobe_syms(&fp, targets, ARRAY_SIZE(targets)); ... unregister_fprobe(&fp); Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]> Tested-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/164735283857.1084943.1154436951479395551.stgit@devnote2
1 parent 4f554e9 commit cad9931

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed

include/linux/fprobe.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
9+
/**
10+
* struct fprobe - ftrace based probe.
11+
* @ops: The ftrace_ops.
12+
* @nmissed: The counter for missing events.
13+
* @flags: The status flag.
14+
* @entry_handler: The callback function for function entry.
15+
*/
16+
struct fprobe {
17+
#ifdef CONFIG_FUNCTION_TRACER
18+
/*
19+
* If CONFIG_FUNCTION_TRACER is not set, CONFIG_FPROBE is disabled too.
20+
* But user of fprobe may keep embedding the struct fprobe on their own
21+
* code. To avoid build error, this will keep the fprobe data structure
22+
* defined here, but remove ftrace_ops data structure.
23+
*/
24+
struct ftrace_ops ops;
25+
#endif
26+
unsigned long nmissed;
27+
unsigned int flags;
28+
void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
29+
};
30+
31+
#define FPROBE_FL_DISABLED 1
32+
33+
static inline bool fprobe_disabled(struct fprobe *fp)
34+
{
35+
return (fp) ? fp->flags & FPROBE_FL_DISABLED : false;
36+
}
37+
38+
#ifdef CONFIG_FPROBE
39+
int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter);
40+
int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num);
41+
int register_fprobe_syms(struct fprobe *fp, const char **syms, int num);
42+
int unregister_fprobe(struct fprobe *fp);
43+
#else
44+
static inline int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
45+
{
46+
return -EOPNOTSUPP;
47+
}
48+
static inline int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
49+
{
50+
return -EOPNOTSUPP;
51+
}
52+
static inline int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
53+
{
54+
return -EOPNOTSUPP;
55+
}
56+
static inline int unregister_fprobe(struct fprobe *fp)
57+
{
58+
return -EOPNOTSUPP;
59+
}
60+
#endif
61+
62+
/**
63+
* disable_fprobe() - Disable fprobe
64+
* @fp: The fprobe to be disabled.
65+
*
66+
* This will soft-disable @fp. Note that this doesn't remove the ftrace
67+
* hooks from the function entry.
68+
*/
69+
static inline void disable_fprobe(struct fprobe *fp)
70+
{
71+
if (fp)
72+
fp->flags |= FPROBE_FL_DISABLED;
73+
}
74+
75+
/**
76+
* enable_fprobe() - Enable fprobe
77+
* @fp: The fprobe to be enabled.
78+
*
79+
* This will soft-enable @fp.
80+
*/
81+
static inline void enable_fprobe(struct fprobe *fp)
82+
{
83+
if (fp)
84+
fp->flags &= ~FPROBE_FL_DISABLED;
85+
}
86+
87+
#endif

kernel/trace/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ config DYNAMIC_FTRACE_WITH_ARGS
236236
depends on DYNAMIC_FTRACE
237237
depends on HAVE_DYNAMIC_FTRACE_WITH_ARGS
238238

239+
config FPROBE
240+
bool "Kernel Function Probe (fprobe)"
241+
depends on FUNCTION_TRACER
242+
depends on DYNAMIC_FTRACE_WITH_REGS
243+
default n
244+
help
245+
This option enables kernel function probe (fprobe) based on ftrace,
246+
which is similar to kprobes, but probes only for kernel function
247+
entries and it can probe multiple functions by one fprobe.
248+
249+
If unsure, say N.
250+
239251
config FUNCTION_PROFILER
240252
bool "Kernel function profiler"
241253
depends on FUNCTION_TRACER

kernel/trace/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
9797
obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
9898
obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
9999
obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o
100+
obj-$(CONFIG_FPROBE) += fprobe.o
100101

101102
obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
102103

kernel/trace/fprobe.c

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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/slab.h>
12+
#include <linux/sort.h>
13+
14+
static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
15+
struct ftrace_ops *ops, struct ftrace_regs *fregs)
16+
{
17+
struct fprobe *fp;
18+
int bit;
19+
20+
fp = container_of(ops, struct fprobe, ops);
21+
if (fprobe_disabled(fp))
22+
return;
23+
24+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
25+
if (bit < 0) {
26+
fp->nmissed++;
27+
return;
28+
}
29+
30+
if (fp->entry_handler)
31+
fp->entry_handler(fp, ip, ftrace_get_regs(fregs));
32+
33+
ftrace_test_recursion_unlock(bit);
34+
}
35+
NOKPROBE_SYMBOL(fprobe_handler);
36+
37+
/* Convert ftrace location address from symbols */
38+
static unsigned long *get_ftrace_locations(const char **syms, int num)
39+
{
40+
unsigned long addr, size;
41+
unsigned long *addrs;
42+
int i;
43+
44+
/* Convert symbols to symbol address */
45+
addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
46+
if (!addrs)
47+
return ERR_PTR(-ENOMEM);
48+
49+
for (i = 0; i < num; i++) {
50+
addr = kallsyms_lookup_name(syms[i]);
51+
if (!addr) /* Maybe wrong symbol */
52+
goto error;
53+
54+
/* Convert symbol address to ftrace location. */
55+
if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size)
56+
goto error;
57+
58+
addr = ftrace_location_range(addr, addr + size - 1);
59+
if (!addr) /* No dynamic ftrace there. */
60+
goto error;
61+
62+
addrs[i] = addr;
63+
}
64+
65+
return addrs;
66+
67+
error:
68+
kfree(addrs);
69+
70+
return ERR_PTR(-ENOENT);
71+
}
72+
73+
static void fprobe_init(struct fprobe *fp)
74+
{
75+
fp->nmissed = 0;
76+
fp->ops.func = fprobe_handler;
77+
fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
78+
}
79+
80+
/**
81+
* register_fprobe() - Register fprobe to ftrace by pattern.
82+
* @fp: A fprobe data structure to be registered.
83+
* @filter: A wildcard pattern of probed symbols.
84+
* @notfilter: A wildcard pattern of NOT probed symbols.
85+
*
86+
* Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
87+
* If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
88+
*
89+
* Return 0 if @fp is registered successfully, -errno if not.
90+
*/
91+
int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
92+
{
93+
unsigned char *str;
94+
int ret, len;
95+
96+
if (!fp || !filter)
97+
return -EINVAL;
98+
99+
fprobe_init(fp);
100+
101+
len = strlen(filter);
102+
str = kstrdup(filter, GFP_KERNEL);
103+
ret = ftrace_set_filter(&fp->ops, str, len, 0);
104+
kfree(str);
105+
if (ret)
106+
return ret;
107+
108+
if (notfilter) {
109+
len = strlen(notfilter);
110+
str = kstrdup(notfilter, GFP_KERNEL);
111+
ret = ftrace_set_notrace(&fp->ops, str, len, 0);
112+
kfree(str);
113+
if (ret)
114+
goto out;
115+
}
116+
117+
ret = register_ftrace_function(&fp->ops);
118+
out:
119+
if (ret)
120+
ftrace_free_filter(&fp->ops);
121+
return ret;
122+
}
123+
EXPORT_SYMBOL_GPL(register_fprobe);
124+
125+
/**
126+
* register_fprobe_ips() - Register fprobe to ftrace by address.
127+
* @fp: A fprobe data structure to be registered.
128+
* @addrs: An array of target ftrace location addresses.
129+
* @num: The number of entries of @addrs.
130+
*
131+
* Register @fp to ftrace for enabling the probe on the address given by @addrs.
132+
* The @addrs must be the addresses of ftrace location address, which may be
133+
* the symbol address + arch-dependent offset.
134+
* If you unsure what this mean, please use other registration functions.
135+
*
136+
* Return 0 if @fp is registered successfully, -errno if not.
137+
*/
138+
int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
139+
{
140+
int ret;
141+
142+
if (!fp || !addrs || num <= 0)
143+
return -EINVAL;
144+
145+
fprobe_init(fp);
146+
147+
ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
148+
if (!ret)
149+
ret = register_ftrace_function(&fp->ops);
150+
151+
if (ret)
152+
ftrace_free_filter(&fp->ops);
153+
154+
return ret;
155+
}
156+
EXPORT_SYMBOL_GPL(register_fprobe_ips);
157+
158+
/**
159+
* register_fprobe_syms() - Register fprobe to ftrace by symbols.
160+
* @fp: A fprobe data structure to be registered.
161+
* @syms: An array of target symbols.
162+
* @num: The number of entries of @syms.
163+
*
164+
* Register @fp to the symbols given by @syms array. This will be useful if
165+
* you are sure the symbols exist in the kernel.
166+
*
167+
* Return 0 if @fp is registered successfully, -errno if not.
168+
*/
169+
int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
170+
{
171+
unsigned long *addrs;
172+
int ret;
173+
174+
if (!fp || !syms || num <= 0)
175+
return -EINVAL;
176+
177+
addrs = get_ftrace_locations(syms, num);
178+
if (IS_ERR(addrs))
179+
return PTR_ERR(addrs);
180+
181+
ret = register_fprobe_ips(fp, addrs, num);
182+
183+
kfree(addrs);
184+
185+
return ret;
186+
}
187+
EXPORT_SYMBOL_GPL(register_fprobe_syms);
188+
189+
/**
190+
* unregister_fprobe() - Unregister fprobe from ftrace
191+
* @fp: A fprobe data structure to be unregistered.
192+
*
193+
* Unregister fprobe (and remove ftrace hooks from the function entries).
194+
*
195+
* Return 0 if @fp is unregistered successfully, -errno if not.
196+
*/
197+
int unregister_fprobe(struct fprobe *fp)
198+
{
199+
int ret;
200+
201+
if (!fp || fp->ops.func != fprobe_handler)
202+
return -EINVAL;
203+
204+
ret = unregister_ftrace_function(&fp->ops);
205+
206+
if (!ret)
207+
ftrace_free_filter(&fp->ops);
208+
209+
return ret;
210+
}
211+
EXPORT_SYMBOL_GPL(unregister_fprobe);

0 commit comments

Comments
 (0)