[PATCH v7 04/20] mm/swap: allocate virtual swap slots

From: Nhat Pham

Date: Thu Dec 03 2026 - 17:00:53 EST


Virtual slots need per-CPU allocation hints for their own device. Add
the virtual allocator and select it from folio_alloc_swap() when vswap
and zswap are enabled and the cgroup permits zswap.

Reuse the cluster allocator while keeping virtual slot usage out of the
physical free-space counter. Avoid reclaiming virtual slots merely
because physical swap is full. Retain direct physical allocation when
virtual allocation fails.

The selector remains gated by vswap_key. Device registration waits for
the physical backend so writeout can fall back to disk when zswap
rejects a folio.

Suggested-by: Kairui Song <kasong@xxxxxxxxxxx>
Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
---
mm/swapfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 93 insertions(+), 8 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 1387c8bb1e97..aa08111bea62 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -131,6 +131,16 @@ static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = {
.lock = INIT_LOCAL_LOCK(),
};

+struct percpu_vswap_cluster {
+ unsigned long offset[SWAP_NR_ORDERS];
+ local_lock_t lock;
+};
+
+static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = {
+ .offset = { [0 ... SWAP_NR_ORDERS - 1] = SWAP_ENTRY_INVALID },
+ .lock = INIT_LOCAL_LOCK(),
+};
+
/* May return NULL on invalid type, caller must check for NULL return */
static struct swap_info_struct *swap_type_to_info(int type)
{
@@ -236,7 +246,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,

need_reclaim = ((flags & TTRS_ANYWAY) ||
((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) ||
- ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio)));
+ ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) &&
+ !is_vswap_entry(folio->swap)));
if (!need_reclaim || !folio_swapcache_freeable(folio))
goto out_unlock;

@@ -585,7 +596,9 @@ swap_cluster_populate(struct swap_info_struct *si,
/*
* Only cluster isolation from the allocator does table allocation.
* Swap allocator uses percpu clusters and holds the local lock.
+ * vswap clusters are destroyed rather than freed to si->free_clusters.
*/
+ VM_WARN_ON_ONCE(swap_is_vswap(si));
lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
if (!(si->flags & SWP_SOLIDSTATE))
lockdep_assert_held(&si->global_cluster_lock);
@@ -986,7 +999,8 @@ static bool cluster_scan_range(struct swap_info_struct *si,
if (swp_tb_is_null(swp_tb))
continue;
if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
- if (!vm_swap_full())
+ /* vswap slots are abundant; never reclaim to reuse one */
+ if (swap_is_vswap(si) || !vm_swap_full())
return false;
*need_reclaim = true;
continue;
@@ -1058,6 +1072,9 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
*/
static bool swap_prefer_free_cluster(struct swap_info_struct *si)
{
+ if (swap_is_vswap(si))
+ return false;
+
return (si->flags & SWP_PAGE_DISCARD) ||
!(si->flags & SWP_SYNCHRONOUS_IO);
}
@@ -1105,6 +1122,10 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
out:
relocate_cluster(si, ci);
swap_cluster_unlock(ci);
+ if (swap_is_vswap(si)) {
+ this_cpu_write(percpu_vswap_cluster.offset[order], next);
+ return found;
+ }
if (si->flags & SWP_SOLIDSTATE) {
this_cpu_write(percpu_swap_cluster.offset[order], next);
this_cpu_write(percpu_swap_cluster.si[order], si);
@@ -1255,7 +1276,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
* Swapfile is not block device so unable
* to allocate large entries.
*/
- if (order && !(si->flags & SWP_BLKDEV))
+ if (order && !(si->flags & SWP_BLKDEV) && !swap_is_vswap(si))
return 0;

if (!(si->flags & SWP_SOLIDSTATE)) {
@@ -1304,7 +1325,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
}

/* Try reclaim full clusters if free and nonfull lists are drained */
- if (vm_swap_full())
+ if (!swap_is_vswap(si) && vm_swap_full())
swap_reclaim_full_clusters(si, false);

if (order < PMD_ORDER) {
@@ -1465,7 +1486,8 @@ static void swap_range_alloc(struct swap_info_struct *si,
if (vm_swap_full())
schedule_work(&si->reclaim_work);
}
- atomic_long_sub(nr_entries, &nr_swap_pages);
+ if (!swap_is_vswap(si))
+ atomic_long_sub(nr_entries, &nr_swap_pages);
}

static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
@@ -1474,7 +1496,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
unsigned long end = offset + nr_entries - 1;
void (*swap_slot_free_notify)(struct block_device *, unsigned long);

- zswap_invalidate(si->type, offset, nr_entries);
+ if (!swap_is_vswap(si))
+ zswap_invalidate(si->type, offset, nr_entries);

if (si->flags & SWP_BLKDEV)
swap_slot_free_notify =
@@ -1493,7 +1516,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
* only after the above cleanups are done.
*/
smp_wmb();
- atomic_long_add(nr_entries, &nr_swap_pages);
+ if (!swap_is_vswap(si))
+ atomic_long_add(nr_entries, &nr_swap_pages);
swap_usage_sub(si, nr_entries);
}

@@ -1881,6 +1905,60 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return err;
}

+static bool folio_may_vswap(struct folio *folio)
+{
+ struct obj_cgroup *objcg;
+ bool may_zswap;
+
+ if (!vswap_is_enabled() || !zswap_is_enabled())
+ return false;
+
+ /*
+ * If zswap will not take the folio, writeout has to find a physical
+ * slot anyway. We are just incurring indirection overhead
+ * unnecessarily.
+ */
+ objcg = get_obj_cgroup_from_folio(folio);
+ may_zswap = !objcg || obj_cgroup_may_zswap(objcg);
+ if (objcg)
+ obj_cgroup_put(objcg);
+ return may_zswap;
+}
+
+static bool vswap_alloc(struct folio *folio)
+{
+ unsigned int order = folio_order(folio);
+ struct swap_cluster_info *ci;
+ unsigned long offset;
+
+ local_lock(&percpu_vswap_cluster.lock);
+ offset = this_cpu_read(percpu_vswap_cluster.offset[order]);
+
+ if (offset != SWAP_ENTRY_INVALID) {
+ ci = swap_cluster_lock(vswap_si, offset);
+ if (ci && cluster_is_usable(ci, order)) {
+ if (cluster_is_empty(ci))
+ offset = cluster_offset(vswap_si, ci);
+ alloc_swap_scan_cluster(vswap_si, ci, folio, offset);
+ } else if (ci) {
+ swap_cluster_unlock(ci);
+ }
+ }
+
+ if (!folio_test_swapcache(folio))
+ cluster_alloc_swap_entry(vswap_si, folio);
+
+ if (folio_test_swapcache(folio)) {
+ /* alloc_swap_scan_cluster updated percpu offset already */
+ local_unlock(&percpu_vswap_cluster.lock);
+ return true;
+ }
+
+ this_cpu_write(percpu_vswap_cluster.offset[order], SWAP_ENTRY_INVALID);
+ local_unlock(&percpu_vswap_cluster.lock);
+ return false;
+}
+
/**
* folio_alloc_swap - allocate swap space for a folio
* @folio: folio we want to move to swap
@@ -1897,10 +1975,13 @@ int folio_alloc_swap(struct folio *folio)
{
unsigned int order = folio_order(folio);
unsigned int size = 1 << order;
+ bool may_vswap;

VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);

+ may_vswap = folio_may_vswap(folio);
+
if (order) {
/*
* Reject large allocation when THP_SWAP is disabled. Check below
@@ -1919,12 +2000,16 @@ int folio_alloc_swap(struct folio *folio)
}
}

+ if (may_vswap && vswap_alloc(folio))
+ goto done;
+
again:
local_lock(&percpu_swap_cluster.lock);
if (!swap_alloc_fast(folio))
swap_alloc_slow(folio);
local_unlock(&percpu_swap_cluster.lock);

+done:
if (!order && unlikely(!folio_test_swapcache(folio))) {
if (swap_sync_discard())
goto again;
@@ -1942,7 +2027,7 @@ int folio_alloc_swap(struct folio *folio)
return 0;

failed:
- if (get_nr_swap_pages() <= 0)
+ if (get_nr_swap_pages() <= 0 && !may_vswap)
return -ENOSPC;
if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
return -ENOMEM;
--
2.53.1-Meta