From: Chris Wiseman zsmalloc registers a shrinker to run automatic pool compaction under memory pressure. Because zs_shrinker_scan() calls zs_compact(), it ignores sc->nr_to_scan and compacts every size class, taking write_lock(&pool->lock) and per-class locks (dropping them only when a destination zspage fills or pool->lock is contended). During active memory pressure, swap-out allocates concurrently from the same size classes, so physical pages freed by mid-swap compaction are immediately reallocated. Incoming swap-out refills free slots on its own, while shrinker compaction contends with page faults and swap-out. In the RFC discussion, Xueyuan Chen reported compaction stalls up to 269.68 ms (p95 38.86 ms) on a 12 GB Android device. Testing on ARM64 QEMU shows that removing the shrinker eliminates mid-swap compaction churn without increasing active pool memory usage for either zram or zswap, and cuts zswap page-fault latency under active swap-out: ARM64 QEMU (Linux 7.3.0+, zram0, 16 boots per config, ~240 MB swap-out): Before After Pages compacted mid-swap 2,218 0 zram mem_used_total (mid-swap) 121.0 MB 120.9 MB zram mem_used_total (post-compact) 111.2 MB 110.9 MB ARM64 QEMU (Linux 7.3.0+, 4 vCPUs, zswap lz4): Before After Active swap-out pool size 95.9 MB 95.1 MB Active swap-out p50 fault 3,262 us 2,414 us (-26%) After swap-out stops (holes held) 44.2 MB 65.1 MB (+20.9 MB) For zram, userspace can still trigger compaction explicitly via /sys/block/zram/compact (zs_compact()), and mm_stat's pages_compacted now reflects manual compaction only. For zswap, the shrinker only reduced pool size after new swap-outs stopped; as discussed on the RFC thread, reclaim-time compaction hurts active fault latency without reducing active pool size, and any proactive compaction interface for zswap can be evaluated separately. Remove the zsmalloc shrinker callbacks, registration, and header include. Because zs_register_shrinker() was the only remaining reader of pool->name (zs_pool_stat_create() passes the name parameter directly to debugfs), also remove pool->name and its kstrdup()/kfree() allocation. On arm64 defconfig (4K pages, Clang), this shrinks struct zs_pool from 2128 bytes (34 cachelines) to 2112 bytes (33 cachelines) via pahole and reduces mm/zsmalloc.o object size (scripts/bloat-o-meter): arm64 defconfig (Clang): 30684 -> 30127 bytes (-557 bytes, -1.82%) x86_64 minimal (GCC): 12584 -> 12169 bytes (-415 bytes, -3.30%) Link: https://lore.kernel.org/cover.1786003779.git.xueyuan.chen@vivo.com Suggested-by: Sergey Senozhatsky Suggested-by: Yosry Ahmed Signed-off-by: Chris Wiseman Reviewed-by: Xueyuan Chen Acked-by: Yosry Ahmed Reviewed-by: Nhat Pham Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 76 --------------------------------------------------- 1 file changed, 76 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index a1b6161fd237..e4a359fcfaa8 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -264,17 +263,12 @@ struct link_free { static struct kmem_cache *zspage_cachep; struct zs_pool { - const char *name; - struct size_class *size_class[ZS_SIZE_CLASSES]; atomic_long_t pages_allocated; struct zs_pool_stats stats; - /* Compact classes */ - struct shrinker *shrinker; - struct dentry *stat_dentry; #ifdef CONFIG_COMPACTION struct work_struct free_work; @@ -2109,62 +2103,6 @@ void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats) } EXPORT_SYMBOL_GPL(zs_pool_stats); -static unsigned long zs_shrinker_scan(struct shrinker *shrinker, - struct shrink_control *sc) -{ - unsigned long pages_freed; - struct zs_pool *pool = shrinker->private_data; - - /* - * Compact classes and calculate compaction delta. - * Can run concurrently with a manually triggered - * (by user) compaction. - */ - pages_freed = zs_compact(pool); - - return pages_freed ? pages_freed : SHRINK_STOP; -} - -static unsigned long zs_shrinker_count(struct shrinker *shrinker, - struct shrink_control *sc) -{ - int i; - struct size_class *class; - unsigned long pages_to_free = 0; - struct zs_pool *pool = shrinker->private_data; - - for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { - class = pool->size_class[i]; - if (class->index != i) - continue; - - pages_to_free += zs_can_compact(class); - } - - return pages_to_free; -} - -static void zs_unregister_shrinker(struct zs_pool *pool) -{ - shrinker_free(pool->shrinker); -} - -static int zs_register_shrinker(struct zs_pool *pool) -{ - pool->shrinker = shrinker_alloc(0, "mm-zspool:%s", pool->name); - if (!pool->shrinker) - return -ENOMEM; - - pool->shrinker->scan_objects = zs_shrinker_scan; - pool->shrinker->count_objects = zs_shrinker_count; - pool->shrinker->batch = 0; - pool->shrinker->private_data = pool; - - shrinker_register(pool->shrinker); - - return 0; -} - static int calculate_zspage_chain_size(int class_size) { int i, min_waste = INT_MAX; @@ -2210,10 +2148,6 @@ struct zs_pool *zs_create_pool(const char *name) rwlock_init(&pool->lock); atomic_set(&pool->compaction_in_progress, 0); - pool->name = kstrdup(name, GFP_KERNEL); - if (!pool->name) - goto err; - /* * Iterate reversely, because, size of size_class that we want to use * for merging should be larger or equal to current size. @@ -2291,14 +2225,6 @@ struct zs_pool *zs_create_pool(const char *name) /* debug only, don't abort if it fails */ zs_pool_stat_create(pool, name); - /* - * Not critical since shrinker is only used to trigger internal - * defragmentation of the pool which is pretty optional thing. If - * registration fails we still can use the pool normally and user can - * trigger compaction manually. Thus, ignore return code. - */ - zs_register_shrinker(pool); - return pool; err: @@ -2311,7 +2237,6 @@ void zs_destroy_pool(struct zs_pool *pool) { int i; - zs_unregister_shrinker(pool); zs_flush_migration(pool); zs_pool_stat_destroy(pool); @@ -2335,7 +2260,6 @@ void zs_destroy_pool(struct zs_pool *pool) kfree(class); } - kfree(pool->name); kfree(pool); } EXPORT_SYMBOL_GPL(zs_destroy_pool); -- 2.56.1.987.g2ec0ab4260-goog __zs_compact() always takes pool->lock for writing and class->lock before checking if a class can be compacted, which can be expensive and slow if the locks are contended. Introduce an optimistic lockless fast path and check zs_class_can_compact() locklessly at the start of __zs_compact() before taking the locks; once locked, the loop checks it again with exact counts. x86_64 QEMU/KVM (8 vCPUs, LZ4 zram, 64 MiB across 64 size-class buckets, medians across interleaved boots): before after No-op compact (2000 calls) 9.50 us 6.01 us (-36.8%) Partial frag (4/64 buckets, 50 rounds) 11.75 us 8.68 us (-26.1%) Contended compact + 4 I/O threads 10.27 us 6.40 us (-37.7%) Contended concurrent I/O p99 4.57 us 4.40 us (-3.7%) Suggested-by: Nhat Pham Suggested-by: Chris Wiseman Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index e4a359fcfaa8..6e4caf264330 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -535,18 +535,27 @@ static struct size_class *lookup_size_class(struct zs_pool *pool, size_t size) static inline void class_stat_add(struct size_class *class, int type, unsigned long cnt) { - class->stats.objs[type] += cnt; + WRITE_ONCE(class->stats.objs[type], class->stats.objs[type] + cnt); } static inline void class_stat_sub(struct size_class *class, int type, unsigned long cnt) { - class->stats.objs[type] -= cnt; + WRITE_ONCE(class->stats.objs[type], class->stats.objs[type] - cnt); } static inline unsigned long class_stat_read(struct size_class *class, int type) { - return class->stats.objs[type]; + return READ_ONCE(class->stats.objs[type]); +} + +static inline bool zs_class_can_compact(struct size_class *class) +{ + unsigned long obj_allocated = class_stat_read(class, ZS_OBJS_ALLOCATED); + unsigned long obj_used = class_stat_read(class, ZS_OBJS_INUSE); + + return obj_allocated > obj_used && + obj_allocated - obj_used >= class->objs_per_zspage; } static void __init zs_stat_init(void) @@ -1999,6 +2008,9 @@ static unsigned long zs_can_compact(struct size_class *class) return 0; obj_wasted = obj_allocated - obj_used; + if (obj_wasted < class->objs_per_zspage) + return 0; + obj_wasted /= class->objs_per_zspage; return obj_wasted * class->pages_per_zspage; @@ -2011,6 +2023,14 @@ static unsigned long __zs_compact(struct zs_pool *pool, struct zspage *dst_zspage = NULL; unsigned long pages_freed = 0; + /* + * Fast lockless check to avoid taking pool->lock and class->lock + * for classes that have no compactable pages. The exact counts + * are re-checked under locks below. + */ + if (!zs_class_can_compact(class)) + return 0; + /* * Protect against races between zpage migration and zs_free() / * zs_rebind_handle() (only when ZS_OBJ_CLASS_BITS does not apply), @@ -2018,7 +2038,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, */ write_lock(&pool->lock); spin_lock(&class->lock); - while (zs_can_compact(class)) { + while (zs_class_can_compact(class)) { int fg; if (!dst_zspage) { -- 2.56.1.987.g2ec0ab4260-goog Removing the shrinker stops mid-swap compaction stalls, but workloads that free objects (app exit, large swap-ins) can leave fragmented pools behind [1] if userspace does not trigger compaction, or when swap-out is proactive and never hits global memory pressure. Add a per-pool delayed work queued on system_dfl_long_wq with a 5s delay. The idea behind a 5s timeout is to space out zs_free() bursts (which are common) and compaction. This, basically, replaces shrinker compaction, that triggers only under memory pressure and potentially overlaps compaction with heavy swap-out IO, with a proactive compaction that keeps pool fragmentation below requested margin (even on a system that is not under memory pressure). Pools default to ZS_DEFAULT_COMPACT_THRESHOLD (5%); callers can adjust or disable deferred compaction via zs_set_compact_threshold(). x86_64 QEMU/KVM (LZ4 zram): Proactive swap (memory.reclaim, 16 x 20 MiB apps, kill half each round, 4 rounds, 12s settle): shrinker removed deferred (5%) Post-settle overhead avg (MiB) 72.2 72.2 3.0 Post-settle overhead avg (%) 48.86% 48.86% 3.79% Total pages_compacted 0 0 17,886 Without global memory pressure the shrinker never runs and 72 MiB of holes stay allocated, while deferred compaction frees 17,886 pages once after the delay and keeps overhead at ~3 MiB (3.8%). Active swap churn (4 vCPUs, 512M RAM, 4 x 140 MiB, 16s, 3 runs median): shrinker removed deferred (5%) Mid-swap mem_used (MiB) 136.1 140.9 140.5 Total pages_compacted 220,864 0 0 Access p99 latency (us) 13.89 13.93 13.81 Under active swap churn incoming swap-outs refill holes on their own, so deferred compaction compacts 0 pages mid-swap, whereas the shrinker compacted ~221k pages for a ~4 MiB difference. Parallel munmap (8 vCPUs, 8 x 64 MiB swapped out, 10 rounds): shrinker removed: median 8.84 ms (mean 9.09 +/- 1.62 ms) deferred (threshold=0): median 8.59 ms (mean 9.03 +/- 2.13 ms) deferred (threshold=5): median 9.16 ms (mean 9.70 +/- 2.12 ms) All three are within noise, with no measurable zs_free() regression from fast zs_class_can_compact() check. Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@mail.gmail.com [1] Signed-off-by: Sergey Senozhatsky --- include/linux/zsmalloc.h | 3 ++ mm/zsmalloc.c | 97 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index 3232bc9c33b2..a90079bb00ad 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -12,6 +12,8 @@ #include #include +#define ZS_DEFAULT_COMPACT_THRESHOLD 5 + struct zs_pool_stats { /* How many pages were migrated (freed) */ atomic_long_t pages_compacted; @@ -54,6 +56,7 @@ size_t zs_huge_class_size(struct zs_pool *pool); unsigned long zs_get_total_pages(struct zs_pool *pool); unsigned long zs_compact(struct zs_pool *pool); +int zs_set_compact_threshold(struct zs_pool *pool, unsigned int threshold); unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size); diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 6e4caf264330..f5a0f9cd4393 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ #include "zpdesc.h" #define ZSPAGE_MAGIC 0x58 +#define ZS_COMPACT_DELAY (5 * HZ) /* * This must be power of 2 and greater than or equal to sizeof(link_free). @@ -273,6 +275,8 @@ struct zs_pool { #ifdef CONFIG_COMPACTION struct work_struct free_work; #endif + unsigned int compact_threshold; + struct delayed_work compact_work; /* protect zspage migration/compaction */ rwlock_t lock; atomic_t compaction_in_progress; @@ -426,6 +430,9 @@ static void init_deferred_free(struct zs_pool *pool) {} static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {} #endif +static void maybe_schedule_compact(struct zs_pool *pool, + struct size_class *class); + static struct zspage *cache_alloc_zspage(gfp_t gfp) { gfp = gfp & ~(__GFP_HIGHMEM | __GFP_MOVABLE); @@ -1492,6 +1499,10 @@ void zs_free(struct zs_pool *pool, struct zs_handle *handle) __free_zspage_lockless(zspage_to_free); atomic_long_sub(class->pages_per_zspage, &pool->pages_allocated); } + + /* Created a hole in a partial zspage, check if compaction is needed */ + if (fullness != ZS_INUSE_RATIO_0) + maybe_schedule_compact(pool, class); } EXPORT_SYMBOL_GPL(zs_free); @@ -2123,6 +2134,90 @@ void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats) } EXPORT_SYMBOL_GPL(zs_pool_stats); +static unsigned long zs_pool_can_compact(struct zs_pool *pool) +{ + int i; + struct size_class *class; + unsigned long pages_to_free = 0; + + for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { + class = pool->size_class[i]; + if (class->index != i) + continue; + + pages_to_free += zs_can_compact(class); + } + + return pages_to_free; +} + +static void async_compact_pool(struct work_struct *work) +{ + struct zs_pool *pool = container_of(work, struct zs_pool, + compact_work.work); + unsigned int threshold = READ_ONCE(pool->compact_threshold); + unsigned long total_pages; + unsigned long pages_to_free; + + if (!threshold || atomic_read(&pool->compaction_in_progress)) + return; + + total_pages = zs_get_total_pages(pool); + if (!total_pages) + return; + + pages_to_free = zs_pool_can_compact(pool); + if ((u64)pages_to_free * 100 < (u64)total_pages * threshold) + return; + + zs_compact(pool); +} + +static void maybe_schedule_compact(struct zs_pool *pool, + struct size_class *class) +{ + if (!READ_ONCE(pool->compact_threshold)) + return; + if (delayed_work_pending(&pool->compact_work)) + return; + if (!zs_class_can_compact(class)) + return; + + queue_delayed_work(system_dfl_long_wq, &pool->compact_work, + ZS_COMPACT_DELAY); +} + +static void init_deferred_compact(struct zs_pool *pool) +{ + INIT_DELAYED_WORK(&pool->compact_work, async_compact_pool); + pool->compact_threshold = ZS_DEFAULT_COMPACT_THRESHOLD; +} + +/** + * zs_set_compact_threshold - Set deferred compaction threshold for a pool. + * @pool: pool to configure + * @threshold: fragmentation percentage (1..99), or 0 to disable + * + * Deferred compaction runs when reclaimable pages reach @threshold percent + * of the pool's allocated pages. Setting @threshold to 0 disables deferred + * compaction and cancels any pending work. + * + * Context: Process context. May sleep when @threshold is 0. + * + * Return: 0 on success, or -EINVAL if @threshold >= 100. + */ +int zs_set_compact_threshold(struct zs_pool *pool, unsigned int threshold) +{ + if (WARN_ON_ONCE(threshold >= 100)) + return -EINVAL; + + WRITE_ONCE(pool->compact_threshold, threshold); + if (!threshold) + cancel_delayed_work_sync(&pool->compact_work); + return 0; +} +EXPORT_SYMBOL_GPL(zs_set_compact_threshold); + static int calculate_zspage_chain_size(int class_size) { int i, min_waste = INT_MAX; @@ -2165,6 +2260,7 @@ struct zs_pool *zs_create_pool(const char *name) return NULL; init_deferred_free(pool); + init_deferred_compact(pool); rwlock_init(&pool->lock); atomic_set(&pool->compaction_in_progress, 0); @@ -2257,6 +2353,7 @@ void zs_destroy_pool(struct zs_pool *pool) { int i; + cancel_delayed_work_sync(&pool->compact_work); zs_flush_migration(pool); zs_pool_stat_destroy(pool); -- 2.56.1.987.g2ec0ab4260-goog 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..9b3041d71407 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. Valid values are 0..99, where 0 disables + deferred compaction. Default is 5. 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 0d585f01e7c2..e2a45783e111 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1971,6 +1971,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 (kstrtou32(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) { @@ -2071,6 +2106,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); @@ -2907,6 +2948,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); @@ -3050,6 +3092,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); @@ -3077,6 +3120,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, @@ -3155,6 +3199,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 bcf4280940a9..3abb1fb1cad8 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -144,6 +144,7 @@ struct zram { * the number of pages zram can consume for storing compressed data */ unsigned long limit_pages; + u32 compact_threshold; struct zram_stats stats; struct zram_pcpu_stats __percpu *pcpu_stats; -- 2.56.1.987.g2ec0ab4260-goog With a fixed 5s delay, long stretches of below-threshold frees or contended compactions that free no pages still wake up the compaction worker every 5s. Follow mm/compaction.c's defer-shift pattern: exponentially back off (5s -> 10s -> 20s -> 40s -> 80s) when deferred compaction has nothing to compact or fails to free pages, resetting back to 5s once compaction makes progress or the threshold is changed. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index f5a0f9cd4393..5cff1eaf364a 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -43,6 +43,7 @@ #define ZSPAGE_MAGIC 0x58 #define ZS_COMPACT_DELAY (5 * HZ) +#define ZS_COMPACT_MAX_DEFER_SHIFT 4 /* * This must be power of 2 and greater than or equal to sizeof(link_free). @@ -276,6 +277,7 @@ struct zs_pool { struct work_struct free_work; #endif unsigned int compact_threshold; + unsigned int compact_defer_shift; struct delayed_work compact_work; /* protect zspage migration/compaction */ rwlock_t lock; @@ -2156,26 +2158,41 @@ static void async_compact_pool(struct work_struct *work) struct zs_pool *pool = container_of(work, struct zs_pool, compact_work.work); unsigned int threshold = READ_ONCE(pool->compact_threshold); + unsigned int shift = READ_ONCE(pool->compact_defer_shift); unsigned long total_pages; unsigned long pages_to_free; - if (!threshold || atomic_read(&pool->compaction_in_progress)) + if (!threshold) return; + if (atomic_read(&pool->compaction_in_progress)) + goto backoff; + total_pages = zs_get_total_pages(pool); - if (!total_pages) + if (!total_pages) { + WRITE_ONCE(pool->compact_defer_shift, 0); return; + } pages_to_free = zs_pool_can_compact(pool); if ((u64)pages_to_free * 100 < (u64)total_pages * threshold) + goto backoff; + + if (zs_compact(pool)) { + WRITE_ONCE(pool->compact_defer_shift, 0); return; + } - zs_compact(pool); +backoff: + if (shift < ZS_COMPACT_MAX_DEFER_SHIFT) + WRITE_ONCE(pool->compact_defer_shift, shift + 1); } static void maybe_schedule_compact(struct zs_pool *pool, struct size_class *class) { + unsigned long delay; + if (!READ_ONCE(pool->compact_threshold)) return; if (delayed_work_pending(&pool->compact_work)) @@ -2183,8 +2200,8 @@ static void maybe_schedule_compact(struct zs_pool *pool, if (!zs_class_can_compact(class)) return; - queue_delayed_work(system_dfl_long_wq, &pool->compact_work, - ZS_COMPACT_DELAY); + delay = ZS_COMPACT_DELAY << READ_ONCE(pool->compact_defer_shift); + queue_delayed_work(system_dfl_long_wq, &pool->compact_work, delay); } static void init_deferred_compact(struct zs_pool *pool) @@ -2212,6 +2229,7 @@ int zs_set_compact_threshold(struct zs_pool *pool, unsigned int threshold) return -EINVAL; WRITE_ONCE(pool->compact_threshold, threshold); + WRITE_ONCE(pool->compact_defer_shift, 0); if (!threshold) cancel_delayed_work_sync(&pool->compact_work); return 0; -- 2.56.1.987.g2ec0ab4260-goog