Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] rethook: link the node before replacing the return address
@ 2026-11-27 18:40 Yifan Feng
  2026-11-27 18:53 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Yifan Feng @ 2026-11-27 18:40 UTC (permalink / raw)
  To: Masami Hiramatsu, Steven Rostedt
  Cc: Mathieu Desnoyers, linux-trace-kernel, linux-kernel, Yifan Feng

rethook_hook() calls arch_rethook_prepare(), which replaces the return
address of the probed function with arch_rethook_trampoline, and only
then adds the node to current->rethooks.

A stack unwinder that runs in between, from an interrupt or an NMI,
finds the trampoline address on the stack but no node for it.
rethook_find_ret_addr() returns 0 and the stack trace ends at the
probed function.

Add the node to the list first.  Until arch_rethook_prepare() has
filled it in, give it the trampoline address as its return address and
a zero frame, so that rethook_find_ret_addr() skips it.

Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
Signed-off-by: Yifan Feng <evanfengff@gmail.com>
---
x86 in QEMU, kretprobes on 44 functions, call chains sampled by a cycles perf event: 388 of 119922 chains ended at a probed function before, 0 of 121436 after. kprobes kunit and the kprobe ftrace selftests give the same results.
Found and written with the help of an AI coding assistant.

 kernel/trace/rethook.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
index 87a27f3aa4a6..c0d89720e1a8 100644
--- a/kernel/trace/rethook.c
+++ b/kernel/trace/rethook.c
@@ -196,8 +196,16 @@ NOKPROBE_SYMBOL(rethook_try_get);
  */
 void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount)
 {
-	arch_rethook_prepare(node, regs, mcount);
+	/*
+	 * Link the node before the return address is replaced, so that a
+	 * stack unwinder never finds the trampoline address without its node.
+	 * Until arch_rethook_prepare() fills the node in, make it one that
+	 * rethook_find_ret_addr() skips.
+	 */
+	node->ret_addr = (unsigned long)arch_rethook_trampoline;
+	node->frame = 0;
 	__llist_add(&node->llist, &current->rethooks);
+	arch_rethook_prepare(node, regs, mcount);
 }
 NOKPROBE_SYMBOL(rethook_hook);
 

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.39.3 (Apple Git-146)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27 18:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 18:40 [PATCH] rethook: link the node before replacing the return address Yifan Feng
2026-11-27 18:53 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox