zram: fix idle age_sec underflow in idle_store()
Subsystems (2)
PATCH 1/1 zram: fix idle age_sec underflow in idle_store()
From: Hao Jia <[email protected]>
After commit 2e8ff2f51dde ("zram: use u32 for entry ac_time tracking"), idle_store() computes the idle cutoff as follows:
cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);
Because the left operand is cast to a 32-bit unsigned integer, 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() subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of /sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an age_sec greater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch to KTIME_MIN instead, which no ac_time can be after.
--- drivers/block/zram/zram_drv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/zram/zram_drv.c +4 -4
@@ -438,7 +438,7 @@ static void mark_idle(struct zram *zram, ktime_t cutoff) }#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME - is_idle = !cutoff || + is_idle = cutoff ==KTIME_MIN ||ktime_after (cutoff, zram->table[index].attr.ac_time);#endif if (is_idle)
@@ -453,7 +453,7 @@ 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 ; +ktime_t cutoff =KTIME_MIN ;if (!sysfs_streq (buf,"all" )) {/*
@@ -464,7 +464,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,if (IS_ENABLED (CONFIG_ZRAM_TRACK_ENTRY_ACTIME ) && !kstrtouint (buf,0 , &age_sec)) - cutoff =ktime_sub ((u32 )ktime_get_boottime_seconds (), + cutoff =ktime_sub (ktime_get_boottime_seconds (), age_sec);else return -EINVAL ;
@@ -475,7 +475,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,return -EINVAL ;/* -* A cutoff of 0 marks everything as idle, this is the +* A cutoff of KTIME_MIN marks everything as idle, this is the * "all" behavior. */ mark_idle (zram, cutoff);
Signature · 1 line
2.34.1
Quoted 16 lines · On Tue, 25 Aug 2026 17:23:16 +0800 Hao Jia <[email protected]> wrote:
From: Hao Jia <[email protected]>
After commit
2e8ff2f51dde("zram: use u32 for entryac_timetracking"),idle_store()computes the idle cutoff as follows:cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);Because the left operand is cast to a 32-bit unsigned integer, when
age_secexceeds the current uptime, the subtraction wraps modulo 2^32 and the huge result is zero-extended into the s64 cutoff.mark_idle()subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of/sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.
Quoted 6 lines
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an
age_secgreater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch toKTIME_MINinstead, which noac_timecan be after.
whoops.
Quoted 1 line
Fixes 2e8ff2f51dde ("zram: use u32 for entry ac_time tracking")
Thanks, I'll add cc:[email protected] to this and shall queue it, pending additional review.
Sergey Senozhatsky
maintainerAdding Brian.
Quoted 14 lines · Hao Jia
After commit
2e8ff2f51dde("zram: use u32 for entryac_timetracking"),idle_store()computes the idle cutoff as follows:cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);Because the left operand is cast to a 32-bit unsigned integer, when
age_secexceeds the current uptime, the subtraction wraps modulo 2^32 and the huge result is zero-extended into the s64 cutoff.mark_idle()subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of/sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.
Quoted 6 lines
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an
age_secgreater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch toKTIME_MINinstead, which noac_timecan be after.
Can we instead just reject such cases? It should be an error to
request writeback of pages that are 2y old on a system that has uptime of 2d.
Maybe something like this:
+ time64_t uptime;
- 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 -ERANGE;
+
+ cutoff = uptime - age_sec;
}We are probably fine doing just basic arithmetics operations on those two. One is u32 the other one is s64: we should be fine just doing >= and sub. I guess we also can use plain comparison
cutoff > zram->table[index].attr.ac_time
in mark_idle(). Or am I hallucinating?
[keeping the rest of the original patch]
Quoted 43 lines
drivers/block/zram/zram_drv.c +4
-4@@ -438,7 +438,7 @@ static void mark_idle(struct zram *zram, ktime_t cutoff) }#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME - is_idle = !cutoff || + is_idle = cutoff ==KTIME_MIN ||ktime_after (cutoff, zram->table[index].attr.ac_time);#endif if (is_idle)@@ -453,7 +453,7 @@ 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 ; +ktime_t cutoff =KTIME_MIN ;if (!sysfs_streq (buf,"all" )) {/* @@ -464,7 +464,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,if (IS_ENABLED (CONFIG_ZRAM_TRACK_ENTRY_ACTIME ) && !kstrtouint (buf,0 , &age_sec)) - cutoff =ktime_sub ((u32 )ktime_get_boottime_seconds (), + cutoff =ktime_sub (ktime_get_boottime_seconds (), age_sec);else return -EINVAL ;@@ -475,7 +475,7 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,return -EINVAL ;/* -* A cutoff of 0 marks everything as idle, this is the +* A cutoff of KTIME_MIN marks everything as idle, this is the * "all" behavior. */ mark_idle (zram, cutoff);Signature · 1 line
2.34.1
Sergey Senozhatsky
maintainerQuoted 25 lines · Sergey Senozhatsky
Can we instead just reject such cases? It should be an error to
request writeback of pages that are 2y old on a system that has uptime of 2d.Maybe something like this:
(quoted diff) +10
-5+ time64_t uptime; - 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 -ERANGE; + + cutoff = uptime - age_sec; }
Quoted 5 lines
We are probably fine doing just basic arithmetics operations on those two. One is u32 the other one is s64: we should be fine just doing >= and sub. I guess we also can use plain comparison
cutoff > zram->table[index].attr.ac_timein
mark_idle(). Or am I hallucinating?
That implies that cutoff becomes time64_t both in idle_store() and in mark_idle().
Sergey Senozhatsky
maintainerQuoted 33 lines · Sergey Senozhatsky
Quoted 14 lines · Hao Jia
After commit
2e8ff2f51dde("zram: use u32 for entryac_timetracking"),idle_store()computes the idle cutoff as follows:cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);Because the left operand is cast to a 32-bit unsigned integer, when
age_secexceeds the current uptime, the subtraction wraps modulo 2^32 and the huge result is zero-extended into the s64 cutoff.mark_idle()subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of/sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.Quoted 6 lines
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an
age_secgreater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch toKTIME_MINinstead, which noac_timecan be after.Can we instead just reject such cases? It should be an error to
request writeback of pages that are 2y old on a system that has uptime of 2d.Maybe something like this:
(quoted diff) +1
-3+ time64_t uptime; - if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && - !kstrtouint(buf, 0, &age_sec)) - cutoff = ktime_sub((u32)ktime_get_boottime_seconds(),
Review
Quoted 10 lines
(quoted diff) +7
-2- 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 -ERANGE;
Or perhaps, instead "return len" and make it a fast path out, w/o the need to iterate any slots, take locks, etc.
Quoted 3 lines
(quoted diff) +2
-0+ + cutoff = uptime - age_sec; }
Quoted 45 lines · Sergey Senozhatsky
Quoted 33 lines · Sergey Senozhatsky
Quoted 14 lines · Hao Jia
After commit
2e8ff2f51dde("zram: use u32 for entryac_timetracking"),idle_store()computes the idle cutoff as follows:cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);Because the left operand is cast to a 32-bit unsigned integer, when
age_secexceeds the current uptime, the subtraction wraps modulo 2^32 and the huge result is zero-extended into the s64 cutoff.mark_idle()subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of/sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.Quoted 6 lines
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an
age_secgreater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch toKTIME_MINinstead, which noac_timecan be after.Can we instead just reject such cases? It should be an error to
request writeback of pages that are 2y old on a system that has uptime of 2d.Maybe something like this:
(quoted diff) +1
-3+ time64_t uptime; - if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && - !kstrtouint(buf, 0, &age_sec)) - cutoff = ktime_sub((u32)ktime_get_boottime_seconds(),Review
Quoted 10 lines
(quoted diff) +7
-2- 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 -ERANGE;
Quoted 2 lines
Or perhaps, instead "return len" and make it a fast path out, w/o the need to iterate any slots, take locks, etc.
Thanks for the feedback! I've sent out v2 of the patch. Please take a look when you get a chance.
https://lore.kernel.org/all/[email protected]
Thanks,
Hao
Quoted 4 lines
Quoted 4 lines
(quoted diff) +2
-0+ + cutoff = uptime - age_sec; }
Sergey Senozhatsky
maintainerQuoted 46 lines · Hao Jia
Quoted 45 lines · Sergey Senozhatsky
Quoted 43 lines · Sergey Senozhatsky
Quoted 14 lines · Hao Jia
After commit
2e8ff2f51dde("zram: use u32 for entryac_timetracking"),idle_store()computes the idle cutoff as follows:cutoff = ktime_sub((u32)ktime_get_boottime_seconds(), age_sec);Because the left operand is cast to a 32-bit unsigned integer, when
age_secexceeds the current uptime, the subtraction wraps modulo 2^32 and the huge result is zero-extended into the s64 cutoff.mark_idle()subsequently marks every entry as idle instead of matching nothing, breaking the intended semantics of/sys/block/zramX/idle. For instance, running echo 86400 > idle on a machine up for only two minutes causes all newly written pages to be marked as idle and handed over to idle writeback and recompression.Quoted 6 lines
Drop the explicit cast to perform the subtraction in signed arithmetic again, restoring the previous behavior: an
age_secgreater than uptime yields a negative cutoff, and one equal to uptime yields 0. Since a cutoff of 0 is now a valid computed value, it can no longer double as the "all" sentinel. Switch toKTIME_MINinstead, which noac_timecan be after.Can we instead just reject such cases? It should be an error to
request writeback of pages that are 2y old on a system that has uptime of 2d.Maybe something like this:
(quoted diff) +8
-5+ time64_t uptime; - 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 -ERANGE;
Quoted 7 lines
Quoted 2 lines
Or perhaps, instead "return len" and make it a fast path out, w/o the need to iterate any slots, take locks, etc.
Thanks for the feedback! I've sent out v2 of the patch. Please take a look when you get a chance.
Thanks, looks good.