Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp477971imi; Fri, 22 Jul 2022 03:17:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vPt27pdysWBpfvKFW3cfB1l9nvKYu+RX6G5n5eaHw7Pf2sEfR0fIi1fiTT0U9IkfaaSAsM X-Received: by 2002:a05:6a00:a26:b0:528:9831:d935 with SMTP id p38-20020a056a000a2600b005289831d935mr2682766pfh.25.1658485032252; Fri, 22 Jul 2022 03:17:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658485032; cv=none; d=google.com; s=arc-20160816; b=V1losaspbCyxbhMAobKCB5o2xNudpK5fJEUGssmqgx4J/XVuWBGbOetRNzT0Bqtlqi zBc/Q/BxURYwp7z2EJtTC6c3deZmp0N1Z3O6PdwE71iqe4VJE3mGsrF4LjHS5rVRNsRx Y/B3CxwsHwsleH+ty7JoftnVOvLrgww1h5n92269Qp8OeFsZIbPqjBRpxxnR3w1s0W9C zT/E3VnaFKROs+Adfg6YPDHLWZuhFUvzDrGh7CrM+WfkL+EN//e2FgYUpN8uMLvIrPeV ppYJ6DOz6FjOquStNVz9itm1ozWsl1n5rpI/iCgJAAQWH/hmfgRDouoeKv9z62B5rdpL ug/A== 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=CDieHc9TmkD01DATUcfRSpd7NivSHBgLAtoXUXnZ5ahLDUDwDr1UldYjMI2AoX34WA 0M7hwmlAKd2/1uUwmIql14CLZeijCI1VeYunSjjEFxqPnvRkajliooePBi0zdRwBinC1 vgXEgcnNaR4E5y4KeE+yFMZo0UwuMfsgWwzJbEVBWOq23d/97iurxa/3saQP/YukjWc6 hAU9S22+JokQCNlDktbJJcAt2gvtxY+nNUlEVJ/vi/OUKoMRQ/38MUXGPod/f+pBMjga n2ikD5PEgbWB/Rsmbgrh+aSfu0O9zm8oTj3iXBk3w7bpRu1751BmfKXKqih/2PNDrZb3 N4PQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Hb/nxUdA"; 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 v18-20020a056a00149200b005228c3285f6si6255705pfu.168.2022.07.22.03.16.57; Fri, 22 Jul 2022 03:17:12 -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="Hb/nxUdA"; 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 S236058AbiGVJUj (ORCPT + 99 others); Fri, 22 Jul 2022 05:20:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235870AbiGVJT5 (ORCPT ); Fri, 22 Jul 2022 05:19:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96B2EBB5D7; Fri, 22 Jul 2022 02:13:39 -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 6C64E61F63; Fri, 22 Jul 2022 09:13:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7718EC341C6; Fri, 22 Jul 2022 09:13:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658481217; bh=WBiwT2yc/NGkHe0muEFiSHX5dPep+wgeR6ZmXIblcp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hb/nxUdAK1Tn8XDu2/2uutuvA6X3UT+hex+JcBrla4wGD0i/C2P5TDpeTJ/YtbLan 0vtZ3Uh9lzrj+6odofrPTt59JnRcrFym0kEd5Nje3NXr/KLzKGk4n86RfqpNzJO1JG BrbyzcpDsveIEBrxiiK0gWzsE4chaK0hBobpjCsw= 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/89] x86/alternative: Try inline spectre_v2=retpoline,amd Date: Fri, 22 Jul 2022 11:10:51 +0200 Message-Id: <20220722091134.336521073@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220722091133.320803732@linuxfoundation.org> References: <20220722091133.320803732@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.8 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 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;