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 32/32] mm, swap: only run the xswap backend hooks on xswap devices
Date: Fri,  4 Dec 2026 02:13:19 +0800	[thread overview]
Message-ID: <20261203181319.901844-33-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

The backend table used to exist only on xswap devices, so the xswap
hooks in the slot free and swapout paths tested for the table.  zswap
now records its entries there too, so a table no longer tells whether
the device can own a physical backend.

Test the device type instead.  The table pointer is resolved once per
range, and a slot that still holds a zswap entry skips the backend
release without the out-of-line call.  The teardown path that drops a
cluster's table does the same: an untagged handle on a non-xswap device
is reported instead of being handed to the xswap release.

Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
 mm/swapfile.c | 48 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 3098a4086cdb..a55f96623037 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -111,6 +111,18 @@ static inline bool swap_backend_is_zswap(unsigned long val)
 static void swap_backend_table_free(struct swap_info_struct *si,
 				    struct swap_cluster_info *ci);
 
+#ifndef CONFIG_XSWAP
+static inline void xswap_mark_cache_only(struct swap_info_struct *si,
+					 struct swap_cluster_info *ci,
+					 unsigned int slot) { }
+static inline void xswap_clear_cache_only(struct swap_info_struct *si,
+					  struct swap_cluster_info *ci,
+					  unsigned int start, unsigned int nr) { }
+static inline void xswap_release_slot_backend(struct swap_info_struct *si,
+					      struct swap_cluster_info *ci,
+					      unsigned int slot) { }
+#endif
+
 #ifdef CONFIG_XSWAP
 /*
  * Clusters mapped per grow: one page worth of cluster_info, so the
@@ -2027,8 +2039,12 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
 	unsigned int nr_reclaimed;
 	unsigned long swp_tb;
 	int ci_batch = -1;
+	unsigned long *backend_tbl = NULL;
 
 	ci = swap_cluster_lock(si, offset);
+	/* Only an xswap slot can own a physical backend. */
+	if (IS_ENABLED(CONFIG_XSWAP) && (si->flags & SWP_XSWAP))
+		backend_tbl = ci->backend_table;
 	ci_off = offset % SWAPFILE_CLUSTER;
 	ci_end = ci_off + nr;
 	do {
@@ -2042,10 +2058,8 @@ 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->backend_table)
+			if (backend_tbl)
 				xswap_mark_cache_only(si, ci, ci_off);
-#endif
 		}
 		/*
 		 * A count != 1 or cached slot can't be freed. Put its swap
@@ -2152,10 +2166,9 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
 			goto failed;
 		}
 	} while (++ci_off < ci_end);
-#ifdef CONFIG_XSWAP
-	if (ci->backend_table)
+	if (IS_ENABLED(CONFIG_XSWAP) && (si->flags & SWP_XSWAP) &&
+	    ci->backend_table)
 		xswap_clear_cache_only(si, ci, ci_start, nr);
-#endif
 	swap_cluster_unlock(ci);
 	return 0;
 failed:
@@ -2496,6 +2509,10 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
 	unsigned long ci_head = cluster_offset(si, ci);
 	unsigned int batch_off = ci_off;
 	bool is_xswap = si->flags & SWP_XSWAP;
+	unsigned long *backend_tbl = NULL;
+
+	if (IS_ENABLED(CONFIG_XSWAP) && is_xswap)
+		backend_tbl = ci->backend_table;
 
 	VM_WARN_ON(ci->count < nr_pages);
 
@@ -2510,10 +2527,12 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
 		VM_WARN_ON(!swp_tb_is_hibernation(old_tb) &&
 			   (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
 
-#ifdef CONFIG_XSWAP
-		if (ci->backend_table && ci->backend_table[ci_off])
-			xswap_release_slot_backend(si, ci, ci_off);
-#endif
+		if (backend_tbl) {
+			unsigned long val = READ_ONCE(backend_tbl[ci_off]);
+
+			if (val && !swap_backend_is_zswap(val))
+				xswap_release_slot_backend(si, ci, ci_off);
+		}
 
 		/* Resetting the slot to NULL also clears the inline flags. */
 		__swap_table_set(ci, ci_off, null_to_swp_tb());
@@ -4938,8 +4957,13 @@ static void swap_backend_table_free(struct swap_info_struct *si,
 			continue;
 		}
 #ifdef CONFIG_XSWAP
-		if (val)
-			xswap_release_slot_backend(si, ci, slot);
+		if (!val)
+			continue;
+		if (!(si->flags & SWP_XSWAP)) {
+			VM_WARN_ON_ONCE(1);
+			continue;
+		}
+		xswap_release_slot_backend(si, ci, slot);
 #else
 		VM_WARN_ON_ONCE(val);
 #endif
-- 
2.54.0



      parent reply	other threads:[~2026-12-03 18:17 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 ` [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 ` Baoquan He [this message]

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-33-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