Re: [RFC PATCH 0/3] RISC-V: KVM: Preserve resident guest Vector state
From: Anup Patel
Date: Sun Nov 29 2026 - 00:28:17 EST
Hi Andy,
On Tue, Oct 27, 2026 at 6:52 PM slack up <yxcnew@xxxxxxxxxxx> wrote:
>
> From: Xiaochun Yu <yxcnew@xxxxxxxxxxx>
>
> kvm_arch_vcpu_put() saves the guest Vector state and restores the host
> register file. A later vCPU load on the same hart saves the host again and
> requests a full guest restore, even when no host user touched the Vector
> data registers in between.
>
> This RFC keeps the guest Vector register file resident across a vCPU
> put/load pair when it is safe to do so. A matching load restores only the
> control CSRs; all uncertain cases retain the existing full-restore path.
>
> The series is intentionally conservative:
>
> - every dirty vCPU put still saves the guest state, so guest memory remains
> authoritative for migration and userspace access;
> - a per-hart owner records an opaque vCPU identity, an allocation cookie and a
> state generation, without retaining a dereferenceable vCPU pointer;
> - task Vector save/restore/discard, kernel-mode Vector use, preemptible kernel
> Vector task resume and CPU state changes invalidate a resident guest before
> the registers can be reused;
> - reset, dirty saves and KVM_SET_ONE_REG advance the generation;
> - KVM_SET_ONE_REG advances the generation before copy_from_user(), because a
> partial copy can modify the destination and still return EFAULT;
> - the optimization is guarded by a read-only module parameter and defaults to
> disabled. The generic invalidation sites are guarded by a static key.
>
> Patch 1 establishes the generic invalidation boundary without changing KVM
> behavior. Patch 2 adds the owner tracking, CSR-only restore and tracepoints.
> Patch 3 adds focused lifetime, ONE_REG and partial-fault regression tests.
>
> This series currently depends on the separately posted build fix:
>
> Message-ID: <OSQPR01MB6326DDE9C2F390EA3F0F444EC2662@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
> Link: https://lore.kernel.org/all/OSQPR01MB6326DDE9C2F390EA3F0F444EC2662@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
>
> Ze Huang <ze.huang@xxxxxxxxxxxxxxxx> independently tested that prerequisite
> with GCC 16.2.1 and Clang 23.1.3 and supplied a Tested-by tag in this reply:
>
> Link: https://lore.kernel.org/all/DMCBQSJ0B22A.2TDXAXPJNPZL2@xxxxxxxxxxxxxxxx/
>
> Validation on Torvalds master 21a7f44fd076:
>
> - CONFIG_RISCV_ISA_V=y, CONFIG_KVM=m: full Image + modules build;
> - CONFIG_RISCV_ISA_V=n, CONFIG_KVM=m: full Image + modules build;
> - lifetime, ONE_REG and partial-fault tests pass with the fast path disabled
> and enabled;
> - nested KVM/RVV smoke passes 3/3 with the fast path enabled;
> - the smoke trace recorded 785 owner hits, 784 CSR-only restores and eight
> conservative full fallbacks, with no trace-buffer overwrite;
> - no emergency, alert, critical or error messages were present in dmesg.
>
> Development note: OpenAI Codex was used in an extended interactive session to
> assist with source analysis, implementation and selftest drafting, experiment
> orchestration, patch review and changelog preparation. The prompts focused on
> modeling per-hart ownership as a cache-validity problem, enumerating Vector
> register invalidation boundaries, reproducing partial KVM_SET_ONE_REG writes
> and reviewing the resulting patches. The build and runtime results above were
> collected from the stated test environment rather than inferred by the model.
>
> The nested QEMU result demonstrates functional behavior and event counts only;
> it is not presented as a performance result. Hardware performance measurement
> remains future work.
>
> Feedback is especially welcome on whether the generic invalidation callback is
> an acceptable RISC-V Vector/KVM boundary, whether the default-off module
> parameter is useful for the first version, and whether the behavior patch
> should be split further after the interface direction is agreed.
>
> Xiaochun Yu (3):
> riscv: vector: Add a guest owner invalidation hook
> RISC-V: KVM: Preserve guest Vector state across vCPU put/load
> KVM: riscv: selftests: Add Vector owner regression tests
>
> arch/riscv/include/asm/kvm_host.h | 4 +
> arch/riscv/include/asm/kvm_vcpu_vector.h | 54 +++-
> arch/riscv/include/asm/processor.h | 1 +
> arch/riscv/include/asm/vector.h | 31 +++
> arch/riscv/kernel/kernel_mode_vector.c | 38 +++
> arch/riscv/kvm/trace.h | 98 +++++++
> arch/riscv/kvm/vcpu.c | 54 +++-
> arch/riscv/kvm/vcpu_vector.c | 252 +++++++++++++++++-
> tools/testing/selftests/kvm/Makefile.kvm | 3 +
> .../kvm/riscv/vector_owner_fault_test.c | 147 ++++++++++
> .../kvm/riscv/vector_owner_lifetime_test.c | 127 +++++++++
> .../kvm/riscv/vector_owner_onereg_test.c | 191 +++++++++++++
> 12 files changed, 976 insertions(+), 24 deletions(-)
> create mode 100644 tools/testing/selftests/kvm/riscv/vector_owner_fault_test.c
> create mode 100644 tools/testing/selftests/kvm/riscv/vector_owner_lifetime_test.c
> create mode 100644 tools/testing/selftests/kvm/riscv/vector_owner_onereg_test.c
>
>
> base-commit: 21a7f44fd0765fcc5194130b28767786eba1d1e7
> prerequisite-patch-id: 370f0468ecd4354950ab075162058e4b7c31768b
> --
> 2.43.0
We need more reviews on this one. Can you please have a look at this series ?
Regards,
Anup