From: Shreesh Adiga <16567adigashreesh@gmail.com>
To: Eric Biggers <ebiggers@kernel.org>,
Ard Biesheuvel <ardb@kernel.org>,
Demian Shulhan <demyansh@gmail.com>
Cc: linux-crypto@vger.kernel.org
Subject: [PATCH v3] lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation
Date: Fri, 27 Nov 2026 22:27:39 +0530 [thread overview]
Message-ID: <20261127165747.512950-1-16567adigashreesh@gmail.com> (raw)
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.
Signed-off-by: Shreesh Adiga <16567adigashreesh@gmail.com>
---
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/20261114115749.1061233-1-16567adigashreesh@gmail.com/
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(-)
diff --git a/lib/crc/arm64/crc64-neon.h b/lib/crc/arm64/crc64-neon.h
index d872f4d4e2e7..40f37ff0407c 100644
--- a/lib/crc/arm64/crc64-neon.h
+++ b/lib/crc/arm64/crc64-neon.h
@@ -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;
+}
diff --git a/lib/crc/arm64/crc64.h b/lib/crc/arm64/crc64.h
index 06dd08a21dd6..0cf79b9da578 100644
--- a/lib/crc/arm64/crc64.h
+++ b/lib/crc/arm64/crc64.h
@@ -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_arch
static 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);
+ }
}
diff --git a/lib/crc/crc64-neon.c b/lib/crc/crc64-neon.c
index 18eb17d5462b..faa681bebfec 100644
--- a/lib/crc/crc64-neon.c
+++ b/lib/crc/crc64-neon.c
@@ -3,12 +3,16 @@
* Accelerated CRC64 (NVMe) using ARM NEON C intrinsics
*/
+#include <stdbool.h>
#include <stddef.h>
#include <asm/neon-intrinsics.h>
#include "crc64-neon.h"
u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len);
+#ifdef CONFIG_ARM64
+u64 crc64_nvme_neon_pmull_eor3(u64 crc, const u8 *p, size_t len);
+#endif
/* x^575 mod G, x^511 mod G */
static const uint64_t fold_consts_4x_val[2] = { 0x0c32cdb31e18a84aULL,
@@ -23,14 +27,27 @@ static const uint64_t fold_consts_val[2] = { 0xeadc41fd2ba3d420ULL,
static const uint64_t bconsts_val[2] = { 0x27ecfa329aef9f77ULL,
0x34d926535897936aULL };
+static __always_inline uint64x2_t xor3(uint64x2_t t1,
+ uint64x2_t t2,
+ uint64x2_t t3,
+ __maybe_unused bool use_eor3)
+{
+#ifdef CONFIG_ARM64
+ if (use_eor3)
+ return eor3(t1, t2, t3);
+#endif
+ return t1 ^ t2 ^ t3;
+}
+
static __always_inline uint64x2_t fold_single_round(uint64x2_t fold,
uint64x2_t temp,
- uint64x2_t consts)
+ uint64x2_t consts,
+ bool use_eor3)
{
uint64x2_t temp1 = pmull64(consts, fold);
uint64x2_t temp2 = pmull64_high(consts, fold);
- return temp ^ temp1 ^ temp2;
+ return xor3(temp, temp1, temp2, use_eor3);
}
/*
@@ -45,7 +62,8 @@ static __always_inline uint64x2_t fold_single_round(uint64x2_t fold,
static __always_inline uint64x2_t fold_partial_block(uint64x2_t fold,
uint64x2_t consts,
const u8 *p,
- size_t len)
+ size_t len,
+ bool use_eor3)
{
static const u8 crc_neon_shift_tab[32] = {
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
@@ -68,10 +86,11 @@ static __always_inline uint64x2_t fold_partial_block(uint64x2_t fold,
return fold_single_round(vreinterpretq_u64_u8(a),
vreinterpretq_u64_u8(b),
- consts);
+ consts, use_eor3);
}
-u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len)
+static __always_inline u64 crc64_nvme_neon_body(u64 crc, const u8 *p,
+ size_t len, bool use_eor3)
{
/* Assumes len >= 128. */
uint64x2_t fold_consts = vld1q_u64(fold_consts_val);
@@ -94,39 +113,39 @@ u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len)
uint64x2_t t2 = vreinterpretq_u64_u8(vld1q_u8(p + 32));
uint64x2_t t3 = vreinterpretq_u64_u8(vld1q_u8(p + 48));
- v0 = fold_single_round(v0, t0, fold_consts_4x);
- v1 = fold_single_round(v1, t1, fold_consts_4x);
- v2 = fold_single_round(v2, t2, fold_consts_4x);
- v3 = fold_single_round(v3, t3, fold_consts_4x);
+ v0 = fold_single_round(v0, t0, fold_consts_4x, use_eor3);
+ v1 = fold_single_round(v1, t1, fold_consts_4x, use_eor3);
+ v2 = fold_single_round(v2, t2, fold_consts_4x, use_eor3);
+ v3 = fold_single_round(v3, t3, fold_consts_4x, use_eor3);
p += 64;
len -= 64;
} while (len >= 64);
- v0 = fold_single_round(v0, v2, fold_consts_2x);
- v1 = fold_single_round(v1, v3, fold_consts_2x);
+ v0 = fold_single_round(v0, v2, fold_consts_2x, use_eor3);
+ v1 = fold_single_round(v1, v3, fold_consts_2x, use_eor3);
if (len >= 32) {
uint64x2_t t0 = vreinterpretq_u64_u8(vld1q_u8(p + 0));
uint64x2_t t1 = vreinterpretq_u64_u8(vld1q_u8(p + 16));
- v0 = fold_single_round(v0, t0, fold_consts_2x);
- v1 = fold_single_round(v1, t1, fold_consts_2x);
+ v0 = fold_single_round(v0, t0, fold_consts_2x, use_eor3);
+ v1 = fold_single_round(v1, t1, fold_consts_2x, use_eor3);
p += 32;
len -= 32;
}
- v0 = fold_single_round(v0, v1, fold_consts);
+ v0 = fold_single_round(v0, v1, fold_consts, use_eor3);
if (len >= 16) {
uint64x2_t t0 = vreinterpretq_u64_u8(vld1q_u8(p));
- v0 = fold_single_round(v0, t0, fold_consts);
+ v0 = fold_single_round(v0, t0, fold_consts, use_eor3);
p += 16;
len -= 16;
}
if (len)
- v0 = fold_partial_block(v0, fold_consts, p, len);
+ v0 = fold_partial_block(v0, fold_consts, p, len, use_eor3);
/* Multiply the 128-bit value by x^64 and reduce it back to 128 bits. */
v0 = vextq_u64(v0, zero, 1) ^ pmull64_hi_lo(fold_consts, v0);
@@ -135,7 +154,20 @@ u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len)
uint64x2_t bconsts = vld1q_u64(bconsts_val);
uint64x2_t final = pmull64(bconsts, v0);
- v0 ^= vextq_u64(zero, final, 1) ^ pmull64_hi_lo(bconsts, final);
+ v0 = xor3(v0, vextq_u64(zero, final, 1),
+ pmull64_hi_lo(bconsts, final), use_eor3);
return vgetq_lane_u64(v0, 1);
}
+
+u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len)
+{
+ return crc64_nvme_neon_body(crc, p, len, false);
+}
+
+#ifdef CONFIG_ARM64
+u64 crc64_nvme_neon_pmull_eor3(u64 crc, const u8 *p, size_t len)
+{
+ return crc64_nvme_neon_body(crc, p, len, true);
+}
+#endif
--
2.56.1
next reply other threads:[~2026-11-27 16:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 16:57 Shreesh Adiga [this message]
2026-11-27 17:10 ` [PATCH v3] lib/crc: arm64: Add NEON PMULL+EOR3 crc64_nvme implementation sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127165747.512950-1-16567adigashreesh@gmail.com \
--to=16567adigashreesh@gmail.com \
--cc=ardb@kernel.org \
--cc=demyansh@gmail.com \
--cc=ebiggers@kernel.org \
--cc=linux-crypto@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox