Skip to contentSkip to conversation

riscv: optimize string functions and add kunit tests

PATCH Superseded Feng Jiang 3 participants 12 messages Jan 7 – Jan 8

Subsystems (2)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Feng Jiang

author
#0 lore raw

cover riscv: optimize string functions and add kunit tests

This series introduces optimized assembly implementations for several string functions on the RISC-V architecture (strnlen, strchr, and strrchr). To ensure correctness and prevent regressions, it also extends the string_kunit test suite with new cases.

The patchset is organized as follows:
- Patches 1-2: Enhance lib/string_kunit.c with new test cases for strlen and strnlen to establish a baseline for verification.
- Patches 3-5: Provide the RISC-V specific optimized implementations for strnlen, strchr, and strrchr.

Testing:
All patches have been verified using the KUnit framework on QEMU virt machine (riscv64). All string-related tests passed.

Changes:
- v1: Initial submission.

---

Feng Jiang (5):
lib/string_kunit: add test case for strlen
lib/string_kunit: add test case for strnlen
riscv: lib: add strnlen implementation
riscv: lib: add strchr implementation
riscv: lib: add strrchr implementation

 arch/riscv/include/asm/string.h |   9 ++
 arch/riscv/lib/Makefile         |   3 +
 arch/riscv/lib/strchr.S         |  35 +++++++
 arch/riscv/lib/strnlen.S        | 164 ++++++++++++++++++++++++++++++++
 arch/riscv/lib/strrchr.S        |  37 +++++++
 arch/riscv/purgatory/Makefile   |  11 ++-
 lib/tests/string_kunit.c        |  53 +++++++++++
 7 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strchr.S
 create mode 100644 arch/riscv/lib/strnlen.S
 create mode 100644 arch/riscv/lib/strrchr.S
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/5 lib/string_kunit: add test case for strlen

Add a KUnit test for strlen() to verify correctness across different string lengths and memory alignments.

---
 lib/tests/string_kunit.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
lib/tests/string_kunit.c +23 -0
index f9a8e557ba77..9f4fc72f0886 100644
@@ -104,6 +104,28 @@ static void string_test_memset64(struct kunit *test)
 	}
 }
 
+static void string_test_strlen(struct kunit *test)
+{
+	char *s;
+	size_t len, offset;
+	const size_t buf_size = 16 + 128 + 1; /* max_offset + max_len + '\0' */
+
+	s = kunit_kzalloc(test, buf_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, s);
+
+	memset(s, 'A', buf_size);
+	s[buf_size - 1] = '\0';
+
+	for (offset = 0; offset < 16; offset++) {
+		for (len = 0; len <= 128; len++) {
+			s[offset + len] = '\0';
+			KUNIT_EXPECT_EQ_MSG(test, strlen(s + offset), len,
+				"offset:%zu len:%zu", offset, len);
+			s[offset + len] = 'A';
+		}
+	}
+}
+
 static void string_test_strchr(struct kunit *test)
 {
 	const char *test_string = "abcdefghijkl";
@@ -618,6 +640,7 @@ static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memset16),
 	KUNIT_CASE(string_test_memset32),
 	KUNIT_CASE(string_test_memset64),
+	KUNIT_CASE(string_test_strlen),
 	KUNIT_CASE(string_test_strchr),
 	KUNIT_CASE(string_test_strnchr),
 	KUNIT_CASE(string_test_strspn),
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/5] lib/string_kunit: add test case for strlen

Quoted 3 lines · Feng Jiang

Add a KUnit test for strlen() to verify correctness across different string lengths and memory alignments.

I assume you also wanted the optimisation tests, i.e. something that does random filling and measure two algorithms by execution time.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw

PATCH 2/5 lib/string_kunit: add test case for strnlen

In reply to Feng Jiang

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

---
 lib/tests/string_kunit.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
lib/tests/string_kunit.c +30 -0
index 9f4fc72f0886..1d08850e439c 100644
@@ -126,6 +126,35 @@ static void string_test_strlen(struct kunit *test)
 	}
 }
 
+static void string_test_strnlen(struct kunit *test)
+{
+	char *s;
+	size_t len, offset;
+	const size_t buf_size = 16 + 128 + 1; /* max_offset + max_len + '\0' */
+
+	s = kunit_kzalloc(test, buf_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, s);
+
+	memset(s, 'A', buf_size);
+	s[buf_size - 1] = '\0';
+
+	for (offset = 0; offset < 16; offset++) {
+		for (len = 0; len <= 128; len++) {
+			s[offset + len] = '\0';
+
+			if (len > 0)
+				KUNIT_EXPECT_EQ(test, strnlen(s + offset, len - 1), len - 1);
+
+			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len), len);
+
+			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 1), len);
+			KUNIT_EXPECT_EQ(test, strnlen(s + offset, len + 10), len);
+
+			s[offset + len] = 'A';
+		}
+	}
+}
+
 static void string_test_strchr(struct kunit *test)
 {
 	const char *test_string = "abcdefghijkl";
@@ -641,6 +670,7 @@ static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memset32),
 	KUNIT_CASE(string_test_memset64),
 	KUNIT_CASE(string_test_strlen),
+	KUNIT_CASE(string_test_strnlen),
 	KUNIT_CASE(string_test_strchr),
 	KUNIT_CASE(string_test_strnchr),
 	KUNIT_CASE(string_test_strspn),
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 2/5] lib/string_kunit: add test case for strnlen

Quoted 3 lines · Feng Jiang

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

Same comment as per patch 1 (it would probably require to call for arch_strnlen() or something like this).

...

lib/tests/string_kunit.c static void string_test_strlen(struct kunit *test) · line 142

Quoted 2 lines
lib/tests/string_kunit.c +2 -0
+       for (offset = 0; offset < 16; offset++) {
+               for (len = 0; len <= 128; len++) {

You want to define these two limits to avoid the possible issues in the future if the code gets changed.

Quoted 1 line

+ }

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#5 lore raw

Re: [PATCH 2/5] lib/string_kunit: add test case for strnlen

Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Feng Jiang

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

Same comment as per patch 1 (it would probably require to call for arch_strnlen() or something like this).

Thanks, makes sense.

I'll add the performance benchmarking (random filling + timing) in V2.

Since string functions are typically exported directly by each architecture without an arch_ prefix, I'll introduce a generic_strnlen() (based on lib/string.c) within the test for comparison.

Quoted 7 lines

...

lib/tests/string_kunit.c static void string_test_strlen(struct kunit *test) · line 142

Quoted 2 lines
lib/tests/string_kunit.c +2 -0
+       for (offset = 0; offset < 16; offset++) {
+               for (len = 0; len <= 128; len++) {

You want to define these two limits to avoid the possible issues in the future if the code gets changed.

Got it, will define these limits as macros in V2. Thanks!

Signature · 2 lines
With Best Regards,
Feng Jiang
#6 lore raw

Re: [PATCH 2/5] lib/string_kunit: add test case for strnlen

Quoted 2 lines · Feng Jiang
Quoted 1 line · Andy Shevchenko

On Wed, Jan 7, 2026 at 4:35 AM Feng Jiang <[email protected]> wrote:

...

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

Same comment as per patch 1 (it would probably require to call for arch_strnlen() or something like this).

Quoted 7 lines

Thanks, makes sense.

I'll add the performance benchmarking (random filling + timing) in V2.

Since string functions are typically exported directly by each architecture without an arch_ prefix, I'll introduce a generic_strnlen() (based on lib/string.c) within the test for comparison.

Probably you want to make the existing one to have that name and use it inside the test and in the fallback wrapper. We don't want to have duplicate code, it is bad from maintenance perspective.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#7 lore raw

Re: [PATCH 2/5] lib/string_kunit: add test case for strnlen

Quoted 16 lines · Andy Shevchenko
Quoted 2 lines · Feng Jiang
Quoted 1 line · Andy Shevchenko

On Wed, Jan 7, 2026 at 4:35 AM Feng Jiang <[email protected]> wrote:

...

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

Same comment as per patch 1 (it would probably require to call for arch_strnlen() or something like this).

Thanks, makes sense.

I'll add the performance benchmarking (random filling + timing) in V2.

Quoted 8 lines
Quoted 3 lines

Since string functions are typically exported directly by each architecture without an arch_ prefix, I'll introduce a generic_strnlen() (based on lib/string.c) within the test for comparison.

Probably you want to make the existing one to have that name and use it inside the test and in the fallback wrapper. We don't want to have duplicate code, it is bad from maintenance perspective.

Thanks for the suggestions!

To avoid code duplication, I'll rename the generic implementation in lib/string.c to __generic_strnlen() and keep the original strnlen() as a wrapper. Then I'll use the generic one in the KUnit test for comparison.

Does this approach look good to you?

Signature · 2 lines
With Best Regards,
Feng Jiang
#8 lore raw

Re: [PATCH 2/5] lib/string_kunit: add test case for strnlen

Quoted 4 lines · Feng Jiang
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Feng Jiang
Quoted 1 line · Andy Shevchenko

On Wed, Jan 7, 2026 at 4:35 AM Feng Jiang <[email protected]> wrote:

...

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Add a KUnit test for strnlen() to verify correctness across different string lengths and memory alignments.

Same comment as per patch 1 (it would probably require to call for arch_strnlen() or something like this).

Thanks, makes sense.

I'll add the performance benchmarking (random filling + timing) in V2.

Quoted 7 lines
Quoted 3 lines

Since string functions are typically exported directly by each architecture without an arch_ prefix, I'll introduce a generic_strnlen() (based on lib/string.c) within the test for comparison.

Probably you want to make the existing one to have that name and use it inside the test and in the fallback wrapper. We don't want to have duplicate code, it is bad from maintenance perspective.

Quoted 7 lines

Thanks for the suggestions!

To avoid code duplication, I'll rename the generic implementation in lib/string.c to __generic_strnlen() and keep the original strnlen() as a wrapper. Then I'll use the generic one in the KUnit test for comparison.

Does this approach look good to you?

To me, yes. To others, we will know later on when you send a new version.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#9 lore raw

PATCH 3/5 riscv: lib: add strnlen implementation

In reply to Feng Jiang

Add strnlen() implementation for RISC-V with both generic and Zbb-optimized versions, derived from strlen.S.

---
 arch/riscv/include/asm/string.h |   3 +
 arch/riscv/lib/Makefile         |   1 +
 arch/riscv/lib/strnlen.S        | 164 ++++++++++++++++++++++++++++++++
 arch/riscv/purgatory/Makefile   |   5 +-
 4 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strnlen.S
arch/riscv/include/asm/string.h +3 -0
index 5ba77f60bf0b..16634d67c217 100644
@@ -28,6 +28,9 @@ extern asmlinkage __kernel_size_t strlen(const char *);
 
 #define __HAVE_ARCH_STRNCMP
 extern asmlinkage int strncmp(const char *cs, const char *ct, size_t count);
+
+#define __HAVE_ARCH_STRNLEN
+extern asmlinkage __kernel_size_t strnlen(const char *, size_t);
 #endif
 
 /* For those files which don't want to check by kasan. */
arch/riscv/lib/Makefile +1 -0
index bbc031124974..0969d8136df0 100644
@@ -7,6 +7,7 @@ ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),)
 lib-y			+= strcmp.o
 lib-y			+= strlen.o
 lib-y			+= strncmp.o
+lib-y			+= strnlen.o
 endif
 lib-y			+= csum.o
 ifeq ($(CONFIG_MMU), y)
arch/riscv/lib/strnlen.S new file +164 -0

164 lines not on this page · raw message

arch/riscv/purgatory/Makefile +4 -1
index 530e497ca2f9..d7c0533108be 100644
@@ -2,7 +2,7 @@
 
 purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o
 ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),)
-purgatory-y += strcmp.o strlen.o strncmp.o
+purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o
 endif
 
 targets += $(purgatory-y)
@@ -32,6 +32,9 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
+$(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FORCE
+	$(call if_changed_rule,as_o_S)
+
 CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
 CFLAGS_string.o := -D__DISABLE_EXPORTS
 CFLAGS_ctype.o := -D__DISABLE_EXPORTS
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH 4/5 riscv: lib: add strchr implementation

In reply to Feng Jiang

Add a basic assembly implementation of strchr() for RISC-V. This provides a functional byte-by-byte version as a foundation for future optimizations.

---
 arch/riscv/include/asm/string.h |  3 +++
 arch/riscv/lib/Makefile         |  1 +
 arch/riscv/lib/strchr.S         | 35 +++++++++++++++++++++++++++++++++
 arch/riscv/purgatory/Makefile   |  5 ++++-
 4 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strchr.S
arch/riscv/include/asm/string.h +3 -0
index 16634d67c217..ca3ade82b124 100644
@@ -31,6 +31,9 @@ extern asmlinkage int strncmp(const char *cs, const char *ct, size_t count);
 
 #define __HAVE_ARCH_STRNLEN
 extern asmlinkage __kernel_size_t strnlen(const char *, size_t);
+
+#define __HAVE_ARCH_STRCHR
+extern asmlinkage char *strchr(const char *, int);
 #endif
 
 /* For those files which don't want to check by kasan. */
arch/riscv/lib/Makefile +1 -0
index 0969d8136df0..b7f804dce1c3 100644
@@ -8,6 +8,7 @@ lib-y			+= strcmp.o
 lib-y			+= strlen.o
 lib-y			+= strncmp.o
 lib-y			+= strnlen.o
+lib-y			+= strchr.o
 endif
 lib-y			+= csum.o
 ifeq ($(CONFIG_MMU), y)
arch/riscv/lib/strchr.S new file +35 -0
new file mode 100644
index 000000000000..48c3a9da53e3
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * Copyright (C) 2025 Feng Jiang <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+
+/* char *strchr(const char *s, int c) */
+SYM_FUNC_START(strchr)
+	/*
+	 * Parameters
+	 *   a0 - The string to be searched
+	 *   a1 - The character to search for
+	 *
+	 * Returns
+	 *   a0 - Address of first occurrence of 'c' or 0
+	 *
+	 * Clobbers
+	 *   t0
+	 */
+	andi	a1, a1, 0xff
+1:
+	lbu	t0, 0(a0)
+	beq	t0, a1, 2f
+	addi	a0, a0, 1
+	bnez	t0, 1b
+	li	a0, 0
+2:
+	ret
+SYM_FUNC_END(strchr)
+
+SYM_FUNC_ALIAS_WEAK(__pi_strchr, strchr)
+EXPORT_SYMBOL(strchr)
arch/riscv/purgatory/Makefile +4 -1
index d7c0533108be..e7b3d748c913 100644
@@ -2,7 +2,7 @@
 
 purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o
 ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),)
-purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o
+purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o strchr.o
 endif
 
 targets += $(purgatory-y)
@@ -35,6 +35,9 @@ $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 $(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FORCE
 	$(call if_changed_rule,as_o_S)
 
+$(obj)/strchr.o: $(srctree)/arch/riscv/lib/strchr.S FORCE
+	$(call if_changed_rule,as_o_S)
+
 CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
 CFLAGS_string.o := -D__DISABLE_EXPORTS
 CFLAGS_ctype.o := -D__DISABLE_EXPORTS
Signature · 1 line
2.25.1
author
#11 lore raw

PATCH 5/5 riscv: lib: add strrchr implementation

In reply to Feng Jiang

Add a basic assembly implementation of strrchr() for RISC-V. This provides a functional byte-by-byte version as a foundation for future optimizations.

---
 arch/riscv/include/asm/string.h |  3 +++
 arch/riscv/lib/Makefile         |  1 +
 arch/riscv/lib/strrchr.S        | 37 +++++++++++++++++++++++++++++++++
 arch/riscv/purgatory/Makefile   |  5 ++++-
 4 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/lib/strrchr.S
arch/riscv/include/asm/string.h +3 -0
index ca3ade82b124..764ffe8f6479 100644
@@ -34,6 +34,9 @@ extern asmlinkage __kernel_size_t strnlen(const char *, size_t);
 
 #define __HAVE_ARCH_STRCHR
 extern asmlinkage char *strchr(const char *, int);
+
+#define __HAVE_ARCH_STRRCHR
+extern asmlinkage char *strrchr(const char *, int);
 #endif
 
 /* For those files which don't want to check by kasan. */
arch/riscv/lib/Makefile +1 -0
index b7f804dce1c3..735d0b665536 100644
@@ -9,6 +9,7 @@ lib-y			+= strlen.o
 lib-y			+= strncmp.o
 lib-y			+= strnlen.o
 lib-y			+= strchr.o
+lib-y			+= strrchr.o
 endif
 lib-y			+= csum.o
 ifeq ($(CONFIG_MMU), y)
arch/riscv/lib/strrchr.S new file +37 -0
new file mode 100644
index 000000000000..ac58b20ca21d
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * Copyright (C) 2025 Feng Jiang <[email protected]>
+ */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+
+/* char *strrchr(const char *s, int c) */
+SYM_FUNC_START(strrchr)
+	/*
+	 * Parameters
+	 *	a0 - The string to be searched
+	 *	a1 - The character to seaerch for
+	 *
+	 * Returns
+	 *	a0 - Address of last occurrence of 'c' or 0
+	 *
+	 * Clobbers
+	 *	t0, t1
+	 */
+	andi	a1, a1, 0xff
+	mv	t1, a0
+	li	a0, 0
+1:
+	lbu	t0, 0(t1)
+	bne	t0, a1, 2f
+	mv	a0, t1
+2:
+	addi	t1, t1, 1
+	bnez	t0, 1b
+	ret
+SYM_FUNC_END(strrchr)
+
+SYM_FUNC_ALIAS_WEAK(__pi_strrchr, strrchr)
+EXPORT_SYMBOL(strrchr)
arch/riscv/purgatory/Makefile +4 -1
index e7b3d748c913..b0358a78f11a 100644
@@ -2,7 +2,7 @@
 
 purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o
 ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),)
-purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o strchr.o
+purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o strchr.o strrchr.o
 endif
 
 targets += $(purgatory-y)
@@ -38,6 +38,9 @@ $(obj)/strnlen.o: $(srctree)/arch/riscv/lib/strnlen.S FORCE
 $(obj)/strchr.o: $(srctree)/arch/riscv/lib/strchr.S FORCE
 	$(call if_changed_rule,as_o_S)
 
+$(obj)/strrchr.o: $(srctree)/arch/riscv/lib/strrchr.S FORCE
+	$(call if_changed_rule,as_o_S)
+
 CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
 CFLAGS_string.o := -D__DISABLE_EXPORTS
 CFLAGS_ctype.o := -D__DISABLE_EXPORTS
Signature · 1 line
2.25.1