* [PATCH] RISC-V: KVM: Use raw_spinlock for VMID update critical section
@ 2026-07-16 6:40 yhchen312
2026-07-17 6:51 ` Sebastian Andrzej Siewior
0 siblings, 1 reply; 3+ messages in thread
From: yhchen312 @ 2026-07-16 6:40 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, bigeasy, clrkwllms, rostedt,
kvm, kvm-riscv, linux-riscv, linux-kernel, linux-rt-devel,
Yuhang.chen, Quan Zhou
From: "Yuhang.chen" <yhchen312@gmail.com>
The VMID update critical section performs an IPI broadcast via
on_each_cpu_mask() on VMID version rollover, which must not be preempted
under PREEMPT_RT. Convert vmid_lock to raw_spinlock_t and use plain
raw_spin_lock()/raw_spin_unlock() (interrupts stay enabled, since
on_each_cpu_mask() requires it).
Assisted-by: YuanSheng:deepseek-v4-pro
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Yuhang.chen <yhchen312@gmail.com>
---
arch/riscv/kvm/vmid.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c
index c15bdb1dd8be..21c2b276b55d 100644
--- a/arch/riscv/kvm/vmid.c
+++ b/arch/riscv/kvm/vmid.c
@@ -21,7 +21,7 @@
static unsigned long vmid_version = 1;
static unsigned long vmid_next;
static unsigned long vmid_bits __ro_after_init;
-static DEFINE_SPINLOCK(vmid_lock);
+static DEFINE_RAW_SPINLOCK(vmid_lock);
void __init kvm_riscv_gstage_vmid_detect(void)
{
@@ -78,14 +78,14 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
if (!kvm_riscv_gstage_vmid_ver_changed(vmid))
return;
- spin_lock(&vmid_lock);
+ raw_spin_lock(&vmid_lock);
/*
* We need to re-check the vmid_version here to ensure that if
* another vcpu already allocated a valid vmid for this vm.
*/
if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) {
- spin_unlock(&vmid_lock);
+ raw_spin_unlock(&vmid_lock);
return;
}
@@ -117,7 +117,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version));
- spin_unlock(&vmid_lock);
+ raw_spin_unlock(&vmid_lock);
/* Request G-stage page table update for all VCPUs */
kvm_for_each_vcpu(i, v, vcpu->kvm)
--
2.34.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] RISC-V: KVM: Use raw_spinlock for VMID update critical section
2026-07-16 6:40 [PATCH] RISC-V: KVM: Use raw_spinlock for VMID update critical section yhchen312
@ 2026-07-17 6:51 ` Sebastian Andrzej Siewior
2026-07-22 12:33 ` Yuhang.chen
0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-07-17 6:51 UTC (permalink / raw)
To: yhchen312
Cc: anup, atish.patra, pjw, palmer, aou, alex, clrkwllms, rostedt,
kvm, kvm-riscv, linux-riscv, linux-kernel, linux-rt-devel,
Quan Zhou
On 2026-07-16 14:40:11 [+0800], yhchen312@gmail.com wrote:
> From: "Yuhang.chen" <yhchen312@gmail.com>
>
> The VMID update critical section performs an IPI broadcast via
> on_each_cpu_mask() on VMID version rollover, which must not be preempted
> under PREEMPT_RT.
Here you state _why_ it must not be preempted. What would be the worst
that could happen.
> Convert vmid_lock to raw_spinlock_t and use plain
That part starting with "Convert" belongs into into a new line.
> raw_spin_lock()/raw_spin_unlock() (interrupts stay enabled, since
> on_each_cpu_mask() requires it).
>
> Assisted-by: YuanSheng:deepseek-v4-pro
> Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
> Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
> Signed-off-by: Yuhang.chen <yhchen312@gmail.com>
Sebastian
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] RISC-V: KVM: Use raw_spinlock for VMID update critical section
2026-07-17 6:51 ` Sebastian Andrzej Siewior
@ 2026-07-22 12:33 ` Yuhang.chen
0 siblings, 0 replies; 3+ messages in thread
From: Yuhang.chen @ 2026-07-22 12:33 UTC (permalink / raw)
To: bigeasy
Cc: anup, atish.patra, clrkwllms, rostedt, kvm, kvm-riscv,
linux-riscv, linux-kernel, linux-rt-devel, zhouquan,
Yuhang . chen
Hi Sebastian,
thanks for having a look.
On 2026-07-17 06:51:37 [+0000], Sebastian Andrzej Siewior wrote:
> Here you state _why_ it must not be preempted. What would be the worst
> that could happen.
The worst case is an unbounded hold of vmid_lock that stalls guest entry
system-wide. On rollover the section bumps vmid_version and then broadcasts
a guest-TLB flush via on_each_cpu_mask(..., wait=1) before assigning a
fresh VMID. Under PREEMPT_RT a plain spinlock_t is preemptible, so the
holder could be scheduled out while still waiting for that broadcast flush
to finish and keep vmid_lock held for an unbounded scheduling delay; every
other vCPU reaching the VMID-(re)assignment path on its way into the guest
would then block on the lock. The rollover is otherwise a short critical
section (a version bump plus one IPI), so keeping it non-preemptible with
raw_spinlock_t bounds its hold time.
I'll fold that into the commit log.
> That part starting with "Convert" belongs into into a new line.
Will fix in v2.
I'll send v2 with both changes.
Yuhang
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-22 12:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-16 6:40 [PATCH] RISC-V: KVM: Use raw_spinlock for VMID update critical section yhchen312
2026-07-17 6:51 ` Sebastian Andrzej Siewior
2026-07-22 12:33 ` Yuhang.chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox