From nobody Sun Feb 8 05:47:59 2026 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 91C6B24886E for ; Thu, 29 Jan 2026 00:43:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769647415; cv=none; b=sdpOswvQGKAUeuorEpW1xomuGbCt6zkf3rV6//uUN3e0hnXgLZUauzT/Y2WS5p9tYi6lUAkV4TucixJsxfxu1InpBmguVexr7m/8aasswmQmvcsAN+ooC88B00sgOdm4l7oKAJruZHvPPgZIvUpTW34VZAGcol3G6aEhqa/BQAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769647415; c=relaxed/simple; bh=wKrqvW+3nNh2Ch+2s1glgCApV0mKCVoK/UY2m6XkPis=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=aeezf0aXx562GbeBxaeA5mLq2sQivqbiDIA0gLijk8tamsbL9XktAEBddsDcvY87G9hj0ZW4gfn8Byjdqjs68fQNDBsW517sprLrPuOKPyBtkUtLXZ1dApDtcA5/cVNmy3V70WafWEDeOkQM747wR8/fc4dbOP7phaIJMWGVs74= 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=JbVp6w1C; arc=none smtp.client-ip=209.85.221.53 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="JbVp6w1C" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-4358fb60802so225450f8f.1 for ; Wed, 28 Jan 2026 16:43:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769647412; x=1770252212; 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=nXvOriKjdO7AP6EHG8XNIDO57PdRSi1XjsvG1SuAeBk=; b=JbVp6w1CM1o37D+d88YUyJ//dpCrmt1Pf95ATaDyH/fCBiTDCGwquxZ0dTR/F7RHBf kLwg2rO1aT4vq2r94xDcj6c5Re5+89Pq7/CGuL/0MwtPLpXUPN+ruUDGPhr/T2RkKl0U FpWTD3XKpRNKhcGgrbJFLlmYbQkv7TJZHmiupdpVnzd6/cdp+WJ+VvjKz6ED4EumXn1s B4w0vMVH3rU6gP4ifdQfePcYKi+zLR70tS05L3L+3QdPBy6Ry0lzmhHGsHNJNThMC8M2 WvForggjq96iOtrJOz8d2WP3ZXxles0OYNRT0gfmvD4fVZuYVm8vHFZvNh3G88oN4VDJ ORVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769647412; x=1770252212; 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=nXvOriKjdO7AP6EHG8XNIDO57PdRSi1XjsvG1SuAeBk=; b=qJbaBEAUJ9vZv2l2gP+n8aJKtRqcM2pQ6296URnlULhHWDZt2/p6YQz/u1pK14ySRf mBUJSST0hwCXE0O1/BGHkrGaZs10XOtLqrsivZTSuQLJRRoMAAieBuqkQRD+WZhQxCa3 IuZ81nsAN0bu+SKgRvZOwYeQJHnblXGaAUoOE70ize1MBobSp2YEPKUjw23V7SW8mVl6 9oFFOoA7lElDnCuvlfiKRaeq0NrMMPKX9ik8ZMqfTk1hPaxO/4v2VaYivirwfs9mXGOY dABEO3i9IU00M8CsbNPyxFhdlMNC6ASkwm6AYeHbCpO5ICa4cpI4IFEBOFvpqAKz3Qtw QucQ== X-Gm-Message-State: AOJu0YyEkXAJ13jCCf2I8q+tOzcEu+HfBUuFnHfN9AnSQIF+yeuigf4g vIAvcdiLLPgStKdvDVtGeb6hwEovGdgiSRzC/LePQ+qJUfHWfroPBPD7BTEf3LmAd1k= X-Gm-Gg: AZuq6aLC4kK//9WcKZNDGh4gISadyclUQPjdwrWayXOxcA0cebHpxPExeQ/LPHRY35c O/oFNyJQvvLNkfKpp1eIA23a1Vje/Ud94wK5dPdeJ3S9DcuepFj4UjbcHqKemLB5YZsqkLdjpBa LmGbzsztWD8OJ5BUJ1//k+ifWsJa4fo5lBBux210/nnuxyGVAID28i13epeKgARM1qc+hUijRkH 0g69FZxwFXXs8iSrGnC0WCKRN9khsotslZDcQ/MlH/CiEPHzLuc8Rsr66a11k1eESHdr0l+EBOz f4oTnUhgmyisZgn3WDCHa+oQwKi15CAq+yDH7JKIqn8QQSVTePFtKhdLMltU3k/Iu2zq2WdODmS iZYgRy+rUUqSiS5HqwtTkyena3BtpTSgqzqEsytHgFYPazVXxny9/t8P/N1Hawmhd2mBS7a51m8 S9J/WZb54D9rLUrPzcl5DjtDaiw+7x8fWB+HngQu2zAfBoigaiVoL3U3EnI1loQPA= X-Received: by 2002:a05:6000:288e:b0:435:953e:5897 with SMTP id ffacd0b85a97d-435ea2104a8mr1751937f8f.25.1769647411511; Wed, 28 Jan 2026 16:43:31 -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-435e13235f5sm10202093f8f.29.2026.01.28.16.43.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 16:43:30 -0800 (PST) From: Matteo Croce X-Google-Original-From: Matteo Croce To: linux-kernel@vger.kernel.org, Andrew Morton Subject: [PATCH] KUnit: memcpy: add benchmark Date: Thu, 29 Jan 2026 01:43:28 +0100 Message-ID: <20260129004328.102770-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. Signed-off-by: Matteo Croce --- lib/Kconfig.debug | 9 ++++ lib/tests/memcpy_kunit.c | 108 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 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..33080dddc58e 100644 --- a/lib/tests/memcpy_kunit.c +++ b/lib/tests/memcpy_kunit.c @@ -493,6 +493,110 @@ 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; + } + + preempt_disable(); + for (int i =3D 0; i < COPIES_NUM; i++) { + start =3D ktime_get_ns(); + memcpy(buf1 + unalign, buf2, COPY_SIZE - unalign); + end =3D ktime_get_ns(); + total_ns +=3D end - start; + cond_resched(); + } + preempt_enable(); + + /* 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; + + preempt_disable(); + for (int i =3D 0; i < COPIES_NUM; i++) { + start =3D ktime_get_ns(); + memmove(buf + POS_SHIFT + unalign, buf, COPY_SIZE - POS_SHIFT - unalign); + end =3D ktime_get_ns(); + total_ns +=3D end - start; + cond_resched(); + } + preempt_enable(); + + 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 +604,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