alpha/perf: add ProfileMe sampling
Subsystems (3)
Matt Turner
maintainerv3 cover alpha/perf: add ProfileMe sampling
The 21264 from EV67 on, and the 21364, have a second performance counter mode, ProfileMe, alongside the Aggregate mode that the driver uses today. An Aggregate mode sample reports the PC where the overflow interrupt was taken. On an EV7 that is anywhere from 15 instructions before to 12 after the one that overflowed the counter, which makes perf annotate output close to useless. In ProfileMe mode the CPU picks one fetched instruction, follows it down the pipeline, and reports its PC along with what happened to it: whether it retired, whether it trapped and why, whether it was a mispredicted branch, stalled before map, or was killed on a wrong path, and what PCTR1 counted while it was in flight.
Patch 1 fixes a constant passed to the wrong PALcode call, with no functional change. Patch 2 adds the sysfs format and event attributes the perf tool needs to name raw events. Patch 3 fixes counts lost when an event is stopped before its first start, and patch 4 stops reporting user registers that the entry code has not saved yet. Patch 5 adds ProfileMe sampling, selected with precise_ip or a new profileme config bit, so cycles:p and perf record's default event use it. Patch 6 attaches the status registers to each sample as PERF_SAMPLE_RAW data, and patch 7 adds an option to randomize the sampling interval so that it cannot lock onto a loop.
A separate series for perf-tools-next adds a decoder for the raw data to perf report -D and perf script -D, and a script that sums the status by symbol or by instruction, in the style of DCPI.
The two modes cannot run at the same time, because PCTR_CTL[SL0] sets the mode for both counters. A ProfileMe event is scheduled alone, and perf multiplexes it with any other hardware events. perf stat does not ask for precise events, so it keeps using Aggregate mode.
To try it:
perf record -e cycles:p -- ./workload
perf annotate
I tested this on an AlphaServer ES47 (EV7). Samples from a 16-instruction loop land on all 16 instructions rather than on 4 PCs. DTB misses and unaligned traps are attributed to the faulting load. A branch on random data shows up as mispredicted in 21% of its samples, and in 0% with sorted data. A 12 minute perf record -a while building git took 4.45 million samples with no warnings. Sampling every 10000 instructions, which used to wedge the CPU, ran 66 times without a hang. On two CPUs, this version survived 26 runs that mix sampling, context switches and multiplexing, with no warnings.
---
Changes in v3:
- New patch 3: clear PERF_HES_UPTODATE when an event starts counting,
so that a stop before the first start does not lose the count.
- New patch 4: report no user registers for a sample taken in the entry code, where the frame at task_pt_regs() is not complete. An interrupt nested on one taken there is still not caught.
- Patch 5: a user instruction whose interrupt is taken in the kernel now reports its PC with no registers or user stack, as the frame at task_pt_regs() may not be built yet. Thanks to Magnus Lindholm for
finding this and for the fix.
- Patch 5: also move the counter away from overflow when a ProfileMe
event is removed, or stopped without PERF_EF_UPDATE, as
alpha_pmu_del() does not go through alpha_pmu_stop().
- Patch 5: ignore an interrupt that was pending when the event was
stopped. Document that rewriting a counter does not cancel one.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Patch 1: explain why bit 0 clear means all processes, although the Architecture Reference Manual says otherwise.
- Patch 3: fix a CPU wedge during high-rate ProfileMe sampling. The ProfileMe counters are now written twice, and a stopped counter is
moved away from overflow, as EV67 HRM PALcode restrictions 32 and 38 require.
- Patch 3: set the profiled PC in place in the trap frame of the instruction's mode instead of in a pt_regs copy on the stack, so
sampled registers and stack values are those of the right frame.
A kernel instruction whose interrupt is taken in user mode gets a
per-CPU frame holding only its PC, so the user frame is not changed.
- Patch 4: drop the reserved field and pack the 64-bit fields of
struct alpha_profileme_sample, so they are 8-byte aligned in the
sample record and the record needs no padding. This changes the raw
data layout.
- Patch 5: compute the jitter with mul_u64_u32_shr() and saturate the
sum, so that large periods cannot overflow.
- Link to v1: https://patch.msgid.link/[email protected]
To: Richard Henderson <[email protected]>
To: Matt Turner <[email protected]>
To: Magnus Lindholm <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Ingo Molnar <[email protected]>
To: Arnaldo Carvalho de Melo <[email protected]>
To: Namhyung Kim <[email protected]>
To: Mark Rutland <[email protected]>
To: Alexander Shishkin <[email protected]>
To: Jiri Olsa <[email protected]>
To: Ian Rogers <[email protected]>
To: Adrian Hunter <[email protected]>
To: James Clark <[email protected]>
To: Oleg Nesterov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
---
Matt Turner (7):
alpha/perf: pass PERFMON_CMD_LOGGING_ALL to the logging options call alpha/perf: add sysfs format and event attributes alpha/perf: clear PERF_HES_UPTODATE when an event starts counting alpha/perf: do not report user registers from the entry code alpha/perf: implement ProfileMe sampling
alpha/perf: attach ProfileMe status as PERF_SAMPLE_RAW data alpha/perf: optionally randomize the ProfileMe sampling interval
arch/alpha/include/asm/perf_event.h | 2 + arch/alpha/include/asm/ptrace.h | 12 + arch/alpha/include/asm/wrperfmon.h | 14 ++ arch/alpha/include/uapi/asm/perf_event.h | 31 +++ arch/alpha/kernel/entry.S | 8 + arch/alpha/kernel/perf_event.c | 365 +++++++++++++++++++++++++++++-- arch/alpha/kernel/perf_regs.c | 13 ++ 7 files changed, 432 insertions(+), 13 deletions(-)
---
base-commit: 330517c63aa9b48227b6ab45f01003effac77ae4
change-id: 20261024-alpha-profileme-6e98a977cb54
Best regards,
Signature · 1 line
Matt Turner <[email protected]>
Matt Turner
maintainerPATCH v3 1/7 alpha/perf: pass PERFMON_CMD_LOGGING_ALL to the logging options call
alpha_pmu_enable() passes EV67_PCTR_MODE_AGGREGATE to PERFMON_CMD_LOGGING_OPTIONS. That constant is the counting mode bit for PCTR_CTL, which belongs in the PERFMON_CMD_DESIRED_EVENTS argument. The logging options call takes a flag in bit 0 where 0 means "count all processes". It works only because EV67_PCTR_MODE_AGGREGATE happens to be 0.
Table E-18 of the Alpha Architecture Reference Manual gives bit 0 the opposite sense, but the PALcode in the ES40 and ES47 firmware agrees with the above: it sets I_CTL[SPCE], which enables counting for all processes, unless bit 0 is set. Linux never sets the PME bit in the PCB, so if 0 meant "selected processes only", nothing would be counted.
Add a name for that argument and pass it instead. No functional change.
--- arch/alpha/include/asm/wrperfmon.h | 2 ++ arch/alpha/kernel/perf_event.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
arch/alpha/include/asm/wrperfmon.h +2 -0
@@ -23,6 +23,8 @@#define PERFMON_CMD_I_STAT 8#define PERFMON_CMD_PMPC 9 +/* PERFMON_CMD_LOGGING_OPTIONS arguments */ +#define PERFMON_CMD_LOGGING_ALL 0/* EV5/EV56/PCA56 Counters */ #define EV5_PCTR_0 (1UL<<0)
arch/alpha/kernel/perf_event.c +1 -1
@@ -728,7 +728,7 @@ static void alpha_pmu_enable(struct pmu *pmu)maybe_change_configuration (cpuc);/* Start counting the desired events. */ -wrperfmon (PERFMON_CMD_LOGGING_OPTIONS ,EV67_PCTR_MODE_AGGREGATE ); +wrperfmon (PERFMON_CMD_LOGGING_OPTIONS ,PERFMON_CMD_LOGGING_ALL );wrperfmon (PERFMON_CMD_DESIRED_EVENTS , cpuc->config);wrperfmon (PERFMON_CMD_ENABLE , cpuc->idx_mask); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 1/7] alpha/perf: pass PERFMON_CMD_LOGGING_ALL to the logging options call
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
alpha_pmu_enable()passesEV67_PCTR_MODE_AGGREGATEtoPERFMON_CMD_LOGGING_OPTIONS. That constant is the counting mode bit forPCTR_CTL, which belongs in thePERFMON_CMD_DESIRED_EVENTSargument. The logging options call takes a flag in bit 0 where 0 means "count all processes". It works only becauseEV67_PCTR_MODE_AGGREGATEhappens to be 0.Table E-18 of the Alpha Architecture Reference Manual gives bit 0 the opposite sense, but the PALcode in the ES40 and ES47 firmware agrees with the above: it sets I_CTL[SPCE], which enables counting for all processes, unless bit 0 is set. Linux never sets the PME bit in the PCB, so if 0 meant "selected processes only", nothing would be counted.
Quoted 3 lines
Add a name for that argument and pass it instead. No functional change.
Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Matt Turner
maintainerPATCH v3 2/7 alpha/perf: add sysfs format and event attributes
In reply to Matt TurnerThe Alpha PMU registers with no attribute groups, so the perf tool has no way to learn the raw config layout, and the PMU's own events can only be named as raw hex numbers.
Describe the event field in format/event and export aliases for the four EV67 event types. mbox-replay has no generic hardware event, and can now be named as cpu/mbox-replay/.
--- arch/alpha/kernel/perf_event.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
arch/alpha/kernel/perf_event.c +39 -0
@@ -212,6 +212,44 @@ static int ev67_raw_event_valid(u64 config) }; +PMU_FORMAT_ATTR (event,"config:0-7" ); + +static struct attribute *ev67_format_attrs[] = { + &format_attr_event.attr, +NULL , +}; + +static const struct attribute_group ev67_format_group = { + .name ="format" , + .attrs = ev67_format_attrs, +}; + +/* Values must match enum ev67_pmc_event_type. */ +PMU_EVENT_ATTR_STRING (cycles, ev67_event_cycles,"event=0x01" ); +PMU_EVENT_ATTR_STRING (instructions, ev67_event_instructions,"event=0x02" ); +PMU_EVENT_ATTR_STRING (bcache-misses, ev67_event_bcache_misses,"event=0x03" ); +PMU_EVENT_ATTR_STRING (mbox-replay, ev67_event_mbox_replay,"event=0x04" ); + +static struct attribute *ev67_event_attrs[] = { + &ev67_event_cycles.attr.attr, + &ev67_event_instructions.attr.attr, + &ev67_event_bcache_misses.attr.attr, + &ev67_event_mbox_replay.attr.attr, +NULL , +}; + +static const struct attribute_group ev67_events_group = { + .name ="events" , + .attrs = ev67_event_attrs, +}; + +static const struct attribute_group *ev67_attr_groups[] = { + &ev67_format_group, + &ev67_events_group, +NULL , +}; + +static const struct alpha_pmu_t ev67_pmu = { .event_map = ev67_perfmon_event_map, .max_events =ARRAY_SIZE (ev67_perfmon_event_map),
@@ -761,6 +799,7 @@ static struct pmu pmu = { .start = alpha_pmu_start, .stop = alpha_pmu_stop, .read = alpha_pmu_read, + .attr_groups = ev67_attr_groups, .capabilities = PERF_PMU_CAP_NO_EXCLUDE, };
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 2/7] alpha/perf: add sysfs format and event attributes
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
The Alpha PMU registers with no attribute groups, so the perf tool has no way to learn the raw config layout, and the PMU's own events can only be named as raw hex numbers.
Describe the event field in format/event and export aliases for the four EV67 event types. mbox-replay has no generic hardware event, and can now be named as cpu/mbox-replay/.
Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Matt Turner
maintainerPATCH v3 3/7 alpha/perf: clear PERF_HES_UPTODATE when an event starts counting
In reply to Matt Turneralpha_pmu_add() sets PERF_HES_UPTODATE and maybe_change_configuration() then programs and enables the counter, but nothing clears the flag until the first alpha_pmu_start(). In that window alpha_pmu_stop() with PERF_EF_UPDATE skips the count update, because the flag claims the count is current. Since the next reload rewrites the counter, everything counted since the event was added is lost.
perf_adjust_freq_unthr_events() does this on the first tick after an event in frequency mode is scheduled in, which is the default for perf record, and it then sees no count to adjust the period with.
Clear the flag where the event starts counting. alpha_pmu_add() cannot do it, since the event is not programmed until the PMU is enabled, and stopping it before then must not read the counter.
--- arch/alpha/kernel/perf_event.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/alpha/kernel/perf_event.c +4 -1
@@ -456,8 +456,11 @@ static void maybe_change_configuration(struct cpu_hw_events *cpuc) cpuc->current_idx[j] = idx; } -if (!(hwc->state &PERF_HES_STOPPED )) +if (!(hwc->state &PERF_HES_STOPPED )) { +/* The counter is live, so hwc->prev_count will go stale. */ + hwc->state &= ~PERF_HES_UPTODATE ; cpuc->idx_mask |= (1 <<cpuc->current_idx[j]); + } } cpuc->config = cpuc->event[0 ]->hw.config_base; }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 3/7] alpha/perf: clear PERF_HES_UPTODATE when an event starts counting
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
alpha_pmu_add()setsPERF_HES_UPTODATEandmaybe_change_configuration()then programs and enables the counter, but nothing clears the flag until the firstalpha_pmu_start(). In that windowalpha_pmu_stop()withPERF_EF_UPDATEskips the count update, because the flag claims the count is current. Since the next reload rewrites the counter, everything counted since the event was added is lost.
perf_adjust_freq_unthr_events()does this on the first tick after an event in frequency mode is scheduled in, which is the default for perf record, and it then sees no count to adjust the period with.
Quoted 7 lines
Clear the flag where the event starts counting.
alpha_pmu_add()cannot do it, since the event is not programmed until the PMU is enabled, and stopping it before then must not read the counter.Fixes a4eaf7f14675 ("perf: Rework the PMU methods")Assisted-by Claude:claude-opus-5-5Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Matt Turner
maintainerPATCH v3 4/7 alpha/perf: do not report user registers from the entry code
In reply to Matt Turnerperf_get_regs_user() returns task_pt_regs() whenever a sample of a user task lands in the kernel. But exception entry runs with interrupts enabled, so the sample can land before the entry code has saved the user registers. Only the part PALcode pushed is valid then. An interrupt taken on the first instruction of entUna, say, pushes its own frame on top of PALcode's, which overlaps the rest of the user frame, so r7 and r8 read as the interrupt's r23 and r24.
Report no user registers while the interrupted PC is in the entry code. Such a sample also gets no user stack dump. The range is all of the entry code from entInt to the end of the save in entSys, so it also drops the registers of samples that land after they were saved there.
An interrupt that nests on one taken in the entry code still reads the incomplete frame, as its own PC has left the entry code.
--- arch/alpha/include/asm/ptrace.h | 12 ++++++++++++ arch/alpha/kernel/entry.S | 8 ++++++++ arch/alpha/kernel/perf_regs.c | 13 +++++++++++++ 3 files changed, 33 insertions(+)
arch/alpha/include/asm/ptrace.h +12 -0
@@ -14,6 +14,18 @@#define task_pt_regs (task) \ ((struct pt_regs *) (task_stack_page(task) + 2*PAGE_SIZE) - 1) +/* +* Whether pc is in entry code that may not have completed the frame at +* task_pt_regs(). See entry.S. +*/ +static inline bool in_entry_save_code (unsigned long pc) +{ +extern char __entry_save_start[], __entry_save_end[]; + +return pc >= (unsigned long )__entry_save_start && + pc < (unsigned long )__entry_save_end; +} +#define current_pt_regs () \ ((struct pt_regs *) ((char *)current_thread_info() + 2*PAGE_SIZE) - 1)
arch/alpha/kernel/entry.S +8 -0
@@ -186,8 +186,14 @@ /* * Non-syscall kernel entry points. + * + * Between __entry_save_start and __entry_save_end the frame at task_pt_regs() + * may not be complete. perf uses these to avoid reading it. */ + .globl __entry_save_start +__entry_save_start: + CFI_START_OSF_FRAME entInt SAVE_ALL lda $8, 0x3fff
@@ -473,6 +479,8 @@ CFI_END_OSF_FRAME entDbg .cfi_rel_offset $gp, 16 entSys: SAVE_ALL + .globl __entry_save_end +__entry_save_end: ldq $1, 0($sp) /* syscall nr from saved r0 */ stq $1, 8($sp) /* regs->r1 = shadow syscall nr */
arch/alpha/kernel/perf_regs.c +13 -0
@@ -59,6 +59,19 @@ u64 perf_reg_abi(struct task_struct *task)void perf_get_regs_user (struct perf_regs *regs_user,struct pt_regs *regs) { +/* +* Exception entry runs with interrupts enabled, so a sample can land +* before the entry code has saved the user registers. PALcode has +* pushed only its part of the frame, and the rest may hold an +* interrupt's frame. An interrupt that nests on top of one taken here +* is not caught, as its PC has left the entry code. +*/ +if (in_entry_save_code (regs->pc)) { + regs_user->regs =NULL ; + regs_user->abi =PERF_SAMPLE_REGS_ABI_NONE ; +return ; + } + regs_user->regs =task_pt_regs (current); regs_user->abi =perf_reg_abi (current); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 4/7] alpha/perf: do not report user registers from the entry code
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] alpha/perf: __entry_save_end precedes r16-r18 save in entSys --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] alpha/perf: __entry_save_end precedes r16-r18 save in entSys
--
commit ce97475b8e0171fa2ccbfec6b4cebc978a8757a6
Author: Matt Turner <[email protected]>
alpha/perf: do not report user registers from the entry code
This commit prevents perf_get_regs_user() from returning an incomplete user pt_regs frame when a perf sample interrupts kernel exception or syscall entry before the user registers have finished being saved.
Quoted 4 lines
arch/alpha/kernel/entry.S +0
-0
[ ... ]
arch/alpha/kernel/entry.S CFI_END_OSF_FRAME entDbg · line 485
Quoted 8 lines
arch/alpha/kernel/entry.S +2
-0@@ -473,6 +479,8 @@ CFI_END_OSF_FRAME entDbg .cfi_rel_offset $gp, 16 entSys: SAVE_ALL + .globl __entry_save_end +__entry_save_end: ldq $1, 0($sp) /* syscall nr from saved r0 */ stq $1, 8($sp) /* regs->r1 = shadow syscall nr */
[Severity: Medium]
Should __entry_save_end be placed after $16, $17, and $18 are stored into SP_OFF+24..40($sp) in entSys?
Unlike non-syscall PALcode entries, PAL_callsys does not save user $16..$18 into the PALcode stack frame, and SAVE_ALL only stores them into trap_a0..trap_a2 at 160..176($sp). In entSys, pt_regs->r16..r18 are populated a few instructions after SAVE_ALL while interrupts are still enabled:
arch/alpha/kernel/entry.S:
entSys:
SAVE_ALL .globl __entry_save_end __entry_save_end: ldq $1, 0($sp) /* syscall nr from saved r0 */ stq $1, 8($sp) /* regs->r1 = shadow syscall nr */ lda $8, 0x3fff bic $sp, $8, $8 stq $16, SP_OFF+24($sp) stq $17, SP_OFF+32($sp) stq $18, SP_OFF+40($sp)
Since pt_regs_offset[] in arch/alpha/kernel/perf_regs.c maps PERF_REG_ALPHA_R16..R18 to offsetof(struct pt_regs, r16..r18) rather than trap_a0..trap_a2, if a perf interrupt lands between __entry_save_end and stq $18, SP_OFF+40($sp), in_entry_save_code(regs->pc) will return false and perf_get_regs_user() will expose task_pt_regs(current) before r16, r17, and r18 have been saved.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Matt Turner
maintainerPATCH v3 5/7 alpha/perf: implement ProfileMe sampling
In reply to Matt TurnerThe 21264 (EV67 and later) and 21364 have two performance counting modes, and the driver only uses Aggregate mode. Its samples report the PC at which the counter interrupt was taken, which lands a variable number of instructions after the one that overflowed the counter. On an EV7 a 16-instruction user loop shows up at only 4 PCs, anywhere from 15 instructions before to 12 after the real one.
In ProfileMe mode, the instruction that overflows PCTR0 opens a window during which PCTR1 counts, and when it closes PALcode delivers the interrupt with that instruction's PC latched in PMPC.
Select ProfileMe with the new profileme config bit or with precise_ip, since PMPC has no skid. PCTR_CTL[SL0] selects the mode for both counters, so a ProfileMe event is scheduled alone and perf core multiplexes it with other hardware events. The event type chooses the SL1 setting, which decides what PCTR0 samples on and what PCTR1 counts in the window:
event PCTR0 PCTR1 instructions instructions cycles bcache-misses instructions Bcache misses cycles cycles inum retire delay mbox-replay cycles Mbox replay traps
Every interrupt rearms both counters, since PCTR0 stops when the window closes. The instruction count is corrected by the overcount I_STAT reports. The sample is attributed to PMPC. The interrupt can be taken in a different mode than the profiled instruction: in an unaligned access test on EV7 that was true of three quarters of the samples. When the modes match, the PC is set in the interrupt's frame for the overflow call and restored afterwards, so perf's mode, register and stack sampling all see the same frame. When they differ the registers are not
available: a kernel instruction whose interrupt is taken after the return to user mode has no kernel state left, and a user instruction's frame may not be built yet, as the interrupt is often taken on the first instruction of the entry code. Those samples use a per-CPU frame holding only the PC, and a user instruction's sample reports no registers or user stack. PALmode PMPCs are physical addresses, so those samples keep the interrupted PC.
PCTR0 is left stopped just past overflow at the end of every window, which the EV67 PALcode restrictions 32 and 38 warn about: a disabled counter near overflow keeps signaling it and blocks all other interrupts, and a disabled counter only clears its overflow state when written twice. So the counters are written twice when rearmed, and are moved away from overflow when a ProfileMe event is stopped or removed or a stray interrupt arrives for one that is gone. Without this, sampling every 10000 instructions wedged an EV7 in 7 of 16 runs, with no console output, until the service processor reset it. With it, 66 runs passed.
Rewriting a counter does not cancel its pending interrupt: wrperfmon has no way to, and PALcode clears the request only when delivering it. An interrupt that was pending when a ProfileMe event was stopped is ignored. One still pending when perf multiplexes in an event of the other mode on PCTR0 is taken as that event's overflow, which costs one false sample and adds a bogus period to the count.
Three behaviors differ from Aggregate mode. perf record's default event is cycles:P, so it now records a ProfileMe profile. In ProfileMe mode PCTR0 counts valid fetched instructions rather than retired ones, so the instructions count includes the wrong path, squashed NOPs, and both halves of each CMOV: on an EV7 loop with a randomly mispredicted branch it is about twice the Aggregate count. And instructions killed on a wrong path are sampled at their own PC, as ProfileMe defines them.
--- arch/alpha/include/asm/wrperfmon.h | 12 ++ arch/alpha/kernel/perf_event.c | 268 +++++++++++++++++++++++++++++++++++-- 2 files changed, 269 insertions(+), 11 deletions(-)
arch/alpha/include/asm/wrperfmon.h +12 -0
@@ -92,5 +92,17 @@#define EV67_PCTR_CYCLES_UNDEF (1UL<<2)#define EV67_PCTR_INSTR_BCACHEMISS (2UL<<2)#define EV67_PCTR_CYCLES_MBOX (3UL<<2) +/* In ProfileMe mode, SL1 = 1 makes PCTR1 count the inum retire delay. */ +#define EV67_PCTR_CYCLES_RETIRE_DELAY (1UL<<2) +/* Set in both SL settings where PCTR0 counts cycles. */ +#define EV67_PCTR_PCTR0_CYCLES (1UL<<2) + +/* ProfileMe status, from the EV67/EV68 Hardware Reference Manuals. */ +#define EV67_I_STAT_OVR_SHIFT 30 +#define EV67_I_STAT_OVR_MASK 0x7UL + +/* PMPC bits 63:2 hold the profiled PC. Bit 0 set means a PALmode address. */ +#define EV67_PMPC_PAL (1UL<<0) +#define EV67_PMPC_PC_MASK (~3UL)#endif
arch/alpha/kernel/perf_event.c +257 -11
391 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 5/7] alpha/perf: implement ProfileMe sampling
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] alpha/perf: perf_get_regs_user() checks PMPC instead of interrupted PC --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] alpha/perf: perf_get_regs_user() checks PMPC instead of interrupted PC --
commit b49acea08699ec26b74c2c7c29ccc098435ea611
Author: Matt Turner <[email protected]>
alpha/perf: implement ProfileMe sampling
This commit implements ProfileMe sampling mode on Alpha EV67 and later processors, selectable via the profileme config attribute or precise_ip. It attributes samples to the instruction address latched in PMPC, adjusts for I_STAT instruction overcount, and works around PALcode counter overflow restrictions when stopping or rearming counters.
Quoted 4 lines
arch/alpha/kernel/perf_event.c +0
-0
[ ... ]
Quoted 1 line
arch/alpha/kernel/perf_event.c +0
-0@@ -835,6 +939,131 @@ void perf_event_print_debug(void)
[ ... ]
Quoted 4 lines
arch/alpha/kernel/perf_event.c +4
-0+static void alpha_perf_event_profileme (struct perf_event *event, +struct hw_perf_event *hwc, +struct pt_regs *regs) +{
[ ... ]
Quoted 31 lines
arch/alpha/kernel/perf_event.c +31
-0+ pm_regs = regs; + pc = pmpc &EV67_PMPC_PC_MASK ; +if (pmpc &EV67_PMPC_PAL ) { + pc = regs->pc; + }else if (pc <TASK_SIZE && !user_mode (regs)) { +if (is_user_task (current)) { + pc_only = true; + pm_regs =this_cpu_ptr (&alpha_profileme_regs); +memset (pm_regs,0 ,sizeof (*pm_regs)); +/* PS bit 3 is the mode bit that user_mode() tests. */ + pm_regs->ps =8 ; + }else { + pc = regs->pc; + } + }else if (pc >=TASK_SIZE &&user_mode (regs)) { + pm_regs =this_cpu_ptr (&alpha_profileme_regs); +memset (pm_regs,0 ,sizeof (*pm_regs)); + } + +perf_sample_data_init (&data,0 , hwc->last_period); +if (pc_only) +alpha_profileme_drop_regs (event, &data,PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_REGS_INTR ); +/* +* perf_get_regs_user() has the same check, but it will find the PMPC +* in regs->pc rather than where the interrupt was taken. +*/ +else if (!user_mode (regs) &&in_entry_save_code (regs->pc)) +alpha_profileme_drop_regs (event, &data,PERF_SAMPLE_REGS_USER ); + +if (!alpha_perf_event_set_period (event, hwc,0 ))
arch/alpha/kernel/perf_event.c void perf_event_print_debug(void) · line 1061
Quoted 10 lines
arch/alpha/kernel/perf_event.c +10
-0+return ; + + saved_pc = pm_regs->pc; + saved_ps = pm_regs->ps; + pm_regs->pc = pc; +/* PS bit 3 is the mode bit that user_mode() tests. */ +if (pc >=TASK_SIZE ) + pm_regs->ps &= ~8UL ; + +perf_event_overflow (event, &data, pm_regs);
[Severity: Medium]
When a ProfileMe sample has a kernel PMPC inside the entry save range (in_entry_save_code(pc) is true), but the performance counter interrupt itself is taken after leaving that range—either later in kernel mode after SAVE_ALL has populated task_pt_regs(current), or after returning to user mode where user_mode(regs) is true—alpha_perf_event_profileme() does not call alpha_profileme_drop_regs() because in_entry_save_code(regs->pc) is false.
It then sets pm_regs->pc = pc and clears the user mode bit in pm_regs->ps (or uses the zeroed per-CPU alpha_profileme_regs when user_mode(regs) is true) before calling perf_event_overflow().
During sample preparation, perf_event_overflow() -> __perf_event_overflow() -> perf_prepare_sample() -> perf_sample_regs_user() sees !user_mode(pm_regs) and calls perf_get_regs_user():
arch/alpha/kernel/perf_regs.c:perf_get_regs_user() {
if (in_entry_save_code(regs->pc)) {
regs_user->regs = NULL;
regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
return;
}
regs_user->regs = task_pt_regs(current);
regs_user->abi = perf_reg_abi(current);
}Because perf_get_regs_user() checks in_entry_save_code(pm_regs->pc), which is now the PMPC rather than the interrupted PC regs->pc, won't it falsely discard valid PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER data whenever the profiled instruction is in __entry_save_start..__entry_save_end even though the interrupt was delivered after the user frame was completely saved (or after returning to user mode)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Matt Turner
maintainerPATCH v3 6/7 alpha/perf: attach ProfileMe status as PERF_SAMPLE_RAW data
In reply to Matt TurnerA ProfileMe sample's PC alone does not say what happened to the profiled instruction. I_STAT and PCTR_CTL do: whether it trapped and why, was mispredicted, stalled, or was killed on a wrong path, and what PCTR1 counted during the window.
With PERF_SAMPLE_RAW, attach PMPC, I_STAT and PCTR_CTL to each ProfileMe sample in a versioned struct alpha_profileme_sample. PMPC is included because a PALmode PMPC is not the sample's IP. Killed instructions can be told apart by PM_KILLED_BM and VAL. The 64-bit fields are packed after the 32-bit version, since the payload follows the 32-bit raw size in the sample record: they are then 8-byte aligned in the record, which needs no padding.
--- arch/alpha/include/asm/perf_event.h | 2 ++ arch/alpha/include/uapi/asm/perf_event.h | 31 +++++++++++++++++++++++++++++++ arch/alpha/kernel/perf_event.c | 22 +++++++++++++++++++--- 3 files changed, 52 insertions(+), 3 deletions(-)
arch/alpha/include/asm/perf_event.h +2 -0
@@ -1,6 +1,8 @@#ifndef __ASM_ALPHA_PERF_EVENT_H#define __ASM_ALPHA_PERF_EVENT_H +#include <uapi/asm/perf_event.h> +/* * Software events have no trap frame, so perf_fetch_caller_regs() fills * in a zeroed pt_regs. A zero ps already means kernel mode; only pc is
arch/alpha/include/uapi/asm/perf_event.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_ALPHA_PERF_EVENT_H +#define _UAPI_ASM_ALPHA_PERF_EVENT_H + +#include <linux/types.h> + +/* +* PERF_SAMPLE_RAW payload of a ProfileMe sample from the cpu PMU. +* +* pmpc, i_stat and pctr_ctl are the values of the PMPC, I_STAT and +* PCTR_CTL registers when the ProfileMe window closed. i_stat holds only +* bits 40:30 and pctr_ctl only bits 47:0. See the EV67/EV68 Hardware +* Reference Manuals for the field layout. +* +* In the sample record the payload follows the 32-bit PERF_SAMPLE_RAW +* size, so it starts 4 bytes past an 8-byte boundary. The 64-bit fields +* are packed after the 32-bit version so that they are 8-byte aligned in +* the record and no padding follows the payload. +* +* Fields may be added to the end in later versions. +*/ +#define ALPHA_PROFILEME_SAMPLE_VERSION 1 + +struct alpha_profileme_sample { +__u32 version; +__u64 pmpc __attribute__((packed)); +__u64 i_stat __attribute__((packed)); +__u64 pctr_ctl __attribute__((packed)); +}; + +#endif /* _UAPI_ASM_ALPHA_PERF_EVENT_H */
arch/alpha/kernel/perf_event.c +19 -3
@@ -974,11 +974,14 @@ static void alpha_perf_event_profileme(struct perf_event *event,struct hw_perf_event *hwc, struct pt_regs *regs) { + struct alpha_profileme_sample pm; +struct perf_raw_record raw;struct perf_sample_data data;struct pt_regs *pm_regs;bool pc_only = false; -unsigned long i_stat, pmpc, ovr, pc, saved_pc, saved_ps; +unsigned long pctr_ctl, i_stat, pmpc, ovr, pc, saved_pc, saved_ps; + pctr_ctl =wrperfmon (PERFMON_CMD_READ ,0 ); i_stat =wrperfmon (PERFMON_CMD_I_STAT ,0 ); pmpc =wrperfmon (PERFMON_CMD_PMPC ,0 );
@@ -1015,8 +1018,8 @@ static void alpha_perf_event_profileme(struct perf_event *event,* happens for ITB miss, Istream access violation and interrupt * redirects, and for instructions inside PALcode. Keep the * interrupted PC, which for redirects is the instruction that -* trapped. Do the same for a user PMPC in a kernel thread, which has -* no user frame. +* trapped, and report the real PMPC in the raw data. Do the same +* for a user PMPC in a kernel thread, which has no user frame. */ pm_regs = regs; pc = pmpc &EV67_PMPC_PC_MASK ;
@@ -1048,6 +1051,19 @@ static void alpha_perf_event_profileme(struct perf_event *event,else if (!user_mode (regs) &&in_entry_save_code (regs->pc))alpha_profileme_drop_regs (event, &data,PERF_SAMPLE_REGS_USER ); + pm.version =ALPHA_PROFILEME_SAMPLE_VERSION ; + pm.pmpc = pmpc; + pm.i_stat = i_stat; +/* PALcode sign-extends bit 47 of PCTR_CTL. */ + pm.pctr_ctl = pctr_ctl &GENMASK (47 ,0 ); + raw = (struct perf_raw_record ) { + .frag = { + .size =sizeof (pm), + .data = &pm, + }, + }; +perf_sample_save_raw_data (&data, event, &raw); +if (!alpha_perf_event_set_period (event, hwc,0 ))return ;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 6/7] alpha/perf: attach ProfileMe status as PERF_SAMPLE_RAW data
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
A ProfileMe sample's PC alone does not say what happened to the profiled instruction.
I_STATandPCTR_CTLdo: whether it trapped and why, was mispredicted, stalled, or was killed on a wrong path, and what PCTR1 counted during the window.With
PERF_SAMPLE_RAW, attach PMPC,I_STATandPCTR_CTLto each ProfileMe sample in a versioned structalpha_profileme_sample. PMPC is included because a PALmode PMPC is not the sample's IP. Killed instructions can be told apart byPM_KILLED_BMand VAL. The 64-bit fields are packed after the 32-bit version, since the payload follows the 32-bit raw size in the sample record: they are then 8-byte aligned in the record, which needs no padding.
Quoted 1 line
Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Matt Turner
maintainerPATCH v3 7/7 alpha/perf: optionally randomize the ProfileMe sampling interval
In reply to Matt TurnerA fixed sampling interval can phase-lock with a loop whose length divides it, so that the same few instructions are profiled over and over while the rest of the loop is never seen. DCPI avoided this by randomizing the interval.
Add a randomize config bit that varies each interval by up to 1/16 of the period in either direction. Only ProfileMe events accept it, since Aggregate mode samples already land a variable distance from the instruction that overflowed. Counts are unaffected, because prev_count already records the value written to the counter. The jitter is computed with mul_u64_u32_shr() and the sum saturates, so that a period beyond 32 bits cannot overflow.
The jitter comes from a per-CPU prandom state rather than get_random_u32_below(), which takes the batched entropy local lock and triggers an invalid wait context report from lockdep in the PMI.
--- arch/alpha/kernel/perf_event.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
arch/alpha/kernel/perf_event.c +36 -1
@@ -16,6 +16,9 @@#include <linux/kdebug.h> #include <linux/mutex.h> #include <linux/init.h> +#include <linux/math64.h> +#include <linux/overflow.h> +#include <linux/prandom.h> #include <asm/hwrpb.h> #include <linux/atomic.h>
@@ -33,9 +36,11 @@/* Layout of attr.config for PERF_TYPE_RAW events; see format/ in sysfs. */ #define ALPHA_PERF_CONFIG_EVENT 0xffULL#define ALPHA_PERF_CONFIG_PROFILEME BIT_ULL(8) +#define ALPHA_PERF_CONFIG_RANDOMIZE BIT_ULL(9)/* hw_perf_event.flags */ #define ALPHA_PERF_FLAG_PROFILEME 0x1 +#define ALPHA_PERF_FLAG_RANDOMIZE 0x2/* For tracking PMCs and the hw events they monitor on each CPU. */ struct cpu_hw_events {
@@ -59,6 +64,9 @@ struct cpu_hw_events { }; DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events); +/* For randomizing sample intervals from the PMI, where locks are not allowed. */ +static DEFINE_PER_CPU(struct rnd_state, alpha_perf_rnd); +/*
@@ -246,10 +254,12 @@ static int ev67_raw_event_valid(u64 config)PMU_FORMAT_ATTR (event,"config:0-7" );PMU_FORMAT_ATTR (profileme,"config:8" ); +PMU_FORMAT_ATTR (randomize,"config:9" );static struct attribute *ev67_format_attrs[] = { &format_attr_event.attr, &format_attr_profileme.attr, + &format_attr_randomize.attr,NULL , };
@@ -384,6 +394,22 @@ static int alpha_perf_event_set_period(struct perf_event *event, ret =1 ; } +/* +* Vary each new interval by up to 1/16 of the period either way, so +* the samples cannot phase-lock with a loop whose length divides the +* period. The count stays exact because prev_count records the value +* written. +*/ +if (ret && (hwc->flags &ALPHA_PERF_FLAG_RANDOMIZE ) && period >=8 ) { +long jitter = period /8 ; +u32 r =prandom_u32_state (this_cpu_ptr (&alpha_perf_rnd)); +long offset =mul_u64_u32_shr (jitter, r,32 ) - jitter /2 ; + +if (check_add_overflow (left, offset, &left)) + left =LONG_MAX ; +local64_set (&hwc->period_left, left); + } +/* * Hardware restrictions require that the counters must not be * written with values that are too close to the maximum period.
@@ -750,7 +776,8 @@ static int __hw_perf_event_init(struct perf_event *event)return -EOPNOTSUPP ; }else if (attr->type ==PERF_TYPE_RAW ) {if (attr->config & ~(ALPHA_PERF_CONFIG_EVENT | -ALPHA_PERF_CONFIG_PROFILEME )) +ALPHA_PERF_CONFIG_PROFILEME | +ALPHA_PERF_CONFIG_RANDOMIZE ))return -EINVAL ; ev = attr->config &ALPHA_PERF_CONFIG_EVENT ;if (!alpha_pmu->raw_event_valid (ev))
@@ -775,6 +802,14 @@ static int __hw_perf_event_init(struct perf_event *event)return -EINVAL ; hwc->flags = profileme ?ALPHA_PERF_FLAG_PROFILEME :0 ; +if (attr->type ==PERF_TYPE_RAW && + (attr->config &ALPHA_PERF_CONFIG_RANDOMIZE )) { +if (!profileme) +return -EINVAL ; +prandom_init_once (&alpha_perf_rnd); + hwc->flags |=ALPHA_PERF_FLAG_RANDOMIZE ; + } +/* * We place the event type in event_base here and leave calculation * of the codes to programme the PMU for alpha_pmu_enable() because
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 7/7] alpha/perf: optionally randomize the ProfileMe sampling interval
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
A fixed sampling interval can phase-lock with a loop whose length divides it, so that the same few instructions are profiled over and over while the rest of the loop is never seen. DCPI avoided this by randomizing the interval.
Add a randomize config bit that varies each interval by up to 1/16 of the period in either direction. Only ProfileMe events accept it, since Aggregate mode samples already land a variable distance from the instruction that overflowed. Counts are unaffected, because
prev_countalready records the value written to the counter. The jitter is computed withmul_u64_u32_shr()and the sum saturates, so that a period beyond 32 bits cannot overflow.
Quoted 5 lines
The jitter comes from a per-CPU prandom state rather than
get_random_u32_below(), which takes the batched entropy local lock and triggers an invalid wait context report from lockdep in the PMI.Signed-off-by Matt Turner <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7