* [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-04 10:14 ` sashiko-bot
2026-12-03 10:13 ` [RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact() Sergey Senozhatsky
` (4 subsequent siblings)
5 siblings, 1 reply; 11+ 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] 11+ messages in thread* Re: [RFC PATCH 1/4] mm/zsmalloc: remove shrinker
2026-12-03 10:13 ` [RFC PATCH 1/4] mm/zsmalloc: remove shrinker Sergey Senozhatsky
@ 2026-12-04 10:14 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-04 10:14 UTC (permalink / raw)
To: Sergey Senozhatsky, 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-mm,
Chris Wiseman
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/zsmalloc: removing name causes cacheline false sharing in
struct zs_pool
--
commit 2d6bd115e9473ab90f0a6f836b8d71968644c949
Author: Chris Wiseman <chriswiseman@google.com>
mm/zsmalloc: remove shrinker
This commit removes zsmalloc's automatic compaction shrinker along with
its callbacks, registration, and the now-unused pool->name field.
Link: https://lore.kernel.org/cover.1786003779.git.xueyuan.chen@vivo.com
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index f263bd2b8d468..17ef924f748bd 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
[ ... ]
> @@ -270,17 +269,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;
[Severity: Medium]
Does removing name introduce cacheline false sharing between size_class[]
and pages_allocated?
On 4K-page 64-bit kernels, size_class[255] is 2040 bytes, so the 8-byte
name pointer previously aligned it to offset 2048. Without name,
pages_allocated shifts to offset 2040 and shares cacheline 31 with
read-only size_class[248..254], including the huge class at index 254.
Every zspage allocation in zs_store() and free in zs_free() updates
pages_allocated atomically, bouncing that cacheline against concurrent
hot-path reads of size_class[248..254] (such as huge-object lookups in
zs_store(), zs_free(), and zs_obj_read_sg_begin()).
Since struct zs_pool allocates from kmalloc-4k at both 2128 and 2112
bytes, this saves no slab memory. Marking pages_allocated with
____cacheline_aligned_in_smp avoids the false sharing without changing
the kmalloc bucket.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203101411.3609533-1-senozhatsky@chromium.org?part=1
^ permalink raw reply [flat|nested] 11+ 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-03 10:13 ` [RFC PATCH 3/4] zsmalloc: add deferred threshold compaction Sergey Senozhatsky
` (3 subsequent siblings)
5 siblings, 0 replies; 11+ 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] 11+ messages in thread* [RFC PATCH 3/4] zsmalloc: add deferred threshold 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-03 10:13 ` Sergey Senozhatsky
2026-12-04 10:14 ` sashiko-bot
2026-12-03 10:13 ` [RFC PATCH 4/4] zram: add compact_threshold sysfs attribute Sergey Senozhatsky
` (2 subsequent siblings)
5 siblings, 1 reply; 11+ 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
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 <senozhatsky@chromium.org>
---
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 <linux/compiler.h>
#include <linux/types.h>
+#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 <linux/scatterlist.h>
#include <linux/spinlock.h>
#include <linux/sprintf.h>
+#include <linux/jiffies.h>
#include <linux/types.h>
#include <linux/debugfs.h>
#include <linux/zsmalloc.h>
@@ -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
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [RFC PATCH 3/4] zsmalloc: add deferred threshold compaction
2026-12-03 10:13 ` [RFC PATCH 3/4] zsmalloc: add deferred threshold compaction Sergey Senozhatsky
@ 2026-12-04 10:14 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-04 10:14 UTC (permalink / raw)
To: Sergey Senozhatsky, 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-mm
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] zsmalloc: zs_set_compact_threshold() never schedules
compaction on idle pool
- [Medium] zsmalloc: freeing whole zspages shrinks total_pages without
scheduling compact
- [Medium] zsmalloc: false sharing between pages_allocated and
compact_threshold/work
- [Low] zsmalloc: zs_set_compact_threshold() lacks kernel-doc and
changelog coverage
--
commit 6c8a6f9143072a471636aef6ee685db38822eec1
Author: Sergey Senozhatsky <senozhatsky@chromium.org>
zsmalloc: add deferred threshold compaction
This commit adds per-pool delayed compaction work queued after partial
zs_free() calls to compact pools whose reclaimable pages exceed a
configurable percentage threshold. It also exports
zs_set_compact_threshold() to adjust or disable the threshold at runtime.
Link: https://lore.kernel.org/linux-kernel/CAGsJ_4yoogQHpxGigawTfMPEKL_A6OkfEfsmJHzDHLPS3FCUQg@mail.gmail.com [1]
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index c0bad8214853f..6c9211d1fae09 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
[ ... ]
> @@ -276,6 +278,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
[Severity: Medium]
Can placing compact_threshold and compact_work here cause cacheline false
sharing with pages_allocated on 64-bit 8K, 16K, and 64K page kernels?
On those configurations ZS_SIZE_CLASSES is 256 (8K) or 257 (16K/64K),
putting pages_allocated, compact_threshold, and compact_work.work.data in
the same 64-byte cacheline (offsets 2048..2111).
Every partial zs_free() now reads compact_threshold and
compact_work.work.data via maybe_schedule_compact(), bouncing that
cacheline against concurrent atomic updates to pages_allocated in
zs_store() and full-zspage zs_free() even when compact_threshold is 0.
Aligning compact_threshold and compact_work (or pages_allocated) to a
separate cacheline would avoid the contention.
[ ... ]
> @@ -1498,6 +1505,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);
> }
[Severity: Medium]
Can skipping maybe_schedule_compact() when fullness == ZS_INUSE_RATIO_0
leave pool fragmentation above compact_threshold indefinitely?
async_compact_pool() checks the pool-wide ratio pages_to_free * 100 >=
total_pages * threshold, so freeing whole zspages shrinks total_pages and
can push existing waste in other classes above the threshold.
If fragmented non-huge classes sit below 5% alongside many huge-class
objects (objs_per_zspage == 1), freeing the huge objects always hits
ZS_INUSE_RATIO_0 and never queues compact_work; maybe_schedule_compact()
only checks the freed class anyway, stranding pages until a partial free.
[ ... ]
> +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;
> +}
[Severity: Medium]
Should zs_set_compact_threshold() schedule compact_work when enabling or
lowering threshold on an already-fragmented pool?
Because maybe_schedule_compact() is only called from partial zs_free(),
enabling deferred compaction (0 to non-zero) or tightening the threshold
on an idle pool leaves existing fragmentation above the new threshold
indefinitely until a future partial free happens.
> +EXPORT_SYMBOL_GPL(zs_set_compact_threshold);
[Severity: Low]
Should zs_set_compact_threshold() have a kernel-doc comment documenting
its parameter semantics (0 disables, 1..99 is the pool-wide percentage,
>= 100 returns -EINVAL) and its sleeping contract?
When threshold is 0, cancel_delayed_work_sync() sleeps and cannot be
called from atomic context or while holding locks taken by
async_compact_pool(). The commit message also omits the new
zs_set_compact_threshold() API and ZS_DEFAULT_COMPACT_THRESHOLD constant.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203101411.3609533-1-senozhatsky@chromium.org?part=3
^ permalink raw reply [flat|nested] 11+ messages in thread
* [RFC PATCH 4/4] zram: add compact_threshold sysfs attribute
2026-12-03 10:13 [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Sergey Senozhatsky
` (2 preceding siblings ...)
2026-12-03 10:13 ` [RFC PATCH 3/4] zsmalloc: add deferred threshold compaction Sergey Senozhatsky
@ 2026-12-03 10:13 ` Sergey Senozhatsky
2026-12-04 10:14 ` sashiko-bot
2026-12-04 2:55 ` [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction Xueyuan Chen
2026-12-04 3:51 ` Sergey Senozhatsky
5 siblings, 1 reply; 11+ 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
Add a per-device sysfs attribute, compact_threshold, to configure
zsmalloc's deferred compaction threshold percentage at runtime.
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
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<id>/compact_threshold
+Date: December 2026
+Contact: Sergey Senozhatsky <senozhatsky@chromium.org>
+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<id>/io_stat
Date: August 2015
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
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
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [RFC PATCH 4/4] zram: add compact_threshold sysfs attribute
2026-12-03 10:13 ` [RFC PATCH 4/4] zram: add compact_threshold sysfs attribute Sergey Senozhatsky
@ 2026-12-04 10:14 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-04 10:14 UTC (permalink / raw)
To: Sergey Senozhatsky, 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-mm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] zram: compact_threshold ABI doc uses internal macro and omits
valid range
--
commit 45192552c92b313d87a6a0c3bbe74b27e88a52d9
Author: Sergey Senozhatsky <senozhatsky@chromium.org>
zram: add compact_threshold sysfs attribute
This commit adds a compact_threshold sysfs attribute for zram devices to
configure zsmalloc's deferred compaction threshold at runtime.
> diff --git a/Documentation/ABI/testing/sysfs-block-zram b/Documentation/ABI/testing/sysfs-block-zram
> index 1ea9018a627f2..84a924f4e8d28 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<id>/compact_threshold
> +Date: December 2026
> +Contact: Sergey Senozhatsky <senozhatsky@chromium.org>
> +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.
[Severity: Low]
Should this ABI documentation state the numeric default (5) instead of
the kernel-internal macro ZS_DEFAULT_COMPACT_THRESHOLD, and document the
valid 0-99 input range?
ZS_DEFAULT_COMPACT_THRESHOLD is defined in include/linux/zsmalloc.h and
is not exposed to userspace. In addition, compact_threshold_store()
rejects values >= 100 with -EINVAL, so users reading "percentage
threshold" without an upper bound will get an unexpected error if they
write 100.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203101411.3609533-1-senozhatsky@chromium.org?part=4
^ permalink raw reply [flat|nested] 11+ 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
` (3 preceding siblings ...)
2026-12-03 10:13 ` [RFC PATCH 4/4] zram: add compact_threshold sysfs attribute Sergey Senozhatsky
@ 2026-12-04 2:55 ` Xueyuan Chen
2026-12-04 3:49 ` Sergey Senozhatsky
2026-12-04 3:51 ` Sergey Senozhatsky
5 siblings, 1 reply; 11+ 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] 11+ 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
0 siblings, 0 replies; 11+ 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] 11+ 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
` (4 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
5 siblings, 0 replies; 11+ 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] 11+ messages in thread