This is a note to let you know that I've just added the patch titled x86/speculation: Enable cross-hyperthread spectre v2 STIBP mitigation to the 4.14-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: x86speculation_Enable_cross-hyperthread_spectre_v2_STIBP_mitigation.patch and it can be found in the queue-4.14 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. Subject: x86/speculation: Enable cross-hyperthread spectre v2 STIBP mitigation From: Jiri Kosina jkosina@xxxxxxx Date: Tue Sep 25 14:38:55 2018 +0200 From: Jiri Kosina jkosina@xxxxxxx commit 53c613fe6349994f023245519265999eed75957f upstream 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@xxxxxxx> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx> Cc: Andrea Arcangeli <aarcange@xxxxxxxxxx> Cc: "WoodhouseDavid" <dwmw@xxxxxxxxxxxx> Cc: Andi Kleen <ak@xxxxxxxxxxxxxxx> Cc: Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx> Cc: "SchauflerCasey" <casey.schaufler@xxxxxxxxx> Cc: stable@xxxxxxxxxxxxxxx Link: https://lkml.kernel.org/r/nycvar.YFH.7.76.1809251438240.15880@xxxxxxxxxxxxx Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- arch/x86/kernel/cpu/bugs.c | 57 ++++++++++++++++++++++++++++++++++++++++----- kernel/cpu.c | 11 +++++++- 2 files changed, 61 insertions(+), 7 deletions(-) --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -34,12 +34,10 @@ static void __init spectre_v2_select_mit 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 @@ -324,6 +322,46 @@ static enum spectre_v2_mitigation_cmd __ 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) +{ + u64 mask; + + if (!stibp_needed()) + return; + + 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(); @@ -423,6 +461,9 @@ specv2_set_mode: 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 @@ -813,6 +854,8 @@ static ssize_t l1tf_show_state(char *buf static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr, char *buf, unsigned int bug) { + int ret; + if (!boot_cpu_has_bug(bug)) return sprintf(buf, "Not affected\n"); @@ -827,10 +870,12 @@ static ssize_t cpu_show_common(struct de return sprintf(buf, "Mitigation: __user pointer sanitization\n"); case X86_BUG_SPECTRE_V2: - return sprintf(buf, "%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], + ret = sprintf(buf, "%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "", boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "", + (x86_spec_ctrl_base & SPEC_CTRL_STIBP) ? ", STIBP" : "", spectre_v2_module_string()); + return ret; case X86_BUG_SPEC_STORE_BYPASS: return sprintf(buf, "%s\n", ssb_strings[ssb_mode]); --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -2045,6 +2045,12 @@ static void cpuhp_online_cpu_device(unsi kobject_uevent(&dev->kobj, KOBJ_ONLINE); } +/* + * Architectures that need SMT-specific errata handling during SMT hotplug + * should override this. + */ +void __weak arch_smt_update(void) { }; + static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) { int cpu, ret = 0; @@ -2071,8 +2077,10 @@ static int cpuhp_smt_disable(enum cpuhp_ */ cpuhp_offline_cpu_device(cpu); } - if (!ret) + if (!ret) { cpu_smt_control = ctrlval; + arch_smt_update(); + } cpu_maps_update_done(); return ret; } @@ -2083,6 +2091,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))) Patches currently in stable-queue which might be from jkosina@xxxxxxx are queue-4.14/x86speculation_Prepare_for_conditional_IBPB_in_switch_mm.patch queue-4.14/x86speculation_Rework_SMT_state_change.patch queue-4.14/x86Kconfig_Select_SCHED_SMT_if_SMP_enabled.patch queue-4.14/x86speculation_Move_STIPBIBPB_string_conditionals_out_of_cpu_show_common().patch queue-4.14/x86speculation_Propagate_information_about_RSB_filling_mitigation_to_sysfs.patch queue-4.14/x86speculation_Prepare_arch_smt_update_for_PRCTL_mode.patch queue-4.14/x86speculation_Enable_cross-hyperthread_spectre_v2_STIBP_mitigation.patch queue-4.14/x86speculation_Prevent_stale_SPEC_CTRL_msr_content.patch queue-4.14/ptrace_Remove_unused_ptrace_may_access_sched_and_MODE_IBRS.patch queue-4.14/x86speculation_Mark_string_arrays_const_correctly.patch queue-4.14/x86process_Consolidate_and_simplify_switch_to_xtra_code.patch queue-4.14/x86speculation_Apply_IBPB_more_strictly_to_avoid_cross-process_data_leak.patch queue-4.14/x86speculation_Enable_prctl_mode_for_spectre_v2_user.patch queue-4.14/x86speculation_Rename_SSBD_update_functions.patch queue-4.14/x86speculation_Add_command_line_control_for_indirect_branch_speculation.patch queue-4.14/x86l1tf_Show_actual_SMT_state.patch queue-4.14/x86speculation_Split_out_TIF_update.patch queue-4.14/schedsmt_Make_sched_smt_present_track_topology.patch queue-4.14/schedsmt_Expose_sched_smt_present_static_key.patch queue-4.14/x86speculation_Reorder_the_spec_v2_code.patch queue-4.14/x86speculation_Add_prctl()_control_for_indirect_branch_speculation.patch queue-4.14/x86speculation_Provide_IBPB_always_command_line_options.patch queue-4.14/x86speculation_Prepare_for_per_task_indirect_branch_speculation_control.patch queue-4.14/x86speculataion_Mark_command_line_parser_data___initdata.patch queue-4.14/x86speculation_Disable_STIBP_when_enhanced_IBRS_is_in_use.patch queue-4.14/x86speculation_Reorganize_speculation_control_MSRs_update.patch queue-4.14/x86speculation_Clean_up_spectre_v2_parse_cmdline().patch queue-4.14/x86speculation_Update_the_TIF_SSBD_comment.patch queue-4.14/x86speculation_Add_seccomp_Spectre_v2_user_space_protection_mode.patch queue-4.14/x86speculation_Unify_conditional_spectre_v2_print_functions.patch queue-4.14/x86speculation_Avoid___switch_to_xtra_calls.patch queue-4.14/x86speculation_Remove_unnecessary_ret_variable_in_cpu_show_common().patch