From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=216.40.44.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796074672; b=zX/es6nEn2uiAoTfOYeZwarL1nGEdGckgekmDcQLj0gnEyCklusEvmLZkDomlQTCF7dU nrUh8v8X+YOqXIWxqPu6hGNs0TMb4dtnhHttZSKL3O+LB1xJL6gbN5FIxlYTiYlkV+uQf f4nvXA2bLnjD7gxYBuiy32UeY2mKgc2Ix9g8BrBmy63lzyYMAXU6Di0mXHeQmNG0yJCcC U+4Diqe8E/Akx4FBqEuS+aLpaYV2wWDvaFD11BuGOJRlz9uYX4oaUJtkwsYxRRvC8sNtz 6VXHFieYyxnlfeP+gme3LpSZjguQ3w0On+XnQ+jkEAhYmpbTwokuKbrc/BkYn3zOUhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796074672; h=DMARC-Filter:Received:Received:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding:X-Stat-Signature:X-Spam-Status: X-Rspamd-Server:X-Rspamd-Queue-Id:X-Session-Marker:X-Session-ID: DKIM-Signature:X-HE-Tag:X-HE-Meta; bh=pn74Wm7XnU7q9VMp43nS332Qt/6egtOF/rKoZmOozK0=; b=3cxFuzN+CvulOzAzrpLIH3+buTnEA6CTuSIXYQ0SN+Jil5G+doJFq/uMEvYvAsv2r1AS 0Cl7W7cecUdAqAzkrRI//lSp+Rmf/JYceEZ3DI/W6WXNzLMt8HaBMUhcI2MinDRqv0TLS q42vyNw0M8o8XG0gLula6L/b7XMOXE2dCP3zTzTkonMIIDr+rxLQssJNjRx/56aws8s1Y IhUMSbK44ZosSHbtwP4IrH31vp/ygJeaD80EaS/lpOPAWLbONQENkZ/M3iT0f8kuf71ng 2FFKCbCysuKk8UCXQHzAaXazBxXzwUPyeXAP2LhswN7VBzcyugtCLWPCzUhlCe24uZQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; dkim=pass header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=FflDJsxS; arc=none smtp.remote-ip=216.40.44.11 Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBF6F606A31A; Mon, 30 Nov 2026 21:37:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EBF6F606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=goodmis.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=goodmis.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=FflDJsxS Received: from omf09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AF7F9160C55; Mon, 30 Nov 2026 21:37:48 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: rostedt@goodmis.org) by omf09.hostedemail.com (Postfix) with ESMTPA id 5CCC42002B; Mon, 30 Nov 2026 21:37:46 +0000 (UTC) Date: Mon, 30 Nov 2026 16:37:45 -0500 From: Steven Rostedt To: Yifan Feng Cc: Josh Poimboeuf , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Masami Hiramatsu , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] x86/unwind: Recover the fgraph return address behind a rethook Message-ID: <20261130163745.65948a57@gandalf.local.home> In-Reply-To: <20261125210905.33319-1-evanfengff@gmail.com> References: <20261125210905.33319-1-evanfengff@gmail.com> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Stat-Signature: 81bzkk94uwzzpu1rz4yafhuhsmby4ftf X-Rspamd-Server: rspamout01 X-Rspamd-Queue-Id: 5CCC42002B X-Session-Marker: 726F737465647440676F6F646D69732E6F7267 X-Session-ID: U2FsdGVkX184iWghCW2Wj/0aiMT8NpLT/E/d7Ixorek= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=goodmis.org; h=date:from:to:cc:subject:message-id:in-reply-to:references:mime-version:content-type:content-transfer-encoding; s=dkim1; bh=pn74Wm7XnU7q9VMp43nS332Qt/6egtOF/rKoZmOozK0=; b=FflDJsxSedIb6YcB8DHT/BKnqmDxqErufcbXb/vgUwxxoczYO+Gt0cTEt9cBYVoznxhoVVyZ1FBZ2Y+/1Z37YU4I2FgIDc5CLMOh3nN9GnDqU3wMSKiN/EpwP6tGJ9FQLNziZLR3jPEDW9qAVLghUdjq9NC7r1Fv35635NtHnKU= X-HE-Tag: 1796074666-6891 X-HE-Meta: U2FsdGVkX1/NpqziN1Fndhg5zwFfOf5ApgbM97mUNPSoYWBfTopl6Xls8wuZTGzS78+RpJMW1zoyjeXSk84ndpTlZExDoqsia8qMIt6/AJAViL4iYWDVxWuwtXl5hIB4z3zEsde1RoT/jKIOKnD9dWjQiJWqs19FPcX8pvAtZrbcOGAEH7rpsghZmF7RpeL1MCup2tPlA+xx4ZMsEr2IY8oyp4Qnq2fKejRMmHwxWQzeUvF6+evTZC1SxpKlaWCKHPMFDr7UyjXQs76lsWk4MSguJmgJXQgsgMcBI5mZeHns1GPa1O/BwFDWONrJcfljLmSC6lM5zmcsVz1JE2tZvy/4k4y517T52JInBEKVuU3aIwMfUUefFZZlFhX2OEKY On Wed, 25 Nov 2026 13:09:05 -0800 Yifan Feng 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: => 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: => 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 > --- > 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