From: "Rui Qi" <qirui.001@bytedance.com>
To: <rostedt@goodmis.org>, <mhiramat@kernel.org>
Cc: <mark.rutland@arm.com>, <pjw@kernel.org>, <palmer@dabbelt.com>,
<aou@eecs.berkeley.edu>, <alex@ghiti.fr>,
<linux-kernel@vger.kernel.org>,
<linux-trace-kernel@vger.kernel.org>,
<linux-riscv@lists.infradead.org>,
"Rui Qi" <qirui.001@bytedance.com>
Subject: [PATCH] riscv: ftrace: reject out-of-range non-direct targets
Date: Tue, 7 Jul 2026 20:12:35 +0800 [thread overview]
Message-ID: <20260707121235.759104-1-qirui.001@bytedance.com> (raw)
RISC-V initializes the AUIPC half of each ftrace callsite to reach
ftrace_caller and later only patches the JALR instruction. The old
ftrace_make_call() code redirected any requested target outside the
JALR immediate range back to FTRACE_ADDR.
That fallback is only valid for direct-call targets, where
ftrace_caller can still dispatch through op->direct_call. For an
ops-specific trampoline, the generic ftrace core requested a call to
ops->trampoline. Redirecting the site to FTRACE_ADDR instead enters
ftrace_caller and invokes op->func, bypassing that trampoline.
ftrace_modify_call() had the same issue more directly: it ignored
old_addr and addr and always validated and updated the site as
FTRACE_ADDR.
Add a helper to resolve call targets consistently. Keep targets that
fit in the existing AUIPC/JALR window, redirect only out-of-range
direct-call targets through FTRACE_ADDR, and reject other out-of-range
targets. Use the resolved old and new targets in ftrace_modify_call()
so the architecture implementation follows the generic contract.
Signed-off-by: Rui Qi <qirui.001@bytedance.com>
---
arch/riscv/kernel/ftrace.c | 79 +++++++++++++++++++++++++++++++-------
1 file changed, 65 insertions(+), 14 deletions(-)
diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c
index b430edfb83f4..26604bbc4bb5 100644
--- a/arch/riscv/kernel/ftrace.c
+++ b/arch/riscv/kernel/ftrace.c
@@ -46,16 +46,19 @@ void arch_ftrace_update_code(int command)
flush_icache_all();
}
-static int __ftrace_modify_call(unsigned long source, unsigned long target, bool validate)
+static int __ftrace_modify_call(unsigned long source, unsigned long old,
+ unsigned long target, bool validate)
{
- unsigned int call[2], offset;
+ unsigned int call[2], old_call[2], offset;
unsigned int replaced[2];
offset = target - source;
call[1] = to_jalr_t0(offset);
if (validate) {
- call[0] = to_auipc_t0(offset);
+ offset = old - source;
+ old_call[0] = to_auipc_t0(offset);
+ old_call[1] = to_jalr_t0(offset);
/*
* Read the text we want to modify;
* return must be -EFAULT on read error
@@ -63,9 +66,10 @@ static int __ftrace_modify_call(unsigned long source, unsigned long target, bool
if (copy_from_kernel_nofault(replaced, (void *)source, 2 * MCOUNT_INSN_SIZE))
return -EFAULT;
- if (replaced[0] != call[0]) {
- pr_err("%p: expected (%08x) but got (%08x)\n",
- (void *)source, call[0], replaced[0]);
+ if (replaced[0] != old_call[0] || replaced[1] != old_call[1]) {
+ pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
+ (void *)source, old_call[0], old_call[1],
+ replaced[0], replaced[1]);
return -EINVAL;
}
}
@@ -77,6 +81,46 @@ static int __ftrace_modify_call(unsigned long source, unsigned long target, bool
return 0;
}
+static bool ftrace_call_target_in_range(unsigned long addr)
+{
+ unsigned long ftrace_addr = FTRACE_ADDR;
+ unsigned long distance;
+
+ distance = addr > ftrace_addr ? addr - ftrace_addr : ftrace_addr - addr;
+
+ return distance <= JALR_RANGE;
+}
+
+static int ftrace_resolve_call_addr(struct dyn_ftrace *rec, unsigned long addr,
+ unsigned long *target)
+{
+ unsigned long direct;
+
+ /*
+ * The AUIPC instruction is initialized for ftrace_caller and this
+ * implementation only patches the JALR instruction afterwards. Targets
+ * that fit in the existing AUIPC/JALR window can be called as-is.
+ */
+ if (ftrace_call_target_in_range(addr)) {
+ *target = addr;
+ return 0;
+ }
+
+ /*
+ * Out-of-range direct-call targets can still be reached through the
+ * ftrace_caller path, which dispatches via op->direct_call. Do not use
+ * this fallback for ops-specific trampolines, because ftrace_caller
+ * invokes op->func and would not preserve the requested trampoline.
+ */
+ direct = ftrace_find_rec_direct(rec->ip);
+ if (direct && addr == direct) {
+ *target = FTRACE_ADDR;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
static const struct ftrace_ops *riscv64_rec_get_ops(struct dyn_ftrace *rec)
{
@@ -116,19 +160,18 @@ static int ftrace_rec_update_ops(struct dyn_ftrace *rec) { return 0; }
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
{
- unsigned long distance, orig_addr, pc = rec->ip - MCOUNT_AUIPC_SIZE;
+ unsigned long pc = rec->ip - MCOUNT_AUIPC_SIZE;
int ret;
- ret = ftrace_rec_update_ops(rec);
+ ret = ftrace_resolve_call_addr(rec, addr, &addr);
if (ret)
return ret;
- orig_addr = (unsigned long)&ftrace_caller;
- distance = addr > orig_addr ? addr - orig_addr : orig_addr - addr;
- if (distance > JALR_RANGE)
- addr = FTRACE_ADDR;
+ ret = ftrace_rec_update_ops(rec);
+ if (ret)
+ return ret;
- return __ftrace_modify_call(pc, addr, false);
+ return __ftrace_modify_call(pc, 0, addr, false);
}
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
@@ -215,11 +258,19 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
unsigned long caller = rec->ip - MCOUNT_AUIPC_SIZE;
int ret;
+ ret = ftrace_resolve_call_addr(rec, old_addr, &old_addr);
+ if (ret)
+ return ret;
+
+ ret = ftrace_resolve_call_addr(rec, addr, &addr);
+ if (ret)
+ return ret;
+
ret = ftrace_rec_update_ops(rec);
if (ret)
return ret;
- return __ftrace_modify_call(caller, FTRACE_ADDR, true);
+ return __ftrace_modify_call(caller, old_addr, addr, true);
}
#endif
--
2.20.1
reply other threads:[~2026-07-07 12:13 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260707121235.759104-1-qirui.001@bytedance.com \
--to=qirui.001@bytedance.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=palmer@dabbelt.com \
--cc=pjw@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox