Re: [RFC PATCH 0/4] zsmalloc: replace shrinker with deferred compaction

From: Nanzhe Zhao

Date: Sat Dec 05 2026 - 03:58:59 EST


> pages_to_free = zs_pool_can_compact(pool);
> if ((u64)pages_to_free * 100 < (u64)total_pages * threshold)
> goto backoff;
...
>backoff:
> if (shift < ZS_COMPACT_MAX_DEFER_SHIFT)
> WRITE_ONCE(pool->compact_defer_shift, shift + 1);

Is increasing the delay every time the threshold is not met a bit too
aggressive?

I tested this on Pixel 6 with a shorter workload. Sampling the wasted memory
(mem_used_total - compr_data_size) time series, the dynamic delay (5s-80s)
gave a median waste of ~33 MB and P90 of ~127 MB (P99 ~217 MB). With a fixed
5s delay, the median stayed at ~18 MB with a P90 of ~47 MB (P99 ~109 MB), and
with 1s it was ~15 MB with a P90 of ~31 MB.

What happened was that in about 62% of the executions, the work ran when pool
fragmentation was still below the 5% threshold, taking the backoff branch.
Because so many checks found the pool below threshold, the delay quickly
climbed to the 80s ceiling (about a quarter of all queues were at 40s-80s).
As a result of staying at 80s, waste above 100 MB lingered for 15.4% of the
run time on average (ranging 9% to 27%, lasting 14-71s). By contrast, under
fixed 5s waste above 100 MB only lasted 1.1% of the time (2-3s episodes), and
under 1s it was 0.3%.

Looking at the queueing side:

>static void maybe_schedule_compact(struct zs_pool *pool,
> struct size_class *class)
>{
> if (!READ_ONCE(pool->compact_threshold))
> return;
> if (delayed_work_pending(&pool->compact_work))
> return;
> if (!zs_class_can_compact(class))
> return;
>
> delay = ZS_COMPACT_DELAY << READ_ONCE(pool->compact_defer_shift);
> queue_delayed_work(system_dfl_long_wq, &pool->compact_work, delay);
>}

Could we also check the pool fragmentation threshold at queue time before
queueing the work, and only queue when the threshold is reached?

In our data, about 75% of the works were queued when pool-wide fragmentation
was actually below 5%. Under active churn, zs_class_can_compact(class) almost
always passes for some class. These premature queues are the direct source
of why the queued works repeatedly run, find the threshold not met, and
rapidly push the delay to 80s.

Perhaps we could consider a dual-watermark design? For example, check against
a lower watermark at queue time to avoid scheduling premature work, and check
against a higher watermark when the work runs before actually compacting.

What do you think?

Thanks,
Nanzhe