Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1903355pxb; Wed, 30 Mar 2022 12:15:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJziAHDSpScZ8zYCTgeNDpFT2A00FvAMkfcDvyT1/irnY0QU0i8dNZpHsXcjai/oBIPk8npP X-Received: by 2002:a17:907:7284:b0:6df:9120:d935 with SMTP id dt4-20020a170907728400b006df9120d935mr1223168ejc.276.1648667739094; Wed, 30 Mar 2022 12:15:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648667739; cv=none; d=google.com; s=arc-20160816; b=SRcZnrXcw45+mg5PfYeHuoBm2kme8c345n3vNAaxwhpS7Buo9qaU8DQv8NFgQwFYrl 1gpQxLSpTPYC4VjjgC1w3XOSlY8yzFvZ0+1pFpAo3ziQzqWogvo5zoDSqITmALddtJm7 7ekw04jbdBts3VVeB7SA8lMKJi9aAVast25d7Tdw/47wt5KGsmoFDd0ioxGIfsODhWg1 d22iHhyU0FdYvQMSIijmCP1PZR7qj/D/52TsTbZ/+QQJr5Lb8fgaPEGXV2mrHzvFNSZr wMLhuWFv7kVqm8p+TUV4tyRgYIciCwr1IkRHpo5+aPCkocjClIxz9gRMOw50ll9dF2XY 2POQ== 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=tDRnkHwEA7mv4ECsAhptVG0/sP/IoE24V+X0cCujOhU=; b=PMX+EOHGlSGZOkW4xJXMayaqLkHV3x1G5bgOCuKA3J+/FJVmJwNhwVHYINMSDhSlnc ZcLaULTt+dOSDl/EfzpyNzQ1CczFRaOtIywtwJyEYAVrbWHh9trXXM5WtX4QsVLh8W8y Oo2+a5Pz4B4pKoUdOesRm3TDHI2p6xigWEWze4YH6Yrc7ej9HBDzPL0/cIdOdefgXVxu X1LCD1+i0588S8Bjt7Zpu7c0w1ITNWCNO6VxQdQtFkcOMzwMHc7xhrnjJ/++7FUoKBNX 4fBIGk5dRTiDP4mf4we3XidOpE4y6Ax711PUSX851n7ZeKdbtbmIwWnb6iM5ONMJqexF UX2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu3d28Fl; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a170906608e00b006df76385c6asi20884726ejj.266.2022.03.30.12.15.14; Wed, 30 Mar 2022 12:15:39 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=Eu3d28Fl; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344586AbiC3MI5 (ORCPT + 99 others); Wed, 30 Mar 2022 08:08:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344593AbiC3LxS (ORCPT ); Wed, 30 Mar 2022 07:53:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 958FB267F88; Wed, 30 Mar 2022 04:49:15 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id D7A5561624; Wed, 30 Mar 2022 11:49:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C371C340EE; Wed, 30 Mar 2022 11:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648640949; bh=Ka5jbGX8/lg8MhfDu2So8lLl4xM7sYE+I8cl5BMDAWU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eu3d28FlhTHJwasVwZ6bxctZs8vXA/Jlnkt+w8zF1mB8in9xYeFXAaBOA4pg0jvo2 1q1Q6aBJa4VT3o7zFF41adNcEtlukkzcMu1Ey99tkz6st1S7AjAmjsIgtT3AX8OC98 +miL+UZRg9kkpsnQXZXNBDT1XvYeD4EYh2NKZtRJgtIYS6pYejD1AbBGHBT6OYTPIN h82+cuv4cu5rj6P62HItCQaBUwff6VmX2SZECQUSupCOrn1f/6SACk0sEGEkyW5oLF A1lMuO2Y6StEU/GyLsojvaw33dJ5ZMGmYNiv1AK/ZsrUIm2rUWlGi1Yf4AxiRhwvwG wFo9gIkGorwDQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ard Biesheuvel , Steven Rostedt , Sasha Levin , linux@armlinux.org.uk, linux-arm-kernel@lists.infradead.org Subject: [PATCH AUTOSEL 5.16 23/59] ARM: ftrace: avoid redundant loads or clobbering IP Date: Wed, 30 Mar 2022 07:47:55 -0400 Message-Id: <20220330114831.1670235-23-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220330114831.1670235-1-sashal@kernel.org> References: <20220330114831.1670235-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Ard Biesheuvel [ Upstream commit d11967870815b5ab89843980e35aab616c97c463 ] 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 Reviewed-by: Steven Rostedt (Google) Signed-off-by: Sasha Levin --- 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 a74289ebc803..5f1b1ce10473 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 "pop {lr}" * 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.34.1