Re: [PATCH v3 2/2] LoongArch: KVM: Prevent division by zero in periodic timer restore

From: Bibo Mao

Date: Thu Jul 16 2026 - 02:14:46 EST




On 2026/7/15 下午6:06, Tao Cui wrote:
From: Tao Cui <cuitao@xxxxxxxxxx>

A guest can write CSR_TCFG with the periodic bit set but a period value
of zero. When kvm_restore_timer() later enters the periodic branch,
period = cfg & CSR_TCFG_VAL evaluates to 0, causing delta % period to
trigger a division by zero and crash the host kernel.

Clamp the period to 1 to avoid the panic.

Fixes: a5857b9ff6e0 ("LoongArch: KVM: Implement vcpu timer operations")
Signed-off-by: Tao Cui <cuitao@xxxxxxxxxx>
---
arch/loongarch/kvm/timer.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/arch/loongarch/kvm/timer.c b/arch/loongarch/kvm/timer.c
index 96f33e34855a..524d4096bac0 100644
--- a/arch/loongarch/kvm/timer.c
+++ b/arch/loongarch/kvm/timer.c
@@ -135,6 +135,8 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
delta = ktime_to_tick(vcpu, ktime_sub(expire, now));
else if (cfg & CSR_TCFG_PERIOD) {
period = cfg & CSR_TCFG_VAL;
+ if (!period)
+ period = 1;
delta = ktime_to_tick(vcpu, ktime_sub(now, expire));
delta = period - (delta % period);

Reviewed-by: Bibo Mao <maobibo@xxxxxxxxxxx>