From: Yosry Ahmed <yosry.ahmed@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
Nhat Pham <nphamcs@gmail.com>,
Chengming Zhou <chengming.zhou@linux.dev>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Herbert Xu <herbert@gondor.apana.org.au>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry.ahmed@linux.dev>
Subject: [PATCH] mm: zswap: Use SG list decompression APIs from zsmalloc
Date: Wed, 21 Jan 2026 01:36:15 +0000 [thread overview]
Message-ID: <20260121013615.2906368-1-yosry.ahmed@linux.dev> (raw)
Use the new zs_obj_read_sg_*() APIs in zswap_decompress(), instead of
zs_obj_read_*() APIs returning a linear address. The SG list is passed
directly to the crypto API, simplifying the logic and dropping the
workaround that copies highmem addresses to a buffer. The crypto API
should internally linearize the SG list if needed.
This avoids the memcpy() in zsmalloc for objects spanning multiple
pages, although an equivalent operation will be done internally by
acomp/scomp. However, in the future compression algorithms could support
handling discontiguous SG lists, completely eliminating the copying for
spanning objects.
Zsmalloc fills an SG list up to 2 entries in size, so change the input
SG list to fit 2 entries.
Update the incompressible entries path to use memcpy_from_sglist() to
copy the data to the folio. Opportunistically set dlen to PAGE_SIZE in
the same code path (rather that at the top of the function) to make it
clearer.
Drop the goto in zswap_compress() as the code now is not simple enough
for an if-else statement instead. Rename 'decomp_ret' to 'ret' and reuse
it to keep the intermediate return value of crypto_acomp_decompress() to
keep line lengths manageable.
No functional change intended.
Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
---
This patch depends on "zsmalloc: introduce SG-list based object read
API":
https://lore.kernel.org/linux-mm/20260113034645.2729998-1-senozhatsky@chromium.org/
---
mm/zswap.c | 49 +++++++++++++++++++------------------------------
1 file changed, 19 insertions(+), 30 deletions(-)
diff --git a/mm/zswap.c b/mm/zswap.c
index 2f410507cbc8..aea3267c5a96 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -26,6 +26,7 @@
#include <linux/mempolicy.h>
#include <linux/mempool.h>
#include <crypto/acompress.h>
+#include <crypto/scatterwalk.h>
#include <linux/zswap.h>
#include <linux/mm_types.h>
#include <linux/page-flags.h>
@@ -936,53 +937,41 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,
static bool zswap_decompress(struct zswap_entry *entry, struct folio *folio)
{
struct zswap_pool *pool = entry->pool;
- struct scatterlist input, output;
+ struct scatterlist input[2]; /* zsmalloc returns an SG list 1-2 entries */
+ struct scatterlist output;
struct crypto_acomp_ctx *acomp_ctx;
- int decomp_ret = 0, dlen = PAGE_SIZE;
- u8 *src, *obj;
+ int ret = 0, dlen;
acomp_ctx = acomp_ctx_get_cpu_lock(pool);
- obj = zs_obj_read_begin(pool->zs_pool, entry->handle, entry->length,
- acomp_ctx->buffer);
+ zs_obj_read_sg_begin(pool->zs_pool, entry->handle, input, entry->length);
/* zswap entries of length PAGE_SIZE are not compressed. */
if (entry->length == PAGE_SIZE) {
- memcpy_to_folio(folio, 0, obj, entry->length);
- goto read_done;
- }
-
- /*
- * zs_obj_read_begin() might return a kmap address of highmem when
- * acomp_ctx->buffer is not used. However, sg_init_one() does not
- * handle highmem addresses, so copy the object to acomp_ctx->buffer.
- */
- if (virt_addr_valid(obj)) {
- src = obj;
+ WARN_ON_ONCE(input->length != PAGE_SIZE);
+ memcpy_from_sglist(kmap_local_folio(folio, 0), input, 0, PAGE_SIZE);
+ dlen = PAGE_SIZE;
} else {
- WARN_ON_ONCE(obj == acomp_ctx->buffer);
- memcpy(acomp_ctx->buffer, obj, entry->length);
- src = acomp_ctx->buffer;
+ sg_init_table(&output, 1);
+ sg_set_folio(&output, folio, PAGE_SIZE, 0);
+ acomp_request_set_params(acomp_ctx->req, input, &output,
+ entry->length, PAGE_SIZE);
+ ret = crypto_acomp_decompress(acomp_ctx->req);
+ ret = crypto_wait_req(ret, &acomp_ctx->wait);
+ dlen = acomp_ctx->req->dlen;
}
- sg_init_one(&input, src, entry->length);
- sg_init_table(&output, 1);
- sg_set_folio(&output, folio, PAGE_SIZE, 0);
- acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE);
- decomp_ret = crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait);
- dlen = acomp_ctx->req->dlen;
-
-read_done:
- zs_obj_read_end(pool->zs_pool, entry->handle, entry->length, obj);
+ zs_obj_read_sg_end(pool->zs_pool, entry->handle);
acomp_ctx_put_unlock(acomp_ctx);
- if (!decomp_ret && dlen == PAGE_SIZE)
+ if (!ret && dlen == PAGE_SIZE)
return true;
zswap_decompress_fail++;
pr_alert_ratelimited("Decompression error from zswap (%d:%lu %s %u->%d)\n",
swp_type(entry->swpentry),
swp_offset(entry->swpentry),
- entry->pool->tfm_name, entry->length, dlen);
+ entry->pool->tfm_name,
+ entry->length, dlen);
return false;
}
--
2.52.0.457.g6b5491de43-goog
next reply other threads:[~2026-01-21 1:36 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-21 1:36 Yosry Ahmed [this message]
2026-01-21 3:49 ` Sergey Senozhatsky
2026-02-03 23:18 ` Nhat Pham
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=20260121013615.2906368-1-yosry.ahmed@linux.dev \
--to=yosry.ahmed@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=hannes@cmpxchg.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=senozhatsky@chromium.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox