Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] arm64: csum: Fix carry loss in ip_fast_csum()
@ 2026-11-30 15:32 Jai Menon via B4 Relay
  2026-12-01 16:02 ` Catalin Marinas
  0 siblings, 1 reply; 2+ messages in thread
From: Jai Menon via B4 Relay @ 2026-11-30 15:32 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon; +Cc: Mark Rutland, linux-arm-kernel, Jai Menon

From: Jai Menon <jai.menon@datadoghq.com>

ip_fast_csum() accumulates the trailing 32-bit header words without
end-around carry. Overflow of the 64-bit accumulator produces
incorrect IPv4 header checksums on TX and false checksum failures on RX.

Preserve the end-around carry while retaining two loads for a typical
20-byte IPv4 header.

An AI-based formal verification tool identified the carry loss in the
arm64 IPv4 receive path.

Fixes: 0e455d8e80aa ("arm64: Implement optimised IP checksum helpers")
Cc: stable@vger.kernel.org
Signed-off-by: Jai Menon <jai.menon@datadoghq.com>
---
All five arm64 checksum KUnit tests passed with GCC 15.2 and Clang 22.
Reference checks covered all valid IPv4 header lengths, all byte
alignments modulo 16, and both byte orders.

For a compile-time header length of 20 bytes, both compilers emit 12
instructions before and after the change. The microbenchmark used
cache-hot 20-byte inputs and 17 batches of 1,048,576 calls on a pinned
Neoverse-N1 core. Both rows use 20-byte headers. Each cell shows the
median before and after the change.

Length argument         GCC 15.2 (cycles/call)   Clang 22 (cycles/call)
Compile-time constant   6.21 -> 6.26             6.39 -> 6.58
Runtime value           7.54 -> 6.36             9.08 -> 6.78

An AI agent assisted with drafting and testing this patch.
---
 arch/arm64/include/asm/checksum.h | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/include/asm/checksum.h b/arch/arm64/include/asm/checksum.h
index dc52b7336..d32969b16 100644
--- a/arch/arm64/include/asm/checksum.h
+++ b/arch/arm64/include/asm/checksum.h
@@ -23,18 +23,25 @@ static inline __sum16 csum_fold(__wsum csum)
 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
 {
 	__uint128_t tmp;
-	u64 sum;
+	u64 sum, tail;
 	int n = ihl; /* we want it signed */
 
 	tmp = *(const __uint128_t *)iph;
-	iph += 16;
+	tail = *(const u32 *)(iph + 16);
+	iph += 20;
 	n -= 4;
-	tmp += ((tmp >> 64) | (tmp << 64));
-	sum = tmp >> 64;
-	do {
-		sum += *(const u32 *)iph;
+	while (--n > 0) {
+		tail += *(const u32 *)iph;
 		iph += 4;
-	} while (--n > 0);
+	}
+
+	/* Feed the pair's carry into the tail addition, then fold its carry. */
+	asm("adds\t%0, %1, %2\n\t"
+	    "adcs\t%0, %0, %3\n\t"
+	    "adc\t%0, %0, xzr"
+	    : "=&r" (sum)
+	    : "r" ((u64)tmp), "r" ((u64)(tmp >> 64)), "r" (tail)
+	    : "cc");
 
 	sum += ((sum >> 32) | (sum << 32));
 	return csum_fold((__force __wsum)(sum >> 32));

---
base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046
change-id: 20261128-arm64-ip-fast-csum-carry-e50ae269a0fc

Best regards,
--  
Jai Menon <jai.menon@datadoghq.com>




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] arm64: csum: Fix carry loss in ip_fast_csum()
  2026-11-30 15:32 [PATCH] arm64: csum: Fix carry loss in ip_fast_csum() Jai Menon via B4 Relay
@ 2026-12-01 16:02 ` Catalin Marinas
  0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2026-12-01 16:02 UTC (permalink / raw)
  To: jai.menon; +Cc: Will Deacon, Mark Rutland, linux-arm-kernel, Robin Murphy

On Mon, Nov 30, 2026 at 03:32:57PM +0000, Jai Menon via B4 Relay wrote:
> From: Jai Menon <jai.menon@datadoghq.com>
> 
> ip_fast_csum() accumulates the trailing 32-bit header words without
> end-around carry. Overflow of the 64-bit accumulator produces
> incorrect IPv4 header checksums on TX and false checksum failures on RX.

Thanks for this. The bug is real and we should fix it, but it's probably
not urgent for -rc. I think it's really hard to hit in practice based on
my analysis with an LLM: it needs the high 32 bits of the first 16-byte
sum to be all ones, which for an unfragmented header means e.g. a source
address ending in .191.255 (DF) or .255.255 (no DF), together with one
specific IP ID. Linux recently started sending DF packets with ID 0,
which never triggers it for a unicast source.

When it does hit, it's a packet drop, not some permanent communication
failure.

> All five arm64 checksum KUnit tests passed with GCC 15.2 and Clang 22.
> Reference checks covered all valid IPv4 header lengths, all byte
> alignments modulo 16, and both byte orders.

Could we have (as a separate patch) a KUnit test that exercises this
carry loss? Just to show its status before and after this patch.

> diff --git a/arch/arm64/include/asm/checksum.h b/arch/arm64/include/asm/checksum.h
> index dc52b7336..d32969b16 100644
> --- a/arch/arm64/include/asm/checksum.h
> +++ b/arch/arm64/include/asm/checksum.h
> @@ -23,18 +23,25 @@ static inline __sum16 csum_fold(__wsum csum)
>  static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
>  {
>  	__uint128_t tmp;
> -	u64 sum;
> +	u64 sum, tail;
>  	int n = ihl; /* we want it signed */
>  
>  	tmp = *(const __uint128_t *)iph;
> -	iph += 16;
> +	tail = *(const u32 *)(iph + 16);
> +	iph += 20;
>  	n -= 4;
> -	tmp += ((tmp >> 64) | (tmp << 64));
> -	sum = tmp >> 64;
> -	do {
> -		sum += *(const u32 *)iph;
> +	while (--n > 0) {
> +		tail += *(const u32 *)iph;
>  		iph += 4;
> -	} while (--n > 0);
> +	}
> +
> +	/* Feed the pair's carry into the tail addition, then fold its carry. */
> +	asm("adds\t%0, %1, %2\n\t"
> +	    "adcs\t%0, %0, %3\n\t"
> +	    "adc\t%0, %0, xzr"
> +	    : "=&r" (sum)
> +	    : "r" ((u64)tmp), "r" ((u64)(tmp >> 64)), "r" (tail)
> +	    : "cc");
>  
>  	sum += ((sum >> 32) | (sum << 32));
>  	return csum_fold((__force __wsum)(sum >> 32));

I'm not keen on inline asm if we can avoid it. I think we can use
__builtin_add_overflow() or something like accumulate() in
arch/arm64/lib/csum.c.

-- 
Catalin


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 16:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 15:32 [PATCH] arm64: csum: Fix carry loss in ip_fast_csum() Jai Menon via B4 Relay
2026-12-01 16:02 ` Catalin Marinas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox