Re: [PATCH] RISC-V: KVM: Run vCPU timer callbacks in hardirq context on RT
From: Anup Patel
Date: Sun Nov 29 2026 - 23:28:35 EST
On Wed, Nov 18, 2026 at 11:51 AM Koichiro Den <den@xxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxx
> 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@xxxxxxxxxxxxx>
LGTM.
Reviewed-by: Anup Patel <anup@xxxxxxxxxxxxxx>
Queued this patch for Linux-7.5
Thanks,
Anup
> ---
> 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
>