mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] RISC-V: KVM: Run vCPU timer callbacks in hardirq context on RT
@ 2026-11-18  6:21 Koichiro Den
  2026-11-18  7:13 ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 2+ messages in thread
From: Koichiro Den @ 2026-11-18  6:21 UTC (permalink / raw)
  To: Anup Patel, Atish Patra
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt, kvm,
	kvm-riscv, linux-riscv, linux-kernel, linux-rt-devel

On hosts with Sstc, kvm_arch_vcpu_load() cancels the vCPU wakeup hrtimer
with preemption disabled, including when called from sched-in preemption
notifier. The vCPU reset path also cancels the timer under get_cpu(),
with or without Sstc.

On PREEMPT_RT, these callbacks run in softirq thread context, so
hrtimer_cancel() may sleep waiting for a running callback, which is
invalid with preemption disabled.

Let the callbacks run in hardirq context on -RT. They only rearm the
timer, update interrupt state under a raw spinlock, and kick the vCPU.
Similar to commit 9090825fa997 ("KVM: arm/arm64: Let the timer expire in
hardirq context on RT").

Cc: stable@vger.kernel.org
Fixes: 3e1d86569c21 ("RISC-V: KVM: Add SBI HSM extension in KVM")
Fixes: 8f5cb44b1bae ("RISC-V: KVM: Support sstc extension")
Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
Tested on SpacemiT K3 (which has Sstc) with an -RT host kernel.
- Without this patch, with the vCPU thread running at say SCHED_FIFO/50,
  just booting a Linux guest triggered "scheduling while atomic" splats.
- With this patch, the issue disappeared.
---
 arch/riscv/kvm/vcpu_timer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c
index adbd1617b78a..79500aa61130 100644
--- a/arch/riscv/kvm/vcpu_timer.c
+++ b/arch/riscv/kvm/vcpu_timer.c
@@ -98,7 +98,7 @@ static int kvm_riscv_vcpu_update_hrtimer(struct kvm_vcpu *vcpu, u64 ncycles)
 
 	delta_ns = kvm_riscv_delta_cycles2ns(ncycles, gt, t);
 	t->next_cycles = ncycles;
-	hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL);
+	hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL_HARD);
 	t->next_set = true;
 
 	return 0;
@@ -148,7 +148,7 @@ static void kvm_riscv_vcpu_timer_blocking(struct kvm_vcpu *vcpu)
 		return;
 
 	delta_ns = kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t);
-	hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL);
+	hrtimer_start(&t->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL_HARD);
 	t->next_set = true;
 }
 
@@ -256,12 +256,12 @@ int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu)
 	if (!kvm_riscv_isa_check_host(SSTC)) {
 		t->sstc_enabled = true;
 		hrtimer_setup(&t->hrt, kvm_riscv_vcpu_vstimer_expired, CLOCK_MONOTONIC,
-			      HRTIMER_MODE_REL);
+			      HRTIMER_MODE_REL_HARD);
 		t->timer_next_event = kvm_riscv_vcpu_update_vstimecmp;
 	} else {
 		t->sstc_enabled = false;
 		hrtimer_setup(&t->hrt, kvm_riscv_vcpu_hrtimer_expired, CLOCK_MONOTONIC,
-			      HRTIMER_MODE_REL);
+			      HRTIMER_MODE_REL_HARD);
 		t->timer_next_event = kvm_riscv_vcpu_update_hrtimer;
 	}
 

base-commit: e9bf49bbc4227048685c78f97e1446845760df4f
-- 
2.54.0


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

* Re: [PATCH] RISC-V: KVM: Run vCPU timer callbacks in hardirq context on RT
  2026-11-18  6:21 [PATCH] RISC-V: KVM: Run vCPU timer callbacks in hardirq context on RT Koichiro Den
@ 2026-11-18  7:13 ` Sebastian Andrzej Siewior
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-18  7:13 UTC (permalink / raw)
  To: Koichiro Den
  Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Alexandre Ghiti, Clark Williams, Steven Rostedt, kvm,
	kvm-riscv, linux-riscv, linux-kernel, linux-rt-devel

On 2026-11-18 15:21:04 [+0900], Koichiro Den wrote:
> On hosts with Sstc, kvm_arch_vcpu_load() cancels the vCPU wakeup hrtimer
> with preemption disabled, including when called from sched-in preemption
> notifier. The vCPU reset path also cancels the timer under get_cpu(),
> with or without Sstc.
> 
> On PREEMPT_RT, these callbacks run in softirq thread context, so
> hrtimer_cancel() may sleep waiting for a running callback, which is
> invalid with preemption disabled.
> 
> Let the callbacks run in hardirq context on -RT. They only rearm the
> timer, update interrupt state under a raw spinlock, and kick the vCPU.
> Similar to commit 9090825fa997 ("KVM: arm/arm64: Let the timer expire in
> hardirq context on RT").
> 
> Cc: stable@vger.kernel.org
> Fixes: 3e1d86569c21 ("RISC-V: KVM: Add SBI HSM extension in KVM")
> Fixes: 8f5cb44b1bae ("RISC-V: KVM: Support sstc extension")
> Signed-off-by: Koichiro Den <den@valinux.co.jp>

Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

Sebastian

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

end of thread, other threads:[~2026-11-18  7:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  6:21 [PATCH] RISC-V: KVM: Run vCPU timer callbacks in hardirq context on RT Koichiro Den
2026-11-18  7:13 ` Sebastian Andrzej Siewior

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®