lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation
Subsystems (2)
PATCH v3 1/1 lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation
Add a crc64_nvme implementation which benefits ARMv8.2 and later processors with the SHA3 extension.
Usage of EOR3 from SHA3 extension improves performance by 20% compared to the regular PMULL without EOR3 on Cortex-X925 measured in userspace benchmark for 512 byte input size.
Move the existing PMULL implementation into a __always_inline function and instantiate it twice with a boolean parameter which controls when EOR3 is used. This is only enabled for AArch64 since the SHA3 extension is not available in AArch32.
Use static_branch mechanism for dispatch in arm64 and enable the EOR3 variant when the CPU is determined to be not slower due to EOR3 and PMULL sharing pipeline stage. Currently Neoverse N2 is placed on the list to disable EOR3 based on Software Optimization Guide mentioning the pipeline V0 being used by both PMULL and EOR3 and V used by EOR.
Tested on KUnit for correctness on both AArch32 and AArch64.
---
Changes in v3:
Avoid running EOR3 routine on Neoverse N2.
Reuses code from PMULL implementation which was extended to support various optimizations like 4x unrolling and partial_block handling. Use inline assembly for EOR3 rather than veor3q_u64() intrinsic for GCC 8.1 support.
Changes in v2:
Added 2x reduction based on fold_consts_v2, use static key for PMULL and EOR3 instead of regular branch.
Link to v1: https://lore.kernel.org/linux-crypto/[email protected]/
Note:
This patch depends on mainline ff049836f05f5e46f702858a7cb949998e85a310
lib/crc: Don't include <linux/types.h> in NEON intrinsics code It also builds on top of crc-next submitted patch lib/crc: Add 4-way folding in NEON crc64_nvme implementation
lib/crc/arm64/crc64-neon.h | 15 +++++++++ lib/crc/arm64/crc64.h | 28 ++++++++++++++-- lib/crc/crc64-neon.c | 66 ++++++++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 20 deletions(-)
lib/crc/arm64/crc64-neon.h +15 -0
@@ -32,3 +32,18 @@ static inline uint64x2_t pmull64_hi_lo(uint64x2_t a, uint64x2_t b) {return pmull64 (vextq_u64 (a, a,1 ), b); } + +/* +* Use inline asm instead of veor3q_u64() intrinsic: GCC 8.1 doesn't provide +* that, and the target("+sha3") attribute it needs prevents some GCC versions +* from inlining the PMULL helpers. This follows lib/raid/xor/arm64/xor-eor3.c. +*/ +static inline uint64x2_t eor3 (uint64x2_t p,uint64x2_t q,uint64x2_t r) +{ +uint64x2_t res; + + asm(".arch_extension sha3\n" +"eor3 %0.16b, %1.16b, %2.16b, %3.16b" + :"=w" (res) :"w" (p),"w" (q),"w" (r)); +return res; +}
lib/crc/arm64/crc64.h +25 -3
@@ -4,13 +4,16 @@*/ #include <linux/cpufeature.h> +#include <asm/cputype.h> #include <asm/simd.h> #include <linux/minmax.h> #include <linux/sizes.h> u64 crc64_nvme_neon (u64 crc,const u8 *p,size_t len); +u64 crc64_nvme_neon_pmull_eor3 (u64 crc,const u8 *p,size_t len);static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_pmull); +static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_eor3);#define crc64_be_arch crc64_be_generic
@@ -18,8 +21,12 @@ static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len) {if (static_branch_likely (&have_pmull) && len >=128 &&likely (may_use_simd ())) { -scoped_ksimd () - crc = crc64_nvme_neon(crc, p, len); + scoped_ksimd() { +if (static_branch_likely (&have_eor3)) + crc =crc64_nvme_neon_pmull_eor3 (crc, p, len); +else + crc =crc64_nvme_neon (crc, p, len); + }return crc; }
@@ -29,6 +36,21 @@ static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len)#define crc64_mod_init_arch crc64_mod_init_archstatic void crc64_mod_init_arch (void ) { -if (cpu_have_named_feature (PMULL )) +/* +* On Neoverse N2, EOR3 can only issue on V0 pipeline, which PMULL +* also uses, so the EOR3 variant has lower throughput than plain EOR +* as per documentation in Arm Neoverse N2 Software Optimization Guide. +*/ +static const struct midr_range eor3_slow_cpus[] = { +MIDR_ALL_VERSIONS (MIDR_NEOVERSE_N2 ), + {} + }; + +if (cpu_have_named_feature (PMULL )) {static_branch_enable (&have_pmull); + +if (cpu_have_named_feature (SHA3 ) && + !is_midr_in_range_list (eor3_slow_cpus)) +static_branch_enable (&have_eor3); + } }
lib/crc/crc64-neon.c +49 -17
138 lines not on this page · raw message
Signature · 1 line
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] lib/crc: arm64: eor3() fails to assemble on binutils 2.40 and earlier --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] lib/crc: arm64: eor3() fails to assemble on binutils 2.40 and earlier
--
commit 54e29282d614c667430bde2ef55715d4cb4f1a05
Author: Shreesh Adiga <[email protected]>
lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation
This commit adds an arm64 NEON PMULL+EOR3 implementation of crc64_nvme for processors with the SHA3 extension and dispatches to it via a static branch.
Quoted 13 lines
lib/crc/arm64/crc64-neon.h +5
-0@@ -32,3 +32,18 @@ static inline uint64x2_t pmull64_hi_lo(uint64x2_t a, uint64x2_t b) {return pmull64 (vextq_u64 (a, a,1 ), b); } + +/* +* Use inline asm instead of veor3q_u64() intrinsic: GCC 8.1 doesn't provide +* that, and the target("+sha3") attribute it needs prevents some GCC versions +* from inlining the PMULL helpers. This follows lib/raid/xor/arm64/xor-eor3.c.
lib/crc/arm64/crc64-neon.h static inline uint64x2_t pmull64_hi_lo(uint64x2_t a, uint64x2_t b) · line 49
Quoted 10 lines
lib/crc/arm64/crc64-neon.h +10
-0+*/ +static inline uint64x2_t eor3 (uint64x2_t p,uint64x2_t q,uint64x2_t r) +{ +uint64x2_t res; + + asm(".arch_extension sha3\n" +"eor3 %0.16b, %1.16b, %2.16b, %3.16b" + :"=w" (res) :"w" (p),"w" (q),"w" (r)); +return res; +}
[Severity: High]
Does eor3() fail to assemble on binutils 2.40 and earlier?
lib/crc/Makefile compiles crc64-neon.o with -march=armv8-a+crypto, so the compiler emits .arch armv8-a+crypto and sets the assembler base architecture to armv8-a. In GNU binutils 2.40 and earlier, the sha3 extension and eor3 require armv8.2-a or later, unlike lib/raid/xor/arm64/xor-eor3.c which prefixes .arch_extension sha3 with ARM64_ASM_PREAMBLE:
lib/raid/xor/arm64/xor-eor3.c:eor3() {
asm(ARM64_ASM_PREAMBLE ".arch_extension sha3\n"
"eor3 %0.16b, %1.16b, %2.16b, %3.16b"
: "=w"(res) : "w"(p), "w"(q), "w"(r));
}With the base architecture left at armv8-a, GNU as 2.40 and earlier reject eor3 with "Error: selected processor does not support `eor3'". Note that selecting armv8.2-a or later via .arch (or ARM64_ASM_PREAMBLE) also resets active extensions, so crypto must remain enabled (for example via ".arch armv8.2-a+crypto+sha3\n") for subsequent inlined pmull and pmull2 instructions in crc64_nvme_neon_pmull_eor3().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ard Biesheuvel
reviewerQuoted 18 lines · Shreesh Adiga
Add a
crc64_nvmeimplementation which benefits ARMv8.2 and later processors with the SHA3 extension.Usage of EOR3 from SHA3 extension improves performance by 20% compared to the regular PMULL without EOR3 on Cortex-X925 measured in userspace benchmark for 512 byte input size.
Move the existing PMULL implementation into a
__always_inlinefunction and instantiate it twice with a boolean parameter which controls when EOR3 is used. This is only enabled for AArch64 since the SHA3 extension is not available in AArch32.Use
static_branchmechanism for dispatch in arm64 and enable the EOR3 variant when the CPU is determined to be not slower due to EOR3 and PMULL sharing pipeline stage. Currently Neoverse N2 is placed on the list to disable EOR3 based on Software Optimization Guide mentioning the pipeline V0 being used by both PMULL and EOR3 and V used by EOR.
Quoted 6 lines
Tested on KUnit for correctness on both AArch32 and AArch64.
Signed-off-by Shreesh Adiga <[email protected]>---
Changes in v3:
Avoid running EOR3 routine on Neoverse N2.
Ugh I don't like this at all.
Obviously, the h/w designers did not understand very well that EOR3 has much wider use beyond SHA-3.
...
Quoted 13 lines
lib/crc/arm64/crc64.h +4
-1@@ -29,6 +36,21 @@ static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len)#define crc64_mod_init_arch crc64_mod_init_archstatic void crc64_mod_init_arch (void ) { -if (cpu_have_named_feature (PMULL )) +/* +* On Neoverse N2, EOR3 can only issue on V0 pipeline, which PMULL +* also uses, so the EOR3 variant has lower throughput than plain EOR +* as per documentation in Arm Neoverse N2 Software Optimization Guide.
lib/crc/arm64/crc64.h +2
-0+*/ +static const struct midr_range eor3_slow_cpus[] = {
lib/crc/arm64/crc64.h static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len) · line 54
Quoted 10 lines
lib/crc/arm64/crc64.h +9
-0+ MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N2), + {} + }; + +if (cpu_have_named_feature (PMULL )) {static_branch_enable (&have_pmull); + +if (cpu_have_named_feature (SHA3 ) && + !is_midr_in_range_list (eor3_slow_cpus)) +static_branch_enable (&have_eor3);
Does this check the MIDR of the CPU that happens to be loading the module?
Hi Ard,
Quoted 52 lines · Ard Biesheuvel
Quoted 18 lines · Shreesh Adiga
Add a
crc64_nvmeimplementation which benefits ARMv8.2 and later processors with the SHA3 extension.Usage of EOR3 from SHA3 extension improves performance by 20% compared to the regular PMULL without EOR3 on Cortex-X925 measured in userspace benchmark for 512 byte input size.
Move the existing PMULL implementation into a
__always_inlinefunction and instantiate it twice with a boolean parameter which controls when EOR3 is used. This is only enabled for AArch64 since the SHA3 extension is not available in AArch32.Use
static_branchmechanism for dispatch in arm64 and enable the EOR3 variant when the CPU is determined to be not slower due to EOR3 and PMULL sharing pipeline stage. Currently Neoverse N2 is placed on the list to disable EOR3 based on Software Optimization Guide mentioning the pipeline V0 being used by both PMULL and EOR3 and V used by EOR.Quoted 6 lines
Tested on KUnit for correctness on both AArch32 and AArch64.
Signed-off-by Shreesh Adiga <[email protected]>---
Changes in v3:
Avoid running EOR3 routine on Neoverse N2.Ugh I don't like this at all.
Obviously, the h/w designers did not understand very well that EOR3 has much wider use beyond SHA-3.
...
Quoted 17 lines
lib/crc/arm64/crc64.h +4
-1@@ -29,6 +36,21 @@ static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len)#define crc64_mod_init_arch crc64_mod_init_archstatic void crc64_mod_init_arch (void ) { -if (cpu_have_named_feature (PMULL )) +/* +* On Neoverse N2, EOR3 can only issue on V0 pipeline, which PMULL +* also uses, so the EOR3 variant has lower throughput than plain EOR +* as per documentation in Arm Neoverse N2 Software Optimization Guide.
lib/crc/arm64/crc64.h +6
-0+*/ +static const struct midr_range eor3_slow_cpus[] = { +MIDR_ALL_VERSIONS (MIDR_NEOVERSE_N2 ), + {} + }; +
Quoted 8 lines
lib/crc/arm64/crc64.h static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len) · line 54
Quoted 6 lines
lib/crc/arm64/crc64.h +5
-0+if (cpu_have_named_feature (PMULL )) {static_branch_enable (&have_pmull); + +if (cpu_have_named_feature (SHA3 ) && + !is_midr_in_range_list (eor3_slow_cpus)) +static_branch_enable (&have_eor3);Does this check the MIDR of the CPU that happens to be loading the module?
Yes I believe it is only checking the CPU which happens to be loading the module.
It doesn't cover heterogeneous SoC which have different cores. Based on my reading, Neoverse N2 systems are homogeneous core systems used in cloud hosting by Azure Cobalt 100.
Apart from this I checked the manual for A710 and A715, which have similar pipeline to N2 but I'm not sure if there is any system where this code would run since on Phones I'm assuming nvme is usually not used.
Given that Big.Little system like Snapdragon 8 Gen 2 would also have cores like X3 which would be faster with EOR3 I am not sure if it needs to be disabled on A710 and A715.
Even the N2 case, I don't have access to that CPU and this approach is based on previous advice by Demian to avoid EOR3 everywhere even if available.
I still need to investigate the binutils <2.40 build failure and come up with a fix and then send out a new patch probably in a couple of days.
Kindly let me know if you have feedback related to the current approach for handling EOR3.
Thanks,
Shreesh
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Shreesh,
kernel test robot noticed the following build errors:
[auto build test ERROR on next-20261126]
[cannot apply to linux-review/Shreesh-Adiga/lib-crc-arm64-Add-NEON-PMULL-EOR3-crc64_nvme-implementation/20261114-172745 ebiggers/crc-next v7.4-rc4 v7.4-rc3 v7.4-rc2 linus/master v7.4-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Shreesh-Adiga/lib-crc-arm64-Add-NEON-PMULL-EOR3-crc64_nvme-implementation/20261127-222739 base: next-20261126 patch link: https://lore.kernel.org/r/20261127165747.512950-1-16567adigashreesh%40gmail.com patch subject: [PATCH v3] lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation config: arm64-randconfig-1001-20261128 (https://download.01.org/0day-ci/archive/20261129/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261129/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
/tmp/ccHTYrFm.s: Assembler messages:
>> /tmp/ccHTYrFm.s:251: Error: selected processor does not support `eor3 v4.16b,v18.16b,v19.16b,v4.16b' >> /tmp/ccHTYrFm.s:268: Error: selected processor does not support `eor3 v5.16b,v17.16b,v18.16b,v5.16b' >> /tmp/ccHTYrFm.s:278: Error: selected processor does not support `eor3 v2.16b,v0.16b,v17.16b,v2.16b' >> /tmp/ccHTYrFm.s:288: Error: selected processor does not support `eor3 v1.16b,v16.16b,v17.16b,v0.16b' >> /tmp/ccHTYrFm.s:315: Error: selected processor does not support `eor3 v2.16b,v2.16b,v0.16b,v4.16b' >> /tmp/ccHTYrFm.s:328: Error: selected processor does not support `eor3 v0.16b,v1.16b,v4.16b,v0.16b' >> /tmp/ccHTYrFm.s:346: Error: selected processor does not support `eor3 v2.16b,v4.16b,v5.16b,v2.16b' /tmp/ccHTYrFm.s:359: Error: selected processor does not support `eor3 v0.16b,v1.16b,v4.16b,v0.16b' >> /tmp/ccHTYrFm.s:375: Error: selected processor does not support `eor3 v0.16b,v0.16b,v1.16b,v2.16b' >> /tmp/ccHTYrFm.s:392: Error: selected processor does not support `eor3 v0.16b,v1.16b,v2.16b,v0.16b' >> /tmp/ccHTYrFm.s:423: Error: selected processor does not support `eor3 v0.16b,v1.16b,v0.16b,v2.16b' >> /tmp/ccHTYrFm.s:450: Error: selected processor does not support `eor3 v0.16b,v0.16b,v2.16b,v1.16b'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki