From: Catalin Marinas <catalin.marinas@arm.com>
To: jai.menon@datadoghq.com
Cc: Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
linux-arm-kernel@lists.infradead.org,
Robin Murphy <robin.murphy@arm.com>
Subject: Re: [PATCH] arm64: csum: Fix carry loss in ip_fast_csum()
Date: Tue, 1 Dec 2026 16:02:40 +0000 [thread overview]
Message-ID: <aw7voDqXAXD-PMpA@arm.com> (raw)
In-Reply-To: <20261130-arm64-ip-fast-csum-carry-v1-1-009f6a39adad@datadoghq.com>
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
prev parent reply other threads:[~2026-12-01 16:03 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aw7voDqXAXD-PMpA@arm.com \
--to=catalin.marinas@arm.com \
--cc=jai.menon@datadoghq.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox