x86: Remove cpu_has_xfeatures() and add AVX-512 xor_gen()
Subsystems (6)
Eric Biggers
maintainerv4 cover x86: Remove cpu_has_xfeatures() and add AVX-512 xor_gen()
This series consolidates and cleans up how the kernel handles CPU feature flags for AVX and AVX-512 when the underlying OS or hypervisor (if any) doesn't enable the required xstate features in XCR0.
Specifically, it makes the checks for xstate features be done in a single place at boot time for both UML and native x86, instead of everywhere kernel code wants to check for AVX or AVX-512 support.
Patch 8 then adds an AVX-512 optimized implementation of xor_gen() for RAID, which had been blocked on confusion around cpu_has_xfeatures() and its lack of implementation on UML.
Changed in v4:
- Added missing vzeroupper instructions in xor-avx512.c
Changed in v3:
- Updated the implementation of fpu__init_system_xstate() as suggested by Borislav
- Added Acked-by tags
- Used "xor:" commit subject
Changed in v2:
- Made x86 clear the AVX* bits if FPU or XSAVE features are missing.
- Updated comment in x86 arch_xor_init().
- Updated commit message for the UML commit to clarify that checking XCR0 is standard practice in userspace
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()
xor: Remove redundant X86_FEATURE_OSXSAVE check
xor: 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 | 69 +++++------- 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 | 125 +++++++++++++++++++++ lib/raid/xor/x86/xor_arch.h | 31 +++-- 29 files changed, 279 insertions(+), 196 deletions(-) create mode 100644 lib/raid/xor/x86/xor-avx512.c
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v4 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 | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
arch/x86/kernel/fpu/xstate.c +23 -2
@@ -806,7 +806,7 @@ static u64 __init guest_default_mask(void)void __init fpu__init_system_xstate (unsigned int legacy_size) {unsigned int eax, ebx, ecx, edx; -u64 xfeatures; +u64 xfeatures, mask;int err;int i;
@@ -818,6 +818,8 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)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" ); +/* Disable all dependent flags too */ +setup_clear_cpu_cap (X86_FEATURE_XSAVE );return ; }
@@ -833,7 +835,8 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)cpuid_count (CPUID_LEAF_XSTATE ,1 , &eax, &ebx, &ecx, &edx); fpu_kernel_cfg.max_features |= ecx + ((u64 )edx <<32 ); -if ((fpu_kernel_cfg.max_features &XFEATURE_MASK_FPSSE ) !=XFEATURE_MASK_FPSSE ) { + mask =XFEATURE_MASK_FPSSE ; +if ((fpu_kernel_cfg.max_features & mask) != mask) {/* * This indicates that something really unexpected happened * with the enumeration. Disable XSAVE and try to continue
@@ -844,6 +847,24 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) goto out_disable; } + mask |=XFEATURE_MASK_YMM ; +if (boot_cpu_has (X86_FEATURE_AVX )) { +if ((fpu_kernel_cfg.max_features & mask) != mask) { +pr_err (FW_BUG +"x86/fpu: Disabling AVX support due to missing xstate features\n" ); +setup_clear_cpu_cap (X86_FEATURE_AVX ); + } + } + + mask |=XFEATURE_MASK_AVX512 ; +if (boot_cpu_has (X86_FEATURE_AVX512F )) { +if ((fpu_kernel_cfg.max_features & mask) != mask) { +pr_err (FW_BUG +"x86/fpu: Disabling AVX-512 support due to missing xstate features\n" ); +setup_clear_cpu_cap (X86_FEATURE_AVX512F ); + } + } +if (fpu_kernel_cfg.max_features &XFEATURE_MASK_APX && fpu_kernel_cfg.max_features & (XFEATURE_MASK_BNDREGS |XFEATURE_MASK_BNDCSR )) {/*
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v4 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A malformed CPUID reporting AVX512F=1 but AVX=0 will cause a #GP exception during xsetbv. --
--- Patch [1]: [PATCH v4 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits --- commit 7ad6682a0d9e6a99cd138d1dc9dfbd430c1da651
Author: Eric Biggers <[email protected]>
x86/fpu: Check for missing AVX and AVX-512 xstate bits
This patch checks if the CPU declares AVX or AVX-512 support, and verifies that the corresponding xstate bits are also set. If not, it warns and clears the CPU capabilities.
Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +0
-0
[ ... ]
Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +2
-0@@ -844,6 +847,24 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) goto out_disable; } + mask |=XFEATURE_MASK_YMM ; +if (boot_cpu_has (X86_FEATURE_AVX )) {
arch/x86/kernel/fpu/xstate.c void __init fpu__init_system_xstate(unsigned int legacy_size) · line 861
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +10
-0+if ((fpu_kernel_cfg.max_features & mask) != mask) { +pr_err (FW_BUG +"x86/fpu: Disabling AVX support due to missing xstate features\n" ); +setup_clear_cpu_cap (X86_FEATURE_AVX ); + } + } + + mask |=XFEATURE_MASK_AVX512 ; +if (boot_cpu_has (X86_FEATURE_AVX512F )) { +if ((fpu_kernel_cfg.max_features & mask) != mask) {
[Severity: High]
This is a pre-existing issue, but does this logic handle a malformed CPUID reporting AVX512F=1 but AVX=0 correctly?
If the xstate leaf reports both YMM and AVX512 support, the AVX validation block above is skipped because AVX=0. Since mask accumulates both YMM and AVX512, the check here passes, so X86_FEATURE_AVX512F is not cleared.
Later in fpu__init_system_xstate(), the xsave_cpuid_features loop:
for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) {
unsigned short cid = xsave_cpuid_features[i];
/* Careful: X86_FEATURE_FPU is 0! */
if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid))
fpu_kernel_cfg.max_features &= ~BIT_ULL(i);
}will clear the YMM bit from max_features because AVX=0, but will retain the AVX512 bits because AVX512F=1.
Could this result in passing an invalid xstate combination (AVX512 without YMM) to xsetbv, triggering a #GP exception during boot?
Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +6
-0+pr_err (FW_BUG +"x86/fpu: Disabling AVX-512 support due to missing xstate features\n" ); +setup_clear_cpu_cap (X86_FEATURE_AVX512F ); + } + } +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Eric Biggers
maintainerPATCH v4 2/8 um: Check for missing AVX and AVX-512 xstate bits
In reply to Eric BiggersIf the CPU declares AVX or AVX-512 support, verify that all the corresponding bits are set in the XCR0 register. 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 XCR0 bits in addition to X86_FEATURE_AVX*.
This aligns UML with the vast majority of userspace programs, which check the XCR0 bits before considering AVX and AVX-512 to be supported, as per the procedure documented in Intel's CPU manual.
--- arch/um/kernel/um_arch.c | 78 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
arch/um/kernel/um_arch.c +77 -1
@@ -264,12 +264,88 @@ unsigned long brk_start;#define MIN_VMALLOC (32 * 1024 * 1024) +static u64 __init read_xcr0 (void ) +{ +u32 a, b, c, d; + + asmvolatile ("cpuid" + :"=a" (a),"=b" (b),"=c" (c),"=d" (d) + :"a" (0 ),"c" (0 )); +if (a >=1 ) {/* max_leaf >= 1 */ + asmvolatile ("cpuid" + :"=a" (a),"=b" (b),"=c" (c),"=d" (d) + :"a" (1 ),"c" (0 )); +if (c & (1 <<27 )) {/* XSAVE enabled by OS */ + asmvolatile ("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); } }
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v4 3/8 crypto: x86 - Stop using cpu_has_xfeatures()
In reply to Eric BiggersChecking 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
@@ -265,8 +265,7 @@ 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
@@ -1548,8 +1548,7 @@ static int __init register_avx_algs(void)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));
@@ -1562,9 +1561,7 @@ static int __init register_avx_algs(void)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 )) {
arch/x86/crypto/aria_aesni_avx2_glue.c +1 -10
@@ -195,22 +195,13 @@ 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;
arch/x86/crypto/aria_aesni_avx_glue.c +1 -10
@@ -182,21 +182,12 @@ 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;
arch/x86/crypto/aria_gfni_avx512_glue.c +1 -10
@@ -196,24 +196,15 @@ 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;
arch/x86/crypto/camellia_aesni_avx2_glue.c +1 -10
@@ -97,22 +97,13 @@ 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)); }
arch/x86/crypto/camellia_aesni_avx_glue.c +1 -10
@@ -98,21 +98,12 @@ 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)); }
arch/x86/crypto/cast5_avx_glue.c +2 -5
@@ -92,11 +92,8 @@ 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 ; }
arch/x86/crypto/cast6_avx_glue.c +2 -5
@@ -92,11 +92,8 @@ 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 ; }
arch/x86/crypto/serpent_avx2_glue.c +1 -8
@@ -93,17 +93,10 @@ 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
@@ -100,11 +100,8 @@ 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 ; }
arch/x86/crypto/sm4_aesni_avx2_glue.c +1 -10
@@ -98,22 +98,13 @@ 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)); }
arch/x86/crypto/sm4_aesni_avx_glue.c +1 -10
@@ -314,21 +314,12 @@ 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)); }
arch/x86/crypto/twofish_avx_glue.c +2 -4
@@ -102,10 +102,8 @@ 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 ; }
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v4 4/8 lib/crypto: x86: Stop using cpu_has_xfeatures()
In reply to Eric BiggersChecking 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
@@ -55,8 +55,6 @@ static void blake2s_mod_init_arch(void)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
@@ -165,8 +165,7 @@ static void chacha_mod_init_arch(void)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) &&
lib/crypto/x86/nh.h +1 -3
@@ -37,9 +37,7 @@ 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
@@ -143,15 +143,12 @@ static void poly1305_emit(const struct poly1305_state *ctx,#define poly1305_mod_init_arch poly1305_mod_init_archstatic 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
@@ -59,9 +59,7 @@ 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 ))
lib/crypto/x86/sha256.h +1 -3
@@ -104,9 +104,7 @@ static void sha256_mod_init_arch(void) 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,
lib/crypto/x86/sha512.h +1 -2
@@ -37,8 +37,7 @@ static void sha512_blocks(struct sha512_block_state *state,#define sha512_mod_init_arch sha512_mod_init_archstatic 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,
lib/crypto/x86/sm3.h +1 -2
@@ -33,7 +33,6 @@ static void sm3_blocks(struct sm3_block_state *state,#define sm3_mod_init_arch sm3_mod_init_archstatic 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.55.0
Eric Biggers
maintainerPATCH v4 5/8 lib/crc: x86: Stop using cpu_has_xfeatures()
In reply to Eric BiggersChecking 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
@@ -27,16 +27,14 @@ 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 ); }/*
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v4 6/8 x86/fpu: Remove cpu_has_xfeatures()
In reply to Eric BiggersThe 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
@@ -99,15 +99,6 @@ static inline void fpregs_assert_state_consistent(void) { }*/ 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);
arch/x86/kernel/fpu/xstate.c +3 -41
64 lines not on this page · raw message
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v4 7/8 xor: Remove redundant X86_FEATURE_OSXSAVE check
In reply to Eric BiggersX86_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
@@ -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 )) {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
2.55.0
Eric Biggers
maintainerPATCH v4 8/8 xor: Add AVX-512 optimized xor_gen()
In reply to Eric BiggersAdd 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 | 125 ++++++++++++++++++++++++++++++++++ lib/raid/xor/x86/xor_arch.h | 30 +++++--- 3 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 lib/raid/xor/x86/xor-avx512.c
lib/raid/xor/Makefile +1 -1
@@ -29,7 +29,7 @@ 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)
lib/raid/xor/x86/xor-avx512.c new file +125 -0
125 lines not on this page · raw message
lib/raid/xor/x86/xor_arch.h +20 -10
@@ -6,21 +6,31 @@ 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 )) { +/* +* Use the AVX-512 code on CPUs that support AVX-512 without +* overly-eager downclocking. On such CPUs the AVX-512 code +* should always work at least as well as the AVX code, so +* runtime selection is unnecessary. +* +* The AVX-512 code can work on X86_32. However, due to lack of +* use case for that, for now it's built only for X86_64. +*/ +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 )) {
Signature · 1 line
2.55.0