All of lore.kernel.org
 help / color / mirror / Atom feed
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 v5 9/9] mm: zswap: Batched zswap_compress() for storing large folios in batches.
Date: Wed, 02 Dec 2026 05:38:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-244917@kernel.org> (raw)
In-Reply-To: <20261201053711.8090-10-kanchanapsridhar2026@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 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: unhandled request-level error from
  crypto_acomp_compress()
- [Medium] mm: zswap: signed int dlen promoted to unsigned long in
  zswap_compress()
--

commit a77c5e5885be01b07e755de49f91856a93af9170
Author: Kanchana P. Sridhar <kanchanapsridhar2026@gmail.com>

mm: zswap: Batched zswap_compress() for storing large folios in batches.

This commit replaces single-page compression in zswap_store_pages() with a
batched zswap_compress() using per-CPU destination scatterlists and adds
zswap_update_entries() to store the compressed batch in zsmalloc.

> diff --git a/mm/zswap.c b/mm/zswap.c
> index 8a80b6e8e2359..f391edb380160 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -947,6 +969,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);
[ ... ]
> +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;
>  
>  	/*
> -	 * it maybe looks a little bit silly that we send an asynchronous request,
> -	 * then wait for its completion synchronously. This makes the process look
> -	 * synchronous in fact.
> -	 * Theoretically, acomp supports users send multiple acomp requests in one
> -	 * acomp instance, then get those requests done simultaneously. but in this
> -	 * case, zswap actually does store and load page by page, there is no
> -	 * existing method to send the second page before the first page is done
> -	 * in one thread doing zswap.
> -	 * but in different threads running on different cpu, we have different
> -	 * acomp instance, and compression and decompression use separate
> -	 * requests, so multiple threads can do (de)compression in parallel.
> +	 * @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.
>  	 */
> -	comp_ret = crypto_wait_req(crypto_acomp_compress(comp_ctx->areq.req),
> -				   &comp_ctx->areq.wait);
> -	dlen = comp_ctx->areq.req->dlen;
> -	if (!comp_ret) {
> -		if (dlen == PAGE_SIZE)
> -			comp_ret = -ENOSPC;
> -		else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE))
> -			comp_ret = -EINVAL;
> -	}
> +	nr_batch_pages = min(zc->comp_ctx->compr_batch_size, zc->nr_pages);
>  
>  	/*
> -	 * 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.
> +	 * Compress the @nr_pages in @folio starting at index @folio_start
> +	 * in batches of @nr_batch_pages.
>  	 */
> -	if (comp_ret) {
> -		if (!wb_enabled)
> -			goto unlock;
> -		comp_ret = 0;
> -		dlen = PAGE_SIZE;
> -		dst = kmap_local_folio(folio, index * PAGE_SIZE);
> -		mapped = true;
> +	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]
When nr_batch_pages > 1, doesn't setting dst_sg->length = slen assign
nr_batch_pages * PAGE_SIZE to dst_sg[0].length while leaving
dst_sg[1 .. nr_batch_pages - 1].length unreset?

Each entry in acomp_ctx->comp.sg_table->sgl is backed by a single
PAGE_SIZE slab buffer in acomp_ctx->comp.buffers[i], and its sg->length
is overwritten in place by each compression call.  On a multi-page batch,
dst_sg[0].length advertises up to ZSWAP_MAX_BATCH_SIZE * PAGE_SIZE bytes
for a PAGE_SIZE buffer, while dst_sg[1 .. nr_batch_pages - 1].length
retain stale compressed lengths or negative error codes from previous
calls on that CPU.  In addition, if compression fails while
zc->wb_enabled is true without updating every dst_sg[i].length, any entry
holding a stale positive length (< PAGE_SIZE) bypasses the PAGE_SIZE
fallback and causes zswap_update_entries() to store stale compressed data
from an earlier folio.

Should each entry in dst_sg[0 .. nr_batch_pages - 1] have its sg->length
reset to PAGE_SIZE before calling acomp_request_set_dst_sg()?

> +
> +		/*
> +		 * 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]
Does passing dst_sg from the 4-entry sg_table without terminating
dst_sg[nr_batch_pages - 1] with SG_END break hardware acomp drivers when
batch_start + nr_batch_pages < ZSWAP_MAX_BATCH_SIZE?

Because sg_alloc_table() only sets SG_END on sgl[ZSWAP_MAX_BATCH_SIZE - 1],
sg_nents(req->dst) is ZSWAP_MAX_BATCH_SIZE - batch_start (4, 3, or 2)
instead of nr_batch_pages whenever the batch does not reach the end of
sgl.  In iaa_comp_acompress(), sg_nents(req->dst) > 1 forces a fallback to
software deflate_generic_compress() for all order-0 folios and the first
three pages of every 4-page batch:

drivers/crypto/intel/iaa/iaa_crypto_main.c:iaa_comp_acompress() {
	/* Fall back to software if src or dst has multiple sg entries */
	if (sg_nents(req->src) > 1 || sg_nents(req->dst) > 1)
		return deflate_generic_compress(req);
}

In QAT (__qat_bl_sgl_to_bufl()) and HiSilicon ZIP
(hisi_acc_sg_buf_map_to_hw_sgl()), the driver walks and DMA-maps all
sg_nents(req->dst) entries up to SG_END using each entry's sg->length.
If an earlier call failed at batch_start >= 1 and left sgl[batch_start].length
set to a negative error code such as (unsigned int)-ENOSPC (~4 GiB), the
next call starting at batch_start == 0 only resets sgl[0].length and
leaves the trailing entries chained to req->dst, causing those drivers to
attempt a ~4 GiB DMA mapping on a PAGE_SIZE buffer.

> +
> +		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) {
> +			int dlen = (int)sg->length;
> +
> +			if ((dlen <= 0) || (dlen >= PAGE_SIZE)) {
> +				if (!zc->wb_enabled) {
> +					if (dlen == PAGE_SIZE)
> +						sg->length = -ENOSPC;
> +					else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE))
> +						sg->length = -EINVAL;
> +					goto compress_error;
> +				}
> +
> +				sg->length = PAGE_SIZE;
> +			}
> +		}

[Severity: Medium]
In WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE), doesn't comparing signed int
dlen against unsigned long PAGE_SIZE promote negative error values to
unsigned long?

When dlen < 0 (such as -ENOSPC from a failed page in the batch) and
!zc->wb_enabled, dlen > PAGE_SIZE evaluates to true after integer
promotion, firing a spurious WARN_ON_ONCE() and overwriting sg->length
with -EINVAL so that compress_error counts the failure as
zswap_reject_compress_fail instead of zswap_reject_compress_poor.  Placing
the WARN_ON_ONCE() inside if (!zc->wb_enabled) also skips the warning
when zc->wb_enabled is true and a compressor returns dlen == 0 or
dlen > PAGE_SIZE.

> +	}
>  
> -	gfp = GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE;
> -	alloc_ret = zs_store(pool->zs_pool, &entry->handle, dst, dlen, gfp,
> -			     folio_nid(folio));
> -	if (alloc_ret)
> -		goto unlock;
> +	return zswap_update_entries(zc);
>  
> -	entry->length = dlen;
> +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++;
> +		}
> +	}
>  
> -unlock:
> -	if (mapped)
> -		kunmap_local(dst);
> -	if (comp_ret == -ENOSPC || alloc_ret == -ENOSPC)
> -		zswap_reject_compress_poor++;
> -	else if (comp_ret)
> -		zswap_reject_compress_fail++;
> -	else if (alloc_ret)
> -		zswap_reject_alloc_fail++;
> -
> -	mutex_unlock(&comp_ctx->areq.mutex);
> -	return comp_ret == 0 && alloc_ret == 0;
> +	return false;
>  }

[Severity: Medium]
What happens when crypto_acomp_compress() returns a request-level error
without writing negative error codes into dst_sg[i].length?

crypto_acomp_compress() can return early request-level errors (such as
-EINVAL, -EAGAIN, or -ENOSYS when req->slen > req->unit_size without
CRYPTO_ALG_REQ_SEG) or fail inside a segmented driver's tfm->compress(req)
without setting negative error values in each dst_sg[i].length.  When
!zc->wb_enabled, jumping to compress_error only checks if
(int)sg->length < 0, so the rejection counters are not incremented if
sg->length is still positive.  When zc->wb_enabled is true, err is ignored
and the post-compression loop inspects each sg->length, treating any entry
whose sg->length happens to be in (0, PAGE_SIZE) as a valid compressed
page instead of falling back to uncompressed PAGE_SIZE storage for the
failed request.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201053711.8090-1-kanchanapsridhar2026@gmail.com?part=9

      reply	other threads:[~2026-12-02  5:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  5:37 [PATCH v5 0/9] Batched zswap_store() framework with compression batching Kanchana P. Sridhar
2026-12-01  5:37 ` [PATCH v5 1/9] 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-12-01  5:37 ` [PATCH v5 2/9] crypto: acomp - Add bit to indicate segmentation support Kanchana P. Sridhar
2026-12-01  5:37 ` [PATCH v5 3/9] crypto: acomp - Add trivial segmentation wrapper Kanchana P. Sridhar
2026-12-01  5:37 ` [PATCH v5 4/9] crypto: acomp - Add API to get a compression algorithm's batch-size Kanchana P. Sridhar
2026-12-01  5:37 ` [PATCH v5 5/9] mm: zswap: Store the folio's node id in the zswap_entry Kanchana P. Sridhar
2026-12-02  5:38   ` sashiko-bot
2026-12-01  5:37 ` [PATCH v5 6/9] mm: zswap: Obtain folio's wb_enabled status once in zswap_store() Kanchana P. Sridhar
2026-12-02  5:38   ` sashiko-bot
2026-12-01  5:37 ` [PATCH v5 7/9] mm: zswap: Store large folios in batches Kanchana P. Sridhar
2026-12-02  5:38   ` sashiko-bot
2026-12-01  5:37 ` [PATCH v5 8/9] mm: zswap: Refactor and optimize zswap_store_pages() batch operations Kanchana P. Sridhar
2026-12-01  5:37 ` [PATCH v5 9/9] mm: zswap: Batched zswap_compress() for storing large folios in batches Kanchana P. Sridhar
2026-12-02  5:38   ` sashiko-bot [this message]

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-244917@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.