riscv: vector: vcsr (vxrm/vxsat) across syscalls

Andrzej Mialkowski andrzej.mialkowski@aheadcomputing.com
Tue Dec 1 15:57:18 GMT 2026


Hi Andy,

I work on vector code optimizations. Comparing my code with your v7
series "riscv: optimize mode switch latency for Vector", I found that
patch 3/6 [1] leaks vcsr state (vxrm/vxsat) across the process
boundary.

In v7, a syscall moves the vector state to INITIAL. This state is
neither saved at a context switch nor restored when the task runs
again; the return to user mode resets most of the vector state but
leaves vcsr from the last task.
This is new with v7: mainline marks the state DIRTY, so vcsr is saved
and restored.

The commit message of 3/6 says:

> The RISC-V vector specification states that executing a system call
> causes all caller-saved vector registers (v0-v31, vl, vtype) and vstart
> to become unspecified.

This sentence, and the mainline behavior, follow the non-authoritative
calling-convention appendix of the V specification (last changed in
2021), which gives vxrm and vxsat thread storage duration. The psABI
dropped that model in 2022 [2][3]: vxrm and vxsat "are not preserved
across calls and their values are unspecified upon entry". The
appendix also notes that "Most OSes will choose to either leave these
registers intact or reset them to their initial state to avoid leaking
information across process boundaries". For vcsr, v7 does neither,
which causes the leak.

The question is what should happen with vcsr across a syscall.
Mainline preserves it, which works, but it goes against the general
rule that applications should not rely on vector state after a
syscall. As far as I can see, the behavior of vcsr is not documented
on the kernel side, and a later change could lead to nasty bugs such
as an unexpected rounding mode for parts of a calculation.

User space already treats vxrm/vxsat as not preserved: compilers follow
the psABI, and glibc and the pending musl patch list them as clobbered
by syscalls. I scanned sources and the binaries of two distributions
(Bianbu 4.0.8 and a minimal Ubuntu 26.04.1). I don't see code that
depends on vcsr being preserved. Code either writes vxrm before use,
or assumes it is 0, the value after exec (the V8 wasm JIT, Cranelift,
some x265 routines). The latter is technically outside the psABI, but
it is why the reset value should be 0.

I would suggest resetting vcsr to 0 together with the rest of the
V state, and updating the description in vector.rst. It changes the
mainline behavior, but makes the kernel consistent with the psABI,
aligned with user space, and removes the leak. This can be a separate
patch against mainline: it requires a single CSR write in
__riscv_v_vstate_discard(), so the syscall overhead is negligible.
v7 then gets it on rebase, as it uses the same function.
The alternative is to keep preserving vcsr, which does not feel right
to me. Please let me know, and I can send the patch.

Separately, the kernel test robot reported a build error on 3/6
without CONFIG_RISCV_ISA_V (missing __riscv_v_vstate_clean() stub in
asm/vector.h); it is still there in v7.

Thanks,
Andrzej

[1] https://lore.kernel.org/all/20261111184359.1239821-4-tchiu@tenstorrent.com/
[2] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/287
[3] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/294

P.S. AI disclosure: I used an AI assistant for the source survey,
testing and the analysis, and verified the findings myself.  




More information about the Gdb mailing list