]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
x86/bugs: Restructure BHI mitigation
authorDavid Kaplan <david.kaplan@amd.com>
Fri, 18 Apr 2025 16:17:17 +0000 (11:17 -0500)
committerBorislav Petkov (AMD) <bp@alien8.de>
Tue, 29 Apr 2025 16:51:29 +0000 (18:51 +0200)
Restructure BHI mitigation to use select/update/apply functions to create
consistent vulnerability handling.  BHI mitigation was previously selected
from within spectre_v2_select_mitigation() and now is selected from
cpu_select_mitigation() like with all others.

Define new AUTO mitigation for BHI.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Josh Poimboeuf <jpoimboe@kernel.org>
Link: https://lore.kernel.org/20250418161721.1855190-13-david.kaplan@amd.com
arch/x86/kernel/cpu/bugs.c

index dc75195760ca533d222f5e48c3836a7c53aba84d..100a320443856311b84d7f5d625f9ff9a4c22888 100644 (file)
@@ -82,6 +82,9 @@ static void __init l1d_flush_select_mitigation(void);
 static void __init srso_select_mitigation(void);
 static void __init gds_select_mitigation(void);
 static void __init gds_apply_mitigation(void);
+static void __init bhi_select_mitigation(void);
+static void __init bhi_update_mitigation(void);
+static void __init bhi_apply_mitigation(void);
 
 /* The base value of the SPEC_CTRL MSR without task-specific bits set */
 u64 x86_spec_ctrl_base;
@@ -208,6 +211,7 @@ void __init cpu_select_mitigations(void)
         */
        srso_select_mitigation();
        gds_select_mitigation();
+       bhi_select_mitigation();
 
        /*
         * After mitigations are selected, some may need to update their
@@ -225,6 +229,7 @@ void __init cpu_select_mitigations(void)
        taa_update_mitigation();
        mmio_update_mitigation();
        rfds_update_mitigation();
+       bhi_update_mitigation();
 
        spectre_v1_apply_mitigation();
        retbleed_apply_mitigation();
@@ -235,6 +240,7 @@ void __init cpu_select_mitigations(void)
        rfds_apply_mitigation();
        srbds_apply_mitigation();
        gds_apply_mitigation();
+       bhi_apply_mitigation();
 }
 
 /*
@@ -1794,12 +1800,13 @@ static bool __init spec_ctrl_bhi_dis(void)
 
 enum bhi_mitigations {
        BHI_MITIGATION_OFF,
+       BHI_MITIGATION_AUTO,
        BHI_MITIGATION_ON,
        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;
+       IS_ENABLED(CONFIG_MITIGATION_SPECTRE_BHI) ? BHI_MITIGATION_AUTO : BHI_MITIGATION_OFF;
 
 static int __init spectre_bhi_parse_cmdline(char *str)
 {
@@ -1820,6 +1827,25 @@ static int __init spectre_bhi_parse_cmdline(char *str)
 early_param("spectre_bhi", spectre_bhi_parse_cmdline);
 
 static void __init bhi_select_mitigation(void)
+{
+       if (!boot_cpu_has(X86_BUG_BHI) || cpu_mitigations_off())
+               bhi_mitigation = BHI_MITIGATION_OFF;
+
+       if (bhi_mitigation == BHI_MITIGATION_AUTO)
+               bhi_mitigation = BHI_MITIGATION_ON;
+}
+
+static void __init bhi_update_mitigation(void)
+{
+       if (spectre_v2_cmd == SPECTRE_V2_CMD_NONE)
+               bhi_mitigation = BHI_MITIGATION_OFF;
+
+       if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) &&
+            spectre_v2_cmd == SPECTRE_V2_CMD_AUTO)
+               bhi_mitigation = BHI_MITIGATION_OFF;
+}
+
+static void __init bhi_apply_mitigation(void)
 {
        if (bhi_mitigation == BHI_MITIGATION_OFF)
                return;
@@ -1961,9 +1987,6 @@ static void __init spectre_v2_select_mitigation(void)
            mode == SPECTRE_V2_RETPOLINE)
                spec_ctrl_disable_kernel_rrsba();
 
-       if (boot_cpu_has(X86_BUG_BHI))
-               bhi_select_mitigation();
-
        spectre_v2_enabled = mode;
        pr_info("%s\n", spectre_v2_strings[mode]);