arm64: Speed up CRC-32 using PMULL instructions
Subsystems (2)
cover arm64: Speed up CRC-32 using PMULL instructions
From: Ard Biesheuvel <[email protected]>
The CRC-32 code is library code, and is not part of the crypto subsystem. This means that callers may not generally be aware of the kind of implementation that backs it, and so we've refrained from using FP/SIMD code in the past, as it disables preemption, and this may incur scheduling latencies that the caller did not anticipate.
This was solved a while ago, and on arm64, kernel mode FP/SIMD no longer disables preemption.
This means we can happily use PMULL instructions in the CRC-32 library code, which permits an optimization to be implemented that results in a speedup of 2 - 2.8x for inputs >1k in size (on Apple M2)
Patch #1 implements some prepwork to handle the scalar CRC-32 alternatives patching in C code.
Ard Biesheuvel (2):
arm64/lib: Handle CRC-32 alternative in C code arm64/crc32: Implement 4-way interleave using PMULL
arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/crc32-glue.c | 70 ++++++ arch/arm64/lib/crc32-pmull.S | 240 ++++++++++++++++++++ arch/arm64/lib/crc32.S | 21 +- 4 files changed, 317 insertions(+), 16 deletions(-) create mode 100644 arch/arm64/lib/crc32-glue.c create mode 100644 arch/arm64/lib/crc32-pmull.S
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
PATCH 1/2 arm64/lib: Handle CRC-32 alternative in C code
From: Ard Biesheuvel <[email protected]>
In preparation for adding another code path for performing CRC-32, move the alternative patching for ARM64_HAS_CRC32 into C code. The logic for deciding whether to use this new code path will be implemented in C too.
--- arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/crc32-glue.c | 34 ++++++++++++++++++++ arch/arm64/lib/crc32.S | 21 ++++-------- 3 files changed, 41 insertions(+), 16 deletions(-)
arch/arm64/lib/Makefile +1 -1
@@ -13,7 +13,7 @@ endif lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o -obj-$(CONFIG_CRC32) += crc32.o +obj-$(CONFIG_CRC32) += crc32.o crc32-glue.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
arch/arm64/lib/crc32-glue.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/crc32.h> +#include <linux/linkage.h> + +#include <asm/alternative.h> + +asmlinkage u32 crc32_le_arm64(u32 crc, unsigned char const *p, size_t len); +asmlinkage u32 crc32c_le_arm64(u32 crc, unsigned char const *p, size_t len); +asmlinkage u32 crc32_be_arm64(u32 crc, unsigned char const *p, size_t len); + +u32 __pure crc32_le (u32 crc,unsigned char const *p,size_t len) +{ +if (!alternative_has_cap_likely (ARM64_HAS_CRC32 )) +return crc32_le_base (crc, p, len); + +return crc32_le_arm64 (crc, p, len); +} + +u32 __pure __crc32c_le (u32 crc,unsigned char const *p,size_t len) +{ +if (!alternative_has_cap_likely (ARM64_HAS_CRC32 )) +return __crc32c_le_base (crc, p, len); + +return crc32c_le_arm64 (crc, p, len); +} + +u32 __pure crc32_be (u32 crc,unsigned char const *p,size_t len) +{ +if (!alternative_has_cap_likely (ARM64_HAS_CRC32 )) +return crc32_be_base (crc, p, len); + +return crc32_be_arm64 (crc, p, len); +}
arch/arm64/lib/crc32.S +6 -15
@@ -136,25 +136,16 @@ CPU_BE( rev16 \reg, \reg ) .endm .align 5 -SYM_FUNC_START(crc32_le) -alternative_if_not ARM64_HAS_CRC32 - b crc32_le_base -alternative_else_nop_endif +SYM_FUNC_START(crc32_le_arm64) __crc32 -SYM_FUNC_END(crc32_le) +SYM_FUNC_END(crc32_le_arm64) .align 5 -SYM_FUNC_START(__crc32c_le) -alternative_if_not ARM64_HAS_CRC32 - b __crc32c_le_base -alternative_else_nop_endif +SYM_FUNC_START(crc32c_le_arm64) __crc32 c -SYM_FUNC_END(__crc32c_le) +SYM_FUNC_END(crc32c_le_arm64) .align 5 -SYM_FUNC_START(crc32_be) -alternative_if_not ARM64_HAS_CRC32 - b crc32_be_base -alternative_else_nop_endif +SYM_FUNC_START(crc32_be_arm64) __crc32 be=1 -SYM_FUNC_END(crc32_be) +SYM_FUNC_END(crc32_be_arm64)
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
PATCH 2/2 arm64/crc32: Implement 4-way interleave using PMULL
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that kernel mode NEON no longer disables preemption, using FP/SIMD in library code which is not obviously part of the crypto subsystem is no longer problematic, as it will no longer incur unexpected latencies.
So accelerate the CRC-32 library code on arm64 to use a 4-way interleave, using PMULL instructions to implement the folding.
On Apple M2, this results in a speedup of 2 - 2.8x when using input sizes of 1k - 8k. For smaller sizes, the overhead of preserving and restoring the FP/SIMD register file may not be worth it, so 1k is used as a threshold for choosing this code path.
The coefficient tables were generated using code provided by Eric. [0]
[0] https://github.com/ebiggers/libdeflate/blob/master/scripts/gen_crc32_multipliers.c
--- arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/crc32-glue.c | 36 +++ arch/arm64/lib/crc32-pmull.S | 240 ++++++++++++++++++++ 3 files changed, 277 insertions(+), 1 deletion(-)
arch/arm64/lib/Makefile +1 -1
@@ -13,7 +13,7 @@ endif lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o -obj-$(CONFIG_CRC32) += crc32.o crc32-glue.o +obj-$(CONFIG_CRC32) += crc32.o crc32-glue.o crc32-pmull.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
arch/arm64/lib/crc32-glue.c +36 -0
@@ -4,16 +4,36 @@#include <linux/linkage.h> #include <asm/alternative.h> +#include <asm/cpufeature.h> +#include <asm/neon.h> +#include <asm/simd.h> + +#include <crypto/internal/simd.h> + +// The minimum input length to consider the 4-way interleaved code path +static const size_t min_len =1024 ; asmlinkage u32 crc32_le_arm64(u32 crc, unsigned char const *p, size_t len); asmlinkage u32 crc32c_le_arm64(u32 crc, unsigned char const *p, size_t len); asmlinkage u32 crc32_be_arm64(u32 crc, unsigned char const *p, size_t len); +asmlinkage u32 crc32_le_pmull(u32 crc, unsigned char const *p, size_t len); +asmlinkage u32 crc32c_le_pmull(u32 crc, unsigned char const *p, size_t len); +asmlinkage u32 crc32_be_pmull(u32 crc, unsigned char const *p, size_t len); +u32 __pure crc32_le (u32 crc,unsigned char const *p,size_t len) {if (!alternative_has_cap_likely (ARM64_HAS_CRC32 ))return crc32_le_base (crc, p, len); +if (len >= min_len &&cpu_have_named_feature (PMULL ) &&crypto_simd_usable ()) { +kernel_neon_begin (); + crc =crc32_le_pmull (crc, p, len); + p +=round_down (len,64 ); + len %=64 ; +kernel_neon_end (); + } +return crc32_le_arm64 (crc, p, len); }
@@ -22,6 +42,14 @@ u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)if (!alternative_has_cap_likely (ARM64_HAS_CRC32 ))return __crc32c_le_base (crc, p, len); +if (len >= min_len &&cpu_have_named_feature (PMULL ) &&crypto_simd_usable ()) { +kernel_neon_begin (); + crc =crc32c_le_pmull (crc, p, len); + p +=round_down (len,64 ); + len %=64 ; +kernel_neon_end (); + } +return crc32c_le_arm64 (crc, p, len); }
@@ -30,5 +58,13 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)if (!alternative_has_cap_likely (ARM64_HAS_CRC32 ))return crc32_be_base (crc, p, len); +if (len >= min_len &&cpu_have_named_feature (PMULL ) &&crypto_simd_usable ()) { +kernel_neon_begin (); + crc =crc32_be_pmull (crc, p, len); + p +=round_down (len,64 ); + len %=64 ; +kernel_neon_end (); + } +return crc32_be_arm64 (crc, p, len); }
arch/arm64/lib/crc32-pmull.S new file +240 -0
240 lines not on this page · raw message
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Re: [PATCH 2/2] arm64/crc32: Implement 4-way interleave using PMULL
Quoted 18 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Now that kernel mode NEON no longer disables preemption, using FP/SIMD in library code which is not obviously part of the crypto subsystem is no longer problematic, as it will no longer incur unexpected latencies.
So accelerate the CRC-32 library code on arm64 to use a 4-way interleave, using PMULL instructions to implement the folding.
On Apple M2, this results in a speedup of 2 - 2.8x when using input sizes of 1k - 8k. For smaller sizes, the overhead of preserving and restoring the FP/SIMD register file may not be worth it, so 1k is used as a threshold for choosing this code path.
The coefficient tables were generated using code provided by Eric. [0]
[0] https://github.com/ebiggers/libdeflate/blob/master/scripts/gen_crc32_multipliers.c
Quoted 7 lines
Cc Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/crc32-glue.c | 36 +++ arch/arm64/lib/crc32-pmull.S | 240 ++++++++++++++++++++ 3 files changed, 277 insertions(+), 1 deletion(-)
Thanks for doing this! The new code looks good to me. 4-way does seem like the right choice for arm64.
I'd recommend calling the file crc32-4way.S and the functions crc32*_arm64_4way(), rather than crc32-pmull.S and crc32*_pmull(). This would avoid confusion with a CRC implementation that is actually based entirely on pmull (which is possible). The proposed implementation uses the crc32 instructions to do most of the work and only uses pmull for combining the CRCs. Yes, crc32c-pcl-intel-asm_64.S made this same mistake, but it is a mistake, IMO.
- Eric
Re: [PATCH 2/2] arm64/crc32: Implement 4-way interleave using PMULL
Quoted 28 lines · Eric Biggers
Quoted 18 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Now that kernel mode NEON no longer disables preemption, using FP/SIMD in library code which is not obviously part of the crypto subsystem is no longer problematic, as it will no longer incur unexpected latencies.
So accelerate the CRC-32 library code on arm64 to use a 4-way interleave, using PMULL instructions to implement the folding.
On Apple M2, this results in a speedup of 2 - 2.8x when using input sizes of 1k - 8k. For smaller sizes, the overhead of preserving and restoring the FP/SIMD register file may not be worth it, so 1k is used as a threshold for choosing this code path.
The coefficient tables were generated using code provided by Eric. [0]
[0] https://github.com/ebiggers/libdeflate/blob/master/scripts/gen_crc32_multipliers.c
Quoted 7 lines
Cc Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/lib/Makefile | 2 +- arch/arm64/lib/crc32-glue.c | 36 +++ arch/arm64/lib/crc32-pmull.S | 240 ++++++++++++++++++++ 3 files changed, 277 insertions(+), 1 deletion(-)
Quoted 3 lines
Thanks for doing this! The new code looks good to me. 4-way does seem like the right choice for arm64.
Agreed.
Quoted 4 lines
I'd recommend calling the file
crc32-4way.Sand the functions crc32*_arm64_4way(), rather thancrc32-pmull.Sand crc32*_pmull(). This would avoid confusion with a CRC implementation that is actually based entirely on pmull (which is possible).
I'm well aware :-)
commit 8fefde90e90c9f5c2770e46ceb127813d3f20c34
Author: Ard Biesheuvel <[email protected]> Date: Mon Dec 5 18:42:27 2016 +0000
crypto: arm64/crc32 - accelerated support based on x86 SSE implementation
commit 598b7d41e544322c8c4f3737ee8ddf905a44175e
Author: Ard Biesheuvel <[email protected]> Date: Mon Aug 27 13:02:45 2018 +0200
crypto: arm64/crc32 - remove PMULL based CRC32 driver
I removed it because it wasn't actually faster, although that might be different on modern cores.
Quoted 4 lines
The proposed implementation uses the crc32
instructions to do most of the work and only uses pmull for combining the CRCs. Yes,crc32c-pcl-intel-asm_64.Smade this same mistake, but it is a mistake, IMO.
Yeah good point.
Re: [PATCH 2/2] arm64/crc32: Implement 4-way interleave using PMULL
Quoted 13 lines · Ard Biesheuvel
Quoted 4 lines
I'd recommend calling the file
crc32-4way.Sand the functions crc32*_arm64_4way(), rather thancrc32-pmull.Sand crc32*_pmull(). This would avoid confusion with a CRC implementation that is actually based entirely on pmull (which is possible).I'm well aware :-)
commit
8fefde90e90c9f5c2770e46ceb127813d3f20c34Author: Ard Biesheuvel <[email protected]> Date: Mon Dec 5 18:42:27 2016 +0000crypto: arm64/crc32 - accelerated support based on x86 SSE implementation
Quoted 8 lines
commit
598b7d41e544322c8c4f3737ee8ddf905a44175eAuthor: Ard Biesheuvel <[email protected]> Date: Mon Aug 27 13:02:45 2018 +0200crypto: arm64/crc32 - remove PMULL based CRC32 driver
I removed it because it wasn't actually faster, although that might be different on modern cores.
The PMULL-based code removed by commit 598b7d41e544 was only 4-wide. On Apple M1, a 12-wide PMULL-based CRC32 is actually faster than 4-way CRC32, especially if the eor3 instruction from the sha3 extension is utilized.
This was not the case on non-Apple CPUs I tested (in 2022), though. 12-wide is very wide and is a bit inconvenient, and IMO it's not worth doing in the kernel at this point. It would be interesting to test the very latest CPUs, though.
- Eric