arm64/ftrace,bpf: Fix partial regs after bpf_prog_run
Jiri Olsa
reviewerPATCH 1/2 arm64/ftrace,bpf: Fix partial regs after bpf_prog_run
Mahe reported issue with bpf_override_return helper not working when executed from kprobe.multi bpf program on arm.
The problem is 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.
Fixing this by introducing and calling ftrace_partial_regs_update function to propagate the values of changed registers (ip and stack).
---
v1 changes:
- used ftrace_partial_regs_update with comments from Steven
arch/arm64/include/asm/ftrace.h | 24 ++++++++++++++++++++++++ include/linux/ftrace.h | 3 +++ kernel/trace/bpf_trace.c | 1 + 3 files changed, 28 insertions(+)
arch/arm64/include/asm/ftrace.h +24 -0
@@ -157,6 +157,30 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)return regs; } +/* +* ftrace_partial_regs_update - update the original ftrace_regs from regs +* @fregs: The ftrace_regs to update from @regs +* @regs: The partial regs from ftrace_partial_regs() that was updated +* +* Some architectures have the partial regs living in the ftrace_regs +* structure, whereas other architectures need to make a different copy +* of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and +* if the code using @regs updates a field (like the instruction pointer or +* stack pointer) it may need to propagate that change to the original @fregs +* it retrieved the partial @regs from. Use this function to guarantee that +* update happens. +*/ +static __always_inline void +ftrace_partial_regs_update (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; \
include/linux/ftrace.h +3 -0
@@ -213,6 +213,9 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs)return regs; } +static __always_inline void +ftrace_partial_regs_update (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
kernel/trace/bpf_trace.c +1 -0
@@ -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_update (fregs,bpf_kprobe_multi_pt_regs_ptr ());rcu_read_unlock (); out:
Signature · 1 line
2.52.0
Jiri Olsa
reviewerPATCH 2/2 selftests/bpf: Add test for bpf_override_return helper
We do not actually 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.
---
v1 changes:
- added ack from Song
- moved SYS_PREFIX to trace_helpers.h
.../bpf/prog_tests/kprobe_multi_test.c | 44 +++++++++++++++++++ .../bpf/progs/kprobe_multi_override.c | 15 +++++++ tools/testing/selftests/bpf/trace_helpers.h | 12 +++++ 3 files changed, 71 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +44 -0
@@ -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,46 @@ static void test_attach_override(void)kprobe_multi_override__destroy (skel); } +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 +639,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" ))
tools/testing/selftests/bpf/progs/kprobe_multi_override.c +15 -0
@@ -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 ; }
tools/testing/selftests/bpf/trace_helpers.h +12 -0
@@ -4,6 +4,18 @@#include <bpf/libbpf.h> +#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 +#define __ALIGN_MASK (x, mask) (((x)+(mask))&~(mask))#define ALIGN (x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
Signature · 1 line
2.52.0
Will Deacon
maintainerQuoted 44 lines · Jiri Olsa
Mahe reported issue with
bpf_override_returnhelper not working when executed from kprobe.multi bpf program on arm.The problem is that on arm we use alternate storage for
pt_regsobject that is passed tobpf_prog_runand if any register is changed (which is the case ofbpf_override_return) it's not propagated back to actualpt_regsobject.Fixing this by introducing and calling
ftrace_partial_regs_updatefunction to propagate the values of changed registers (ip and stack).Fixes b9b55c8912ce ("tracing: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs")Reported-by Mahe Tardy <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
v1 changes:
- usedftrace_partial_regs_updatewith comments from Stevenarch/arm64/include/asm/ftrace.h | 24 ++++++++++++++++++++++++ include/linux/ftrace.h | 3 +++ kernel/trace/bpf_trace.c | 1 + 3 files changed, 28 insertions(+)arch/arm64/include/asm/ftrace.h +13
-0@@ -157,6 +157,30 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)return regs; } +/* +* ftrace_partial_regs_update - update the original ftrace_regs from regs +* @fregs: The ftrace_regs to update from @regs +* @regs: The partial regs from ftrace_partial_regs() that was updated +* +* Some architectures have the partial regs living in the ftrace_regs +* structure, whereas other architectures need to make a different copy +* of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and +* if the code using @regs updates a field (like the instruction pointer or +* stack pointer) it may need to propagate that change to the original @fregs +* it retrieved the partial @regs from. Use this function to guarantee that +* update happens. +*/
arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 182
Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10
-0+static __always_inline void +ftrace_partial_regs_update (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 ]; + } +}
I still don't understand why we need anything new in the arch code for this.
We've selected HAVE_ARCH_FTRACE_REGS and we implement ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() so the core code already has everything it needs to make this work without additional arch support.
Will
Steven Rostedt
maintainerQuoted 6 lines · On Wed, 7 Jan 2026 14:16:05 +0000 Will Deacon <[email protected]> wrote:
I still don't understand why we need anything new in the arch code for this.
We've selected
HAVE_ARCH_FTRACE_REGSand we implementftrace_regs_set_instruction_pointer()andftrace_regs_set_return_value()so the core code already has everything it needs to make this work without additional arch support.
I believe the issue is that the BPF code takes a pt_regs and does the update directly with that, and not the ftrace_regs.
I'm guessing this is due to BPF programs modifying the pt_regs directly, and BPF programs do not yet understand ftrace_regs?
Because arm64 requires making a copy of pt_regs as the ftrace_regs has a different layout, and the ftrace_regs is what does the changes, if the pt_regs passed to the BPF program modifies the values it needs a way to propagate that back to the ftrace_regs.
-- Steve
Will Deacon
maintainerQuoted 13 lines · Steven Rostedt
Quoted 6 lines · On Wed, 7 Jan 2026 14:16:05 +0000 Will Deacon <[email protected]> wrote:
I still don't understand why we need anything new in the arch code for this.
We've selected
HAVE_ARCH_FTRACE_REGSand we implementftrace_regs_set_instruction_pointer()andftrace_regs_set_return_value()so the core code already has everything it needs to make this work without additional arch support.I believe the issue is that the BPF code takes a
pt_regsand does the update directly with that, and not theftrace_regs.
Quoted 7 lines
I'm guessing this is due to BPF programs modifying the
pt_regsdirectly, and BPF programs do not yet understandftrace_regs?Because arm64 requires making a copy of
pt_regsas theftrace_regshas a different layout, and theftrace_regsis what does the changes, if thept_regspassed to the BPF program modifies the values it needs a way to propagate that back to theftrace_regs.
Sure, but isn't that exactly what selecting HAVE_ARCH_FTRACE_REGS says, and so any other architecture (i.e. riscv) using a separate structure will run into the same problem?
In other words, add the helper to the core code along the lines of the diff below (purely untested, just to illustrate what I'm trying to get at).
Will
--->8
include/linux/ftrace_regs.h +9 -0
@@ -33,6 +33,15 @@ struct ftrace_regs;#define ftrace_regs_get_frame_pointer (fregs) \ frame_pointer(&arch_ftrace_regs(fregs)->regs) +#else + +static __always_inline void +ftrace_partial_regs_update (const struct ftrace_regs *fregs,struct pt_regs *regs) +{ +ftrace_regs_set_instruction_pointer (fregs,instruction_pointer (regs)); +ftrace_regs_set_return_value (fregs,regs_return_value (regs)); +} +#endif /* HAVE_ARCH_FTRACE_REGS */ /* This can be overridden by the architectures */
Steven Rostedt
maintainerQuoted 10 lines · On Wed, 7 Jan 2026 16:52:52 +0000 Will Deacon <[email protected]> wrote:
include/linux/ftrace_regs.h +2
-0@@ -33,6 +33,15 @@ struct ftrace_regs;#define ftrace_regs_get_frame_pointer (fregs) \ frame_pointer(&arch_ftrace_regs(fregs)->regs) +#else +
include/linux/ftrace_regs.h struct ftrace_regs; · line 47
Quoted 10 lines
include/linux/ftrace_regs.h +7
-0+static __always_inline void +ftrace_partial_regs_update (const struct ftrace_regs *fregs,struct pt_regs *regs) +{ +ftrace_regs_set_instruction_pointer (fregs,instruction_pointer (regs)); +ftrace_regs_set_return_value (fregs,regs_return_value (regs)); +} +#endif /* HAVE_ARCH_FTRACE_REGS */ /* This can be overridden by the architectures */
Hmm, maybe that would work. Of course you forgot to add the helper for the !HAVE_ARCH_FTRACE_REGS case ;-)
-- Steve
Quoted 22 lines · Steven Rostedt
Quoted 19 lines · On Wed, 7 Jan 2026 16:52:52 +0000 Will Deacon <[email protected]> wrote:
include/linux/ftrace_regs.h +9
-0@@ -33,6 +33,15 @@ struct ftrace_regs;#define ftrace_regs_get_frame_pointer (fregs) \ frame_pointer(&arch_ftrace_regs(fregs)->regs) +#else + +static __always_inline void +ftrace_partial_regs_update (const struct ftrace_regs *fregs,struct pt_regs *regs) +{ +ftrace_regs_set_instruction_pointer (fregs,instruction_pointer (regs)); +ftrace_regs_set_return_value (fregs,regs_return_value (regs)); +} +#endif /* HAVE_ARCH_FTRACE_REGS */
Quoted 4 lines
include/linux/ftrace_regs.h struct ftrace_regs; · line 47
Quoted 1 line
include/linux/ftrace_regs.h +0
-0/* This can be overridden by the architectures */ Hmm, maybe that would work. Of course you forgot to add the helper for the !HAVE_ARCH_FTRACE_REGS case ;-)
seems to work, will send new version with that
thanks,
jirka
---
include/linux/ftrace_regs.h +12 -0
@@ -33,6 +33,18 @@ struct ftrace_regs;#define ftrace_regs_get_frame_pointer (fregs) \ frame_pointer(&arch_ftrace_regs(fregs)->regs) +static __always_inline void +ftrace_partial_regs_update (const struct ftrace_regs *fregs,struct pt_regs *regs) { } + +#else + +static __always_inline void +ftrace_partial_regs_update (const struct ftrace_regs *fregs,struct pt_regs *regs) +{ +ftrace_regs_set_instruction_pointer (fregs,instruction_pointer (regs)); +ftrace_regs_set_return_value (fregs,regs_return_value (regs)); +} +#endif /* HAVE_ARCH_FTRACE_REGS */ /* This can be overridden by the architectures */
kernel/trace/bpf_trace.c +1 -0
@@ -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_update (fregs,bpf_kprobe_multi_pt_regs_ptr ());rcu_read_unlock (); out:
Steven Rostedt
maintainerarch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 159
Quoted 8 lines · On Wed, 7 Jan 2026 10:32:55 +0100 Jiri Olsa <[email protected]> wrote:
arch/arm64/include/asm/ftrace.h +0
-0@@ -157,6 +157,30 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)return regs; }
See my reply in the other email thread:
https://lore.kernel.org/all/[email protected]/
-- Steve
Quoted 23 lines
arch/arm64/include/asm/ftrace.h +23
-0+/* +* ftrace_partial_regs_update - update the original ftrace_regs from regs +* @fregs: The ftrace_regs to update from @regs +* @regs: The partial regs from ftrace_partial_regs() that was updated +* +* Some architectures have the partial regs living in the ftrace_regs +* structure, whereas other architectures need to make a different copy +* of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and +* if the code using @regs updates a field (like the instruction pointer or +* stack pointer) it may need to propagate that change to the original @fregs +* it retrieved the partial @regs from. Use this function to guarantee that +* update happens. +*/ +static __always_inline void +ftrace_partial_regs_update (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 ]; + } +}
Quoted 14 lines · Steven Rostedt
arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 159
Quoted 8 lines · On Wed, 7 Jan 2026 10:32:55 +0100 Jiri Olsa <[email protected]> wrote:
arch/arm64/include/asm/ftrace.h +0
-0@@ -157,6 +157,30 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)return regs; }See my reply in the other email thread:
Quoted 1 line
ah ok, thanks
jirka
Quoted 27 lines
-- Steve
Quoted 23 lines
arch/arm64/include/asm/ftrace.h +23
-0+/* +* ftrace_partial_regs_update - update the original ftrace_regs from regs +* @fregs: The ftrace_regs to update from @regs +* @regs: The partial regs from ftrace_partial_regs() that was updated +* +* Some architectures have the partial regs living in the ftrace_regs +* structure, whereas other architectures need to make a different copy +* of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and +* if the code using @regs updates a field (like the instruction pointer or +* stack pointer) it may need to propagate that change to the original @fregs +* it retrieved the partial @regs from. Use this function to guarantee that +* update happens. +*/ +static __always_inline void +ftrace_partial_regs_update (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 ]; + } +}