Skip to content

Commit 762abbc

Browse files
mhiramatrostedt
authored andcommitted
fprobe: Use ftrace_regs in fprobe exit handler
Change the fprobe exit handler to use ftrace_regs structure instead of pt_regs. This also introduce HAVE_FTRACE_REGS_HAVING_PT_REGS which means the ftrace_regs is including the pt_regs so that ftrace_regs can provide pt_regs without memory allocation. Fprobe introduces a new dependency with that. Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Acked-by: Heiko Carstens <hca@linux.ibm.com> # s390 Cc: Huacai Chen <chenhuacai@kernel.org> Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com> Cc: Florent Revest <revest@chromium.org> Cc: bpf <bpf@vger.kernel.org> Cc: Alan Maguire <alan.maguire@oracle.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: WANG Xuerui <kernel@xen0n.name> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Christian Borntraeger <borntraeger@linux.ibm.com> Cc: Sven Schnelle <svens@linux.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: x86@kernel.org Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Song Liu <song@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: KP Singh <kpsingh@kernel.org> Cc: Matt Bobrowski <mattbobrowski@google.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Andrii Nakryiko <andrii@kernel.org> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: Eduard Zingerman <eddyz87@gmail.com> Cc: Yonghong Song <yonghong.song@linux.dev> Cc: John Fastabend <john.fastabend@gmail.com> Cc: Stanislav Fomichev <sdf@fomichev.me> Cc: Hao Luo <haoluo@google.com> Cc: Andrew Morton <akpm@linux-foundation.org> Link: https://lore.kernel.org/173518995092.391279.6765116450352977627.stgit@devnote2 Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 46bc082 commit 762abbc

11 files changed

Lines changed: 33 additions & 8 deletions

File tree

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ config LOONGARCH
128128
select HAVE_DMA_CONTIGUOUS
129129
select HAVE_DYNAMIC_FTRACE
130130
select HAVE_DYNAMIC_FTRACE_WITH_ARGS
131+
select HAVE_FTRACE_REGS_HAVING_PT_REGS
131132
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
132133
select HAVE_DYNAMIC_FTRACE_WITH_REGS
133134
select HAVE_EBPF_JIT

arch/s390/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ config S390
183183
select HAVE_DMA_CONTIGUOUS
184184
select HAVE_DYNAMIC_FTRACE
185185
select HAVE_DYNAMIC_FTRACE_WITH_ARGS
186+
select HAVE_FTRACE_REGS_HAVING_PT_REGS
186187
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
187188
select HAVE_DYNAMIC_FTRACE_WITH_REGS
188189
select HAVE_EBPF_JIT if HAVE_MARCH_Z196_FEATURES

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ config X86
225225
select HAVE_DYNAMIC_FTRACE
226226
select HAVE_DYNAMIC_FTRACE_WITH_REGS
227227
select HAVE_DYNAMIC_FTRACE_WITH_ARGS if X86_64
228+
select HAVE_FTRACE_REGS_HAVING_PT_REGS if X86_64
228229
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
229230
select HAVE_SAMPLE_FTRACE_DIRECT if X86_64
230231
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if X86_64

include/linux/fprobe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigned long entry_ip,
1414
void *entry_data);
1515

1616
typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsigned long entry_ip,
17-
unsigned long ret_ip, struct pt_regs *regs,
17+
unsigned long ret_ip, struct ftrace_regs *regs,
1818
void *entry_data);
1919

2020
/**

include/linux/ftrace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ static inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs *fregs)
176176
#define ftrace_regs_set_instruction_pointer(fregs, ip) do { } while (0)
177177
#endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
178178

179+
#ifdef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS
180+
181+
static_assert(sizeof(struct pt_regs) == ftrace_regs_size());
182+
183+
#endif /* CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */
184+
179185
static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
180186
{
181187
if (!fregs)

kernel/trace/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ config HAVE_DYNAMIC_FTRACE_WITH_ARGS
5757
This allows for use of ftrace_regs_get_argument() and
5858
ftrace_regs_get_stack_pointer().
5959

60+
config HAVE_FTRACE_REGS_HAVING_PT_REGS
61+
bool
62+
help
63+
If this is set, ftrace_regs has pt_regs, thus it can convert to
64+
pt_regs without allocating memory.
65+
6066
config HAVE_DYNAMIC_FTRACE_NO_PATCHABLE
6167
bool
6268
help
@@ -298,6 +304,7 @@ config FPROBE
298304
bool "Kernel Function Probe (fprobe)"
299305
depends on FUNCTION_TRACER
300306
depends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS
307+
depends on HAVE_FTRACE_REGS_HAVING_PT_REGS || !HAVE_DYNAMIC_FTRACE_WITH_ARGS
301308
depends on HAVE_RETHOOK
302309
select RETHOOK
303310
default n

kernel/trace/bpf_trace.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2830,10 +2830,14 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
28302830

28312831
static void
28322832
kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
2833-
unsigned long ret_ip, struct pt_regs *regs,
2833+
unsigned long ret_ip, struct ftrace_regs *fregs,
28342834
void *data)
28352835
{
28362836
struct bpf_kprobe_multi_link *link;
2837+
struct pt_regs *regs = ftrace_get_regs(fregs);
2838+
2839+
if (!regs)
2840+
return;
28372841

28382842
link = container_of(fp, struct bpf_kprobe_multi_link, fp);
28392843
kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, data);

kernel/trace/fprobe.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ static void fprobe_exit_handler(struct rethook_node *rh, void *data,
124124
{
125125
struct fprobe *fp = (struct fprobe *)data;
126126
struct fprobe_rethook_node *fpr;
127+
struct ftrace_regs *fregs = (struct ftrace_regs *)regs;
127128
int bit;
128129

129130
if (!fp || fprobe_disabled(fp))
@@ -141,7 +142,7 @@ static void fprobe_exit_handler(struct rethook_node *rh, void *data,
141142
return;
142143
}
143144

144-
fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs,
145+
fp->exit_handler(fp, fpr->entry_ip, ret_ip, fregs,
145146
fp->entry_data_size ? (void *)fpr->data : NULL);
146147
ftrace_test_recursion_unlock(bit);
147148
}

kernel/trace/trace_fprobe.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,14 @@ static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip,
361361
NOKPROBE_SYMBOL(fentry_dispatcher);
362362

363363
static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip,
364-
unsigned long ret_ip, struct pt_regs *regs,
364+
unsigned long ret_ip, struct ftrace_regs *fregs,
365365
void *entry_data)
366366
{
367367
struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
368+
struct pt_regs *regs = ftrace_get_regs(fregs);
369+
370+
if (!regs)
371+
return;
368372

369373
if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE))
370374
fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data);

lib/test_fprobe.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip,
5959

6060
static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip,
6161
unsigned long ret_ip,
62-
struct pt_regs *regs, void *data)
62+
struct ftrace_regs *fregs, void *data)
6363
{
64-
unsigned long ret = regs_return_value(regs);
64+
unsigned long ret = ftrace_regs_get_return_value(fregs);
6565

6666
KUNIT_EXPECT_FALSE(current_test, preemptible());
6767
if (ip != target_ip) {
@@ -89,7 +89,7 @@ static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip,
8989

9090
static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip,
9191
unsigned long ret_ip,
92-
struct pt_regs *regs, void *data)
92+
struct ftrace_regs *fregs, void *data)
9393
{
9494
KUNIT_EXPECT_FALSE(current_test, preemptible());
9595
KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip);

0 commit comments

Comments
 (0)