Skip to content

Commit 53c613f

Browse files
Jiri KosinaKAGA-KOKO
authored andcommitted
x86/speculation: Enable cross-hyperthread spectre v2 STIBP mitigation
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 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. Signed-off-by: Jiri Kosina <jkosina@suse.cz> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: "WoodhouseDavid" <dwmw@amazon.co.uk> Cc: Andi Kleen <ak@linux.intel.com> Cc: Tim Chen <tim.c.chen@linux.intel.com> Cc: "SchauflerCasey" <casey.schaufler@intel.com> Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/nycvar.YFH.7.76.1809251438240.15880@cbobk.fhfr.pm
1 parent dbfe295 commit 53c613f

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

arch/x86/kernel/cpu/bugs.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ static void __init spectre_v2_select_mitigation(void);
3535
static void __init ssb_select_mitigation(void);
3636
static void __init l1tf_select_mitigation(void);
3737

38-
/*
39-
* Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
40-
* writes to SPEC_CTRL contain whatever reserved bits have been set.
41-
*/
42-
u64 __ro_after_init x86_spec_ctrl_base;
38+
/* The base value of the SPEC_CTRL MSR that always has to be preserved. */
39+
u64 x86_spec_ctrl_base;
4340
EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
41+
static DEFINE_MUTEX(spec_ctrl_mutex);
4442

4543
/*
4644
* The vendor and possibly platform specific bits which can be modified in
@@ -325,6 +323,46 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
325323
return cmd;
326324
}
327325

326+
static bool stibp_needed(void)
327+
{
328+
if (spectre_v2_enabled == SPECTRE_V2_NONE)
329+
return false;
330+
331+
if (!boot_cpu_has(X86_FEATURE_STIBP))
332+
return false;
333+
334+
return true;
335+
}
336+
337+
static void update_stibp_msr(void *info)
338+
{
339+
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
340+
}
341+
342+
void arch_smt_update(void)
343+
{
344+
u64 mask;
345+
346+
if (!stibp_needed())
347+
return;
348+
349+
mutex_lock(&spec_ctrl_mutex);
350+
mask = x86_spec_ctrl_base;
351+
if (cpu_smt_control == CPU_SMT_ENABLED)
352+
mask |= SPEC_CTRL_STIBP;
353+
else
354+
mask &= ~SPEC_CTRL_STIBP;
355+
356+
if (mask != x86_spec_ctrl_base) {
357+
pr_info("Spectre v2 cross-process SMT mitigation: %s STIBP\n",
358+
cpu_smt_control == CPU_SMT_ENABLED ?
359+
"Enabling" : "Disabling");
360+
x86_spec_ctrl_base = mask;
361+
on_each_cpu(update_stibp_msr, NULL, 1);
362+
}
363+
mutex_unlock(&spec_ctrl_mutex);
364+
}
365+
328366
static void __init spectre_v2_select_mitigation(void)
329367
{
330368
enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline();
@@ -424,6 +462,9 @@ static void __init spectre_v2_select_mitigation(void)
424462
setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
425463
pr_info("Enabling Restricted Speculation for firmware calls\n");
426464
}
465+
466+
/* Enable STIBP if appropriate */
467+
arch_smt_update();
427468
}
428469

429470
#undef pr_fmt
@@ -814,6 +855,8 @@ static ssize_t l1tf_show_state(char *buf)
814855
static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
815856
char *buf, unsigned int bug)
816857
{
858+
int ret;
859+
817860
if (!boot_cpu_has_bug(bug))
818861
return sprintf(buf, "Not affected\n");
819862

@@ -831,10 +874,12 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr
831874
return sprintf(buf, "Mitigation: __user pointer sanitization\n");
832875

833876
case X86_BUG_SPECTRE_V2:
834-
return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
877+
ret = sprintf(buf, "%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled],
835878
boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
836879
boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
880+
(x86_spec_ctrl_base & SPEC_CTRL_STIBP) ? ", STIBP" : "",
837881
spectre_v2_module_string());
882+
return ret;
838883

839884
case X86_BUG_SPEC_STORE_BYPASS:
840885
return sprintf(buf, "%s\n", ssb_strings[ssb_mode]);

kernel/cpu.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,12 @@ static void cpuhp_online_cpu_device(unsigned int cpu)
20252025
kobject_uevent(&dev->kobj, KOBJ_ONLINE);
20262026
}
20272027

2028+
/*
2029+
* Architectures that need SMT-specific errata handling during SMT hotplug
2030+
* should override this.
2031+
*/
2032+
void __weak arch_smt_update(void) { };
2033+
20282034
static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
20292035
{
20302036
int cpu, ret = 0;
@@ -2051,8 +2057,10 @@ static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
20512057
*/
20522058
cpuhp_offline_cpu_device(cpu);
20532059
}
2054-
if (!ret)
2060+
if (!ret) {
20552061
cpu_smt_control = ctrlval;
2062+
arch_smt_update();
2063+
}
20562064
cpu_maps_update_done();
20572065
return ret;
20582066
}
@@ -2063,6 +2071,7 @@ static int cpuhp_smt_enable(void)
20632071

20642072
cpu_maps_update_begin();
20652073
cpu_smt_control = CPU_SMT_ENABLED;
2074+
arch_smt_update();
20662075
for_each_present_cpu(cpu) {
20672076
/* Skip online CPUs and CPUs on offline nodes */
20682077
if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))

0 commit comments

Comments
 (0)