mirror of
https://github.com/torvalds/linux.git
synced 2026-11-07 06:17:02 +01:00
zram: fix idle age_sec underflow in idle_store()
After commit2e8ff2f51d("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. Link: https://lore.kernel.org/20260828083149.45760-1-jiahao.kernel@gmail.com Fixes:2e8ff2f51d("zram: use u32 for entry ac_time tracking") Signed-off-by: Hao Jia <jiahao1@lixiang.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Suggested-by: Sergey Senozhatsky <senozhatsky@chromium.org> Cc: Brian Geffon <bgeffon@google.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Minchan Kim <minchan@kernel.org> Cc: <stable@vger.kernel.org>
This commit is contained in:
parent
0e774fdb2e
commit
59042a88ef
|
|
@ -415,7 +415,7 @@ static ssize_t mem_used_max_store(struct device *dev,
|
|||
* Mark all pages which are older than or equal to cutoff as IDLE.
|
||||
* Callers should hold the zram init lock in read mode
|
||||
*/
|
||||
static void mark_idle(struct zram *zram, ktime_t cutoff)
|
||||
static void mark_idle(struct zram *zram, time64_t cutoff)
|
||||
{
|
||||
int is_idle = 1;
|
||||
unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
|
||||
|
|
@ -439,7 +439,7 @@ static void mark_idle(struct zram *zram, ktime_t cutoff)
|
|||
|
||||
#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
|
||||
is_idle = !cutoff ||
|
||||
ktime_after(cutoff, zram->table[index].attr.ac_time);
|
||||
cutoff > zram->table[index].attr.ac_time;
|
||||
#endif
|
||||
if (is_idle)
|
||||
set_slot_flag(zram, index, ZRAM_IDLE);
|
||||
|
|
@ -453,21 +453,26 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
|
|||
const char *buf, size_t len)
|
||||
{
|
||||
struct zram *zram = dev_to_zram(dev);
|
||||
ktime_t cutoff = 0;
|
||||
time64_t cutoff = 0;
|
||||
|
||||
if (!sysfs_streq(buf, "all")) {
|
||||
/*
|
||||
* If it did not parse as 'all' try to treat it as an integer
|
||||
* when we have memory tracking enabled.
|
||||
*/
|
||||
time64_t uptime;
|
||||
u32 age_sec;
|
||||
|
||||
if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) &&
|
||||
!kstrtouint(buf, 0, &age_sec))
|
||||
cutoff = ktime_sub((u32)ktime_get_boottime_seconds(),
|
||||
age_sec);
|
||||
else
|
||||
if (!IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) ||
|
||||
kstrtouint(buf, 0, &age_sec))
|
||||
return -EINVAL;
|
||||
|
||||
/* No slot can be older than the system uptime */
|
||||
uptime = ktime_get_boottime_seconds();
|
||||
if (age_sec >= uptime)
|
||||
return len;
|
||||
|
||||
cutoff = uptime - age_sec;
|
||||
}
|
||||
|
||||
guard(rwsem_read)(&zram->dev_lock);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user