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 10s delay. The idea behind a 10s 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). 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 10s 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 | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 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..920ac07443ae 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 (10 * HZ) /* * This must be power of 2 and greater than or equal to sizeof(link_free). @@ -270,6 +272,8 @@ struct zs_pool { struct zs_pool_stats stats; struct dentry *stat_dentry; + unsigned int compact_threshold; + struct delayed_work compact_work; #ifdef CONFIG_COMPACTION struct work_struct free_work; #endif @@ -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,77 @@ 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; +} + +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 +2247,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 +2340,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