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 17/32] mm, swap: enable THP swapin for xswap entries
Date: Fri, 4 Dec 2026 02:13:04 +0800 [thread overview]
Message-ID: <20261203181319.901844-18-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
Read a large anon folio back as one unit when its xswap entries are
contiguous on one physical swap device; otherwise read it page by page
and retry at a smaller order. The device is SWP_SYNCHRONOUS_IO, which
THP swapin requires and which also skips readahead.
xswap_check_backing() refuses a zswap-backed or mixed range under the
cluster lock, before the folio is allocated, so zswap_load() takes the
physical path. If an earlier writeout fell back to a physical range and
zswap later accepts the folio, drop the stale range in swap_writeout()
on the zswap and zero-fill paths; reuse the backend loop as
xswap_backend_drop().
Co-developed-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/memory.c | 25 ++++++++-------
mm/page_io.c | 6 +++-
mm/swap.h | 13 ++++++++
mm/swap_state.c | 6 ++++
mm/swapfile.c | 82 +++++++++++++++++++++++++++++++++++++++++--------
5 files changed, 107 insertions(+), 25 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 94b0b1921f54..aa9caf576564 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4910,23 +4910,26 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
if (unlikely(userfaultfd_armed(vma)))
return 0;
+ entry = softleaf_from_pte(vmf->orig_pte);
+
/*
* A large swapped out folio could be partially or fully in zswap. We
* lack handling for such cases, so fallback to swapping in order-0
* folio.
+ * An xswap entry is deferred to __swap_cache_add_check() instead.
*/
- if (!zswap_never_enabled())
- return 0;
-
- /*
- * Neighbouring slots of a swap file can lie in two extents, and a folio
- * is read with one bio. The flag is set in swapon() and stays, so the
- * race is benign.
- */
- if (!data_race(si->flags & SWP_BLKDEV))
- return 0;
+ if (!(__swap_entry_to_info(entry)->flags & SWP_XSWAP)) {
+ if (!zswap_never_enabled())
+ return 0;
- entry = softleaf_from_pte(vmf->orig_pte);
+ /*
+ * Neighbouring slots of a swap file can lie in two extents, and
+ * a folio is read with one bio. The flag is set in swapon() and
+ * stays, so the race is benign.
+ */
+ if (!data_race(si->flags & SWP_BLKDEV))
+ return 0;
+ }
/*
* Get a list of all the (large) orders below PMD_ORDER that are enabled
* and suitable for swapping THP.
diff --git a/mm/page_io.c b/mm/page_io.c
index 080255ac7314..a49287162538 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -232,10 +232,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
/*
* The folio may have been stored in zswap before and dirtied
* again while it stayed in the swap cache. Drop the old copy,
- * or zswap writeback would bring it back into the swap cache.
+ * or zswap writeback would bring it back into the swap cache;
+ * a physical slot range recorded for it is stale for the same reason.
*/
zswap_invalidate(swp_type(entry), swp_offset(entry),
folio_nr_pages(folio));
+ xswap_backend_drop(entry, folio_order(folio));
goto out_unlock;
}
@@ -248,6 +250,8 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_store(folio, ctx->may_enter_fs)) {
count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
+ /* A slot range recorded by an earlier writeout is stale now. */
+ xswap_backend_drop(folio->swap, folio_order(folio));
goto out_unlock;
}
diff --git a/mm/swap.h b/mm/swap.h
index 1260b0ac6f41..2cd87a26843a 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -515,6 +515,9 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot);
swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order,
bool may_enter_fs);
void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order);
+void xswap_backend_drop(swp_entry_t entry, unsigned int order);
+int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
+ unsigned int nr);
#else
static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci,
unsigned int slot)
@@ -531,6 +534,16 @@ static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int or
static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order)
{
}
+
+static inline void xswap_backend_drop(swp_entry_t entry, unsigned int order)
+{
+}
+
+static inline int xswap_check_backing(struct swap_cluster_info *ci,
+ unsigned int off, unsigned int nr)
+{
+ return nr;
+}
#endif /* CONFIG_XSWAP */
#endif /* _MM_SWAP_H */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 7298a9894737..044e4856b7d8 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -209,6 +209,12 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
if (nr == 1)
return 0;
+ /* A zswap-backed or mixed batch falls back to a smaller order. */
+ if (__swap_entry_to_info(targ_entry)->flags & SWP_XSWAP) {
+ if (xswap_check_backing(ci, round_down(ci_off, nr), nr) != nr)
+ return -EBUSY;
+ }
+
is_zero = __swap_table_test_zero(ci, ci_off);
ci_off = round_down(ci_off, nr);
ci_end = ci_off + nr;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index f1257ae98685..463f53286a05 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4634,6 +4634,39 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot)
return (swp_entry_t){ .val = xs[slot] };
}
+/*
+ * A large xswap folio is read back as one IO only when the whole range is
+ * backed by one device, contiguously. Returns how many leading slots are.
+ */
+int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
+ unsigned int nr)
+{
+ swp_entry_t first;
+ unsigned int i;
+
+ lockdep_assert_held(&ci->lock);
+ /* A folio's slots never straddle a cluster, or the walk runs off. */
+ if (off + nr > SWAPFILE_CLUSTER) {
+ VM_WARN_ON_ONCE(1);
+ nr = SWAPFILE_CLUSTER - off;
+ }
+
+ /* In zswap, which cannot load a large folio. */
+ if (!ci->xs_table || !ci->xs_table[off])
+ return 1;
+
+ first.val = ci->xs_table[off];
+ for (i = 1; i < nr; i++) {
+ swp_entry_t cur = { .val = ci->xs_table[off + i] };
+
+ if (swp_type(cur) != swp_type(first) ||
+ swp_offset(cur) != swp_offset(first) + i)
+ break;
+ }
+
+ return i;
+}
+
/* Cluster lock held. The mapped prefix only grows while the device exists. */
static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci,
gfp_t gfp)
@@ -4798,6 +4831,36 @@ static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry)
}
}
+/*
+ * The data of the range of 1 << @order slots at @entry has been replaced,
+ * by zswap or by the zero mark. Drop the physical slot range recorded for it:
+ * otherwise its slots stay held until the xswap entry is freed, and a
+ * large folio can be read from a stale physical copy.
+ */
+void xswap_backend_drop(swp_entry_t entry, unsigned int order)
+{
+ struct swap_info_struct *si = __swap_entry_to_info(entry);
+ unsigned long nr = 1UL << order;
+ unsigned long off = swp_offset(entry);
+ unsigned int i;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ for (i = 0; i < nr; i++) {
+ struct swap_cluster_info *ci;
+ swp_entry_t old;
+
+ ci = swap_cluster_lock(si, off + i);
+ if (!ci)
+ continue;
+ old = xswap_slot_backend(ci, (off + i) % SWAPFILE_CLUSTER);
+ swap_cluster_unlock(ci);
+ if (old.val)
+ xswap_backend_free(swp_entry(si->type, off + i), old, 0);
+ }
+}
+
/* Back the range of 1 << @order slots at @entry with contiguous physical slots. */
swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order,
bool may_enter_fs)
@@ -4814,18 +4877,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order,
* cache and is now written out again still records the old slot range.
* Drop it before the new one replaces it.
*/
- for (i = 0; i < nr; i++) {
- struct swap_cluster_info *oci;
- swp_entry_t old;
-
- oci = swap_cluster_lock(si, off + i);
- if (!oci)
- continue;
- old = xswap_slot_backend(oci, (off + i) % SWAPFILE_CLUSTER);
- swap_cluster_unlock(oci);
- if (old.val)
- xswap_backend_free(swp_entry(si->type, off + i), old, 0);
- }
+ xswap_backend_drop(entry, order);
/*
* Get the xswap side ready before a physical slot exists, so the
@@ -5281,7 +5333,11 @@ static int xswap_create(int prio)
si->bdev = NULL;
si->swap_file = NULL;
- si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;
+ /*
+ * Reads are served from zswap, so no readahead. Only a synchronous
+ * device can swap a large folio back in as a unit.
+ */
+ si->flags |= SWP_XSWAP | SWP_SOLIDSTATE | SWP_SYNCHRONOUS_IO;
si->max = maxpages;
si->pages = maxpages - 1;
/*
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:15 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 ` [PATCH v2 06/32] mm, swap: use the " Baoquan He
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 ` Baoquan He [this message]
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-18-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