From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Barry Song <baohua@kernel.org>, Nhat Pham <nphamcs@gmail.com>,
Yosry Ahmed <yosry@kernel.org>
Cc: Chris Wiseman <chriswiseman@google.com>,
Andrew Morton <akpm@linux-foundation.org>,
Minchan Kim <minchan@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Chengming Zhou <chengming.zhou@linux.dev>,
Xueyuan Chen <xueyuan.chen21@gmail.com>,
Xueyuan Chen <xueyuan.chen@vivo.com>,
Brian Geffon <bgeffon@google.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Nanzhe Zhao <nzzhao@126.com>,
Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: Re: [PATCH] mm/zsmalloc: remove shrinker
Date: Thu, 3 Dec 2026 13:52:25 +0900 [thread overview]
Message-ID: <axDxrAla_MHEVROJ@google.com> (raw)
In-Reply-To: <axDknU_Jp0asc1nS@google.com>
On (26/12/03 13:24), Sergey Senozhatsky wrote:
[..]
> > I wonder if we should consider some sort of a background compaction
> > triggered by zram instead of shrinker callback. E.g. when zram observes
> > that mem_used_total to compr_data_size ration goes above some run-time
>
> ^ ratio. Every single time!
> Every single time I try to spell
> "ratio" a "ration" comes out.
>
> So, atop of Chris' patch. Just a (vibe coded) sketch of an idea.
> Writeback is another case which creates fragmentation, but writeback
> is already called by user-space, so user-space can call compaction
> immediately after writeback.
>
An alternative idea: do deferred threshold-based compaction on the
zsmalloc side, so that zswap folks also get it.
This basically replaces zsmalloc shrinker (which triggers when things
are bad) with a threshold-based compaction, which keeps zsmalloc memory
waste within a certain limit. Sort of sounds like a decent improvement,
but there are some tradeoffs.
---
diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h
index 3232bc9c33b2..7c689135cdc5 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);
+void 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 a1b6161fd237..c381ae768375 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -32,7 +32,7 @@
#include <linux/scatterlist.h>
#include <linux/spinlock.h>
#include <linux/sprintf.h>
-#include <linux/shrinker.h>
+#include <linux/jiffies.h>
#include <linux/types.h>
#include <linux/debugfs.h>
#include <linux/zsmalloc.h>
@@ -42,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).
@@ -264,21 +265,19 @@ 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;
#endif
+ struct delayed_work compact_work;
+ unsigned int compact_threshold;
+
/* protect zspage migration/compaction */
rwlock_t lock;
atomic_t compaction_in_progress;
@@ -432,6 +431,8 @@ static void init_deferred_free(struct zs_pool *pool) {}
static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
#endif
+static void kick_deferred_compact(struct zs_pool *pool);
+
static struct zspage *cache_alloc_zspage(gfp_t gfp)
{
gfp = gfp & ~(__GFP_HIGHMEM | __GFP_MOVABLE);
@@ -541,18 +542,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)
@@ -1461,6 +1471,7 @@ void zs_free(struct zs_pool *pool, struct zs_handle *handle)
struct size_class *class;
int fullness;
struct zspage *zspage_to_free = NULL;
+ bool kick_compact = false;
if (!zs_handle_is_allocated(handle))
return;
@@ -1481,6 +1492,10 @@ void zs_free(struct zs_pool *pool, struct zs_handle *handle)
} else {
kick_deferred_free(pool);
}
+ } else if (READ_ONCE(pool->compact_threshold) &&
+ !delayed_work_pending(&pool->compact_work) &&
+ zs_class_can_compact(class)) {
+ kick_compact = true;
}
spin_unlock(&class->lock);
@@ -1488,6 +1503,8 @@ void zs_free(struct zs_pool *pool, struct zs_handle *handle)
if (zspage_to_free) {
__free_zspage_lockless(zspage_to_free);
atomic_long_sub(class->pages_per_zspage, &pool->pages_allocated);
+ } else if (kick_compact) {
+ kick_deferred_compact(pool);
}
}
EXPORT_SYMBOL_GPL(zs_free);
@@ -2005,6 +2022,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;
@@ -2017,6 +2037,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),
@@ -2024,7 +2052,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) {
@@ -2109,29 +2137,11 @@ 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)
+static unsigned long zs_pool_can_compact(struct zs_pool *pool)
{
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];
@@ -2144,26 +2154,50 @@ static unsigned long zs_shrinker_count(struct shrinker *shrinker,
return pages_to_free;
}
-static void zs_unregister_shrinker(struct zs_pool *pool)
+static void async_compact_pool(struct work_struct *work)
{
- shrinker_free(pool->shrinker);
+ 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 int zs_register_shrinker(struct zs_pool *pool)
+static void kick_deferred_compact(struct zs_pool *pool)
{
- pool->shrinker = shrinker_alloc(0, "mm-zspool:%s", pool->name);
- if (!pool->shrinker)
- return -ENOMEM;
+ queue_delayed_work(system_dfl_long_wq, &pool->compact_work,
+ ZS_COMPACT_DELAY);
+}
- pool->shrinker->scan_objects = zs_shrinker_scan;
- pool->shrinker->count_objects = zs_shrinker_count;
- pool->shrinker->batch = 0;
- pool->shrinker->private_data = pool;
+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;
+}
- shrinker_register(pool->shrinker);
+void zs_set_compact_threshold(struct zs_pool *pool, unsigned int threshold)
+{
+ if (threshold >= 100)
+ threshold = 99;
- return 0;
+ WRITE_ONCE(pool->compact_threshold, threshold);
+ if (!threshold)
+ cancel_delayed_work_sync(&pool->compact_work);
}
+EXPORT_SYMBOL_GPL(zs_set_compact_threshold);
static int calculate_zspage_chain_size(int class_size)
{
@@ -2207,13 +2241,10 @@ 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);
- 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 +2322,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 +2334,7 @@ void zs_destroy_pool(struct zs_pool *pool)
{
int i;
- zs_unregister_shrinker(pool);
+ cancel_delayed_work_sync(&pool->compact_work);
zs_flush_migration(pool);
zs_pool_stat_destroy(pool);
@@ -2335,7 +2358,6 @@ void zs_destroy_pool(struct zs_pool *pool)
kfree(class);
}
- kfree(pool->name);
kfree(pool);
}
EXPORT_SYMBOL_GPL(zs_destroy_pool);
prev parent reply other threads:[~2026-12-03 4:52 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 15:02 [PATCH] mm/zsmalloc: remove shrinker Chris Wiseman
2026-12-01 16:45 ` Nhat Pham
2026-12-01 16:53 ` Yosry Ahmed
2026-12-02 0:46 ` Xueyuan Chen
2026-12-02 2:09 ` Sergey Senozhatsky
2026-12-02 7:04 ` Barry Song
2026-12-02 8:14 ` Sergey Senozhatsky
2026-12-02 10:21 ` Barry Song
2026-12-02 16:17 ` Chris Wiseman
2026-12-03 3:08 ` Sergey Senozhatsky
2026-12-03 3:24 ` Barry Song
2026-12-03 3:37 ` Sergey Senozhatsky
2026-12-03 4:24 ` Sergey Senozhatsky
2026-12-03 4:42 ` Chris Wiseman
2026-12-03 4:58 ` Sergey Senozhatsky
2026-12-03 4:52 ` Sergey Senozhatsky [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axDxrAla_MHEVROJ@google.com \
--to=senozhatsky@chromium.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=bgeffon@google.com \
--cc=chengming.zhou@linux.dev \
--cc=chriswiseman@google.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=nzzhao@126.com \
--cc=xueyuan.chen21@gmail.com \
--cc=xueyuan.chen@vivo.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox