Skip to contentSkip to conversation

tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64

PATCH Applied Feng Yang 5 participants 5 messages Sep 25, 2025

Subsystems (4)
lore
Share & tools
lore

Feng Yang

author
#0 lore raw

PATCH 1/1 tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64

From: Feng Yang <[email protected]>

When using bpf_program__attach_kprobe_multi_opts on ARM64 to hook a BPF program that contains the bpf_get_stackid function, the BPF program fails to obtain the stack trace and returns -EFAULT.

This is because ftrace_partial_regs omits the configuration of the pstate register, leaving pstate at the default value of 0. When get_perf_callchain executes, it uses user_mode(regs) to determine whether it is in kernel mode. This leads to a misjudgment that the code is in user mode,
so perf_callchain_kernel is not executed and the function returns directly. As a result, trace->nr becomes 0, and finally -EFAULT is returned.

Therefore, the assignment of the pstate register is added here.

Fixes b9b55c8912ce ("tracing: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs")
---
 arch/arm64/include/asm/ftrace.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/ftrace.h +1 -0
index bfe3ce9df197..ba7cf7fec5e9 100644
@@ -153,6 +153,7 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	regs->pc = afregs->pc;
 	regs->regs[29] = afregs->fp;
 	regs->regs[30] = afregs->lr;
+	regs->pstate = PSR_MODE_EL1h;
 	return regs;
 }
 
Signature · 1 line
2.25.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · On Thu, 25 Sep 2025 10:08:22 +0800 Feng Yang <[email protected]> wrote:

From: Feng Yang <[email protected]>

When using bpf_program__attach_kprobe_multi_opts on ARM64 to hook a BPF program that contains the bpf_get_stackid function, the BPF program fails to obtain the stack trace and returns -EFAULT.

This is because ftrace_partial_regs omits the configuration of the pstate register, leaving pstate at the default value of 0. When get_perf_callchain executes, it uses user_mode(regs) to determine whether it is in kernel mode. This leads to a misjudgment that the code is in user mode,
so perf_callchain_kernel is not executed and the function returns directly. As a result, trace->nr becomes 0, and finally -EFAULT is returned.

Quoted 5 lines

Therefore, the assignment of the pstate register is added here.

Fixes b9b55c8912ce ("tracing: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs")

Thanks for fixing!

This is actually a fix for arm64. So I think it will be merged via arm64 tree, right?

Thank you,

Quoted 19 lines
---
 arch/arm64/include/asm/ftrace.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/ftrace.h +1 -0
index bfe3ce9df197..ba7cf7fec5e9 100644
@@ -153,6 +153,7 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	regs->pc = afregs->pc;
 	regs->regs[29] = afregs->fp;
 	regs->regs[30] = afregs->lr;
+	regs->pstate = PSR_MODE_EL1h;
 	return regs;
 }
 
Signature · 1 line
2.25.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#2 lore raw
Quoted 2 lines · On Thu, 25 Sep 2025 12:33:31 +0900 Masami Hiramatsu (Google)…

This is actually a fix for arm64. So I think it will be merged via arm64 tree, right?

Correct.

-- Steve

Tested-by on v1 1/1
#3 lore raw
In reply to Feng Yang
Quoted 13 lines · Feng Yang

From: Feng Yang <[email protected]>

When using bpf_program__attach_kprobe_multi_opts on ARM64 to hook a BPF program that contains the bpf_get_stackid function, the BPF program fails to obtain the stack trace and returns -EFAULT.

This is because ftrace_partial_regs omits the configuration of the pstate register, leaving pstate at the default value of 0. When get_perf_callchain executes, it uses user_mode(regs) to determine whether it is in kernel mode. This leads to a misjudgment that the code is in user mode,
so perf_callchain_kernel is not executed and the function returns directly. As a result, trace->nr becomes 0, and finally -EFAULT is returned.

Quoted 5 lines

Therefore, the assignment of the pstate register is added here.

Fixes b9b55c8912ce ("tracing: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs")

thanks,
jirka

Quoted 19 lines
---
 arch/arm64/include/asm/ftrace.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/ftrace.h +1 -0
index bfe3ce9df197..ba7cf7fec5e9 100644
@@ -153,6 +153,7 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
 	regs->pc = afregs->pc;
 	regs->regs[29] = afregs->fp;
 	regs->regs[30] = afregs->lr;
+	regs->pstate = PSR_MODE_EL1h;
 	return regs;
 }
 
Signature · 1 line
2.25.1
maintainer Applied
#4 lore raw
In reply to Feng Yang
Quoted 4 lines · Feng Yang

When using bpf_program__attach_kprobe_multi_opts on ARM64 to hook a BPF program that contains the bpf_get_stackid function, the BPF program fails to obtain the stack trace and returns -EFAULT.

Quoted 8 lines

This is because ftrace_partial_regs omits the configuration of the pstate register, leaving pstate at the default value of 0. When get_perf_callchain executes, it uses user_mode(regs) to determine whether it is in kernel mode. This leads to a misjudgment that the code is in user mode,
so perf_callchain_kernel is not executed and the function returns directly. As a result, trace->nr becomes 0, and finally -EFAULT is returned.

[...]

Applied to arm64 (for-next/core), thanks!

[1/1] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64

      https://git.kernel.org/arm64/c/fd2f74f8f3d3

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev