linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Lexi Shao <shaolexi@huawei.com>
To: <linux-arm-kernel@lists.infradead.org>
Cc: <will@kernel.org>, <mark.rutland@arm.com>, <peterz@infradead.org>,
	<mingo@redhat.com>, <acme@kernel.org>, <shaolexi@huawei.com>,
	<qiuxi1@huawei.com>, <nixiaoming@huawei.com>,
	<zengweilin@huawei.com>
Subject: [PATCH] arm: perf: fix sw event kernel backtrace with unwind
Date: Tue, 11 May 2021 21:09:12 +0800	[thread overview]
Message-ID: <20210511130912.118504-1-shaolexi@huawei.com> (raw)

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@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


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2021-05-11 13:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-11 13:09 Lexi Shao [this message]
2021-06-01 12:42 ` [PATCH] arm: perf: fix sw event kernel backtrace with unwind Will Deacon
2021-06-03 13:43   ` Lexi Shao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210511130912.118504-1-shaolexi@huawei.com \
    --to=shaolexi@huawei.com \
    --cc=acme@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=nixiaoming@huawei.com \
    --cc=peterz@infradead.org \
    --cc=qiuxi1@huawei.com \
    --cc=will@kernel.org \
    --cc=zengweilin@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).