Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp9813432rwb; Thu, 24 Nov 2022 19:37:22 -0800 (PST) X-Google-Smtp-Source: AA0mqf5g/dhb+LpNmpIXgiry2j1cjdFt+K0Xzu84pG/h1Zr/hQ3+5vK2jYKBz1GPNKApyjKaB7bJ X-Received: by 2002:a17:90a:2b47:b0:218:9671:3b85 with SMTP id y7-20020a17090a2b4700b0021896713b85mr29893565pjc.4.1669347442367; Thu, 24 Nov 2022 19:37:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669347442; cv=none; d=google.com; s=arc-20160816; b=Dme13RqVIZ9kcr4KujpV5EhkZQqbrBWQp1ckhQM4lxDfQQRORvPy6P+0k+gu8egVQQ bA/b/Oqc5SRE9BlBSUuDH7BaXjA8CRPWqd2NTgBgsR/DRBSheGHLyrFgjd4YRQM2YYyV amybj+3cz+JLBJnpkCH5qEfybReSVGs2tdxPnRx+BhFP1yls8n24QZE3wR0BayTt+wAV SBD+6UCJftG4QrSF0xvTGEWNHQXd5VO7XjaMbOapJ8nF6L46YwlV5q7CDpS4yipldvWo TX+I1x4fLLxFrm25xRotueoKqq6W8UpU0ClqHomwq85dhqefaa1iSGCpTbZaiOvV6SjV /NWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=5pt0VB4oGeu/HKxuXZG2DMwDq6ADxS0ZE6BQVIAEIsA=; b=CHAzSEHa+2k2sH+gUKZAj7/OKu3v+5hoEB4vCW+uLfkpghI9XTOTsRpLHljVu75MtJ /P/YV7yKLJ/Ui3Y2s+R7S3MDmU/kFFUWsCCargZzxJDg++XAroSkHoiqC8T7nmksOZ7M tJcrbFK5YinJOmgtKIpjv2OFRns9lpabzdiUpBQGn7/KKxOQYchyy66BWSAKP9buSGfG JnOO60hYRicCDMwRZjCoNZKMm83/VamRAqh43HcLib+4rh5T1wxJkfPqF5Y+M6UI1dlh QmXw2Is79Va68pkGmI8yFL+bS2gBktmpiBR5wqCKTkRN1v4Qxrx65qwR/gw68ZL88/rd qX7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PZXX2W6R; 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 i6-20020a170902c94600b001890c6feffasi2821519pla.568.2022.11.24.19.37.09; Thu, 24 Nov 2022 19:37:22 -0800 (PST) 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=PZXX2W6R; 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 S229538AbiKYDKq (ORCPT + 87 others); Thu, 24 Nov 2022 22:10:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229564AbiKYDKm (ORCPT ); Thu, 24 Nov 2022 22:10:42 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1584627B38 for ; Thu, 24 Nov 2022 19:10:41 -0800 (PST) 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 BAF59B828FF for ; Fri, 25 Nov 2022 03:10:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52D6FC4314A for ; Fri, 25 Nov 2022 03:10:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669345838; bh=YD1OJKovdpUC2Nge/ZjqMz79WqqUoI25lGdOW5MoehI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=PZXX2W6RNRlTqrmztC79E5BBIVyljue38SLdUj/p/ZS307UNqPuV73AIsqKd5jG1I OIWlXPotpmyy+vZVXp6GafzYnNLHw15+BUry962ZktCXx4el2Skjr8ZnktIVwnU3H7 9feHhN/BEwGdC8DyxAmo+U1KvIZhVHGOL/6a7/KBXUakwVvuZ4VB8It2Z3d2gRdUk+ GEN53BWaaMAE0VZmk9qriEs1aGmHCr4ApdGkU6YqZp8Xkx9t7KnQL+BDd3g0M03vt/ vIC5AM57MoUke84jZE5StPkHHf5TX3kF9xCCaiSGDtzLZNV3Zq4mT/Hvi5TOBZiakc Mhy+2SFXEUFiA== Received: by mail-ej1-f53.google.com with SMTP id bj12so7476245ejb.13 for ; Thu, 24 Nov 2022 19:10:38 -0800 (PST) X-Gm-Message-State: ANoB5pnoYx76oJP0NhcZeqydYAeteLCu0DY6UtCjA8QI270CWRUsj+/b k5mA8Z+XlAHUC15gAXGofwXTDb2YYloE9e7ossg= X-Received: by 2002:a17:906:4e8c:b0:7ba:9c18:1204 with SMTP id v12-20020a1709064e8c00b007ba9c181204mr7071645eju.262.1669345836283; Thu, 24 Nov 2022 19:10:36 -0800 (PST) MIME-Version: 1.0 References: <20221123142025.1504030-1-suagrfillet@gmail.com> <20221123142025.1504030-2-suagrfillet@gmail.com> In-Reply-To: From: Guo Ren Date: Fri, 25 Nov 2022 11:10:24 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/2] riscv/ftrace: add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support To: Song Shuai Cc: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, peterz@infradead.org, jolsa@redhat.com, bp@suse.de, jpoimboe@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 On Fri, Nov 25, 2022 at 9:53 AM Song Shuai wrote: > > Guo Ren =E4=BA=8E2022=E5=B9=B411=E6=9C=8824=E6=97=A5= =E5=91=A8=E5=9B=9B 15:31=E5=86=99=E9=81=93=EF=BC=9A > > > > On Thu, Nov 24, 2022 at 1:27 AM Song Shuai wrot= e: > > > > > > Guo Ren =E4=BA=8E2022=E5=B9=B411=E6=9C=8823=E6=97= =A5=E5=91=A8=E4=B8=89 23:02=E5=86=99=E9=81=93=EF=BC=9A > > > > > > > > Cool job, thx. > > > > > > > > On Wed, Nov 23, 2022 at 10:20 PM Song Shuai = wrote: > > > >> > > > >> This patch adds DYNAMIC_FTRACE_WITH_DIRECT_CALLS support for RISC-= V. > > > >> > > > >> select the DYNAMIC_FTRACE_WITH_DIRECT_CALLS to provide the > > > >> register_ftrace_direct[_multi] interfaces allowing users to regist= er > > > >> the customed trampoline (direct_caller) as the mcount for one or > > > >> more target functions. And modify_ftrace_direct[_multi] are also > > > >> provided for modifying direct_caller. > > > >> > > > >> To make the direct_caller and the other ftrace hooks (eg. function= /fgraph > > > >> tracer, k[ret]probes) co-exist, a temporary register is nominated = to > > > >> store the address of direct_caller in ftrace_regs_caller. After th= e > > > >> setting of the address direct_caller by direct_ops->func and the > > > >> RESTORE_REGS in ftrace_regs_caller, direct_caller will be jumped t= o > > > >> by the `jr` inst. > > > >> > > > >> Signed-off-by: Song Shuai > > > >> --- > > > >> arch/riscv/Kconfig | 1 + > > > >> arch/riscv/include/asm/ftrace.h | 6 ++++++ > > > >> arch/riscv/kernel/mcount-dyn.S | 4 ++++ > > > >> 3 files changed, 11 insertions(+) > > > >> > > > >> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig > > > >> index 39ec8d628cf6..d083ec08d0b6 100644 > > > >> --- a/arch/riscv/Kconfig > > > >> +++ b/arch/riscv/Kconfig > > > >> @@ -278,6 +278,7 @@ config ARCH_RV64I > > > >> select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 > > > >> select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && MMU && $(cc-o= ption,-fpatchable-function-entry=3D8) > > > >> select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRAC= E > > > >> + select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS > > > >> select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL > > > >> select HAVE_FUNCTION_GRAPH_TRACER > > > >> select HAVE_FUNCTION_TRACER if !XIP_KERNEL && !PREEMPTION > > > >> diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/= asm/ftrace.h > > > >> index 01bebb28eabe..be4d57566139 100644 > > > >> --- a/arch/riscv/include/asm/ftrace.h > > > >> +++ b/arch/riscv/include/asm/ftrace.h > > > >> @@ -114,6 +114,12 @@ struct ftrace_regs; > > > >> void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, > > > >> struct ftrace_ops *op, struct ftrace_regs *= fregs); > > > >> #define ftrace_graph_func ftrace_graph_func > > > >> + > > > >> +static inline void arch_ftrace_set_direct_caller(struct pt_regs *= regs, unsigned long addr) > > > >> +{ > > > >> + regs->t1 =3D addr; > > > > > > > > How about regs->t0 =3D addr; ? > > > > And delete all mcount-dyn.S modification. > > > > > > > The direct_caller has the same program layout as the ftrace_caller, w= hich means > > > the reg t0 will never be changed when direct_caller returns. > > > > > > If regs->t0 changes here and ftrace_regs_caller executes `jr t0`, > > > direct_caller will enter the dead loop. > > > > > > Actually the reg t0 always saves the address of function entry with 8= B > > > offset, it should only > > > changed by the IPMODIFY ops instead of the direct_ops. > > How about: > > static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs, > > unsigned long addr) > > { > > regs->t1 =3D regs->t0; > > regs->t0 =3D addr; > > > > direct_caller: > > add sp,sp,-? > > sd t1,?(sp) > direct_caller also serves as the first trampoline as ftrace_caller, like = this: > ``` > func -- direct_caller > -- ftrace_[regs]_caller > ``` > So the t1 in this line has to be t0 to save the PC. direct_caller: add sp,sp,-? sd t1,?(sp) sd t0, ?(so) sd ra,?(sp) mov t0, t1 call foo ld t0,?(sp) ld t1,?(sp) ld ra,?(sp) add sp,sp,? jr t1 // <- back to function entry > > sd ra,?(sp) > > call foo > > ld t1,?(sp) > And this line. > > ld ra,?(sp) > > add sp,sp,? > > jr t1 // <- back to function entry > > > > And delete all mcount-dyn.S modification. > > > > > >> > > > >> +} > > > >> + > > > >> #endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ > > > >> > > > >> #endif /* __ASSEMBLY__ */ > > > >> diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mc= ount-dyn.S > > > >> index 466c6ef217b1..b89c85a58569 100644 > > > >> --- a/arch/riscv/kernel/mcount-dyn.S > > > >> +++ b/arch/riscv/kernel/mcount-dyn.S > > > >> @@ -233,6 +233,7 @@ ENDPROC(ftrace_caller) > > > >> #else /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ > > > >> ENTRY(ftrace_regs_caller) > > > >> SAVE_ABI_REGS 1 > > > >> + REG_S x0, PT_T1(sp) > > > >> PREPARE_ARGS > > > >> > > > >> ftrace_regs_call: > > > >> @@ -241,7 +242,10 @@ ftrace_regs_call: > > > >> > > > >> > > > >> RESTORE_ABI_REGS 1 > > > >> + bnez t1,.Ldirect > > > >> jr t0 > > > >> +.Ldirect: > > > >> + jr t1 > > > >> ENDPROC(ftrace_regs_caller) > > > >> > > > >> ENTRY(ftrace_caller) > > > >> -- > > > >> 2.20.1 > > > >> > > > > > > > > > > > > -- > > > > Best Regards > > > > Guo Ren > > > > > > > > -- > > Best Regards > > Guo Ren > Thanks, > Song --=20 Best Regards Guo Ren