[Deepin-Kernel-SIG] [linux 6.18-y] [Fromlist] Improve RISC-V strnlen() and strchr() performance#1622
Conversation
Add an optimized strnlen() implementation for RISC-V. This version includes a generic optimization and a Zbb-powered optimization using the 'orc.b' instruction, derived from the strlen() implementation. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 16 B | 179 | 309 | +72.6% 512 B | 347 | 1562 | +350.1% 4096 B | 356 | 1878 | +427.5% Suggested-by: Qingfang Deng <dqfext@gmail.com> Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn> Link: https://patch.msgid.link/20260130025018.172925-7-jiangfeng@kylinos.cn Signed-off-by: Paul Walmsley <pjw@kernel.org> Link: https://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git/commit/?h=for-next&id=5ba15d419fab848a3813eb56bbcad00e291fbc49 Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
Add an assembly implementation of strchr() for RISC-V. By eliminating stack frame management (prologue/epilogue) and optimizing the function entries, the assembly version provides significant relative gains for short strings where the fixed overhead of the C function is most prominent. As string length increases, performance converges with the generic C implementation. Benchmark results (QEMU TCG, rv64): Length | Original (MB/s) | Optimized (MB/s) | Improvement -------|-----------------|------------------|------------ 1 B | 21 | 22 | +4.8% 7 B | 113 | 121 | +7.1% 16 B | 195 | 202 | +3.6% 512 B | 376 | 389 | +3.5% 4096 B | 394 | 393 | -0.3% Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn> Tested-by: Joel Stanley <joel@jms.id.au> Link: https://patch.msgid.link/20260130025018.172925-8-jiangfeng@kylinos.cn Signed-off-by: Paul Walmsley <pjw@kernel.org> Link: https://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git/commit/?h=for-next&id=adf542133960d402f63c976b00e46be4d986d4c3 Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
Reviewer's GuideAdds RISC-V assembly implementations of strnlen() and strchr(), wires them into the generic RISC-V string infrastructure and purgatory, and provides an optimized Zbb-based strnlen() variant when the ISA and toolchain support it. Flow diagram for RISC-V strchr implementationflowchart TD
Start[Start strchr
a0 = s, a1 = c] --> MaskChar
MaskChar[Mask search char
a1 = a1 & 0xff] --> Loop
Loop[Load byte t0 = *a0] --> FoundCheck
FoundCheck{t0 == a1?} -->|yes| ReturnPtr
FoundCheck -->|no| NullCheck
NullCheck{t0 == 0?} -->|yes| ReturnNull
NullCheck -->|no| IncPtr
IncPtr[a0 = a0 + 1] --> Loop
ReturnPtr[Return a0] --> End
ReturnNull[Set a0 = 0
Return NULL] --> End
End[End strchr]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
__pi_strnlenalias is defined withSYM_FUNC_ALIASwhile__pi_strchrusesSYM_FUNC_ALIAS_WEAK; it would be good to align this with how other RISC-V string helpers are defined (e.g.,strlen,strcmp) so the__pi_*aliases all follow a consistent strength and override behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `__pi_strnlen` alias is defined with `SYM_FUNC_ALIAS` while `__pi_strchr` uses `SYM_FUNC_ALIAS_WEAK`; it would be good to align this with how other RISC-V string helpers are defined (e.g., `strlen`, `strcmp`) so the `__pi_*` aliases all follow a consistent strength and override behavior.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR adds RISC-V architecture-specific implementations of strnlen() (including a Zbb-accelerated variant) and strchr(), and wires them into both the normal RISC-V lib build and the kexec purgatory build so the generic C implementations can be bypassed when KASAN is not enabled.
Changes:
- Add
arch/riscv/lib/strnlen.Swith an optional Zbb fast-path selected via alternatives. - Add
arch/riscv/lib/strchr.Sand expose both functions as arch-provided string routines. - Update RISC-V lib and purgatory Makefiles plus
asm/string.hto build/use these routines when KASAN isn’t in use.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| arch/riscv/purgatory/Makefile | Link strnlen.o/strchr.o into purgatory and add build rules for the new assembly sources. |
| arch/riscv/lib/strnlen.S | New RISC-V strnlen() implementation with Zbb alternative path. |
| arch/riscv/lib/strchr.S | New RISC-V strchr() implementation and symbol alias/export plumbing. |
| arch/riscv/lib/Makefile | Build strnlen.o/strchr.o as part of arch/riscv/lib/ when KASAN is disabled. |
| arch/riscv/include/asm/string.h | Advertise __HAVE_ARCH_STRNLEN / __HAVE_ARCH_STRCHR and declare the new symbols. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /* SPDX-License-Identifier: GPL-2.0-only */ | ||
|
|
||
| /* | ||
| * Base on arch/riscv/lib/strlen.S |
There was a problem hiding this comment.
Comment typo: "Base on" should be "Based on".
| * Base on arch/riscv/lib/strlen.S | |
| * Based on arch/riscv/lib/strlen.S |
| ret | ||
| SYM_FUNC_END(strchr) | ||
|
|
||
| SYM_FUNC_ALIAS_WEAK(__pi_strchr, strchr) |
There was a problem hiding this comment.
__pi_strchr is declared as a weak alias to strchr, which is inconsistent with the other RISC-V string routines in this directory (they use SYM_FUNC_ALIAS(__pi_*, *)). Unless there is a specific need for weak binding here, prefer a non-weak alias for consistency and to avoid surprising symbol resolution behavior.
| SYM_FUNC_ALIAS_WEAK(__pi_strchr, strchr) | |
| SYM_FUNC_ALIAS(__pi_strchr, strchr) |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: opsiff The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Summary by Sourcery
Add optimized RISC-V implementations of strnlen() and strchr() and integrate them into the architecture-specific string library and purgatory build.
New Features:
Enhancements: