From: Nhat Pham <nphamcs@gmail.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Chris Li" <chrisl@kernel.org>,
"Kairui Song" <kasong@tencent.com>,
"Kemeng Shi" <shikemeng@huaweicloud.com>,
"Baoquan He" <baoquan.he@linux.dev>,
"Barry Song" <baohua@kernel.org>,
"Youngjun Park" <youngjun.park@lge.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Yosry Ahmed" <yosry@kernel.org>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Qi Zheng" <qi.zheng@linux.dev>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Axel Rasmussen" <axelrasmussen@google.com>,
"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Michal Hocko" <mhocko@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Hugh Dickins" <hughd@google.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Roman Gushchin" <roman.gushchin@linux.dev>,
"Muchun Song" <muchun.song@linux.dev>,
"Arnd Bergmann" <arnd@arndb.de>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
"Marco Elver" <elver@google.com>,
"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
"Borislav Petkov (AMD)" <bp@alien8.de>,
"Jeff Layton" <jlayton@kernel.org>,
linux-doc@vger.kernel.org, cgroups@vger.kernel.org,
"Tejun Heo" <tj@kernel.org>, "Michal Koutný" <mkoutny@suse.com>
Subject: [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap
Date: Thu, 3 Dec 2026 13:59:51 -0800 [thread overview]
Message-ID: <20261203220010.1302207-2-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>
A virtual device can span far more slots than it uses, so a dense
descriptor array would scale with its maximum size. Store allocated
virtual clusters in an XArray while retaining the indexed array for
physical devices.
Share cluster lookup, locking and swap-table allocation across the
layouts. This lets the virtual allocator reuse the physical allocator's
cluster machinery, with descriptors allocated on demand.
Suggested-by: Kairui Song <kasong@tencent.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
MAINTAINERS | 1 +
include/linux/swap.h | 9 ++
mm/page_io.c | 12 +++
mm/swap.h | 77 ++++++++++++++++-
mm/swap_state.c | 43 +++++-----
mm/swap_table.h | 25 ++++++
mm/swapfile.c | 198 ++++++++++++++++++++++++++++++++++++++-----
mm/vswap.h | 22 +++++
mm/zswap.c | 6 ++
9 files changed, 347 insertions(+), 46 deletions(-)
create mode 100644 mm/vswap.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 895c33a99acb..1b102d3ca838 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17907,6 +17907,7 @@ F: mm/swap.h
F: mm/swap_state.c
F: mm/swap_table.h
F: mm/swapfile.c
+F: mm/vswap.h
MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)
M: David Hildenbrand <david@kernel.org>
diff --git a/include/linux/swap.h b/include/linux/swap.h
index a3a98dc474c3..b3849df810f7 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -201,6 +201,7 @@ enum {
SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */
SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */
SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */
+ SWP_VSWAP = (1 << 14), /* virtual swap device */
/* add others here before... */
};
@@ -270,8 +271,14 @@ struct swap_info_struct {
struct list_head discard_clusters; /* discard clusters list */
struct plist_node avail_list; /* entry in swap_avail_head */
const struct swap_ops *ops;
+ struct xarray cluster_info_pool; /* Xarray for vswap dynamic cluster info */
};
+static inline bool swap_is_vswap(struct swap_info_struct *si)
+{
+ return si->flags & SWP_VSWAP;
+}
+
/**
* folio_swap_entry - Return the swap entry at a page index within a folio.
* @folio: The folio.
@@ -434,6 +441,8 @@ void swap_free_hibernation_slot(swp_entry_t entry);
static inline void put_swap_device(struct swap_info_struct *si)
{
+ if (swap_is_vswap(si))
+ return;
percpu_ref_put(&si->users);
}
diff --git a/mm/page_io.c b/mm/page_io.c
index a57cdcf2892d..62d1a5d42287 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -28,6 +28,7 @@
#include <linux/swap_ops.h>
#include "swap.h"
#include "swap_table.h"
+#include "vswap.h"
int generic_swapfile_activate(struct swap_info_struct *sis,
struct file *swap_file,
@@ -258,6 +259,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
}
rcu_read_unlock();
+ /* A vswap folio has no physical slot to write to. */
+ if (is_vswap_entry(folio->swap)) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
+ }
+
__swap_writeout(ctx, folio);
return 0;
out_unlock:
@@ -515,6 +522,11 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
return;
+ if (unlikely(swap_is_vswap(sis))) {
+ folio_unlock(folio);
+ return;
+ }
+
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
swap_add_folio(ctx, folio, READ);
diff --git a/mm/swap.h b/mm/swap.h
index d488a69c2809..0eaedbedc3d6 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -67,6 +67,12 @@ struct swap_cluster_info {
struct list_head list;
};
+struct swap_cluster_info_dynamic {
+ struct swap_cluster_info ci;
+ unsigned int index; /* for cluster_index() */
+ struct rcu_head rcu;
+};
+
/* All on-list cluster must have a non-zero flag. */
enum swap_cluster_flags {
CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */
@@ -77,6 +83,7 @@ enum swap_cluster_flags {
CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG,
CLUSTER_FLAG_FULL,
CLUSTER_FLAG_DISCARD,
+ CLUSTER_FLAG_DEAD, /* Vswap dynamic cluster pending kfree_rcu */
CLUSTER_FLAG_MAX,
};
@@ -120,12 +127,33 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry)
return __swap_type_to_info(swp_type(entry));
}
+/**
+ * __swap_offset_to_cluster - look up the cluster holding a swap offset
+ * @si: the swap device
+ * @offset: the swap entry offset
+ *
+ * Context: A vswap cluster is freed by kfree_rcu(). Callers must hold the
+ * RCU read lock, or know the cluster is pinned by an in-use entry.
+ *
+ * Return: the cluster, or NULL if @si is a vswap device with no cluster
+ * allocated at @offset.
+ */
static inline struct swap_cluster_info *__swap_offset_to_cluster(
struct swap_info_struct *si, pgoff_t offset)
{
+ unsigned int cluster_idx = offset / SWAPFILE_CLUSTER;
+
VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
VM_WARN_ON_ONCE(offset >= roundup(si->max, SWAPFILE_CLUSTER));
- return &si->cluster_info[offset / SWAPFILE_CLUSTER];
+
+ if (swap_is_vswap(si)) {
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = xa_load(&si->cluster_info_pool, cluster_idx);
+ return ci_dyn ? &ci_dyn->ci : NULL;
+ }
+
+ return &si->cluster_info[cluster_idx];
}
static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry)
@@ -134,10 +162,36 @@ static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entr
swp_offset(entry));
}
+static inline struct swap_cluster_info *__vswap_cluster_lock(
+ struct swap_info_struct *si, unsigned long offset, bool irq)
+{
+ struct swap_cluster_info *ci;
+
+ rcu_read_lock();
+ ci = __swap_offset_to_cluster(si, offset);
+ if (ci) {
+ if (irq)
+ spin_lock_irq(&ci->lock);
+ else
+ spin_lock(&ci->lock);
+
+ /* The cluster can be torn down while we wait for the lock. */
+ if (ci->flags == CLUSTER_FLAG_DEAD) {
+ if (irq)
+ spin_unlock_irq(&ci->lock);
+ else
+ spin_unlock(&ci->lock);
+ ci = NULL;
+ }
+ }
+ rcu_read_unlock();
+ return ci;
+}
+
static __always_inline struct swap_cluster_info *__swap_cluster_lock(
struct swap_info_struct *si, unsigned long offset, bool irq)
{
- struct swap_cluster_info *ci = __swap_offset_to_cluster(si, offset);
+ struct swap_cluster_info *ci;
/*
* Nothing modifies swap cache in an IRQ context. All access to
@@ -150,6 +204,11 @@ static __always_inline struct swap_cluster_info *__swap_cluster_lock(
*/
VM_WARN_ON_ONCE(!in_task());
VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
+
+ if (swap_is_vswap(si))
+ return __vswap_cluster_lock(si, offset, irq);
+
+ ci = __swap_offset_to_cluster(si, offset);
if (irq)
spin_lock_irq(&ci->lock);
else
@@ -160,10 +219,12 @@ static __always_inline struct swap_cluster_info *__swap_cluster_lock(
/**
* swap_cluster_lock - Lock and return the swap cluster of given offset.
* @si: swap device the cluster belongs to.
- * @offset: the swap entry offset, pointing to a valid slot.
+ * @offset: the swap entry offset.
*
* Context: The caller must ensure the offset is in the valid range and
* protect the swap device with reference count or locks.
+ * Return: the locked cluster, or NULL if it is gone. Only a vswap device
+ * can return NULL, as its clusters are allocated and freed on demand.
*/
static inline struct swap_cluster_info *swap_cluster_lock(
struct swap_info_struct *si, unsigned long offset)
@@ -382,6 +443,16 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry)
return NULL;
}
+static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry)
+{
+ return NULL;
+}
+
+static inline unsigned int swp_cluster_offset(swp_entry_t entry)
+{
+ return 0;
+}
+
static inline int folio_alloc_swap(struct folio *folio)
{
return -EINVAL;
diff --git a/mm/swap_state.c b/mm/swap_state.c
index da65d75ceead..436f82319425 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -92,13 +92,11 @@ void show_swap_cache_info(void)
*/
struct folio *swap_cache_get_folio(swp_entry_t entry)
{
- struct swap_cluster_info *ci = __swap_entry_to_cluster(entry);
- unsigned int ci_off = swp_cluster_offset(entry);
unsigned long swp_tb;
struct folio *folio;
for (;;) {
- swp_tb = swap_table_get(ci, ci_off);
+ swp_tb = swap_table_lookup(entry);
if (!swp_tb_is_folio(swp_tb))
return NULL;
folio = swp_tb_to_folio(swp_tb);
@@ -108,7 +106,7 @@ struct folio *swap_cache_get_folio(swp_entry_t entry)
* The folio may have been freed and its memory reused before
* we got the reference. Make sure it is still in the slot.
*/
- swp_tb = swap_table_get(ci, ci_off);
+ swp_tb = swap_table_lookup(entry);
if (likely(swp_tb_is_folio(swp_tb) &&
swp_tb_to_folio(swp_tb) == folio))
return folio;
@@ -129,8 +127,7 @@ bool swap_cache_has_folio(swp_entry_t entry)
{
unsigned long swp_tb;
- swp_tb = swap_table_get(__swap_entry_to_cluster(entry),
- swp_cluster_offset(entry));
+ swp_tb = swap_table_lookup(entry);
return swp_tb_is_folio(swp_tb);
}
@@ -146,8 +143,7 @@ void *swap_cache_get_shadow(swp_entry_t entry)
{
unsigned long swp_tb;
- swp_tb = swap_table_get(__swap_entry_to_cluster(entry),
- swp_cluster_offset(entry));
+ swp_tb = swap_table_lookup(entry);
if (swp_tb_is_shadow(swp_tb))
return swp_tb_to_shadow(swp_tb);
return NULL;
@@ -429,14 +425,16 @@ void __swap_cache_replace_folio(struct swap_cluster_info *ci,
* -ENOENT / -EEXIST: Target swap entry is unavailable or cached, the caller
* should abort or try to use the cached folio instead
*/
-static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
- swp_entry_t targ_entry, gfp_t gfp,
+static struct folio *__swap_cache_alloc(swp_entry_t targ_entry, gfp_t gfp,
unsigned int order, struct vm_fault *vmf,
struct mempolicy *mpol, pgoff_t ilx)
{
int err;
swp_entry_t entry;
struct folio *folio;
+ struct swap_cluster_info *ci;
+ struct swap_info_struct *si = __swap_entry_to_info(targ_entry);
+ unsigned long offset = swp_offset(targ_entry);
void *shadow = NULL;
unsigned short memcg_id;
unsigned long address, nr_pages = 1UL << order;
@@ -446,9 +444,12 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
entry.val = round_down(targ_entry.val, nr_pages);
/* Check if the slot and range are available, skip allocation if not */
- spin_lock(&ci->lock);
- err = __swap_cache_add_check(ci, targ_entry, nr_pages, NULL, NULL);
- spin_unlock(&ci->lock);
+ err = -ENOENT;
+ ci = swap_cluster_lock(si, offset);
+ if (ci) {
+ err = __swap_cache_add_check(ci, targ_entry, nr_pages, NULL, NULL);
+ swap_cluster_unlock(ci);
+ }
if (unlikely(err))
return ERR_PTR(err);
@@ -469,10 +470,13 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
return ERR_PTR(-ENOMEM);
/* Double check the range is still not in conflict */
- spin_lock(&ci->lock);
- err = __swap_cache_add_check(ci, targ_entry, nr_pages, &shadow, &memcg_id);
+ err = -ENOENT;
+ ci = swap_cluster_lock(si, offset);
+ if (ci)
+ err = __swap_cache_add_check(ci, targ_entry, nr_pages, &shadow, &memcg_id);
if (unlikely(err)) {
- spin_unlock(&ci->lock);
+ if (ci)
+ swap_cluster_unlock(ci);
folio_put(folio);
return ERR_PTR(err);
}
@@ -480,10 +484,11 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
__folio_set_locked(folio);
__folio_set_swapbacked(folio);
__swap_cache_do_add_folio(ci, folio, entry);
- spin_unlock(&ci->lock);
+ swap_cluster_unlock(ci);
if (mem_cgroup_swapin_charge_folio(folio, memcg_id,
vmf ? vmf->vma->vm_mm : NULL, gfp)) {
+ /* The folio pins the cluster */
spin_lock(&ci->lock);
__swap_cache_do_del_folio(ci, folio, entry, shadow);
spin_unlock(&ci->lock);
@@ -542,9 +547,7 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp,
{
int order, err;
struct folio *ret;
- struct swap_cluster_info *ci;
- ci = __swap_entry_to_cluster(targ_entry);
order = highest_order(orders);
/* orders must be non-zero, and must not exceed cluster size. */
@@ -552,7 +555,7 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp,
return ERR_PTR(-EINVAL);
do {
- ret = __swap_cache_alloc(ci, targ_entry, gfp, order,
+ ret = __swap_cache_alloc(targ_entry, gfp, order,
vmf, mpol, ilx);
if (!IS_ERR(ret))
break;
diff --git a/mm/swap_table.h b/mm/swap_table.h
index 072625d60d54..c21a48c98cc6 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -278,6 +278,31 @@ static inline unsigned long swap_table_get(struct swap_cluster_info *ci,
return swp_tb;
}
+/*
+ * Resolve @entry's cluster and read its slot, both under RCU. A vswap
+ * cluster is allocated on demand and freed by kfree_rcu(), so a caller
+ * starting from an entry cannot resolve it beforehand.
+ */
+static inline unsigned long swap_table_lookup(swp_entry_t entry)
+{
+ struct swap_cluster_info *ci;
+ atomic_long_t *table;
+ unsigned long swp_tb;
+
+ rcu_read_lock();
+ ci = __swap_entry_to_cluster(entry);
+ if (!ci) {
+ rcu_read_unlock();
+ return null_to_swp_tb();
+ }
+ table = rcu_dereference(ci->table);
+ swp_tb = table ? atomic_long_read(&table[swp_cluster_offset(entry)])
+ : null_to_swp_tb();
+ rcu_read_unlock();
+
+ return swp_tb;
+}
+
static inline void __swap_table_set_zero(struct swap_cluster_info *ci,
unsigned int ci_off)
{
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d59fa8220862..818189c82ea9 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -36,6 +36,7 @@
#include <linux/poll.h>
#include <linux/oom.h>
#include <linux/swapfile.h>
+#include <linux/swap_ops.h>
#include <linux/export.h>
#include <linux/sort.h>
#include <linux/completion.h>
@@ -46,6 +47,7 @@
#include <asm/tlbflush.h>
#include <linux/leafops.h>
#include "swap_table.h"
+#include "vswap.h"
#include "internal.h"
#include "swap.h"
@@ -399,6 +401,8 @@ static inline bool cluster_is_usable(struct swap_cluster_info *ci, int order)
static inline unsigned int cluster_index(struct swap_info_struct *si,
struct swap_cluster_info *ci)
{
+ if (swap_is_vswap(si))
+ return container_of(ci, struct swap_cluster_info_dynamic, ci)->index;
return ci - si->cluster_info;
}
@@ -451,7 +455,7 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
/*
* Return with ci locked. An unlocked allocator also needs si's outer locks;
- * initial bad-slot setup passes NULL because the device is not published.
+ * physical bad-slot setup passes NULL because the device is not published.
*/
static int swap_cluster_alloc_table(struct swap_info_struct *si,
struct swap_cluster_info *ci,
@@ -631,10 +635,15 @@ static void move_cluster(struct swap_info_struct *si,
lockdep_assert_held(&ci->lock);
spin_lock(&si->lock);
- if (ci->flags == CLUSTER_FLAG_NONE)
+ if (!list) {
+ /* Going away. An isolated cluster is already off its list. */
+ if (ci->flags != CLUSTER_FLAG_NONE)
+ list_del(&ci->list);
+ } else if (ci->flags == CLUSTER_FLAG_NONE) {
list_add_tail(&ci->list, list);
- else
+ } else {
list_move_tail(&ci->list, list);
+ }
spin_unlock(&si->lock);
ci->flags = new_flags;
}
@@ -652,6 +661,18 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
{
swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false);
swap_cluster_free_table(ci);
+
+ if (swap_is_vswap(si)) {
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ /* vswap clusters are destroyed, not returned to free_clusters. */
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ xa_erase(&si->cluster_info_pool, ci_dyn->index);
+ move_cluster(si, ci, NULL, CLUSTER_FLAG_DEAD);
+ kfree_rcu(ci_dyn, rcu);
+ return;
+ }
+
move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
ci->order = 0;
}
@@ -848,6 +869,8 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
unsigned int ci_off = offset % SWAPFILE_CLUSTER;
unsigned long idx = offset / SWAPFILE_CLUSTER;
struct swap_cluster_info *ci;
+ struct swap_info_struct *alloc_si = swap_is_vswap(si) ? si : NULL;
+ bool need_table;
int ret = 0;
/* si->max may got shrunk by swap swap_activate() */
@@ -868,9 +891,10 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
}
ci = cluster_info + idx;
+ need_table = !ci->count;
/* Need to allocate swap table first for initial bad slot marking. */
- if (!ci->count) {
- ret = swap_cluster_alloc_table(NULL, ci, GFP_KERNEL, false);
+ if (need_table) {
+ ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
if (ret)
goto out;
} else {
@@ -885,6 +909,8 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
}
out:
spin_unlock(&ci->lock);
+ if (need_table && alloc_si)
+ local_unlock(&percpu_swap_cluster.lock);
WARN_ON(ci->count > SWAPFILE_CLUSTER);
WARN_ON(ci->flags);
@@ -905,6 +931,8 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
unsigned long offset = start, end = start + nr_pages;
unsigned long swp_tb;
+ VM_WARN_ON_ONCE(swap_is_vswap(si));
+
spin_unlock(&ci->lock);
do {
swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
@@ -1107,6 +1135,45 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
return found;
}
+static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
+ struct folio *folio)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ struct swap_cluster_info *ci;
+ unsigned long offset;
+
+ VM_WARN_ON(!swap_is_vswap(si));
+
+ ci_dyn = kzalloc_obj(*ci_dyn, GFP_ATOMIC | __GFP_NOWARN);
+ if (!ci_dyn)
+ return SWAP_ENTRY_INVALID;
+
+ spin_lock_init(&ci_dyn->ci.lock);
+ INIT_LIST_HEAD(&ci_dyn->ci.list);
+
+ /* Lock before publishing: xa_alloc makes the cluster findable by offset. */
+ ci = &ci_dyn->ci;
+ spin_lock(&ci->lock);
+
+ if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
+ spin_unlock(&ci->lock);
+ kfree(ci_dyn);
+ return SWAP_ENTRY_INVALID;
+ }
+
+ if (xa_alloc(&si->cluster_info_pool, &ci_dyn->index, ci_dyn,
+ XA_LIMIT(1, DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER) - 1),
+ GFP_ATOMIC | __GFP_NOWARN)) {
+ spin_unlock(&ci->lock);
+ swap_cluster_free_table(&ci_dyn->ci);
+ kfree(ci_dyn);
+ return SWAP_ENTRY_INVALID;
+ }
+
+ offset = cluster_offset(si, ci);
+ return alloc_swap_scan_cluster(si, ci, folio, offset);
+}
+
static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
{
long to_scan = 1;
@@ -1215,6 +1282,12 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
goto done;
}
+ if (swap_is_vswap(si)) {
+ found = vswap_alloc_cluster(si, folio);
+ if (found)
+ goto done;
+ }
+
if (!swap_prefer_free_cluster(si)) {
found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
if (found)
@@ -1351,8 +1424,10 @@ static bool swap_usage_add(struct swap_info_struct *si, unsigned int nr_entries)
/*
* If device is full, and SWAP_USAGE_OFFLIST_BIT is not set,
* remove it from the plist.
+ *
+ * Vswap is never on the avail list, so skip it.
*/
- if (unlikely(val == si->pages)) {
+ if (unlikely(val == si->pages) && !swap_is_vswap(si)) {
del_from_avail_list(si, false);
return true;
}
@@ -1367,8 +1442,10 @@ static void swap_usage_sub(struct swap_info_struct *si, unsigned int nr_entries)
/*
* If device is not full, and SWAP_USAGE_OFFLIST_BIT is set,
* add it to the plist.
+ *
+ * Vswap is never on the avail list, so skip it.
*/
- if (unlikely(val & SWAP_USAGE_OFFLIST_BIT))
+ if (unlikely(val & SWAP_USAGE_OFFLIST_BIT) && !swap_is_vswap(si))
add_to_avail_list(si, false);
}
@@ -1413,6 +1490,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
static bool get_swap_device_info(struct swap_info_struct *si)
{
+ /* The vswap device is always alive, so it needs no refcount. */
+ if (swap_is_vswap(si))
+ return true;
+
if (!percpu_ref_tryget_live(&si->users))
return false;
/*
@@ -1448,11 +1529,11 @@ static bool swap_alloc_fast(struct folio *folio)
return false;
ci = swap_cluster_lock(si, offset);
- if (cluster_is_usable(ci, order)) {
+ if (ci && cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
alloc_swap_scan_cluster(si, ci, folio, offset);
- } else {
+ } else if (ci) {
swap_cluster_unlock(ci);
}
@@ -1574,6 +1655,7 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
if (IS_ERR_OR_NULL(si))
return 0;
+ /* The source PTE pins the entry, so its cluster is alive. */
ci = __swap_offset_to_cluster(si, offset);
ret = swap_extend_table_alloc(si, ci, swp_cluster_offset(entry), gfp);
@@ -1984,7 +2066,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
return NULL;
put_out:
pr_err_ratelimited("%s: %s%08lx\n", __func__, Bad_offset, entry.val);
- percpu_ref_put(&si->users);
+ put_swap_device(si);
return ERR_PTR(-EIO);
}
@@ -2065,7 +2147,7 @@ bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry)
unsigned long swp_tb;
ci = swap_cluster_lock(si, offset);
- swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
+ swp_tb = __swap_table_get(ci, swp_cluster_offset(entry));
swap_cluster_unlock(ci);
return swp_tb_get_count(swp_tb) > 0;
@@ -2118,6 +2200,7 @@ static bool folio_maybe_swapped(struct folio *folio)
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
+ /* Folio is locked and in swap cache, so ci->count > 0: cluster is alive. */
ci = __swap_entry_to_cluster(entry);
ci_off = swp_cluster_offset(entry);
ci_end = ci_off + folio_nr_pages(folio);
@@ -2301,6 +2384,9 @@ static int __find_hibernation_swap_type(dev_t device, sector_t offset)
if (!(sis->flags & SWP_WRITEOK))
continue;
+ /* vswap has no bdev, so it is never a hibernation target. */
+ if (swap_is_vswap(sis))
+ continue;
if (device == sis->bdev->bd_dev) {
struct swap_extent *se = first_se(sis);
@@ -2488,6 +2574,9 @@ int find_first_swap(dev_t *device)
if (!(sis->flags & SWP_WRITEOK))
continue;
+ /* vswap has no bdev, so it is never a hibernation target. */
+ if (swap_is_vswap(sis))
+ continue;
*device = sis->bdev->bd_dev;
spin_unlock(&swap_lock);
return type;
@@ -2714,8 +2803,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
&vmf);
}
if (!folio) {
- swp_tb = swap_table_get(__swap_entry_to_cluster(entry),
- swp_cluster_offset(entry));
+ swp_tb = swap_table_lookup(entry);
if (swp_tb_get_count(swp_tb) <= 0)
continue;
return -ENOMEM;
@@ -3147,15 +3235,24 @@ static int setup_swap_extents(struct swap_info_struct *sis,
static void _enable_swap_info(struct swap_info_struct *si)
{
- atomic_long_add(si->pages, &nr_swap_pages);
- total_swap_pages += si->pages;
+ if (!swap_is_vswap(si)) {
+ atomic_long_add(si->pages, &nr_swap_pages);
+ total_swap_pages += si->pages;
+ }
assert_spin_locked(&swap_lock);
- plist_add(&si->list, &swap_active_head);
+ /*
+ * Vswap has no backing file and no swapoff support, so keep it
+ * off swap_active_head (used by swapoff filename lookup and
+ * swap_sync_discard) and swap_avail_head (physical allocator).
+ */
+ if (!swap_is_vswap(si)) {
+ plist_add(&si->list, &swap_active_head);
- /* Add back to available list */
- add_to_avail_list(si, true);
+ /* Add back to available list */
+ add_to_avail_list(si, true);
+ }
}
/*
@@ -3199,12 +3296,31 @@ static void wait_for_allocation(struct swap_info_struct *si)
}
}
-static void free_swap_cluster_info(struct swap_cluster_info *cluster_info,
+static void free_swap_cluster_info(struct swap_info_struct *si,
+ struct swap_cluster_info *cluster_info,
unsigned long maxpages)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
+ unsigned long idx;
int i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
+ if (swap_is_vswap(si)) {
+ xa_for_each(&si->cluster_info_pool, idx, ci_dyn) {
+ ci = &ci_dyn->ci;
+ spin_lock(&ci->lock);
+ if (cluster_table_is_alloced(ci)) {
+ swap_cluster_assert_empty(ci, 0,
+ SWAPFILE_CLUSTER, true);
+ swap_cluster_free_table(ci);
+ }
+ spin_unlock(&ci->lock);
+ kfree(ci_dyn);
+ }
+ xa_destroy(&si->cluster_info_pool);
+ return;
+ }
+
if (!cluster_info)
return;
for (i = 0; i < nr_clusters; i++) {
@@ -3351,7 +3467,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
mutex_unlock(&swapon_mutex);
kfree(p->global_cluster);
p->global_cluster = NULL;
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(p, cluster_info, maxpages);
inode = mapping->host;
@@ -3698,10 +3814,39 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
unsigned long maxpages)
{
unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
- struct swap_cluster_info *cluster_info;
+ struct swap_cluster_info *cluster_info = NULL;
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
int err = -ENOMEM;
unsigned long i;
+ /* A vswap device uses an xarray pool instead of a static array. */
+ if (swap_is_vswap(si)) {
+ nr_clusters = 0;
+ xa_init_flags(&si->cluster_info_pool, XA_FLAGS_ALLOC);
+
+ /*
+ * Pre-allocate cluster 0 and mark slot 0 (header page)
+ * as bad so the allocator never hands out page offset 0.
+ */
+ ci_dyn = kzalloc_obj(*ci_dyn, GFP_KERNEL);
+ if (!ci_dyn)
+ goto err;
+ spin_lock_init(&ci_dyn->ci.lock);
+ INIT_LIST_HEAD(&ci_dyn->ci.list);
+
+ err = xa_insert(&si->cluster_info_pool, 0, ci_dyn, GFP_KERNEL);
+ if (err) {
+ kfree(ci_dyn);
+ goto err;
+ }
+
+ err = swap_cluster_setup_bad_slot(si, &ci_dyn->ci, 0, false);
+ if (err)
+ goto err;
+
+ goto setup_cluster_info;
+ }
+
cluster_info = kvzalloc_objs(*cluster_info, nr_clusters);
if (!cluster_info)
goto err;
@@ -3745,6 +3890,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
goto err;
}
+setup_cluster_info:
INIT_LIST_HEAD(&si->free_clusters);
INIT_LIST_HEAD(&si->full_clusters);
INIT_LIST_HEAD(&si->discard_clusters);
@@ -3766,10 +3912,16 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
}
}
+ /* Slot 0 is bad, so cluster 0 never empties. The rest of it is usable. */
+ if (swap_is_vswap(si)) {
+ ci_dyn->ci.flags = CLUSTER_FLAG_NONFULL;
+ list_add_tail(&ci_dyn->ci.list, &si->nonfull_clusters[0]);
+ }
+
si->cluster_info = cluster_info;
return 0;
err:
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(si, cluster_info, maxpages);
return err;
}
@@ -3991,7 +4143,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
si->global_cluster = NULL;
inode = NULL;
destroy_swap_extents(si, swap_file);
- free_swap_cluster_info(si->cluster_info, si->max);
+ free_swap_cluster_info(si, si->cluster_info, si->max);
si->cluster_info = NULL;
/*
* Clear the SWP_USED flag after all resources are freed so
diff --git a/mm/vswap.h b/mm/vswap.h
new file mode 100644
index 000000000000..bf1606263e8e
--- /dev/null
+++ b/mm/vswap.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Virtual swap space
+ *
+ * Copyright (C) 2026 Nhat Pham
+ */
+#ifndef _MM_VSWAP_H
+#define _MM_VSWAP_H
+
+#include <linux/swap.h>
+#include "swap.h"
+
+#ifdef CONFIG_SWAP
+
+static inline bool is_vswap_entry(swp_entry_t entry)
+{
+ return swap_is_vswap(__swap_entry_to_info(entry));
+}
+
+#endif /* CONFIG_SWAP */
+
+#endif /* _MM_VSWAP_H */
diff --git a/mm/zswap.c b/mm/zswap.c
index 6a66b898ab47..ff3b0ceb10f4 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1093,6 +1093,12 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
if (IS_ERR_OR_NULL(si))
return -ENOENT;
+ /* Vswap entries have no physical backing to write to. */
+ if (swap_is_vswap(si)) {
+ put_swap_device(si);
+ return -EINVAL;
+ }
+
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
NO_INTERLEAVE_INDEX);
--
2.53.1-Meta
next prev parent reply other threads:[~2026-12-03 22:00 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` Nhat Pham [this message]
2026-12-04 22:09 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap sashiko-bot
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
2026-12-03 23:00 ` Nhat Pham
2026-12-03 23:05 ` Yosry Ahmed
2026-12-03 23:14 ` Nhat Pham
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=20261203220010.1302207-2-nphamcs@gmail.com \
--to=nphamcs@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bp@alien8.de \
--cc=cgroups@vger.kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=dapeng1.mi@linux.intel.com \
--cc=david@kernel.org \
--cc=elver@google.com \
--cc=enelsonmoore@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jlayton@kernel.org \
--cc=kasong@tencent.com \
--cc=kmehltretter@gmail.com \
--cc=liam@infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=qi.zheng@linux.dev \
--cc=rdunlap@infradead.org \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=skhan@linuxfoundation.org \
--cc=tj@kernel.org \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
--cc=yuanchu@google.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.