[PATCH v5 0/7] riscv: optimize Vector context restore on syscall

From: Andy Chiu

Date: Mon Aug 10 2026 - 13:25:04 EST


This patch series optimizes riscv vector state handling across syscall
boundaries and context switches. The kernel now keeps track of the
INITIAL state in status.vs to optimize unnecessary context management
operations.

In addition to v4, there are 3 new patches. Patch 1 drops a csr read
from the vector context switch path and 2 adds a selftest covering the
vectorized user copy.

Patch 7 is the larger addition, it adds CONFIG_RISCV_VSTATE_OPT, which
keeps vector enabled on kernel entry and skip nulling the user vector
context on the syscall fast path. That cuts ~80 cycles on getpid and
improves 3.5% reqps on a nginx workload, but it gives up the ability to
catch illegal use of vector in the kernel space and stale-vstate bugs in
the user code.

Patch 7 can be sent independent of patch 1-6. If we have concerns about
the trade off, please feel free to take 1-6 on their own. I am happy to
collect more discussion and respin it separately.

This series is based on [1], which has not been applied yet. The
prerequisite-patch-id entries below correspond to its three patches.

Link to v4: https://patchwork.kernel.org/project/linux-riscv/cover/20260528190927.886558-1-tchiu@xxxxxxxxxxxxxxx/
Patch summary:
- New patches: 1, 2, 7
- Modified patches: 3
- Unchanged patches: 4, 5, 6

Changelog v5:
- Rebase on top of the kvm fix
- Do not read sstatus in vector context swicth (1)
- Add a test for vectorized user copy (2)
- Enable vector in the kernel-mode and skip nulling at syscall fast
path, gurad the optimization in a new config (7)

Changelog v4:
- Fix a build warning (1)
- Prevent setting INIT when it is already and provide performance
meassurements (2)
- Address comments from sashiko (4)

Changelog v3:
- Refactor function names. (1, 2)
- Merge daichengrong's patch, with a fix and optimzation. (2)
- Fix ptrace GETREGSET failure. (3)
- Strengthen ptrace SETREGSET semantics and add a test to cover it. (3,
4)
- Fix a potential ABI break in signal and add a test to prevent future
breaks. (3, 4)

Link to v2: https://lore.kernel.org/linux-riscv/20260402043414.2421916-1-andybnac@xxxxxxxxx/
Changelog v2: rebase on top of for-next

[1] [PATCH v5 0/3] RISC-V: KVM: fix vcpu vector context handling
https://lore.kernel.org/all/20260803215250.824417-1-tchiu@xxxxxxxxxxxxxxx/

Andy Chiu (6):
riscv: do not read csr in vector context switch
selftest: riscv: test vectorized user copy
riscv: vector: refactor vector context operations
riscv: vector: adjust ptrace and signal behavior for INITIAL state
selftests: riscv: Extend vector tests for sigreturn and ptrace
riscv: vector: optimize vstate operations

daichengrong (1):
riscv: clarify vector state semantics on syscall and context switch

arch/riscv/Kconfig | 11 +
arch/riscv/include/asm/kvm_vcpu_vector.h | 8 +-
arch/riscv/include/asm/vector.h | 63 +++--
arch/riscv/kernel/entry.S | 13 +-
arch/riscv/kernel/kernel_mode_vector.c | 12 +-
arch/riscv/kernel/process.c | 4 +
arch/riscv/kernel/ptrace.c | 13 +-
arch/riscv/kernel/signal.c | 11 +-
arch/riscv/kernel/vector.c | 42 +++-
arch/riscv/kvm/vcpu.c | 2 +-
arch/riscv/kvm/vcpu_vector.c | 6 +-
.../selftests/riscv/sigreturn/sigreturn.c | 71 ++++++
tools/testing/selftests/riscv/vector/Makefile | 6 +-
.../selftests/riscv/vector/v_uaccess_stress.c | 221 ++++++++++++++++++
.../selftests/riscv/vector/vstate_ptrace.c | 111 ++++++++-
15 files changed, 538 insertions(+), 56 deletions(-)
create mode 100644 tools/testing/selftests/riscv/vector/v_uaccess_stress.c


base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
prerequisite-patch-id: 1e2a563c66af3b5397eb66443b1668ce91e1c20e
prerequisite-patch-id: 74285ae638395cf8fbaeb0258c97c57994d3d3a8
prerequisite-patch-id: 544077ce90baabab876cd02335e066c0ccedd6aa
--
2.43.0