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 02/32] mm, swap: tag a swap table entry with its owning xswap entry
Date: Fri, 4 Dec 2026 02:12:49 +0800 [thread overview]
Message-ID: <20261203181319.901844-3-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
An xswap folio can be written out to a physical device. The data then
lives in a slot on that device, with no folio in the swap cache. Store
the xswap entry in that slot's swap table entry, so the owner of a
physical slot can be found again.
The low three bits of a swap table entry tell its type; 0b100 is free.
Use it to mark an entry that points at its owner, with the xswap type and
offset in the bits above.
Two paths hand out or cache a slot and know only the other two types.
Make them skip a pointer entry: cluster_scan_range() must not hand it out
as free, and __swap_cache_add_check() must not put a folio over it.
Otherwise readahead on a physical device can walk into it.
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_state.c | 3 +++
mm/swap_table.h | 61 +++++++++++++++++++++++++++++++++++++++++++++----
mm/swapfile.c | 3 +++
3 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 90cd99c25dfe..7298a9894737 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -189,6 +189,9 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
return -ENOENT;
ci_off = swp_cluster_offset(targ_entry);
old_tb = __swap_table_get(ci, ci_off);
+ /* Readahead of a physical device can hit an xswap-backing slot. */
+ if (swp_tb_is_pointer(old_tb))
+ return -ENOENT;
if (swp_tb_is_folio(old_tb))
return -EEXIST;
/*
diff --git a/mm/swap_table.h b/mm/swap_table.h
index 072625d60d54..eadd0547d2ad 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -28,7 +28,7 @@ struct swap_memcg_table {
* NULL: |---------------- 0 ---------------| - Free slot
* Shadow: |SWAP_COUNT|Z|---- SHADOW_VAL ---|1| - Swapped out slot
* PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot
- * Pointer: |----------- Pointer ----------|100| - (Unused)
+ * Pointer: |C|-- xswap type/offset --|100| - Backend slot
* Bad: |------------- 1 -------------|1000| - Bad slot
* HIB: | 0 |------- 1 -------|10000| - Hibernation slot
*
@@ -50,9 +50,9 @@ struct swap_memcg_table {
* - PFN: Swap slot is in use, and cached. Memcg info is recorded on the page
* struct.
*
- * - Pointer: Unused yet. `0b100` is reserved for potential pointer usage
- * because only the lower three bits can be used as a marker for 8 bytes
- * aligned pointers.
+ * - Pointer: A physical swap slot backed by an xswap entry. `0b100` marks it
+ * and the xswap type and offset go above it; C is SWP_RMAP_CACHE_ONLY. See
+ * the layout in the CONFIG_XSWAP block below.
*
* - Bad: Swap slot is reserved, protects swap header or holes on swap devices.
*
@@ -90,6 +90,59 @@ struct swap_memcg_table {
/* Hibernation slot: ends with 0b10000, no count, rests of bits are all 1 */
#define SWP_TB_HIB (((~0UL) << 4) & ~SWP_TB_COUNT_MASK)
+#ifdef CONFIG_XSWAP
+/*
+ * 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|
+ *
+ * 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.
+ */
+#define SWP_TB_PTR_MARK 0b100UL
+#define SWP_TB_PTR_OFF_BITS 53
+#define SWP_TB_PTR_TYPE_BITS 8
+#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)
+
+static inline bool swp_tb_is_pointer(unsigned long swp_tb)
+{
+ return (swp_tb & (BIT(3) - 1)) == SWP_TB_PTR_MARK;
+}
+
+static inline unsigned long xswap_entry_to_rmap(swp_entry_t entry)
+{
+ unsigned long type = swp_type(entry);
+ unsigned long off = swp_offset(entry);
+
+ VM_WARN_ON_ONCE(type > SWP_TB_PTR_TYPE_MASK);
+ VM_WARN_ON_ONCE(off > SWP_TB_PTR_OFF_MASK);
+ return (type << SWP_TB_PTR_TYPE_SHIFT) |
+ (off << SWP_TB_PTR_OFF_SHIFT) |
+ SWP_TB_PTR_MARK;
+}
+
+static inline swp_entry_t xswap_rmap_to_entry(unsigned long swp_tb)
+{
+ unsigned long type = (swp_tb >> SWP_TB_PTR_TYPE_SHIFT) &
+ SWP_TB_PTR_TYPE_MASK;
+ unsigned long off = (swp_tb >> SWP_TB_PTR_OFF_SHIFT) &
+ SWP_TB_PTR_OFF_MASK;
+
+ return swp_entry(type, off);
+}
+#else /* !CONFIG_XSWAP */
+static inline bool swp_tb_is_pointer(unsigned long swp_tb)
+{
+ return false;
+}
+#endif /* CONFIG_XSWAP */
+
/* Macro for shadow offset calculation */
#define SWAP_COUNT_SHIFT SWP_TB_FLAGS_BITS
diff --git a/mm/swapfile.c b/mm/swapfile.c
index e15b20274dfb..7b6c7e9614b5 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1133,6 +1133,9 @@ static bool cluster_scan_range(struct swap_info_struct *si,
swp_tb = __swap_table_get(ci, ci_off);
if (swp_tb_is_null(swp_tb))
continue;
+ /* Backs an xswap entry: the slot is not allocatable. */
+ if (swp_tb_is_pointer(swp_tb))
+ return false;
if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
if (!vm_swap_full())
return false;
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:13 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 ` Baoquan He [this message]
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 ` [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-3-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