[PATCH] arm64: csum: Fix carry loss in ip_fast_csum()
Catalin Marinas
catalin.marinas at arm.com
Tue Dec 1 08:02:40 PST 2026
On Mon, Nov 30, 2026 at 03:32:57PM +0000, Jai Menon via B4 Relay wrote:
> From: Jai Menon <jai.menon at 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
More information about the linux-arm-kernel
mailing list