Re: [PATCH] rethook: link the node before replacing the return address

From: Masami Hiramatsu (Google)

Date: Sat Dec 05 2026 - 23:18:19 EST


On Fri, 27 Nov 2026 10:40:37 -0800, Yifan Feng <evanfengff@xxxxxxxxx> wrote:
> 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.

Thanks for finding the issue. Yeah, this can happen and need to be fixed.
However, as Sashiko pointed[1], this still needs compiler barriers.

[1] https://lore.kernel.org/r/sashiko-outbox-238117@xxxxxxxxxx

To fix this issue cleanly, I think we need to split setting return address
from the arch_rethook_prepare() and append WRITE_ONCE() so that updates
are done by correct order. e.g.:

To properly resolve this issue, I think we should extract the return
address setting from `arch_rethook_prepare()` and add `WRITE_ONCE()` to it,
ensuring that updates have done in the correct order. For example:

1. arch_rethook_prepare(node, regs, mcount):
Only saves the original return address and frame into node->ret_addr
and node->frame, without modifying the stack or pt_regs yet.

2. Link the node in rethook_hook() with explicit compiler ordering:

arch_rethook_prepare(node, regs, mcount);

// I think this should be implemented in llist.h as __llist_add_ordered().
WRITE_ONCE(node->llist.next, current->rethooks.first);
barrier();
WRITE_ONCE(current->rethooks.first, &node->llist);

arch_rethook_set_return(regs, mcount);

3. arch_rethook_set_return(regs, mcount):
Replaces the return address on the stack / pt_regs with
arch_rethook_trampoline (using WRITE_ONCE()).

This may requires more changes (adding __llist_addr_ordered() and
arch_rethook_set_return()) but safer and cleaner.
(Note that the latter change can be done by just one tree wide patch.)

Can you update it?

Thank you,

--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>