Re: [PATCH v2] zram: fix idle age_sec underflow in idle_store()

From: Andrew Morton

Date: Fri Aug 28 2026 - 13:25:48 EST


On Fri, 28 Aug 2026 16:31:49 +0800 Hao Jia <jiahao.kernel@xxxxxxxxx> wrote:

> After commit 2e8ff2f51dde ("zram: use u32 for entry ac_time tracking"),
> idle_store() computes the idle cutoff as:
>
> cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);
>
> Because the left operand is cast to u32, when age_sec exceeds the current
> uptime the subtraction wraps modulo 2^32 and the huge result is
> zero-extended into the s64 cutoff. mark_idle() then marks every entry as
> idle instead of matching nothing. For instance, running
>
> echo 86400 > /sys/block/zramX/idle
>
> on a machine up for only two minutes marks all newly written pages idle
> and hands them to idle writeback and recompression.
>
> No slot can have been accessed before the system booted, so an age_sec
> that reaches back past uptime cannot match any slot. Return early in that
> case, without walking the table or taking any slot locks.
>
> Track the cutoff as time64_t rather than ktime_t. Both cutoff and
> ac_time are boot-time values in seconds, so a plain arithmetic
> comparison against ac_time in mark_idle() is correct and no ktime
> helpers are needed.

Thanks. Sashiko asked a couple of questions about this change:
https://sashiko.dev/#/patchset/20260828083149.45760-1-jiahao.kernel@xxxxxxxxx