Linux Hardening
 help / color / mirror / Atom feed
From: Milan Tripkovic <milant2002@gmail.com>
To: Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>, Kees Cook <kees@kernel.org>
Cc: Alexandre Ghiti <alex@ghiti.fr>,
	Andy Shevchenko <andy@kernel.org>,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org,
	Dusan Stojkovic <Dusan.Stojkovic@rt-rk.com>,
	Milan Tripkovic <Milan.Tripkovic@rt-rk.com>
Subject: [PATCH 2/2] lib/string_kunit: extend benchmarks and unit test to memchr()
Date: Wed, 20 May 2026 17:55:31 +0200	[thread overview]
Message-ID: <20260520155532.1139471-3-milant2002@gmail.com> (raw)
In-Reply-To: <20260520155532.1139471-1-milant2002@gmail.com>

From: Milan Tripkovic <Milan.Tripkovic@rt-rk.com>

Extend the string benchmarking suite to include memchr().
Extend the string unit test to include memchr()

Signed-off-by: Milan Tripkovic <Milan.Tripkovic@rt-rk.com>
---
 lib/tests/string_kunit.c | 53 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c
index 0819ace5b027..3a7d96e292da 100644
--- a/lib/tests/string_kunit.c
+++ b/lib/tests/string_kunit.c
@@ -881,6 +881,57 @@ static void string_bench_strrchr(struct kunit *test)
 	STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0');
 }
 
+static void string_test_memchr(struct kunit *test)
+{
+	char *buf;
+	size_t max_len = 128;
+	size_t off, len, match_pos;
+	char *res;
+	char target = 0x88;
+
+	buf = kunit_kzalloc(test, max_len + 64, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+
+	memset(buf, 'A', max_len + 64);
+
+	for (off = 0; off < 16; off++) {
+		for (len = 1; len < max_len; len++) {
+			match_pos = len / 2;
+			buf[off + match_pos] = target;
+
+			res = memchr(buf + off, target, len);
+			KUNIT_EXPECT_PTR_EQ_MSG(test, res, buf + off + match_pos,
+						"Failed at:%zu len:%zu pos:%zu", off, len,
+						 match_pos);
+
+			buf[off + match_pos] = 'A';
+		}
+	}
+
+	memset(buf, 'A', max_len);
+	buf[10] = 'B';
+	buf[20] = 'B';
+	res = memchr(buf, 'B', max_len);
+	KUNIT_EXPECT_PTR_EQ(test, res, &buf[10]);
+
+	buf[50] = (char)0xFF;
+	res = memchr(buf, 0xFF, max_len);
+	KUNIT_EXPECT_PTR_EQ(test, res, &buf[50]);
+
+	buf[60] = '\0';
+	res = memchr(buf, '\0', max_len);
+	KUNIT_EXPECT_PTR_EQ(test, res, &buf[60]);
+
+	buf[max_len - 1] = 'Z';
+	res = memchr(buf, 'Z', max_len);
+	KUNIT_EXPECT_PTR_EQ(test, res, &buf[max_len - 1]);
+}
+
+static void string_bench_memchr(struct kunit *test)
+{
+	STRING_BENCH_BUF(test, buf, len, memchr, buf, '\0', len);
+}
+
 static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memset16),
 	KUNIT_CASE(string_test_memset32),
@@ -910,6 +961,8 @@ static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_bench_strnlen),
 	KUNIT_CASE(string_bench_strchr),
 	KUNIT_CASE(string_bench_strrchr),
+	KUNIT_CASE(string_test_memchr),
+	KUNIT_CASE(string_bench_memchr),
 	{}
 };
 
-- 
2.43.0


      parent reply	other threads:[~2026-05-20 15:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20 15:55 [PATCH 0/2] riscv: lib: add optimized memchr() and extend KUnit tests Milan Tripkovic
2026-05-20 15:55 ` [PATCH 1/2] riscv: lib: add memchr() implementation Milan Tripkovic
2026-05-20 15:55 ` Milan Tripkovic [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260520155532.1139471-3-milant2002@gmail.com \
    --to=milant2002@gmail.com \
    --cc=Dusan.Stojkovic@rt-rk.com \
    --cc=Milan.Tripkovic@rt-rk.com \
    --cc=alex@ghiti.fr \
    --cc=andy@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox