]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
cpu/hotplug: Provide knobs to control SMT
authorThomas Gleixner <tglx@linutronix.de>
Tue, 29 May 2018 15:48:27 +0000 (17:48 +0200)
committerKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Fri, 10 Aug 2018 22:56:35 +0000 (18:56 -0400)
Provide a command line and a sysfs knob to control SMT.

The command line options are:

 'nosmt': Enumerate secondary threads, but do not online them

 'nosmt=force': Ignore secondary threads completely during enumeration
  via MP table and ACPI/MADT.

The sysfs control file has the following states (read/write):

 'on':  SMT is enabled. Secondary threads can be freely onlined
 'off':  SMT is disabled. Secondary threads, even if enumerated
   cannot be onlined
 'forceoff':  SMT is permanentely disabled. Writes to the control
   file are rejected.
 'notsupported': SMT is not supported by the CPU

The command line option 'nosmt' sets the sysfs control to 'off'. This
can be changed to 'on' to reenable SMT during runtime.

The command line option 'nosmt=force' sets the sysfs control to
'forceoff'. This cannot be changed during runtime.

When SMT is 'on' and the control file is changed to 'off' then all online
secondary threads are offlined and attempts to online a secondary thread
later on are rejected.

When SMT is 'off' and the control file is changed to 'on' then secondary
threads can be onlined again. The 'off' -> 'on' transition does not
automatically online the secondary threads.

When the control file is set to 'forceoff', the behaviour is the same as
setting it to 'off', but the operation is irreversible and later writes to
the control file are rejected.

When the control status is 'notsupported' then writes to the control file
are rejected.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Orabug: 28220674
CVE: CVE-2018-3620

(cherry picked from commit 05736e4ac13c08a4a9b1ef2de26dd31a32cbee57)

Signed-off-by: Mihai Carabas <mihai.carabas@oracle.com>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Conflicts:
Documentation/admin-guide/kernel-parameters.txt
arch/Kconfig
arch/x86/Kconfig
kernel/cpu.c

All contextual, but in kernel-cpu.c it was a little more complicated as we did
not have cpuhp_sysfs_init and I created a device_initcall for
cpu_smt_state_init.

Documentation/ABI/testing/sysfs-devices-system-cpu
Documentation/kernel-parameters.txt
arch/Kconfig
arch/x86/Kconfig
include/linux/cpu.h
kernel/cpu.c

index 50f95689ab387fe4409e295916f423d243ca68ed..cddd01d9eaa6d8ef914396c1f3e875d4f81c3e42 100644 (file)
@@ -288,3 +288,23 @@ Description:       Information about CPU vulnerabilities
                "Not affected"    CPU is not affected by the vulnerability
                "Vulnerable"      CPU is affected and no mitigation in effect
                "Mitigation: $M"  CPU is affected and mitigation $M is in effect
+
+What:          /sys/devices/system/cpu/smt
+               /sys/devices/system/cpu/smt/active
+               /sys/devices/system/cpu/smt/control
+Date:          June 2018
+Contact:       Linux kernel mailing list <linux-kernel@vger.kernel.org>
+Description:   Control Symetric Multi Threading (SMT)
+
+               active:  Tells whether SMT is active (enabled and siblings online)
+
+               control: Read/write interface to control SMT. Possible
+                        values:
+
+                        "on"           SMT is enabled
+                        "off"          SMT is disabled
+                        "forceoff"     SMT is force disabled. Cannot be changed.
+                        "notsupported" SMT is not supported by the CPU
+
+                        If control status is "forceoff" or "notsupported" writes
+                        are rejected.
index 1095842a766ae7d831d3ce7996c6a888162262b4..df288fcc105d874a98fb62fa1de366c1fdee2026 100644 (file)
@@ -2387,6 +2387,15 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
        nohugeiomap     [KNL,x86] Disable kernel huge I/O mappings.
 
+       nosmt           [KNL,x86] Disable symmetric multithreading (SMT).
+                       nosmt=force: Force disable SMT, similar to disabling
+                                    it in the BIOS except that some of the
+                                    resource partitioning effects which are
+                                    caused by having SMT enabled in the BIOS
+                                    cannot be undone. Depending on the CPU
+                                    type this might have a performance impact.
+
+
        nospectre_v2    [X86] Disable all mitigations for the Spectre variant 2
                        (indirect branch prediction) vulnerability. System may
                        allow data leaks with this option, which is equivalent
index de7ac6f3f698b0718bc9d21eb47a49b8d9d4fae2..2497ca8fa01d4967a43aab19b48ae831536cc6e2 100644 (file)
@@ -2,6 +2,9 @@
 # General architecture dependent options
 #
 
+config HOTPLUG_SMT
+       bool
+
 config OPROFILE
        tristate "OProfile system profiling"
        depends on PROFILING
index a80630fa381fe75a8c179bc76d92a3116ff983a4..e73c64fec23194298b08523c6b453380250b5011 100644 (file)
@@ -96,6 +96,7 @@ config X86
        select HAVE_ARCH_KMEMCHECK
        select HAVE_ARCH_KASAN if X86_64 && SPARSEMEM_VMEMMAP
        select HAVE_USER_RETURN_NOTIFIER
+       select HOTPLUG_SMT                      if SMP
        select ARCH_HAS_ELF_RANDOMIZE
        select HAVE_ARCH_JUMP_LABEL
        select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
index 39761736c177b068e1cfa42ae13018f832a8313d..b7c865c997adc1acc17e039b7e68925f56a07b7d 100644 (file)
@@ -307,4 +307,17 @@ bool cpu_wait_death(unsigned int cpu, int seconds);
 bool cpu_report_death(void);
 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
 
+enum cpuhp_smt_control {
+       CPU_SMT_ENABLED,
+       CPU_SMT_DISABLED,
+       CPU_SMT_FORCE_DISABLED,
+       CPU_SMT_NOT_SUPPORTED,
+};
+
+#if defined(CONFIG_SMP) && defined(CONFIG_HOTPLUG_SMT)
+extern enum cpuhp_smt_control cpu_smt_control;
+#else
+# define cpu_smt_control               (CPU_SMT_ENABLED)
+#endif
+
 #endif /* _LINUX_CPU_H_ */
index d3142aabd92d2f7a31697d6c6b6685da0df6265b..217e6501c0f6f1ae004089dca79b40df915e9050 100644 (file)
@@ -494,6 +494,29 @@ void __cpuinit smpboot_thread_init(void)
        register_cpu_notifier(&smpboot_thread_notifier);
 }
 
+#ifdef CONFIG_HOTPLUG_SMT
+enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
+
+static int __init smt_cmdline_disable(char *str)
+{
+       cpu_smt_control = CPU_SMT_DISABLED;
+       if (str && !strcmp(str, "force")) {
+               pr_info("SMT: Force disabled\n");
+               cpu_smt_control = CPU_SMT_FORCE_DISABLED;
+       }
+       return 0;
+}
+early_param("nosmt", smt_cmdline_disable);
+
+static inline bool cpu_smt_allowed(unsigned int cpu)
+{
+       return cpu_smt_control == CPU_SMT_ENABLED ||
+               topology_is_primary_thread(cpu);
+}
+#else
+static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
+#endif
+
 /* Requires cpu_add_remove_lock to be held */
 static int _cpu_up(unsigned int cpu, int tasks_frozen)
 {
@@ -570,6 +593,11 @@ int cpu_up(unsigned int cpu)
                goto out;
        }
 
+       if (!cpu_smt_allowed(cpu)) {
+               err = -EPERM;
+               goto out;
+       }
+
        err = _cpu_up(cpu, 0);
 
 out:
@@ -735,6 +763,143 @@ void notify_cpu_starting(unsigned int cpu)
        cpu_notify(val, (void *)(long)cpu);
 }
 
+#ifdef CONFIG_HOTPLUG_SMT
+
+static const char *smt_states[] = {
+       [CPU_SMT_ENABLED]               = "on",
+       [CPU_SMT_DISABLED]              = "off",
+       [CPU_SMT_FORCE_DISABLED]        = "forceoff",
+       [CPU_SMT_NOT_SUPPORTED]         = "notsupported",
+};
+
+static ssize_t
+show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
+}
+
+static void cpuhp_offline_cpu_device(unsigned int cpu)
+{
+       struct device *dev = get_cpu_device(cpu);
+
+       dev->offline = true;
+       /* Tell user space about the state change */
+       kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
+}
+
+static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
+{
+       int cpu, ret = 0;
+
+       cpu_maps_update_begin();
+       for_each_online_cpu(cpu) {
+               if (topology_is_primary_thread(cpu))
+                       continue;
+               ret = cpu_down_maps_locked(cpu);
+               if (ret)
+                       break;
+               /*
+                * As this needs to hold the cpu maps lock it's impossible
+                * to call device_offline() because that ends up calling
+                * cpu_down() which takes cpu maps lock. cpu maps lock
+                * needs to be held as this might race against in kernel
+                * abusers of the hotplug machinery (thermal management).
+                *
+                * So nothing would update device:offline state. That would
+                * leave the sysfs entry stale and prevent onlining after
+                * smt control has been changed to 'off' again. This is
+                * called under the sysfs hotplug lock, so it is properly
+                * serialized against the regular offline usage.
+                */
+               cpuhp_offline_cpu_device(cpu);
+       }
+       if (!ret)
+               cpu_smt_control = ctrlval;
+       cpu_maps_update_done();
+       return ret;
+}
+
+static void cpuhp_smt_enable(void)
+{
+       cpu_maps_update_begin();
+       cpu_smt_control = CPU_SMT_ENABLED;
+       cpu_maps_update_done();
+}
+
+static ssize_t
+store_smt_control(struct device *dev, struct device_attribute *attr,
+                 const char *buf, size_t count)
+{
+       int ctrlval, ret;
+
+       if (sysfs_streq(buf, "on"))
+               ctrlval = CPU_SMT_ENABLED;
+       else if (sysfs_streq(buf, "off"))
+               ctrlval = CPU_SMT_DISABLED;
+       else if (sysfs_streq(buf, "forceoff"))
+               ctrlval = CPU_SMT_FORCE_DISABLED;
+       else
+               return -EINVAL;
+
+       if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
+               return -EPERM;
+
+       if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
+               return -ENODEV;
+
+       ret = lock_device_hotplug_sysfs();
+       if (ret)
+               return ret;
+
+       if (ctrlval != cpu_smt_control) {
+               switch (ctrlval) {
+               case CPU_SMT_ENABLED:
+                       cpuhp_smt_enable();
+                       break;
+               case CPU_SMT_DISABLED:
+               case CPU_SMT_FORCE_DISABLED:
+                       ret = cpuhp_smt_disable(ctrlval);
+                       break;
+               }
+       }
+
+       unlock_device_hotplug();
+       return ret ? ret : count;
+}
+static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
+
+static ssize_t
+show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       bool active = topology_max_smt_threads() > 1;
+
+       return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
+}
+static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
+
+static struct attribute *cpuhp_smt_attrs[] = {
+       &dev_attr_control.attr,
+       &dev_attr_active.attr,
+       NULL
+};
+
+static const struct attribute_group cpuhp_smt_attr_group = {
+       .attrs = cpuhp_smt_attrs,
+       .name = "smt",
+       NULL
+};
+
+static int __init cpu_smt_state_init(void)
+{
+       if (!topology_smt_supported())
+               cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
+
+       return sysfs_create_group(&cpu_subsys.dev_root->kobj,
+                                 &cpuhp_smt_attr_group);
+}
+device_initcall(cpu_smt_state_init);
+#endif
+
 #endif /* CONFIG_SMP */
 
 /*