On 11/18/26 9:47 PM, Matt Turner wrote:
On 32-bit, perf_read_regs() sets regs->result to 0, so regs_use_siar()
is always false and every sample is attributed to the interrupted PC.
The MPC7450 family's SIAR holds the address of the last instruction to
complete before the performance monitor exception is generated
(MPC7450UM Rev. 5, section 11.3.7). The exception can be generated
with MSR[EE] = 0 and is taken once EE is set again (section 11.2), so
when interrupts are disabled the interrupted PC is wherever they are
re-enabled, while SIAR still points into the disabled region.
On a 7448, sampling cycles system-wide with --intr-regs=nip during dd,
perf bench sched pipe and find, 46% of the kernel samples have SIAR and
the interrupted PC in different functions. 13270 samples have their PC
on the instruction after the mtmsr that restores interrupts in
ata_scsi_queuecmd(), and 11661 of those have SIAR in udelay().
With interrupts enabled SIAR helps less. Sampling a 32-instruction
loop with one store and one branch with -c 10007, 99.8% of the samples
land at:
event interrupted PC SIAR
r122 (branches) br + 13 br + 9
r014 (stores) stw + 21 stw + 17
Because the exception is held off, it can also be taken on the other
side of a user/kernel transition from the instruction in SIAR, e.g.
just after the rfi at the end of a system call. Recording a loop that
alternates user work with getppid() with perf record -g -c 1000000,
3022 of 16953 samples had SIAR in a different mode from the
interrupted PC. The callchain is walked from regs, so taking its first
entry from SIAR there starts a user callchain with a kernel address.
SIAR also holds a real address while translation is off, and a real
address looks like a user address. The hash_page() fast path on
CONFIG_PPC_BOOK3S_604 runs with translation off and EE clear and
returns straight to user space, so an exception generated there is
taken at a user PC with SIAR holding a kernel real address. Reading
one word from each of 2048 pages after an mprotect() round trip has
flushed their hash PTEs, with -c 10007, 1674 of 2659 user samples land
on the real addresses of DataAccess, hash_page() and
fast_hash_page_return when SIAR is used whenever its address matches
the mode of the interrupted context.
A user SIAR cannot be told apart from a kernel real address, and the
table above shows user samples gain little from SIAR. Use SIAR for PMU
exceptions only when the interrupted context is the kernel and SIAR is
a kernel address, and the interrupted PC otherwise. With that, neither
test has user samples at kernel addresses or callchains with mixed
modes, and 99.9% of the kernel samples in the mprotect() test still
take their address from SIAR.
Suggested-by: Tanushree Shah <[email protected]>
Signed-off-by: Matt Turner <[email protected]>
Hi Matt, v3 looks good for this patch, and the hash_page() explanation
in the message and comment is clear.
Reviewed-by: Tanushree Shah <[email protected]>
Thanks
Tanushree Shah
---
Changes in v3:
- Use SIAR only for kernel samples, and only when it is a kernel
address. A real-mode SIAR from the hash_page() fast path looks like a
user address and was used for user samples taken after its rfi
(Tanushree Shah).
- Retitled from "Use SIAR for sample addresses on 32-bit".
- Link to v2:
https://patch.msgid.link/[email protected]
arch/powerpc/perf/core-book3s.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c
index 10ded865583c..aab18f4dda94 100644
--- a/arch/powerpc/perf/core-book3s.c
+++ b/arch/powerpc/perf/core-book3s.c
@@ -112,9 +112,28 @@ static inline u32 perf_get_misc_flags(struct pt_regs *regs)
{
return 0;
}
+
+/*
+ * The MPC7450 family's SIAR holds the address of the last instruction to
+ * complete before the performance monitor exception was generated, so for
+ * a PMU exception it is closer to the event than the interrupted PC. The
+ * exception is held off while MSR[EE] is clear, so with interrupts
+ * disabled the interrupted PC is wherever they are re-enabled.
+ *
+ * Because it is held off, the exception can also be taken on the other
+ * side of a user/kernel transition from the instruction in SIAR, e.g.
+ * just after the rfi at the end of a system call. SIAR also holds a real
+ * address for code running with translation off, and a real address
+ * looks like a user address. The hash_page() fast path runs with
+ * translation off and returns straight to user space, so a user SIAR
+ * cannot be trusted. Use SIAR only for kernel samples, and only when it
+ * is a kernel address.
+ */
static inline void perf_read_regs(struct pt_regs *regs)
{
- regs->result = 0;
+ unsigned long siar = mfspr(SPRN_SIAR);
+
+ regs->result = siar && !user_mode(regs) && is_kernel_addr(siar);
}
static inline int siar_valid(struct pt_regs *regs)