From patchwork Fri Mar 24 03:33:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 13186387 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99206C76195 for ; Fri, 24 Mar 2023 03:34:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231346AbjCXDeJ (ORCPT ); Thu, 23 Mar 2023 23:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231355AbjCXDeF (ORCPT ); Thu, 23 Mar 2023 23:34:05 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 092B228EA1; Thu, 23 Mar 2023 20:34:04 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so415791pjb.0; Thu, 23 Mar 2023 20:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679628843; 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=785nkvLXLOqwUJFTFY6fK6X9RV3Nhiya91FR689lrSE=; b=fpPJUBJY/38xpcsJF34FALF9CVjEQAMWhBPDDm/na6hhnWg/3rU4gJPBftAmERc6cY 0Mc54qaTDoaMYfDgbpR3aWOWcCikvj42q4NnB8xCfh+Q58V41V4GfnrTN8qSx5PWkb89 S5J4GLKqKzOnFxrFU4+xc5gCSzo1mpyKzv9e50p3EQr6byhqi1ELedYgKOuUwV70zPjz 6kyyVG2gHna35IxbLEEvsiuQlCa+3CYoj7sckdLDhqf9H4iHgcE9yUhCEo+874sznLGZ i6o2IAjGcli9kNiHH1arXWxGiN3D/+z20cMolYqVg6dVJYF81/W15SZWUFXFOPAYmcov ceVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679628843; 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=785nkvLXLOqwUJFTFY6fK6X9RV3Nhiya91FR689lrSE=; b=p0QFDMZollI3u5HEJdOf8mJ7v0iPIXU9DHhKFuLBQKWMs8DVDwAneA1I4cBMa4qg71 hqz3Tac+x0OnUvDnrkW5zb3nXY1ARKsI/+rYfMLsHc+8QhRqJAWa8qfSJ6u6pB8rqPzw B0ABAa3wLrx9/rOV5EBjPAPiBxxFjueO/9kYQV57ge/KnAOe/emcH/kat9u03eftFZmI +fZlSSGmHzQJtXq9Cx80fol357+QWNHLHk+WQj/Z5+kAxgy8k/fLNCP1kns+Hl9b2Ktm 4P0aSxj+o4nSCZNyXAY0w34wcbHWVW2ZkdYBiUlS23Nl5tYs78M8b4Pyzy3LNeuYrYQA nSXg== X-Gm-Message-State: AAQBX9f2JTv9+EAtdFO75aJRvCA9n2jN/l9LXlzgInq9LeyB43p7MPTC 7onM8EtHqE0Ds7+p1cE1thY= X-Google-Smtp-Source: AKy350bgucBSfvpj3dOPdElhT92C0wbEvfTy1RgvG4nTZod13oI+HvcD0n1Mo1cpihpzAhGmvM/eqQ== X-Received: by 2002:a17:902:e886:b0:1a0:67b1:a777 with SMTP id w6-20020a170902e88600b001a067b1a777mr1265996plg.61.1679628843698; Thu, 23 Mar 2023 20:34:03 -0700 (PDT) Received: from localhost.localdomain ([221.226.144.218]) by smtp.gmail.com with ESMTPSA id h13-20020a170902f7cd00b0019d1f42b00csm13014101plw.17.2023.03.23.20.34.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 20:34:03 -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, e.shatokhin@yadro.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Song Shuai Subject: [PATCH V8 3/4] riscv: ftrace: Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support Date: Fri, 24 Mar 2023 11:33:41 +0800 Message-Id: <20230324033342.3177979-4-suagrfillet@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230324033342.3177979-1-suagrfillet@gmail.com> References: <20230324033342.3177979-1-suagrfillet@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.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 --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/ftrace.h | 8 ++++++++ arch/riscv/kernel/mcount-dyn.S | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 93d4817c9d73..17052b2dc83c 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -137,6 +137,7 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && MMU && $(cc-option,-fpatchable-function-entry=8) + 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 f26e9f6e2fed..7801c1c8bb5a 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) + move 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)