Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
From: Sergey Senozhatsky
Date: Thu Dec 03 2026 - 22:52:00 EST
On (26/12/03 19:13), Sergey Senozhatsky wrote:
> This is a follow-up to Chris's shrinker removal patch [1] and Barry's
> Pixel 6 fragmentation numbers [2].
>
> Removing the shrinker avoids mid-swap compaction stalls, but without
> any automatic compaction we risk regressing memory footprint on systems
> that do not run a userspace compaction daemon, and on systems that do
> proactive swap-out without hitting memory pressure (no shrinker
> triggers).
>
> Replace shrinker callbacks (now removed) with deferred threshold-based
> compaction in zsmalloc (plus a runtime-tunable zram sysfs knob to
> configure or disable it).
>
> Link: https://lore.kernel.org/linux-kernel/18ed2967f99a2eff.3137322e32332e3235332e313736.2c2157.0000sendgmr@xxxxxxxxxx [1]
> Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@xxxxxxxxxxxxxx [2]
>
> Chris Wiseman (1):
> mm/zsmalloc: remove shrinker
>
> Sergey Senozhatsky (3):
> zsmalloc: introduce lockless zs_class_can_compact()
> zsmalloc: add deferred threshold compaction
> zram: add compact_threshold sysfs attribute
And just as a follow-up, here is the current compaction timeout
exponential backoff patch, which replaces a static 10s delay with
a dynamic delay in the range of 5s to 80s.
---
mm/zsmalloc.c | 28 ++++++++++++++++++++++------
1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 920ac07443ae..72c4152c65d7 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -42,7 +42,8 @@
#include "zpdesc.h"
#define ZSPAGE_MAGIC 0x58
-#define ZS_COMPACT_DELAY (10 * HZ)
+#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).
@@ -273,6 +274,7 @@ struct zs_pool {
struct dentry *stat_dentry;
unsigned int compact_threshold;
+ unsigned int compact_defer_shift;
struct delayed_work compact_work;
#ifdef CONFIG_COMPACTION
struct work_struct free_work;
@@ -2156,26 +2158,39 @@ 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)
- return;
+ goto backoff;
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 +2198,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)
@@ -2199,6 +2214,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