[PATCH v3 05/35] x86/bugs: Restructure taa mitigation

David Kaplan posted 35 patches 11 months, 2 weeks ago
[PATCH v3 05/35] x86/bugs: Restructure taa mitigation
Posted by David Kaplan 11 months, 2 weeks ago
Restructure taa mitigation to use select/update/apply functions to
create consistent vulnerability handling.

Signed-off-by: David Kaplan <david.kaplan@amd.com>
---
 arch/x86/kernel/cpu/bugs.c | 92 ++++++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 34 deletions(-)

diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index ff2d6f2e01f4..7beb2d6c43bb 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -65,6 +65,8 @@ static void __init mds_apply_mitigation(void);
 static void __init md_clear_update_mitigation(void);
 static void __init md_clear_select_mitigation(void);
 static void __init taa_select_mitigation(void);
+static void __init taa_update_mitigation(void);
+static void __init taa_apply_mitigation(void);
 static void __init mmio_select_mitigation(void);
 static void __init srbds_select_mitigation(void);
 static void __init l1d_flush_select_mitigation(void);
@@ -187,6 +189,7 @@ void __init cpu_select_mitigations(void)
 	ssb_select_mitigation();
 	l1tf_select_mitigation();
 	mds_select_mitigation();
+	taa_select_mitigation();
 	md_clear_select_mitigation();
 	srbds_select_mitigation();
 	l1d_flush_select_mitigation();
@@ -203,8 +206,10 @@ void __init cpu_select_mitigations(void)
 	 * choices.
 	 */
 	mds_update_mitigation();
+	taa_update_mitigation();
 
 	mds_apply_mitigation();
+	taa_apply_mitigation();
 }
 
 /*
@@ -375,9 +380,6 @@ static int __init mds_cmdline(char *str)
 }
 early_param("mds", mds_cmdline);
 
-#undef pr_fmt
-#define pr_fmt(fmt)	"TAA: " fmt
-
 static bool taa_nosmt __ro_after_init;
 
 static const char * const taa_strings[] = {
@@ -400,48 +402,71 @@ static void __init taa_select_mitigation(void)
 		return;
 	}
 
-	if (cpu_mitigations_off()) {
+	if (cpu_mitigations_off())
 		taa_mitigation = TAA_MITIGATION_OFF;
-		return;
-	}
 
 	/*
 	 * TAA mitigation via VERW is turned off if both
 	 * tsx_async_abort=off and mds=off are specified.
+	 *
+	 * MDS mitigation will be checked in taa_update_mitigation().
 	 */
-	if (taa_mitigation == TAA_MITIGATION_OFF &&
-	    mds_mitigation == MDS_MITIGATION_OFF)
+	if (taa_mitigation == TAA_MITIGATION_OFF)
 		return;
 
-	if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
+	/* Microcode will be checked in taa_update_mitigation(). */
+	if (taa_mitigation == TAA_MITIGATION_AUTO)
 		taa_mitigation = TAA_MITIGATION_VERW;
-	else
-		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
 
-	/*
-	 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
-	 * A microcode update fixes this behavior to clear CPU buffers. It also
-	 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
-	 * ARCH_CAP_TSX_CTRL_MSR bit.
-	 *
-	 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
-	 * update is required.
-	 */
-	if ( (x86_arch_cap_msr & ARCH_CAP_MDS_NO) &&
-	    !(x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))
-		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
+}
 
-	/*
-	 * TSX is enabled, select alternate mitigation for TAA which is
-	 * the same as MDS. Enable MDS static branch to clear CPU buffers.
-	 *
-	 * For guests that can't determine whether the correct microcode is
-	 * present on host, enable the mitigation for UCODE_NEEDED as well.
-	 */
-	setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
+static void __init taa_update_mitigation(void)
+{
+	if (!boot_cpu_has_bug(X86_BUG_TAA) || cpu_mitigations_off())
+		return;
+
+	if (verw_mitigation_enabled())
+		taa_mitigation = TAA_MITIGATION_VERW;
+
+	if (taa_mitigation == TAA_MITIGATION_VERW) {
+		/* Check if the requisite ucode is available. */
+		if (!boot_cpu_has(X86_FEATURE_MD_CLEAR))
+			taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
+
+		/*
+		 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
+		 * A microcode update fixes this behavior to clear CPU buffers. It also
+		 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
+		 * ARCH_CAP_TSX_CTRL_MSR bit.
+		 *
+		 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
+		 * update is required.
+		 */
+		if ((x86_arch_cap_msr & ARCH_CAP_MDS_NO) &&
+		   !(x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))
+			taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
+	}
+
+	pr_info("TAA: %s\n", taa_strings[taa_mitigation]);
+}
+
+static void __init taa_apply_mitigation(void)
+{
+	if (taa_mitigation == TAA_MITIGATION_VERW ||
+	    taa_mitigation == TAA_MITIGATION_UCODE_NEEDED) {
+		/*
+		 * TSX is enabled, select alternate mitigation for TAA which is
+		 * the same as MDS. Enable MDS static branch to clear CPU buffers.
+		 *
+		 * For guests that can't determine whether the correct microcode is
+		 * present on host, enable the mitigation for UCODE_NEEDED as well.
+		 */
+		setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
+
+		if (taa_nosmt || cpu_mitigations_auto_nosmt())
+			cpu_smt_disable(false);
+	}
 
-	if (taa_nosmt || cpu_mitigations_auto_nosmt())
-		cpu_smt_disable(false);
 }
 
 static int __init tsx_async_abort_parse_cmdline(char *str)
@@ -650,7 +675,6 @@ static void __init md_clear_update_mitigation(void)
 
 static void __init md_clear_select_mitigation(void)
 {
-	taa_select_mitigation();
 	mmio_select_mitigation();
 	rfds_select_mitigation();
 
-- 
2.34.1
Re: [PATCH v3 05/35] x86/bugs: Restructure taa mitigation
Posted by Josh Poimboeuf 10 months, 1 week ago
On Wed, Jan 08, 2025 at 02:24:45PM -0600, David Kaplan wrote:
> @@ -400,48 +402,71 @@ static void __init taa_select_mitigation(void)
>  		return;
>  	}
>  
> -	if (cpu_mitigations_off()) {
> +	if (cpu_mitigations_off())
>  		taa_mitigation = TAA_MITIGATION_OFF;
> -		return;
> -	}
>  
>  	/*
>  	 * TAA mitigation via VERW is turned off if both
>  	 * tsx_async_abort=off and mds=off are specified.
> +	 *
> +	 * MDS mitigation will be checked in taa_update_mitigation().
>  	 */
> -	if (taa_mitigation == TAA_MITIGATION_OFF &&
> -	    mds_mitigation == MDS_MITIGATION_OFF)
> +	if (taa_mitigation == TAA_MITIGATION_OFF)
>  		return;

This check seems rather pointless, the only thing after this is the
TAA_MITIGATION_AUTO check.

>  
> -	if (boot_cpu_has(X86_FEATURE_MD_CLEAR))
> +	/* Microcode will be checked in taa_update_mitigation(). */
> +	if (taa_mitigation == TAA_MITIGATION_AUTO)
>  		taa_mitigation = TAA_MITIGATION_VERW;
> -	else
> -		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;

In the previous patch, MDS checks for ucode in both select and update,
which is overkill.  That should probably be done only in
mds_update_mitigation() to be consistent with how TAA does it here?

>  
> -	/*
> -	 * VERW doesn't clear the CPU buffers when MD_CLEAR=1 and MDS_NO=1.
> -	 * A microcode update fixes this behavior to clear CPU buffers. It also
> -	 * adds support for MSR_IA32_TSX_CTRL which is enumerated by the
> -	 * ARCH_CAP_TSX_CTRL_MSR bit.
> -	 *
> -	 * On MDS_NO=1 CPUs if ARCH_CAP_TSX_CTRL_MSR is not set, microcode
> -	 * update is required.
> -	 */
> -	if ( (x86_arch_cap_msr & ARCH_CAP_MDS_NO) &&
> -	    !(x86_arch_cap_msr & ARCH_CAP_TSX_CTRL_MSR))
> -		taa_mitigation = TAA_MITIGATION_UCODE_NEEDED;
> +}

Extra whitespace here at the end of the function.

> +static void __init taa_update_mitigation(void)
> +{
> +	if (!boot_cpu_has_bug(X86_BUG_TAA) || cpu_mitigations_off())
> +		return;
> +
> +	if (verw_mitigation_enabled())
> +		taa_mitigation = TAA_MITIGATION_VERW;

This overwrites TAA_MITIGATION_TSX_DISABLED?

I think reporting TSX disabled here is more accurate than reporting
VERW, since the VERW is only done to mitigate the other vulns.

> +static void __init taa_apply_mitigation(void)
> +{
> +	if (taa_mitigation == TAA_MITIGATION_VERW ||
> +	    taa_mitigation == TAA_MITIGATION_UCODE_NEEDED) {
> +		/*
> +		 * TSX is enabled, select alternate mitigation for TAA which is
> +		 * the same as MDS. Enable MDS static branch to clear CPU buffers.
> +		 *
> +		 * For guests that can't determine whether the correct microcode is
> +		 * present on host, enable the mitigation for UCODE_NEEDED as well.
> +		 */
> +		setup_force_cpu_cap(X86_FEATURE_CLEAR_CPU_BUF);
> +
> +		if (taa_nosmt || cpu_mitigations_auto_nosmt())
> +			cpu_smt_disable(false);
> +	}
>  
> -	if (taa_nosmt || cpu_mitigations_auto_nosmt())
> -		cpu_smt_disable(false);
>  }

Another extra whitespace here at the end of the function.

-- 
Josh
Re: [PATCH v3 05/35] x86/bugs: Restructure taa mitigation
Posted by Brendan Jackman 10 months, 1 week ago
On Wed, 8 Jan 2025 at 21:27, David Kaplan <david.kaplan@amd.com> wrote:
> @@ -400,48 +402,71 @@ static void __init taa_select_mitigation(void)
>                 return;
>         }
>
> -       if (cpu_mitigations_off()) {
> +       if (cpu_mitigations_off())
>                 taa_mitigation = TAA_MITIGATION_OFF;
> -               return;
> -       }
>
>         /*
>          * TAA mitigation via VERW is turned off if both
>          * tsx_async_abort=off and mds=off are specified.
> +        *
> +        * MDS mitigation will be checked in taa_update_mitigation().

What we are actually talking about here is the new
verw_mitigation_enabled(), right? I don't think this block/commentary
adds any clarity any more. Maybe just delete it?