Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2456633rdb; Fri, 8 Dec 2023 08:37:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuLfkyDRfde/War8p4Ns9RO7avTh/DZGa8OMeAfYK7nAb/k0G+o0e7UfgTjCxWlUo1HnIc X-Received: by 2002:a17:90a:4942:b0:286:bf89:5db7 with SMTP id c60-20020a17090a494200b00286bf895db7mr309955pjh.39.1702053421020; Fri, 08 Dec 2023 08:37:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702053421; cv=none; d=google.com; s=arc-20160816; b=HyovB+J0vQI3IGbvfMEcw08vuYoAy8J6SpzmflGFvpK9qMqg4RXSVeQBcYdZ+Cg7mm onHBEEgzH6IpEGSiBokqwk7ibmLKyIDCoRQgnLIlILxhd0M94BUXzS9Y0lNBwjHA7azT drXGugxiPLXRg8N4idfKaKh/AAdreB05tK5UNO6cOmQtoRlVJ6b23fEXNpS/qYs6ZGYx IevmlSI+lM0tHcoHO2W0FcNYmZIWHecaBeyw+epg1dNt5zBfAnAwOg7iEF2xf1Wmfbca UzQxSTHR1BTV4iU3GjDrhnqe5UYZsRy4A/ZwPAiQa1Vl9+Cd5/P4h51ukbqbotEA7WD/ BQ3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FnEOzGeGfI/+FIRIb/xM+hwx8+xQu1ycnrYn9zXT5C8=; fh=hPWrg32dlFKNN5MdTsgIUJTVaEJ0Jhxi8OfbSIPXQ0A=; b=C4Urs5pfsS/cLjIJ1ILfD5vlPi5C9hEvT+mCqauf1fD0646+ZFPgEjvluy1NLqsSIs BGt8AnN4Z/3ESD2TwAT3E8GGghtKrTOACmO/kS548R+EdQlZ5X7f1ILfjCdujYtWmD8u 9Jss5s3BCF4b0YVBMRVpMqWxISDiLiG5yTSTSOpfT5WVMFZyCu5zenFWAmdsQixLoSlT CSi8gd1VhbogkY+DrCoo1aUtjznNZ86aEN1HBkghkfsSRZx4ot+bdmoZd+TrdYlH5B0M Knc/61K45FJ0hfmrNyxdzxZ7Bc0dskPBF+QmHtF+cCDSXdCNKdGHCBdotEbMCopmQGEX lE/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ucggwy5F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id iz22-20020a170902ef9600b001d0050e2452si1792102plb.45.2023.12.08.08.37.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 08:37:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ucggwy5F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 7495383AC5F0; Fri, 8 Dec 2023 08:36:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1574201AbjLHQgS (ORCPT + 99 others); Fri, 8 Dec 2023 11:36:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1574469AbjLHQfc (ORCPT ); Fri, 8 Dec 2023 11:35:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7ED1319B3 for ; Fri, 8 Dec 2023 08:35:37 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97072C433C8; Fri, 8 Dec 2023 16:35:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702053337; bh=7raJIERHI+frfy4evcCjXbQCBLrz7+ndhekYbkUWCw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ucggwy5F5BZXz172PhI07i2kTcoe04uwSX7QNPhw2P4840kQPSngfiErOdWEZVZUf 8T76BRVjSRpjIIU9xWEIo8pMG3XrF3vBB3vODslqGDQ2vY283eRo8d9c1KBSpQaQnn VUNhDQVCQR8kVKY1Hje9m5HxyBmBJELQ1OwdZaTl2b2ogywS4CFPwZC33E7WCdnS8m eyvRbf9C0z970RJOqzOUyc7lcK0vJ9Y7pDV5pvaqEEZLMzYrMLyIkhnFsL4Tl7DVOi 4yAMhNA/Idh3RtTl3h2SrraGMtbcBDfSTVXNghBw9OZNZFWT7rQJm83AGQK9WBHtKp UPBXMaKW9yzig== From: Naveen N Rao To: , Cc: Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , Steven Rostedt , Mark Rutland , Florent Revest , Masami Hiramatsu Subject: [RFC PATCH 8/9] powerpc/ftrace: Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS Date: Fri, 8 Dec 2023 22:00:47 +0530 Message-ID: <62b7c2fcaca546c790a825cf79b9cced1ac8d1db.1702045299.git.naveen@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 08 Dec 2023 08:36:58 -0800 (PST) Add support for DYNAMIC_FTRACE_WITH_DIRECT_CALLS similar to the arm64 implementation. ftrace direct calls allow custom trampolines to be called into directly from function ftrace call sites, bypassing the ftrace trampoline completely. This functionality is currently utilized by BPF trampolines to hook into kernel function entries. Since we have limited relative branch range, we support ftrace direct calls through support for DYNAMIC_FTRACE_WITH_CALL_OPS. In this approach, ftrace trampoline is not entirely bypassed. Rather, it is re-purposed into a stub that reads direct_call field from the associated ftrace_ops structure and branches into that, if it is not NULL. For this, it is sufficient if we can ensure that the ftrace trampoline is reachable from all traceable functions. When multiple ftrace_ops are associated with a call site, we utilize a call back to set pt_regs->orig_gpr3 that can then be tested on the return path from the ftrace trampoline to branch into the direct caller. Signed-off-by: Naveen N Rao --- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/ftrace.h | 15 ++++ arch/powerpc/kernel/asm-offsets.c | 3 + arch/powerpc/kernel/trace/ftrace_entry.S | 99 ++++++++++++++++++------ 4 files changed, 93 insertions(+), 25 deletions(-) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index c8ecc9dcc914..4fe04fdca33a 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -235,6 +235,7 @@ config PPC select HAVE_DYNAMIC_FTRACE select HAVE_DYNAMIC_FTRACE_WITH_ARGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 select HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY + select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS if HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS select HAVE_DYNAMIC_FTRACE_WITH_REGS if ARCH_USING_PATCHABLE_FUNCTION_ENTRY || MPROFILE_KERNEL || PPC32 select HAVE_EBPF_JIT select HAVE_EFFICIENT_UNALIGNED_ACCESS diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/ftrace.h index d9b99781bea3..986c4fffb9ec 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -93,6 +93,21 @@ struct ftrace_ops; #define ftrace_graph_func ftrace_graph_func void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs); + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +/* + * When an ftrace registered caller is tracing a function that is also set by a + * register_ftrace_direct() call, it needs to be differentiated in the + * ftrace_caller trampoline so that the direct call can be invoked after the + * other ftrace ops. To do this, place the direct caller in the orig_gpr3 field + * of pt_regs. This tells ftrace_caller that there's a direct caller. + */ +static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr) +{ + struct pt_regs *regs = &fregs->regs; + regs->orig_gpr3 = addr; +} +#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ #endif #endif /* __ASSEMBLY__ */ diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c index 8b8a39b57a9f..85da10726d98 100644 --- a/arch/powerpc/kernel/asm-offsets.c +++ b/arch/powerpc/kernel/asm-offsets.c @@ -678,6 +678,9 @@ int main(void) #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS OFFSET(FTRACE_OPS_FUNC, ftrace_ops, func); +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + OFFSET(FTRACE_OPS_DIRECT_CALL, ftrace_ops, direct_call); +#endif #endif return 0; diff --git a/arch/powerpc/kernel/trace/ftrace_entry.S b/arch/powerpc/kernel/trace/ftrace_entry.S index 4d1220c2e32f..ab60395fc34b 100644 --- a/arch/powerpc/kernel/trace/ftrace_entry.S +++ b/arch/powerpc/kernel/trace/ftrace_entry.S @@ -33,14 +33,57 @@ * and then arrange for the ftrace function to be called. */ .macro ftrace_regs_entry allregs - /* Save the original return address in A's stack frame */ - PPC_STL r0, LRSAVE(r1) /* Create a minimal stack frame for representing B */ PPC_STLU r1, -STACK_FRAME_MIN_SIZE(r1) /* Create our stack frame + pt_regs */ PPC_STLU r1,-SWITCH_FRAME_SIZE(r1) + .if \allregs == 1 + SAVE_GPRS(11, 12, r1) + .endif + + /* Get the _mcount() call site out of LR */ + mflr r11 + +#ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY + /* + * This points after the bl at 'mtlr r0', but this sequence could be + * outside the function. Move this to point just after the ftrace + * location inside the function for proper unwind. + */ + addi r11, r11, FTRACE_MCOUNT_TRAMP_OFFSET - MCOUNT_INSN_SIZE + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS + /* Load the ftrace_op */ + PPC_LL r12, -SZL-MCOUNT_INSN_SIZE(r11) + +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + /* Load direct_call from the ftrace_op */ + PPC_LL r12, FTRACE_OPS_DIRECT_CALL(r12) + PPC_LCMPI r12, 0 + beq 1f + mtctr r12 + .if \allregs == 1 + REST_GPRS(11, 12, r1) + .endif + addi r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE + bctr +1: +#endif +#endif +#endif + + /* Save the previous LR in pt_regs->link */ + PPC_STL r0, _LINK(r1) + /* Also save it in A's stack frame */ + PPC_STL r0, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE+LRSAVE(r1) + + /* Save our return address as pt_regs->nip */ + PPC_STL r11, _NIP(r1) + /* Also save it in B's stackframe header for proper unwind */ + PPC_STL r11, SWITCH_FRAME_SIZE+LRSAVE(r1) + /* Save all gprs to pt_regs */ SAVE_GPR(0, r1) SAVE_GPRS(3, 10, r1) @@ -54,7 +97,7 @@ .if \allregs == 1 SAVE_GPR(2, r1) - SAVE_GPRS(11, 31, r1) + SAVE_GPRS(13, 31, r1) .else #ifdef CONFIG_LIVEPATCH_64 SAVE_GPR(14, r1) @@ -65,6 +108,13 @@ addi r8, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE PPC_STL r8, GPR1(r1) +#ifdef CONFIG_LIVEPATCH_64 + mr r14, r11 /* remember old NIP */ +#endif + + /* Calculate ip from nip-4 into r3 for call below */ + subi r3, r11, MCOUNT_INSN_SIZE + .if \allregs == 1 /* Load special regs for save below */ mfmsr r8 @@ -76,22 +126,11 @@ li r8, 0 .endif - /* Get the _mcount() call site out of LR */ - mflr r7 -#ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY - /* - * This points after the bl at 'mtlr r0', but this sequence could be - * outside the function. Move this to point just after the ftrace - * location inside the function for proper unwind. - */ - addi r7, r7, FTRACE_MCOUNT_TRAMP_OFFSET - MCOUNT_INSN_SIZE +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + /* Clear orig_gpr3 to later detect ftrace_direct call */ + li r7, 0 + PPC_STL r7, ORIG_GPR3(r1) #endif - /* Save it as pt_regs->nip */ - PPC_STL r7, _NIP(r1) - /* Also save it in B's stackframe header for proper unwind */ - PPC_STL r7, LRSAVE+SWITCH_FRAME_SIZE(r1) - /* Save the read LR in pt_regs->link */ - PPC_STL r0, _LINK(r1) #ifdef CONFIG_PPC64 /* Save callee's TOC in the ABI compliant location */ @@ -99,13 +138,6 @@ LOAD_PACA_TOC() /* get kernel TOC in r2 */ #endif -#ifdef CONFIG_LIVEPATCH_64 - mr r14, r7 /* remember old NIP */ -#endif - - /* Calculate ip from nip-4 into r3 for call below */ - subi r3, r7, MCOUNT_INSN_SIZE - /* Put the original return address in r4 as parent_ip */ mr r4, r0 @@ -122,6 +154,13 @@ .endm .macro ftrace_regs_exit allregs +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + /* Check orig_gpr3 to detect ftrace_direct call */ + PPC_LL r7, ORIG_GPR3(r1) + PPC_LCMPI cr1, r7, 0 + mtctr r7 +#endif + /* Load ctr with the possibly modified NIP */ PPC_LL r3, _NIP(r1) @@ -164,8 +203,12 @@ /* Based on the cmpd above, if the NIP was altered handle livepatch */ bne- livepatch_handler #endif + /* jump after _mcount site */ #ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS + bnectr cr1 +#endif blr #else bctr @@ -227,6 +270,12 @@ ftrace_no_trace: #endif #endif +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS +SYM_FUNC_START(ftrace_stub_direct_tramp) + blr +SYM_FUNC_END(ftrace_stub_direct_tramp) +#endif + #ifdef CONFIG_LIVEPATCH_64 /* * This function runs in the mcount context, between two functions. As -- 2.43.0