Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4723663rwb; Tue, 20 Sep 2022 19:32:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6QxI8toi7SoPiPl6l4PP96iIZQWY5ntcgPZT2AmKiIIbZ+QtGIWqPxvSS3EXDCe5C/ZA2M X-Received: by 2002:a17:907:3f87:b0:781:f9d1:811 with SMTP id hr7-20020a1709073f8700b00781f9d10811mr1956784ejc.74.1663727545248; Tue, 20 Sep 2022 19:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663727545; cv=none; d=google.com; s=arc-20160816; b=R62boppRMTG5pVKqz+ZKcShJPii3H5nahKWduyBpb+rYlu82G53XWUBSp2xs2ISc/S 3L9MV5TwObRWWwZdUn6DIxU7MY9izqRisLWd3o5Ey8pKQZt0MQcaL5M/UxKqHt2gQ+LP t2OEZ++70YHgv48UsI0zVh7p0XQBfTH3LCuYHp5fc+HvvT3NR4ihTY+KMMjGt8/Md57P WYKpj2aOwIxWExet+sE5qRCR8+yWaQKdphCdlqj9YGXm2zGySi2brP6633PZw7zhOX5/ YQCd7VsWVBh//UG8NP8i0pfXtDv7JXAdpkBNNIsU+s/aFAHPCle9javNJZS+24OFVEpl q4jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=g0giYUWjb+Af2+jfxqxr4olp5klmQW/LtcgYcXQjr6g=; b=YrTGrlRQkxV78dowv/HwhA47wVJArGsDsOTctREoIYctfa47+eVkecXnRKsVgxFWsM WIbSFoPvDVBw42XFwZ0ip61MNNgu9DQHoB8YnJY1wziUZ/J5nQB0ZNILHeoWcONNqM+2 zeEcxJabRuR/Y6Lg7Xd1JiaNMRZ9SngnRPd8X0gcKbxtdmlig4bzvHTM6lZxPeNTrEAx IqHeZG5xQL+pfLyk0RFFMgPFbOU3lFHKS/0K0DXEpNqXYvnNoVLe6by/hFvHYu8mUyxe yehkodvXP3UudYK0kHmNryulq2mSczIT3lhgVdA4p4GeOp9j+a9q/70BqlMclNA2xFKo KJOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RCWADFT0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a170906130600b007743e36f03esi1104277ejb.876.2022.09.20.19.31.59; Tue, 20 Sep 2022 19:32:25 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RCWADFT0; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229862AbiIUCOS (ORCPT + 99 others); Tue, 20 Sep 2022 22:14:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbiIUCOQ (ORCPT ); Tue, 20 Sep 2022 22:14:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ACA57C1F7; Tue, 20 Sep 2022 19:14:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 312DBB82D5D; Wed, 21 Sep 2022 02:14:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7E0EC4347C; Wed, 21 Sep 2022 02:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663726452; bh=qI7h/5oDIg8KfeFQUixqYzNKwl6VFPqXAJjhtSbuMi4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=RCWADFT0FYCuKHjJOaL1bTscBnD+CJHvzrIflu0vrtjEq+XDtYR6luTG0RrfCVKXT zqeADrU7M0ulVNWynrS3swEBDzod6A3vsWB4CrJfg0Yox8J4b1C3NutSRePRbwANnN g3/V50iOphmleNsFLtDPf1Wnz0+4If40uRG6f/wzF82kKJukOAt68ALyK+hGChw3rF 56lNNI5bO4Q2sLr/kEiK6544PdAuv/w8HqNCWTBJrxAPzBsNJiSSix3RnsOMvnMtIc EANrNFVKE2XpYalKJhOjDHJ/zOoi31UhRRLYG3rJwcByuo1lQPx2LEipklt0eOk1QH 2nwhGmzlhyoOQ== Received: by mail-oa1-f53.google.com with SMTP id 586e51a60fabf-1279948d93dso7049706fac.10; Tue, 20 Sep 2022 19:14:12 -0700 (PDT) X-Gm-Message-State: ACrzQf1q9Ilm8M/wzPJIc1m69o7Pb/qncoFtK4iM3MiN/j9JcUQe4VT0 36CQ+53vRoM26yhPLvMraOmAPKAbpax/rdBkDu8= X-Received: by 2002:a05:6870:a78e:b0:12b:542b:e5b2 with SMTP id x14-20020a056870a78e00b0012b542be5b2mr3833650oao.112.1663726451988; Tue, 20 Sep 2022 19:14:11 -0700 (PDT) MIME-Version: 1.0 References: <20220920151202.180057-1-chenzhongjin@huawei.com> <20220920151202.180057-4-chenzhongjin@huawei.com> In-Reply-To: <20220920151202.180057-4-chenzhongjin@huawei.com> From: Guo Ren Date: Wed, 21 Sep 2022 10:13:57 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH -next 3/7] riscv: stacktrace: Save pt_regs in encoded fp on irq entry 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, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, namhyung@kernel.org, jolsa@kernel.org, nsaenzju@redhat.com, frederic@kernel.org, changbin.du@intel.com, vincent.chen@sifive.com, ardb@kernel.org, mhiramat@kernel.org, rostedt@goodmis.org, keescook@chromium.org, catalin.marinas@arm.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 Reviewed-by: Guo Ren ENCODE fp with BIT[0] is okay. On Tue, Sep 20, 2022 at 11:15 PM Chen Zhongjin wrote: > > To support stack unwinding at irq entry, the position of pt_regs saved > on stack is nessesary. Because for some functions, compiler only push > s0/fp on stack without ra. > > As the situation described in > commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the stack top") > > When irq happens there, the the function frame looks like: > > prev function | ... | > | | > normal function +-----------------+ > | ra to prev | > | s0 of prev | > | ... |<-+ > leaf function +-----------------+ | > | s0 of normal | | > | empty slot | | > irq pt_regs +-----------------+ | > | epc (ra to leaf)| | > | ra (ra to norm)| | > | ... | | > | s0 of leaf |--+ > | ... | > +-----------------+ > > If the position of register in pt_regs is {epc, s0}, we can easily > unwind from irq frame to leaf function, as normal functions do. > > However when unwinding from unwinding from leaf to normal, beacause > (ra to norm) is saved in pt_regs, but not stackframe of leaf, we > have to get pt_regs for that. > > To get pt_regs position on stack, we can save the encoded *pt_regs > in s0, as x86 architecture did. Then we can get s0, epc and ra > easily. > > Signed-off-by: Chen Zhongjin > --- > arch/riscv/include/asm/frame.h | 45 ++++++++++++++++++++++++++++++++++ > arch/riscv/kernel/entry.S | 3 +++ > 2 files changed, 48 insertions(+) > create mode 100644 arch/riscv/include/asm/frame.h > > diff --git a/arch/riscv/include/asm/frame.h b/arch/riscv/include/asm/frame.h > new file mode 100644 > index 000000000000..2a1f45cf3a4e > --- /dev/null > +++ b/arch/riscv/include/asm/frame.h > @@ -0,0 +1,45 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _ASM_RISCV_FRAME_H > +#define _ASM_RISCV_FRAME_H > + > +#include > + > +#ifdef CONFIG_FRAME_POINTER > + > +#ifdef __ASSEMBLY__ > + > +/* > + * This is a sneaky trick to help the unwinder find pt_regs on the stack. The > + * frame pointer is replaced with an encoded pointer to pt_regs. The encoding > + * is just setting the LSB, which makes it an invalid stack address and is also > + * a signal to the unwinder that it's a pt_regs pointer in disguise. > + * > + * This macro must be used when sp point to pt_regs > + */ > +.macro ENCODE_FRAME_POINTER > + add s0, sp, 0x1 > +.endm > + > +#else /* !__ASSEMBLY__ */ > + > +#define ENCODE_FRAME_POINTER \ > + "add s0, sp, 0x1\n\t" > + > +#endif /* __ASSEMBLY__ */ > + > +#else /* !CONFIG_FRAME_POINTER */ > + > +#ifdef __ASSEMBLY__ > + > +.macro ENCODE_FRAME_POINTER ptregs_offset=0 > +.endm > + > +#else /* !__ASSEMBLY */ > + > +#define ENCODE_FRAME_POINTER > + > +#endif /* !__ASSEMBLY */ > + > +#endif /* CONFIG_FRAME_POINTER */ > + > +#endif /* _ASM_RISCV_FRAME_H */ > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index b9eda3fcbd6d..ecb15c7430b4 100644 > --- a/arch/riscv/kernel/entry.S > +++ b/arch/riscv/kernel/entry.S > @@ -13,6 +13,7 @@ > #include > #include > #include > +#include > > #if !IS_ENABLED(CONFIG_PREEMPTION) > .set resume_kernel, restore_all > @@ -95,6 +96,8 @@ _save_context: > REG_S s4, PT_CAUSE(sp) > REG_S s5, PT_TP(sp) > > + ENCODE_FRAME_POINTER > + > /* > * Set the scratch register to 0, so that if a recursive exception > * occurs, the exception vector knows it came from the kernel > -- > 2.17.1 > -- Best Regards Guo Ren