arm64: csum: Add fused copy and Internet checksum
Subsystems (4)
cover arm64: csum: Add fused copy and Internet checksum
arm64 currently uses the generic csum_partial_copy_nocheck(), which performs memcpy() followed by a second pass for csum_partial(). This double pass exerts unnecessary pressure on the L1 cache.
Replace it with a single-pass implementation. The new implementation provides a general-purpose register path for short buffers and atomic contexts, and a kernel-mode NEON path for lengths >= 1024 bytes.
Measured in-kernel on an Ampere Altra (Neoverse-N1):
- Scalar path: 1.2x-1.6x faster for lengths < 1024 bytes.
- NEON path: 1.2x faster at 1024 bytes, scaling up to 1.6x-1.8x at 4096 bytes.
On Apple M-series cores, gains are 1.3-1.7x below 1024 bytes and 1.6-2.4x above. No length or alignment regresses on either microarchitecture.
Patch 1 implements the fused routines and the dispatcher. Patch 2 adds KUnit test coverage for the new API and internal paths.
Tested: in-kernel benchmark module on Neoverse-N1 with both implementations cross-checked (0 mismatches); KUnit suite under QEMU (with/without KASAN, with PREEMPT_RT), exhaustive and random userspace testing of both routines against a naive reference with PROT_NONE guard pages, gcc 13 and clang 18 W=1 builds, checkpatch --strict.
Demian Shulhan (2):
arm64: csum: Add fused copy and Internet checksum lib/tests: checksum: Add KUnit tests for csum_partial_copy_nocheck()
arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/Makefile | 7 +- arch/arm64/lib/csum-copy-neon.c | 168 +++++++++++ arch/arm64/lib/csum-copy.c | 108 +++++++ arch/arm64/lib/csum-copy.h | 84 ++++++ arch/arm64/lib/csum.c | 54 ++++ lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 463 ++++++++++++++++++++++++++++++ 8 files changed, 896 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/lib/csum-copy-neon.c create mode 100644 arch/arm64/lib/csum-copy.c create mode 100644 arch/arm64/lib/csum-copy.h
base-commit: 93f51579e7df248780214094418f205253383cc5
Signature · 1 line
2.43.0
PATCH 1/2 arm64: csum: Add fused copy and Internet checksum
arm64 currently falls back to the generic csum_partial_copy_nocheck(), requiring two passes over the data (memcpy + csum_partial).
Replace it with a single-pass implementation. The implementation provides two paths handling any src/dst alignment:
1. __csum_partial_copy_scalar(): Uses general-purpose registers, processing 32 bytes per iteration through four independent ones' complement accumulators. Used below the NEON threshold or when may_use_simd() is false.
2. __csum_partial_copy_neon(): Uses UADDLP/UADALP accumulating into two 64-bit accumulators, processing 64 bytes per iteration.
The NEON threshold is fixed at 1024 bytes (CSUM_COPY_NEON_MIN_LEN). Boot-time calibration is avoided due to measurement noise and big.LITTLE asymmetries. 1024 bytes ensures MTU-sized buffers utilize the NEON path while avoiding kernel_neon_begin() overhead for small payloads.
All loads remain within [src, src + len) and stores within [dst, dst + len), requiring no KASAN annotations. To guarantee the returned checksum matches dst even under concurrent source modification (e.g., MSG_ZEROCOPY), overlapping tail bytes are deliberately copied before the body.
Performance:
1) Ampere Altra (Neoverse-N1, MIDR part 0xd0c), GCP t2a-standard-2, Ubuntu 24.04, kernel 7.0.0-1011-gcp, gcc 13.3.
Measured in-kernel (minimum over 20000 calls, ns per call):
align len | generic | new | speedup aligned 64 | 14.7 | 11.0 | 1.33x aligned 128 | 18.7 | 13.8 | 1.36x aligned 256 | 27.7 | 19.4 | 1.42x aligned 512 | 42.9 | 31.5 | 1.36x aligned 1000 | 72.0 | 54.9 | 1.31x aligned 1023 | 73.8 | 57.9 | 1.27x aligned 1024 | 73.0 | 59.8 | 1.21x aligned 1500 | 102.1 | 73.8 | 1.38x aligned 2048 | 134.6 | 91.2 | 1.47x aligned 4096 | 257.1 | 153.4 | 1.67x src+1/dst+0 1500 | 118.2 | 78.9 | 1.49x src+1/dst+0 4096 | 300.8 | 164.3 | 1.83x src+0/dst+1 1500 | 117.6 | 79.7 | 1.47x src+0/dst+1 4096 | 303.8 | 167.4 | 1.81x src+15/dst+15 64 | 16.0 | 11.0 | 1.44x src+15/dst+15 512 | 42.9 | 33.1 | 1.29x src+15/dst+15 1023 | 71.5 | 61.3 | 1.16x src+15/dst+15 1024 | 72.5 | 61.9 | 1.17x src+15/dst+15 1500 | 99.7 | 77.1 | 1.29x src+15/dst+15 4096 | 255.5 | 156.3 | 1.63x
Scalar vs NEON break-even on the same core (NEON includes kernel_neon_begin()/end()):
align len | scalar | neon+begin/end aligned 512 | 29.6 | 40.6 aligned 768 | 42.2 | 48.8 aligned 1024 | 54.9 | 56.6 aligned 1280 | 67.4 | 64.6
2) Apple M-series core (MIDR implementer 0x61).
Userspace harness, fixed CPU affinity, minimum over 3 runs x 9 batches (ns per call):
align len | generic scalar neon | new | speedup aligned 20 | 2.8 1.8 - | 1.8 | 1.56x aligned 40 | 2.8 1.7 - | 1.7 | 1.65x aligned 64 | 3.0 2.0 - | 2.0 | 1.50x aligned 128 | 4.6 3.1 2.5 | 3.1 | 1.48x aligned 256 | 14.8 5.1 3.4 | 5.1 | 2.90x aligned 512 | 12.8 9.6 5.3 | 9.6 | 1.33x aligned 1000 | 26.6 20.7 8.2 | 20.7 | 1.29x aligned 1024 | 24.4 18.9 8.2 | 15.2 | 1.61x aligned 1500 | 35.2 30.7 13.0 | 20.0 | 1.76x aligned 2048 | 53.1 41.7 17.3 | 24.3 | 2.19x aligned 4096 | 104.1 85.6 37.9 | 44.9 | 2.32x
Testing: All sizes and alignments cross-checked against memcpy()+csum_partial() (0 mismatches). Concurrent-writer test confirms checksum(dst) == returned sum under source modification (2M iterations, 0 mismatches). Builds clean with gcc 13 and clang 18 at W=1; checkpatch --strict clean.
--- arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/Makefile | 7 +- arch/arm64/lib/csum-copy-neon.c | 168 ++++++++++++++++++++++++++++++ arch/arm64/lib/csum-copy.c | 108 +++++++++++++++++++ arch/arm64/lib/csum-copy.h | 84 +++++++++++++++ arch/arm64/lib/csum.c | 54 ++++++++++ 6 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/lib/csum-copy-neon.c create mode 100644 arch/arm64/lib/csum-copy.c create mode 100644 arch/arm64/lib/csum-copy.h
arch/arm64/include/asm/checksum.h +3 -0
@@ -44,6 +44,9 @@ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)extern unsigned int do_csum (const unsigned char *buff,int len);#define do_csum do_csum +#define _HAVE_ARCH_CSUM_AND_COPY +__wsum csum_partial_copy_nocheck (const void *src,void *dst,int len); +#include <asm-generic/checksum.h> #endif /* __ASM_CHECKSUM_H */
arch/arm64/lib/Makefile +6 -1
@@ -5,10 +5,15 @@ KCSAN_SANITIZE_delay.o := n lib-y := clear_user.o delay.o copy_from_user.o \ copy_to_user.o copy_page.o \ - clear_page.o csum.o insn.o memchr.o memcpy.o \ + clear_page.o csum.o csum-copy.o csum-copy-neon.o \ + insn.o memchr.o memcpy.o \ memset.o memcmp.o strcmp.o strncmp.o strlen.o \ strnlen.o strchr.o strrchr.o tishift.o +# NEON intrinsics; only ever called between kernel_neon_begin()/end(). +CFLAGS_csum-copy-neon.o += $(CC_FLAGS_FPU) +CFLAGS_REMOVE_csum-copy-neon.o += $(CC_FLAGS_NO_FPU) + lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
arch/arm64/lib/csum-copy-neon.c new file +168 -0
168 lines not on this page · raw message
arch/arm64/lib/csum-copy.c new file +108 -0
108 lines not on this page · raw message
arch/arm64/lib/csum-copy.h new file +84 -0
84 lines not on this page · raw message
arch/arm64/lib/csum.c +54 -0
68 lines not on this page · raw message
Signature · 1 line
2.43.0
Hi Demian,
Quoted 6 lines · Demian Shulhan
arch/arm64/lib/csum-copy-neon.c new file +0
-0@@ -0,0 +1,168 @@
...
arch/arm64/lib/csum-copy-neon.c line 56
Quoted 2 lines
arch/arm64/lib/csum-copy-neon.c new file +2
-0+#include <linux/types.h> +#include <asm/neon-intrinsics.h>
Please avoid combining these files, it will be forbidden soon:
https://lore.kernel.org/[email protected]/
Signature · 2 lines
Cheers, Nathan
PATCH 2/2 lib/tests: checksum: Add KUnit tests for csum_partial_copy_nocheck()
In reply to Demian ShulhanThe existing checksum KUnit suite covers csum_partial(), csum_fold(), ip_fast_csum(), and csum_ipv6_magic(), but lacks coverage for csum_partial_copy_nocheck().
Add tests to exercise the public csum_partial_copy_nocheck() API. The length and misalignment sweeps are structured to reach all internal branches of the arm64 implementation (scalar ladder, NEON switch-over, unaligned heads/tails) without requiring arch-specific #ifdefs.
Test cases include:
- 0..64 bytes with all 16x16 src/dst misalignments.
- Threshold boundary lengths (1008..1040) and overlapping tail combinations (1024..1103).
- Data patterns stressing carries (all ones), zero carries, and byte-swap sensitivity.
- Concurrent execution from task, softirq, and hardirq contexts to exercise may_use_simd() fallbacks.
- Throughput benchmark against memcpy() + csum_partial() (gated by CONFIG_CHECKSUM_KUNIT_BENCHMARK).
Buffers are vmalloc()ed with page-aligned lengths. The source is placed flush against the guard page to catch over-reads; test_csum_copy_dst_guard_page() does the same for the destination. Each call is checked against an independent naive reference checksum, the destination is compared with the source, and canary bytes on both sides are verified untouched.
Tested under QEMU on arm64 with CONFIG_CHECKSUM_KUNIT built-in, with CONFIG_KASAN=y and with CONFIG_PREEMPT_RT=y (all tests pass); the file also builds as a module (CONFIG_KUNIT=m) with gcc 13 and clang 18 at W=1.
--- lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 463 +++++++++++++++++++++++++++++++++++++ 2 files changed, 473 insertions(+)
lib/Kconfig.debug +10 -0
@@ -2736,6 +2736,16 @@ config CHECKSUM_KUNIT If unsure, say N. +config CHECKSUM_KUNIT_BENCHMARK + bool "Benchmark for the checksum functions" + depends on CHECKSUM_KUNIT + help + Include a benchmark of csum_partial_copy_nocheck() against + memcpy() followed by csum_partial() in the checksum KUnit test + suite, for a range of buffer lengths and misalignments. The + throughput of both is logged as test output. Only meaningful on + real hardware; not intended for production builds. + config UTIL_MACROS_KUNIT tristate "KUnit test util_macros.h functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT
lib/tests/checksum_kunit.c +463 -0
489 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 3 lines · On Sun, 27 Sep 2026 15:17:56 +0200 Demian Shulhan <[email protected]> wrote:
arm64 currently uses the generic
csum_partial_copy_nocheck(), which performsmemcpy()followed by a second pass forcsum_partial(). This double pass exerts unnecessary pressure on the L1 cache.
Which workload actually needs this?
Most modern ethernet MAC support checksum setting on transmit and checking on receive.
So the software checksum shouldn't be needed very often.
IIRC there is also code to defer UDP checksum validation until the copy_to_user().
I'd bet (a few pints of beer) that the complication this adds isn't actually worth while.
Even Linus can't remember why it was done, my guess is it improved the performance of the userspace NFS (over UDP) daemon that would be doing 8k UDP send/receive (fragmented by IP).
There is certainly still code to checksum data during copy_from_user() in send().
Last time I looked I couldn't see why send on TCP sockets didn't go through it. On x86 (in particular) copies can be done far faster than ones that include a checksum.
David
Quoted 41 lines
Replace it with a single-pass implementation. The new implementation provides a general-purpose register path for short buffers and atomic contexts, and a kernel-mode NEON path for lengths >= 1024 bytes.
Measured in-kernel on an Ampere Altra (Neoverse-N1):
- Scalar path: 1.2x-1.6x faster for lengths < 1024 bytes.
- NEON path: 1.2x faster at 1024 bytes, scaling up to 1.6x-1.8x at 4096 bytes.
On Apple M-series cores, gains are 1.3-1.7x below 1024 bytes and 1.6-2.4x above. No length or alignment regresses on either microarchitecture.Patch 1 implements the fused routines and the dispatcher. Patch 2 adds KUnit test coverage for the new API and internal paths.
Tested: in-kernel benchmark module on Neoverse-N1 with both implementations cross-checked (0 mismatches); KUnit suite under QEMU (with/without KASAN, with
PREEMPT_RT), exhaustive and random userspace testing of both routines against a naive reference withPROT_NONEguard pages, gcc 13 and clang 18 W=1 builds, checkpatch --strict.Demian Shulhan (2):
arm64: csum: Add fused copy and Internet checksum lib/tests: checksum: Add KUnit tests forcsum_partial_copy_nocheck()arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/Makefile | 7 +- arch/arm64/lib/csum-copy-neon.c | 168 +++++++++++ arch/arm64/lib/csum-copy.c | 108 +++++++ arch/arm64/lib/csum-copy.h | 84 ++++++ arch/arm64/lib/csum.c | 54 ++++ lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 463 ++++++++++++++++++++++++++++++ 8 files changed, 896 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/lib/csum-copy-neon.c create mode 100644 arch/arm64/lib/csum-copy.c create mode 100644 arch/arm64/lib/csum-copy.hbase-commit:
93f51579e7df248780214094418f205253383cc5
Hi David,
Sorry for the delay, I was in the middle of a company change.
You are right about SG and TX csum, but only when the traffic goes straight to a NIC that offloads them. There is another side: software GSO (skb_segment() with !sg && !csum) hits this path on 100% of TCP payload over WireGuard (wg_xmit() -> skb_gso_segment(skb, 0)), and on drivers without SG/TX csum (u_ether/f_ncm, cdc_ncm, mt76, rtw88/89, ath9k).
So in general the software copy+csum path is still alive.
On the other hand, NEON is useless here. skb_segment() splits GSO packets into 1-MSS segments before copying, so on these paths calls never exceed 1448 bytes (1368 on WireGuard). At that size kernel_neon_begin()/end() costs more than NEON saves, on real hardware v1 was ~3% slower in cycles/byte than the scalar loop on the slower cores. So that part can go.
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
Thanks,
Demian
нд, 27 вер. 2026 р. о 19:44 David Laight <[email protected]> пише:
Quoted 71 lines
Quoted 3 lines · On Sun, 27 Sep 2026 15:17:56 +0200 Demian Shulhan <[email protected]> wrote:
arm64 currently uses the generic
csum_partial_copy_nocheck(), which performsmemcpy()followed by a second pass forcsum_partial(). This double pass exerts unnecessary pressure on the L1 cache.Which workload actually needs this?
Most modern ethernet MAC support checksum setting on transmit and checking on receive.
So the software checksum shouldn't be needed very often.IIRC there is also code to defer UDP checksum validation until the
copy_to_user().
I'd bet (a few pints of beer) that the complication this adds isn't actually worth while.
Even Linus can't remember why it was done, my guess is it improved the performance of the userspace NFS (over UDP) daemon that would be doing 8k UDP send/receive (fragmented by IP).There is certainly still code to checksum data during
copy_from_user()insend().
Last time I looked I couldn't see why send on TCP sockets didn't go through it. On x86 (in particular) copies can be done far faster than ones that include a checksum.David
Quoted 41 lines
Replace it with a single-pass implementation. The new implementation provides a general-purpose register path for short buffers and atomic contexts, and a kernel-mode NEON path for lengths >= 1024 bytes.
Measured in-kernel on an Ampere Altra (Neoverse-N1):
- Scalar path: 1.2x-1.6x faster for lengths < 1024 bytes.
- NEON path: 1.2x faster at 1024 bytes, scaling up to 1.6x-1.8x at 4096 bytes.
On Apple M-series cores, gains are 1.3-1.7x below 1024 bytes and 1.6-2.4x above. No length or alignment regresses on either microarchitecture.Patch 1 implements the fused routines and the dispatcher. Patch 2 adds KUnit test coverage for the new API and internal paths.
Tested: in-kernel benchmark module on Neoverse-N1 with both implementations cross-checked (0 mismatches); KUnit suite under QEMU (with/without KASAN, with
PREEMPT_RT), exhaustive and random userspace testing of both routines against a naive reference withPROT_NONEguard pages, gcc 13 and clang 18 W=1 builds, checkpatch --strict.Demian Shulhan (2):
arm64: csum: Add fused copy and Internet checksum lib/tests: checksum: Add KUnit tests forcsum_partial_copy_nocheck()arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/Makefile | 7 +- arch/arm64/lib/csum-copy-neon.c | 168 +++++++++++ arch/arm64/lib/csum-copy.c | 108 +++++++ arch/arm64/lib/csum-copy.h | 84 ++++++ arch/arm64/lib/csum.c | 54 ++++ lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 463 ++++++++++++++++++++++++++++++ 8 files changed, 896 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/lib/csum-copy-neon.c create mode 100644 arch/arm64/lib/csum-copy.c create mode 100644 arch/arm64/lib/csum-copy.hbase-commit:
93f51579e7df248780214094418f205253383cc5
Quoted 4 lines · On Sun, 1 Nov 2026 14:33:49 +0100 Demian Shulhan <[email protected]> wrote:
Hi David,
Sorry for the delay, I was in the middle of a company change.
Quoted 8 lines
You are right about SG and TX csum, but only when the traffic goes straight to a NIC that offloads them. There is another side: software GSO (
skb_segment()with !sg && !csum) hits this path on 100% of TCP payload over WireGuard (wg_xmit()->skb_gso_segment(skb, 0)), and on drivers without SG/TX csum (u_ether/f_ncm,cdc_ncm, mt76, rtw88/89, ath9k).So in general the software copy+csum path is still alive.
I was partially worried that someone had just looked at the code and noticed that arm didn't have the code to do checksum+copy and decided to write it.
David
Quoted 94 lines
On the other hand, NEON is useless here.
skb_segment()splits GSO packets into 1-MSS segments before copying, so on these paths calls never exceed 1448 bytes (1368 on WireGuard). At that size kernel_neon_begin()/end() costs more than NEON saves, on real hardware v1 was ~3% slower in cycles/byte than the scalar loop on the slower cores. So that part can go.I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
Thanks,
Demianнд, 27 вер. 2026 р. о 19:44 David Laight <[email protected]> пише:
Quoted 71 lines
Quoted 3 lines · On Sun, 27 Sep 2026 15:17:56 +0200 Demian Shulhan <[email protected]> wrote:
arm64 currently uses the generic
csum_partial_copy_nocheck(), which performsmemcpy()followed by a second pass forcsum_partial(). This double pass exerts unnecessary pressure on the L1 cache.Which workload actually needs this?
Most modern ethernet MAC support checksum setting on transmit and checking on receive.
So the software checksum shouldn't be needed very often.IIRC there is also code to defer UDP checksum validation until the
copy_to_user().
I'd bet (a few pints of beer) that the complication this adds isn't actually worth while.
Even Linus can't remember why it was done, my guess is it improved the performance of the userspace NFS (over UDP) daemon that would be doing 8k UDP send/receive (fragmented by IP).There is certainly still code to checksum data during
copy_from_user()insend().
Last time I looked I couldn't see why send on TCP sockets didn't go through it. On x86 (in particular) copies can be done far faster than ones that include a checksum.David
Quoted 41 lines
Replace it with a single-pass implementation. The new implementation provides a general-purpose register path for short buffers and atomic contexts, and a kernel-mode NEON path for lengths >= 1024 bytes.
Measured in-kernel on an Ampere Altra (Neoverse-N1):
- Scalar path: 1.2x-1.6x faster for lengths < 1024 bytes.
- NEON path: 1.2x faster at 1024 bytes, scaling up to 1.6x-1.8x at 4096 bytes.
On Apple M-series cores, gains are 1.3-1.7x below 1024 bytes and 1.6-2.4x above. No length or alignment regresses on either microarchitecture.Patch 1 implements the fused routines and the dispatcher. Patch 2 adds KUnit test coverage for the new API and internal paths.
Tested: in-kernel benchmark module on Neoverse-N1 with both implementations cross-checked (0 mismatches); KUnit suite under QEMU (with/without KASAN, with
PREEMPT_RT), exhaustive and random userspace testing of both routines against a naive reference withPROT_NONEguard pages, gcc 13 and clang 18 W=1 builds, checkpatch --strict.Demian Shulhan (2):
arm64: csum: Add fused copy and Internet checksum lib/tests: checksum: Add KUnit tests forcsum_partial_copy_nocheck()arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/Makefile | 7 +- arch/arm64/lib/csum-copy-neon.c | 168 +++++++++++ arch/arm64/lib/csum-copy.c | 108 +++++++ arch/arm64/lib/csum-copy.h | 84 ++++++ arch/arm64/lib/csum.c | 54 ++++ lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 463 ++++++++++++++++++++++++++++++ 8 files changed, 896 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/lib/csum-copy-neon.c create mode 100644 arch/arm64/lib/csum-copy.c create mode 100644 arch/arm64/lib/csum-copy.hbase-commit:
93f51579e7df248780214094418f205253383cc5
Quoted 8 lines · Demian Shulhan
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Quoted 9 lines · On Mon, 2 Nov 2026 13:44:37 +0200 saeed bishara <[email protected]>…
Quoted 8 lines · Demian Shulhan
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 6 lines
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly
The x86 asm version manages to do more than one 'add with carry' every clock. That needs the massive out-of-order execution unit and the ability to have multiple independent 'flags' registers live at the same time. The generic C do_csum() (I just looked at lib/checksum.c) is horrid There are three dependent alu operations per iteration.
It is likely that the pipelineable:
u64 sum64_a(unsigned char *buf, unsigned long count)
{
u64 val, sum = 0, carry = 0;
buf += count;
count = -count;
do {
val = *(u64 *)(buf + count);
sum += val;
carry += sum < val;
} while ((count += 8));
sum += carry;
sum += sum < carry;
return sum;
}
or:
u64 sum64_b(unsigned char *buf, unsigned long count)
{
u64 val, sum = 0, carry = 0;
unsigned char *end = buf + count;
do {
val = *(u64 *)(buf);
sum += val;
carry += sum < val;
} while ((buf += 8) != end);
sum += carry;
sum += sum < carry;
return sum;
}will generate 'pretty good' code https://godbolt.org/z/K9ob6d8vv.
The first needs 'reg+reg' addressing, the second better for mips-like cpu that have 'compare and branch' rather than a flags register.
gcc and clang seem to make a reasonable job of compiling those (provided buf isn't u64 *).
Today it left the 'negative offset from end' loop alone, but it has a nasty habit of pessimising it.
For x86-64 gcc is generating:
.L4:
addq (%rdi,%rdx), %rax
adcq $0, %rcx
addq $8, %rdx
jne .L4I think a zen5 would execute that at one iteration per clock. Intel cpu can only execute two clock loops - so it needs unrolling once. If you unroll it once (with separate sum/carry variables) there is a moderate chance of it running faster than 'one word per clock' (two words/clock is probably wishful thinking, zen5 can do 6 add/clock but some timings I've done imply that memory addressing can use and alu add).
For slower cpu (especially in-order ones with memory read delays) you may need interleave the code, eg giving the loop:
val0 = *(u64 *)buf;
do {
val1 = *(u64 *)(buf + 8);
sum0 += val0;
carry0 += sum0 < val0;
val0 = *(u64 *)(buf + 16);
sum1 += val1;
carry1 += sum1 < val1;
} while ((buf += 16) != end);I didn't try those loops when I was timing the cksum code a few years back.
They have the big advantage of a small loop stride.
David
Quoted 2 lines
optimized implementation.
saeed
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Depends. I think __builtin_addcll() is needed to get roughly the same performance as the asm version. In my tests GCC on arm64 turns it into adds + cset instead of adcs like on x86.
But I will look at it, thanks!
пн, 2 лист. 2026 р. о 12:44 saeed bishara <[email protected]> пише:
Quoted 18 lines · Demian Shulhan
Quoted 8 lines
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Quoted 7 lines · On Mon, 2 Nov 2026 16:14:06 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Depends. I think
__builtin_addcll()is needed to get roughly the same performance as the asm version. In my tests GCC on arm64 turns it into adds + cset instead of adcs like on x86.
But I will look at it, thanks!
The C version I posted earlier has the loop:
buf += count;
count = -count;
sum = carry = 0;
do {
val = *(u64 *)(buf + count);
sum += val;
carry += sum < val;
} while ((count += 8));for aarch64 you get:
.L4:
ldr x1, [x0, x4]
adds x2, x2, x1
cinc x3, x3, cs # adc $0,%rxx on x86
adds x4, x4, 8
bne .L4I suspect that is close to optimal - especially for fast cpu with out of order execution.
https://godbolt.org/z/eso79K7Wq
David
Quoted 20 lines
пн, 2 лист. 2026 р. о 12:44 saeed bishara <[email protected]> пише:
Quoted 18 lines · Demian Shulhan
Quoted 8 lines
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Quoted 6 lines
.L4:
ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4
Agreed, that is a nice loop for the sum-only case, one dependent op on the sum chain and no builtin needed.
For the copy case I tried the same form on x86 (Zen 3, GCC 15, Clang 19-22, -O2) with a store added, against csum-copy_64.S, as posted it ran at 0.85x of the asm with GCC and 0.73-0.76x with Clang (which rewrites it into setb/adc), and ~0.8x unrolled 2x/4x - a bit slower than the posted 4-accumulator loop (0.88-0.95x).
пн, 2 лист. 2026 р. о 17:14 David Laight <[email protected]> пише:
Quoted 58 lines
Quoted 7 lines · On Mon, 2 Nov 2026 16:14:06 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Depends. I think
__builtin_addcll()is needed to get roughly the same performance as the asm version. In my tests GCC on arm64 turns it into adds + cset instead of adcs like on x86.
But I will look at it, thanks!The C version I posted earlier has the loop:
buf += count; count = -count; sum = carry = 0; do { val = *(u64 *)(buf + count); sum += val; carry += sum < val; } while ((count += 8));for aarch64 you get:
.L4:ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4I suspect that is close to optimal - especially for fast cpu with out of order execution.
https://godbolt.org/z/eso79K7Wq
David
Quoted 20 lines
пн, 2 лист. 2026 р. о 12:44 saeed bishara <[email protected]> пише:
Quoted 18 lines · Demian Shulhan
Quoted 8 lines
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Quoted 6 lines · On Tue, 3 Nov 2026 16:47:29 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 6 lines
.L4:
ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4
Quoted 7 lines
Agreed, that is a nice loop for the sum-only case, one dependent op on the sum chain and no builtin needed.
For the copy case I tried the same form on x86 (Zen 3, GCC 15, Clang 19-22, -O2) with a store added, againstcsum-copy_64.S, as posted it ran at 0.85x of the asm with GCC and 0.73-0.76x with Clang (which rewrites it into setb/adc), and ~0.8x unrolled 2x/4x - a bit slower than the posted 4-accumulator loop (0.88-0.95x).
I just added *(u64 *)(dest + count) = val to one of my loops. gcc just replaces the 'add from memory' with mov/mov/add.
I'm now confused though, I seem to be getting different output from earlier. With -Os (which I didn't think I was passing) clang is refusing to do the 'offset from end of buffer' loop.
With -O2 it bloats everything by unrolling 4 times and adding lots of extra instructions.
(AFAICT the optimiser has a completely incorrect cpu model.)
If the compiler doesn't generate the expected code it is all pointless. The xor parity code loop had to be in the asm block to stop the compiler changing it to a much slower form.
For the checksum code I have managed to work out how many bytes/clock the code was generating and getting it to (reasonably) match the expected values.
But you have to use relatively short buffers to avoid D-cache effects.
David
Quoted 62 lines
пн, 2 лист. 2026 р. о 17:14 David Laight <[email protected]> пише:
Quoted 58 lines
Quoted 7 lines · On Mon, 2 Nov 2026 16:14:06 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Depends. I think
__builtin_addcll()is needed to get roughly the same performance as the asm version. In my tests GCC on arm64 turns it into adds + cset instead of adcs like on x86.
But I will look at it, thanks!The C version I posted earlier has the loop:
buf += count; count = -count; sum = carry = 0; do { val = *(u64 *)(buf + count); sum += val; carry += sum < val; } while ((count += 8));for aarch64 you get:
.L4:ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4I suspect that is close to optimal - especially for fast cpu with out of order execution.
https://godbolt.org/z/eso79K7Wq
David
Quoted 20 lines
пн, 2 лист. 2026 р. о 12:44 saeed bishara <[email protected]> пише:
Quoted 18 lines · Demian Shulhan
Quoted 8 lines
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Hi David, Saeed,
I tested David's sum64() loop, with the store added, on x86-64 and arm64. It is portable and correct on both, but no single C form was fast on both:
- x86-64, vs csum-copy_64.S (GCC 15, Clang 19-22): with dst alignment and 64 bytes per pass it reaches 0.89-0.94 of the asm. The form costs two ALU ops per word (add + adc $0) against one adc in the asm, so even a hand-written asm version of it stays at 0.94-0.98.
- arm64, vs the scalar loop from my series (four accumulators, res + (res < data)): the same 64-bit loop is 0.90x, while the same loop on 128-bit val/sum with 2-4 independent lanes is 1.19-1.24x. On x86 those 128-bit lanes drop to 0.40-0.52 of the asm with GCC.
So I'll keep it in arch/arm64, and x86 keeps its asm. In the next version I'll move the loop to 128-bit val/sum with independent lanes, the same way arm64's do_csum() already works on __uint128_t.
Demian
вт, 3 лист. 2026 р. о 23:38 David Laight <[email protected]> пише:
Quoted 102 lines
Quoted 6 lines · On Tue, 3 Nov 2026 16:47:29 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 6 lines
.L4:
ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4Quoted 7 lines
Agreed, that is a nice loop for the sum-only case, one dependent op on the sum chain and no builtin needed.
For the copy case I tried the same form on x86 (Zen 3, GCC 15, Clang 19-22, -O2) with a store added, againstcsum-copy_64.S, as posted it ran at 0.85x of the asm with GCC and 0.73-0.76x with Clang (which rewrites it into setb/adc), and ~0.8x unrolled 2x/4x - a bit slower than the posted 4-accumulator loop (0.88-0.95x).I just added *(u64 *)(dest + count) = val to one of my loops. gcc just replaces the 'add from memory' with mov/mov/add.
I'm now confused though, I seem to be getting different output from earlier. With -Os (which I didn't think I was passing) clang is refusing to do the 'offset from end of buffer' loop.
With -O2 it bloats everything by unrolling 4 times and adding lots of extra instructions.
(AFAICT the optimiser has a completely incorrect cpu model.)If the compiler doesn't generate the expected code it is all pointless. The xor parity code loop had to be in the asm block to stop the compiler changing it to a much slower form.
For the checksum code I have managed to work out how many bytes/clock the code was generating and getting it to (reasonably) match the expected values.
But you have to use relatively short buffers to avoid D-cache effects.David
Quoted 62 lines
пн, 2 лист. 2026 р. о 17:14 David Laight <[email protected]> пише:
Quoted 58 lines
Quoted 7 lines · On Mon, 2 Nov 2026 16:14:06 +0100 Demian Shulhan <[email protected]> wrote:
Quoted 3 lines
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
Depends. I think
__builtin_addcll()is needed to get roughly the same performance as the asm version. In my tests GCC on arm64 turns it into adds + cset instead of adcs like on x86.
But I will look at it, thanks!The C version I posted earlier has the loop:
buf += count; count = -count; sum = carry = 0; do { val = *(u64 *)(buf + count); sum += val; carry += sum < val; } while ((count += 8));for aarch64 you get:
.L4:ldr x1, [x0, x4] adds x2, x2, x1 cinc x3, x3, cs # adc $0,%rxx on x86 adds x4, x4, 8 bne .L4I suspect that is close to optimal - especially for fast cpu with out of order execution.
https://godbolt.org/z/eso79K7Wq
David
Quoted 20 lines
пн, 2 лист. 2026 р. о 12:44 saeed bishara <[email protected]> пише:
Quoted 18 lines · Demian Shulhan
Quoted 8 lines
I would prefer to keep only the scalar version of the fused copy+csum. It reads the data once instead of twice, and on real Armv9 hardware it cuts end-to-end cycles/byte for bulk TCP over WireGuard by 3-12% depending on the core (+6-8% throughput). It uses only general-purpose registers, so there is no FPSIMD state to save and it is safe in any context.
Quoted 4 lines
If you are OK with the scalar path, I can send v2 with NEON dropped, the scalar path only, and the KUnit fix for the x86 zero-length case Jakub reported.
The scalar path seems like generic C code and not restricted to arm64, curious how that performs against existing (e.g. x86) assembly optimized implementation.
saeed
Jakub Kicinski
maintainerQuoted 16 lines · On Sun, 27 Sep 2026 15:17:56 +0200 Demian Shulhan wrote:
arm64 currently uses the generic
csum_partial_copy_nocheck(), which performsmemcpy()followed by a second pass forcsum_partial(). This double pass exerts unnecessary pressure on the L1 cache.Replace it with a single-pass implementation. The new implementation provides a general-purpose register path for short buffers and atomic contexts, and a kernel-mode NEON path for lengths >= 1024 bytes.
Measured in-kernel on an Ampere Altra (Neoverse-N1):
- Scalar path: 1.2x-1.6x faster for lengths < 1024 bytes.
- NEON path: 1.2x faster at 1024 bytes, scaling up to 1.6x-1.8x at 4096 bytes.
On Apple M-series cores, gains are 1.3-1.7x below 1024 bytes and 1.6-2.4x above. No length or alignment regresses on either microarchitecture.
Quoted 9 lines
Patch 1 implements the fused routines and the dispatcher. Patch 2 adds KUnit test coverage for the new API and internal paths.
Tested: in-kernel benchmark module on Neoverse-N1 with both implementations cross-checked (0 mismatches); KUnit suite under QEMU (with/without KASAN, with
PREEMPT_RT), exhaustive and random userspace testing of both routines against a naive reference withPROT_NONEguard pages, gcc 13 and clang 18 W=1 builds, checkpatch --strict.
NIPA CI flagged a regression from this patch: the new "checksum" KUnit suite fails on the x86-64 test kernel (ARCH=x86_64, qemu). Specifically:
test_csum_copy_small_all_alignments (len=0, src_off=0, dst_off=0)
test_csum_copy_patterns (len=1, src_off=0, dst_off=0)
test_csum_copy_zero_len
All three failures involve a zero-length (or very short) copy. Digging into it, x86-64's csum_partial_copy_generic() (arch/x86/lib/csum-copy_64.S) seeds its accumulator with -1 (0xffffffff) rather than 0:
movl $-1, %eax
...
cmpl $8, %ecx
jb .LshortFor len == 0 (and other very short lengths that never execute an add/adc against the seed) it returns that -1 unmodified, which folds to
0. The naive reference used by the new tests, and csum_partial()'s own convention for an empty input, instead treat the "empty checksum" as raw sum 0, which folds to 0xffff. So the new tests' expectations don't match the pre-existing x86-64 assembly implementation for these edge cases.
It looks like the tests were validated against the new arm64 implementation and against memcpy()+csum_partial() under QEMU on arm64, but not run against x86-64's existing csum_partial_copy_nocheck() implementation, which is what our CI kunit runner builds by default.
Could you take a look at either:
- adjusting the zero/short-length expectations in the new test to match the existing (documented?) x86-64 behavior, or
- treating this as a real x86-64 bug and fixing csum_partial_copy_generic()'s handling of very short lengths,
whichever is judged correct? Happy to share the full kunit log if useful.