From patchwork Tue Jun 27 11:16:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 13294360 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 2C6C5EB64DD for ; Tue, 27 Jun 2023 11:17:05 +0000 (UTC) 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=z1UwCv/nDlTrSfAf8oL4W0LlwXGqZgEaj9k2tE8EOL8=; b=pEM58mZCh6Wot4 qMkJyxaXT3zozpHOKLdvXxRuDhMqoQbdtrjqWzZUs78vCU0I8U2kP/sllXTvJJq6KaHdWs5Lkux/p RxWZHmqYPwItqQ2H8C47uskDSW8LntXSjWgPAZ3L24tfDigUvZpdUB3jGHbIoRKvqICww1TGwKtEi IH7LVpSoPKL4zMkrO3zVoFf5jPqTj2CdMwFEUkq4tqZSGAxxsW1fPVVRa2V+WTNd55aJki1iNVuZX ntI/qZBU+6a32SD8XSD10JbRzHtBltiGDTpn6OfYGV8TTiEVOCFcq6nbXJrFs0nScdRXkvmbj9YX7 GFV0mh58edawPySfy+Gw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qE6gt-00D2NI-2S; Tue, 27 Jun 2023 11:16:59 +0000 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qE6gj-00D2IY-1g for linux-riscv@lists.infradead.org; Tue, 27 Jun 2023 11:16:58 +0000 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6b74e2d8c98so1682700a34.2 for ; Tue, 27 Jun 2023 04:16:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687864599; x=1690456599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=d2K0OuQGxCrdsKt9Q57JYfi5owLHsSbU96G7U8K7Wyc=; b=CLCvM5Ht93QC4AHE/cHmirC2W6l/TIb1ObX5XhXGP8SHTXzvEs2lCRw+Oeri1hVkQC HBmppRBNLP+85jU+6Prb0ahTKN+wZFA5T912jwD8hLJE1CHl6nSV86KRVapsPkxtpxB9 MGRWGHhT81tjm6G+Tgrec5vxJtGBtncLmbh9QefCTGC1KWQ0cepAUTejlZYJf08Jxrd5 7bjth7uO4z8JVkSr11qDSFlldY3tfWAYoNy2aHgNxqvRfWjSnAC2tztXuRGciow1vhK+ h/syJOQewa9LCZ+u/qNZpH4JTGKfxuTOMRIoW5kB1H3Rx6/a1h7OX2Mhar9Rm7tN7v1z nWow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687864599; x=1690456599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d2K0OuQGxCrdsKt9Q57JYfi5owLHsSbU96G7U8K7Wyc=; b=WFys2eiTBVRCibdUbdCGMOGxT1826j+nxB/fMtsMhJS9L/JEKv1cKrJNCQJPWempyT RlE1ucQ6xqt2n4FaOJFh242uWIYUlm75p4dHLHUs4DHosyvbA+SJq/fk8YDNGvMIw/Nt HftaqPoGGJ22DXkVJjLYfmEC4xE+2VSI7DA7w+Y0Ux3crtzEI4cv5h+by/T7uhKaD0qr Y7IdV00W/QuklMoV5QLxNXiIgI1wVnbxtOnBSolSjHEbOOJ1r0A44SJ60riQydWfgUk0 KYxzzZLmM+Ra/trmil2Fj9ARukFaB3zaoDdPs5V4RsTPN9XGGFo5HlzTDE6Dz8OKLwT4 U6Pg== X-Gm-Message-State: AC+VfDxbFW1MRe3CX37mDvdDcJJX96Yaqw4vYRBZvJ57GamwXjHUYVRZ oP0SgHAloS3Da3HTK59zHNKnV2e/H/KPeg== X-Google-Smtp-Source: ACHHUZ6mnka0WW9MVoMGh7/o2+bdi8ZgTA17NxscHP0+9UmQ8Asw92nbo6Zj8qGPGWMQpIW9ln/+GA== X-Received: by 2002:a05:6808:2202:b0:3a1:aa90:d486 with SMTP id bd2-20020a056808220200b003a1aa90d486mr17879039oib.26.1687864598842; Tue, 27 Jun 2023 04:16:38 -0700 (PDT) Received: from localhost.localdomain ([58.240.82.166]) by smtp.gmail.com with ESMTPSA id 8-20020a17090a004800b00262d6ac0140sm5438658pjb.9.2023.06.27.04.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 04:16:38 -0700 (PDT) From: Song Shuai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, rostedt@goodmis.org, mhiramat@kernel.org, mark.rutland@arm.com, guoren@kernel.org, suagrfillet@gmail.com, bjorn@rivosinc.com, jszhang@kernel.org, conor.dooley@microchip.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, songshuaishuai@tinylab.org Subject: [PATCH V11 3/5] riscv: ftrace: Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support Date: Tue, 27 Jun 2023 19:16:10 +0800 Message-Id: <20230627111612.761164-4-suagrfillet@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230627111612.761164-1-suagrfillet@gmail.com> References: <20230627111612.761164-1-suagrfillet@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230627_041649_568660_7CD2934D X-CRM114-Status: GOOD ( 13.43 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/fgraph 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 Tested-by: Guo Ren Signed-off-by: Guo Ren Acked-by: Björn Töpel --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/ftrace.h | 8 ++++++++ arch/riscv/kernel/mcount-dyn.S | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 756d854e6cdd..c3e678450acf 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -145,6 +145,7 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && MMU && (CLANG_SUPPORTS_DYNAMIC_FTRACE || GCC_SUPPORTS_DYNAMIC_FTRACE) + select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRACE select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL select HAVE_FUNCTION_GRAPH_TRACER diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h index 84f856a3286e..84904c1e4369 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -114,6 +114,14 @@ 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 = addr; +} +#define arch_ftrace_set_direct_caller(fregs, addr) \ + __arch_ftrace_set_direct_caller(&(fregs)->regs, addr) #endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S index fb8286b80cfc..b6f4e1847d61 100644 --- a/arch/riscv/kernel/mcount-dyn.S +++ b/arch/riscv/kernel/mcount-dyn.S @@ -231,6 +231,7 @@ ENDPROC(ftrace_caller) #else /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ ENTRY(ftrace_regs_caller) + mv t1, zero SAVE_ABI_REGS 1 PREPARE_ARGS @@ -239,7 +240,10 @@ ftrace_regs_call: call ftrace_stub RESTORE_ABI_REGS 1 + bnez t1,.Ldirect jr t0 +.Ldirect: + jr t1 ENDPROC(ftrace_regs_caller) ENTRY(ftrace_caller) @@ -254,3 +258,9 @@ ftrace_call: jr t0 ENDPROC(ftrace_caller) #endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +SYM_CODE_START(ftrace_stub_direct_tramp) + jr t0 +SYM_CODE_END(ftrace_stub_direct_tramp) +#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */