Re: [PATCH v4 6/8] mm: zswap: Store large folios in batches.
From: Kanchana P. Sridhar
Date: Fri Nov 20 2026 - 18:51:28 EST
On Fri, Nov 20, 2026 at 3:25 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.
> >
> > 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.
> >
> > Signed-off-by: Kanchana P Sridhar <kanchana.p.sridhar@xxxxxxxxx>
> > Signed-off-by: Kanchana P. Sridhar <kanchanapsridhar2026@xxxxxxxxx>
> > ---
> > mm/zswap.c | 307 +++++++++++++++++++++++++++++++++++++----------------
> > 1 file changed, 217 insertions(+), 90 deletions(-)
> >
> > diff --git a/mm/zswap.c b/mm/zswap.c
> > index 78d9694f75b5..df59ab0c1029 100644
> > --- a/mm/zswap.c
> > +++ b/mm/zswap.c
> > @@ -85,6 +85,9 @@ static bool zswap_pool_reached_full;
> >
> > #define ZSWAP_PARAM_UNSET ""
> >
> > +/* Limit the batch size to limit per-CPU memory usage for dst buffers. */
> > +#define ZSWAP_MAX_BATCH_SIZE 4U
> > +
> > static int zswap_setup(void);
> >
> > /* Enable/disable zswap */
> > @@ -144,10 +147,12 @@ struct zswap_acomp_req {
> > struct mutex mutex;
> > };
> >
> > -/* The compression mutex also protects the output buffer. */
> > +/*
> > + * The compression mutex also protects the output buffer(s).
> > + */
> > struct zswap_comp_ctx {
> > struct zswap_acomp_req areq;
> > - u8 *buffer;
> > + u8 **buffers;
> > };
> >
> > /* Separate requests, so that decompression does not wait for compression. */
> > @@ -162,6 +167,7 @@ struct crypto_acomp_ctx {
> > * The only case where lru_lock is not acquired while holding tree.lock is
> > * when a zswap_entry is taken off the lru for writeback, in that case it
> > * needs to be verified that it's still valid in the tree.
> > + *
> > */
> > struct zswap_pool {
> > struct zs_pool *zs_pool;
> > @@ -226,6 +232,15 @@ struct zswap_entry {
> > struct list_head lru;
> > };
> >
> > +struct zswap_store_context {
> > + struct folio *folio;
> > + long folio_start;
> > + bool wb_enabled;
> > + int nid;
> > + u8 nr_pages;
> > + struct zswap_entry *entries[ZSWAP_MAX_BATCH_SIZE];
> > +};
> > +
> > /*
> > * No RCU section is needed around the returned pointer: a stored entry pins
> > * its pool via percpu_ref (taken in zswap_store_page()), so the id cannot be
> > @@ -284,6 +299,8 @@ static void __zswap_pool_empty(struct percpu_ref *ref);
> >
> > static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
> > {
> > + u8 i;
> > +
> > if (!acomp_ctx)
> > return;
> >
> > @@ -302,8 +319,12 @@ static void acomp_ctx_free(struct crypto_acomp_ctx *acomp_ctx)
> >
> > acomp_ctx->acomp = NULL;
> >
> > - kfree(acomp_ctx->comp.buffer);
> > - acomp_ctx->comp.buffer = NULL;
> > + if (acomp_ctx->comp.buffers) {
> > + for (i = 0; i < ZSWAP_MAX_BATCH_SIZE; ++i)
> > + kfree(acomp_ctx->comp.buffers[i]);
> > + kfree(acomp_ctx->comp.buffers);
> > + }
> > + acomp_ctx->comp.buffers = NULL;
> > }
> >
> > static struct zswap_pool *zswap_pool_create(char *compressor)
> > @@ -388,6 +409,7 @@ static struct zswap_pool *zswap_pool_create(char *compressor)
> > cpuhp_add_fail:
> > for_each_possible_cpu(cpu)
> > acomp_ctx_free(per_cpu_ptr(pool->acomp_ctx, cpu));
> > +
> > error:
> > if (pool->acomp_ctx)
> > free_percpu(pool->acomp_ctx);
> > @@ -778,6 +800,44 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)
> > kmem_cache_free(zswap_entry_cache, entry);
> > }
> >
> > +static inline void zswap_entries_cache_free_batch(
> > + struct zswap_entry **entries,
> > + u8 nr_entries)
> > +{
> > + /*
> > + * It is okay to use this to free entries allocated separately
> > + * by zswap_entry_cache_alloc().
> > + */
> > + kmem_cache_free_bulk(zswap_entry_cache, nr_entries, (void **)entries);
> > +}
> > +
> > +static bool zswap_entries_cache_alloc_batch(
> > + struct zswap_store_context *zc,
> > + gfp_t gfp)
> > +{
> > + u8 i;
> > +
> > + bool ret = kmem_cache_alloc_bulk(zswap_entry_cache, gfp,
> > + zc->nr_pages, (void **)zc->entries);
> > +
> > + if (likely(ret)) {
> > + for (i = 0; i < zc->nr_pages; ++i)
> > + zs_handle_init(&zc->entries[i]->handle);
> > + } else {
> > + for (i = 0; i < zc->nr_pages; ++i) {
> > + zc->entries[i] = zswap_entry_cache_alloc(GFP_KERNEL);
>
> Oh and why are we using GFP_KERNEL in the fallback path, but passing a
> gfp to kmem_cache_alloc_bulk above?
>
> (and it seems like the only caller of
> zswap_entries_cache_alloc_batch() passed GFP_KERNEL for gfp anyway?).
>
> Let's just drop gfp and use GFP_KERNEL if that's the intention?
Good catch! Makes sense, I will drop the gfp and use GFP_KERNEL in the next rev.
Thanks,
Kanchana