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 05/32] mm, swap: support swapoff of an xswap physical backend
Date: Fri,  4 Dec 2026 02:12:52 +0800	[thread overview]
Message-ID: <20261203181319.901844-6-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

swapoff of a real swap device iterates its slots through try_to_unuse()
and reads each one back into memory. A slot that was written back
from an xswap entry has no folio in the swap cache, so the loop
skips it and the slot is freed without ever reading the data. While
the xswap entry will keep pointing at a slot that no longer holds
anything.

Now change try_to_unuse() to analyze such a slot through its reverse
mapping: bring the folio back, mark it dirty so that reclaim stores it
into zswap again, and only then drop the backend.

Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 mm/swapfile.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 149 insertions(+), 1 deletion(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 9eb4284e9bc0..a9d7ba3d2692 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -43,6 +43,7 @@
 #include <linux/suspend.h>
 #include <linux/zswap.h>
 #include <linux/plist.h>
+#include <linux/swap_ops.h>
 #include <linux/debugfs.h>
 
 #include <asm/tlbflush.h>
@@ -3361,6 +3362,140 @@ static void xswap_release_slot_backend(struct swap_info_struct *si,
 		xswap_free_phys_slot(psi, entry, phys);
 }
 
+/*
+ * 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.
+ */
+static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset)
+{
+	struct swap_cluster_info *pci, *xci;
+	struct swap_info_struct *xsi;
+	struct swap_io_ctx ctx = {};
+	struct mempolicy *mpol;
+	struct folio *folio;
+	unsigned long mapped, swp_tb, xoff;
+	swp_entry_t xentry, phys;
+	bool drop = false;
+	int ret = 0;
+
+	phys = swp_entry(si->type, offset);
+
+	pci = swap_cluster_lock(si, offset);
+	if (!pci)
+		return 0;
+	/*
+	 * The cluster can have emptied since find_next_to_unuse() read its
+	 * count without the lock, and freeing a cluster clears its table.
+	 */
+	if (!cluster_table_is_alloced(pci)) {
+		swap_cluster_unlock(pci);
+		return 0;
+	}
+	swp_tb = __swap_table_get(pci, offset % SWAPFILE_CLUSTER);
+	if (!swp_tb_is_pointer(swp_tb)) {
+		swap_cluster_unlock(pci);
+		return 0;
+	}
+	xentry = xswap_rmap_to_entry(swp_tb);
+	swap_cluster_unlock(pci);
+
+	xoff = swp_offset(xentry);
+
+	/*
+	 * Pin the owner before reading its flags or cluster_info:
+	 * free_swap_cluster_info() only unmaps that after killing si->users.
+	 * No owner left means the slot is dropped below.
+	 */
+	xsi = swap_type_to_info(swp_type(xentry));
+	if (!xsi || !get_swap_device_info(xsi))
+		goto out_free;
+	/* The record can outlive the device, or name a reused type slot. */
+	if (!(xsi->flags & SWP_XSWAP)) {
+		put_swap_device(xsi);
+		goto out_free;
+	}
+
+	/*
+	 * An owner's slot is inside its mapped range; one past it is gone.
+	 * Pairs with the smp_store_release() in xswap_map_clusters().
+	 */
+	mapped = smp_load_acquire(&xsi->nr_clusters_mapped);
+	if (xoff >= mapped * SWAPFILE_CLUSTER) {
+		/* Owner gone, so the slot is an orphan and has to go. */
+		drop = true;
+		goto out_put;
+	}
+
+	/*
+	 * Bring the data back before the slot goes. A dirty folio is what
+	 * reclaim stores into zswap next time.
+	 */
+	folio = swap_cache_get_folio(xentry);
+	if (!folio) {
+		mpol = get_task_policy(current);
+		folio = __swap_cache_alloc_folio(xentry, GFP_HIGHUSER_MOVABLE,
+						 BIT(0), NULL, mpol,
+						 NO_INTERLEAVE_INDEX);
+		if (IS_ERR(folio)) {
+			ret = PTR_ERR(folio) == -ENOMEM ? -ENOMEM : -EAGAIN;
+			goto out_put;
+		}
+		/* A folio off the LRU would be lost if a fault took it. */
+		folio_add_lru(folio);
+		swap_read_folio(&ctx, folio);
+		swap_read_submit(&ctx);
+		folio_lock(folio);
+	} else {
+		folio_lock(folio);
+	}
+
+	if (folio_matches_swap_entry(folio, xentry)) {
+		folio_wait_writeback(folio);
+		if (unlikely(!folio_test_uptodate(folio))) {
+			/* The read itself failed; a retry cannot help. */
+			swap_cache_del_folio(folio);
+			ret = -EIO;
+		} else {
+			folio_mark_dirty(folio);
+			drop = true;
+		}
+	} else {
+		/* Taken from under us, so nothing was read back either. */
+		ret = -EAGAIN;
+	}
+	folio_unlock(folio);
+	folio_put(folio);
+
+	/*
+	 * The slot is going away, so drop the record that names it. The
+	 * mapped range cannot shrink while the device is pinned, so @xoff
+	 * still has a cluster here whenever @drop is set.
+	 */
+	if (drop) {
+		xci = swap_cluster_lock(xsi, xoff);
+		if (xci) {
+			if (xswap_slot_backend(xci, xoff % SWAPFILE_CLUSTER).val ==
+			    phys.val)
+				WRITE_ONCE(xci->xs_table[xoff % SWAPFILE_CLUSTER],
+					   0);
+			swap_cluster_unlock(xci);
+		}
+	}
+out_put:
+	put_swap_device(xsi);
+	if (!drop)
+		return ret;
+out_free:
+	/* Leaving the mapping in place would make the unuse loop spin. */
+	xswap_free_phys_slot(si, xentry, phys);
+	return 0;
+}
+#else
+static inline int xswap_unuse_rmap(struct swap_info_struct *si,
+				   unsigned long offset)
+{
+	return 0;
+}
 #endif /* CONFIG_XSWAP */
 static int try_to_unuse(unsigned int type)
 {
@@ -3420,8 +3555,21 @@ static int try_to_unuse(unsigned int type)
 
 		entry = swp_entry(type, i);
 		folio = swap_cache_get_folio(entry);
-		if (!folio)
+		if (!folio) {
+			/*
+			 * Only a physical backend holds pointer entries. An
+			 * xswap device's slots are shadows, and its
+			 * cluster_info is what find_next_to_unuse() reads
+			 * without the lock.
+			 */
+			if (!(si->flags & SWP_XSWAP)) {
+				retval = xswap_unuse_rmap(si, i);
+				/* A lost race is retried on the next pass. */
+				if (retval && retval != -EAGAIN)
+					return retval;
+			}
 			continue;
+		}
 
 		/*
 		 * It is conceivable that a racing task removed this folio from
-- 
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 ` Baoquan He [this message]
2026-12-03 18:12 ` [PATCH v2 06/32] mm, swap: use the xswap physical backend 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 ` [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-6-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