linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [BUG/RFC 1/2] arm64/ftrace,bpf: Fix partial regs after bpf_prog_run
@ 2025-11-05 12:59 Jiri Olsa
  2025-11-05 12:59 ` [PATCH 2/2] selftests/bpf: Add test for bpf_override_return helper Jiri Olsa
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Olsa @ 2025-11-05 12:59 UTC (permalink / raw)
  To: Masami Hiramatsu, Steven Rostedt, Will Deacon
  Cc: Peter Zijlstra, bpf, linux-trace-kernel, linux-arm-kernel, x86,
	Yonghong Song, Song Liu, Andrii Nakryiko, Mark Rutland,
	Mahe Tardy

hi,
Mahe reported issue with bpf_override_return helper not working
when executed from kprobe.multi bpf program on arm.

The problem seems to be that on arm we use alternate storage for
pt_regs object that is passed to bpf_prog_run and if any register
is changed (which is the case of bpf_override_return) it's not
propagated back to actual pt_regs object.

The change below seems to fix the issue, but I have no idea if
that's proper fix for arm, thoughts?

I'm attaching selftest to actually test bpf_override_return helper
functionality, because currently we only test that we are able to
attach a program with it, but not the override itself.

thanks,
jirka


---
 arch/arm64/include/asm/ftrace.h | 11 +++++++++++
 include/linux/ftrace.h          |  3 +++
 kernel/trace/bpf_trace.c        |  1 +
 3 files changed, 15 insertions(+)

diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h
index ba7cf7fec5e9..ad6cf587885c 100644
--- a/arch/arm64/include/asm/ftrace.h
+++ b/arch/arm64/include/asm/ftrace.h
@@ -157,6 +157,17 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	return regs;
 }
 
+static __always_inline void
+ftrace_partial_regs_fix(const struct ftrace_regs *fregs, struct pt_regs *regs)
+{
+	struct __arch_ftrace_regs *afregs = arch_ftrace_regs(fregs);
+
+	if (afregs->pc != regs->pc) {
+		afregs->pc = regs->pc;
+		afregs->regs[0] = regs->regs[0];
+	}
+}
+
 #define arch_ftrace_fill_perf_regs(fregs, _regs) do {		\
 		(_regs)->pc = arch_ftrace_regs(fregs)->pc;			\
 		(_regs)->regs[29] = arch_ftrace_regs(fregs)->fp;		\
diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
index 7ded7df6e9b5..4cb1315522bb 100644
--- a/include/linux/ftrace.h
+++ b/include/linux/ftrace.h
@@ -205,6 +205,9 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
 	return &arch_ftrace_regs(fregs)->regs;
 }
 
+static __always_inline void
+ftrace_partial_regs_fix(struct ftrace_regs *fregs, struct pt_regs *regs) { }
+
 #endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */
 
 #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index a795f7afbf3d..7b5768ced9b3 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2564,6 +2564,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
 	err = bpf_prog_run(link->link.prog, regs);
 	bpf_reset_run_ctx(old_run_ctx);
+	ftrace_partial_regs_fix(fregs, bpf_kprobe_multi_pt_regs_ptr());
 	rcu_read_unlock();
 
  out:
-- 
2.51.1


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

end of thread, other threads:[~2025-11-05 22:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-05 12:59 [BUG/RFC 1/2] arm64/ftrace,bpf: Fix partial regs after bpf_prog_run Jiri Olsa
2025-11-05 12:59 ` [PATCH 2/2] selftests/bpf: Add test for bpf_override_return helper Jiri Olsa
2025-11-05 22:04   ` Song Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).