Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp645073pxb; Tue, 5 Apr 2022 17:07:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeiuBPQQgKyXIBpxmAkNILG173J2sQ++OvRtgn6mDVSPgjekBuZQkAFxcjtHDTOd/+3UjO X-Received: by 2002:a17:903:2446:b0:154:3bb0:7ba0 with SMTP id l6-20020a170903244600b001543bb07ba0mr5748718pls.38.1649203676835; Tue, 05 Apr 2022 17:07:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649203676; cv=none; d=google.com; s=arc-20160816; b=llzHJ7CgvHMnU3vg88qycaGgk7P8b7d/yQvyqGba8yASPAxk6jGX4/Bmv+7euhaSur jzgQBb9CUJlPoMNmrO9EwTSa7vqB8wFjpeR5WpWqRICGDSf67hKBalj4QAuGKoXLRBxu F791IAtwmaxGmVeN9yq84WGdRw8k3gBA5LKQ1F1AdL/n0YLMrzUFIJ1DaW80wPO3RzF1 efVUGDIzIZOksI4pQ3bEXmOdBspnkCOE9PWDg8xXEljIjUPNfnz4C39Q9cVssiRHE67h weWdMgZflC/ify1KMtxyGrbtEZbIkkDWr3kBeyPVeV1WLlxNj6/CVnxDfaCTx5Z2GTxF ZG3g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=R+PQajgwPRGD7x4gXtktOm/Fr82JqJ2+68RD43hgb5E=; b=bAsiiVYTPIEbjv/FwsjY1j2rf2e2+an4NgnAYQQ0DUGv/DTJlO5n2Yt/uGaMjUnT/C 1V1L9pFlmifcBBA2L2xvi2/PZ1hIMJ4og6Wye0Yd5BtaB3kEYkTYtQTtHAM9LQ5ve3/Q U3MxV/hHPkKdMbBqCO6xnBmd79U450EozD7RnEJmD1QY+UfGBkmAdP28odOsePPJjlBd qGYUFIauGD+vRWidUpExicYjiYu3zvkgPBeIGn2bAoWRyMraFr1CbKDeRF4JxStQWpF/ pBkXbuaoRWJNnXi9RHnK+uQLL32OLGs6DlKt7NpuhzNiHfJHjZ2SwRlCFTeCMF4+MlU1 kYZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ShKoHxYE; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id bd14-20020a656e0e000000b003991e150e5fsi7435198pgb.798.2022.04.05.17.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 17:07:56 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ShKoHxYE; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 355061717BB; Tue, 5 Apr 2022 16:52:32 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354816AbiDENXv (ORCPT + 99 others); Tue, 5 Apr 2022 09:23:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345085AbiDEJWP (ORCPT ); Tue, 5 Apr 2022 05:22:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 718E61A83A; Tue, 5 Apr 2022 02:09:07 -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 15670614FC; Tue, 5 Apr 2022 09:09:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22705C385A2; Tue, 5 Apr 2022 09:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149746; bh=IrqnNTvRac6zDFBz8lPZ519erod6BwbMDCIQfnHanu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ShKoHxYEfhqtr1xLSlCZkOVafxg0Skgr+PXUkZTT1OaovAKoAR9o/Nj4ijeW1z0Dc JqMadKgzon76MRqYBmKr5bXD2tiA+sClYu40FXxT8uXuEEiYMBFv3cgc6H7z3VZg1t Hg74NG7TTTbB0Dc59VBLka5aYnOgz3JqAaYaRN/o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ard Biesheuvel , "Steven Rostedt (Google)" , Sasha Levin Subject: [PATCH 5.16 0825/1017] ARM: ftrace: avoid redundant loads or clobbering IP Date: Tue, 5 Apr 2022 09:28:58 +0200 Message-Id: <20220405070418.735094466@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 f4886fb6e9ba..f33c171e3090 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