Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1365832imi; Sat, 23 Jul 2022 03:47:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sglGS7yaVnm64EnHdp6z+Zr7V+hfbNb2DUvWL8PIQscfqAQTPZ4i+PINIypcX7377Ynkfh X-Received: by 2002:a05:6402:5384:b0:431:6d84:b451 with SMTP id ew4-20020a056402538400b004316d84b451mr3876093edb.46.1658573255820; Sat, 23 Jul 2022 03:47:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658573255; cv=none; d=google.com; s=arc-20160816; b=iQCQdzGr2qtndjzL/zt5smuC5vO0EsY6Paq6UOLraY0wGzgF68a72vb0FzwGk02P8s 7py6wmZ/Qb+gT6UU3RCpR3uZb1XoZP3rwgIv5+BxIjC35wF24Vfl33FdntuHSFdYnCIS 7uKqC7DvjwhDaGf/tZKE1R5dBAMgiDRoP6ZiBVQcRF/bEvojL+qM7ZHebAP3gT34641p 4Yvafnn2ypKIaykmiMjocU7DSeAsLSGG1d2i55LlI5E+jidet5xRM8kbP2dYH4Anrdo2 NrUKkMgm4ze1YyvE8nOzLU4d9A5g3+jijtyO1Y/D9aDWP0ynQbjW4pZJ3BIC1WJKL7SB Hs2Q== 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=/s6J7dBzp+eEIVuo9scyIWwdicbpPSXxk53ie5RoDwE=; b=BtlFphxBxIy93HzCU9dKr18aFUWAy4AdYDFn7BXijFtBB4t8JR1tjuPnIq/Rb6zfUe TIdIve28BNwJqae8T8MseelKOCMpXc9F7+Qyl4Emi75lJFSORASsMnkKBGlUJAyTbYXO IWBy+L6iGfjR7dT2SLYzbUMPeoG6oZbYJXcRe5Bvn+nV82SW6enzHvV9Y/vkwo16nlo6 zMb02ymwZ64dud+iNg3rC+DEHcj9VV3epqBKAGmaCv9O2Y/dXmRMdKdkZ1MX6CLtPWLA uHTqrSwJv+RNtjjxUQkPNdSdBm20t6N4wsxPUF5s9itW262Kw78BsZ3D/0ijwW/xBJgM Yx2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SaQgJ4La; 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 l26-20020a170906231a00b0072397249ecbsi6675548eja.928.2022.07.23.03.47.08; Sat, 23 Jul 2022 03:47:35 -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=SaQgJ4La; 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 S238655AbiGWKHx (ORCPT + 99 others); Sat, 23 Jul 2022 06:07:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238407AbiGWKHD (ORCPT ); Sat, 23 Jul 2022 06:07:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A560D78596; Sat, 23 Jul 2022 03:01:16 -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 6E4C261212; Sat, 23 Jul 2022 10:01:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A6EBC341C0; Sat, 23 Jul 2022 10:01:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658570475; bh=3SxuAmCWCYNcIuuFMjjQd8FxJhJUQ1ERaFp9yFZaXFk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SaQgJ4LazZOocYbdaRyCwyr8AtUt3oMck3VqmRYozMkTC8cltrhDjuiYUIg6PucMG 9D4mBvgwGfYWkn/vRS4SCw0CH5Xl0IyRyj64T13OSt2Ujs+Pb9YNlfQmRYT9yypre5 cAOmUXebOz7QaINx50IxeNhkCHbpiLJ+UaZ8AUC4= 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 , Thadeu Lima de Souza Cascardo , Ben Hutchings Subject: [PATCH 5.10 099/148] x86/bugs: Keep a per-CPU IA32_SPEC_CTRL value Date: Sat, 23 Jul 2022 11:55:11 +0200 Message-Id: <20220723095252.138766172@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220723095224.302504400@linuxfoundation.org> References: <20220723095224.302504400@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 caa0ff24d5d0e02abce5e65c3d2b7f20a6617be5 upstream. Due to TIF_SSBD and TIF_SPEC_IB the actual IA32_SPEC_CTRL value can differ from x86_spec_ctrl_base. As such, keep a per-CPU value reflecting the current task's MSR content. [jpoimboe: rename] Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Borislav Petkov Reviewed-by: Josh Poimboeuf Signed-off-by: Borislav Petkov Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/nospec-branch.h | 1 + arch/x86/kernel/cpu/bugs.c | 28 +++++++++++++++++++++++----- arch/x86/kernel/process.c | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -254,6 +254,7 @@ static inline void indirect_branch_predi /* The Intel SPEC CTRL MSR base value cache */ extern u64 x86_spec_ctrl_base; +extern void write_spec_ctrl_current(u64 val); /* * With retpoline, we must use IBRS to restrict branch prediction --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -48,12 +48,30 @@ static void __init taa_select_mitigation static void __init mmio_select_mitigation(void); static void __init srbds_select_mitigation(void); -/* The base value of the SPEC_CTRL MSR that always has to be preserved. */ +/* The base value of the SPEC_CTRL MSR without task-specific bits set */ u64 x86_spec_ctrl_base; EXPORT_SYMBOL_GPL(x86_spec_ctrl_base); + +/* The current value of the SPEC_CTRL MSR with task-specific bits set */ +DEFINE_PER_CPU(u64, x86_spec_ctrl_current); +EXPORT_SYMBOL_GPL(x86_spec_ctrl_current); + static DEFINE_MUTEX(spec_ctrl_mutex); /* + * Keep track of the SPEC_CTRL MSR value for the current task, which may differ + * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update(). + */ +void write_spec_ctrl_current(u64 val) +{ + if (this_cpu_read(x86_spec_ctrl_current) == val) + return; + + this_cpu_write(x86_spec_ctrl_current, val); + wrmsrl(MSR_IA32_SPEC_CTRL, val); +} + +/* * The vendor and possibly platform specific bits which can be modified in * x86_spec_ctrl_base. */ @@ -1235,7 +1253,7 @@ static void __init spectre_v2_select_mit if (spectre_v2_in_eibrs_mode(mode)) { /* Force it so VMEXIT will restore correctly */ x86_spec_ctrl_base |= SPEC_CTRL_IBRS; - wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base); } switch (mode) { @@ -1290,7 +1308,7 @@ static void __init spectre_v2_select_mit static void update_stibp_msr(void * __unused) { - wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base); } /* Update x86_spec_ctrl_base in case SMT state changed. */ @@ -1533,7 +1551,7 @@ static enum ssb_mitigation __init __ssb_ x86_amd_ssb_disable(); } else { x86_spec_ctrl_base |= SPEC_CTRL_SSBD; - wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base); } } @@ -1751,7 +1769,7 @@ int arch_prctl_spec_ctrl_get(struct task void x86_spec_ctrl_setup_ap(void) { if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) - wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base); if (ssb_mode == SPEC_STORE_BYPASS_DISABLE) x86_amd_ssb_disable(); --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -556,7 +556,7 @@ static __always_inline void __speculatio } if (updmsr) - wrmsrl(MSR_IA32_SPEC_CTRL, msr); + write_spec_ctrl_current(msr); } static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)