From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.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 351112772D for ; Wed, 4 Feb 2026 23:22:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770247324; cv=none; b=azfCC8ItoliOmsywSg9zYwADfGVmjp79qJUCgJ04QhYTx1KijfwLdbdBwU0fIdnuqQksPIdUUXYi6WXBfsDSIATODdsrqCKRas4a9lgBlbAsFPF2O1BrbCXPen9VK6yd5xvH/AFUHfjQi7LG5JVpAL/ize0LuBHBJdQBjoMGN6g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770247324; c=relaxed/simple; bh=orfDQNrfPP4TpH1kwllZQ1/AdbMqP1eZlspxdCES7hc=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ez0HVrM2IrL6lHJYKK2ABDnXRnSqdAOJtCDrDlcCvwF38C9vmzMYISO9wkxiwmVFjK+XJnMzxlvKJeL0REeWXRweaQmHO6QE9cCogQanbZM6rqtDH/5ZXcySTjP0wZVNct3onQSJxit+iMGTK5VWH8pDqOIiIFISsixyBB6qXrM= 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=Ykz/mqwV; arc=none smtp.client-ip=209.85.221.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="Ykz/mqwV" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-42fbc544b09so356185f8f.1 for ; Wed, 04 Feb 2026 15:22:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770247322; x=1770852122; 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=db6u3Xm9jBOH2qCacsGKBMpOEu5rbKh2MGPseD5T27k=; b=Ykz/mqwVHaJoYRJPxUj+fNpEKbEpxf3FEAGGsbn7mprFkV8gEDn/3IVYdnoGK0JlMn LYRCLK0Mk0BFq8RY7jtkd7JfRvsbm1LYtpjycAMvpduXxSoXDvR+mr9/Hv2CmJ9TPrCH rVFqt5CarFbZYipDzETPppQCM8CxLtl+/jrB0d28EiXMS23u9OZZOl1AUqd8eTterU/V nXirppVyeVpck5gugerhe6DhuKgb9lz67cmpsWvL33h4L09lr/hA0eJ1I1vfnRxEVP6p 627SXMvk05WuXw22kyXrOQGNNKVaxYAxq5wli1g44lSAceLxOTu4BxGC0aU8ii0Gf+ZT kWPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770247323; x=1770852123; 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=db6u3Xm9jBOH2qCacsGKBMpOEu5rbKh2MGPseD5T27k=; b=OLmo92xVwKIkrGSdpvjUa5oh3Jt5UJ1QGW+TTSPMLRVm2fmhcKIzKolzllYiKRV1Es CRmLVbgIzHWot1n/yfLK5Hwz/z3ZMqP+iTdycDgW4WEtkmDHz/J9fFwxyCsu8IMZV/e9 Jtf7WnEBVdxRDwebMQDYK4GtV/nSbUl92W7WXyBNIpHWuNkrVTrGN1qbrFmsMi9nX5fj dpGMOrS9wJZ5fFdIU4vyUARzy4YP8HTlOnevqpOF8SX8VbE5elNN42dcIP5FyMkuNQC4 TM5SNh+oNxnygNT6MifDkbwjigsLd/mzxZ9hd18I3ixTMhEYmG/t+ZAD2PWN/2IwdGnt 67Bg== X-Gm-Message-State: AOJu0YyUaT1Ai12OMQPTrhRg+/WT/gtC9VOd4GMQKM4inx5m9IX7Nm36 wfWR1BEtLFvkBX42W+wINeJjAHTbEeCn0LdozZrXyE0TrM3OHATr4upD5Pc06ruGYek= X-Gm-Gg: AZuq6aKXAkwhV6r6Exi0g6Ih9A03ycKC94JrP46AA3ZGyJbU9ZPrKfPZZov7zL9m8Ud iiSJC2Tzv1EGIvgdHtugSUnu1EZz25EeOLLzGBUtpYSFpbrcZlRgp9V3dYlEUu533736J329Z7f 2VdUvEwExbXfRC1eNih7v0WtQT+ir7HpD+sjafERc6NP++fdBVC9DuBnfQl9ttPxeXaZ0TTEOIY r4gPuAQjkT7Ywxc48UmWPkLNvXMopEWWsZ33iaWUKBrmu5kMLsqmOjC14Dszta3fTMKcYoNC+io 3Bot4EzFjkm65vQYxEe+BQBQ33b8xrBKmIVzWjXCJkzilpxftva2o8Kba0mCTWRoVqijrR76gWP 2pdijAKAYsJRaPlBaD4CV40OYWUftcwNpNuXwRD9QeQUrUZi3hh5nPXn7xL9fdgzxPfKuVUqRmI Uz3Sif/lPaRhQVHWvfyhelQXAZFcUrl+0tGZe5KnCEuhN1DXtbGSscF9RDci7pCD0= X-Received: by 2002:a05:6000:3106:b0:430:fc5a:40ac with SMTP id ffacd0b85a97d-4361805bd9dmr7624721f8f.54.1770247322343; Wed, 04 Feb 2026 15:22:02 -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-4361805815esm9171003f8f.20.2026.02.04.15.22.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Feb 2026 15:22:01 -0800 (PST) From: Matteo Croce X-Google-Original-From: Matteo Croce To: linux-kernel@vger.kernel.org, Andrew Morton , David Laight Subject: [PATCH v4] KUnit: memcpy: add benchmark Date: Thu, 5 Feb 2026 00:21:59 +0100 Message-ID: <20260204232159.12046-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: 8bit 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 --- 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 ba36939fda79..02868c4397cb 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2880,6 +2880,15 @@ config MEMCPY_KUNIT_TEST If unsure, say N. +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) } } +#ifdef CONFIG_MEMCPY_KUNIT_BENCHMARK + +#define COPIES_NUM 100 + +static void memcpy_bench_size_align(struct kunit *test, int size, bool unalign) +{ + u64 start, end, total_ns = 0; + char *dst, *src; + int ret = 0; + + dst = kzalloc(size, GFP_KERNEL); + if (!dst) { + ret = -ENOMEM; + goto out; + } + + src = kzalloc(size, GFP_KERNEL); + if (!src) { + ret = -ENOMEM; + goto out_free; + } + + for (int i = 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start = ktime_get_ns(); + memcpy(dst + unalign, src, size - unalign); + end = ktime_get_ns(); + local_irq_enable(); + total_ns += end - start; + } + + /* Avoid division by zero */ + if (!total_ns) + total_ns = 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 unalign) +{ + u64 start, end, total_ns = 0; + char *buf; + const int shift = size / 10; + + buf = kzalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, buf); + + for (int i = 0; i < COPIES_NUM; i++) { + local_irq_disable(); + start = ktime_get_ns(); + memmove(buf + shift + unalign, buf, size - shift - unalign); + end = ktime_get_ns(); + local_irq_enable(); + total_ns += end - start; + } + + if (!total_ns) + total_ns = 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 * 1024)); + 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[] = { KUNIT_CASE(memset_test), KUNIT_CASE(memcpy_test), @@ -500,6 +603,10 @@ static struct kunit_case memcpy_test_cases[] = { 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 {} }; -- 2.52.0