From: sashiko-bot@kernel.org
To: "Kanchana P. Sridhar" <kanchanapsridhar2026@gmail.com>,
hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com,
chengming.zhou@linux.dev, akpm@linux-foundation.org,
linux-mm@kvack.org, herbert@gondor.apana.org.au,
davem@davemloft.net, linux-crypto@vger.kernel.org
Cc: rientjes@google.com, willy@infradead.org, senozhatsky@chromium.org
Subject: Re: [PATCH v4 8/8] mm: zswap: Batched zswap_compress() for storing large folios in batches.
Date: Sat, 21 Nov 2026 04:20:06 +0000 [thread overview]
Message-ID: <sashiko-outbox-226638@kernel.org> (raw)
In-Reply-To: <20261120041906.5785-9-kanchanapsridhar2026@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: zswap: invalid and unreset dst scatterlist lengths in
zswap_compress()
- [High] mm: zswap: unterminated dst scatterlist slice passed in
zswap_compress()
- [Medium] mm: zswap: unsigned sg->length check and error accounting in
zswap_compress()
--
commit a77cc3e636d5134b6371aa977bb1025c031583c2
Author: Kanchana P. Sridhar <kanchanapsridhar2026@gmail.com>
mm: zswap: Batched zswap_compress() for storing large folios in batches.
This commit replaces the per-page zswap_compress() call in
zswap_store_pages() with a batched zswap_compress() and
zswap_update_entries() that compress up to ZSWAP_MAX_BATCH_SIZE pages per
batch using a per-CPU scatterlist table mapped to destination buffers.
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 5e1fb5624ffb8..56354a568bad0 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -949,6 +971,26 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
> }
> }
>
> + acomp_ctx->comp.sg_table = kmalloc(sizeof(*acomp_ctx->comp.sg_table),
> + GFP_KERNEL);
> + if (!acomp_ctx->comp.sg_table) {
> + ret = -ENOMEM;
> + goto req_fail;
> + }
> +
> + if (sg_alloc_table(acomp_ctx->comp.sg_table, ZSWAP_MAX_BATCH_SIZE,
> + GFP_KERNEL)) {
> + ret = -ENOMEM;
> + goto req_fail;
> + }
> +
> + /*
> + * Statically map the per-CPU destination buffers to the per-CPU
> + * SG lists; crucial for performance.
> + */
> + for_each_sg(acomp_ctx->comp.sg_table->sgl, sg, ZSWAP_MAX_BATCH_SIZE, i)
> + sg_set_buf(sg, acomp_ctx->comp.buffers[i], PAGE_SIZE);
> +
[ ... ]
> @@ -965,88 +1007,179 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
[ ... ]
> +static bool zswap_compress(struct zswap_store_context *zc)
> +{
> + u8 nr_batch_pages, batch_start, comp_batch_iter;
> + struct scatterlist *sg, *dst_sg;
> + unsigned int slen;
> + int err;
>
> /*
> + * @nr_batch_pages is constrained by the per-CPU comp_ctx's compressor
> + * batch size and the @zc->nr_pages in this store batch. @slen indicates
> + * the total source length bytes for @nr_batch_pages.
> */
> + nr_batch_pages = min(zc->comp_ctx->compr_batch_size, zc->nr_pages);
>
> /*
> + * Compress the @nr_pages in @folio starting at index @folio_start
> + * in batches of @nr_batch_pages.
> */
> + for (batch_start = 0; batch_start < zc->nr_pages;
> + batch_start += nr_batch_pages) {
> + /*
> + * Clamp the last iteration's batch size if the compressor's
> + * batch_size does not evenly divide @zc->nr_pages.
> + */
> + nr_batch_pages = min(nr_batch_pages, zc->nr_pages - batch_start);
> + slen = nr_batch_pages * PAGE_SIZE;
> +
> + /*
> + * Send @nr_batch_pages to crypto_acomp for compression:
> + *
> + * These pages are in @zc->folio's range of indices in the
> + * interval:
> + * [@zc->folio_start + @batch_start,
> + * @zc->folio_start + @batch_start + @nr_batch_pages).
> + *
> + * The source folio pages in the batch are directly submitted to
> + * crypto_acomp via acomp_request_set_src_folio().
> + */
> + acomp_request_set_src_folio(zc->comp_ctx->areq.req, zc->folio,
> + (zc->folio_start + batch_start) * PAGE_SIZE,
> + slen);
> +
> + dst_sg = zc->comp_ctx->sg_table->sgl + batch_start;
> + dst_sg->length = slen;
[Severity: High]
Does setting only dst_sg->length = slen overrun acomp_ctx->comp.buffers[]
and leave dst_sg[1 .. nr_batch_pages - 1].length uninitialized when
nr_batch_pages > 1?
Each entry in acomp_ctx->comp.sg_table->sgl is mapped to a single
PAGE_SIZE buffer in acomp_ctx->comp.buffers[i], and earlier runs overwrite
each entry's sg->length with compressed lengths or negative error codes.
When a batching compressor runs with nr_batch_pages > 1, dst_sg[0].length
is set to nr_batch_pages * PAGE_SIZE (exceeding its PAGE_SIZE buffer),
while dst_sg[1 .. nr_batch_pages - 1].length are not reset to PAGE_SIZE.
If the compressor fails when zc->wb_enabled is true without updating
dst_sg[1 .. nr_batch_pages - 1].length, any entry still holding a stale
compressed length (0 < sg->length < PAGE_SIZE) from a previous call will
skip the PAGE_SIZE fallback and cause zswap_update_entries() to store
stale data from the previous folio.
Each of the nr_batch_pages scatterlist entries in dst_sg needs its length
reset to PAGE_SIZE before crypto_acomp_compress().
> +
> + /*
> + * The per-CPU @zc->comp_ctx->sg_table scatterlists are statically
> + * mapped to the per-CPU dst @buffers at pool creation time.
> + * There are ZSWAP_MAX_BATCH_SIZE per-CPU SG lists and dst
> + * buffers allocated for all compressors.
> + */
> + acomp_request_set_dst_sg(zc->comp_ctx->areq.req, dst_sg, slen);
[Severity: High]
Isn't dst_sg an unterminated scatterlist slice whenever
batch_start + nr_batch_pages < ZSWAP_MAX_BATCH_SIZE?
sg_alloc_table() only sets SG_END on sgl[ZSWAP_MAX_BATCH_SIZE - 1], so
passing dst_sg as req->dst leaves sg_nents(req->dst) equal to
ZSWAP_MAX_BATCH_SIZE - batch_start (4, 3, or 2 instead of nr_batch_pages).
In-tree hardware acomp drivers rely on sg_nents(req->dst):
iaa_comp_acompress() falls back to software deflate whenever
sg_nents(req->dst) > 1 (affecting all order-0 folios and 3 of every 4
pages in a batch), while __qat_bl_sgl_to_bufl() and
hisi_acc_sg_buf_map_to_hw_sgl() walk and DMA-map all sg_nents(req->dst)
entries up to sgl[3] using their sg->length fields, which can still hold a
negative errno such as (unsigned int)-ENOSPC (~4 GiB) left behind by a
prior failed batch.
> +
> + err = crypto_wait_req(crypto_acomp_compress(zc->comp_ctx->areq.req),
> + &zc->comp_ctx->areq.wait);
> +
> + /*
> + * If a page cannot be compressed into a size smaller than
> + * PAGE_SIZE, save the content as is without a compression, to
> + * keep the LRU order of writebacks. If writeback is disabled,
> + * reject the page since it only adds metadata overhead.
> + * swap_writeout() will put the page back to the active LRU list
> + * in the case.
> + */
> + if (err && !zc->wb_enabled)
> + goto compress_error;
> +
> + /*
> + * For each output SG list in @acomp_ctx->req->sg_table->sgl,
> + * the @sg->length should be set to either the page's compressed
> + * length (success), or it's negative value compression error.
> + */
> + for_each_sg(dst_sg, sg, nr_batch_pages, comp_batch_iter) {
> + if (((int)sg->length <= 0) || (sg->length >= PAGE_SIZE)) {
> + if (!zc->wb_enabled) {
> + if (sg->length == PAGE_SIZE)
> + sg->length = -ENOSPC;
> + else if (WARN_ON_ONCE(!sg->length || sg->length > PAGE_SIZE))
> + sg->length = -EINVAL;
> + goto compress_error;
> + }
> +
> + sg->length = PAGE_SIZE;
> + }
> + }
> }
>
> + return zswap_update_entries(zc);
>
> +compress_error:
> + for_each_sg(dst_sg, sg, nr_batch_pages, comp_batch_iter) {
> + if ((int)sg->length < 0) {
> + if ((int)sg->length == -ENOSPC)
> + zswap_reject_compress_poor++;
> + else
> + zswap_reject_compress_fail++;
> + }
> + }
>
> + return false;
> }
[Severity: Medium]
Won't evaluating sg->length > PAGE_SIZE on an unsigned int trigger
WARN_ON_ONCE() whenever sg->length holds a negative error code?
Because sg->length is unsigned, a negative error code in sg->length (such
as -ENOSPC from a failed page in a batch) satisfies sg->length > PAGE_SIZE
when !zc->wb_enabled, firing a spurious WARN_ON_ONCE() and overwriting the
error code with -EINVAL so compress_error increments
zswap_reject_compress_fail instead of zswap_reject_compress_poor.
Conversely, if crypto_acomp_compress() returns an early request-level
error in err before writing negative error codes into dst_sg->length,
jumping to compress_error leaves all sg->length values positive and
returns false without incrementing either rejection counter.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120041906.5785-1-kanchanapsridhar2026@gmail.com?part=8
next prev parent reply other threads:[~2026-11-21 4:20 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 4:18 [PATCH v4 0/8] Batched zswap_store() framework with compression batching Kanchana P. Sridhar
2026-11-20 4:18 ` [PATCH v4 1/8] crypto: acomp - Define a unit_size in struct acomp_req to enable batching. mm: zswap: Set the unit size for zswap to PAGE_SIZE Kanchana P. Sridhar
2026-11-20 4:19 ` [PATCH v4 2/8] crypto: acomp - Add bit to indicate segmentation support Kanchana P. Sridhar
2026-11-20 4:19 ` [PATCH v4 3/8] crypto: acomp - Add trivial segmentation wrapper Kanchana P. Sridhar
2026-11-20 4:19 ` [PATCH v4 4/8] crypto: acomp - Add API to get a compression algorithm's batch-size Kanchana P. Sridhar
2026-11-21 4:20 ` sashiko-bot
2026-11-20 4:19 ` [PATCH v4 5/8] mm: zswap: Store the folio's node id in the zswap_entry Kanchana P. Sridhar
2026-11-20 22:46 ` Nhat Pham
2026-11-20 23:01 ` Kanchana P. Sridhar
2026-11-21 4:20 ` sashiko-bot
2026-11-20 4:19 ` [PATCH v4 6/8] mm: zswap: Store large folios in batches Kanchana P. Sridhar
2026-11-20 23:08 ` Nhat Pham
2026-11-20 23:40 ` Kanchana P. Sridhar
2026-11-20 23:25 ` Nhat Pham
2026-11-20 23:51 ` Kanchana P. Sridhar
2026-11-20 4:19 ` [PATCH v4 7/8] mm: zswap: Refactor and optimize zswap_store_pages() batch operations Kanchana P. Sridhar
2026-11-20 23:45 ` Nhat Pham
2026-11-21 1:36 ` Kanchana P. Sridhar
2026-11-20 4:19 ` [PATCH v4 8/8] mm: zswap: Batched zswap_compress() for storing large folios in batches Kanchana P. Sridhar
2026-11-20 23:55 ` Nhat Pham
2026-11-21 1:17 ` Kanchana P. Sridhar
2026-11-21 3:24 ` Kanchana P. Sridhar
2026-11-21 4:20 ` sashiko-bot [this message]
2026-11-20 23:16 ` [PATCH v4 0/8] Batched zswap_store() framework with compression batching Nhat Pham
2026-11-21 1:04 ` Kanchana P. Sridhar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-226638@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=davem@davemloft.net \
--cc=hannes@cmpxchg.org \
--cc=herbert@gondor.apana.org.au \
--cc=kanchanapsridhar2026@gmail.com \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=rientjes@google.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=senozhatsky@chromium.org \
--cc=willy@infradead.org \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.