* [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* [PATCH 2/2] selftests/bpf: Add test for bpf_override_return helper
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 ` Jiri Olsa
2025-11-05 22:04 ` Song Liu
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
We do not actualy test the bpf_override_return helper functionality
itself at the moment, only the bpf program being able to attach it.
Adding test that override prctl syscall return value on top of
kprobe and kprobe.multi.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../bpf/prog_tests/kprobe_multi_test.c | 61 +++++++++++++++++++
.../bpf/progs/kprobe_multi_override.c | 15 +++++
2 files changed, 76 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
index 6cfaa978bc9a..b5e5cc54b89a 100644
--- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
@@ -1,4 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <sys/prctl.h>
#include <test_progs.h>
#include "kprobe_multi.skel.h"
#include "trace_helpers.h"
@@ -540,6 +542,63 @@ static void test_attach_override(void)
kprobe_multi_override__destroy(skel);
}
+/* XXX I'll move this to common place and share with
+ * SYS_NANOSLEEP_KPROBE_NAME macro on repost.
+ */
+#ifdef __x86_64__
+#define SYS_PREFIX "__x64_"
+#elif defined(__s390x__)
+#define SYS_PREFIX "__s390x_"
+#elif defined(__aarch64__)
+#define SYS_PREFIX "__arm64_"
+#elif defined(__riscv)
+#define SYS_PREFIX "__riscv_"
+#else
+#define SYS_PREFIX ""
+#endif
+
+static void test_override(void)
+{
+ struct kprobe_multi_override *skel = NULL;
+ int err;
+
+ skel = kprobe_multi_override__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load"))
+ goto cleanup;
+
+ skel->bss->pid = getpid();
+
+ /* no override */
+ err = prctl(0xffff, 0);
+ ASSERT_EQ(err, -1, "err");
+
+ /* kprobe.multi override */
+ skel->links.test_override = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override,
+ SYS_PREFIX "sys_prctl", NULL);
+ if (!ASSERT_OK_PTR(skel->links.test_override, "bpf_program__attach_kprobe_multi_opts")) {
+ goto cleanup;
+ }
+
+ err = prctl(0xffff, 0);
+ ASSERT_EQ(err, 123, "err");
+
+ bpf_link__destroy(skel->links.test_override);
+ skel->links.test_override = NULL;
+
+ /* kprobe override */
+ skel->links.test_kprobe_override = bpf_program__attach_kprobe(skel->progs.test_kprobe_override,
+ false, SYS_PREFIX "sys_prctl");
+ if (!ASSERT_OK_PTR(skel->links.test_kprobe_override, "bpf_program__attach_kprobe")) {
+ goto cleanup;
+ }
+
+ err = prctl(0xffff, 0);
+ ASSERT_EQ(err, 123, "err");
+
+cleanup:
+ kprobe_multi_override__destroy(skel);
+}
+
#ifdef __x86_64__
static void test_attach_write_ctx(void)
{
@@ -597,6 +656,8 @@ void test_kprobe_multi_test(void)
test_attach_api_fails();
if (test__start_subtest("attach_override"))
test_attach_override();
+ if (test__start_subtest("override"))
+ test_override();
if (test__start_subtest("session"))
test_session_skel_api();
if (test__start_subtest("session_cookie"))
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_override.c b/tools/testing/selftests/bpf/progs/kprobe_multi_override.c
index 28f8487c9059..14f39fa6d515 100644
--- a/tools/testing/selftests/bpf/progs/kprobe_multi_override.c
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_override.c
@@ -5,9 +5,24 @@
char _license[] SEC("license") = "GPL";
+int pid = 0;
+
SEC("kprobe.multi")
int test_override(struct pt_regs *ctx)
{
+ if (bpf_get_current_pid_tgid() >> 32 != pid)
+ return 0;
+
+ bpf_override_return(ctx, 123);
+ return 0;
+}
+
+SEC("kprobe")
+int test_kprobe_override(struct pt_regs *ctx)
+{
+ if (bpf_get_current_pid_tgid() >> 32 != pid)
+ return 0;
+
bpf_override_return(ctx, 123);
return 0;
}
--
2.51.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH 2/2] selftests/bpf: Add test for bpf_override_return helper
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
0 siblings, 0 replies; 3+ messages in thread
From: Song Liu @ 2025-11-05 22:04 UTC (permalink / raw)
To: Jiri Olsa
Cc: Masami Hiramatsu, Steven Rostedt, Will Deacon, Peter Zijlstra,
bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, x86@kernel.org,
Yonghong Song, Song Liu, Andrii Nakryiko, Mark Rutland,
Mahe Tardy
> On Nov 5, 2025, at 4:59 AM, Jiri Olsa <jolsa@kernel.org> wrote:
>
> We do not actualy test the bpf_override_return helper functionality
> itself at the moment, only the bpf program being able to attach it.
>
> Adding test that override prctl syscall return value on top of
> kprobe and kprobe.multi.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
The test looks good to me.
Acked-by: Song Liu <song@kernel.org>
^ permalink raw reply [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).