All of lore.kernel.org
 help / color / mirror / Atom feed
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 27/32] mm, swap: keep a per-cluster backend record for every swap device
Date: Fri,  4 Dec 2026 02:13:14 +0800	[thread overview]
Message-ID: <20261203181319.901844-28-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

xswap keeps an xs_table per cluster to record the physical slot backing
each xswap slot.  Give every swap device that table instead, renamed to
backend_table, so a future user does not need a second per-cluster
array.  The table is allocated on demand and freed with the cluster in
__free_cluster() and at teardown; a device that never records a backend
pays only the pointer.

At teardown, give the backend records back before the swap table that
holds the owner id.  Allocate the table without entering direct reclaim:
the caller's folio is locked, and reclaim could wait for it.

Only xswap uses the table for now, so this is a no-op.

Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 mm/swap.h     |   4 +-
 mm/swapfile.c | 112 ++++++++++++++++++++++++++++----------------------
 2 files changed, 63 insertions(+), 53 deletions(-)

diff --git a/mm/swap.h b/mm/swap.h
index 2cd87a26843a..cdb82504269b 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -58,9 +58,7 @@ struct swap_cluster_info {
 	u8 order;
 	atomic_long_t __rcu *table;	/* Swap table entries, see mm/swap_table.h */
 	unsigned int *extend_table;	/* For large swap count, protected by ci->lock */
-#ifdef CONFIG_XSWAP
-	unsigned long *xs_table;	/* Physical backend per slot, lazily allocated */
-#endif
+	unsigned long *backend_table;
 #ifdef CONFIG_MEMCG
 	struct swap_memcg_table *memcg_table;	/* Swap table entries' cgroup record */
 #endif
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 2bd573b159ee..4bf4d4d3f597 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -63,6 +63,9 @@
 /* File-less, so /proc/swaps shows this name; swapoff -a passes it back. */
 #define XSWAP_NAME	"xswap"
 
+static void swap_backend_table_free(struct swap_info_struct *si,
+				    struct swap_cluster_info *ci);
+
 #ifdef CONFIG_XSWAP
 /*
  * Clusters mapped per grow: one page worth of cluster_info, so the
@@ -93,8 +96,6 @@ static void xswap_free_phys_slot(struct swap_info_struct *si,
 static void xswap_release_slot_backend(struct swap_info_struct *si,
 				       struct swap_cluster_info *ci,
 				       unsigned int slot);
-static void xswap_free_xs_table(struct swap_info_struct *si,
-				struct swap_cluster_info *ci);
 static void xswap_mark_cache_only(struct swap_info_struct *si,
 				  struct swap_cluster_info *ci,
 				  unsigned int slot);
@@ -834,14 +835,11 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si,
 static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci)
 {
 	swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false);
-#ifdef CONFIG_XSWAP
 	/*
 	 * An empty cluster can still hold records. Give them back first:
 	 * swap_cluster_free_table() frees the table the owner ID comes from.
 	 */
-	if (si->flags & SWP_XSWAP)
-		xswap_free_xs_table(si, ci);
-#endif
+	swap_backend_table_free(si, ci);
 	swap_cluster_free_table(ci);
 	move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
 	ci->order = 0;
@@ -2000,7 +1998,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
 			/* count will be 0 after put, slot can be reclaimed */
 			need_reclaim = true;
 #ifdef CONFIG_XSWAP
-			if (ci->xs_table)
+			if (ci->backend_table)
 				xswap_mark_cache_only(si, ci, ci_off);
 #endif
 		}
@@ -2110,7 +2108,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
 		}
 	} while (++ci_off < ci_end);
 #ifdef CONFIG_XSWAP
-	if (ci->xs_table)
+	if (ci->backend_table)
 		xswap_clear_cache_only(si, ci, ci_start, nr);
 #endif
 	swap_cluster_unlock(ci);
@@ -2468,7 +2466,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
 			   (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
 
 #ifdef CONFIG_XSWAP
-		if (ci->xs_table && ci->xs_table[ci_off])
+		if (ci->backend_table && ci->backend_table[ci_off])
 			xswap_release_slot_backend(si, ci, ci_off);
 #endif
 
@@ -3486,9 +3484,9 @@ static void xswap_release_slot_backend(struct swap_info_struct *si,
 	swp_entry_t entry = swp_entry(si->type, cluster_offset(si, ci) + slot);
 	struct swap_info_struct *psi;
 	unsigned short id = xswap_entry_id(ci, slot);
-	swp_entry_t phys = { .val = ci->xs_table[slot] };
+	swp_entry_t phys = { .val = ci->backend_table[slot] };
 
-	WRITE_ONCE(ci->xs_table[slot], 0);
+	WRITE_ONCE(ci->backend_table[slot], 0);
 
 	psi = swap_type_to_info(swp_type(phys));
 	if (psi)
@@ -3517,7 +3515,7 @@ static void xswap_mark_cache_only(struct swap_info_struct *si,
 {
 	struct swap_cluster_info *pci;
 	swp_entry_t xentry = swp_entry(si->type, cluster_offset(si, ci) + slot);
-	swp_entry_t phys = { .val = READ_ONCE(ci->xs_table[slot]) };
+	swp_entry_t phys = { .val = READ_ONCE(ci->backend_table[slot]) };
 	unsigned long swp_tb;
 
 	if (!phys.val)
@@ -3551,7 +3549,7 @@ static void xswap_clear_cache_only(struct swap_info_struct *si,
 		xswp_tb = __swap_table_get(ci, slot);
 		if (!swp_tb_is_folio(xswp_tb) || swp_tb_get_count(xswp_tb) != 1)
 			continue;
-		phys.val = READ_ONCE(ci->xs_table[slot]);
+		phys.val = READ_ONCE(ci->backend_table[slot]);
 		if (!phys.val)
 			continue;
 		pci = __swap_entry_to_cluster(phys);
@@ -3743,7 +3741,7 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset)
 
 			if (xswap_slot_backend(xci, slot).val == phys.val) {
 				id = xswap_entry_id(xci, slot);
-				WRITE_ONCE(xci->xs_table[slot], 0);
+				WRITE_ONCE(xci->backend_table[slot], 0);
 				if (id && !mem_cgroup_swap_memsw_accounted())
 					mem_cgroup_swap_uncharge(id, 1);
 			}
@@ -4113,7 +4111,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si)
 		for (i = 0; i < nr_mapped; i++) {
 			ci = &cluster_info[i];
 			spin_lock(&ci->lock);
-			xswap_free_xs_table(si, ci);
+			swap_backend_table_free(si, ci);
 			if (cluster_table_is_alloced(ci)) {
 				swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true);
 				swap_cluster_free_table(ci);
@@ -4134,6 +4132,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si)
 		ci = cluster_info + i;
 		/* Cluster with bad marks count will have a remaining table */
 		spin_lock(&ci->lock);
+		swap_backend_table_free(si, ci);
 		if (cluster_table_is_alloced(ci)) {
 			swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true);
 			swap_cluster_free_table(ci);
@@ -4686,6 +4685,35 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
 	return maxpages;
 }
 
+/*
+ * Drop the table of a cluster that is going away. A recorded backend is
+ * given back first, or its physical slot and reverse mapping would leak.
+ * Caller holds ci->lock.
+ */
+static void swap_backend_table_free(struct swap_info_struct *si,
+				    struct swap_cluster_info *ci)
+{
+	unsigned long *tbl = ci->backend_table;
+	unsigned int slot;
+
+	if (!tbl)
+		return;
+
+	for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) {
+		unsigned long val = READ_ONCE(tbl[slot]);
+
+#ifdef CONFIG_XSWAP
+		if (val)
+			xswap_release_slot_backend(si, ci, slot);
+#else
+		VM_WARN_ON_ONCE(val);
+#endif
+	}
+
+	WRITE_ONCE(ci->backend_table, NULL);
+	kfree(tbl);
+}
+
 #ifdef CONFIG_XSWAP
 /*
  * Link the new clusters and publish the larger range in one si->lock
@@ -4720,7 +4748,7 @@ static void xswap_publish_clusters(struct swap_info_struct *si,
 /* The physical slot backing @slot, or zero. */
 swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot)
 {
-	unsigned long *xs = READ_ONCE(ci->xs_table);
+	unsigned long *xs = READ_ONCE(ci->backend_table);
 
 	if (!xs || !xs[slot])
 		return (swp_entry_t){};
@@ -4746,12 +4774,12 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
 	}
 
 	/* In zswap, which cannot load a large folio. */
-	if (!ci->xs_table || !ci->xs_table[off])
+	if (!ci->backend_table || !ci->backend_table[off])
 		return 1;
 
-	first.val = ci->xs_table[off];
+	first.val = ci->backend_table[off];
 	for (i = 1; i < nr; i++) {
-		swp_entry_t cur = { .val = ci->xs_table[off + i] };
+		swp_entry_t cur = { .val = ci->backend_table[off + i] };
 
 		if (swp_type(cur) != swp_type(first) ||
 		    swp_offset(cur) != swp_offset(first) + i)
@@ -4762,18 +4790,26 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
 }
 
 /* Cluster lock held. The mapped prefix only grows while the device exists. */
-static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci,
+static unsigned long *xswap_backend_table_locked(struct swap_cluster_info *ci,
 					    gfp_t gfp)
 {
-	unsigned long *xs = ci->xs_table;
+	unsigned long *xs = ci->backend_table;
 
 	if (xs)
 		return xs;
 
 	/* The owning entry is pinned, so the cluster survives the relock. */
 	if (gfp & __GFP_DIRECT_RECLAIM) {
+		unsigned int noreclaim_flags;
+
 		spin_unlock(&ci->lock);
+		/*
+		 * The folio being stored is locked.  Do not recurse into
+		 * reclaim, which could wait for that folio.
+		 */
+		noreclaim_flags = memalloc_noreclaim_save();
 		xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp);
+		memalloc_noreclaim_restore(noreclaim_flags);
 		spin_lock(&ci->lock);
 	} else {
 		xs = kcalloc(SWAPFILE_CLUSTER, sizeof(*xs), gfp);
@@ -4781,9 +4817,9 @@ static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci,
 
 	if (!xs)
 		return NULL;
-	if (cmpxchg(&ci->xs_table, NULL, xs)) {
+	if (cmpxchg(&ci->backend_table, NULL, xs)) {
 		kfree(xs);
-		xs = ci->xs_table;
+		xs = ci->backend_table;
 	}
 	return xs;
 }
@@ -4794,7 +4830,7 @@ static unsigned long *xswap_xs_table_locked(struct swap_cluster_info *ci,
 static bool xswap_slot_set_backend(struct swap_cluster_info *ci,
 				   unsigned int slot, swp_entry_t phys)
 {
-	unsigned long *xs = xswap_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH |
+	unsigned long *xs = xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH |
 						  __GFP_NOMEMALLOC);
 
 	if (!xs)
@@ -4803,30 +4839,6 @@ static bool xswap_slot_set_backend(struct swap_cluster_info *ci,
 	return true;
 }
 
-/*
- * Drop the table of cluster @idx and every backend it still records. The
- * cluster is going away, so nothing else will: the physical slots would stay
- * allocated and their reverse mappings would keep pointing at an xswap entry
- * whose cluster_info is about to be unmapped. Caller holds ci->lock.
- */
-static void xswap_free_xs_table(struct swap_info_struct *si,
-				struct swap_cluster_info *ci)
-{
-	unsigned long *xs = ci->xs_table;
-	unsigned int slot;
-
-	if (!xs)
-		return;
-
-	for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) {
-		if (xs[slot])
-			xswap_release_slot_backend(si, ci, slot);
-	}
-
-	kfree(xs);
-	ci->xs_table = NULL;
-}
-
 /* A range of 1 << @order slots, no folio attached. */
 static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si,
 					    unsigned int order)
@@ -4981,7 +4993,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order,
 	ci = swap_cluster_lock(si, off);
 	if (!ci)
 		return (swp_entry_t){};
-	if (!xswap_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) {
+	if (!xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) {
 		swap_cluster_unlock(ci);
 		return (swp_entry_t){};
 	}
@@ -5066,7 +5078,7 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys,
 		ci = swap_cluster_lock(si, off + i);
 		if (ci) {
 			unsigned int slot = (off + i) % SWAPFILE_CLUSTER;
-			unsigned long *xs = ci->xs_table;
+			unsigned long *xs = ci->backend_table;
 
 			if (xs && xs[slot] == phys.val + i) {
 				unsigned short id = xswap_entry_id(ci, slot);
-- 
2.54.0



  parent reply	other threads:[~2026-12-03 18:16 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 ` [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 ` Baoquan He [this message]
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-28-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.