您可能听说过影响了许多现代处理器,包括英特尔,AMD,System Z,Power和ARM的某些处理器。幸运的是,内核开发人员已经发布了安全补丁来解决Meltdown/Spectre问题,在最新的内核版本中,默认情况下应用补丁。尽管安全补丁有助于缓解漏洞,但是它们也会影响系统的性能,如果您的系统受到了很好的保护,并且希望获得所有的性能,那么本指南非常适合您。本简要指南介绍了如何通过关闭Spectre和Meltdown缓解措施使Linux系统在Intel CPU上更快地运行。
警告 :
在实施以下解决方案之前,我必须警告你 - 这是高度不安全的,不建议这样做。这将禁用Intel CPU上的所有Spectre和Meltdown缓解措施,并使Linux系统对风险敞开大门。除非清楚地知道你在做什么,不要这样做。
如果您根本不关心安全性,请继续按照以下说明禁用缓解措施。
使Linux系统在Intel CPU上运行更快
使用你喜欢的文本编辑器编辑GRUB文件。
在Debian上,Ubuntu:
$ sudo nano /etc/default/grub
如果你使用的是Linux内核版本5.1.13及更新版本,请添加/编辑以下内核参数,如下所示:
GRUB_CMDLINE_LINUX="mitigations=off"
这将禁用所有可选的CPU缓解措施。
如果你使用的内核版本早于5.1.13,请添加/编辑以下内容:
GRUB_CMDLINE_LINUX="noibrs noibpb nopti nospectre_v2 nospectre_v1 l1tf=off nospec_store_bypass_disable no_stf_barrier mds=off tsx=on tsx_async_abort=off mitigations=off"
这些是内核参数,可用于禁用所有降低Linux系统速度的Spectre/Meltdown缓解措施。
有关每个标志的更多详细信息,请快速搜索google。
添加内核parameter之后,使用命令更新GRUB配置:
$ sudo update-grub
最后,重新启动系统:
$ sudo reboot
在CentOS和RHEL这样的RPM-based系统上,编辑/etc/sysconfig/grub文件:
$ sudo /etc/sysconfig/grub
在GRUB_CMDLINE_LINUX中添加上面的参数,然后使用命令更新GRUB配置:
$ sudo grub2-mkconfig
最后重新启动:
$ sudo reboot
在一些Linux系统中,需要在"GRUB_CMDLINE_LINUX_DEFAULT ="中添加这些内核参数。
我们现在已禁用所有"Spectre"和"Meltdown"缓解措施。这会稍微提高系统的性能,但也可能使用户面临多个CPU漏洞。
检查Spectre/Meltdown mitigations是否被禁用
我们可以使用"spectre-meltdown-checker"工具来帮助你识别Linux中的specre和missdown漏洞,在一些Linux发行版的官方存储库中可以找到它。
在Debian上,Ubuntu:
$ sudo apt install spectre-meltdown-checker
在CentOS上,RHEL:
$ sudo yum install epel-release
$ sudo yum install spectre-meltdown-checker
在Fedora上:
$ sudo dnf install $ sudo apt install spectre-meltdown-checker
安装spectre-meltdown-checker后,以root用户身份或以sudo权限运行它,以检查是否关闭了Spectre和Meltdown:
你应该看到如下所示的消息。
[...]
> STATUS: VULNERABLE (Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers)
[...]
> STATUS: VULNERABLE (IBRS+IBPB or retpoline+IBPB is needed to mitigate the vulnerability)
[...]
> STATUS: VULNERABLE (PTI is needed to mitigate the vulnerability)
或者,你可以检查Spectre/Meltdown漏洞,如下所示。
$ ls /sys/devices/system/cpu/vulnerabilities/
示例输出:
itlb_multihit l1tf mds meltdown spec_store_bypass spectre_v1 spectre_v2 tsx_async_abort
还有
$ grep . /sys/devices/system/cpu/vulnerabilities/*
示例输出:
/sys/devices/system/cpu/vulnerabilities/itlb_multihit:KVM: Vulnerable
/sys/devices/system/cpu/vulnerabilities/l1tf:Mitigation: PTE Inversion
/sys/devices/system/cpu/vulnerabilities/mds:Vulnerable; SMT Host state unknown
/sys/devices/system/cpu/vulnerabilities/meltdown:Vulnerable
/sys/devices/system/cpu/vulnerabilities/spec_store_bypass:Vulnerable
/sys/devices/system/cpu/vulnerabilities/spectre_v1:Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
/sys/devices/system/cpu/vulnerabilities/spectre_v2:Vulnerable, STIBP: disabled
/sys/devices/system/cpu/vulnerabilities/tsx_async_abort:Not affected
运行一些基准测试,并检查你将获得的性能,然后决定是否有必要禁用所有功能。
我已经警告过:对于家庭或单用户计算机,此技巧是一个有用且明智的选择。但不建议用于生产系统。
参考:
在Intel CPU上,如何使Linux系统运行得更快
导致Linux 4.20性能下降的STIBP已被Kernel 4.19系列内核移除