Skip to contentSkip to conversation

arm64/ftrace,bpf: Fix partial regs after bpf_prog_run

PATCH In review Jiri Olsa 6 participants 10 messages Nov 5, 2025 – Jan 7

Subsystems (8)
Kselftest BPF arm64 Tracing +4 more subsystems
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/2 arm64/ftrace,bpf: Fix partial regs after bpf_prog_run

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(+)
arch/arm64/include/asm/ftrace.h +11 -0
index ba7cf7fec5e9..ad6cf587885c 100644
@@ -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;		\
include/linux/ftrace.h +3 -0
index 7ded7df6e9b5..4cb1315522bb 100644
@@ -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
kernel/trace/bpf_trace.c +1 -0
index a795f7afbf3d..7b5768ced9b3 100644
@@ -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:
Signature · 1 line
2.51.1
author reviewer
#1 lore raw

PATCH 2/2 selftests/bpf: Add test for bpf_override_return helper

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.

---
 .../bpf/prog_tests/kprobe_multi_test.c        | 61 +++++++++++++++++++
 .../bpf/progs/kprobe_multi_override.c         | 15 +++++
 2 files changed, 76 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +61 -0
index 6cfaa978bc9a..b5e5cc54b89a 100644
@@ -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"))
tools/testing/selftests/bpf/progs/kprobe_multi_override.c +15 -0
index 28f8487c9059..14f39fa6d515 100644
@@ -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;
 }
Signature · 1 line
2.51.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] selftests/bpf: Add test for bpf_override_return helper

Quoted 11 lines · Jiri Olsa

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.

The test looks good to me.

#3 lore raw
In reply to Jiri Olsa

ping, thanks

jirka

Quoted 77 lines · Jiri Olsa

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(+)
arch/arm64/include/asm/ftrace.h +11 -0
index ba7cf7fec5e9..ad6cf587885c 100644
@@ -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;		\
include/linux/ftrace.h +3 -0
index 7ded7df6e9b5..4cb1315522bb 100644
@@ -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
kernel/trace/bpf_trace.c +1 -0
index a795f7afbf3d..7b5768ced9b3 100644
@@ -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:
Signature · 1 line
2.51.1
maintainer
#4 lore raw
In reply to Jiri Olsa
Quoted 34 lines · Jiri Olsa

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(+)
arch/arm64/include/asm/ftrace.h +0 -0
index ba7cf7fec5e9..ad6cf587885c 100644
@@ -157,6 +157,17 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	return regs;
 }
 

arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 169

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+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];
+	}
+}

This looks a bit grotty to me and presumably other architectures would need similar treatement. Wouldn't it be cleaner to reuse the existing API instead? For example, by calling ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() to update the relevant registers from the core code?

Will

#5 lore raw
Quoted 35 lines · Will Deacon
Quoted 34 lines · Jiri Olsa

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(+)
arch/arm64/include/asm/ftrace.h +0 -0
index ba7cf7fec5e9..ad6cf587885c 100644
@@ -157,6 +157,17 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	return regs;
 }
 
Quoted 16 lines

arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 169

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+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];
+	}
+}

This looks a bit grotty to me and presumably other architectures would need similar treatement. Wouldn't it be cleaner to reuse the existing API instead? For example, by calling ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() to update the relevant registers from the core code?

I knew I forgot some change.. thanks for replying

ftrace_partial_regs is overloaded in arm64 and because of that we need to propagate the change to pt_regs, so I think the ftrace_partial_regs_fix code is arm64 specific, so can't see that in core code

also wrt ftrace_partial_regs_fix name, I was thinking it might be better to have begin/end functions, like:

ftrace_partial_regs_begin
ftrace_partial_regs_end

thanks,
jirka

kernel/trace/bpf_trace.c +2 -1
@@ -2560,10 +2560,11 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
 	}
 
 	rcu_read_lock();
-	regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr());
+	regs = ftrace_partial_regs_begin(fregs, bpf_kprobe_multi_pt_regs_ptr());
 	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_end(fregs, bpf_kprobe_multi_pt_regs_ptr());
 	rcu_read_unlock();
 
  out:
#6 lore raw
In reply to Will Deacon
Quoted 35 lines · On Fri, 2 Jan 2026 14:52:25 +0000 Will Deacon <[email protected]> wrote:
Quoted 34 lines · Jiri Olsa

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(+)
arch/arm64/include/asm/ftrace.h +0 -0
index ba7cf7fec5e9..ad6cf587885c 100644
@@ -157,6 +157,17 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	return regs;
 }
 
Quoted 16 lines

arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 169

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+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];
+	}
+}

This looks a bit grotty to me and presumably other architectures would need similar treatement. Wouldn't it be cleaner to reuse the existing API instead? For example, by calling ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() to update the relevant registers from the core code?

I agreed with using the generic APIs. Also, ftrace_partial_regs_fix() is not self-explained. Maybe ftrace_regs_set_by_regs()?

Thank you,

Quoted 2 lines

Will

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#7 lore raw
Quoted 8 lines · On Sun, 4 Jan 2026 22:34:15 +0900 Masami Hiramatsu (Google)…
Quoted 5 lines

This looks a bit grotty to me and presumably other architectures would need similar treatement. Wouldn't it be cleaner to reuse the existing API instead? For example, by calling ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() to update the relevant registers from the core code?

I agreed with using the generic APIs. Also, ftrace_partial_regs_fix() is not self-explained. Maybe ftrace_regs_set_by_regs()?

Or perhaps: ftrace_partial_regs_update() where you call it if you need to update the 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];
	}
}

-- Steve

#8 lore raw
Quoted 38 lines · Steven Rostedt
Quoted 8 lines · On Sun, 4 Jan 2026 22:34:15 +0900 Masami Hiramatsu (Google)…
Quoted 5 lines

This looks a bit grotty to me and presumably other architectures would need similar treatement. Wouldn't it be cleaner to reuse the existing API instead? For example, by calling ftrace_regs_set_instruction_pointer() and ftrace_regs_set_return_value() to update the relevant registers from the core code?

I agreed with using the generic APIs. Also, ftrace_partial_regs_fix() is not self-explained. Maybe ftrace_regs_set_by_regs()?

Or perhaps: ftrace_partial_regs_update() where you call it if you need to update the 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];
	}
}
Quoted 1 line

-- Steve

lgtm, the full change is below, I'll send new version of the patchset

thanks,
jirka

---

arch/arm64/include/asm/ftrace.h +24 -0
index 1621c84f44b3..177c7bbf3b84 100644
@@ -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
index 770f0dc993cc..ae22559b4099 100644
@@ -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
index 6e076485bf70..3a17f79b20c2 100644
@@ -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
maintainer
#9 lore raw
Quoted 12 lines · On Wed, 7 Jan 2026 09:23:01 +0100 Jiri Olsa <[email protected]> wrote:

---

arch/arm64/include/asm/ftrace.h +3 -0
index 1621c84f44b3..177c7bbf3b84 100644
@@ -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

arch/arm64/include/asm/ftrace.h ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs) · line 172

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+ * @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.
+ */

I think the kernel doc should go into the generic function, and this just say:

/* See description of this function in the generic header */

or something similar.

Also, should probably have the description in the general function say:

*
* For architectures that need to update @fregs from @regs, this function needs to be
* defined.

Or something similar.

-- Steve

Quoted 41 lines
arch/arm64/include/asm/ftrace.h +11 -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];
+	}
+}
+
 #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
index 770f0dc993cc..ae22559b4099 100644
@@ -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
index 6e076485bf70..3a17f79b20c2 100644
@@ -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: