Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4718imw; Tue, 12 Jul 2022 13:23:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tuKES5DPyhRZRsSLt7KWgFvUk4GhBM3yXxuhrrqcLs7RYZdBQlWqWyMToX74MNzfQyKQnV X-Received: by 2002:a17:906:149:b0:712:502:bc62 with SMTP id 9-20020a170906014900b007120502bc62mr26104879ejh.720.1657657397326; Tue, 12 Jul 2022 13:23:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657657397; cv=none; d=google.com; s=arc-20160816; b=yIBtP+c/HW2eLlggUd3T3F7EVatwXgKvkSxQ2ijyurY5QIXZE6rabybb4DdRSufk4y PZWgK5ZfS7WjnFiooz2mDBVrre9qmXufDVF9/af4/mekYS33bsvyqfgciYWBbTC//1KY jFNLPvUmL+dxZdTZETWL3AycR/MC8Gb6sd7ZpaVtzBvOYQAFP/RolqZoVbxXiGmOcR1g cqjU6+djPKwqeJrJsfR6JFK6fvC/36DkmFvTyEJNPnTOj5bRAHgo7QhCpJHTzGWpD5IY j+FH2SrfCmt5odbvP0kYFyuHcK0wAZJf1hdx5cRChIVBijBKtshe7gFPLJeDTWjW/ymQ OBCw== 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=0v5bjNyHk3kfe4JtUhqxCqexMDBPhThqAKBxMhGG6Kc=; b=E///rBUL2tD4AFd7pG8rxXwvkG/GuL3DqJtOWwiQRsg6Dx2x/X6IV+kAIPybvDu4q4 YFgDi+AJY282dkiAyDrqDR8LaCRUYNuT+z9iRejdpoHLw1K2WDpJD0s7JZKGYcXtpfRH beIw5VgaFkLkRC5c0u+xz+9Wj1oQk/4D163p9xTksboHTuVdqqnZh5rfa0fHsJMznuJx O+Ft8VfDWA8HD2UUTpU1eVCCJu+//Q64/nI5jNd1gIGpLI3d05HlbROxeG+RgXJJfZZZ 2VdlbfkRnK1/0a3FrX3/FgvUDvyHoUVIJEKodTdrPr/iSRulNaXm0an56KQIbrXR3mSF p5gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WVYTiHcU; 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 go31-20020a1709070d9f00b0072b3cd7beb8si19090863ejc.938.2022.07.12.13.22.52; Tue, 12 Jul 2022 13:23:17 -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=WVYTiHcU; 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 S235668AbiGLTK2 (ORCPT + 99 others); Tue, 12 Jul 2022 15:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235783AbiGLTJb (ORCPT ); Tue, 12 Jul 2022 15:09:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8634E586B; Tue, 12 Jul 2022 11:52:17 -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 4DA49B81BAC; Tue, 12 Jul 2022 18:52:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A643FC3411C; Tue, 12 Jul 2022 18:52:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651935; bh=YviMuseI7ZZnS9AUuLIn0isWO6Kg2NnspcB5yQa98XU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WVYTiHcUenj99H0tvSIeA2C8nz9RF//UOL98XOB1wjIFYzSK4cJBGmgFNZlSAghXE DgK5K6YOd+a4GKQiyNRWJde4CMguQiZ+HcOyvcqFZlhI0GB9u2ZTD1SkmeQN9zVDy+ K7VP4zWyPR5U/EwzglnQPNvZYPRTxV/xetY8MPvs= 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 Subject: [PATCH 5.18 27/61] x86/bugs: Keep a per-CPU IA32_SPEC_CTRL value Date: Tue, 12 Jul 2022 20:39:24 +0200 Message-Id: <20220712183238.048856685@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183236.931648980@linuxfoundation.org> References: <20220712183236.931648980@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 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: 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 @@ -253,6 +253,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 @@ -49,12 +49,30 @@ static void __init mmio_select_mitigatio static void __init srbds_select_mitigation(void); static void __init l1d_flush_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. */ @@ -1272,7 +1290,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) { @@ -1327,7 +1345,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. */ @@ -1570,7 +1588,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); } } @@ -1821,7 +1839,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 @@ -600,7 +600,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)