lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20251110052741.92031-1-senozhatsky@chromium.org>
Date: Mon, 10 Nov 2025 14:27:41 +0900
From: Sergey Senozhatsky <senozhatsky@...omium.org>
To: Andrew Morton <akpm@...ux-foundation.org>,
	Minchan Kim <minchan@...nel.org>,
	Shin Kawamura <kawasin@...gle.com>,
	Brian Geffon <bgeffon@...gle.com>
Cc: linux-kernel@...r.kernel.org,
	linux-block@...r.kernel.org,
	Sergey Senozhatsky <senozhatsky@...omium.org>
Subject: [RFC PATCH] zram: do not hardcode 4K page size for writeback

Writeback operates on physical pages and writes a whole
physical page to a backing device at a time.  We should
not use hard-coded 4K units, as on systems with PAGE_SIZE
larger than 4K this leads to incorrect writeback limit
handling and bd_stat accounting.

Reported-by: Shin Kawamura <kawasin@...gle.com>
Signed-off-by: Sergey Senozhatsky <senozhatsky@...omium.org>
---
 Documentation/admin-guide/blockdev/zram.rst | 21 +++++++++------------
 drivers/block/zram/zram_drv.c               | 13 ++++++-------
 2 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst
index 3e273c1bb749..afba174e3471 100644
--- a/Documentation/admin-guide/blockdev/zram.rst
+++ b/Documentation/admin-guide/blockdev/zram.rst
@@ -211,8 +211,8 @@ reset             	WO	trigger device reset
 mem_used_max      	WO	reset the `mem_used_max` counter (see later)
 mem_limit         	WO	specifies the maximum amount of memory ZRAM can
 				use to store the compressed data
-writeback_limit   	WO	specifies the maximum amount of write IO zram
-				can write out to backing device as 4KB unit
+writeback_limit   	WO	specifies the maximum number of physical pages
+				zram can write out to backing device
 writeback_limit_enable  RW	show and set writeback_limit feature
 comp_algorithm    	RW	show and change the compression algorithm
 algorithm_params	WO	setup compression algorithm parameters
@@ -286,12 +286,9 @@ The bd_stat file represents a device's backing device statistics. It consists of
 a single line of text and contains the following stats separated by whitespace:
 
  ============== =============================================================
- bd_count	size of data written in backing device.
-		Unit: 4K bytes
+ bd_count	the number of physical pages currently stored on backing device
  bd_reads	the number of reads from backing device
-		Unit: 4K bytes
  bd_writes	the number of writes to backing device
-		Unit: 4K bytes
  ============== =============================================================
 
 9) Deactivate
@@ -409,17 +406,17 @@ assigned via /sys/block/zramX/writeback_limit is meaningless.)
 If admin wants to limit writeback as per-day 400M, they could do it
 like below::
 
-	$ MB_SHIFT=20
-	$ 4K_SHIFT=12
-	$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
-		/sys/block/zram0/writeback_limit.
+	$ PAGE_SIZE=$(getconf PAGESIZE)
+	$ echo $((419430400/PAGE_SIZE)) > /sys/block/zram0/writeback_limit
 	$ echo 1 > /sys/block/zram0/writeback_limit_enable
 
+Note that writeback operates with physical pages, so please make sure that
+the limit value is in PAGE_SIZE units.
+
 If admins want to allow further write again once the budget is exhausted,
 they could do it like below::
 
-	$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
-		/sys/block/zram0/writeback_limit
+	$ echo $((419430400/PAGE_SIZE)) > /sys/block/zram0/writeback_limit
 
 If an admin wants to see the remaining writeback budget since last set::
 
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index a43074657531..51074fed342c 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -823,7 +823,7 @@ static int zram_writeback_slots(struct zram *zram, struct zram_pp_ctl *ctl)
 		atomic64_inc(&zram->stats.pages_stored);
 		spin_lock(&zram->wb_limit_lock);
 		if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
-			zram->bd_wb_limit -=  1UL << (PAGE_SHIFT - 12);
+			zram->bd_wb_limit -= 1;
 		spin_unlock(&zram->wb_limit_lock);
 next:
 		zram_slot_unlock(zram, index);
@@ -1529,9 +1529,8 @@ static ssize_t mm_stat_show(struct device *dev,
 }
 
 #ifdef CONFIG_ZRAM_WRITEBACK
-#define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
-static ssize_t bd_stat_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+static ssize_t bd_stat_show(struct device *dev, struct device_attribute *attr,
+			    char *buf)
 {
 	struct zram *zram = dev_to_zram(dev);
 	ssize_t ret;
@@ -1539,9 +1538,9 @@ static ssize_t bd_stat_show(struct device *dev,
 	down_read(&zram->init_lock);
 	ret = sysfs_emit(buf,
 			"%8llu %8llu %8llu\n",
-			FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
-			FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
-			FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
+			atomic64_read(&zram->stats.bd_count),
+			atomic64_read(&zram->stats.bd_reads),
+			atomic64_read(&zram->stats.bd_writes));
 	up_read(&zram->init_lock);
 
 	return ret;
-- 
2.51.2.1041.gc1ab5b90ca-goog


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ