Disabling HyperThreading on Linux

Posted on .

In light of recent CPU vulnerabilities like TLBleed and L1TF you may want to disable HyperThreading on your computer if you have an Intel CPU with that feature. Note this is a personal decision. Most desktop computers run untrusted code in the form of untrusted JavaScript inside your web browser, but some people may consider disabling HyperThreading an excessive measure for a desktop computer. The situation for some servers is very different.

In any case, if you want to do it, your computer’s BIOS is your friend and the easiest solution. However, you may want a more flexible solution. Passing “noht” to the kernel command line used to work in the past but today it doesn’t seem to work properly. Following that link you can read a solution by “Paul M” that works reliably in more recent Linux systems. It’s also explained in some other articles, but I’ll repeat the explanation here.

Linux has a directory called /sys/devices/system/cpu that contains a subdirectory for each virtual CPU in the system, i.e. each thread you can run. Inside each directory, there’s a file called topology/thread_siblings_list that contains the list of CPUs that belong to the same thread group. In other words, the list of CPUs that are part of the same physical core. For example, if CPUs number 0 and 4 execute threads that run on the same core, the thread_siblings_list file for CPUs 0 and 4 contain the string 0,4 (always in that order). To disable hyperthreading, you need to disable the second CPU in each of those groups, which can be done by writing 0 to the online file in each CPU subdirectory.

This solution is flexible in the sense that the change can be reverted dynamically without rebooting by re-enabling the CPUs. I’d personally do it from /etc/rc.d/rc.local (or the equivalent file in your distribution). Note that file is normally executed as one of the last steps of the boot process, so hyperthreading would still be enabled for a few seconds after booting up. Normally, that’s OK in a desktop computer.

You’ll read many small variants of the following code:

# Disable Hyperthreading.
#
# /sys/devices/system/cpu/cpu*/topology/thread_siblings_list contains the list
# of sibling threads for each CPU core, always in the same order. For example,
# if cores 0 and 4 are threads on the same physical core, they both contain the
# string "0,4" in their thread_siblings_list file.
#
# The following script gets the second number of each list and disables that
# core by writing a 0 to its "online" control file.
#
for n in $( cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | cut -d, -f2 | sort -u ); do
    echo 0 >/sys/devices/system/cpu/cpu${n}/online
done
Load comments