From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796189920; b=TzH8XEyfChX+MeivFS/8u5B73RCK6X368K8KljsKgMD/Aa+7BYWaAs+Gym4gPPVIbMjH qFIpq+DZqH5dVRPStxSOVJitbCMvCdDLDapPtTc4mYe91HqUjXvecu+v+X/+qQFlrFBEx /70UbN86bRatXgGvk8qWUefnbyWp/GAxwyIKXxsCJ0SBzSuEGu2JsDW7Yyo3jQbA90XD6 ZsV1ooYs598o3LIb8geAaeWv6y0N2pTHnHXOWsAeVYCJDbh2e9xzW93O/R4ExPGavH/kX SdtIqpkHYdNrvJ8XGOrk4LVJoTItamMkokWOwbxtIm6BxgjmVPee8YFE7MLGjgI+WEw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796189920; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=XCeae4MZ3UO4BMC3RcXeOSw8GI/XLs0J+roV5+VVawM=; b=VK09SeqmysBRI1hxBQZcb23gXcwjVkaopIiT8JMtbGWoxb36pp/uxrCbVmNiVMI8vQXt 9w/oxoESCwKxKhncq0a0hB1xACB1MpBQTcpiJBRKYDYgHHw4oAEM48So5FI/WwU3Gz0Gv Wy+ZH/FkV1cMMfgiW67iUuJP505PLe13+Y8cKi2B/lYdEnLPi2K/fS1kh1VwPYoqBW8TJ C+Qhqg0C3TecmTOw5bVodTFirFWLQLbv0Vc+pMqr0IZe0nGs3k8onlRcYhem8NmnaZthk wJynFa6rdijvuDoFgX63YA8pqjFoKsa7JgUVUVUkbCVigDtv7fDx9iLCX8l29DIbT1A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Khlk7GLm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E310606A31A for ; Wed, 2 Dec 2026 05:38:40 +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 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-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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