Linux-mm Archive on 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 29/32] mm, swap: add accessors for the backend record
Date: Fri,  4 Dec 2026 02:13:16 +0800	[thread overview]
Message-ID: <20261203181319.901844-30-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

Add swap_backend_get(), swap_backend_set(), swap_backend_erase() and
swap_backend_present() to read and update a swap device's per-slot
backend record, plus the zswap entry encoding. The next patch wires
zswap to them.

get, erase and present read without the cluster lock: the table is
allocated before the first record is published and freed only when the
cluster is empty, so an in-use slot can always reach it. Updates are
serialized by the folio lock (store, load, writeback) or the cluster
lock (slot free). Only set takes the cluster lock, only to allocate,
and it avoids direct reclaim because the caller's folio is locked.

present() walks a range within one cluster and returns early without a
table; a range that would cross the cluster is refused after a warning.
Setting the same handle is a no-op; only a different handle is reported
as displaced.

Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 mm/swap.h     |   5 ++
 mm/swapfile.c | 208 +++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 170 insertions(+), 43 deletions(-)

diff --git a/mm/swap.h b/mm/swap.h
index cdb82504269b..9b2d8d556bd0 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -226,6 +226,11 @@ static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci)
 
 extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp);
 
+void *swap_backend_get(swp_entry_t entry);
+void *swap_backend_set(swp_entry_t entry, void *handle);
+void *swap_backend_erase(swp_entry_t entry);
+bool swap_backend_present(swp_entry_t entry, unsigned int nr);
+
 /*
  * Below are the core routines for doing swap for a folio.
  * All helpers requires the folio to be locked, and a locked folio
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 96019307ce75..1862f0b93c92 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -90,6 +90,19 @@ static inline swp_entry_t swap_backend_decode_phys(unsigned long val)
 	return (swp_entry_t){ .val = val >> 1 };
 }
 
+static inline unsigned long swap_backend_encode_zswap(void *handle)
+{
+	unsigned long val = (unsigned long)handle;
+
+	VM_WARN_ON_ONCE(val & 1UL);
+	return val | 1UL;
+}
+
+static inline void *swap_backend_decode_zswap(unsigned long val)
+{
+	return (void *)(val & ~1UL);
+}
+
 static inline bool swap_backend_is_zswap(unsigned long val)
 {
 	return val & 1UL;
@@ -4731,9 +4744,151 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
 }
 
 /*
- * 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.
+ * Per-slot backend record of a cluster, allocated on demand and freed
+ * with the cluster.
+ */
+static unsigned long *swap_backend_table_locked(struct swap_cluster_info *ci,
+						gfp_t gfp)
+{
+	unsigned long *tbl = ci->backend_table;
+
+	if (tbl)
+		return tbl;
+
+	/* 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();
+		tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp);
+		memalloc_noreclaim_restore(noreclaim_flags);
+		spin_lock(&ci->lock);
+	} else {
+		tbl = kcalloc(SWAPFILE_CLUSTER, sizeof(*tbl), gfp);
+	}
+
+	if (!tbl)
+		return NULL;
+	if (cmpxchg(&ci->backend_table, NULL, tbl)) {
+		kfree(tbl);
+		tbl = ci->backend_table;
+	}
+	return tbl;
+}
+
+/*
+ * The zswap side of the table.
+ *
+ * The table pointer and its slots are read without taking the cluster lock:
+ * the table is allocated before the first entry is published and freed only
+ * once the cluster has no in-use slot left, so a caller that holds an
+ * in-use slot can always reach it. Per-slot updates are serialized by the
+ * folio lock (store, load and writeback all run with the folio locked) or
+ * by the cluster lock (the slot free path runs under it). Only set takes
+ * the cluster lock, and only to allocate the table.
+ */
+void *swap_backend_get(swp_entry_t swp)
+{
+	struct swap_cluster_info *ci = __swap_entry_to_cluster(swp);
+	unsigned long *tbl = READ_ONCE(ci->backend_table);
+	unsigned long val;
+
+	if (!tbl)
+		return NULL;
+
+	val = READ_ONCE(tbl[swp_cluster_offset(swp)]);
+	return swap_backend_is_zswap(val) ? swap_backend_decode_zswap(val) : NULL;
+}
+
+/* Is any slot in [swp, swp + nr) in zswap? The range sits in one cluster. */
+bool swap_backend_present(swp_entry_t swp, unsigned int nr)
+{
+	struct swap_cluster_info *ci = __swap_entry_to_cluster(swp);
+	unsigned long *tbl = READ_ONCE(ci->backend_table);
+	unsigned int slot = swp_cluster_offset(swp);
+	unsigned int i;
+
+	/* The range is aligned to its own size and never straddles a cluster. */
+	if (slot + nr > SWAPFILE_CLUSTER) {
+		VM_WARN_ON_ONCE(1);
+		return false;
+	}
+
+	if (!tbl)
+		return false;
+
+	for (i = 0; i < nr; i++) {
+		if (swap_backend_is_zswap(READ_ONCE(tbl[slot + i])))
+			return true;
+	}
+	return false;
+}
+
+void *swap_backend_set(swp_entry_t swp, void *handle)
+{
+	struct swap_info_struct *si = __swap_entry_to_info(swp);
+	struct swap_cluster_info *ci = __swap_entry_to_cluster(swp);
+	unsigned int slot = swp_cluster_offset(swp);
+	unsigned long *tbl, old, encoded;
+
+	tbl = READ_ONCE(ci->backend_table);
+	if (!tbl) {
+		ci = swap_cluster_lock(si, swp_offset(swp));
+		tbl = swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH |
+						__GFP_NOMEMALLOC);
+		swap_cluster_unlock(ci);
+		if (!tbl)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	encoded = swap_backend_encode_zswap(handle);
+	old = READ_ONCE(tbl[slot]);
+	/* Re-setting the same handle is a no-op, not a displaced entry. */
+	if (old == encoded)
+		return NULL;
+
+	/*
+	 * A folio written back and then redirtied while still in the swap
+	 * cache is swapped out again into the same slot. The physical copy
+	 * is stale then, and zswap takes the slot over.
+	 */
+#ifdef CONFIG_XSWAP
+	if (old && !swap_backend_is_zswap(old))
+		xswap_backend_free(swp, swap_backend_decode_phys(old), 0);
+#endif
+
+	WRITE_ONCE(tbl[slot], encoded);
+
+	return swap_backend_is_zswap(old) ? swap_backend_decode_zswap(old) : NULL;
+}
+
+void *swap_backend_erase(swp_entry_t swp)
+{
+	struct swap_cluster_info *ci = __swap_entry_to_cluster(swp);
+	unsigned long *tbl = READ_ONCE(ci->backend_table);
+	unsigned int slot = swp_cluster_offset(swp);
+	unsigned long val;
+
+	if (!tbl)
+		return NULL;
+
+	val = READ_ONCE(tbl[slot]);
+	if (!swap_backend_is_zswap(val))
+		return NULL;
+
+	WRITE_ONCE(tbl[slot], 0);
+	return swap_backend_decode_zswap(val);
+}
+
+/*
+ * Drop the table of a cluster that is going away. An untagged value is an
+ * xswap physical backend that has to be given back; a tagged one would be a
+ * leak, the slot free path invalidates those first. Caller holds ci->lock.
  */
 static void swap_backend_table_free(struct swap_info_struct *si,
 				    struct swap_cluster_info *ci)
@@ -4848,56 +5003,23 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
 	return i;
 }
 
-/* Cluster lock held. The mapped prefix only grows while the device exists. */
-static unsigned long *xswap_backend_table_locked(struct swap_cluster_info *ci,
-					    gfp_t gfp)
-{
-	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);
-	}
-
-	if (!xs)
-		return NULL;
-	if (cmpxchg(&ci->backend_table, NULL, xs)) {
-		kfree(xs);
-		xs = ci->backend_table;
-	}
-	return xs;
-}
-
 /*
  * 4K per cluster, and this runs in reclaim, so it has to be able to sleep.
  */
 static bool xswap_slot_set_backend(struct swap_cluster_info *ci,
 				   unsigned int slot, swp_entry_t phys)
 {
-	unsigned long *xs = xswap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH |
-						  __GFP_NOMEMALLOC);
+	unsigned long *tbl = swap_backend_table_locked(ci, GFP_KERNEL |
+						       __GFP_HIGH |
+						       __GFP_NOMEMALLOC);
 
-	if (!xs)
+	if (!tbl)
 		return false;
-	WRITE_ONCE(xs[slot], swap_backend_encode_phys(phys));
+	WRITE_ONCE(tbl[slot], swap_backend_encode_phys(phys));
 	return true;
 }
 
+
 /* A range of 1 << @order slots, no folio attached. */
 static unsigned long xswap_alloc_slot_local(struct swap_info_struct *si,
 					    unsigned int order)
@@ -5052,7 +5174,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_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) {
+	if (!swap_backend_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) {
 		swap_cluster_unlock(ci);
 		return (swp_entry_t){};
 	}
-- 
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 ` [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 ` Baoquan He [this message]
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-30-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