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. SIAR also holds a real address while translation is off, such as 0xc00 in the system call vector. 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, or reports a kernel sample as a user sample at 0xc00. siar_valid() already returns true on 32-bit. Use SIAR for PMU exceptions when it is in the same mode as the interrupted context, and the interrupted PC otherwise. With that, the same test has no samples with mixed modes. Signed-off-by: Matt Turner <[email protected]> --- arch/powerpc/perf/core-book3s.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c index 10ded865583c..4c9c9ff1073e 100644 --- a/arch/powerpc/perf/core-book3s.c +++ b/arch/powerpc/perf/core-book3s.c @@ -112,9 +112,25 @@ 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 it can 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, such as the start + * of an exception vector. Use SIAR only when it is in the same mode as + * the interrupted context, so the sample's mode and the callchain built + * from regs stay consistent with it. + */ static inline void perf_read_regs(struct pt_regs *regs) { - regs->result = 0; + unsigned long siar = mfspr(SPRN_SIAR); + + regs->result = siar && is_kernel_addr(siar) == !user_mode(regs); } static inline int siar_valid(struct pt_regs *regs) -- 2.56.1
