From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D00D3606A31A for ; Wed, 2 Dec 2026 05:39:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Khlk7GLm Received: by kanga.kvack.org (Postfix) id 890BC6B05C4; Wed, 2 Dec 2026 00:38:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CC406B0629; Wed, 2 Dec 2026 00:38:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66CB86B062A; Wed, 2 Dec 2026 00:38:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B947E6B05C4 for ; Wed, 2 Dec 2026 00:38:47 -0500 (EST) Received: from smtpin06.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 35B11A755C for ; Wed, 2 Dec 2026 05:38:42 +0000 (UTC) X-FDA: 85497938484.06.764758A Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf09.hostedemail.com (Postfix) with ESMTP id 98327140003 for ; Wed, 2 Dec 2026 05:38:40 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Khlk7GLm; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796189920; b=0ttzxit7CqjIUzVlppGFQ61mdX3I3KloPRh/SnA7csjF74uIJ4z/wfb7cHScuDwDQBQtiM uyHvHmJOSsS4r23nvcFAmJliC9IEYAc75M9j3LYUYp+BccNqgFPb/hGppi2pnc3TrGJOHB P2lVnrurZY3xM/NTTVfJE9Xi03hlIsI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796189920; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XCeae4MZ3UO4BMC3RcXeOSw8GI/XLs0J+roV5+VVawM=; b=iT0pXaqX2bupOevfFn+n0hGOoUzWKZg1LW4wMVdIcSJAA78z5IlzVEOi+bnJb7fFdDflrE +iK8VtWaqezjSezjd+BYepBzlUHp+qsH2+Wyg1Y2qB2sWZjfa79uHF708qb7YCaJSthDpe +GxTNyhEgWoDDP6TtjNpgf4eucx7i4E= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Khlk7GLm; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 3889F61C68; Wed, 2 Dec 2026 05:38:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12CF01F0089A; Wed, 2 Dec 2026 05:38:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796189919; bh=XCeae4MZ3UO4BMC3RcXeOSw8GI/XLs0J+roV5+VVawM=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Khlk7GLm5k0PFihYpHK13fe81PfGqMtvRjc+xiXxc2xL9rcaV5hdF2v+4UyX3+KtU 0NPUv8ThDp21dkror0xxaPfAJSiuMxqBask8qWRUhIKHGFtdOBu6Btqw9UEtUT8F5r Gj9y/E1qBKBxiZeV0i35JJmYff6wwlls5j6t5qdciQ4vBnmZkr+pjRS8lS+gla7c3D Eff6iLfqp3o92wheHRK0BnA/eIvaSjGJMxl7pZsA6YXRQtOlglxf323nJomImfWzgn RnKjgnd1+0NM82HQ22vptiTdFxFhiNq4L6SnDzWjih/KZbya0dMLocezxVtVwuwT6i 8a4Htdu53WPqw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v5 9/9] mm: zswap: Batched zswap_compress() for storing large folios in batches. Reply-To: sashiko-reviews@lists.linux.dev To: "Kanchana P. Sridhar" , 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 In-Reply-To: <20261201053711.8090-10-kanchanapsridhar2026@gmail.com> References: <20261201053711.8090-1-kanchanapsridhar2026@gmail.com> <20261201053711.8090-10-kanchanapsridhar2026@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 05:38:38 +0000 X-Rspam-User: X-Rspamd-Queue-Id: 98327140003 X-Stat-Signature: nk3bc81iop1mz9s67xzxwdk7699bnhed X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam06 X-HE-Tag: 1796189920-153614 X-HE-Meta: U2FsdGVkX1/DHC0iPh9rUR9l2Yf24I87OJJPs3jizg3ksgaVIJZyXFxR7JVJ9/JT6DXwrg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wixeTZ7S3asH2CLHNkQpV9lme9A7wzAEsJS3RP6qd7IjA8J/sBIPY772qSrHXMx2DVQCJKuRqjey6dfMl2kfK7IkKyfrcdR0ftV7SF+82mIdgvMqOdJHJ1OgSciyx5m44MfjW8tmFNZr4rWpgS371lSiFsA4Fzb12sNxQD+JmLKZQcODVOaGd/DML4X8WEVORIW8K7h/+p6+RLFeCkG2z7i2Zm4HgiiuB13aQs3XohFRfgFnvO1opKk79xmsDJrhrEabAKc2hGXCFuJV6I8RxIhfzEe4Rf9RSO9CyguVf0mwYPNwdwXulub463Jer9bJkliarKyGIpqc/3h/DgFE19oIMA5Qh2efN1KCADPep21XUxmeUV0DKDDSpjXadepKGnRl06pBKSmQ+/CrxV6cZomywglcD52GYveGNrSD8YmccioKiXN3FCqeSCE4kk6UxxQOTN56u3YuCTsOeOwYHBOOdgRcQztt80VOpNMMO+PTly7X21QvZu/H/Y0WHwSC0/gr3PlYnQtr3N8uJNEZhVz06MHzkQjFuXwlGIIOPKTxB6cHtUmd/yaG6MAlnPOWlEdQpoKbk9MvT/LzLvKs+1kHLLshx7oT6+0nRqMxzGUiWc7GYxsHy55gZx1D23pHnlM3YjzMMpfRQ6gnF4lpuFBcD5BQDpjSkpdkuOzDOgoFDoT2ZK2rhtRka3YGuC3CVHAnY= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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) > } > } > =20 > + acomp_ctx->comp.sg_table =3D kmalloc(sizeof(*acomp_ctx->comp.sg_table), > + GFP_KERNEL); > + if (!acomp_ctx->comp.sg_table) { > + ret =3D -ENOMEM; > + goto req_fail; > + } > + > + if (sg_alloc_table(acomp_ctx->comp.sg_table, ZSWAP_MAX_BATCH_SIZE, > + GFP_KERNEL)) { > + ret =3D -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; > =20 > /* > - * it maybe looks a little bit silly that we send an asynchronous reque= st, > - * then wait for its completion synchronously. This makes the process l= ook > - * 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 =3D crypto_wait_req(crypto_acomp_compress(comp_ctx->areq.req), > - &comp_ctx->areq.wait); > - dlen =3D comp_ctx->areq.req->dlen; > - if (!comp_ret) { > - if (dlen =3D=3D PAGE_SIZE) > - comp_ret =3D -ENOSPC; > - else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE)) > - comp_ret =3D -EINVAL; > - } > + nr_batch_pages =3D min(zc->comp_ctx->compr_batch_size, zc->nr_pages); > =20 > /* > - * 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 =3D 0; > - dlen =3D PAGE_SIZE; > - dst =3D kmap_local_folio(folio, index * PAGE_SIZE); > - mapped =3D true; > + for (batch_start =3D 0; batch_start < zc->nr_pages; > + batch_start +=3D 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 =3D min(nr_batch_pages, zc->nr_pages - batch_start); > + slen =3D 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 =3D zc->comp_ctx->sg_table->sgl + batch_start; > + dst_sg->length =3D slen; [Severity: High] When nr_batch_pages > 1, doesn't setting dst_sg->length =3D 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 >=3D 1 and left sgl[batch_start].l= ength set to a negative error code such as (unsigned int)-ENOSPC (~4 GiB), the next call starting at batch_start =3D=3D 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 =3D 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 =3D (int)sg->length; > + > + if ((dlen <=3D 0) || (dlen >=3D PAGE_SIZE)) { > + if (!zc->wb_enabled) { > + if (dlen =3D=3D PAGE_SIZE) > + sg->length =3D -ENOSPC; > + else if (WARN_ON_ONCE(!dlen || dlen > PAGE_SIZE)) > + sg->length =3D -EINVAL; > + goto compress_error; > + } > + > + sg->length =3D 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 =3D=3D 0 or dlen > PAGE_SIZE. > + } > =20 > - gfp =3D GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE; > - alloc_ret =3D zs_store(pool->zs_pool, &entry->handle, dst, dlen, gfp, > - folio_nid(folio)); > - if (alloc_ret) > - goto unlock; > + return zswap_update_entries(zc); > =20 > - entry->length =3D dlen; > +compress_error: > + for_each_sg(dst_sg, sg, nr_batch_pages, comp_batch_iter) { > + if ((int)sg->length < 0) { > + if ((int)sg->length =3D=3D -ENOSPC) > + zswap_reject_compress_poor++; > + else > + zswap_reject_compress_fail++; > + } > + } > =20 > -unlock: > - if (mapped) > - kunmap_local(dst); > - if (comp_ret =3D=3D -ENOSPC || alloc_ret =3D=3D -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 =3D=3D 0 && alloc_ret =3D=3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201053711.8090= -1-kanchanapsridhar2026@gmail.com?part=3D9