Re: [PATCH 4/4] alpha: add HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP support

From: Magnus Lindholm

Date: Mon Oct 12 2026 - 13:39:07 EST


Hi Matt,

On Sun, Oct 11, 2026 at 10:16 PM Matt Turner <mattst88@xxxxxxxxx> wrote:
>
> Let perf sample registers and dump the user stack, which is what the
> perf tool needs for --call-graph dwarf.
>
> The register indices follow the hardware numbering, so r0-r30 map
> directly to DWARF register numbers, with pc after them. r9-r15
> are callee-saved and live in switch_stack rather than pt_regs, so they
> cannot be sampled. Without r15 an unwinder cannot recover the CFA of a
> first frame that uses $15 as its frame pointer.
>
> sp is not in pt_regs either: for user frames it comes from
> user_stack_pointer(), and for kernel frames (PERF_SAMPLE_REGS_INTR) it
> is the end of the trap frame, as in stacktrace.c.
>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Matt Turner <mattst88@xxxxxxxxx>
> ---
> .../features/perf/perf-regs/arch-support.txt | 2 +-
> .../features/perf/perf-stackdump/arch-support.txt | 2 +-
> arch/alpha/Kconfig | 2 +
> arch/alpha/include/uapi/asm/perf_regs.h | 45 +++++++++++++++++
> arch/alpha/kernel/Makefile | 1 +
> arch/alpha/kernel/perf_regs.c | 56 ++++++++++++++++++++++
> 6 files changed, 106 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/features/perf/perf-regs/arch-support.txt b/Documentation/features/perf/perf-regs/arch-support.txt
> index 4c9a5a012075..7d736d678fd4 100644
> --- a/Documentation/features/perf/perf-regs/arch-support.txt
> +++ b/Documentation/features/perf/perf-regs/arch-support.txt
> @@ -6,7 +6,7 @@
> -----------------------
> | arch |status|
> -----------------------
> - | alpha: | TODO |
> + | alpha: | ok |
> | arc: | TODO |
> | arm: | ok |
> | arm64: | ok |
> diff --git a/Documentation/features/perf/perf-stackdump/arch-support.txt b/Documentation/features/perf/perf-stackdump/arch-support.txt
> index dd362b5cb638..17b84ea7769d 100644
> --- a/Documentation/features/perf/perf-stackdump/arch-support.txt
> +++ b/Documentation/features/perf/perf-stackdump/arch-support.txt
> @@ -6,7 +6,7 @@
> -----------------------
> | arch |status|
> -----------------------
> - | alpha: | TODO |
> + | alpha: | ok |
> | arc: | TODO |
> | arm: | ok |
> | arm64: | ok |
> diff --git a/arch/alpha/Kconfig b/arch/alpha/Kconfig
> index b2c359b947f6..8fafbfabd1e5 100644
> --- a/arch/alpha/Kconfig
> +++ b/arch/alpha/Kconfig
> @@ -41,6 +41,8 @@ config ALPHA
> select HAVE_PAGE_SIZE_8KB
> select HAVE_PCSPKR_PLATFORM
> select HAVE_PERF_EVENTS
> + select HAVE_PERF_REGS
> + select HAVE_PERF_USER_STACK_DUMP
> select NEED_DMA_MAP_STATE
> select NEED_SG_DMA_LENGTH
> select GENERIC_IRQ_PROBE
> diff --git a/arch/alpha/include/uapi/asm/perf_regs.h b/arch/alpha/include/uapi/asm/perf_regs.h
> new file mode 100644
> index 000000000000..da96a31b12fb
> --- /dev/null
> +++ b/arch/alpha/include/uapi/asm/perf_regs.h
> @@ -0,0 +1,45 @@
> +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
> +#ifndef _UAPI_ASM_ALPHA_PERF_REGS_H
> +#define _UAPI_ASM_ALPHA_PERF_REGS_H
> +
> +/*
> + * Indices 0-30 match the hardware (and DWARF) register numbers.
> + * r9-r15 are not saved in pt_regs and cannot be sampled.
> + */
> +enum perf_event_alpha_regs {
> + PERF_REG_ALPHA_R0,
> + PERF_REG_ALPHA_R1,
> + PERF_REG_ALPHA_R2,
> + PERF_REG_ALPHA_R3,
> + PERF_REG_ALPHA_R4,
> + PERF_REG_ALPHA_R5,
> + PERF_REG_ALPHA_R6,
> + PERF_REG_ALPHA_R7,
> + PERF_REG_ALPHA_R8,
> + PERF_REG_ALPHA_R9,
> + PERF_REG_ALPHA_R10,
> + PERF_REG_ALPHA_R11,
> + PERF_REG_ALPHA_R12,
> + PERF_REG_ALPHA_R13,
> + PERF_REG_ALPHA_R14,
> + PERF_REG_ALPHA_R15,
> + PERF_REG_ALPHA_R16,
> + PERF_REG_ALPHA_R17,
> + PERF_REG_ALPHA_R18,
> + PERF_REG_ALPHA_R19,
> + PERF_REG_ALPHA_R20,
> + PERF_REG_ALPHA_R21,
> + PERF_REG_ALPHA_R22,
> + PERF_REG_ALPHA_R23,
> + PERF_REG_ALPHA_R24,
> + PERF_REG_ALPHA_R25,
> + PERF_REG_ALPHA_R26,
> + PERF_REG_ALPHA_R27,
> + PERF_REG_ALPHA_R28,
> + PERF_REG_ALPHA_GP,
> + PERF_REG_ALPHA_SP,
> + PERF_REG_ALPHA_PC,
> + PERF_REG_ALPHA_MAX,
> +};
> +
> +#endif /* _UAPI_ASM_ALPHA_PERF_REGS_H */
> diff --git a/arch/alpha/kernel/Makefile b/arch/alpha/kernel/Makefile
> index 4ea5c189e60e..0c04b89676ed 100644
> --- a/arch/alpha/kernel/Makefile
> +++ b/arch/alpha/kernel/Makefile
> @@ -18,6 +18,7 @@ obj-$(CONFIG_PCI) += pci.o pci_iommu.o pci-sysfs.o
> obj-$(CONFIG_SRM_ENV) += srm_env.o
> obj-$(CONFIG_MODULES) += module.o
> obj-$(CONFIG_PERF_EVENTS) += perf_event.o
> +obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o
> obj-$(CONFIG_RTC_DRV_ALPHA) += rtc.o
> obj-$(CONFIG_AUDIT) += audit.o
>
> diff --git a/arch/alpha/kernel/perf_regs.c b/arch/alpha/kernel/perf_regs.c
> new file mode 100644
> index 000000000000..10afe4e205e9
> --- /dev/null
> +++ b/arch/alpha/kernel/perf_regs.c
> @@ -0,0 +1,56 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/bits.h>
> +#include <linux/perf_event.h>
> +#include <asm/perf_regs.h>
> +
> +#define REG(idx, field) \
> + [PERF_REG_ALPHA_##idx] = offsetof(struct pt_regs, field)
> +
> +#define REG_RESERVED (~GENMASK_ULL(PERF_REG_ALPHA_MAX - 1, 0) | \
> + GENMASK_ULL(PERF_REG_ALPHA_R15, PERF_REG_ALPHA_R9))
> +
> +static const unsigned int pt_regs_offset[PERF_REG_ALPHA_MAX] = {
> + REG(R0, r0), REG(R1, r1), REG(R2, r2), REG(R3, r3),
> + REG(R4, r4), REG(R5, r5), REG(R6, r6), REG(R7, r7),
> + REG(R8, r8), REG(R16, r16), REG(R17, r17), REG(R18, r18),
> + REG(R19, r19), REG(R20, r20), REG(R21, r21), REG(R22, r22),
> + REG(R23, r23), REG(R24, r24), REG(R25, r25), REG(R26, r26),
> + REG(R27, r27), REG(R28, r28), REG(GP, gp), REG(PC, pc),
> +};
> +
> +u64 perf_reg_value(struct pt_regs *regs, int idx)
> +{
> + if (idx == PERF_REG_ALPHA_SP) {
> + if (user_mode(regs))
> + return user_stack_pointer(regs);
> + /* Same kernel sp approximation as stacktrace.c. */
> + return (unsigned long)(regs + 1);
> + }
> +

Tested on my ES40: register masks, SP/PC sampling and user stack dumps
passed with software-clock, page-fault and hardware-cycle events.

For context-switch events, however, regs points to the per-CPU
__perf_regs[0], not an on-stack trap frame. This therefore returns
a per-CPU address as SP.

I reproduced this with:

perf record -e context-switches -c 1 -Isp,pc -- sleep 1

The sample reported SP=0xfffffc00045b4310 and PC=0. The missing PC
predates this patch, but the incorrect SP is newly exposed here.

One limitation I noticed: for context-switch events, regs points to
the per-CPU synthetic frame, so regs + 1 reports its address rather
than a kernel SP. Alpha already lacks perf_arch_fetch_caller_regs(),
so perhaps synthetic-event register capture is better handled as a
follow-up. Could we document that limitation here?


> + if (WARN_ON_ONCE((u32)idx >= PERF_REG_ALPHA_MAX ||
> + BIT_ULL(idx) & REG_RESERVED))
> + return 0;
> +
> + return *(unsigned long *)((void *)regs + pt_regs_offset[idx]);
> +}
> +
> +int perf_reg_validate(u64 mask)
> +{
> + if (!mask || mask & REG_RESERVED)
> + return -EINVAL;
> +
> + return 0;
> +}
> +
> +u64 perf_reg_abi(struct task_struct *task)
> +{
> + return PERF_SAMPLE_REGS_ABI_64;
> +}
> +
> +void perf_get_regs_user(struct perf_regs *regs_user,
> + struct pt_regs *regs)
> +{
> + regs_user->regs = task_pt_regs(current);
> + regs_user->abi = perf_reg_abi(current);
> +}
>
> --
> 2.55.0
>

Tested-by: Magnus Lindholm <linmag7@xxxxxxxxx>