arm64/ftrace,bpf: Fix partial regs after bpf_prog_run
Subsystems (7)
Jiri Olsa
reviewerPATCH v2 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).
---
v2 changes:
- moved ftrace_partial_regs_update to generic code [Will]
include/linux/ftrace_regs.h | 25 +++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 1 + 2 files changed, 26 insertions(+)
include/linux/ftrace_regs.h +25 -0
@@ -33,6 +33,31 @@ 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 + +/* +* 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) +{ +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:
Signature · 1 line
2.52.0
Jiri Olsa
reviewerPATCH v2 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.
--- .../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
[email protected]
AI reviewShould the fregs parameter be non-const here? On architectures with HAVE_ARCH_FTRACE_REGS (arm64, riscv), the setter functions expect a non-const pointer:
Quoted 5 lines
include/linux/ftrace_regs.h +0
-0@@ -33,6 +33,31 @@ struct ftrace_regs;
[ ... ]
Quoted 6 lines
include/linux/ftrace_regs.h +6
-0+#else + +/* +* 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
[ ... ]
include/linux/ftrace_regs.h struct ftrace_regs; · line 58
Quoted 6 lines
+ */
include/linux/ftrace_regs.h +5
-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));
^^^^^
Should the fregs parameter be non-const here? On architectures with HAVE_ARCH_FTRACE_REGS (arm64, riscv), the setter functions expect a non-const pointer:
ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, ...) ftrace_regs_set_return_value(struct ftrace_regs *fregs, ...)
Passing the const-qualified fregs to these functions discards the const qualifier, which may cause compiler warnings with -Werror or undefined behavior since the setters do modify the ftrace_regs structure.
include/linux/ftrace_regs.h struct ftrace_regs; · line 60
Quoted 2 lines
include/linux/ftrace_regs.h +2
-0+} +
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20847954035
Quoted 43 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).Reported-by Mahe Tardy <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
v2 changes:
- movedftrace_partial_regs_updateto generic code [Will]include/linux/ftrace_regs.h | 25 +++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 1 + 2 files changed, 26 insertions(+)include/linux/ftrace_regs.h +14
-0@@ -33,6 +33,31 @@ 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 + +/* +* 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
include/linux/ftrace_regs.h struct ftrace_regs; · line 59
Quoted 10 lines
include/linux/ftrace_regs.h +10
-0+* 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) +{ +ftrace_regs_set_instruction_pointer (fregs,instruction_pointer (regs)); +ftrace_regs_set_return_value (fregs,regs_return_value (regs)); +}
I think the AI thingy is right about dropping the const qualifier here but overall I prefer this to the previous revisions. Thanks for sticking with it!
Will
Steven Rostedt
maintainerQuoted 3 lines · On Fri, 9 Jan 2026 13:47:23 +0000 Will Deacon <[email protected]> wrote:
I think the AI thingy is right about dropping the const qualifier here but overall I prefer this to the previous revisions. Thanks for sticking with it!
Yep, I missed that too :-p
I do think AI is better at reviewing than writing code ;-)
-- Steve
Quoted 48 lines · Will Deacon
Quoted 47 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).Reported-by Mahe Tardy <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
v2 changes:
- movedftrace_partial_regs_updateto generic code [Will]include/linux/ftrace_regs.h | 25 +++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 1 + 2 files changed, 26 insertions(+)include/linux/ftrace_regs.h +18
-0@@ -33,6 +33,31 @@ 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 + +/* +* 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. +*/
Quoted 8 lines
include/linux/ftrace_regs.h struct ftrace_regs; · line 59
Quoted 6 lines
include/linux/ftrace_regs.h +6
-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)); +}I think the AI thingy is right about dropping the const qualifier here
yes, will resend, thanks
jirka
Quoted 6 lines
but overall I prefer this to the previous revisions. Thanks for sticking with it!
Acked-by Will Deacon <[email protected]>Will
Quoted 8 lines · On Fri, 9 Jan 2026 10:34:53 +0100 Jiri Olsa <[email protected]> wrote:
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.
Quoted 6 lines
Fixing this by introducing and calling
ftrace_partial_regs_updatefunction to propagate the values of changed registers (ip and stack).Reported-by Mahe Tardy <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
-- Steve
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Jiri,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Jiri-Olsa/selftests-bpf-Add-test-for-bpf_override_return-helper/20260109-174224 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20260109093454.389295-1-jolsa%40kernel.org patch subject: [PATCHv2 bpf-next 1/2] arm64/ftrace,bpf: Fix partial regs after bpf_prog_run config: arm64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260109/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project f43d6834093b19baf79beda8c0337ab020ac5f17) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260109/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from arch/arm64/kernel/asm-offsets.c:13:
In file included from ./include/linux/ftrace.h:115:
>> ./include/linux/ftrace_regs.h:57:38: error: passing 'const struct ftrace_regs *' to parameter of type 'struct ftrace_regs *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
57 | ftrace_regs_set_instruction_pointer(fregs, instruction_pointer(regs));
| ^~~~~
./arch/arm64/include/asm/ftrace.h:95:57: note: passing argument to parameter 'fregs' here
95 | ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
| ^
In file included from arch/arm64/kernel/asm-offsets.c:13:
In file included from ./include/linux/ftrace.h:115:
./include/linux/ftrace_regs.h:58:31: error: passing 'const struct ftrace_regs *' to parameter of type 'struct ftrace_regs *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
58 | ftrace_regs_set_return_value(fregs, regs_return_value(regs));
| ^~~~~ ./arch/arm64/include/asm/ftrace.h:122:50: note: passing argument to parameter 'fregs' here
122 | ftrace_regs_set_return_value(struct ftrace_regs *fregs,
| ^
2 errors generated.
vim +57 ./include/linux/ftrace_regs.h
40
41 /*
42 * ftrace_partial_regs_update - update the original ftrace_regs from regs
43 * @fregs: The ftrace_regs to update from @regs
44 * @regs: The partial regs from ftrace_partial_regs() that was updated
45 *
46 * Some architectures have the partial regs living in the ftrace_regs
47 * structure, whereas other architectures need to make a different copy
48 * of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and
49 * if the code using @regs updates a field (like the instruction pointer or
50 * stack pointer) it may need to propagate that change to the original @fregs
51 * it retrieved the partial @regs from. Use this function to guarantee that
52 * update happens.
53 */
54 static __always_inline void
55 ftrace_partial_regs_update(const struct ftrace_regs *fregs, struct pt_regs *regs)
56 {
> 57 ftrace_regs_set_instruction_pointer(fregs, instruction_pointer(regs));
58 ftrace_regs_set_return_value(fregs, regs_return_value(regs));
59 }
60 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Jiri,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Jiri-Olsa/selftests-bpf-Add-test-for-bpf_override_return-helper/20260109-174224 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20260109093454.389295-1-jolsa%40kernel.org patch subject: [PATCHv2 bpf-next 1/2] arm64/ftrace,bpf: Fix partial regs after bpf_prog_run config: riscv-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260110/[email protected]/config) compiler: riscv64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260110/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from ./include/linux/ftrace.h:115,
from arch/riscv/kernel/asm-offsets.c:11:
./include/linux/ftrace_regs.h: In function 'ftrace_partial_regs_update':
>> ./include/linux/ftrace_regs.h:57:45: warning: passing argument 1 of 'ftrace_regs_set_instruction_pointer' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
57 | ftrace_regs_set_instruction_pointer(fregs, instruction_pointer(regs));
| ^~~~~
In file included from ./include/linux/ftrace.h:23:
./arch/riscv/include/asm/ftrace.h:164:85: note: expected 'struct ftrace_regs *' but argument is of type 'const struct ftrace_regs *'
164 | static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
| ~~~~~~~~~~~~~~~~~~~~^~~~~
>> ./include/linux/ftrace_regs.h:58:38: warning: passing argument 1 of 'ftrace_regs_set_return_value' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
58 | ftrace_regs_set_return_value(fregs, regs_return_value(regs));
| ^~~~~ ./arch/riscv/include/asm/ftrace.h:198:78: note: expected 'struct ftrace_regs *' but argument is of type 'const struct ftrace_regs *'
198 | static __always_inline void ftrace_regs_set_return_value(struct ftrace_regs *fregs,
| ~~~~~~~~~~~~~~~~~~~~^~~~~
vim +57 ./include/linux/ftrace_regs.h
40
41 /*
42 * ftrace_partial_regs_update - update the original ftrace_regs from regs
43 * @fregs: The ftrace_regs to update from @regs
44 * @regs: The partial regs from ftrace_partial_regs() that was updated
45 *
46 * Some architectures have the partial regs living in the ftrace_regs
47 * structure, whereas other architectures need to make a different copy
48 * of the @regs. If a partial @regs is retrieved by ftrace_partial_regs() and
49 * if the code using @regs updates a field (like the instruction pointer or
50 * stack pointer) it may need to propagate that change to the original @fregs
51 * it retrieved the partial @regs from. Use this function to guarantee that
52 * update happens.
53 */
54 static __always_inline void
55 ftrace_partial_regs_update(const struct ftrace_regs *fregs, struct pt_regs *regs)
56 {
> 57 ftrace_regs_set_instruction_pointer(fregs, instruction_pointer(regs));
> 58 ftrace_regs_set_return_value(fregs, regs_return_value(regs));
59 }
60 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki