* [PATCH v2 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
[not found] <20260715051629.1169645-1-tchiu@tenstorrent.com>
@ 2026-07-15 5:16 ` Andy Chiu
2026-07-15 5:32 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Andy Chiu @ 2026-07-15 5:16 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 kvm module can call into it.
Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
---
Changelog v2:
- new patch since v2
---
arch/riscv/include/asm/simd.h | 8 ++------
arch/riscv/kernel/kernel_mode_vector.c | 18 ++++++++++++------
2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/arch/riscv/include/asm/simd.h b/arch/riscv/include/asm/simd.h
index adb50f3ec205..678c8b97cd49 100644
--- a/arch/riscv/include/asm/simd.h
+++ b/arch/riscv/include/asm/simd.h
@@ -44,12 +44,8 @@ static __must_check inline bool may_use_simd(void)
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..6627e1a82e19 100644
--- a/arch/riscv/kernel/kernel_mode_vector.c
+++ b/arch/riscv/kernel/kernel_mode_vector.c
@@ -55,13 +55,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_GPL(get_cpu_vector_context);
/*
* Release the CPU vector context.
@@ -74,11 +77,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_GPL(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 v2 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
2026-07-15 5:16 ` [PATCH v2 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off Andy Chiu
@ 2026-07-15 5:32 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-07-15 5:32 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] Exported `get_cpu_vector_context` and `put_cpu_vector_context` dynamically evaluate `irqs_disabled()` to balance `local_bh_disable()` and `local_bh_enable()`. If a caller alters the IRQ state between the two calls, it will lead to preempt count corruption.
--
--- Patch [2]: [PATCH v2 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off ---
commit cfe6cf4c78a9ea0fadcde3b4c39aafb48ebeaec6
Author: Andy Chiu <tchiu@tenstorrent.com>
riscv: vector: allow non-preemptible kernel-mode vector with IRQs off
This commit updates get_cpu_vector_context and put_cpu_vector_context to
conditionally disable and enable bottom halves based on whether interrupts
are disabled. It also exports these symbols so they can be used by the KVM
module, specifically for kvm_arch_vcpu_load and put.
> diff --git a/arch/riscv/kernel/kernel_mode_vector.c b/arch/riscv/kernel/kernel_mode_vector.c
> index 307ac369c3d4a..6627e1a82e19e 100644
> --- a/arch/riscv/kernel/kernel_mode_vector.c
> +++ b/arch/riscv/kernel/kernel_mode_vector.c
[ ... ]
> @@ -55,13 +55,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_GPL(get_cpu_vector_context);
>
> /*
> * Release the CPU vector context.
> @@ -74,11 +77,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 {
[Severity: High]
Does dynamically checking irqs_disabled() in both get_cpu_vector_context()
and put_cpu_vector_context() risk corrupting the preempt count if the IRQ
state changes between the two calls?
The commit message mentions these are intended for kvm_arch_vcpu_load() and
kvm_arch_vcpu_put(). If KVM calls vcpu_load() during sched_in with IRQs
enabled, get_cpu_vector_context() will call local_bh_disable().
If it later calls vcpu_put() during sched_out with IRQs disabled,
put_cpu_vector_context() will skip local_bh_enable().
Will this permanently leak the BH disable count on every context switch?
> preempt_enable();
> + }
> }
> +EXPORT_SYMBOL_GPL(put_cpu_vector_context);
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260715051629.1169645-1-tchiu@tenstorrent.com?part=2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-15 5:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20260715051629.1169645-1-tchiu@tenstorrent.com>
2026-07-15 5:16 ` [PATCH v2 2/3] riscv: vector: allow non-preemptible kernel-mode vector with IRQs off Andy Chiu
2026-07-15 5:32 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox