Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp9762285rwb; Thu, 24 Nov 2022 18:37:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4nUt4/HsDUVEJrznTslZl1Vpg8JrKj4cm/wuSONpbtVJ1EXowIFApDjJX/UtEOuB/714CI X-Received: by 2002:a17:902:bc86:b0:189:6ab:a97c with SMTP id bb6-20020a170902bc8600b0018906aba97cmr16020155plb.39.1669343823993; Thu, 24 Nov 2022 18:37:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669343823; cv=none; d=google.com; s=arc-20160816; b=AO7DyPYalt7AEFPMgUTwF+BZ5+O/DkoOvzQgAyIcXL78/Cf+JrU+Kg9Fob2qjfIVvU bIWOXH9bEKSPn2C11DrHPBKPbY5Bnu8FSsacqpAGnStXhyxMq2KtdBoKzhDs572GlYwg uEOJO/lbRyMVywXB6SnsASPCdrPHi9ctdly9Mpn9jjn/lLyyBM5z4ZafqcVdw/ta+ptY dPwzs5bjULLLT6tyIwnXopZTBFecXpCqVoUBhEW6C3nWebyLKO9zsx6Y/WEJQzwSaRS7 EVVD8Qq7bFQzqFMBtnfJ3k76a4NVdi7rhB6/YjM74DH7OJMMJH6byKc1FcKdaYrzM/dn HRUQ== 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=KXQVt0h+pcpEUzQ0rG/bapcrBmAHayOWk2YjYsGPK0c=; b=z3gS/lcSJ0jVLlE2UWna3moMeTCNTZlhCuZK8iadCJTCSyEHNqbGxuY7RgakMC9CEQ VPoyMxEiVrt2Vbwg5jrvgTCjEbk3xd2Zv3m8KPqyTsQRnVPwlEwdJV8lBch2Qvw+lb0g AL7OZnKnXZnbAtSqJfKeyrHGY0om78BOSaGfK7rmM+TrHDXUBFsIP/XCz2wD3+nETclp u6kAWJ/EFeJIXxwBZS0C2qhNwHf4sKzfQggsDtDBhxV6MZ+brPY00+sLQvzJRgmFQBNp 893J7R9D22C4z3zJJQwyfm3Si7SQ9UHTUFeWjyk7VOmgGX+VbjESkC1wE1+wZ+t16us7 HTQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=L6Yegctt; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i12-20020a170902c94c00b001889d3b1c07si2907535pla.566.2022.11.24.18.36.52; Thu, 24 Nov 2022 18:37:03 -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=@gmail.com header.s=20210112 header.b=L6Yegctt; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229602AbiKYBxe (ORCPT + 87 others); Thu, 24 Nov 2022 20:53:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbiKYBxd (ORCPT ); Thu, 24 Nov 2022 20:53:33 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 063A72AE0A for ; Thu, 24 Nov 2022 17:53:31 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id c21so992765pfb.10 for ; Thu, 24 Nov 2022 17:53:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=KXQVt0h+pcpEUzQ0rG/bapcrBmAHayOWk2YjYsGPK0c=; b=L6YegcttMNJWrf/sMnBsIZ2psJKiN3k0P5iY6QHS965IMIzXJFAu618/8qy70uvlnf yY3Uir/UEnDb1nN6vLycAulTCcBIOm7kK51BjxnGkzcmUazWTr5deEKD8Bl9uzVrKsDI wMaP1ogdR11PgdXJalJYWQZ6q543NMvVAuZCIFHtPXNETiF9FoRdDkGPLnWCi1PlGeyP zyrd05JKGjDC02xH3+lSXh95NnmBZ0IY2bnxMiXLF8P/S4YsqlrpVRazsi3cw2XCe1fE NMqQ5D+fswnqxXHxYvx8cPAQwbJjZMZIn6M9CzqiP3dw5SDGfFMVbi1f0IPROh18LVvc o8bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KXQVt0h+pcpEUzQ0rG/bapcrBmAHayOWk2YjYsGPK0c=; b=v284D57+TVDWF9X4UiaMdizkssY8Q1aUWsWwf32aqZaVl3H2+C+lyc0taNc1ewUhW5 rbjLORK2iZM3on2fIg3PiTWjK/KJywc/DHTSp+ZsThdtEIT0s/AB3jkkqlY5/swZlsZG c2ufHFWN1A2GcpM0ZSO0FUhlqZLBRFRGhiH+zTylXNPLNEQ5kaSZQwVqgT+e1gMIICTp aaO13lkmsK8OAUDcDtS4CS97fzA0/qR9TUwYRflnh3dbcMFryV/a2g6gBwZaAAv+65RS uztJ2jHwe6XPs10166PWRef0mE2vnzrNhxMYRobgdPsTclCC7OrDLQy/aUnYoyf08YrH cqOg== X-Gm-Message-State: ANoB5pmG81kavkpFAnbz5k2VSKiWBc54EFDYNhGq6IV94VtNWHyHLxZ0 ExD671LK2tcnZ7Vs2KHZEhs+EPwAJUgte9SxScA= X-Received: by 2002:aa7:824f:0:b0:56e:8ed7:569f with SMTP id e15-20020aa7824f000000b0056e8ed7569fmr8656515pfn.19.1669341211152; Thu, 24 Nov 2022 17:53:31 -0800 (PST) MIME-Version: 1.0 References: <20221123142025.1504030-1-suagrfillet@gmail.com> <20221123142025.1504030-2-suagrfillet@gmail.com> In-Reply-To: From: Song Shuai Date: Fri, 25 Nov 2022 01:53:19 +0000 Message-ID: Subject: Re: [PATCH 1/2] riscv/ftrace: add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support To: Guo Ren 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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 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 wrote: > > > > 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 w= rote: > > >> > > >> 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 register > > >> 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/f= graph > > >> tracer, k[ret]probes) co-exist, a temporary register is nominated to > > >> store the address of direct_caller in ftrace_regs_caller. After the > > >> setting of the address direct_caller by direct_ops->func and the > > >> RESTORE_REGS in ftrace_regs_caller, direct_caller will be jumped to > > >> 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-opt= ion,-fpatchable-function-entry=3D8) > > >> select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRACE > > >> + 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/as= m/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 *fr= egs); > > >> #define ftrace_graph_func ftrace_graph_func > > >> + > > >> +static inline void arch_ftrace_set_direct_caller(struct pt_regs *re= gs, 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, whi= ch 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 8B > > 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 th= is: ``` func -- direct_caller -- ftrace_[regs]_caller ``` So the t1 in this line has to be t0 to save the PC. > 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/mcou= nt-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