Message ID | 20230324134958.2496891-2-mark.rutland@arm.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | arm64: stacktrace: cleanups | expand |
On Fri, Mar 24, 2023 at 6:50 AM Mark Rutland <mark.rutland@arm.com> wrote: > > The function which calls the top-level backtracing function may have > been instrumented with ftrace and/or kprobes, and hence the first return > address may have been rewritten. > > Factor out the existing fgraph / kretprobes address recovery, and use > this for the first address. As the comment for the fgraph case isn't all > that helpful, I've also dropped that. > > Signed-off-by: Mark Rutland <mark.rutland@arm.com> > Cc: Catalin Marinas <catalin.marinas@arm.com> > Cc: Kalesh Singh <kaleshsingh@google.com> > Cc: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> > Cc: Mark Brown <broonie@kernel.org> > Cc: Will Deacon <will@kernel.org> Reviewed-by: Kalesh Singh <kaleshsingh@google.com> Thanks, Kalesh > --- > arch/arm64/kernel/stacktrace.c | 52 +++++++++++++++++++--------------- > 1 file changed, 29 insertions(+), 23 deletions(-) > > diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c > index 83154303e682c..219ce0668a3dd 100644 > --- a/arch/arm64/kernel/stacktrace.c > +++ b/arch/arm64/kernel/stacktrace.c > @@ -69,6 +69,31 @@ static __always_inline void unwind_init_from_task(struct unwind_state *state, > state->pc = thread_saved_pc(task); > } > > +static __always_inline int > +unwind_recover_return_address(struct unwind_state *state) > +{ > + struct task_struct *tsk = state->task; > + > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER > + if (tsk->ret_stack && > + (state->pc == (unsigned long)return_to_handler)) { > + unsigned long orig_pc; > + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, > + (void *)state->fp); > + if (WARN_ON_ONCE(state->pc == orig_pc)) > + return -EINVAL; > + state->pc = orig_pc; > + } > +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > + > +#ifdef CONFIG_KRETPROBES > + if (is_kretprobe_trampoline(state->pc)) > + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); > +#endif /* CONFIG_KRETPROBES */ > + > + return 0; > +} > + > /* > * Unwind from one frame record (A) to the next frame record (B). > * > @@ -92,35 +117,16 @@ static int notrace unwind_next(struct unwind_state *state) > > state->pc = ptrauth_strip_insn_pac(state->pc); > > -#ifdef CONFIG_FUNCTION_GRAPH_TRACER > - if (tsk->ret_stack && > - (state->pc == (unsigned long)return_to_handler)) { > - unsigned long orig_pc; > - /* > - * This is a case where function graph tracer has > - * modified a return address (LR) in a stack frame > - * to hook a function return. > - * So replace it to an original value. > - */ > - orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, > - (void *)state->fp); > - if (WARN_ON_ONCE(state->pc == orig_pc)) > - return -EINVAL; > - state->pc = orig_pc; > - } > -#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > -#ifdef CONFIG_KRETPROBES > - if (is_kretprobe_trampoline(state->pc)) > - state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); > -#endif > - > - return 0; > + return unwind_recover_return_address(state); > } > NOKPROBE_SYMBOL(unwind_next); > > static void notrace unwind(struct unwind_state *state, > stack_trace_consume_fn consume_entry, void *cookie) > { > + if (unwind_recover_return_address(state)) > + return; > + > while (1) { > int ret; > > -- > 2.30.2 >
On Fri, Mar 24, 2023 at 01:49:56PM +0000, Mark Rutland wrote: > The function which calls the top-level backtracing function may have > been instrumented with ftrace and/or kprobes, and hence the first return > address may have been rewritten. > > Factor out the existing fgraph / kretprobes address recovery, and use > this for the first address. As the comment for the fgraph case isn't all > that helpful, I've also dropped that. > > Signed-off-by: Mark Rutland <mark.rutland@arm.com> > Cc: Catalin Marinas <catalin.marinas@arm.com> > Cc: Kalesh Singh <kaleshsingh@google.com> > Cc: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> > Cc: Mark Brown <broonie@kernel.org> > Cc: Will Deacon <will@kernel.org> > --- > arch/arm64/kernel/stacktrace.c | 52 +++++++++++++++++++--------------- > 1 file changed, 29 insertions(+), 23 deletions(-) > > diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c > index 83154303e682c..219ce0668a3dd 100644 > --- a/arch/arm64/kernel/stacktrace.c > +++ b/arch/arm64/kernel/stacktrace.c > @@ -69,6 +69,31 @@ static __always_inline void unwind_init_from_task(struct unwind_state *state, > state->pc = thread_saved_pc(task); > } > > +static __always_inline int > +unwind_recover_return_address(struct unwind_state *state) > +{ > + struct task_struct *tsk = state->task; > + > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER > + if (tsk->ret_stack && > + (state->pc == (unsigned long)return_to_handler)) { > + unsigned long orig_pc; > + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, > + (void *)state->fp); > + if (WARN_ON_ONCE(state->pc == orig_pc)) > + return -EINVAL; > + state->pc = orig_pc; > + } > +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > + > +#ifdef CONFIG_KRETPROBES > + if (is_kretprobe_trampoline(state->pc)) > + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); > +#endif /* CONFIG_KRETPROBES */ > + > + return 0; > +} This generates an unused variable warning with defconfig: arch/arm64/kernel/stacktrace.c: In function ‘unwind_recover_return_address’: arch/arm64/kernel/stacktrace.c:78:22: warning: unused variable ‘tsk’ [-Wunused-variable] 78 | struct task_struct *tsk = state->task; | ^~~ Will
On Thu, Apr 06, 2023 at 04:25:50PM +0100, Will Deacon wrote: > On Fri, Mar 24, 2023 at 01:49:56PM +0000, Mark Rutland wrote: > > +static __always_inline int > > +unwind_recover_return_address(struct unwind_state *state) > > +{ > > + struct task_struct *tsk = state->task; > > + > > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER > > + if (tsk->ret_stack && > > + (state->pc == (unsigned long)return_to_handler)) { > > + unsigned long orig_pc; > > + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, > > + (void *)state->fp); > > + if (WARN_ON_ONCE(state->pc == orig_pc)) > > + return -EINVAL; > > + state->pc = orig_pc; > > + } > > +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > > + > > +#ifdef CONFIG_KRETPROBES > > + if (is_kretprobe_trampoline(state->pc)) > > + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); > > +#endif /* CONFIG_KRETPROBES */ > > + > > + return 0; > > +} > > This generates an unused variable warning with defconfig: > > arch/arm64/kernel/stacktrace.c: In function ‘unwind_recover_return_address’: > arch/arm64/kernel/stacktrace.c:78:22: warning: unused variable ‘tsk’ [-Wunused-variable] > 78 | struct task_struct *tsk = state->task; > | ^~~ Sorry about that; I had tested with and without each of CONFIG_FUNCTION_GRAPH_TRACER and CONFIG_KRETPROBES, but hadn't tested without both of them. I'll get rid of `tsk` and use `state->task` directly in each case for v2. Thanks, Mark.
diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 83154303e682c..219ce0668a3dd 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -69,6 +69,31 @@ static __always_inline void unwind_init_from_task(struct unwind_state *state, state->pc = thread_saved_pc(task); } +static __always_inline int +unwind_recover_return_address(struct unwind_state *state) +{ + struct task_struct *tsk = state->task; + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + if (tsk->ret_stack && + (state->pc == (unsigned long)return_to_handler)) { + unsigned long orig_pc; + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, + (void *)state->fp); + if (WARN_ON_ONCE(state->pc == orig_pc)) + return -EINVAL; + state->pc = orig_pc; + } +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ + +#ifdef CONFIG_KRETPROBES + if (is_kretprobe_trampoline(state->pc)) + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); +#endif /* CONFIG_KRETPROBES */ + + return 0; +} + /* * Unwind from one frame record (A) to the next frame record (B). * @@ -92,35 +117,16 @@ static int notrace unwind_next(struct unwind_state *state) state->pc = ptrauth_strip_insn_pac(state->pc); -#ifdef CONFIG_FUNCTION_GRAPH_TRACER - if (tsk->ret_stack && - (state->pc == (unsigned long)return_to_handler)) { - unsigned long orig_pc; - /* - * This is a case where function graph tracer has - * modified a return address (LR) in a stack frame - * to hook a function return. - * So replace it to an original value. - */ - orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, - (void *)state->fp); - if (WARN_ON_ONCE(state->pc == orig_pc)) - return -EINVAL; - state->pc = orig_pc; - } -#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ -#ifdef CONFIG_KRETPROBES - if (is_kretprobe_trampoline(state->pc)) - state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); -#endif - - return 0; + return unwind_recover_return_address(state); } NOKPROBE_SYMBOL(unwind_next); static void notrace unwind(struct unwind_state *state, stack_trace_consume_fn consume_entry, void *cookie) { + if (unwind_recover_return_address(state)) + return; + while (1) { int ret;
The function which calls the top-level backtracing function may have been instrumented with ftrace and/or kprobes, and hence the first return address may have been rewritten. Factor out the existing fgraph / kretprobes address recovery, and use this for the first address. As the comment for the fgraph case isn't all that helpful, I've also dropped that. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Kalesh Singh <kaleshsingh@google.com> Cc: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> Cc: Mark Brown <broonie@kernel.org> Cc: Will Deacon <will@kernel.org> --- arch/arm64/kernel/stacktrace.c | 52 +++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 23 deletions(-)