From nobody Thu Apr 16 09:10:37 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8DBF21256C for ; Sun, 1 Mar 2026 01:12:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772327535; cv=none; b=a3exJ6zK/7PGwGB5XsgVKbwkUHdQ425TEm24hxhdL57G30bhV5me/pKT9V0f6NGkfslEjeiD8VqWCLA5nQsjfKdI6KuB5QNfntBfxsipx6sswtzsnKwEXVMzqpZ0OjzYri3MBumjiyN9Ctbv4JtVVkojHb2zWX404ieELz+BMb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772327535; c=relaxed/simple; bh=w4eclj6kdzdNsnSdB+1o3tuF5GnVygFxn6neXhOQL8Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=WCgVXlUu3cWxzfTPTAKMqoBlHFVtB2Y6h8YVp4Xx10Za/BlkcCeaZWU267gKGtr44ICq+6K8zIo6lsNAMfiUk4B0p8dGyyisFmVtZG54VBww2QXW7gm3i9gc7BCMwgrKI+PeClnaJz63D6L+1u4UNKLJwoHeYDeIpi2uL09YPx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MMjqIjww; arc=none smtp.client-ip=209.85.128.47 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.b="MMjqIjww" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-483abed83b6so26395425e9.0 for ; Sat, 28 Feb 2026 17:12:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772327532; x=1772932332; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ihdUIJ7l2jZWLbyOPaTctCE8ugQZGI6BqGQqEl0kh7w=; b=MMjqIjww586ZOQpFjO+CKu0bwSKyxvyR0rzeOUDegzXkvJcIvexl0nljugApXZiDVd MSJa887m68K2uRBF0S+69gP20ZMW5lfNl5Vhpfy2jNRydtT6xjHtt+CyKplTE3+vYAyr 6wzuzMdP8PvcbK7VnOgUqEaF4yYqFT+eA3vHy5JgsYmh95J15Lk4ox4PGeQQBDCyQ62f uUJj10g/Jhs4rpIwbrZKsCYYq1GQuxoa/0JADhBuT4hGbFcIWSxIpn7+YNcZzaK/w7D7 vfLbIbCGZ/ca4T5qBfaNdieIAuweo2dhpK6ns8BUaX0pEheOWm/4gyNS+hpn5TO0PDZ8 QKLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772327532; x=1772932332; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ihdUIJ7l2jZWLbyOPaTctCE8ugQZGI6BqGQqEl0kh7w=; b=BGuy5v4HuxtlBinc9nVmj4J7Sl5r8lvM2gel4qHrZF2dRNVPuncdlou6Uhgq05lbIt BlbtXzJzV2+vONt7DjEadYJjFdDhdJQ2I++SQMhPqZDVLdISIpZnlfVBn73JH9bAgx+p fLpIUX9SfBzEjL72lP8kq+faCOBWWgHhfVnCPsXsSEaVxFdtMCCkLK+UbqKSOSgCqG5x +gZznWIkGucm+jncApKlOgi4s3dsFzlwq9fed3bdz62TaB/11HccJG5Dcvihmj6ir410 QHWoJjW86tl8pLuxMy+8xGovwskSgvgwgZbMwhdPSuVLqSQgzKVvDVxsWp8NHlm6y8rs 8DtA== X-Forwarded-Encrypted: i=1; AJvYcCX7iRIYS8nG0UyUmfANruzrf5owJVyWEMXmIO1Tv+RcpDiqXhFv2JXJdLtDcR3toIEVEXj0otRvPlo8tJg=@vger.kernel.org X-Gm-Message-State: AOJu0YwWMMmb6m0ugJhObrQDGjoIa1HagQYaPdUmT57H51WgDOukXuMy 7PHgFmt3iSyxi+vGgzfUSZLfNQ7Gsi6ceM9kFIPq3BDPVMX45pPXbarx X-Gm-Gg: ATEYQzwgSQExWoH+OTKfW6A18itNlscooMJMkP9zznP2LjZo41aucF/nYZr1GsX8yQ3 NydCG756llO/fqtH7U5bWSbm7sjNZV7jy/HCJf62V8bl+VsXaAhhJW90lmQrAp+v7qox167iom0 MCILVKzfrFoZd3xHkJUdNNQK3g3zYNN+byaELMQ3tE6AS652s2NDTUTuSIQ/k9NlHMBhBAksfGr /1ULlUUmrIMgzBXIIFU7XXtLB4dECRr8TUsion0pdurVnAK1J9nEwXwY1Odij40wKe1m/TsBRLY flqhVYM12AZBXeaxsLy2d2cqscApElZYuWziEQryn189HNu8da+q1+tabCOvCw1tls64ZioHlxD vymlgClRdfoqozE5nl0LWFS7EGgmPrLPLXFkQi8B7X04r+n7Mr/dzvUcy2JvsT5gH6BO7nyy6Zt l/fXMB8p2t3EH9FWwzaMZIAUzx9xE+7X5MgRDyrWQkqUP8OH5k0DjS9ZrCuxktngY97Ejcr0dW X-Received: by 2002:a05:600c:5392:b0:480:4a4f:c36f with SMTP id 5b1f17b1804b1-483c9bde3famr123763355e9.21.1772327531839; Sat, 28 Feb 2026 17:12:11 -0800 (PST) Received: from turbo.teknoraver.net (net-5-95-156-124.cust.vodafonedsl.it. [5.95.156.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4399c764546sm18825859f8f.29.2026.02.28.17.12.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Feb 2026 17:12:11 -0800 (PST) From: Matteo Croce X-Google-Original-From: Matteo Croce To: Brendan Higgins , David Gow , Andrew Morton , David Laight Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5] KUnit: memcpy: add benchmark Date: Sun, 1 Mar 2026 02:12:09 +0100 Message-ID: <20260301011209.4160-1-teknoraver@meta.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add optional benchmarks for memcpy() and memmove() functions. Each benchmark is run with different size and both aligned and unaligned buffers, to spot unaligned issues on platforms where they have a noticeable performance impact. Sample output on a RISC-V machine: # modprobe memcpy_kunit KTAP version 1 1..1 KTAP version 1 # Subtest: memcpy # module: memcpy_kunit 1..8 [...] # memcpy_bench_test: memcpy: aligned copy of len 2: 6 MB/s # memcpy_bench_test: memcpy: unaligned copy of len 2: 6 MB/s # memcpy_bench_test: memcpy: aligned copy of len 64: 179 MB/s # memcpy_bench_test: memcpy: unaligned copy of len 64: 170 MB/s # memcpy_bench_test: memcpy: aligned copy of len 256: 697 MB/s # memcpy_bench_test: memcpy: unaligned copy of len 256: 421 MB/s # memcpy_bench_test: memcpy: aligned copy of len 4194304: 935 MB/s # memcpy_bench_test: memcpy: unaligned copy of len 4194304: 333 MB/s # memcpy_bench_test.speed: slow ok 7 memcpy_bench_test # memmove_bench_test: memmove: aligned move of len 64: 162 MB/s # memmove_bench_test: memmove: unaligned move of len 64: 162 MB/s # memmove_bench_test: memmove: aligned move of len 256: 647 MB/s # memmove_bench_test: memmove: unaligned move of len 256: 647 MB/s # memmove_bench_test: memmove: aligned move of len 4194304: 1540 MB/s # memmove_bench_test: memmove: unaligned move of len 4194304: 1557 MB/s # memmove_bench_test.speed: slow ok 8 memmove_bench_test # memcpy: pass:8 fail:0 skip:0 total:8 # Totals: pass:8 fail:0 skip:0 total:8 ok 1 memcpy Signed-off-by: Matteo Croce --- v5: rebase to fix a conflict with recent changes lib/Kconfig.debug | 9 ++++ lib/tests/memcpy_kunit.c | 107 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 318df4c75454..a61774690362 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2986,6 +2986,15 @@ config MIN_HEAP_KUNIT_TEST =20 If unsure, say N =20 +config MEMCPY_KUNIT_BENCHMARK + bool "Benchmark string functions" + depends on MEMCPY_KUNIT_TEST + help + A benchmark for memcpy() and memmove() functions, + with both aligned and unaligned buffers. + + If unsure, say N. + config IS_SIGNED_TYPE_KUNIT_TEST tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/memcpy_kunit.c b/lib/tests/memcpy_kunit.c index d36933554e46..e7588b868125 100644 --- a/lib/tests/memcpy_kunit.c +++ b/lib/tests/memcpy_kunit.c @@ -493,6 +493,109 @@ static void memmove_overlap_test(struct kunit *test) } } =20 +#ifdef CONFIG_MEMCPY_KUNIT_BENCHMARK + +#define COPIES_NUM 100 + +static void memcpy_bench_size_align(struct kunit *test, int size, bool una= lign) +{ + u64 start, end, total_ns =3D 0; + char *dst, *src; + int ret =3D 0; + + dst =3D kzalloc(size, GFP_KERNEL); + if (!dst) { + ret =3D -ENOMEM; + goto out; + } + + src =3D kzalloc(size, GFP_KERNEL); + if (!src) { + ret =3D -ENOMEM; + goto out_free; + } + + for (int i =3D 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start =3D ktime_get_ns(); + memcpy(dst + unalign, src, size - unalign); + end =3D ktime_get_ns(); + local_irq_enable(); + total_ns +=3D end - start; + } + + /* Avoid division by zero */ + if (!total_ns) + total_ns =3D 1; + + kunit_info(test, "memcpy: %saligned copy of len %d: %lld MB/s\n", + unalign ? "un" : "", size, + (COPIES_NUM * size * 1000000000ULL / total_ns) / (1024 * 1024)); + + kfree(src); + +out_free: + kfree(dst); + +out: + KUNIT_ASSERT_EQ(test, ret, 0); +} + +static void memcpy_bench_size(struct kunit *test, int size) +{ + memcpy_bench_size_align(test, size, false); + memcpy_bench_size_align(test, size, true); +} + +static void memcpy_bench_test(struct kunit *test) +{ + memcpy_bench_size(test, 2); + memcpy_bench_size(test, 64); + memcpy_bench_size(test, 256); + memcpy_bench_size(test, PAGE_SIZE << MAX_PAGE_ORDER); +} + +static void memmove_bench_size_align(struct kunit *test, int size, bool un= align) +{ + u64 start, end, total_ns =3D 0; + char *buf; + const int shift =3D size / 10; + + buf =3D kzalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + + for (int i =3D 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start =3D ktime_get_ns(); + memmove(buf + shift + unalign, buf, size - shift - unalign); + end =3D ktime_get_ns(); + local_irq_enable(); + total_ns +=3D end - start; + } + + if (!total_ns) + total_ns =3D 1; + + kunit_info(test, "memmove: %saligned move of len %d: %lld MB/s\n", + unalign ? "un" : "", size, + (COPIES_NUM * (size - shift) * 1000000000ULL / total_ns) / (1024 * 10= 24)); + kfree(buf); +} + +static void memmove_bench_size(struct kunit *test, int size) +{ + memmove_bench_size_align(test, size, false); + memmove_bench_size_align(test, size, true); +} + +static void memmove_bench_test(struct kunit *test) +{ + memmove_bench_size(test, 64); + memmove_bench_size(test, 256); + memmove_bench_size(test, PAGE_SIZE << MAX_PAGE_ORDER); +} +#endif + static struct kunit_case memcpy_test_cases[] =3D { KUNIT_CASE(memset_test), KUNIT_CASE(memcpy_test), @@ -500,6 +603,10 @@ static struct kunit_case memcpy_test_cases[] =3D { KUNIT_CASE_SLOW(memmove_test), KUNIT_CASE_SLOW(memmove_large_test), KUNIT_CASE_SLOW(memmove_overlap_test), +#ifdef CONFIG_MEMCPY_KUNIT_BENCHMARK + KUNIT_CASE_SLOW(memcpy_bench_test), + KUNIT_CASE_SLOW(memmove_bench_test), +#endif {} }; =20 --=20 2.53.0