From nobody Sun Feb 8 05:28:44 2026 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 E152127FD7D for ; Fri, 30 Jan 2026 00:32:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769733164; cv=none; b=jpS3LVyoh3u5ldGWH9dYmcVZgdn8at7xy3eBd3mnQ0NJCH1MvybtbvkvynJRY4ZB/uhTgIg00cI/iaduNdArP69DoeaO+SEZmwdDVHw4LvJ06KicMY0d1h/hWlMlC0VAML4e8ccxqceW8+Ms80GCp40It/JqiyPYFU4fIdZHXEY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769733164; c=relaxed/simple; bh=Q78uS4dk24NS63wDfvVyVIG5jjLq5Je7Z/wJmPfHIZk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=CPvpW/HiG3NXFwivoHud5jerRf8SeaDDp17R4Kl0YumZiuhccz37cLceU20BZaWg7HHhM3lSzLpmABU7mbVst2/vmFAo66OgQbYdT9OnYX9/PO6x08ICB4p/7WaQJCJyU2EtjCs9PiwbLXylmoECoc5dm0EYRqJHTt6PvCE6Vko= 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=EnIO8s5u; arc=none smtp.client-ip=209.85.221.46 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="EnIO8s5u" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-42fbc305552so1487396f8f.0 for ; Thu, 29 Jan 2026 16:32:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769733161; x=1770337961; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=bTWhAbNdUVlgri/WhPecZoDgZrAX+j68eoP73DsEs5A=; b=EnIO8s5uVRrDnpa1l2/a5KD0e73QOTcAoK7o8BBPC1ZqKvbtx53tN0pbijHomvlq4B 7WgyvXiN4CDWYKhMwf+RfHXILfQv/Dc/oEnyGHGxTPSfHxR4FyJqU02LBSDsIj2zUc3H ymiUbPVIP63f13u7EWIpd1bsa0mIOQeTGQXk/WE5p3UfGPVcYerDH6/54Gapp3iS6JC4 2UztifJRb6Vd5fO/NrqGIxf6yuXTL64lazBCB4d3bc4rV388Dd3ENEk4A8Pw4O/mqKhP LRROBX4WPrd5VH2Ar5ff9nllIKXE7jx1WAsPoqGsn0WXVBLNv+WZn9oWGsx6qfgMSrN+ b6fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769733161; x=1770337961; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bTWhAbNdUVlgri/WhPecZoDgZrAX+j68eoP73DsEs5A=; b=XsgPdMEtl3ixIPhCd8SDVeCYmIlkYg2LKgFEDBulrLISrgw8VTF9btQ+Njw0IOMsma tfM1/zrjUrF8o8VPUFY/go5+NWNK6hFFBJ6BeDBZABlYcG7AWcFJ5CfLL5TgRZkL+R0i G5f7oFv8q1ciXfRkPnWIMGUtDYA067I5zN+OjZuTmLMJ/3Ii5v5k2MHhxEohQIe+UIsm yIxH9IIBRSap11PBU7wF8WHI0M5O27aUzEJOD7Fc+Imrwr8zfksOWa0uvg3Tq7CZG4NQ rtUArIMJTgcSJoGMQ2TsGh/UhCxz6ln7eizt/xylr/4NMkbc1TBelojihZnzKN2I9yoR 8xRw== X-Gm-Message-State: AOJu0Yyjz3SUgBhpXmVv0hDlqKK6SAm4mCuHwQfzOy8EV35v/ZFcCb2D fb2USdUveuazfbsN3BrVMp+IbnpcFCERKbQ8viB9pcXiKWfQXSZTd1mBm2xewqwRPI0= X-Gm-Gg: AZuq6aKPdsFQYwBA0aUFSfoF5YwlFwoUtpvkZl9AgsijuFLP4AGK0E9vg6hYo/tkUmz C/4qU2hOb3FbUid4vHiO9SdEJG+VhKboKQ229G15/OF+g3U/oRaJ25vQcJNcJe6hlgkOCPibYnR QJn+c1tEVy+OHU7E5uqdxpd9ecfTvskBfmPdzW30+XEwUBRHbfWvynhxrteXuzDZrLdkKg/EEVd zYspSmD6ogom667JiLL46sqDaREVA1LfhOM45c6vz48Z6gro0r4cyQLJz4JaZNTSg55awCK7r/C pLBDzFmjPf2gEUI+zI6MazqScOebrWxZcU3ltyj+eE0aE5+0i3cthTI9HnSfvH+QyMXmqblcOS3 2Y0vBOZJAG5R4o28ShsGlF/QypsXtGfBoFqYVqOSYRjXWtP4M7ApYCeDKknvEyl1efMfWgpg7Je uBqO8By4LgGUrGceEJxTEq823i5UCbtQOz1FV9y73vSEN2U7sQwS5R3PtQUMyrSQg= X-Received: by 2002:a05:600c:474f:b0:47b:de05:aa28 with SMTP id 5b1f17b1804b1-482db45927bmr12038185e9.2.1769733161060; Thu, 29 Jan 2026 16:32:41 -0800 (PST) Received: from turbo.teknoraver.net (net-37-117-189-76.cust.vodafonedsl.it. [37.117.189.76]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-435e10edfe7sm17611999f8f.14.2026.01.29.16.32.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jan 2026 16:32:40 -0800 (PST) From: Matteo Croce X-Google-Original-From: Matteo Croce To: linux-kernel@vger.kernel.org, Andrew Morton Subject: [PATCH v3] KUnit: memcpy: add benchmark Date: Fri, 30 Jan 2026 01:32:38 +0100 Message-ID: <20260130003238.22704-1-teknoraver@meta.com> X-Mailer: git-send-email 2.52.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 twice: first with buffers aligned and then with buffers unaligned, to spot unaligned accesses on platforms where they have a noticeable performance impact. Sample output: # 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 400 MBytes in 22 msecs (1= 8027 MB/s) # memcpy_bench_test: memcpy: unaligned copy of 400 MBytes in 23 msecs = (17360 MB/s) # memcpy_bench_test.speed: slow ok 7 memcpy_bench_test # memmove_bench_test: memmove: aligned move of 399 MBytes in 17 msecs = (23012 MB/s) # memmove_bench_test: memmove: unaligned move of 399 MBytes in 17 msec= s (22381 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 --- lib/Kconfig.debug | 9 ++++ lib/tests/memcpy_kunit.c | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ba36939fda79..02868c4397cb 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2880,6 +2880,15 @@ config MEMCPY_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..7e7918365db4 100644 --- a/lib/tests/memcpy_kunit.c +++ b/lib/tests/memcpy_kunit.c @@ -493,6 +493,108 @@ static void memmove_overlap_test(struct kunit *test) } } =20 +#ifdef CONFIG_MEMCPY_KUNIT_BENCHMARK + +#define COPY_SIZE (PAGE_SIZE << MAX_PAGE_ORDER) +#define COPIES_NUM 100 + +static int memcpy_bench_align(struct kunit *test, bool unalign) +{ + u64 start, end, total_ns =3D 0; + char *buf1; + char *buf2; + int ret =3D 0; + + buf1 =3D kzalloc(COPY_SIZE, GFP_KERNEL); + if (!buf1) + return -ENOMEM; + + buf2 =3D kzalloc(COPY_SIZE, GFP_KERNEL); + if (!buf2) { + ret =3D -ENOMEM; + goto out_free; + } + + for (int i =3D 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start =3D ktime_get_ns(); + memcpy(buf1 + unalign, buf2, COPY_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 %lu MBytes in %lld msecs (%ll= d MB/s)\n", + unalign ? "un" : "", + (unsigned long)(COPIES_NUM * COPY_SIZE) / (1024 * 1024), + total_ns / 1000000, + (COPIES_NUM * COPY_SIZE * 1000000000ULL / total_ns) / (1024 * 1024)); + + kfree(buf2); + +out_free: + kfree(buf1); + + return ret; +} + +static void memcpy_bench_test(struct kunit *test) +{ + KUNIT_ASSERT_EQ_MSG(test, memcpy_bench_align(test, false), 0, + "aligned memcpy benchmark failed"); + KUNIT_ASSERT_EQ_MSG(test, memcpy_bench_align(test, true), 0, + "unaligned memcpy benchmark failed"); +} + +#define POS_SHIFT (2 * PAGE_SIZE) + +static int memmove_bench_align(struct kunit *test, bool unalign) +{ + u64 start, end, total_ns =3D 0; + char *buf; + int ret =3D 0; + + buf =3D kzalloc(COPY_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + for (int i =3D 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start =3D ktime_get_ns(); + memmove(buf + POS_SHIFT + unalign, buf, COPY_SIZE - POS_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 %lu MBytes in %lld msecs (%l= ld MB/s)\n", + unalign ? "un" : "", + (unsigned long)(COPIES_NUM * (COPY_SIZE - POS_SHIFT)) / (1024 * 1024), + total_ns / 1000000, + (COPIES_NUM * (COPY_SIZE - POS_SHIFT) * 1000000000ULL / total_ns) / + (1024 * 1024)); + + kfree(buf); + + return ret; +} + +static void memmove_bench_test(struct kunit *test) +{ + KUNIT_ASSERT_EQ_MSG(test, memmove_bench_align(test, false), 0, + "aligned memmove benchmark failed"); + KUNIT_ASSERT_EQ_MSG(test, memmove_bench_align(test, true), 0, + "unaligned memmove benchmark failed"); +} +#endif + static struct kunit_case memcpy_test_cases[] =3D { KUNIT_CASE(memset_test), KUNIT_CASE(memcpy_test), @@ -500,6 +602,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.52.0