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.1 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,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 7105BC47082 for ; Thu, 3 Jun 2021 13:45:07 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 2D94F61263 for ; Thu, 3 Jun 2021 13:45:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2D94F61263 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=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: 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: List-Owner; bh=v3tSc3SFL0Jnh2dYlMQqX0/H9NXiGOeKwrw5/i91x+U=; b=Tu1VqjFHABPILh RE2VjtT2MCPc2xMIeJ29rvX4IMevdShyzwlPxDf2/UdMyKbHJbOFAVU7b7vroNbp90i9QzamVxf/+ ee5bheXTEFTYc7zssm0PeMV0c6fyBasiMk5lll5gyoMK1ErWE5VTz4ZSXKS+dI6VUj5tyIEHINSmm J9kM1KWuTbHKAiMo28L+SnUHlbDqritIbW5uc1uF7spBVo+vFGUFP0KhTO63rfBNnQwlIgMjuUaVw u4mUJR2bj2iq+QmGh9slztwoXfXS5mijwoBnZD5JnytCMwqoBx/YabDdq6HMqmO+1iV74KvprXE/m gs9JyAZHs5rjS9LOaJoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1loncz-008xug-1X; Thu, 03 Jun 2021 13:43:17 +0000 Received: from szxga01-in.huawei.com ([45.249.212.187]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1loncu-008xt2-Dg for linux-arm-kernel@lists.infradead.org; Thu, 03 Jun 2021 13:43:14 +0000 Received: from dggeme762-chm.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Fwn8M5GPYzYnXT; Thu, 3 Jun 2021 21:40:19 +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; Thu, 3 Jun 2021 21:43:04 +0800 From: Lexi Shao To: CC: , , , , , , , , Subject: Re: [PATCH] arm: perf: fix sw event kernel backtrace with unwind Date: Thu, 3 Jun 2021 21:43:00 +0800 Message-ID: <20210603134300.117549-1-shaolexi@huawei.com> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20210601124254.GA28025@willie-the-truck> References: <20210601124254.GA28025@willie-the-truck> MIME-Version: 1.0 X-Originating-IP: [10.67.189.2] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) 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-20210603_064312_907255_F2F715D7 X-CRM114-Status: GOOD ( 20.60 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org >On Tue, May 11, 2021 at 09:09:12PM +0800, Lexi Shao wrote: >> 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; \ > >This doesn't feel like the right solution to me: the caller is passing us '__ip' with the expectation that it corresponds to the PC, so that is what we should be using, not recreating our own value like this. > >In other words, if unwinding doesn't work using CALLER_ADDR0, then this seems like an issue that would affect more than just perf. > >Will CALLER_ADDR0 isn't used as PC for backtracing in other places. For arm backtracing function such as __save_stack_trace, function is marked as noinline and the address is used as PC. So it seems that only perf is affected. I understand that arg __ip of macro perf_arch_fetch_caller_regs can cause confusion. Since perf_arch_fetch_caller_regs is only called in the wrapper function perf_fetch_caller_regs(struct pt_regs *regs), is it better to remove the __ip argument and let perf_arch_fetch_caller_regs to decide the pc value it uses ? e.g.: static inline void perf_fetch_caller_regs(struct pt_regs *regs) { perf_arch_fetch_caller_regs(regs); } #define perf_arch_fetch_caller_regs(regs) { \ (regs)->ARM_pc = CALLER_ADDR0; \ (regs)->ARM_fp = (unsigned long) __builtin_frame_address(0); \ (regs)->ARM_sp = current_stack_pointer; \ (regs)->ARM_cpsr = SVC_MODE; \ } Lexi _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel