Add a per-device sysfs attribute, compact_threshold, to configure zsmalloc's deferred compaction threshold percentage at runtime. Signed-off-by: Sergey Senozhatsky --- Documentation/ABI/testing/sysfs-block-zram | 9 +++++ Documentation/admin-guide/blockdev/zram.rst | 2 + drivers/block/zram/zram_drv.c | 45 +++++++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 4 files changed, 57 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-block-zram b/Documentation/ABI/testing/sysfs-block-zram index 1ea9018a627f..84a924f4e8d2 100644 --- a/Documentation/ABI/testing/sysfs-block-zram +++ b/Documentation/ABI/testing/sysfs-block-zram @@ -57,6 +57,15 @@ Description: allocator zrm uses. The allocator moves some objects so that it could free fragment space. +What: /sys/block/zram/compact_threshold +Date: December 2026 +Contact: Sergey Senozhatsky +Description: + The compact_threshold file is read-write and specifies the + zsmalloc pool fragmentation percentage threshold for deferred + background compaction. Default is ZS_DEFAULT_COMPACT_THRESHOLD; + "0" disables deferred compaction. Can be changed at runtime. + What: /sys/block/zram/io_stat Date: August 2015 Contact: Sergey Senozhatsky diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst index db256c53b127..0c18b7fd144a 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -254,6 +254,8 @@ compressed_writeback RW show and set compressed writeback feature comp_algorithm RW show and change the compression algorithm algorithm_params WO setup compression algorithm parameters compact WO trigger memory compaction +compact_threshold RW show and set fragmentation threshold (%) for + deferred background compaction debug_stat RO this file is used for zram debugging purposes backing_dev RW set up backend storage for zram to write out idle WO mark allocated slot as idle diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 36873b655d66..058aa089a18d 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1962,6 +1962,41 @@ static ssize_t compact_store(struct device *dev, struct device_attribute *attr, return len; } +static ssize_t compact_threshold_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct zram *zram = dev_to_zram(dev); + u32 val; + + guard(rwsem_read)(&zram->dev_lock); + val = zram->compact_threshold; + + return sysfs_emit(buf, "%u\n", val); +} + +static ssize_t compact_threshold_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct zram *zram = dev_to_zram(dev); + u32 val; + int ret; + + if (kstrtouint(buf, 10, &val) || val >= 100) + return -EINVAL; + + guard(rwsem_write)(&zram->dev_lock); + if (init_done(zram)) { + ret = zs_set_compact_threshold(zram->mem_pool, val); + if (ret) + return ret; + } + zram->compact_threshold = val; + + return len; +} + static ssize_t io_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -2062,6 +2097,12 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize) zram->table = NULL; return false; } + if (zs_set_compact_threshold(zram->mem_pool, zram->compact_threshold)) { + zs_destroy_pool(zram->mem_pool); + vfree(zram->table); + zram->table = NULL; + return false; + } if (!huge_class_size) huge_class_size = zs_huge_class_size(zram->mem_pool); @@ -2898,6 +2939,7 @@ static void zram_reset_device(struct zram *zram) guard(rwsem_write)(&zram->dev_lock); zram->limit_pages = 0; + zram->compact_threshold = ZS_DEFAULT_COMPACT_THRESHOLD; set_capacity_and_notify(zram->disk, 0); part_stat_set_all(zram->disk->part0, 0); @@ -3038,6 +3080,7 @@ static DEVICE_ATTR_RO(io_stat); static DEVICE_ATTR_RO(mm_stat); static DEVICE_ATTR_RO(debug_stat); static DEVICE_ATTR_WO(compact); +static DEVICE_ATTR_RW(compact_threshold); static DEVICE_ATTR_RW(disksize); static DEVICE_ATTR_RO(initstate); static DEVICE_ATTR_WO(reset); @@ -3065,6 +3108,7 @@ static struct attribute *zram_disk_attrs[] = { &dev_attr_initstate.attr, &dev_attr_reset.attr, &dev_attr_compact.attr, + &dev_attr_compact_threshold.attr, &dev_attr_mem_limit.attr, &dev_attr_mem_used_max.attr, &dev_attr_idle.attr, @@ -3140,6 +3184,7 @@ static int zram_add(void) device_id = ret; init_rwsem(&zram->dev_lock); + zram->compact_threshold = ZS_DEFAULT_COMPACT_THRESHOLD; #ifdef CONFIG_ZRAM_WRITEBACK zram->wb_batch_size = 32; zram->compressed_wb = false; diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index a3fa6e3d007f..86a959f594ae 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -143,6 +143,7 @@ struct zram { * the number of pages zram can consume for storing compressed data */ unsigned long limit_pages; + unsigned int compact_threshold; struct zram_stats stats; struct zram_pcpu_stats __percpu *pcpu_stats; -- 2.56.1.987.g2ec0ab4260-goog