Skip to contentSkip to conversation

x86: Remove cpu_has_xfeatures() and add AVX-512 xor_gen()

PATCH Superseded Eric Biggers 6 participants 22 messages Jun 26 – Jul 1

Subsystems (6)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

cover x86: Remove cpu_has_xfeatures() and add AVX-512 xor_gen()

My patch "lib/raid/xor: x86: Add AVX-512 optimized xor_gen()"
(https://lore.kernel.org/r/[email protected]/)
still seems to be blocked on a Sashiko comment about cpu_has_xfeatures() not being called.

However, the x86-optimized RAID library code supports UML, and currently UML doesn't implement cpu_has_xfeatures(). That's perhaps why the existing AVX-512 optimized RAID6 code doesn't check it either.

In fact, it seems to have been getting by fine without it, which suggests that it's not truly needed.

But to eliminate any doubts, I've had a go at fully resolving the situation by making both native x86 and UML explicitly clear any X86_FEATURE_* flags at boot time whose xfeatures are missing.

Then, cpu_has_xfeatures() is entirely removed from the kernel.

The last patch adds the AVX-512 optimized xor_gen(). I do still think it would be fine to proceed with it without the rest. But if there are any doubts, we can take this more comprehensive cleanup route.

Eric Biggers (8):
x86/fpu: Check for missing AVX and AVX-512 xstate bits
um: Check for missing AVX and AVX-512 xstate bits
crypto: x86 - Stop using cpu_has_xfeatures() lib/crypto: x86: Stop using cpu_has_xfeatures() lib/crc: x86: Stop using cpu_has_xfeatures() x86/fpu: Remove cpu_has_xfeatures()
lib/raid/xor: x86: Remove redundant X86_FEATURE_OSXSAVE check lib/raid/xor: x86: Add AVX-512 optimized xor_gen()

 arch/um/kernel/um_arch.c                   |  78 ++++++++++++-
 arch/x86/crypto/aegis128-aesni-glue.c      |   3 +-
 arch/x86/crypto/aesni-intel_glue.c         |   7 +-
 arch/x86/crypto/aria_aesni_avx2_glue.c     |  11 +-
 arch/x86/crypto/aria_aesni_avx_glue.c      |  11 +-
 arch/x86/crypto/aria_gfni_avx512_glue.c    |  11 +-
 arch/x86/crypto/camellia_aesni_avx2_glue.c |  11 +-
 arch/x86/crypto/camellia_aesni_avx_glue.c  |  11 +-
 arch/x86/crypto/cast5_avx_glue.c           |   7 +-
 arch/x86/crypto/cast6_avx_glue.c           |   7 +-
 arch/x86/crypto/serpent_avx2_glue.c        |   9 +-
 arch/x86/crypto/serpent_avx_glue.c         |   7 +-
 arch/x86/crypto/sm4_aesni_avx2_glue.c      |  11 +-
 arch/x86/crypto/sm4_aesni_avx_glue.c       |  11 +-
 arch/x86/crypto/twofish_avx_glue.c         |   6 +-
 arch/x86/include/asm/fpu/api.h             |   9 --
 arch/x86/kernel/fpu/xstate.c               |  63 ++++-------
 lib/crc/x86/crc-pclmul-template.h          |   6 +-
 lib/crypto/x86/blake2s.h                   |   4 +-
 lib/crypto/x86/chacha.h                    |   3 +-
 lib/crypto/x86/nh.h                        |   4 +-
 lib/crypto/x86/poly1305.h                  |   7 +-
 lib/crypto/x86/sha1.h                      |   4 +-
 lib/crypto/x86/sha256.h                    |   4 +-
 lib/crypto/x86/sha512.h                    |   3 +-
 lib/crypto/x86/sm3.h                       |   3 +-
 lib/raid/xor/Makefile                      |   2 +-
 lib/raid/xor/x86/xor-avx512.c              | 121 +++++++++++++++++++++
 lib/raid/xor/x86/xor_arch.h                |  24 ++--
 29 files changed, 264 insertions(+), 194 deletions(-)
 create mode 100644 lib/raid/xor/x86/xor-avx512.c

base-commit: 4edcdefd4083ae04b1a5656f4be6cd83ae919ef4

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 1/8 x86/fpu: Check for missing AVX and AVX-512 xstate bits

If the CPU declares AVX or AVX-512 support, verify that the corresponding xstate bits are also set. If not, warn and clear them.

This eliminates the perceived need for AVX and AVX-512 optimized code in the kernel to call cpu_has_xfeatures(). That has never been universally done, which strongly suggests that it has never really been needed in practice, but this should remove any remaining doubt.

---
 arch/x86/kernel/fpu/xstate.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/x86/kernel/fpu/xstate.c +19 -0
index a7b6524a9dea..7f7e62e4ebc5 100644
@@ -797,10 +797,27 @@ static u64 __init guest_default_mask(void)
 	 * for KVM guests.
 	 */
 	return ~(u64)XFEATURE_MASK_USER_DYNAMIC;
 }
 
+/* Clear any X86_FEATURE_* used by the kernel whose xfeatures are missing. */
+static void __init clear_cpu_caps_with_missing_xfeatures(u64 xfeatures)
+{
+	u64 mask;
+
+	mask = XFEATURE_MASK_FPSSE | XFEATURE_MASK_YMM;
+	if (boot_cpu_has(X86_FEATURE_AVX) && (xfeatures & mask) != mask) {
+		pr_err("x86/fpu: Disabling AVX support due to missing xstate features\n");
+		setup_clear_cpu_cap(X86_FEATURE_AVX);
+	}
+	mask = XFEATURE_MASK_FPSSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512;
+	if (boot_cpu_has(X86_FEATURE_AVX512F) && (xfeatures & mask) != mask) {
+		pr_err("x86/fpu: Disabling AVX-512 support due to missing xstate features\n");
+		setup_clear_cpu_cap(X86_FEATURE_AVX512F);
+	}
+}
+
 /*
  * Enable and initialize the xsave feature.
  * Called once per system bootup.
  */
 void __init fpu__init_system_xstate(unsigned int legacy_size)
@@ -853,10 +870,12 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
 		pr_err("x86/fpu: Both APX/MPX present in the CPU's xstate features: 0x%llx.\n",
 		       fpu_kernel_cfg.max_features);
 		goto out_disable;
 	}
 
+	clear_cpu_caps_with_missing_xfeatures(fpu_kernel_cfg.max_features);
+
 	fpu_kernel_cfg.independent_features = fpu_kernel_cfg.max_features &
 					      XFEATURE_MASK_INDEPENDENT;
 
 	/*
 	 * Clear XSAVE features that are disabled in the normal CPUID.
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH 2/8 um: Check for missing AVX and AVX-512 xstate bits

In reply to Eric Biggers

If the CPU declares AVX or AVX-512 support, verify that all the corresponding xstate bits are also set. If any are missing, warn and don't set the corresponding X86_FEATURE_* flags.

This eliminates the perceived need for UML-supporting AVX and AVX-512 optimized code in the kernel (that is, lib/raid/ currently) to start checking the xstate bits in addition to X86_FEATURE_AVX*.

---
 arch/um/kernel/um_arch.c | 78 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)
arch/um/kernel/um_arch.c +77 -1
index 2141f5f1f5a2..aafbaef2ae82 100644
@@ -262,16 +262,92 @@ EXPORT_SYMBOL(task_size);
 
 unsigned long brk_start;
 
 #define MIN_VMALLOC (32 * 1024 * 1024)
 
+static u64 __init read_xcr0(void)
+{
+	u32 a, b, c, d;
+
+	asm volatile("cpuid"
+		     : "=a"(a), "=b"(b), "=c"(c), "=d"(d)
+		     : "a"(0), "c"(0));
+	if (a >= 1) { /* max_leaf >= 1 */
+		asm volatile("cpuid"
+			     : "=a"(a), "=b"(b), "=c"(c), "=d"(d)
+			     : "a"(1), "c"(0));
+		if (c & (1 << 27)) { /* XSAVE enabled by OS */
+			asm volatile("xgetbv" : "=d"(d), "=a"(a) : "c"(0));
+			return ((u64)d << 32) | a;
+		}
+	}
+	return 0;
+}
+
+static void __init validate_and_set_cpu_cap(int cap, u64 xcr0)
+{
+	/*
+	 * Check for missing xstate features right away, so that there's no
+	 * perceived need for all optimized code in the kernel to do so.
+	 */
+	switch (cap) {
+	case X86_FEATURE_AVX:
+	case X86_FEATURE_AVX2:
+	case X86_FEATURE_AVX_VNNI:
+	case X86_FEATURE_FMA:
+	case X86_FEATURE_VAES:
+	case X86_FEATURE_VPCLMULQDQ:
+		if ((xcr0 & 0x7) != 0x7) {
+			static bool warned;
+
+			if (!warned) {
+				os_warn("Disabling AVX support due to missing xstate features\n");
+				warned = true;
+			}
+			return;
+		}
+		break;
+	case X86_FEATURE_AVX512F:
+	case X86_FEATURE_AVX512BW:
+	case X86_FEATURE_AVX512CD:
+	case X86_FEATURE_AVX512DQ:
+	case X86_FEATURE_AVX512ER:
+	case X86_FEATURE_AVX512IFMA:
+	case X86_FEATURE_AVX512PF:
+	case X86_FEATURE_AVX512VBMI:
+	case X86_FEATURE_AVX512VL:
+	case X86_FEATURE_AVX512_4FMAPS:
+	case X86_FEATURE_AVX512_4VNNIW:
+	case X86_FEATURE_AVX512_BF16:
+	case X86_FEATURE_AVX512_BITALG:
+	case X86_FEATURE_AVX512_FP16:
+	case X86_FEATURE_AVX512_VBMI2:
+	case X86_FEATURE_AVX512_VNNI:
+	case X86_FEATURE_AVX512_VP2INTERSECT:
+	case X86_FEATURE_AVX512_VPOPCNTDQ:
+		if ((xcr0 & 0xe7) != 0xe7) {
+			static bool warned;
+
+			if (!warned) {
+				os_warn("Disabling AVX-512 support due to missing xstate features\n");
+				warned = true;
+			}
+			return;
+		}
+		break;
+	}
+	set_cpu_cap(&boot_cpu_data, cap);
+}
+
 static void __init parse_host_cpu_flags(char *line)
 {
+	u64 xcr0 = read_xcr0();
 	int i;
+
 	for (i = 0; i < 32*NCAPINTS; i++) {
 		if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
-			set_cpu_cap(&boot_cpu_data, i);
+			validate_and_set_cpu_cap(i, xcr0);
 	}
 }
 
 static void __init parse_cache_line(char *line)
 {
Signature · 1 line
2.54.0
author maintainer
#11 lore raw

PATCH 3/8 crypto: x86 - Stop using cpu_has_xfeatures()

In reply to Eric Biggers

Checking both boot_cpu_has(X86_FEATURE_AVX*) and cpu_has_xfeatures() has never really been needed in practice, and it's never been universally done (e.g., lib/raid/ omits cpu_has_xfeatures()). Nevertheless, both x86 and UML now explicitly clear the AVX and AVX-512 flags if their xfeatures are missing, which should remove any remaining doubts.

Thus, remove all the calls to cpu_has_xfeatures(), as well as the related checks of boot_cpu_has(X86_FEATURE_OSXSAVE).

In a few cases there was no corresponding boot_cpu_has(X86_FEATURE_AVX*) check, so add the missing ones.

---
 arch/x86/crypto/aegis128-aesni-glue.c      |  3 +--
 arch/x86/crypto/aesni-intel_glue.c         |  7 ++-----
 arch/x86/crypto/aria_aesni_avx2_glue.c     | 11 +----------
 arch/x86/crypto/aria_aesni_avx_glue.c      | 11 +----------
 arch/x86/crypto/aria_gfni_avx512_glue.c    | 11 +----------
 arch/x86/crypto/camellia_aesni_avx2_glue.c | 11 +----------
 arch/x86/crypto/camellia_aesni_avx_glue.c  | 11 +----------
 arch/x86/crypto/cast5_avx_glue.c           |  7 ++-----
 arch/x86/crypto/cast6_avx_glue.c           |  7 ++-----
 arch/x86/crypto/serpent_avx2_glue.c        |  9 +--------
 arch/x86/crypto/serpent_avx_glue.c         |  7 ++-----
 arch/x86/crypto/sm4_aesni_avx2_glue.c      | 11 +----------
 arch/x86/crypto/sm4_aesni_avx_glue.c       | 11 +----------
 arch/x86/crypto/twofish_avx_glue.c         |  6 ++----
 14 files changed, 19 insertions(+), 104 deletions(-)
arch/x86/crypto/aegis128-aesni-glue.c +1 -2
index f1adfba1a76e..09fc0b15b0e9 100644
@@ -263,12 +263,11 @@ static struct aead_alg crypto_aegis128_aesni_alg = {
 };
 
 static int __init crypto_aegis128_aesni_module_init(void)
 {
 	if (!boot_cpu_has(X86_FEATURE_XMM4_1) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL))
+	    !boot_cpu_has(X86_FEATURE_AES))
 		return -ENODEV;
 
 	return crypto_register_aead(&crypto_aegis128_aesni_alg);
 }
 
arch/x86/crypto/aesni-intel_glue.c +2 -5
index f522fff9231e..f6f899db7482 100644
@@ -1546,12 +1546,11 @@ static int __init register_avx_algs(void)
 	 * For simplicity, just always check for VAES and VPCLMULQDQ together.
 	 */
 	if (!boot_cpu_has(X86_FEATURE_AVX2) ||
 	    !boot_cpu_has(X86_FEATURE_VAES) ||
 	    !boot_cpu_has(X86_FEATURE_VPCLMULQDQ) ||
-	    !boot_cpu_has(X86_FEATURE_PCLMULQDQ) ||
-	    !cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
+	    !boot_cpu_has(X86_FEATURE_PCLMULQDQ))
 		return 0;
 	err = crypto_register_skciphers(skcipher_algs_vaes_avx2,
 					ARRAY_SIZE(skcipher_algs_vaes_avx2));
 	if (err)
 		return err;
@@ -1560,13 +1559,11 @@ static int __init register_avx_algs(void)
 	if (err)
 		return err;
 
 	if (!boot_cpu_has(X86_FEATURE_AVX512BW) ||
 	    !boot_cpu_has(X86_FEATURE_AVX512VL) ||
-	    !boot_cpu_has(X86_FEATURE_BMI2) ||
-	    !cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM |
-			       XFEATURE_MASK_AVX512, NULL))
+	    !boot_cpu_has(X86_FEATURE_BMI2))
 		return 0;
 
 	if (boot_cpu_has(X86_FEATURE_PREFER_YMM)) {
 		int i;
 
arch/x86/crypto/aria_aesni_avx2_glue.c +1 -10
index 1487a49bfbac..371be2fb6469 100644
@@ -193,26 +193,17 @@ static struct skcipher_alg aria_algs[] = {
 	}
 };
 
 static int __init aria_avx2_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
 	    !boot_cpu_has(X86_FEATURE_AVX2) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX2 or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	if (boot_cpu_has(X86_FEATURE_GFNI)) {
 		aria_ops.aria_encrypt_16way = aria_aesni_avx_gfni_encrypt_16way;
 		aria_ops.aria_decrypt_16way = aria_aesni_avx_gfni_decrypt_16way;
 		aria_ops.aria_ctr_crypt_16way = aria_aesni_avx_gfni_ctr_crypt_16way;
 		aria_ops.aria_encrypt_32way = aria_aesni_avx2_gfni_encrypt_32way;
arch/x86/crypto/aria_aesni_avx_glue.c +1 -10
index e4e3d78915a5..d23fc91c0ebd 100644
@@ -180,25 +180,16 @@ static struct skcipher_alg aria_algs[] = {
 	}
 };
 
 static int __init aria_avx_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	if (boot_cpu_has(X86_FEATURE_GFNI)) {
 		aria_ops.aria_encrypt_16way = aria_aesni_avx_gfni_encrypt_16way;
 		aria_ops.aria_decrypt_16way = aria_aesni_avx_gfni_decrypt_16way;
 		aria_ops.aria_ctr_crypt_16way = aria_aesni_avx_gfni_ctr_crypt_16way;
 	} else {
arch/x86/crypto/aria_gfni_avx512_glue.c +1 -10
index 363cbf4399cc..e05bbeb22d4a 100644
@@ -194,28 +194,19 @@ static struct skcipher_alg aria_algs[] = {
 	}
 };
 
 static int __init aria_avx512_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
 	    !boot_cpu_has(X86_FEATURE_AVX2) ||
 	    !boot_cpu_has(X86_FEATURE_AVX512F) ||
 	    !boot_cpu_has(X86_FEATURE_AVX512VL) ||
-	    !boot_cpu_has(X86_FEATURE_GFNI) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_GFNI)) {
 		pr_info("AVX512/GFNI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM |
-			       XFEATURE_MASK_AVX512, &feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	aria_ops.aria_encrypt_16way = aria_aesni_avx_gfni_encrypt_16way;
 	aria_ops.aria_decrypt_16way = aria_aesni_avx_gfni_decrypt_16way;
 	aria_ops.aria_ctr_crypt_16way = aria_aesni_avx_gfni_ctr_crypt_16way;
 	aria_ops.aria_encrypt_32way = aria_aesni_avx2_gfni_encrypt_32way;
 	aria_ops.aria_decrypt_32way = aria_aesni_avx2_gfni_decrypt_32way;
arch/x86/crypto/camellia_aesni_avx2_glue.c +1 -10
index 2d2f4e16537c..073fa3bb8388 100644
@@ -95,26 +95,17 @@ static struct skcipher_alg camellia_algs[] = {
 	},
 };
 
 static int __init camellia_aesni_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
 	    !boot_cpu_has(X86_FEATURE_AVX2) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX2 or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	return crypto_register_skciphers(camellia_algs,
 					 ARRAY_SIZE(camellia_algs));
 }
 
 static void __exit camellia_aesni_fini(void)
arch/x86/crypto/camellia_aesni_avx_glue.c +1 -10
index 5c321f255eb7..872e5e07220f 100644
@@ -96,25 +96,16 @@ static struct skcipher_alg camellia_algs[] = {
 	}
 };
 
 static int __init camellia_aesni_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	return crypto_register_skciphers(camellia_algs,
 					 ARRAY_SIZE(camellia_algs));
 }
 
 static void __exit camellia_aesni_fini(void)
arch/x86/crypto/cast5_avx_glue.c +2 -5
index 3aca04d43b34..5de35e863370 100644
@@ -90,15 +90,12 @@ static struct skcipher_alg cast5_algs[] = {
 	}
 };
 
 static int __init cast5_init(void)
 {
-	const char *feature_name;
-
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
+	if (!boot_cpu_has(X86_FEATURE_AVX)) {
+		pr_info("AVX instructions are not detected.\n");
 		return -ENODEV;
 	}
 
 	return crypto_register_skciphers(cast5_algs,
 					 ARRAY_SIZE(cast5_algs));
arch/x86/crypto/cast6_avx_glue.c +2 -5
index c4dd28c30303..3d7ea48007bc 100644
@@ -90,15 +90,12 @@ static struct skcipher_alg cast6_algs[] = {
 	},
 };
 
 static int __init cast6_init(void)
 {
-	const char *feature_name;
-
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
+	if (!boot_cpu_has(X86_FEATURE_AVX)) {
+		pr_info("AVX instructions are not detected.\n");
 		return -ENODEV;
 	}
 
 	return crypto_register_skciphers(cast6_algs, ARRAY_SIZE(cast6_algs));
 }
arch/x86/crypto/serpent_avx2_glue.c +1 -8
index f5f2121b7956..72a9e2b306d6 100644
@@ -91,21 +91,14 @@ static struct skcipher_alg serpent_algs[] = {
 	},
 };
 
 static int __init serpent_avx2_init(void)
 {
-	const char *feature_name;
-
-	if (!boot_cpu_has(X86_FEATURE_AVX2) || !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	if (!boot_cpu_has(X86_FEATURE_AVX2)) {
 		pr_info("AVX2 instructions are not detected.\n");
 		return -ENODEV;
 	}
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
 
 	return crypto_register_skciphers(serpent_algs,
 					 ARRAY_SIZE(serpent_algs));
 }
 
arch/x86/crypto/serpent_avx_glue.c +2 -5
index 9c8b3a335d5c..42c4e1569674 100644
@@ -98,15 +98,12 @@ static struct skcipher_alg serpent_algs[] = {
 	},
 };
 
 static int __init serpent_init(void)
 {
-	const char *feature_name;
-
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
+	if (!boot_cpu_has(X86_FEATURE_AVX)) {
+		pr_info("AVX instructions are not detected.\n");
 		return -ENODEV;
 	}
 
 	return crypto_register_skciphers(serpent_algs,
 					 ARRAY_SIZE(serpent_algs));
arch/x86/crypto/sm4_aesni_avx2_glue.c +1 -10
index fec0ab7a63dd..eef73894e777 100644
@@ -96,26 +96,17 @@ static struct skcipher_alg sm4_aesni_avx2_skciphers[] = {
 	}
 };
 
 static int __init sm4_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
 	    !boot_cpu_has(X86_FEATURE_AVX2) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX2 or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	return crypto_register_skciphers(sm4_aesni_avx2_skciphers,
 					 ARRAY_SIZE(sm4_aesni_avx2_skciphers));
 }
 
 static void __exit sm4_exit(void)
arch/x86/crypto/sm4_aesni_avx_glue.c +1 -10
index 88caf418a06f..ed383da5ff46 100644
@@ -312,25 +312,16 @@ static struct skcipher_alg sm4_aesni_avx_skciphers[] = {
 	}
 };
 
 static int __init sm4_init(void)
 {
-	const char *feature_name;
-
 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
-	    !boot_cpu_has(X86_FEATURE_AES) ||
-	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	    !boot_cpu_has(X86_FEATURE_AES)) {
 		pr_info("AVX or AES-NI instructions are not detected.\n");
 		return -ENODEV;
 	}
 
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				&feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
-		return -ENODEV;
-	}
-
 	return crypto_register_skciphers(sm4_aesni_avx_skciphers,
 					 ARRAY_SIZE(sm4_aesni_avx_skciphers));
 }
 
 static void __exit sm4_exit(void)
arch/x86/crypto/twofish_avx_glue.c +2 -4
index 9e20db013750..985bc54a2340 100644
@@ -100,14 +100,12 @@ static struct skcipher_alg twofish_algs[] = {
 	},
 };
 
 static int __init twofish_init(void)
 {
-	const char *feature_name;
-
-	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
-		pr_info("CPU feature '%s' is not supported.\n", feature_name);
+	if (!boot_cpu_has(X86_FEATURE_AVX)) {
+		pr_info("AVX instructions are not detected.\n");
 		return -ENODEV;
 	}
 
 	return crypto_register_skciphers(twofish_algs,
 					 ARRAY_SIZE(twofish_algs));
Signature · 1 line
2.54.0
author maintainer
#12 lore raw

PATCH 4/8 lib/crypto: x86: Stop using cpu_has_xfeatures()

In reply to Eric Biggers

Checking both boot_cpu_has() and cpu_has_xfeatures() has never really been needed in practice, and it's never been universally done (e.g., lib/raid/ omits cpu_has_xfeatures()). Nevertheless, both x86 and UML now explicitly clear the AVX and AVX-512 flags if their xfeatures are missing, which should remove any remaining doubts.

Thus, remove all the calls to cpu_has_xfeatures().

---
 lib/crypto/x86/blake2s.h  | 4 +---
 lib/crypto/x86/chacha.h   | 3 +--
 lib/crypto/x86/nh.h       | 4 +---
 lib/crypto/x86/poly1305.h | 7 ++-----
 lib/crypto/x86/sha1.h     | 4 +---
 lib/crypto/x86/sha256.h   | 4 +---
 lib/crypto/x86/sha512.h   | 3 +--
 lib/crypto/x86/sm3.h      | 3 +--
 8 files changed, 9 insertions(+), 23 deletions(-)
lib/crypto/x86/blake2s.h +1 -3
index f8eed6cb042e..0f7c51f055c8 100644
@@ -53,10 +53,8 @@ static void blake2s_mod_init_arch(void)
 		static_branch_enable(&blake2s_use_ssse3);
 
 	if (boot_cpu_has(X86_FEATURE_AVX) &&
 	    boot_cpu_has(X86_FEATURE_AVX2) &&
 	    boot_cpu_has(X86_FEATURE_AVX512F) &&
-	    boot_cpu_has(X86_FEATURE_AVX512VL) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM |
-			      XFEATURE_MASK_AVX512, NULL))
+	    boot_cpu_has(X86_FEATURE_AVX512VL))
 		static_branch_enable(&blake2s_use_avx512);
 }
lib/crypto/x86/chacha.h +1 -2
index 10cf8f1c569d..c79562aac56b 100644
@@ -163,12 +163,11 @@ static void chacha_mod_init_arch(void)
 		return;
 
 	static_branch_enable(&chacha_use_simd);
 
 	if (boot_cpu_has(X86_FEATURE_AVX) &&
-	    boot_cpu_has(X86_FEATURE_AVX2) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
+	    boot_cpu_has(X86_FEATURE_AVX2)) {
 		static_branch_enable(&chacha_use_avx2);
 
 		if (boot_cpu_has(X86_FEATURE_AVX512VL) &&
 		    boot_cpu_has(X86_FEATURE_AVX512BW)) /* kmovq */
 			static_branch_enable(&chacha_use_avx512vl);
lib/crypto/x86/nh.h +1 -3
index 83361c2e9783..342636dcb750 100644
@@ -35,11 +35,9 @@ static bool nh_arch(const u32 *key, const u8 *message, size_t message_len,
 #define nh_mod_init_arch nh_mod_init_arch
 static void nh_mod_init_arch(void)
 {
 	if (boot_cpu_has(X86_FEATURE_XMM2)) {
 		static_branch_enable(&have_sse2);
-		if (boot_cpu_has(X86_FEATURE_AVX2) &&
-		    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				      NULL))
+		if (boot_cpu_has(X86_FEATURE_AVX2))
 			static_branch_enable(&have_avx2);
 	}
 }
lib/crypto/x86/poly1305.h +2 -5
index ee92e3740a78..b061b9926fa5 100644
@@ -141,18 +141,15 @@ static void poly1305_emit(const struct poly1305_state *ctx,
 }
 
 #define poly1305_mod_init_arch poly1305_mod_init_arch
 static void poly1305_mod_init_arch(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
+	if (boot_cpu_has(X86_FEATURE_AVX))
 		static_branch_enable(&poly1305_use_avx);
-	if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
+	if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2))
 		static_branch_enable(&poly1305_use_avx2);
 	if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
 	    boot_cpu_has(X86_FEATURE_AVX512F) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512, NULL) &&
 	    /* Skylake downclocks unacceptably much when using zmm, but later generations are fast. */
 	    boot_cpu_data.x86_vfm != INTEL_SKYLAKE_X)
 		static_branch_enable(&poly1305_use_avx512);
 }
lib/crypto/x86/sha1.h +1 -3
index c48a0131fd12..6aff433466e7 100644
@@ -57,13 +57,11 @@ static void sha1_blocks(struct sha1_block_state *state,
 #define sha1_mod_init_arch sha1_mod_init_arch
 static void sha1_mod_init_arch(void)
 {
 	if (boot_cpu_has(X86_FEATURE_SHA_NI)) {
 		static_call_update(sha1_blocks_x86, sha1_blocks_ni);
-	} else if (cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				     NULL) &&
-		   boot_cpu_has(X86_FEATURE_AVX)) {
+	} else if (boot_cpu_has(X86_FEATURE_AVX)) {
 		if (boot_cpu_has(X86_FEATURE_AVX2) &&
 		    boot_cpu_has(X86_FEATURE_BMI1) &&
 		    boot_cpu_has(X86_FEATURE_BMI2))
 			static_call_update(sha1_blocks_x86, sha1_blocks_avx2);
 		else
lib/crypto/x86/sha256.h +1 -3
index 0ee69d8e39fe..e98ffdaf4b14 100644
@@ -102,13 +102,11 @@ static void sha256_mod_init_arch(void)
 		static_branch_enable(&have_sha_ni);
 	} else if (IS_ENABLED(CONFIG_CPU_SUP_ZHAOXIN) &&
 		   boot_cpu_has(X86_FEATURE_PHE_EN) &&
 		   boot_cpu_data.x86 >= 0x07) {
 		static_call_update(sha256_blocks_x86, sha256_blocks_phe);
-	} else if (cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
-				     NULL) &&
-		   boot_cpu_has(X86_FEATURE_AVX)) {
+	} else if (boot_cpu_has(X86_FEATURE_AVX)) {
 		if (boot_cpu_has(X86_FEATURE_AVX2) &&
 		    boot_cpu_has(X86_FEATURE_BMI2))
 			static_call_update(sha256_blocks_x86,
 					   sha256_blocks_avx2);
 		else
lib/crypto/x86/sha512.h +1 -2
index 0213c70cedd0..4e177b4606bd 100644
@@ -35,12 +35,11 @@ static void sha512_blocks(struct sha512_block_state *state,
 }
 
 #define sha512_mod_init_arch sha512_mod_init_arch
 static void sha512_mod_init_arch(void)
 {
-	if (cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL) &&
-	    boot_cpu_has(X86_FEATURE_AVX)) {
+	if (boot_cpu_has(X86_FEATURE_AVX)) {
 		if (boot_cpu_has(X86_FEATURE_AVX2) &&
 		    boot_cpu_has(X86_FEATURE_BMI2))
 			static_call_update(sha512_blocks_x86,
 					   sha512_blocks_avx2);
 		else
lib/crypto/x86/sm3.h +1 -2
index 3834780f2f6a..e06d4a22e4fa 100644
@@ -31,9 +31,8 @@ static void sm3_blocks(struct sm3_block_state *state,
 }
 
 #define sm3_mod_init_arch sm3_mod_init_arch
 static void sm3_mod_init_arch(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_BMI2) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
+	if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_BMI2))
 		static_call_update(sm3_blocks_x86, sm3_blocks_avx);
 }
Signature · 1 line
2.54.0
author maintainer
#14 lore raw

PATCH 5/8 lib/crc: x86: Stop using cpu_has_xfeatures()

In reply to Eric Biggers

Checking both boot_cpu_has() and cpu_has_xfeatures() has never really been needed in practice, and it's never been universally done (e.g., lib/raid/ omits cpu_has_xfeatures()). Nevertheless, both x86 and UML now explicitly clear the AVX and AVX-512 flags if their xfeatures are missing, which should remove any remaining doubts.

Thus, remove all the calls to cpu_has_xfeatures().

---
 lib/crc/x86/crc-pclmul-template.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
lib/crc/x86/crc-pclmul-template.h +2 -4
index 02744831c6fa..893119bb7c07 100644
@@ -25,20 +25,18 @@ crc_t prefix##_vpclmul_avx512(crc_t crc, const u8 *p, size_t len,	\
 DEFINE_STATIC_CALL(prefix##_pclmul, prefix##_pclmul_sse)
 
 static inline bool have_vpclmul(void)
 {
 	return boot_cpu_has(X86_FEATURE_VPCLMULQDQ) &&
-	       boot_cpu_has(X86_FEATURE_AVX2) &&
-	       cpu_has_xfeatures(XFEATURE_MASK_YMM, NULL);
+	       boot_cpu_has(X86_FEATURE_AVX2);
 }
 
 static inline bool have_avx512(void)
 {
 	return boot_cpu_has(X86_FEATURE_AVX512BW) &&
 	       boot_cpu_has(X86_FEATURE_AVX512VL) &&
-	       !boot_cpu_has(X86_FEATURE_PREFER_YMM) &&
-	       cpu_has_xfeatures(XFEATURE_MASK_AVX512, NULL);
+	       !boot_cpu_has(X86_FEATURE_PREFER_YMM);
 }
 
 /*
  * Call a [V]PCLMULQDQ optimized CRC function if the data length is at least 16
  * bytes, the CPU has PCLMULQDQ support, and the current context may use SIMD.
Signature · 1 line
2.54.0
author maintainer
#15 lore raw

PATCH 6/8 x86/fpu: Remove cpu_has_xfeatures()

In reply to Eric Biggers

The only remaining caller of cpu_has_xfeatures() is print_xstate_features(), which uses it only to check and get the name of a single feature.

Remove it and just inline the needed code into print_xstate_features().

This also makes the "unknown xstate feature" entry at index XFEATURE_MAX of xfeature_names[] unnecessary, so remove that too.

---
 arch/x86/include/asm/fpu/api.h |  9 -------
 arch/x86/kernel/fpu/xstate.c   | 44 +++-------------------------------
 2 files changed, 3 insertions(+), 50 deletions(-)
arch/x86/include/asm/fpu/api.h +0 -9
index 90c63fe19c0f..cfed8b24d64f 100644
@@ -97,19 +97,10 @@ static inline void fpregs_assert_state_consistent(void) { }
 /*
  * Load the task FPU state before returning to userspace.
  */
 extern void switch_fpu_return(void);
 
-/*
- * Query the presence of one or more xfeatures. Works on any legacy CPU as well.
- *
- * If 'feature_name' is set then put a human-readable description of
- * the feature there as well - this can be used to print error (or success)
- * messages.
- */
-extern int cpu_has_xfeatures(u64 xfeatures_mask, const char **feature_name);
-
 /* Trap handling */
 extern int  fpu__exception_code(struct fpu *fpu, int trap_nr);
 extern void fpu_sync_fpstate(struct fpu *fpu);
 extern void fpu_reset_from_exception_fixup(void);
 
arch/x86/kernel/fpu/xstate.c +3 -41

76 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#16 lore raw

PATCH 7/8 lib/raid/xor: x86: Remove redundant X86_FEATURE_OSXSAVE check

In reply to Eric Biggers

X86_FEATURE_AVX implies X86_FEATURE_OSXSAVE already.

---
 lib/raid/xor/x86/xor_arch.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/raid/xor/x86/xor_arch.h +1 -2
index 99fe85a213c6..991abe3f4bbd 100644
@@ -16,12 +16,11 @@ extern struct xor_block_template xor_block_avx;
  *
  * 32-bit without MMX can fall back to the generic routines.
  */
 static __always_inline void __init arch_xor_init(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX) &&
-	    boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	if (boot_cpu_has(X86_FEATURE_AVX)) {
 		xor_force(&xor_block_avx);
 	} else if (IS_ENABLED(CONFIG_X86_64) || boot_cpu_has(X86_FEATURE_XMM)) {
 		xor_register(&xor_block_sse);
 		xor_register(&xor_block_sse_pf64);
 	} else if (boot_cpu_has(X86_FEATURE_MMX)) {
Signature · 1 line
2.54.0
author maintainer
#19 lore raw

PATCH 8/8 lib/raid/xor: x86: Add AVX-512 optimized xor_gen()

In reply to Eric Biggers

Add an implementation of xor_gen() using AVX-512.

It uses 512-bit vectors, i.e. ZMM registers. It also uses the vpternlogq instruction to do three-input XORs when applicable.

It's enabled on x86_64 CPUs that have AVX512F && !PREFER_YMM. In practice that means:

- AMD Zen 4 and later (client and server)
- Intel Sapphire Rapids and later (server)
- Intel Rocket Lake (client)
- Intel Nova Lake and later (client)

The !PREFER_YMM condition excludes the older AVX-512 implementations in Intel Skylake Server and Intel Ice Lake. They could run this code, but they're known to have overly-eager downclocking when ZMM registers are used. This is the same policy that the crypto and CRC code uses.

Benchmark on AMD Ryzen 9 9950X (Zen 5):

    src_cnt    avx          avx512       Improvement
    =======    ==========   ==========   ===========
    1          56353 MB/s   75388 MB/s   33%
    2          54274 MB/s   68409 MB/s   26%
    3          44649 MB/s   64042 MB/s   43%
    4          41315 MB/s   55002 MB/s   33%
---
 lib/raid/xor/Makefile         |   2 +-
 lib/raid/xor/x86/xor-avx512.c | 121 ++++++++++++++++++++++++++++++++++
 lib/raid/xor/x86/xor_arch.h   |  23 ++++---
 3 files changed, 135 insertions(+), 11 deletions(-)
 create mode 100644 lib/raid/xor/x86/xor-avx512.c
lib/raid/xor/Makefile +1 -1
index e8ecec3c09f9..4a0e5c6d8298 100644
@@ -27,11 +27,11 @@ xor-$(CONFIG_ALTIVEC)		+= powerpc/xor_vmx.o powerpc/xor_vmx_glue.o
 xor-$(CONFIG_RISCV_ISA_V)	+= riscv/xor.o riscv/xor-glue.o
 xor-$(CONFIG_SPARC32)		+= sparc/xor-sparc32.o
 xor-$(CONFIG_SPARC64)		+= sparc/xor-sparc64.o sparc/xor-sparc64-glue.o
 xor-$(CONFIG_S390)		+= s390/xor.o
 xor-$(CONFIG_X86_32)		+= x86/xor-avx.o x86/xor-sse.o x86/xor-mmx.o
-xor-$(CONFIG_X86_64)		+= x86/xor-avx.o x86/xor-sse.o
+xor-$(CONFIG_X86_64)		+= x86/xor-avx.o x86/xor-sse.o x86/xor-avx512.o
 obj-y				+= tests/
 
 CFLAGS_xor-neon.o		+= $(CC_FLAGS_FPU) -I$(src)/$(SRCARCH)
 CFLAGS_REMOVE_xor-neon.o	+= $(CC_FLAGS_NO_FPU)
 
lib/raid/xor/x86/xor-avx512.c new file +121 -0

121 lines not on this page · raw message

lib/raid/xor/x86/xor_arch.h +13 -10
index 991abe3f4bbd..d5e192b8793f 100644
@@ -4,25 +4,28 @@
 extern struct xor_block_template xor_block_pII_mmx;
 extern struct xor_block_template xor_block_p5_mmx;
 extern struct xor_block_template xor_block_sse;
 extern struct xor_block_template xor_block_sse_pf64;
 extern struct xor_block_template xor_block_avx;
+extern struct xor_block_template xor_block_avx512;
 
-/*
- * When SSE is available, use it as it can write around L2.  We may also be able
- * to load into the L1 only depending on how the cpu deals with a load to a line
- * that is being prefetched.
- *
- * When AVX2 is available, force using it as it is better by all measures.
- *
- * 32-bit without MMX can fall back to the generic routines.
- */
 static __always_inline void __init arch_xor_init(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX)) {
+	if (IS_ENABLED(CONFIG_X86_64) && boot_cpu_has(X86_FEATURE_AVX512F) &&
+	    !boot_cpu_has(X86_FEATURE_PREFER_YMM)) {
+		/* AVX-512 will be the best; no need to try others. */
+		/* !PREFER_YMM excludes CPUs with overly-eager downclocking. */
+		xor_force(&xor_block_avx512);
+	} else if (boot_cpu_has(X86_FEATURE_AVX)) {
+		/* AVX will be the best; no need to try others. */
 		xor_force(&xor_block_avx);
 	} else if (IS_ENABLED(CONFIG_X86_64) || boot_cpu_has(X86_FEATURE_XMM)) {
+		/*
+		 * When SSE is available, use it as it can write around L2.  We
+		 * may also be able to load into the L1 only depending on how
+		 * the cpu deals with a load to a line that is being prefetched.
+		 */
 		xor_register(&xor_block_sse);
 		xor_register(&xor_block_sse_pf64);
 	} else if (boot_cpu_has(X86_FEATURE_MMX)) {
 		xor_register(&xor_block_pII_mmx);
 		xor_register(&xor_block_p5_mmx);
Signature · 1 line
2.54.0
#17 lore raw

Re: [PATCH 7/8] lib/raid/xor: x86: Remove redundant X86_FEATURE_OSXSAVE check

In reply to Eric Biggers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing the X86_FEATURE_OSXSAVE check can cause an Invalid Opcode (#UD) kernel panic during XOR…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the X86_FEATURE_OSXSAVE check can cause an Invalid Opcode (#UD) kernel panic during XOR RAID operations if the CPU natively advertises AVX but not XSAVE. --

commit 6fce8e80966a4c49b0b494779bd6206c6d6bed1f
Author: Eric Biggers <[email protected]>

lib/raid/xor: x86: Remove redundant X86_FEATURE_OSXSAVE check

This commit removes the X86_FEATURE_OSXSAVE check when selecting the AVX XOR implementation, assuming X86_FEATURE_AVX implies X86_FEATURE_OSXSAVE.

lib/raid/xor/x86/xor_arch.h extern struct xor_block_template xor_block_avx; · line 21

Quoted 11 lines
lib/raid/xor/x86/xor_arch.h +1 -2
index 99fe85a213c66..991abe3f4bbda 100644
@@ -18,8 +18,7 @@ extern struct xor_block_template xor_block_avx;
  */
 static __always_inline void __init arch_xor_init(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX) &&
-	    boot_cpu_has(X86_FEATURE_OSXSAVE)) {
+	if (boot_cpu_has(X86_FEATURE_AVX)) {

[Severity: High]
Can this lead to an Invalid Opcode (#UD) kernel panic during XOR RAID operations if a CPU advertises AVX but not XSAVE?

If a hypervisor exposes AVX but hides XSAVE (for example, using QEMU's -cpu host,-xsave flag), get_cpu_cap() still sets X86_FEATURE_AVX in boot_cpu_data.

Although check_cpufeature_deps() detects the missing dependency, it only issues a pr_warn_once warning without actually clearing X86_FEATURE_AVX.

During boot, fpu__init_system_xstate() evaluates !boot_cpu_has(X86_FEATURE_XSAVE) as true and executes an early return. This early return bypasses clear_cpu_caps_with_missing_xfeatures(), meaning X86_FEATURE_AVX is never cleared by the kernel.

Since the X86_FEATURE_OSXSAVE check was removed here, arch_xor_init() will forcibly select xor_block_avx based solely on X86_FEATURE_AVX. When VEX-encoded AVX instructions are later executed, the CPU will throw an Invalid Opcode (#UD) exception because CR4.OSXSAVE was never set to 1 by the OS.

Is there another mechanism that guarantees X86_FEATURE_AVX is cleared in this scenario, or should the X86_FEATURE_OSXSAVE check be retained to safely ensure the OS has fully initialized and enabled XSAVE?

Quoted 3 lines
lib/raid/xor/x86/xor_arch.h +0 -0
 		xor_force(&xor_block_avx);
 	} else if (IS_ENABLED(CONFIG_X86_64) || boot_cpu_has(X86_FEATURE_XMM)) {
 		xor_register(&xor_block_sse);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#13 lore raw

Re: [PATCH 4/8] lib/crypto: x86: Stop using cpu_has_xfeatures()

In reply to Eric Biggers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing `cpu_has_xfeatures()` causes a kernel #UD panic on systems advertising AVX without XSAVE.…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing cpu_has_xfeatures() causes a kernel #UD panic on systems advertising AVX without XSAVE. --

commit c4725d088a9c170c27e417f42ccdfa48f567b845
Author: Eric Biggers <[email protected]>

lib/crypto: x86: Stop using cpu_has_xfeatures()

This commit removes calls to cpu_has_xfeatures() across the x86 crypto library, relying entirely on boot_cpu_has() to check for features. The assumption is that x86 explicitly clears AVX and AVX-512 flags if their xfeatures are missing.

lib/crypto/x86/poly1305.h static void poly1305_emit(const struct poly1305_state *ctx, · line 147

Quoted 12 lines
lib/crypto/x86/poly1305.h +1 -2
index ee92e3740a787..b061b9926fa5d 100644
@@ -143,15 +143,12 @@ static void poly1305_emit(const struct poly1305_state *ctx,
 #define poly1305_mod_init_arch poly1305_mod_init_arch
 static void poly1305_mod_init_arch(void)
 {
-	if (boot_cpu_has(X86_FEATURE_AVX) &&
-	    cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
+	if (boot_cpu_has(X86_FEATURE_AVX))
 		static_branch_enable(&poly1305_use_avx);

[Severity: High]
Will this cause a invalid opcode panic on systems advertising AVX without XSAVE such as virtualized environments using a missing xsave cpu flag?

If XSAVE is missing natively, fpu__init_system_xstate() in arch/x86/kernel/fpu/xstate.c returns early without clearing features:

arch/x86/kernel/fpu/xstate.c:fpu__init_system_xstate() {
        ...
        if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
                pr_info("x86/fpu: x87 FPU will use %s\n",
                        boot_cpu_has(X86_FEATURE_FXSR) ? "FXSAVE" : "FSAVE");
                return;
        }
        ...
}

This early return bypasses clear_cpu_caps_with_missing_xfeatures(). Since the CPUID dependency enforcement is only triggered when a feature is explicitly cleared, X86_FEATURE_AVX is never cleared in this specific situation.

Without the cpu_has_xfeatures() check here, the crypto subsystems will see boot_cpu_has(X86_FEATURE_AVX) as true and enable AVX instructions. Because XSAVE is disabled, the OS never sets CR4.OSXSAVE=1, which can lead to a panic when AVX instructions subsequently execute.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#2 lore raw

Re: [PATCH 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits

In reply to Eric Biggers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The patch fails to clear AVX and AVX-512 CPU capabilities when the CPU natively enumerates AVX but omits XSAVE or FPU support, bypassing the new sanitization logic and leading to #UD panics.

Pre-existing issues:
- [Critical] Memory Protection Keys (PKU) isolation can be bypassed because the kernel enables X86_FEATURE_PKU without verifying the presence of XFEATURE_PKRU in xstate. --

commit 0cd2494a6ed30cd9eb21bc480a6d7d789ab47e00
Author: Eric Biggers <[email protected]>

x86/fpu: Check for missing AVX and AVX-512 xstate bits

If the CPU declares AVX or AVX-512 support, verify that the corresponding xstate bits are also set. If not, warn and clear them. This eliminates the perceived need for AVX and AVX-512 optimized code in the kernel to call cpu_has_xfeatures().

Quoted 15 lines
arch/x86/kernel/fpu/xstate.c +7 -0
index a7b6524a9dea2..7f7e62e4ebc5b 100644
@@ -799,6 +799,23 @@ static u64 __init guest_default_mask(void)
 	return ~(u64)XFEATURE_MASK_USER_DYNAMIC;
 }
 
+/* Clear any X86_FEATURE_* used by the kernel whose xfeatures are missing. */
+static void __init clear_cpu_caps_with_missing_xfeatures(u64 xfeatures)
+{
+	u64 mask;
+
+	mask = XFEATURE_MASK_FPSSE | XFEATURE_MASK_YMM;
+	if (boot_cpu_has(X86_FEATURE_AVX) && (xfeatures & mask) != mask) {

arch/x86/kernel/fpu/xstate.c static u64 __init guest_default_mask(void) · line 818

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10 -0
+		pr_err("x86/fpu: Disabling AVX support due to missing xstate features\n");
+		setup_clear_cpu_cap(X86_FEATURE_AVX);
+	}
+	mask = XFEATURE_MASK_FPSSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512;
+	if (boot_cpu_has(X86_FEATURE_AVX512F) && (xfeatures & mask) != mask) {
+		pr_err("x86/fpu: Disabling AVX-512 support due to missing xstate features\n");
+		setup_clear_cpu_cap(X86_FEATURE_AVX512F);
+	}
+}
+

[Severity: Critical]
This is a pre-existing issue, but Memory Protection Keys (PKU) isolation can be bypassed because we don't verify X86_FEATURE_PKU against the presence of XFEATURE_PKRU in xstate here.

If a hypervisor reports X86_FEATURE_PKU in CPUID but masks out the XFEATURE_PKRU xstate component, the kernel's setup_pku() enables CR4.PKE and forces X86_FEATURE_OSPKE. Because X86_FEATURE_OSPKE is artificially forced, the feature is not cleared.

CR4.PKE=1 permits userspace to use WRPKRU, but because XCR0.PKRU=0, xsaves/xrstors silently ignore the PKRU register during task context switches, leading to state leakage across tasks.

Should we also validate PKU against missing xstate components in this function to prevent isolation bypass?

Quoted 3 lines
arch/x86/kernel/fpu/xstate.c +0 -0
 /*
  * Enable and initialize the xsave feature.
  * Called once per system bootup.

[ ... ]

arch/x86/kernel/fpu/xstate.c void __init fpu__init_system_xstate(unsigned int legacy_size) · line 876

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +2 -0
@@ -855,6 +872,8 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
 		goto out_disable;
 	}
 
+	clear_cpu_caps_with_missing_xfeatures(fpu_kernel_cfg.max_features);
+

[Severity: High]
Does this logic get bypassed if XSAVE or FPU support is missing?

Earlier in fpu__init_system_xstate(), there are early returns if X86_FEATURE_FPU or X86_FEATURE_XSAVE are missing:

	if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
		pr_info("x86/fpu: x87 FPU will use %s\n",
			boot_cpu_has(X86_FEATURE_FXSR) ? "FXSAVE" : "FSAVE");
		return;
	}

If a hypervisor natively enumerates AVX=1 but XSAVE=0 (such as QEMU with -cpu host,-xsave), check_cpufeature_deps() only warns about unmet dependencies without clearing them. The lack of X86_FEATURE_XSAVE triggers the early return, completely bypassing this new capability cleanup logic.

Since the kernel will then attempt to execute VEX-encoded AVX instructions while CR4.OSXSAVE=0, will this result in an invalid opcode (#UD) panic?

Quoted 2 lines
arch/x86/kernel/fpu/xstate.c +0 -0
 	fpu_kernel_cfg.independent_features = fpu_kernel_cfg.max_features &
 					      XFEATURE_MASK_INDEPENDENT;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits

In reply to Eric Biggers
Quoted 7 lines · Eric Biggers

If the CPU declares AVX or AVX-512 support, verify that the corresponding xstate bits are also set. If not, warn and clear them.

This eliminates the perceived need for AVX and AVX-512 optimized code in the kernel to call cpu_has_xfeatures(). That has never been universally done, which strongly suggests that it has never really been needed in practice, but this should remove any remaining doubt.

I'll leave it to the x86-experts if the low-level details are right, but the model behind this makes life so much easier, thanks a lot!

#20 lore raw

Re: [PATCH 8/8] lib/raid/xor: x86: Add AVX-512 optimized xor_gen()

In reply to Eric Biggers

lib/raid/xor/x86/xor_arch.h line 16

Quoted 4 lines · Eric Biggers
lib/raid/xor/x86/xor_arch.h +4 -0
+	if (IS_ENABLED(CONFIG_X86_64) && boot_cpu_has(X86_FEATURE_AVX512F) &&
+	    !boot_cpu_has(X86_FEATURE_PREFER_YMM)) {
+		/* AVX-512 will be the best; no need to try others. */
+		/* !PREFER_YMM excludes CPUs with overly-eager downclocking. */

Can you turn this into a single block comment using full sentences? Right now the two separate comments almost feel contradictory even if I get what you mean. While you're at it also through in a blurb why we dont bother with AVX-512 (number of register, no one in the right mind would bother running high performance code on modern cpus in 32-bit mode).

Reviewed-by
#21 lore raw

Re: [PATCH 8/8] lib/raid/xor: x86: Add AVX-512 optimized xor_gen()

Quoted 12 lines · Christoph Hellwig

lib/raid/xor/x86/xor_arch.h line 16

Quoted 4 lines · Eric Biggers
lib/raid/xor/x86/xor_arch.h +4 -0
+	if (IS_ENABLED(CONFIG_X86_64) && boot_cpu_has(X86_FEATURE_AVX512F) &&
+	    !boot_cpu_has(X86_FEATURE_PREFER_YMM)) {
+		/* AVX-512 will be the best; no need to try others. */
+		/* !PREFER_YMM excludes CPUs with overly-eager downclocking. */

Can you turn this into a single block comment using full sentences? Right now the two separate comments almost feel contradictory even if I get what you mean. While you're at it also through in a blurb why we dont bother with AVX-512 (number of register, no one in the right mind would bother running high performance code on modern cpus in 32-bit mode).

Otherwise looks good, btw:

#6 lore raw

Re: [PATCH 2/8] um: Check for missing AVX and AVX-512 xstate bits

In reply to Eric Biggers
Quoted 8 lines · On Thu, 25 Jun 2026 21:37:25 -0700 Eric Biggers <[email protected]> wrote:

If the CPU declares AVX or AVX-512 support, verify that all the corresponding xstate bits are also set. If any are missing, warn and don't set the corresponding X86_FEATURE_* flags.

This eliminates the perceived need for UML-supporting AVX and AVX-512 optimized code in the kernel (that is, lib/raid/ currently) to start checking the xstate bits in addition to X86_FEATURE_AVX*.

...

arch/um/kernel/um_arch.c EXPORT_SYMBOL(task_size); · line 347

Quoted 7 lines

static void __init parse_host_cpu_flags(char *line) {

arch/um/kernel/um_arch.c +2 -0
+	u64 xcr0 = read_xcr0();
 	int i;
+
 	for (i = 0; i < 32*NCAPINTS; i++) {
 		if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))

'line' comes from /proc/cpuinfo
Surely something would be terribly wrong if that included something the kernel had disabled (or didn't support).

David

Quoted 7 lines
arch/um/kernel/um_arch.c +1 -1
-			set_cpu_cap(&boot_cpu_data, i);
+			validate_and_set_cpu_cap(i, xcr0);
 	}
 }
 
 static void __init parse_cache_line(char *line)
 {
maintainer
#7 lore raw

Re: [PATCH 2/8] um: Check for missing AVX and AVX-512 xstate bits

Quoted 27 lines · David Laight
Quoted 8 lines · On Thu, 25 Jun 2026 21:37:25 -0700 Eric Biggers <[email protected]> wrote:

If the CPU declares AVX or AVX-512 support, verify that all the corresponding xstate bits are also set. If any are missing, warn and don't set the corresponding X86_FEATURE_* flags.

This eliminates the perceived need for UML-supporting AVX and AVX-512 optimized code in the kernel (that is, lib/raid/ currently) to start checking the xstate bits in addition to X86_FEATURE_AVX*.

...

arch/um/kernel/um_arch.c EXPORT_SYMBOL(task_size); · line 347

Quoted 7 lines
  static void __init parse_host_cpu_flags(char *line)
  {
arch/um/kernel/um_arch.c +2 -0
+	u64 xcr0 = read_xcr0();
  	int i;
+
  	for (i = 0; i < 32*NCAPINTS; i++) {
  		if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))

'line' comes from /proc/cpuinfo
Surely something would be terribly wrong if that included something the kernel had disabled (or didn't support).

David

Quoted 11 lines

arch/um/kernel/um_arch.c EXPORT_SYMBOL(task_size); · line 353

Quoted 7 lines
arch/um/kernel/um_arch.c +1 -1
-			set_cpu_cap(&boot_cpu_data, i);
+			validate_and_set_cpu_cap(i, xcr0);
  	}
  }
 
  static void __init parse_cache_line(char *line)
  {

Lots of other stuff will go wrong before that. Glibc, things compiled with LLVM, python, perl, etc.

Half of the userland will go belly up, because AVX is used in string operations and hashing if it is available.

UML is just another userland application from this perspective, so there is no reason for it to behave any different from the rest of the userland.

Signature · 2 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
#8 lore raw

Re: [PATCH 2/8] um: Check for missing AVX and AVX-512 xstate bits

Quoted 39 lines · On Fri, 26 Jun 2026 09:21:49 +0100 Anton Ivanov…
Quoted 27 lines · David Laight
Quoted 8 lines · On Thu, 25 Jun 2026 21:37:25 -0700 Eric Biggers <[email protected]> wrote:

If the CPU declares AVX or AVX-512 support, verify that all the corresponding xstate bits are also set. If any are missing, warn and don't set the corresponding X86_FEATURE_* flags.

This eliminates the perceived need for UML-supporting AVX and AVX-512 optimized code in the kernel (that is, lib/raid/ currently) to start checking the xstate bits in addition to X86_FEATURE_AVX*.

...

arch/um/kernel/um_arch.c EXPORT_SYMBOL(task_size); · line 347

Quoted 7 lines
  static void __init parse_host_cpu_flags(char *line)
  {
arch/um/kernel/um_arch.c +2 -0
+	u64 xcr0 = read_xcr0();
  	int i;
+
  	for (i = 0; i < 32*NCAPINTS; i++) {
  		if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))  

'line' comes from /proc/cpuinfo
Surely something would be terribly wrong if that included something the kernel had disabled (or didn't support).

David

Quoted 11 lines

arch/um/kernel/um_arch.c EXPORT_SYMBOL(task_size); · line 353

Quoted 7 lines
arch/um/kernel/um_arch.c +1 -1
-			set_cpu_cap(&boot_cpu_data, i);
+			validate_and_set_cpu_cap(i, xcr0);
  	}
  }
 
  static void __init parse_cache_line(char *line)
  {  
Quoted 3 lines

Lots of other stuff will go wrong before that. Glibc, things compiled with LLVM, python, perl, etc.

Half of the userland will go belly up, because AVX is used in string operations and hashing if it is available.

And glibc will check xcr0.

Quoted 2 lines

UML is just another userland application from this perspective, so there is no reason for it to behave any different from the rest of the userland.

author maintainer
#9 lore raw

Re: [PATCH 2/8] um: Check for missing AVX and AVX-512 xstate bits

Quoted 3 lines · David Laight
Quoted 3 lines

UML is just another userland application from this perspective, so there is no reason for it to behave any different from the rest of the userland.

Which is why it should do the XCR0 check that the vast majority of userspace programs do, right? It has always been part of the documented way to detect AVX and AVX-512 support.

(I think this helps explain why LLMs notice this too. They've been trained on lots of code that does it correctly.)

That being said, it does seem likely that it's basically obsolete now. So maybe we could take a shortcut and omit it.

The important thing is really that we make a definitive decision *once* for each of UML and native x86. The status quo is that the decision is instead punted to every individual AVX optimized function in the kernel, which isn't working well.

- Eric

#10 lore raw

Re: [PATCH 2/8] um: Check for missing AVX and AVX-512 xstate bits

Quoted 8 lines · On Fri, 26 Jun 2026 20:55:44 +0000 Eric Biggers <[email protected]> wrote:
Quoted 3 lines · David Laight
Quoted 3 lines

UML is just another userland application from this perspective, so there is no reason for it to behave any different from the rest of the userland.

Which is why it should do the XCR0 check that the vast majority of userspace programs do, right? It has always been part of the documented way to detect AVX and AVX-512 support.

That and looking at the other 'cpuid' registers rather than reading /proc/cpuinfo.
If you want to run 'um' on (say) NetBSD you'd need to do it differently.

Quoted 5 lines

(I think this helps explain why LLMs notice this too. They've been trained on lots of code that does it correctly.)

That being said, it does seem likely that it's basically obsolete now. So maybe we could take a shortcut and omit it.

It would need the linux kernel to report a cpu feature that needed kernel support, but that the kernel didn't support.

But for things like popcnt um should probably check the relevant cpuid bit rather than scanning /proc/cpuinfo.

Quoted 5 lines

The important thing is really that we make a definitive decision *once* for each of UML and native x86. The status quo is that the decision is instead punted to every individual AVX optimized function in the kernel, which isn't working well.

Indeed.

David

Quoted 2 lines

- Eric

maintainer
#3 lore raw

Re: [PATCH 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits

In reply to [email protected]
Quoted 10 lines · On Fri, Jun 26, 2026 at 05:00:37AM +0000, [email protected] wrote:

[Severity: Critical]
This is a pre-existing issue, but Memory Protection Keys (PKU) isolation can be bypassed because we don't verify X86_FEATURE_PKU against the presence of XFEATURE_PKRU in xstate here.

If a hypervisor reports X86_FEATURE_PKU in CPUID but masks out the XFEATURE_PKRU xstate component, the kernel's setup_pku() enables CR4.PKE and forces X86_FEATURE_OSPKE. Because X86_FEATURE_OSPKE is artificially forced, the feature is not cleared.

Quoted 6 lines

CR4.PKE=1 permits userspace to use WRPKRU, but because XCR0.PKRU=0, xsaves/xrstors silently ignore the PKRU register during task context switches, leading to state leakage across tasks.

Should we also validate PKU against missing xstate components in this function to prevent isolation bypass?

I *think* Sashiko means here our preexisting fpu__init_system_xstate() here but I'm not sure...

Quoted 6 lines

[Severity: High]
Does this logic get bypassed if XSAVE or FPU support is missing?

Earlier in fpu__init_system_xstate(), there are early returns if X86_FEATURE_FPU or X86_FEATURE_XSAVE are missing:

Quoted 8 lines
	if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
		pr_info("x86/fpu: x87 FPU will use %s\n",
			boot_cpu_has(X86_FEATURE_FXSR) ? "FXSAVE" : "FSAVE");
		return;
	}

If a hypervisor natively enumerates AVX=1 but XSAVE=0 (such as QEMU with -cpu host,-xsave), check_cpufeature_deps() only warns about unmet

Yah, we don't care. We won't support non-sensical guest configs.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette