Skip to contentSkip to conversation

ftrace: Hide a extra entry in stack trace

PATCH Superseded Tatsuya S 3 participants 7 messages Sep 26, 2024 – Oct 2, 2024 Tracing

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tatsuya S

author
#0 lore raw

PATCH v2 1/1 ftrace: Hide a extra entry in stack trace

A extra entry is shown on stack trace(CONFIG_UNWINDER_ORC=y).

    [003] .....   110.171589: vfs_write <-__x64_sys_write
    [003] .....   110.171600: <stack trace>

=> XXXXXXXXX (Wrong function name)
=> vfs_write
=> __x64_sys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe

To resolve this, increment skip in __ftrace_trace_stack(). The reason why skip is incremented in __ftrace_trace_stack() is because __ftrace_trace_stack() in stack trace is the only function that wasn't skipped from anywhere.

---
 kernel/trace/trace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace.c +1 -3
index c3b2c7dfadef..0f2e255f563c 100644
@@ -2916,10 +2916,8 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,
 	 * Add one, for this function and the call to save_stack_trace()
 	 * If regs is set, then these functions will not be in the way.
 	 */
-#ifndef CONFIG_UNWINDER_ORC
-	if (!regs)
+	if (IS_ENABLED(CONFIG_UNWINDER_ORC) || !regs)
 		skip++;
-#endif
 
 	preempt_disable_notrace();
 
Signature · 1 line
2.46.1
#1 lore raw
Quoted 9 lines · On Thu, 26 Sep 2024 15:13:07 +0900 Tatsuya S <[email protected]> wrote:

A extra entry is shown on stack trace(CONFIG_UNWINDER_ORC=y).

    [003] .....   110.171589: vfs_write <-__x64_sys_write
    [003] .....   110.171600: <stack trace>

=> XXXXXXXXX (Wrong function name)
=> vfs_write
=> __x64_sys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe

OK, I confirmed it;

------
echo 1 > options/func_stack_trace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo > /dev/null
cat trace

              sh-136     [005] .....   266.884180: vfs_write <-ksys_write
              sh-136     [005] .....   266.884188: <stack trace>

=> 0xffffffffa0004099
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------

Quoted 16 lines

To resolve this, increment skip in __ftrace_trace_stack(). The reason why skip is incremented in __ftrace_trace_stack() is because __ftrace_trace_stack() in stack trace is the only function that wasn't skipped from anywhere.

---
 kernel/trace/trace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace.c +0 -0
index c3b2c7dfadef..0f2e255f563c 100644

kernel/trace/trace.c static void __ftrace_trace_stack(struct trace_buffer *buffer, · line 2918

Quoted 3 lines
kernel/trace/trace.c +0 -0
@@ -2916,10 +2916,8 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,
 	 * Add one, for this function and the call to save_stack_trace()
 	 * If regs is set, then these functions will not be in the way.
 	 */

Hmm, with this change, the above comment should also be updated.

kernel/trace/trace.c static void __ftrace_trace_stack(struct trace_buffer *buffer, · line 2922

Quoted 5 lines
kernel/trace/trace.c +1 -3
-#ifndef CONFIG_UNWINDER_ORC
-	if (!regs)
+	if (IS_ENABLED(CONFIG_UNWINDER_ORC) || !regs)
 		skip++;
-#endif

Also, this solves just one pattern (only enable function tracer) but if there are fprobes (or kprobes) on the same function, it introduces another issue. e.g. (with this fix)

------
echo 1 > options/func_stack_trace
echo 1 > options/stacktrace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo "f:myevent vfs_write" > dynamic_events
echo 1 > events/fprobes/myevent/enable
echo > /dev/null
cat trace
...

              sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560)
              sh-140     [001] ...1.    18.352602: <stack trace>
 => ksys_write
 => do_syscall_64
 => entry_SYSCALL_64_after_hwframe
              sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write
              sh-140     [001] ...1.    18.352604: <stack trace>

=> ftrace_regs_call
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Thank you,

Quoted 6 lines
kernel/trace/trace.c +0 -0
 	preempt_disable_notrace();
 
Signature · 1 line
2.46.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#2 lore raw
Quoted 76 lines · Masami Hiramatsu (Google)
Quoted 9 lines · On Thu, 26 Sep 2024 15:13:07 +0900 Tatsuya S<[email protected]> wrote:

A extra entry is shown on stack trace(CONFIG_UNWINDER_ORC=y).

     [003] .....   110.171589: vfs_write <-__x64_sys_write
     [003] .....   110.171600: <stack trace>

=> XXXXXXXXX (Wrong function name)
=> vfs_write
=> __x64_sys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe

OK, I confirmed it;

------
echo 1 > options/func_stack_trace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo > /dev/null
cat trace

               sh-136     [005] .....   266.884180: vfs_write <-ksys_write
               sh-136     [005] .....   266.884188: <stack trace>

=> 0xffffffffa0004099
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------

Quoted 15 lines

To resolve this, increment skip in __ftrace_trace_stack(). The reason why skip is incremented in __ftrace_trace_stack() is because __ftrace_trace_stack() in stack trace is the only function that wasn't skipped from anywhere.

---
  kernel/trace/trace.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace.c +0 -0
index c3b2c7dfadef..0f2e255f563c 100644

kernel/trace/trace.c static void __ftrace_trace_stack(struct trace_buffer *buffer, · line 2918

Quoted 3 lines
kernel/trace/trace.c +0 -0
@@ -2916,10 +2916,8 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,
  	 * Add one, for this function and the call to save_stack_trace()
  	 * If regs is set, then these functions will not be in the way.
  	 */

Hmm, with this change, the above comment should also be updated.

kernel/trace/trace.c static void __ftrace_trace_stack(struct trace_buffer *buffer, · line 2922

Quoted 5 lines
kernel/trace/trace.c +1 -3
-#ifndef CONFIG_UNWINDER_ORC
-	if (!regs)
+	if (IS_ENABLED(CONFIG_UNWINDER_ORC) || !regs)
  		skip++;
-#endif

Also, this solves just one pattern (only enable function tracer) but if there are fprobes (or kprobes) on the same function, it introduces another issue. e.g. (with this fix)

------
echo 1 > options/func_stack_trace
echo 1 > options/stacktrace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo "f:myevent vfs_write" > dynamic_events
echo 1 > events/fprobes/myevent/enable
echo > /dev/null
cat trace
...

               sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560)
               sh-140     [001] ...1.    18.352602: <stack trace>
  => ksys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe
               sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write
               sh-140     [001] ...1.    18.352604: <stack trace>
Quoted 8 lines

=> ftrace_regs_call
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Thanks for the other tests. This issue may be function_trace_call() specific problem.

So I will change the place to increment skip number.

Quoted 8 lines

Thank you,

Quoted 6 lines
kernel/trace/trace.c +0 -0
  	preempt_disable_notrace();
 
Signature · 1 line
2.46.1

Thank you,

author
#3 lore raw
Quoted 88 lines · ts
Quoted 77 lines · Masami Hiramatsu (Google)
Quoted 9 lines · On Thu, 26 Sep 2024 15:13:07 +0900 Tatsuya S<[email protected]> wrote:

A extra entry is shown on stack trace(CONFIG_UNWINDER_ORC=y).

     [003] .....   110.171589: vfs_write <-__x64_sys_write
     [003] .....   110.171600: <stack trace>
  => XXXXXXXXX (Wrong function name)
  => vfs_write
  => __x64_sys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe

OK, I confirmed it;

------
echo 1 > options/func_stack_trace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo > /dev/null
cat trace
               sh-136     [005] .....   266.884180: vfs_write <-ksys_write sh-136     [005] .....   266.884188: <stack trace> => 0xffffffffa0004099
  => vfs_write
  => ksys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe
------

Quoted 15 lines

To resolve this, increment skip in __ftrace_trace_stack(). The reason why skip is incremented in __ftrace_trace_stack() is because __ftrace_trace_stack() in stack trace is the only function that wasn't skipped from anywhere.

---
  kernel/trace/trace.c | 4 +---

  1 file changed, 1 insertion(+), 3 deletions(-)
kernel/trace/trace.c +0 -0
index c3b2c7dfadef..0f2e255f563c 100644

kernel/trace/trace.c static void __ftrace_trace_stack(struct · line 2919

Quoted 4 lines
kernel/trace/trace.c +0 -0
@@ -2916,10 +2916,8 @@ static void __ftrace_trace_stack(struct 
 trace_buffer *buffer,
        * Add one, for this function and the call to save_stack_trace()
        * If regs is set, then these functions will not be in the way.
        */

Hmm, with this change, the above comment should also be updated.

kernel/trace/trace.c static void __ftrace_trace_stack(struct trace_buffer *buffer, · line 2922

Quoted 5 lines
kernel/trace/trace.c +1 -3
-#ifndef CONFIG_UNWINDER_ORC
-    if (!regs)
+    if (IS_ENABLED(CONFIG_UNWINDER_ORC) || !regs)
           skip++;
-#endif

Also, this solves just one pattern (only enable function tracer) but if there are fprobes (or kprobes) on the same function, it introduces another issue. e.g. (with this fix)

------
echo 1 > options/func_stack_trace
echo 1 > options/stacktrace
echo "vfs_write" >> set_ftrace_filter
echo "function" > current_tracer
echo "f:myevent vfs_write" > dynamic_events
echo 1 > events/fprobes/myevent/enable
echo > /dev/null
cat trace
...
               sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560) sh-140     [001] ...1.    18.352602: <stack trace>
  => ksys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe
               sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write sh-140     [001] ...1.    18.352604: <stack trace>

Quoted 8 lines

  => ftrace_regs_call
  => vfs_write
  => ksys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Quoted 2 lines

Thanks for the other tests. This issue may be function_trace_call() specific problem.

Not function_trace_call(),  function_stack_trace_call().

Quoted 9 lines

So I will change the place to increment skip number.

Quoted 6 lines

Thank you,

Quoted 4 lines
kernel/trace/trace.c +0 -0
         preempt_disable_notrace();

  --
2.46.1

Thank you,

maintainer
#4 lore raw
In reply to ts
Quoted 17 lines · On Tue, 1 Oct 2024 22:27:03 +0900 ts <[email protected]> wrote:
Quoted 8 lines

...

               sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560)
               sh-140     [001] ...1.    18.352602: <stack trace>  
  => ksys_write
  => do_syscall_64
  => entry_SYSCALL_64_after_hwframe  
               sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write
               sh-140     [001] ...1.    18.352604: <stack trace>  
Quoted 8 lines

=> ftrace_regs_call
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Quoted 4 lines

Thanks for the other tests. This issue may be function_trace_call() specific problem.

So I will change the place to increment skip number.

My fear is that we are going to just break it elsewhere. The problem with the "skip" is that there's so many configurations when we get here, we may not really know what to skip. If the compiler inlines something, then we may skip something we do not want to.

I rather have extra information than not enough.

-- Steve

author
#5 lore raw
Quoted 25 lines · Steven Rostedt
Quoted 17 lines · On Tue, 1 Oct 2024 22:27:03 +0900 ts <[email protected]> wrote:
Quoted 8 lines

...

                sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560)
                sh-140     [001] ...1.    18.352602: <stack trace>
   => ksys_write
   => do_syscall_64
   => entry_SYSCALL_64_after_hwframe
                sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write
                sh-140     [001] ...1.    18.352604: <stack trace>
Quoted 8 lines

=> ftrace_regs_call
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Quoted 4 lines

Thanks for the other tests. This issue may be function_trace_call() specific problem.

So I will change the place to increment skip number.

Quoted 8 lines

My fear is that we are going to just break it elsewhere. The problem with the "skip" is that there's so many configurations when we get here, we may not really know what to skip. If the compiler inlines something, then we may skip something we do not want to.

I rather have extra information than not enough.

-- Steve

It may not be clean and be bit redundant, but I think it would be more maintainable to treat

"skip(and skipped functions)" separately only at the top(parent) of functions that display stack trace.

thanks.

#6 lore raw
Quoted 35 lines · On Wed, 2 Oct 2024 15:43:35 +0900 Tatsuya S <[email protected]> wrote:
Quoted 32 lines · Steven Rostedt
Quoted 17 lines · On Tue, 1 Oct 2024 22:27:03 +0900 ts <[email protected]> wrote:
Quoted 8 lines

...

                sh-140     [001] ...1.    18.352601: myevent: (vfs_write+0x4/0x560)
                sh-140     [001] ...1.    18.352602: <stack trace>
   => ksys_write
   => do_syscall_64
   => entry_SYSCALL_64_after_hwframe
                sh-140     [001] ...1.    18.352602: vfs_write <-ksys_write
                sh-140     [001] ...1.    18.352604: <stack trace>
Quoted 8 lines

=> ftrace_regs_call
=> vfs_write
=> ksys_write
=> do_syscall_64
=> entry_SYSCALL_64_after_hwframe
------
As you can see, myevent skips "vfs_write". (and function tracer still have ftrace_regs_call() )

Quoted 4 lines

Thanks for the other tests. This issue may be function_trace_call() specific problem.

So I will change the place to increment skip number.

My fear is that we are going to just break it elsewhere. The problem with the "skip" is that there's so many configurations when we get here, we may not really know what to skip. If the compiler inlines something, then we may skip something we do not want to.

I rather have extra information than not enough.

Quoted 7 lines
Quoted 1 line

-- Steve

It may not be clean and be bit redundant, but I think it would be more maintainable to treat

"skip(and skipped functions)" separately only at the top(parent) of functions that display stack trace.

I think you'd better make a set of test programs which gets the stacktrace with several different conditions (combinations of tracers/probes/kconfgis) at first. Then we can make sure it does not break anything.

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>