Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1369132imi; Sat, 23 Jul 2022 03:55:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1seddMm6n1n/tdULkryomSczF+8nzw0b9vrr6bAwDlMXGPYkKgWAScYzq3VG/AKTlfCYYpL X-Received: by 2002:a05:6402:4381:b0:43b:cb55:ae3c with SMTP id o1-20020a056402438100b0043bcb55ae3cmr3746637edc.45.1658573713569; Sat, 23 Jul 2022 03:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658573713; cv=none; d=google.com; s=arc-20160816; b=Va9JKHYIsKseW23Fe68npeOAV+35LC0hcv8JsKna2+g7ay8ci3A5r0ETz+sBiaOmI0 a7j/sZWZUF53KPHd9jOMQNNv3XyQyf6n8HYEp28vBGyCN+AAJv649ScPPjJSGKuAwM08 iG0PI0sn0AA7Jujg7bfA7RlwQfamRXezr92sTGn4LfVF/e5l7WJpVplz/VoWwcydLD8o L/uQaODhsJTRxAJPIHcyxYzgSIDczmGhNujoYMaZO9Ow5P1jPur2OHf1XRvTXKvk+o1g iyjaFM5Mf9Al7D0kDYbBJlEGviQ3kZ9uL4WX11aax47f44+9MquIVTvRs5qZJ2Vs52Uc p4iQ== 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=8/4lenf3P3UilpG5a2TQ8Tarquot4zWDfFixQXswJ7I=; b=BjHJkOm6NJ6g9azyCXb6nvQP85Gn5K0x9AJ75i0BLketHlKSdmoUY/OBtW4xvZxL8h VO56izbqmnbQz1wSjnru/UpLMgE0AW/DLw6mFKBvsBko1njzrtfjB1MxPCAjsEKO3uqt l1XhV8Zxe4D5kohpyJHs9ZVIjJEUMwBGfGc27qZugjWmhwv6poNOrvEjbYC94WLEHUkR NIKZYJd+b06ONqlJBS1x5qQGLMCYzphOU0dHZY72kd7mKkptATiDt6ircP0NiQtUaBVF /0P9Pm23nSJ+xNg7HbTpi6DeclpbhhDya+CXt02LbnOn5OeoJhN3tU5bGTrFlAthYCaR 2IjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RfJd0RMx; 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 g13-20020a1709065d0d00b0072ee7a299f5si9143504ejt.19.2022.07.23.03.54.48; Sat, 23 Jul 2022 03:55:13 -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=RfJd0RMx; 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 S238797AbiGWKIA (ORCPT + 99 others); Sat, 23 Jul 2022 06:08:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238692AbiGWKHS (ORCPT ); Sat, 23 Jul 2022 06:07:18 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 873E8C47F9; Sat, 23 Jul 2022 03:01:24 -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 B9BD9B82C1F; Sat, 23 Jul 2022 10:01:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23DCCC341C0; Sat, 23 Jul 2022 10:01:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658570481; bh=kcMSOh3DA5LA50BmAg8peRwkcHb2FZBw+YEVoL5xv/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RfJd0RMxPwZ647VcF6MVqwHnL6E0KlE79pfIqqkVuW5iLxiy2MVd//FF71ZUV4bj5 w24WGvnZBScwgjt2qNuyXCqC4poA2kUNbkrRYUJ5yEV5P5/PsLVRH9BS3tcemVWW+t 9+ngstt07AL83mvknI4EVfXbhAya2luDT0n0MQKI= 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 101/148] x86/bugs: Optimize SPEC_CTRL MSR writes Date: Sat, 23 Jul 2022 11:55:13 +0200 Message-Id: <20220723095252.687980835@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 c779bc1a9002fa474175b80e72b85c9bf628abb0 upstream. When changing SPEC_CTRL for user control, the WRMSR can be delayed until return-to-user when KERNEL_IBRS has been enabled. This avoids an MSR write during context switch. 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 | 2 +- arch/x86/kernel/cpu/bugs.c | 18 ++++++++++++------ arch/x86/kernel/process.c | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -254,7 +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); +extern void write_spec_ctrl_current(u64 val, bool force); /* * With retpoline, we must use IBRS to restrict branch prediction --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -62,13 +62,19 @@ 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) +void write_spec_ctrl_current(u64 val, bool force) { if (this_cpu_read(x86_spec_ctrl_current) == val) return; this_cpu_write(x86_spec_ctrl_current, val); - wrmsrl(MSR_IA32_SPEC_CTRL, val); + + /* + * When KERNEL_IBRS this MSR is written on return-to-user, unless + * forced the update can be delayed until that time. + */ + if (force || !cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS)) + wrmsrl(MSR_IA32_SPEC_CTRL, val); } /* @@ -1253,7 +1259,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; - write_spec_ctrl_current(x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base, true); } switch (mode) { @@ -1308,7 +1314,7 @@ static void __init spectre_v2_select_mit static void update_stibp_msr(void * __unused) { - write_spec_ctrl_current(x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base, true); } /* Update x86_spec_ctrl_base in case SMT state changed. */ @@ -1551,7 +1557,7 @@ static enum ssb_mitigation __init __ssb_ x86_amd_ssb_disable(); } else { x86_spec_ctrl_base |= SPEC_CTRL_SSBD; - write_spec_ctrl_current(x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base, true); } } @@ -1769,7 +1775,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)) - write_spec_ctrl_current(x86_spec_ctrl_base); + write_spec_ctrl_current(x86_spec_ctrl_base, true); 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) - write_spec_ctrl_current(msr); + write_spec_ctrl_current(msr, false); } static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)