[RFC PATCH 0/2] riscv: Use the vector registers for large memcpy()

From: Cong Wang

Date: Tue Dec 01 2026 - 23:03:51 EST


From: Cong Wang <cwang@xxxxxxxxxxxxxx>

Vectorized mem* routines were part of the kernel-mode vector series in
2023 and were dropped in v9 [1], because kernel_vector_begin() changes
state in the task struct and a memcpy() of that task struct (fork) then
copied the changed state. This series brings memcpy() back with a check
for that case: a copy that overlaps what a vector section writes to
stays scalar. memmove() and memset() are not touched.

Patch 1 gives the scalar memcpy() body its own symbol. Patch 2 adds the
entry sequence, the helper that decides, and the vector loop.

On a SpacemiT A100 core (1024-bit vectors) the vector copy is 1.3 times
faster at 8 KB and 3 times faster from 64 KB up. On a SpacemiT X100 core
(256-bit vectors) it gains much less: nothing below 16 KB, 1.3 times at
256 KB and above. Both tables are in patch 2.

Dependencies: the series applies on top of the first three patches of
"riscv: optimize mode switch latency for Vector" v7 [2]. The second
threshold in patch 2, for user vector state that is live in the
registers, relies on a system call leaving that state discarded, which
is what patch 3 of that series provides.

Testing: on a SpacemiT K3 board, with a backport of this series and of
its dependencies to 7.0, on both core types. The A100 cores are not
brought up by mainline and ran a kernel of their own, started with
out-of-tree patches. The kernel's riscv vector and sigreturn self tests
show no change. A test module, not part of this series, checks the data
of copies of 0 to 600 bytes and of larger sizes at all alignments,
that a copy at the threshold takes the vector path, and that a copy
stays scalar in each case where it has to.

[1] https://lore.kernel.org/linux-riscv/20231229143627.22898-1-andy.chiu@xxxxxxxxxx/
[2] https://lore.kernel.org/linux-riscv/20261111184359.1239821-1-tchiu@xxxxxxxxxxxxxxx/

---
Cong Wang (2):
riscv: lib: Give the scalar memcpy() body a symbol of its own
riscv: lib: Use the vector registers for large memcpy()

arch/riscv/Kconfig | 10 +++
arch/riscv/include/asm/asm-prototypes.h | 4 +
arch/riscv/lib/Makefile | 1 +
arch/riscv/lib/memcpy.S | 19 ++++-
arch/riscv/lib/memcpy_vector.S | 17 ++++
arch/riscv/lib/riscv_v_helpers.c | 102 ++++++++++++++++++++++--
arch/riscv/purgatory/Makefile | 2 +
7 files changed, 147 insertions(+), 8 deletions(-)
create mode 100644 arch/riscv/lib/memcpy_vector.S

--
2.43.0