From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC8823EC831 for ; Tue, 25 Aug 2026 09:23:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649809; cv=none; b=NClev/T+arODIJ9tPNLh59rTa/FKywtl0kq0orNQjq8N8tzgIeyUKGSgvNzyux+TmX3RMZpbYN4rI49t/36JLu43k0cGLNDuVGmrKWB6n/jOX2Epe8DE+tcuDj7Kao+B/loaqwmPO6TEYwYk3U0Qin+YByB6sOmuAVzUI4yFLBs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649809; c=relaxed/simple; bh=HiKrd3eV2rwkL3o/DZk2JSzup1z/32LkdO/qyn2di6E=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=RtseRNSHOyD/1hermX1D6OG3v8a9TYKYD4Sjmo1ZJmsJ926QX6r4FPPwhE3j9q1GI6nh4Vw0yCX3sWeH2HmZYdIoPDiKeOcGFlxrRorXEjeNUbLkCeRfVlnuZQWbCaPwADrqFSu06uMGt3OU2erfc1CMnV8aBJPoLz7s3Df2BTU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dtq2Ukwr; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dtq2Ukwr" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-39647184c73so581341a91.1 for ; Tue, 25 Aug 2026 02:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787649806; x=1788254606; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=bp7aIFrM5Ow6CazQpbG7KkEXOIatOW1QaOjDzgI64Kg=; b=dtq2Ukwr9za2V5PoIQ8FjRxoW2duQYps8omNrXS+5qK8wDiq/LbvLnSR2cI23eAgCB BqBSwED1hK7wIBXNK+rTETLPVi+Vqndmi85b0wDpze+EvEFF9DKMxpR3tFEcfsO3SSH8 wjromQxHIJ00Ot1b9219wM+tld7X9+nwiHZ/mP/CfW8y6IWsfRw8ztVeLaLD0EmB5yJa MfMfKPgGj5t4o/Txu4OXeX5KIG28nHwMdzul6NEj8AI4mVkYvVieOuvnljdYOq/gRo52 nK984v9I6/o/5O2IP+qluwzhvWye3orU4y7QLqsxzfk+CepBDkBVwKFoGYQLqTy2K5BS onKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787649806; x=1788254606; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bp7aIFrM5Ow6CazQpbG7KkEXOIatOW1QaOjDzgI64Kg=; b=WUA4glokn5l+Hu5U6OHmPRKbkDSm0HhT0Fo9WMBK5hlDQMuBmQ4qBNr9L/JmvrFPzh mhbDQow5lGK8bnN/RYEwT03/GRGrAfvnIRtowv5zx8Qi/zgG9r5jQHkw60XEPvILpqlh +Bl3CK8LfnYFjEJQtvQF9cvQBW+vI6xZ728QAnTtkqdVZ7ufrosxBolozQo/Qb97E2G0 ylp9zTljZrgGF171hl1iC3rqkdho8S96JQrW1a9Qt6ByOjr5lsuuAft5eN0LJ5121I66 CrPJA4OctPHtDVnVzKsg0Zkl/1VHYlzKD6iEZkqDKSB+tbIsQQ+XrRoCWXQStJdhx3zb h5cA== X-Gm-Message-State: AFuF++l8EvnQWSDYUfxOJzsQvRzWQ5qcVbkKVyfa8lmJqEykiXkAsg71 wXNNQCJBFrH5Pb/z7s1ph+bQzOWwtaWKMNoSq+oy2DpNKoW1+rCDG7aL X-Gm-Gg: AR+sD13lm/IPH2CizaS8ZpziAzD2HE1p6RLyjY9Z0kiF5GFqwmVe+bh8c5IQr6kkHII VcHEHGSJAiw900iOTVb2cf35E541nv5wgX+RpmUhCalANoJ8P4XO0WWp4XCzoL8WJdRnDVAIXgW N6qmRW7WRRaoVU4ujb20BwVUcAdF81C0hzTy1ANaHilahUe4fpbEejf9WFPf6RK20sYI1citAo1 Nwv8vf1m2naX37wXzDRgwlgFjeoq8nhsva4Lu21i8SK2k6YNCE5/rPZla6q0NCZ5oOFhyBbtMTk tn8KQZgG3sGMc8LyFmQUHRJRc9/vUNih/B67LYllSjdeP/kwCez2SseUTzOSSQhFERy/w1wj9fy 1vlTXzKN4n8y9d0A5Yl2PPFFHptInTq9m2mLsPYCo87oeUUOv0abgGy6CC6bWJ9YmRx7uu78m/I TeLJJuGysmihmJlL8T1ItRg3qMKHPklJj5HEtyf+SmRmXzKE7C7AP/vDAaqcI3hzi+u2qrG51n8 JMswRPSXYdPpBQ= X-Received: by 2002:a17:90b:4b42:b0:390:84db:888e with SMTP id 98e67ed59e1d1-39645b1c818mr5264179a91.7.1787649806023; Tue, 25 Aug 2026 02:23:26 -0700 (PDT) Received: from localhost.localdomain ([122.11.210.25]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39645c06e02sm3048627a91.17.2026.08.25.02.23.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:23:25 -0700 (PDT) From: Hao Jia To: minchan@kernel.org, senozhatsky@chromium.org, axboe@kernel.dk, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Hao Jia Subject: [PATCH] zram: fix idle age_sec underflow in idle_store() Date: Tue, 25 Aug 2026 17:23:16 +0800 Message-Id: <20260825092316.69658-1-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Hao Jia 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. Fixes: 2e8ff2f51dde ("zram: use u32 for entry ac_time tracking") Signed-off-by: Hao Jia --- drivers/block/zram/zram_drv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index a9b3bb1d3bef..da51f70b7121 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -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); -- 2.34.1