Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Barry Song <baohua@kernel.org>
Cc: Chris Wiseman <chriswiseman@google.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Minchan Kim <minchan@kernel.org>,
	 Johannes Weiner <hannes@cmpxchg.org>,
	Yosry Ahmed <yosry@kernel.org>, Nhat Pham <nphamcs@gmail.com>,
	 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:24:21 +0900	[thread overview]
Message-ID: <axDknU_Jp0asc1nS@google.com> (raw)
In-Reply-To: <axDhwDyXvP7jRmoP@google.com>

On (26/12/03 12:37), Sergey Senozhatsky wrote:
> On (26/12/03 11:24), Barry Song wrote:
> [..]
> > > I suppose compaction will be beneficial after a huge app swap-in, e.g.
> > > bringing back a web browser with (usual) million tabs.  Or after a huge
> > > app kill/termination, if the app had swap presence.  Both of these
> > > release memory from zsmalloc and create fragmentation, so we might
> > > benefit from doing a de-fragmentation.
> > 
> > Right. I had Nanzhe(Cc-ed) test this patch on Pixel 6 phones by repeatedly
> > launching apps in a loop. We observed some negative results with
> > this patch in the absence of proactive compaction.
> > 
> > Nanzhe collected the time series of (mem_used_total - compr_data_size).
> > Without the patch, the median was 32 MB. With the patch, it increased
> > to 182 MB, peaking at 622 MB.
> > 
> > The median memory overhead due to fragmentation increased from
> > 2.5% without the patch to 16.87% with the patch.
> 
> Hmm that's a high number...
> 
> > So it seems that while mid-swap compaction is undesirable from a
> > performance perspective, it does help reduce the memory footprint
> > on phones.
> 
> A bit of problem is that you need memory pressure to trigger compaction.
> On a device, which does proactive per-app swap outs w/o hitting memory
> pressure, the fragmentation footprint just sits there (and maybe even
> grows).
> 
> 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.

Opinions?

----

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..4d410c918ef1 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -29,6 +29,7 @@
 #include <linux/scatterlist.h>
 #include <linux/kernel_read_file.h>
 #include <linux/rcupdate.h>
+#include <linux/jiffies.h>
 
 #include "zram_drv.h"
 
@@ -40,6 +41,7 @@ static int zram_major;
 static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
 
 #define ZRAM_MAX_ALGO_NAME_SZ	128
+#define ZRAM_COMPACT_DELAY	(10 * HZ)
 
 /* Module params (documentation at end) */
 static unsigned int num_devices = 1;
@@ -1948,6 +1950,28 @@ static ssize_t recomp_algorithm_store(struct device *dev,
 }
 #endif
 
+static void zram_schedule_compaction(struct zram *zram)
+{
+	if (!zram->compact_threshold)
+		return;
+	if (delayed_work_pending(&zram->compact_work))
+		return;
+
+	queue_delayed_work(system_dfl_long_wq, &zram->compact_work,
+			   ZRAM_COMPACT_DELAY);
+}
+
+static void zram_compact_work_fn(struct work_struct *work)
+{
+	struct zram *zram = container_of(work, struct zram, compact_work.work);
+
+	guard(rwsem_read)(&zram->dev_lock);
+	if (!init_done(zram) || !zram->compact_threshold)
+		return;
+
+	zs_compact_threshold(zram->mem_pool, zram->compact_threshold);
+}
+
 static ssize_t compact_store(struct device *dev, struct device_attribute *attr,
 			     const char *buf, size_t len)
 {
@@ -1962,6 +1986,38 @@ 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;
+
+	if (kstrtouint(buf, 10, &val) || val >= 100)
+		return -EINVAL;
+
+	guard(rwsem_write)(&zram->dev_lock);
+	if (init_done(zram))
+		return -EBUSY;
+
+	zram->compact_threshold = val;
+
+	return len;
+}
+
 static ssize_t io_stat_show(struct device *dev, struct device_attribute *attr,
 			    char *buf)
 {
@@ -2120,6 +2176,7 @@ static void slot_free(struct zram *zram, unsigned long index)
 
 	zram_stat_sub(zram, ZRAM_STAT_COMPR_DATA_SIZE,
 		      get_slot_size(zram, index));
+	zram_schedule_compaction(zram);
 out:
 	zram_stat_sub(zram, ZRAM_STAT_PAGES_STORED, 1);
 	zram->table[index].element = 0;
@@ -2895,9 +2952,12 @@ static void zram_reset_device(struct zram *zram)
 {
 	int cpu;
 
+	disable_delayed_work_sync(&zram->compact_work);
+
 	guard(rwsem_write)(&zram->dev_lock);
 
 	zram->limit_pages = 0;
+	zram->compact_threshold = 0;
 
 	set_capacity_and_notify(zram->disk, 0);
 	part_stat_set_all(zram->disk->part0, 0);
@@ -2917,6 +2977,7 @@ static void zram_reset_device(struct zram *zram)
 	zram->wb_batch_size = 32;
 	zram->compressed_wb = false;
 #endif
+	enable_delayed_work(&zram->compact_work);
 }
 
 static ssize_t disksize_store(struct device *dev, struct device_attribute *attr,
@@ -3038,6 +3099,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 +3127,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 +3203,7 @@ static int zram_add(void)
 	device_id = ret;
 
 	init_rwsem(&zram->dev_lock);
+	INIT_DELAYED_WORK(&zram->compact_work, zram_compact_work_fn);
 #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..7f8223d3d490 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -12,6 +12,7 @@
 #include <asm/byteorder.h>
 #include <asm/local64.h>
 #include <linux/rwsem.h>
+#include <linux/workqueue.h>
 #include <linux/zsmalloc.h>
 
 #include "zcomp.h"
@@ -143,6 +144,8 @@ struct zram {
 	 * the number of pages zram can consume for storing compressed data
 	 */
 	unsigned long limit_pages;
+	unsigned int compact_threshold;
+	struct delayed_work compact_work;
 
 	struct zram_stats stats;
 	struct zram_pcpu_stats __percpu *pcpu_stats;
diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h
index 3232bc9c33b2..9bb544a48e47 100644
--- a/include/linux/zsmalloc.h
+++ b/include/linux/zsmalloc.h
@@ -54,6 +54,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_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..5cbb8cb34a25 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -541,18 +541,18 @@ 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 void __init zs_stat_init(void)
@@ -2017,6 +2017,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_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),
@@ -2103,6 +2111,44 @@ unsigned long zs_compact(struct zs_pool *pool)
 }
 EXPORT_SYMBOL_GPL(zs_compact);
 
+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;
+}
+
+void zs_compact_threshold(struct zs_pool *pool, unsigned int threshold)
+{
+	unsigned long total_pages;
+	unsigned long pages_to_free;
+
+	if (!threshold || threshold >= 100 ||
+	    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);
+}
+EXPORT_SYMBOL_GPL(zs_compact_threshold);
+
 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
 {
 	memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));


  reply	other threads:[~2026-12-03  4:24 UTC|newest]

Thread overview: 15+ 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 [this message]
2026-12-03  4:42               ` Chris Wiseman
2026-12-03  4:52               ` Sergey Senozhatsky

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=axDknU_Jp0asc1nS@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