From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796271888; b=BAIe4jfdHKBW4/x6f3Yvsi2Tb/aPFRYnSeZy1qTbFePsRhjNC2BHhmJRbZcVcjWs1CAl Fcw5t9zloUdffq1RIYEMgKGg/sYdYamfsoKjNthqpydmQrO9ZPdAGl/bbOTjZyGS0bWAg elp01jI9i+UA7gjkXgAkZSbfwK33iO4is2JWNOhKPGeLPRNOtdIY4U39a1+eDmi93b5K0 L6YihSrrC8g6fBdZICe/uFRQeJT/05xVBaCsDD4CBe5ydwvoFPq5+SegIiJnYV8RJxzHb YpwPWfGiP16bXHTUNLBHSx6l1WuKXgWYE5GyaRotg3EkFz28Ktuqc4UR8kf0wttpjVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796271888; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=X5kRFK67OFhyLVkPCARAegu4WBwyizYtEoR6vMCQXkI=; b=PxMp+jmqC5n8x4t8ayAlJsSBleNfkUTOIk4Hj+7j4fOB8OBgSbXM7l+pDKsMTLbzQ/mq YSfrgqX8Lybq6UW5wITBpop3t/dMxzOqhLxXiHsZCviguyDp8UYedGX89yZSrRAtMSmmv jLYu+igOdquCDuJxMmWKe6Ww0GG5UyKTICvjirbAypIKWfJhZ1sHIsttXe019RK18OQOl SEax6jVRMupYuLgb11cTsYoE/5aobjx3vXt80aKozJ8l+fQvl19HTZJV/U+bjziVCgpzP i+SMrrE69WlHaxPat6t+asLUdS23+OTqdapQRV/qvFoGm+4FmCNXgMKgjtYR4Z70yXQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b="ik9Fp/cN"; arc=none smtp.remote-ip=74.125.82.53 Received: from mail-dl1-f53.google.com (mail-dl1-f53.google.com [74.125.82.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9F40606A32C for ; Thu, 3 Dec 2026 04:24:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B9F40606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=ik9Fp/cN Received: by mail-dl1-f53.google.com with SMTP id a92af1059eb24-1b589050416so7525130c88.0 for ; Wed, 02 Dec 2026 20:24:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1796271887; x=1796876687; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=X5kRFK67OFhyLVkPCARAegu4WBwyizYtEoR6vMCQXkI=; b=ik9Fp/cN5tu9yvPatlP5hnr57ncYMC4oXP5mTkt2XUp58pOSy+26qbGkkYLDnX7dUg pfVbzQiKSrfVpCeOssVurKfNsgAwVBb9wIcSR49dzQhI5Le5GKWwA0DaDh3+y3917Pkx /hoeP7XJfNe3Sp5EVUN4CDPXR00tK3KUUhBAs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796271887; x=1796876687; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=X5kRFK67OFhyLVkPCARAegu4WBwyizYtEoR6vMCQXkI=; b=2FmdJidp5+z7YESWAklKmZUd7jpaSL/+/53r16H3NjYwChomByQt89RQpCY8d0EISD Wc13YcB7rr1emI0x6KS1Q/Iazl60mkD1KIFiCixR+ulVKpduViSXi1RQqeTmaznfCwnJ UNfray4YNMK7Y/+rYdWLhStzf3kxiolbLO/ksP1PPhWOctwBIaa+jNsFsA0i09sje+NG qnQGAa32CK4O39+1R6BlBZLoHxdz9l4U1U29LyUOr41efOkltI3rJ2aLgyZ80Cmk+OFt +9I5q3EQLC9+jcvupFZnNiW3Te5SHxsKvZVcPmbBv/mKDRjKVADtAIjYbryfMek+WXeV qWNA== X-Forwarded-Encrypted: i=1; AL+hErH8kwb6yB+cY0WjjLeyZH8r5ZbOgAmRjZtNjelVy399MXvRXmBL32iTIrDrCTHgwQH27P0751vQQQ6v2TQ=@vger.kernel.org X-Gm-Message-State: APuU4Z/csasumg0+D5bA8jSTNwPavt3OnBC7QjueDW+y6DfxzgOKmp2+ Pn/lo3r5538/PFpmt2W1EelCNJouMd2Bya/+4u9yXqOQ7Vv7vpaxpgYrWdi+lvBfpkpicbWGjRI 1uig= X-Gm-Gg: AZFdTZfh59h0pIw4Y+OtvhRLe1jnL5wQOfoisp/mD0QpVmnI/rH6AOadJdpAl0dyOff hKctdumThbh3/WlyYo0B4mXdZ6wFOi2LMNVJYV3f16CdjEtKHM2VDuyu2wTC0hN7X6jyUsA7nqS L0JRuLUdGWye4mk57ZaS5bFwOCw35ywhw5ZOQr8U6V+jE/HAVqxRvJRK3xKEXZ5VnOORLI3qdjp VJ/MoyLIe+TtIM4rOm6aFNdxXWy0a83m3Yia7EJ5d1pNcge/zFFoHDFf9YYZgmrmRGW1sGFERJg LJNxyf1GPA4i549odNqySOuw4lc5afzFDrrOQKI7gyK9i4cx+F/8njcquzYfwAEK8rXkT+QGM/k kL4TyxJMOTrLdeRkLWLLIv+CU3YOBU9RkQV7wuYF2dGJxqB1pE4lxgflUrH5S86aVmsodE57FZ6 Ozj5rHLN7BirfEt9W1r1eXbIWfINFFgxooL2aca5sF/NWnvTHxwT63xl0Kj1DYzjumLdK0FjAr4 wbj1tO0zbhDyNLucAW1LBTih9UPC0Hi/k0gUfavFMFXIkXJJT0w937rILfAb8fuwitsvrh8xnJD 8X2q0NZP1YnDNzu80/+pWDjReLlme03YXsJMJSQW8FsX2mxTMqs3r7bS9dE5RSctPUi6yeHoLdV hZZpGN5eaCuZ4mw== X-Received: by 2002:a05:701b:2816:b0:1b5:3b83:5374 with SMTP id a92af1059eb24-1b5568cfbd9mr39449066c88.19.1796271886781; Wed, 02 Dec 2026 20:24:46 -0800 (PST) Received: from google.com ([2a00:79e0:2031:6:e18c:e957:aa04:7114]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b556847fdasm109279296c88.13.2026.12.02.20.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 20:24:46 -0800 (PST) Date: Thu, 3 Dec 2026 13:24:21 +0900 From: Sergey Senozhatsky To: Barry Song Cc: Chris Wiseman , Andrew Morton , Minchan Kim , Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Xueyuan Chen , Xueyuan Chen , Brian Geffon , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Nanzhe Zhao , Sergey Senozhatsky Subject: Re: [PATCH] mm/zsmalloc: remove shrinker Message-ID: References: <18ed2967f99a2eff.3137322e32332e3235332e313736.2c2157.0000sendgmr@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 #include #include +#include #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 #include #include +#include #include #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));