All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/3] RISC-V: KVM: fix vcpu vector context handling
@ 2026-08-03 21:52 ` Andy Chiu
  0 siblings, 0 replies; 15+ messages in thread
From: Andy Chiu @ 2026-08-03 21:52 UTC (permalink / raw)
  To: anup, Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	linux-riscv
  Cc: kvm-riscv, Andy Chiu, dfustini, greentime.hu, andybnac,
	linux-kernel, olof, Yong-Xuan Wang

This series fixes a vtype corruption encountered when running perf +
vector workload on KVM.

The root cause of the bug is that the kernel-mode vector (KMV)
misattributes the guest's vcpu context as the user's context. To solve
this, we need to correctly save the vcpu context when the kernel-mode
vector is serving a guest.

However, calling directly into KVM from RISC-V generic architecture code
creates a reverse dependency, which is problematic when KVM is built as
a module. To address this, we introduce an RCU-protected callback for
context flushing, which KVM registers during module init.

Patch 1 is a preparatory cleanup that refactors
riscv_v_start_kernel_context().
Patch 2 prepares get/put_cpu_vector_context() for gaurding the use of
vector in kvm_arch_vcpu_load/put()
Patch 3 implements the callback mechanism and fixes the context handling.

Reason for this respin:

put_cpu_vector_context may casue a volutary ctxswch. If we prematurely
set RISCV_PREEMPT_V, the context restore path will rasise NEED_RESTORE
for this kernel thread. As the result, any irq taken during the
subsequence preempt_v would trigger a restore from the empty/stale
context memory on the irq return path in riscv_v_context_nesting_end()

The fix is summarized as below:
--- a/arch/riscv/kernel/kernel_mode_vector.c
+++ b/arch/riscv/kernel/kernel_mode_vector.c
@@ -181,8 +181,8 @@ static int riscv_v_start_kernel_context(void)
        /* Transfer the ownership of V from user to kernel, then save */
        get_cpu_vector_context();
        __riscv_flush_vector_context();
-       riscv_v_start(RISCV_PREEMPT_V);
        put_cpu_vector_context();
+       riscv_v_start(RISCV_PREEMPT_V);
        return 0;
 }

We will send a optimization patch that removes the costly riscv_v_is_on()
for voluntary switch detection. A kselftest that stresses the vectorized
user copy will also be included into that series.

Patch summary:
 - unchanged patch: 1, 2
 - new patch: none
 - modified patch: 3

Changelog v5:
 - add the r-b from Yong-Xuan
 - fix a guest boot fail that hits ~1/100 in host kernel-mode vector
 - Link to v4: https://lore.kernel.org/all/20260725001749.2579274-1-tchiu@tenstorrent.com/

Changelog v4:
 - Include a header to solve a mid-series build fail (patchwork ci)
 - Drop preempt_v_started test in may_use_simd (Sashiko)
 - Link to v3: https://lore.kernel.org/all/20260724165001.2317788-1-tchiu@tenstorrent.com/

Changelog v3:
 - Limit the export scope for {get,put}_cpu_vector_context()
 - clears RISCV_V_VCPU_NEED_RESTORE flag in host restore to prevent
   leaking
 - consolidates guest vector restore at returning to guest to prevent
   unnecessary save/restore between the preemptible window from
   vcpu_load to vcpu_enter_exit
 - Document flags added to riscv_v_flags
 - Link to v2: https://lore.kernel.org/all/20260715051629.1169645-1-tchiu@tenstorrent.com/

Changelog v2:
 - Address issues pointed out by sashiko (2, 3)
 - Link to v1: https://lore.kernel.org/all/20260711015835.767259-1-tchiu@tenstorrent.com/


Andy Chiu (3):
  riscv: vector: refactor riscv_v_start_kernel_context
  riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
  RISC-V: KVM: fix vcpu vector context handling for kernel-mode vector

 arch/riscv/include/asm/kvm_vcpu_vector.h | 24 +++++++
 arch/riscv/include/asm/processor.h       |  8 +++
 arch/riscv/include/asm/simd.h            | 16 +----
 arch/riscv/include/asm/vector.h          |  5 ++
 arch/riscv/kernel/kernel_mode_vector.c   | 88 +++++++++++++++++-------
 arch/riscv/kvm/main.c                    |  4 ++
 arch/riscv/kvm/vcpu.c                    | 12 ++++
 arch/riscv/kvm/vcpu_vector.c             | 22 +++++-
 8 files changed, 139 insertions(+), 40 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-08-13  8:40 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-03 21:52 [PATCH v5 0/3] RISC-V: KVM: fix vcpu vector context handling Andy Chiu
2026-08-03 21:52 ` Andy Chiu
2026-08-03 21:52 ` Andy Chiu
2026-08-03 21:52 ` [PATCH v5 1/3] riscv: vector: refactor riscv_v_start_kernel_context Andy Chiu
2026-08-03 21:52   ` Andy Chiu
2026-08-03 21:52 ` [PATCH v5 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off Andy Chiu
2026-08-03 21:52   ` Andy Chiu
2026-08-03 21:52   ` Andy Chiu
2026-08-03 21:52 ` [PATCH v5 3/3] RISC-V: KVM: fix vcpu vector context handling for kernel-mode vector Andy Chiu
2026-08-03 21:52   ` Andy Chiu
2026-08-03 21:52   ` Andy Chiu
2026-08-03 22:37   ` sashiko-bot
2026-08-13  8:40 ` [PATCH v5 0/3] RISC-V: KVM: fix vcpu vector context handling Anup Patel
2026-08-13  8:40   ` Anup Patel
2026-08-13  8:40   ` Anup Patel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.