Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Yifan Feng <evanfengff@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] x86/unwind: Recover the fgraph return address behind a rethook
Date: Mon, 30 Nov 2026 16:37:45 -0500	[thread overview]
Message-ID: <20261130163745.65948a57@gandalf.local.home> (raw)
In-Reply-To: <20261125210905.33319-1-evanfengff@gmail.com>

On Wed, 25 Nov 2026 13:09:05 -0800
Yifan Feng <evanfengff@gmail.com> wrote:

> unwind_recover_ret_addr() asks ftrace_graph_ret_addr() and then
> unwind_recover_rethook() for the original return address. That works
> when the function graph tracer hooks the return of a function after a
> kretprobe did: the stack has return_to_handler(), the shadow stack of
> fgraph has arch_rethook_trampoline() and the rethook node has the
> original address.
> 
> When the kretprobe hooks the return last, the stack has
> arch_rethook_trampoline() and the rethook node has return_to_handler().
> Nothing translates that address, so the stack trace ends there:
> 
>   => rw_verify_area
>   => vfs_read
>   => return_to_handler  
> 
> This happens when the kretprobe is registered before the function_graph
> tracer or an fprobe exit handler is enabled for the same function. It
> also happens in the other order when the function is reached by a tail
> call from a traced function, like ksys_read(): the rethook node then
> has the return_to_handler() of the caller.
> 
> Translate the address found in the rethook node with
> ftrace_graph_ret_addr(), and repeat while that gives the rethook
> trampoline again. Do it in unwind_recover_rethook(), so that the
> unwinding from pt_regs gets it, too.

I'm able to reproduce this with the following commands:

 ~# echo 'r:myexit do_exit ' > /sys/kernel/tracing/dynamic_events
 ~# trace-cmd start -e myexit -e sched_switch -R stacktrace
 ~# trace-cmd start -B foo -p function_graph -l do_exit

Which produces:

 ~# trace-cmd show
[..]
             sed-35746   [003] d..2. 257700.284656: sched_switch: prev_comm=sed prev_pid=35746 prev_prio=120 prev_state=R+ ==> next_comm=rcu_preempt next_pid=16 next_prio=120
             sed-35746   [003] d..2. 257700.284668: <stack trace>
 => trace_event_raw_event_sched_switch
 => __traceiter_sched_switch
 => __schedule
 => preempt_schedule
 => preempt_schedule_thunk
 => _raw_spin_unlock_irqrestore
 => sched_move_task
 => do_exit
 => return_to_handler

And with your patch...

             sed-1101    [002] d..2.   217.519332: sched_switch: prev_comm=sed prev_pid=1101 prev_prio=120 prev_state=R+ ==> next_comm=ksoftirqd/2 next_pid=29 next_prio=120
             sed-1101    [002] d..2.   217.519354: <stack trace>
 => trace_event_raw_event_sched_switch
 => __traceiter_sched_switch
 => __schedule
 => preempt_schedule
 => preempt_schedule_thunk
 => _raw_spin_unlock_irqrestore
 => sched_move_task
 => do_exit
 => do_group_exit
 => __x64_sys_exit_group
 => x64_sys_call
 => do_syscall_64
 => entry_SYSCALL_64_after_hwframe



> 
> Fixes: 19138af1bd88 ("x86/unwind: Recover kretprobe trampoline entry")
> Signed-off-by: Yifan Feng <evanfengff@gmail.com>
> ---
> QEMU: function_graph, fprobe exit and kretprobes on vfs_read()/ksys_read() in 6 orders, stacktrace trigger in rw_verify_area(): complete in 0 of 6 before, 6 of 6 after. kprobes kunit and ftrace selftests (kprobe, dynevent, ftrace) give the same results.
> Found and written with the help of an AI coding assistant.
> 
>  arch/x86/include/asm/unwind.h | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
> index 7cede4dc21f0..7ded718a8d6b 100644
> --- a/arch/x86/include/asm/unwind.h
> +++ b/arch/x86/include/asm/unwind.h
> @@ -108,9 +108,16 @@ unsigned long unwind_recover_rethook(struct unwind_state *state,
>  				     unsigned long addr, unsigned long *addr_p)
>  {
>  #ifdef CONFIG_RETHOOK
> -	if (is_rethook_trampoline(addr))
> -		return rethook_find_ret_addr(state->task, (unsigned long)addr_p,
> +	/*
> +	 * The rethook and the hook of ftrace_graph can sit on top of each
> +	 * other in any order, and more than once after a tail call.
> +	 */
> +	while (is_rethook_trampoline(addr)) {

My fear is that if there's an issue with the look up where it doesn't
continue and always returns the same addr this turns into an infinite loop.
I would rather have this be:

	/* Max 100 tries, way more than enough */
	for (int i = 0; i < 100 && is_rethook_trampoline(addr); i++) {

Maybe 100 is even too much. Perhaps make it 10 tries?

-- Steve

> +		addr = rethook_find_ret_addr(state->task, (unsigned long)addr_p,
>  					     &state->kr_cur);
> +		addr = ftrace_graph_ret_addr(state->task, &state->graph_idx,
> +					     addr, addr_p);
> +	}
>  #endif
>  	return addr;
>  }
> 
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f


      reply	other threads:[~2026-11-30 21:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 21:09 [PATCH] x86/unwind: Recover the fgraph return address behind a rethook Yifan Feng
2026-11-30 21:37 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130163745.65948a57@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=evanfengff@gmail.com \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox