Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp814063rdb; Fri, 25 Aug 2023 01:40:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzvdH4zqvZXq4+g+LSLrnnMfaJj5r8lH7MgnXz/wMMQsuw3Xvt1Ol1XhR/qNJvWXldK+bv X-Received: by 2002:a17:906:51c7:b0:9a1:c2fe:41d9 with SMTP id v7-20020a17090651c700b009a1c2fe41d9mr7379659ejk.47.1692952830562; Fri, 25 Aug 2023 01:40:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692952830; cv=none; d=google.com; s=arc-20160816; b=fEHPV/8FX/qkXE049lS7kgtjYklrQ89haiqHkvcIs9N6k5YIlKlh7sJxBbhGtCASAN k4GzK9+T9rJuDDm+ouoLZ+6YXUpjhjcSN+U3QEcqfvTQF+Pu+h9I75PjuKLgymEEOn4f K505XmGg8WX3OsABQ3PRu1XVpIDl/zr1ORUmIjgukbCcIJPNElEHsXufbNBFotPgwBJ1 KWZ13GpaYFQnFn9Vv0mP3hmLdhlcanIuyXdBfGxDI2x2Rqr0J1Mwv+4Pygn+bXLn6Xsc jbBX41WwO8ovtlA1rC8UPRwGKPuDoi/4Ojrq3T60QOJjCBtwLd1vqTs4k0k8jRE5fWI/ JFDw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9FMo+SjWKkBAAx5yFA94DLNYZ8yTDUtw3bvtyWfOH3M=; fh=ZwaEcDYNCnlh+U7YxZ1LCEcet9pcdhSkEX0cCEOksvY=; b=m1FrNJSAP42aWw/RwTUUXq6UlTJTCk/aoK2ex7bvu1wo5l3Kh1NBM5czz+hZZ69CWc TnS6vJDgdnwhHK8dOpqL24KzZT95nGQJLIyj9Y8xdObGFXxK+osafhu7o+ReLPUgTM/D HL3cuioUw3g85YYEaPQ1G2OungiA+2hNhFDpFv4/JVpZXMtoOL44aaEZJsJQKmMr7Vs0 St5fA/r6B3C67XdGutZLVQTIwqGVijrMv3vGZiTez89DilPjZ5oYHHwwhDZrh4YiQVLH +kpHn/vLFcppa3GQcS2HqO41aeMzfLbFGyFq3Ba56qQxoYL3aeqVyWrov1dFT4CgurN/ MQ+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f86tODei; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i2-20020a1709064ec200b0099cbf068b6bsi672523ejv.588.2023.08.25.01.40.00; Fri, 25 Aug 2023 01:40:30 -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=@kernel.org header.s=k20201202 header.b=f86tODei; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242796AbjHYHCV (ORCPT + 99 others); Fri, 25 Aug 2023 03:02:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241459AbjHYHCH (ORCPT ); Fri, 25 Aug 2023 03:02:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 975BFD3 for ; Fri, 25 Aug 2023 00:02:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 14D5B64B51 for ; Fri, 25 Aug 2023 07:02:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F30FC433C8; Fri, 25 Aug 2023 07:02:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692946923; bh=Xp2jfXjyEgqmoD8jY9HdUw+ZtjFXipVZdEkED1w56r0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f86tODeiYcBdYxfl6H8GtHo/0zU1CHLRqj04HI+KL0ztUrS1Bwfx0b8SxBYzjsL/j vanVR10c0Dyeh2Y14tQAsm4No9I2PlHYktSQyvyPS3wbwahDxumMys1pQOV1vzVoWx 97aBxbQ86PFDJVEefb0oGnRKlhCMUOs9Xu/JnExLxoEF1P6Pyo5U5cKjYP6rj6ozln NebnZDKKvNxAE/O8iXkbF4oq24UM+j+wet+Q5PmbsNE81A+XnAt2k1gxFnVsOsDW+h gORzeMZGsK8tRHdFIE9d+06ywmia1HsC2yROjcXeqTlzh6ovUpYARmhL5rhzifU9U8 p8eE8wQKpxB+g== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Borislav Petkov , Peter Zijlstra , Babu Moger , Paolo Bonzini , Sean Christopherson , David.Kaplan@amd.com, Andrew Cooper , Nikolay Borisov , gregkh@linuxfoundation.org, Thomas Gleixner Subject: [PATCH 02/23] x86/srso: Set CPUID feature bits independently of bug or mitigation status Date: Fri, 25 Aug 2023 00:01:33 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 Booting with mitigations=off incorrectly prevents the X86_FEATURE_{IBPB_BRTYPE,SBPB} CPUID bits from getting set. Also, future CPUs without X86_BUG_SRSO might still have IBPB with branch type prediction flushing, in which case SBPB should be used instead of IBPB. The current code doesn't allow for that. Also, cpu_has_ibpb_brtype_microcode() has some surprising side effects and the setting of these feature bits really doesn't belong in the mitigation code anyway. Move it to earlier. Fixes: fb3bd914b3ec ("x86/srso: Add a Speculative RAS Overflow mitigation") Reviewed-by: Nikolay Borisov Reviewed-by: Borislav Petkov (AMD) Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/processor.h | 2 -- arch/x86/kernel/cpu/amd.c | 28 +++++++++------------------- arch/x86/kernel/cpu/bugs.c | 13 +------------ 3 files changed, 10 insertions(+), 33 deletions(-) diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index fd750247ca89..9e26294e415c 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -676,12 +676,10 @@ extern u16 get_llc_id(unsigned int cpu); #ifdef CONFIG_CPU_SUP_AMD extern u32 amd_get_nodes_per_socket(void); extern u32 amd_get_highest_perf(void); -extern bool cpu_has_ibpb_brtype_microcode(void); extern void amd_clear_divider(void); #else static inline u32 amd_get_nodes_per_socket(void) { return 0; } static inline u32 amd_get_highest_perf(void) { return 0; } -static inline bool cpu_has_ibpb_brtype_microcode(void) { return false; } static inline void amd_clear_divider(void) { } #endif diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 7eca6a8abbb1..b08af929135d 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -766,6 +766,15 @@ static void early_init_amd(struct cpuinfo_x86 *c) if (cpu_has(c, X86_FEATURE_TOPOEXT)) smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1; + + if (!cpu_has(c, X86_FEATURE_IBPB_BRTYPE)) { + if (c->x86 == 0x17 && boot_cpu_has(X86_FEATURE_AMD_IBPB)) + setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE); + else if (c->x86 >= 0x19 && !wrmsrl_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB)) { + setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE); + setup_force_cpu_cap(X86_FEATURE_SBPB); + } + } } static void init_amd_k8(struct cpuinfo_x86 *c) @@ -1301,25 +1310,6 @@ void amd_check_microcode(void) on_each_cpu(zenbleed_check_cpu, NULL, 1); } -bool cpu_has_ibpb_brtype_microcode(void) -{ - switch (boot_cpu_data.x86) { - /* Zen1/2 IBPB flushes branch type predictions too. */ - case 0x17: - return boot_cpu_has(X86_FEATURE_AMD_IBPB); - case 0x19: - /* Poke the MSR bit on Zen3/4 to check its presence. */ - if (!wrmsrl_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB)) { - setup_force_cpu_cap(X86_FEATURE_SBPB); - return true; - } else { - return false; - } - default: - return false; - } -} - /* * Issue a DIV 0/1 insn to clear any division data from previous DIV * operations. diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index bdd3e296f72b..b0ae985aa6a4 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -2404,26 +2404,15 @@ early_param("spec_rstack_overflow", srso_parse_cmdline); static void __init srso_select_mitigation(void) { - bool has_microcode; + bool has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE); if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off()) goto pred_cmd; - /* - * The first check is for the kernel running as a guest in order - * for guests to verify whether IBPB is a viable mitigation. - */ - has_microcode = boot_cpu_has(X86_FEATURE_IBPB_BRTYPE) || cpu_has_ibpb_brtype_microcode(); if (!has_microcode) { pr_warn("IBPB-extending microcode not applied!\n"); pr_warn(SRSO_NOTICE); } else { - /* - * Enable the synthetic (even if in a real CPUID leaf) - * flags for guests. - */ - setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE); - /* * Zen1/2 with SMT off aren't vulnerable after the right * IBPB microcode has been applied. -- 2.41.0