* [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, ¤t->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
* Re: [PATCH] rethook: link the node before replacing the return address
2026-11-27 18:40 [PATCH] rethook: link the node before replacing the return address Yifan Feng
@ 2026-11-27 18:53 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 18:53 UTC (permalink / raw)
To: Yifan Feng; +Cc: linux-trace-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] rethook: missing compiler barriers in rethook_hook() and
arch_rethook_prepare()
--
commit 8555f1c8da702428f75ba61ef07accb91b4115a4
Author: Yifan Feng <evanfengff@gmail.com>
rethook: link the node before replacing the return address
This commit links the rethook_node into current->rethooks with sentinel
values before calling arch_rethook_prepare(), so that an interrupt or NMI
stack unwinder never observes the trampoline return address without a
corresponding node on the list.
> diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> index 87a27f3aa4a61..c0d89720e1a8b 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, ¤t->rethooks);
> + arch_rethook_prepare(node, regs, mcount);
> }
> NOKPROBE_SYMBOL(rethook_hook);
[Severity: Medium]
Do these stores and the updates in arch_rethook_prepare() need compiler
barriers to protect same-CPU interrupt and NMI unwinders?
__llist_add() expands to plain stores:
include/linux/llist.h:__llist_add_batch() {
new_last->next = head->first;
head->first = new_first;
}
Because node comes from an objpool and rethook_recycle() does not clear
its fields, reordering current->rethooks.first ahead of the stores to
node->ret_addr, node->frame, or node->llist.next exposes stale values to
rethook_find_ret_addr(). With LTO, arch_rethook_prepare() can also be
inlined into rethook_hook(), allowing the compiler to eliminate the
sentinel stores to node->ret_addr and node->frame as dead stores before
they are overwritten.
In addition, because node is now already visible on current->rethooks
when arch_rethook_prepare() runs, arch_rethook_prepare() itself needs a
barrier() before replacing the return address with
arch_rethook_trampoline so that the real node->ret_addr and node->frame
are committed first:
arch/x86/kernel/rethook.c:arch_rethook_prepare() {
...
rh->ret_addr = stack[0];
rh->frame = regs->sp;
/* Replace the return addr with trampoline addr */
stack[0] = (unsigned long) arch_rethook_trampoline;
}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127184037.71903-1-evanfengff@gmail.com?part=1
^ permalink raw reply [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