2024-05-27 10:26:22

by Tatsuya S

[permalink] [raw]
Subject: [PATCH v2] ftrace: Fix stack trace entry generated by ftrace_pid_func()

On setting set_ftrace_pid, a extra entry generated by ftrace_pid_func()
is shown on stack trace(CONFIG_UNWINDER_FRAME_POINTER=y).

[004] ..... 68.459382: <stack trace>
=> 0xffffffffa00090af
=> ksys_read
=> __x64_sys_read
=> x64_sys_call
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe

To resolve this issue, increment skip count
in function_stack_trace_call() if pids are set.

Signed-off-by: Tatsuya S <[email protected]>
---
Changes in v2:
- Fix build warnings reported by kernel test robot
- Link to v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

include/linux/ftrace.h | 2 ++
kernel/trace/ftrace.c | 2 +-
kernel/trace/trace_functions.c | 7 ++++++-
3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
index 800995c425e0..0855dfe768eb 100644
--- a/include/linux/ftrace.h
+++ b/include/linux/ftrace.h
@@ -913,6 +913,8 @@ static inline bool is_ftrace_trampoline(unsigned long addr)
/* totally disable ftrace - can not re-enable after this */
void ftrace_kill(void);

+bool ftrace_pids_enabled(struct ftrace_ops *ops);
+
static inline void tracer_disable(void)
{
#ifdef CONFIG_FUNCTION_TRACER
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 65208d3b5ed9..e8ddd56d1e55 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -99,7 +99,7 @@ struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
/* What to set function_trace_op to */
static struct ftrace_ops *set_function_trace_op;

-static bool ftrace_pids_enabled(struct ftrace_ops *ops)
+bool ftrace_pids_enabled(struct ftrace_ops *ops)
{
struct trace_array *tr;

diff --git a/kernel/trace/trace_functions.c b/kernel/trace/trace_functions.c
index 9f1bfbe105e8..455c9a880199 100644
--- a/kernel/trace/trace_functions.c
+++ b/kernel/trace/trace_functions.c
@@ -223,6 +223,7 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
long disabled;
int cpu;
unsigned int trace_ctx;
+ int skip = STACK_SKIP;

if (unlikely(!tr->function_enabled))
return;
@@ -239,7 +240,11 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
if (likely(disabled == 1)) {
trace_ctx = tracing_gen_ctx_flags(flags);
trace_function(tr, ip, parent_ip, trace_ctx);
- __trace_stack(tr, trace_ctx, STACK_SKIP);
+#ifdef CONFIG_UNWINDER_FRAME_POINTER
+ if (ftrace_pids_enabled(op))
+ skip++;
+#endif
+ __trace_stack(tr, trace_ctx, skip);
}

atomic_dec(&data->disabled);
--
2.45.1



2024-05-27 23:49:23

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v2] ftrace: Fix stack trace entry generated by ftrace_pid_func()

On Mon, 27 May 2024 18:44:56 +0900
Tatsuya S <[email protected]> wrote:

> On setting set_ftrace_pid, a extra entry generated by ftrace_pid_func()
> is shown on stack trace(CONFIG_UNWINDER_FRAME_POINTER=y).
>
> [004] ..... 68.459382: <stack trace>
> => 0xffffffffa00090af
> => ksys_read
> => __x64_sys_read
> => x64_sys_call
> => do_syscall_64
> => entry_SYSCALL_64_after_hwframe
>
> To resolve this issue, increment skip count
> in function_stack_trace_call() if pids are set.

Just a note, this isn't a "fix" but simply an improvement in output.
I'm happy to take this (after testing and more reviewing), but it will
be for the next merge window, and with a different subject line.

"ftrace: Hide one more entry in stack trace when ftrace_pid is enabled"

Or something like that.

Thanks,

-- Steve

2024-05-28 02:57:04

by Tatsuya S

[permalink] [raw]
Subject: Re: [PATCH v2] ftrace: Fix stack trace entry generated by ftrace_pid_func()

On Mon, May 27, 2024 at 07:49:14PM GMT, Steven Rostedt wrote:
> On Mon, 27 May 2024 18:44:56 +0900
> Tatsuya S <[email protected]> wrote:
>
> > On setting set_ftrace_pid, a extra entry generated by ftrace_pid_func()
> > is shown on stack trace(CONFIG_UNWINDER_FRAME_POINTER=y).
> >
> > [004] ..... 68.459382: <stack trace>
> > => 0xffffffffa00090af
> > => ksys_read
> > => __x64_sys_read
> > => x64_sys_call
> > => do_syscall_64
> > => entry_SYSCALL_64_after_hwframe
> >
> > To resolve this issue, increment skip count
> > in function_stack_trace_call() if pids are set.
>
> Just a note, this isn't a "fix" but simply an improvement in output.
> I'm happy to take this (after testing and more reviewing), but it will
> be for the next merge window, and with a different subject line.
>
> "ftrace: Hide one more entry in stack trace when ftrace_pid is enabled"
>
> Or something like that.
>
> Thanks,
>
> -- Steve
I will send patch with new subject line.

Thank you.

Tatsuya