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 Signed-off-by: Sergey Senozhatsky --- 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<>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<>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