Skip to contentSkip to conversation

arm64: csum: Add fused copy and Internet checksum

PATCH New Demian Shulhan 1 participant 3 messages Nov 20

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 cover arm64: csum: Add fused copy and Internet checksum

Add a single-pass csum_partial_copy_nocheck() for arm64 and KUnit coverage (plus an optional throughput benchmark) in checksum_kunit.

On ARM Neoverse-V2 (3.0 GHz), this reaches 39.2-45.1 GB/s aligned (1.8-1.9x) and 36.5-43.8 GB/s unaligned (2.0-2.2x) for >= 1368B buffers vs generic memcpy() + csum_partial().

Changes in v3:
- Use a 64B carry-tree primitive unrolled 4x to 256B (~21 insns/64B, pure leaf function, zero spills on GCC 15 and Clang 21).
- Align @src to 16B for len >= 272, stashing the odd-offset bit in bit 62 of the carry counter.
- Add zero-length, 72/256/272B threshold, and dst[-1]/dst[len] sentinel checks to checksum_kunit.

Changes in v2:
- Drop the NEON path and separate csum-copy* files, folding the scalar implementation directly into arch/arm64/lib/csum.c (resolves the <asm/neon-intrinsics.h> include issue noted by Nathan Chancellor).
- Switch the scalar loop to four __uint128_t accumulators (David Laight).
- Canonicalize 0xffff (-0) to 0x0000 (+0) after csum_fold() in KUnit to fix x86_64 test failures on zero/all-zero inputs (Jakub Kicinski).

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/csum.c             | 189 +++++++++++++
 lib/Kconfig.debug                 |  10 +
 lib/tests/checksum_kunit.c        | 453 ++++++++++++++++++++++++++++++
 4 files changed, 655 insertions(+)

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/2 arm64: csum: Add fused copy and Internet checksum

arm64 falls back to the generic csum_partial_copy_nocheck(), which does two passes (memcpy() + csum_partial()) and hits software GSO paths (e.g. TCP over WireGuard) and UDP/ICMP replies.

Implement a single-pass csum_partial_copy_nocheck() in arch/arm64/lib/csum.c using a 64-byte carry-tree primitive unrolled 4x to 256 bytes (~21 instructions per 64B, pure leaf function with zero spills on GCC 15 and Clang 21) and 16-byte @src alignment for len >= 272. Each summed word is read once into GPRs before storing and summing, so the checksum always matches @dst under concurrent @src modification.

In-kernel KUnit benchmark on ARM Neoverse-V2 (3.0 GHz, CONFIG_CHECKSUM_KUNIT_BENCHMARK=y, GCC 15.2 / Clang 21.1, median of 3):

  case (src/dst)    memcpy+csum_partial  fused (GCC / Clang)    speedup
  ----------------  -------------------  -------------------  -----------
  16B    (+0 / +0)     4.0 /  3.7 GB/s      6.9 /  5.9 GB/s   1.73x/1.61x
  64B    (+0 / +0)     9.4 / 10.5 GB/s     19.1 / 22.0 GB/s   2.04x/2.08x
  256B   (+0 / +0)    16.3 / 18.4 GB/s     33.2 / 35.2 GB/s   2.04x/1.92x
  512B   (+0 / +0)    19.4 / 21.0 GB/s     36.5 / 39.3 GB/s   1.88x/1.87x
  1368B  (+0 / +0)    21.7 / 22.2 GB/s     39.5 / 41.2 GB/s   1.82x/1.85x
  1448B  (+0 / +0)    21.8 / 22.2 GB/s     39.2 / 41.1 GB/s   1.79x/1.86x
  4096B  (+0 / +0)    23.4 / 23.5 GB/s     42.5 / 43.9 GB/s   1.82x/1.87x
  16384B (+0 / +0)    23.8 / 24.0 GB/s     43.3 / 45.1 GB/s   1.82x/1.87x
  16B    (+1 / +3)     1.5 /  2.6 GB/s      6.9 /  5.9 GB/s   4.44x/2.25x
  128B   (+1 / +3)     6.4 / 10.5 GB/s     24.3 / 27.2 GB/s   3.78x/2.60x
  1448B  (+1 / +3)    17.8 / 18.0 GB/s     38.0 / 39.0 GB/s   2.14x/2.16x
  1500B  (+1 / +3)    17.9 / 18.2 GB/s     36.5 / 39.6 GB/s   2.04x/2.18x
  4096B  (+1 / +3)    19.1 / 19.5 GB/s     40.6 / 42.0 GB/s   2.12x/2.15x
  16384B (+1 / +3)    19.4 / 19.6 GB/s     42.3 / 43.8 GB/s   2.18x/2.24x
---
 arch/arm64/include/asm/checksum.h |   3 +
 arch/arm64/lib/csum.c             | 189 ++++++++++++++++++++++++++++++
 2 files changed, 192 insertions(+)
arch/arm64/include/asm/checksum.h +3 -0
index dc52b733675d..9f766db01152 100644
@@ -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/csum.c +189 -0

204 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/2 lib/tests: checksum: Add KUnit tests for csum_partial_copy_nocheck()

In reply to Demian Shulhan

Add KUnit tests for csum_partial_copy_nocheck() covering 0..64B exhaustive alignments, threshold and MTU/page lengths up to 4352B, carry-stressing patterns, vmalloc() guard pages and dst canaries, zero-length calls, and task/softirq/hardirq execution.

Checksums are verified against a standalone C reference and csum_partial(), canonicalizing 0xffff (-0) to 0x0000 (+0) per RFC 1071. Also add an optional throughput benchmark gated behind CONFIG_CHECKSUM_KUNIT_BENCHMARK (default n).

---
 lib/Kconfig.debug          |  10 +
 lib/tests/checksum_kunit.c | 453 +++++++++++++++++++++++++++++++++++++
 2 files changed, 463 insertions(+)
lib/Kconfig.debug +10 -0
index 2d22638ec13a..588343ced260 100644
@@ -2770,6 +2770,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 and is 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 +453 -0

479 lines not on this page · raw message

Signature · 1 line
2.53.0