perf: user stack dump on sparc64 needs an arch hook
Subsystems (2)
cover perf: user stack dump on sparc64 needs an arch hook
I am adding HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP to sparc64, so that perf record --call-graph dwarf and elfutils' eu-stackprof work there. The sparc side (patch 2) is straightforward and follows parisc. One thing does not fit in arch code, and I would like to get the shape of that agreed before sending the rest.
The user stack dump copies the stack as it is in memory and assumes the call chain is there. On sparc it may not be: the sampled register window's %l/%i registers, which hold the frame pointer and return address the unwinder starts from (the CFI after save defines the CFA in terms of %i6), stay in the register file until a window spills. The kernel already deals with this wherever it exposes user stack memory:perf_callchain_user() on sparc calls flushw_user() before walking the chain, and ptrace does the same. The stack dump has no arch entry point where that could happen.
I looked for a sparc-only way and did not find a correct one:
- flushing in the sparc PMU interrupt handler misses software events (cpu-clock, tracepoints), which reach perf_event_overflow() without passing through it;
- perf_user_stack_pointer() is private to kernel/events/internal.h, so the arch cannot override it;
- perf_reg_abi() is called at the right time but is a query, and a flush as a side effect of it would be wrong.
So patch 1 adds a no-op hook in the style of perf_arch_misc_flags():perf_arch_prepare_ustack(), called from perf_prepare_sample() when PERF_SAMPLE_STACK_USER is requested and user regs exist. Patch 2 is the sparc64 implementation and its user. Happy to take a different name or placement.
Tested on an UltraSPARC T4-1 on 7.3-rc4 with a perf tool taught the sparc registers (that patch, and the matching elfutils backend, follow once the hook is settled): register values check out against known contents, and --call-graph dwarf unwinds correctly for both cycles and cpu-clock. perf stat/record/record -g are unchanged.
Stian Halseth (2):
perf/core: Let an arch prepare the user stack before it is dumped
sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
arch/sparc/Kconfig | 2 + arch/sparc/include/asm/perf_event.h | 3 ++ arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++++++ arch/sparc/kernel/Makefile | 2 +- arch/sparc/kernel/perf_regs.c | 65 +++++++++++++++++++++++++ include/linux/perf_event.h | 7 +++ kernel/events/core.c | 2 + 7 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 arch/sparc/kernel/perf_regs.c
--
2.55.0
RFC PATCH 1/2 perf/core: Let an arch prepare the user stack before it is dumped
PERF_SAMPLE_STACK_USER copies the user stack as it is in memory, which assumes the whole call chain is there. On sparc the sampled window's %l and %i registers, including the frame pointer and return address an unwinder starts from, may still be in the register file: they only reach the stack when a window spills. perf_callchain_user() already handles this for the callchain by calling flushw_user() first; the user stack dump has no equivalent arch entry point.
Add perf_arch_prepare_ustack(), a no-op by default, called from perf_prepare_sample() before the dump size is computed, so an arch can write back whatever part of the user's stack state it still holds in registers. Doing this in the arch PMU interrupt handler would not do:
software events such as cpu-clock reach perf_event_overflow() without passing through it.
--- include/linux/perf_event.h | 7 +++++++ kernel/events/core.c | 2 ++ 2 files changed, 9 insertions(+)
include/linux/perf_event.h +7 -0
@@ -1815,6 +1815,13 @@ extern unsigned long perf_instruction_pointer(struct perf_event *event,#ifndef perf_arch_bpf_user_pt_regs# define perf_arch_bpf_user_pt_regs (regs) regs#endif +/* +* Called before the user stack of the current task is dumped, for an arch +* that still holds part of the user's stack state in registers. +*/ +#ifndef perf_arch_prepare_ustack +static inline void perf_arch_prepare_ustack (void ) { } +#endif #ifndef perf_arch_guest_misc_flagsstatic inline unsigned long perf_arch_guest_misc_flags (struct pt_regs *regs)
kernel/events/core.c +2 -0
@@ -8718,6 +8718,8 @@ void perf_prepare_sample(struct perf_sample_data *data,u16 header_size =perf_sample_data_size (data, event);u16 size =sizeof (u64); +if (data->regs_user.regs) +perf_arch_prepare_ustack (); stack_size =perf_sample_ustack_size (stack_size, header_size, data->regs_user.regs);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [RFC PATCH 1/2] perf/core: Let an arch prepare the user stack before it is dumped
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
PERF_SAMPLE_STACK_USERcopies the user stack as it is in memory, which assumes the whole call chain is there. On sparc the sampled window's %l and %i registers, including the frame pointer and return address an unwinder starts from, may still be in the register file: they only reach the stack when a window spills.perf_callchain_user()already handles this for the callchain by callingflushw_user()first; the user stack dump has no equivalent arch entry point.
Quoted 8 lines
Add
perf_arch_prepare_ustack(), a no-op by default, called fromperf_prepare_sample()before the dump size is computed, so an arch can write back whatever part of the user's stack state it still holds in registers. Doing this in the arch PMU interrupt handler would not do:
software events such as cpu-clock reachperf_event_overflow()without passing through it.Signed-off-by Stian Halseth <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/2 sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
In reply to Stian HalsethSelect HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP and add the perf_regs implementation. The exposed registers mirror struct pt_regs:
%g0-%g7, %o0-%o7 (named from the user's point of view; pt_regs calls them UREG_I*), %tstate, PC, NPC and %y. %o6 is reported as held in the register, i.e. with the 2047 stack bias for a 64-bit task, the same as ptrace and user_stack_pointer(). Values of a 32-bit task are truncated to 32 bits as genregs32_get() does, since the hardware does not zero-extend them.
perf_arch_prepare_ustack() flushes the user register windows so the sampled window's %l and %i registers are in the dumped stack, where a DWARF unwinder needs them: the CFI of a function after save defines the CFA in terms of %i6.
This makes perf record --call-graph dwarf and eu-stackprof usable on sparc64.
--- arch/sparc/Kconfig | 2 + arch/sparc/include/asm/perf_event.h | 3 ++ arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++++++ arch/sparc/kernel/Makefile | 2 +- arch/sparc/kernel/perf_regs.c | 65 +++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 arch/sparc/kernel/perf_regs.c
arch/sparc/Kconfig +2 -0
@@ -93,6 +93,8 @@ config SPARC64 select RTC_DRV_SUN4V select RTC_DRV_STARFIRE select HAVE_PERF_EVENTS + select HAVE_PERF_REGS + select HAVE_PERF_USER_STACK_DUMP select PERF_USE_VMALLOC select ARCH_HAVE_NMI_SAFE_CMPXCHG select HAVE_C_RECORDMCOUNT
arch/sparc/include/asm/perf_event.h +3 -0
@@ -25,6 +25,9 @@ do { \ (regs)->u_regs[UREG_I6 ] = _fp; \ (regs)->u_regs[UREG_I7 ] = _i7; \ } while (0) + +void perf_arch_prepare_ustack(void); +#define perf_arch_prepare_ustack perf_arch_prepare_ustack#endif #endif
arch/sparc/include/uapi/asm/perf_regs.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_SPARC_PERF_REGS_H +#define _UAPI_ASM_SPARC_PERF_REGS_H + +/* +* Mirrors struct pt_regs. O6 is %sp as held in the register, i.e. biased +* by 2047 for a 64-bit task. +*/ +enum perf_event_sparc_regs { +PERF_REG_SPARC_G0 , +PERF_REG_SPARC_G1 , +PERF_REG_SPARC_G2 , +PERF_REG_SPARC_G3 , +PERF_REG_SPARC_G4 , +PERF_REG_SPARC_G5 , +PERF_REG_SPARC_G6 , +PERF_REG_SPARC_G7 , +PERF_REG_SPARC_O0 , +PERF_REG_SPARC_O1 , +PERF_REG_SPARC_O2 , +PERF_REG_SPARC_O3 , +PERF_REG_SPARC_O4 , +PERF_REG_SPARC_O5 , +PERF_REG_SPARC_O6 , +PERF_REG_SPARC_O7 , +PERF_REG_SPARC_TSTATE , +PERF_REG_SPARC_PC , +PERF_REG_SPARC_NPC , +PERF_REG_SPARC_Y , +PERF_REG_SPARC_MAX +}; + +#endif /* _UAPI_ASM_SPARC_PERF_REGS_H */
arch/sparc/kernel/Makefile +1 -1
@@ -113,7 +113,7 @@ obj-$(CONFIG_AUDIT) += audit.o audit--$(CONFIG_AUDIT) := compat_audit.o obj-$(CONFIG_COMPAT) += $(audit--y) -pc--$(CONFIG_PERF_EVENTS) := perf_event.o +pc--$(CONFIG_PERF_EVENTS) := perf_event.o perf_regs.o obj-$(CONFIG_SPARC64) += $(pc--y) obj-$(CONFIG_UPROBES) += uprobes.o
arch/sparc/kernel/perf_regs.c new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [RFC PATCH 2/2] sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf: sparc64 user stack dumps read from unmapped biased pointer - [Medium] perf: sparc64…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf: sparc64 user stack dumps read from unmapped biased pointer
- [Medium] perf: sparc64 truncates 64-bit kernel registers for 32-bit compat tasks --
commit 549c908d74e0e448486cd60566d4f3e6ff5d6f8e
Author: Stian Halseth <[email protected]>
sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
This commit adds support for PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER on sparc64. It selects HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP, implements the perf_regs interface to expose registers mirroring pt_regs, and provides perf_arch_prepare_ustack() to flush user register windows.
Quoted 4 lines
arch/sparc/include/asm/perf_event.h +0
-0
[ ... ]
arch/sparc/include/asm/perf_event.h do { \ · line 30
Quoted 7 lines
arch/sparc/include/asm/perf_event.h +3
-0@@ -25,6 +25,9 @@ do { \ (regs)->u_regs[UREG_I6 ] = _fp; \ (regs)->u_regs[UREG_I7 ] = _i7; \ }while (0 ) + +void perf_arch_prepare_ustack(void); +#define perf_arch_prepare_ustack perf_arch_prepare_ustack
[Severity: Medium]
Since HAVE_PERF_USER_STACK_DUMP is selected in Kconfig, shouldn't there also be an override for perf_user_stack_pointer() here to account for STACK_BIAS on 64-bit tasks?
When a user requests a perf record with user stack sampling (--call-graph dwarf) on a 64-bit SPARC process, the generic perf core attempts to copy memory from the address returned by perf_user_stack_pointer(regs). On 64-bit SPARC, the stack pointer register %o6 (UREG_FP) is biased by 2047 bytes.
Without overriding perf_user_stack_pointer() to add STACK_BIAS, the core perf code will attempt to read memory starting 2047 bytes below the valid stack. Does this cause the copy to fail immediately upon hitting an unmapped guard page, resulting in an empty stack dump?
Quoted 6 lines
arch/sparc/kernel/perf_regs.c new file +0
-0@@ -0,0 +1,65 @@
[ ... ]
Quoted 5 lines
arch/sparc/kernel/perf_regs.c new file +5
-0+u64 perf_reg_value (struct pt_regs *regs,int idx) +{ +u64 val; + +switch (idx) {
[ ... ]
arch/sparc/kernel/perf_regs.c line 32
Quoted 9 lines
arch/sparc/kernel/perf_regs.c new file +9
-0+case PERF_REG_SPARC_Y : +return regs->y; +default : +WARN_ON_ONCE (1 ); +return 0 ; + } + +if (test_thread_flag (TIF_32BIT )) + val = (u32 )val;
[Severity: Medium]
Will this unconditionally truncate 64-bit kernel registers for 32-bit compat tasks?
When profiling kernel state via PERF_SAMPLE_REGS_INTR, the interrupted registers belong to the 64-bit kernel mode, even if the current task is a 32-bit compat task (for instance, when servicing a system call).
Should this truncation be conditional on user_mode(regs) to avoid destroying kernel register samples and breaking unwinding and symbol resolution for kernel execution?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RFC PATCH 2/2] sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Quoted 7 lines · On Tue, 2026-09-22 at 14:18 +0000, [email protected] wrote:
[Severity: Medium]
SinceHAVE_PERF_USER_STACK_DUMPis selected inKconfig, shouldn't there also
be an override forperf_user_stack_pointer()here to account forSTACK_BIAS
on 64-bit tasks?
Nothing to override. perf_user_stack_pointer() is a plain macro in kernel/events/internal.h. Also, I don't think we should, as long as every consumer takes the sampled stack pointer register as the base.
sparc-aware consumers add the 2047 when they interpret the stack. Like for example ptrace data. It would make sparc the only arch where dump doesn't begin at the SP register.
Quoted 5 lines
[Severity: Medium]
Will this unconditionally truncate 64-bit kernel registers for 32-bit compat
tasks?
Yes, will correct that.
Next version truncates only when user_mode(regs), the same test arm64 makes with compat_user_mode().
Hi Stian,
Quoted 56 lines · Stian Halseth
I am adding
HAVE_PERF_REGSandHAVE_PERF_USER_STACK_DUMPto sparc64, so that perf record --call-graph dwarf and elfutils' eu-stackprof work there. The sparc side (patch 2) is straightforward and follows parisc. One thing does not fit in arch code, and I would like to get the shape of that agreed before sending the rest.The user stack dump copies the stack as it is in memory and assumes the call chain is there. On sparc it may not be: the sampled register window's %l/%i registers, which hold the frame pointer and return address the unwinder starts from (the CFI after
savedefines the CFA in terms of %i6), stay in the register file until a window spills. The kernel already deals with this wherever it exposes user stack memory:perf_callchain_user()on sparc callsflushw_user()before walking the chain, and ptrace does the same. The stack dump has no arch entry point where that could happen.I looked for a sparc-only way and did not find a correct one:
- flushing in the sparc PMU interrupt handler misses software events (cpu-clock, tracepoints), which reach
perf_event_overflow()without passing through it;
-perf_user_stack_pointer()is private tokernel/events/internal.h, so the arch cannot override it;
-perf_reg_abi()is called at the right time but is a query, and a flush as a side effect of it would be wrong.So patch 1 adds a no-op hook in the style of
perf_arch_misc_flags():perf_arch_prepare_ustack(), called fromperf_prepare_sample()whenPERF_SAMPLE_STACK_USERis requested and user regs exist. Patch 2 is the sparc64 implementation and its user. Happy to take a different name or placement.Tested on an UltraSPARC T4-1 on 7.3-rc4 with a perf tool taught the sparc registers (that patch, and the matching elfutils backend, follow once the hook is settled): register values check out against known contents, and --call-graph dwarf unwinds correctly for both cycles and cpu-clock. perf stat/record/record -g are unchanged.
Stian Halseth (2):
perf/core: Let an arch prepare the user stack before it is dumped
sparc64: SupportPERF_SAMPLE_REGS_USERandPERF_SAMPLE_STACK_USERarch/sparc/Kconfig | 2 + arch/sparc/include/asm/perf_event.h | 3 ++ arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++++++ arch/sparc/kernel/Makefile | 2 +- arch/sparc/kernel/perf_regs.c | 65 +++++++++++++++++++++++++ include/linux/perf_event.h | 7 +++ kernel/events/core.c | 2 + 7 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 arch/sparc/kernel/perf_regs.c
Quoted 3 lines
--
2.55.0
Very nice series, thanks for working on this!
I built and booted the series on a sparc64 box and haven't noticed any regressions so far.
I also smoke-tested the new interfaces from 64-bit userspace with a small standalone C program using perf_event_open() and software cpu-clock events. The register-only, stack-only, and combined register/stack tests all passed. The test does not use tools/perf or request PERF_SAMPLE_CALLCHAIN. I haven't tested end-to-end DWARF unwinding yet.
I see that you plan to post the userspace changes once the hook is settled. Could you include the tools/perf support in the next revision, or post it as a follow-up or linked companion series? Having that available, along with a link to the matching elfutils changes, would make testing easier and let us exercise the intended DWARF unwinding use case directly.
Also, could you update the sparc entries from TODO to ok in:
Documentation/features/perf/perf-regs/arch-support.txt Documentation/features/perf/perf-stackdump/arch-support.txt
Those updates would fit naturally in patch 2 alongside the HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP selections.
Thanks,
Magnus
Hi Magnus, thanks for the testing and feedback.
Almost done with a v2.
It has some minor changes based on the AI bot feedback, and my comments to you below.
Quoted 13 lines · Magnus Lindholm
Very nice series, thanks for working on this!
I built and booted the series on a sparc64 box and haven't noticed any regressions so far.
I also smoke-tested the new interfaces from 64-bit userspace with a small standalone C program using
perf_event_open()and software cpu-clock events. The register-only, stack-only, and combined register/stack tests all passed. The test does not usetools/perfor requestPERF_SAMPLE_CALLCHAIN. I haven't tested end-to-end DWARF unwinding yet.
Quoted 7 lines
I see that you plan to post the userspace changes once the hook is settled. Could you include the
tools/perfsupport in the next revision,
or post it as a follow-up or linked companion series? Having that available, along with a link to the matching elfutils changes, would make testing easier and let us exercise the intended DWARF unwinding use case directly.
Yes, I originally planned to post tools/perf after I got some feedback on the first patches. Especially since it touches some generic code that I'm not too familiar with, and I half expect that I have to rewrite some of it based on maintainer feedback :-)
But since its "just" a RFC patch at this point, I guess I can post the full series to allow end-to-end testing.
I have mainly focused on the first patches, and the rest was done a bit faster as a means to validate the "core" changes. It works (TM). I'm looking through it now, but I probably make a second thorough review of the entire final series, once the I know what changes I need to make.
I will publish and link to the elfutils changes as well.
Quoted 8 lines
Also, could you update the sparc entries from TODO to ok in:
Documentation/features/perf/perf-regs/arch-support.txtDocumentation/features/perf/perf-stackdump/arch-support.txtThose updates would fit naturally in patch 2 alongside the
HAVE_PERF_REGSandHAVE_PERF_USER_STACK_DUMPselections.
Yes, agree.
Signature · 3 lines
Best regards Stian Halseth