From patchwork Thu Oct 12 02:47:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 151660 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp945001vqb; Wed, 11 Oct 2023 19:47:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLC1zvR1xr7F9T3hKDQLafUNen2BJone/9wFZyBCg2CqyJBEAv02LtxXHTFt6ZbunvkY8n X-Received: by 2002:a05:6870:509:b0:1bf:9fa2:bfa3 with SMTP id j9-20020a056870050900b001bf9fa2bfa3mr24506344oao.1.1697078874602; Wed, 11 Oct 2023 19:47:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697078874; cv=none; d=google.com; s=arc-20160816; b=iSFam1WuXA4e4jK7u9ARyplzVwWoQJqaROT+NWVTryX1AQFboJvzu8WfXAwyRsxh/K SncAt7AcsnqbCxOWWfwygsd71YWiA6cvpJIkUu0PTy1ZaU7E0v0Cp21C7SkGeFZxvbb3 m0Ey0vS0DPBw86kNrRsAK/oERES5RVj3jGI6CI+S8Ib6LfG4S2mfY2bV8qqH/6vXMPd2 D+CYExtAOrl9+d8pZRLLPcCLVxLi3cN5Le7pCRcBwMzWHCO6DLT47ppETM9aYSvS7mIh usQ41+X+yN29remVCelfz7G5nHh4m9stNlqnfJtJvlHT+VnUYg/i4KxaO9l4fDyg9eAP JvNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=AQuzfbD0Khk5x5KuLO16sQu2sqt/lovedljOZqndKJg=; fh=GfP2fXubZ1Wn/83gcagvnbPSZkRsv/t7CztUMqA9EH8=; b=mbXo3Wbiy1GtBEIEpywWmMKIxLufwdgX89qvzplE92gHv26R27dqdqPqGBX/JRpo8B aHagRSO60poFMb9raJOZpzmkvXYiR2Mei6FV0FUWMaNsGw3pJoBKYwy3nU9/venAESPu xGdhNAVitPluMqcZqXNyJGjjWKFFyeGfKOb5q7/4WTkwOKqREtXbCV6Wj46qHpkxBbbt uyc5F1xPNe5zlBtUXYvSHUxHiBNGGvMMPDsKGvC0adMdV9Xq8i0mLctAmx2D1SWjvr7v SJrBKiwXvWfaC92YZKpjRZNMlQQ32irlGoJxubTwWFsJzWscaUweF+I6w7sXLCYpb0zm Qdhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eL+erSy7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id a185-20020a6390c2000000b0059ee2eaa346si1196028pge.495.2023.10.11.19.47.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 19:47:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eL+erSy7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 129CA8041E9B; Wed, 11 Oct 2023 19:47:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376758AbjJLCrm (ORCPT + 18 others); Wed, 11 Oct 2023 22:47:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233040AbjJLCrl (ORCPT ); Wed, 11 Oct 2023 22:47:41 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58B50A4 for ; Wed, 11 Oct 2023 19:47:40 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9EC2C433C8; Thu, 12 Oct 2023 02:47:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697078860; bh=vhaHa8oKbBL75azKtpMU5ef0xkRutZtJTgdLyMCYYkg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eL+erSy7d10Jqji3GPYgAwTz6KfGrwSZ38v1Yhjdgos3aZ4jVE2EFku1x6pGc1NVK 3kwMcM9THnC6qTF9iCJBOdIK50UH8tciwpeNXAc44rGz/m5PDFplWN8E9Msoxt9y34 a5+Z+jVf087YhX+BNBInV01ed+EZGAsrvopZOr0aNGaw5S9Dyu8TLnKC0Y1n2Gfma+ fSBqNp5Mqte4siSpugopi9qfkLvhjWt/uskk0IKX/3NMc+PG0RiKs7Jpow89q6E3Gt /BmFvA6mCfCwCnoIaiDXceOYBY5C006Es0IL1yNHk5i+xA574KO0hANbo/HOFJE+MD QKNJurnGQ+6bA== Date: Wed, 11 Oct 2023 19:47:37 -0700 From: Josh Poimboeuf To: Peter Zijlstra Cc: Borislav Petkov , David Kaplan , x86@kernel.org, luto@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] objtool: Fix return thunk patching in retpolines Message-ID: <20231012024737.eg5phclogp67ik6x@treble> References: <20231010171020.462211-2-david.kaplan@amd.com> <20231010174833.GG14330@noisy.programming.kicks-ass.net> <20231010195721.p5pb273kevg7ydxz@treble> <20231010200429.GIZSWuTWSUM9aId7a6@fat_crate.local> <20231010201912.7pjksbparssqu34k@treble> <20231010212254.ypk2wdogno55shit@treble> <20231011074142.GK14330@noisy.programming.kicks-ass.net> <20231011162843.grv6kixw4ides6uw@treble> <20231011223513.GH6307@noisy.programming.kicks-ass.net> <20231012022758.lf62lgf5jx5xrno7@treble> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20231012022758.lf62lgf5jx5xrno7@treble> 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 11 Oct 2023 19:47:52 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779516178032927751 X-GMAIL-MSGID: 1779516178032927751 With CONFIG_RETHUNK enabled, the compiler replaces every RET with a tail call to a return thunk ('JMP __x86_return_thunk'). Objtool annotates all such return sites so they can be patched during boot by apply_returns(). The implementation of __x86_return_thunk() is just a bare RET. It's only meant to be used temporarily until apply_returns() patches all return sites with either a JMP to another return thunk or an actual RET. The following commit e92626af3234 ("x86/retpoline: Remove .text..__x86.return_thunk section") retpolines broke objtool's detection of return sites in retpolines. Since retpolines and return thunks are now in the same section, the compiler no longer uses relocations for the intra-section jumps between the retpolines and the return thunk, causing objtool to overlook them. As a result, none of the retpolines' return sites get patched. Each one stays at 'JMP __x86_return_thunk', effectively a bare RET. Fix it by teaching objtool to detect when a non-relocated jump target is a return thunk (or retpoline). Fixes: e92626af3234 ("x86/retpoline: Remove .text..__x86.return_thunk section") Reported-by: David Kaplan Signed-off-by: Josh Poimboeuf Acked-by: Peter Zijlstra (Intel) --- tools/objtool/check.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index e308d1ba664e..e94756e09ca9 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1610,6 +1610,22 @@ static int add_jump_destinations(struct objtool_file *file) return -1; } + /* + * An intra-TU jump in retpoline.o might not have a relocation + * for its jump dest, in which case the above + * add_{retpoline,return}_call() didn't happen. + */ + if (jump_dest->sym && jump_dest->offset == jump_dest->sym->offset) { + if (jump_dest->sym->retpoline_thunk) { + add_retpoline_call(file, insn); + continue; + } + if (jump_dest->sym->return_thunk) { + add_return_call(file, insn, true); + continue; + } + } + /* * Cross-function jump. */