* Re: [PATCH] mm/zsmalloc: remove shrinker
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
` (3 subsequent siblings)
4 siblings, 0 replies; 17+ messages in thread
From: Nhat Pham @ 2026-12-01 16:45 UTC (permalink / raw)
To: Chris Wiseman
Cc: Andrew Morton, Minchan Kim, Sergey Senozhatsky, Johannes Weiner,
Yosry Ahmed, Chengming Zhou, Xueyuan Chen, Xueyuan Chen,
Barry Song, Brian Geffon, linux-mm, linux-kernel
On Tue, Dec 1, 2026 at 8:02 AM Chris Wiseman <chriswiseman@google.com> wrote:
>
> 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: Nhat Pham <nphamcs@gmail.com>
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
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
` (2 subsequent siblings)
4 siblings, 0 replies; 17+ messages in thread
From: Yosry Ahmed @ 2026-12-01 16:53 UTC (permalink / raw)
To: Chris Wiseman
Cc: Andrew Morton, Minchan Kim, Sergey Senozhatsky, Johannes Weiner,
Nhat Pham, Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Barry Song,
Brian Geffon, linux-mm, linux-kernel
On Tue, Dec 1, 2026 at 7:02 AM Chris Wiseman <chriswiseman@google.com> wrote:
>
> 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>
Acked-by: Yosry Ahmed <yosry@kernel.org>
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
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
4 siblings, 0 replies; 17+ messages in thread
From: Xueyuan Chen @ 2026-12-02 0:46 UTC (permalink / raw)
To: Chris Wiseman
Cc: Andrew Morton, Minchan Kim, Sergey Senozhatsky, Johannes Weiner,
Yosry Ahmed, Nhat Pham, Chengming Zhou, Xueyuan Chen,
Xueyuan Chen, Barry Song, Brian Geffon, linux-mm, linux-kernel
On 2026-12-01 15:02 +0000, Chris Wiseman wrote:
> 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>
> ---
> mm/zsmalloc.c | 76 ---------------------------------------------------
> 1 file changed, 76 deletions(-)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index f263bd2b8d46..17ef924f748b 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -38,7 +38,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>
> @@ -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;
>
> struct zs_pool_stats stats;
>
> - /* Compact classes */
> - struct shrinker *shrinker;
> -
> struct dentry *stat_dentry;
> #ifdef CONFIG_COMPACTION
> struct work_struct free_work;
> @@ -2115,62 +2109,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;
> @@ -2216,10 +2154,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.
> @@ -2297,14 +2231,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:
> @@ -2317,7 +2243,6 @@ void zs_destroy_pool(struct zs_pool *pool)
> {
> int i;
>
> - zs_unregister_shrinker(pool);
> zs_flush_migration(pool);
> zs_pool_stat_destroy(pool);
>
> @@ -2341,7 +2266,6 @@ void zs_destroy_pool(struct zs_pool *pool)
> kfree(class);
> }
>
> - kfree(pool->name);
> kfree(pool);
> }
> EXPORT_SYMBOL_GPL(zs_destroy_pool);
Reviewed-by: Xueyuan Chen <xueyuan.chen21@gmail.com>
>
> base-commit: d2e5fd43304200927d92c50ffa8bbe8420753f88
> --
> 2.47.0
>
>
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-01 15:02 [PATCH] mm/zsmalloc: remove shrinker Chris Wiseman
` (2 preceding siblings ...)
2026-12-02 0:46 ` Xueyuan Chen
@ 2026-12-02 2:09 ` Sergey Senozhatsky
2026-12-02 7:04 ` Barry Song
4 siblings, 0 replies; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-02 2:09 UTC (permalink / raw)
To: Chris Wiseman
Cc: Andrew Morton, Minchan Kim, Sergey Senozhatsky, Johannes Weiner,
Yosry Ahmed, Nhat Pham, Chengming Zhou, Xueyuan Chen,
Xueyuan Chen, Barry Song, Brian Geffon, linux-mm, linux-kernel
On (26/12/01 15:02), Chris Wiseman wrote:
> 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>
Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-01 15:02 [PATCH] mm/zsmalloc: remove shrinker Chris Wiseman
` (3 preceding siblings ...)
2026-12-02 2:09 ` Sergey Senozhatsky
@ 2026-12-02 7:04 ` Barry Song
2026-12-02 8:14 ` Sergey Senozhatsky
4 siblings, 1 reply; 17+ messages in thread
From: Barry Song @ 2026-12-02 7:04 UTC (permalink / raw)
To: Chris Wiseman
Cc: Andrew Morton, Minchan Kim, Sergey Senozhatsky, Johannes Weiner,
Yosry Ahmed, Nhat Pham, Chengming Zhou, Xueyuan Chen,
Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel
On Tue, Dec 1, 2026 at 11:02 PM Chris Wiseman <chriswiseman@google.com> wrote:
>
> 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.
Hi Chris, Xueyuan and all,
Thanks for the patch and the report. Just curious, do we need
userspace-triggered proactive compaction for zram on Android after
this patch? I mean, do you have any userspace code that triggers
compaction via /sys/block/zram<id>/compact?
Thanks,
Barry
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-02 7:04 ` Barry Song
@ 2026-12-02 8:14 ` Sergey Senozhatsky
2026-12-02 10:21 ` Barry Song
0 siblings, 1 reply; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-02 8:14 UTC (permalink / raw)
To: Barry Song
Cc: Chris Wiseman, Andrew Morton, Minchan Kim, Sergey Senozhatsky,
Johannes Weiner, Yosry Ahmed, Nhat Pham, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel
On (26/12/02 15:04), Barry Song wrote:
> > 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.
>
> Hi Chris, Xueyuan and all,
>
> Thanks for the patch and the report. Just curious, do we need
> userspace-triggered proactive compaction
Wouldn't hurt to keep it around, I guess. But maybe can NOOP it
in the future (removing zram device attributes is a bit difficult,
we usually give around 3-5 years of deprecation cooldown period
before removal.)
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-02 8:14 ` Sergey Senozhatsky
@ 2026-12-02 10:21 ` Barry Song
2026-12-03 3:08 ` Sergey Senozhatsky
0 siblings, 1 reply; 17+ messages in thread
From: Barry Song @ 2026-12-02 10:21 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Chris Wiseman, Andrew Morton, Minchan Kim, Johannes Weiner,
Yosry Ahmed, Nhat Pham, Chengming Zhou, Xueyuan Chen,
Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel
On Wed, Dec 2, 2026 at 4:14 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> On (26/12/02 15:04), Barry Song wrote:
> > > 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.
> >
> > Hi Chris, Xueyuan and all,
> >
> > Thanks for the patch and the report. Just curious, do we need
> > userspace-triggered proactive compaction
>
> Wouldn't hurt to keep it around, I guess. But maybe can NOOP it
> in the future (removing zram device attributes is a bit difficult,
> we usually give around 3-5 years of deprecation cooldown period
> before removal.)
Hi Sergey,
Thanks for your reply. Sorry, I might not have expressed my question
clearly. Here is what I meant:
We used to have automatic compaction through the shrinker. Now that
we have removed it, do we still expect compaction to be triggered,
either automatically or manually by writing to the zram sysfs
attribute?
I know the changelog already says:
" 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."
But what about periods when swap-out is not active? Would compaction
still be beneficial in those cases?
Thanks,
Barry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-02 10:21 ` Barry Song
@ 2026-12-03 3:08 ` Sergey Senozhatsky
2026-12-03 3:24 ` Barry Song
0 siblings, 1 reply; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 3:08 UTC (permalink / raw)
To: Barry Song
Cc: Sergey Senozhatsky, Chris Wiseman, Andrew Morton, Minchan Kim,
Johannes Weiner, Yosry Ahmed, Nhat Pham, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel
Hi Barry,
On (26/12/02 18:21), Barry Song wrote:
> > > Hi Chris, Xueyuan and all,
> > >
> > > Thanks for the patch and the report. Just curious, do we need
> > > userspace-triggered proactive compaction
> >
> > Wouldn't hurt to keep it around, I guess. But maybe can NOOP it
> > in the future (removing zram device attributes is a bit difficult,
> > we usually give around 3-5 years of deprecation cooldown period
> > before removal.)
>
> Hi Sergey,
>
> Thanks for your reply. Sorry, I might not have expressed my question
> clearly. Here is what I meant:
Oh, my bad!
> We used to have automatic compaction through the shrinker. Now that
> we have removed it, do we still expect compaction to be triggered,
> either automatically or manually by writing to the zram sysfs
> attribute?
>
> I know the changelog already says:
>
> " 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."
>
> But what about periods when swap-out is not active? Would compaction
> still be beneficial in those cases?
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.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 3:08 ` Sergey Senozhatsky
@ 2026-12-03 3:24 ` Barry Song
2026-12-03 3:37 ` Sergey Senozhatsky
0 siblings, 1 reply; 17+ messages in thread
From: Barry Song @ 2026-12-03 3:24 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Chris Wiseman, Andrew Morton, Minchan Kim, Johannes Weiner,
Yosry Ahmed, Nhat Pham, Chengming Zhou, Xueyuan Chen,
Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel, Nanzhe Zhao
On Thu, Dec 3, 2026 at 11:08 AM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> Hi Barry,
>
> On (26/12/02 18:21), Barry Song wrote:
> > > > Hi Chris, Xueyuan and all,
> > > >
> > > > Thanks for the patch and the report. Just curious, do we need
> > > > userspace-triggered proactive compaction
> > >
> > > Wouldn't hurt to keep it around, I guess. But maybe can NOOP it
> > > in the future (removing zram device attributes is a bit difficult,
> > > we usually give around 3-5 years of deprecation cooldown period
> > > before removal.)
> >
> > Hi Sergey,
> >
> > Thanks for your reply. Sorry, I might not have expressed my question
> > clearly. Here is what I meant:
>
> Oh, my bad!
>
> > We used to have automatic compaction through the shrinker. Now that
> > we have removed it, do we still expect compaction to be triggered,
> > either automatically or manually by writing to the zram sysfs
> > attribute?
> >
> > I know the changelog already says:
> >
> > " 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."
> >
> > But what about periods when swap-out is not active? Would compaction
> > still be beneficial in those cases?
>
> 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.
So it seems that while mid-swap compaction is undesirable from a
performance perspective, it does help reduce the memory footprint
on phones.
I agree that once we remove mid-swap compaction, we should trigger
compaction from userspace, either after killing some apps or after
proactive swap-out.
Thanks,
Barry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 3:24 ` Barry Song
@ 2026-12-03 3:37 ` Sergey Senozhatsky
2026-12-03 4:24 ` Sergey Senozhatsky
0 siblings, 1 reply; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 3:37 UTC (permalink / raw)
To: Barry Song
Cc: Sergey Senozhatsky, Chris Wiseman, Andrew Morton, Minchan Kim,
Johannes Weiner, Yosry Ahmed, Nhat Pham, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel,
Nanzhe Zhao
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
configurable threshold (e.g. 5%).
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
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:52 ` Sergey Senozhatsky
0 siblings, 2 replies; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 4:24 UTC (permalink / raw)
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, linux-kernel, Nanzhe Zhao,
Sergey Senozhatsky
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));
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
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
1 sibling, 1 reply; 17+ messages in thread
From: Chris Wiseman @ 2026-12-03 4:42 UTC (permalink / raw)
To: Sergey Senozhatsky, Barry Song, Nanzhe Zhao, Andrew Morton
Cc: Minchan Kim, Johannes Weiner, Yosry Ahmed, Nhat Pham,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Brian Geffon,
linux-mm, linux-kernel, Chris Wiseman
Hi Sergey, Barry, Nanzhe, Andrew,
Barry, Nanzhe, thanks for the Pixel 6 numbers. 32 MB to 182 MB median
(622 MB peak) is a real regression, so the shrinker removal shouldn't go
in alone.
Sergey, moving compaction to a background worker gated on
zs_pool_can_compact() looks good to me. Three quick things on the
sketch:
1. Can we make compact_threshold writable after init (like mem_limit)
instead of returning -EBUSY? Android needs to tune it live without
resetting zram. That'll just need WRITE_ONCE() in the store and
READ_ONCE() in zram_schedule_compaction(), since slot_free() doesn't
hold dev_lock.
2. If it defaults to 0, removing the shrinker regresses anyone who
doesn't know the knob exists. We should pick a non-zero default from
data rather than guessing. Barry, Nanzhe, could you test 5 and 10 on
that same Pixel 6 loop?
3. The 10-second timer doesn't know about memory pressure, whereas the
shrinker ran when reclaim wanted pages. Not a blocker, just the first
thing I'd watch if the Pixel numbers don't come back down.
Andrew, please drop mm-zsmalloc-remove-shrinker.patch from mm-new for
now. Once we have the Pixel 6 numbers, we can send a 2-patch series
with Sergey's patch first and the shrinker removal second so git bisect
doesn't hit a regressing commit. Sergey, happy to carry your patch or
rebase mine on top of yours, whichever you prefer.
Thanks,
Chris
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 4:42 ` Chris Wiseman
@ 2026-12-03 4:58 ` Sergey Senozhatsky
0 siblings, 0 replies; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 4:58 UTC (permalink / raw)
To: Chris Wiseman
Cc: Sergey Senozhatsky, Barry Song, Nanzhe Zhao, Andrew Morton,
Minchan Kim, Johannes Weiner, Yosry Ahmed, Nhat Pham,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Brian Geffon,
linux-mm, linux-kernel
On (26/12/03 04:42), Chris Wiseman wrote:
> Hi Sergey, Barry, Nanzhe, Andrew,
>
> Barry, Nanzhe, thanks for the Pixel 6 numbers. 32 MB to 182 MB median
> (622 MB peak) is a real regression, so the shrinker removal shouldn't go
> in alone.
>
> Sergey, moving compaction to a background worker gated on
> zs_pool_can_compact() looks good to me. Three quick things on the
> sketch:
>
> 1. Can we make compact_threshold writable after init (like mem_limit)
> instead of returning -EBUSY? Android needs to tune it live without
> resetting zram. That'll just need WRITE_ONCE() in the store and
> READ_ONCE() in zram_schedule_compaction(), since slot_free() doesn't
> hold dev_lock.
Sure. I didn't want to complicate the patch. It can be run-time
tunable.
> 2. If it defaults to 0, removing the shrinker regresses anyone who
> doesn't know the knob exists. We should pick a non-zero default from
> data rather than guessing. Barry, Nanzhe, could you test 5 and 10 on
> that same Pixel 6 loop?
Right. I guess 5 is a good default.
> 3. The 10-second timer doesn't know about memory pressure, whereas the
> shrinker ran when reclaim wanted pages. Not a blocker, just the first
> thing I'd watch if the Pixel numbers don't come back down.
So that 10 seconds cooldown is there because those slot-free calls don't
come alone, usually. It's more likely to be a burst of slot-free's and
10 seconds wait kind of tries to space them (burst and compaction) out.
> Andrew, please drop mm-zsmalloc-remove-shrinker.patch from mm-new for
> now. Once we have the Pixel 6 numbers, we can send a 2-patch series
> with Sergey's patch first and the shrinker removal second so git bisect
> doesn't hit a regressing commit. Sergey, happy to carry your patch or
> rebase mine on top of yours, whichever you prefer.
I think your patch will go in as is, no need to rebase, the remaining
work will stack on top of it. I guess we want shrinker replacement,
just need to figure out what do we replace it with.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 4:24 ` Sergey Senozhatsky
2026-12-03 4:42 ` Chris Wiseman
@ 2026-12-03 4:52 ` Sergey Senozhatsky
2026-12-03 5:10 ` Barry Song
1 sibling, 1 reply; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 4:52 UTC (permalink / raw)
To: Barry Song, Nhat Pham, Yosry Ahmed
Cc: Chris Wiseman, Andrew Morton, Minchan Kim, Johannes Weiner,
Chengming Zhou, Xueyuan Chen, Xueyuan Chen, Brian Geffon,
linux-mm, linux-kernel, Nanzhe Zhao, Sergey Senozhatsky
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);
^ permalink raw reply related [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 4:52 ` Sergey Senozhatsky
@ 2026-12-03 5:10 ` Barry Song
2026-12-03 6:17 ` Sergey Senozhatsky
0 siblings, 1 reply; 17+ messages in thread
From: Barry Song @ 2026-12-03 5:10 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Nhat Pham, Yosry Ahmed, Chris Wiseman, Andrew Morton, Minchan Kim,
Johannes Weiner, Chengming Zhou, Xueyuan Chen, Xueyuan Chen,
Brian Geffon, linux-mm, linux-kernel, Nanzhe Zhao
On Thu, Dec 3, 2026 at 12:52 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> On (26/12/03 13:24), Sergey Senozhatsky wrote:
> [..]
>
> 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);
> }
> }
Hi Sergey,
Thanks very much for the quick patch! I'm always amazed by how
quickly you come up with patches :-)
For both zram and zsmalloc deferred compaction, the kick is currently
in the free path (zs_free() or slot_free()). I wonder whether we
also need to kick compaction from the allocation path.
For example, if userspace triggers proactive swap-out but no swap-in
has occurred yet, would we miss the opportunity to kick compaction?
I think compaction after proactive swap-out (e.g., when an app moves
to the background) could be quite beneficial. It could also help
reduce future memory pressure by freeing physical pages earlier,
rather than deferring all automatic compaction until memory pressure
arises, as we currently do in mainline.
Thanks,
Barry
^ permalink raw reply [flat|nested] 17+ messages in thread* Re: [PATCH] mm/zsmalloc: remove shrinker
2026-12-03 5:10 ` Barry Song
@ 2026-12-03 6:17 ` Sergey Senozhatsky
0 siblings, 0 replies; 17+ messages in thread
From: Sergey Senozhatsky @ 2026-12-03 6:17 UTC (permalink / raw)
To: Barry Song
Cc: Sergey Senozhatsky, Nhat Pham, Yosry Ahmed, Chris Wiseman,
Andrew Morton, Minchan Kim, Johannes Weiner, Chengming Zhou,
Xueyuan Chen, Xueyuan Chen, Brian Geffon, linux-mm, linux-kernel,
Nanzhe Zhao
Hi Barry,
On (26/12/03 13:10), Barry Song wrote:
> > 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);
> > }
> > }
>
> Hi Sergey,
>
> Thanks very much for the quick patch! I'm always amazed by how
> quickly you come up with patches :-)
:)
> For both zram and zsmalloc deferred compaction, the kick is currently
> in the free path (zs_free() or slot_free()). I wonder whether we
> also need to kick compaction from the allocation path.
So allocation path doesn't create fragmentation. It adds a new zspage,
but only if other class zspages are full. And that new zspage with one
active element is the worst case, but there is nothing to compact: we
have all full zspages and one nearly empty zspage. Am I missing the
point you are trying to make?
^ permalink raw reply [flat|nested] 17+ messages in thread