All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yifan Feng <evanfengff@gmail.com>
To: Masami Hiramatsu <mhiramat@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yifan Feng <evanfengff@gmail.com>
Subject: [PATCH] rethook: link the node before replacing the return address
Date: Fri, 27 Nov 2026 10:40:37 -0800	[thread overview]
Message-ID: <20261127184037.71903-1-evanfengff@gmail.com> (raw)

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)


             reply	other threads:[~2026-11-27 18:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 18:40 Yifan Feng [this message]
2026-11-27 18:53 ` [PATCH] rethook: link the node before replacing the return address sashiko-bot

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=20261127184037.71903-1-evanfengff@gmail.com \
    --to=evanfengff@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.