Re: [PATCH v5 3/3] x86/bugs: Add 'spectre_bhi=vmexit' cmdline option

From: Josh Poimboeuf
Date: Wed Jun 26 2024 - 01:58:37 EST


On Mon, May 27, 2024 at 01:45:59PM +0300, Maksim Davydov wrote:
> I think it will be useful for us to have appropriate Kconfig option. Could
> you please add it to the next version?

That should probably be a separate patch, something like the below?

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 1d7122a1883e..ab1ea701bc42 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2642,17 +2642,46 @@ config MITIGATION_RFDS
stored in floating point, vector and integer registers.
See also <file:Documentation/admin-guide/hw-vuln/reg-file-data-sampling.rst>

-config MITIGATION_SPECTRE_BHI
- bool "Mitigate Spectre-BHB (Branch History Injection)"
+choice
+ prompt "Mitigate Spectre-BHB (Branch History Injection)"
depends on CPU_SUP_INTEL
- default y
+ default MITIGATION_SPECTRE_BHI_ON
help
Enable BHI mitigations. BHI attacks are a form of Spectre V2 attacks
where the branch history buffer is poisoned to speculatively steer
indirect branches.
+
+ The compile-time default can be set to on, vmexit, or off,
+ corresponding to the "spectre_bhi=" cmdline defaults described in
+ Documentation/admin-guide/kernel-parameters.rst. The cmdline
+ options can be used to override this compile-time default.
+
See <file:Documentation/admin-guide/hw-vuln/spectre.rst>

-endif
+config MITIGATION_SPECTRE_BHI_ON
+ bool "on"
+ help
+ Enable the HW or SW mitigation as needed. This protects the kernel
+ from both syscalls and VMs. Equivalent to the spectre_bhi=on cmdline
+ option.
+
+config MITIGATION_SPECTRE_BHI_VMEXIT
+ bool "vmexit"
+ help
+ On systems which don't have the HW mitigation available, enable the
+ SW mitigation on vmexit ONLY. On such systems, the host kernel is
+ protected from VM-originated BHI attacks, but may still be vulnerable
+ to syscall attacks. Equivalent to the spectre_bhi=vmexit cmdline
+ option.
+
+config MITIGATION_SPECTRE_BHI_OFF
+ bool "off"
+ help
+ Disable the mitigation. Equivalent to the spectre_bhi=off cmdline
+ option.
+endchoice
+
+endif # CPU_MITIGATIONS

config ARCH_HAS_ADD_PAGES
def_bool y
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index 94bcf29df465..d415f24b7169 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -1628,8 +1628,13 @@ enum bhi_mitigations {
BHI_MITIGATION_VMEXIT_ONLY,
};

-static enum bhi_mitigations bhi_mitigation __ro_after_init =
- IS_ENABLED(CONFIG_MITIGATION_SPECTRE_BHI) ? BHI_MITIGATION_ON : BHI_MITIGATION_OFF;
+#ifdef CONFIG_MITIGATION_SPECTRE_BHI_ON
+static enum bhi_mitigations bhi_mitigation __ro_after_init = BHI_MITIGATION_ON;
+#elif CONFIG_MITIGATION_SPECTRE_BHI_VMEXIT
+static enum bhi_mitigations bhi_mitigation __ro_after_init = BHI_MITIGATION_VMEXIT;
+#else
+static enum bhi_mitigations bhi_mitigation __ro_after_init = BHI_MITIGATION_OFF;
+#endif

static int __init spectre_bhi_parse_cmdline(char *str)
{