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 30/32] mm, zswap: index entries in the swap device's backend table
Date: Fri, 4 Dec 2026 02:13:17 +0800 [thread overview]
Message-ID: <20261203181319.901844-31-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
Every swap device already has a lazily allocated per-cluster backend
table, so record zswap entries there instead of in a per-type xarray:
O(1) lookup by cluster, no radix tree, and no per-type tree to allocate
at swapon or free at swapoff. The swap table is untouched, so the count
and fault paths are unchanged.
xswap shares the table: an untagged word is its physical backend. A
tagged zswap handle replaced by a physical backend is left for the
writeback path that is freeing it, and released after the data has been
decompressed.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/zswap.h | 7 --
mm/swapfile.c | 21 +++---
mm/zswap.c | 156 ++++++++++++++----------------------------
3 files changed, 59 insertions(+), 125 deletions(-)
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index 6d6c791b1167..c7faad1d1b42 100644
--- a/include/linux/zswap.h
+++ b/include/linux/zswap.h
@@ -28,8 +28,6 @@ unsigned long zswap_total_pages(void);
bool zswap_store(struct folio *folio, bool may_enter_fs);
int zswap_load(struct folio *folio);
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries);
-int zswap_swapon(int type, unsigned long nr_pages);
-void zswap_swapoff(int type);
void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg);
void zswap_lruvec_state_init(struct lruvec *lruvec);
void zswap_folio_swapin(struct folio *folio);
@@ -55,11 +53,6 @@ static inline void zswap_invalidate(int type, pgoff_t offset,
{
}
-static inline int zswap_swapon(int type, unsigned long nr_pages)
-{
- return 0;
-}
-static inline void zswap_swapoff(int type) {}
static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {}
static inline void zswap_lruvec_state_init(struct lruvec *lruvec) {}
static inline void zswap_folio_swapin(struct folio *folio) {}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 1862f0b93c92..66a7682e29f0 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4376,7 +4376,6 @@ static int __swapoff(struct swap_info_struct *p)
spin_unlock(&p->lock);
spin_unlock(&swap_lock);
arch_swap_invalidate_area(p->type);
- zswap_swapoff(p->type);
mutex_unlock(&swapon_mutex);
kfree(p->global_cluster);
p->global_cluster = NULL;
@@ -5141,6 +5140,14 @@ void xswap_backend_drop(swp_entry_t entry, unsigned int order)
ci = swap_cluster_lock(si, off + i);
if (!ci)
continue;
+ /*
+ * A fresh cluster has no table yet, and therefore no old slot range
+ * to drop.
+ */
+ if (!ci->backend_table) {
+ swap_cluster_unlock(ci);
+ continue;
+ }
old = xswap_slot_backend(ci, (off + i) % SWAPFILE_CLUSTER);
swap_cluster_unlock(ci);
if (old.val)
@@ -5678,10 +5685,6 @@ static int xswap_create(int prio)
if (error)
goto bad_swap;
- error = zswap_swapon(si->type, si->max);
- if (error)
- goto bad_swap;
-
mutex_lock(&swapon_mutex);
xswap_debugfs_add(si);
si->prio = prio;
@@ -5952,10 +5955,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
}
}
- error = zswap_swapon(si->type, maxpages);
- if (error)
- goto bad_swap_unlock_inode;
-
/*
* Flush any pending IO and dirty mappings before we start using this
* swap device.
@@ -5964,7 +5963,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
error = inode_drain_writes(inode);
if (error) {
inode->i_flags &= ~S_SWAPFILE;
- goto free_swap_zswap;
+ goto bad_swap_unlock_inode;
}
mutex_lock(&swapon_mutex);
@@ -5998,8 +5997,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
error = 0;
goto out;
-free_swap_zswap:
- zswap_swapoff(si->type);
bad_swap_unlock_inode:
inode_unlock(inode);
bad_swap:
diff --git a/mm/zswap.c b/mm/zswap.c
index dfb7ea058d66..1903367a9576 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -158,10 +158,10 @@ struct crypto_acomp_ctx {
};
/*
- * The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock.
- * The only case where lru_lock is not acquired while holding tree.lock is
- * when a zswap_entry is taken off the lru for writeback, in that case it
- * needs to be verified that it's still valid in the tree.
+ * The lock ordering is swap cluster lock -> zswap_pool.lru_lock.
+ * The only case where lru_lock is not acquired while holding the cluster
+ * lock is when a zswap_entry is taken off the lru for writeback, in that
+ * case it needs to be verified that it is still recorded in the slot.
*/
struct zswap_pool {
struct zs_pool *zs_pool;
@@ -199,7 +199,7 @@ static struct shrinker *zswap_shrinker;
* This structure contains the metadata for tracking a single compressed
* page within zswap.
*
- * swpentry - associated swap entry, the offset indexes into the xarray
+ * swpentry - associated swap entry, the slot's backend record points here
* length - the length in bytes of the compressed page data. Needed during
* decompression.
* referenced - true if the entry recently entered the zswap pool. Unset by the
@@ -231,9 +231,6 @@ static struct zswap_pool *zswap_entry_pool(struct zswap_entry *entry)
return xa_load(&zswap_pools, entry->pool_idx);
}
-static struct xarray *zswap_trees[MAX_SWAPFILES];
-static unsigned int nr_zswap_trees[MAX_SWAPFILES];
-
/* pool counter to provide unique names to zsmalloc */
static atomic_t zswap_pools_count = ATOMIC_INIT(0);
@@ -255,20 +252,6 @@ static bool zswap_has_pool;
* helpers and fwd declarations
**********************************/
-/* One swap address space for each 64M swap space */
-#define ZSWAP_ADDRESS_SPACE_SHIFT 14
-#define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT)
-
-static inline struct xarray *zswap_tree(int type, pgoff_t offset)
-{
- return &zswap_trees[type][offset >> ZSWAP_ADDRESS_SPACE_SHIFT];
-}
-
-static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
-{
- return zswap_tree(swp_type(swp), swp_offset(swp));
-}
-
#define zswap_pool_debug(msg, p) \
pr_debug("%s pool %s\n", msg, (p)->tfm_name)
@@ -1089,8 +1072,6 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct zswap_entry *entry,
swp_entry_t swpentry, bool may_enter_fs)
{
- struct xarray *tree;
- pgoff_t offset = swp_offset(swpentry);
struct folio *folio;
struct mempolicy *mpol;
struct swap_info_struct *si;
@@ -1147,14 +1128,13 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
/*
* folio is locked, and the swapcache is now secured against
* concurrent swapping to and from the slot, and concurrent
- * swapoff so we can safely dereference the zswap tree here.
+ * swapoff so we can safely dereference the backend table here.
* Verify that the swap entry hasn't been invalidated and recycled
* behind our backs, to avoid overwriting a new swap folio with
* old compressed data. Only when this is successful can the entry
* be dereferenced.
*/
- tree = swap_zswap_tree(swpentry);
- if (entry != xa_load(tree, offset)) {
+ if (entry != swap_backend_get(swpentry)) {
ret = -ENOMEM;
goto err;
}
@@ -1173,7 +1153,11 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
goto err;
}
- xa_erase(tree, offset);
+ /*
+ * On an xswap device the physical slot was recorded first, so the
+ * entry this clears was already replaced and nothing is returned.
+ */
+ swap_backend_erase(swpentry);
count_vm_event(ZSWPWB);
if (entry->objcg)
@@ -1205,8 +1189,21 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
return 0;
err:
- if (is_xswap && phys.val)
+ if (is_xswap && phys.val) {
+ void *old;
+
xswap_backend_free(swpentry, phys, 0);
+ /*
+ * Reserving the destination displaced the entry from the
+ * backend table, and it is still in zswap: put it back.
+ * The table exists by now, so this cannot fail. A handle
+ * that was already there is stale; do not leak it.
+ */
+ old = swap_backend_set(swpentry, entry);
+ WARN_ON_ONCE(IS_ERR(old));
+ if (!IS_ERR_OR_NULL(old) && old != entry)
+ zswap_entry_free(old);
+ }
swap_cache_del_folio(folio);
folio_unlock(folio);
folio_put(folio);
@@ -1290,7 +1287,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
/*
* Once the lru lock is dropped, the entry might get freed. The
* swpentry is copied to the stack, and entry isn't deref'd again
- * until the entry is verified to still be alive in the tree.
+ * until the entry is verified to still be in the backend table.
*/
swpentry = entry->swpentry;
@@ -1623,19 +1620,17 @@ static bool zswap_store_page(struct folio *folio, long index,
goto compress_failed;
/*
- * Set pool_idx before the xa_store() below publishes the entry, or a
- * concurrent reader could resolve a stale pool_idx left by slab reuse
- * to an unrelated live pool.
+ * Set pool_idx before swap_backend_set() below publishes the entry,
+ * or a concurrent reader could resolve a stale pool_idx left by slab
+ * reuse to an unrelated live pool.
*/
entry->pool_idx = pool->idx;
- old = xa_store(swap_zswap_tree(page_swpentry),
- swp_offset(page_swpentry),
- entry, GFP_KERNEL);
- if (xa_is_err(old)) {
- int err = xa_err(old);
+ old = swap_backend_set(page_swpentry, entry);
+ if (IS_ERR(old)) {
+ int err = PTR_ERR(old);
- WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
+ WARN_ONCE(err != -ENOMEM, "unexpected backend index error: %d\n", err);
zswap_reject_alloc_fail++;
goto store_failed;
}
@@ -1649,11 +1644,8 @@ static bool zswap_store_page(struct folio *folio, long index,
zswap_entry_free(old);
/*
- * The entry is successfully compressed and stored in the tree, there is
- * no further possibility of failure. Grab refs to the pool and objcg,
- * charge zswap memory, and increment zswap_stored_pages.
- * The opposite actions will be performed by zswap_entry_free()
- * when the entry is removed from the tree.
+ * The entry is stored and cannot fail now. Take the pool and objcg
+ * refs and charge zswap memory; zswap_entry_free() undoes them.
*/
zswap_pool_get(pool);
if (objcg) {
@@ -1665,8 +1657,8 @@ static bool zswap_store_page(struct folio *folio, long index,
atomic_long_inc(&zswap_stored_incompressible_pages);
/*
- * We finish initializing the entry while it's already in xarray.
- * This is safe because:
+ * We finish initializing the entry while it's already in the backend
+ * table. This is safe because:
*
* 1. Concurrent stores and invalidations are excluded by folio lock.
*
@@ -1773,22 +1765,13 @@ bool zswap_store(struct folio *folio, bool may_enter_fs)
*/
bool zswap_is_present(swp_entry_t entry, unsigned int nr)
{
- pgoff_t offset = swp_offset(entry);
- struct xarray *tree = swap_zswap_tree(entry);
- unsigned long index = offset;
-
- if (zswap_never_enabled() || xa_empty(tree))
- return false;
-
/*
* A pinned range is at most SWAPFILE_CLUSTER slots and is aligned to
- * its own size, so one tree covers all of it and a single lookup is
- * enough. Scanning only part of the range would report a false
+ * its own size, so the whole range sits in one cluster and one
+ * table. Scanning only part of the range would report a false
* "absent" and let the caller read a stale copy from the device.
*/
- BUILD_BUG_ON(SWAPFILE_CLUSTER > ZSWAP_ADDRESS_SPACE_PAGES);
-
- return xa_find(tree, &index, offset + nr - 1, XA_PRESENT);
+ return swap_backend_present(entry, nr);
}
/**
@@ -1811,8 +1794,6 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr)
int zswap_load(struct folio *folio)
{
swp_entry_t swp = folio->swap;
- pgoff_t offset = swp_offset(swp);
- struct xarray *tree = swap_zswap_tree(swp);
struct zswap_entry *entry;
VM_WARN_ON_ONCE(!folio_test_locked(folio));
@@ -1836,7 +1817,7 @@ int zswap_load(struct folio *folio)
return -ENOENT;
}
- entry = xa_load(tree, offset);
+ entry = swap_backend_get(swp);
if (!entry)
return -ENOENT;
@@ -1859,7 +1840,7 @@ int zswap_load(struct folio *folio)
* compression work.
*/
folio_mark_dirty(folio);
- xa_erase(tree, offset);
+ swap_backend_erase(swp);
zswap_entry_free(entry);
folio_unlock(folio);
@@ -1868,62 +1849,25 @@ int zswap_load(struct folio *folio)
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries)
{
- struct zswap_entry *entry;
- struct xarray *tree;
unsigned long i;
if (zswap_never_enabled())
return;
+ /*
+ * The slot free path runs under the cluster lock once the slots are
+ * no longer allocatable, and the store rollback holds the folio
+ * lock, so only one caller can see a tagged handle.
+ */
for (i = 0; i < nr_entries; i++) {
- tree = zswap_tree(type, offset + i);
-
- if (xa_empty(tree))
- continue;
+ struct zswap_entry *entry;
- entry = xa_erase(tree, offset + i);
+ entry = swap_backend_erase(swp_entry(type, offset + i));
if (entry)
zswap_entry_free(entry);
}
}
-int zswap_swapon(int type, unsigned long nr_pages)
-{
- struct xarray *trees, *tree;
- unsigned int nr, i;
-
- nr = DIV_ROUND_UP(nr_pages, ZSWAP_ADDRESS_SPACE_PAGES);
- trees = kvzalloc_objs(*tree, nr);
- if (!trees) {
- pr_err("alloc failed, zswap disabled for swap type %d\n", type);
- return -ENOMEM;
- }
-
- for (i = 0; i < nr; i++)
- xa_init(trees + i);
-
- nr_zswap_trees[type] = nr;
- zswap_trees[type] = trees;
- return 0;
-}
-
-void zswap_swapoff(int type)
-{
- struct xarray *trees = zswap_trees[type];
- unsigned int i;
-
- if (!trees)
- return;
-
- /* try_to_unuse() invalidated all the entries already */
- for (i = 0; i < nr_zswap_trees[type]; i++)
- WARN_ON_ONCE(!xa_empty(trees + i));
-
- kvfree(trees);
- nr_zswap_trees[type] = 0;
- zswap_trees[type] = NULL;
-}
-
/*********************************
* debugfs functions
**********************************/
--
2.54.0
next prev 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 ` [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 ` Baoquan He [this message]
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-31-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