[PATCH] arm: perf: fix sw event kernel backtrace with unwind

Lexi Shao shaolexi at huawei.com
Tue May 11 06:09:12 PDT 2021


For perf sample not generated with interrupt/exception, there is no
pt_regs information, so perf_fetch_caller_regs() is used to fill
necessary information for sampling and backtracing.
perf_fetch_caller_regs() puts LR as PC to skip unwanted frame.

For ARM images with CONFIG_ARM_UNWIND=y, this does not work because
backtracing with unwind requires PC to match FP and SP, but we have the
previous PC with current FP and SP here. Therefore backtrace fails and
stack in kernel only has one entry.

Before this patch:
perf record -e sched:sched_switch -ag sleep 2
perf script
perf  1512 [000]    51.976113: sched:sched_switch: perf:1512 [120] S
	==> swapper/0:0 [120]
                c048d9e8 __schedule ([kernel.kallsyms])
                   d0618 __poll (/lib/libc-2.31.so)
                   6b678 [unknown] (/usr/bin/perf)
                    b0b0 [unknown] (/usr/bin/perf)
                   17700 __libc_start_main (/lib/libc-2.31.so)

Notice there is only one frame in kernel text.

After this patch, the kernel callchain is fully displayed:
perf  1545 [000] 10403.005915: sched:sched_switch: perf:1545 [120] S
	==> swapper/0:0 [120]
                c014bff0 perf_trace_sched_switch ([kernel.kallsyms])
                c048d9d8 __schedule ([kernel.kallsyms])
                c048df4c schedule ([kernel.kallsyms])
                c0491940 schedule_hrtimeout_range_clock ([kernel.kallsyms])
                c0255164 poll_schedule_timeout ([kernel.kallsyms])
                c02567dc do_sys_poll ([kernel.kallsyms])
                c02568f8 sys_poll ([kernel.kallsyms])
                c01084b0 __sys_trace_return ([kernel.kallsyms])
                   d0618 __poll (/lib/libc-2.31.so)
                   6b678 [unknown] (/usr/bin/perf)
                    b0b0 [unknown] (/usr/bin/perf)
                   17700 __libc_start_main (/lib/libc-2.31.so)

Fixes: b3eac0265bf62 ("arm: perf: Fix callchain parse error with kernel
tracepoint events") #v4.2+

Signed-off-by: Lexi Shao <shaolexi at huawei.com>
---
 arch/arm/include/asm/perf_event.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/arch/arm/include/asm/perf_event.h b/arch/arm/include/asm/perf_event.h
index fe87397c3d8c..e326520f9386 100644
--- a/arch/arm/include/asm/perf_event.h
+++ b/arch/arm/include/asm/perf_event.h
@@ -15,8 +15,20 @@ extern unsigned long perf_misc_flags(struct pt_regs *regs);
 #define perf_misc_flags(regs)	perf_misc_flags(regs)
 #endif
 
+#ifdef CONFIG_ARM_UNWIND
+/*
+ * With ARM unwind, the pc must match with fp and sp, otherwise
+ * backtrace method unwind_frame() does not work properly.
+ * Get pc with assembly instead of using __ip.
+ */
+#define perf_get_arm_pc(regs, __ip) \
+	__asm__ __volatile__ ("mov %0, pc" : "=r"((regs)->ARM_pc)::)
+#else
+#define perf_get_arm_pc(regs, __ip)	((regs)->ARM_pc = (__ip))
+#endif
+
 #define perf_arch_fetch_caller_regs(regs, __ip) { \
-	(regs)->ARM_pc = (__ip); \
+	perf_get_arm_pc(regs, __ip); \
 	(regs)->ARM_fp = (unsigned long) __builtin_frame_address(0); \
 	(regs)->ARM_sp = current_stack_pointer; \
 	(regs)->ARM_cpsr = SVC_MODE; \
-- 
2.12.3




More information about the linux-arm-kernel mailing list