linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64
@ 2025-09-25  2:08 Feng Yang
  2025-09-25  3:33 ` Masami Hiramatsu
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Feng Yang @ 2025-09-25  2:08 UTC (permalink / raw)
  To: rostedt, mhiramat, mark.rutland, catalin.marinas, will, revest,
	alexei.starovoitov, olsajiri, andrii, ast
  Cc: linux-kernel, linux-trace-kernel, linux-arm-kernel, bpf

From: Feng Yang <yangfeng@kylinos.cn>

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")
Closes: https://lore.kernel.org/bpf/20250919071902.554223-1-yangfeng59949@163.com/
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
 arch/arm64/include/asm/ftrace.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h
index bfe3ce9df197..ba7cf7fec5e9 100644
--- a/arch/arm64/include/asm/ftrace.h
+++ b/arch/arm64/include/asm/ftrace.h
@@ -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;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64
  2025-09-25  2:08 [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64 Feng Yang
@ 2025-09-25  3:33 ` Masami Hiramatsu
  2025-09-25  7:14   ` Steven Rostedt
  2025-09-25  6:50 ` Jiri Olsa
  2025-09-25 15:32 ` Will Deacon
  2 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2025-09-25  3:33 UTC (permalink / raw)
  To: Feng Yang
  Cc: rostedt, mark.rutland, catalin.marinas, will, revest,
	alexei.starovoitov, olsajiri, andrii, ast, linux-kernel,
	linux-trace-kernel, linux-arm-kernel, bpf

On Thu, 25 Sep 2025 10:08:22 +0800
Feng Yang <yangfeng59949@163.com> wrote:

> From: Feng Yang <yangfeng@kylinos.cn>
> 
> 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")
> Closes: https://lore.kernel.org/bpf/20250919071902.554223-1-yangfeng59949@163.com/
> Signed-off-by: Feng Yang <yangfeng@kylinos.cn>

Thanks for fixing!

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

Thank you,

> ---
>  arch/arm64/include/asm/ftrace.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h
> index bfe3ce9df197..ba7cf7fec5e9 100644
> --- a/arch/arm64/include/asm/ftrace.h
> +++ b/arch/arm64/include/asm/ftrace.h
> @@ -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;
>  }
>  
> -- 
> 2.25.1
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64
  2025-09-25  2:08 [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64 Feng Yang
  2025-09-25  3:33 ` Masami Hiramatsu
@ 2025-09-25  6:50 ` Jiri Olsa
  2025-09-25 15:32 ` Will Deacon
  2 siblings, 0 replies; 5+ messages in thread
From: Jiri Olsa @ 2025-09-25  6:50 UTC (permalink / raw)
  To: Feng Yang
  Cc: rostedt, mhiramat, mark.rutland, catalin.marinas, will, revest,
	alexei.starovoitov, olsajiri, andrii, ast, linux-kernel,
	linux-trace-kernel, linux-arm-kernel, bpf

On Thu, Sep 25, 2025 at 10:08:22AM +0800, Feng Yang wrote:
> From: Feng Yang <yangfeng@kylinos.cn>
> 
> 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")
> Closes: https://lore.kernel.org/bpf/20250919071902.554223-1-yangfeng59949@163.com/
> Signed-off-by: Feng Yang <yangfeng@kylinos.cn>

Tested-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> ---
>  arch/arm64/include/asm/ftrace.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrace.h
> index bfe3ce9df197..ba7cf7fec5e9 100644
> --- a/arch/arm64/include/asm/ftrace.h
> +++ b/arch/arm64/include/asm/ftrace.h
> @@ -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;
>  }
>  
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64
  2025-09-25  3:33 ` Masami Hiramatsu
@ 2025-09-25  7:14   ` Steven Rostedt
  0 siblings, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2025-09-25  7:14 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Feng Yang, mark.rutland, catalin.marinas, will, revest,
	alexei.starovoitov, olsajiri, andrii, ast, linux-kernel,
	linux-trace-kernel, linux-arm-kernel, bpf

On Thu, 25 Sep 2025 12:33:31 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:

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

Correct.

-- Steve

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64
  2025-09-25  2:08 [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64 Feng Yang
  2025-09-25  3:33 ` Masami Hiramatsu
  2025-09-25  6:50 ` Jiri Olsa
@ 2025-09-25 15:32 ` Will Deacon
  2 siblings, 0 replies; 5+ messages in thread
From: Will Deacon @ 2025-09-25 15:32 UTC (permalink / raw)
  To: rostedt, mhiramat, mark.rutland, catalin.marinas, revest,
	olsajiri, andrii, ast, Feng Yang
  Cc: kernel-team, Will Deacon, linux-kernel, linux-trace-kernel,
	linux-arm-kernel, bpf

On Thu, 25 Sep 2025 10:08:22 +0800, Feng Yang wrote:
> 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.
> 
> [...]

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,
-- 
Will

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-09-25 15:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-25  2:08 [PATCH] tracing: Fix the bug where bpf_get_stackid returns -EFAULT on the ARM64 Feng Yang
2025-09-25  3:33 ` Masami Hiramatsu
2025-09-25  7:14   ` Steven Rostedt
2025-09-25  6:50 ` Jiri Olsa
2025-09-25 15:32 ` Will Deacon

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).