[PATCH 2/2] raid6: arm64: use the EOR3 instruction where available

Demian Shulhan demyansh at gmail.com
Thu Nov 19 15:55:29 PST 2026


Hi Ard,

A few EOR3-specific suggestions on top of patch 2:

> @@ -129,7 +145,7 @@ static inline void STEP4(unative_t *wp, unative_t *wq, const uint8_t *s3,
>   unative_t d1 = vld1q_u8(s1);
>   unative_t d0 = vld1q_u8(s0);
>
> - *wp ^= d3 ^ d2 ^ d1 ^ d0;
> + *wp = XOR3(XOR3(*wp, d3, d2), d1, d0);
>   *wq = HORNER4(*wq, d3, d2, d1, d0, x1d);
>  }

Writing this as:

*wp = XOR3(*wp, XOR3(d3, d2, d1), d0);

keeps *wp out of the inner EOR3 so d3 ^ d2 ^ d1 can issue in parallel
with the previous iteration's P update, cutting the loop-carried latency
on *wp in half.

Similarly, in HORNER4(), keeping 'q >> 4' at the outer XOR of 'hi' and
folding 'q << 4' and PMUL(hi, x1d) together in a single final XOR3():

unative_t hi = vshrq_n_u8(q, 4) ^
      XOR3(vshrq_n_u8(d3, 5), vshrq_n_u8(d2, 6), vshrq_n_u8(d1, 7));
unative_t lo_d = XOR3(vshlq_n_u8(d3, 3), vshlq_n_u8(d2, 2),
     vshlq_n_u8(d1, 1)) ^ d0;

return XOR3(vshlq_n_u8(q, 4), PMUL(hi, x1d), lo_d);

shortens the critical loop-carried Q chain (+7-8% for 16-32 data disks
with GCC).

Finally, in xor_syndrome_real(), neither GCC nor Clang folds GFMUL() with
the final q[d] XOR across the 'if (start)' merge point. Passing q[d] into
a GFMUL_XOR() helper using XOR3():

static inline unative_t GFMUL_XOR(unative_t v, unative_t m0, unative_t m1,
 unative_t q)
{
return XOR3(vqtbl1q_u8(m0, v & 0x0f),
   vqtbl1q_u8(m1, vshrq_n_u8(v, 4)), q);
}

and calling on a single line (for unroll.awk):

if (start) {
wq$$ = GFMUL_XOR(wq$$, gm0, gm1, vld1q_u8(&q[d+NSIZE*$$]));
} else {
wq$$ ^= vld1q_u8(&q[d+NSIZE*$$]);
}

saves one vector XOR per lane when start > 0 (+3-4% on partial stripe
updates with GCC).

BR,
Demian

ср, 18 лист. 2026 р. о 19:33 Ard Biesheuvel <ardb+git at google.com> пише:
>
> From: Ard Biesheuvel <ardb at kernel.org>
>
> The SHA3 extension to the arm64 instruction set provides EOR3, which XORs
> three vectors in a single instruction. Use it to fold the XORs in the
> Horner steps of the NEON syndrome code, which reduces the instruction
> count from 5.25 to 4 per data disk and vector for the 4-disk step, i.e.,
> a reduction of a third compared to the original code.
>
> Build the EOR3 versions from the same neon.uc source, with
> RAID6_NEON_EOR3 defined and the SHA3 extension enabled, and use them
> instead of the plain NEON versions on CPUs that implement SHA3. Use
> -march=armv8.2-a+sha3 for this: SHA3 is an optional extension of
> ARMv8.2, so any CPU that implements it implements ARMv8.2 as well. This
> is also what older toolchains (e.g., GCC 8.5 with binutils 2.36) need to
> accept EOR3.
>
> With SHA3 enabled, GCC and Clang fold a ^ b ^ c into EOR3 by themselves.
> The exception is the loop carried P accumulator: recent GCC versions
> reassociate the XORs into it in a way that only permits a single EOR3
> instruction where two can be used, so use the veor3q_u8() intrinsic
> explicitly for that case.
>
> Results on an Apple M2 running Linux, best of the 1x/2x/4x/8x versions,
> in MB/s, using a userspace build of the same code (4 KiB per disk):
>
>   gen_syndrome()      2 data    4 data    8 data   16 data   32 data
>   Avalanche (P) neon   54099     46028     41733     40484     39569
>                 eor3   63505     56936     53229     51807     51380
>   Blizzard (E)  neon   21670     16197     15524     13915     12475
>                 eor3   23388     19800     19583     17278     15609
>
>   xor_syndrome(), 16 data disks, stop = 15, in GB/s by value of start:
>                            0         4         8        12        15
>   Avalanche (P) neon      39        37        36        33        25
>                 eor3      50        46        44        38        25
>   Blizzard (E)  neon      13        14        13        12         9
>                 eor3      16        17        16        14         9
>
> Compared to the code before the previous patch, this is 15-48% faster for
> gen_syndrome() on the performance cores, and 18-39% on the efficiency
> cores.
>
> Signed-off-by: Ard Biesheuvel <ardb at kernel.org>
> ---
>  lib/raid/raid6/.gitignore      |  1 +
>  lib/raid/raid6/Makefile        | 19 +++++++++
>  lib/raid/raid6/arm/neon.c      | 42 +++++++++++---------
>  lib/raid/raid6/arm/neon.h      | 16 ++++++++
>  lib/raid/raid6/arm/neon.uc     | 20 +++++++++-
>  lib/raid/raid6/arm64/pq_arch.h | 34 +++++++++++++++-
>  6 files changed, 110 insertions(+), 22 deletions(-)
>
> diff --git a/lib/raid/raid6/.gitignore b/lib/raid/raid6/.gitignore
> index 6be57745afd1..2e9bd6299837 100644
> --- a/lib/raid/raid6/.gitignore
> +++ b/lib/raid/raid6/.gitignore
> @@ -4,5 +4,6 @@ altivec*.c
>  int*.c
>  tables.c
>  neon?.c
> +eor3_?.c
>  s390vx?.c
>  vpermxor*.c
> diff --git a/lib/raid/raid6/Makefile b/lib/raid/raid6/Makefile
> index 5cdb7223de2a..314ed1012cc0 100644
> --- a/lib/raid/raid6/Makefile
> +++ b/lib/raid/raid6/Makefile
> @@ -24,6 +24,10 @@ raid6_pq-$(CONFIG_KERNEL_MODE_NEON) += arm/neon.o \
>                                    arm/neon8.o \
>                                    arm/recov_neon.o \
>                                    arm/recov_neon_inner.o
> +raid6_pq-$(CONFIG_ARM64)       += arm64/eor3_1.o \
> +                                  arm64/eor3_2.o \
> +                                  arm64/eor3_4.o \
> +                                  arm64/eor3_8.o
>  raid6_pq-$(CONFIG_LOONGARCH)   += loongarch/loongarch_simd.o \
>                                    loongarch/recov_loongarch_simd.o
>  raid6_pq-$(CONFIG_ALTIVEC)     += powerpc/altivec1.o \
> @@ -56,11 +60,21 @@ CFLAGS_arm/neon2.o += $(CC_FLAGS_FPU)
>  CFLAGS_arm/neon4.o += $(CC_FLAGS_FPU)
>  CFLAGS_arm/neon8.o += $(CC_FLAGS_FPU)
>  CFLAGS_arm/recov_neon_inner.o += $(CC_FLAGS_FPU)
> +# SHA3 is an optional extension of ARMv8.2, so any CPU that implements it
> +# implements ARMv8.2 as well
> +CFLAGS_arm64/eor3_1.o += $(CC_FLAGS_FPU) -DRAID6_NEON_EOR3 -march=armv8.2-a+sha3
> +CFLAGS_arm64/eor3_2.o += $(CC_FLAGS_FPU) -DRAID6_NEON_EOR3 -march=armv8.2-a+sha3
> +CFLAGS_arm64/eor3_4.o += $(CC_FLAGS_FPU) -DRAID6_NEON_EOR3 -march=armv8.2-a+sha3
> +CFLAGS_arm64/eor3_8.o += $(CC_FLAGS_FPU) -DRAID6_NEON_EOR3 -march=armv8.2-a+sha3
>  CFLAGS_REMOVE_arm/neon1.o += $(CC_FLAGS_NO_FPU)
>  CFLAGS_REMOVE_arm/neon2.o += $(CC_FLAGS_NO_FPU)
>  CFLAGS_REMOVE_arm/neon4.o += $(CC_FLAGS_NO_FPU)
>  CFLAGS_REMOVE_arm/neon8.o += $(CC_FLAGS_NO_FPU)
>  CFLAGS_REMOVE_arm/recov_neon_inner.o += $(CC_FLAGS_NO_FPU)
> +CFLAGS_REMOVE_arm64/eor3_1.o += $(CC_FLAGS_NO_FPU)
> +CFLAGS_REMOVE_arm64/eor3_2.o += $(CC_FLAGS_NO_FPU)
> +CFLAGS_REMOVE_arm64/eor3_4.o += $(CC_FLAGS_NO_FPU)
> +CFLAGS_REMOVE_arm64/eor3_8.o += $(CC_FLAGS_NO_FPU)
>
>  ifeq ($(CONFIG_ALTIVEC),y)
>  altivec_flags := -maltivec $(call cc-option,-mabi=altivec)
> @@ -109,6 +123,11 @@ targets += arm/neon1.c arm/neon2.c arm/neon4.c arm/neon8.c
>  $(obj)/arm/neon%.c: $(src)/arm/neon.uc $(src)/unroll.awk FORCE
>         $(call if_changed,unroll)
>
> +# the arm64 EOR3 versions are built from the same source
> +targets += arm64/eor3_1.c arm64/eor3_2.c arm64/eor3_4.c arm64/eor3_8.c
> +$(obj)/arm64/eor3_%.c: $(src)/arm/neon.uc $(src)/unroll.awk FORCE
> +       $(call if_changed,unroll)
> +
>  targets += powerpc/altivec1.c \
>            powerpc/altivec2.c \
>            powerpc/altivec4.c \
> diff --git a/lib/raid/raid6/arm/neon.c b/lib/raid/raid6/arm/neon.c
> index 2f8e98f5556e..722f15f2a6ad 100644
> --- a/lib/raid/raid6/arm/neon.c
> +++ b/lib/raid/raid6/arm/neon.c
> @@ -7,6 +7,7 @@
>
>  #include <asm/simd.h>
>  #include "algos.h"
> +#include "arm/neon.h"
>
>  /*
>   * There are 2 reasons these wrappers are kept in a separate compilation unit
> @@ -19,35 +20,38 @@
>   *   outside a kernel_neon_begin()/kernel_neon_end() pair.
>   */
>
> -#define RAID6_NEON_WRAPPER(_n)                                         \
> -       static void raid6_neon ## _n ## _gen_syndrome(int disks,        \
> +#define RAID6_NEON_WRAPPER(_pfx, _n, _name)                            \
> +       static void _pfx ## _n ## _gen_syndrome(int disks,              \
>                                         size_t bytes, void **ptrs)      \
>         {                                                               \
> -               void raid6_neon ## _n  ## _gen_syndrome_real(int,       \
> -                                               unsigned long, void**); \
>                 scoped_ksimd()                                          \
> -                       raid6_neon ## _n ## _gen_syndrome_real(disks,   \
> +                       _pfx ## _n ## _gen_syndrome_real(disks,         \
>                                         (unsigned long)bytes, ptrs);    \
>         }                                                               \
> -       static void raid6_neon ## _n ## _xor_syndrome(int disks,        \
> -                                       int start, int stop,            \
> +       static void _pfx ## _n ## _xor_syndrome(int disks,              \
> +                                       int start, int stop,            \
>                                         size_t bytes, void **ptrs)      \
>         {                                                               \
> -               void raid6_neon ## _n  ## _xor_syndrome_real(int,       \
> -                               int, int, unsigned long, void**,        \
> -                               const u8 *);                            \
>                 scoped_ksimd()                                          \
> -                       raid6_neon ## _n ## _xor_syndrome_real(disks,   \
> +                       _pfx ## _n ## _xor_syndrome_real(disks,         \
>                                 start, stop, (unsigned long)bytes, ptrs,\
>                                 raid6_vgfmul[raid6_gfexp[start]]);      \
>         }                                                               \
> -       struct raid6_calls const raid6_neonx ## _n = {                  \
> -               .gen_syndrome   = raid6_neon ## _n ## _gen_syndrome,    \
> -               .xor_syndrome   = raid6_neon ## _n ## _xor_syndrome,    \
> -               .name           = "neonx" #_n,                          \
> +       struct raid6_calls const raid6_ ## _name = {                    \
> +               .gen_syndrome   = _pfx ## _n ## _gen_syndrome,          \
> +               .xor_syndrome   = _pfx ## _n ## _xor_syndrome,          \
> +               .name           = #_name,                               \
>         }
>
> -RAID6_NEON_WRAPPER(1);
> -RAID6_NEON_WRAPPER(2);
> -RAID6_NEON_WRAPPER(4);
> -RAID6_NEON_WRAPPER(8);
> +RAID6_NEON_WRAPPER(raid6_neon, 1, neonx1);
> +RAID6_NEON_WRAPPER(raid6_neon, 2, neonx2);
> +RAID6_NEON_WRAPPER(raid6_neon, 4, neonx4);
> +RAID6_NEON_WRAPPER(raid6_neon, 8, neonx8);
> +
> +#ifdef CONFIG_ARM64
> +/* versions using the EOR3 instruction from the SHA3 extension */
> +RAID6_NEON_WRAPPER(raid6_eor3_, 1, eor3x1);
> +RAID6_NEON_WRAPPER(raid6_eor3_, 2, eor3x2);
> +RAID6_NEON_WRAPPER(raid6_eor3_, 4, eor3x4);
> +RAID6_NEON_WRAPPER(raid6_eor3_, 8, eor3x8);
> +#endif
> diff --git a/lib/raid/raid6/arm/neon.h b/lib/raid/raid6/arm/neon.h
> index a9977ec57f27..a8fc4ba06944 100644
> --- a/lib/raid/raid6/arm/neon.h
> +++ b/lib/raid/raid6/arm/neon.h
> @@ -16,6 +16,22 @@ void raid6_neon8_gen_syndrome_real(int disks, unsigned long bytes, void **ptrs);
>  void raid6_neon8_xor_syndrome_real(int disks, int start, int stop,
>                                     unsigned long bytes, void **ptrs,
>                                     const uint8_t *gmul);
> +void raid6_eor3_1_gen_syndrome_real(int disks, unsigned long bytes, void **ptrs);
> +void raid6_eor3_1_xor_syndrome_real(int disks, int start, int stop,
> +                                   unsigned long bytes, void **ptrs,
> +                                   const uint8_t *gmul);
> +void raid6_eor3_2_gen_syndrome_real(int disks, unsigned long bytes, void **ptrs);
> +void raid6_eor3_2_xor_syndrome_real(int disks, int start, int stop,
> +                                   unsigned long bytes, void **ptrs,
> +                                   const uint8_t *gmul);
> +void raid6_eor3_4_gen_syndrome_real(int disks, unsigned long bytes, void **ptrs);
> +void raid6_eor3_4_xor_syndrome_real(int disks, int start, int stop,
> +                                   unsigned long bytes, void **ptrs,
> +                                   const uint8_t *gmul);
> +void raid6_eor3_8_gen_syndrome_real(int disks, unsigned long bytes, void **ptrs);
> +void raid6_eor3_8_xor_syndrome_real(int disks, int start, int stop,
> +                                   unsigned long bytes, void **ptrs,
> +                                   const uint8_t *gmul);
>  void __raid6_2data_recov_neon(int bytes, uint8_t *p, uint8_t *q, uint8_t *dp,
>                               uint8_t *dq, const uint8_t *pbmul,
>                               const uint8_t *qmul);
> diff --git a/lib/raid/raid6/arm/neon.uc b/lib/raid/raid6/arm/neon.uc
> index 6f74e2b82fc4..e1084a8d3a18 100644
> --- a/lib/raid/raid6/arm/neon.uc
> +++ b/lib/raid/raid6/arm/neon.uc
> @@ -33,6 +33,10 @@
>   * Since this reduction is linear, the bits shifted out of all terms can be
>   * XORed together first, and reduced using a single polynomial multiplication
>   * by 0x1d, which is exact as long as no more than 4 bits are shifted out.
> + *
> + * When built with RAID6_NEON_EOR3 defined, this file is compiled with the
> + * arm64 SHA3 extension enabled, allowing the compiler to fold the additions
> + * into three-way XOR (EOR3) instructions.
>   */
>
>  #include <arm_neon.h>
> @@ -43,7 +47,19 @@ typedef uint8x16_t unative_t;
>
>  #define NSIZE  sizeof(unative_t)
>
> +#ifdef RAID6_NEON_EOR3
> +#define RAID6_NEON_FN(n, fn)   raid6_eor3_ ## n ## _ ## fn
> +
> +/*
> + * The compiler emits EOR3 for a ^ b ^ c by itself, but recent GCC versions
> + * reassociate the XORs into the loop carried P accumulator such that EOR3
> + * can no longer be used, so spell it out for that case.
> + */
> +#define XOR3(a, b, c)          veor3q_u8(a, b, c)
> +#else
>  #define RAID6_NEON_FN(n, fn)   raid6_neon ## n ## _ ## fn
> +#define XOR3(a, b, c)          ((a) ^ (b) ^ (c))
> +#endif
>
>  /*
>   * The SHLBYTE() operation shifts each byte left by 1, *not*
> @@ -116,7 +132,7 @@ static inline void STEP2(unative_t *wp, unative_t *wq, const uint8_t *s1,
>         unative_t d1 = vld1q_u8(s1);
>         unative_t d0 = vld1q_u8(s0);
>
> -       *wp ^= d1 ^ d0;
> +       *wp = XOR3(*wp, d1, d0);
>         *wq = HORNER2(*wq, d1, d0, x1d);
>  }
>
> @@ -129,7 +145,7 @@ static inline void STEP4(unative_t *wp, unative_t *wq, const uint8_t *s3,
>         unative_t d1 = vld1q_u8(s1);
>         unative_t d0 = vld1q_u8(s0);
>
> -       *wp ^= d3 ^ d2 ^ d1 ^ d0;
> +       *wp = XOR3(XOR3(*wp, d3, d2), d1, d0);
>         *wq = HORNER4(*wq, d3, d2, d1, d0, x1d);
>  }
>
> diff --git a/lib/raid/raid6/arm64/pq_arch.h b/lib/raid/raid6/arm64/pq_arch.h
> index 27ff564d7594..4e397aa36480 100644
> --- a/lib/raid/raid6/arm64/pq_arch.h
> +++ b/lib/raid/raid6/arm64/pq_arch.h
> @@ -1 +1,33 @@
> -#include "arm/pq_arch.h"
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#include <linux/cpufeature.h>
> +#include <asm/neon.h>
> +
> +extern const struct raid6_calls raid6_neonx1;
> +extern const struct raid6_calls raid6_neonx2;
> +extern const struct raid6_calls raid6_neonx4;
> +extern const struct raid6_calls raid6_neonx8;
> +extern const struct raid6_calls raid6_eor3x1;
> +extern const struct raid6_calls raid6_eor3x2;
> +extern const struct raid6_calls raid6_eor3x4;
> +extern const struct raid6_calls raid6_eor3x8;
> +extern const struct raid6_recov_calls raid6_recov_neon;
> +
> +static __always_inline void __init arch_raid6_init(void)
> +{
> +       raid6_algo_add_default();
> +       if (cpu_has_neon()) {
> +               if (cpu_have_named_feature(SHA3)) {
> +                       raid6_algo_add(&raid6_eor3x1);
> +                       raid6_algo_add(&raid6_eor3x2);
> +                       raid6_algo_add(&raid6_eor3x4);
> +                       raid6_algo_add(&raid6_eor3x8);
> +               } else {
> +                       raid6_algo_add(&raid6_neonx1);
> +                       raid6_algo_add(&raid6_neonx2);
> +                       raid6_algo_add(&raid6_neonx4);
> +                       raid6_algo_add(&raid6_neonx8);
> +               }
> +               raid6_recov_algo_add(&raid6_recov_neon);
> +       }
> +}
> --
> 2.56.1.708.g77570a6a1d-goog
>



More information about the linux-arm-kernel mailing list