From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
kasong@tencent.com, hannes@cmpxchg.org, corbet@lwn.net,
yosry@kernel.org, nphamcs@gmail.com, muchun.song@linux.dev,
baohua@kernel.org, youngjun.park@lge.com, hughd@google.com,
baolin.wang@linux.alibaba.com, baoquan.he@linux.dev,
Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v2 06/32] mm, swap: use the xswap physical backend
Date: Fri, 4 Dec 2026 02:12:53 +0800 [thread overview]
Message-ID: <20261203181319.901844-7-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
An xswap entry that zswap evicts had nowhere to go. Send it to the
physical backend, and reserve the backend before decompressing so a
failed allocation wastes no work.
Now that an xswap entry can be written back, put it on the zswap
writeback LRU again. With no real swap device it stays on the LRU and
the shrinker rotates it until one appears, instead of staying pinned
until swapin or destroy.
An incompressible xswap page is stored uncompressed only while a real
swap device exists to hold its backend.
The zswap shrinker copies a swpentry and drops the LRU lock before
verifying it, while xswap maps cluster_info on demand. Check the range
under xswap_lock and reject a swpentry outside nr_clusters_mapped.
trylock avoids a deadlock: the allocation can trigger reclaim that
calls this path again.
A slot that was written out has no zswap copy, so swap_read_folio()
forwards its read to the physical entry.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/page_io.c | 32 ++++++++++++++++++----
mm/zswap.c | 75 ++++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 91 insertions(+), 16 deletions(-)
diff --git a/mm/page_io.c b/mm/page_io.c
index 2bc3b46e5529..76e8d08961bb 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -481,9 +481,27 @@ static bool swap_read_folio_zeromap(struct folio *folio)
void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
{
struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
- bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
+ swp_entry_t entry = folio->swap;
+ swp_entry_t phys = {};
+ bool synchronous;
bool workingset = folio_test_workingset(folio);
+ /* Resolved here: the batch and submit mode precede zswap_load(). */
+ if (unlikely(sis->flags & SWP_XSWAP)) {
+ struct swap_cluster_info *ci;
+ unsigned long offset = swp_offset(folio->swap);
+
+ ci = __swap_offset_to_cluster(sis, offset);
+ if (ci) {
+ spin_lock(&ci->lock);
+ phys = xswap_slot_backend(ci, offset % SWAPFILE_CLUSTER);
+ spin_unlock(&ci->lock);
+ }
+ if (phys.val)
+ entry = phys;
+ }
+ synchronous = __swap_entry_to_info(entry)->flags & SWP_SYNCHRONOUS_IO;
+
VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
@@ -493,7 +511,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
* window opens, so that each batch is billed for its own read
* rather than for the next folio's lookup and decompression.
*/
- if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ))
+ if (ctx->sio && !swap_can_merge(ctx, folio, entry, READ))
swap_read_submit(ctx);
/*
@@ -522,8 +540,12 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
return;
- if (unlikely(sis->flags & SWP_XSWAP)) {
- /* An xswap entry only lives in zswap: zswap_load() must hit. */
+ if (unlikely(sis->flags & SWP_XSWAP) && !phys.val) {
+ /*
+ * Neither zswap nor a physical slot has it. No
+ * folio_mark_uptodate(), so do_swap_page() sees this as a
+ * failed read and SIGBUSes silently.
+ */
WARN_ON_ONCE(1);
folio_unlock(folio);
return;
@@ -531,7 +553,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
- swap_add_folio(ctx, folio, folio->swap, READ);
+ swap_add_folio(ctx, folio, entry, READ);
/*
* Synchronous devices do the whole read at submission time, submit
diff --git a/mm/zswap.c b/mm/zswap.c
index b6a328ab787a..188f763a8af9 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -935,9 +935,10 @@ static bool zswap_compress(struct folio *folio, long index,
* 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. An xswap entry in this phase
- * is never put on the LRU, so an incompressible page stored for it
- * could never be reclaimed: reject it the same way.
+ * to the active LRU list in the case. An xswap entry can be written
+ * back only while a real swap device exists to hold its backend;
+ * with none, an incompressible page stored for it could never be
+ * reclaimed, so reject it the same way.
*/
if (comp_ret) {
if (!can_writeback)
@@ -1091,6 +1092,8 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct folio *folio;
struct mempolicy *mpol;
struct swap_info_struct *si;
+ swp_entry_t phys = {};
+ bool is_xswap;
int ret = 0;
/* try to allocate swap cache folio */
@@ -1098,14 +1101,33 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
if (IS_ERR_OR_NULL(si))
return -ENOENT;
- if (si->flags & SWP_XSWAP) {
- put_swap_device(si);
- return -EINVAL;
+ is_xswap = !!(si->flags & SWP_XSWAP);
+
+ /*
+ * The allocation can enter reclaim and grow this device, which takes
+ * the lock again, so do not hold it across. Check that the entry's
+ * range is mapped before the allocation.
+ */
+#ifdef CONFIG_XSWAP
+ if (is_xswap) {
+ if (!mutex_trylock(&si->xswap_lock)) {
+ put_swap_device(si);
+ return -EAGAIN;
+ }
+ if (swp_offset(swpentry) / SWAPFILE_CLUSTER >=
+ READ_ONCE(si->nr_clusters_mapped)) {
+ mutex_unlock(&si->xswap_lock);
+ put_swap_device(si);
+ return -ENOENT;
+ }
+ mutex_unlock(&si->xswap_lock);
}
+#endif
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
NO_INTERLEAVE_INDEX);
+
put_swap_device(si);
/*
@@ -1135,6 +1157,15 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
goto err;
}
+ /* Reserve the destination before dropping the zswap copy. */
+ if (is_xswap) {
+ phys = xswap_backend_alloc(swpentry);
+ if (!phys.val) {
+ ret = -ENOMEM;
+ goto err;
+ }
+ }
+
if (!zswap_decompress(entry, folio)) {
ret = -EIO;
goto err;
@@ -1167,11 +1198,13 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
folio_put(folio);
/* start writeback */
- __swap_writeout(ctx, folio, folio->swap);
+ __swap_writeout(ctx, folio, is_xswap ? phys : folio->swap);
return 0;
err:
+ if (is_xswap && phys.val)
+ xswap_backend_free(swpentry, phys);
swap_cache_del_folio(folio);
folio_unlock(folio);
folio_put(folio);
@@ -1207,10 +1240,12 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
void *arg)
{
struct zswap_entry *entry = container_of(item, struct zswap_entry, lru);
+ struct swap_info_struct *si;
struct zswap_shrink_ctl *ctl = arg;
swp_entry_t swpentry;
enum lru_status ret = LRU_REMOVED_RETRY;
int writeback_result;
+ bool no_backend;
/*
* Second chance algorithm: if the entry has its referenced bit set, give it
@@ -1256,6 +1291,21 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
*/
swpentry = entry->swpentry;
+ /*
+ * An xswap entry with no real swap device has nowhere to be
+ * written back to, so keep it on the LRU for when one appears.
+ * LRU_ROTATE needs the lru lock and the get/put_swap_device()
+ * pair does not sleep, so this stays ahead of the unlock.
+ */
+ si = get_swap_device(swpentry);
+ if (IS_ERR_OR_NULL(si))
+ return LRU_ROTATE;
+ no_backend = (si->flags & SWP_XSWAP) &&
+ !atomic_read(&nr_real_swapfiles);
+ put_swap_device(si);
+ if (no_backend)
+ return LRU_ROTATE;
+
/*
* It's safe to drop the lock here because we return either
* LRU_REMOVED_RETRY, LRU_RETRY or LRU_STOP.
@@ -1555,8 +1605,12 @@ static bool zswap_store_page(struct folio *folio, long index,
return false;
}
- /* In this phase xswap has no device to write the page back to. */
- can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP);
+ /*
+ * An xswap entry can be written back only while a real swap device
+ * exists to hold its physical backend.
+ */
+ can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP) ||
+ atomic_read(&nr_real_swapfiles);
if (!zswap_compress(folio, index, entry, pool, can_writeback))
goto compress_failed;
@@ -1617,8 +1671,7 @@ static bool zswap_store_page(struct folio *folio, long index,
entry->objcg = objcg;
entry->referenced = true;
INIT_LIST_HEAD(&entry->lru);
- if (can_writeback)
- zswap_lru_add(entry);
+ zswap_lru_add(entry);
return true;
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:14 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 18:12 [PATCH v2 00/32] mm, swap: extendable swap devices (xswap phases II-IV) Baoquan He
2026-12-03 18:12 ` [PATCH v2 01/32] mm, swap: prepare the swap IO path for xswap backends Baoquan He
2026-12-03 18:12 ` [PATCH v2 02/32] mm, swap: tag a swap table entry with its owning xswap entry Baoquan He
2026-12-03 18:12 ` [PATCH v2 03/32] mm, swap: prepare the folio-less allocation path for xswap Baoquan He
2026-12-03 18:12 ` [PATCH v2 04/32] mm, swap: add a physical backend for xswap slots Baoquan He
2026-12-03 18:12 ` [PATCH v2 05/32] mm, swap: support swapoff of an xswap physical backend Baoquan He
2026-12-03 18:12 ` Baoquan He [this message]
2026-12-03 18:12 ` [PATCH v2 07/32] mm, swap: fall back to disk when zswap refuses an xswap page Baoquan He
2026-12-03 18:12 ` [PATCH v2 08/32] mm, swap: reclaim physical slots backing cache-only xswap entries Baoquan He
2026-12-03 18:12 ` [PATCH v2 09/32] mm, swap: drop swap_folio_sector() Baoquan He
2026-12-03 18:12 ` [PATCH v2 10/32] mm, swap: drop a stale xswap physical slot range before reallocating Baoquan He
2026-12-03 18:12 ` [PATCH v2 11/32] mm, swap: publish the xswap backend owner before its reverse map Baoquan He
2026-12-03 18:12 ` [PATCH v2 12/32] mm, swap: skip a NOFS backend for xswap physical writeback Baoquan He
2026-12-03 18:13 ` [PATCH v2 13/32] mm, swap: pass the folio order through the swap slot allocator Baoquan He
2026-12-03 18:13 ` [PATCH v2 14/32] mm, swap: allocate a multi-slot range without a folio Baoquan He
2026-12-03 18:13 ` [PATCH v2 15/32] mm, swap: pass the folio order to the xswap physical backend Baoquan He
2026-12-03 18:13 ` [PATCH v2 16/32] mm, swap: enable large xswap folios with a contiguous physical slot range Baoquan He
2026-12-03 18:13 ` [PATCH v2 17/32] mm, swap: enable THP swapin for xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 18/32] mm, swap: split the swap memcg charge helpers Baoquan He
2026-12-03 18:13 ` [PATCH v2 19/32] mm, swap: do not charge zswap-backed xswap entries Baoquan He
2026-12-03 18:13 ` [PATCH v2 20/32] mm, swap: charge an xswap entry when it gets physical backing Baoquan He
2026-12-03 18:13 ` [PATCH v2 21/32] mm, swap: don't gate xswap on the physical swap free count Baoquan He
2026-12-03 18:13 ` [PATCH v2 22/32] mm, swap: do not retake the cluster lock when uncharging an xswap slot Baoquan He
2026-12-03 18:13 ` [PATCH v2 23/32] mm, swap: uncharge an xswap backend where its record is cleared Baoquan He
2026-12-03 18:13 ` [PATCH v2 24/32] mm, swap: account xswap backends for cgroup v1 Baoquan He
2026-12-03 18:13 ` [PATCH v2 25/32] mm, swap: drop a refused xswap backend slot range directly Baoquan He
2026-12-03 18:13 ` [PATCH v2 26/32] mm, swap: route xswap by the zswap eligibility without flushing Baoquan He
2026-12-03 18:13 ` [PATCH v2 27/32] mm, swap: keep a per-cluster backend record for every swap device Baoquan He
2026-12-03 18:13 ` [PATCH v2 28/32] mm, swap: encode the per-slot backend record Baoquan He
2026-12-03 18:13 ` [PATCH v2 29/32] mm, swap: add accessors for the " Baoquan He
2026-12-03 18:13 ` [PATCH v2 30/32] mm, zswap: index entries in the swap device's backend table Baoquan He
2026-12-03 18:13 ` [PATCH v2 31/32] mm, swap: release a range of backend handles at once Baoquan He
2026-12-03 18:13 ` [PATCH v2 32/32] mm, swap: only run the xswap backend hooks on xswap devices Baoquan He
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=20261203181319.901844-7-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
/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