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

Bibo Mao <[email protected]> Thu, 16 Jul 2026 14:14:26 +0800
Newsgroups dev.linux.lists.loongarch,org.kernel.vger.kvm,org.kernel.vger.linux-kernel
Message-ID <[email protected]>

On 2026/7/15 下午6:06, Tao Cui wrote:
> From: Tao Cui <[email protected]>
> 
> 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 <[email protected]>
> ---
>   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 <[email protected]>