From nobody Thu Apr 2 00:13:50 2026 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 47CE13B2A0 for ; Mon, 2 Mar 2026 02:31:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772418663; cv=none; b=GPWDJQTNvpkizE5Eckk85jJDKsQWB4H8BiVJAAv2ERN/37850NkwvuaCD3FVm4nISza9CnSoTeBM+od2D3pDJe81xDR/d5r89F0VHNJn1emj9tzt/BHzoNREqPvE4j0WuXy/tQEqiTt8uZTtw+QzalvHXkr/DIZxMsTHpiS4TJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772418663; c=relaxed/simple; bh=u3euo/mpfzq7bpYJZUZWX2vY3seyAu7wZYcM5iBuuBA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=HPOWHmateMtvMgd55UNmGJ349dJYfFUmCEIhO43IA6G8X5njZTb6rmRc1l8/UyeyybTcHSnj/jHUgu3HrMSQSEyNXYMgpF1F2IDibWPHSqiXIFTWAgX2crnc/7sJ3o48fiKVwP645yuD7i6j4mI8gBT67cym+wbrRI1+9mvgMaA= 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=bk5lUGyO; arc=none smtp.client-ip=209.85.128.44 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="bk5lUGyO" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4838c15e3cbso34540585e9.3 for ; Sun, 01 Mar 2026 18:31:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772418661; x=1773023461; 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=WrRh6t/RdWKqG/BjR0id2zS6GqhG+1lVaxqoEpCdDJM=; b=bk5lUGyOyGLLPPD6BD4pDjAwXcE0OsLzT8UIVfaxZJECBifE43byFq/WFdPoxPJzmf /MiVRvvRE9kivQbzE8e1rniCWouKUIO1GsjkN/l9YFdhZLEEKmsDw5gQyfY6CXRgLEAM jL4C/Wiets3hbkCEszY9xSrFWMoNOoizAQPHadwGgjSeESSgwxu+bfzV8Vwsmp+A3hzN evUR1v+cvfRWwneg4nrAZTapkkfC6fyZvFQogSrQcr7TGLjy55IlcaHcj2JoNWpQKgKm NcPc889v9QH9c9yIVU4TCQ543uupbnktVgcS2dombHD14hS42TArkSvsgelqlf6m8UoJ 7ELQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772418661; x=1773023461; 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=WrRh6t/RdWKqG/BjR0id2zS6GqhG+1lVaxqoEpCdDJM=; b=LhtpjUmszxNZ+8+GSzy6/xLSUxepQl0S5YXjeQr74jEsPFQq8v1RHD/S5s7DXUmYEm oXxgXNGRamDG/zjZ4VTjnhztulgl5cQtDkGkCC4gtNCtMkD7gvTvlnGTGMn/TlzU5SQV HiXtneKMxorvyCqzX7I7GFZN8OAR5x8KViPB/7imyn/B3SRiS4r48ckqHotBZ4/HHVCG wr0t4f1X0BWXEea/cNDydATUXBnfe5+wp1sf7H2bEjQI+rgX6SJkYdIxD5YSGbs1Regr 6S2BUmvQBQ4oGdO9FK5xeqLkl3NL/FxSxA65BdKgmcO/W1vY4do/dft3+F4SZOpHx+tm 3nCg== X-Forwarded-Encrypted: i=1; AJvYcCX/m49KHZVwMHfjpUvvC0GRtJVdPAa5dCTjM6i6IcEjPHBFmASDdnjPrlLZw5U4jkCUsNhr9kReB2qsI4Y=@vger.kernel.org X-Gm-Message-State: AOJu0YzBnQRrTghCIoQykHxbyWySK/qQqGcssAbmnNmFDRE9uyqmjPav GH0xeAAv7xnQskx8pFMIyypCf7Us09IYd120Dye7LP2fTyaJSImy49ut X-Gm-Gg: ATEYQzytTmUwvMKkyPBSybwXrZUTJjhBUNppzaPD+D0yMFVQRgomUUxOlBJA4ASyxKX TfGeI+/LSpvQVyRzxFh7PcD3QgM6Rc0n86w04KI8inv9a3iROV5I34L9EnwtVZMHdL/H0/3TiXO gABU8wi458jRkOpiWkqNEGmWZRmUXisJmgpkxhEQeAPlW58BFJl0fkFELyToG4Iny+uFJYibM5y B6xCyOaC4AVSyoeOokWXZXRqGGUW/uxGd/I/2jRfEwVPClL+kboR4NUcko/HjwFYWfYXinb0kNv GS1Xdl+LRpfJDhGVRlEKntcboAw03l3sLQVY+/gBydRGjyN0mW/2wDUFhWA+p1WTo4ZdCnm9WYZ TCexfWkrcKqdfr7Ox4MXe6vtpvZf+cxG7XtSZhPxvWY+QoIzpWKWuFHZSGOEfkIL7kXKrUh7roB eW34bQtUQfVxWuKMFqR5jUu7MVhttEwshoCJSPqGNs6mSW7aGLAz/J4WqaNk+cjooWQJB49kv3 X-Received: by 2002:a05:600c:3542:b0:480:1e8f:d15f with SMTP id 5b1f17b1804b1-483c9bc5596mr195144855e9.2.1772418660419; Sun, 01 Mar 2026 18:31:00 -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-439b4175fd2sm7606417f8f.14.2026.03.01.18.30.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2026 18:30:59 -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 v6] KUnit: memcpy: add benchmark Date: Mon, 2 Mar 2026 03:30:52 +0100 Message-ID: <20260302023052.73943-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 --- v6: fixed u64 div on 32-bit architectures lib/Kconfig.debug | 9 ++++ lib/tests/memcpy_kunit.c | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 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..ea9877e4985a 100644 --- a/lib/tests/memcpy_kunit.c +++ b/lib/tests/memcpy_kunit.c @@ -493,6 +493,112 @@ 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: %llu MB/s", + unalign ? "un" : "", size, + div64_u64(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: %llu MB/s", + unalign ? "un" : "", size, + div64_u64(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[] =3D { KUNIT_CASE(memset_test), KUNIT_CASE(memcpy_test), @@ -500,6 +606,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