Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp991574rwb; Fri, 23 Sep 2022 06:57:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7nyxHMkcSuixTS2dhR7vXVaCZIj3K7Dzhc5G70xO5vyX1nWTF2PbTj9nXpRdfFhDHdZU4Q X-Received: by 2002:a05:6402:380a:b0:451:ae08:7a6c with SMTP id es10-20020a056402380a00b00451ae087a6cmr8277138edb.161.1663941460339; Fri, 23 Sep 2022 06:57:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663941460; cv=none; d=google.com; s=arc-20160816; b=Zq/WBX3ADu0KXmSPW2vQ/S+M3WamrIZq0QZkX+iJBtUcDLVR1clXhhM6x03LHVwyX1 PXjz+e/c6c3tXxqRllyf5i/yF8cNVPScaeJ1O7hy5KDpOi/UChCva4077ilUagcezmxG U2EOjF3cWwoAESWEPPjVOyzgrRVb0ApchV9k8OSTwxkHnkK0p81iJIEn1TG9Ly9HphC3 SaVYOC+XrNGVlBUFGH4NGH2lm9fqJ58mfhkl2q1MB2i2sx9zP0hnh2EQI4bpoyedBVBl N/wYM4UWotvoZaUPjAXn4eqQ+cOdCwUqKeQYpDV09J8nDL0fJQp/pUb9cwnRII3+jAom /kNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=l90waPbCLR0wsTDkxIexMYWgpnlapjeDSC62rHfgg1U=; b=UrBgFrAK+i1BASadBHO4WowgZ/0hMpKpHtSEMAnOd7Gb9VAl7tVUBF55ILDjobF/px qf3jfyG55WxQxP1xRKAp8+V9QfURQ32EpukaKEN9U/phQTDDRWPFx0YPZhavAFh8WUYl 2tiO/3X2cQOP3uNdQ0LwiXgUyK2va2EUffe/kAJuZaPEmC86bgL5S5z6UirR6ZKrRGGZ sYJmeeQXLo8VfPnYWW5pwdIQdCQU4e4EXuFbBOU/1yfYXIBmtIrmVgy9c9vkgJREF3dR eJXqgHOI2Nq7G1n9akVPeM+rTqpWbDjZozQef+hrCFe6YDutBJhrl72KAMPmLsFHVr1B Ah/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g14-20020a1709065d0e00b00780a7ebc522si8369073ejt.596.2022.09.23.06.57.14; Fri, 23 Sep 2022 06:57:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231176AbiIWNeT (ORCPT + 99 others); Fri, 23 Sep 2022 09:34:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229726AbiIWNeP (ORCPT ); Fri, 23 Sep 2022 09:34:15 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E99EE131F54; Fri, 23 Sep 2022 06:34:13 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DFD3A1042; Fri, 23 Sep 2022 06:34:19 -0700 (PDT) Received: from FVFF77S0Q05N (unknown [10.57.80.223]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2712F3F73B; Fri, 23 Sep 2022 06:34:10 -0700 (PDT) Date: Fri, 23 Sep 2022 14:34:07 +0100 From: Mark Rutland To: Chen Zhongjin Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-perf-users@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, namhyung@kernel.org, jolsa@kernel.org, guoren@kernel.org, frederic@kernel.org, vincent.chen@sifive.com, ardb@kernel.org, mhiramat@kernel.org, rostedt@goodmis.org, keescook@chromium.org, catalin.marinas@arm.com, Josh Poimboeuf Subject: Re: [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Message-ID: References: <20220921125128.33913-1-chenzhongjin@huawei.com> <7834a259-4bd7-a955-acaa-21c36c7c22c2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <7834a259-4bd7-a955-acaa-21c36c7c22c2@huawei.com> X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 22, 2022 at 04:22:23PM +0800, Chen Zhongjin wrote: > Hi, > > On 2022/9/21 22:02, Mark Rutland wrote: > > On Wed, Sep 21, 2022 at 08:51:23PM +0800, Chen Zhongjin wrote: > > > Currently, the stacktrace with FRAME_POINTER on riscv has some problem: > > > > > > 1. stacktrace will stop at irq so it can't get the stack frames before > > > irq entry. > > > 2. stacktrace can't unwind all the real stack frames when there is > > > k{ret}probes or ftrace. > > > > > > These are mainly becase when there is a pt_regs on stack, we can't unwind > > > the stack frame as normal function. > > > > > > Some architectures (e.g. arm64) create a extra stackframe inside pt_regs. > > > However this doesn't work for riscv because the ra is not ensured to be > > > pushed to stack. As explained in: > > > commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the stack top") > > FWIW, this is also a latent problem on arm64, since we don't know whether the > > LR is live at an exception boundary (and we currently always ignore it). > > In fact this is still a problem for riscv, I didn't unwind the caller of > ftrace or probed func > > of kretprobe because they are inside function pro/epilogue. > > The problem on riscv is a little more complex, for leaf function, ra will > always not be pushed to stack and fp will be pushed to ra slot. This patch > solved this problem. That's the same on arm64, our `LR` is equivalent to your `RA`. I have a series at: https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/log/?h=arm64/stacktrace/metadata ... which explciitly indicates pt_regs::pc and pt_regs::lr in the stack trace output (and for RELIABLE_STACKTRACE we'd mark the LR entries as unreliable). > > My plan to fix that on arm64 is to push an empty frame record to the stack upon > > an exception, and use that to find the pt_regs, from which we can get the PC > > and LR (and then we can report the later as unreliable). That should be roughly > > equivalent to what you do in this series (where use use the LSB to identify > > that the pointer is actually a pt_regs). > > IIRC, this solution looks like: > > ===== > > Func1     { lr, fp } or { nothing } > > irq entry { pt_regs & empty stackframe[2] } > > handler   { lr, fp } > > ====== > > handler->fp points to stackframe, and when we find stackframe is empty, we > know we are inside > > an pt_regs and can find registers by offset. > > I think it's available, there no other scenario will cause the frame list > contains zero (unless stack is clobbered). That's basically what I do in my series; see: https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/commit/?h=arm64/stacktrace/metadata&id=7394a825bc4f8243b28261517999793acb6f11cd ... but I haven't posted that to the list yet as I *also* want to add code to check that we were at an exception boundary when we saw this (based on the prior PC/LR being in the entry text, using some metadata I have yet to add). > And it seems only fp slot should be zero, lr will be clobbered after > function call, we cannot use lr inside pt_regs. I'm not sure what you mean here. I agree that the LR in the pt_regs isn't *reliable*, and won't *always* be valid, but it will sometimes be valid. > > One important thing to note is that when crossing an exception boundary you > > won't know whether RA is live, and so you might try to consume an address twice > > (if it has also been pushed to the stack). That could be problematic for > > unwinding ftrace or kretprobes. On arm64 we had to implement > > HAVE_FUNCTION_GRAPH_RET_ADDR_PTR so that we could reliably unwind ftrace. See > > commit: > > > > c6d3cd32fd0064af ("arm64: ftrace: use HAVE_FUNCTION_GRAPH_RET_ADDR_PTR") > > > > ... and we haven't yet come up with something similar for kretprobes (though I > > suspect we'll need to). > > Can we test the sp and fp inside pt_regs? Because luckily arm64 saves lr, fp > and moves sp together. > > Before sp is moved we will have fp == sp from last frame 'mov x29, sp'. So > if they are same, we uses the lr in pt_regs, conversely we use lr on stack. The frame record (which FP points to) can live anywhere in a stack frame, so comparing aginst SP doesn't tell us anything. Regardless of whether FP == SP, the LTR might be valid or invalid. There is no way of knowing if LR is live without information from the compiler that we don't have today (though from LPC 2022, it seems like CTF *might* be sufficient -- that's on my list of things to look at). Thanks, Mark.