cpu: Fix default mitigation behavior
Subsystems (4)
cover cpu: Fix default mitigation behavior
This is effectively v2 of a previous series[*] that was intended to be x86-only, but accidentally disabled CPU mitigations by default for every other architectures. Unfortunately, the buggy code has already made it's way to Linus' tree.
Patch 1 fixes that goof by adding a generic Kconfig to control the default behavior.
Patch 2 disallows retroactively enabling mitigations via command line if the kernel was built with CPU_MITIGATIONS=n, i.e. with SPECULATION_MITIGATIONS=n on x86, as it's infeasible for the kernel to provide sane, predictable behavior for this scenario.
[*] https://lore.kernel.org/all/[email protected]
Sean Christopherson (2):
cpu: Re-enable CPU mitigations by default for !X86 architectures
cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n
Documentation/admin-guide/kernel-parameters.txt | 3 +++ arch/x86/Kconfig | 11 ++++++++--- drivers/base/Kconfig | 3 +++ kernel/cpu.c | 6 ++++-- 4 files changed, 18 insertions(+), 5 deletions(-)
base-commit: 96fca68c4fbf77a8185eb10f7557e23352732ea2
Signature · 1 line
2.44.0.683.g7961c838ac-goog
PATCH 1/2 cpu: Re-enable CPU mitigations by default for !X86 architectures
Add a generic Kconfig, CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where as SPECULATION_MITIGATIONS is x86 specific.
Alternatively, SPECULATION_MITIGATIONS could simply be defined in common code, but that creates weirdness for x86 because SPECULATION_MITIGATIONS ends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).
Ideally, CPU_MITIGATIONS would be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect a Kconfig.
--- arch/x86/Kconfig | 1 + drivers/base/Kconfig | 3 +++ kernel/cpu.c | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -0
@@ -2490,6 +2490,7 @@ config PREFIX_SYMBOLS menuconfig SPECULATION_MITIGATIONS bool "Mitigations for speculative execution vulnerabilities" + select CPU_MITIGATIONS default y help Say Y here to enable options which enable mitigations for
drivers/base/Kconfig +3 -0
@@ -191,6 +191,9 @@ config GENERIC_CPU_AUTOPROBE config GENERIC_CPU_VULNERABILITIES bool +config CPU_MITIGATIONS + def_bool !X86 + config SOC_BUS bool select GLOB
kernel/cpu.c +2 -2
@@ -3207,8 +3207,8 @@ enum cpu_mitigations { };static enum cpu_mitigations cpu_mitigations__ro_after_init = -IS_ENABLED (CONFIG_SPECULATION_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : -CPU_MITIGATIONS_OFF ; +IS_ENABLED (CONFIG_CPU_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : +CPU_MITIGATIONS_OFF ;static int __init mitigations_parse_cmdline (char *arg) {
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
Quoted 15 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.
Quoted 8 lines
Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>---
Thanks for fixing it up.
cheers
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
In reply to Sean ChristophersonQuoted 15 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.
Quoted 7 lines
Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
In reply to Sean ChristophersonQuoted 71 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/Kconfig | 1 + drivers/base/Kconfig | 3 +++ kernel/cpu.c | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-)arch/x86/Kconfig +1
-0@@ -2490,6 +2490,7 @@ config PREFIX_SYMBOLS menuconfig SPECULATION_MITIGATIONS bool "Mitigations for speculative execution vulnerabilities" + select CPU_MITIGATIONS default y help Say Y here to enable options which enable mitigations fordrivers/base/Kconfig +3
-0@@ -191,6 +191,9 @@ config GENERIC_CPU_AUTOPROBE config GENERIC_CPU_VULNERABILITIES bool +config CPU_MITIGATIONS + def_bool !X86 + config SOC_BUS bool select GLOBkernel/cpu.c +2
-2@@ -3207,8 +3207,8 @@ enum cpu_mitigations { };static enum cpu_mitigations cpu_mitigations__ro_after_init = -IS_ENABLED (CONFIG_SPECULATION_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : -CPU_MITIGATIONS_OFF ; +IS_ENABLED (CONFIG_CPU_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : +CPU_MITIGATIONS_OFF ;static int __init mitigations_parse_cmdline (char *arg) {Signature · 1 line
2.44.0.683.g7961c838ac-goog
Thanks, Sean!
Will
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
In reply to Sean ChristophersonQuoted 15 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.
Quoted 7 lines
Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>
It seems confusing to have two config options which have very similar names and similar purposes (with subtle differences depending on the arch).
How about we instead just get rid of the x86-specific SPECULATION_MITIGATIONS and replace it with a menu which depends on CPU_MITIGATIONS:
arch/x86/Kconfig +3 -12
@@ -2488,17 +2488,8 @@ config PREFIX_SYMBOLS def_bool y depends on CALL_PADDING && !CFI_CLANG -menuconfig SPECULATION_MITIGATIONS - bool "Mitigations for speculative execution vulnerabilities" - default y - help - Say Y here to enable options which enable mitigations for - speculative execution hardware vulnerabilities. - - If you say N, all mitigations will be disabled. You really - should know what you are doing to say so. - -if SPECULATION_MITIGATIONS +menu "CPU speculative execution mitigation defaults" + depends on CPU_MITIGATIONS config MITIGATION_PAGE_TABLE_ISOLATION bool "Remove the kernel mapping in user mode"
@@ -2643,7 +2634,7 @@ config MITIGATION_SPECTRE_BHI indirect branches. See <file:Documentation/admin-guide/hw-vuln/spectre.rst> -endif +endmenu config ARCH_HAS_ADD_PAGES def_bool y
drivers/base/Kconfig +10 -0
@@ -191,6 +191,16 @@ config GENERIC_CPU_AUTOPROBE config GENERIC_CPU_VULNERABILITIES bool +config CPU_MITIGATIONS + bool "Mitigations for CPU speculative execution vulnerabilities" + default y + help + Say Y here to enable mitigations for CPU speculative execution + vulnerabilities. + + If you say N, all mitigations will be disabled. You really + should know what you are doing to say so. + config SOC_BUS bool select GLOB
kernel/cpu.c +2 -2
@@ -3207,8 +3207,8 @@ enum cpu_mitigations { };static enum cpu_mitigations cpu_mitigations__ro_after_init = -IS_ENABLED (CONFIG_SPECULATION_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : -CPU_MITIGATIONS_OFF ; +IS_ENABLED (CONFIG_CPU_MITIGATIONS ) ?CPU_MITIGATIONS_AUTO : +CPU_MITIGATIONS_OFF ;static int __init mitigations_parse_cmdline (char *arg) {
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
Quoted 24 lines · Josh Poimboeuf
Quoted 15 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.Quoted 7 lines
Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>
Quoted 7 lines
It seems confusing to have two config options which have very similar names and similar purposes (with subtle differences depending on the arch).
How about we instead just get rid of the x86-specific
SPECULATION_MITIGATIONSand replace it with a menu which depends onCPU_MITIGATIONS:
Huh, didn't realize that was possible.
I agree that having two things for the same thing is confusing, though Boris' idea to do s/SPECULATION_MITIGATIONS/X86_CPU_MITIGATIONS would help a fair bit on that front.
My only hesitation is that x86's menu and the common config knob end up in completely different locations. And AFAICT, the parser doesn't allow sourcing menu entires from a different file:
init/Kconfig:1959: 'menu' in different file than 'menu'
e.g. we can't declare the menuconfig in common code and then include arch definitions.
Regardless of whether or not we shuffle things around, CPU_MITIGATIONS really should be in init/Kconfig, not drivers/base/Kconfig, e.g. so that if we make it a user-selectable option, it shows up under "General setup" instead of being buried two layers deep in drivers.
That makes it less hard to find CPU_MITIGATIONS, but I still find it cumbersome to have to enable CPU_MITIGATIONS, and then go hunting for x86's menu.
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
Quoted 8 lines · Sean Christopherson
Quoted 7 lines
It seems confusing to have two config options which have very similar names and similar purposes (with subtle differences depending on the arch).
How about we instead just get rid of the x86-specific
SPECULATION_MITIGATIONSand replace it with a menu which depends onCPU_MITIGATIONS:
Quoted 8 lines
Huh, didn't realize that was possible.
I agree that having two things for the same thing is confusing, though Boris' idea to do s/SPECULATION_MITIGATIONS/X86_CPU_MITIGATIONS would help a fair bit on that front.
My only hesitation is that x86's menu and the common config knob end up in completely different locations.
I'm thinking this is a minor issue because CPU_MITIGATIONS is enabled by default, so it should almost always be enabled unless the user disables it, in which case they wouldn't be looking for the x86-specific mitigations anyway.
Regardless it seems very common for a menu "depends on" to be in a different file. We could put CPU_MITIGATIONS in arch/Kconfig which is a fairly logical place for the dependency.
Signature · 1 line
Josh
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
Quoted 15 lines · Josh Poimboeuf
Quoted 14 lines · Sean Christopherson
Quoted 7 lines
It seems confusing to have two config options which have very similar names and similar purposes (with subtle differences depending on the arch).
How about we instead just get rid of the x86-specific
SPECULATION_MITIGATIONSand replace it with a menu which depends onCPU_MITIGATIONS:Huh, didn't realize that was possible.
I agree that having two things for the same thing is confusing, though Boris' idea to do s/SPECULATION_MITIGATIONS/X86_CPU_MITIGATIONS would help a fair bit on that front.
Quoted 7 lines
Quoted 2 lines
My only hesitation is that x86's menu and the common config knob end up in completely different locations.
I'm thinking this is a minor issue because
CPU_MITIGATIONSis enabled by default, so it should almost always be enabled unless the user disables it, in which case they wouldn't be looking for the x86-specific mitigations anyway.
Yeah, this isn't a sticking point by any means.
Oh, and another hiccup I almost forgot about (I just recalled Geert's report). Letting CPU_MITIGATIONS be disabled for every arch at compile time will obsolete a small amount of kernel code, e.g. arm64 explicitly says "disabled by command line option" in a few places.
Those are easy enough to fixup though, but it's not clear that other architectures *want* to allow mitigations to be completely compiled out. x86 appears to be relatively unique in that it has a bajillion different things being mitigated.
Rather than making CPU_MITIGATIONS configured for all architectures, what if use another Kconfig to tell common code that arch code has already defined CPU_MITIGATIONS? The big downside is that if another arch does end up letting the user disable CPU_MITIGATIONS, then we'll probably end up duplicating the help text. But again, it's not clear that any other arch wants to allow that, i.e. we can cross that bridge if we come to it.
config ARCH_CONFIGURES_CPU_MITIGATIONS
bool
if !ARCH_CONFIGURES_CPU_MITIGATIONS
config CPU_MITIGATIONS
def_bool y
endif
Quoted 3 lines
Regardless it seems very common for a menu "depends on" to be in a different file. We could put
CPU_MITIGATIONSinarch/Kconfigwhich is a fairly logical place for the dependency.
Yeah, arch/Kconfig is probably better than init/Kconfig.
Given that it's late on Friday, I'll somewhat speculatively (ba-dump ching!) post a v2, and Cc Linus to explain the mess so that he can apply it directly if he thinks it's urgent enough to squeeze into -rc5, and if if my idea isn't completely off the rails.
Re: [PATCH 1/2] cpu: Re-enable CPU mitigations by default for !X86 architectures
In reply to Josh PoimboeufQuoted 24 lines · Josh Poimboeuf
Quoted 15 lines · Sean Christopherson
Add a generic
Kconfig,CPU_MITIGATIONS, to control whether or not CPU mitigations are enabled by default, and force it on for all architectures except x86. A recent commit to turn mitigations off by default if SPECULATION_MITIGATIONS=n kinda sorta missed that "cpu_mitigations" is completely generic, where asSPECULATION_MITIGATIONSis x86 specific.Alternatively,
SPECULATION_MITIGATIONScould simply be defined in common code, but that creates weirdness for x86 becauseSPECULATION_MITIGATIONSends up being defined twice, and the default behavior would likely depend on the arbitrary include order (if the two definitions diverged).Ideally,
CPU_MITIGATIONSwould be unconditionally on by default for all architectures, and manually turned off, but there is no way to unselect aKconfig.Quoted 7 lines
Reported-by Stephen Rothwell <[email protected]>Reported-by Michael Ellerman <[email protected]>Reported-by Geert Uytterhoeven <[email protected]>Fixes f337a6a21e2f ("x86/cpu: Actually turn off mitigations by default for SPECULATION_MITIGATIONS=n")Signed-off-by Sean Christopherson <[email protected]>
Quoted 3 lines
It seems confusing to have two config options which have very similar names and similar purposes (with subtle differences depending on the arch).
I agree.
But can we please get Sean's fix into mainline before rc5.
cheers
PATCH 2/2 cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n
In reply to Sean ChristophersonExplicitly disallow enabling mitigations at runtime for kernels that were built with CONFIG_CPU_MITIGATIONS=n, which currently is possible only on x86 (via x86's SPECULATION_MITIGATIONS menuconfig).
On x86, a large pile of Kconfigs are buried behind SPECULATION_MITIGATIONS, and trying to provide sane behavior for retroactively enabling mitigations is extremely difficult, bordering on impossible. E.g. page table isolation and call depth tracking requrie build-time support, BHI mitigations will still be off without additional kernel parameters, etc.
--- Documentation/admin-guide/kernel-parameters.txt | 3 +++ arch/x86/Kconfig | 10 +++++++--- kernel/cpu.c | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
@@ -3423,6 +3423,9 @@ arch-independent options, each of which is an aggregation of existing arch-specific options. + Note, "mitigations" is supported on x86 if and only if + the kernel was built with SPECULATION_MITIGATIONS=y. + off Disable all optional CPU mitigations. This improves system performance, but it may also
arch/x86/Kconfig +7 -3
@@ -2494,10 +2494,14 @@ menuconfig SPECULATION_MITIGATIONS default y help Say Y here to enable options which enable mitigations for - speculative execution hardware vulnerabilities. + speculative execution hardware vulnerabilities. Mitigations can + be disabled or restricted to SMT systems at runtime via the + "mitigations" kernel parameter. - If you say N, all mitigations will be disabled. You really - should know what you are doing to say so. + If you say N, all mitigations will be disabled. This CANNOT be + overridden at runtime. + + Say 'Y', unless you really know what you are doing. if SPECULATION_MITIGATIONS
kernel/cpu.c +2 -0
@@ -3214,6 +3214,8 @@ static int __init mitigations_parse_cmdline(char *arg) {if (!strcmp (arg,"off" )) cpu_mitigations =CPU_MITIGATIONS_OFF ; +else if (!IS_ENABLED (CONFIG_CPU_MITIGATIONS )) +pr_crit ("Kernel compiled without mitigations, ignoring 'mitigations'; system may still be vulnerable\n" );else if (!strcmp (arg,"auto" )) cpu_mitigations =CPU_MITIGATIONS_AUTO ;else if (!strcmp (arg,"auto,nosmt" ))
Signature · 1 line
2.44.0.683.g7961c838ac-goog
Borislav Petkov
maintainerRe: [PATCH 2/2] cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n
Quoted 3 lines · Sean Christopherson
Explicitly disallow enabling mitigations at runtime for kernels that were built with
CONFIG_CPU_MITIGATIONS=n, which currently is possible only on x86 (via x86'sSPECULATION_MITIGATIONSmenuconfig).
Hm, so the umbrella term is CPU_MITIGATIONS, the x86-one is SPECULATION_MITIGATIONS.
I wanna streamline our namespacing and say, the arch agnostic term should be CPU_MITIGATIONS and the x86 one should be then X86_CPU_MITIGATIONS, the Arm one would be ARM_CPU_MITIGATIONS and so on.
This way we can stick all kinds of special mitigations code - not only speculative execution ones - under those config items and have it all straight from the get-go.
And I think we should do it now, before it all propagates down the tree and becomes a lot harder to rename.
Thoughts?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH 2/2] cpu: Ignore "mitigations" kernel parameter if CPU_MITIGATIONS=n
Quoted 11 lines · Borislav Petkov
Quoted 3 lines · Sean Christopherson
Explicitly disallow enabling mitigations at runtime for kernels that were built with
CONFIG_CPU_MITIGATIONS=n, which currently is possible only on x86 (via x86'sSPECULATION_MITIGATIONSmenuconfig).Hm, so the umbrella term is
CPU_MITIGATIONS, the x86-one isSPECULATION_MITIGATIONS.I wanna streamline our namespacing and say, the arch agnostic term should be
CPU_MITIGATIONSand the x86 one should be thenX86_CPU_MITIGATIONS, the Arm one would beARM_CPU_MITIGATIONSand so on.
+1. That would help avoid goofs like mine. Maybe. :-)
Quoted 16 lines
This way we can stick all kinds of special mitigations code - not only speculative execution ones - under those config items and have it all straight from the get-go.
And I think we should do it now, before it all propagates down the tree and becomes a lot harder to rename.
Thoughts?
Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette