* [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
@ 2026-12-03 10:13 Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 1/4] mm/zsmalloc: remove shrinker Sergey Senozhatsky
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 10:13 UTC (permalink / raw)
To: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song
Cc: Brian Geffon, Minchan Kim, Richard Chang, Johannes Weiner,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao,
linux-kernel, linux-block, linux-mm, Sergey Senozhatsky
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@google.com [1]
Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@mail.gmail.com [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
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 +
include/linux/zsmalloc.h | 3 +
mm/zsmalloc.c | 136 ++++++++++++--------
6 files changed, 142 insertions(+), 54 deletions(-)
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 8+ messages in thread
* [RFC PATCH 1/4] mm/zsmalloc: remove shrinker
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
@ 2026-12-03 10:13 ` Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact() Sergey Senozhatsky
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 10:13 UTC (permalink / raw)
To: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song
Cc: Brian Geffon, Minchan Kim, Richard Chang, Johannes Weiner,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao,
linux-kernel, linux-block, linux-mm, Chris Wiseman,
Sergey Senozhatsky
From: Chris Wiseman <chriswiseman@google.com>
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<id>/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 <senozhatsky@chromium.org>
Suggested-by: Yosry Ahmed <yosry@kernel.org>
Signed-off-by: Chris Wiseman <chriswiseman@google.com>
Reviewed-by: Xueyuan Chen <xueyuan.chen21@gmail.com>
Acked-by: Yosry Ahmed <yosry@kernel.org>
Reviewed-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
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 <linux/scatterlist.h>
#include <linux/spinlock.h>
#include <linux/sprintf.h>
-#include <linux/shrinker.h>
#include <linux/types.h>
#include <linux/debugfs.h>
#include <linux/zsmalloc.h>
@@ -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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact()
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 1/4] mm/zsmalloc: remove shrinker Sergey Senozhatsky
@ 2026-12-03 10:13 ` Sergey Senozhatsky
2026-12-04 2:55 ` [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Xueyuan Chen
2026-12-04 3:51 ` Sergey Senozhatsky
3 siblings, 0 replies; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 10:13 UTC (permalink / raw)
To: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song
Cc: Brian Geffon, Minchan Kim, Richard Chang, Johannes Weiner,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao,
linux-kernel, linux-block, linux-mm, Sergey Senozhatsky,
Chris Wiseman
__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 <nphamcs@gmail.com>
Suggested-by: Chris Wiseman <chriswiseman@google.com>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 1/4] mm/zsmalloc: remove shrinker Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact() Sergey Senozhatsky
@ 2026-12-04 2:55 ` Xueyuan Chen
2026-12-04 3:49 ` Sergey Senozhatsky
2026-12-04 3:51 ` Sergey Senozhatsky
3 siblings, 1 reply; 8+ messages in thread
From: Xueyuan Chen @ 2026-12-04 2:55 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song, Brian Geffon,
Minchan Kim, Richard Chang, Johannes Weiner, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao, linux-kernel,
linux-block, linux-mm
On 2026-12-03 19:13 +0900, 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).
Hi Sergey,
I wonder if we could also consider cross-class reuse to reduce the
need for compaction.
In my Pixel 6 Pro workload, I observed that the 2048-byte class
grew by 3.5 MiB within a 2-second window while other size classes
had about 125 MiB of compactable memory.
When a class runs out of free objects, could we try allocating from
an existing zspage in a slightly larger class before allocating
a new zspage?
This would introduce some internal fragmentation, but might reduce
physical page allocations and object migration during compaction,
without changing the existing zspage layout or the allocation fast
path.
What do you think about this trade-off?
>
> Link: https://lore.kernel.org/linux-kernel/18ed2967f99a2eff.3137322e32332e3235332e313736.2c2157.0000sendgmr@google.com [1]
> Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@mail.gmail.com [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
>
> 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 +
> include/linux/zsmalloc.h | 3 +
> mm/zsmalloc.c | 136 ++++++++++++--------
> 6 files changed, 142 insertions(+), 54 deletions(-)
>
> --
> 2.56.1.987.g2ec0ab4260-goog
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
2026-12-04 2:55 ` [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Xueyuan Chen
@ 2026-12-04 3:49 ` Sergey Senozhatsky
2026-12-06 6:37 ` Sergey Senozhatsky
0 siblings, 1 reply; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-04 3:49 UTC (permalink / raw)
To: Xueyuan Chen
Cc: Sergey Senozhatsky, Andrew Morton, Yosry Ahmed, Nhat Pham,
Barry Song, Brian Geffon, Minchan Kim, Richard Chang,
Johannes Weiner, Chengming Zhou, Xueyuan Chen, Nanzhe Zhao,
linux-kernel, linux-block, linux-mm
On (26/12/04 10:55), Xueyuan Chen wrote:
> On 2026-12-03 19:13 +0900, 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).
>
> Hi Sergey,
>
> I wonder if we could also consider cross-class reuse to reduce the
> need for compaction.
>
> In my Pixel 6 Pro workload, I observed that the 2048-byte class
> grew by 3.5 MiB within a 2-second window while other size classes
> had about 125 MiB of compactable memory.
>
> When a class runs out of free objects, could we try allocating from
> an existing zspage in a slightly larger class before allocating
> a new zspage?
>
> This would introduce some internal fragmentation, but might reduce
> physical page allocations and object migration during compaction,
> without changing the existing zspage layout or the allocation fast
> path.
>
> What do you think about this trade-off?
That idea did pop-up sometime in 2015-2017, but we were never sure
about it. Borrowing free space from a higher size class comes with
higher object padding overhead, potentially, so we never explored
it in depth. This kind of changes zsmalloc design, from being slab-like
to managing free spaces.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
` (2 preceding siblings ...)
2026-12-04 2:55 ` [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Xueyuan Chen
@ 2026-12-04 3:51 ` Sergey Senozhatsky
2026-12-05 8:57 ` Nanzhe Zhao
3 siblings, 1 reply; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-04 3:51 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song, Brian Geffon,
Minchan Kim, Richard Chang, Johannes Weiner, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao, linux-kernel,
linux-block, linux-mm
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@google.com [1]
> Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@mail.gmail.com [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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
2026-12-04 3:51 ` Sergey Senozhatsky
@ 2026-12-05 8:57 ` Nanzhe Zhao
0 siblings, 0 replies; 8+ messages in thread
From: Nanzhe Zhao @ 2026-12-05 8:57 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song, Brian Geffon,
Minchan Kim, Richard Chang, Johannes Weiner, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Nanzhe Zhao, linux-kernel,
linux-block, linux-mm
> pages_to_free = zs_pool_can_compact(pool);
> if ((u64)pages_to_free * 100 < (u64)total_pages * threshold)
> goto backoff;
...
>backoff:
> if (shift < ZS_COMPACT_MAX_DEFER_SHIFT)
> WRITE_ONCE(pool->compact_defer_shift, shift + 1);
Is increasing the delay every time the threshold is not met a bit too
aggressive?
I tested this on Pixel 6 with a shorter workload. Sampling the wasted memory
(mem_used_total - compr_data_size) time series, the dynamic delay (5s-80s)
gave a median waste of ~33 MB and P90 of ~127 MB (P99 ~217 MB). With a fixed
5s delay, the median stayed at ~18 MB with a P90 of ~47 MB (P99 ~109 MB), and
with 1s it was ~15 MB with a P90 of ~31 MB.
What happened was that in about 62% of the executions, the work ran when pool
fragmentation was still below the 5% threshold, taking the backoff branch.
Because so many checks found the pool below threshold, the delay quickly
climbed to the 80s ceiling (about a quarter of all queues were at 40s-80s).
As a result of staying at 80s, waste above 100 MB lingered for 15.4% of the
run time on average (ranging 9% to 27%, lasting 14-71s). By contrast, under
fixed 5s waste above 100 MB only lasted 1.1% of the time (2-3s episodes), and
under 1s it was 0.3%.
Looking at the queueing side:
>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;
>
> delay = ZS_COMPACT_DELAY << READ_ONCE(pool->compact_defer_shift);
> queue_delayed_work(system_dfl_long_wq, &pool->compact_work, delay);
>}
Could we also check the pool fragmentation threshold at queue time before
queueing the work, and only queue when the threshold is reached?
In our data, about 75% of the works were queued when pool-wide fragmentation
was actually below 5%. Under active churn, zs_class_can_compact(class) almost
always passes for some class. These premature queues are the direct source
of why the queued works repeatedly run, find the threshold not met, and
rapidly push the delay to 80s.
Perhaps we could consider a dual-watermark design? For example, check against
a lower watermark at queue time to avoid scheduling premature work, and check
against a higher watermark when the work runs before actually compacting.
What do you think?
Thanks,
Nanzhe
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction
2026-12-04 3:49 ` Sergey Senozhatsky
@ 2026-12-06 6:37 ` Sergey Senozhatsky
0 siblings, 0 replies; 8+ messages in thread
From: Sergey Senozhatsky @ 2026-12-06 6:37 UTC (permalink / raw)
To: Xueyuan Chen
Cc: Andrew Morton, Yosry Ahmed, Nhat Pham, Barry Song, Brian Geffon,
Minchan Kim, Richard Chang, Johannes Weiner, Chengming Zhou,
Xueyuan Chen, Nanzhe Zhao, linux-kernel, linux-block, linux-mm,
Sergey Senozhatsky
On (26/12/04 12:49), Sergey Senozhatsky wrote:
> On (26/12/04 10:55), Xueyuan Chen wrote:
> > On 2026-12-03 19:13 +0900, 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).
> >
> > Hi Sergey,
> >
> > I wonder if we could also consider cross-class reuse to reduce the
> > need for compaction.
> >
> > In my Pixel 6 Pro workload, I observed that the 2048-byte class
> > grew by 3.5 MiB within a 2-second window while other size classes
> > had about 125 MiB of compactable memory.
> >
> > When a class runs out of free objects, could we try allocating from
> > an existing zspage in a slightly larger class before allocating
> > a new zspage?
> >
> > This would introduce some internal fragmentation, but might reduce
> > physical page allocations and object migration during compaction,
> > without changing the existing zspage layout or the allocation fast
> > path.
> >
> > What do you think about this trade-off?
>
> That idea did pop-up sometime in 2015-2017, but we were never sure
> about it. Borrowing free space from a higher size class comes with
> higher object padding overhead, potentially, so we never explored
> it in depth. This kind of changes zsmalloc design, from being slab-like
> to managing free spaces.
The last sentence in my previous email can be ignored, I don't
understand what I was trying to say there :)
Now, back to your numbers. I guess with threshold-based compaction
we won't be having a problem of wasted 125 MiB in some size classes.
So maybe rather than counting on possibility of lower size clasess
borrowing free space from upper classes, which they may not do and
then we just have wasted memory, we can just free unused memory.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-06 6:38 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 1/4] mm/zsmalloc: remove shrinker Sergey Senozhatsky
2026-12-03 10:13 ` [RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact() Sergey Senozhatsky
2026-12-04 2:55 ` [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Xueyuan Chen
2026-12-04 3:49 ` Sergey Senozhatsky
2026-12-06 6:37 ` Sergey Senozhatsky
2026-12-04 3:51 ` Sergey Senozhatsky
2026-12-05 8:57 ` Nanzhe Zhao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox