mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Hao Jia <jiahao.kernel@gmail.com>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	 Brian Geffon <bgeffon@google.com>,
	minchan@kernel.org, axboe@kernel.dk, akpm@linux-foundation.org,
	 linux-kernel@vger.kernel.org, linux-block@vger.kernel.org
Subject: Re: [PATCH] zram: fix idle age_sec underflow in idle_store()
Date: Fri, 28 Aug 2026 18:22:09 +0900	[thread overview]
Message-ID: <apFS2a3lkskcZaag@google.com> (raw)
In-Reply-To: <55c09f16-4b0d-58a8-9413-f88279ed69fe@gmail.com>

On (26/08/28 16:38), Hao Jia wrote:
> On 2026/8/28 14:02, Sergey Senozhatsky wrote:
> > On (26/08/28 13:23), Sergey Senozhatsky wrote:
> > > On (26/08/25 17:23), Hao Jia wrote:
> > > > 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.
> > > 
> > > 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;
> > 
> > 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/20260828083149.45760-1-jiahao.kernel@gmail.com

Thanks, looks good.

      reply	other threads:[~2026-08-28  9:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25  9:23 Hao Jia
2026-08-28  3:26 ` Andrew Morton
2026-08-28  4:23 ` Sergey Senozhatsky
2026-08-28  4:30   ` Sergey Senozhatsky
2026-08-28  6:02   ` Sergey Senozhatsky
2026-08-28  8:38     ` Hao Jia
2026-08-28  9:22       ` Sergey Senozhatsky [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=apFS2a3lkskcZaag@google.com \
    --to=senozhatsky@chromium.org \
    --cc=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=bgeffon@google.com \
    --cc=jiahao.kernel@gmail.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minchan@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®