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 15/32] mm, swap: pass the folio order to the xswap physical backend
Date: Fri, 4 Dec 2026 02:13:02 +0800 [thread overview]
Message-ID: <20261203181319.901844-16-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
An xswap folio can need more than one physical swap slot. A large folio
needs 1 << order contiguous slots on the real device, so the physical
backend must know the order when it allocates. Pass the order to the
backend functions, set up every slot in the range, and free the whole
range together.
Callers still pass order 0 for now. Large xswap folios are not enabled
yet.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/page_io.c | 3 +-
mm/swap.h | 10 +++--
mm/swapfile.c | 120 +++++++++++++++++++++++++++++---------------------
mm/zswap.c | 4 +-
4 files changed, 80 insertions(+), 57 deletions(-)
diff --git a/mm/page_io.c b/mm/page_io.c
index 5aa3717b3d70..71e442a8f744 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -265,7 +265,8 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
swp_entry_t phys;
/* The pool refused it: fall back to a real swap device. */
- phys = xswap_backend_alloc(entry, ctx->may_enter_fs);
+ phys = xswap_backend_alloc(entry, folio_order(folio),
+ ctx->may_enter_fs);
if (!phys.val) {
folio_mark_dirty(folio);
return AOP_WRITEPAGE_ACTIVATE;
diff --git a/mm/swap.h b/mm/swap.h
index 6e3128c54b4f..9739f9d9e9db 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -510,8 +510,9 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
#ifdef CONFIG_XSWAP
swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot);
-swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs);
-void xswap_backend_free(swp_entry_t entry, swp_entry_t phys);
+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);
#else
static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci,
unsigned int slot)
@@ -519,12 +520,13 @@ static inline swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci,
return (swp_entry_t){};
}
-static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
+static inline swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order,
+ bool may_enter_fs)
{
return (swp_entry_t){};
}
-static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys)
+static inline void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order)
{
}
#endif /* CONFIG_XSWAP */
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 0e4b468ad87b..6bc14f5d1618 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4663,8 +4663,9 @@ static void xswap_free_xs_table(struct swap_info_struct *si,
ci->xs_table = NULL;
}
-/* Order-0 slot from @si, no folio attached; goes through the per-CPU cache. */
-static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si)
+/* A range of 1 << @order slots, no folio attached. */
+static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si,
+ unsigned int order)
{
struct swap_cluster_info *ci;
unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID;
@@ -4677,26 +4678,26 @@ static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si)
* again. Start from the cached cursor if it already points at this
* device, but never install this device for the next swapout.
*/
- if (this_cpu_read(percpu_swap_cluster.si[0]) == si) {
- pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]);
+ if (this_cpu_read(percpu_swap_cluster.si[order]) == si) {
+ pcp_offset = this_cpu_read(percpu_swap_cluster.offset[order]);
if (pcp_offset) {
ci = swap_cluster_lock(si, pcp_offset);
- if (cluster_is_usable(ci, 0))
+ if (cluster_is_usable(ci, order))
offset = alloc_swap_scan_cluster(si, ci, NULL,
- pcp_offset, 0,
+ pcp_offset, order,
true);
else
swap_cluster_unlock(ci);
}
}
if (offset == SWAP_ENTRY_INVALID)
- offset = cluster_alloc_swap_entry(si, NULL, 0, true);
+ offset = cluster_alloc_swap_entry(si, NULL, order, true);
local_unlock(&percpu_swap_cluster.lock);
return offset;
}
-static swp_entry_t xswap_alloc_phys_slot(bool may_enter_fs)
+static swp_entry_t xswap_alloc_phys_slot(unsigned int order, bool may_enter_fs)
{
struct swap_info_struct *devs[MAX_SWAPFILES], *si;
int nr = 0, i;
@@ -4734,7 +4735,7 @@ static swp_entry_t xswap_alloc_phys_slot(bool may_enter_fs)
put_swap_device(devs[i]);
continue;
}
- offset = xswap_alloc_slot_local(devs[i]);
+ offset = xswap_alloc_slot_local(devs[i], order);
put_swap_device(devs[i]);
if (offset && offset != SWAP_ENTRY_INVALID)
return swp_entry(devs[i]->type, offset);
@@ -4760,25 +4761,33 @@ static void xswap_install_rmap(swp_entry_t phys, swp_entry_t entry)
}
}
-swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
+/* 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)
{
struct swap_info_struct *si = __swap_entry_to_info(entry);
struct swap_cluster_info *ci;
+ unsigned long nr = 1UL << order;
unsigned long off = swp_offset(entry);
- unsigned long swp_tb;
- swp_entry_t phys, old;
+ swp_entry_t phys;
+ unsigned int i;
/*
* A folio that was written back, redirtied while still in the swap
* cache and is now written out again still records the old slot range.
* Drop it before the new one replaces it.
*/
- ci = swap_cluster_lock(si, off);
- if (ci) {
- old = xswap_slot_backend(ci, off % SWAPFILE_CLUSTER);
- swap_cluster_unlock(ci);
+ 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(entry, old);
+ xswap_backend_free(swp_entry(si->type, off + i), old, 0);
}
/*
@@ -4794,7 +4803,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
}
swap_cluster_unlock(ci);
- phys = xswap_alloc_phys_slot(may_enter_fs);
+ phys = xswap_alloc_phys_slot(order, may_enter_fs);
if (!phys.val)
return phys;
@@ -4802,53 +4811,64 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, bool may_enter_fs)
* The slot is a shadow until the reverse map is installed below.
* Physical swapoff can see it and retry; the window is short and
* try_to_unuse() reschedules.
- *
- * Owner first, then the reverse map, under one hold of the lock.
*/
+ /* One IO reads a large folio, so its range has to be contiguous. */
+ VM_WARN_ON_ONCE(swp_offset(phys) % nr);
+ VM_WARN_ON_ONCE(off % SWAPFILE_CLUSTER + nr > SWAPFILE_CLUSTER);
+
+ /* Owner first, then the reverse map, under one hold of the lock. */
ci = swap_cluster_lock(si, off);
- if (!ci) {
- xswap_backend_free(entry, phys);
- return (swp_entry_t){};
- }
- /* The table is preallocated above, so this cannot fail. */
- if (!xswap_slot_set_backend(ci, off % SWAPFILE_CLUSTER, phys))
- VM_WARN_ON_ONCE(1);
- xswap_install_rmap(phys, entry);
+ for (i = 0; i < nr; i++) {
+ unsigned int slot = (off + i) % SWAPFILE_CLUSTER;
+ swp_entry_t cur = swp_entry(swp_type(phys), swp_offset(phys) + i);
+ unsigned long swp_tb;
- /*
- * The folio can lose its last reference while the owner is not
- * recorded yet; that path cannot mark the slot for us.
- */
- swp_tb = __swap_table_get(ci, off % SWAPFILE_CLUSTER);
- if (swp_tb_is_folio(swp_tb) && !swp_tb_get_count(swp_tb))
- xswap_mark_cache_only(si, ci, off % SWAPFILE_CLUSTER);
+ /* The table is preallocated above, so this cannot fail. */
+ if (!xswap_slot_set_backend(ci, slot, cur))
+ VM_WARN_ON_ONCE(1);
+ xswap_install_rmap(cur, swp_entry(si->type, off + i));
+
+ /*
+ * The folio can lose its last reference while the owner is
+ * not recorded yet; that path cannot mark the slot for us.
+ */
+ swp_tb = __swap_table_get(ci, slot);
+ if (swp_tb_is_folio(swp_tb) && !swp_tb_get_count(swp_tb))
+ xswap_mark_cache_only(si, ci, slot);
+ }
swap_cluster_unlock(ci);
return phys;
}
/* Undo xswap_backend_alloc(): the zswap copy is still in place. */
-void xswap_backend_free(swp_entry_t entry, swp_entry_t phys)
+void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order)
{
struct swap_info_struct *si = __swap_entry_to_info(entry);
- struct swap_info_struct *psi;
- struct swap_cluster_info *ci;
+ struct swap_info_struct *psi = swap_type_to_info(swp_type(phys));
+ unsigned long nr = 1UL << order;
unsigned long off = swp_offset(entry);
+ unsigned long poff = swp_offset(phys);
+ unsigned int i;
- ci = swap_cluster_lock(si, off);
- if (ci) {
- unsigned int slot = off % SWAPFILE_CLUSTER;
- unsigned long *xs = ci->xs_table;
+ for (i = 0; i < nr; i++) {
+ struct swap_cluster_info *ci;
- if (xs && xs[slot] == phys.val)
- WRITE_ONCE(xs[slot], 0);
- swap_cluster_unlock(ci);
- }
+ ci = swap_cluster_lock(si, off + i);
+ if (ci) {
+ unsigned int slot = (off + i) % SWAPFILE_CLUSTER;
+ unsigned long *xs = ci->xs_table;
- /* xswap_free_phys_slot() clears the reverse mapping. */
- psi = swap_type_to_info(swp_type(phys));
- if (psi)
- xswap_free_phys_slot(psi, entry, phys);
+ if (xs && xs[slot] == phys.val + i)
+ WRITE_ONCE(xs[slot], 0);
+ swap_cluster_unlock(ci);
+ }
+
+ /* xswap_free_phys_slot() clears the reverse mapping. */
+ if (psi)
+ xswap_free_phys_slot(psi, swp_entry(si->type, off + i),
+ swp_entry(swp_type(phys), poff + i));
+ }
}
static int xswap_map_clusters(struct swap_info_struct *si,
diff --git a/mm/zswap.c b/mm/zswap.c
index c367fd04551d..dfb7ea058d66 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1161,7 +1161,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
/* Reserve the destination before dropping the zswap copy. */
if (is_xswap) {
- phys = xswap_backend_alloc(swpentry, may_enter_fs);
+ phys = xswap_backend_alloc(swpentry, 0, may_enter_fs);
if (!phys.val) {
ret = -ENOMEM;
goto err;
@@ -1206,7 +1206,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
err:
if (is_xswap && phys.val)
- xswap_backend_free(swpentry, phys);
+ xswap_backend_free(swpentry, phys, 0);
swap_cache_del_folio(folio);
folio_unlock(folio);
folio_put(folio);
--
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 ` Baoquan He [this message]
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-16-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