Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp200265lqg; Wed, 10 Apr 2024 22:58:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX7TY8FMvZcA89vHd4f5cD82FdEH63uQODNrchz3sPhwnVEzmSfO/ZWAngf1TsENljMzqKQmIac7t9d2BTuMuvtIFhIiRCoIfyCOPvhaQ== X-Google-Smtp-Source: AGHT+IHlvLbPd7e6NLkxJfgqJ5mZS/ubFSvDw/WqcAKb31DSvTUdIao03TMlH7NDo0GXsOwToLIA X-Received: by 2002:a17:902:d34c:b0:1e2:952b:81fc with SMTP id l12-20020a170902d34c00b001e2952b81fcmr4175808plk.29.1712815105034; Wed, 10 Apr 2024 22:58:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712815105; cv=pass; d=google.com; s=arc-20160816; b=MIgTL7AnvJ7Kx9me9gN2Yq7+pna7BlHrfM+Sr2kEh9uk9kg6KQWMFSy9muP11m5Qoh RpBcEe9BGoDgzHOMQ2mgzVO3x5u+6YSRS70q7W3WcsBdULMAdqb25I+g/qhZ3Rh+CV7i zGQu3ahw7Ho6Y2Ooi1Zcr2nCJQ9BuNrhT3CacLo4JrEA3SLY4yo8wHDFyA4KMIq1dQDZ Ep1doeNJSU1E2Z4JJTbLcd8T1DmjLdnC68nWMxp37fyNxoMU6xIVXG5L0wcH0n2Ej42q Jtw/WNvYm3SHzbLbGwr25LEUc9vcvmI063JUqsFJHwTmvFH2E/aeFC1a4Z4aNtBEZTEg 5Myw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Bz3Fhw/FmWc2+sjb+NiMUVY5pebZB4MyyD7lGxW4diw=; fh=7OJFrLnOA6s1ycE/HULnfMliS+3S6sqMIgc3sc3R36M=; b=0XsiUrh+QYGgxUUVaijfY0ZjDT6w/JGQTVdMBZzVfkC6bmV7DN2jHPXGlCG+Rj9yqb LFtzMbWulVkaQex+7XgZrEFxJMq9Jl2BIgyoGDP6anvd+tjUvBauiJLOp2e0r3nsPVhZ NhcuoRyQmGemmGbmDOEQutXlipHx1UaM8X4NejGOFcpKHvRnFaPufBnE1y2R6yGcZb89 QeUwE8xsfFq1U3bz/Sobad22M/FWBfcQ2E+9J8zPOAayrbk5FFiKADm1yMxx3IBIBDiw 3/oeLjLHMHtj82QTbDGqkKeqk3157oySeIvTQ4kXzUO56ZcTNL5zibjMG0YKw1eOuLdI mwjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nJMNErly; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-139797-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139797-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id l11-20020a170902e2cb00b001e44dbfa468si648534plc.532.2024.04.10.22.58.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 22:58:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139797-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nJMNErly; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-139797-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139797-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E0009B242F4 for ; Thu, 11 Apr 2024 05:42:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA61013CABD; Thu, 11 Apr 2024 05:42:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nJMNErly" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE62713CA9C for ; Thu, 11 Apr 2024 05:41:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712814120; cv=none; b=EaLMYXsJj64o05C1wfMAxd+5FKqb5PBeqpw+7FqqTOVZsJX57JIyxOyPR3I3u78kqzSWbuEHqdRK9AlQ8d/0cB7ZGv4G86NMxbuZx8EOkkBA5MIWgV0mXdpTMBTjk1qh/2/yVBOB4q1zy5v5JmiK7i+bcLqFPGh+qDkINGKjgcM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712814120; c=relaxed/simple; bh=OncdHB68GtNbx3dHWQR7cZg44SO1zu1bq2L0lGrG/HY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GZhCftIQFK6xgqV7lt9FrI/G9PrcAAIaq3wwxofN0cX3IzGB6ychjmuBO4Uszx7dcNZRIiJT8UheYL4ZNflcuRWrzLFiFopaBAcacYkTIIowjb0ALMxzKop09G+DaLBt3NFaEmlfuaFgrMqgWeKYI7I16rUaYxvIooOL92sxZno= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nJMNErly; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7CFDC43390; Thu, 11 Apr 2024 05:41:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712814119; bh=OncdHB68GtNbx3dHWQR7cZg44SO1zu1bq2L0lGrG/HY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nJMNErlyM8Vi/USIIrNgPRTSHavW9bC2+Mx1Fuf1DlUxKw73y9z3pCg5uJLvJ/ZdO F61OJgRVUv4sp28NUVPcWuZVlrgTjJIfYI3zRHyJFBeVT9NEqLwkf73amij4D3Tx12 5BH1tM04W83g0xtXbPC1rKNU4EP8JSl7pCZdzUVbXWo3v16Cim4qiexK9yXNK6jUp7 O2wEmDl32kvIE9d+np8Yly7gkcZUzbAqXnHzCjUB+xjTIqNwGlPj+g3UR22JyPDhMx /6QF1GVYXA2/rEsHi3p3WbQkHMw+gJel5fE2yNSB3KYyGvYIh/DyxhQoqhCVlT33nj 9DU1b1lDLyr8Q== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Daniel Sneddon , Pawan Gupta , Thomas Gleixner , Alexandre Chartre , Konrad Rzeszutek Wilk , Peter Zijlstra , Greg Kroah-Hartman , Sean Christopherson , Andrew Cooper , Dave Hansen , Nikolay Borisov , KP Singh , Waiman Long , Borislav Petkov Subject: [PATCH 3/7] x86/bugs: Fix BHI handling of RRSBA Date: Wed, 10 Apr 2024 22:40:47 -0700 Message-ID: <6f56f13da34a0834b69163467449be7f58f253dc.1712813475.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The ARCH_CAP_RRSBA check isn't correct: RRSBA may have already been disabled by the Spectre v2 mitigation (or can otherwise be disabled by the BHI mitigation itself if needed). In that case retpolines are fine. Fixes: ec9404e40e8f ("x86/bhi: Add BHI mitigation knob") Signed-off-by: Josh Poimboeuf --- arch/x86/kernel/cpu/bugs.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 27d6d64eeec3..0755600d5d18 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -1538,20 +1538,25 @@ static enum spectre_v2_mitigation __init spectre_v2_select_retpoline(void) return SPECTRE_V2_RETPOLINE; } +static bool __ro_after_init rrsba_disabled; + /* Disable in-kernel use of non-RSB RET predictors */ static void __init spec_ctrl_disable_kernel_rrsba(void) { - u64 ia32_cap; + if (rrsba_disabled) + return; + + if (!(ia32_cap & ARCH_CAP_RRSBA)) { + rrsba_disabled = true; + return; + } if (!boot_cpu_has(X86_FEATURE_RRSBA_CTRL)) return; - ia32_cap = x86_read_arch_cap_msr(); - - if (ia32_cap & ARCH_CAP_RRSBA) { - x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S; - update_spec_ctrl(x86_spec_ctrl_base); - } + x86_spec_ctrl_base |= SPEC_CTRL_RRSBA_DIS_S; + update_spec_ctrl(x86_spec_ctrl_base); + rrsba_disabled = true; } static void __init spectre_v2_determine_rsb_fill_type_at_vmexit(enum spectre_v2_mitigation mode) @@ -1652,9 +1657,11 @@ static void __init bhi_select_mitigation(void) return; /* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */ - if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) && - !(x86_read_arch_cap_msr() & ARCH_CAP_RRSBA)) - return; + if (cpu_feature_enabled(X86_FEATURE_RETPOLINE)) { + spec_ctrl_disable_kernel_rrsba(); + if (rrsba_disabled) + return; + } if (spec_ctrl_bhi_dis()) return; @@ -2809,8 +2816,7 @@ static const char * const spectre_bhi_state(void) return "; BHI: BHI_DIS_S"; else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP)) return "; BHI: SW loop, KVM: SW loop"; - else if (boot_cpu_has(X86_FEATURE_RETPOLINE) && - !(ia32_cap & ARCH_CAP_RRSBA)) + else if (boot_cpu_has(X86_FEATURE_RETPOLINE) && rrsba_disabled) return "; BHI: Retpoline"; else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP_ON_VMEXIT)) return "; BHI: Syscall hardening, KVM: SW loop"; -- 2.44.0