* [PATCH v4 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
[not found] <20260725001749.2579274-1-tchiu@tenstorrent.com>
@ 2026-07-25 0:17 ` Andy Chiu
2026-07-25 0:37 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Andy Chiu @ 2026-07-25 0:17 UTC (permalink / raw)
To: anup, Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-riscv, linux-rt-devel
Cc: kvm-riscv, Andy Chiu, dfustini, greentime.hu
Similar to commit 7137a203b251 ("arm64/fpsimd: Permit kernel mode NEON
with IRQs off"), we are upgrading get/put_cpu_vector_context such that
kvm_arch_vcpu_load/put can be safely called under both irq off and
regular process context.
Also, export both symbols so the kvm module can call into it.
Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
---
Changelog v4:
- Include the linux/kvm_types.h to prevent build fail (patchwork ci)
- drop preempt_v_started check in may_use_simd()
Changelog v3:
- Export {get,put}_cpu_vector_context() only to kvm modules (Sebastian)
Changelog v2:
- new patch since v2
---
arch/riscv/include/asm/simd.h | 16 +++-------------
arch/riscv/kernel/kernel_mode_vector.c | 19 +++++++++++++------
2 files changed, 16 insertions(+), 19 deletions(-)
diff --git a/arch/riscv/include/asm/simd.h b/arch/riscv/include/asm/simd.h
index adb50f3ec205..f176a8072c62 100644
--- a/arch/riscv/include/asm/simd.h
+++ b/arch/riscv/include/asm/simd.h
@@ -36,20 +36,10 @@ static __must_check inline bool may_use_simd(void)
/*
* Nesting is achieved in preempt_v by spreading the control for
* preemptible and non-preemptible kernel-mode Vector into two fields.
- * Always try to match with preempt_v if kernel V-context exists. Then,
- * fallback to check non preempt_v if nesting happens, or if the config
- * is not set.
+ * Only non-preempt_v can nest on top of preempt_v, if non-preempt_v is
+ * unavailable, then preempt_v is not allowed.
*/
- if (IS_ENABLED(CONFIG_RISCV_ISA_V_PREEMPTIVE) && current->thread.kernel_vstate.datap) {
- if (!riscv_preempt_v_started(current))
- return true;
- }
- /*
- * Non-preemptible kernel-mode Vector temporarily disables bh. So we
- * must not return true on irq_disabled(). Otherwise we would fail the
- * lockdep check calling local_bh_enable()
- */
- return !irqs_disabled() && !(riscv_v_flags() & RISCV_KERNEL_MODE_V);
+ return !(riscv_v_flags() & RISCV_KERNEL_MODE_V);
}
#else /* ! CONFIG_RISCV_ISA_V */
diff --git a/arch/riscv/kernel/kernel_mode_vector.c b/arch/riscv/kernel/kernel_mode_vector.c
index 307ac369c3d4..965c8edbe984 100644
--- a/arch/riscv/kernel/kernel_mode_vector.c
+++ b/arch/riscv/kernel/kernel_mode_vector.c
@@ -10,6 +10,7 @@
#include <linux/percpu.h>
#include <linux/preempt.h>
#include <linux/types.h>
+#include <linux/kvm_types.h>
#include <asm/vector.h>
#include <asm/switch_to.h>
@@ -55,13 +56,16 @@ void get_cpu_vector_context(void)
* disable softirqs so it is impossible for softirqs to nest
* get_cpu_vector_context() when kernel is actively using Vector.
*/
- if (!IS_ENABLED(CONFIG_PREEMPT_RT))
- local_bh_disable();
- else
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
+ if (!irqs_disabled())
+ local_bh_disable();
+ } else {
preempt_disable();
+ }
riscv_v_start(RISCV_KERNEL_MODE_V);
}
+EXPORT_SYMBOL_FOR_KVM(get_cpu_vector_context);
/*
* Release the CPU vector context.
@@ -74,11 +78,14 @@ void put_cpu_vector_context(void)
{
riscv_v_stop(RISCV_KERNEL_MODE_V);
- if (!IS_ENABLED(CONFIG_PREEMPT_RT))
- local_bh_enable();
- else
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
+ if (!irqs_disabled())
+ local_bh_enable();
+ } else {
preempt_enable();
+ }
}
+EXPORT_SYMBOL_FOR_KVM(put_cpu_vector_context);
#ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
static __always_inline u32 *riscv_v_flags_ptr(void)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v4 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
2026-07-25 0:17 ` [PATCH v4 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off Andy Chiu
@ 2026-07-25 0:37 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-07-25 0:37 UTC (permalink / raw)
To: Andy Chiu; +Cc: linux-kernel, linux-rt-devel
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the `!irqs_disabled()` check from `may_use_simd()` exposes KVM guest vector state to corruption and leakage by hardirqs.
--
--- Patch [2]: [PATCH v4 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off ---
commit db1e5a6ad1a80d124f168f9dae7f399f6eec4f67
Author: Andy Chiu <tchiu@tenstorrent.com>
riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
This commit upgrades get/put_cpu_vector_context to allow safe execution under
both IRQ off and regular process context, similar to arm64/fpsimd. It also
exports these symbols for the KVM module to call.
> diff --git a/arch/riscv/include/asm/simd.h b/arch/riscv/include/asm/simd.h
> index adb50f3ec2057..f176a8072c625 100644
> --- a/arch/riscv/include/asm/simd.h
> +++ b/arch/riscv/include/asm/simd.h
[ ... ]
> @@ -36,20 +36,10 @@ static __must_check inline bool may_use_simd(void)
> /*
> * Nesting is achieved in preempt_v by spreading the control for
> * preemptible and non-preemptible kernel-mode Vector into two fields.
> - * Always try to match with preempt_v if kernel V-context exists. Then,
> - * fallback to check non preempt_v if nesting happens, or if the config
> - * is not set.
> + * Only non-preempt_v can nest on top of preempt_v, if non-preempt_v is
> + * unavailable, then preempt_v is not allowed.
> */
> - if (IS_ENABLED(CONFIG_RISCV_ISA_V_PREEMPTIVE) && current->thread.kernel_vstate.datap) {
> - if (!riscv_preempt_v_started(current))
> - return true;
> - }
> - /*
> - * Non-preemptible kernel-mode Vector temporarily disables bh. So we
> - * must not return true on irq_disabled(). Otherwise we would fail the
> - * lockdep check calling local_bh_enable()
> - */
> - return !irqs_disabled() && !(riscv_v_flags() & RISCV_KERNEL_MODE_V);
> + return !(riscv_v_flags() & RISCV_KERNEL_MODE_V);
[Severity: High]
Does removing the !irqs_disabled() check here expose KVM guest vector state
to corruption and leakage by hardirqs?
If KVM loads guest vector state into CPU registers during kvm_arch_vcpu_load(),
and a hardirq using vector instructions fires while running KVM code,
may_use_simd() will now return true.
Would kernel_vector_begin() then save the CPU hardware registers (which contain
KVM guest data) into the host task's current->thread.vstate, followed by the
hardirq clobbering the CPU registers with its own vector operations?
If this happens, when the hardirq returns KVM would resume with corrupted
vector state, and the host task (e.g., QEMU) could receive the leaked guest
state.
While this series exports KVM symbols to potentially let KVM set
RISCV_KERNEL_MODE_V to protect the state, are the actual KVM patches to set
this flag missing from this series, leaving the tree vulnerable in the interim?
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260725001749.2579274-1-tchiu@tenstorrent.com?part=2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-25 0:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260725001749.2579274-1-tchiu@tenstorrent.com>
2026-07-25 0:17 ` [PATCH v4 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off Andy Chiu
2026-07-25 0:37 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox