Skip to content

Commit 245d949

Browse files
author
Alexei Starovoitov
committed
Merge branch 'fprobe: Introduce fprobe function entry/exit probe'
Masami Hiramatsu says: ==================== Hi, Here is the 12th version of fprobe. This version fixes a possible gcc-11 issue which was reported as kretprobes on arm issue, and also I updated the fprobe document. The previous version (v11) is here[1]; [1] https://lore.kernel.org/all/164701432038.268462.3329725152949938527.stgit@devnote2/T/#u This series introduces the fprobe, the function entry/exit probe with multiple probe point support for x86, arm64 and powerpc64le. This also introduces the rethook for hooking function return as same as the kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch to it from the rethook in fprobe, depending on the kernel configuration. The patch [1/12] is from Jiri's series[2]. [2] https://lore.kernel.org/all/[email protected]/T/#u And the patch [9/10] adds the FPROBE_FL_KPROBE_SHARED flag for the case if user wants to share the same code (or share a same resource) on the fprobe and the kprobes. I forcibly updated my kprobes/fprobe branch, you can pull this series from: https://git.kernel.org/pub/scm/linux/kernel/git/mhiramat/linux.git kprobes/fprobe Thank you, --- Jiri Olsa (1): ftrace: Add ftrace_set_filter_ips function ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents e0999c8 + f4616fa commit 245d949

File tree

40 files changed

+1876
-14
lines changed

40 files changed

+1876
-14
lines changed

Documentation/trace/fprobe.rst

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
==================================
4+
Fprobe - Function entry/exit probe
5+
==================================
6+
7+
.. Author: Masami Hiramatsu <[email protected]>
8+
9+
Introduction
10+
============
11+
12+
Fprobe is a function entry/exit probe mechanism based on ftrace.
13+
Instead of using ftrace full feature, if you only want to attach callbacks
14+
on function entry and exit, similar to the kprobes and kretprobes, you can
15+
use fprobe. Compared with kprobes and kretprobes, fprobe gives faster
16+
instrumentation for multiple functions with single handler. This document
17+
describes how to use fprobe.
18+
19+
The usage of fprobe
20+
===================
21+
22+
The fprobe is a wrapper of ftrace (+ kretprobe-like return callback) to
23+
attach callbacks to multiple function entry and exit. User needs to set up
24+
the `struct fprobe` and pass it to `register_fprobe()`.
25+
26+
Typically, `fprobe` data structure is initialized with the `entry_handler`
27+
and/or `exit_handler` as below.
28+
29+
.. code-block:: c
30+
31+
struct fprobe fp = {
32+
.entry_handler = my_entry_callback,
33+
.exit_handler = my_exit_callback,
34+
};
35+
36+
To enable the fprobe, call one of register_fprobe(), register_fprobe_ips(), and
37+
register_fprobe_syms(). These functions register the fprobe with different types
38+
of parameters.
39+
40+
The register_fprobe() enables a fprobe by function-name filters.
41+
E.g. this enables @fp on "func*()" function except "func2()".::
42+
43+
register_fprobe(&fp, "func*", "func2");
44+
45+
The register_fprobe_ips() enables a fprobe by ftrace-location addresses.
46+
E.g.
47+
48+
.. code-block:: c
49+
50+
unsigned long ips[] = { 0x.... };
51+
52+
register_fprobe_ips(&fp, ips, ARRAY_SIZE(ips));
53+
54+
And the register_fprobe_syms() enables a fprobe by symbol names.
55+
E.g.
56+
57+
.. code-block:: c
58+
59+
char syms[] = {"func1", "func2", "func3"};
60+
61+
register_fprobe_syms(&fp, syms, ARRAY_SIZE(syms));
62+
63+
To disable (remove from functions) this fprobe, call::
64+
65+
unregister_fprobe(&fp);
66+
67+
You can temporally (soft) disable the fprobe by::
68+
69+
disable_fprobe(&fp);
70+
71+
and resume by::
72+
73+
enable_fprobe(&fp);
74+
75+
The above is defined by including the header::
76+
77+
#include <linux/fprobe.h>
78+
79+
Same as ftrace, the registered callbacks will start being called some time
80+
after the register_fprobe() is called and before it returns. See
81+
:file:`Documentation/trace/ftrace.rst`.
82+
83+
Also, the unregister_fprobe() will guarantee that the both enter and exit
84+
handlers are no longer being called by functions after unregister_fprobe()
85+
returns as same as unregister_ftrace_function().
86+
87+
The fprobe entry/exit handler
88+
=============================
89+
90+
The prototype of the entry/exit callback function is as follows:
91+
92+
.. code-block:: c
93+
94+
void callback_func(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
95+
96+
Note that both entry and exit callbacks have same ptototype. The @entry_ip is
97+
saved at function entry and passed to exit handler.
98+
99+
@fp
100+
This is the address of `fprobe` data structure related to this handler.
101+
You can embed the `fprobe` to your data structure and get it by
102+
container_of() macro from @fp. The @fp must not be NULL.
103+
104+
@entry_ip
105+
This is the ftrace address of the traced function (both entry and exit).
106+
Note that this may not be the actual entry address of the function but
107+
the address where the ftrace is instrumented.
108+
109+
@regs
110+
This is the `pt_regs` data structure at the entry and exit. Note that
111+
the instruction pointer of @regs may be different from the @entry_ip
112+
in the entry_handler. If you need traced instruction pointer, you need
113+
to use @entry_ip. On the other hand, in the exit_handler, the instruction
114+
pointer of @regs is set to the currect return address.
115+
116+
Share the callbacks with kprobes
117+
================================
118+
119+
Since the recursion safeness of the fprobe (and ftrace) is a bit different
120+
from the kprobes, this may cause an issue if user wants to run the same
121+
code from the fprobe and the kprobes.
122+
123+
Kprobes has per-cpu 'current_kprobe' variable which protects the kprobe
124+
handler from recursion in all cases. On the other hand, fprobe uses
125+
only ftrace_test_recursion_trylock(). This allows interrupt context to
126+
call another (or same) fprobe while the fprobe user handler is running.
127+
128+
This is not a matter if the common callback code has its own recursion
129+
detection, or it can handle the recursion in the different contexts
130+
(normal/interrupt/NMI.)
131+
But if it relies on the 'current_kprobe' recursion lock, it has to check
132+
kprobe_running() and use kprobe_busy_*() APIs.
133+
134+
Fprobe has FPROBE_FL_KPROBE_SHARED flag to do this. If your common callback
135+
code will be shared with kprobes, please set FPROBE_FL_KPROBE_SHARED
136+
*before* registering the fprobe, like:
137+
138+
.. code-block:: c
139+
140+
fprobe.flags = FPROBE_FL_KPROBE_SHARED;
141+
142+
register_fprobe(&fprobe, "func*", NULL);
143+
144+
This will protect your common callback from the nested call.
145+
146+
The missed counter
147+
==================
148+
149+
The `fprobe` data structure has `fprobe::nmissed` counter field as same as
150+
kprobes.
151+
This counter counts up when;
152+
153+
- fprobe fails to take ftrace_recursion lock. This usually means that a function
154+
which is traced by other ftrace users is called from the entry_handler.
155+
156+
- fprobe fails to setup the function exit because of the shortage of rethook
157+
(the shadow stack for hooking the function return.)
158+
159+
The `fprobe::nmissed` field counts up in both cases. Therefore, the former
160+
skips both of entry and exit callback and the latter skips the exit
161+
callback, but in both case the counter will increase by 1.
162+
163+
Note that if you set the FTRACE_OPS_FL_RECURSION and/or FTRACE_OPS_FL_RCU to
164+
`fprobe::ops::flags` (ftrace_ops::flags) when registering the fprobe, this
165+
counter may not work correctly, because ftrace skips the fprobe function which
166+
increase the counter.
167+
168+
169+
Functions and structures
170+
========================
171+
172+
.. kernel-doc:: include/linux/fprobe.h
173+
.. kernel-doc:: kernel/trace/fprobe.c
174+

Documentation/trace/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Linux Tracing Technologies
99
tracepoint-analysis
1010
ftrace
1111
ftrace-uses
12+
fprobe
1213
kprobes
1314
kprobetrace
1415
uprobetracer

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ config ARM
107107
select HAVE_MOD_ARCH_SPECIFIC
108108
select HAVE_NMI
109109
select HAVE_OPTPROBES if !THUMB2_KERNEL
110+
select HAVE_RETHOOK
110111
select HAVE_PERF_EVENTS
111112
select HAVE_PERF_REGS
112113
select HAVE_PERF_USER_STACK_DUMP

arch/arm/include/asm/stacktrace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct stackframe {
1414
unsigned long sp;
1515
unsigned long lr;
1616
unsigned long pc;
17-
#ifdef CONFIG_KRETPROBES
17+
#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
1818
struct llist_node *kr_cur;
1919
struct task_struct *tsk;
2020
#endif
@@ -27,7 +27,7 @@ void arm_get_current_stackframe(struct pt_regs *regs, struct stackframe *frame)
2727
frame->sp = regs->ARM_sp;
2828
frame->lr = regs->ARM_lr;
2929
frame->pc = regs->ARM_pc;
30-
#ifdef CONFIG_KRETPROBES
30+
#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
3131
frame->kr_cur = NULL;
3232
frame->tsk = current;
3333
#endif

arch/arm/kernel/stacktrace.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
#include <linux/export.h>
33
#include <linux/kprobes.h>
4+
#include <linux/rethook.h>
45
#include <linux/sched.h>
56
#include <linux/sched/debug.h>
67
#include <linux/stacktrace.h>
@@ -66,6 +67,11 @@ int notrace unwind_frame(struct stackframe *frame)
6667
frame->sp = *(unsigned long *)(fp - 8);
6768
frame->pc = *(unsigned long *)(fp - 4);
6869
#endif
70+
#ifdef CONFIG_RETHOOK
71+
if (is_rethook_trampoline(frame->pc))
72+
frame->pc = rethook_find_ret_addr(frame->tsk, frame->fp,
73+
&frame->kr_cur);
74+
#endif
6975
#ifdef CONFIG_KRETPROBES
7076
if (is_kretprobe_trampoline(frame->pc))
7177
frame->pc = kretprobe_find_ret_addr(frame->tsk,

arch/arm/probes/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ obj-$(CONFIG_KPROBES) += decode-thumb.o
66
else
77
obj-$(CONFIG_KPROBES) += decode-arm.o
88
endif
9+
obj-$(CONFIG_RETHOOK) += rethook.o

arch/arm/probes/rethook.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* arm implementation of rethook. Mostly copied from arch/arm/probes/kprobes/core.c
4+
*/
5+
6+
#include <linux/kprobes.h>
7+
#include <linux/rethook.h>
8+
9+
/* Called from arch_rethook_trampoline */
10+
static __used unsigned long arch_rethook_trampoline_callback(struct pt_regs *regs)
11+
{
12+
return rethook_trampoline_handler(regs, regs->ARM_fp);
13+
}
14+
NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
15+
16+
/*
17+
* When a rethook'ed function returns, it returns to arch_rethook_trampoline
18+
* which calls rethook callback. We construct a struct pt_regs to
19+
* give a view of registers r0-r11, sp, lr, and pc to the user
20+
* return-handler. This is not a complete pt_regs structure, but that
21+
* should be enough for stacktrace from the return handler with or
22+
* without pt_regs.
23+
*/
24+
asm(
25+
".text\n"
26+
".global arch_rethook_trampoline\n"
27+
".type arch_rethook_trampoline, %function\n"
28+
"arch_rethook_trampoline:\n"
29+
#ifdef CONFIG_FRAME_POINTER
30+
"ldr lr, =arch_rethook_trampoline \n\t"
31+
/* this makes a framepointer on pt_regs. */
32+
#ifdef CONFIG_CC_IS_CLANG
33+
"stmdb sp, {sp, lr, pc} \n\t"
34+
"sub sp, sp, #12 \n\t"
35+
/* In clang case, pt_regs->ip = lr. */
36+
"stmdb sp!, {r0 - r11, lr} \n\t"
37+
/* fp points regs->r11 (fp) */
38+
"add fp, sp, #44 \n\t"
39+
#else /* !CONFIG_CC_IS_CLANG */
40+
/* In gcc case, pt_regs->ip = fp. */
41+
"stmdb sp, {fp, sp, lr, pc} \n\t"
42+
"sub sp, sp, #16 \n\t"
43+
"stmdb sp!, {r0 - r11} \n\t"
44+
/* fp points regs->r15 (pc) */
45+
"add fp, sp, #60 \n\t"
46+
#endif /* CONFIG_CC_IS_CLANG */
47+
#else /* !CONFIG_FRAME_POINTER */
48+
"sub sp, sp, #16 \n\t"
49+
"stmdb sp!, {r0 - r11} \n\t"
50+
#endif /* CONFIG_FRAME_POINTER */
51+
"mov r0, sp \n\t"
52+
"bl arch_rethook_trampoline_callback \n\t"
53+
"mov lr, r0 \n\t"
54+
"ldmia sp!, {r0 - r11} \n\t"
55+
"add sp, sp, #16 \n\t"
56+
#ifdef CONFIG_THUMB2_KERNEL
57+
"bx lr \n\t"
58+
#else
59+
"mov pc, lr \n\t"
60+
#endif
61+
".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"
62+
);
63+
NOKPROBE_SYMBOL(arch_rethook_trampoline);
64+
65+
/*
66+
* At the entry of function with mcount. The stack and registers are prepared
67+
* for the mcount function as below.
68+
*
69+
* mov ip, sp
70+
* push {fp, ip, lr, pc}
71+
* sub fp, ip, #4 ; FP[0] = PC, FP[-4] = LR, and FP[-12] = call-site FP.
72+
* push {lr}
73+
* bl <__gnu_mcount_nc> ; call ftrace
74+
*
75+
* And when returning from the function, call-site FP, SP and PC are restored
76+
* from stack as below;
77+
*
78+
* ldm sp, {fp, sp, pc}
79+
*
80+
* Thus, if the arch_rethook_prepare() is called from real function entry,
81+
* it must change the LR and save FP in pt_regs. But if it is called via
82+
* mcount context (ftrace), it must change the LR on stack, which is next
83+
* to the PC (= FP[-4]), and save the FP value at FP[-12].
84+
*/
85+
void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
86+
{
87+
unsigned long *ret_addr, *frame;
88+
89+
if (mcount) {
90+
ret_addr = (unsigned long *)(regs->ARM_fp - 4);
91+
frame = (unsigned long *)(regs->ARM_fp - 12);
92+
} else {
93+
ret_addr = &regs->ARM_lr;
94+
frame = &regs->ARM_fp;
95+
}
96+
97+
rh->ret_addr = *ret_addr;
98+
rh->frame = *frame;
99+
100+
/* Replace the return addr with trampoline addr. */
101+
*ret_addr = (unsigned long)arch_rethook_trampoline;
102+
}
103+
NOKPROBE_SYMBOL(arch_rethook_prepare);

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ config ARM64
201201
select HAVE_SYSCALL_TRACEPOINTS
202202
select HAVE_KPROBES
203203
select HAVE_KRETPROBES
204+
select HAVE_RETHOOK
204205
select HAVE_GENERIC_VDSO
205206
select IOMMU_DMA if IOMMU_SUPPORT
206207
select IRQ_DOMAIN

arch/arm64/include/asm/stacktrace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct stackframe {
5858
DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
5959
unsigned long prev_fp;
6060
enum stack_type prev_type;
61-
#ifdef CONFIG_KRETPROBES
61+
#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
6262
struct llist_node *kr_cur;
6363
#endif
6464
};

arch/arm64/kernel/probes/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o \
44
simulate-insn.o
55
obj-$(CONFIG_UPROBES) += uprobes.o decode-insn.o \
66
simulate-insn.o
7+
obj-$(CONFIG_RETHOOK) += rethook.o rethook_trampoline.o

arch/arm64/kernel/probes/rethook.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Generic return hook for arm64.
4+
* Most of the code is copied from arch/arm64/kernel/probes/kprobes.c
5+
*/
6+
7+
#include <linux/kprobes.h>
8+
#include <linux/rethook.h>
9+
10+
/* This is called from arch_rethook_trampoline() */
11+
unsigned long __used arch_rethook_trampoline_callback(struct pt_regs *regs)
12+
{
13+
return rethook_trampoline_handler(regs, regs->regs[29]);
14+
}
15+
NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
16+
17+
void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount)
18+
{
19+
rhn->ret_addr = regs->regs[30];
20+
rhn->frame = regs->regs[29];
21+
22+
/* replace return addr (x30) with trampoline */
23+
regs->regs[30] = (u64)arch_rethook_trampoline;
24+
}
25+
NOKPROBE_SYMBOL(arch_rethook_prepare);

0 commit comments

Comments
 (0)