LoongArch: KVM: Prevent division by zero in periodic timer restore

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: a5857b9ff6 ("LoongArch: KVM: Implement vcpu timer operations")
Reviewed-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
This commit is contained in:
Tao Cui 2026-08-10 12:21:50 +08:00 committed by Huacai Chen
parent fe41c0759b
commit 01c823f14e

View File

@ -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);