x86: Remove cpu_has_xfeatures() and add AVX-512 xor_gen()
Subsystems (6)
Eric Biggers
maintainerv2 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 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()
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 | 65 ++++------- 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 | 31 ++++-- 29 files changed, 273 insertions(+), 194 deletions(-) create mode 100644 lib/raid/xor/x86/xor-avx512.c
base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 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 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
arch/x86/kernel/fpu/xstate.c +21 -0
@@ -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) { +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.
@@ -812,12 +829,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)if (!boot_cpu_has (X86_FEATURE_FPU )) {pr_info ("x86/fpu: No FPU detected\n" ); +clear_cpu_caps_with_missing_xfeatures (0 );return ; }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" ); +clear_cpu_caps_with_missing_xfeatures (0 );return ; }
@@ -855,6 +874,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); + fpu_kernel_cfg.independent_features = fpu_kernel_cfg.max_features &XFEATURE_MASK_INDEPENDENT ;
Signature · 1 line
2.55.0
Borislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 4 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
s/This eliminates/Eliminate/
Quoted 14 lines
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.Signed-off-by Eric Biggers <[email protected]>--- arch/x86/kernel/fpu/xstate.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)arch/x86/kernel/fpu/xstate.c +0
-0
arch/x86/kernel/fpu/xstate.c static u64 __init guest_default_mask(void) · line 803
Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +2
-0@@ -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)
That function name is a bit too long. How about:
clear_cpu_caps_xft()
or so.
Quoted 20 lines
arch/x86/kernel/fpu/xstate.c +15
-0+{ +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. @@ -812,12 +829,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
arch/x86/kernel/fpu/xstate.c void __init fpu__init_system_xstate(unsigned int legacy_size) · line 839
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +2
-0if (!boot_cpu_has (X86_FEATURE_FPU )) {pr_info ("x86/fpu: No FPU detected\n" ); +clear_cpu_caps_with_missing_xfeatures (0 );return ; }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" ); +clear_cpu_caps_with_missing_xfeatures (0 );
Also, I'm not really clear on the usage here: if the CPU doesn't have FPU or XSAVE, we pass in xfeature 0 which is XFEATURE_FP in both cases. And then we clear AVX and AVX-512.
The 0 is basically forcing the checks in the function to match, i.e., it looks to me like we're defining a new interface but then we're misusing it so that those basic CPU flags are cleared.
What are we even protecting against here?
AVX and AVX-512 code needs to check whether it has FPU and XSAVE support?
I.e., we're protecting against some weird guests?
I wanna say, we should not protect but let them crash'n'burn in big big flames which can be seen from a mile away.
Or do you have a sensible use case in mind which we really wanna protect against and this all actually makes sense?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 17 lines · Borislav Petkov
arch/x86/kernel/fpu/xstate.c void __init fpu__init_system_xstate(unsigned int legacy_size) · line 839
Quoted 12 lines
arch/x86/kernel/fpu/xstate.c +2
-0@@ -812,12 +829,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)if (!boot_cpu_has (X86_FEATURE_FPU )) {pr_info ("x86/fpu: No FPU detected\n" ); +clear_cpu_caps_with_missing_xfeatures (0 );return ; }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" ); +clear_cpu_caps_with_missing_xfeatures (0 );Also, I'm not really clear on the usage here: if the CPU doesn't have FPU or XSAVE, we pass in xfeature 0 which is
XFEATURE_FPin both cases. And then we clear AVX and AVX-512.
Quoted 3 lines
The 0 is basically forcing the checks in the function to match, i.e., it looks to me like we're defining a new interface but then we're misusing it so that those basic CPU flags are cleared.
*xfeatures* 0, not *xfeature* 0. It's a bitmask.
Quoted 11 lines
What are we even protecting against here?
AVX and AVX-512 code needs to check whether it has FPU and XSAVE support?
I.e., we're protecting against some weird guests?
I wanna say, we should not protect but let them crash'n'burn in big big flames which can be seen from a mile away.
Or do you have a sensible use case in mind which we really wanna protect against and this all actually makes sense?
Hypervisors that set AVX CPUID bits without their prerequisite xfeatures. Or the host OS, in the case of the UML patch.
There are about 25 sites where kernel code is checking for this. This is just checking it at the architectural level instead.
We could try taking the position that this is not supported at all and just delete all the cpu_has_xfeatures() checks without replacing them with anything, and see what bug reports we receive (other than all the AI ones which we'd need to just ignore). This would mean just taking patches 3-8. Is that what you'd prefer?
- Eric
Borislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 1 line · Eric Biggers
*xfeatures* 0, not *xfeature* 0. It's a bitmask.
That doesn't make it more clear. It does "clear cpu caps with missing xfeatures 0". I can't parse that code, maybe I don't see it yet.
Quoted 2 lines
Hypervisors that set AVX CPUID bits without their prerequisite xfeatures.
Yeah, those are the ones I don't care about.
Quoted 1 line
Or the host OS, in the case of the UML patch.
That was because UML didn't have cpu_has_xfeatures(), right?
Quoted 2 lines
There are about 25 sites where kernel code is checking for this. This is just checking it at the architectural level instead.
So tglx mentioned another case - firmware. Apparently we can have firmware which fumbles the configuration of those bits and I can very much see that happening - it is firmware, doh, ofc.
Quoted 5 lines
We could try taking the position that this is not supported at all and just delete all the
cpu_has_xfeatures()checks without replacing them with anything, and see what bug reports we receive (other than all the AI ones which we'd need to just ignore). This would mean just taking patches 3-8. Is that what you'd prefer?
I had another idea this morning: towards the end of fpu__init_system_xstate(), we have figured out everything we support, components, buffer sizes, etc. There we also do a paranoia check on the xfeatures mask.
How about we go through the final xfeatures mask and then clear bits which are wrong?
This way we'll have all the checking done in one place, if checks pass we can say the machine is ok to do xstate handling and then we can drop all other checks around the tree.
How does that sound?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Eric Biggers
*xfeatures* 0, not *xfeature* 0. It's a bitmask.
That doesn't make it more clear. It does "clear cpu caps with missing xfeatures 0". I can't parse that code, maybe I don't see it yet.
The argument is the bitmask of xfeatures that are present. 0 is an empty bitmask, i.e. no xfeatures are present.
I can rename it to xfeatures_present and/or document the argument explicitly in the function comment, if that would help.
Quoted 8 lines
Quoted 2 lines
Hypervisors that set AVX CPUID bits without their prerequisite xfeatures.
Yeah, those are the ones I don't care about.
Quoted 1 line
Or the host OS, in the case of the UML patch.
That was because UML didn't have
cpu_has_xfeatures(), right?
I'm not sure what this question is asking, but UML indeed doesn't implement cpu_has_xfeatures(). Currently most of the AVX optimized code isn't enabled on UML, which avoids this limitation. The RAID code is different though, as it's enabled on UML.
Quoted 17 lines
Quoted 2 lines
There are about 25 sites where kernel code is checking for this. This is just checking it at the architectural level instead.
So tglx mentioned another case - firmware. Apparently we can have firmware which fumbles the configuration of those bits and I can very much see that happening - it is firmware, doh, ofc.
Quoted 5 lines
We could try taking the position that this is not supported at all and just delete all the
cpu_has_xfeatures()checks without replacing them with anything, and see what bug reports we receive (other than all the AI ones which we'd need to just ignore). This would mean just taking patches 3-8. Is that what you'd prefer?I had another idea this morning: towards the end of
fpu__init_system_xstate(), we have figured out everything we support, components, buffer sizes, etc. There we also do a paranoia check on the xfeatures mask.
Quoted 8 lines
How about we go through the final xfeatures mask and then clear bits which are wrong?
This way we'll have all the checking done in one place, if checks pass we can say the machine is ok to do xstate handling and then we can drop all other checks around the tree.
How does that sound?
First, the code being added *is* to fpu__init_system_xstate(). Do you think it needs to be moved later in the function, and if so why?
Second, clearing xfeatures is the opposite of what's needed.
(It's also already there. See loop with the comment "Clear XSAVE features that are disabled in the normal CPUID".)
What's needed is clearing X86_FEATURE_* flags based on missing xfeatures.
- Eric
Borislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 2 lines · Eric Biggers
The argument is the bitmask of xfeatures that are present. 0 is an empty bitmask, i.e. no xfeatures are present.
Ok, so what does that mean when you see it in the code:
clear_cpu_caps_with_missing_xfeatures(0)
clear the CPU caps with missing xfeatures bitmask 0?
Quoted 2 lines
I can rename it to
xfeatures_presentand/or document the argument explicitly in the function comment, if that would help.
I guess. I like the documentation aspect.
Quoted 4 lines
I'm not sure what this question is asking, but UML indeed doesn't implement
cpu_has_xfeatures(). Currently most of the AVX optimized code isn't enabled on UML, which avoids this limitation. The RAID code is different though, as it's enabled on UML.
You brought up the UML example upthread. I'm trying to figure out why we care about the UML case. I'm also assuming that case will be fixed with whatever we end up doing here.
Quoted 2 lines
First, the code being added *is* to
fpu__init_system_xstate(). Do you think it needs to be moved later in the function, and if so why?
Lemme try again:
there where we sanity check the xfeatures mask, at that same place we can sanity check the X86_FEATURE flags we have set in our internal representation of CPUID flags. At exactly the same place. And we warn and clear the ones are wrong.
Quoted 1 line
Second, clearing xfeatures is the opposite of what's needed.
I meant clearing X86_FEATURE flags. Basically what you started doing.
All I'm proposing is having the *whole* X86_FEATURE clearing concentrated in a single function so that it is called once per CPU and then we're done.
That's it.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 9 lines · Borislav Petkov
Quoted 2 lines · Eric Biggers
The argument is the bitmask of xfeatures that are present. 0 is an empty bitmask, i.e. no xfeatures are present.
Ok, so what does that mean when you see it in the code:
clear_cpu_caps_with_missing_xfeatures(0)clear the CPU caps with missing xfeatures bitmask 0?
It clears the CPU caps whose xfeature dependencies are missing from the given mask, which denotes the present xfeatures. An empty mask means nothing present, i.e. everything is missing.
It could be a separate function that clears unconditionally, but then the clearing and the log messages would need to be duplicated.
I don't know what your suggestion is. Are you still asking for the function to be renamed to clear_cpu_caps_xft()? Note that the abbreviation "xft" doesn't appear anywhere else in arch/x86/. Maybe you want the argument inverted? I don't know what you want, sorry.
Quoted 3 lines
You brought up the UML example upthread. I'm trying to figure out why we care about the UML case. I'm also assuming that case will be fixed with whatever we end up doing here.
The UML update is in this series. See patch 2. Maybe you don't care about UML but many people do. kunit.py uses it by default, for example.
Quoted 9 lines
Quoted 2 lines
First, the code being added *is* to
fpu__init_system_xstate(). Do you think it needs to be moved later in the function, and if so why?Lemme try again:
there where we sanity check the xfeatures mask, at that same place we can sanity check the
X86_FEATUREflags we have set in our internal representation of CPUID flags. At exactly the same place. And we warn and clear the ones are wrong.
This is basically what this patch does already:
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.
*/
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);
}Maybe you want it moved down one statement so that it's immediately adjacent to the loop?
Or maybe you want it to be integrated into the loop itself? It's doable, but a bit awkward because the two CPU features we'd like to clear each depend on multiple xfeatures rather than just one. And also there are those two cases earlier in the function (XSAVE or FPU being disabled) that do an early return and never reach here. It's nice to use the same helper function in those cases.
Quoted 6 lines
Quoted 1 line
Second, clearing xfeatures is the opposite of what's needed.
I meant clearing
X86_FEATUREflags. Basically what you started doing.All I'm proposing is having the *whole*
X86_FEATUREclearing concentrated in a single function so that it is called once per CPU and then we're done.
fpu__init_system_xstate() is already a function called once on the boot CPU. Is your suggestion that the feature clearing should be once per CPU instead of just on the boot CPU?
- Eric
Borislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 4 lines · Eric Biggers
I don't know what your suggestion is. Are you still asking for the function to be renamed to
clear_cpu_caps_xft()? Note that the abbreviation "xft" doesn't appear anywhere else inarch/x86/. Maybe you want the argument inverted? I don't know what you want, sorry.
That's fine - I'll take a look at the final version after having gone through the whole pile and then holler if I still see a need.
Quoted 2 lines
The UML update is in this series. See patch 2. Maybe you don't care about UML but many people do.
kunit.pyuses it by default, for example.
So I've heard.
Quoted 3 lines
fpu__init_system_xstate()is already a function called once on the boot CPU. Is your suggestion that the feature clearing should be once per CPU instead of just on the boot CPU?
Yes, we need it to run on every CPU if we're going to enforce the same xfeatures and CPUID bits configuration everywhere.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Eric Biggers
I don't know what your suggestion is. Are you still asking for the function to be renamed to
clear_cpu_caps_xft()? Note that the abbreviation "xft" doesn't appear anywhere else inarch/x86/. Maybe you want the argument inverted? I don't know what you want, sorry.That's fine - I'll take a look at the final version after having gone through the whole pile and then holler if I still see a need.
Ok, here's something ontop of yours. It is tested only in a guest but it should show the intention.
The first call in:
if (!boot_cpu_has(X86_FEATURE_FPU)) {is not needed because that is caught earlier:
[ 0.553199] x86/fpu: Giving up, no FPU found and no math emulation present
and we stop there.
The second one can then simply do:
/* Disable all dependent flags too */
setup_clear_cpu_cap(X86_FEATURE_XSAVE);because that'll clear all dependent flags and we should be good there:
[ 0.560698] do_clear_cpu_cap: clearing 10:0 (xsaveopt) [ 0.561697] do_clear_cpu_cap: clearing 10:1 (xsavec) [ 0.562697] do_clear_cpu_cap: clearing 10:3 (xsaves) [ 0.563697] do_clear_cpu_cap: clearing 4:28 (avx) [ 0.564697] do_clear_cpu_cap: clearing 16:3 (pku) [ 0.565697] do_clear_cpu_cap: clearing 9:14 (mpx) [ 0.566697] do_clear_cpu_cap: clearing 10:2 (xgetbv1) [ 0.567697] do_clear_cpu_cap: clearing 21:9 ((null)) [ 0.568698] do_clear_cpu_cap: clearing 12:4 (avx_vnni) [ 0.569697] do_clear_cpu_cap: clearing 4:12 (fma) [ 0.570697] do_clear_cpu_cap: clearing 16:9 (vaes) [ 0.571697] do_clear_cpu_cap: clearing 16:10 (vpclmulqdq) [ 0.572697] do_clear_cpu_cap: clearing 9:5 (avx2) [ 0.573697] do_clear_cpu_cap: clearing 9:16 (avx512f) [ 0.574697] do_clear_cpu_cap: clearing 9:21 (avx512ifma) [ 0.575698] do_clear_cpu_cap: clearing 9:26 (avx512pf) [ 0.576697] do_clear_cpu_cap: clearing 9:27 (avx512er) [ 0.577697] do_clear_cpu_cap: clearing 9:28 (avx512cd) [ 0.578697] do_clear_cpu_cap: clearing 9:17 (avx512dq) [ 0.579697] do_clear_cpu_cap: clearing 9:30 (avx512bw) [ 0.580697] do_clear_cpu_cap: clearing 9:31 (avx512vl) [ 0.581697] do_clear_cpu_cap: clearing 16:1 (avx512vbmi) [ 0.582697] do_clear_cpu_cap: clearing 16:6 (avx512_vbmi2) [ 0.583697] do_clear_cpu_cap: clearing 16:11 (avx512_vnni) [ 0.584697] do_clear_cpu_cap: clearing 16:12 (avx512_bitalg) [ 0.585697] do_clear_cpu_cap: clearing 18:2 (avx512_4vnniw) [ 0.586697] do_clear_cpu_cap: clearing 18:3 (avx512_4fmaps) [ 0.587697] do_clear_cpu_cap: clearing 16:14 (avx512_vpopcntdq) [ 0.588698] do_clear_cpu_cap: clearing 18:8 (avx512_vp2intersect) [ 0.589698] do_clear_cpu_cap: clearing 12:5 (avx512_bf16) [ 0.590697] do_clear_cpu_cap: clearing 18:23 (avx512_fp16) [ 0.592697] do_clear_cpu_cap: clearing 16:29 (enqcmd) [ 0.593697] do_clear_cpu_cap: clearing 10:4 ((null)) [ 0.594697] do_clear_cpu_cap: clearing 18:24 (amx_tile) [ 0.595697] do_clear_cpu_cap: clearing 12:21 ((null)) [ 0.596697] do_clear_cpu_cap: clearing 18:22 (amx_bf16) [ 0.597697] do_clear_cpu_cap: clearing 18:25 (amx_int8) [ 0.598697] do_clear_cpu_cap: clearing 16:7 ((null))
and then the rest is streamlined into fpu__init_system_xstate() where all sanity checking should be performed, as I mentioned intially.
Full diff ontop:
arch/x86/kernel/cpu/cpuid-deps.c +2 -0
@@ -142,6 +142,8 @@ static void do_clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)continue ; changed = true; +pr_info ("%s: clearing %d:%d (%s)\n" , + __func__, d->feature >>5 , d->feature &31 , x86_cap_flags[d->feature]);clear_feature (c, d->feature); } }while (changed);
arch/x86/kernel/fpu/xstate.c +21 -23
85 lines not on this page · raw message
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 15 lines · Borislav Petkov
Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Eric Biggers
I don't know what your suggestion is. Are you still asking for the function to be renamed to
clear_cpu_caps_xft()? Note that the abbreviation "xft" doesn't appear anywhere else inarch/x86/. Maybe you want the argument inverted? I don't know what you want, sorry.That's fine - I'll take a look at the final version after having gone through the whole pile and then holler if I still see a need.
Ok, here's something ontop of yours. It is tested only in a guest but it should show the intention.
The first call in:
Quoted 7 lines
if (!boot_cpu_has(X86_FEATURE_FPU)) {is not needed because that is caught earlier:
[ 0.553199] x86/fpu: Giving up, no FPU found and no math emulation presentand we stop there.
Interesting, so the !boot_cpu_has(X86_FEATURE_FPU) check is dead code since ab05214025ee ("x86/fpu: Remove MATH_EMULATION and related glue code"). Sure, there's no need to add anything to there then, though we should do a separate patch that removes it.
Quoted 6 lines
The second one can then simply do:
/* Disable all dependent flags too */ setup_clear_cpu_cap(X86_FEATURE_XSAVE);because that'll clear all dependent flags and we should be good there:
Sure, that works. I'm not sure it's actually easier to understand than what I proposed; it's kind of contradictory to call "setup_clear_cpu_cap(X86_FEATURE_XSAVE)" when we just verified it is clear. But the comment mentioning that it's actually for clearing dependent flags helps. If you prefer this way, let's do this.
Quoted 2 lines
and then the rest is streamlined into
fpu__init_system_xstate()where all sanity checking should be performed, as I mentioned intially.
It's functionally equivalent, but yes we can write it this way if you prefer. Is it okay if I fold your diff (minus the pr_info()) into this patch? (I'll also add FW_BUG to the error messages as Thomas requested.)
- Eric
Borislav Petkov
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
Quoted 4 lines · Eric Biggers
Interesting, so the !boot_cpu_has(X86_FEATURE_FPU) check is dead code since
ab05214025ee("x86/fpu: RemoveMATH_EMULATIONand related glue code"). Sure, there's no need to add anything to there then, though we should do a separate patch that removes it.
Sure.
Quoted 5 lines
Sure, that works. I'm not sure it's actually easier to understand than what I proposed; it's kind of contradictory to call "
setup_clear_cpu_cap(X86_FEATURE_XSAVE)" when we just verified it is clear. But the comment mentioning that it's actually for clearing dependent flags helps. If you prefer this way, let's do this.
Yes please, that's why I added the comment. And we have this machinery in do_clear_cpu_cap() so let's use it.
Quoted 4 lines
It's functionally equivalent, but yes we can write it this way if you prefer. Is it okay if I fold your diff (minus the
pr_info()) into this patch? (I'll also addFW_BUGto the error messages as Thomas requested.)
Yap, that's perfectly fine.
Thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
In reply to Borislav PetkovQuoted 25 lines · On Mon, 27 Jul 2026 22:27:27 -0700 Borislav Petkov <[email protected]> wrote:
Quoted 4 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
s/This eliminates/Eliminate/
Quoted 17 lines
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.Signed-off-by Eric Biggers <[email protected]>--- arch/x86/kernel/fpu/xstate.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)arch/x86/kernel/fpu/xstate.c +0
-0@@ -799,6 +799,23 @@ static u64 __init guest_default_mask(void)return ~(u64 )XFEATURE_MASK_USER_DYNAMIC ; }
Quoted 6 lines
arch/x86/kernel/fpu/xstate.c static u64 __init guest_default_mask(void) · line 803
Quoted 2 lines
arch/x86/kernel/fpu/xstate.c +2
-0+/* Clear any X86_FEATURE_* used by the kernel whose xfeatures are missing. */ +static void __init clear_cpu_caps_with_missing_xfeatures (u64 xfeatures)That function name is a bit too long. How about:
clear_cpu_caps_xft()
That is too terse.
Since none of the uses are overlong lines it really doesn't matter.
Quoted 42 lines
or so.
Quoted 20 lines
arch/x86/kernel/fpu/xstate.c +15
-0+{ +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. @@ -812,12 +829,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)arch/x86/kernel/fpu/xstate.c void __init fpu__init_system_xstate(unsigned int legacy_size) · line 839
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +2
-0if (!boot_cpu_has (X86_FEATURE_FPU )) {pr_info ("x86/fpu: No FPU detected\n" ); +clear_cpu_caps_with_missing_xfeatures (0 );return ; }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" ); +clear_cpu_caps_with_missing_xfeatures (0 );Also, I'm not really clear on the usage here: if the CPU doesn't have FPU or XSAVE, we pass in xfeature 0 which is
XFEATURE_FPin both cases. And then we clear AVX and AVX-512.The 0 is basically forcing the checks in the function to match, i.e., it looks to me like we're defining a new interface but then we're misusing it so that those basic CPU flags are cleared.
Quoted 5 lines
What are we even protecting against here?
AVX and AVX-512 code needs to check whether it has FPU and XSAVE support?
I.e., we're protecting against some weird guests?
More likely userspace running an old kernel on a new cpu.
While unlikely to be a problem with AVX and AVX-512, it did happen when they were first added and might happen when the next feature is added.
Quoted 3 lines
I wanna say, we should not protect but let them crash'n'burn in big big flames which can be seen from a mile away.
I don't want to see the bug reports.
The flames aren't big, what happens is that the registers don't get preserved across a process switch - so code tends to work a lot of the time.
For userspace the XCR bits are the important ones.
David
Quoted 6 lines
Or do you have a sensible use case in mind which we really wanna protect against and this all actually makes sense?
Thx.
Thomas Gleixner
maintainerRe: [PATCH v2 1/8] x86/fpu: Check for missing AVX and AVX-512 xstate bits
In reply to Eric BiggersQuoted 2 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.
That's clearly a firmware bug which has to be treated as all other inconsistencies of that sort.
Quoted 4 lines
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.
Indeed it is inconsistent all over the place. While looking at the follow up patches I wondered how many of these remaining nested conditions are redundant. Most of them should be covered by the dependency checks. If they are not, then we should add them.
Quoted 11 lines
Signed-off-by Eric Biggers <[email protected]>--- arch/x86/kernel/fpu/xstate.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)arch/x86/kernel/fpu/xstate.c +0
-0@@ -799,6 +799,23 @@ static u64 __init guest_default_mask(void)return ~(u64 )XFEATURE_MASK_USER_DYNAMIC ;
arch/x86/kernel/fpu/xstate.c static u64 __init guest_default_mask(void) · line 809
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +8
-0} +/* 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" );
Please make these
pr_err(FW_BUG "...")
As that's what it is.
Thanks,
tglx
Eric Biggers
maintainerPATCH v2 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
[email protected]
AI reviewRe: [PATCH v2 2/8] um: Check for missing AVX and AVX-512 xstate bits
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] Incomplete XCR0 validation whitelist allows AVX-dependent and AMX features to bypass state checks, causing guest application crashes.
Pre-existing issues:
- [High] Substring matching flaw in parse_host_cpu_flags() incorrectly enables CPU features, leading to application crashes. --
--- Patch [2]: [PATCH v2 2/8] um: Check for missing AVX and AVX-512 xstate bits --- commit 14fa8af6eb070800589e8e98d9a0ba3804acb926
Author: Eric Biggers <[email protected]>
um: 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 bits are set in the XCR0 register. If missing, it warns and does not set the X86_FEATURE_* flags.
Quoted 4 lines
arch/um/kernel/um_arch.c +0
-0
[ ... ]
Quoted 1 line
arch/um/kernel/um_arch.c +0
-0@@ -264,12 +264,88 @@ unsigned long brk_start;
[ ... ]
arch/um/kernel/um_arch.c unsigned long brk_start; · line 294
Quoted 9 lines
arch/um/kernel/um_arch.c +9
-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 :
[Severity: High]
Will this switch statement inadvertently bypass state checks for features like X86_FEATURE_AMX_TILE, X86_FEATURE_F16C, X86_FEATURE_XOP, and X86_FEATURE_FMA4?
If these features are missing from the switch cases, they will fall through and be unconditionally enabled at the end of the function. If the host CPU supports AMX, UML will advertise amx_tile. If a guest application attempts to use it without the UML process calling prctl(ARCH_REQ_XCOMP_PERM), the host will send SIGILL.
A similar #UD crash could occur for f16c, fma4, or xop if host AVX XCR0 state is disabled but the features are not cleared by the host.
Quoted 2 lines
arch/um/kernel/um_arch.c +2
-0+case X86_FEATURE_AVX_VNNI : +case X86_FEATURE_FMA :
[ ... ]
Quoted 4 lines
arch/um/kernel/um_arch.c +4
-0+break ; + } +set_cpu_cap (&boot_cpu_data, cap); +}
arch/um/kernel/um_arch.c unsigned long brk_start; · line 348
Quoted 10 lines
arch/um/kernel/um_arch.c +4
-1+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);
[Severity: High]
This is a pre-existing issue, but does this substring match cause false positive feature detection?
Using strstr without word boundary checks means that if a host CPU supports fma4 but not fma (like AMD Bulldozer), the string "fma" will match inside "fma4".
This causes UML to falsely advertise fma to the guest, and guest applications probing for fma will reliably crash with a #UD exception when attempting to execute FMA3 instructions.
Quoted 2 lines
}
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Borislav Petkov
maintainerRe: [PATCH v2 2/8] um: Check for missing AVX and AVX-512 xstate bits
In reply to Eric BiggersQuoted 8 lines · Eric Biggers
If 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*.
Quoted 8 lines
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.
Signed-off-by Eric Biggers <[email protected]>--- arch/um/kernel/um_arch.c | 78 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
./scripts/get_maintainer.pl -f arch/um/kernel/um_arch.c
Richard Weinberger <[email protected]> (maintainer:USER-MODE LINUX (UML))
Anton Ivanov <[email protected]> (maintainer:USER-MODE LINUX (UML))
Johannes Berg <[email protected]> (maintainer:USER-MODE LINUX (UML),commit_signer:8/10=80%,authored:1/10=10%,removed_lines:2/40=5%)
...
Please use that script. CCed now.
Quoted 14 lines
arch/um/kernel/um_arch.c +6
-0@@ -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)
arch/um/kernel/um_arch.c unsigned long brk_start; · line 282
Quoted 10 lines
arch/um/kernel/um_arch.c +10
-0+ : "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; + } + }
I don't know - I probably would pull in some helpers for cpuid and xgetbv instead of doing this ad-hoc. UML maintainers' call.
arch/um/kernel/um_arch.c unsigned long brk_start; · line 284
Quoted 2 lines
arch/um/kernel/um_arch.c +2
-0+return 0 ; +}
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerPATCH v2 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
Re: [PATCH v2 3/8] crypto: x86 - Stop using cpu_has_xfeatures()
Quoted 14 lines · On Mon, 27 Jul 2026 19:15:58 -0700 Eric Biggers <[email protected]> wrote:
Checking both
boot_cpu_has(X86_FEATURE_AVX*)andcpu_has_xfeatures()has never really been needed in practice, and it's never been universally done (e.g.,lib/raid/omitscpu_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 ofboot_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.Signed-off-by Eric Biggers <[email protected]>---
...
arch/x86/crypto/aria_gfni_avx512_glue.c static struct skcipher_alg aria_algs[] = { · line 200
Quoted 10 lines
arch/x86/crypto/aria_gfni_avx512_glue.c +0
-2@@ -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) ||
Doesn't AVX2 require AVX support?
arch/x86/crypto/aria_gfni_avx512_glue.c static struct skcipher_alg aria_algs[] = { · line 202
Quoted 2 lines
arch/x86/crypto/aria_gfni_avx512_glue.c +0
-0!boot_cpu_has (X86_FEATURE_AVX512F ) || !boot_cpu_has(X86_FEATURE_AVX512VL) ||
And (less sure) AVX512xxx require AVX2?
So the extra checks could be reduced.
David
Quoted 3 lines
arch/x86/crypto/aria_gfni_avx512_glue.c +1
-2- !boot_cpu_has(X86_FEATURE_GFNI) || - !boot_cpu_has(X86_FEATURE_OSXSAVE)) { + !boot_cpu_has(X86_FEATURE_GFNI)) {
Eric Biggers
maintainerRe: [PATCH v2 3/8] crypto: x86 - Stop using cpu_has_xfeatures()
Quoted 31 lines · David Laight
Quoted 14 lines · On Mon, 27 Jul 2026 19:15:58 -0700 Eric Biggers <[email protected]> wrote:
Checking both
boot_cpu_has(X86_FEATURE_AVX*)andcpu_has_xfeatures()has never really been needed in practice, and it's never been universally done (e.g.,lib/raid/omitscpu_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 ofboot_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.Signed-off-by Eric Biggers <[email protected]>---
...
arch/x86/crypto/aria_gfni_avx512_glue.c static struct skcipher_alg aria_algs[] = { · line 200
Quoted 10 lines
arch/x86/crypto/aria_gfni_avx512_glue.c +0
-2@@ -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) ||Doesn't AVX2 require AVX support?
Quoted 6 lines
arch/x86/crypto/aria_gfni_avx512_glue.c static struct skcipher_alg aria_algs[] = { · line 202
Quoted 2 lines
arch/x86/crypto/aria_gfni_avx512_glue.c +0
-0!boot_cpu_has (X86_FEATURE_AVX512F ) || !boot_cpu_has(X86_FEATURE_AVX512VL) ||And (less sure) AVX512xxx require AVX2?
So the extra checks could be reduced.
Yes, but this patch is just intended to clean up the xfeatures and OSXSAVE checks. Further cleanups would be separate patches.
- Eric
Eric Biggers
maintainerPATCH v2 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
Borislav Petkov
maintainerRe: [PATCH v2 4/8] lib/crypto: x86: Stop using cpu_has_xfeatures()
Quoted 25 lines · Eric Biggers
Checking both
boot_cpu_has()andcpu_has_xfeatures()has never really been needed in practice, and it's never been universally done (e.g.,lib/raid/omitscpu_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().Signed-off-by Eric Biggers <[email protected]>--- 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 +0
-0
lib/crypto/x86/blake2s.h static void blake2s_mod_init_arch(void) · line 60
Quoted 9 lines
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); }
You could add a patch at the end or I can do it which does
s/boot_cpu_has/cpu_feature_enabled/
as latter is the interface we use now everywhere.
But yeah, it should be a separate patch.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerRe: [PATCH v2 4/8] lib/crypto: x86: Stop using cpu_has_xfeatures()
Quoted 38 lines · Borislav Petkov
Quoted 25 lines · Eric Biggers
Checking both
boot_cpu_has()andcpu_has_xfeatures()has never really been needed in practice, and it's never been universally done (e.g.,lib/raid/omitscpu_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().Signed-off-by Eric Biggers <[email protected]>--- 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 +0
-0lib/crypto/x86/blake2s.h static void blake2s_mod_init_arch(void) · line 60
Quoted 9 lines
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); }You could add a patch at the end or I can do it which does
Quoted 5 lines
s/boot_cpu_has/cpu_feature_enabled/
as latter is the interface we use now everywhere.
But yeah, it should be a separate patch.
cpu_feature_enabled() turns into an alternative (code patching), which doesn't make much sense in any of the places you're asking for it to be used, as they just check the features once at module init time to toggle static keys.
I suppose that the static keys that are conditional on only one CPU feature can be replaced with cpu_feature_enabled(), which should be equally efficient. However, many of the keys are conditional on two or more features.
After that, there would be a mix of both versions. So sure, it could make sense to use cpu_feature_enabled() everywhere anyway, even when not helpful. I'm not sure it's clearly better than the status quo, though.
- Eric
Borislav Petkov
maintainerRe: [PATCH v2 4/8] lib/crypto: x86: Stop using cpu_has_xfeatures()
Quoted 3 lines · Eric Biggers
cpu_feature_enabled()turns into an alternative (code patching), which doesn't make much sense in any of the places you're asking for it to be used,
It is the interface to use for checking X86_FEATURE flags. The fact that it alternative-patches doesn't really matter.
Quoted 4 lines
I suppose that the static keys that are conditional on only one CPU feature can be replaced with
cpu_feature_enabled(), which should be equally efficient. However, many of the keys are conditional on two or more features.
Yes, they all can be converted to cpu_feature_enabled(). Just forget the efficiency angle and think of this as us unifying the interfaces to a single one instead of the zoo we have right now.
Quoted 2 lines
After that, there would be a mix of both versions. So sure, it could make sense to use
cpu_feature_enabled()everywhere anyway,
Yap, that's the goal.
Quoted 1 line
I'm not sure it's clearly better than the status quo, though.
Yes, it is. See above.
Once everything is converted, there will be a single cpu_feature_enabled() and all users will not care whether to use boot_cpu_has(), this_cpu_has(), cpu_has(c, ...), static_cpu_has() and so on.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEric Biggers
maintainerPATCH v2 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 v2 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 v2 7/8 lib/raid/xor: x86: 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
Re: [PATCH v2 7/8] lib/raid/xor: x86: Remove redundant X86_FEATURE_OSXSAVE check
The prefix here should be just xor: to stick with the existing commits.
Eric Biggers
maintainerPATCH v2 8/8 lib/raid/xor: x86: 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 | 121 ++++++++++++++++++++++++++++++++++ lib/raid/xor/x86/xor_arch.h | 30 ++++++--- 3 files changed, 142 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 +121 -0
121 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
Re: [PATCH v2 8/8] lib/raid/xor: x86: Add AVX-512 optimized xor_gen()
Stick to the xor: prefix here as well please.
The whole approach looks nice from the user POV:
I'll leave it to the x86 experts to make sure nothing is missed in the nitty gritty details.