Re: [PATCH] psi:fix divide by zero in psi_update_stats

From: Johannes Weiner
Date: Tue Nov 12 2019 - 10:41:49 EST


On Fri, Nov 08, 2019 at 03:33:24PM +0800, tim wrote:
> In psi_update_stats, it is possible that period has value like
> 0xXXXXXXXX00000000 where the lower 32 bit is 0, then it calls div_u64 which
> truncates u64 period to u32, results in zero divisor.
> Use div64_u64() instead of div_u64() if the divisor is u64 to avoid
> truncation to 32-bit on 64-bit platforms.
>
> Signed-off-by: xiejingfeng <xiejingfeng@xxxxxxxxxxxxxxxxx>

This is legit. When we stop the periodic averaging worker due to an
idle CPU, the period after restart can be much longer than the ~4 sec
in the lower 32 bits. See the missed_periods logic in update_averages.

What is surprising is that you can hit this repeatedly, as the odds
are 1 in 4,294,967,296. An extremely coarse clock source?

Acked-by: Johannes Weiner <hannes@xxxxxxxxxxx>

There are several more instances of div_u64 in psi.c. They all look
fine to me except for one in the psi poll() windowing code, where we
divide by the window size, which can be up to 10s. CCing Suren.

---