From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C863122097; Sun, 10 Nov 2024 15:49:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253741; cv=none; b=pZOMpE3Trq/qZ/7sRQSI5iIx1GPFxqnV1Dqu2B+Xm5eE7kwp8TnLx1zwSYXxGzzieTVRNcMVVtdyW+1s9ACoaOBBobRd+Aa2NHqq6OJJ4P9f2653oJrtGpb/5xckMWbn4jHhl7w+OWFA68qeDABdHXraEH4XbjXsq2DN0RJuDmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253741; c=relaxed/simple; bh=Xxl8oWo+jy78v8CKcFfl9/j2muPhKUTw3b1CnprB3TM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=A/c1uQiyJdbxwaaYBV+TQXbzSmdEe7QSr26CUdPxhOj8WuEgnB8TvWVU2JUq4AVXIbvGOzaOpRCPU5sbEespPI7vYblDyVrIWjmpn7R1t7dwgBdWv/XOD/ab6Fl7kZP/Cr9T/o9xstduGBpwj3Bxutil6VGhs6EqLpQUbFXD7lo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iXMC6i2A; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iXMC6i2A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC500C4CECD; Sun, 10 Nov 2024 15:48:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253741; bh=Xxl8oWo+jy78v8CKcFfl9/j2muPhKUTw3b1CnprB3TM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iXMC6i2AQW087f2Lz4SRJaXK3IIYEpWbKHbuG7z2HntC7HRA8PSNsTKi+311bToZO pUoLJS40bJKF4YjO/G7jFahuM+/WXZpedU7QMaNsZDliRPMNCBlsOx5HwsHVksx4XL VSyJPRecsOUvEkbvio+dasaDpjZZLFjCJpCzpUlzB4rq6YGHHKD65o4fuehQJQcG62 C6hbzF8JQguDxRreS1M9m3eU3wkKOAD8WH4NSbJ3s3lqxdCKonuKlP9evCDFY6B1VM 91+9kpIsccxl4XS4PSvq1F+A+EQvIZwjEGSPfXqMkdKT5S3dcIWT3n8dLZGXiUzVyA IHoLj4kUKNQOQ== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Mathieu Desnoyers Subject: [PATCH v19 01/19] fgraph: Pass ftrace_regs to entryfunc Date: Mon, 11 Nov 2024 00:48:52 +0900 Message-ID: <173125373222.172790.5298554924991579910.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Pass ftrace_regs to the fgraph_ops::entryfunc(). If ftrace_regs is not available, it passes a NULL instead. User callback function can access some registers (including return address) via this ftrace_regs. Note that the ftrace_regs can be NULL when the arch does NOT define: HAVE_DYNAMIC_FTRACE_WITH_ARGS or HAVE_DYNAMIC_FTRACE_WITH_REGS. More specifically, if HAVE_DYNAMIC_FTRACE_WITH_REGS is defined but not the HAVE_DYNAMIC_FTRACE_WITH_ARGS, and the ftrace ops used to register the function callback does not set FTRACE_OPS_FL_SAVE_REGS. In this case, ftrace_regs can be NULL in user callback. Signed-off-by: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Cc: Huacai Chen Cc: WANG Xuerui Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Naveen N Rao Cc: Madhavan Srinivasan Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Mathieu Desnoyers --- Changes in v19: - Remove unneeded FTRACE_OPS_FL_SAVE_ARGS from fgraph_ops. Changes in v18: - Remove unclear comment about `regs->fp` access on arm64. Changes in v16: - Add a note when the ftrace_regs can be NULL. - Update against for the latest kernel. Changes in v11: - Update for the latest for-next branch. Changes in v8: - Just pass ftrace_regs to the handler instead of adding a new entryregfunc. - Update riscv ftrace_graph_func(). Changes in v3: - Update for new multiple fgraph. --- arch/arm64/kernel/ftrace.c | 15 ++++++++- arch/loongarch/kernel/ftrace_dyn.c | 10 +++++- arch/powerpc/kernel/trace/ftrace.c | 2 + arch/powerpc/kernel/trace/ftrace_64_pg.c | 10 ++++-- arch/riscv/kernel/ftrace.c | 17 ++++++++++ arch/x86/kernel/ftrace.c | 50 +++++++++++++++++++++-----= ---- include/linux/ftrace.h | 17 ++++++++-- kernel/trace/fgraph.c | 20 +++++++----- kernel/trace/ftrace.c | 3 +- kernel/trace/trace.h | 3 +- kernel/trace/trace_functions_graph.c | 3 +- kernel/trace/trace_irqsoff.c | 3 +- kernel/trace/trace_sched_wakeup.c | 3 +- kernel/trace/trace_selftest.c | 8 +++-- 14 files changed, 121 insertions(+), 43 deletions(-) diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c index b2d947175cbe..606fd6994578 100644 --- a/arch/arm64/kernel/ftrace.c +++ b/arch/arm64/kernel/ftrace.c @@ -481,7 +481,20 @@ void prepare_ftrace_return(unsigned long self_addr, un= signed long *parent, void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { - prepare_ftrace_return(ip, &arch_ftrace_regs(fregs)->lr, arch_ftrace_regs(= fregs)->fp); + unsigned long return_hooker =3D (unsigned long)&return_to_handler; + unsigned long frame_pointer =3D arch_ftrace_regs(fregs)->fp; + unsigned long *parent =3D &arch_ftrace_regs(fregs)->lr; + unsigned long old; + + if (unlikely(atomic_read(¤t->tracing_graph_pause))) + return; + + old =3D *parent; + + if (!function_graph_enter_regs(old, ip, frame_pointer, + (void *)frame_pointer, fregs)) { + *parent =3D return_hooker; + } } #else /* diff --git a/arch/loongarch/kernel/ftrace_dyn.c b/arch/loongarch/kernel/ftr= ace_dyn.c index 18056229e22e..25c9a4cfd5fa 100644 --- a/arch/loongarch/kernel/ftrace_dyn.c +++ b/arch/loongarch/kernel/ftrace_dyn.c @@ -243,8 +243,16 @@ void ftrace_graph_func(unsigned long ip, unsigned long= parent_ip, { struct pt_regs *regs =3D &arch_ftrace_regs(fregs)->regs; unsigned long *parent =3D (unsigned long *)®s->regs[1]; + unsigned long return_hooker =3D (unsigned long)&return_to_handler; + unsigned long old; + + if (unlikely(atomic_read(¤t->tracing_graph_pause))) + return; + + old =3D *parent; =20 - prepare_ftrace_return(ip, (unsigned long *)parent); + if (!function_graph_enter_regs(old, ip, 0, parent, fregs)) + *parent =3D return_hooker; } #else static int ftrace_modify_graph_caller(bool enable) diff --git a/arch/powerpc/kernel/trace/ftrace.c b/arch/powerpc/kernel/trace= /ftrace.c index df41f4a7c738..c3ec437b530d 100644 --- a/arch/powerpc/kernel/trace/ftrace.c +++ b/arch/powerpc/kernel/trace/ftrace.c @@ -434,7 +434,7 @@ void ftrace_graph_func(unsigned long ip, unsigned long = parent_ip, if (bit < 0) goto out; =20 - if (!function_graph_enter(parent_ip, ip, 0, (unsigned long *)sp)) + if (!function_graph_enter_regs(parent_ip, ip, 0, (unsigned long *)sp, fre= gs)) parent_ip =3D ppc_function_entry(return_to_handler); =20 ftrace_test_recursion_unlock(bit); diff --git a/arch/powerpc/kernel/trace/ftrace_64_pg.c b/arch/powerpc/kernel= /trace/ftrace_64_pg.c index d3c5552e4984..7964d632d13d 100644 --- a/arch/powerpc/kernel/trace/ftrace_64_pg.c +++ b/arch/powerpc/kernel/trace/ftrace_64_pg.c @@ -800,7 +800,8 @@ int ftrace_disable_ftrace_graph_caller(void) * in current thread info. Return the address we want to divert to. */ static unsigned long -__prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned l= ong sp) +__prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned l= ong sp, + struct ftrace_regs *fregs) { unsigned long return_hooker; int bit; @@ -817,7 +818,7 @@ __prepare_ftrace_return(unsigned long parent, unsigned = long ip, unsigned long sp =20 return_hooker =3D ppc_function_entry(return_to_handler); =20 - if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp)) + if (!function_graph_enter_regs(parent, ip, 0, (unsigned long *)sp, fregs)) parent =3D return_hooker; =20 ftrace_test_recursion_unlock(bit); @@ -829,13 +830,14 @@ __prepare_ftrace_return(unsigned long parent, unsigne= d long ip, unsigned long sp void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { - arch_ftrace_regs(fregs)->regs.link =3D __prepare_ftrace_return(parent_ip,= ip, arch_ftrace_regs(fregs)->regs.gpr[1]); + arch_ftrace_regs(fregs)->regs.link =3D __prepare_ftrace_return(parent_ip,= ip, + arch_ftrace_regs(fregs)->regs.gpr[1], fregs); } #else unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned long sp) { - return __prepare_ftrace_return(parent, ip, sp); + return __prepare_ftrace_return(parent, ip, sp, NULL); } #endif #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 5081ad886841..d6ebdb5d7537 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -214,7 +214,22 @@ void prepare_ftrace_return(unsigned long *parent, unsi= gned long self_addr, void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { - prepare_ftrace_return(&arch_ftrace_regs(fregs)->ra, ip, arch_ftrace_regs(= fregs)->s0); + unsigned long return_hooker =3D (unsigned long)&return_to_handler; + unsigned long frame_pointer =3D arch_ftrace_regs(fregs)->s0; + unsigned long *parent =3D &arch_ftrace_regs(fregs)->ra; + unsigned long old; + + if (unlikely(atomic_read(¤t->tracing_graph_pause))) + return; + + /* + * We don't suffer access faults, so no extra fault-recovery assembly + * is needed here. + */ + old =3D *parent; + + if (!function_graph_enter_regs(old, ip, frame_pointer, parent, fregs)) + *parent =3D return_hooker; } #else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */ extern void ftrace_graph_call(void); diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index adb09f78edb2..be224f1c2078 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -605,16 +605,8 @@ int ftrace_disable_ftrace_graph_caller(void) } #endif /* CONFIG_DYNAMIC_FTRACE && !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ =20 -/* - * Hook the return address and push it in the stack of return addrs - * in current thread info. - */ -void prepare_ftrace_return(unsigned long ip, unsigned long *parent, - unsigned long frame_pointer) +static inline bool skip_ftrace_return(void) { - unsigned long return_hooker =3D (unsigned long)&return_to_handler; - int bit; - /* * When resuming from suspend-to-ram, this function can be indirectly * called from early CPU startup code while the CPU is in real mode, @@ -624,13 +616,28 @@ void prepare_ftrace_return(unsigned long ip, unsigned= long *parent, * This check isn't as accurate as virt_addr_valid(), but it should be * good enough for this purpose, and it's fast. */ - if (unlikely((long)__builtin_frame_address(0) >=3D 0)) - return; + if ((long)__builtin_frame_address(0) >=3D 0) + return true; =20 - if (unlikely(ftrace_graph_is_dead())) - return; + if (ftrace_graph_is_dead()) + return true; + + if (atomic_read(¤t->tracing_graph_pause)) + return true; + return false; +} + +/* + * Hook the return address and push it in the stack of return addrs + * in current thread info. + */ +void prepare_ftrace_return(unsigned long ip, unsigned long *parent, + unsigned long frame_pointer) +{ + unsigned long return_hooker =3D (unsigned long)&return_to_handler; + int bit; =20 - if (unlikely(atomic_read(¤t->tracing_graph_pause))) + if (unlikely(skip_ftrace_return())) return; =20 bit =3D ftrace_test_recursion_trylock(ip, *parent); @@ -649,8 +656,21 @@ void ftrace_graph_func(unsigned long ip, unsigned long= parent_ip, { struct pt_regs *regs =3D &arch_ftrace_regs(fregs)->regs; unsigned long *stack =3D (unsigned long *)kernel_stack_pointer(regs); + unsigned long return_hooker =3D (unsigned long)&return_to_handler; + unsigned long *parent =3D (unsigned long *)stack; + int bit; =20 - prepare_ftrace_return(ip, (unsigned long *)stack, 0); + if (unlikely(skip_ftrace_return())) + return; + + bit =3D ftrace_test_recursion_trylock(ip, *parent); + if (bit < 0) + return; + + if (!function_graph_enter_regs(*parent, ip, 0, parent, fregs)) + *parent =3D return_hooker; + + ftrace_test_recursion_unlock(bit); } #endif =20 diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index aa9ddd1e4bb6..c86ac786da3d 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1071,10 +1071,12 @@ struct fgraph_ops; typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *, struct fgraph_ops *); /* return */ typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *, - struct fgraph_ops *); /* entry */ + struct fgraph_ops *, + struct ftrace_regs *); /* entry */ =20 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops); + struct fgraph_ops *gops, + struct ftrace_regs *fregs); bool ftrace_pids_enabled(struct ftrace_ops *ops); =20 #ifdef CONFIG_FUNCTION_GRAPH_TRACER @@ -1114,8 +1116,15 @@ struct ftrace_ret_stack { extern void return_to_handler(void); =20 extern int -function_graph_enter(unsigned long ret, unsigned long func, - unsigned long frame_pointer, unsigned long *retp); +function_graph_enter_regs(unsigned long ret, unsigned long func, + unsigned long frame_pointer, unsigned long *retp, + struct ftrace_regs *fregs); + +static inline int function_graph_enter(unsigned long ret, unsigned long fu= nc, + unsigned long fp, unsigned long *retp) +{ + return function_graph_enter_regs(ret, func, fp, retp, NULL); +} =20 struct ftrace_ret_stack * ftrace_graph_get_ret_stack(struct task_struct *task, int skip); diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 0bf78517b5d4..b2e3df6cce96 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -292,7 +292,8 @@ static inline unsigned long make_data_type_val(int idx,= int size, int offset) } =20 /* ftrace_graph_entry set to this to tell some archs to run function graph= */ -static int entry_run(struct ftrace_graph_ent *trace, struct fgraph_ops *op= s) +static int entry_run(struct ftrace_graph_ent *trace, struct fgraph_ops *op= s, + struct ftrace_regs *fregs) { return 0; } @@ -520,7 +521,8 @@ int __weak ftrace_disable_ftrace_graph_caller(void) #endif =20 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { return 0; } @@ -644,8 +646,9 @@ ftrace_push_return_trace(unsigned long ret, unsigned lo= ng func, #endif =20 /* If the caller does not use ftrace, call this function. */ -int function_graph_enter(unsigned long ret, unsigned long func, - unsigned long frame_pointer, unsigned long *retp) +int function_graph_enter_regs(unsigned long ret, unsigned long func, + unsigned long frame_pointer, unsigned long *retp, + struct ftrace_regs *fregs) { struct ftrace_graph_ent trace; unsigned long bitmap =3D 0; @@ -663,7 +666,7 @@ int function_graph_enter(unsigned long ret, unsigned lo= ng func, if (static_branch_likely(&fgraph_do_direct)) { int save_curr_ret_stack =3D current->curr_ret_stack; =20 - if (static_call(fgraph_func)(&trace, fgraph_direct_gops)) + if (static_call(fgraph_func)(&trace, fgraph_direct_gops, fregs)) bitmap |=3D BIT(fgraph_direct_gops->idx); else /* Clear out any saved storage */ @@ -681,7 +684,7 @@ int function_graph_enter(unsigned long ret, unsigned lo= ng func, =20 save_curr_ret_stack =3D current->curr_ret_stack; if (ftrace_ops_test(&gops->ops, func, NULL) && - gops->entryfunc(&trace, gops)) + gops->entryfunc(&trace, gops, fregs)) bitmap |=3D BIT(i); else /* Clear out any saved storage */ @@ -1174,7 +1177,8 @@ void ftrace_graph_exit_task(struct task_struct *t) =20 #ifdef CONFIG_DYNAMIC_FTRACE static int fgraph_pid_func(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct trace_array *tr =3D gops->ops.private; int pid; @@ -1188,7 +1192,7 @@ static int fgraph_pid_func(struct ftrace_graph_ent *t= race, return 0; } =20 - return gops->saved_func(trace, gops); + return gops->saved_func(trace, gops, fregs); } =20 void fgraph_update_pid_func(void) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 71e53eaba8bc..8183b45edcca 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -827,7 +827,8 @@ struct profile_fgraph_data { }; =20 static int profile_graph_entry(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct profile_fgraph_data *profile_data; =20 diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index ecae6db66f34..d491618bf75e 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -695,7 +695,8 @@ void trace_default_header(struct seq_file *m); void print_trace_header(struct seq_file *m, struct trace_iterator *iter); =20 void trace_graph_return(struct ftrace_graph_ret *trace, struct fgraph_ops = *gops); -int trace_graph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *g= ops); +int trace_graph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *g= ops, + struct ftrace_regs *fregs); =20 void tracing_start_cmdline_record(void); void tracing_stop_cmdline_record(void); diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_func= tions_graph.c index 5504b5e4e7b4..b62ad912d84f 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -175,7 +175,8 @@ struct fgraph_times { }; =20 int trace_graph_entry(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { unsigned long *task_var =3D fgraph_get_task_var(gops); struct trace_array *tr =3D gops->private; diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c index fce064e20570..ad739d76fc86 100644 --- a/kernel/trace/trace_irqsoff.c +++ b/kernel/trace/trace_irqsoff.c @@ -176,7 +176,8 @@ static int irqsoff_display_graph(struct trace_array *tr= , int set) } =20 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct trace_array *tr =3D irqsoff_trace; struct trace_array_cpu *data; diff --git a/kernel/trace/trace_sched_wakeup.c b/kernel/trace/trace_sched_w= akeup.c index d6c7f18daa15..0d9e1075d815 100644 --- a/kernel/trace/trace_sched_wakeup.c +++ b/kernel/trace/trace_sched_wakeup.c @@ -113,7 +113,8 @@ static int wakeup_display_graph(struct trace_array *tr,= int set) } =20 static int wakeup_graph_entry(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct trace_array *tr =3D wakeup_trace; struct trace_array_cpu *data; diff --git a/kernel/trace/trace_selftest.c b/kernel/trace/trace_selftest.c index 38b5754790c9..f54493f8783d 100644 --- a/kernel/trace/trace_selftest.c +++ b/kernel/trace/trace_selftest.c @@ -774,7 +774,8 @@ struct fgraph_fixture { }; =20 static __init int store_entry(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct fgraph_fixture *fixture =3D container_of(gops, struct fgraph_fixtu= re, gops); const char *type =3D fixture->store_type_name; @@ -1025,7 +1026,8 @@ static unsigned int graph_hang_thresh; =20 /* Wrap the real function entry probe to avoid possible hanging */ static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { /* This is harmlessly racy, we want to approximately detect a hang */ if (unlikely(++graph_hang_thresh > GRAPH_MAX_FUNC_TEST)) { @@ -1039,7 +1041,7 @@ static int trace_graph_entry_watchdog(struct ftrace_g= raph_ent *trace, return 0; } =20 - return trace_graph_entry(trace, gops); + return trace_graph_entry(trace, gops, fregs); } =20 static struct fgraph_ops fgraph_ops __initdata =3D { From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 458EA3E49D; Sun, 10 Nov 2024 15:49:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253756; cv=none; b=eB5tMKKykVUaNz9aoqQdytUqhpIp4/OciYvzRNqQKeeXFeU8EvF8Z92WZKLiqy5wi0uOQwt/In96DbXZIQot8tpjD0M06QD1hF0Q7ZMrwRjHlRdmwWW2Tl0JYVOhso0F9GFCbc9KTG2T6W7JS01B7kMYklsZmPSJEpnnymNdrHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253756; c=relaxed/simple; bh=L9cc0eSX6Di0j9HpAyemNPQVbn/sAGkXjWXh9gyZE3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=daMVrq0ZLaX2PTMxm36Cyyh6Kc97D45cy4IBVbpaEs4B5nan6mrgD42e0H5uWteVgb7mrxXlwGDxF9AGqjQaiMP8KyBvLjGYdvlEBIf4AEmYmAL0DTJh8rUQRnAA73/kjsEY+4fj1IKOS92Ba8k1I3sSBSAUStQZQZKqX7ahWeo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sKEbaUCd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sKEbaUCd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5331DC4CECF; Sun, 10 Nov 2024 15:49:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253756; bh=L9cc0eSX6Di0j9HpAyemNPQVbn/sAGkXjWXh9gyZE3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sKEbaUCdnMiBb33Sw0IlnHM0VW9z7csV6NIuw9RaXOecHrHDqX6ReHi083gUnbBiq IWoeXk+3+dO+KvoNJ7KBMTMPFB24bgeH1wkN29Zn0vLnpv4NoGJcpmftOxd3RU7vpo 6hPQFpbgaTghOrmMCFYZK0RsvA9qakNNUNuUp+iNMJAlBoDRV2jaeUIG/kjuKxTdrp nQqOTRPfncumACiaUgJZnI0O7+u9NBH2Tb4U05vsYA32UY8b0Zqh98Yjyib/WsXIcV vGSBPwC+owr0I7xDV7t1X8LJa80AUir6GZYai1Lac9TDN7+kAkO9rFGo12Pm+njLeC VXfkBcgC5+Qqw== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Heiko Carstens , Will Deacon , Catalin Marinas , Huacai Chen , WANG Xuerui , Paul Walmsley , Palmer Dabbelt , Albert Ou , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Mathieu Desnoyers Subject: [PATCH v19 02/19] fgraph: Replace fgraph_ret_regs with ftrace_regs Date: Mon, 11 Nov 2024 00:49:06 +0900 Message-ID: <173125374686.172790.6697511301406596641.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Use ftrace_regs instead of fgraph_ret_regs for tracing return value on function_graph tracer because of simplifying the callback interface. The CONFIG_HAVE_FUNCTION_GRAPH_RETVAL is also replaced by CONFIG_HAVE_FUNCTION_GRAPH_FREGS. Signed-off-by: Masami Hiramatsu (Google) Acked-by: Heiko Carstens Acked-by: Will Deacon Cc: Catalin Marinas Cc: Steven Rostedt Cc: Mark Rutland Cc: Huacai Chen Cc: WANG Xuerui Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Heiko Carstens Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Mathieu Desnoyers --- Changes in v18: - Use PTREGS_SIZE instead of redefining FRAME_SIZE on i386. Changes in v17: - Fixes s390 return_to_handler according to Heiko's advice. Changes in v16: - According to the recent ftrace_regs.h change, override ftrace_regs_get_frame_pointer() if needed. - s390: keep stack_frame on stack, just replace fgraph_ret_regs with ftrace_regs. Changes in v8: - Newly added. --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/ftrace.h | 23 ++++++----------------- arch/arm64/kernel/asm-offsets.c | 12 ------------ arch/arm64/kernel/entry-ftrace.S | 32 ++++++++++++++++++-------------- arch/loongarch/Kconfig | 2 +- arch/loongarch/include/asm/ftrace.h | 26 ++++---------------------- arch/loongarch/kernel/asm-offsets.c | 12 ------------ arch/loongarch/kernel/mcount.S | 17 ++++++++++------- arch/loongarch/kernel/mcount_dyn.S | 14 +++++++------- arch/riscv/Kconfig | 2 +- arch/riscv/include/asm/ftrace.h | 26 +++++--------------------- arch/riscv/kernel/mcount.S | 24 +++++++++++++----------- arch/s390/Kconfig | 2 +- arch/s390/include/asm/ftrace.h | 24 +++++++----------------- arch/s390/kernel/asm-offsets.c | 6 ------ arch/s390/kernel/mcount.S | 12 ++++++------ arch/x86/Kconfig | 2 +- arch/x86/include/asm/ftrace.h | 20 -------------------- arch/x86/kernel/ftrace_32.S | 13 +++++++------ arch/x86/kernel/ftrace_64.S | 17 +++++++++-------- include/linux/ftrace.h | 12 +++++++++--- include/linux/ftrace_regs.h | 2 ++ kernel/trace/Kconfig | 4 ++-- kernel/trace/fgraph.c | 21 +++++++++------------ 24 files changed, 119 insertions(+), 207 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index fd9df6dcc593..012bb9dcb631 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -216,6 +216,7 @@ config ARM64 select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_ERROR_INJECTION + select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_GRAPH_RETVAL select HAVE_GCC_PLUGINS diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index 5ccff4de7f09..b5fa57b61378 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -129,6 +129,12 @@ ftrace_override_function_with_return(struct ftrace_reg= s *fregs) arch_ftrace_regs(fregs)->pc =3D arch_ftrace_regs(fregs)->lr; } =20 +static __always_inline unsigned long +ftrace_regs_get_frame_pointer(const struct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->fp; +} + int ftrace_regs_query_register_offset(const char *name); =20 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); @@ -186,23 +192,6 @@ static inline bool arch_syscall_match_sym_name(const c= har *sym, =20 #ifndef __ASSEMBLY__ #ifdef CONFIG_FUNCTION_GRAPH_TRACER -struct fgraph_ret_regs { - /* x0 - x7 */ - unsigned long regs[8]; - - unsigned long fp; - unsigned long __unused; -}; - -static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret= _regs *ret_regs) -{ - return ret_regs->regs[0]; -} - -static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_re= t_regs *ret_regs) -{ - return ret_regs->fp; -} =20 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent, unsigned long frame_pointer); diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offset= s.c index a5de57f68219..66011aaf0a0a 100644 --- a/arch/arm64/kernel/asm-offsets.c +++ b/arch/arm64/kernel/asm-offsets.c @@ -201,18 +201,6 @@ int main(void) DEFINE(FTRACE_OPS_FUNC, offsetof(struct ftrace_ops, func)); #endif BLANK(); -#ifdef CONFIG_FUNCTION_GRAPH_TRACER - DEFINE(FGRET_REGS_X0, offsetof(struct fgraph_ret_regs, regs[0])); - DEFINE(FGRET_REGS_X1, offsetof(struct fgraph_ret_regs, regs[1])); - DEFINE(FGRET_REGS_X2, offsetof(struct fgraph_ret_regs, regs[2])); - DEFINE(FGRET_REGS_X3, offsetof(struct fgraph_ret_regs, regs[3])); - DEFINE(FGRET_REGS_X4, offsetof(struct fgraph_ret_regs, regs[4])); - DEFINE(FGRET_REGS_X5, offsetof(struct fgraph_ret_regs, regs[5])); - DEFINE(FGRET_REGS_X6, offsetof(struct fgraph_ret_regs, regs[6])); - DEFINE(FGRET_REGS_X7, offsetof(struct fgraph_ret_regs, regs[7])); - DEFINE(FGRET_REGS_FP, offsetof(struct fgraph_ret_regs, fp)); - DEFINE(FGRET_REGS_SIZE, sizeof(struct fgraph_ret_regs)); -#endif #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS DEFINE(FTRACE_OPS_DIRECT_CALL, offsetof(struct ftrace_ops, direct_call)); #endif diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftr= ace.S index f0c16640ef21..169ccf600066 100644 --- a/arch/arm64/kernel/entry-ftrace.S +++ b/arch/arm64/kernel/entry-ftrace.S @@ -329,24 +329,28 @@ SYM_FUNC_END(ftrace_stub_graph) * @fp is checked against the value passed by ftrace_graph_caller(). */ SYM_CODE_START(return_to_handler) - /* save return value regs */ - sub sp, sp, #FGRET_REGS_SIZE - stp x0, x1, [sp, #FGRET_REGS_X0] - stp x2, x3, [sp, #FGRET_REGS_X2] - stp x4, x5, [sp, #FGRET_REGS_X4] - stp x6, x7, [sp, #FGRET_REGS_X6] - str x29, [sp, #FGRET_REGS_FP] // parent's fp + /* Make room for ftrace_regs */ + sub sp, sp, #FREGS_SIZE + + /* Save return value regs */ + stp x0, x1, [sp, #FREGS_X0] + stp x2, x3, [sp, #FREGS_X2] + stp x4, x5, [sp, #FREGS_X4] + stp x6, x7, [sp, #FREGS_X6] + + /* Save the callsite's FP */ + str x29, [sp, #FREGS_FP] =20 mov x0, sp - bl ftrace_return_to_handler // addr =3D ftrace_return_to_hander(regs); + bl ftrace_return_to_handler // addr =3D ftrace_return_to_hander(fregs); mov x30, x0 // restore the original return address =20 - /* restore return value regs */ - ldp x0, x1, [sp, #FGRET_REGS_X0] - ldp x2, x3, [sp, #FGRET_REGS_X2] - ldp x4, x5, [sp, #FGRET_REGS_X4] - ldp x6, x7, [sp, #FGRET_REGS_X6] - add sp, sp, #FGRET_REGS_SIZE + /* Restore return value regs */ + ldp x0, x1, [sp, #FREGS_X0] + ldp x2, x3, [sp, #FREGS_X2] + ldp x4, x5, [sp, #FREGS_X4] + ldp x6, x7, [sp, #FREGS_X6] + add sp, sp, #FREGS_SIZE =20 ret SYM_CODE_END(return_to_handler) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index bb35c34f86d2..73466c9947f6 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -135,7 +135,7 @@ config LOONGARCH select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_ERROR_INJECTION - select HAVE_FUNCTION_GRAPH_RETVAL if HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS diff --git a/arch/loongarch/include/asm/ftrace.h b/arch/loongarch/include/a= sm/ftrace.h index 8f13eaeaa325..ceb3e3d9c0d3 100644 --- a/arch/loongarch/include/asm/ftrace.h +++ b/arch/loongarch/include/asm/ftrace.h @@ -57,6 +57,10 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *= fregs, unsigned long ip) instruction_pointer_set(&arch_ftrace_regs(fregs)->regs, ip); } =20 +#undef ftrace_regs_get_frame_pointer +#define ftrace_regs_get_frame_pointer(fregs) \ + (arch_ftrace_regs(fregs)->regs.regs[22]) + #define ftrace_graph_func ftrace_graph_func void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs); @@ -78,26 +82,4 @@ __arch_ftrace_set_direct_caller(struct pt_regs *regs, un= signed long addr) =20 #endif /* CONFIG_FUNCTION_TRACER */ =20 -#ifndef __ASSEMBLY__ -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -struct fgraph_ret_regs { - /* a0 - a1 */ - unsigned long regs[2]; - - unsigned long fp; - unsigned long __unused; -}; - -static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret= _regs *ret_regs) -{ - return ret_regs->regs[0]; -} - -static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_re= t_regs *ret_regs) -{ - return ret_regs->fp; -} -#endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */ -#endif - #endif /* _ASM_LOONGARCH_FTRACE_H */ diff --git a/arch/loongarch/kernel/asm-offsets.c b/arch/loongarch/kernel/as= m-offsets.c index bee9f7a3108f..714f5b5f1956 100644 --- a/arch/loongarch/kernel/asm-offsets.c +++ b/arch/loongarch/kernel/asm-offsets.c @@ -279,18 +279,6 @@ static void __used output_pbe_defines(void) } #endif =20 -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -static void __used output_fgraph_ret_regs_defines(void) -{ - COMMENT("LoongArch fgraph_ret_regs offsets."); - OFFSET(FGRET_REGS_A0, fgraph_ret_regs, regs[0]); - OFFSET(FGRET_REGS_A1, fgraph_ret_regs, regs[1]); - OFFSET(FGRET_REGS_FP, fgraph_ret_regs, fp); - DEFINE(FGRET_REGS_SIZE, sizeof(struct fgraph_ret_regs)); - BLANK(); -} -#endif - static void __used output_kvm_defines(void) { COMMENT("KVM/LoongArch Specific offsets."); diff --git a/arch/loongarch/kernel/mcount.S b/arch/loongarch/kernel/mcount.S index 3015896016a0..b6850503e061 100644 --- a/arch/loongarch/kernel/mcount.S +++ b/arch/loongarch/kernel/mcount.S @@ -79,10 +79,11 @@ SYM_FUNC_START(ftrace_graph_caller) SYM_FUNC_END(ftrace_graph_caller) =20 SYM_FUNC_START(return_to_handler) - PTR_ADDI sp, sp, -FGRET_REGS_SIZE - PTR_S a0, sp, FGRET_REGS_A0 - PTR_S a1, sp, FGRET_REGS_A1 - PTR_S zero, sp, FGRET_REGS_FP + /* Save return value regs */ + PTR_ADDI sp, sp, -PT_SIZE + PTR_S a0, sp, PT_R4 + PTR_S a1, sp, PT_R5 + PTR_S zero, sp, PT_R22 =20 move a0, sp bl ftrace_return_to_handler @@ -90,9 +91,11 @@ SYM_FUNC_START(return_to_handler) /* Restore the real parent address: a0 -> ra */ move ra, a0 =20 - PTR_L a0, sp, FGRET_REGS_A0 - PTR_L a1, sp, FGRET_REGS_A1 - PTR_ADDI sp, sp, FGRET_REGS_SIZE + /* Restore return value regs */ + PTR_L a0, sp, PT_R4 + PTR_L a1, sp, PT_R5 + PTR_ADDI sp, sp, PT_SIZE + jr ra SYM_FUNC_END(return_to_handler) #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/loongarch/kernel/mcount_dyn.S b/arch/loongarch/kernel/mco= unt_dyn.S index 0c65cf09110c..d6b474ad1d5e 100644 --- a/arch/loongarch/kernel/mcount_dyn.S +++ b/arch/loongarch/kernel/mcount_dyn.S @@ -140,19 +140,19 @@ SYM_CODE_END(ftrace_graph_caller) SYM_CODE_START(return_to_handler) UNWIND_HINT_UNDEFINED /* Save return value regs */ - PTR_ADDI sp, sp, -FGRET_REGS_SIZE - PTR_S a0, sp, FGRET_REGS_A0 - PTR_S a1, sp, FGRET_REGS_A1 - PTR_S zero, sp, FGRET_REGS_FP + PTR_ADDI sp, sp, -PT_SIZE + PTR_S a0, sp, PT_R4 + PTR_S a1, sp, PT_R5 + PTR_S zero, sp, PT_R22 =20 move a0, sp bl ftrace_return_to_handler move ra, a0 =20 /* Restore return value regs */ - PTR_L a0, sp, FGRET_REGS_A0 - PTR_L a1, sp, FGRET_REGS_A1 - PTR_ADDI sp, sp, FGRET_REGS_SIZE + PTR_L a0, sp, PT_R4 + PTR_L a1, sp, PT_R5 + PTR_ADDI sp, sp, PT_SIZE =20 jr ra SYM_CODE_END(return_to_handler) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 62545946ecf4..8afe0f0436d4 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -144,7 +144,7 @@ config RISCV select HAVE_DYNAMIC_FTRACE_WITH_ARGS if HAVE_DYNAMIC_FTRACE select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL select HAVE_FUNCTION_GRAPH_TRACER - select HAVE_FUNCTION_GRAPH_RETVAL if HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_TRACER if !XIP_KERNEL && !PREEMPTION select HAVE_EBPF_JIT if MMU select HAVE_GUP_FAST if MMU diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrac= e.h index 3d66437a1029..9372f8d7036f 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -168,6 +168,11 @@ static __always_inline unsigned long ftrace_regs_get_s= tack_pointer(const struct return arch_ftrace_regs(fregs)->sp; } =20 +static __always_inline unsigned long ftrace_regs_get_frame_pointer(const s= truct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->s0; +} + static __always_inline unsigned long ftrace_regs_get_argument(struct ftrac= e_regs *fregs, unsigned int n) { @@ -208,25 +213,4 @@ static inline void arch_ftrace_set_direct_caller(struc= t ftrace_regs *fregs, unsi =20 #endif /* CONFIG_DYNAMIC_FTRACE */ =20 -#ifndef __ASSEMBLY__ -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -struct fgraph_ret_regs { - unsigned long a1; - unsigned long a0; - unsigned long s0; - unsigned long ra; -}; - -static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret= _regs *ret_regs) -{ - return ret_regs->a0; -} - -static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_re= t_regs *ret_regs) -{ - return ret_regs->s0; -} -#endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */ -#endif - #endif /* _ASM_RISCV_FTRACE_H */ diff --git a/arch/riscv/kernel/mcount.S b/arch/riscv/kernel/mcount.S index 3a42f6287909..068168046e0e 100644 --- a/arch/riscv/kernel/mcount.S +++ b/arch/riscv/kernel/mcount.S @@ -12,6 +12,8 @@ #include #include =20 +#define ABI_SIZE_ON_STACK 80 + .text =20 .macro SAVE_ABI_STATE @@ -26,12 +28,12 @@ * register if a0 was not saved. */ .macro SAVE_RET_ABI_STATE - addi sp, sp, -4*SZREG - REG_S s0, 2*SZREG(sp) - REG_S ra, 3*SZREG(sp) - REG_S a0, 1*SZREG(sp) - REG_S a1, 0*SZREG(sp) - addi s0, sp, 4*SZREG + addi sp, sp, -ABI_SIZE_ON_STACK + REG_S ra, 1*SZREG(sp) + REG_S s0, 8*SZREG(sp) + REG_S a0, 10*SZREG(sp) + REG_S a1, 11*SZREG(sp) + addi s0, sp, ABI_SIZE_ON_STACK .endm =20 .macro RESTORE_ABI_STATE @@ -41,11 +43,11 @@ .endm =20 .macro RESTORE_RET_ABI_STATE - REG_L ra, 3*SZREG(sp) - REG_L s0, 2*SZREG(sp) - REG_L a0, 1*SZREG(sp) - REG_L a1, 0*SZREG(sp) - addi sp, sp, 4*SZREG + REG_L ra, 1*SZREG(sp) + REG_L s0, 8*SZREG(sp) + REG_L a0, 10*SZREG(sp) + REG_L a1, 11*SZREG(sp) + addi sp, sp, ABI_SIZE_ON_STACK .endm =20 SYM_TYPED_FUNC_START(ftrace_stub) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index d339fe4fdedf..112e83601ed5 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -184,7 +184,7 @@ config S390 select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_ERROR_INJECTION - select HAVE_FUNCTION_GRAPH_RETVAL + select HAVE_FUNCTION_GRAPH_FREGS select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index fc97d75dc752..5c94c1fc1bc1 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -62,23 +62,6 @@ static __always_inline struct pt_regs *arch_ftrace_get_r= egs(struct ftrace_regs * return NULL; } =20 -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -struct fgraph_ret_regs { - unsigned long gpr2; - unsigned long fp; -}; - -static __always_inline unsigned long fgraph_ret_regs_return_value(struct f= graph_ret_regs *ret_regs) -{ - return ret_regs->gpr2; -} - -static __always_inline unsigned long fgraph_ret_regs_frame_pointer(struct = fgraph_ret_regs *ret_regs) -{ - return ret_regs->fp; -} -#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ - static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip) @@ -86,6 +69,13 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *= fregs, arch_ftrace_regs(fregs)->regs.psw.addr =3D ip; } =20 +#undef ftrace_regs_get_frame_pointer +static __always_inline unsigned long +ftrace_regs_get_frame_pointer(struct ftrace_regs *fregs) +{ + return ftrace_regs_get_stack_pointer(fregs); +} + #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS /* * When an ftrace registered caller is tracing a function that is diff --git a/arch/s390/kernel/asm-offsets.c b/arch/s390/kernel/asm-offsets.c index db9659980175..be82fd8c1414 100644 --- a/arch/s390/kernel/asm-offsets.c +++ b/arch/s390/kernel/asm-offsets.c @@ -178,12 +178,6 @@ int main(void) DEFINE(OLDMEM_SIZE, PARMAREA + offsetof(struct parmarea, oldmem_size)); DEFINE(COMMAND_LINE, PARMAREA + offsetof(struct parmarea, command_line)); DEFINE(MAX_COMMAND_LINE_SIZE, PARMAREA + offsetof(struct parmarea, max_co= mmand_line_size)); -#ifdef CONFIG_FUNCTION_GRAPH_TRACER - /* function graph return value tracing */ - OFFSET(__FGRAPH_RET_GPR2, fgraph_ret_regs, gpr2); - OFFSET(__FGRAPH_RET_FP, fgraph_ret_regs, fp); - DEFINE(__FGRAPH_RET_SIZE, sizeof(struct fgraph_ret_regs)); -#endif OFFSET(__FTRACE_REGS_PT_REGS, __arch_ftrace_regs, regs); DEFINE(__FTRACE_REGS_SIZE, sizeof(struct __arch_ftrace_regs)); =20 diff --git a/arch/s390/kernel/mcount.S b/arch/s390/kernel/mcount.S index 7e267ef63a7f..2b628aa3d809 100644 --- a/arch/s390/kernel/mcount.S +++ b/arch/s390/kernel/mcount.S @@ -134,14 +134,14 @@ SYM_CODE_END(ftrace_common) SYM_FUNC_START(return_to_handler) stmg %r2,%r5,32(%r15) lgr %r1,%r15 - aghi %r15,-(STACK_FRAME_OVERHEAD+__FGRAPH_RET_SIZE) + # allocate ftrace_regs and stack frame for ftrace_return_to_handler + aghi %r15,-STACK_FRAME_SIZE_FREGS stg %r1,__SF_BACKCHAIN(%r15) - la %r3,STACK_FRAME_OVERHEAD(%r15) - stg %r1,__FGRAPH_RET_FP(%r3) - stg %r2,__FGRAPH_RET_GPR2(%r3) - lgr %r2,%r3 + stg %r2,(STACK_FREGS_PTREGS_GPRS+2*8)(%r15) + stg %r1,(STACK_FREGS_PTREGS_GPRS+15*8)(%r15) + la %r2,STACK_FRAME_OVERHEAD(%r15) brasl %r14,ftrace_return_to_handler - aghi %r15,STACK_FRAME_OVERHEAD+__FGRAPH_RET_SIZE + aghi %r15,STACK_FRAME_SIZE_FREGS lgr %r14,%r2 lmg %r2,%r5,32(%r15) BR_EX %r14 diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2852fcd82cbd..2697ebbfce8d 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -232,7 +232,7 @@ config X86 select HAVE_GUP_FAST select HAVE_FENTRY if X86_64 || DYNAMIC_FTRACE select HAVE_FTRACE_MCOUNT_RECORD - select HAVE_FUNCTION_GRAPH_RETVAL if HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FUNCTION_GRAPH_FREGS if HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_GRAPH_TRACER if X86_32 || (X86_64 && DYNAMIC_FTRACE) select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index 6e8cf0fa48fc..d61407c680c2 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -134,24 +134,4 @@ static inline bool arch_trace_is_compat_syscall(struct= pt_regs *regs) #endif /* !COMPILE_OFFSETS */ #endif /* !__ASSEMBLY__ */ =20 -#ifndef __ASSEMBLY__ -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -struct fgraph_ret_regs { - unsigned long ax; - unsigned long dx; - unsigned long bp; -}; - -static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret= _regs *ret_regs) -{ - return ret_regs->ax; -} - -static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_re= t_regs *ret_regs) -{ - return ret_regs->bp; -} -#endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */ -#endif - #endif /* _ASM_X86_FTRACE_H */ diff --git a/arch/x86/kernel/ftrace_32.S b/arch/x86/kernel/ftrace_32.S index 58d9ed50fe61..8e1a27d2c1eb 100644 --- a/arch/x86/kernel/ftrace_32.S +++ b/arch/x86/kernel/ftrace_32.S @@ -187,14 +187,15 @@ SYM_CODE_END(ftrace_graph_caller) =20 .globl return_to_handler return_to_handler: - pushl $0 - pushl %edx - pushl %eax + subl $(PTREGS_SIZE), %esp + movl $0, PT_EBP(%esp) + movl %edx, PT_EDX(%esp) + movl %eax, PT_EAX(%esp) movl %esp, %eax call ftrace_return_to_handler movl %eax, %ecx - popl %eax - popl %edx - addl $4, %esp # skip ebp + movl %eax, PT_EAX(%esp) + movl %edx, PT_EDX(%esp) + addl $(PTREGS_SIZE), %esp JMP_NOSPEC ecx #endif diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S index 214f30e9f0c0..d51647228596 100644 --- a/arch/x86/kernel/ftrace_64.S +++ b/arch/x86/kernel/ftrace_64.S @@ -348,21 +348,22 @@ STACK_FRAME_NON_STANDARD_FP(__fentry__) SYM_CODE_START(return_to_handler) UNWIND_HINT_UNDEFINED ANNOTATE_NOENDBR - subq $24, %rsp =20 - /* Save the return values */ - movq %rax, (%rsp) - movq %rdx, 8(%rsp) - movq %rbp, 16(%rsp) + /* Save ftrace_regs for function exit context */ + subq $(FRAME_SIZE), %rsp + + movq %rax, RAX(%rsp) + movq %rdx, RDX(%rsp) + movq %rbp, RBP(%rsp) movq %rsp, %rdi =20 call ftrace_return_to_handler =20 movq %rax, %rdi - movq 8(%rsp), %rdx - movq (%rsp), %rax + movq RDX(%rsp), %rdx + movq RAX(%rsp), %rax =20 - addq $24, %rsp + addq $(FRAME_SIZE), %rsp /* * Jump back to the old return address. This cannot be JMP_NOSPEC rdi * since IBT would demand that contain ENDBR, which simply isn't so for diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index c86ac786da3d..069f270bd7ae 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -43,9 +43,8 @@ struct dyn_ftrace; =20 char *arch_ftrace_match_adjust(char *str, const char *search); =20 -#ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL -struct fgraph_ret_regs; -unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs); +#ifdef CONFIG_HAVE_FUNCTION_GRAPH_FREGS +unsigned long ftrace_return_to_handler(struct ftrace_regs *fregs); #else unsigned long ftrace_return_to_handler(unsigned long frame_pointer); #endif @@ -134,6 +133,13 @@ extern int ftrace_enabled; * Also, architecture dependent fields can be used for internal process. * (e.g. orig_ax on x86_64) * + * Basically, ftrace_regs stores the registers related to the context. + * On function entry, registers for function parameters and hooking the + * function call are stored, and on function exit, registers for function + * return value and frame pointers are stored. + * + * And also, it dpends on the context that which registers are restored + * from the ftrace_regs. * On the function entry, those registers will be restored except for * the stack pointer, so that user can change the function parameters * and instruction pointer (e.g. live patching.) diff --git a/include/linux/ftrace_regs.h b/include/linux/ftrace_regs.h index be1ed0c891d0..bbc1873ca6b8 100644 --- a/include/linux/ftrace_regs.h +++ b/include/linux/ftrace_regs.h @@ -30,6 +30,8 @@ struct ftrace_regs; override_function_with_return(&arch_ftrace_regs(fregs)->regs) #define ftrace_regs_query_register_offset(name) \ regs_query_register_offset(name) +#define ftrace_regs_get_frame_pointer(fregs) \ + frame_pointer(&arch_ftrace_regs(fregs)->regs) =20 #endif /* HAVE_ARCH_FTRACE_REGS */ =20 diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 74c2b1d43bb9..c5ab2a561272 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -31,7 +31,7 @@ config HAVE_FUNCTION_GRAPH_TRACER help See Documentation/trace/ftrace-design.rst =20 -config HAVE_FUNCTION_GRAPH_RETVAL +config HAVE_FUNCTION_GRAPH_FREGS bool =20 config HAVE_DYNAMIC_FTRACE @@ -232,7 +232,7 @@ config FUNCTION_GRAPH_TRACER =20 config FUNCTION_GRAPH_RETVAL bool "Kernel Function Graph Return Value" - depends on HAVE_FUNCTION_GRAPH_RETVAL + depends on HAVE_FUNCTION_GRAPH_FREGS depends on FUNCTION_GRAPH_TRACER default n help diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index b2e3df6cce96..126671cd38e5 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -795,15 +795,12 @@ static struct notifier_block ftrace_suspend_notifier = =3D { .notifier_call =3D ftrace_suspend_notifier_call, }; =20 -/* fgraph_ret_regs is not defined without CONFIG_FUNCTION_GRAPH_RETVAL */ -struct fgraph_ret_regs; - /* * Send the trace to the ring-buffer. * @return the original return address. */ -static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *re= t_regs, - unsigned long frame_pointer) +static inline unsigned long +__ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_= pointer) { struct ftrace_ret_stack *ret_stack; struct ftrace_graph_ret trace; @@ -823,7 +820,7 @@ static unsigned long __ftrace_return_to_handler(struct = fgraph_ret_regs *ret_regs =20 trace.rettime =3D trace_clock_local(); #ifdef CONFIG_FUNCTION_GRAPH_RETVAL - trace.retval =3D fgraph_ret_regs_return_value(ret_regs); + trace.retval =3D ftrace_regs_get_return_value(fregs); #endif =20 bitmap =3D get_bitmap_bits(current, offset); @@ -858,14 +855,14 @@ static unsigned long __ftrace_return_to_handler(struc= t fgraph_ret_regs *ret_regs } =20 /* - * After all architecures have selected HAVE_FUNCTION_GRAPH_RETVAL, we can - * leave only ftrace_return_to_handler(ret_regs). + * After all architecures have selected HAVE_FUNCTION_GRAPH_FREGS, we can + * leave only ftrace_return_to_handler(fregs). */ -#ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL -unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs) +#ifdef CONFIG_HAVE_FUNCTION_GRAPH_FREGS +unsigned long ftrace_return_to_handler(struct ftrace_regs *fregs) { - return __ftrace_return_to_handler(ret_regs, - fgraph_ret_regs_frame_pointer(ret_regs)); + return __ftrace_return_to_handler(fregs, + ftrace_regs_get_frame_pointer(fregs)); } #else unsigned long ftrace_return_to_handler(unsigned long frame_pointer) From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B2A754670; Sun, 10 Nov 2024 15:49:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253767; cv=none; b=ARBEvOLyXV/h7/2z537lXVwg228XK2cob4xdOLEMVIDnmAiD1uzYOjfou5nrYwfWEI/UFu+8T2DQwemCfsq/fYcj6Sk3wsjw6KvLT53BS9YpaHOvrg6NngcyHaVXu6wKYyt5i+RLZ6II5eqX2WWyred//JyRNStkH3pfmSp2UZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253767; c=relaxed/simple; bh=fy5HBN3FEQJW3L0aIlEA7Y6GGFOSQl58j9oW53za+hU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s/RqofM+99PoInEUSZQ1ZJiz5JlvbxDzbDec1wBodzELupqoMVoU3euXXVk5+zI7rFbPNZCJx2JWbwr2CDfoCU5TpeQKkV3Eb7mpCOD2GYL3se2JRGTEBSzgMu/WW7np98x6DbGjYwK5e3vLB810yRBCe6ZyUAcSSv4I+viPUxk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gDAeLxjo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gDAeLxjo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6A11C4CECD; Sun, 10 Nov 2024 15:49:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253766; bh=fy5HBN3FEQJW3L0aIlEA7Y6GGFOSQl58j9oW53za+hU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gDAeLxjo3Rc6Rm85LV+wH4MzFWRfjLHFKCYOSqUknVoA7sSRhL0IYBHz1m0Jefcx1 tb/op/sVPMyx1Kbzj0q+rgi6P5d/wuWpLXmH9gKIncMFV5p8qbK10r9BnwOwH2ok5a rXxw+8lWEH6pkD3hkL5FudOAiny5IexSa6DQtDyw0/wGBOoYoFh4WQg1Wirb7nCMPj uQQw3UdFBTESsvzTp2znI7EAkouik+92SXzPboeqFc3w/aI46Zd1b7/YkuPq/uwtgm tnnqx9d72cLjveXrHAYJPEw0120tZXl2ioPJN1HXFSFEpC0h7nF5VPq1tSPKvZseJR yCOizqKrVzIWQ== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 03/19] fgraph: Pass ftrace_regs to retfunc Date: Mon, 11 Nov 2024 00:49:21 +0900 Message-ID: <173125376150.172790.9363349123343684485.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Pass ftrace_regs to the fgraph_ops::retfunc(). If ftrace_regs is not available, it passes a NULL instead. User callback function can access some registers (including return address) via this ftrace_regs. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v8: - Pass ftrace_regs to retfunc, instead of adding retregfunc. Changes in v6: - update to use ftrace_regs_get_return_value() because of reordering patches. Changes in v3: - Update for new multiple fgraph. - Save the return address to instruction pointer in ftrace_regs. --- include/linux/ftrace.h | 3 ++- kernel/trace/fgraph.c | 16 +++++++++++----- kernel/trace/ftrace.c | 3 ++- kernel/trace/trace.h | 3 ++- kernel/trace/trace_functions_graph.c | 7 ++++--- kernel/trace/trace_irqsoff.c | 3 ++- kernel/trace/trace_sched_wakeup.c | 3 ++- kernel/trace/trace_selftest.c | 3 ++- 8 files changed, 27 insertions(+), 14 deletions(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 069f270bd7ae..9a1e768e47da 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1075,7 +1075,8 @@ struct fgraph_ops; =20 /* Type of the callback handlers for tracing function graph*/ typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *, - struct fgraph_ops *); /* return */ + struct fgraph_ops *, + struct ftrace_regs *); /* return */ typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *, struct fgraph_ops *, struct ftrace_regs *); /* entry */ diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 126671cd38e5..7f4c2001e382 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -299,7 +299,8 @@ static int entry_run(struct ftrace_graph_ent *trace, st= ruct fgraph_ops *ops, } =20 /* ftrace_graph_return set to this to tell some archs to run function grap= h */ -static void return_run(struct ftrace_graph_ret *trace, struct fgraph_ops *= ops) +static void return_run(struct ftrace_graph_ret *trace, struct fgraph_ops *= ops, + struct ftrace_regs *fregs) { } =20 @@ -528,7 +529,8 @@ int ftrace_graph_entry_stub(struct ftrace_graph_ent *tr= ace, } =20 static void ftrace_graph_ret_stub(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { } =20 @@ -819,6 +821,9 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, u= nsigned long frame_pointe } =20 trace.rettime =3D trace_clock_local(); + if (fregs) + ftrace_regs_set_instruction_pointer(fregs, ret); + #ifdef CONFIG_FUNCTION_GRAPH_RETVAL trace.retval =3D ftrace_regs_get_return_value(fregs); #endif @@ -828,7 +833,7 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, u= nsigned long frame_pointe #ifdef CONFIG_HAVE_STATIC_CALL if (static_branch_likely(&fgraph_do_direct)) { if (test_bit(fgraph_direct_gops->idx, &bitmap)) - static_call(fgraph_retfunc)(&trace, fgraph_direct_gops); + static_call(fgraph_retfunc)(&trace, fgraph_direct_gops, fregs); } else #endif { @@ -838,7 +843,7 @@ __ftrace_return_to_handler(struct ftrace_regs *fregs, u= nsigned long frame_pointe if (gops =3D=3D &fgraph_stub) continue; =20 - gops->retfunc(&trace, gops); + gops->retfunc(&trace, gops, fregs); } } =20 @@ -1010,7 +1015,8 @@ void ftrace_graph_sleep_time_control(bool enable) * Simply points to ftrace_stub, but with the proper protocol. * Defined by the linker script in linux/vmlinux.lds.h */ -void ftrace_stub_graph(struct ftrace_graph_ret *trace, struct fgraph_ops *= gops); +void ftrace_stub_graph(struct ftrace_graph_ret *trace, struct fgraph_ops *= gops, + struct ftrace_regs *fregs); =20 /* The callbacks that hook a function */ trace_func_graph_ret_t ftrace_graph_return =3D ftrace_stub_graph; diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 8183b45edcca..6c8b1d51946c 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -850,7 +850,8 @@ static int profile_graph_entry(struct ftrace_graph_ent = *trace, } =20 static void profile_graph_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct profile_fgraph_data *profile_data; struct ftrace_profile_stat *stat; diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index d491618bf75e..358a694aa796 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -694,7 +694,8 @@ void trace_latency_header(struct seq_file *m); void trace_default_header(struct seq_file *m); void print_trace_header(struct seq_file *m, struct trace_iterator *iter); =20 -void trace_graph_return(struct ftrace_graph_ret *trace, struct fgraph_ops = *gops); +void trace_graph_return(struct ftrace_graph_ret *trace, struct fgraph_ops = *gops, + struct ftrace_regs *fregs); int trace_graph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *g= ops, struct ftrace_regs *fregs); =20 diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_func= tions_graph.c index b62ad912d84f..d0e4f412c298 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -315,7 +315,7 @@ static void handle_nosleeptime(struct ftrace_graph_ret = *trace, } =20 void trace_graph_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, struct ftrace_regs *fregs) { unsigned long *task_var =3D fgraph_get_task_var(gops); struct trace_array *tr =3D gops->private; @@ -355,7 +355,8 @@ void trace_graph_return(struct ftrace_graph_ret *trace, } =20 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct fgraph_times *ftimes; int size; @@ -379,7 +380,7 @@ static void trace_graph_thresh_return(struct ftrace_gra= ph_ret *trace, (trace->rettime - ftimes->calltime < tracing_thresh)) return; else - trace_graph_return(trace, gops); + trace_graph_return(trace, gops, fregs); } =20 static struct fgraph_ops funcgraph_ops =3D { diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c index ad739d76fc86..504de7a05498 100644 --- a/kernel/trace/trace_irqsoff.c +++ b/kernel/trace/trace_irqsoff.c @@ -208,7 +208,8 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent = *trace, } =20 static void irqsoff_graph_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct trace_array *tr =3D irqsoff_trace; struct trace_array_cpu *data; diff --git a/kernel/trace/trace_sched_wakeup.c b/kernel/trace/trace_sched_w= akeup.c index 0d9e1075d815..8165382a174a 100644 --- a/kernel/trace/trace_sched_wakeup.c +++ b/kernel/trace/trace_sched_wakeup.c @@ -144,7 +144,8 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *= trace, } =20 static void wakeup_graph_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct trace_array *tr =3D wakeup_trace; struct trace_array_cpu *data; diff --git a/kernel/trace/trace_selftest.c b/kernel/trace/trace_selftest.c index f54493f8783d..d88c44f1dfa5 100644 --- a/kernel/trace/trace_selftest.c +++ b/kernel/trace/trace_selftest.c @@ -808,7 +808,8 @@ static __init int store_entry(struct ftrace_graph_ent *= trace, } =20 static __init void store_return(struct ftrace_graph_ret *trace, - struct fgraph_ops *gops) + struct fgraph_ops *gops, + struct ftrace_regs *fregs) { struct fgraph_fixture *fixture =3D container_of(gops, struct fgraph_fixtu= re, gops); const char *type =3D fixture->store_type_name; From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B79C55C603; Sun, 10 Nov 2024 15:49:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253777; cv=none; b=hBKv9Gm8IhTjtfRUqUZvsWOFhMd0CwuEYlV0IIKu3kAyPAu3RdAzC2cFG6jdo75RlqHsVPtwAPGux6+e7wZzl5PWVfEdbO1/z8AHVRT+LlbbcohqJw9byz6zA8eDSAeQOUueMDwRfRZIVIO4PKCvDOXdhhjzQYENHkMUD+NwX+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253777; c=relaxed/simple; bh=0guW880MFdqcOEZZF4PeBunb0D0It/NmNJp3VWvlvwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RZ8tSf2ZAMsSVZ/4YwGEe8WofYO5dBd6lXAETpcmDjgbj3d+OMZxjUimn2ssDn/w/ap7+d9AGU6P0STnGrhe8fC5KxlLQYJaD87u+4SrPc1gbFEvKHJdsBrsASNZ4Y+/NIQuWa/JvNpRsRV7jHZVTFPC0xZiutNfFwNnL/o56w8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=icshDaSU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="icshDaSU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9AA80C4CECD; Sun, 10 Nov 2024 15:49:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253777; bh=0guW880MFdqcOEZZF4PeBunb0D0It/NmNJp3VWvlvwg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=icshDaSUcbF8Lpf4YgZCjnz0mVInN4a3mq+VjkB0TPn5D8QW+gdL3i9zdt8/I6lbM we9v2/J2a8+rzNFjWe3csG9mglBb/4dujaQgi9n8xqF3ywK1o9jWlKbZ6/aiQwMFHe JKt+1a1Jf2Zy9219i5z/R3Rl10RhgrlR95xRFNHVP6ziQNkuioSIkT4RI8v92g5umb F7S5yLfBiajLXTLdlReV2JCB22DaGy6eBUqbHsBYCiThhdBtZLicsgb+vUwv84GHUj 84SGHWWOn+iULSiOMPw4a5M42oGPi9HBXgQkRTySqP441NHQHgsX0QZZpC7QTYRPmt iApFKn4J9SQLQ== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 04/19] fprobe: Use ftrace_regs in fprobe entry handler Date: Mon, 11 Nov 2024 00:49:32 +0900 Message-ID: <173125377212.172790.1184199226080903806.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) This allows fprobes to be available with CONFIG_DYNAMIC_FTRACE_WITH_ARGS instead of CONFIG_DYNAMIC_FTRACE_WITH_REGS, then we can enable fprobe on arm64. Signed-off-by: Masami Hiramatsu (Google) Acked-by: Florent Revest --- Changes in v6: - Keep using SAVE_REGS flag to avoid breaking bpf kprobe-multi test. --- include/linux/fprobe.h | 2 +- kernel/trace/Kconfig | 3 ++- kernel/trace/bpf_trace.c | 10 +++++++--- kernel/trace/fprobe.c | 3 ++- kernel/trace/trace_fprobe.c | 6 +++++- lib/test_fprobe.c | 4 ++-- samples/fprobe/fprobe_example.c | 2 +- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index f39869588117..ca64ee5e45d2 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -10,7 +10,7 @@ struct fprobe; =20 typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *regs, void *entry_data); =20 typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsigned long entry_ip, diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index c5ab2a561272..f10ca86fbfad 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -297,7 +297,7 @@ config DYNAMIC_FTRACE_WITH_ARGS config FPROBE bool "Kernel Function Probe (fprobe)" depends on FUNCTION_TRACER - depends on DYNAMIC_FTRACE_WITH_REGS + depends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS depends on HAVE_RETHOOK select RETHOOK default n @@ -682,6 +682,7 @@ config FPROBE_EVENTS select TRACING select PROBE_EVENTS select DYNAMIC_EVENTS + depends on DYNAMIC_FTRACE_WITH_REGS default y help This allows user to add tracing events on the function entry and diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3bd402fa62a4..09fb6ce92144 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2517,7 +2517,7 @@ struct bpf_session_run_ctx { void *data; }; =20 -#ifdef CONFIG_FPROBE +#if defined(CONFIG_FPROBE) && defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) struct bpf_kprobe_multi_link { struct bpf_link link; struct fprobe fp; @@ -2769,12 +2769,16 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_= link *link, =20 static int kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *data) { + struct pt_regs *regs =3D ftrace_get_regs(fregs); struct bpf_kprobe_multi_link *link; int err; =20 + if (!regs) + return 0; + link =3D container_of(fp, struct bpf_kprobe_multi_link, fp); err =3D kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, f= alse, data); return is_kprobe_session(link->link.prog) ? err : 0; @@ -3049,7 +3053,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr= *attr, struct bpf_prog *pr kvfree(cookies); return err; } -#else /* !CONFIG_FPROBE */ +#else /* !CONFIG_FPROBE || !CONFIG_DYNAMIC_FTRACE_WITH_REGS */ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_pr= og *prog) { return -EOPNOTSUPP; diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 9ff018245840..3d3789283873 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -46,7 +46,7 @@ static inline void __fprobe_handler(unsigned long ip, uns= igned long parent_ip, } =20 if (fp->entry_handler) - ret =3D fp->entry_handler(fp, ip, parent_ip, ftrace_get_regs(fregs), ent= ry_data); + ret =3D fp->entry_handler(fp, ip, parent_ip, fregs, entry_data); =20 /* If entry_handler returns !0, nmissed is not counted. */ if (rh) { @@ -182,6 +182,7 @@ static void fprobe_init(struct fprobe *fp) fp->ops.func =3D fprobe_kprobe_handler; else fp->ops.func =3D fprobe_handler; + fp->ops.flags |=3D FTRACE_OPS_FL_SAVE_REGS; } =20 diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index a079abd8955b..f73b78187389 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -339,12 +339,16 @@ NOKPROBE_SYMBOL(fexit_perf_func); #endif /* CONFIG_PERF_EVENTS */ =20 static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); + struct pt_regs *regs =3D ftrace_get_regs(fregs); int ret =3D 0; =20 + if (!regs) + return 0; + if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) fentry_trace_func(tf, entry_ip, regs); #ifdef CONFIG_PERF_EVENTS diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c index 24de0e5ff859..ff607babba18 100644 --- a/lib/test_fprobe.c +++ b/lib/test_fprobe.c @@ -40,7 +40,7 @@ static noinline u32 fprobe_selftest_nest_target(u32 value= , u32 (*nest)(u32)) =20 static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, - struct pt_regs *regs, void *data) + struct ftrace_regs *fregs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); /* This can be called on the fprobe_selftest_target and the fprobe_selfte= st_target2 */ @@ -81,7 +81,7 @@ static notrace void fp_exit_handler(struct fprobe *fp, un= signed long ip, =20 static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, - struct pt_regs *regs, void *data) + struct ftrace_regs *fregs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); return 0; diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_exampl= e.c index 0a50b05add96..c234afae52d6 100644 --- a/samples/fprobe/fprobe_example.c +++ b/samples/fprobe/fprobe_example.c @@ -50,7 +50,7 @@ static void show_backtrace(void) =20 static int sample_entry_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, - struct pt_regs *regs, void *data) + struct ftrace_regs *fregs, void *data) { if (use_trace) /* From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA7A53E49D; Sun, 10 Nov 2024 15:49:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253793; cv=none; b=jq5sYMD6CaRZMDfCM+kCqcdMUNzvejeg9V+oHz/Cv0qSfcMXXqcnZBzeUEJyzbXmWUaqUeHXfiUo3xVPYqGjMhgheBN7tlMSPM4BleYYpqBxo9Hjiwgue5XxDU5xV+WC7uZTTKMNo9C5Sjm4jKb9Pl1yn6hx/AFVO8VIZ4zd3iM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253793; c=relaxed/simple; bh=bUZ71nSp1lAbwu1vjYiCdSgDlI1ghUdUB1XefNIsAfc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uPZw6Er79RMeLtF2kmaSrqJbRVtJlP6hz5PoDbwp4q2t2OLjkx0UWN1Vg4mWtZ9dAKIv1ZxuounLFf7Cezi37UYv/Sozj7B/SQ9o3AnkbtEorEXTgV97PRJOuCQwPjJhI7+uMAzc6uOHffGFJSLAyVNlMsANUjsfI7ULj1ZVaak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T4reWUcB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="T4reWUcB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC1BDC4CECD; Sun, 10 Nov 2024 15:49:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253792; bh=bUZ71nSp1lAbwu1vjYiCdSgDlI1ghUdUB1XefNIsAfc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4reWUcBSLgMoH0I2hcbSm1AWwuJFi87ssM+6Fko71bXRSI1UvSesVjh4HY4yHPI5 b379DvUD0HLw0VOdeHoWz5mzLtp6K/YKLHpbTHTVDZDfE3TINTy6P2B/tJ+yEZPYb+ O6Xmb10RtOBerKMdMKgpp5DtixaiV3SiZO68bcXMAc/MGP7AOtElf1hcGeDlxfGmjF qPgPRfVvTC9GVZk3u9BpvwMM9Ki3R0BOmy1pNXwTXbeHkSdxOspQlLkf7S8XNEpM/J pe3XJy56x/ywxCE7u6Bb7kL+TpORg6ti9DNzTxEiDTzDNKmJqlqwM5p1P03OMqRyjE Dkwy9YL54jbdA== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Huacai Chen , WANG Xuerui , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Mathieu Desnoyers , Song Liu , KP Singh , Matt Bobrowski , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Andrew Morton Subject: [PATCH v19 05/19] fprobe: Use ftrace_regs in fprobe exit handler Date: Mon, 11 Nov 2024 00:49:42 +0900 Message-ID: <173125378270.172790.5407978814601760638.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) 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) Cc: Huacai Chen Cc: WANG Xuerui Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Steven Rostedt Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Song Liu Cc: Jiri Olsa Cc: KP Singh Cc: Matt Bobrowski Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Eduard Zingerman Cc: Yonghong Song Cc: John Fastabend Cc: Stanislav Fomichev Cc: Hao Luo Cc: Andrew Morton Acked-by: Heiko Carstens # s390 --- Changes in v16: - Rename HAVE_PT_REGS_TO_FTRACE_REGS_CAST to HAVE_FTRACE_REGS_HAVING_PT_REGS. Changes in v3: - Use ftrace_regs_get_return_value() Changes from previous series: NOTHING, just forward ported. --- arch/loongarch/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + include/linux/fprobe.h | 2 +- include/linux/ftrace.h | 6 ++++++ kernel/trace/Kconfig | 7 +++++++ kernel/trace/bpf_trace.c | 6 +++++- kernel/trace/fprobe.c | 3 ++- kernel/trace/trace_fprobe.c | 6 +++++- lib/test_fprobe.c | 6 +++--- samples/fprobe/fprobe_example.c | 2 +- 11 files changed, 33 insertions(+), 8 deletions(-) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index 73466c9947f6..2b0dc9830ec1 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -126,6 +126,7 @@ config LOONGARCH select HAVE_DMA_CONTIGUOUS select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_ARGS + select HAVE_FTRACE_REGS_HAVING_PT_REGS select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_DYNAMIC_FTRACE_WITH_REGS select HAVE_EBPF_JIT diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 112e83601ed5..eeb95342ddeb 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -175,6 +175,7 @@ config S390 select HAVE_DMA_CONTIGUOUS select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_ARGS + select HAVE_FTRACE_REGS_HAVING_PT_REGS select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_DYNAMIC_FTRACE_WITH_REGS select HAVE_EBPF_JIT if HAVE_MARCH_Z196_FEATURES diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2697ebbfce8d..22cb0d181552 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -222,6 +222,7 @@ config X86 select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_REGS select HAVE_DYNAMIC_FTRACE_WITH_ARGS if X86_64 + select HAVE_FTRACE_REGS_HAVING_PT_REGS if X86_64 select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_SAMPLE_FTRACE_DIRECT if X86_64 select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if X86_64 diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index ca64ee5e45d2..ef609bcca0f9 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -14,7 +14,7 @@ typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigne= d long entry_ip, void *entry_data); =20 typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *regs, void *entry_data); =20 /** diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 9a1e768e47da..bf8bb6c10553 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -176,6 +176,12 @@ static inline struct pt_regs *arch_ftrace_get_regs(str= uct ftrace_regs *fregs) #define ftrace_regs_set_instruction_pointer(fregs, ip) do { } while (0) #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ =20 +#ifdef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS + +static_assert(sizeof(struct pt_regs) =3D=3D ftrace_regs_size()); + +#endif /* CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */ + static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs = *fregs) { if (!fregs) diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index f10ca86fbfad..7f8165f2049a 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -57,6 +57,12 @@ config HAVE_DYNAMIC_FTRACE_WITH_ARGS This allows for use of ftrace_regs_get_argument() and ftrace_regs_get_stack_pointer(). =20 +config HAVE_FTRACE_REGS_HAVING_PT_REGS + bool + help + If this is set, ftrace_regs has pt_regs, thus it can convert to + pt_regs without allocating memory. + config HAVE_DYNAMIC_FTRACE_NO_PATCHABLE bool help @@ -298,6 +304,7 @@ config FPROBE bool "Kernel Function Probe (fprobe)" depends on FUNCTION_TRACER depends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS + depends on HAVE_FTRACE_REGS_HAVING_PT_REGS || !HAVE_DYNAMIC_FTRACE_WITH_A= RGS depends on HAVE_RETHOOK select RETHOOK default n diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 09fb6ce92144..e7ea5568471c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2786,10 +2786,14 @@ kprobe_multi_link_handler(struct fprobe *fp, unsign= ed long fentry_ip, =20 static void kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *data) { struct bpf_kprobe_multi_link *link; + struct pt_regs *regs =3D ftrace_get_regs(fregs); + + if (!regs) + return; =20 link =3D container_of(fp, struct bpf_kprobe_multi_link, fp); kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, dat= a); diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 3d3789283873..90a3c8e2bbdf 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -124,6 +124,7 @@ static void fprobe_exit_handler(struct rethook_node *rh= , void *data, { struct fprobe *fp =3D (struct fprobe *)data; struct fprobe_rethook_node *fpr; + struct ftrace_regs *fregs =3D (struct ftrace_regs *)regs; int bit; =20 if (!fp || fprobe_disabled(fp)) @@ -141,7 +142,7 @@ static void fprobe_exit_handler(struct rethook_node *rh= , void *data, return; } =20 - fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs, + fp->exit_handler(fp, fpr->entry_ip, ret_ip, fregs, fp->entry_data_size ? (void *)fpr->data : NULL); ftrace_test_recursion_unlock(bit); } diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index f73b78187389..b68d0aa26097 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -360,10 +360,14 @@ static int fentry_dispatcher(struct fprobe *fp, unsig= ned long entry_ip, NOKPROBE_SYMBOL(fentry_dispatcher); =20 static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); + struct pt_regs *regs =3D ftrace_get_regs(fregs); + + if (!regs) + return; =20 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data); diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c index ff607babba18..271ce0caeec0 100644 --- a/lib/test_fprobe.c +++ b/lib/test_fprobe.c @@ -59,9 +59,9 @@ static notrace int fp_entry_handler(struct fprobe *fp, un= signed long ip, =20 static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, - struct pt_regs *regs, void *data) + struct ftrace_regs *fregs, void *data) { - unsigned long ret =3D regs_return_value(regs); + unsigned long ret =3D ftrace_regs_get_return_value(fregs); =20 KUNIT_EXPECT_FALSE(current_test, preemptible()); if (ip !=3D target_ip) { @@ -89,7 +89,7 @@ static notrace int nest_entry_handler(struct fprobe *fp, = unsigned long ip, =20 static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, - struct pt_regs *regs, void *data) + struct ftrace_regs *fregs, void *data) { KUNIT_EXPECT_FALSE(current_test, preemptible()); KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip); diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_exampl= e.c index c234afae52d6..bfe98ce826f3 100644 --- a/samples/fprobe/fprobe_example.c +++ b/samples/fprobe/fprobe_example.c @@ -67,7 +67,7 @@ static int sample_entry_handler(struct fprobe *fp, unsign= ed long ip, } =20 static void sample_exit_handler(struct fprobe *fp, unsigned long ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *regs, void *data) { unsigned long rip =3D ret_ip; From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58E5984E1C; Sun, 10 Nov 2024 15:50:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253804; cv=none; b=ay8TrA3s9Xw6kSErJZz9URidyMjy4kBJx2wwT1ZHBbp0B3vWI57CFQZ9fVIh0JsQ+67khB6mk9sJ5T05VjhVkoT+KaqgNVwjs8CLxrMhC2z8SwFcX686iCrXB5G0bWzuLR0z093XZsPbVjohd8nMg7jtlwCk9kErZgwQH01PUKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253804; c=relaxed/simple; bh=VwgU/FJ/MOK68HNLI5n7pBBrw9lEjFt14etIqRD5i6k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=S6F8vb2r6H9JFc2gaIL/jS6Jh3WgDoQLdWjxiw84xZCLf1dO+cVtVP6pAsYozCEVKCIR4h+9xS3/A5vjbxAcM/GLuSzutDvcfbFTxDudWTBaThLAfhC8l774t/wWsY8e0l+BycW2800xdmt9GTtxxuAkbOXcQjvTSyk0756MiBs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q9/IzJ8w; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q9/IzJ8w" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20085C4CECD; Sun, 10 Nov 2024 15:49:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253804; bh=VwgU/FJ/MOK68HNLI5n7pBBrw9lEjFt14etIqRD5i6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q9/IzJ8wdps9bpHwjQ+zxPgXJa5XixDTXnG4KfzruHMahWL0SvESQzPzau2HhKJ+m bVzwGHB+dItk2MDt/0SlXOesciaZeA4wyNcB/pIWu6SWIAAAn6qDg5QtQ72/95NEth YjX2QB57/7Q/Oy3dv/4Q7nFO0sigULNp3kvqySrHCkooWvO5SddFTa+3JDfCVhgYIC HsjSxF2hoHnfVkWwHBTlAzDy6MrtIbLGtQH/iKflYeX+g+rWIAw4YipXjJvgyoCxsH XLTd1lSWdAPrPyQOqlMj2qa+OHc88VyWFuQTEDpOmCcu7iDkRGy7L8sQsVN52b5edk KQxQpuD0boz6A== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou Subject: [PATCH v19 06/19] tracing: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs Date: Mon, 11 Nov 2024 00:49:57 +0900 Message-ID: <173125379786.172790.567610414442463419.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Add ftrace_partial_regs() which converts the ftrace_regs to pt_regs. This is for the eBPF which needs this to keep the same pt_regs interface to access registers. Thus when replacing the pt_regs with ftrace_regs in fprobes (which is used by kprobe_multi eBPF event), this will be used. If the architecture defines its own ftrace_regs, this copies partial registers to pt_regs and returns it. If not, ftrace_regs is the same as pt_regs and ftrace_partial_regs() will return ftrace_regs::regs. Signed-off-by: Masami Hiramatsu (Google) Acked-by: Florent Revest Cc: Steven Rostedt Cc: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou --- Changes in v18: - Fix to use sizeof() for calculating array size. Changes in v14: - Add riscv change. Changes in v8: - Add the reason why this required in changelog. Changes from previous series: NOTHING, just forward ported. --- arch/arm64/include/asm/ftrace.h | 13 +++++++++++++ arch/riscv/include/asm/ftrace.h | 14 ++++++++++++++ include/linux/ftrace.h | 17 +++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index b5fa57b61378..09210f853f12 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -135,6 +135,19 @@ ftrace_regs_get_frame_pointer(const struct ftrace_regs= *fregs) return arch_ftrace_regs(fregs)->fp; } =20 +static __always_inline struct pt_regs * +ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) +{ + struct __arch_ftrace_regs *afregs =3D arch_ftrace_regs(fregs); + + memcpy(regs->regs, afregs->regs, sizeof(afregs->regs)); + regs->sp =3D afregs->sp; + regs->pc =3D afregs->pc; + regs->regs[29] =3D afregs->fp; + regs->regs[30] =3D afregs->lr; + return regs; +} + int ftrace_regs_query_register_offset(const char *name); =20 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrac= e.h index 9372f8d7036f..7064a530794b 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -197,6 +197,20 @@ static __always_inline void ftrace_override_function_w= ith_return(struct ftrace_r arch_ftrace_regs(fregs)->epc =3D arch_ftrace_regs(fregs)->ra; } =20 +static __always_inline struct pt_regs * +ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) +{ + struct __arch_ftrace_regs *afregs =3D arch_ftrace_regs(fregs); + + memcpy(®s->a0, afregs->args, sizeof(afregs->args)); + regs->epc =3D afregs->epc; + regs->ra =3D afregs->ra; + regs->sp =3D afregs->sp; + regs->s0 =3D afregs->s0; + regs->t1 =3D afregs->t1; + return regs; +} + int ftrace_regs_query_register_offset(const char *name); =20 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index bf8bb6c10553..ad2b46e1d5b0 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -190,6 +190,23 @@ static __always_inline struct pt_regs *ftrace_get_regs= (struct ftrace_regs *fregs return arch_ftrace_get_regs(fregs); } =20 +#if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) || \ + defined(CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS) + +static __always_inline struct pt_regs * +ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + /* + * If CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS=3Dy, ftrace_regs memory + * layout is including pt_regs. So always returns that address. + * Since arch_ftrace_get_regs() will check some members and may return + * NULL, we can not use it. + */ + return &arch_ftrace_regs(fregs)->regs; +} + +#endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_FTRACE_REGS= _HAVING_PT_REGS */ + /* * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs. * Note: this can be true even when ftrace_get_regs() cannot provide a pt_= regs. From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A2951369AE; Sun, 10 Nov 2024 15:50:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253818; cv=none; b=dkSqnAPIeGB7yYRgRM5TVQX0MBWbtDE9hAOSNu1s8wzVuU5eEXE0379/DgFpHyGXDNKfdQidNNs5USDTGSsBKlkRR2p3ZH5HzDVb7etwD4zBEPv4lc6j0BK4n4wpMOis58qLbMciBDPu9IGUoSDrVcyDuKOIwhET9dyk30xoCM4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253818; c=relaxed/simple; bh=O6aPdlc3GU/IJJYpZi5XgJjtV62OSvEqbzGak40xdP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Wh2TGOXtG3J/E2Dgmyz52TmuAZ9CejpOTHcU/pk2G8Yzv7Pqr9XP6ZXUp2TskEnFyxV4rpGMAxHXJOCYzs4tvWIh1SSipd1RJrD6yqAv05kvPjLF0tWFjLZQBy0WmQb+mT7JYlmZKy9j0O4j5ltdEFZ3Ax47Kn0PpstKwulWFEY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Uu80gdyH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uu80gdyH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C46D6C4CECD; Sun, 10 Nov 2024 15:50:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253818; bh=O6aPdlc3GU/IJJYpZi5XgJjtV62OSvEqbzGak40xdP0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Uu80gdyHsgGvNkugiPO/4FyI9FebDJAOt7jdcGYMMZEAtEjaNkQMRuNKr3BV244fJ 1DsnzghU0iYnb38xr4a7SNtbH7IpUVY3DTyxgPF6KeCFr5uDMLBuQU9maZF1HQCg3K 37jEt+9DZBN/u8OccrETzIKQzinwhkeEbf5ijluvYGyWLGpfWwhle6Y9TUVFdptxFw ojemhVrnpSJGKEHtCizbQuzwQOs6vqSupCGA+BLI6JeTLTKXP+zoK4uESkEEKfNvY1 qEFF2MNSFBN1kJ+lrVUS+di80t1/+N45qg+VNbjc11SVVpjf5hnX++NrZOFFoFTDU2 4WJRpeG9f3Reg== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Will Deacon , Catalin Marinas , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH v19 07/19] tracing: Add ftrace_fill_perf_regs() for perf event Date: Mon, 11 Nov 2024 00:50:09 +0900 Message-ID: <173125380933.172790.13871919598133716103.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Add ftrace_fill_perf_regs() which should be compatible with the perf_fetch_caller_regs(). In other words, the pt_regs returned from the ftrace_fill_perf_regs() must satisfy 'user_mode(regs) =3D=3D false' and can= be used for stack tracing. Signed-off-by: Masami Hiramatsu (Google) Acked-by: Will Deacon Cc: Steven Rostedt Cc: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Naveen N Rao Cc: Madhavan Srinivasan Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Acked-by: Heiko Carstens # s390 --- Changes in v16: - Fix s390 to clear psw.mask according to Heiko's suggestion. --- arch/arm64/include/asm/ftrace.h | 7 +++++++ arch/powerpc/include/asm/ftrace.h | 7 +++++++ arch/s390/include/asm/ftrace.h | 6 ++++++ arch/x86/include/asm/ftrace.h | 7 +++++++ include/linux/ftrace.h | 31 +++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index 09210f853f12..10e56522122a 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -148,6 +148,13 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, s= truct pt_regs *regs) return regs; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->pc =3D arch_ftrace_regs(fregs)->pc; \ + (_regs)->regs[29] =3D arch_ftrace_regs(fregs)->fp; \ + (_regs)->sp =3D arch_ftrace_regs(fregs)->sp; \ + (_regs)->pstate =3D PSR_MODE_EL1h; \ + } while (0) + int ftrace_regs_query_register_offset(const char *name); =20 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/f= trace.h index 0edfb874eb02..407ce6eccc04 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -40,6 +40,13 @@ static __always_inline struct pt_regs *arch_ftrace_get_r= egs(struct ftrace_regs * return arch_ftrace_regs(fregs)->regs.msr ? &arch_ftrace_regs(fregs)->regs= : NULL; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->result =3D 0; \ + (_regs)->nip =3D arch_ftrace_regs(fregs)->regs.nip; \ + (_regs)->gpr[1] =3D arch_ftrace_regs(fregs)->regs.gpr[1]; \ + asm volatile("mfmsr %0" : "=3Dr" ((_regs)->msr)); \ + } while (0) + static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip) diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index 5c94c1fc1bc1..5b7cb49c41ee 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -76,6 +76,12 @@ ftrace_regs_get_frame_pointer(struct ftrace_regs *fregs) return ftrace_regs_get_stack_pointer(fregs); } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->psw.mask =3D 0; \ + (_regs)->psw.addr =3D arch_ftrace_regs(fregs)->regs.psw.addr; \ + (_regs)->gprs[15] =3D arch_ftrace_regs(fregs)->regs.gprs[15]; \ + } while (0) + #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS /* * When an ftrace registered caller is tracing a function that is diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index d61407c680c2..7e06f8c7937a 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -47,6 +47,13 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs) return &arch_ftrace_regs(fregs)->regs; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->ip =3D arch_ftrace_regs(fregs)->regs.ip; \ + (_regs)->sp =3D arch_ftrace_regs(fregs)->regs.sp; \ + (_regs)->cs =3D __KERNEL_CS; \ + (_regs)->flags =3D 0; \ + } while (0) + #define ftrace_regs_set_instruction_pointer(fregs, _ip) \ do { arch_ftrace_regs(fregs)->regs.ip =3D (_ip); } while (0) =20 diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index ad2b46e1d5b0..6d29c640697c 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -207,6 +207,37 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct = pt_regs *regs) =20 #endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_FTRACE_REGS= _HAVING_PT_REGS */ =20 +#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS + +/* + * Please define arch dependent pt_regs which compatible to the + * perf_arch_fetch_caller_regs() but based on ftrace_regs. + * This requires + * - user_mode(_regs) returns false (always kernel mode). + * - able to use the _regs for stack trace. + */ +#ifndef arch_ftrace_fill_perf_regs +/* As same as perf_arch_fetch_caller_regs(), do nothing by default */ +#define arch_ftrace_fill_perf_regs(fregs, _regs) do {} while (0) +#endif + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + arch_ftrace_fill_perf_regs(fregs, regs); + return regs; +} + +#else /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + return &arch_ftrace_regs(fregs)->regs; +} + +#endif + /* * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs. * Note: this can be true even when ftrace_get_regs() cannot provide a pt_= regs. From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BB4584D29; Sun, 10 Nov 2024 15:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253829; cv=none; b=c5E91HgGe9G5SCK64ibXMjm1QFOhCemjUMO44y1gWAwW4xznljLhygMTkdIl7R88oiyeLkPr0Tp8MrKiWAhU3wZZDP9mPR1KBXjtCaKY4eqRorOLcZLGpffBQ6YnfWGY2MUOhgpgPUj4MkWXqbGwTv4L0FSTk+A0uKRJCQy84Rk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253829; c=relaxed/simple; bh=XOmB0ULOHupz4kFjwpmxxMW5DZpDImZiDmErO/2b6yk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DBMNoCzeRE2MDn65Ly9lP0t6oipB9PlehM3ip2Mab3BHRp2h3Q/2Wa/c1BCCVtNkOPGQGqVCAZZ9gL+Pr/vWMRjYrvWjZam4jrJClYhZfRsb4JACKoOHuFeJlAYXWG6HJXKvlPvTbrnIRwBUDJ46Nk4vvhUzTnCcoX92P5Nz3tw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T3s0Su9A; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="T3s0Su9A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4A7CC4CECD; Sun, 10 Nov 2024 15:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253828; bh=XOmB0ULOHupz4kFjwpmxxMW5DZpDImZiDmErO/2b6yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T3s0Su9AcrW+LllZNCjEP4hBnsqGyJPq1eiMYMywsAvjn9OSGEhpaT24K/MIx+8cV iOR8jatFtDQQZSt2eH+I5FnDuBUb7+7h7uO4mpH6Hnw/+ooK4RYDI+Gdpa8QzCzgge inPOTIJJiFlQ4ZtOqYBewgfp1eY/1vV8CZBmlhh40X7ahpQwYM1FuCTQUnOubMGRBz tT5ZmWWRAcvmm0Jc0+ptmJQLN4+Wzh2+4+aNla4c+JhlIRpQy0rbj39bM4S4VPlVul U8pE2Qcb9hzd4N/gDHeB60PcODD7hGpxU3yeuZH+CCVVfxUojES4BcG7Dm7OMw5pHq qApazzqt48pVA== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 08/19] tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS Date: Mon, 11 Nov 2024 00:50:23 +0900 Message-ID: <173125382335.172790.9364683419794043855.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Allow fprobe events to be enabled with CONFIG_DYNAMIC_FTRACE_WITH_ARGS. With this change, fprobe events mostly use ftrace_regs instead of pt_regs. Note that if the arch doesn't enable HAVE_FTRACE_REGS_HAVING_PT_REGS, fprobe events will not be able to be used from perf. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v9: - Copy store_trace_entry_data() as store_fprobe_entry_data() for fprobe. Chagnes in v3: - Use ftrace_regs_get_return_value(). Changes in v2: - Define ftrace_regs_get_kernel_stack_nth() for !CONFIG_HAVE_REGS_AND_STACK_ACCESS_API. Changes from previous series: Update against the new series. --- include/linux/ftrace.h | 17 ++++++ kernel/trace/Kconfig | 1=20 kernel/trace/trace_fprobe.c | 107 +++++++++++++++++++++++++----------= ---- kernel/trace/trace_probe_tmpl.h | 2 - 4 files changed, 86 insertions(+), 41 deletions(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 6d29c640697c..4c553fe9c026 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -250,6 +250,23 @@ static __always_inline bool ftrace_regs_has_args(struc= t ftrace_regs *fregs) return ftrace_get_regs(fregs) !=3D NULL; } =20 +#ifdef CONFIG_HAVE_REGS_AND_STACK_ACCESS_API +static __always_inline unsigned long +ftrace_regs_get_kernel_stack_nth(struct ftrace_regs *fregs, unsigned int n= th) +{ + unsigned long *stackp; + + stackp =3D (unsigned long *)ftrace_regs_get_stack_pointer(fregs); + if (((unsigned long)(stackp + nth) & ~(THREAD_SIZE - 1)) =3D=3D + ((unsigned long)stackp & ~(THREAD_SIZE - 1))) + return *(stackp + nth); + + return 0; +} +#else /* !CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */ +#define ftrace_regs_get_kernel_stack_nth(fregs, nth) (0L) +#endif /* CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */ + typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs); =20 diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 7f8165f2049a..82654bbfad9a 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -689,7 +689,6 @@ config FPROBE_EVENTS select TRACING select PROBE_EVENTS select DYNAMIC_EVENTS - depends on DYNAMIC_FTRACE_WITH_REGS default y help This allows user to add tracing events on the function entry and diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index b68d0aa26097..41595e38fea5 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -134,7 +134,7 @@ static int process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, void *dest, void *base) { - struct pt_regs *regs =3D rec; + struct ftrace_regs *fregs =3D rec; unsigned long val; int ret; =20 @@ -142,17 +142,17 @@ process_fetch_insn(struct fetch_insn *code, void *rec= , void *edata, /* 1st stage: get value from context */ switch (code->op) { case FETCH_OP_STACK: - val =3D regs_get_kernel_stack_nth(regs, code->param); + val =3D ftrace_regs_get_kernel_stack_nth(fregs, code->param); break; case FETCH_OP_STACKP: - val =3D kernel_stack_pointer(regs); + val =3D ftrace_regs_get_stack_pointer(fregs); break; case FETCH_OP_RETVAL: - val =3D regs_return_value(regs); + val =3D ftrace_regs_get_return_value(fregs); break; #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API case FETCH_OP_ARG: - val =3D regs_get_kernel_argument(regs, code->param); + val =3D ftrace_regs_get_argument(fregs, code->param); break; case FETCH_OP_EDATA: val =3D *(unsigned long *)((unsigned long)edata + code->offset); @@ -175,7 +175,7 @@ NOKPROBE_SYMBOL(process_fetch_insn) /* function entry handler */ static nokprobe_inline void __fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - struct pt_regs *regs, + struct ftrace_regs *fregs, struct trace_event_file *trace_file) { struct fentry_trace_entry_head *entry; @@ -189,41 +189,71 @@ __fentry_trace_func(struct trace_fprobe *tf, unsigned= long entry_ip, if (trace_trigger_soft_disabled(trace_file)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, NULL); + dsize =3D __get_data_size(&tf->tp, fregs, NULL); =20 entry =3D trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry) + tf->tp.size + dsize); if (!entry) return; =20 - fbuffer.regs =3D regs; + fbuffer.regs =3D ftrace_get_regs(fregs); entry =3D fbuffer.entry =3D ring_buffer_event_data(fbuffer.event); entry->ip =3D entry_ip; - store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); + store_trace_args(&entry[1], &tf->tp, fregs, NULL, sizeof(*entry), dsize); =20 trace_event_buffer_commit(&fbuffer); } =20 static void fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - struct pt_regs *regs) + struct ftrace_regs *fregs) { struct event_file_link *link; =20 trace_probe_for_each_link_rcu(link, &tf->tp) - __fentry_trace_func(tf, entry_ip, regs, link->file); + __fentry_trace_func(tf, entry_ip, fregs, link->file); } NOKPROBE_SYMBOL(fentry_trace_func); =20 +static nokprobe_inline +void store_fprobe_entry_data(void *edata, struct trace_probe *tp, struct f= trace_regs *fregs) +{ + struct probe_entry_arg *earg =3D tp->entry_arg; + unsigned long val =3D 0; + int i; + + if (!earg) + return; + + for (i =3D 0; i < earg->size; i++) { + struct fetch_insn *code =3D &earg->code[i]; + + switch (code->op) { + case FETCH_OP_ARG: + val =3D ftrace_regs_get_argument(fregs, code->param); + break; + case FETCH_OP_ST_EDATA: + *(unsigned long *)((unsigned long)edata + code->offset) =3D val; + break; + case FETCH_OP_END: + goto end; + default: + break; + } + } +end: + return; +} + /* function exit handler */ static int trace_fprobe_entry_handler(struct fprobe *fp, unsigned long ent= ry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); =20 if (tf->tp.entry_arg) - store_trace_entry_data(entry_data, &tf->tp, regs); + store_fprobe_entry_data(entry_data, &tf->tp, fregs); =20 return 0; } @@ -231,7 +261,7 @@ NOKPROBE_SYMBOL(trace_fprobe_entry_handler) =20 static nokprobe_inline void __fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data, struct trace_event_file *trace_file) { struct fexit_trace_entry_head *entry; @@ -245,60 +275,63 @@ __fexit_trace_func(struct trace_fprobe *tf, unsigned = long entry_ip, if (trace_trigger_soft_disabled(trace_file)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, entry_data); + dsize =3D __get_data_size(&tf->tp, fregs, entry_data); =20 entry =3D trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry) + tf->tp.size + dsize); if (!entry) return; =20 - fbuffer.regs =3D regs; + fbuffer.regs =3D ftrace_get_regs(fregs); entry =3D fbuffer.entry =3D ring_buffer_event_data(fbuffer.event); entry->func =3D entry_ip; entry->ret_ip =3D ret_ip; - store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), ds= ize); + store_trace_args(&entry[1], &tf->tp, fregs, entry_data, sizeof(*entry), d= size); =20 trace_event_buffer_commit(&fbuffer); } =20 static void fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, void *entry_data) + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct event_file_link *link; =20 trace_probe_for_each_link_rcu(link, &tf->tp) - __fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data, link->file); + __fexit_trace_func(tf, entry_ip, ret_ip, fregs, entry_data, link->file); } NOKPROBE_SYMBOL(fexit_trace_func); =20 #ifdef CONFIG_PERF_EVENTS =20 static int fentry_perf_func(struct trace_fprobe *tf, unsigned long entry_i= p, - struct pt_regs *regs) + struct ftrace_regs *fregs) { struct trace_event_call *call =3D trace_probe_event_call(&tf->tp); struct fentry_trace_entry_head *entry; struct hlist_head *head; int size, __size, dsize; + struct pt_regs *regs; int rctx; =20 head =3D this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return 0; =20 - dsize =3D __get_data_size(&tf->tp, regs, NULL); + dsize =3D __get_data_size(&tf->tp, fregs, NULL); __size =3D sizeof(*entry) + tf->tp.size + dsize; size =3D ALIGN(__size + sizeof(u32), sizeof(u64)); size -=3D sizeof(u32); =20 - entry =3D perf_trace_buf_alloc(size, NULL, &rctx); + entry =3D perf_trace_buf_alloc(size, ®s, &rctx); if (!entry) return 0; =20 + regs =3D ftrace_fill_perf_regs(fregs, regs); + entry->ip =3D entry_ip; memset(&entry[1], 0, dsize); - store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); + store_trace_args(&entry[1], &tf->tp, fregs, NULL, sizeof(*entry), dsize); perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, head, NULL); return 0; @@ -307,31 +340,34 @@ NOKPROBE_SYMBOL(fentry_perf_func); =20 static void fexit_perf_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_event_call *call =3D trace_probe_event_call(&tf->tp); struct fexit_trace_entry_head *entry; struct hlist_head *head; int size, __size, dsize; + struct pt_regs *regs; int rctx; =20 head =3D this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, entry_data); + dsize =3D __get_data_size(&tf->tp, fregs, entry_data); __size =3D sizeof(*entry) + tf->tp.size + dsize; size =3D ALIGN(__size + sizeof(u32), sizeof(u64)); size -=3D sizeof(u32); =20 - entry =3D perf_trace_buf_alloc(size, NULL, &rctx); + entry =3D perf_trace_buf_alloc(size, ®s, &rctx); if (!entry) return; =20 + regs =3D ftrace_fill_perf_regs(fregs, regs); + entry->func =3D entry_ip; entry->ret_ip =3D ret_ip; - store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), ds= ize); + store_trace_args(&entry[1], &tf->tp, fregs, entry_data, sizeof(*entry), d= size); perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, head, NULL); } @@ -343,17 +379,14 @@ static int fentry_dispatcher(struct fprobe *fp, unsig= ned long entry_ip, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); - struct pt_regs *regs =3D ftrace_get_regs(fregs); int ret =3D 0; =20 - if (!regs) - return 0; - if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) - fentry_trace_func(tf, entry_ip, regs); + fentry_trace_func(tf, entry_ip, fregs); + #ifdef CONFIG_PERF_EVENTS if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) - ret =3D fentry_perf_func(tf, entry_ip, regs); + ret =3D fentry_perf_func(tf, entry_ip, fregs); #endif return ret; } @@ -364,16 +397,12 @@ static void fexit_dispatcher(struct fprobe *fp, unsig= ned long entry_ip, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); - struct pt_regs *regs =3D ftrace_get_regs(fregs); - - if (!regs) - return; =20 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) - fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data); + fexit_trace_func(tf, entry_ip, ret_ip, fregs, entry_data); #ifdef CONFIG_PERF_EVENTS if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) - fexit_perf_func(tf, entry_ip, ret_ip, regs, entry_data); + fexit_perf_func(tf, entry_ip, ret_ip, fregs, entry_data); #endif } NOKPROBE_SYMBOL(fexit_dispatcher); diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmp= l.h index 2caf0d2afb32..f39b37fcdb3b 100644 --- a/kernel/trace/trace_probe_tmpl.h +++ b/kernel/trace/trace_probe_tmpl.h @@ -232,7 +232,7 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsi= gned long val, =20 /* Sum up total data length for dynamic arrays (strings) */ static nokprobe_inline int -__get_data_size(struct trace_probe *tp, struct pt_regs *regs, void *edata) +__get_data_size(struct trace_probe *tp, void *regs, void *edata) { struct probe_arg *arg; int i, len, ret =3D 0; From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8162481741; Sun, 10 Nov 2024 15:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253839; cv=none; b=r1sd3UU5lFwiF8pznHguxIrIm9Iq/KMpKvjrScDtgq5vF0eZvy6+EZh7evrkILdZQQAB593nOG/FjB0J1NCGqWelgBzoRj/Ubdb/eHdepUH56HMxQ+zxuiIvTS22XgKewcq36M27G+fSFlRvMvqVVC6akQ5S9Ry8XaPyS8I+K8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253839; c=relaxed/simple; bh=nBiONpEv/wdcj9ivHPwXYq4ae4MTgGFPim3vv0QzGS4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s6rAtvTZ1/e3vMuDQvzMchFDiVGNWGPiRBt4LSVio36VHC7PmQchBvJvnePIq4YnBxHEuywr+BPfyoWNo0csYgzdoSqKssBJl+dMf1RCIxzo03M+LxqLQQWpzCEUPPoVkRA0llZXYtym2q9hwRnhd8eYPrsImjFGxM+VhbEsjEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Oujixnpx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Oujixnpx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 440B9C4CECD; Sun, 10 Nov 2024 15:50:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253839; bh=nBiONpEv/wdcj9ivHPwXYq4ae4MTgGFPim3vv0QzGS4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OujixnpxUSkor8NS90Jei5XtCTIgh1P4cfquF6l7o1NsQDr+5xFCA8uX0d4itRpaQ y+bV8ko1DdDfAi+10jXH9jiK/6PIowZVOS1yf+I+/VVvCOKeujxjgAYREfuA8/bCh4 IHHeBvskGbeb2X6XLEraWrUjYBfBKZxxukdEk9Z8Zu3FdSyMt0gyTFpvdIAZVkMa1Y +BQujoq0zzRDuY3C78+APOOescTKj+Tb5dIKNpGcWnBi5+pffUbnvCEAEWe0khaiWn Rx7AFLfTWD3h+LkHb1XyE7lfriQN7J23LQzKPj2ceyVnZ8IgoIZG9GM4FeN5NpPozl XxNizzu+xLBdw== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 09/19] bpf: Enable kprobe_multi feature if CONFIG_FPROBE is enabled Date: Mon, 11 Nov 2024 00:50:34 +0900 Message-ID: <173125383411.172790.4900050824383325604.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Enable kprobe_multi feature if CONFIG_FPROBE is enabled. The pt_regs is converted from ftrace_regs by ftrace_partial_regs(), thus some registers may always returns 0. But it should be enough for function entry (access arguments) and exit (access return value). Signed-off-by: Masami Hiramatsu (Google) Acked-by: Florent Revest --- Changes in v9: - Avoid wasting memory for bpf_kprobe_multi_pt_regs when CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST=3Dy --- kernel/trace/bpf_trace.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index e7ea5568471c..1532e9172bf9 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2517,7 +2517,7 @@ struct bpf_session_run_ctx { void *data; }; =20 -#if defined(CONFIG_FPROBE) && defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) +#ifdef CONFIG_FPROBE struct bpf_kprobe_multi_link { struct bpf_link link; struct fprobe fp; @@ -2540,6 +2540,13 @@ struct user_syms { char *buf; }; =20 +#ifndef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS +static DEFINE_PER_CPU(struct pt_regs, bpf_kprobe_multi_pt_regs); +#define bpf_kprobe_multi_pt_regs_ptr() this_cpu_ptr(&bpf_kprobe_multi_pt_r= egs) +#else +#define bpf_kprobe_multi_pt_regs_ptr() (NULL) +#endif + static int copy_user_syms(struct user_syms *us, unsigned long __user *usym= s, u32 cnt) { unsigned long __user usymbol; @@ -2734,7 +2741,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_c= tx *ctx) =20 static int kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, - unsigned long entry_ip, struct pt_regs *regs, + unsigned long entry_ip, struct ftrace_regs *fregs, bool is_return, void *data) { struct bpf_kprobe_multi_run_ctx run_ctx =3D { @@ -2746,6 +2753,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_li= nk *link, .entry_ip =3D entry_ip, }; struct bpf_run_ctx *old_run_ctx; + struct pt_regs *regs; int err; =20 if (unlikely(__this_cpu_inc_return(bpf_prog_active) !=3D 1)) { @@ -2756,6 +2764,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_li= nk *link, =20 migrate_disable(); rcu_read_lock(); + regs =3D ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr()); old_run_ctx =3D bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); err =3D bpf_prog_run(link->link.prog, regs); bpf_reset_run_ctx(old_run_ctx); @@ -2772,15 +2781,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsign= ed long fentry_ip, unsigned long ret_ip, struct ftrace_regs *fregs, void *data) { - struct pt_regs *regs =3D ftrace_get_regs(fregs); struct bpf_kprobe_multi_link *link; int err; =20 - if (!regs) - return 0; - link =3D container_of(fp, struct bpf_kprobe_multi_link, fp); - err =3D kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, f= alse, data); + err =3D kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), fregs, = false, data); return is_kprobe_session(link->link.prog) ? err : 0; } =20 @@ -2790,13 +2795,9 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, un= signed long fentry_ip, void *data) { struct bpf_kprobe_multi_link *link; - struct pt_regs *regs =3D ftrace_get_regs(fregs); - - if (!regs) - return; =20 link =3D container_of(fp, struct bpf_kprobe_multi_link, fp); - kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, dat= a); + kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), fregs, true, da= ta); } =20 static int symbols_cmp_r(const void *a, const void *b, const void *priv) @@ -3057,7 +3058,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr= *attr, struct bpf_prog *pr kvfree(cookies); return err; } -#else /* !CONFIG_FPROBE || !CONFIG_DYNAMIC_FTRACE_WITH_REGS */ +#else /* !CONFIG_FPROBE */ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_pr= og *prog) { return -EOPNOTSUPP; From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9CE0381B9; Sun, 10 Nov 2024 15:50:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253854; cv=none; b=okDXmotikIi2+2IyjgtRyD8KJqi7cJEic/2yeyK47QZUoviW2t5lefPG8337XDtqMpPat1aFoRPRF89CRMizb6Npyj0sNj6Rw6yxa4yU+GL9ZHn3PewNIjV0SMNvU+0wu3QozTNnZLK7r2h6RyEaxLWjynTFxiqHxidumE0JBWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253854; c=relaxed/simple; bh=KdYbmsoSUbCNl+CmIGlKxxBZZT6J27cNs7+f9ey+TQk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qg7xZzMRBwotU/aXp3rrv/8LwArprIeu4J4MUheXImWxGlQ5Lu3JOwjsY4HLxyn8cK5/yWuSqNZ0zVLEZiJJ58g0YdE8Hg2SktU9yLSWgLrX45onc/xVsa2wc+n6RfuHEnRVvSJRrudjONHjOwK/svqT5b0pnZscq3HfJzeGBWY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HrCx7DEm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HrCx7DEm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9EDCC4CECD; Sun, 10 Nov 2024 15:50:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253853; bh=KdYbmsoSUbCNl+CmIGlKxxBZZT6J27cNs7+f9ey+TQk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HrCx7DEmMmXfw1XOKyAMHkIfUiwAz8iddyDmDoiAHXxwekimRTUSuc3yiFrcQS09z XlP+6GCw33rHV0uwyfalBxA8GavPzE0ewo1MJ45f7R+LU7zLB3q7J580ok25CKXwQx YetJJKnV3DUm0WJLUX8k2efdgr2bHxTc0wcY9Qd0+ruL45IDA6lj95GBr2Uo3kvnLW IOnxjI9rZOYRg7MqXBUKY8i9J083kikVFLrHQ9rZMExRWo9cdU3+BcFT6IiY40w8eN LHHKfH8gae5LBaf4Eq9DrjSiXn4luUigxgepxk6git0WQegTM8a8jDb3Q5Ntbgc4F0 HlfJIEaZNixXg== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Mathieu Desnoyers Subject: [PATCH v19 10/19] ftrace: Add CONFIG_HAVE_FTRACE_GRAPH_FUNC Date: Mon, 11 Nov 2024 00:50:44 +0900 Message-ID: <173125384445.172790.9187002271644569696.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Add CONFIG_HAVE_FTRACE_GRAPH_FUNC kconfig in addition to ftrace_graph_func macro check. This is for the other feature (e.g. FPROBE) which requires to access ftrace_regs from fgraph_ops::entryfunc() can avoid compiling if the fgraph can not pass the valid ftrace_regs. Signed-off-by: Masami Hiramatsu (Google) Cc: Catalin Marinas Cc: Will Deacon Cc: Huacai Chen Cc: WANG Xuerui Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Naveen N Rao Cc: Madhavan Srinivasan Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Steven Rostedt Cc: Mathieu Desnoyers --- Changes in v8: - Newly added. --- arch/arm64/Kconfig | 1 + arch/loongarch/Kconfig | 1 + arch/powerpc/Kconfig | 1 + arch/riscv/Kconfig | 1 + arch/x86/Kconfig | 1 + kernel/trace/Kconfig | 5 +++++ 6 files changed, 10 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 012bb9dcb631..ef8f2807790d 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -213,6 +213,7 @@ config ARM64 select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_EFFICIENT_UNALIGNED_ACCESS select HAVE_GUP_FAST + select HAVE_FTRACE_GRAPH_FUNC select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_ERROR_INJECTION diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index 2b0dc9830ec1..5e15c8ed7faf 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -133,6 +133,7 @@ config LOONGARCH select HAVE_EFFICIENT_UNALIGNED_ACCESS if !ARCH_STRICT_ALIGN select HAVE_EXIT_THREAD select HAVE_GUP_FAST + select HAVE_FTRACE_GRAPH_FUNC select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_ERROR_INJECTION diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 8094a01974cc..0888aabff071 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -238,6 +238,7 @@ config PPC select HAVE_EBPF_JIT select HAVE_EFFICIENT_UNALIGNED_ACCESS select HAVE_GUP_FAST + select HAVE_FTRACE_GRAPH_FUNC select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_DESCRIPTORS if PPC64_ELF_ABI_V1 diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 8afe0f0436d4..cd3f7d2e55ca 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -142,6 +142,7 @@ config RISCV select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && MMU && (CLANG_SUPPORTS_DYNAM= IC_FTRACE || GCC_SUPPORTS_DYNAMIC_FTRACE) select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_DYNAMIC_FTRACE_WITH_ARGS if HAVE_DYNAMIC_FTRACE + select HAVE_FTRACE_GRAPH_FUNC select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL select HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_GRAPH_FREGS diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 22cb0d181552..b42f9e7f1b87 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -232,6 +232,7 @@ config X86 select HAVE_EXIT_THREAD select HAVE_GUP_FAST select HAVE_FENTRY if X86_64 || DYNAMIC_FTRACE + select HAVE_FTRACE_GRAPH_FUNC if HAVE_FUNCTION_GRAPH_TRACER select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_GRAPH_FREGS if HAVE_FUNCTION_GRAPH_TRACER select HAVE_FUNCTION_GRAPH_TRACER if X86_32 || (X86_64 && DYNAMIC_FTRACE) diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 82654bbfad9a..2fc55a1a88aa 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -34,6 +34,11 @@ config HAVE_FUNCTION_GRAPH_TRACER config HAVE_FUNCTION_GRAPH_FREGS bool =20 +config HAVE_FTRACE_GRAPH_FUNC + bool + help + True if ftrace_graph_func() is defined. + config HAVE_DYNAMIC_FTRACE bool help From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34077381B9; Sun, 10 Nov 2024 15:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253864; cv=none; b=pTNaTUTZp3RorcDUZebgYV9PlxHr8dFMb9eYhclE91KBqwdOL7XRX+t4x+AymNURt35GlZPS7R/Vj0JlhN0nUH2bi8t1jtapGoaKYxYNtboQ0iraCSOIN80Ru57e4xtm/lxA5flhG6S1eWCj47FSgJe6vj3WMs0c3Hhrgpvu7xE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253864; c=relaxed/simple; bh=0QYTctIh8MNddPbLLyRwuchvn/FgSsOdulW4CPDxdaw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dLrTjgJcB/xsaZPmOKtvegl8loyIFBRxQs3cY0V2YqrlACnWWRKSeFli3rnu2LptzXyTk46jY+J5yKnNvMQQZnAg03ehJOodlAcS80tyPd7n7l89MyN0KVE4auRfILTRrNKEMcBV8XaWumT4w3fDPYLvSuH7Te5eAdJvTHYGwik= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ulMn1Xko; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ulMn1Xko" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E6D5C4CECD; Sun, 10 Nov 2024 15:51:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253864; bh=0QYTctIh8MNddPbLLyRwuchvn/FgSsOdulW4CPDxdaw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ulMn1XkoLk7mvCY9vmSDmUDuTJ+7hhrrwK1c7QfOCzfbYhYTi4mJmqfzqp4B8Ck4A A8J41Rh05GRFdX9kYOizWHJ8o6hrb7XNjQlmLIAjcKO4IUi+7aGT4ml5QdIg9WTr2r Ouf+E56wGypoSo/smX8i/jrS6VcrkKvY5H8AY0m2xozf/gxcDpHlJVJfnkoxA1PYJh lJg6DtqoV7wbTCXoggOITtEwOxM5v+fpU8WxGFg+rg8xGTOc9ORdhyweHCd5IqT3f+ oi4xZK5XMoYma8ikm6fs4WJe6JQYuuBsdZXzR5+zfdAWpIToKla/P1E4ZxASjuAIsD ure8JqT8Z68GA== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Sven Schnelle Subject: [PATCH v19 11/19] s390/tracing: Enable HAVE_FTRACE_GRAPH_FUNC Date: Mon, 11 Nov 2024 00:50:58 +0900 Message-ID: <173125385879.172790.60734156759309440.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Sven Schnelle Add ftrace_graph_func() which is required for fprobe to access registers. This also eliminates the need for calling prepare_ftrace_return() from ftrace_caller(). Signed-off-by: Sven Schnelle Acked-by: Heiko Carstens --- Changes in v19: - Newly added. --- arch/s390/Kconfig | 1 + arch/s390/include/asm/ftrace.h | 5 ++++ arch/s390/kernel/entry.h | 1 - arch/s390/kernel/ftrace.c | 48 ++++++++++++------------------------= ---- arch/s390/kernel/mcount.S | 11 --------- 5 files changed, 20 insertions(+), 46 deletions(-) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index eeb95342ddeb..87178183f952 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -182,6 +182,7 @@ config S390 select HAVE_EFFICIENT_UNALIGNED_ACCESS select HAVE_GUP_FAST select HAVE_FENTRY + select HAVE_FTRACE_GRAPH_FUNC select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_ARG_ACCESS_API select HAVE_FUNCTION_ERROR_INJECTION diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index 5b7cb49c41ee..fd3f0fe9f7b3 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -39,6 +39,7 @@ struct dyn_arch_ftrace { }; =20 struct module; struct dyn_ftrace; +struct ftrace_ops; =20 bool ftrace_need_init_nop(void); #define ftrace_need_init_nop ftrace_need_init_nop @@ -122,6 +123,10 @@ static inline bool arch_syscall_match_sym_name(const c= har *sym, return !strcmp(sym + 7, name) || !strcmp(sym + 8, name); } =20 +void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, + struct ftrace_ops *op, struct ftrace_regs *fregs); +#define ftrace_graph_func ftrace_graph_func + #endif /* __ASSEMBLY__ */ =20 #ifdef CONFIG_FUNCTION_TRACER diff --git a/arch/s390/kernel/entry.h b/arch/s390/kernel/entry.h index 21969520f947..a1f28879c87e 100644 --- a/arch/s390/kernel/entry.h +++ b/arch/s390/kernel/entry.h @@ -41,7 +41,6 @@ void do_restart(void *arg); void __init startup_init(void); void die(struct pt_regs *regs, const char *str); int setup_profiling_timer(unsigned int multiplier); -unsigned long prepare_ftrace_return(unsigned long parent, unsigned long sp= , unsigned long ip); =20 struct s390_mmap_arg_struct; struct fadvise64_64_args; diff --git a/arch/s390/kernel/ftrace.c b/arch/s390/kernel/ftrace.c index 51439a71e392..c0b2c97efefb 100644 --- a/arch/s390/kernel/ftrace.c +++ b/arch/s390/kernel/ftrace.c @@ -261,43 +261,23 @@ void ftrace_arch_code_modify_post_process(void) } =20 #ifdef CONFIG_FUNCTION_GRAPH_TRACER -/* - * Hook the return address and push it in the stack of return addresses - * in current thread info. - */ -unsigned long prepare_ftrace_return(unsigned long ra, unsigned long sp, - unsigned long ip) -{ - if (unlikely(ftrace_graph_is_dead())) - goto out; - if (unlikely(atomic_read(¤t->tracing_graph_pause))) - goto out; - ip -=3D MCOUNT_INSN_SIZE; - if (!function_graph_enter(ra, ip, 0, (void *) sp)) - ra =3D (unsigned long) return_to_handler; -out: - return ra; -} -NOKPROBE_SYMBOL(prepare_ftrace_return); =20 -/* - * Patch the kernel code at ftrace_graph_caller location. The instruction - * there is branch relative on condition. To enable the ftrace graph code - * block, we simply patch the mask field of the instruction to zero and - * turn the instruction into a nop. - * To disable the ftrace graph code the mask field will be patched to - * all ones, which turns the instruction into an unconditional branch. - */ -int ftrace_enable_ftrace_graph_caller(void) +void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, + struct ftrace_ops *op, struct ftrace_regs *fregs) { - /* Expect brc 0xf,... */ - return ftrace_patch_branch_mask(ftrace_graph_caller, 0xa7f4, false); -} + unsigned long *parent =3D &arch_ftrace_regs(fregs)->regs.gprs[14]; + int bit; =20 -int ftrace_disable_ftrace_graph_caller(void) -{ - /* Expect brc 0x0,... */ - return ftrace_patch_branch_mask(ftrace_graph_caller, 0xa704, true); + if (unlikely(ftrace_graph_is_dead())) + return; + if (unlikely(atomic_read(¤t->tracing_graph_pause))) + return; + bit =3D ftrace_test_recursion_trylock(ip, *parent); + if (bit < 0) + return; + if (!function_graph_enter_regs(*parent, ip, 0, parent, fregs)) + *parent =3D (unsigned long)&return_to_handler; + ftrace_test_recursion_unlock(bit); } =20 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/s390/kernel/mcount.S b/arch/s390/kernel/mcount.S index 2b628aa3d809..1fec370fecf4 100644 --- a/arch/s390/kernel/mcount.S +++ b/arch/s390/kernel/mcount.S @@ -104,17 +104,6 @@ SYM_CODE_START(ftrace_common) lgr %r3,%r14 la %r5,STACK_FREGS(%r15) BASR_EX %r14,%r1 -#ifdef CONFIG_FUNCTION_GRAPH_TRACER -# The j instruction gets runtime patched to a nop instruction. -# See ftrace_enable_ftrace_graph_caller. -SYM_INNER_LABEL(ftrace_graph_caller, SYM_L_GLOBAL) - j .Lftrace_graph_caller_end - lmg %r2,%r3,(STACK_FREGS_PTREGS_GPRS+14*8)(%r15) - lg %r4,(STACK_FREGS_PTREGS_PSW+8)(%r15) - brasl %r14,prepare_ftrace_return - stg %r2,(STACK_FREGS_PTREGS_GPRS+14*8)(%r15) -.Lftrace_graph_caller_end: -#endif lg %r0,(STACK_FREGS_PTREGS_PSW+8)(%r15) #ifdef MARCH_HAS_Z196_FEATURES ltg %r1,STACK_FREGS_PTREGS_ORIG_GPR2(%r15) From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5762126C08; Sun, 10 Nov 2024 15:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253880; cv=none; b=vGOweK7GngsViijZ7Fj21YvO0pniawdKyt61kicuN+hsijGDxi9idmy1Lo0T6KFY3wZ1+8tM5KvZ2y8i7PnEMt6dOaLv6yw9qUYwlllW/xolGl0dKiJ6DZF0htPhzW6GGuASPM91aOyC3lQ9vHHzfradVrwgeTZ/ZGCksL+ZfNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253880; c=relaxed/simple; bh=6RWcahHDC+tmuybro5CB4VY1R+QRRoJlLCGGdmtUvVM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BrFvz0pnGZfdqYOFD7rTYyqjXA7fwFBanxDkM83RrA7ajBPASlAF7m61L1zJgXCoGzIXz8yUjv3pEEnILTJ4x/aPQubN2WyMU2OMlaZDO8RS4VQ7+CdFew5AgEhtsB5++dlK53jDvZRGMphCxnYQH7tnCBmuY2SbNJEB3LE0/4c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SVPbK+Mi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SVPbK+Mi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3928C4CECD; Sun, 10 Nov 2024 15:51:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253879; bh=6RWcahHDC+tmuybro5CB4VY1R+QRRoJlLCGGdmtUvVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SVPbK+Mi2uCcd9zgcyZ+d8KU7oHwR2KsaCgVMoBvEUmS/fozYvo6B2REyp0CmFtl3 IrB7n52/83NOC8C+DuuqDlUObpBWbZfh5d9OZyCt7keKtR6uslC2BJ9tkonDwh2MEh bxbYctCTIr5b5HoPwh/gEvwLagEVwySSSCH6v7mM6WQfhvw3qR10iaqUW2DYVXRoe6 rMhtjeX5zB9wMZOOoRWAuunXIKEjhnuzsU60/Fap5DQLYhO2EioYQIdM9HNqD8pD58 9oYne0k32LvartxDsYmFU1lRBNhuJhSTj0Zhkbl5WcHTG757zeytghdLJRirrHKkP3 mFCPDOj3k9Cog== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Mathieu Desnoyers , Andrew Morton Subject: [PATCH v19 12/19] fprobe: Rewrite fprobe on function-graph tracer Date: Mon, 11 Nov 2024 00:51:09 +0900 Message-ID: <173125386944.172790.10278368602020246931.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Rewrite fprobe implementation on function-graph tracer. Major API changes are: - 'nr_maxactive' field is deprecated. - This depends on CONFIG_DYNAMIC_FTRACE_WITH_ARGS or !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS, and CONFIG_HAVE_FUNCTION_GRAPH_FREGS. So currently works only on x86_64. - Currently the entry size is limited in 15 * sizeof(long). - If there is too many fprobe exit handler set on the same function, it will fail to probe. Signed-off-by: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Cc: Huacai Chen Cc: WANG Xuerui Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Naveen N Rao Cc: Madhavan Srinivasan Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Mathieu Desnoyers Cc: Andrew Morton Acked-by: Heiko Carstens # s390 --- Changes in v19: - Fix comments on struct fprobe_hlist_node. - Add __counted_by() to struct fprobe_hlist::array. Changes in v18: - Use raw fprobe address and data size instead of encoding data size in fprobe header value. - Instead, allow arch to define its own encode/decode fprobe header. - Fix kconfig dependency issue to depend on DYNAMIC_FTRACE_WITH_ARGS. Changes in v14: - Add ftrace_regs_get_return_addresss() for riscv. Changes in v12: - Skip updating ftrace hash if not required. Changes in v9: - Remove unneeded prototype of ftrace_regs_get_return_address(). - Fix entry data address calculation. - Remove DIV_ROUND_UP() from hotpath. Changes in v8: - Use trace_func_graph_ret/ent_t for fgraph_ops. - Update CONFIG_FPROBE dependencies. - Add ftrace_regs_get_return_address() for each arch. Changes in v3: - Update for new reserve_data/retrieve_data API. - Fix internal push/pop on fgraph data logic so that it can correctly save/restore the returning fprobes. Changes in v2: - Add more lockdep_assert_held(fprobe_mutex) - Use READ_ONCE() and WRITE_ONCE() for fprobe_hlist_node::fp. - Add NOKPROBE_SYMBOL() for the functions which is called from entry/exit callback. --- arch/arm64/include/asm/ftrace.h | 6=20 arch/loongarch/include/asm/ftrace.h | 6=20 arch/powerpc/include/asm/ftrace.h | 6=20 arch/riscv/include/asm/ftrace.h | 5=20 arch/s390/include/asm/ftrace.h | 6=20 arch/x86/include/asm/ftrace.h | 6=20 include/linux/fprobe.h | 58 ++- kernel/trace/Kconfig | 10 - kernel/trace/fprobe.c | 637 +++++++++++++++++++++++++------= ---- lib/test_fprobe.c | 45 -- 10 files changed, 539 insertions(+), 246 deletions(-) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index 10e56522122a..876e88ad4119 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -135,6 +135,12 @@ ftrace_regs_get_frame_pointer(const struct ftrace_regs= *fregs) return arch_ftrace_regs(fregs)->fp; } =20 +static __always_inline unsigned long +ftrace_regs_get_return_address(const struct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->lr; +} + static __always_inline struct pt_regs * ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) { diff --git a/arch/loongarch/include/asm/ftrace.h b/arch/loongarch/include/a= sm/ftrace.h index ceb3e3d9c0d3..6e0a99763a9a 100644 --- a/arch/loongarch/include/asm/ftrace.h +++ b/arch/loongarch/include/asm/ftrace.h @@ -61,6 +61,12 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *= fregs, unsigned long ip) #define ftrace_regs_get_frame_pointer(fregs) \ (arch_ftrace_regs(fregs)->regs.regs[22]) =20 +static __always_inline unsigned long +ftrace_regs_get_return_address(struct ftrace_regs *fregs) +{ + return *(unsigned long *)(arch_ftrace_regs(fregs)->regs.regs[1]); +} + #define ftrace_graph_func ftrace_graph_func void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs); diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/f= trace.h index 407ce6eccc04..f3e7f0e44701 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -54,6 +54,12 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *= fregs, regs_set_return_ip(&arch_ftrace_regs(fregs)->regs, ip); } =20 +static __always_inline unsigned long +ftrace_regs_get_return_address(struct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->regs.link; +} + struct ftrace_ops; =20 #define ftrace_graph_func ftrace_graph_func diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrac= e.h index 7064a530794b..c4721ce44ca4 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -186,6 +186,11 @@ static __always_inline unsigned long ftrace_regs_get_r= eturn_value(const struct f return arch_ftrace_regs(fregs)->a0; } =20 +static __always_inline unsigned long ftrace_regs_get_return_address(const = struct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->ra; +} + static __always_inline void ftrace_regs_set_return_value(struct ftrace_reg= s *fregs, unsigned long ret) { diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index fd3f0fe9f7b3..a3b73a4f626e 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -77,6 +77,12 @@ ftrace_regs_get_frame_pointer(struct ftrace_regs *fregs) return ftrace_regs_get_stack_pointer(fregs); } =20 +static __always_inline unsigned long +ftrace_regs_get_return_address(const struct ftrace_regs *fregs) +{ + return arch_ftrace_regs(fregs)->regs.gprs[14]; +} + #define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ (_regs)->psw.mask =3D 0; \ (_regs)->psw.addr =3D arch_ftrace_regs(fregs)->regs.psw.addr; \ diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index 7e06f8c7937a..cc92c99ef276 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -58,6 +58,12 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs) do { arch_ftrace_regs(fregs)->regs.ip =3D (_ip); } while (0) =20 =20 +static __always_inline unsigned long +ftrace_regs_get_return_address(struct ftrace_regs *fregs) +{ + return *(unsigned long *)ftrace_regs_get_stack_pointer(fregs); +} + struct ftrace_ops; #define ftrace_graph_func ftrace_graph_func void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index ef609bcca0f9..91337bcb452f 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -5,10 +5,11 @@ =20 #include #include -#include +#include +#include +#include =20 struct fprobe; - typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigned long entry_ip, unsigned long ret_ip, struct ftrace_regs *regs, void *entry_data); @@ -17,35 +18,57 @@ typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsig= ned long entry_ip, unsigned long ret_ip, struct ftrace_regs *regs, void *entry_data); =20 +/** + * struct fprobe_hlist_node - address based hash list node for fprobe. + * + * @hlist: The hlist node for address search hash table. + * @addr: One of the probing address of @fp. + * @fp: The fprobe which owns this. + */ +struct fprobe_hlist_node { + struct hlist_node hlist; + unsigned long addr; + struct fprobe *fp; +}; + +/** + * struct fprobe_hlist - hash list nodes for fprobe. + * + * @hlist: The hlist node for existence checking hash table. + * @rcu: rcu_head for RCU deferred release. + * @fp: The fprobe which owns this fprobe_hlist. + * @size: The size of @array. + * @array: The fprobe_hlist_node for each address to probe. + */ +struct fprobe_hlist { + struct hlist_node hlist; + struct rcu_head rcu; + struct fprobe *fp; + int size; + struct fprobe_hlist_node array[] __counted_by(size); +}; + /** * struct fprobe - ftrace based probe. - * @ops: The ftrace_ops. + * * @nmissed: The counter for missing events. * @flags: The status flag. - * @rethook: The rethook data structure. (internal data) * @entry_data_size: The private data storage size. - * @nr_maxactive: The max number of active functions. + * @nr_maxactive: The max number of active functions. (*deprecated) * @entry_handler: The callback function for function entry. * @exit_handler: The callback function for function exit. + * @hlist_array: The fprobe_hlist for fprobe search from IP hash table. */ struct fprobe { -#ifdef CONFIG_FUNCTION_TRACER - /* - * If CONFIG_FUNCTION_TRACER is not set, CONFIG_FPROBE is disabled too. - * But user of fprobe may keep embedding the struct fprobe on their own - * code. To avoid build error, this will keep the fprobe data structure - * defined here, but remove ftrace_ops data structure. - */ - struct ftrace_ops ops; -#endif unsigned long nmissed; unsigned int flags; - struct rethook *rethook; size_t entry_data_size; int nr_maxactive; =20 fprobe_entry_cb entry_handler; fprobe_exit_cb exit_handler; + + struct fprobe_hlist *hlist_array; }; =20 /* This fprobe is soft-disabled. */ @@ -121,4 +144,9 @@ static inline void enable_fprobe(struct fprobe *fp) fp->flags &=3D ~FPROBE_FL_DISABLED; } =20 +/* The entry data size is 4 bits (=3D16) * sizeof(long) in maximum */ +#define FPROBE_DATA_SIZE_BITS 4 +#define MAX_FPROBE_DATA_SIZE_WORD ((1L << FPROBE_DATA_SIZE_BITS) - 1) +#define MAX_FPROBE_DATA_SIZE (MAX_FPROBE_DATA_SIZE_WORD * sizeof(long)) + #endif diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 2fc55a1a88aa..91a6382c04bd 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -307,12 +307,10 @@ config DYNAMIC_FTRACE_WITH_ARGS =20 config FPROBE bool "Kernel Function Probe (fprobe)" - depends on FUNCTION_TRACER - depends on DYNAMIC_FTRACE_WITH_REGS || DYNAMIC_FTRACE_WITH_ARGS - depends on HAVE_FTRACE_REGS_HAVING_PT_REGS || !HAVE_DYNAMIC_FTRACE_WITH_A= RGS - depends on HAVE_RETHOOK - select RETHOOK - default n + depends on HAVE_FUNCTION_GRAPH_FREGS && HAVE_FTRACE_GRAPH_FUNC + depends on DYNAMIC_FTRACE_WITH_ARGS + select FUNCTION_GRAPH_TRACER + default y help This option enables kernel function probe (fprobe) based on ftrace. The fprobe is similar to kprobes, but probes only for kernel function diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 90a3c8e2bbdf..ed9c1d79426a 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -8,98 +8,195 @@ #include #include #include -#include +#include +#include #include #include =20 #include "trace.h" =20 -struct fprobe_rethook_node { - struct rethook_node node; - unsigned long entry_ip; - unsigned long entry_parent_ip; - char data[]; -}; +#define FPROBE_IP_HASH_BITS 8 +#define FPROBE_IP_TABLE_SIZE (1 << FPROBE_IP_HASH_BITS) =20 -static inline void __fprobe_handler(unsigned long ip, unsigned long parent= _ip, - struct ftrace_ops *ops, struct ftrace_regs *fregs) -{ - struct fprobe_rethook_node *fpr; - struct rethook_node *rh =3D NULL; - struct fprobe *fp; - void *entry_data =3D NULL; - int ret =3D 0; +#define FPROBE_HASH_BITS 6 +#define FPROBE_TABLE_SIZE (1 << FPROBE_HASH_BITS) =20 - fp =3D container_of(ops, struct fprobe, ops); +#define SIZE_IN_LONG(x) ((x + sizeof(long) - 1) >> (sizeof(long) =3D=3D 8 = ? 3 : 2)) =20 - if (fp->exit_handler) { - rh =3D rethook_try_get(fp->rethook); - if (!rh) { - fp->nmissed++; - return; - } - fpr =3D container_of(rh, struct fprobe_rethook_node, node); - fpr->entry_ip =3D ip; - fpr->entry_parent_ip =3D parent_ip; - if (fp->entry_data_size) - entry_data =3D fpr->data; +/* + * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still + * exists. The key is the address of fprobe instance. + * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe + * instance related to the funciton address. The key is the ftrace IP + * address. + * + * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array= [*].fp + * are set NULL and delete those from both hash tables (by hlist_del_rcu). + * After an RCU grace period, the fprobe_hlist itself will be released. + * + * fprobe_table and fprobe_ip_table can be accessed from either + * - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held. + * - RCU hlist traversal under disabling preempt + */ +static struct hlist_head fprobe_table[FPROBE_TABLE_SIZE]; +static struct hlist_head fprobe_ip_table[FPROBE_IP_TABLE_SIZE]; +static DEFINE_MUTEX(fprobe_mutex); + +/* + * Find first fprobe in the hlist. It will be iterated twice in the entry + * probe, once for correcting the total required size, the second time is + * calling back the user handlers. + * Thus the hlist in the fprobe_table must be sorted and new probe needs to + * be added *before* the first fprobe. + */ +static struct fprobe_hlist_node *find_first_fprobe_node(unsigned long ip) +{ + struct fprobe_hlist_node *node; + struct hlist_head *head; + + head =3D &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)]; + hlist_for_each_entry_rcu(node, head, hlist, + lockdep_is_held(&fprobe_mutex)) { + if (node->addr =3D=3D ip) + return node; } + return NULL; +} +NOKPROBE_SYMBOL(find_first_fprobe_node); =20 - if (fp->entry_handler) - ret =3D fp->entry_handler(fp, ip, parent_ip, fregs, entry_data); +/* Node insertion and deletion requires the fprobe_mutex */ +static void insert_fprobe_node(struct fprobe_hlist_node *node) +{ + unsigned long ip =3D node->addr; + struct fprobe_hlist_node *next; + struct hlist_head *head; =20 - /* If entry_handler returns !0, nmissed is not counted. */ - if (rh) { - if (ret) - rethook_recycle(rh); - else - rethook_hook(rh, ftrace_get_regs(fregs), true); + lockdep_assert_held(&fprobe_mutex); + + next =3D find_first_fprobe_node(ip); + if (next) { + hlist_add_before_rcu(&node->hlist, &next->hlist); + return; } + head =3D &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)]; + hlist_add_head_rcu(&node->hlist, head); } =20 -static void fprobe_handler(unsigned long ip, unsigned long parent_ip, - struct ftrace_ops *ops, struct ftrace_regs *fregs) +/* Return true if there are synonims */ +static bool delete_fprobe_node(struct fprobe_hlist_node *node) { - struct fprobe *fp; - int bit; + lockdep_assert_held(&fprobe_mutex); =20 - fp =3D container_of(ops, struct fprobe, ops); - if (fprobe_disabled(fp)) - return; + WRITE_ONCE(node->fp, NULL); + hlist_del_rcu(&node->hlist); + return !!find_first_fprobe_node(node->addr); +} =20 - /* recursion detection has to go before any traceable function and - * all functions before this point should be marked as notrace - */ - bit =3D ftrace_test_recursion_trylock(ip, parent_ip); - if (bit < 0) { - fp->nmissed++; - return; +/* Check existence of the fprobe */ +static bool is_fprobe_still_exist(struct fprobe *fp) +{ + struct hlist_head *head; + struct fprobe_hlist *fph; + + head =3D &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)]; + hlist_for_each_entry_rcu(fph, head, hlist, + lockdep_is_held(&fprobe_mutex)) { + if (fph->fp =3D=3D fp) + return true; } - __fprobe_handler(ip, parent_ip, ops, fregs); - ftrace_test_recursion_unlock(bit); + return false; +} +NOKPROBE_SYMBOL(is_fprobe_still_exist); + +static int add_fprobe_hash(struct fprobe *fp) +{ + struct fprobe_hlist *fph =3D fp->hlist_array; + struct hlist_head *head; + + lockdep_assert_held(&fprobe_mutex); + + if (WARN_ON_ONCE(!fph)) + return -EINVAL; + + if (is_fprobe_still_exist(fp)) + return -EEXIST; =20 + head =3D &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)]; + hlist_add_head_rcu(&fp->hlist_array->hlist, head); + return 0; } -NOKPROBE_SYMBOL(fprobe_handler); =20 -static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_i= p, - struct ftrace_ops *ops, struct ftrace_regs *fregs) +static int del_fprobe_hash(struct fprobe *fp) { + struct fprobe_hlist *fph =3D fp->hlist_array; + + lockdep_assert_held(&fprobe_mutex); + + if (WARN_ON_ONCE(!fph)) + return -EINVAL; + + if (!is_fprobe_still_exist(fp)) + return -ENOENT; + + fph->fp =3D NULL; + hlist_del_rcu(&fph->hlist); + return 0; +} + +/* Generic fprobe_header */ +struct __fprobe_header { struct fprobe *fp; - int bit; + unsigned long size_words; +} __packed; =20 - fp =3D container_of(ops, struct fprobe, ops); - if (fprobe_disabled(fp)) - return; +#define FPROBE_HEADER_SIZE_IN_LONG SIZE_IN_LONG(sizeof(struct __fprobe_hea= der)) =20 - /* recursion detection has to go before any traceable function and - * all functions called before this point should be marked as notrace - */ - bit =3D ftrace_test_recursion_trylock(ip, parent_ip); - if (bit < 0) { - fp->nmissed++; - return; - } +static inline bool write_fprobe_header(unsigned long *stack, + struct fprobe *fp, unsigned int size_words) +{ + struct __fprobe_header *fph =3D (struct __fprobe_header *)stack; =20 + if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD)) + return false; + + fph->fp =3D fp; + fph->size_words =3D size_words; + return true; +} + +static inline void read_fprobe_header(unsigned long *stack, + struct fprobe **fp, unsigned int *size_words) +{ + struct __fprobe_header *fph =3D (struct __fprobe_header *)stack; + + *fp =3D fph->fp; + *size_words =3D fph->size_words; +} + +/* + * fprobe shadow stack management: + * Since fprobe shares a single fgraph_ops, it needs to share the stack en= try + * among the probes on the same function exit. Note that a new probe can be + * registered before a target function is returning, we can not use the ha= sh + * table to find the corresponding probes. Thus the probe address is store= d on + * the shadow stack with its entry data size. + * + */ +static inline int __fprobe_handler(unsigned long ip, unsigned long parent_= ip, + struct fprobe *fp, struct ftrace_regs *fregs, + void *data) +{ + if (!fp->entry_handler) + return 0; + + return fp->entry_handler(fp, ip, parent_ip, fregs, data); +} + +static inline int __fprobe_kprobe_handler(unsigned long ip, unsigned long = parent_ip, + struct fprobe *fp, struct ftrace_regs *fregs, + void *data) +{ + int ret; /* * This user handler is shared with other kprobes and is not expected to = be * called recursively. So if any other kprobe handler is running, this wi= ll @@ -108,45 +205,183 @@ static void fprobe_kprobe_handler(unsigned long ip, = unsigned long parent_ip, */ if (unlikely(kprobe_running())) { fp->nmissed++; - goto recursion_unlock; + return 0; } =20 kprobe_busy_begin(); - __fprobe_handler(ip, parent_ip, ops, fregs); + ret =3D __fprobe_handler(ip, parent_ip, fp, fregs, data); kprobe_busy_end(); - -recursion_unlock: - ftrace_test_recursion_unlock(bit); + return ret; } =20 -static void fprobe_exit_handler(struct rethook_node *rh, void *data, - unsigned long ret_ip, struct pt_regs *regs) +static int fprobe_entry(struct ftrace_graph_ent *trace, struct fgraph_ops = *gops, + struct ftrace_regs *fregs) { - struct fprobe *fp =3D (struct fprobe *)data; - struct fprobe_rethook_node *fpr; - struct ftrace_regs *fregs =3D (struct ftrace_regs *)regs; - int bit; + struct fprobe_hlist_node *node, *first; + unsigned long *fgraph_data =3D NULL; + unsigned long func =3D trace->func; + unsigned long ret_ip; + int reserved_words; + struct fprobe *fp; + int used, ret; =20 - if (!fp || fprobe_disabled(fp)) - return; + if (WARN_ON_ONCE(!fregs)) + return 0; =20 - fpr =3D container_of(rh, struct fprobe_rethook_node, node); + first =3D node =3D find_first_fprobe_node(func); + if (unlikely(!first)) + return 0; + + reserved_words =3D 0; + hlist_for_each_entry_from_rcu(node, hlist) { + if (node->addr !=3D func) + break; + fp =3D READ_ONCE(node->fp); + if (!fp || !fp->exit_handler) + continue; + /* + * Since fprobe can be enabled until the next loop, we ignore the + * fprobe's disabled flag in this loop. + */ + reserved_words +=3D + FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size); + } + node =3D first; + if (reserved_words) { + fgraph_data =3D fgraph_reserve_data(gops->idx, reserved_words * sizeof(l= ong)); + if (unlikely(!fgraph_data)) { + hlist_for_each_entry_from_rcu(node, hlist) { + if (node->addr !=3D func) + break; + fp =3D READ_ONCE(node->fp); + if (fp && !fprobe_disabled(fp)) + fp->nmissed++; + } + return 0; + } + } =20 /* - * we need to assure no calls to traceable functions in-between the - * end of fprobe_handler and the beginning of fprobe_exit_handler. + * TODO: recursion detection has been done in the fgraph. Thus we need + * to add a callback to increment missed counter. */ - bit =3D ftrace_test_recursion_trylock(fpr->entry_ip, fpr->entry_parent_ip= ); - if (bit < 0) { - fp->nmissed++; + ret_ip =3D ftrace_regs_get_return_address(fregs); + used =3D 0; + hlist_for_each_entry_from_rcu(node, hlist) { + int data_size; + void *data; + + if (node->addr !=3D func) + break; + fp =3D READ_ONCE(node->fp); + if (!fp || fprobe_disabled(fp)) + continue; + + data_size =3D fp->entry_data_size; + if (data_size && fp->exit_handler) + data =3D fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG; + else + data =3D NULL; + + if (fprobe_shared_with_kprobes(fp)) + ret =3D __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data); + else + ret =3D __fprobe_handler(func, ret_ip, fp, fregs, data); + + /* If entry_handler returns !0, nmissed is not counted but skips exit_ha= ndler. */ + if (!ret && fp->exit_handler) { + int size_words =3D SIZE_IN_LONG(data_size); + + if (write_fprobe_header(&fgraph_data[used], fp, size_words)) + used +=3D FPROBE_HEADER_SIZE_IN_LONG + size_words; + } + } + if (used < reserved_words) + memset(fgraph_data + used, 0, reserved_words - used); + + /* If any exit_handler is set, data must be used. */ + return used !=3D 0; +} +NOKPROBE_SYMBOL(fprobe_entry); + +static void fprobe_return(struct ftrace_graph_ret *trace, + struct fgraph_ops *gops, + struct ftrace_regs *fregs) +{ + unsigned long *fgraph_data =3D NULL; + unsigned long ret_ip; + struct fprobe *fp; + int size, curr; + int size_words; + + fgraph_data =3D (unsigned long *)fgraph_retrieve_data(gops->idx, &size); + if (WARN_ON_ONCE(!fgraph_data)) return; + size_words =3D SIZE_IN_LONG(size); + ret_ip =3D ftrace_regs_get_instruction_pointer(fregs); + + preempt_disable(); + + curr =3D 0; + while (size_words > curr) { + read_fprobe_header(&fgraph_data[curr], &fp, &size); + if (!fp) + break; + curr +=3D FPROBE_HEADER_SIZE_IN_LONG; + if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) { + if (WARN_ON_ONCE(curr + size > size_words)) + break; + fp->exit_handler(fp, trace->func, ret_ip, fregs, + size ? fgraph_data + curr : NULL); + } + curr +=3D size; } + preempt_enable(); +} +NOKPROBE_SYMBOL(fprobe_return); + +static struct fgraph_ops fprobe_graph_ops =3D { + .entryfunc =3D fprobe_entry, + .retfunc =3D fprobe_return, +}; +static int fprobe_graph_active; + +/* Add @addrs to the ftrace filter and register fgraph if needed. */ +static int fprobe_graph_add_ips(unsigned long *addrs, int num) +{ + int ret; =20 - fp->exit_handler(fp, fpr->entry_ip, ret_ip, fregs, - fp->entry_data_size ? (void *)fpr->data : NULL); - ftrace_test_recursion_unlock(bit); + lockdep_assert_held(&fprobe_mutex); + + ret =3D ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); + if (ret) + return ret; + + if (!fprobe_graph_active) { + ret =3D register_ftrace_graph(&fprobe_graph_ops); + if (WARN_ON_ONCE(ret)) { + ftrace_free_filter(&fprobe_graph_ops.ops); + return ret; + } + } + fprobe_graph_active++; + return 0; +} + +/* Remove @addrs from the ftrace filter and unregister fgraph if possible.= */ +static void fprobe_graph_remove_ips(unsigned long *addrs, int num) +{ + lockdep_assert_held(&fprobe_mutex); + + fprobe_graph_active--; + if (!fprobe_graph_active) { + /* Q: should we unregister it ? */ + unregister_ftrace_graph(&fprobe_graph_ops); + return; + } + + ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0); } -NOKPROBE_SYMBOL(fprobe_exit_handler); =20 static int symbols_cmp(const void *a, const void *b) { @@ -176,54 +411,97 @@ static unsigned long *get_ftrace_locations(const char= **syms, int num) return ERR_PTR(-ENOENT); } =20 -static void fprobe_init(struct fprobe *fp) -{ - fp->nmissed =3D 0; - if (fprobe_shared_with_kprobes(fp)) - fp->ops.func =3D fprobe_kprobe_handler; - else - fp->ops.func =3D fprobe_handler; - - fp->ops.flags |=3D FTRACE_OPS_FL_SAVE_REGS; -} +struct filter_match_data { + const char *filter; + const char *notfilter; + size_t index; + size_t size; + unsigned long *addrs; +}; =20 -static int fprobe_init_rethook(struct fprobe *fp, int num) +static int filter_match_callback(void *data, const char *name, unsigned lo= ng addr) { - int size; + struct filter_match_data *match =3D data; =20 - if (!fp->exit_handler) { - fp->rethook =3D NULL; + if (!glob_match(match->filter, name) || + (match->notfilter && glob_match(match->notfilter, name))) return 0; - } =20 - /* Initialize rethook if needed */ - if (fp->nr_maxactive) - num =3D fp->nr_maxactive; - else - num *=3D num_possible_cpus() * 2; - if (num <=3D 0) - return -EINVAL; + if (!ftrace_location(addr)) + return 0; =20 - size =3D sizeof(struct fprobe_rethook_node) + fp->entry_data_size; + if (match->addrs) + match->addrs[match->index] =3D addr; =20 - /* Initialize rethook */ - fp->rethook =3D rethook_alloc((void *)fp, fprobe_exit_handler, size, num); - if (IS_ERR(fp->rethook)) - return PTR_ERR(fp->rethook); + match->index++; + return match->index =3D=3D match->size; +} =20 - return 0; +/* + * Make IP list from the filter/no-filter glob patterns. + * Return the number of matched symbols, or -ENOENT. + */ +static int ip_list_from_filter(const char *filter, const char *notfilter, + unsigned long *addrs, size_t size) +{ + struct filter_match_data match =3D { .filter =3D filter, .notfilter =3D n= otfilter, + .index =3D 0, .size =3D size, .addrs =3D addrs}; + int ret; + + ret =3D kallsyms_on_each_symbol(filter_match_callback, &match); + if (ret < 0) + return ret; + ret =3D module_kallsyms_on_each_symbol(NULL, filter_match_callback, &matc= h); + if (ret < 0) + return ret; + + return match.index ?: -ENOENT; } =20 static void fprobe_fail_cleanup(struct fprobe *fp) { - if (!IS_ERR_OR_NULL(fp->rethook)) { - /* Don't need to cleanup rethook->handler because this is not used. */ - rethook_free(fp->rethook); - fp->rethook =3D NULL; + kfree(fp->hlist_array); + fp->hlist_array =3D NULL; +} + +/* Initialize the fprobe data structure. */ +static int fprobe_init(struct fprobe *fp, unsigned long *addrs, int num) +{ + struct fprobe_hlist *hlist_array; + unsigned long addr; + int size, i; + + if (!fp || !addrs || num <=3D 0) + return -EINVAL; + + size =3D ALIGN(fp->entry_data_size, sizeof(long)); + if (size > MAX_FPROBE_DATA_SIZE) + return -E2BIG; + fp->entry_data_size =3D size; + + hlist_array =3D kzalloc(struct_size(hlist_array, array, num), GFP_KERNEL); + if (!hlist_array) + return -ENOMEM; + + fp->nmissed =3D 0; + + hlist_array->size =3D num; + fp->hlist_array =3D hlist_array; + hlist_array->fp =3D fp; + for (i =3D 0; i < num; i++) { + hlist_array->array[i].fp =3D fp; + addr =3D ftrace_location(addrs[i]); + if (!addr) { + fprobe_fail_cleanup(fp); + return -ENOENT; + } + hlist_array->array[i].addr =3D addr; } - ftrace_free_filter(&fp->ops); + return 0; } =20 +#define FPROBE_IPS_MAX INT_MAX + /** * register_fprobe() - Register fprobe to ftrace by pattern. * @fp: A fprobe data structure to be registered. @@ -237,46 +515,24 @@ static void fprobe_fail_cleanup(struct fprobe *fp) */ int register_fprobe(struct fprobe *fp, const char *filter, const char *not= filter) { - struct ftrace_hash *hash; - unsigned char *str; - int ret, len; + unsigned long *addrs; + int ret; =20 if (!fp || !filter) return -EINVAL; =20 - fprobe_init(fp); - - len =3D strlen(filter); - str =3D kstrdup(filter, GFP_KERNEL); - ret =3D ftrace_set_filter(&fp->ops, str, len, 0); - kfree(str); - if (ret) + ret =3D ip_list_from_filter(filter, notfilter, NULL, FPROBE_IPS_MAX); + if (ret < 0) return ret; =20 - if (notfilter) { - len =3D strlen(notfilter); - str =3D kstrdup(notfilter, GFP_KERNEL); - ret =3D ftrace_set_notrace(&fp->ops, str, len, 0); - kfree(str); - if (ret) - goto out; - } - - /* TODO: - * correctly calculate the total number of filtered symbols - * from both filter and notfilter. - */ - hash =3D rcu_access_pointer(fp->ops.local_hash.filter_hash); - if (WARN_ON_ONCE(!hash)) - goto out; - - ret =3D fprobe_init_rethook(fp, (int)hash->count); - if (!ret) - ret =3D register_ftrace_function(&fp->ops); + addrs =3D kcalloc(ret, sizeof(unsigned long), GFP_KERNEL); + if (!addrs) + return -ENOMEM; + ret =3D ip_list_from_filter(filter, notfilter, addrs, ret); + if (ret > 0) + ret =3D register_fprobe_ips(fp, addrs, ret); =20 -out: - if (ret) - fprobe_fail_cleanup(fp); + kfree(addrs); return ret; } EXPORT_SYMBOL_GPL(register_fprobe); @@ -284,7 +540,7 @@ EXPORT_SYMBOL_GPL(register_fprobe); /** * register_fprobe_ips() - Register fprobe to ftrace by address. * @fp: A fprobe data structure to be registered. - * @addrs: An array of target ftrace location addresses. + * @addrs: An array of target function address. * @num: The number of entries of @addrs. * * Register @fp to ftrace for enabling the probe on the address given by @= addrs. @@ -296,23 +552,27 @@ EXPORT_SYMBOL_GPL(register_fprobe); */ int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) { - int ret; - - if (!fp || !addrs || num <=3D 0) - return -EINVAL; + struct fprobe_hlist *hlist_array; + int ret, i; =20 - fprobe_init(fp); - - ret =3D ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0); + ret =3D fprobe_init(fp, addrs, num); if (ret) return ret; =20 - ret =3D fprobe_init_rethook(fp, num); - if (!ret) - ret =3D register_ftrace_function(&fp->ops); + mutex_lock(&fprobe_mutex); + + hlist_array =3D fp->hlist_array; + ret =3D fprobe_graph_add_ips(addrs, num); + if (!ret) { + add_fprobe_hash(fp); + for (i =3D 0; i < hlist_array->size; i++) + insert_fprobe_node(&hlist_array->array[i]); + } + mutex_unlock(&fprobe_mutex); =20 if (ret) fprobe_fail_cleanup(fp); + return ret; } EXPORT_SYMBOL_GPL(register_fprobe_ips); @@ -350,14 +610,13 @@ EXPORT_SYMBOL_GPL(register_fprobe_syms); =20 bool fprobe_is_registered(struct fprobe *fp) { - if (!fp || (fp->ops.saved_func !=3D fprobe_handler && - fp->ops.saved_func !=3D fprobe_kprobe_handler)) + if (!fp || !fp->hlist_array) return false; return true; } =20 /** - * unregister_fprobe() - Unregister fprobe from ftrace + * unregister_fprobe() - Unregister fprobe. * @fp: A fprobe data structure to be unregistered. * * Unregister fprobe (and remove ftrace hooks from the function entries). @@ -366,23 +625,41 @@ bool fprobe_is_registered(struct fprobe *fp) */ int unregister_fprobe(struct fprobe *fp) { - int ret; + struct fprobe_hlist *hlist_array; + unsigned long *addrs =3D NULL; + int ret =3D 0, i, count; =20 - if (!fprobe_is_registered(fp)) - return -EINVAL; + mutex_lock(&fprobe_mutex); + if (!fp || !is_fprobe_still_exist(fp)) { + ret =3D -EINVAL; + goto out; + } =20 - if (!IS_ERR_OR_NULL(fp->rethook)) - rethook_stop(fp->rethook); + hlist_array =3D fp->hlist_array; + addrs =3D kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL); + if (!addrs) { + ret =3D -ENOMEM; /* TODO: Fallback to one-by-one loop */ + goto out; + } =20 - ret =3D unregister_ftrace_function(&fp->ops); - if (ret < 0) - return ret; + /* Remove non-synonim ips from table and hash */ + count =3D 0; + for (i =3D 0; i < hlist_array->size; i++) { + if (!delete_fprobe_node(&hlist_array->array[i])) + addrs[count++] =3D hlist_array->array[i].addr; + } + del_fprobe_hash(fp); =20 - if (!IS_ERR_OR_NULL(fp->rethook)) - rethook_free(fp->rethook); + if (count) + fprobe_graph_remove_ips(addrs, count); =20 - ftrace_free_filter(&fp->ops); + kfree_rcu(hlist_array, rcu); + fp->hlist_array =3D NULL; =20 +out: + mutex_unlock(&fprobe_mutex); + + kfree(addrs); return ret; } EXPORT_SYMBOL_GPL(unregister_fprobe); diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c index 271ce0caeec0..cf92111b5c79 100644 --- a/lib/test_fprobe.c +++ b/lib/test_fprobe.c @@ -17,10 +17,8 @@ static u32 rand1, entry_val, exit_val; /* Use indirect calls to avoid inlining the target functions */ static u32 (*target)(u32 value); static u32 (*target2)(u32 value); -static u32 (*target_nest)(u32 value, u32 (*nest)(u32)); static unsigned long target_ip; static unsigned long target2_ip; -static unsigned long target_nest_ip; static int entry_return_value; =20 static noinline u32 fprobe_selftest_target(u32 value) @@ -33,11 +31,6 @@ static noinline u32 fprobe_selftest_target2(u32 value) return (value / div_factor) + 1; } =20 -static noinline u32 fprobe_selftest_nest_target(u32 value, u32 (*nest)(u32= )) -{ - return nest(value + 2); -} - static notrace int fp_entry_handler(struct fprobe *fp, unsigned long ip, unsigned long ret_ip, struct ftrace_regs *fregs, void *data) @@ -79,22 +72,6 @@ static notrace void fp_exit_handler(struct fprobe *fp, u= nsigned long ip, KUNIT_EXPECT_NULL(current_test, data); } =20 -static notrace int nest_entry_handler(struct fprobe *fp, unsigned long ip, - unsigned long ret_ip, - struct ftrace_regs *fregs, void *data) -{ - KUNIT_EXPECT_FALSE(current_test, preemptible()); - return 0; -} - -static notrace void nest_exit_handler(struct fprobe *fp, unsigned long ip, - unsigned long ret_ip, - struct ftrace_regs *fregs, void *data) -{ - KUNIT_EXPECT_FALSE(current_test, preemptible()); - KUNIT_EXPECT_EQ(current_test, ip, target_nest_ip); -} - /* Test entry only (no rethook) */ static void test_fprobe_entry(struct kunit *test) { @@ -191,25 +168,6 @@ static void test_fprobe_data(struct kunit *test) KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp)); } =20 -/* Test nr_maxactive */ -static void test_fprobe_nest(struct kunit *test) -{ - static const char *syms[] =3D {"fprobe_selftest_target", "fprobe_selftest= _nest_target"}; - struct fprobe fp =3D { - .entry_handler =3D nest_entry_handler, - .exit_handler =3D nest_exit_handler, - .nr_maxactive =3D 1, - }; - - current_test =3D test; - KUNIT_EXPECT_EQ(test, 0, register_fprobe_syms(&fp, syms, 2)); - - target_nest(rand1, target); - KUNIT_EXPECT_EQ(test, 1, fp.nmissed); - - KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp)); -} - static void test_fprobe_skip(struct kunit *test) { struct fprobe fp =3D { @@ -247,10 +205,8 @@ static int fprobe_test_init(struct kunit *test) rand1 =3D get_random_u32_above(div_factor); target =3D fprobe_selftest_target; target2 =3D fprobe_selftest_target2; - target_nest =3D fprobe_selftest_nest_target; target_ip =3D get_ftrace_location(target); target2_ip =3D get_ftrace_location(target2); - target_nest_ip =3D get_ftrace_location(target_nest); =20 return 0; } @@ -260,7 +216,6 @@ static struct kunit_case fprobe_testcases[] =3D { KUNIT_CASE(test_fprobe), KUNIT_CASE(test_fprobe_syms), KUNIT_CASE(test_fprobe_data), - KUNIT_CASE(test_fprobe_nest), KUNIT_CASE(test_fprobe_skip), {} }; From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8186081741; Sun, 10 Nov 2024 15:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253896; cv=none; b=Azf9uNYnL72bc5Uda/XjbKUiWrkrN07uzBu+rLZMuW/ojtYOmpjhpaQ58SYprcjIPzxpGzlEK/96/mRG59XmY5RIWtjIIWD/HLwrY+oxh5sjB1JzKcJEW3CLqkxvJCyadRWr9oJcFXqIgrE+X3i0ka9oLEfwe4HSftH3hoaOkFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253896; c=relaxed/simple; bh=/HkSeJVyvgLEhQ5zekLtpHg9nCl4G6foTq1nkaeg7FY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zd0vKluxAdXMHFboCzKBzyxO1ISzKsDAzsE112ITUaS0uJpleItQ3Aedb9LmvDtIiTpw0csZ/n1E9JCcGrMfj0EkOy4Q99E+MH+SJ1lypE8+fxnqT6a/Zq9ia6gQfnybNItfw1kTa/26koZh74pjjhGvnYrRyabgbGv2qHtLhCU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lNB+45qC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lNB+45qC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77318C4CECD; Sun, 10 Nov 2024 15:51:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253894; bh=/HkSeJVyvgLEhQ5zekLtpHg9nCl4G6foTq1nkaeg7FY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lNB+45qC6uZp/1TFNdePkucUf4gFtCe4yhhJA2u9XEsPbaAamdcH5YbGzmruhiNHn lTcaWBVhGajyRjMPAm9OLK5FFY5PDPUQ2se9ivOK3mmjyXOXTIYC8njq/0qyWIXd91 4/vuZJGR6Cy8jxAz2yf7k0NRce1m+yTL5jCDOAHT9tt5KlvrNaPsiLext6QK1Hkplk i3SYqXEvmQfG9W693fFV9ks2nfGzDYIuKRllJYKHa7XNqaLrk/MUnyCKEN2OLd5kjm icOsNzxT3FmZ3rOd8lUfzisKW3tJncyzwbCAPO0IZV9HtnbYhB2pbBrdE07F1tW072 FRnhKXIJlAW+A== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org, Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Arnd Bergmann , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v19 13/19] fprobe: Add fprobe_header encoding feature Date: Mon, 11 Nov 2024 00:51:25 +0900 Message-ID: <173125388510.172790.1161831132316963172.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Fprobe store its data structure address and size on the fgraph return stack by __fprobe_header. But most 64bit architecture can combine those to one unsigned long value because 4 MSB in the kernel address are the same. With this encoding, fprobe can consume less space on ret_stack. This introduces asm/fprobe.h to define arch dependent encode/decode macros. Note that since fprobe depends on CONFIG_HAVE_FUNCTION_GRAPH_FREGS, currently only arm64, loongarch, riscv, s390 and x86 are supported. Signed-off-by: Masami Hiramatsu (Google) Cc: Catalin Marinas Cc: Will Deacon Cc: Huacai Chen Cc: WANG Xuerui Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Arnd Bergmann Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Acked-by: Heiko Carstens # s390 --- Changes in v19: - Fix to use Kbuild for generic arm/fprobe.h - Check CONFIG_64BIT in asm-generic/fprobe.h. --- arch/arm64/include/asm/Kbuild | 1 + arch/loongarch/include/asm/fprobe.h | 12 +++++++++ arch/riscv/include/asm/Kbuild | 1 + arch/s390/include/asm/fprobe.h | 10 ++++++++ arch/x86/include/asm/Kbuild | 1 + include/asm-generic/fprobe.h | 46 +++++++++++++++++++++++++++++++= ++++ kernel/trace/fprobe.c | 29 ++++++++++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 arch/loongarch/include/asm/fprobe.h create mode 100644 arch/s390/include/asm/fprobe.h create mode 100644 include/asm-generic/fprobe.h diff --git a/arch/arm64/include/asm/Kbuild b/arch/arm64/include/asm/Kbuild index 4e350df9a02d..d2ff8f6c3231 100644 --- a/arch/arm64/include/asm/Kbuild +++ b/arch/arm64/include/asm/Kbuild @@ -8,6 +8,7 @@ syscall-y +=3D unistd_32.h syscall-y +=3D unistd_compat_32.h =20 generic-y +=3D early_ioremap.h +generic-y +=3D fprobe.h generic-y +=3D mcs_spinlock.h generic-y +=3D mmzone.h generic-y +=3D qrwlock.h diff --git a/arch/loongarch/include/asm/fprobe.h b/arch/loongarch/include/a= sm/fprobe.h new file mode 100644 index 000000000000..7af3b3126caf --- /dev/null +++ b/arch/loongarch/include/asm/fprobe.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_LOONGARCH_FPROBE_H +#define _ASM_LOONGARCH_FPROBE_H + +/* + * Explicitly undef ARCH_DEFINE_ENCODE_FPROBE_HEADER, because loongarch do= es not + * have enough number of fixed MSBs of the address of kernel objects for + * encoding the size of data in fprobe_header. Use 2-entries encoding inst= ead. + */ +#undef ARCH_DEFINE_ENCODE_FPROBE_HEADER + +#endif /* _ASM_LOONGARCH_FPROBE_H */ diff --git a/arch/riscv/include/asm/Kbuild b/arch/riscv/include/asm/Kbuild index 1461af12da6e..e4c8790d728a 100644 --- a/arch/riscv/include/asm/Kbuild +++ b/arch/riscv/include/asm/Kbuild @@ -4,6 +4,7 @@ syscall-y +=3D syscall_table_64.h =20 generic-y +=3D early_ioremap.h generic-y +=3D flat.h +generic-y +=3D fprobe.h generic-y +=3D kvm_para.h generic-y +=3D mmzone.h generic-y +=3D parport.h diff --git a/arch/s390/include/asm/fprobe.h b/arch/s390/include/asm/fprobe.h new file mode 100644 index 000000000000..5ef600b372f4 --- /dev/null +++ b/arch/s390/include/asm/fprobe.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_S390_FPROBE_H +#define _ASM_S390_FPROBE_H + +#include + +#undef FPROBE_HEADER_MSB_PATTERN +#define FPROBE_HEADER_MSB_PATTERN 0 + +#endif /* _ASM_S390_FPROBE_H */ diff --git a/arch/x86/include/asm/Kbuild b/arch/x86/include/asm/Kbuild index 6c23d1661b17..58f4ddecc5fa 100644 --- a/arch/x86/include/asm/Kbuild +++ b/arch/x86/include/asm/Kbuild @@ -10,5 +10,6 @@ generated-y +=3D unistd_64_x32.h generated-y +=3D xen-hypercalls.h =20 generic-y +=3D early_ioremap.h +generic-y +=3D fprobe.h generic-y +=3D mcs_spinlock.h generic-y +=3D mmzone.h diff --git a/include/asm-generic/fprobe.h b/include/asm-generic/fprobe.h new file mode 100644 index 000000000000..8659a4dc6eb6 --- /dev/null +++ b/include/asm-generic/fprobe.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Generic arch dependent fprobe macros. + */ +#ifndef __ASM_GENERIC_FPROBE_H__ +#define __ASM_GENERIC_FPROBE_H__ + +#include + +#ifdef CONFIG_64BIT +/* + * Encoding the size and the address of fprobe into one 64bit entry. + * The 32bit architectures should use 2 entries to store those info. + */ + +#define ARCH_DEFINE_ENCODE_FPROBE_HEADER + +#define FPROBE_HEADER_MSB_SIZE_SHIFT (BITS_PER_LONG - FPROBE_DATA_SIZE_BIT= S) +#define FPROBE_HEADER_MSB_MASK \ + GENMASK(FPROBE_HEADER_MSB_SIZE_SHIFT - 1, 0) + +/* + * By default, this expects the MSBs in the address of kprobe is 0xf. + * If any arch needs another fixed pattern (e.g. s390 is zero filled), + * override this. + */ +#define FPROBE_HEADER_MSB_PATTERN \ + GENMASK(BITS_PER_LONG - 1, FPROBE_HEADER_MSB_SIZE_SHIFT) + +#define arch_fprobe_header_encodable(fp) \ + (((unsigned long)(fp) & ~FPROBE_HEADER_MSB_MASK) =3D=3D \ + FPROBE_HEADER_MSB_PATTERN) + +#define arch_encode_fprobe_header(fp, size) \ + (((unsigned long)(fp) & FPROBE_HEADER_MSB_MASK) | \ + ((unsigned long)(size) << FPROBE_HEADER_MSB_SIZE_SHIFT)) + +#define arch_decode_fprobe_header_size(val) \ + ((unsigned long)(val) >> FPROBE_HEADER_MSB_SIZE_SHIFT) + +#define arch_decode_fprobe_header_fp(val) \ + ((struct fprobe *)(((unsigned long)(val) & FPROBE_HEADER_MSB_MASK) | \ + FPROBE_HEADER_MSB_PATTERN)) +#endif /* CONFIG_64BIT */ + +#endif /* __ASM_GENERIC_FPROBE_H__ */ diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index ed9c1d79426a..2560b312ad57 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -13,6 +13,8 @@ #include #include =20 +#include + #include "trace.h" =20 #define FPROBE_IP_HASH_BITS 8 @@ -143,6 +145,31 @@ static int del_fprobe_hash(struct fprobe *fp) return 0; } =20 +#ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER + +/* The arch should encode fprobe_header info into one unsigned long */ +#define FPROBE_HEADER_SIZE_IN_LONG 1 + +static inline bool write_fprobe_header(unsigned long *stack, + struct fprobe *fp, unsigned int size_words) +{ + if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD || + !arch_fprobe_header_encodable(fp))) + return false; + + *stack =3D arch_encode_fprobe_header(fp, size_words); + return true; +} + +static inline void read_fprobe_header(unsigned long *stack, + struct fprobe **fp, unsigned int *size_words) +{ + *fp =3D arch_decode_fprobe_header_fp(*stack); + *size_words =3D arch_decode_fprobe_header_size(*stack); +} + +#else + /* Generic fprobe_header */ struct __fprobe_header { struct fprobe *fp; @@ -173,6 +200,8 @@ static inline void read_fprobe_header(unsigned long *st= ack, *size_words =3D fph->size_words; } =20 +#endif + /* * fprobe shadow stack management: * Since fprobe shares a single fgraph_ops, it needs to share the stack en= try From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D1694594A; Sun, 10 Nov 2024 15:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253904; cv=none; b=AZutfBqwdq/4XWltjtApxrwyXeoYZSLJmRh5xO9vqos6Dr+P0GP6iRxnUdOGiioj3AbKKfjaYxaouRwL1fGattnAsC0OtXQa3HJy/EPG2mziDqyYyx38KiCLHwBB4pl6cI4EmHVQT40BNM1IUME4W2+cyZ/HyAqtYD0nVXdh7h8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253904; c=relaxed/simple; bh=DyIlxK+rzyV5Z9ObEzLIQrZIcPEZL8giKascGhc6cU4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rla+oTsJWYz1kDTor49Dl5htO7Cvhvqs/4iOXFyy+qDHFq8s61luBXP4jQNyIaUNqfUpnx52Ptt0HPlZ1m8GWIZlyaEFGZbremD+tjs+xV//tzK1+2X1Hwsjsa1IeZQgqqLEP7hurl32ak2RyQ+hWsakyoe24Dy9ICWSWMuPKRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AJFqO3rA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AJFqO3rA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6DD6C4CECD; Sun, 10 Nov 2024 15:51:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253904; bh=DyIlxK+rzyV5Z9ObEzLIQrZIcPEZL8giKascGhc6cU4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AJFqO3rAQ5QR50yAiC2utYJMVLLefG/7pFE7WZXB3kERnPWHYHfp4xzAWbhjCMCty 3FxJ3p6IZcZi16Gaisnz76M218XCUYgHxRcJ2x6swP7KtbT2GSNgbHEE7E03TFzgz4 PgaUNf+fweLkmHNQ9MNJa/kBphjyoonkGb195aNCX5/0crthkCw2rcAlBrM/8Vo4p3 8FaKo97Dg/+J8/dJi4G3hPdOq/VGlxlQjigTI0FeR/nGdnSoZxs1qc9QZg4P5auhY0 FsT2/HSA9qcPbuq0XQTU0DXecVGcQGuiVGuzIhSegDfZnBmoNyT9tSxGNawrIVbyUx j0s3+Af7DvHqg== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 14/19] tracing/fprobe: Remove nr_maxactive from fprobe Date: Mon, 11 Nov 2024 00:51:39 +0900 Message-ID: <173125389954.172790.2219734085182975753.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Remove depercated fprobe::nr_maxactive. This involves fprobe events to rejects the maxactive number. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Newly added. --- include/linux/fprobe.h | 2 -- kernel/trace/trace_fprobe.c | 43 ++++++---------------------------------= ---- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/include/linux/fprobe.h b/include/linux/fprobe.h index 91337bcb452f..702099f08929 100644 --- a/include/linux/fprobe.h +++ b/include/linux/fprobe.h @@ -54,7 +54,6 @@ struct fprobe_hlist { * @nmissed: The counter for missing events. * @flags: The status flag. * @entry_data_size: The private data storage size. - * @nr_maxactive: The max number of active functions. (*deprecated) * @entry_handler: The callback function for function entry. * @exit_handler: The callback function for function exit. * @hlist_array: The fprobe_hlist for fprobe search from IP hash table. @@ -63,7 +62,6 @@ struct fprobe { unsigned long nmissed; unsigned int flags; size_t entry_data_size; - int nr_maxactive; =20 fprobe_entry_cb entry_handler; fprobe_exit_cb exit_handler; diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index 41595e38fea5..0a17ed405b6a 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -424,7 +424,6 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, const char *symbol, struct tracepoint *tpoint, struct module *mod, - int maxactive, int nargs, bool is_return) { struct trace_fprobe *tf; @@ -445,7 +444,6 @@ static struct trace_fprobe *alloc_trace_fprobe(const ch= ar *group, =20 tf->tpoint =3D tpoint; tf->mod =3D mod; - tf->fp.nr_maxactive =3D maxactive; =20 ret =3D trace_probe_init(&tf->tp, event, group, false, nargs); if (ret < 0) @@ -1098,12 +1096,11 @@ static int __trace_fprobe_create(int argc, const ch= ar *argv[]) * FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_fprobe *tf =3D NULL; - int i, len, new_argc =3D 0, ret =3D 0; + int i, new_argc =3D 0, ret =3D 0; bool is_return =3D false; char *symbol =3D NULL; const char *event =3D NULL, *group =3D FPROBE_EVENT_SYSTEM; const char **new_argv =3D NULL; - int maxactive =3D 0; char buf[MAX_EVENT_NAME_LEN]; char gbuf[MAX_EVENT_NAME_LEN]; char sbuf[KSYM_NAME_LEN]; @@ -1126,33 +1123,13 @@ static int __trace_fprobe_create(int argc, const ch= ar *argv[]) =20 trace_probe_log_init("trace_fprobe", argc, argv); =20 - event =3D strchr(&argv[0][1], ':'); - if (event) - event++; - - if (isdigit(argv[0][1])) { - if (event) - len =3D event - &argv[0][1] - 1; - else - len =3D strlen(&argv[0][1]); - if (len > MAX_EVENT_NAME_LEN - 1) { - trace_probe_log_err(1, BAD_MAXACT); - goto parse_error; - } - memcpy(buf, &argv[0][1], len); - buf[len] =3D '\0'; - ret =3D kstrtouint(buf, 0, &maxactive); - if (ret || !maxactive) { + if (argv[0][1] !=3D '\0') { + if (argv[0][1] !=3D ':') { + trace_probe_log_set_index(0); trace_probe_log_err(1, BAD_MAXACT); goto parse_error; } - /* fprobe rethook instances are iterated over via a list. The - * maximum should stay reasonable. - */ - if (maxactive > RETHOOK_MAXACTIVE_MAX) { - trace_probe_log_err(1, MAXACT_TOO_BIG); - goto parse_error; - } + event =3D &argv[0][2]; } =20 trace_probe_log_set_index(1); @@ -1162,12 +1139,6 @@ static int __trace_fprobe_create(int argc, const cha= r *argv[]) if (ret < 0) goto parse_error; =20 - if (!is_return && maxactive) { - trace_probe_log_set_index(0); - trace_probe_log_err(1, BAD_MAXACT_TYPE); - goto parse_error; - } - trace_probe_log_set_index(0); if (event) { ret =3D traceprobe_parse_event_name(&event, &group, gbuf, @@ -1231,7 +1202,7 @@ static int __trace_fprobe_create(int argc, const char= *argv[]) =20 /* setup a probe */ tf =3D alloc_trace_fprobe(group, event, symbol, tpoint, tp_mod, - maxactive, argc, is_return); + argc, is_return); if (IS_ERR(tf)) { ret =3D PTR_ERR(tf); /* This must return -ENOMEM, else there is a bug */ @@ -1311,8 +1282,6 @@ static int trace_fprobe_show(struct seq_file *m, stru= ct dyn_event *ev) seq_putc(m, 't'); else seq_putc(m, 'f'); - if (trace_fprobe_is_return(tf) && tf->fp.nr_maxactive) - seq_printf(m, "%d", tf->fp.nr_maxactive); seq_printf(m, ":%s/%s", trace_probe_group_name(&tf->tp), trace_probe_name(&tf->tp)); From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDA914594A; Sun, 10 Nov 2024 15:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253914; cv=none; b=pRs4aAVTpYQOUB6t2e5N/UtbyHja0cgB4dmOZoZwiM5ZgfDXwiWwk5aiylVPf5dDh6wLQzQ/RNLpEa3iMTx2fV7KdsRJqH91IW6wwkblsC6ek6OoSyJfxspzWNOxfec8bfTOFVdwJJ3pTMBtugPH/s95cczKJpiXN1mQwp1JhEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253914; c=relaxed/simple; bh=wZEjksI48uEO6V4KtS7yN1QVU17ztXbJ6mD6JSkXebk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a2oPkj3AEAWsl4PzpQINjxdYIn9R+OO/hBqbdcPCcmIGzHld4/uPdffCJ1+V7Rqg7LlVeqSQ2iqm1qju7wtlH/zkWieNbdMOgqzMNTJPBrC+0te99oKsHStTanP2vExmGK5UGrDkdcimt0v5ULseC+gS93zUi5o2/BqNRA+EWQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oBvygyV/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oBvygyV/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12225C4CECD; Sun, 10 Nov 2024 15:51:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253914; bh=wZEjksI48uEO6V4KtS7yN1QVU17ztXbJ6mD6JSkXebk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oBvygyV/Mjvld0aIkiXZXIflBOJfZRlCAqNUvBK0dOZan07Uqw16bhnmkC3Z/QcIy 27481oNJ1ysDhJJdYcJX3Ul8cO0Ela/l6A6n00UdLBi8FRhY+WeKJhUe6bU0Bg/LDX vZn5Hz99AzI02syK1CFYF5wSUEoK0BZWHVFcmZk+TTAm+P6rzd4dizTN0LRea5gnxE 5RcDF2piiKuSmIHB97e0UXEujC2rs75K0Y6RFolARtr1RimygrX4SJ5zbmyLjvqioK UOhKRhqvK0Uf+4kW73FvkxgrmKXZeejmOevl00kephjz9Gd6GWY+Eg4eyXItburk+u wcbfQgiKjvg4Q== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 15/19] selftests: ftrace: Remove obsolate maxactive syntax check Date: Mon, 11 Nov 2024 00:51:49 +0900 Message-ID: <173125390957.172790.2130011540406916936.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Since the fprobe event does not support maxactive anymore, stop testing the maxactive syntax error checking. Signed-off-by: Masami Hiramatsu (Google) --- .../ftrace/test.d/dynevent/fprobe_syntax_errors.tc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/fprobe_syntax_e= rrors.tc b/tools/testing/selftests/ftrace/test.d/dynevent/fprobe_syntax_err= ors.tc index 61877d166451..c9425a34fae3 100644 --- a/tools/testing/selftests/ftrace/test.d/dynevent/fprobe_syntax_errors.tc +++ b/tools/testing/selftests/ftrace/test.d/dynevent/fprobe_syntax_errors.tc @@ -16,9 +16,7 @@ aarch64) REG=3D%r0 ;; esac =20 -check_error 'f^100 vfs_read' # MAXACT_NO_KPROBE -check_error 'f^1a111 vfs_read' # BAD_MAXACT -check_error 'f^100000 vfs_read' # MAXACT_TOO_BIG +check_error 'f^100 vfs_read' # BAD_MAXACT =20 check_error 'f ^non_exist_func' # BAD_PROBE_ADDR (enoent) check_error 'f ^vfs_read+10' # BAD_PROBE_ADDR From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83771136347; Sun, 10 Nov 2024 15:52:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253925; cv=none; b=hQcAg41z0cDsBUtWTrBOvnUpFOEbzF/rXLAZzEHJZlxaBPY3sMBybV/m6dXVuRQDOTpMQmptOoQ3+djv7F/EPAw5AigvqEzRkBx0ZlnoGaZp8EUUD7Y4TTu457JvzrU+AEJ+f7o5vGdyeUOvCElITk/vt2KKaZY0CqE+cScV2rM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253925; c=relaxed/simple; bh=MTl3WOEWmkU7N1ZYT3IdZA3wW+cclx8nbkw1UKe91tQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jJLxEv1YZW6TTkBee8OvaXgGe9/gW7dMQRi5SgMbxU2rTU99sNTuY6cUScfYJuX+vgHgQNmuvm1eOOFx63GtUzkaWJtGivOyTMXeT5sganbiyq1JA1ULLjK3aTAc1K/Aj2awvojsjBz/nqdHta6+U7eW3hU37HawTIAV8TLBrHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qsAR5RXT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qsAR5RXT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 612ACC4CECD; Sun, 10 Nov 2024 15:52:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253925; bh=MTl3WOEWmkU7N1ZYT3IdZA3wW+cclx8nbkw1UKe91tQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qsAR5RXTmlUzDa7xg1P0UL2FrVnFMevrqWZPCkR5wM7n88lTUaTO/vT6Ghzi7zcAq asrZ3UcbrZvJ+eT215HvI7aJoKOoJ82Rg8uNGv1wVWafW9EYMoDajAryRnKT926C8H BYxFbEtWU0pvrAEY7W861stpoHVx39Q344mU+t9RPDLiTXZMWkTDNRI4oR6hpLGAr+ XjhDQHZXXRI2iFEu1ToBEChxQVfMaDgvnStyA2qw8k0dUpcvQy35UsXa7L9Q7O0inB Wff4enNOeaprg6wdOrCd1pNA2RbhCauQOp19186SnohTP+g9fUWmt6HgxgDnndUq14 ye4uAWliQl7wg== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 16/19] selftests/ftrace: Add a test case for repeating register/unregister fprobe Date: Mon, 11 Nov 2024 00:52:00 +0900 Message-ID: <173125392003.172790.6224691741259126677.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) This test case repeats define and undefine the fprobe dynamic event to ensure that the fprobe does not cause any issue with such operations. Signed-off-by: Masami Hiramatsu (Google) --- .../test.d/dynevent/add_remove_fprobe_repeat.tc | 19 ++++++++++++++++= +++ 1 file changed, 19 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/add_remo= ve_fprobe_repeat.tc diff --git a/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fpro= be_repeat.tc b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fp= robe_repeat.tc new file mode 100644 index 000000000000..b4ad09237e2a --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/dynevent/add_remove_fprobe_repe= at.tc @@ -0,0 +1,19 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Generic dynamic event - Repeating add/remove fprobe events +# requires: dynamic_events "f[:[/][]] [%return] [= ]":README + +echo 0 > events/enable +echo > dynamic_events + +PLACE=3D$FUNCTION_FORK +REPEAT_TIMES=3D64 + +for i in `seq 1 $REPEAT_TIMES`; do + echo "f:myevent $PLACE" >> dynamic_events + grep -q myevent dynamic_events + test -d events/fprobes/myevent + echo > dynamic_events +done + +clear_trace From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 243D013AA53; Sun, 10 Nov 2024 15:52:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253936; cv=none; b=Q1Pwla719DKaaQ8/Y+pgKCpusHUlB7aq7AIaAmknoqsWcXjfy9TmXZHC/mCKM4IgI5zmYJBOtjGms8eG6BY2hPzywqOSbJiRMPATxUs6YFt71BEgywwlZTQuOvtW/UJkq52HwCNO5LOOMnp6VxOeb3qvJcD68FBhgMnuvNritXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253936; c=relaxed/simple; bh=8t7YVZmoVUR7dCk/2nFP4SmQbrkuMwJVZH30PnGw0sc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TDcBUO8T3oSBSaqrlSnROW3sCfSjA2H0i071kUzaedulcGF/NE1DSGJ3IOZIrDmbNbHJtCzUGpPzPZ9GQTLcsNjj002XfUQHGeOQuherN/ZnN4sOtU+9MxLEKywaROShzR7Qfzk+cxwVjdhCiEZD1zBycBsjt/M3S57vJODFVPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tSbcIZyJ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tSbcIZyJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E75CEC4CECD; Sun, 10 Nov 2024 15:52:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253935; bh=8t7YVZmoVUR7dCk/2nFP4SmQbrkuMwJVZH30PnGw0sc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tSbcIZyJLgARbAOdS+pJopHmvO+0dcvkSRcZouRStNw+dKYE2wHYe18Rm44DF2j3P VlS+btt2v8xiVdU2ueTP3MtTdYHl9fDXByRNiGIvUUGN8E8gjQwtxIDB9SRHSjPzSz uDSmSbopSKoIF7pO6g3kpSK6iGVPdW/mgVhrSRuELh2LJJd6+XfDvyrXlM4mMaaJ+h iZfwizIU/UteKqNr1v2QJjTEV1Nx7vRl17we3xte62TjHHTpqsit4bwXVjuT40uhrb Vg8Fk7++DYvcd+KH3XfvgP+BK/9wK7IqCYKn6vTQoUPh07z2OfuhMOWRxMffzo480d Z3kUNSe8rl0lA== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 17/19] Documentation: probes: Update fprobe on function-graph tracer Date: Mon, 11 Nov 2024 00:52:10 +0900 Message-ID: <173125393047.172790.8427492388173333634.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Update fprobe documentation for the new fprobe on function-graph tracer. This includes some bahvior changes and pt_regs to ftrace_regs interface change. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Update @fregs parameter explanation. --- Documentation/trace/fprobe.rst | 42 ++++++++++++++++++++++++++----------= ---- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Documentation/trace/fprobe.rst b/Documentation/trace/fprobe.rst index 196f52386aaa..f58bdc64504f 100644 --- a/Documentation/trace/fprobe.rst +++ b/Documentation/trace/fprobe.rst @@ -9,9 +9,10 @@ Fprobe - Function entry/exit probe Introduction =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 -Fprobe is a function entry/exit probe mechanism based on ftrace. -Instead of using ftrace full feature, if you only want to attach callbacks -on function entry and exit, similar to the kprobes and kretprobes, you can +Fprobe is a function entry/exit probe mechanism based on the function-graph +tracer. +Instead of tracing all functions, if you want to attach callbacks on speci= fic +function entry and exit, similar to the kprobes and kretprobes, you can use fprobe. Compared with kprobes and kretprobes, fprobe gives faster instrumentation for multiple functions with single handler. This document describes how to use fprobe. @@ -91,12 +92,14 @@ The prototype of the entry/exit callback function are a= s follows: =20 .. code-block:: c =20 - int entry_callback(struct fprobe *fp, unsigned long entry_ip, unsigned lo= ng ret_ip, struct pt_regs *regs, void *entry_data); + int entry_callback(struct fprobe *fp, unsigned long entry_ip, unsigned lo= ng ret_ip, struct ftrace_regs *fregs, void *entry_data); =20 - void exit_callback(struct fprobe *fp, unsigned long entry_ip, unsigned lo= ng ret_ip, struct pt_regs *regs, void *entry_data); + void exit_callback(struct fprobe *fp, unsigned long entry_ip, unsigned lo= ng ret_ip, struct ftrace_regs *fregs, void *entry_data); =20 -Note that the @entry_ip is saved at function entry and passed to exit hand= ler. -If the entry callback function returns !0, the corresponding exit callback= will be cancelled. +Note that the @entry_ip is saved at function entry and passed to exit +handler. +If the entry callback function returns !0, the corresponding exit callback +will be cancelled. =20 @fp This is the address of `fprobe` data structure related to this han= dler. @@ -112,12 +115,10 @@ If the entry callback function returns !0, the corres= ponding exit callback will This is the return address that the traced function will return to, somewhere in the caller. This can be used at both entry and exit. =20 -@regs - This is the `pt_regs` data structure at the entry and exit. Note t= hat - the instruction pointer of @regs may be different from the @entry_= ip - in the entry_handler. If you need traced instruction pointer, you = need - to use @entry_ip. On the other hand, in the exit_handler, the inst= ruction - pointer of @regs is set to the current return address. +@fregs + This is the `ftrace_regs` data structure at the entry and exit. Th= is + includes the function parameters, or the return values. So user can + access thos values via appropriate `ftrace_regs_*` APIs. =20 @entry_data This is a local storage to share the data between entry and exit h= andlers. @@ -125,6 +126,17 @@ If the entry callback function returns !0, the corresp= onding exit callback will and `entry_data_size` field when registering the fprobe, the stora= ge is allocated and passed to both `entry_handler` and `exit_handler`. =20 +Entry data size and exit handlers on the same function +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D + +Since the entry data is passed via per-task stack and it is has limited si= ze, +the entry data size per probe is limited to `15 * sizeof(long)`. You also = need +to take care that the different fprobes are probing on the same function, = this +limit becomes smaller. The entry data size is aligned to `sizeof(long)` and +each fprobe which has exit handler uses a `sizeof(long)` space on the stac= k, +you should keep the number of fprobes on the same function as small as +possible. + Share the callbacks with kprobes =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -165,8 +177,8 @@ This counter counts up when; - fprobe fails to take ftrace_recursion lock. This usually means that a f= unction which is traced by other ftrace users is called from the entry_handler. =20 - - fprobe fails to setup the function exit because of the shortage of reth= ook - (the shadow stack for hooking the function return.) + - fprobe fails to setup the function exit because of failing to allocate = the + data buffer from the per-task shadow stack. =20 The `fprobe::nmissed` field counts up in both cases. Therefore, the former skips both of entry and exit callback and the latter skips the exit From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FFDF13B2BB; Sun, 10 Nov 2024 15:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253946; cv=none; b=UwSlu+neKg0wu/UHJ+Np4eOncFbG0kN74MZ2Hai83i5q1hC4HkvMfNDdGG2a60jcp826iBmfhoZ1wVWktDH+5rfirdT5BMvzfsN4TES56LTrcAVH0xA7hggOHBUggWTnzzYVP+gJhQZcswU+XXvPvI+SnutPphmHdWo88I/Fpm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253946; c=relaxed/simple; bh=ljV+7i1gHE0GvTLzOVr5+Punuc1M73hF7pzOK4hFm44=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Yl5XEHKXOi0M487ZPN+jCU81OR1FqwoduD6dXNEBlXZzPVAe4KRoJUt4dRXCUqkhr6Fjx+h2TMXpQkmg8E1MKCyHnjIZtFkHSLXjqhgd0vxyYonXkhVLO9qUN6FehUBG05LtuUe9Vua4b1IFUfAw5DmDH2ua1rW6mTGo/DPvVRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Lin1a3x3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Lin1a3x3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 461D5C4CECD; Sun, 10 Nov 2024 15:52:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253946; bh=ljV+7i1gHE0GvTLzOVr5+Punuc1M73hF7pzOK4hFm44=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lin1a3x3MSmgk9wJ/NIjgm7RnFE4+WmlXVL2U66a+fB0n5PeT0BwBbla/o28K+Boj yssTIfvtB2Ax6W8DL7uKoeIULorJTjpNp18r7bXvTFfpsUlBZ+llnka4fmKrqsAXam NAo+DzTU/+Z+/8b3Kl/RWcGSi1zvhIZ+3VgAEP3lSdtusLkO8hUNC+S5VQhJNZAOwQ O5NPICO5WFjg5BjJ73E9kd0dXY9WLl9MNNjNL7BUkKHdyR3gCp1ynSED4SqVhJWPNf 3MwF3leOKIVisU4pmsCZ04IxXL1z+SDE3ztPs7Xa1WXg4/PznTKNePwPYA4kzeBnC7 4KNK67sSPfL5w== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 18/19] ftrace: Add ftrace_get_symaddr to convert fentry_ip to symaddr Date: Mon, 11 Nov 2024 00:52:21 +0900 Message-ID: <173125394102.172790.7669548166614384865.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) This introduces ftrace_get_symaddr() which tries to convert fentry_ip passed by ftrace or fgraph callback to symaddr without calling kallsyms API. It returns the symbol address or 0 if it fails to convert it. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v19: - Newly added. --- arch/arm64/include/asm/ftrace.h | 2 + arch/arm64/kernel/ftrace.c | 63 +++++++++++++++++++++++++++++++++++= ++++ arch/x86/include/asm/ftrace.h | 21 +++++++++++++ include/linux/ftrace.h | 13 ++++++++ 4 files changed, 99 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index 876e88ad4119..f08e70bf09ea 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -52,6 +52,8 @@ extern unsigned long ftrace_graph_call; extern void return_to_handler(void); =20 unsigned long ftrace_call_adjust(unsigned long addr); +unsigned long arch_ftrace_call_adjust(unsigned long fentry_ip); +#define ftrace_call_adjust(fentry_ip) arch_ftrace_call_adjust(fentry_ip) =20 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS #define HAVE_ARCH_FTRACE_REGS diff --git a/arch/arm64/kernel/ftrace.c b/arch/arm64/kernel/ftrace.c index 606fd6994578..de1223669758 100644 --- a/arch/arm64/kernel/ftrace.c +++ b/arch/arm64/kernel/ftrace.c @@ -143,6 +143,69 @@ unsigned long ftrace_call_adjust(unsigned long addr) return addr; } =20 +/* Convert fentry_ip to the symbol address without kallsyms */ +unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip) +{ + u32 insn; + + /* + * When using patchable-function-entry without pre-function NOPS, ftrace + * entry is the address of the first NOP after the function entry point. + * + * The compiler has either generated: + * + * func+00: func: NOP // To be patched to MOV X9, LR + * func+04: NOP // To be patched to BL + * + * Or: + * + * func-04: BTI C + * func+00: func: NOP // To be patched to MOV X9, LR + * func+04: NOP // To be patched to BL + * + * The fentry_ip is the address of `BL ` which is at `func + 4` + * bytes in either case. + */ + if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS)) + return fentry_ip - AARCH64_INSN_SIZE; + + /* + * When using patchable-function-entry with pre-function NOPs, BTI is + * a bit different. + * + * func+00: func: NOP // To be patched to MOV X9, LR + * func+04: NOP // To be patched to BL + * + * Or: + * + * func+00: func: BTI C + * func+04: NOP // To be patched to MOV X9, LR + * func+08: NOP // To be patched to BL + * + * The fentry_ip is the address of `BL ` which is at either + * `func + 4` or `func + 8` depends on whether there is a BTI. + */ + + /* If there is no BTI, the func address should be one instruction before.= */ + if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) + return fentry_ip - AARCH64_INSN_SIZE; + + /* We want to be extra safe in case entry ip is on the page edge, + * but otherwise we need to avoid get_kernel_nofault()'s overhead. + */ + if ((fentry_ip & ~PAGE_MASK) < AARCH64_INSN_SIZE * 2) { + if (get_kernel_nofault(insn, (u32 *)(fentry_ip - AARCH64_INSN_SIZE * 2))) + return 0; + } else { + insn =3D *(u32 *)(fentry_ip - AARCH64_INSN_SIZE * 2); + } + + if (aarch64_insn_is_bti(le32_to_cpu((__le32)insn))) + return fentry_ip - AARCH64_INSN_SIZE * 2; + + return fentry_ip - AARCH64_INSN_SIZE; +} + /* * Replace a single instruction, which may be a branch or NOP. * If @validate =3D=3D true, a replaced instruction is checked against 'ol= d'. diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index cc92c99ef276..f9cb4d07df58 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -34,6 +34,27 @@ static inline unsigned long ftrace_call_adjust(unsigned = long addr) return addr; } =20 +static inline unsigned long arch_ftrace_get_symaddr(unsigned long fentry_i= p) +{ +#ifdef CONFIG_X86_KERNEL_IBT + u32 instr; + + /* We want to be extra safe in case entry ip is on the page edge, + * but otherwise we need to avoid get_kernel_nofault()'s overhead. + */ + if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) { + if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE))) + return fentry_ip; + } else { + instr =3D *(u32 *)(fentry_ip - ENDBR_INSN_SIZE); + } + if (is_endbr(instr)) + fentry_ip -=3D ENDBR_INSN_SIZE; +#endif + return fentry_ip; +} +#define ftrace_get_symaddr(fentry_ip) arch_ftrace_get_symaddr(fentry_ip) + #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS =20 #include diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 4c553fe9c026..9659bb2cd76c 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -652,6 +652,19 @@ struct ftrace_ops *ftrace_ops_trampoline(unsigned long= addr); =20 bool is_ftrace_trampoline(unsigned long addr); =20 +/* Arches can override ftrace_get_symaddr() to convert fentry_ip to symadd= r. */ +#ifndef ftrace_get_symaddr +/** + * ftrace_get_symaddr - return the symbol address from fentry_ip + * @fentry_ip: the address of ftrace location + * + * Get the symbol address from @fentry_ip (fast path). If there is no fast + * search path, this returns 0. + * User may need to use kallsyms API to find the symbol address. + */ +#define ftrace_get_symaddr(fentry_ip) (0) +#endif + /* * The dyn_ftrace record's flags field is split into two parts. * the first part which is '0-FTRACE_REF_MAX' is a counter of From nobody Mon Nov 25 13:17:19 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F4BA13DDDD; Sun, 10 Nov 2024 15:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253957; cv=none; b=mXwXDY10EEnWx0KVm12d7RIjXTOBvtc0EvNxOXRfHKzwUyHs1DUjA2CUYEGtl2W7HQw06umAn85ocDe4hLmlfVlYPJEjJDWMPi8aJXNGvp7bf7Xyi1wy/QPTJ58E5PKEeoEKBl/bbfSUCagAH9qLwwLNP3Kwslfp6G8IYSJYZCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253957; c=relaxed/simple; bh=kzuMS+tyBQSahtSqkEji9Egz79KXjp5UmvcHMUaWrrg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gJvNcfERjNXr7W/RM5NdyR1zvZ6tj1d1meN4xNMb2HvV9In067SdGhDhyVTO3rFvCKJeC6nNhCaZFA2n+MRKa0zYYMXrQqf0F5sycgPRD7oo/PbTap5oZHp6OOkcExkzDM7SpzOpAK4Rh/a3DC7q5f4I6R9XGqMwaqr0/gATsHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CBRu+jJa; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CBRu+jJa" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E562FC4CECF; Sun, 10 Nov 2024 15:52:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253956; bh=kzuMS+tyBQSahtSqkEji9Egz79KXjp5UmvcHMUaWrrg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CBRu+jJaXPo18SnMpvvYDfvy7jg5vEN6mgf0tmmvOXqbh3FCr7CQM0FhL48yUGbmv jOj8CfVIE0cmu8+WsD2inq5k/um9mUmZDb6s19oWyJvyHWlQxKaE5Fh8tbVXqBynJf zIBgUZB0Dbvdqeslp83y77m0Qk4lXD7vovZr8NAJv0B6BN/HXGNRRZpeuL2akGGSFu +4r+F/r9RGDwokJt6VtZhv3VLzX1oUvYBbcOsuQEkmvDSzKF6ymgYzTFYSHWrsQbyP 4ZLwY80bgMqcXqOC5yyJMi9JrVx+hcTKvW22KYR+kEG7eBoQkGiF/6eZ+fiGYCJbj/ FCPdNfXSqeU6w== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 19/19] bpf: Use ftrace_get_symaddr() in get_entry_ip() Date: Mon, 11 Nov 2024 00:52:31 +0900 Message-ID: <173125395146.172790.15945895464150788842.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Rewrite get_entry_ip() to use ftrace_get_symaddr() macro. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v19: - Use ftrace_get_symaddr() instead of introducing new arch dependent code. - Also, replace x86 code with ftrace_get_symaddr(), which does the same thing. --- kernel/trace/bpf_trace.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 1532e9172bf9..e848a782bc8d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1024,27 +1024,12 @@ static const struct bpf_func_proto bpf_get_func_ip_= proto_tracing =3D { .arg1_type =3D ARG_PTR_TO_CTX, }; =20 -#ifdef CONFIG_X86_KERNEL_IBT static unsigned long get_entry_ip(unsigned long fentry_ip) { - u32 instr; + unsigned long ret =3D ftrace_get_symaddr(fentry_ip); =20 - /* We want to be extra safe in case entry ip is on the page edge, - * but otherwise we need to avoid get_kernel_nofault()'s overhead. - */ - if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) { - if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE))) - return fentry_ip; - } else { - instr =3D *(u32 *)(fentry_ip - ENDBR_INSN_SIZE); - } - if (is_endbr(instr)) - fentry_ip -=3D ENDBR_INSN_SIZE; - return fentry_ip; + return ret ? : fentry_ip; } -#else -#define get_entry_ip(fentry_ip) fentry_ip -#endif =20 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs) {