Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4725606imw; Tue, 12 Jul 2022 13:05:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sqNBuG5vzDoQJfNN6IcG91pl0+YCrAZBrAix7zcESOun+9ID1uOYrD0AnU6rAaWgaDkkpe X-Received: by 2002:a17:906:ef8b:b0:72b:45a2:2232 with SMTP id ze11-20020a170906ef8b00b0072b45a22232mr16332267ejb.484.1657656341118; Tue, 12 Jul 2022 13:05:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657656341; cv=none; d=google.com; s=arc-20160816; b=YMJnScgHNrTOn1LDTBa/54ro9YF4acL9gwcugzJcQaLM+WV0ljNKxAxrXMtjkbVjvW HmtCkVOzgtje9GAFQnZy/GZxyT5tHhlTYdCi8lYIF3pexxhd3bClJoZhvjRJPJshmnSQ 226/XQdLTTHuiJxkPrSIw8c0aBntCtJdaJgFkAkwNATANW122wIozbjnfThjnqrGUiHk OupIUCOw/r1z0eVy+cotDiKsoo/Jo85f0imcncuTegntJiMU8f1h0iJlBRZuPGl89xBN iwifxJcKUj5Q8d8lUZPlZkQ6ZSKP9oancehWx4RUUADPotn+w+HDwWC9+CF2vqO8mXzS Kgiw== 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=4kgvDWbIsUzQ5YoAiKiGY4Zy1CHD7LjXClZA7R2UVaM=; b=nSJUTdcytuZ8vU4PdSA1YEMpcAxOyraHH8V9Kw4e8YLlQX7o5Ctgatw7MMpKya9dNr f6+NVJC3hNBG/TvEWnq1uR2x93lrssELlOEiLMovOI/EvrS7D6DYHJjOBZSRveSUVObe vIsDg5obYr7Rd9C8btQov7xyOLdvl7M3nRlEuUK2I6aRdbqpWjL7ktQMUcn5Mdw+qLGC zwB/DaSJm1vi1B/AveUOPMjWTQ9nAgNRy1H9fhCk5JsOk0zqrB/2IEAfZEG2ti67/55i D82I0XjoFUGfZ7Vz72RV36Luy00t92WGFF+KnMi1tH4ArjxhXmyYg0pEFdX88Aa+MD8p QUZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="VeuPd/d3"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q7-20020a056402248700b0043a739b4b93si13328058eda.303.2022.07.12.13.05.13; Tue, 12 Jul 2022 13:05:41 -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=@linuxfoundation.org header.s=korg header.b="VeuPd/d3"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235029AbiGLTAC (ORCPT + 99 others); Tue, 12 Jul 2022 15:00:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235093AbiGLS6j (ORCPT ); Tue, 12 Jul 2022 14:58:39 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A711EEA92; Tue, 12 Jul 2022 11:47:46 -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 ams.source.kernel.org (Postfix) with ESMTPS id F2BB7B81BAC; Tue, 12 Jul 2022 18:47:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D9ECC3411C; Tue, 12 Jul 2022 18:47:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651663; bh=WBiwT2yc/NGkHe0muEFiSHX5dPep+wgeR6ZmXIblcp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VeuPd/d36LSZOmhTQsFnMllwMtgSTsZjgRzVL9xyzu+d0/BfwTFVoTiNzsTcXWXBw FGNUH6VHOA49NLSYZomZk0cLbXbwsYqhi6AAUfeQ/NXH6yyr9dgaeIM0ieF+7kplKP TSB8mNR47vN7b16QMkWeTNLGLxgSt7YV+DaDTaK8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Borislav Petkov , Josh Poimboeuf , Alexei Starovoitov , Thadeu Lima de Souza Cascardo Subject: [PATCH 5.15 17/78] x86/alternative: Try inline spectre_v2=retpoline,amd Date: Tue, 12 Jul 2022 20:38:47 +0200 Message-Id: <20220712183239.542957036@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183238.844813653@linuxfoundation.org> References: <20220712183238.844813653@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=-7.7 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: Peter Zijlstra commit bbe2df3f6b6da7848398d55b1311d58a16ec21e4 upstream. Try and replace retpoline thunk calls with: LFENCE CALL *%\reg for spectre_v2=retpoline,amd. Specifically, the sequence above is 5 bytes for the low 8 registers, but 6 bytes for the high 8 registers. This means that unless the compilers prefix stuff the call with higher registers this replacement will fail. Luckily GCC strongly favours RAX for the indirect calls and most (95%+ for defconfig-x86_64) will be converted. OTOH clang strongly favours R11 and almost nothing gets converted. Note: it will also generate a correct replacement for the Jcc.d32 case, except unless the compilers start to prefix stuff that, it'll never fit. Specifically: Jncc.d8 1f LFENCE JMP *%\reg 1: is 7-8 bytes long, where the original instruction in unpadded form is only 6 bytes. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Borislav Petkov Acked-by: Josh Poimboeuf Tested-by: Alexei Starovoitov Link: https://lore.kernel.org/r/20211026120310.359986601@infradead.org [cascardo: RETPOLINE_AMD was renamed to RETPOLINE_LFENCE] Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/alternative.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -389,6 +389,7 @@ static int emit_indirect(int op, int reg * * CALL *%\reg * + * It also tries to inline spectre_v2=retpoline,amd when size permits. */ static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) { @@ -405,7 +406,8 @@ static int patch_retpoline(void *addr, s /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */ BUG_ON(reg == 4); - if (cpu_feature_enabled(X86_FEATURE_RETPOLINE)) + if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) && + !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) return -1; op = insn->opcode.bytes[0]; @@ -418,8 +420,9 @@ static int patch_retpoline(void *addr, s * into: * * Jncc.d8 1f + * [ LFENCE ] * JMP *%\reg - * NOP + * [ NOP ] * 1: */ /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ @@ -434,6 +437,15 @@ static int patch_retpoline(void *addr, s op = JMP32_INSN_OPCODE; } + /* + * For RETPOLINE_AMD: prepend the indirect CALL/JMP with an LFENCE. + */ + if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { + bytes[i++] = 0x0f; + bytes[i++] = 0xae; + bytes[i++] = 0xe8; /* LFENCE */ + } + ret = emit_indirect(op, reg, bytes + i); if (ret < 0) return ret;