From nobody Sun Feb 8 04:33:47 2026 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (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 0C2031AF0AF for ; Thu, 29 Jan 2026 23:45:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769730347; cv=none; b=GopW4GMFgOj8LGopplc32tuhe/4SRm5L+Riwa2NjA7pfL3GEohacgHatGmfj7HSB9E9y7Ew4Jko5T90mNjLRU4RPcDQ997j2DJGAUKW6bU98frk3E4QOVn2qYOz40uTDnwQDfxBWAAiMPeLgcv/gdJGo56XQH0kRz7pilyhRzrg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769730347; c=relaxed/simple; bh=sIoPlCrqhEd/rbN/EWQYL9DR1iKOf7xrkEQZLZwVhr8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=WlX1b5rITVTKDXtQvIidUH8TbIxTF91Dntbg6AJGyu2H9IS9eygwpkZmi/p/LfEtkpxS+Kc7UocsJub8rPdhNOynS89vuGEsLjr5+P5qtn9J0nJo0OwGAS3CKAk3H/SWYIu9sngSS6Fgq5iTD5KoWOxxjyZ+nTbxY6qmJ9iMT/M= 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=SivU2SAf; arc=none smtp.client-ip=209.85.221.50 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="SivU2SAf" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-432755545fcso1115722f8f.1 for ; Thu, 29 Jan 2026 15:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769730344; x=1770335144; 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=HCyoZKOugjzmCDUZ0RMHLeBMaovT6oebnqfYJZOcmAE=; b=SivU2SAfakzFb9aeYve/6MLKLEc3xLy8gU0KXPFcN2REQoSZz6BKypTEOSURGoEbed cx7DUaMjcQblvPGpI4tO//lXamwrPfRWbh4YBXzfpNbmO0K7Npr8bXR4myTcX34WkFcZ ixuco9pKMmOdBG/+JzqBg8njbpr6801VaydmDQkW//dzdmRxX3yQ1ZPKe1Q2hYWdnJbE CLOO5VgSlHp+UFo3dbr0RkG1JZne0TnZkawtN2sN8RVBDLBX3AbvG9oOLFD3nx2RwVsD HGtN98tV6Vu2zo8wuK6vp4OaP7eAl39E2bdeXEYE19R1La+eABf/iZ1Bv33rHzk+BBxf w/Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769730344; x=1770335144; 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=HCyoZKOugjzmCDUZ0RMHLeBMaovT6oebnqfYJZOcmAE=; b=N6t4xIhdDgcnhlDXk4u3zN2IOLvnBTaiHMsTRW4RntFtjdeCKZTYZkBGv0TdxLTuPW rseUyW9g55feY+fkvr8QFyPrCCu5YMPYZwwb7X+zH0I+NE/h+paDT5x0mtfwPFkoOk5A UVg6eEUgcMLgtUHEbB6/842rQB1RMrwP50QvppQOQ77MVfWUaY+y7tAIS+OjT9A276a+ bxu9uXBw1fEsl+0UPd8FIfkokR8dFydeFA71EWcpbQ7TruNhCoIFjMlyf2dptx5RLI+y tjCWBTR/ebfrjIYh2hxxc3tcRHOeLGarVgDbJpLY8ww1UBCi+NJtixbmKRULNCM4VXSi 1FNw== X-Gm-Message-State: AOJu0YzQyY/lcQFHHONZY0BKC5Xg5flS1ToHDbNkG3SWQB5tcHK1/pSj anmXsPHvZnS36cACSDitXF0LTehae6huVG2PDTGXTOGtnXy3AMwco1CwG3xRmyJc7y0= X-Gm-Gg: AZuq6aKwxdN5RDZ1vYTPCF7jg/mnnENQ9sHZutR4QwXHIaEo6RHetL72qJ9WNZBC08v egR3JxYnqY4GsmeLGgi9OR4jG68NRC3zQiPiHQU9ObKA54964xM+D8akRw3XINEYb5Xgx1dWnLl G2IBykBWT/d1mEFLR8gafNCTV4LySxpWOHtx/1Df85zIMliAnQj0j2DI2miQJdeOU1jraIxN1+o vGTsxW63afSOAL/XZDq7OuRjIhB5VXBpcNy/TimsnwE9p3wApGG9V3oyuX37rFzEfFPCCbaLvLi NsJJYFWCTh2LznIzdrAqmobmLJYpQtus8pRtoZkiSi79tBbVXj0KbsBDqMYk0AeVWKTBIiEtzp8 V5jojAZmLXPQ+xlKPrBW3uyaDSrdDTHYobS2uHq9mOPjd1Rb9hZOlZZzCP0p+VzTJi1L1odQv06 rM7aKwtw+XUgZPO4brqfFBeOpv2z8RvpEuZWJMIf6mKa+my4FihJ49yYx9QOudz3A= X-Received: by 2002:a5d:64e6:0:b0:431:488:b9b4 with SMTP id ffacd0b85a97d-435f3a7e627mr1846298f8f.17.1769730344211; Thu, 29 Jan 2026 15:45:44 -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-435e132303fsm18455828f8f.36.2026.01.29.15.45.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jan 2026 15:45:41 -0800 (PST) From: Matteo Croce X-Google-Original-From: Matteo Croce To: linux-kernel@vger.kernel.org, Andrew Morton Subject: [PATCH v2] KUnit: memcpy: add benchmark Date: Fri, 30 Jan 2026 00:45:39 +0100 Message-ID: <20260129234539.18513-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..100cda8d4f34 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 (4 * 1024 * 1024) +#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++) { + preempt_disable(); + start =3D ktime_get_ns(); + memcpy(buf1 + unalign, buf2, COPY_SIZE - unalign); + end =3D ktime_get_ns(); + preempt_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++) { + preempt_disable(); + start =3D ktime_get_ns(); + memmove(buf + POS_SHIFT + unalign, buf, COPY_SIZE - POS_SHIFT - unalign); + end =3D ktime_get_ns(); + preempt_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