From patchwork Tue May 11 13:09:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lexi Shao X-Patchwork-Id: 12251039 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6123AC433B4 for ; Tue, 11 May 2021 13:50:03 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B5993610F8 for ; Tue, 11 May 2021 13:50:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B5993610F8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=Tl9q7fr9ZLsgQaXhnoa5jZkb0/W+EMJSHlTavL/SlNw=; b=AaW/wKgAN9/T3j3J+ALtPyFSPh w8qawIHosESI6hFEa7MyBxtlB4XIaxcJdAZZjxsfqa2TZfEaWTvqsSifmL3m1LdA+yZnQMNL2MLIH l9GQTjwfc91TC5yWl1Q5LyAHZF/qxy+/RUtUgyXaIAeT92TZIv9zSCVKiSjqMqqeux6CrbBHrtSlh /4l1FrybnnFaDB6moAKowIz7ROL7n/oK0NOSARBhLnT6ECTTGfD5wDdpJoGRaMLeRHmIsPgh9hrct lZ/RzufzsH4G8ajL3EosOUhqIZfEOOvXENIU9WEn2lZ88IIkQcj2h7Mwbmf9qcrGPpCYieQJpzkyA 8SRUVuLg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lgSkC-000GZI-5F; Tue, 11 May 2021 13:48:16 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lgS8k-00HaYj-1y for linux-arm-kernel@desiato.infradead.org; Tue, 11 May 2021 13:09:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Type:MIME-Version:Message-ID: Date:Subject:CC:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=xSzs3lKMND/MxulfsstKRGjzlmsiynKnDKP5NMjqgHE=; b=pBLJrJvMngnAfQp0GtJu8KzazP z7vNIGRCTU3jOuzx5ci1c/EkjXImj0VcS6a38NcSLgMQ4AWA9dS8qKAaVyA+clAm4PR4Xm34L9+2j xkVpHaI7/f9ImxCfX6jBQdrJKS03DuNpLxb0CkZijsFAsc0VD3EIKkmkITeQlm3n3iQBCjI3H04ZB atangDV4UMtkHE1drOe/zPszV6d0n8dwjbemaJ+/osLa+IJzgXkpbcoDJm44JwjnssTV6vzvV1z0+ HJM3COtQ+UqvKiRamfJFYG/aO7i9Vo4s3/8/T0MfI7YnmMpyDdDGUglJBpaVnbLbAjYJZDxhfPnil q9LRnbYQ==; Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lgS8g-009bVl-QR for linux-arm-kernel@lists.infradead.org; Tue, 11 May 2021 13:09:32 +0000 Received: from dggeme762-chm.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4FfdV83zbxz61QB; Tue, 11 May 2021 21:06:40 +0800 (CST) Received: from huawei.com (10.67.189.2) by dggeme762-chm.china.huawei.com (10.3.19.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Tue, 11 May 2021 21:09:19 +0800 From: Lexi Shao To: CC: , , , , , , , , Subject: [PATCH] arm: perf: fix sw event kernel backtrace with unwind Date: Tue, 11 May 2021 21:09:12 +0800 Message-ID: <20210511130912.118504-1-shaolexi@huawei.com> X-Mailer: git-send-email 2.12.3 MIME-Version: 1.0 X-Originating-IP: [10.67.189.2] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggeme762-chm.china.huawei.com (10.3.19.108) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210511_060931_215174_11B0ECB9 X-CRM114-Status: GOOD ( 11.10 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- 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; \