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 31/32] mm, swap: release a range of backend handles at once
Date: Fri, 4 Dec 2026 02:13:18 +0800 [thread overview]
Message-ID: <20261203181319.901844-32-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
Freeing a range of swap slots calls swap_backend_erase() for every slot,
which resolves the device and the cluster again each time: two
percpu_ref_is_zero() calls and a division per slot. A slot range is a folio's
slots or a cluster's free batch, so it never crosses a cluster.
Resolve the cluster once and walk the slots. A tagged handle is only
ever a zswap entry, so the walk releases those directly, which is why
zswap_entry_free() is no longer static. The backend table is the only
owner of a stored zswap entry, so the range erase is where it goes;
collecting the handles first would only add an array or a callback to
the free path. Release a tagged entry that is still in a cluster's
table when the table goes as well, instead of only warning about it, so
a broken invariant cannot leak it.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/zswap.h | 4 ++++
mm/swap.h | 1 +
mm/swapfile.c | 43 +++++++++++++++++++++++++++++++++++++++----
mm/zswap.c | 12 ++----------
4 files changed, 46 insertions(+), 14 deletions(-)
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index c7faad1d1b42..df9e0f29480e 100644
--- a/include/linux/zswap.h
+++ b/include/linux/zswap.h
@@ -6,6 +6,7 @@
#include <linux/mm_types.h>
struct lruvec;
+struct zswap_entry;
extern atomic_long_t zswap_stored_pages;
@@ -25,6 +26,7 @@ struct zswap_lruvec_state {
};
unsigned long zswap_total_pages(void);
+void zswap_entry_free(struct zswap_entry *entry);
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);
@@ -38,6 +40,8 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr);
struct zswap_lruvec_state {};
+static inline void zswap_entry_free(struct zswap_entry *entry) {}
+
static inline bool zswap_store(struct folio *folio, bool may_enter_fs)
{
return false;
diff --git a/mm/swap.h b/mm/swap.h
index 9b2d8d556bd0..4f6e2c2de16f 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -229,6 +229,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp);
void *swap_backend_get(swp_entry_t entry);
void *swap_backend_set(swp_entry_t entry, void *handle);
void *swap_backend_erase(swp_entry_t entry);
+void swap_backend_erase_range(swp_entry_t entry, unsigned long nr);
bool swap_backend_present(swp_entry_t entry, unsigned int nr);
/*
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 66a7682e29f0..3098a4086cdb 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4884,10 +4884,40 @@ void *swap_backend_erase(swp_entry_t swp)
return swap_backend_decode_zswap(val);
}
+/*
+ * Drop the handles of a range of slots. A range is a folio's slots or a
+ * cluster's free batch, so it never crosses a cluster: the caller holds
+ * either the cluster lock (slot free) or the folio lock (store rollback).
+ *
+ * Clamp and warn if the one-cluster invariant ever breaks; the next
+ * cluster is not protected here and the remaining slots are not erased.
+ */
+void swap_backend_erase_range(swp_entry_t swp, unsigned long nr)
+{
+ struct swap_cluster_info *ci = __swap_entry_to_cluster(swp);
+ unsigned int slot = swp_cluster_offset(swp);
+ unsigned long *tbl = READ_ONCE(ci->backend_table);
+ unsigned long n = min_t(unsigned long, nr, SWAPFILE_CLUSTER - slot);
+
+ VM_WARN_ON_ONCE(nr > SWAPFILE_CLUSTER - slot);
+ if (!tbl)
+ return;
+
+ for (unsigned int i = 0; i < n; i++) {
+ unsigned long val = READ_ONCE(tbl[slot + i]);
+
+ if (!swap_backend_is_zswap(val))
+ continue;
+ WRITE_ONCE(tbl[slot + i], 0);
+ zswap_entry_free(swap_backend_decode_zswap(val));
+ }
+}
+
/*
* Drop the table of a cluster that is going away. An untagged value is an
- * xswap physical backend that has to be given back; a tagged one would be a
- * leak, the slot free path invalidates those first. Caller holds ci->lock.
+ * xswap physical backend that has to be given back; a tagged one should
+ * have been invalidated by the slot free path, but release it here too so a
+ * broken invariant cannot leak it. Caller holds ci->lock.
*/
static void swap_backend_table_free(struct swap_info_struct *si,
struct swap_cluster_info *ci)
@@ -4901,9 +4931,14 @@ 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));
+ if (swap_backend_is_zswap(val)) {
+ VM_WARN_ON_ONCE(1);
+ WRITE_ONCE(tbl[slot], 0);
+ zswap_entry_free(swap_backend_decode_zswap(val));
+ continue;
+ }
#ifdef CONFIG_XSWAP
- if (val && !swap_backend_is_zswap(val))
+ if (val)
xswap_release_slot_backend(si, ci, slot);
#else
VM_WARN_ON_ONCE(val);
diff --git a/mm/zswap.c b/mm/zswap.c
index 1903367a9576..9df2eaf4a21c 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -766,7 +766,7 @@ static void zswap_entry_cache_free(struct zswap_entry *entry)
* Carries out the common pattern of freeing an entry's zsmalloc allocation,
* freeing the entry itself, and decrementing the number of stored pages.
*/
-static void zswap_entry_free(struct zswap_entry *entry)
+void zswap_entry_free(struct zswap_entry *entry)
{
struct zswap_pool *pool = zswap_entry_pool(entry);
@@ -1849,8 +1849,6 @@ int zswap_load(struct folio *folio)
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries)
{
- unsigned long i;
-
if (zswap_never_enabled())
return;
@@ -1859,13 +1857,7 @@ void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries)
* 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++) {
- struct zswap_entry *entry;
-
- entry = swap_backend_erase(swp_entry(type, offset + i));
- if (entry)
- zswap_entry_free(entry);
- }
+ swap_backend_erase_range(swp_entry(type, offset), nr_entries);
}
/*********************************
--
2.54.0
next prev 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 ` Baoquan He [this message]
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-32-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.