Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2522478imm; Mon, 10 Sep 2018 02:26:26 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbcPnICfqxRgT0PMEwmkaksj93I4BkYeF23UbwIXcVGgwKsQ3j4kS1cAC5VQHTJkopbLmCa X-Received: by 2002:a63:c912:: with SMTP id o18-v6mr20950380pgg.331.1536571586379; Mon, 10 Sep 2018 02:26:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536571586; cv=none; d=google.com; s=arc-20160816; b=HzBRK28OJ7dLCLl4IAjYUiPHcie+csUbKLUJYz1eCd+jPJAWTsfGEGeRjCi+dOXFF1 Yesrd1S6ngqzksAli2cE5POCoyYw3FgI5tCaIES5cg/5WmzE/Ln+Xo7rnhZv5ddiNwmZ ILCN+zPkmVJ9kcwDzDvlff1f2z/dxlSWCQ8edGDfv7NzGl78GrM+0XGEFHjOSN0IIiwW 3nwOP6po5h/9p51yK+05m3CjlxrDYIZTYimWhyT2Y6SRJwQr5LHyO8CGHDvGP0Wxhsqc uUTc4fIQ6ANLpg/ZPRNIz1aUFuiV599ubCNj5ka9mPCLNBATgreSkw0RnKlhXrGsoASL kgLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=fjvZBqiIxxVK+uH1d7Ka1wCw2vHUvHBmqAKsMlVjgIQ=; b=jD12QbqyYOHqjuWj3UWLOLRuZaVDgNk3ED7Rv3iz4xuMnOK4vpeo2Ojk7bEVqg1qZ6 xhODDWsZNi/p1doJBuXuNXWxa9WyNe/DJs8cFjngKcNgNjCbVHzm8Q1PJ+ttPoH//q3g QvOsjb93+3TmSEW+EyCA3pyceuU2pCDW9DNZlIa9yhF4yf6bg0TNCGZlvAd2MXRAw+D1 TZFln5imU8RHecpZhAMr71kVEpS7Q/NG+Ea+NDV7iX8YbqT4KA/+UyPuUK+qTckOFX4b +gJ0w7wY4iA9KfXwi9xaIuu/ksIBhDzxVKIXNfcTXjJw6A3NqcWTT1QJGyL2kBdgfNt7 E+NQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z20-v6si6376224pgj.159.2018.09.10.02.26.11; Mon, 10 Sep 2018 02:26:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728009AbeIJORs (ORCPT + 99 others); Mon, 10 Sep 2018 10:17:48 -0400 Received: from mx2.suse.de ([195.135.220.15]:39852 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727593AbeIJORs (ORCPT ); Mon, 10 Sep 2018 10:17:48 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 970ECAED0; Mon, 10 Sep 2018 09:24:40 +0000 (UTC) Date: Mon, 10 Sep 2018 11:24:39 +0200 (CEST) From: Jiri Kosina To: Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Josh Poimboeuf , Andrea Arcangeli , "Woodhouse, David" , Andi Kleen , Tim Chen , "Schaufler, Casey" cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v5 2/2] x86/speculation: Enable cross-hyperthread spectre v2 STIBP mitigation In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Kosina STIBP is a feature provided by certain Intel ucodes / CPUs. This feature (once enabled) prevents cross-hyperthread control of decisions made by indirect branch predictors. Enable this feature if - the CPU is vulnerable to spectre v2 - the CPU supports SMT and has SMT siblings online - spectre_v2 mitigation autoselection is enabled (default) After some previous discussion, this patch leaves STIBP on all the time, as wrmsr on crossing kernel boundary is a no-no. This could perhaps later be a bit more optimized (like disabling it in NOHZ, experiment with disabling it in idle, etc) if needed. Note that the synchronization of the mask manipulation via newly added spec_ctrl_mutex is currently not strictly needed, as the only updater is already being serialized by cpu_add_remove_lock, but let's make this a little bit more future-proof. Cc: stable@vger.kernel.org Signed-off-by: Jiri Kosina --- arch/x86/kernel/cpu/bugs.c | 49 +++++++++++++++++++++++++++++++++++++++++----- kernel/cpu.c | 11 ++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 40bdaea97fe7..ccc5e6b7fe40 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -35,12 +35,10 @@ static void __init spectre_v2_select_mitigation(void); static void __init ssb_select_mitigation(void); static void __init l1tf_select_mitigation(void); -/* - * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any - * writes to SPEC_CTRL contain whatever reserved bits have been set. - */ -u64 __ro_after_init x86_spec_ctrl_base; +/* The base value of the SPEC_CTRL MSR that always has to be preserved. */ +u64 x86_spec_ctrl_base; EXPORT_SYMBOL_GPL(x86_spec_ctrl_base); +static DEFINE_MUTEX(spec_ctrl_mutex); /* * The vendor and possibly platform specific bits which can be modified in @@ -325,6 +323,44 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) return cmd; } +static bool stibp_needed(void) +{ + if (spectre_v2_enabled == SPECTRE_V2_NONE) + return false; + + if (!boot_cpu_has(X86_FEATURE_STIBP)) + return false; + + return true; +} + +static void update_stibp_msr(void *info) +{ + wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); +} + +void arch_smt_update(void) +{ + if (stibp_needed()) { + u64 mask; + mutex_lock(&spec_ctrl_mutex); + mask = x86_spec_ctrl_base; + if (cpu_smt_control == CPU_SMT_ENABLED) + mask |= SPEC_CTRL_STIBP; + else + mask &= ~SPEC_CTRL_STIBP; + + if (mask != x86_spec_ctrl_base) { + pr_info("Spectre v2 cross-process SMT mitigation: %s STIBP\n", + cpu_smt_control == CPU_SMT_ENABLED ? + "Enabling" : "Disabling"); + x86_spec_ctrl_base = mask; + on_each_cpu(update_stibp_msr, NULL, 1); + } + mutex_unlock(&spec_ctrl_mutex); + } +} + static void __init spectre_v2_select_mitigation(void) { enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); @@ -424,6 +460,9 @@ static void __init spectre_v2_select_mitigation(void) setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW); pr_info("Enabling Restricted Speculation for firmware calls\n"); } + + /* Enable STIBP if appropriate */ + arch_smt_update(); } #undef pr_fmt diff --git a/kernel/cpu.c b/kernel/cpu.c index aa7fe85ad62e..4bba5071d61e 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -2025,6 +2025,12 @@ static void cpuhp_online_cpu_device(unsigned int cpu) kobject_uevent(&dev->kobj, KOBJ_ONLINE); } +/* + * Architectures that need SMT-specific errata handling during SMT hotplug + * should override these. + */ +void __weak arch_smt_update(void) { }; + static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) { int cpu, ret = 0; @@ -2051,8 +2057,10 @@ static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) */ cpuhp_offline_cpu_device(cpu); } - if (!ret) + if (!ret) { cpu_smt_control = ctrlval; + arch_smt_update(); + } cpu_maps_update_done(); return ret; } @@ -2063,6 +2071,7 @@ static int cpuhp_smt_enable(void) cpu_maps_update_begin(); cpu_smt_control = CPU_SMT_ENABLED; + arch_smt_update(); for_each_present_cpu(cpu) { /* Skip online CPUs and CPUs on offline nodes */ if (cpu_online(cpu) || !node_online(cpu_to_node(cpu))) -- Jiri Kosina SUSE Labs