From mboxrd@z Thu Jan 1 00:00:00 1970 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 37E54C433F5 for ; Mon, 31 Jan 2022 17:19:26 +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=AGmPxn493J637xS0jcAxXqWIa/3O1gObulEtIloxi38=; b=Rt7K0BJs0aK7V5 KWVYDTfCjQv9X3sxJ5/0tbwcJAS70bKoc709Gdn26RMu6ba01pDprFDChwhVMH7SlGkS4+W1I5DvA q9FLwN9RAT9fCcKJf6eCCkMNPTtL3qMDRaWW09erg3Fxrhg8sll3zO/CsuWuupSI79pzbuvZqN/9D y6sL2Wne4RwlbYMXYw+Jig1wRWWBx0qtFqU0pW01ALORYs7LjnIyN7+K3q6UBTo9fTMguXzYnlVS6 CtuSSQZsuLZ2SjlpvQ6RMDHL9Tx7iN9ilKIW2TS7c8V8N55g3oyb0zWRNVDfJDyJO1SyNJBLy6B4Q BWEPQV2QZ0rQZkKcXHsw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nEaJA-00A3MF-Mm; Mon, 31 Jan 2022 17:17:41 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nEa63-009zGg-ST for linux-arm-kernel@lists.infradead.org; Mon, 31 Jan 2022 17:04:10 +0000 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 8BD88B82BB0; Mon, 31 Jan 2022 17:04:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08D7BC340EE; Mon, 31 Jan 2022 17:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643648645; bh=vlKhqHRgXa/2PAAiHgYRPQ+moro6dzL/dC9QRAOQGV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bT40aoPVPbL2VaftUCQd+E4yi7o0gABuN7FUqdYRa4Hg2U6CeSfgXmxE1kjAdtGfz A3I4Dieqv1BQMlT3SkbcidRmKmvPicaXc25Agn993Fi+dwnGynnIXxxVuisvWvCoCB vUM054yJdplX5GMkj3Tn7e5GIkmZAbXEF/iA2yeF9yU+loVZYSL1mUysm7wSxYe5Em QH401arhVek0X6HVuUUSCMHXHIHfiwgfkFa17Gupjf2Lwr8ref9Xq+xd5UD/jCQuBx Te2il8MI21lb9YQ7DlxvRDQDSlidqNTuKXlwCPv+k7fCix3VXYpRWZEHFYzuVpAHv9 it8fc9YnRZJIQ== From: Ard Biesheuvel To: linux@armlinux.org.uk Cc: linux-arm-kernel@lists.infradead.org, Ard Biesheuvel , Steven Rostedt , Sudeep Holla , Cristian Marussi , Nathan Chancellor , Nick Desaulniers , Arnd Bergmann , Linus Walleij , Masami Hiramatsu Subject: [PATCH v2 04/12] ARM: ftrace: avoid redundant loads or clobbering IP Date: Mon, 31 Jan 2022 18:03:39 +0100 Message-Id: <20220131170347.381551-5-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220131170347.381551-1-ardb@kernel.org> References: <20220131170347.381551-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3992; h=from:subject; bh=vlKhqHRgXa/2PAAiHgYRPQ+moro6dzL/dC9QRAOQGV0=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBh+BZivWsbzE+mj9ZwyB2SWZq35jcLW9YwfrrKSjAU w5kVFz6JAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYfgWYgAKCRDDTyI5ktmPJESTC/ 92cj2rNHE56/EeCCErYcObUhUzWRESiCrvXmjA3ydLNZ0429gjDQjapqkcgIFR14/CA6MM6F28mN9Y YSL8nETqzsNSbmHCtFKJ36bKB8fSqY4zudMJNilMv9tAvhCrSsCjfoSCenCqnuKZ9pkfdq5hw6CpuG 22KdJQuAX3AjwsuE2goIBnqgUJiV6k8V8Sddf+Z87wEz3VpfsjHfrWvPs0vnmVYoe4qfQTrMzD2mpY DkdYMc84tmdNfbru5gfCx5qN13ngaWFPKIPVVdxHYQbdo5eYsp9RPlP7kp9xNJ7HZV4oI261prcex7 o9TJuttFhtwRNoHMLfrrWfw4mGZmD7DzWGNjiO6Ku8tSBktXqqg4ZHs98hMngFrF/ggizfzHF0Ncuo BhhZdDV3tzf+zdUNIbQTvAEn+azm2IS5CaH1aBHXCWZ0vyUu7H5c+R3YmcRVaYQguUwtDYwkKLUbMZ RwdGJNC/QFQN6m0gFDVOsKt/uSVEepbs8ueblzgHgBgHc= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220131_090408_270488_72A73F53 X-CRM114-Status: GOOD ( 17.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Tweak the ftrace return paths to avoid redundant loads of SP, as well as unnecessary clobbering of IP. This also fixes the inconsistency of using MOV to perform a function return, which is sub-optimal on recent micro-architectures but more importantly, does not perform an interworking return, unlike compiler generated function returns in Thumb2 builds. Let's fix this by popping PC from the stack like most ordinary code does. Signed-off-by: Ard Biesheuvel --- arch/arm/kernel/entry-ftrace.S | 51 +++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/arch/arm/kernel/entry-ftrace.S b/arch/arm/kernel/entry-ftrace.S index 237d435e29aa..67548c38a567 100644 --- a/arch/arm/kernel/entry-ftrace.S +++ b/arch/arm/kernel/entry-ftrace.S @@ -22,10 +22,7 @@ * mcount can be thought of as a function called in the middle of a subroutine * call. As such, it needs to be transparent for both the caller and the * callee: the original lr needs to be restored when leaving mcount, and no - * registers should be clobbered. (In the __gnu_mcount_nc implementation, we - * clobber the ip register. This is OK because the ARM calling convention - * allows it to be clobbered in subroutines and doesn't use it to hold - * parameters.) + * registers should be clobbered. * * When using dynamic ftrace, we patch out the mcount call by a "add sp, #4" * instead of the __gnu_mcount_nc call (see arch/arm/kernel/ftrace.c). @@ -70,26 +67,25 @@ .macro __ftrace_regs_caller - sub sp, sp, #8 @ space for PC and CPSR OLD_R0, + str lr, [sp, #-8]! @ store LR as PC and make space for CPSR/OLD_R0, @ OLD_R0 will overwrite previous LR - add ip, sp, #12 @ move in IP the value of SP as it was - @ before the push {lr} of the mcount mechanism + ldr lr, [sp, #8] @ get previous LR - str lr, [sp, #0] @ store LR instead of PC + str r0, [sp, #8] @ write r0 as OLD_R0 over previous LR - ldr lr, [sp, #8] @ get previous LR + str lr, [sp, #-4]! @ store previous LR as LR - str r0, [sp, #8] @ write r0 as OLD_R0 over previous LR + add lr, sp, #16 @ move in LR the value of SP as it was + @ before the push {lr} of the mcount mechanism - stmdb sp!, {ip, lr} - stmdb sp!, {r0-r11, lr} + push {r0-r11, ip, lr} @ stack content at this point: @ 0 4 48 52 56 60 64 68 72 - @ R0 | R1 | ... | LR | SP + 4 | previous LR | LR | PSR | OLD_R0 | + @ R0 | R1 | ... | IP | SP + 4 | previous LR | LR | PSR | OLD_R0 | - mov r3, sp @ struct pt_regs* + mov r3, sp @ struct pt_regs* ldr r2, =function_trace_op ldr r2, [r2] @ pointer to the current @@ -112,11 +108,9 @@ ftrace_graph_regs_call: #endif @ pop saved regs - ldmia sp!, {r0-r12} @ restore r0 through r12 - ldr ip, [sp, #8] @ restore PC - ldr lr, [sp, #4] @ restore LR - ldr sp, [sp, #0] @ restore SP - mov pc, ip @ return + pop {r0-r11, ip, lr} @ restore r0 through r12 + ldr lr, [sp], #4 @ restore LR + ldr pc, [sp], #12 .endm #ifdef CONFIG_FUNCTION_GRAPH_TRACER @@ -132,11 +126,9 @@ ftrace_graph_regs_call: bl prepare_ftrace_return @ pop registers saved in ftrace_regs_caller - ldmia sp!, {r0-r12} @ restore r0 through r12 - ldr ip, [sp, #8] @ restore PC - ldr lr, [sp, #4] @ restore LR - ldr sp, [sp, #0] @ restore SP - mov pc, ip @ return + pop {r0-r11, ip, lr} @ restore r0 through r12 + ldr lr, [sp], #4 @ restore LR + ldr pc, [sp], #12 .endm #endif @@ -202,16 +194,17 @@ ftrace_graph_call\suffix: .endm .macro mcount_exit - ldmia sp!, {r0-r3, ip, lr} - ret ip + ldmia sp!, {r0-r3} + ldr lr, [sp, #4] + ldr pc, [sp], #8 .endm ENTRY(__gnu_mcount_nc) UNWIND(.fnstart) #ifdef CONFIG_DYNAMIC_FTRACE - mov ip, lr - ldmia sp!, {lr} - ret ip + push {lr} + ldr lr, [sp, #4] + ldr pc, [sp], #8 #else __mcount #endif -- 2.30.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel