From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212223; b=q2UKSb0qoDBLPo0iv3I638VXJ9Dk1+DfjDppVcMLJGtJo4WkasXQbMUnyevgl7iS48KA rmQ2Foax/0XMuQ+Ta3t0YitT3mYqH/fYAtpo1rg4wxTSndwMjwdYodzmsp7cXJSg5p4ce Zw0jVx3SY1URR2tHkKLNtI2p7ZAPk/MXv6qAxcTiYSh6gtM0IaqxU/F/dv3JNVZ4wTwrw G853jFUWioIprAS+7ScHWpz0YxL+ptEXBxXmCNTN9I06N+YVBFSP5YsI2MI7TobL9nU+8 ewSnBTpWT/uoD9wZVWPyhP/QdNgQ53tR5Lrk99o+hJNnvz9ZgdZJR4pYDY+s64hdGAQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212223; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=FYgilbWDAFhzqBX4pr5QCnT909akeptX2wNGaYFkpnY=; b=XTFEcr+N3wBtgWHHBcV2VEHnna3jXnQnyJpXKeKLFlnOT1VMaRuacBhIrQ+mSqB/GnQz NnfVK96Gi5qIeokNrMrTVtFAfwAGVH6rBEfPQ6xwk2isqnjoimweonKNVPI4GwZP+Joyu JjmSHAteMARVlXe46x7bvJT7/p+KbCTpqvL9tg9T1XPnWm791pUVMR1Xm+iIkttxcJRmW zmMJZmSsrYHHMWIAPJgpHw1GtkSHjn4JWV6nWLh47o8oLd4ToXuJhP8r8jsmracTb4AEK v/pLIZGqZhf2TOSKcTU6tbl3cMN0z0SihpJ9aP0Hk7ZOqfZ/KxzJTUDpwnruWOE3FKA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ycsH7AuE; arc=none smtp.remote-ip=209.85.221.54 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E96F4606A6AA for ; Fri, 20 Nov 2026 22:03:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E96F4606A6AA Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ycsH7AuE Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-4ad6219f7d8so681299f8f.0 for ; Fri, 20 Nov 2026 14:03:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795212220; x=1795817020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FYgilbWDAFhzqBX4pr5QCnT909akeptX2wNGaYFkpnY=; b=ycsH7AuEwi/PG5y/j3/+nKuyPibue+HdCEuLWy1JtDiD1DR8F35ujuFKyeuDodYdU1 efDxqM7PZfq0ObpZuX+GRe7pH/8QDXd746FnF3tBu5Qqnu74a0ywuz6JOp/IKqcpnyfs BZEecralbSxUvc25sdjAmrWaCNv8tEtoiOyqPoow7HLYdDhOOBkrc8CNq34klDkTjt5d 7Gl6eAStcFMl5h5BDH2qzNke+cN/1HGyVWwc/0nWy8GQgxcS6JYpIUTUOXwwlF9IYXan W+h4utjdM6e+a18hfqQqoqsHPE4oXOU1U8rvvOusitKoe5FRxzkLmTAEvQiiWS5M/H59 dLyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212220; x=1795817020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=FYgilbWDAFhzqBX4pr5QCnT909akeptX2wNGaYFkpnY=; b=HPXFCydjFAN+tA2Px+lvz6olXES43tVOIA7daC2hqRu/C+9b9mFcBgWjy6jwaJufSS csoHOS7PKK44/4CqQh0WlEncpNBvI4obh9LtVQdkDVYsepOqUuri0AU5wb8xh1LNfuZ4 RFOkbTKdvYKmGL4sEbgYLff1UNHjJMPkFBDHwZEwgZ1+qcVexCTOflzjl3vdGBDfslxb mpbSgwVm1w9hwGfs8y+J2SVKLIdiK0akDKx53MrCt74Qm3wl2ikoQg8O4f16tW9o2oxS boPaClOZf2r9putRHW/fKPEGKY7RNbLm20SEKArqIjJk//PTYyiqGanKabfrV3KBejnc 9Itw== X-Forwarded-Encrypted: i=1; AL+hErH/bAPdpe9joFmIs/yeczhEaGRmjjViZw3M9xGWZ0vTYrdu8HC2QfbeEn5gIfcCZMPIdiuQ3nKq/pXosIY=@vger.kernel.org X-Gm-Message-State: APuU4Z/bG7w9GuEC6UAM7SvahWZltPa5NlXMfbSQzqiq9Doq7UixitMB lgeYx68GEMj9dpDzKkBh+I6oIplIvbiAq+ShdPurmla5NJ/6oysqkwiQ X-Gm-Gg: AYBFou2CzOU8UMMl8kNVXrNGKusQ+boKRNDUIhdUymz+iJE6I++0hl/PeXewzu1h1kc iU2CTymwMapWhwd8e14Tacqjdz+xV/2Md4top6cR7oegKRErsUfvXpkPXif4KaX13h2UhJsVItF HanzoChQUvY37Q9Jix0yVYhE2/Yd1xjvRZAq2j71JS+g++1brP7n8FC9yok/tWm+dy86Uq5oxxF AhVuaSrFfcfUSvPs8t47mOCFlt6el+RrI+G5IOwdBHwPmmz1ns/YJu3lgT11IKeDip44jO8CaSs oQ1nMfAfQkB0hWG4BRj9ILhnLLSDuxmj8J5/bxmYuJxXwuc/4tR2Q9Gq6/0txtIebwnk5xlmJQv yy+pwdpTF0aDmb0yK33biOKh862vVQRcn/BPaK0gcZFLJkAsPaxPtkk/ZeS4Vw7Fb2rtHaOgeYF OofjnY7yRaNv2yOX8V9t9D688FQfZ15WxeKuDagPoDAC6syXewulp0X+OlzU89HjxfBHmn+j2xV s/ZJv7yie6QrgTm6CsSuGG3UYoH/D8F7T0RmjV8t1soPka+YCjS5K9JQsUUU8FZIhg9CpjdSnQh /PTPccZAYRnJeDfE2LswuGAK7jnsbCQKVRQl3cG28ckhgipcyetfqGbJCYGGEVhrW+SOUZd+t47 owDjsSkzJ X-Received: by 2002:a05:600c:64c9:b0:4bb:679a:86ee with SMTP id 5b1f17b1804b1-4bb88938dbemr105794155e9.29.1795212220053; Fri, 20 Nov 2026 14:03:40 -0800 (PST) Received: from demian.play.pl (83-175-177-170.dynamic.play.pl. [83.175.177.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b9abf15sm7396031f8f.43.2026.11.20.14.03.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 14:03:38 -0800 (PST) From: Demian Shulhan To: Catalin Marinas , Will Deacon Cc: Robin Murphy , David Laight , Nathan Chancellor , Jakub Kicinski , Eric Dumazet , Andrew Morton , David Gow , Brendan Higgins , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] arm64: csum: Add fused copy and Internet checksum Date: Fri, 20 Nov 2026 23:03:23 +0100 Message-ID: <20261120220330.1067165-2-demyansh@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261120220330.1067165-1-demyansh@gmail.com> References: <20261120220330.1067165-1-demyansh@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 >=3D 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=3Dy, 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 Suggested-by: David Laight Signed-off-by: Demian Shulhan --- arch/arm64/include/asm/checksum.h | 3 + arch/arm64/lib/csum.c | 189 ++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) diff --git a/arch/arm64/include/asm/checksum.h b/arch/arm64/include/asm/che= cksum.h index dc52b733675d..9f766db01152 100644 --- a/arch/arm64/include/asm/checksum.h +++ b/arch/arm64/include/asm/checksum.h @@ -44,6 +44,9 @@ static inline __sum16 ip_fast_csum(const void *iph, unsig= ned int ihl) extern unsigned int do_csum(const unsigned char *buff, int len); #define do_csum do_csum =20 +#define _HAVE_ARCH_CSUM_AND_COPY +__wsum csum_partial_copy_nocheck(const void *src, void *dst, int len); + #include =20 #endif /* __ASM_CHECKSUM_H */ diff --git a/arch/arm64/lib/csum.c b/arch/arm64/lib/csum.c index 75db718e5b6a..2bf2d63cfd42 100644 --- a/arch/arm64/lib/csum.c +++ b/arch/arm64/lib/csum.c @@ -1,9 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-only // Copyright (C) 2019-2020 Arm Ltd. =20 +#include #include +#include #include #include +#include =20 #include =20 @@ -116,6 +119,192 @@ unsigned int __no_sanitize_address do_csum(const unsi= gned char *buff, int len) return sum >> 16; } =20 +static __always_inline __uint128_t get_unaligned128(const u8 *p) +{ + u64 lo =3D get_unaligned((const u64 *)p); + u64 hi =3D get_unaligned((const u64 *)(p + 8)); + + return ((__uint128_t)hi << 64) | lo; +} + +static __always_inline void put_unaligned128(__uint128_t val, u8 *p) +{ + put_unaligned((u64)val, (u64 *)p); + put_unaligned((u64)(val >> 64), (u64 *)(p + 8)); +} + +static __always_inline __wsum csum_fold_acc_cy(__uint128_t acc, u64 cy) +{ + __uint128_t t; + u64 s; + + acc +=3D (acc >> 64) | (acc << 64); + t =3D (__uint128_t)(u64)(acc >> 64) + cy; + s =3D (u64)t + (u64)(t >> 64); + s +=3D (s >> 32) | (s << 32); + return (__force __wsum)(u32)(s >> 32); +} + +static __always_inline void csum_tree64_ovf(const u8 *s, u8 *d, + __uint128_t *acc, s64 *cy) +{ + __uint128_t v0 =3D get_unaligned128(s); + __uint128_t v1 =3D get_unaligned128(s + 16); + __uint128_t v2, v3; + s64 o; + + put_unaligned128(v0, d); + put_unaligned128(v1, d + 16); + o =3D __builtin_add_overflow(v0, v1, &v0); + + v2 =3D get_unaligned128(s + 32); + v3 =3D get_unaligned128(s + 48); + put_unaligned128(v2, d + 32); + put_unaligned128(v3, d + 48); + o +=3D __builtin_add_overflow(v2, v3, &v2); + + o +=3D __builtin_add_overflow(v0, v2, &v0); + *cy +=3D o + __builtin_add_overflow(*acc, v0, acc); +} + +static __always_inline void csum_align_src16(const u8 **ps, u8 **pd, + unsigned int *pulen, + __uint128_t *pacc, s64 *pc0) +{ + uintptr_t off =3D (uintptr_t)(*ps) & 15; + + if (unlikely(off) && *pulen >=3D 272U) { + unsigned int adv =3D 16U - (unsigned int)off; + u64 lo =3D get_unaligned((const u64 *)(*ps)); + u64 hi =3D get_unaligned((const u64 *)(*ps + 8)); + u64 odd =3D (u64)off & 1ULL; + u64 m =3D ~0ULL >> ((off & 7) * 8); + u64 h; + + put_unaligned(lo, (u64 *)(*pd)); + put_unaligned(hi, (u64 *)(*pd + 8)); + lo =3D (off & 8) ? (lo & m) : lo; + hi =3D (off & 8) ? 0ULL : (hi & m); + h =3D lo + hi; + h +=3D h < lo; + *pacc =3D ror64(h, (unsigned int)(odd * 56ULL)); + /* + * At len <=3D INT_MAX, the 256B loop adds <=3D 16 carries per + * iteration (< 2^27 total before bit 62 is cleared, and c0 + * stays < 2^33 overall), so bit 62 of @c0 cannot overflow. + */ + *pc0 =3D (s64)(odd << 62); + *ps +=3D adv; + *pd +=3D adv; + *pulen -=3D adv; + } +} + +static __always_inline __wsum csum_fold_final(__uint128_t acc, u64 cy, u64= odd) +{ + __wsum res =3D csum_fold_acc_cy(acc, cy); + + if (unlikely(odd)) + return (__force __wsum)ror32((__force u32)res, 8); + return res; +} + +/* + * Single-pass copy and ones' complement sum. Each summed word is read once + * into a GPR before storing and summing, so the returned sum always match= es + * @dst even if @src is concurrently modified. + */ +__wsum __no_sanitize_address csum_partial_copy_nocheck(const void *src, vo= id *dst, int len) +{ + const u8 *s =3D src; + u8 *d =3D dst; + __uint128_t acc =3D 0; + s64 c0 =3D 0; + u64 odd =3D 0; + unsigned int ulen =3D (unsigned int)len; + + if (unlikely(len <=3D 0)) + return 0; + + kasan_check_read(src, len); + kasan_check_write(dst, len); + + if (ulen >=3D 64U) { + if (ulen >=3D 256U) { + csum_align_src16(&s, &d, &ulen, &acc, &c0); + ulen -=3D 256U; + do { + csum_tree64_ovf(s, d, &acc, &c0); + csum_tree64_ovf(s + 64, d + 64, &acc, &c0); + csum_tree64_ovf(s + 128, d + 128, &acc, &c0); + csum_tree64_ovf(s + 192, d + 192, &acc, &c0); + s +=3D 256; + d +=3D 256; + } while (!__builtin_sub_overflow(ulen, 256U, &ulen)); + odd =3D (u64)c0 >> 62; + c0 &=3D (1LL << 62) - 1; + ulen &=3D 255U; + if (!ulen) + goto fold; + } + if (ulen >=3D 64U) { + ulen -=3D 64U; + do { + csum_tree64_ovf(s, d, &acc, &c0); + s +=3D 64; + d +=3D 64; + } while (!__builtin_sub_overflow(ulen, 64U, &ulen)); + ulen &=3D 63U; + if (!ulen) + goto fold; + } + } + if (likely(ulen >=3D 16U)) { + ulen -=3D 16U; + do { + __uint128_t v =3D get_unaligned128(s); + + put_unaligned128(v, d); + s +=3D 16; + d +=3D 16; + c0 +=3D __builtin_add_overflow(acc, v, &acc); + } while (!__builtin_sub_overflow(ulen, 16U, &ulen)); + } + if (likely(ulen & 8U)) { + u64 v =3D get_unaligned((const u64 *)s); + + put_unaligned(v, (u64 *)d); + s +=3D 8; + d +=3D 8; + c0 +=3D __builtin_add_overflow(acc, v, &acc); + } + if (likely(ulen & 4U)) { + u32 v =3D get_unaligned((const u32 *)s); + + put_unaligned(v, (u32 *)d); + s +=3D 4; + d +=3D 4; + c0 +=3D v; + } + if (likely(ulen & 2U)) { + u16 v =3D get_unaligned((const u16 *)s); + + put_unaligned(v, (u16 *)d); + s +=3D 2; + d +=3D 2; + c0 +=3D v; + } + if (ulen & 1U) { + u8 v =3D *s; + + *d =3D v; + c0 +=3D v; + } +fold: + return csum_fold_final(acc, (u64)c0, odd); +} +EXPORT_SYMBOL(csum_partial_copy_nocheck); + __sum16 csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __u32 len, __u8 proto, __wsum csum) --=20 2.53.0 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795212227; b=eI7CW+Q/G0sdql2R4nPBsmW0vl3EB0HF/koYNCcOhbUwKhp41g1jI44CK2JwrMMqlgp3 xOeCLI77BcW+dEd4wBGCqiuBsFj23GqhXRQMaSQgeblKenMZaPQB+LT9zzInKgMO4xZIs wou/ZcC1NaZMa46nd3MKZEeOOpNXxneKzTRcioHJ7M8H2uboWy51YWGFQWA9VVmV0hQ6p KOJ2Dg6DHehFpg+4+qz7Vw30DbYqrvTs/Z5sjGDIZa0DVVtGzOv5L1NZMGia9+b4c1E19 XPc/ixicCZ7o9H+m52cawlNo+TPCLPbjg5pIRYwsQ6r4hqVomtNjUCWuX3jrgXn0c+w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795212227; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=r/eS/kYEg+SnNJxT3nwm1sLf+UOfr/jsxW42/srYOhI=; b=CxYxzLYBpRnGv2mpDpWAYSOSKkPFh3T8ONc5lA41KQ1vHIEzc0c+Nua/HIL0avP4vVti 3oXIg7pOV1cmy1UnlMBl0l/ekCL6o1YeErWlpKSMgR2kgEmKy5R2C/DJnMomSZxFV7Wz5 4IS2HS/tV6Qyp/m40yH0BEa8ywPmRbeAaAMGsgf71ygBOaxYCXQZQCC8nsYwo1F68qjdz /mxmUGjxTwwpij/uGkLCTwwLhTtPOx2WZmZdxdJgW7SL/0V+FM/9TeZ/J1zj/WecoNhgE gbp1S9BmfT9D0ma2JUVEdHjP926WeYmY1Nc4QkPQj/26wAQfNzc1RlISyrmDdpSrCsg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dTCw3ioN; arc=none smtp.remote-ip=209.85.128.54 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D016606A68C for ; Fri, 20 Nov 2026 22:03:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3D016606A68C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dTCw3ioN Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4bb6ba3b244so12930275e9.3 for ; Fri, 20 Nov 2026 14:03:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795212222; x=1795817022; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=r/eS/kYEg+SnNJxT3nwm1sLf+UOfr/jsxW42/srYOhI=; b=dTCw3ioNfMlXLVP8P2olWV6HlNDISprIaF8npb7s+hpd6xVVTQAgZO87LpPXk9bVx/ 85SU8TFewOqCG2O/WV5juenQctvdej7jbBdMQeOCH1SvJ6F3r2Vqsn0JJtUgqTJNw3wy CyaH3TAl/tJMQRXU3u6WBZlz6Ws0lx4nJAo6EpzuIm0RKROGJQqqi65z7fF2xZIcX2Of ZNznhFHzvctw77RQkSaSuyR5Bd3/WQcestfO6pbU+wpO6ExxU2sLqphn71sDevSwSWfo lM2NkNMPY/K0q/HA823ym59LFcVpT/j4DNy4cRq8fzvzYYOtLbfoH3+OK83Ki2xG9dlX uwyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795212222; x=1795817022; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=r/eS/kYEg+SnNJxT3nwm1sLf+UOfr/jsxW42/srYOhI=; b=IAluLNLcWRoFOaKObPtDqir9PErj85cZ2ysHoIN1RyJXasi3wPhjl9FjCaZpyp/Ymp 5xzbIb4OvuM6QGfx26oM2KJLtsbRZyzHVnnheWYQcxTv8cWvX00t4CNrT/k0FXO+r0cL te6RufzmwgNVtySCdkfswX7mUGCwhW1sRYz8pWHodjA00G5r8eVI8rf7N2DGwE6WQfTq qtVxthv1Mc5kT/6WIIK9aDfFYmzrzURxfvgwiS+RrdArptYEDMV79sUb8zErWyRqiiv7 WOk2RENcYK7xYdcSrabZeC7iik2NhGXxa+s6gqRM++CRhrWVNSQA8MFpRn8RHMf2b6Bu XBpQ== X-Forwarded-Encrypted: i=1; AL+hErHBsQF3I03nyWyrILvVpiP5TFGfnlItGQZzTuEuVCgWh3PWmVxSrob6i8UCpLPb/ogrTc3ioQInRDsXEjI=@vger.kernel.org X-Gm-Message-State: APuU4Z/iOirVrng1zYbkJv13ZGTekyrKrZiXAN3oCqVYkecghilvCKUK 2qek7CCYAzwX1YcEBqJ9L7sk9hyeoDIE0ZhfPcu88Ziho6kGDaSjqYZB X-Gm-Gg: AYBFou0BrZzbMQcSFDgY16M8kA9vFvS0OuJkhKgOp/r3Ls6bm2BPCZVr7J1pWdvL20K uEb/ZqG9RbgyioRLBGn6RgmyObnKZeRalg7mMFhmQx0FPn3ymgkWRmQ1Aph3NqObuvhlrmW9WxA eu2Ufpfj6ti/qCp6Ek3CNSMItx6Hvp7UnPaCMWo9HPswzjifraaezAwnkUTOC42E2VsIi4iRLOK bIEMGo8pUlKOwJZj5QOJYA9WQunuOD9JdUefaGOO4+dXnkqoX398ghJc0h/JIz+mKEJeNsIZeUz S0QDv60PmWcTzwI6pL2otVGK7dZYv1oExOW/t+aWAUEsgdCw9gAg4q9thkFGhTYgsqOJrsCOhbc +tymOiJgq0/Lo765B2c0tlzS4x76+H/E6cP44JcZ2N82lwIRWTb1tcXhYBJze5+BZ1XKNXRy8r/ LxZq3y6LTxOVDum77cq4HRMkzY1KvMCSkB086nwsEgWj0TwNntney/RUZTuSN3W859JK60qW4d6 JDxcFfjU/9SMaVbh2+UFNsUxjXpd7TOCrShEhDNjgGCFhQDDmFMGQAA7RZ96vA6Nj0IGq5Yq1pD ZLTs9VnPiSDUVZ+7JH7y7quNHokRAnzzjoJnzZa+opuLjmravkGiOiYdtgwf+Eo= X-Received: by 2002:a05:600c:45d4:b0:4a2:84c:52f3 with SMTP id 5b1f17b1804b1-4bb8880988cmr104087575e9.6.1795212221505; Fri, 20 Nov 2026 14:03:41 -0800 (PST) Received: from demian.play.pl (83-175-177-170.dynamic.play.pl. [83.175.177.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b9abf15sm7396031f8f.43.2026.11.20.14.03.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 14:03:40 -0800 (PST) From: Demian Shulhan To: Catalin Marinas , Will Deacon Cc: Robin Murphy , David Laight , Nathan Chancellor , Jakub Kicinski , Eric Dumazet , Andrew Morton , David Gow , Brendan Higgins , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] lib/tests: checksum: Add KUnit tests for csum_partial_copy_nocheck() Date: Fri, 20 Nov 2026 23:03:24 +0100 Message-ID: <20261120220330.1067165-3-demyansh@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261120220330.1067165-1-demyansh@gmail.com> References: <20261120220330.1067165-1-demyansh@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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). Signed-off-by: Demian Shulhan --- lib/Kconfig.debug | 10 + lib/tests/checksum_kunit.c | 453 +++++++++++++++++++++++++++++++++++++ 2 files changed, 463 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 2d22638ec13a..588343ced260 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2770,6 +2770,16 @@ config CHECKSUM_KUNIT =20 If unsure, say N. =20 +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_TE= STS depends on KUNIT diff --git a/lib/tests/checksum_kunit.c b/lib/tests/checksum_kunit.c index be04aa42125c..ac4be0a27c7d 100644 --- a/lib/tests/checksum_kunit.c +++ b/lib/tests/checksum_kunit.c @@ -3,8 +3,15 @@ * Test cases csum_partial, csum_fold, ip_fast_csum, csum_ipv6_magic */ =20 +#include #include +#include +#include +#include +#include +#include #include +#include #include =20 #define MAX_LEN 512 @@ -619,18 +626,464 @@ static void test_csum_ipv6_magic(struct kunit *test) } } =20 +/* + * csum_partial_copy_nocheck() tests: verify copied bytes, folded checksum + * against a standalone reference and csum_partial(), canary/guard-page + * bounds, and concurrent task/softirq/hardirq execution. + */ +#define COPY_MAX_OFF 16 +#define COPY_MAX_LEN 4352 +#define COPY_BENCH_MAX_LEN 16384 +#define COPY_GUARD 128 /* canary span on each side of dst */ +#define COPY_CANARY 0xa5 +#define COPY_SEED 0x5eed + +static struct rnd_state copy_rng; +/* vmalloc()ed with a page-aligned length, so each is followed by a guard = page */ +static u8 *copy_src_area; +static u8 *copy_dst_area; +static u8 *copy_shadow; /* expected contents of copy_src_area */ +static size_t copy_area_len; + +static int checksum_suite_init(struct kunit_suite *suite) +{ + copy_area_len =3D round_up(COPY_BENCH_MAX_LEN + COPY_MAX_OFF, PAGE_SIZE); + copy_src_area =3D vmalloc(copy_area_len); + copy_dst_area =3D vmalloc(copy_area_len); + copy_shadow =3D vmalloc(copy_area_len); + if (!copy_src_area || !copy_dst_area || !copy_shadow) { + vfree(copy_src_area); + vfree(copy_dst_area); + vfree(copy_shadow); + copy_src_area =3D NULL; + copy_dst_area =3D NULL; + copy_shadow =3D NULL; + return -ENOMEM; + } + prandom_seed_state(©_rng, COPY_SEED); + return 0; +} + +static void checksum_suite_exit(struct kunit_suite *suite) +{ + vfree(copy_src_area); + vfree(copy_dst_area); + vfree(copy_shadow); + copy_src_area =3D NULL; + copy_dst_area =3D NULL; + copy_shadow =3D NULL; +} + +static void copy_fill_random(void) +{ + prandom_bytes_state(©_rng, copy_src_area, copy_area_len); + memcpy(copy_shadow, copy_src_area, copy_area_len); +} + +static void copy_fill_bytes(const u8 *pattern, int pattern_len) +{ + size_t i; + + for (i =3D 0; i < copy_area_len; i++) + copy_src_area[i] =3D pattern[i % pattern_len]; + memcpy(copy_shadow, copy_src_area, copy_area_len); +} + +/* Place @len bytes at misalignment @off as close as possible to the guard= page. */ +static u8 *copy_place_at_end(u8 *area, int off, int len) +{ + u8 *end =3D area + copy_area_len; + int pad; + + /* + * UML lacks EX_TYPE_ZEROPAD fixup on vmalloc guard pages in x86 + * csum_partial(), and RISC-V do_csum_no_alignment() over-reads up to + * sizeof(unsigned long) - 1 bytes past the buffer end. + */ + if (IS_ENABLED(CONFIG_UML) || IS_ENABLED(CONFIG_RISCV)) + end -=3D sizeof(unsigned long); + + pad =3D ((unsigned long)(end - len) - off) & (COPY_MAX_OFF - 1); + return end - len - pad; +} + +/* Naive reference: folded ones' complement sum, same semantics as csum_fo= ld(). */ +static u16 ref_csum_fold(const u8 *buf, int len) +{ + u64 sum =3D 0; + u16 word; + int i; + + for (i =3D 0; i + 1 < len; i +=3D 2) { + memcpy(&word, buf + i, sizeof(word)); /* native endian */ + sum +=3D word; + } + if (len & 1) { + u8 pad[2] =3D { buf[len - 1], 0 }; + + memcpy(&word, pad, sizeof(word)); /* LE: low byte, BE: high byte */ + sum +=3D word; + } + while (sum >> 16) + sum =3D (sum & 0xffff) + (sum >> 16); + + sum =3D ~sum & 0xffff; + return sum =3D=3D 0xffff ? 0 : sum; +} + +/* + * RFC 1071: 0xffff (-0) and 0x0000 (+0) are equivalent. Canonicalize to 0= so + * ~0U-seeded archs (e.g. x86_64) match 0-seeded refs on zero/all-zero inp= uts. + */ +static u16 fold_csum(__wsum csum) +{ + u16 sum =3D (__force u16)csum_fold(csum); + + return sum =3D=3D 0xffff ? 0 : sum; +} + +static int first_non_canary(const u8 *p, int n) +{ + int i; + + for (i =3D 0; i < n; i++) + if (p[i] !=3D COPY_CANARY) + return i; + return -1; +} + +/* Verify one csum_partial_copy_nocheck() call with @before/@after canarie= s. */ +static void check_csum_copy_at(struct kunit *test, const u8 *src, u8 *dst, + int len, int before, int after) +{ + unsigned long src_off =3D (unsigned long)src & (COPY_MAX_OFF - 1); + unsigned long dst_off =3D (unsigned long)dst & (COPY_MAX_OFF - 1); + const u8 *lo, *hi; + u16 expect, got; + int bad; + + memset(dst - before, COPY_CANARY, before + len + after); + expect =3D ref_csum_fold(src, len); + + got =3D fold_csum(csum_partial_copy_nocheck(src, dst, len)); + + KUNIT_ASSERT_EQ_MSG(test, got, expect, + "checksum mismatch src_off=3D%lu dst_off=3D%lu len=3D%d", + src_off, dst_off, len); + KUNIT_ASSERT_EQ_MSG(test, memcmp(src, dst, len), 0, + "copied data mismatch src_off=3D%lu dst_off=3D%lu len=3D%d", + src_off, dst_off, len); + + if (before > 0) + KUNIT_ASSERT_EQ_MSG(test, dst[-1], COPY_CANARY, + "clobbered dst[-1] (src_off=3D%lu dst_off=3D%lu len=3D%d)", + src_off, dst_off, len); + if (after > 0) + KUNIT_ASSERT_EQ_MSG(test, dst[len], COPY_CANARY, + "clobbered dst[len] (src_off=3D%lu dst_off=3D%lu len=3D%d)", + src_off, dst_off, len); + + bad =3D first_non_canary(dst - before, before); + KUNIT_ASSERT_EQ_MSG(test, bad, -1, + "wrote %d bytes before dst (src_off=3D%lu dst_off=3D%lu len=3D%d)", + before - bad, src_off, dst_off, len); + + bad =3D first_non_canary(dst + len, after); + KUNIT_ASSERT_EQ_MSG(test, bad, -1, + "wrote at dst+len+%d (src_off=3D%lu dst_off=3D%lu len=3D%d)", + bad, src_off, dst_off, len); + + /* Source untouched, including a margin on either side. */ + lo =3D max(src - COPY_GUARD, (const u8 *)copy_src_area); + hi =3D min(src + len + COPY_GUARD, (const u8 *)copy_src_area + copy_area_= len); + KUNIT_ASSERT_EQ_MSG(test, + memcmp(lo, copy_shadow + (lo - copy_src_area), hi - lo), 0, + "source buffer modified (src_off=3D%lu dst_off=3D%lu len=3D%d)", + src_off, dst_off, len); + + /* Must be interchangeable with the arch csum_partial() semantics. */ + KUNIT_ASSERT_EQ_MSG(test, got, + fold_csum(csum_partial(src, len, 0)), + "disagrees with csum_partial() src_off=3D%lu dst_off=3D%lu len=3D%d= ", + src_off, dst_off, len); +} + +/* Source against its guard page, destination between canaries. */ +static void check_csum_copy(struct kunit *test, int src_off, int dst_off, + int len) +{ + KUNIT_ASSERT_TRUE(test, src_off >=3D 0 && src_off < COPY_MAX_OFF); + KUNIT_ASSERT_TRUE(test, dst_off >=3D 0 && dst_off < COPY_MAX_OFF); + KUNIT_ASSERT_TRUE(test, len >=3D 0 && len <=3D COPY_MAX_LEN); + + check_csum_copy_at(test, copy_place_at_end(copy_src_area, src_off, len), + copy_dst_area + COPY_GUARD + dst_off, len, + COPY_GUARD, COPY_GUARD); +} + +/* Representative source offsets used with all 16 destination offsets. */ +static const int copy_src_offs[] =3D { 0, 1, 3, 8, 15 }; + +/* Every len 0..64 with every src/dst misalignment: the scalar tail ladder= . */ +static void test_csum_copy_small_all_alignments(struct kunit *test) +{ + int len, src_off, dst_off; + + copy_fill_random(); + for (len =3D 0; len <=3D 64; len++) + for (src_off =3D 0; src_off < COPY_MAX_OFF; src_off++) + for (dst_off =3D 0; dst_off < COPY_MAX_OFF; dst_off++) + check_csum_copy(test, src_off, dst_off, len); +} + +/* Threshold boundaries & large unaligned buffers across [0..15]x[0..7] an= d [0..15]. */ +static void test_csum_copy_large_unaligned(struct kunit *test) +{ + static const int threshold_lens[] =3D { + 48, 63, 64, 65, 71, 72, 73, 79, 80, 96, 127, 128, + 255, 256, 257, 271, 272, 273, + 1368, 1448, 1500, 4096, + }; + static const int extra_lens[] =3D { + 1368, 1448, 1500, 2033, 2034, 2035, 2039, 2040, 2041, 2047, + 2048, 2049, 2055, 2056, 2057, 2062, 2063, 3072, 4095, 4096, + 4097, 4352, + }; + int len, i, j, src_off, dst_off; + + copy_fill_random(); + for (j =3D 0; j < ARRAY_SIZE(threshold_lens); j++) + for (src_off =3D 0; src_off < COPY_MAX_OFF; src_off++) + for (dst_off =3D 0; dst_off < 8; dst_off++) + check_csum_copy(test, src_off, dst_off, + threshold_lens[j]); + + for (len =3D 1024; len < 1024 + 80; len++) + for (i =3D 0; i < ARRAY_SIZE(copy_src_offs); i++) + for (dst_off =3D 0; dst_off < COPY_MAX_OFF; dst_off++) + check_csum_copy(test, copy_src_offs[i], dst_off, + len); + + for (j =3D 0; j < ARRAY_SIZE(extra_lens); j++) + for (i =3D 0; i < ARRAY_SIZE(copy_src_offs); i++) + for (dst_off =3D 0; dst_off < COPY_MAX_OFF; dst_off++) + check_csum_copy(test, copy_src_offs[i], dst_off, + extra_lens[j]); +} + +/* Patterns stressing carries, zero/MSB bytes, and odd-offset byte order. = */ +static void test_csum_copy_patterns(struct kunit *test) +{ + static const u8 pat_ff[] =3D { 0xff }; + static const u8 pat_00[] =3D { 0x00 }; + static const u8 pat_80[] =3D { 0x80 }; + static const u8 pat_ff00[] =3D { 0xff, 0x00 }; + static const u8 pat_ramp[] =3D { 0x01, 0x02, 0x04, 0x08, + 0x10, 0x20, 0x40, 0x80 }; + static const struct { + const u8 *pat; + int len; + } patterns[] =3D { + { pat_ff, ARRAY_SIZE(pat_ff) }, + { pat_00, ARRAY_SIZE(pat_00) }, + { pat_80, ARRAY_SIZE(pat_80) }, + { pat_ff00, ARRAY_SIZE(pat_ff00) }, + { pat_ramp, ARRAY_SIZE(pat_ramp) }, + { NULL, 0 }, /* random */ + }; + static const int lens[] =3D { + 1, 2, 3, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, + 255, 256, 511, 512, 1023, 1024, 1025, 1368, 1448, 1500, 2048, + 4095, 4096, 4352, + }; + static const int offs[][2] =3D { + { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 }, { 3, 7 }, { 7, 3 }, + { 9, 5 }, { 15, 15 }, + }; + int p, l, o; + + for (p =3D 0; p < ARRAY_SIZE(patterns); p++) { + if (patterns[p].pat) + copy_fill_bytes(patterns[p].pat, patterns[p].len); + else + copy_fill_random(); + + for (l =3D 0; l < ARRAY_SIZE(lens); l++) + for (o =3D 0; o < ARRAY_SIZE(offs); o++) + check_csum_copy(test, offs[o][0], offs[o][1], + lens[l]); + } +} + +/* Place dst against its trailing guard page so any over-write faults. */ +static void test_csum_copy_dst_guard_page(struct kunit *test) +{ + static const int lens[] =3D { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 31, 32, 33, 63, 64, 65, 71, 72, 73, 79, 80, 81, 96, 127, 128, + 129, 255, 256, 257, 271, 272, 273, 1023, 1024, 1025, 1368, + 1448, 1500, 2048, 4095, 4096, 4097, + }; + int l, src_off, dst_off; + + copy_fill_random(); + for (l =3D 0; l < ARRAY_SIZE(lens); l++) + for (dst_off =3D 0; dst_off < COPY_MAX_OFF; dst_off++) { + u8 *dst =3D copy_place_at_end(copy_dst_area, dst_off, lens[l]); + int after =3D copy_dst_area + copy_area_len - (dst + lens[l]); + + src_off =3D (dst_off * 7 + lens[l]) & (COPY_MAX_OFF - 1); + check_csum_copy_at(test, + copy_place_at_end(copy_src_area, + src_off, lens[l]), + dst, lens[l], COPY_GUARD, after); + } +} + +/* len =3D=3D 0: fold to 0 and never touch src or dst, even against guard = pages. */ +static void test_csum_copy_zero_len(struct kunit *test) +{ + u8 *unmapped_end =3D copy_src_area + copy_area_len; + u8 *dst =3D copy_dst_area + COPY_GUARD; + __wsum sum; + + copy_fill_random(); + memset(copy_dst_area, COPY_CANARY, 2 * COPY_GUARD); + sum =3D csum_partial_copy_nocheck(unmapped_end, dst, 0); + KUNIT_EXPECT_EQ(test, fold_csum(sum), 0); + KUNIT_EXPECT_EQ(test, first_non_canary(copy_dst_area, 2 * COPY_GUARD), -1= ); +} + +/* Concurrent execution across task, softirq, and hardirq contexts. */ +#define COPY_IRQ_NUM_MSGS 3 +#define COPY_IRQ_NUM_CTX 3 /* task, softirq, hardirq */ + +static const int copy_irq_lens[COPY_IRQ_NUM_MSGS] =3D { 100, 1500, 4097 }; + +struct copy_irq_test_state { + const u8 *src[COPY_IRQ_NUM_MSGS]; + u16 expected[COPY_IRQ_NUM_MSGS]; + u8 *dst[COPY_IRQ_NUM_CTX]; + atomic_t seqno; +}; + +static bool copy_irq_test_func(void *state_) +{ + struct copy_irq_test_state *state =3D state_; + u32 i =3D (u32)atomic_inc_return(&state->seqno) % COPY_IRQ_NUM_MSGS; + int ctx =3D in_hardirq() ? 2 : in_serving_softirq() ? 1 : 0; + const u8 *src =3D state->src[i]; + u8 *dst =3D state->dst[ctx]; + int len =3D copy_irq_lens[i]; + u16 got; + + got =3D fold_csum(csum_partial_copy_nocheck(src, dst, len)); + return got =3D=3D state->expected[i] && memcmp(src, dst, len) =3D=3D 0; +} + +static void test_csum_copy_interrupt_context(struct kunit *test) +{ + struct copy_irq_test_state state =3D { }; + int i; + + copy_fill_random(); + for (i =3D 0; i < COPY_IRQ_NUM_MSGS; i++) { + /* Distinct, misaligned messages. */ + state.src[i] =3D copy_src_area + i * (COPY_MAX_LEN + 64) + 2 * i + 1; + state.expected[i] =3D ref_csum_fold(state.src[i], copy_irq_lens[i]); + } + for (i =3D 0; i < COPY_IRQ_NUM_CTX; i++) + state.dst[i] =3D copy_dst_area + i * (COPY_MAX_LEN + 64) + 2 * i + 1; + + kunit_run_irq_test(test, copy_irq_test_func, 100000, &state); +} + +/* Optional throughput benchmark vs memcpy() + csum_partial(). */ +static void test_csum_copy_benchmark(struct kunit *test) +{ + static const int lens[] =3D { + 16, 40, 64, 128, 256, 512, 1000, 1024, 1368, 1448, 1500, + 2048, 4096, 16384, + }; + static const int offs[][2] =3D { { 0, 0 }, { 1, 3 } }; + int l, o, i, len, num_iters; + u64 t, t_fused, t_split; + u32 acc =3D 0; /* keeps every call alive via OPTIMIZER_HIDE_VAR() */ + + if (!IS_ENABLED(CONFIG_CHECKSUM_KUNIT_BENCHMARK)) + kunit_skip(test, "not enabled"); + + copy_fill_random(); + + /* warm-up */ + for (i =3D 0; i < 10000000; i +=3D COPY_BENCH_MAX_LEN) + acc ^=3D (__force u32)csum_partial_copy_nocheck(copy_src_area, + copy_dst_area, + COPY_BENCH_MAX_LEN); + OPTIMIZER_HIDE_VAR(acc); + + for (o =3D 0; o < ARRAY_SIZE(offs); o++) { + const u8 *src =3D copy_src_area + offs[o][0]; + u8 *dst =3D copy_dst_area + offs[o][1]; + + for (l =3D 0; l < ARRAY_SIZE(lens); l++) { + len =3D lens[l]; + num_iters =3D 10000000 / (len + 128); + + preempt_disable(); + t =3D ktime_get_ns(); + for (i =3D 0; i < num_iters; i++) + acc ^=3D (__force u32)csum_partial_copy_nocheck(src, dst, len); + t_fused =3D ktime_get_ns() - t; + OPTIMIZER_HIDE_VAR(acc); + + t =3D ktime_get_ns(); + for (i =3D 0; i < num_iters; i++) { + memcpy(dst, src, len); + acc ^=3D (__force u32)csum_partial(dst, len, 0); + } + t_split =3D ktime_get_ns() - t; + OPTIMIZER_HIDE_VAR(acc); + preempt_enable(); + + kunit_info(test, + "src+%d dst+%d len=3D%5d: csum_partial_copy_nocheck %6llu MB/s, mem= cpy+csum_partial %6llu MB/s\n", + offs[o][0], offs[o][1], len, + div64_u64((u64)len * num_iters * 1000, + max_t(u64, t_fused, 1)), + div64_u64((u64)len * num_iters * 1000, + max_t(u64, t_split, 1))); + } + } + { + u32 ref_acc =3D acc; + + OPTIMIZER_HIDE_VAR(acc); + KUNIT_ASSERT_EQ(test, acc, ref_acc); + } +} + static struct kunit_case __refdata checksum_test_cases[] =3D { KUNIT_CASE(test_csum_fixed_random_inputs), KUNIT_CASE(test_csum_all_carry_inputs), KUNIT_CASE(test_csum_no_carry_inputs), KUNIT_CASE(test_ip_fast_csum), KUNIT_CASE(test_csum_ipv6_magic), + KUNIT_CASE(test_csum_copy_small_all_alignments), + KUNIT_CASE(test_csum_copy_large_unaligned), + KUNIT_CASE(test_csum_copy_patterns), + KUNIT_CASE(test_csum_copy_dst_guard_page), + KUNIT_CASE(test_csum_copy_zero_len), + KUNIT_CASE(test_csum_copy_interrupt_context), + KUNIT_CASE(test_csum_copy_benchmark), {} }; =20 static struct kunit_suite checksum_test_suite =3D { .name =3D "checksum", .test_cases =3D checksum_test_cases, + .suite_init =3D checksum_suite_init, + .suite_exit =3D checksum_suite_exit, }; =20 kunit_test_suites(&checksum_test_suite); --=20 2.53.0