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 28/32] mm, swap: encode the per-slot backend record
Date: Fri,  4 Dec 2026 02:13:15 +0800	[thread overview]
Message-ID: <20261203181319.901844-29-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>

From: Baoquan He <hebaoquan@kylinos.cn>

A backend record holds either the physical swap entry that backs an
xswap slot, or, once zswap records its entries here, a zswap entry.
Encode the two so they can share a word:

  0          no backend
  entry << 1 physical backend swap entry
  ptr | 1    zswap entry pointer

A zswap entry comes from a slab cache and is aligned to at least
sizeof(void *), so bit 0 is free for the tag.  Tagging it in place
leaves every other bit of the pointer alone, including any PAC/MTE tag,
and works on 32-bit too.  The physical entry is shifted, so it needs
the top bit of the entry clear; swp_entry() leaves it clear.

A word from this table must never be handed to the swp_tb_* helpers; it
is only a swp_entry_t after swap_backend_decode_phys().

Only xswap writes the table today, so this is a no-op.

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

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 4bf4d4d3f597..96019307ce75 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -63,6 +63,38 @@
 /* File-less, so /proc/swaps shows this name; swapoff -a passes it back. */
 #define XSWAP_NAME	"xswap"
 
+/*
+ * The backend table holds one word per slot:
+ *
+ *   0          - no backend
+ *   entry << 1 - physical backend swap entry (bit 0 clear)
+ *   ptr | 1    - zswap entry pointer (bit 0 set)
+ *
+ * A zswap entry comes from a slab cache, so it is aligned to at least
+ * sizeof(void *): bit 0 is free for the tag.  Only that bit is touched:
+ * the rest of the pointer, including any PAC/MTE tag, is kept as is.
+ *
+ * This is not the swap table: a word here must never be handed to the
+ * swp_tb_* helpers, and the shifted entry is not a swp_entry_t until it
+ * goes through swap_backend_decode_phys().
+ */
+static inline unsigned long swap_backend_encode_phys(swp_entry_t entry)
+{
+	/* The shift needs the top bit free.  swp_entry() leaves it clear. */
+	VM_WARN_ON_ONCE(entry.val & (1UL << (BITS_PER_LONG - 1)));
+	return entry.val << 1;
+}
+
+static inline swp_entry_t swap_backend_decode_phys(unsigned long val)
+{
+	return (swp_entry_t){ .val = val >> 1 };
+}
+
+static inline bool swap_backend_is_zswap(unsigned long val)
+{
+	return val & 1UL;
+}
+
 static void swap_backend_table_free(struct swap_info_struct *si,
 				    struct swap_cluster_info *ci);
 
@@ -3484,8 +3516,18 @@ 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->backend_table[slot] };
+	unsigned long val = READ_ONCE(ci->backend_table[slot]);
+	swp_entry_t phys;
 
+	/*
+	 * Still in zswap: zswap_invalidate() drops the entry once the slot
+	 * is released, and this pointer must not be taken for a physical
+	 * slot here.
+	 */
+	if (!val || swap_backend_is_zswap(val))
+		return;
+
+	phys = swap_backend_decode_phys(val);
 	WRITE_ONCE(ci->backend_table[slot], 0);
 
 	psi = swap_type_to_info(swp_type(phys));
@@ -3515,11 +3557,13 @@ 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->backend_table[slot]) };
+	unsigned long val = READ_ONCE(ci->backend_table[slot]);
 	unsigned long swp_tb;
+	swp_entry_t phys;
 
-	if (!phys.val)
+	if (!val || swap_backend_is_zswap(val))
 		return;			/* still in zswap, no backend */
+	phys = swap_backend_decode_phys(val);
 	pci = __swap_entry_to_cluster(phys);
 	spin_lock(&pci->lock);
 	swp_tb = __swap_table_get(pci, swp_cluster_offset(phys));
@@ -3543,15 +3587,16 @@ static void xswap_clear_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);
-		unsigned long xswp_tb, pswp_tb;
+		unsigned long xswp_tb, pswp_tb, val;
 		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->backend_table[slot]);
-		if (!phys.val)
+		val = READ_ONCE(ci->backend_table[slot]);
+		if (!val || swap_backend_is_zswap(val))
 			continue;
+		phys = swap_backend_decode_phys(val);
 		pci = __swap_entry_to_cluster(phys);
 		spin_lock(&pci->lock);
 		pswp_tb = __swap_table_get(pci, swp_cluster_offset(phys));
@@ -4702,8 +4747,9 @@ static void swap_backend_table_free(struct swap_info_struct *si,
 	for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) {
 		unsigned long val = READ_ONCE(tbl[slot]);
 
+		VM_WARN_ON_ONCE(swap_backend_is_zswap(val));
 #ifdef CONFIG_XSWAP
-		if (val)
+		if (val && !swap_backend_is_zswap(val))
 			xswap_release_slot_backend(si, ci, slot);
 #else
 		VM_WARN_ON_ONCE(val);
@@ -4748,12 +4794,17 @@ 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->backend_table);
+	unsigned long *tbl = READ_ONCE(ci->backend_table);
+	unsigned long val;
+
+	if (!tbl)
+		return (swp_entry_t){};
 
-	if (!xs || !xs[slot])
+	val = READ_ONCE(tbl[slot]);
+	if (!val || swap_backend_is_zswap(val))
 		return (swp_entry_t){};
 
-	return (swp_entry_t){ .val = xs[slot] };
+	return swap_backend_decode_phys(val);
 }
 
 /*
@@ -4763,6 +4814,7 @@ swp_entry_t xswap_slot_backend(struct swap_cluster_info *ci, unsigned int slot)
 int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
 			unsigned int nr)
 {
+	unsigned long val;
 	swp_entry_t first;
 	unsigned int i;
 
@@ -4774,13 +4826,20 @@ int xswap_check_backing(struct swap_cluster_info *ci, unsigned int off,
 	}
 
 	/* In zswap, which cannot load a large folio. */
-	if (!ci->backend_table || !ci->backend_table[off])
+	if (!ci->backend_table)
+		return 1;
+	val = READ_ONCE(ci->backend_table[off]);
+	if (!val || swap_backend_is_zswap(val))
 		return 1;
+	first = swap_backend_decode_phys(val);
 
-	first.val = ci->backend_table[off];
 	for (i = 1; i < nr; i++) {
-		swp_entry_t cur = { .val = ci->backend_table[off + i] };
+		swp_entry_t cur;
 
+		val = READ_ONCE(ci->backend_table[off + i]);
+		if (!val || swap_backend_is_zswap(val))
+			break;
+		cur = swap_backend_decode_phys(val);
 		if (swp_type(cur) != swp_type(first) ||
 		    swp_offset(cur) != swp_offset(first) + i)
 			break;
@@ -4835,7 +4894,7 @@ static bool xswap_slot_set_backend(struct swap_cluster_info *ci,
 
 	if (!xs)
 		return false;
-	WRITE_ONCE(xs[slot], phys.val);
+	WRITE_ONCE(xs[slot], swap_backend_encode_phys(phys));
 	return true;
 }
 
@@ -5078,12 +5137,13 @@ 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->backend_table;
+			unsigned long *tbl = ci->backend_table;
 
-			if (xs && xs[slot] == phys.val + i) {
+			if (tbl && tbl[slot] == swap_backend_encode_phys(
+					swp_entry(swp_type(phys), poff + i))) {
 				unsigned short id = xswap_entry_id(ci, slot);
 
-				WRITE_ONCE(xs[slot], 0);
+				WRITE_ONCE(tbl[slot], 0);
 				if (id && !mem_cgroup_swap_memsw_accounted())
 					mem_cgroup_swap_uncharge(id, 1);
 			}
-- 
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 ` Baoquan He [this message]
2026-12-03 18:13 ` [PATCH v2 29/32] mm, swap: add accessors for the backend record 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-29-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