[PATCH] arm64: csum: Fix carry loss in ip_fast_csum()
Robin Murphy
robin.murphy at arm.com
Tue Dec 1 11:22:17 PST 2026
On 2026-12-01 4:02 pm, Catalin Marinas wrote:
> 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 mean, it's been like this for over 10 years now...
Also: good grief, how the heck has it been _over 10 years_ since I wrote
this!? :O
> 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.
Agreed - we have tests, so if this does realistically matter, the tests
should be testing for it.
>> 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.
Yeah, once we get into the loop we can only ever overflow by a single
bit, so it seems easy enough to accumulate as we go:
diff --git a/arch/arm64/include/asm/checksum.h b/arch/arm64/include/asm/checksum.h
index dc52b733675d..9698baadaf3e 100644
--- a/arch/arm64/include/asm/checksum.h
+++ b/arch/arm64/include/asm/checksum.h
@@ -32,7 +32,8 @@ static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
tmp += ((tmp >> 64) | (tmp << 64));
sum = tmp >> 64;
do {
- sum += *(const u32 *)iph;
+ tmp = sum + *(const u32 *)iph;
+ sum = tmp + (tmp < sum);
iph += 4;
} while (--n > 0);
Trying to convince compilers to pipeline the carry flag _through_ the
loop seems like a lost cause (I don't think GCC even knows ADCS), but
this just turns the ADD into ADDS/CINC which seems painless enough.
__builtin_add_overflow() also works with the same result, but this one
was easier to write, so I'd argue it must be easier to read too :)
Thanks,
Robin.
More information about the linux-arm-kernel
mailing list