Re: [PATCH v4 6/8] mm: zswap: Store large folios in batches.
From: Kanchana P. Sridhar
Date: Fri Nov 20 2026 - 18:40:35 EST
On Fri, Nov 20, 2026 at 3:08 PM Nhat Pham <nphamcs@xxxxxxxxx> wrote:
>
> On Thu, Nov 19, 2026 at 8:19 PM Kanchana P. Sridhar
> <kanchanapsridhar2026@xxxxxxxxx> wrote:
> >
> > Use batching when storing large folios in zswap. A new constant,
> > ZSWAP_MAX_BATCH_SIZE is defined as 4U to support processing batches of
> > up to 4 pages at a time while storing large folios. This commit
> > modifies the "store single page" approach to "store a batch of pages".
> >
> > As part of CPU onlining per-CPU acomp_ctx setup, ZSWAP_MAX_BATCH_SIZE
> > compression buffers are allocated to eventually be able to compress and
> > store ZSWAP_MAX_BATCH_SIZE pages. This happens in subsequent commits.
> >
> > For existing software compressors, the extra overhead is the
> > the extra 'buffers' pointer, 3 additional elements in 'buffers' and 3
> > additional 4K space per 'buffers[i]': in all ~12 KB per-CPU on
> > x86_64. Only the first buffer is currently used.
>
> It's not a lot but do we need multiple buffers here for all software
> compressors? Other than the hardware accelerated compressors,
> compression happens sequentially, no?
That's true, compression happens sequentially. However, multiple
buffers here helps software compressors because it allows the
"batch-centric" compute and lean working set concept, which results in
the sys time improvements (cache locality, fewer cache evictions).
Hence, I think multiple buffers here benefit latency.
>
>
> >
> > zswap_store_page() is replaced with zswap_store_pages(), which processes
> > a batch of pages and allows for batching optimizations. For now, only
> > optimize allocating entries by using batch allocations from the slab
> > cache.
> >
> > Avoid repeatedly calling mem_cgroup_zswap_writeback_enabled() for every
> > page and only call it once for the folio, since the entire folio is
> > charged to a single memcg. Similarly, obtain the nid once to use during
> > each batch store of a folio. To facilitate this, a "struct
> > zswap_store_context" is introduced to keep function parameter lists
> > concise.
>
> The wb_enabled stuff sounds like sth you can pull into its own patch?
Do you mean we should obtain wb_enabled status once and pass it to
zswap_store_page(), and factor this into a patch before patch 6 in
this series? Sure, I can do this.
>
> [...]
>
> > - /*
> > - * We may have had an existing entry that became stale when
> > - * the folio was redirtied and now the new version is being
> > - * swapped out. Get rid of the old.
> > - */
> > - if (old)
> > - zswap_entry_free(old);
> > + for (i = 0; i < zc->nr_pages; ++i) {
> > + struct zswap_entry *old, *entry = zc->entries[i];
> > +
> > + old = xa_store(swap_zswap_tree(entry->swpentry),
> > + swp_offset(entry->swpentry),
> > + entry, GFP_KERNEL);
>
> This range is guaranteed to be contiguous and always belong to the
> same zswap tree right? Why are we not using xas API here?
>
> (Or maybe we can do this as follow-up/in a different patch?)
Great point. I was looking into this based on your earlier comment,
and was studying the repercussions of obtaining the xa_lock once,
followed by __xa_store() or xas() API, but still needed to figure out
a few things, hence tabled this for a subsequent patch:
- Impact on zswap loads from being locked out for significantly more
time, potentially causing page fault handling errors.
- Impact of holding the xa_lock for 4 entries at a time during zswap
stores: would it result in errors due to the task blocked in an atomic
path.
- Ascertaining if the assumption is true: contiguous range of offsets
is guaranteed to always belong to the same xarray.
- Comprehend tree rebalancing with xas approach, would it change the
above assumption.
This seemed to require some more focus, and deserving of a patch of
its own. I agree, we should pursue this in a follow-up patch.
Thanks,
Kanchana