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 08/32] mm, swap: reclaim physical slots backing cache-only xswap entries
Date: Fri,  4 Dec 2026 02:12:55 +0800	[thread overview]
Message-ID: <20261203181319.901844-9-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

The swap count of an xswap entry can drop to 0 while its folio is still
in the swap cache, which leaves the physical slot redundant but occupied
until the folio leaves the cache. This happens when the page is faulted
back in during its write to the backend: do_swap_page() does not wait for
writeback, folio_free_swap() refuses a folio under writeback, and a
real-device write does not drop the cache when it completes.

Mark the slot with a cache-only bit in the swap table entry that points
back to the owner, set when the count drops to 0 and cleared when the
slot is reused. Check that the physical slot still points back to the
entry before touching the bit. The physical reclaim scanner already walks
full clusters; make it free the slots with the bit set. It reads the bit
directly, without the xswap cluster lock, which would invert the lock
order. A cache-only slot in a non-full cluster waits until the cluster
fills.

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/swap_table.h |  37 ++++++++++-
 mm/swapfile.c   | 168 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 201 insertions(+), 4 deletions(-)

diff --git a/mm/swap_table.h b/mm/swap_table.h
index eadd0547d2ad..f27cb1ba385e 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -95,21 +95,52 @@ struct swap_memcg_table {
  * Pointer-tagged swap table entry: the reverse map from a physical slot to
  * the xswap entry it backs. Layout:
  *
- *   Pointer:  | xswap_type(8) | xswap_offset(53) |100|
+ *   Pointer:  |C| xswap_type(7) | xswap_offset(53) |100|
  *
  * The low three bits identify the entry: 0b000 is a free, bad or hibernation
  * slot, a shadow entry ends in 0b01 and a cached folio in 0b10, which leaves
- * 0b100 as the only marker still free.
+ * 0b100 as the only marker still free. C is SWP_RMAP_CACHE_ONLY, the one
+ * bit left at the top.
  */
 #define SWP_TB_PTR_MARK			0b100UL
 #define SWP_TB_PTR_OFF_BITS		53
-#define SWP_TB_PTR_TYPE_BITS		8
+#define SWP_TB_PTR_TYPE_BITS		7
 #define SWP_TB_PTR_OFF_SHIFT		3
 #define SWP_TB_PTR_TYPE_SHIFT		(SWP_TB_PTR_OFF_SHIFT + \
 					 SWP_TB_PTR_OFF_BITS)
 #define SWP_TB_PTR_OFF_MASK		((1UL << SWP_TB_PTR_OFF_BITS) - 1)
 #define SWP_TB_PTR_TYPE_MASK		((1UL << SWP_TB_PTR_TYPE_BITS) - 1)
 
+/*
+ * Set when the xswap entry owning a physical slot has swap count 0 but its
+ * folio is still in the swap cache. The slot is redundant then, and the
+ * physical reclaim scanner may free it. The bit sits on the physical slot
+ * so that scanner can read it without the xswap cluster lock.
+ */
+#define SWP_RMAP_CACHE_ONLY		(1UL << (BITS_PER_LONG - 1))
+
+/* swp_type() must fit, and SWP_RMAP_CACHE_ONLY must own the top bit. */
+static_assert(BITS_PER_LONG - SWP_TYPE_SHIFT <= SWP_TB_PTR_TYPE_BITS,
+	      "xswap rmap type field is too narrow");
+static_assert(SWP_TB_PTR_TYPE_SHIFT + SWP_TB_PTR_TYPE_BITS == BITS_PER_LONG - 1,
+	      "xswap rmap fields do not pack");
+
+static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *ci,
+					     unsigned int off)
+{
+	atomic_long_t *table = rcu_dereference_check(ci->table, true);
+
+	atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]);
+}
+
+static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
+					      unsigned int off)
+{
+	atomic_long_t *table = rcu_dereference_check(ci->table, true);
+
+	atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]);
+}
+
 static inline bool swp_tb_is_pointer(unsigned long swp_tb)
 {
 	return (swp_tb & (BIT(3) - 1)) == SWP_TB_PTR_MARK;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index a9d7ba3d2692..aeaf36776ef7 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -95,6 +95,14 @@ static void xswap_release_slot_backend(struct swap_info_struct *si,
 				       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);
+static void xswap_clear_cache_only(struct swap_info_struct *si,
+				   struct swap_cluster_info *ci,
+				   unsigned int start, unsigned int nr);
+static int xswap_reclaim_backing(struct swap_info_struct *si,
+				 unsigned long offset, swp_entry_t xentry);
 
 static int xswap_create(int prio);
 static int xswap_create_by_name(const char *name, int swap_flags);
@@ -1324,6 +1332,10 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 	if (force)
 		to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER;
 
+	/*
+	 * Only full clusters are scanned.  A cache-only slot in a non-full
+	 * cluster stays until the cluster fills or the device goes away.
+	 */
 	while ((ci = isolate_lock_cluster(si, &si->full_clusters))) {
 		offset = cluster_offset(si, ci);
 		end = min(si->max, offset + SWAPFILE_CLUSTER);
@@ -1340,6 +1352,18 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 					offset += abs(nr_reclaim);
 					continue;
 				}
+#ifdef CONFIG_XSWAP
+			} else if (swp_tb_is_pointer(swp_tb) &&
+				   (swp_tb & SWP_RMAP_CACHE_ONLY)) {
+				spin_unlock(&ci->lock);
+				nr_reclaim = xswap_reclaim_backing(si, offset,
+						xswap_rmap_to_entry(swp_tb));
+				spin_lock(&ci->lock);
+				if (nr_reclaim) {
+					offset += abs(nr_reclaim);
+					continue;
+				}
+#endif
 			}
 			offset++;
 		}
@@ -1953,6 +1977,10 @@ 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)
+				xswap_mark_cache_only(si, ci, ci_off);
+#endif
 		}
 		/*
 		 * A count != 1 or cached slot can't be freed. Put its swap
@@ -2059,6 +2087,10 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
 			goto failed;
 		}
 	} while (++ci_off < ci_end);
+#ifdef CONFIG_XSWAP
+	if (ci->xs_table)
+		xswap_clear_cache_only(si, ci, ci_start, nr);
+#endif
 	swap_cluster_unlock(ci);
 	return 0;
 failed:
@@ -3362,6 +3394,129 @@ static void xswap_release_slot_backend(struct swap_info_struct *si,
 		xswap_free_phys_slot(psi, entry, phys);
 }
 
+/*
+ * Mark the physical slot backing xswap slot @slot as cache-only. Its folio
+ * is in the swap cache, so the physical copy is redundant. The physical
+ * cluster lock is needed: xswap_free_phys_slot() clears the rmap under it.
+ *
+ * The caller holds the xci lock, so the backend record cannot be cleared.
+ * A live record keeps the physical slot counted, and physical teardown
+ * cannot free cluster_info while the slot is counted.
+ */
+static void xswap_mark_cache_only(struct swap_info_struct *si,
+				  struct swap_cluster_info *ci,
+				  unsigned int slot)
+{
+	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]) };
+	unsigned long swp_tb;
+
+	if (!phys.val)
+		return;			/* still in zswap, no backend */
+	pci = __swap_entry_to_cluster(phys);
+	spin_lock(&pci->lock);
+	swp_tb = __swap_table_get(pci, swp_cluster_offset(phys));
+	if (swp_tb_is_pointer(swp_tb) &&
+	    xswap_rmap_to_entry(swp_tb).val == xentry.val)
+		swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys));
+	spin_unlock(&pci->lock);
+}
+
+/*
+ * Clear the cache-only mark of slots that were re-referenced. A slot that
+ * was cache-only had count 0, so count 1 is exactly the one to clear.
+ */
+static void xswap_clear_cache_only(struct swap_info_struct *si,
+				   struct swap_cluster_info *ci,
+				   unsigned int start, unsigned int nr)
+{
+	unsigned int slot;
+
+	for (slot = start; slot < start + nr; slot++) {
+		struct swap_cluster_info *pci;
+		swp_entry_t xentry = swp_entry(si->type,
+					       cluster_offset(si, ci) + slot);
+		unsigned long xswp_tb, pswp_tb;
+		swp_entry_t phys;
+
+		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]);
+		if (!phys.val)
+			continue;
+		pci = __swap_entry_to_cluster(phys);
+		spin_lock(&pci->lock);
+		pswp_tb = __swap_table_get(pci, swp_cluster_offset(phys));
+		if (swp_tb_is_pointer(pswp_tb) &&
+		    xswap_rmap_to_entry(pswp_tb).val == xentry.val)
+			swap_rmap_clear_cache_only(pci, swp_cluster_offset(phys));
+		spin_unlock(&pci->lock);
+	}
+}
+
+/*
+ * Try to reclaim the physical slot backing cache-only @xentry. The physical
+ * cluster lock must not be held. Returns the slots from @offset to the end
+ * of the slot range, negated if the free failed, or 0 if @offset
+ * turned out not to be the folio's slot.
+ */
+static int xswap_reclaim_backing(struct swap_info_struct *si,
+				 unsigned long offset, swp_entry_t xentry)
+{
+	struct swap_info_struct *xsi;
+	struct swap_cluster_info *xci;
+	swp_entry_t first;
+	struct folio *folio;
+	unsigned long xoff, i;
+	int ret = 0;
+
+	/* @xentry can name a device that is going away right now. */
+	xsi = get_swap_device(xentry);
+	if (IS_ERR_OR_NULL(xsi))
+		return 0;
+
+	folio = swap_cache_get_folio(xentry);
+	if (!folio)
+		goto out_put;
+	if (!folio_trylock(folio)) {
+		folio_put(folio);
+		goto out_put;
+	}
+
+	/*
+	 * The folio must own @xentry, and @offset must be the slot this device
+	 * holds for that page of the folio. Otherwise the rmap went stale.
+	 */
+	if (!folio_matches_swap_entry(folio, xentry))
+		goto out;
+	i = xentry.val - folio->swap.val;
+	xoff = swp_offset(folio->swap);
+	xci = swap_cluster_lock(xsi, xoff);
+	if (!xci)
+		goto out;
+	first = xswap_slot_backend(xci, xoff % SWAPFILE_CLUSTER);
+	swap_cluster_unlock(xci);
+	if (!first.val || swp_type(first) != si->type ||
+	    swp_offset(first) + i != offset)
+		goto out;
+
+	/*
+	 * The rest of the slot range is ours: skip it, whether or not the
+	 * free succeeds.
+	 */
+	ret = folio_nr_pages(folio) - i;
+	if (!folio_free_swap(folio))
+		ret = -ret;
+out:
+	folio_unlock(folio);
+	folio_put(folio);
+out_put:
+	put_swap_device(xsi);
+	return ret;
+}
+
 /*
  * A written-back slot has no folio of its own here, so the unuse loop would
  * skip it. Resolve it through the reverse mapping and free both sides.
@@ -4635,7 +4790,18 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry)
 
 	ci = swap_cluster_lock(si, off);
 	if (ci) {
-		recorded = xswap_slot_set_backend(ci, off % SWAPFILE_CLUSTER, phys);
+		unsigned int slot = off % SWAPFILE_CLUSTER;
+		unsigned long swp_tb;
+
+		recorded = xswap_slot_set_backend(ci, slot, phys);
+		/*
+		 * 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 (recorded && swp_tb_is_folio(swp_tb) &&
+		    !swp_tb_get_count(swp_tb))
+			xswap_mark_cache_only(si, ci, slot);
 		swap_cluster_unlock(ci);
 	}
 
-- 
2.54.0



  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 ` [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 ` Baoquan He [this message]
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-9-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