* [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
@ 2026-12-03 21:59 ` Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
` (19 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
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
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
@ 2026-12-04 22:09 ` sashiko-bot
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm/swap: duplicate swap_table_lookup() in
swap_cache_get_folio()
- [Medium] mm/swap: dual-personality swap_is_vswap() branching bypasses
swap_ops
- [Medium] mm/swap: vswap_alloc_cluster() omits __GFP_NOMEMALLOC and
sleep fallback
- [Medium] mm/swap: vswap bypasses swapon/swapoff lifecycle and
si->users refcounting
--
commit 4227eddefaec09d67150d2d6a27d66cb6f9fe4cd
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: prepare dynamic clusters for vswap
This commit adds an XArray-backed dynamic cluster pool for virtual swap
devices alongside the static cluster array used by physical devices, and
updates cluster lookup, locking, allocation, and teardown paths to handle
both layouts.
> diff --git a/mm/swap.h b/mm/swap.h
> index d488a69c28091..0eaedbedc3d6a 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;
> +};
[ ... ]
> @@ -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];
> }
[Severity: Medium]
As reported by Chris Li on v3 [1], splitting cluster storage, lookup, and
locking into dual physical and virtual personalities via
swap_cluster_info_dynamic and swap_is_vswap() checks fragments core swap
paths instead of encapsulating virtual indirection behind struct swap_ops.
This patch adds over twenty swap_is_vswap() branches across core swap
helpers (including nullable locking in __vswap_cluster_lock() and
special-cased teardown in __free_cluster()), and by the end of the series
vswap_ops is still empty. Can this indirection live behind swap_ops?
[ ... ]
> diff --git a/mm/swap_state.c b/mm/swap_state.c
> index da65d75ceead1..436f82319425f 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;
[Severity: Medium]
Does calling swap_table_lookup(entry) twice here double the XArray walk on
every swap-cache hit?
Each swap_table_lookup() enters RCU, looks up swap_info[], checks
swap_is_vswap(), and for vswap traverses si->cluster_info_pool via
xa_load(). Once commit "mm/swap: make vswap mandatory" makes vswap
mandatory, every swap-in cache hit pays two full XArray walks.
Because folio_try_get() does not sleep, a single rcu_read_lock() across
__swap_entry_to_cluster(), folio_try_get(), and re-reading
rcu_dereference(ci->table) avoids the second lookup while still detecting
concurrent cluster teardown (which clears ci->table before kfree_rcu()).
[ ... ]
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d59fa82208620..818189c82ea9f 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -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);
> +}
[Severity: Medium]
Can vswap_alloc_cluster() deplete emergency memory reserves during reclaim
by omitting __GFP_NOMEMALLOC?
Most folio_alloc_swap() callers (such as kswapd() and direct reclaim) run
with PF_MEMALLOC set. Without __GFP_NOMEMALLOC, __gfp_pfmemalloc_flags()
grants ALLOC_NO_WATERMARKS to the ci_dyn, swap table, and XArray node
allocations here, bypassing zone watermarks and risking reclaim deadlocks.
In addition, non-PF_MEMALLOC callers such as ttm_backup_backup_folio() ->
shmem_write_folio() have no sleepable fallback and fail immediately when
non-blocking watermarks are tight. Physical swap_cluster_populate() passes
__GFP_NOMEMALLOC and drops locks to retry with GFP_KERNEL on failure.
[ ... ]
> @@ -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;
[Severity: Medium]
As reported by Chris Li on v3 [1], bypassing si->users refcounting here
(and in put_swap_device()) along with keeping vswap off swap_active_head in
_enable_swap_info() removes vswap from the standard swapon/swapoff
lifecycle and /etc/fstab control.
This changes user-visible behavior by allowing swapout to zswap without an
explicit swapon, and commit "mm/swap: make vswap mandatory" later makes
vswap unconditional at boot. Should vswap instead go through normal swapon
and swapoff lifecycle management?
[1] https://lore.kernel.org/r/CACePvbX+3tO91BmRwaLqf3Xia32GCemt3W8tayErG81BYLgrzA@mail.gmail.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=1
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 02/20] mm, swap: prepare the swap IO path for vswap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
@ 2026-12-03 21:59 ` 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
` (18 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Swap I/O derives its device and sector from folio->swap. A virtual entry
identifies the folio's slot, so the I/O path needs its selected physical
entry separately.
Pass the physical entry through submission and completion. Check batch
adjacency using the previous folio's entry and size, and carry reclaim's
NOFS restriction for the backend check. Physical folios still supply
their own entry.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
include/linux/swap.h | 2 +-
include/linux/swap_ops.h | 11 ++++---
mm/page_io.c | 62 +++++++++++++++++++++-------------------
mm/swap.h | 3 +-
mm/swapfile.c | 6 ++--
mm/vmscan.c | 4 ++-
mm/zswap.c | 2 +-
7 files changed, 49 insertions(+), 41 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index b3849df810f7..26f63356da82 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -412,7 +412,7 @@ extern int __swap_count(swp_entry_t entry);
extern bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry);
extern int swp_swapcount(swp_entry_t entry);
extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
-sector_t swap_folio_sector(struct folio *folio);
+sector_t swap_entry_sector(swp_entry_t entry);
/*
* If there is an existing swap slot reference (swap entry) and the caller
diff --git a/include/linux/swap_ops.h b/include/linux/swap_ops.h
index d57b95931cd9..61c5abf59a7b 100644
--- a/include/linux/swap_ops.h
+++ b/include/linux/swap_ops.h
@@ -12,11 +12,14 @@ struct swap_iocb {
struct bio_vec bvecs[SWAP_CLUSTER_MAX];
int nr_bvecs;
int len;
+ swp_entry_t entry; /* first slot in the batch; addresses the IO */
+ swp_entry_t prev_entry;
};
struct swap_io_ctx {
struct swap_iocb *sio;
struct swap_info_struct *sis;
+ bool nofs;
/*
* Pressure window: opened by swap_read_folio() on the first folio
@@ -40,15 +43,15 @@ struct swap_io_ctx {
struct swap_ops {
unsigned int flags;
- bool (*can_merge)(struct folio *folio, struct folio *prev_folio,
- size_t prev_folio_size, int rw);
+ bool (*can_merge)(struct folio *folio, swp_entry_t phys,
+ struct swap_iocb *sio, int rw);
void (*submit_write)(struct swap_io_ctx *ctx);
void (*submit_read)(struct swap_io_ctx *ctx);
};
void swap_fs_prepare_rw(struct swap_io_ctx *ctx, int rw, struct iov_iter *iter);
-bool swap_fs_can_merge(struct folio *folio, struct folio *prev_folio,
- size_t prev_folio_size, int rw);
+bool swap_fs_can_merge(struct folio *folio, swp_entry_t phys,
+ struct swap_iocb *sio, int rw);
int swap_fs_activate(struct swap_info_struct *sis, const struct swap_ops *ops);
#endif /* _MM_SWAP_OPS_H */
diff --git a/mm/page_io.c b/mm/page_io.c
index 62d1a5d42287..062bfce6f0de 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -265,7 +265,7 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
return AOP_WRITEPAGE_ACTIVATE;
}
- __swap_writeout(ctx, folio);
+ __swap_writeout(ctx, folio, folio->swap);
return 0;
out_unlock:
folio_unlock(folio);
@@ -340,36 +340,36 @@ int sio_pool_init(void)
}
static bool swap_can_merge(struct swap_io_ctx *ctx, struct folio *folio,
- int rw)
+ swp_entry_t phys, int rw)
{
- struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
- struct bio_vec *last_bv = &ctx->sio->bvecs[ctx->sio->nr_bvecs - 1];
- struct folio *prev_folio = bvec_folio(last_bv);
- size_t prev_folio_size = folio_size(prev_folio);
+ struct swap_info_struct *sis = __swap_entry_to_info(phys);
if (ctx->sis != sis)
return false;
- return sis->ops->can_merge(folio, prev_folio, prev_folio_size, rw);
+ return sis->ops->can_merge(folio, phys, ctx->sio, rw);
}
-static void swap_add_folio(struct swap_io_ctx *ctx, struct folio *folio, int rw)
+static void swap_add_folio(struct swap_io_ctx *ctx, struct folio *folio,
+ swp_entry_t phys, int rw)
{
- struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
+ struct swap_info_struct *sis = __swap_entry_to_info(phys);
struct swap_iocb *sio = ctx->sio;
/*
* Callers flush a batch this folio can't join before adding it,
* see swap_read_folio() and __swap_writeout().
*/
- VM_WARN_ON_ONCE(sio && !swap_can_merge(ctx, folio, rw));
+ VM_WARN_ON_ONCE(sio && !swap_can_merge(ctx, folio, phys, rw));
if (!sio) {
ctx->sis = sis;
ctx->sio = sio = mempool_alloc(sio_pool, GFP_NOIO);
sio->nr_bvecs = 0;
sio->len = 0;
+ sio->entry = phys;
}
bvec_set_folio(&sio->bvecs[sio->nr_bvecs], folio, folio_size(folio), 0);
+ sio->prev_entry = phys;
sio->len += folio_size(folio);
/*
@@ -388,7 +388,8 @@ static void swap_add_folio(struct swap_io_ctx *ctx, struct folio *folio, int rw)
}
}
-void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
+void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio,
+ swp_entry_t phys)
{
VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
@@ -410,9 +411,9 @@ void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
* swap_read_folio(). But the write path opens no pressure window,
* so there is nothing to order this against.
*/
- if (ctx->sio && !swap_can_merge(ctx, folio, WRITE))
+ if (ctx->sio && !swap_can_merge(ctx, folio, phys, WRITE))
swap_write_submit(ctx);
- swap_add_folio(ctx, folio, WRITE);
+ swap_add_folio(ctx, folio, phys, WRITE);
}
/*
@@ -493,7 +494,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
* window opens, so that each batch is billed for its own read
* rather than for the next folio's lookup and decompression.
*/
- if (ctx->sio && !swap_can_merge(ctx, folio, READ))
+ if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ))
swap_read_submit(ctx);
/*
@@ -529,7 +530,7 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
- swap_add_folio(ctx, folio, READ);
+ swap_add_folio(ctx, folio, folio->swap, READ);
/*
* Synchronous devices do the whole read at submission time, submit
@@ -561,8 +562,6 @@ static void swap_fs_write_complete(struct kiocb *iocb, long ret)
bool failed = ret != sio->len;
if (failed) {
- struct folio *folio = bvec_folio(&sio->bvecs[0]);
-
/*
* In the case of swap-over-nfs, this can be a temporary failure
* if the system has limited memory for allocating transmit
@@ -570,7 +569,7 @@ static void swap_fs_write_complete(struct kiocb *iocb, long ret)
* folio_rotate_reclaimable but rate-limit the messages.
*/
pr_err_ratelimited("Write error %ld on dio swapfile (%llu)\n",
- ret, swap_dev_pos(folio->swap));
+ ret, swap_dev_pos(sio->entry));
}
swap_write_end(sio, failed);
@@ -642,7 +641,7 @@ static void swap_bdev_submit_write(struct swap_io_ctx *ctx)
bio_init(bio, ctx->sis->bdev, sio->bvecs, ARRAY_SIZE(sio->bvecs),
REQ_OP_WRITE | REQ_SWAP);
bio->bi_iter.bi_size = sio->len;
- bio->bi_iter.bi_sector = swap_folio_sector(bio_first_folio_all(bio));
+ bio->bi_iter.bi_sector = swap_entry_sector(sio->entry);
bio_associate_blkg_from_folio(bio, bio_first_folio_all(bio));
if (ctx->sis->flags & SWP_SYNCHRONOUS_IO) {
@@ -671,7 +670,7 @@ static void swap_bdev_submit_read(struct swap_io_ctx *ctx)
bio_init(bio, ctx->sis->bdev, sio->bvecs, ARRAY_SIZE(sio->bvecs),
REQ_OP_READ);
bio->bi_iter.bi_size = sio->len;
- bio->bi_iter.bi_sector = swap_folio_sector(bio_first_folio_all(bio));
+ bio->bi_iter.bi_sector = swap_entry_sector(sio->entry);
if (ctx->sis->flags & SWP_SYNCHRONOUS_IO) {
/*
@@ -689,13 +688,15 @@ static void swap_bdev_submit_read(struct swap_io_ctx *ctx)
}
}
-static bool swap_bdev_can_merge(struct folio *folio, struct folio *prev_folio,
- size_t prev_folio_size, int rw)
+static bool swap_bdev_can_merge(struct folio *folio, swp_entry_t phys,
+ struct swap_iocb *sio, int rw)
{
- if (swap_folio_sector(folio) !=
- swap_folio_sector(prev_folio) + (prev_folio_size >> SECTOR_SHIFT))
+ struct bio_vec *last_bv = &sio->bvecs[sio->nr_bvecs - 1];
+
+ if (swap_entry_sector(phys) !=
+ swap_entry_sector(sio->prev_entry) + (last_bv->bv_len >> SECTOR_SHIFT))
return false;
- if (rw == WRITE && !folio_blkg_can_merge(folio, prev_folio))
+ if (rw == WRITE && !folio_blkg_can_merge(folio, bvec_folio(last_bv)))
return false;
return true;
}
@@ -711,7 +712,7 @@ void swap_fs_prepare_rw(struct swap_io_ctx *ctx, int rw, struct iov_iter *iter)
struct swap_iocb *sio = ctx->sio;
init_sync_kiocb(&sio->iocb, ctx->sis->swap_file);
- sio->iocb.ki_pos = swap_dev_pos(bvec_folio(&sio->bvecs[0])->swap);
+ sio->iocb.ki_pos = swap_dev_pos(sio->entry);
if (rw == WRITE)
sio->iocb.ki_complete = swap_fs_write_complete;
else
@@ -722,11 +723,12 @@ void swap_fs_prepare_rw(struct swap_io_ctx *ctx, int rw, struct iov_iter *iter)
}
EXPORT_SYMBOL_GPL(swap_fs_prepare_rw);
-bool swap_fs_can_merge(struct folio *folio, struct folio *prev_folio,
- size_t prev_folio_size, int rw)
+bool swap_fs_can_merge(struct folio *folio, swp_entry_t phys,
+ struct swap_iocb *sio, int rw)
{
- return swap_dev_pos(folio->swap) ==
- swap_dev_pos(prev_folio->swap) + prev_folio_size;
+ struct bio_vec *last_bv = &sio->bvecs[sio->nr_bvecs - 1];
+
+ return swap_dev_pos(phys) == swap_dev_pos(sio->prev_entry) + last_bv->bv_len;
}
EXPORT_SYMBOL_GPL(swap_fs_can_merge);
diff --git a/mm/swap.h b/mm/swap.h
index 0eaedbedc3d6..95c5be9c8c27 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -320,7 +320,8 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio);
void swap_read_submit(struct swap_io_ctx *ctx);
void swap_write_submit(struct swap_io_ctx *ctx);
int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio);
-void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio);
+void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio,
+ swp_entry_t phys);
/* linux/mm/swap_state.c */
extern struct address_space swap_space __read_mostly;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 818189c82ea9..153e73b96a7a 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -330,14 +330,14 @@ offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)
BUG();
}
-sector_t swap_folio_sector(struct folio *folio)
+sector_t swap_entry_sector(swp_entry_t entry)
{
- struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
+ struct swap_info_struct *sis = __swap_entry_to_info(entry);
struct swap_extent *se;
sector_t sector;
pgoff_t offset;
- offset = swp_offset(folio->swap);
+ offset = swp_offset(entry);
se = offset_to_swap_extent(sis, offset);
sector = se->start_block + (offset - se->start_page);
return sector << (PAGE_SHIFT - 9);
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 99cb209797b4..3ad66f2a16d8 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1115,7 +1115,9 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
unsigned int nr_reclaimed = 0, nr_demoted = 0;
unsigned int pgactivate = 0;
bool do_demote_pass;
- struct swap_io_ctx ctx = {};
+ struct swap_io_ctx ctx = {
+ .nofs = !(sc->gfp_mask & __GFP_FS),
+ };
folio_batch_init(&free_folios);
memset(stat, 0, sizeof(*stat));
diff --git a/mm/zswap.c b/mm/zswap.c
index ff3b0ceb10f4..4732d2938f4a 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1163,7 +1163,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
folio_put(folio);
/* start writeback */
- __swap_writeout(ctx, folio);
+ __swap_writeout(ctx, folio, folio->swap);
return 0;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 02/20] mm, swap: prepare the swap IO path for vswap
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: swap_ops and vswap architecture conflict with swap
maintainer direction
--
commit 429f81aecb8acc9ffb2ffad6070376dea9df3f7e
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: prepare the swap IO path for vswap
This commit updates the swap I/O submission, batch merging, and
completion paths to pass a separate physical swap entry alongside each
folio, and adds a nofs flag to struct swap_io_ctx for reclaim.
> diff --git a/include/linux/swap_ops.h b/include/linux/swap_ops.h
> index d57b95931cd94..61c5abf59a7b5 100644
> --- a/include/linux/swap_ops.h
> +++ b/include/linux/swap_ops.h
> @@ -12,11 +12,14 @@ struct swap_iocb {
> struct bio_vec bvecs[SWAP_CLUSTER_MAX];
> int nr_bvecs;
> int len;
> + swp_entry_t entry; /* first slot in the batch; addresses the IO */
> + swp_entry_t prev_entry;
> };
>
> struct swap_io_ctx {
> struct swap_iocb *sio;
> struct swap_info_struct *sis;
> + bool nofs;
>
> /*
> * Pressure window: opened by swap_read_folio() on the first folio
> @@ -40,15 +43,15 @@ struct swap_io_ctx {
> struct swap_ops {
> unsigned int flags;
>
> - bool (*can_merge)(struct folio *folio, struct folio *prev_folio,
> - size_t prev_folio_size, int rw);
> + bool (*can_merge)(struct folio *folio, swp_entry_t phys,
> + struct swap_iocb *sio, int rw);
> void (*submit_write)(struct swap_io_ctx *ctx);
> void (*submit_read)(struct swap_io_ctx *ctx);
> };
>
> void swap_fs_prepare_rw(struct swap_io_ctx *ctx, int rw, struct iov_iter *iter);
> -bool swap_fs_can_merge(struct folio *folio, struct folio *prev_folio,
> - size_t prev_folio_size, int rw);
> +bool swap_fs_can_merge(struct folio *folio, swp_entry_t phys,
> + struct swap_iocb *sio, int rw);
> int swap_fs_activate(struct swap_info_struct *sis, const struct swap_ops *ops);
>
> #endif /* _MM_SWAP_OPS_H */
[Severity: Medium]
As reported by Kairui Song on RFC [1], should virtual swap be routed
through a generic VFS-like swap_ops extension layer rather than plumbing
a separate physical swp_entry_t around swap_ops?
Changing struct swap_ops::can_merge and exported swap_fs_can_merge() to
pass phys alongside swap_iocb threads vswap-specific state through
mm/page_io.c while vswap_ops stays empty. Landing that non-generic
interface first adds extra churn to reach extendable swap_ops later.
The series also keeps the two-personality cluster layout (cluster_info
and cluster_info_pool), enables zswap without swapon in commit
"mm/swap: make vswap mandatory", and bundles vswap plumbing, charging,
and metadata changes into one series rather than incremental phases.
[1] https://lore.kernel.org/r/ahz_iYG4lqWL4g-J@KASONG-MC4
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=2
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
@ 2026-12-03 21:59 ` 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
` (17 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Virtual swap entries need backend state that survives swapcache changes.
Store compressed entries in a per-cluster vtable and retain the swap
table's zero bit for zero-filled pages.
Store aligned zswap pointers directly in the vtable. Update virtual
entry release and swapcache handling to release compressed backing
through the vtable when entries are freed or their backing is replaced.
Keep the virtual device unregistered until physical I/O and swapoff are
ready. Once virtual slots are allocated, zswap rejection must have a
working physical fallback.
Suggested-by: Kairui Song <kasong@tencent.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
include/linux/zswap.h | 4 +
mm/memory.c | 11 ++-
mm/page_io.c | 17 +++-
mm/swap.h | 1 +
mm/swap_state.c | 8 ++
mm/swap_table.h | 2 +
mm/swapfile.c | 87 ++++++++++++++++++++
mm/vswap.h | 182 ++++++++++++++++++++++++++++++++++++++++++
mm/zswap.c | 81 ++++++++++++++++---
9 files changed, 374 insertions(+), 19 deletions(-)
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index 94746fb71bb6..b90f330f87cd 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;
@@ -28,6 +29,7 @@ unsigned long zswap_total_pages(void);
bool zswap_store(struct folio *folio);
int zswap_load(struct folio *folio);
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries);
+void zswap_entry_free(struct zswap_entry *entry);
int zswap_swapon(int type, unsigned long nr_pages);
void zswap_swapoff(int type);
void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg);
@@ -55,6 +57,8 @@ static inline void zswap_invalidate(int type, pgoff_t offset,
{
}
+static inline void zswap_entry_free(struct zswap_entry *entry) {}
+
static inline int zswap_swapon(int type, unsigned long nr_pages)
{
return 0;
diff --git a/mm/memory.c b/mm/memory.c
index 90fc7c395124..9ba6ebd58132 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -89,6 +89,7 @@
#include "pgalloc-track.h"
#include "internal.h"
#include "swap.h"
+#include "vswap.h"
#if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
@@ -4753,6 +4754,9 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
{
if (!folio_test_swapcache(folio))
return false;
+ /* A vswap entry holds no physical slot, so keeping it saves no IO. */
+ if (is_vswap_entry(folio->swap))
+ return true;
/*
* Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
* cache can help save some IO or memory overhead, but these devices
@@ -4910,12 +4914,16 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
if (unlikely(userfaultfd_armed(vma)))
return 0;
+ entry = softleaf_from_pte(vmf->orig_pte);
+
/*
* A large swapped out folio could be partially or fully in zswap. We
* lack handling for such cases, so fallback to swapping in order-0
* folio.
+ *
+ * THP swapin for vswap is not supported yet either.
*/
- if (!zswap_never_enabled())
+ if (is_vswap_entry(entry) || !zswap_never_enabled())
return 0;
/*
@@ -4926,7 +4934,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
if (!data_race(si->flags & SWP_BLKDEV))
return 0;
- entry = softleaf_from_pte(vmf->orig_pte);
/*
* Get a list of all the (large) orders below PMD_ORDER that are enabled
* and suitable for swapping THP.
diff --git a/mm/page_io.c b/mm/page_io.c
index 062bfce6f0de..9397ef83a60d 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -162,13 +162,18 @@ static void swap_zeromap_folio_set(struct folio *folio)
int nr_pages = folio_nr_pages(folio);
struct swap_cluster_info *ci;
swp_entry_t entry = folio->swap;
- unsigned int i;
+ unsigned int voff, i;
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
ci = swap_cluster_get_and_lock(folio);
- for (i = 0; i < folio_nr_pages(folio); i++) {
+ if (is_vswap_entry(folio->swap)) {
+ /* Free any prior backing (e.g. ZSWAP entry from earlier swapout) */
+ voff = swp_cluster_offset(folio->swap);
+ __vswap_release_backing(ci, voff, nr_pages);
+ }
+ for (i = 0; i < nr_pages; i++) {
__swap_table_set_zero(ci, swp_cluster_offset(entry));
entry.val++;
}
@@ -234,8 +239,9 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
* again while it stayed in the swap cache. Drop the old copy,
* or zswap writeback would bring it back into the swap cache.
*/
- zswap_invalidate(swp_type(entry), swp_offset(entry),
- folio_nr_pages(folio));
+ if (!is_vswap_entry(entry))
+ zswap_invalidate(swp_type(entry), swp_offset(entry),
+ folio_nr_pages(folio));
goto out_unlock;
}
@@ -246,6 +252,9 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
*/
swap_zeromap_folio_clear(folio);
+ if (is_vswap_entry(folio->swap))
+ folio_release_vswap_backing(folio);
+
if (zswap_store(folio)) {
count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
goto out_unlock;
diff --git a/mm/swap.h b/mm/swap.h
index 95c5be9c8c27..7b9a65232d19 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -71,6 +71,7 @@ struct swap_cluster_info_dynamic {
struct swap_cluster_info ci;
unsigned int index; /* for cluster_index() */
struct rcu_head rcu;
+ atomic_long_t *virtual_table; /* Backing pointers for vswap slots */
};
/* All on-list cluster must have a non-zero flag. */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 436f82319425..110a0c095825 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -27,6 +27,7 @@
#include "internal.h"
#include "swap_table.h"
#include "swap.h"
+#include "vswap.h"
/* Swap readahead cluster size, as a power of 2 pages. */
static int page_cluster;
@@ -202,6 +203,13 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
if (nr == 1)
return 0;
+ /*
+ * Reject a vswap batch so __swap_cache_alloc_folio falls back to
+ * order 0.
+ */
+ if (is_vswap_entry(targ_entry))
+ return -EBUSY;
+
is_zero = __swap_table_test_zero(ci, ci_off);
ci_off = round_down(ci_off, nr);
ci_end = ci_off + nr;
diff --git a/mm/swap_table.h b/mm/swap_table.h
index c21a48c98cc6..9c9e3cd3b37e 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -6,6 +6,8 @@
#include <linux/atomic.h>
#include "swap.h"
+extern struct swap_info_struct *vswap_si;
+
/* A typical flat array in each cluster as swap table */
struct swap_table {
atomic_long_t entries[SWAPFILE_CLUSTER];
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 153e73b96a7a..1387c8bb1e97 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -669,6 +669,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
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);
+ vswap_cluster_free_vtable(ci);
kfree_rcu(ci_dyn, rcu);
return;
}
@@ -1161,11 +1162,19 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
return SWAP_ENTRY_INVALID;
}
+ if (vswap_cluster_alloc_vtable(ci_dyn, GFP_ATOMIC | __GFP_NOWARN)) {
+ spin_unlock(&ci->lock);
+ swap_cluster_free_table(ci);
+ 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);
+ vswap_cluster_free_vtable(&ci_dyn->ci);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
}
@@ -1941,6 +1950,73 @@ int folio_alloc_swap(struct folio *folio)
return order ? -E2BIG : -ENOMEM;
}
+/**
+ * __vswap_release_backing - release the backing of a range of vtable slots
+ * @ci: the locked vswap cluster
+ * @ci_start: first slot offset within @ci
+ * @nr: number of slots
+ *
+ * Releases the backing of each slot in [@ci_start, @ci_start + @nr).
+ * Clears the zero marks if set.
+ *
+ * Context: caller must hold @ci->lock.
+ */
+void __vswap_release_backing(struct swap_cluster_info *ci,
+ unsigned int ci_start, unsigned int nr)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int ci_off;
+ unsigned long vt;
+
+ lockdep_assert_held(&ci->lock);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+
+ for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
+ vt = __vtable_get(ci_dyn, ci_off);
+
+ switch (vtable_type(vt)) {
+ case VSWAP_ZSWAP:
+ zswap_entry_free(vtable_to_zswap(vt));
+ break;
+ case VSWAP_NONE:
+ break;
+ default:
+ /* VSWAP_ZERO/VSWAP_FOLIO are return-only, not vtable tags */
+ break;
+ }
+
+ __vtable_set(ci_dyn, ci_off, VSWAP_NONE);
+ /* Zero-backed state lives in swap_table; clear it too. */
+ if (__swap_table_test_zero(ci, ci_off))
+ __swap_table_clear_zero(ci, ci_off);
+ }
+}
+
+/**
+ * folio_release_vswap_backing() - Drop all backing for a folio's vswap entry.
+ * @folio: the folio, occupying a virtual swap entry.
+ *
+ * Release whatever backing the folio's virtual swap slots currently hold and
+ * reset them to empty, so a fresh backing can be installed. Used when a
+ * folio's swap backend is replaced.
+ *
+ * Context: Caller must hold the folio lock; @folio must be in the swap cache
+ * and occupy a virtual swap entry.
+ */
+void folio_release_vswap_backing(struct folio *folio)
+{
+ struct swap_cluster_info *ci;
+ int nr = folio_nr_pages(folio);
+ unsigned int voff;
+
+ ci = __swap_entry_to_cluster(folio->swap);
+ voff = swp_cluster_offset(folio->swap);
+
+ spin_lock(&ci->lock);
+ __vswap_release_backing(ci, voff, nr);
+ spin_unlock(&ci->lock);
+}
+
/**
* folio_dup_swap() - Increase swap count of swap entries of a folio.
* @folio: folio with swap entries bounded.
@@ -2086,6 +2162,9 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
VM_WARN_ON(ci->count < nr_pages);
+ if (swap_is_vswap(si))
+ __vswap_release_backing(ci, ci_start, nr_pages);
+
ci->count -= nr_pages;
do {
old_tb = __swap_table_get(ci, ci_off);
@@ -3315,6 +3394,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si,
swap_cluster_free_table(ci);
}
spin_unlock(&ci->lock);
+ vswap_cluster_free_vtable(ci);
kfree(ci_dyn);
}
xa_destroy(&si->cluster_info_pool);
@@ -3844,6 +3924,10 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
if (err)
goto err;
+ err = vswap_cluster_alloc_vtable(ci_dyn, GFP_KERNEL);
+ if (err)
+ goto err;
+
goto setup_cluster_info;
}
@@ -4274,3 +4358,6 @@ static int __init swapfile_init(void)
return 0;
}
subsys_initcall(swapfile_init);
+
+struct swap_info_struct *vswap_si;
+DEFINE_STATIC_KEY_FALSE(vswap_key);
diff --git a/mm/vswap.h b/mm/vswap.h
index bf1606263e8e..f64b1543e01f 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -7,16 +7,198 @@
#ifndef _MM_VSWAP_H
#define _MM_VSWAP_H
+#include <linux/jump_label.h>
#include <linux/swap.h>
#include "swap.h"
+struct zswap_entry;
+
+/*
+ * VSWAP_ZERO and VSWAP_FOLIO are derived from swap_table state.
+ */
+enum vswap_backing_type {
+ VSWAP_NONE = 0,
+ VSWAP_ZSWAP = 1,
+ VSWAP_ZERO,
+ VSWAP_FOLIO,
+};
+
#ifdef CONFIG_SWAP
+#include "swap_table.h"
+DECLARE_STATIC_KEY_FALSE(vswap_key);
+
+static inline bool vswap_is_enabled(void)
+{
+ return static_branch_unlikely(&vswap_key);
+}
+
static inline bool is_vswap_entry(swp_entry_t entry)
{
return swap_is_vswap(__swap_entry_to_info(entry));
}
+/*
+ * Virtual table entry encoding for vswap clusters.
+ *
+ * Zero represents no separate backing. An aligned zswap pointer
+ * has bit 0 clear and is stored directly in the vtable.
+ *
+ * vtable[i] = NONE does not by itself mean "free". The swap_table entry
+ * and the per-slot zero flag carry the rest of the state. The full
+ * per-slot state table is:
+ *
+ * vtable[i] | swap_table[i] | zero | meaning
+ * ----------+---------------+-------+--------------------------------
+ * NONE | NULL | clear | truly free / unbacked
+ * NONE | PFN | clear | folio cached, no backing
+ * NONE | shadow | clear | evicted, no backing: data lost
+ * NONE | * | set | zero-backed; cached if PFN set
+ * ZSWAP | PFN | clear | folio cached + zswap entry
+ * ZSWAP | shadow / NULL | clear | evicted, only in zswap
+ *
+ * Locking: the caller must own and lock the slot's swapcache folio to
+ * stabilize its backing. The cluster lock only makes individual vtable
+ * reads atomic; it does not permit backend changes. Revalidate speculative
+ * reads under the folio lock before using them.
+ *
+ * Zero-backed slots use the swap_table per-slot zero flag (same as
+ * direct-mapped physical swap), via __swap_table_test_zero() and friends,
+ * which fall back to ci->zero_bitmap where the flag does not fit. Cached
+ * folios are read out of the swap_table PFN entry; there is no separate FOLIO
+ * vtable type because the folio pointer would duplicate that PFN and
+ * would go stale on folio migration / split.
+ */
+
+#define VTABLE_TAG_BITS 1
+#define VTABLE_TAG_MASK ((1UL << VTABLE_TAG_BITS) - 1)
+
+static inline enum vswap_backing_type vtable_type(unsigned long vt)
+{
+ return vt ? VSWAP_ZSWAP : VSWAP_NONE;
+}
+
+static inline struct zswap_entry *vtable_to_zswap(unsigned long vt)
+{
+ VM_WARN_ON(vtable_type(vt) != VSWAP_ZSWAP);
+ return (struct zswap_entry *)vt;
+}
+
+/* Virtual table accessors */
+
+static inline unsigned long __vtable_get(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int off)
+{
+ VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
+ return atomic_long_read(&ci_dyn->virtual_table[off]);
+}
+
+static inline void __vtable_set(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int off, unsigned long vt)
+{
+ VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
+ atomic_long_set(&ci_dyn->virtual_table[off], vt);
+}
+
+/**
+ * vswap_lock_cluster - look up and lock the vswap cluster for an entry
+ * @entry: the virtual swap entry
+ * @voff: out param, receives @entry's slot offset within the cluster
+ *
+ * Return: the locked vswap cluster, or NULL if @entry has no live cluster.
+ */
+static inline struct swap_cluster_info_dynamic *
+vswap_lock_cluster(swp_entry_t entry, unsigned int *voff)
+{
+ struct swap_cluster_info *ci;
+
+ ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry));
+ if (!ci)
+ return NULL;
+ *voff = swp_cluster_offset(entry);
+ return container_of(ci, struct swap_cluster_info_dynamic, ci);
+}
+
+void __vswap_release_backing(struct swap_cluster_info *ci,
+ unsigned int ci_start, unsigned int nr);
+
+/**
+ * vswap_zswap_store - record a zswap entry as the backing for a vswap entry.
+ * @entry: the vswap entry
+ * @ze: the zswap entry now holding @entry's compressed data
+ *
+ * Releases @entry's previous backing, and sets the zswap entry @ze as the new
+ * backing.
+ *
+ * Context: takes and drops the vswap cluster lock internally.
+ */
+static inline void vswap_zswap_store(swp_entry_t entry,
+ struct zswap_entry *ze)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int voff;
+
+ ci_dyn = vswap_lock_cluster(entry, &voff);
+ __vswap_release_backing(&ci_dyn->ci, voff, 1);
+ __vtable_set(ci_dyn, voff, (unsigned long)ze);
+ swap_cluster_unlock(&ci_dyn->ci);
+}
+
+/**
+ * vswap_zswap_load - return the zswap entry backing a vswap entry
+ * @entry: the virtual swap entry
+ *
+ * Context: takes and drops the vswap cluster lock internally.
+ * Return: the backing zswap entry, or NULL if @entry is not zswap-backed.
+ */
+static inline struct zswap_entry *vswap_zswap_load(swp_entry_t entry)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int voff;
+ unsigned long vt;
+
+ ci_dyn = vswap_lock_cluster(entry, &voff);
+ if (!ci_dyn)
+ return NULL;
+ vt = __vtable_get(ci_dyn, voff);
+ swap_cluster_unlock(&ci_dyn->ci);
+
+ if (vtable_type(vt) != VSWAP_ZSWAP)
+ return NULL;
+ return vtable_to_zswap(vt);
+}
+
+void folio_release_vswap_backing(struct folio *folio);
+
+static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
+ gfp_t gfp)
+{
+ ci_dyn->virtual_table = kcalloc(SWAPFILE_CLUSTER,
+ sizeof(*ci_dyn->virtual_table), gfp);
+ return ci_dyn->virtual_table ? 0 : -ENOMEM;
+}
+
+static inline void vswap_cluster_free_vtable(struct swap_cluster_info *ci)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ kfree(ci_dyn->virtual_table);
+ ci_dyn->virtual_table = NULL;
+}
+
+#else /* !CONFIG_SWAP */
+
+static inline bool vswap_is_enabled(void)
+{
+ return false;
+}
+
+static inline bool is_vswap_entry(swp_entry_t entry)
+{
+ return false;
+}
+
#endif /* CONFIG_SWAP */
#endif /* _MM_VSWAP_H */
diff --git a/mm/zswap.c b/mm/zswap.c
index 4732d2938f4a..aab8ec782d03 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -40,6 +40,7 @@
#include <linux/zsmalloc.h>
#include "swap.h"
+#include "vswap.h"
#include "internal.h"
/*********************************
@@ -269,6 +270,25 @@ static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
return zswap_tree(swp_type(swp), swp_offset(swp));
}
+static struct zswap_entry *zswap_entry_load(swp_entry_t swp)
+{
+ if (is_vswap_entry(swp))
+ return vswap_zswap_load(swp);
+ return xa_load(swap_zswap_tree(swp), swp_offset(swp));
+}
+
+static struct zswap_entry *zswap_entry_store(swp_entry_t swp,
+ struct zswap_entry *entry)
+{
+ if (is_vswap_entry(swp)) {
+ vswap_zswap_store(swp, entry);
+ return NULL;
+ }
+
+ return xa_store(swap_zswap_tree(swp), swp_offset(swp), entry,
+ GFP_KERNEL);
+}
+
#define zswap_pool_debug(msg, p) \
pr_debug("%s pool %s\n", msg, (p)->tfm_name)
@@ -783,7 +803,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);
@@ -1322,6 +1342,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
return 0;
+ if (vswap_is_enabled())
+ return 0;
+
/*
* The shrinker resumes swap writeback, which will enter block
* and may enter fs. XXX: Harmonize with vmscan.c __GFP_FS
@@ -1404,6 +1427,8 @@ static struct shrinker *zswap_alloc_shrinker(void)
* Return: 0 if at least one entry was written back, -EAGAIN if entries
* were scanned but none could be written back, or -ENOENT if @memcg has
* writeback disabled, is a zombie cgroup, or has empty zswap LRUs.
+ *
+ * Also returns -ENOENT when vswap is enabled.
*/
static int shrink_memcg(struct mem_cgroup *memcg)
{
@@ -1414,6 +1439,9 @@ static int shrink_memcg(struct mem_cgroup *memcg)
if (!mem_cgroup_zswap_writeback_enabled(memcg))
return -ENOENT;
+ if (vswap_is_enabled())
+ return -ENOENT;
+
/*
* Skip zombies because their LRUs are reparented and we would be
* reclaiming from the parent instead of the dead memcg.
@@ -1446,6 +1474,13 @@ static void shrink_worker(struct work_struct *w)
int ret, failures = 0, attempts = 0;
unsigned long thr;
+ /*
+ * When vswap is enabled, zswap entries are almost all vswap backed,
+ * with no slot to write back to.
+ */
+ if (vswap_is_enabled())
+ return;
+
/* Reclaim down to the accept threshold */
thr = zswap_accept_thr_pages();
@@ -1549,15 +1584,13 @@ 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
+ * Set pool_idx before the store 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);
+ old = zswap_entry_store(page_swpentry, entry);
if (xa_is_err(old)) {
int err = xa_err(old);
@@ -1623,7 +1656,7 @@ bool zswap_store(struct folio *folio)
struct mem_cgroup *memcg = NULL;
struct zswap_pool *pool;
bool ret = false;
- long index;
+ long index = 0;
VM_WARN_ON_ONCE(!folio_test_locked(folio));
VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
@@ -1676,14 +1709,21 @@ bool zswap_store(struct folio *folio)
if (!ret && zswap_pool_reached_full)
queue_work(shrink_wq, &zswap_shrink_work);
check_old:
+ if (ret)
+ return ret;
+
/*
* If the zswap store fails or zswap is disabled, we must invalidate
* the possibly stale entries which were previously stored at the
* offsets corresponding to each page of the folio. Otherwise,
* writeback could overwrite the new data in the swapfile.
*/
- if (!ret)
+ if (is_vswap_entry(swp)) {
+ if (index > 0)
+ folio_release_vswap_backing(folio);
+ } else {
zswap_invalidate(swp_type(swp), swp_offset(swp), nr_pages);
+ }
return ret;
}
@@ -1700,8 +1740,18 @@ bool zswap_store(struct folio *folio)
bool zswap_is_present(swp_entry_t entry, unsigned int nr)
{
pgoff_t offset = swp_offset(entry);
- struct xarray *tree = swap_zswap_tree(entry);
+ struct xarray *tree;
unsigned long index = offset;
+ unsigned int i;
+
+ /* Virtual entries have no native zswap tree. */
+ if (is_vswap_entry(entry)) {
+ for (i = 0; i < nr; i++)
+ if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i)))
+ return true;
+ return false;
+ }
+ tree = swap_zswap_tree(entry);
if (zswap_never_enabled() || xa_empty(tree))
return false;
@@ -1737,8 +1787,7 @@ 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 swap_info_struct *si = __swap_entry_to_info(swp);
struct zswap_entry *entry;
VM_WARN_ON_ONCE(!folio_test_locked(folio));
@@ -1762,7 +1811,7 @@ int zswap_load(struct folio *folio)
return -ENOENT;
}
- entry = xa_load(tree, offset);
+ entry = zswap_entry_load(swp);
if (!entry)
return -ENOENT;
@@ -1785,8 +1834,13 @@ int zswap_load(struct folio *folio)
* compression work.
*/
folio_mark_dirty(folio);
- xa_erase(tree, offset);
- zswap_entry_free(entry);
+
+ if (swap_is_vswap(si)) {
+ folio_release_vswap_backing(folio);
+ } else {
+ xa_erase(swap_zswap_tree(swp), swp_offset(swp));
+ zswap_entry_free(entry);
+ }
folio_unlock(folio);
return 0;
@@ -1928,6 +1982,7 @@ static int zswap_setup(void)
struct zswap_pool *pool;
int ret;
+ BUILD_BUG_ON(__alignof__(struct zswap_entry) <= VTABLE_TAG_MASK);
zswap_entry_cache = KMEM_CACHE(zswap_entry, 0);
if (!zswap_entry_cache) {
pr_err("entry cache creation failed\n");
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] mm/swap: __vswap_release_backing() holds ci->lock across
zswap_entry_free()
- [Medium] mm: zswap_is_present() bypasses zswap_never_enabled() and
locks per slot
- [Medium] mm/swap: redundant cluster lookup and lock cycle in
swap_writeout()
- [Medium] mm: zswap_store() queues shrink work even when
shrink_worker() bails out
- [Low] mm: zswap_shrinker_scan() omits vswap writeback availability
guard
--
commit f00d7cee664321ec03d5feb2f3fe7858aa5f1f87
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: prepare zswap and zero storage for virtual entries
This commit adds a per-cluster vtable to virtual swap clusters to store
aligned zswap_entry pointers, retains the swap table's zero flag for
zero-filled pages, and updates zswap and swapcache paths to release
compressed backing through the vtable.
> diff --git a/mm/page_io.c b/mm/page_io.c
> index 062bfce6f0de3..9397ef83a60de 100644
> --- a/mm/page_io.c
> +++ b/mm/page_io.c
[ ... ]
> @@ -246,6 +252,9 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
> */
> swap_zeromap_folio_clear(folio);
>
> + if (is_vswap_entry(folio->swap))
> + folio_release_vswap_backing(folio);
> +
> if (zswap_store(folio)) {
[Severity: Medium]
Does this do a redundant cluster XArray lookup and ci->lock cycle on every
non-zero vswap writeout?
swap_zeromap_folio_clear() already looks up the cluster in
si->cluster_info_pool, acquires ci->lock, clears the zero flags, and unlocks.
folio_release_vswap_backing() immediately looks up the same cluster again,
reacquires ci->lock, and calls __vswap_release_backing(), which re-tests and
clears those same zero flags. Combining them under one cluster lock, as
swap_zeromap_folio_set() does, avoids the duplicate lookup and lock cycle.
Similarly, during multi-page zswap_store(), vswap_zswap_store() repeats
vswap_lock_cluster() and __vswap_release_backing() for each page even though
all pages of the locked folio belong to one pinned cluster.
[ ... ]
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 153e73b96a7ab..1387c8bb1e976 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1941,6 +1950,73 @@ int folio_alloc_swap(struct folio *folio)
[ ... ]
> +void __vswap_release_backing(struct swap_cluster_info *ci,
> + unsigned int ci_start, unsigned int nr)
> +{
> + struct swap_cluster_info_dynamic *ci_dyn;
> + unsigned int ci_off;
> + unsigned long vt;
> +
> + lockdep_assert_held(&ci->lock);
> + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> +
> + for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
> + vt = __vtable_get(ci_dyn, ci_off);
> +
> + switch (vtable_type(vt)) {
> + case VSWAP_ZSWAP:
> + zswap_entry_free(vtable_to_zswap(vt));
> + break;
[Severity: Medium]
Can calling zswap_entry_free() while holding ci->lock cause lock contention
across the cluster?
zswap_entry_free() acquires the zswap list_lru lock and zsmalloc's size_class
lock (and may free zspages back to the page allocator), updates percpu_ref
and memcg counters, and frees the entry to slab. On every vswap zswap
swap-in via zswap_load() -> folio_release_vswap_backing(), this holds
ci->lock across that teardown for all 512 slots in the cluster, whereas the
physical swap path detaches the entry and frees it unlocked.
Because callers such as zswap_load() and vswap_zswap_store() already hold
the swapcache folio lock, they could detach the old zswap_entry under
ci->lock and call zswap_entry_free() after unlocking.
[ ... ]
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 4732d2938f4ad..aab8ec782d033 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -1322,6 +1342,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
> if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
> return 0;
>
> + if (vswap_is_enabled())
> + return 0;
> +
[Severity: Low]
Should zswap_shrinker_scan() also carry this vswap_is_enabled() guard?
zswap_shrinker_scan() mirrors the !zswap_shrinker_enabled and
!mem_cgroup_zswap_writeback_enabled() checks, but omits vswap_is_enabled().
shrinker_debugfs_scan_write() invokes scan_objects() directly without calling
count_objects() first, and do_shrink_slab() can invoke another scan batch
after swap space is exhausted, which walks zswap_list_lru, clears
entry->referenced on warm entries, rotates the LRU, and increments
zswap_reject_reclaim_fail on futile writeback attempts.
[ ... ]
> @@ -1446,6 +1474,13 @@ static void shrink_worker(struct work_struct *w)
> int ret, failures = 0, attempts = 0;
> unsigned long thr;
>
> + /*
> + * When vswap is enabled, zswap entries are almost all vswap backed,
> + * with no slot to write back to.
> + */
> + if (vswap_is_enabled())
> + return;
> +
[ ... ]
> @@ -1676,14 +1709,21 @@ bool zswap_store(struct folio *folio)
> if (!ret && zswap_pool_reached_full)
> queue_work(shrink_wq, &zswap_shrink_work);
> check_old:
[Severity: Medium]
Will zswap_store() keep waking shrink_worker() needlessly when
vswap_is_enabled() is true?
Because shrink_worker() returns immediately without shrinking the pool or
clearing zswap_pool_reached_full, the work item clears its pending state
right away and every subsequent rejected zswap_store() under memory pressure
wakes the zswap-shrink kworker again. Guarding queue_work() here with the
same writeback-availability check avoids these spurious wakeups.
[ ... ]
> @@ -1700,8 +1740,18 @@ bool zswap_store(struct folio *folio)
> bool zswap_is_present(swp_entry_t entry, unsigned int nr)
> {
> pgoff_t offset = swp_offset(entry);
> - struct xarray *tree = swap_zswap_tree(entry);
> + struct xarray *tree;
> unsigned long index = offset;
> + unsigned int i;
> +
> + /* Virtual entries have no native zswap tree. */
> + if (is_vswap_entry(entry)) {
> + for (i = 0; i < nr; i++)
> + if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i)))
> + return true;
> + return false;
> + }
> + tree = swap_zswap_tree(entry);
>
> if (zswap_never_enabled() || xa_empty(tree))
> return false;
[Severity: Medium]
Should the zswap_never_enabled() check stay ahead of is_vswap_entry(entry)?
do_swap_page() calls swap_entry_synchronous() -> zswap_is_present(entry, 1)
on every swap-cache-miss page fault. Placing is_vswap_entry() before
zswap_never_enabled() makes virtual swap faults bypass the static-branch fast
path even when zswap was never enabled, performing an RCU read, a
si->cluster_info_pool XArray lookup, and a ci->lock acquire/release on every
fault.
When nr > 1, the loop also repeats the cluster lookup and ci->lock cycle for
each slot instead of locking the cluster once for the range.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=3
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 04/20] mm/swap: allocate virtual swap slots
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (2 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
@ 2026-12-03 21:59 ` 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
` (16 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
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@tencent.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 04/20] mm/swap: allocate virtual swap slots
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
@ 2026-12-04 22:09 ` sashiko-bot
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: folio_may_vswap() doubles rstat flushes and bypasses
vswap at zswap.max
--
commit f62612a57a2bc7b47001c49359c2a34546d0ba09
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: allocate virtual swap slots
This commit adds a per-CPU virtual swap slot allocator and selects it in
folio_alloc_swap() when vswap and zswap are enabled and the cgroup permits
zswap.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 1387c8bb1e976..aa08111bea62d 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -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;
> +}
[Severity: Medium]
Does calling obj_cgroup_may_zswap() here double global rstat flushes and
bypass vswap for cgroups at a finite memory.zswap.max?
For any ancestor with a finite non-zero memory.zswap.max,
obj_cgroup_may_zswap() runs __mem_cgroup_flush_stats(memcg, true), flushing
rstat across all possible CPUs under the global rstat lock. Since
zswap_store() calls it again at writeout, every swapout flushes twice.
obj_cgroup_may_zswap() also returns false when usage reaches
memory.zswap.max, even though zswap_store() calls shrink_memcg() to make
room and still stores the folio. Capped cgroups then bypass vswap_alloc(),
consume physical swap slots, and hit -ENOSPC when physical swap is full.
Checking only whether memory.zswap.max is zero along the cgroup hierarchy
would avoid both the extra rstat flushes and bypassing vswap when zswap can
shrink.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=4
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (3 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
@ 2026-12-03 21:59 ` Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
` (15 subsequent siblings)
20 siblings, 0 replies; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
The physical allocator reports whether the folio entered swapcache.
A folio that already occupies a virtual entry needs the newly allocated
physical entry, so its swapcache state cannot report the allocation result.
Return the allocated entry from the fast and scanning allocation paths.
Share their existing allocation and discard retry in
folio_alloc_phys_swap(), which the native folio_alloc_swap() path calls
immediately.
Keep cache installation inside __swap_cluster_alloc_entries(). The folio
metadata and cache pointer retain their existing publication and locking.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swapfile.c | 59 ++++++++++++++++++++++++++++++---------------------
1 file changed, 35 insertions(+), 24 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index aa08111bea62..fa315389399b 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1545,12 +1545,14 @@ static bool get_swap_device_info(struct swap_info_struct *si)
* Fast path try to get swap entries with specified order from current
* CPU's swap entry pool (a cluster).
*/
-static bool swap_alloc_fast(struct folio *folio)
+static swp_entry_t swap_alloc_fast(struct folio *folio)
{
unsigned int order = folio_order(folio);
struct swap_cluster_info *ci;
struct swap_info_struct *si;
- unsigned int offset;
+ unsigned long offset, found = 0;
+
+ lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
/*
* Once allocated, swap_info_struct will never be completely freed,
@@ -1559,25 +1561,28 @@ static bool swap_alloc_fast(struct folio *folio)
si = this_cpu_read(percpu_swap_cluster.si[order]);
offset = this_cpu_read(percpu_swap_cluster.offset[order]);
if (!si || !offset || !get_swap_device_info(si))
- return false;
+ return (swp_entry_t){};
ci = swap_cluster_lock(si, offset);
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);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
} else if (ci) {
swap_cluster_unlock(ci);
}
put_swap_device(si);
- return folio_test_swapcache(folio);
+ if (found)
+ return swp_entry(si->type, found);
+ return (swp_entry_t){};
}
/* Rotate the device and switch to a new cluster */
-static void swap_alloc_slow(struct folio *folio)
+static swp_entry_t swap_alloc_slow(struct folio *folio)
{
struct swap_info_struct *si, *next;
+ unsigned long found;
spin_lock(&swap_avail_lock);
start_over:
@@ -1586,12 +1591,12 @@ static void swap_alloc_slow(struct folio *folio)
plist_requeue(&si->avail_list, &swap_avail_head);
spin_unlock(&swap_avail_lock);
if (get_swap_device_info(si)) {
- cluster_alloc_swap_entry(si, folio);
+ found = cluster_alloc_swap_entry(si, folio);
put_swap_device(si);
- if (folio_test_swapcache(folio))
- return;
+ if (found)
+ return swp_entry(si->type, found);
if (folio_test_large(folio))
- return;
+ return (swp_entry_t){};
}
spin_lock(&swap_avail_lock);
@@ -1609,6 +1614,7 @@ static void swap_alloc_slow(struct folio *folio)
goto start_over;
}
spin_unlock(&swap_avail_lock);
+ return (swp_entry_t){};
}
/*
@@ -1959,6 +1965,23 @@ static bool vswap_alloc(struct folio *folio)
return false;
}
+static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
+{
+ swp_entry_t entry;
+
+again:
+ local_lock(&percpu_swap_cluster.lock);
+ entry = swap_alloc_fast(folio);
+ if (!entry.val)
+ entry = swap_alloc_slow(folio);
+ local_unlock(&percpu_swap_cluster.lock);
+
+ if (!entry.val && !folio_order(folio) && swap_sync_discard())
+ goto again;
+
+ return entry;
+}
+
/**
* folio_alloc_swap - allocate swap space for a folio
* @folio: folio we want to move to swap
@@ -2000,20 +2023,8 @@ 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;
- }
+ if (!may_vswap || !vswap_alloc(folio))
+ folio_alloc_phys_swap(folio);
/* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
if (unlikely(mem_cgroup_try_charge_swap(folio))) {
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (4 preceding siblings ...)
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 ` 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
` (14 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
A virtual entry does not identify the device that holds its disk data.
Add physical-entry encoding and lookup to the vtable, and use the lookup
for reads, synchronous I/O, stable writes and filesystem-reclaim checks.
Prepare these readers before writeout can install physical backing.
They must resolve the physical device when the physical backend starts
writing virtual folios to disk. Direct physical entries retain their
existing device selection.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/memory.c | 15 ++--
mm/page_io.c | 21 +++++-
mm/swapfile.c | 2 +-
mm/vmscan.c | 23 +++++--
mm/vswap.h | 184 +++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 228 insertions(+), 17 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 9ba6ebd58132..3eb1841530bb 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4754,8 +4754,11 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
{
if (!folio_test_swapcache(folio))
return false;
- /* A vswap entry holds no physical slot, so keeping it saves no IO. */
- if (is_vswap_entry(folio->swap))
+ /*
+ * Non-swapfile backends cannot be reused for future swapouts.
+ * Free the swap slot unless backed by contiguous physical swap.
+ */
+ if (!folio_phys_swap_backed(folio))
return true;
/*
* Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
@@ -4763,7 +4766,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
* are fast, and meanwhile, swap cache pinning the slot deferring the
* release of metadata or fragmentation is a more critical issue.
*/
- if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
+ if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO))
return true;
if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) ||
folio_test_mlocked(folio))
@@ -5109,7 +5112,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
swap_update_readahead(folio, vma, vmf->address);
if (!folio) {
/* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */
- if (swap_entry_synchronous(si, entry))
+ if (swap_entry_synchronous(si, entry) ||
+ (is_vswap_entry(entry) &&
+ swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO)))
folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
thp_swapin_suitable_orders(si, vmf) | BIT(0),
vmf, NULL, 0);
@@ -5268,7 +5273,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
*/
exclusive = true;
} else if (exclusive && folio_test_writeback(folio) &&
- data_race(si->flags & SWP_STABLE_WRITES)) {
+ swap_entry_backend_has_flag(si, entry, SWP_STABLE_WRITES)) {
/*
* This is tricky: not all swap backends support
* concurrent page modifications while under writeback.
diff --git a/mm/page_io.c b/mm/page_io.c
index 9397ef83a60d..956c3ffc1039 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -493,17 +493,32 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
bool workingset = folio_test_workingset(folio);
+ swp_entry_t phys;
VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
+ /*
+ * Resolve the physical slot to read from. A vswap entry keeps
+ * folio->swap virtual, so map it to its physical backing; a folio with
+ * no backing has nothing to read.
+ */
+ if (swap_is_vswap(sis)) {
+ phys = vswap_to_phys(folio->swap);
+ if (phys.val)
+ synchronous = __swap_entry_to_info(phys)->flags &
+ SWP_SYNCHRONOUS_IO;
+ } else {
+ phys = folio->swap;
+ }
+
/*
* A batch this folio can't join is submitted before the pressure
* window opens, so that each batch is billed for its own read
* rather than for the next folio's lookup and decompression.
*/
- if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ))
+ if (ctx->sio && (!phys.val || !swap_can_merge(ctx, folio, phys, READ)))
swap_read_submit(ctx);
/*
@@ -532,14 +547,14 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
return;
- if (unlikely(swap_is_vswap(sis))) {
+ if (!phys.val) {
folio_unlock(folio);
return;
}
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
- swap_add_folio(ctx, folio, folio->swap, READ);
+ swap_add_folio(ctx, folio, phys, READ);
/*
* Synchronous devices do the whole read at submission time, submit
diff --git a/mm/swapfile.c b/mm/swapfile.c
index fa315389399b..d9e8c9365c44 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -247,7 +247,7 @@ 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) &&
- !is_vswap_entry(folio->swap)));
+ folio_phys_swap_backed(folio)));
if (!need_reclaim || !folio_swapcache_freeable(folio))
goto out_unlock;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 3ad66f2a16d8..5bf20b57fe45 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -70,6 +70,7 @@
#include "memory-failure.h"
#include "page_alloc.h"
#include "swap.h"
+#include "vswap.h"
#define CREATE_TRACE_POINTS
#include "trace_vmscan.h"
@@ -1029,17 +1030,26 @@ static unsigned int demote_folio_list(struct list_head *demote_folios,
static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
{
+ swp_entry_t entry;
+
if (gfp_mask & __GFP_FS)
return true;
/*
* We can "enter_fs" for swap-cache with only __GFP_IO unless backed by
* a swapfile that requires GFP_NOFS I/O.
*/
- if (folio_test_swapcache(folio) && (gfp_mask & __GFP_IO) &&
- !(__swap_entry_to_info(folio->swap)->ops->flags &
- SWAP_OPS_F_REQUIRE_NOFS))
- return true;
- return false;
+ if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
+ return false;
+
+ entry = folio->swap;
+ if (is_vswap_entry(entry)) {
+ entry = vswap_to_phys(entry);
+ if (!entry.val)
+ return true;
+ }
+
+ return !(__swap_entry_to_info(entry)->ops->flags &
+ SWAP_OPS_F_REQUIRE_NOFS);
}
/*
@@ -1583,7 +1593,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
activate_locked:
/* Not a candidate for swapping, so reclaim swap space. */
if (folio_test_swapcache(folio) &&
- (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio)))
+ ((mem_cgroup_swap_full(folio) && folio_phys_swap_backed(folio)) ||
+ folio_test_mlocked(folio)))
folio_free_swap(folio);
VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
if (!folio_test_mlocked(folio)) {
diff --git a/mm/vswap.h b/mm/vswap.h
index f64b1543e01f..c4f0670c9b5c 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -19,6 +19,7 @@ struct zswap_entry;
enum vswap_backing_type {
VSWAP_NONE = 0,
VSWAP_ZSWAP = 1,
+ VSWAP_SWAPFILE = 2,
VSWAP_ZERO,
VSWAP_FOLIO,
};
@@ -41,8 +42,13 @@ static inline bool is_vswap_entry(swp_entry_t entry)
/*
* Virtual table entry encoding for vswap clusters.
*
- * Zero represents no separate backing. An aligned zswap pointer
- * has bit 0 clear and is stored directly in the vtable.
+ * The architecture-independent swp_entry_t leaves its top bit unused.
+ * Shift the entry left by one to leave bit 0 free as the physical tag.
+ * Aligned zswap_entry pointers already have bit 0 clear.
+ *
+ * NONE: |------ 0 -------|0|
+ * ZSWAP: |-- zswap_entry pointer --|
+ * SWAPFILE: |-- swp_entry_t -|1|
*
* vtable[i] = NONE does not by itself mean "free". The swap_table entry
* and the per-slot zero flag carry the rest of the state. The full
@@ -56,6 +62,8 @@ static inline bool is_vswap_entry(swp_entry_t entry)
* NONE | * | set | zero-backed; cached if PFN set
* ZSWAP | PFN | clear | folio cached + zswap entry
* ZSWAP | shadow / NULL | clear | evicted, only in zswap
+ * SWAPFILE | PFN | clear | folio cached + physical slot
+ * SWAPFILE | shadow / NULL | clear | evicted, only on the swapfile
*
* Locking: the caller must own and lock the slot's swapcache folio to
* stabilize its backing. The cluster lock only makes individual vtable
@@ -75,9 +83,17 @@ static inline bool is_vswap_entry(swp_entry_t entry)
static inline enum vswap_backing_type vtable_type(unsigned long vt)
{
+ if (vt & VTABLE_TAG_MASK)
+ return VSWAP_SWAPFILE;
return vt ? VSWAP_ZSWAP : VSWAP_NONE;
}
+static inline swp_entry_t vtable_to_phys(unsigned long vt)
+{
+ VM_WARN_ON(vtable_type(vt) != VSWAP_SWAPFILE);
+ return (swp_entry_t){ .val = vt >> VTABLE_TAG_BITS };
+}
+
static inline struct zswap_entry *vtable_to_zswap(unsigned long vt)
{
VM_WARN_ON(vtable_type(vt) != VSWAP_ZSWAP);
@@ -119,6 +135,33 @@ vswap_lock_cluster(swp_entry_t entry, unsigned int *voff)
return container_of(ci, struct swap_cluster_info_dynamic, ci);
}
+/**
+ * vswap_to_phys - resolve a vswap entry's physical swap backing
+ * @entry: the virtual swap entry
+ *
+ * Context: takes and drops the vswap cluster lock internally.
+ * Return: the backing physical swp_entry_t, or the null entry (.val == 0)
+ * when @entry has no physical backing (NONE/ZSWAP/ZERO).
+ */
+static inline swp_entry_t vswap_to_phys(swp_entry_t entry)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int voff;
+ unsigned long vt;
+
+ ci_dyn = vswap_lock_cluster(entry, &voff);
+ if (!ci_dyn)
+ return (swp_entry_t){};
+
+ vt = __vtable_get(ci_dyn, voff);
+ swap_cluster_unlock(&ci_dyn->ci);
+
+ if (vtable_type(vt) != VSWAP_SWAPFILE)
+ return (swp_entry_t){};
+
+ return vtable_to_phys(vt);
+}
+
void __vswap_release_backing(struct swap_cluster_info *ci,
unsigned int ci_start, unsigned int nr);
@@ -170,6 +213,102 @@ static inline struct zswap_entry *vswap_zswap_load(swp_entry_t entry)
void folio_release_vswap_backing(struct folio *folio);
+/*
+ * Walk nr vtable slots starting at voff in ci_dyn. Returns the prefix
+ * length of slots sharing one effective backing type. For SWAPFILE,
+ * the prefix is also restricted to contiguous offsets in the same
+ * swapfile.
+ *
+ * Effective type per slot:
+ * vtable=NONE + zero flag set -> VSWAP_ZERO
+ * vtable=NONE + swap_table PFN tag -> VSWAP_FOLIO
+ * vtable=NONE + neither -> VSWAP_NONE
+ * vtable=SWAPFILE -> VSWAP_SWAPFILE
+ * vtable=ZSWAP -> VSWAP_ZSWAP
+ *
+ * *typep returns the effective type of slot 0. Caller holds
+ * ci_dyn->ci.lock.
+ */
+static inline int __vswap_check_backing(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int voff, int nr,
+ enum vswap_backing_type *typep)
+{
+ enum vswap_backing_type first_type = VSWAP_NONE;
+ enum vswap_backing_type slot_type;
+ swp_entry_t first_phys = {};
+ unsigned long vt, swap_tb;
+ int i;
+
+ lockdep_assert_held(&ci_dyn->ci.lock);
+
+ for (i = 0; i < nr; i++) {
+ vt = __vtable_get(ci_dyn, voff + i);
+ if (vtable_type(vt) == VSWAP_NONE) {
+ swap_tb = __swap_table_get(&ci_dyn->ci, voff + i);
+ if (__swap_table_test_zero(&ci_dyn->ci, voff + i))
+ slot_type = VSWAP_ZERO;
+ else if (swp_tb_is_folio(swap_tb))
+ slot_type = VSWAP_FOLIO;
+ else
+ slot_type = VSWAP_NONE;
+ } else {
+ slot_type = vtable_type(vt);
+ }
+
+ if (!i) {
+ first_type = slot_type;
+ if (first_type == VSWAP_SWAPFILE)
+ first_phys = vtable_to_phys(vt);
+ } else if (slot_type != first_type) {
+ break;
+ } else if (first_type == VSWAP_SWAPFILE &&
+ vtable_to_phys(vt).val != first_phys.val + i) {
+ break;
+ }
+ }
+
+ if (typep)
+ *typep = first_type;
+ return i;
+}
+
+static inline int vswap_check_backing(swp_entry_t entry, int nr,
+ enum vswap_backing_type *typep)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int voff;
+ int ret;
+
+ ci_dyn = vswap_lock_cluster(entry, &voff);
+ if (!ci_dyn) {
+ if (typep)
+ *typep = VSWAP_NONE;
+ return 0;
+ }
+ ret = __vswap_check_backing(ci_dyn, voff, nr, typep);
+ swap_cluster_unlock(&ci_dyn->ci);
+ return ret;
+}
+
+/**
+ * folio_phys_swap_backed - test whether a folio is backed by a contiguous
+ * range of physical swap slots.
+ * @folio: a swap-cache resident folio
+ *
+ * Return: %true if @folio->swap is not a vswap entry, or if these vswap
+ * entries are backed by a contiguous range of physical slots.
+ */
+static inline bool folio_phys_swap_backed(struct folio *folio)
+{
+ swp_entry_t entry = folio->swap;
+ int nr = folio_nr_pages(folio);
+ enum vswap_backing_type type;
+
+ return !is_vswap_entry(entry) ||
+ (vswap_check_backing(entry, nr, &type) == nr &&
+ type == VSWAP_SWAPFILE);
+}
+
static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
gfp_t gfp)
{
@@ -199,6 +338,47 @@ static inline bool is_vswap_entry(swp_entry_t entry)
return false;
}
+static inline swp_entry_t vswap_to_phys(swp_entry_t entry)
+{
+ return (swp_entry_t){};
+}
+
+static inline bool folio_phys_swap_backed(struct folio *folio)
+{
+ return true;
+}
+
#endif /* CONFIG_SWAP */
+/*
+ * Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...)
+ * for @entry. For a vswap entry the property belongs to the current
+ * physical backing rather than vswap_si itself; resolve to the backing
+ * and test there. Returns false for zswap/zero/unbacked vswap entries
+ * as they don't have a backing bdev.
+ */
+static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
+ swp_entry_t entry,
+ unsigned long flag)
+{
+ struct swap_info_struct *phys_si;
+ swp_entry_t phys;
+ bool has_flag;
+
+ if (!swap_is_vswap(si))
+ return data_race(si->flags & flag);
+
+ phys = vswap_to_phys(entry);
+ if (!phys.val)
+ return false;
+
+ phys_si = get_swap_device(phys);
+ if (IS_ERR_OR_NULL(phys_si))
+ return false;
+
+ has_flag = data_race(phys_si->flags & flag);
+ put_swap_device(phys_si);
+ return has_flag;
+}
+
#endif /* _MM_VSWAP_H */
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm/swap: redundant vswap cluster locks and XArray walks on
swap-in hot paths
- [Low] mm: unlocked gap in swap_entry_backend_has_flag() races with
swapoff+swapon
--
commit 3f81dcd7e1282bf4edcad334ba3c53805854c831
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: resolve physical backing for reads and device flags
This commit adds physical swap-entry encoding and lookup helpers to the
vswap vtable and updates swap-in reads, synchronous I/O, stable-write,
and filesystem-reclaim checks to resolve physical backing for virtual
swap entries.
> diff --git a/mm/memory.c b/mm/memory.c
> index 9ba6ebd581324..3eb1841530bb3 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4754,8 +4754,11 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
> {
> if (!folio_test_swapcache(folio))
> return false;
> - /* A vswap entry holds no physical slot, so keeping it saves no IO. */
> - if (is_vswap_entry(folio->swap))
> + /*
> + * Non-swapfile backends cannot be reused for future swapouts.
> + * Free the swap slot unless backed by contiguous physical swap.
> + */
> + if (!folio_phys_swap_backed(folio))
> return true;
> /*
> * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
> @@ -4763,7 +4766,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
> * are fast, and meanwhile, swap cache pinning the slot deferring the
> * release of metadata or fragmentation is a more critical issue.
> */
> - if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
> + if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO))
> return true;
[Severity: Medium]
Can these swap-in hot paths avoid repeating the cluster_info_pool XArray
walk and ci->lock acquisition on the same vswap slot?
In should_try_to_free_swap(), which runs on every do_swap_page() fault
under vmf->ptl and folio_lock, folio_phys_swap_backed() locks the cluster
and discards the resolved physical entry, and the next line calls
swap_entry_backend_has_flag() to walk and lock the same cluster again
along with get_swap_device()/put_swap_device().
The same duplicate lookup happens in do_swap_page() on non-zswap major
faults, where swap_entry_synchronous() and swap_entry_backend_has_flag()
walk and lock the same cluster back-to-back, and in swap_read_folio(),
where vswap_to_phys() reads the vtable before swap_read_folio_zeromap()
and zswap_load() look up the same slot again.
Returning the resolved physical entry from folio_phys_swap_backed() and
dispatching once on the decoded vtable entry in swap_entry_synchronous()
and swap_read_folio() would avoid the extra spinlock contention and
vmf->ptl hold time.
[ ... ]
> diff --git a/mm/vswap.h b/mm/vswap.h
> index f64b1543e01fc..c4f0670c9b5c0 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h
[ ... ]
> +static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
> + swp_entry_t entry,
> + unsigned long flag)
> +{
> + struct swap_info_struct *phys_si;
> + swp_entry_t phys;
> + bool has_flag;
> +
> + if (!swap_is_vswap(si))
> + return data_race(si->flags & flag);
> +
> + phys = vswap_to_phys(entry);
> + if (!phys.val)
> + return false;
> +
> + phys_si = get_swap_device(phys);
> + if (IS_ERR_OR_NULL(phys_si))
> + return false;
> +
> + has_flag = data_race(phys_si->flags & flag);
> + put_swap_device(phys_si);
> + return has_flag;
> +}
[Severity: Low]
Can the unlocked gap between vswap_to_phys() and get_swap_device() race
with swapoff and swapon and trigger spurious "Bad swap offset entry"
errors?
In do_swap_page(), swap_entry_backend_has_flag() is called without
holding folio_lock, vmf->ptl, rcu_read_lock(), or a reference on the
physical swap device:
CPU0 CPU1
do_swap_page()
swap_entry_backend_has_flag()
vswap_to_phys()
vswap_lock_cluster()
vt = __vtable_get()
swap_cluster_unlock()
swapoff()
try_to_unuse()
__vswap_release_backing()
synchronize_rcu()
swapon() /* smaller, same slot */
get_swap_device(phys)
offset >= si->max <-- logs "Bad swap offset entry"
Reading data_race(__swap_entry_to_info(phys)->flags & flag) while still
holding ci_dyn->ci.lock avoids both this race and the
get_swap_device()/put_swap_device() round-trip, since try_to_unuse() must
take ci_dyn->ci.lock to release the backing and swap_info[] slots are
never freed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=6
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (5 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
@ 2026-12-03 21:59 ` 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
` (13 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Physical swapoff matches the swap type in PTEs and shmem entries. That
match misses virtual entries, whose type stays unchanged when their
physical backing changes.
Match the physical device through the vtable and resolve physical
reverse maps to their virtual entry during the cache walk. Recheck
backing under the folio lock before installing data or freeing slots.
Use order-0 allocations for fresh virtual reads during unuse.
Read shmem entries and their order together under the XArray lock so
collapse cannot give the scan mismatched values. Prepare these consumers
before the physical backend publishes reverse maps, ensuring swapoff
can find the entries backed by the device being removed.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/shmem.c | 67 ++++++++++++++++++++++++++++++++++++--------
mm/swap.h | 6 ++++
mm/swap_table.h | 39 +++++++++++++++++++++++---
mm/swapfile.c | 74 +++++++++++++++++++++++++++++++++++++++++++------
4 files changed, 162 insertions(+), 24 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 344f7edf5e15..5d6807518474 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -87,6 +87,7 @@ static struct vfsmount *shm_mnt __ro_after_init;
#include <linux/uaccess.h>
#include "internal.h"
+#include "vswap.h"
#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
@@ -161,7 +162,8 @@ static unsigned long shmem_default_max_inodes(void)
static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
struct folio **foliop, enum sgp_type sgp, gfp_t gfp,
- struct vm_fault *vmf, vm_fault_t *fault_type);
+ struct vm_fault *vmf, vm_fault_t *fault_type,
+ int swapoff_type);
static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
{
@@ -1347,7 +1349,8 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index)
* Truncation can look up a large swap entry at or beyond i_size.
* Swap it in directly to bypass the size check in shmem_get_folio().
*/
- if (shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp, NULL, NULL) == -EEXIST)
+ if (shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp,
+ NULL, NULL, -1) == -EEXIST)
goto retry;
return folio;
}
@@ -1710,6 +1713,24 @@ static void shmem_evict_inode(struct inode *inode)
#endif
}
+static bool shmem_find_swapoff_entry(swp_entry_t entry, pgoff_t *index,
+ unsigned int order, unsigned int type)
+{
+ unsigned long nr = 1UL << order;
+ unsigned long off = *index & (nr - 1);
+
+ if (!softleaf_is_swap(entry))
+ return false;
+ entry.val += off;
+ for (; off < nr; off++, entry.val++) {
+ if (swap_entry_is_backed_by(entry, type)) {
+ *index = round_down(*index, nr) + off;
+ return true;
+ }
+ }
+ return false;
+}
+
static unsigned int shmem_find_swap_entries(struct address_space *mapping,
pgoff_t start, struct folio_batch *fbatch,
pgoff_t *indices, unsigned int type)
@@ -1717,6 +1738,8 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping,
XA_STATE(xas, &mapping->i_pages, start);
struct folio *folio;
swp_entry_t entry;
+ pgoff_t index;
+ unsigned int order;
rcu_read_lock();
xas_for_each(&xas, folio, ULONG_MAX) {
@@ -1726,15 +1749,27 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping,
if (!xa_is_value(folio))
continue;
+ /* A concurrent collapse can change both the value and its order. */
+ xas_lock_irq(&xas);
+ xas_set(&xas, xas.xa_index);
+ folio = xas_load(&xas);
+ if (!xa_is_value(folio)) {
+ xas_unlock_irq(&xas);
+ continue;
+ }
entry = radix_to_swp_entry(folio);
+ index = xas.xa_index;
+ order = xas_get_order(&xas);
+ xas_unlock_irq(&xas);
/*
* swapin error entries can be found in the mapping. But they're
* deliberately ignored here as we've done everything we can do.
*/
- if (swp_type(entry) != type)
+ if (!shmem_find_swapoff_entry(entry, &index,
+ order, type))
continue;
- indices[folio_batch_count(fbatch)] = xas.xa_index;
+ indices[folio_batch_count(fbatch)] = index;
if (!folio_batch_add(fbatch, folio))
break;
@@ -1753,7 +1788,7 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping,
* of pages swapped in, or the error in case of failure.
*/
static int shmem_unuse_swap_entries(struct inode *inode,
- struct folio_batch *fbatch, pgoff_t *indices)
+ struct folio_batch *fbatch, pgoff_t *indices, unsigned int type)
{
int i = 0;
int ret = 0;
@@ -1764,7 +1799,7 @@ static int shmem_unuse_swap_entries(struct inode *inode,
struct folio *folio = fbatch->folios[i];
error = shmem_swapin_folio(inode, indices[i], &folio, SGP_CACHE,
- mapping_gfp_mask(mapping), NULL, NULL);
+ mapping_gfp_mask(mapping), NULL, NULL, type);
if (error == 0) {
folio_unlock(folio);
folio_put(folio);
@@ -1796,7 +1831,7 @@ static int shmem_unuse_inode(struct inode *inode, unsigned int type)
break;
}
- ret = shmem_unuse_swap_entries(inode, &fbatch, indices);
+ ret = shmem_unuse_swap_entries(inode, &fbatch, indices, type);
if (ret < 0)
break;
@@ -2527,18 +2562,19 @@ static int shmem_split_large_entry(struct inode *inode, pgoff_t index,
static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
struct folio **foliop, enum sgp_type sgp,
gfp_t gfp, struct vm_fault *vmf,
- vm_fault_t *fault_type)
+ vm_fault_t *fault_type, int swapoff_type)
{
struct address_space *mapping = inode->i_mapping;
struct vm_area_struct *vma = vmf ? vmf->vma : NULL;
struct mm_struct *fault_mm = vmf ? vmf->vma->vm_mm : NULL;
struct shmem_inode_info *info = SHMEM_I(inode);
- swp_entry_t swap;
+ swp_entry_t swap, target_swap;
softleaf_t index_entry;
struct swap_info_struct *si;
struct folio *folio = NULL;
int error, nr_pages, order;
pgoff_t offset;
+ bool vswap_unuse;
VM_BUG_ON(!*foliop || !xa_is_value(*foliop));
index_entry = radix_to_swp_entry(*foliop);
@@ -2566,14 +2602,16 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
offset = index - round_down(index, 1 << order);
swap = swp_entry(swp_type(swap), swp_offset(swap) + offset);
}
+ target_swap = swap;
+ vswap_unuse = swapoff_type >= 0 && swap_is_vswap(si);
/* Look it up and read it in.. */
folio = swap_cache_get_folio(swap);
if (!folio) {
- if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
+ if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
/* Direct swapin skipping swap cache & readahead */
folio = shmem_swap_alloc_folio(inode, vmf, index,
- swap, order, gfp);
+ swap, vswap_unuse ? 0 : order, gfp);
} else {
/* Cached swapin only supports order 0 folio */
folio = shmem_swapin_cluster(swap, gfp, info, index);
@@ -2648,6 +2686,11 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
goto failed;
}
folio_wait_writeback(folio);
+ if (swapoff_type >= 0 &&
+ !swap_entry_is_backed_by(target_swap, swapoff_type)) {
+ error = -EEXIST;
+ goto unlock;
+ }
/*
* Some architectures may have to restore extra metadata to the
@@ -2748,7 +2791,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
if (xa_is_value(folio)) {
error = shmem_swapin_folio(inode, index, &folio,
- sgp, gfp, vmf, fault_type);
+ sgp, gfp, vmf, fault_type, -1);
if (error == -EEXIST)
goto repeat;
diff --git a/mm/swap.h b/mm/swap.h
index 7b9a65232d19..018250b814bb 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -309,6 +309,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp);
int folio_alloc_swap(struct folio *folio);
int folio_dup_swap(struct folio *folio, struct page *page);
void folio_put_swap(struct folio *folio, struct page *page);
+bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type);
/* For internal use */
extern void __swap_cluster_free_entries(struct swap_info_struct *si,
@@ -460,6 +461,11 @@ static inline int folio_alloc_swap(struct folio *folio)
return -EINVAL;
}
+static inline bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type)
+{
+ return false;
+}
+
static inline int folio_dup_swap(struct folio *folio, struct page *page)
{
return -EINVAL;
diff --git a/mm/swap_table.h b/mm/swap_table.h
index 9c9e3cd3b37e..36b17f03bd9b 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -4,6 +4,7 @@
#include <linux/rcupdate.h>
#include <linux/atomic.h>
+#include <linux/swapops.h>
#include "swap.h"
extern struct swap_info_struct *vswap_si;
@@ -30,7 +31,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)
+ * Vswap: |-------- vswap offset --------|100| - vswap rmap
* Bad: |------------- 1 -------------|1000| - Bad slot
* HIB: | 0 |------- 1 -------|10000| - Hibernation slot
*
@@ -52,9 +53,8 @@ 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.
+ * - Vswap: Reverse map from a physical slot to the vswap entry that owns
+ * it. See the layout below.
*
* - Bad: Swap slot is reserved, protects swap header or holes on swap devices.
*
@@ -407,4 +407,35 @@ static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
}
#endif
+/*
+ * Vswap-tagged swap table entry: rmap for vswap-backing physical slots.
+ *
+ * On physical clusters, a Vswap-tagged entry stores the offset of the
+ * vswap entry that owns this physical slot (the reverse map). Only the
+ * offset is stored; the swap type is implicit (always vswap_si->type,
+ * since there is exactly one vswap device).
+ *
+ * Vswap: |---- vswap offset ----|100|
+ */
+#define SWP_TB_VSWAP_MARK_BITS 3
+#define SWP_TB_VSWAP_MARK 0b100UL
+#define SWP_TB_VSWAP_MARK_MASK ((1UL << SWP_TB_VSWAP_MARK_BITS) - 1)
+#define SWP_RMAP_ENTRY_MASK (~SWP_TB_VSWAP_MARK_MASK)
+
+static inline bool swp_tb_is_vswap(unsigned long swp_tb)
+{
+ return (swp_tb & SWP_TB_VSWAP_MARK_MASK) == SWP_TB_VSWAP_MARK;
+}
+
+#ifdef CONFIG_SWAP
+static inline swp_entry_t swp_tb_vswap_to_swp_entry(unsigned long swp_tb)
+{
+ unsigned long offset;
+
+ VM_WARN_ON(!swp_tb_is_vswap(swp_tb));
+ offset = (swp_tb & SWP_RMAP_ENTRY_MASK) >> SWP_TB_VSWAP_MARK_BITS;
+ return swp_entry(vswap_si->type, offset);
+}
+#endif
+
#endif
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d9e8c9365c44..6933e069bd2e 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2807,13 +2807,36 @@ static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
return pte_same(pte_swp_clear_flags(pte), swp_pte);
}
+bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type)
+{
+ struct swap_info_struct *si;
+ swp_entry_t phys;
+ bool match = false;
+
+ if (!softleaf_is_swap(entry))
+ return false;
+ /* Direct entries retain their physical slots while zswapped. */
+ if (swp_type(entry) == type)
+ return true;
+ si = get_swap_device(entry);
+ if (IS_ERR_OR_NULL(si))
+ return false;
+ if (swap_is_vswap(si)) {
+ phys = vswap_to_phys(entry);
+ match = phys.val && swp_type(phys) == type;
+ }
+ put_swap_device(si);
+ return match;
+}
+
/*
* No need to decide whether this PTE shares the swap entry with others,
* just let do_wp_page work it out if a write is requested later - to
* force COW, vm_page_prot omits write permission from any private vma.
*/
static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
- unsigned long addr, swp_entry_t entry, struct folio *folio)
+ unsigned long addr, swp_entry_t entry, struct folio *folio,
+ unsigned int type)
{
struct page *page;
struct folio *swapcache;
@@ -2826,14 +2849,16 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
* If the folio is removed from swap cache by others, continue to
* unuse other PTEs. try_to_unuse may try again if we missed this one.
*/
- if (!folio_matches_swap_entry(folio, entry))
+ if (!folio_matches_swap_entry(folio, entry) ||
+ !swap_entry_is_backed_by(entry, type))
return 0;
/*
* A large folio that a fault didn't map can't be mapped one PTE at a
* time here, so drop it and swap in small folios instead.
*/
- if (unlikely(!swap_cache_folio_mappable(folio, 1))) {
+ if (unlikely(!is_vswap_entry(entry) &&
+ !swap_cache_folio_mappable(folio, 1))) {
swap_cache_del_folio(folio);
return 0;
}
@@ -2875,6 +2900,18 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
goto setpte;
}
+ if (is_vswap_entry(entry) && !folio_test_anon(folio) &&
+ folio_test_large(folio)) {
+ if (folio_test_dirty(folio)) {
+ ret = -EBUSY;
+ goto out;
+ }
+ /* A fresh large folio has no partial anonymous rmap yet. */
+ swap_cache_del_folio(folio);
+ ret = 0;
+ goto out;
+ }
+
/*
* Some architectures may have to restore extra metadata to the page
* when reading from swap. This metadata may be indexed by swap entry
@@ -2959,7 +2996,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
if (!softleaf_is_swap(entry))
continue;
- if (swp_type(entry) != type)
+ if (!swap_entry_is_backed_by(entry, type))
continue;
pte_unmap(pte);
@@ -2974,8 +3011,15 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
.pmd = pmd,
};
- folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
- &vmf);
+ if (is_vswap_entry(entry)) {
+ folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
+ BIT(0), &vmf, NULL, 0);
+ if (IS_ERR(folio))
+ folio = NULL;
+ } else {
+ folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
+ &vmf);
+ }
}
if (!folio) {
swp_tb = swap_table_lookup(entry);
@@ -2986,7 +3030,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
folio_lock(folio);
folio_wait_writeback(folio);
- ret = unuse_pte(vma, pmd, addr, entry, folio);
+ ret = unuse_pte(vma, pmd, addr, entry, folio, type);
if (ret < 0) {
folio_unlock(folio);
folio_put(folio);
@@ -3167,7 +3211,8 @@ static int try_to_unuse(unsigned int type)
int retval = 0;
struct swap_info_struct *si = swap_info[type];
struct folio *folio;
- swp_entry_t entry;
+ swp_entry_t entry, phys;
+ unsigned long swp_tb;
unsigned int i;
if (!swap_usage_in_pages(si))
@@ -3216,6 +3261,10 @@ static int try_to_unuse(unsigned int type)
(i = find_next_to_unuse(si, i)) != 0) {
entry = swp_entry(type, i);
+
+ swp_tb = swap_table_lookup(entry);
+ if (swp_tb_is_vswap(swp_tb))
+ entry = swp_tb_vswap_to_swp_entry(swp_tb);
folio = swap_cache_get_folio(entry);
if (!folio)
continue;
@@ -3228,7 +3277,16 @@ static int try_to_unuse(unsigned int type)
*/
folio_lock(folio);
folio_wait_writeback(folio);
+ if (is_vswap_entry(entry)) {
+ if (!folio_matches_swap_entry(folio, entry))
+ goto unlock_cache;
+ phys = vswap_to_phys(entry);
+ if (!phys.val || swp_type(phys) != type ||
+ swp_offset(phys) != i)
+ goto unlock_cache;
+ }
folio_free_swap(folio);
+unlock_cache:
folio_unlock(folio);
folio_put(folio);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm/swap: spurious swapoff -ENOMEM when unuse_pte_range()
discards -ENOENT
- [Medium] mm/swap: swapoff loses readahead and I/O batching for vswap
entries
- [Medium] mm: shmem_find_swap_entries() takes xa_lock_irq and restarts
walk per entry
- [Medium] mm/shmem: shmem_find_swapoff_entry() relocks vswap cluster
per sub-slot
--
commit f05da385c356349b81af60a33b89b354ca7cc636
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: prepare physical swapoff for virtual backing
This commit updates physical swapoff in shmem and swapfile to match
virtual swap entries backed by the device being removed, resolve physical
reverse maps during the swap cache walk, and use order-0 allocations when
reading vswap entries during unuse.
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 344f7edf5e15a..5d68075184746 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
[ ... ]
> @@ -1710,6 +1713,24 @@ static void shmem_evict_inode(struct inode *inode)
> #endif
> }
>
> +static bool shmem_find_swapoff_entry(swp_entry_t entry, pgoff_t *index,
> + unsigned int order, unsigned int type)
> +{
> + unsigned long nr = 1UL << order;
> + unsigned long off = *index & (nr - 1);
> +
> + if (!softleaf_is_swap(entry))
> + return false;
> + entry.val += off;
> + for (; off < nr; off++, entry.val++) {
> + if (swap_entry_is_backed_by(entry, type)) {
> + *index = round_down(*index, nr) + off;
> + return true;
> + }
> + }
> + return false;
> +}
[Severity: Medium]
Can this loop avoid re-looking up and relocking the same vswap cluster
for every sub-slot of a large shmem swap entry?
For a large entry (up to 512 sub-slots for a 2 MiB THP on 4 KiB pages)
backed by zswap or another swap device, swap_entry_is_backed_by() ->
vswap_to_phys() walks vswap_si->cluster_info_pool and acquires/releases
ci->lock on each iteration, even though all sub-slots of a large entry
reside in a single vswap cluster. Because shmem_find_swap_entries()
calls this under rcu_read_lock() and continues past cond_resched_rcu() on
non-matching entries, scanning large non-matching shmem files also
extends the RCU read-side critical section across those lookups and lock
round-trips.
Locking the vswap cluster once and scanning the contiguous range in its
virtual_table would eliminate the redundant XArray walks and lock cycles.
[ ... ]
> @@ -1726,15 +1749,27 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping,
> if (!xa_is_value(folio))
> continue;
>
> + /* A concurrent collapse can change both the value and its order. */
> + xas_lock_irq(&xas);
> + xas_set(&xas, xas.xa_index);
> + folio = xas_load(&xas);
> + if (!xa_is_value(folio)) {
> + xas_unlock_irq(&xas);
> + continue;
> + }
> entry = radix_to_swp_entry(folio);
> + index = xas.xa_index;
> + order = xas_get_order(&xas);
> + xas_unlock_irq(&xas);
[Severity: Medium]
Does taking xas_lock_irq(&xas) and restarting the XArray walk on every
swap entry regress swapoff scanning on large swapped shmem files?
shmem_unuse() scans every inode on shmem_swaplist regardless of which
swap device or zswap backs it. xas_set() resets xas->xa_node to
XAS_RESTART, so xas_load() re-descends from the root on every entry while
repeatedly disabling IRQs and taking mapping->i_pages.xa_lock in
contention with active shmem faults and reads.
When CONFIG_THP_SWAP is disabled, shmem swap entries are always order 0
and need no lock; with CONFIG_THP_SWAP, batching entries under a single
xas_lock_irq() acquisition with xas_pause() before unlocking would
preserve sequential leaf scanning and reduce lock contention.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d9e8c9365c447..6933e069bd2ea 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -2974,8 +3011,15 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
> .pmd = pmd,
> };
>
> - folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
> - &vmf);
> + if (is_vswap_entry(entry)) {
> + folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
> + BIT(0), &vmf, NULL, 0);
> + if (IS_ERR(folio))
> + folio = NULL;
> + } else {
> + folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
> + &vmf);
> + }
[Severity: Medium]
Will switching vswap entries to swapin_sync() with BIT(0) here and in
shmem_swapin_folio() drop swapoff readahead and I/O batching?
Previously, unuse_pte_range() called swapin_readahead() and
shmem_swapin_folio() called shmem_swapin_cluster(), both of which plug
the block queue, merge contiguous reads, and pipeline I/O across a
readahead window. swapin_sync() submits a single un-plugged 4 KiB read
and returns directly to folio_lock(folio), serializing physical swapoff
to queue-depth-1 4 KiB synchronous reads.
In shmem_swapin_folio(), forcing order 0 also triggers
shmem_split_large_entry() on every large swap entry, and because
shmem_unuse_inode() advances start to the last batched index, split
siblings of earlier entries in a batch are deferred to subsequent
try_to_unuse() retry passes.
> }
> if (!folio) {
> swp_tb = swap_table_lookup(entry);
[Severity: Medium]
Could discarding ERR_PTR(-ENOENT) from swapin_sync() cause swapoff(2) to
fail spuriously with -ENOMEM?
Unlike a physical swap device being removed, which has SWP_WRITEOK
cleared before try_to_unuse(), vswap_si stays writable throughout
physical swapoff so a freed vswap slot can be reallocated immediately:
CPU0 (swapoff) CPU1
unuse_pte_range()
pte_unmap(pte)
do_swap_page() / zap_pte_range()
frees vswap slot (count -> 0)
swapin_sync(entry, ...)
__swap_cache_add_check()
returns ERR_PTR(-ENOENT)
folio = NULL
swapout on live vswap_si
reallocates same vswap slot
(count >= 1)
swap_table_lookup(entry)
swp_tb_get_count(swp_tb) > 0
return -ENOMEM <-- aborts swapoff(2)
The spurious -ENOMEM propagates out of try_to_unuse(), causing swapoff(2)
to fail and reinsert the swap device even though no allocation failed.
Checking PTR_ERR(folio) == -ENOENT and continuing directly avoids the
re-lookup race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=7
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (6 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
@ 2026-12-03 21:59 ` 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
` (12 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Virtual entries need physical backing when zswap rejects a folio.
Allocate physical entries while keeping the virtual entry in the PTE,
and submit I/O with the physical entry.
Record physical slots' reverse maps to their virtual entries. Preserve
existing backing on later writes and release it through the shared
physical allocator.
Register the virtual device here, after allocation, reads and physical
swapoff are ready. Add the vswap boot parameter and VSWAP_DEFAULT_ON
setting. The default is off; either setting can select enablement at boot.
Allow anonymous reclaim and cgroup admission without a physical device
when compressed storage is available. Bound the virtual reservation for
32-bit and 64-bit entry formats.
Suggested-by: Kairui Song <kasong@tencent.com>
Link: https://lore.kernel.org/all/sashiko-outbox-181078@kernel.org/
Link: https://lore.kernel.org/all/sashiko-outbox-181079@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 7 +
include/linux/memcontrol.h | 6 +
include/linux/swap.h | 13 +
mm/Kconfig | 20 ++
mm/memcontrol.c | 58 ++--
mm/page_io.c | 21 +-
mm/shmem.c | 3 +-
mm/swap_state.c | 6 +-
mm/swap_table.h | 7 +
mm/swapfile.c | 292 +++++++++++++++++-
mm/util.c | 13 +-
mm/vmscan.c | 14 +-
mm/vswap.h | 13 +
mm/workingset.c | 2 +-
mm/zswap.c | 2 +-
15 files changed, 432 insertions(+), 45 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index dce9b8f3ae98..68b2d751439b 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -8421,6 +8421,13 @@ Kernel parameters
force - force vulnerability detection even on
unaffected processors
+ vswap= [MM,EARLY]
+ Route swapouts through the virtual swap layer, which
+ allows zswap and zero-filled pages to be used without
+ a physical swap device.
+ Format: { on | off }
+ Default: on if CONFIG_VSWAP_DEFAULT_ON=y, else off.
+
vsyscall= [X86-64,EARLY]
Controls the behavior of vsyscalls (i.e. calls to
fixed addresses of 0xffffffffff600x00 from legacy
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 536375a32691..e20183a9c834 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1922,6 +1922,7 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,
#if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush);
void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);
@@ -1930,6 +1931,11 @@ static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
{
return true;
}
+
+static inline bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
+{
+ return true;
+}
static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
size_t size)
{
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 26f63356da82..0c78121e2887 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -370,6 +370,8 @@ extern void __meminit kswapd_stop(int nid);
bool current_is_kswapd(void);
#ifdef CONFIG_SWAP
+bool mem_cgroup_can_vswap(struct mem_cgroup *memcg);
+
int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
unsigned long nr_pages, sector_t start_block);
int generic_swapfile_activate(struct swap_info_struct *, struct file *,
@@ -448,6 +450,11 @@ static inline void put_swap_device(struct swap_info_struct *si)
#else /* CONFIG_SWAP */
static inline void swap_writeback_dropbehind_folio(struct folio *folio) {}
+static inline bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
+{
+ return false;
+}
+
static inline struct swap_info_struct *get_swap_device(swp_entry_t entry)
{
return NULL;
@@ -546,6 +553,7 @@ static inline void mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_p
long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg);
+bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages);
bool mem_cgroup_swap_full(const struct folio *folio);
#else
static inline int mem_cgroup_try_charge_swap(struct folio *folio)
@@ -568,6 +576,11 @@ static inline long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg)
return get_nr_swap_pages();
}
+static inline bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
+{
+ return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages;
+}
+
static inline bool mem_cgroup_swap_full(const struct folio *folio)
{
return vm_swap_full();
diff --git a/mm/Kconfig b/mm/Kconfig
index 18925ccb4adc..704d43fcd92b 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -19,6 +19,26 @@ menuconfig SWAP
used to provide more virtual memory than the actual RAM present
in your computer. If unsure say Y.
+config VSWAP_DEFAULT_ON
+ bool "Route swapouts through virtual swap by default"
+ depends on SWAP
+ default n
+ help
+ Virtual swap allows zswap and zero-filled pages to be used
+ without swapping on a physical device first, and lets a page
+ move between zswap and a swapfile without invalidating the page
+ table entries that refer to it.
+
+ Swap entries are handed out by a virtual swap device instead of
+ naming a slot on a real one, so the backing can be chosen and
+ changed after the entry exists.
+
+ Say Y to make "vswap=on" the default, routing swapouts through
+ the virtual swap layer from boot.
+
+ Say N (default) to leave vswap off unless "vswap=on" is passed
+ on the kernel command line.
+
config ZSWAP
bool "Compressed cache for swap pages"
depends on SWAP
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index df02308bb80a..a3d065c594bc 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6150,6 +6150,22 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio)
return margin;
}
+/**
+ * mem_cgroup_can_swap - can @memcg swap out at least @nr_pages more pages?
+ * @memcg: the memcg to query
+ * @nr_pages: the number of pages the caller wants to swap out
+ *
+ * Return: true if @memcg can swap out at least @nr_pages more pages.
+ */
+bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
+{
+ if (!mem_cgroup_disabled() && !do_memsw_account() &&
+ page_counter_margin(&memcg->swap) < nr_pages)
+ return false;
+
+ return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages;
+}
+
bool mem_cgroup_swap_full(const struct folio *folio)
{
struct mem_cgroup *memcg;
@@ -6313,8 +6329,10 @@ static struct cftype swap_files[] = {
#ifdef CONFIG_ZSWAP
/**
- * obj_cgroup_may_zswap - check if this cgroup can zswap
- * @objcg: the object cgroup
+ * mem_cgroup_may_zswap - check if this cgroup can zswap
+ * @memcg: the memcg to query
+ * @may_flush: force-flush stats for an accurate check (sleeps). Pass false
+ * from atomic contexts; the check is then best-effort.
*
* Check if the hierarchical zswap limit has been reached.
*
@@ -6324,36 +6342,38 @@ static struct cftype swap_files[] = {
* spending cycles on compression when there is already no room left
* or zswap is disabled altogether somewhere in the hierarchy.
*/
-bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
{
- struct mem_cgroup *memcg, *original_memcg;
- bool ret = true;
-
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
return true;
- original_memcg = get_mem_cgroup_from_objcg(objcg);
- for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
- memcg = parent_mem_cgroup(memcg)) {
+ for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
unsigned long max = READ_ONCE(memcg->zswap_max);
unsigned long pages;
if (max == PAGE_COUNTER_MAX)
continue;
- if (max == 0) {
- ret = false;
- break;
- }
+ if (max == 0)
+ return false;
/* Force flush to get accurate stats for charging */
- __mem_cgroup_flush_stats(memcg, true);
+ if (may_flush)
+ __mem_cgroup_flush_stats(memcg, true);
pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
- if (pages < max)
- continue;
- ret = false;
- break;
+ if (pages >= max)
+ return false;
}
- mem_cgroup_put(original_memcg);
+ return true;
+}
+
+bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+{
+ struct mem_cgroup *memcg;
+ bool ret;
+
+ memcg = get_mem_cgroup_from_objcg(objcg);
+ ret = mem_cgroup_may_zswap(memcg, true);
+ mem_cgroup_put(memcg);
return ret;
}
diff --git a/mm/page_io.c b/mm/page_io.c
index 956c3ffc1039..854b0b71581d 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -209,6 +209,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
*/
int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
{
+ swp_entry_t phys = folio->swap;
int ret = 0;
if (folio_free_swap(folio))
@@ -252,8 +253,14 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
*/
swap_zeromap_folio_clear(folio);
+ /*
+ * For vswap: release stale non-swapfile backings (e.g. ZSWAP from a
+ * previous swapout cycle) so zswap_store or folio_realloc_swap
+ * starts on clean slots. Contiguous PHYS backing is preserved for
+ * reuse by folio_realloc_swap.
+ */
if (is_vswap_entry(folio->swap))
- folio_release_vswap_backing(folio);
+ folio_release_non_phys_swap_backing(folio);
if (zswap_store(folio)) {
count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
@@ -268,13 +275,21 @@ 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)) {
+ phys = folio_realloc_swap(folio);
+ if (!phys.val) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
+ }
+ }
+
+ if (ctx->nofs &&
+ (__swap_entry_to_info(phys)->ops->flags & SWAP_OPS_F_REQUIRE_NOFS)) {
folio_mark_dirty(folio);
return AOP_WRITEPAGE_ACTIVATE;
}
- __swap_writeout(ctx, folio, folio->swap);
+ __swap_writeout(ctx, folio, phys);
return 0;
out_unlock:
folio_unlock(folio);
diff --git a/mm/shmem.c b/mm/shmem.c
index 5d6807518474..5d3695886ecd 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1914,7 +1914,8 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap)
goto redirty;
- if (!total_swap_pages)
+ /* vswap doesn't contribute to total_swap_pages */
+ if (!total_swap_pages && !(vswap_is_enabled() && zswap_is_enabled()))
goto redirty;
/*
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 110a0c095825..ae41730bc806 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -186,6 +186,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);
+ /* Physical readahead can hit a vswap-backing rmap slot; skip it. */
+ if (swp_tb_is_vswap(old_tb))
+ return -ENOENT;
if (swp_tb_is_folio(old_tb))
return -EEXIST;
/*
@@ -215,7 +218,8 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
ci_end = ci_off + nr;
do {
old_tb = __swap_table_get(ci, ci_off);
- if (unlikely(!swp_tb_is_shadow(old_tb) ||
+ if (unlikely(swp_tb_is_vswap(old_tb) ||
+ !swp_tb_is_shadow(old_tb) ||
!__swp_tb_get_count(old_tb) ||
is_zero != __swap_table_test_zero(ci, ci_off) ||
(memcg_id && *memcg_id != __swap_cgroup_get(ci, ci_off))))
diff --git a/mm/swap_table.h b/mm/swap_table.h
index 36b17f03bd9b..a54cacbc2ade 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -428,6 +428,13 @@ static inline bool swp_tb_is_vswap(unsigned long swp_tb)
}
#ifdef CONFIG_SWAP
+static inline unsigned long swp_entry_to_swp_tb_vswap(swp_entry_t entry)
+{
+ BUILD_BUG_ON(SWP_OFFSET_MASK >
+ (SWP_RMAP_ENTRY_MASK >> SWP_TB_VSWAP_MARK_BITS));
+ return (swp_offset(entry) << SWP_TB_VSWAP_MARK_BITS) | SWP_TB_VSWAP_MARK;
+}
+
static inline swp_entry_t swp_tb_vswap_to_swp_entry(unsigned long swp_tb)
{
unsigned long offset;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 6933e069bd2e..3dca9bd55736 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -57,6 +57,10 @@ static bool folio_swapcache_freeable(struct folio *folio);
static void move_cluster(struct swap_info_struct *si,
struct swap_cluster_info *ci, struct list_head *list,
enum swap_cluster_flags new_flags);
+static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
+ struct swap_cluster_info *pci,
+ unsigned int ci_start,
+ unsigned int nr_pages);
/*
* Protects the swap_info array, and the SWP_USED flag. swap_info contains
@@ -1020,6 +1024,8 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
{
unsigned int order;
unsigned long nr_pages;
+ swp_entry_t vswap_entry, v;
+ unsigned int i;
lockdep_assert_held(&ci->lock);
@@ -1029,8 +1035,8 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
/*
* All mm swap allocation starts with a folio (folio_alloc_swap),
* it's also the only allocation path for large orders allocation.
- * Such swap slots starts with count == 0 and will be increased
- * upon folio unmap.
+ * Slots used directly start with count == 0 and gain references
+ * when the folio is unmapped.
*
* Else, it's a exclusive order 0 allocation for hibernation.
* The slot carries no swap count and is freed by offset.
@@ -1039,8 +1045,26 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
order = folio_order(folio);
nr_pages = 1 << order;
swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
- __swap_cache_add_folio(ci, folio, swp_entry(si->type,
- ci_off + cluster_offset(si, ci)));
+ if (folio_test_swapcache(folio)) {
+ /*
+ * Folio already in the swap cache: we are allocating
+ * physical backing for its vswap entry. Point each
+ * physical slot back at its own vswap entry
+ * (Vswap-tagged rmap).
+ */
+ VM_WARN_ON(!is_vswap_entry(folio->swap));
+ vswap_entry = folio->swap;
+ for (i = 0; i < nr_pages; i++) {
+ v = vswap_entry;
+ v.val += i;
+ __swap_table_set(ci, ci_off + i,
+ swp_entry_to_swp_tb_vswap(v));
+ }
+ } else {
+ __swap_cache_add_folio(ci, folio,
+ swp_entry(si->type,
+ ci_off + cluster_offset(si, ci)));
+ }
} else if (IS_ENABLED(CONFIG_HIBERNATION)) {
order = 0;
nr_pages = 1;
@@ -1911,6 +1935,12 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return err;
}
+bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
+{
+ return vswap_is_enabled() && zswap_is_enabled() &&
+ (!memcg || mem_cgroup_may_zswap(memcg, false));
+}
+
static bool folio_may_vswap(struct folio *folio)
{
struct obj_cgroup *objcg;
@@ -2061,8 +2091,11 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
unsigned int ci_start, unsigned int nr)
{
struct swap_cluster_info_dynamic *ci_dyn;
+ struct swap_info_struct *psi;
+ unsigned long phys_off_start = 0, phys_off_end = 0;
unsigned int ci_off;
unsigned long vt;
+ swp_entry_t phys_first = {};
lockdep_assert_held(&ci->lock);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
@@ -2070,7 +2103,30 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
vt = __vtable_get(ci_dyn, ci_off);
+ /* The free helper takes one contiguous run within one cluster. */
+ if (phys_off_start != phys_off_end &&
+ (vtable_type(vt) != VSWAP_SWAPFILE ||
+ swp_type(vtable_to_phys(vt)) != swp_type(phys_first) ||
+ swp_offset(vtable_to_phys(vt)) != phys_off_end ||
+ phys_off_end % SWAPFILE_CLUSTER == 0)) {
+ psi = __swap_entry_to_info(phys_first);
+ __swap_cluster_free_phys_backing(psi,
+ __swap_entry_to_cluster(phys_first),
+ phys_off_start % SWAPFILE_CLUSTER,
+ phys_off_end - phys_off_start);
+ phys_off_start = phys_off_end = 0;
+ }
+
switch (vtable_type(vt)) {
+ case VSWAP_SWAPFILE:
+ if (phys_off_start == phys_off_end) {
+ phys_first = vtable_to_phys(vt);
+ phys_off_start = swp_offset(phys_first);
+ phys_off_end = phys_off_start + 1;
+ } else {
+ phys_off_end++;
+ }
+ break;
case VSWAP_ZSWAP:
zswap_entry_free(vtable_to_zswap(vt));
break;
@@ -2086,6 +2142,14 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
if (__swap_table_test_zero(ci, ci_off))
__swap_table_clear_zero(ci, ci_off);
}
+
+ if (phys_off_start != phys_off_end) {
+ psi = __swap_entry_to_info(phys_first);
+ __swap_cluster_free_phys_backing(psi,
+ __swap_entry_to_cluster(phys_first),
+ phys_off_start % SWAPFILE_CLUSTER,
+ phys_off_end - phys_off_start);
+ }
}
/**
@@ -2113,6 +2177,103 @@ void folio_release_vswap_backing(struct folio *folio)
spin_unlock(&ci->lock);
}
+/**
+ * folio_release_non_phys_swap_backing() - Drop a folio's non-physical vswap backing.
+ * @folio: the folio, occupying a virtual swap entry.
+ *
+ * Release the zswap backing recorded for @folio's virtual swap entry,
+ * leaving the slots empty so the writeout path can install fresh physical
+ * backing. Does nothing when the entry is already backed by physical
+ * swapfile slots, which are kept for reuse, or when it has no backing
+ * beyond the swap cache folio itself.
+ *
+ * Context: Caller must hold the folio lock; @folio must be in the swap cache
+ * and occupy a virtual swap entry.
+ */
+void folio_release_non_phys_swap_backing(struct folio *folio)
+{
+ struct swap_cluster_info *ci;
+ struct swap_cluster_info_dynamic *ci_dyn;
+ int nr = folio_nr_pages(folio);
+ unsigned int voff;
+ unsigned long vt;
+ enum vswap_backing_type type;
+
+ ci = __swap_entry_to_cluster(folio->swap);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ voff = swp_cluster_offset(folio->swap);
+
+ spin_lock(&ci->lock);
+ /*
+ * A folio's slots cannot mix swapfile with other backends, except
+ * mid-backend-change, which always starts from slot 0.
+ */
+ vt = __vtable_get(ci_dyn, voff);
+ type = vtable_type(vt);
+
+ if (type == VSWAP_SWAPFILE || type == VSWAP_NONE) {
+ spin_unlock(&ci->lock);
+ return;
+ }
+
+ __vswap_release_backing(ci, voff, nr);
+ spin_unlock(&ci->lock);
+}
+
+/**
+ * folio_realloc_swap() - Back a virtual swap folio with a physical swap slot.
+ * @folio: the folio, occupying a virtual swap entry.
+ *
+ * Ensure @folio's virtual swap entry has physical (swapfile) backing,
+ * allocating a physical slot on demand if it has none. If @folio is
+ * already physically backed, the existing physical entry is returned
+ * unchanged.
+ *
+ * Context: Caller must hold the folio lock; @folio must be in the swap cache
+ * and occupy a virtual swap entry.
+ * Return: The physical swap entry now backing @folio, or an empty entry
+ * (.val == 0) on failure.
+ */
+swp_entry_t folio_realloc_swap(struct folio *folio)
+{
+ swp_entry_t vswap_entry = folio->swap;
+ struct swap_cluster_info *ci;
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int voff;
+ swp_entry_t phys_entry = {};
+ swp_entry_t pe;
+ int i, nr = folio_nr_pages(folio);
+
+ VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
+ VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
+ VM_WARN_ON(!is_vswap_entry(vswap_entry));
+
+ phys_entry = vswap_to_phys(vswap_entry);
+ if (phys_entry.val)
+ return phys_entry;
+
+ phys_entry = folio_alloc_phys_swap(folio);
+ if (!phys_entry.val)
+ return (swp_entry_t){};
+
+ voff = swp_cluster_offset(vswap_entry);
+
+ ci = __swap_entry_to_cluster(vswap_entry);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ spin_lock(&ci->lock);
+ /*
+ * Install PHYS backing without freeing any prior contents of the
+ * vtable. Releasing the old backing is the caller's job.
+ */
+ for (i = 0; i < nr; i++) {
+ pe.val = phys_entry.val + i;
+ __vtable_set(ci_dyn, voff + i, vtable_mk_phys(pe));
+ }
+ spin_unlock(&ci->lock);
+
+ return phys_entry;
+}
+
/**
* folio_dup_swap() - Increase swap count of swap entries of a folio.
* @folio: folio with swap entries bounded.
@@ -2242,6 +2403,47 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
return ERR_PTR(-EIO);
}
+/*
+ * Common tail for freeing swap slots: device-level accounting
+ * and cluster list management.
+ */
+static void __swap_cluster_finish_free(struct swap_info_struct *si,
+ struct swap_cluster_info *ci,
+ unsigned int ci_start,
+ unsigned int nr_pages)
+{
+ lockdep_assert_held(&ci->lock);
+ swap_range_free(si, cluster_offset(si, ci) + ci_start, nr_pages);
+ swap_cluster_assert_empty(ci, ci_start, nr_pages, false);
+
+ if (!ci->count)
+ free_cluster(si, ci);
+ else
+ partial_free_cluster(si, ci);
+}
+
+/*
+ * Free physical swap slots that were backing vswap entries (Vswap-tagged).
+ */
+static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
+ struct swap_cluster_info *pci,
+ unsigned int ci_start,
+ unsigned int nr_pages)
+{
+ unsigned int ci_off;
+
+ spin_lock_nested(&pci->lock, SINGLE_DEPTH_NESTING);
+ VM_WARN_ON(pci->count < nr_pages);
+ pci->count -= nr_pages;
+ for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) {
+ __swap_table_set(pci, ci_off, null_to_swp_tb());
+ if (!SWAP_TABLE_HAS_ZEROFLAG)
+ __swap_table_clear_zero(pci, ci_off);
+ }
+ __swap_cluster_finish_free(psi, pci, ci_start, nr_pages);
+ swap_cluster_unlock(pci);
+}
+
/*
* Free a set of swap slots after their swap count dropped to zero, or will be
* zero after putting the last ref (saves one __swap_cluster_put_entry call).
@@ -2253,7 +2455,6 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
unsigned long old_tb;
unsigned short batch_id = 0, id_cur;
unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
- unsigned long ci_head = cluster_offset(si, ci);
unsigned int batch_off = ci_off;
VM_WARN_ON(ci->count < nr_pages);
@@ -2293,13 +2494,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
if (batch_id)
mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off);
- swap_range_free(si, ci_head + ci_start, nr_pages);
- swap_cluster_assert_empty(ci, ci_start, nr_pages, false);
-
- if (!ci->count)
- free_cluster(si, ci);
- else
- partial_free_cluster(si, ci);
+ __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
}
int __swap_count(swp_entry_t entry)
@@ -4515,3 +4710,76 @@ subsys_initcall(swapfile_init);
struct swap_info_struct *vswap_si;
DEFINE_STATIC_KEY_FALSE(vswap_key);
+
+static bool vswap_enabled_early __initdata = IS_ENABLED(CONFIG_VSWAP_DEFAULT_ON);
+
+static int __init early_vswap(char *buf)
+{
+ return kstrtobool(buf, &vswap_enabled_early);
+}
+early_param("vswap", early_vswap);
+
+/* vswap does no IO on its own. */
+static const struct swap_ops vswap_ops = { };
+
+static int __init vswap_init(void)
+{
+ struct swap_info_struct *si;
+ unsigned long maxpages;
+ int err;
+
+ if (!vswap_enabled_early)
+ return 0;
+
+ si = alloc_swap_info();
+ if (IS_ERR(si)) {
+ pr_warn("vswap: alloc_swap_info failed (%ld); vswap disabled, swapout falls back to direct physical swap\n",
+ PTR_ERR(si));
+ return 0;
+ }
+
+ /*
+ * Each swapped page holds a reference on its cgroup's
+ * objcg->private_id_ref, a 32-bit refcount_t, so a cgroup that swaps
+ * REFCOUNT_MAX pages saturates it and leaks the objcg until reboot.
+ * Cap the device there.
+ */
+ maxpages = min(swapfile_maximum_size,
+ ALIGN_DOWN((unsigned long)REFCOUNT_MAX, SWAPFILE_CLUSTER));
+ /*
+ * SWP_WRITEOK enables slot allocation. SWP_SOLIDSTATE selects
+ * per-CPU cluster allocation; vswap has no si->global_cluster.
+ */
+ si->flags |= SWP_VSWAP | SWP_SOLIDSTATE | SWP_WRITEOK;
+ si->ops = &vswap_ops;
+ si->bdev = NULL;
+ si->max = maxpages;
+ si->pages = maxpages - 1;
+
+ INIT_WORK(&si->discard_work, swap_discard_work);
+ INIT_WORK(&si->reclaim_work, swap_reclaim_work);
+
+ err = setup_swap_clusters_info(si, NULL, maxpages);
+ if (err)
+ goto fail;
+
+ mutex_lock(&swapon_mutex);
+ enable_swap_info(si);
+ mutex_unlock(&swapon_mutex);
+
+ vswap_si = si;
+ pr_info("vswap: created virtual swap device (%lu pages)\n", maxpages);
+
+ /* Last: everything above must be visible before routing starts. */
+ static_branch_enable(&vswap_key);
+ return 0;
+
+fail:
+ pr_warn("vswap: setup_swap_clusters_info failed (%d); vswap disabled, swapout falls back to direct physical swap\n",
+ err);
+ spin_lock(&swap_lock);
+ si->flags = 0;
+ spin_unlock(&swap_lock);
+ return 0;
+}
+late_initcall(vswap_init);
diff --git a/mm/util.c b/mm/util.c
index 80f6c714844f..47c4cf7400cf 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -33,6 +33,7 @@
#include "internal.h"
#include "swap.h"
+#include "vswap.h"
/**
* kfree_const - conditionally free memory
@@ -983,7 +984,17 @@ int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin
return 0;
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
- if (pages > totalram_pages() + total_swap_pages)
+ allowed = totalram_pages() + total_swap_pages;
+ /*
+ * Vswap is in neither term, so with no swapfile the bound
+ * would collapse to RAM alone even though zswap can absorb
+ * more. Compression swap is backed by memory, so budget a 3x
+ * compression ratio, typical of most workloads. Anything that
+ * compresses better can use OVERCOMMIT_ALWAYS.
+ */
+ if (vswap_is_enabled() && zswap_is_enabled())
+ allowed += 2 * totalram_pages();
+ if (pages > allowed)
goto error;
return 0;
}
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 5bf20b57fe45..77aefadc24f9 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -302,10 +302,12 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
if (memcg == NULL) {
/*
* For non-memcg reclaim, is there space in any swap device?
- * And under GFP_NOIO, is there enough swapcached anon to make
- * scanning anon worthwhile?
+ * vswap does not contribute to nr_swap_pages. And under
+ * GFP_NOIO, is there enough swapcached anon to make scanning
+ * anon worthwhile?
*/
- if (get_nr_swap_pages() > 0 &&
+ if ((get_nr_swap_pages() > 0 ||
+ (vswap_is_enabled() && zswap_is_enabled())) &&
!reclaimable_anon_is_low(memcg, nid, sc))
return true;
} else {
@@ -313,7 +315,7 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
* Is the memcg below its swap limit, and under GFP_NOIO does
* it have enough swapcached anon to make scanning worthwhile?
*/
- if (mem_cgroup_get_nr_swap_pages(memcg) > 0 &&
+ if (mem_cgroup_can_swap(memcg, 1) &&
!reclaimable_anon_is_low(memcg, nid, sc))
return true;
}
@@ -2752,7 +2754,7 @@ static bool can_age_anon_pages(struct lruvec *lruvec,
struct scan_control *sc)
{
/* Aging the anon LRU is valuable if swap is present: */
- if (total_swap_pages > 0)
+ if (total_swap_pages > 0 || (vswap_is_enabled() && zswap_is_enabled()))
return true;
/* Also valuable if anon pages can be demoted: */
@@ -2827,7 +2829,7 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
return 0;
if (!can_demote(pgdat->node_id, sc, memcg) &&
- mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
+ !mem_cgroup_can_swap(memcg, MIN_LRU_BATCH))
return 0;
return swappiness;
diff --git a/mm/vswap.h b/mm/vswap.h
index c4f0670c9b5c..080e4bec8280 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -29,6 +29,10 @@ enum vswap_backing_type {
#include "swap_table.h"
DECLARE_STATIC_KEY_FALSE(vswap_key);
+/*
+ * Only true once vswap_init() has published vswap_si, so callers never
+ * see the device half built.
+ */
static inline bool vswap_is_enabled(void)
{
return static_branch_unlikely(&vswap_key);
@@ -88,6 +92,13 @@ static inline enum vswap_backing_type vtable_type(unsigned long vt)
return vt ? VSWAP_ZSWAP : VSWAP_NONE;
}
+static inline unsigned long vtable_mk_phys(swp_entry_t entry)
+{
+ BUILD_BUG_ON(BITS_PER_XA_VALUE + VTABLE_TAG_BITS > BITS_PER_LONG);
+ VM_WARN_ON_ONCE(entry.val >> BITS_PER_XA_VALUE);
+ return (entry.val << VTABLE_TAG_BITS) | VTABLE_TAG_MASK;
+}
+
static inline swp_entry_t vtable_to_phys(unsigned long vt)
{
VM_WARN_ON(vtable_type(vt) != VSWAP_SWAPFILE);
@@ -212,6 +223,8 @@ static inline struct zswap_entry *vswap_zswap_load(swp_entry_t entry)
}
void folio_release_vswap_backing(struct folio *folio);
+swp_entry_t folio_realloc_swap(struct folio *folio);
+void folio_release_non_phys_swap_backing(struct folio *folio);
/*
* Walk nr vtable slots starting at voff in ci_dyn. Returns the prefix
diff --git a/mm/workingset.c b/mm/workingset.c
index 87d5e55dd684..283cf0d150bc 100644
--- a/mm/workingset.c
+++ b/mm/workingset.c
@@ -523,7 +523,7 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset,
workingset_size += lruvec_page_state(eviction_lruvec,
NR_INACTIVE_FILE);
}
- if (mem_cgroup_get_nr_swap_pages(eviction_memcg) > 0) {
+ if (mem_cgroup_can_swap(eviction_memcg, 1)) {
workingset_size += lruvec_page_state(eviction_lruvec,
NR_ACTIVE_ANON);
if (file) {
diff --git a/mm/zswap.c b/mm/zswap.c
index aab8ec782d03..be68ffa1f554 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1720,7 +1720,7 @@ bool zswap_store(struct folio *folio)
*/
if (is_vswap_entry(swp)) {
if (index > 0)
- folio_release_vswap_backing(folio);
+ folio_release_non_phys_swap_backing(folio);
} else {
zswap_invalidate(swp_type(swp), swp_offset(swp), nr_pages);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] mm: large vswap folios declined by zswap cannot split on phys
fallback
- [Medium] mm: vswap reclaim predicates ignore global zswap pool
fullness
- [Medium] mm: duplicate forced rstat flush per swapped folio in
folio_may_vswap()
- [Medium] mm/swap: vswap_init() bypasses swapon/swapoff with empty
vswap_ops
- [Low] mm/swap: unbatched per-page physical backing free in
zswap_store()
--
commit d69d6282bb4267cdc2da182e4603e10f267cd488
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: support physical swap as a vswap backend
This commit adds physical swapfile backing for virtual swap entries when
zswap declines a folio, records physical-to-virtual reverse maps, and
registers the virtual swap device at boot behind the vswap= parameter and
CONFIG_VSWAP_DEFAULT_ON.
Link: https://lore.kernel.org/all/sashiko-outbox-181078@kernel.org/
Link: https://lore.kernel.org/all/sashiko-outbox-181079@kernel.org/
[ ... ]
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index df02308bb80ab..a3d065c594bc9 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -6324,36 +6342,38 @@ static struct cftype swap_files[] = {
> * spending cycles on compression when there is already no room left
> * or zswap is disabled altogether somewhere in the hierarchy.
> */
> -bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
> +bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
> {
[ ... ]
> /* Force flush to get accurate stats for charging */
> - __mem_cgroup_flush_stats(memcg, true);
> + if (may_flush)
> + __mem_cgroup_flush_stats(memcg, true);
> pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
> - if (pages < max)
> - continue;
> - ret = false;
> - break;
> + if (pages >= max)
> + return false;
> }
> - mem_cgroup_put(original_memcg);
> + return true;
> +}
> +
> +bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
> +{
> + struct mem_cgroup *memcg;
> + bool ret;
> +
> + memcg = get_mem_cgroup_from_objcg(objcg);
> + ret = mem_cgroup_may_zswap(memcg, true);
> + mem_cgroup_put(memcg);
> return ret;
> }
[Severity: Medium]
With vswap enabled by this patch, does folio_may_vswap() calling
obj_cgroup_may_zswap() double the forced rstat flushes on every swapped
folio?
For cgroups with a finite non-zero memory.zswap.max, folio_alloc_swap()
now invokes obj_cgroup_may_zswap() -> mem_cgroup_may_zswap(memcg, true),
which unconditionally runs css_rstat_flush() across all possible CPUs
under the global rstat lock, and zswap_store() then repeats the same
forced flush on the same folio during swap_writeout(). Folios that never
reach zswap_store() (such as zero-filled or unmap-failed folios) also pay
this cost.
Since zswap_store() already performs the authoritative flushing check and
mem_cgroup_may_zswap(memcg, false) still catches memory.zswap.max == 0,
could folio_may_vswap() use mem_cgroup_may_zswap(memcg, false) instead?
> diff --git a/mm/page_io.c b/mm/page_io.c
> index 956c3ffc10397..854b0b71581d6 100644
> --- a/mm/page_io.c
> +++ b/mm/page_io.c
[ ... ]
> @@ -252,8 +253,14 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
> */
> swap_zeromap_folio_clear(folio);
>
> + /*
> + * For vswap: release stale non-swapfile backings (e.g. ZSWAP from a
> + * previous swapout cycle) so zswap_store or folio_realloc_swap
> + * starts on clean slots. Contiguous PHYS backing is preserved for
> + * reuse by folio_realloc_swap.
> + */
> if (is_vswap_entry(folio->swap))
> - folio_release_vswap_backing(folio);
> + folio_release_non_phys_swap_backing(folio);
>
> if (zswap_store(folio)) {
[Severity: Low]
When a physically backed large folio is redirtied in the swap cache and
then accepted by zswap_store(), does preserving the physical backing here
cause it to be freed one page at a time?
zswap_store_page() calls vswap_zswap_store() per subpage, which invokes
__vswap_release_backing(&ci_dyn->ci, voff, 1) and bypasses the contiguous
batching in __vswap_release_backing(). For a 2MB THP, that repeats
pci->lock, smp_wmb(), and global nr_swap_pages atomics up to 512 times,
and exposes the half-freed physical cluster on nonfull_clusters while the
remaining subpages are still compressing.
Batch-releasing the physical backing once in zswap_store() after admission
checks pass would keep physical slots intact on rejection while avoiding
per-page teardown on acceptance.
[ ... ]
> - /* A vswap folio has no physical slot to write to. */
> if (is_vswap_entry(folio->swap)) {
> + phys = folio_realloc_swap(folio);
> + if (!phys.val) {
> + folio_mark_dirty(folio);
> + return AOP_WRITEPAGE_ACTIVATE;
> + }
> + }
[Severity: High]
Can a large vswap folio become permanently unreclaimable if zswap_store()
rejects it and folio_realloc_swap() cannot allocate a contiguous physical
entry?
Because cluster_alloc_swap_entry() exempts vswap_si from the SWP_BLKDEV
check, folio_alloc_swap() succeeds for large folios instead of returning
-E2BIG and triggering a split. If zswap_store() later declines the folio
(for example, when the global zswap pool is full or GFP_NOWAIT compression
allocation fails), folio_realloc_swap() calls folio_alloc_phys_swap() on
the unsplit folio, which unconditionally fails on regular swapfiles and
fails on fragmented block swap devices.
Returning AOP_WRITEPAGE_ACTIVATE leaves an anonymous large folio in the
swap cache with vswap PTEs installed, so subsequent shrink_folio_list()
passes skip the !folio_test_swapcache(folio) block and can never reach
split_folio_to_list(). Large shmem folios hit a similar loop because
shmem_writeout() jumps to redirty rather than try_split on
AOP_WRITEPAGE_ACTIVATE, leading to reclaim thrashing and premature OOM
even when plenty of order-0 physical swap is free.
[ ... ]
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 6933e069bd2ea..3dca9bd557361 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1911,6 +1935,12 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
> return err;
> }
>
> +bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
> +{
> + return vswap_is_enabled() && zswap_is_enabled() &&
> + (!memcg || mem_cgroup_may_zswap(memcg, false));
> +}
> +
> static bool folio_may_vswap(struct folio *folio)
> {
> struct obj_cgroup *objcg;
[Severity: Medium]
Should mem_cgroup_can_vswap() and the other vswap reclaim predicates
(folio_may_vswap(), can_reclaim_anon_pages(), can_age_anon_pages(), and
shmem_writeout()) also check global zswap pool capacity?
Right now, zswap_pool_reached_full is only checked inside zswap_store().
When zswap is used without a physical swap device and the pool reaches
zswap_max_pool_percent, reclaim keeps isolating anonymous and shmem
folios, allocating vswap entries, unmapping PTEs with TLB shootdowns, and
failing at swap_writeout() with AOP_WRITEPAGE_ACTIVATE. Checking global
zswap headroom before reclaim and swap allocation would avoid this
thrashing loop.
[ ... ]
> @@ -4515,3 +4710,76 @@ subsys_initcall(swapfile_init);
>
> struct swap_info_struct *vswap_si;
> DEFINE_STATIC_KEY_FALSE(vswap_key);
> +
> +static bool vswap_enabled_early __initdata = IS_ENABLED(CONFIG_VSWAP_DEFAULT_ON);
> +
> +static int __init early_vswap(char *buf)
> +{
> + return kstrtobool(buf, &vswap_enabled_early);
> +}
> +early_param("vswap", early_vswap);
> +
> +/* vswap does no IO on its own. */
> +static const struct swap_ops vswap_ops = { };
> +
> +static int __init vswap_init(void)
[Severity: Medium]
As reported by Chris Li on v5 [1], vswap_ops is left empty while core swap
paths accumulate scattered swap_is_vswap() special cases (including
bypassing si->users refcounting in get_swap_device_info() and
put_swap_device()), and vswap_init() auto-registers a swap device outside
the swapon/swapoff lifecycle.
Later commit "mm/swap: make vswap mandatory" removes the vswap= boot
parameter and CONFIG_VSWAP_DEFAULT_ON, making this auto-registration
unconditional on CONFIG_SWAP kernels while leaving vswap_ops empty. Can
vswap-specific behavior be encapsulated behind swap_ops to avoid
fragmenting the core swap and locking paths?
[1] https://lore.kernel.org/r/CACePvbVaDNspMDSw_qAcapKAzXj3JGV-KMYmow7aePa0POHpSg@mail.gmail.com
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=8
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (7 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
@ 2026-12-03 21:59 ` 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
` (11 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 21:59 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Swap a large anon folio back in as a unit when its vswap entries share a
contiguous run of physical swap slots on a synchronous IO device,
instead of always falling back to order-0 faults.
A zswap-backed or mixed-backing batch is still refused, and the fault
retries at a smaller order. Limit the order mask to one swap cluster.
Require block-device backing: adjacent regular-file slots can cover
different extents, while a large folio is read with one bio.
Link: https://lore.kernel.org/all/sashiko-outbox-181079@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/memory.c | 10 ++++++----
mm/swap_state.c | 38 ++++++++++++++++++++++++++++++--------
mm/zswap.c | 6 +++++-
3 files changed, 41 insertions(+), 13 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 3eb1841530bb..ae9d5e69622f 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4924,9 +4924,10 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
* lack handling for such cases, so fallback to swapping in order-0
* folio.
*
- * THP swapin for vswap is not supported yet either.
+ * Vswap entries are checked later, under the cluster lock in
+ * __swap_cache_add_check().
*/
- if (is_vswap_entry(entry) || !zswap_never_enabled())
+ if (!is_vswap_entry(entry) && !zswap_never_enabled())
return 0;
/*
@@ -4934,7 +4935,7 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
* is read with one bio. The flag is set in swapon() and stays, so the
* race is benign.
*/
- if (!data_race(si->flags & SWP_BLKDEV))
+ if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
return 0;
/*
@@ -5250,7 +5251,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
* large folio is fully applicable before use.
*/
if (!swap_cache_folio_mappable(folio, nr_pages)) {
- swap_cache_del_folio(folio);
+ if (!is_vswap_entry(entry) || !folio_test_dirty(folio))
+ swap_cache_del_folio(folio);
goto out_nomap;
}
diff --git a/mm/swap_state.c b/mm/swap_state.c
index ae41730bc806..ee3d8fa5c1f3 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -174,6 +174,9 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
unsigned int ci_off, ci_end;
unsigned long old_tb;
bool is_zero;
+ struct swap_cluster_info_dynamic *ci_dyn;
+ enum vswap_backing_type type;
+ int ret;
lockdep_assert_held(&ci->lock);
@@ -207,11 +210,29 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
return 0;
/*
- * Reject a vswap batch so __swap_cache_alloc_folio falls back to
- * order 0.
+ * For a vswap entry batch, reject if the backing is not THP-amenable
+ * (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in
+ * __swap_cache_alloc_folio will retry with a smaller order on -EBUSY.
*/
- if (is_vswap_entry(targ_entry))
- return -EBUSY;
+ if (is_vswap_entry(targ_entry)) {
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr),
+ nr, &type);
+ if (ret != nr || type == VSWAP_ZSWAP)
+ return -EBUSY;
+ if (type == VSWAP_SWAPFILE) {
+ struct swap_info_struct *si;
+ swp_entry_t phys;
+ unsigned long vt;
+
+ vt = __vtable_get(ci_dyn, round_down(ci_off, nr));
+ phys = vtable_to_phys(vt);
+ si = __swap_entry_to_info(phys);
+ /* Swapfile extents can end inside a large folio. */
+ if (!data_race(si->flags & SWP_BLKDEV))
+ return -EBUSY;
+ }
+ }
is_zero = __swap_table_test_zero(ci, ci_off);
ci_off = round_down(ci_off, nr);
@@ -560,12 +581,13 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp,
int order, err;
struct folio *ret;
- order = highest_order(orders);
-
- /* orders must be non-zero, and must not exceed cluster size. */
- if (WARN_ON_ONCE(!orders || (1UL << order) > SWAPFILE_CLUSTER))
+ /* A swapcache folio cannot span multiple swap clusters. */
+ orders &= GENMASK(ilog2(SWAPFILE_CLUSTER), 0);
+ if (WARN_ON_ONCE(!orders))
return ERR_PTR(-EINVAL);
+ order = highest_order(orders);
+
do {
ret = __swap_cache_alloc(targ_entry, gfp, order,
vmf, mpol, ilx);
diff --git a/mm/zswap.c b/mm/zswap.c
index be68ffa1f554..25d4c3f7785a 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1801,9 +1801,13 @@ int zswap_load(struct folio *folio)
* range on the backing device, so scan the range rather than rejecting
* it outright. The caller has pinned every slot, so zswap cannot start
* a store or a writeback into the range while we look.
+ *
+ * A vswap batch is checked when the folio enters the swap cache, and
+ * its backing cannot change after that.
*/
if (folio_test_large(folio)) {
- if (WARN_ON_ONCE(zswap_is_present(swp,
+ if (WARN_ON_ONCE(!swap_is_vswap(si) &&
+ zswap_is_present(swp,
folio_nr_pages(folio)))) {
folio_unlock(folio);
return -EIO;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm, swap: zswap vswap faults trigger PTL scans and mTHP
fallback cascade
- [Low] mm: redundant and contradictory dirty guard in do_swap_page()
--
commit 75209e7921ed8209524c9062f6295ef362c17766
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: enable THP swapin for vswap entries
This commit enables large-folio swapin for vswap entries backed by
contiguous physical swap slots on a synchronous block device, checking the
vswap backing under the cluster lock in __swap_cache_add_check() and clamping
the order mask to a single swap cluster.
Link: https://lore.kernel.org/all/sashiko-outbox-181079@kernel.org/
> diff --git a/mm/memory.c b/mm/memory.c
> index 3eb1841530bb3..ae9d5e69622f9 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4924,9 +4924,10 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
> * lack handling for such cases, so fallback to swapping in order-0
> * folio.
> *
> - * THP swapin for vswap is not supported yet either.
> + * Vswap entries are checked later, under the cluster lock in
> + * __swap_cache_add_check().
> */
> - if (is_vswap_entry(entry) || !zswap_never_enabled())
> + if (!is_vswap_entry(entry) && !zswap_never_enabled())
> return 0;
>
> /*
> @@ -4934,7 +4935,7 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
> * is read with one bio. The flag is set in swapon() and stays, so the
> * race is benign.
> */
> - if (!data_race(si->flags & SWP_BLKDEV))
> + if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
> return 0;
>
> /*
[Severity: High]
Can bypassing these early exits for vswap entries cause every zswap-backed
vswap fault in an mTHP-aligned range to take the page-table lock, scan PTEs,
and cascade through every enabled large order before falling back to order 0?
When a large anon folio is swapped out to zswap under vswap, its virtual
offsets are contiguous and aligned. On a 4 KiB fault, do_swap_page() calls
thp_swapin_suitable_orders() even after swap_entry_synchronous() matched
zswap_is_present(), acquiring the PTE lock and scanning up to PMD_ORDER PTEs.
__swap_cache_alloc_folio() then loops from highest_order() down to order 1,
taking ci->lock and scanning all nr virtual_table slots in
__vswap_check_backing() (up to 510 atomic reads for order 8) before returning
-EBUSY and spuriously incrementing MTHP_STAT_SWPIN_FALLBACK on each order.
The same regression also hits synchronous regular swapfiles without
SWP_BLKDEV. Checking for non-zswap SWP_BLKDEV backing before scanning PTEs,
and short-circuiting to order 0 in __swap_cache_add_check() when targ_entry
precludes large swapin, would avoid the lock/scan cost and bogus stats.
> @@ -5250,7 +5251,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> * large folio is fully applicable before use.
> */
> if (!swap_cache_folio_mappable(folio, nr_pages)) {
> - swap_cache_del_folio(folio);
> + if (!is_vswap_entry(entry) || !folio_test_dirty(folio))
> + swap_cache_del_folio(folio);
> goto out_nomap;
> }
[Severity: Low]
Is this folio_test_dirty() guard dead code that contradicts
swap_cache_folio_mappable()?
swap_cache_folio_mappable() already warns via VM_WARN_ON_ONCE_FOLIO() if the
folio is dirty, and a non-anon large swapcache folio cannot be dirty here.
If one ever were, skipping swap_cache_del_folio() would leave it in the swap
cache while the swap PTE stays present, causing the fault to livelock.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=9
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (8 preceding siblings ...)
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
@ 2026-12-03 22:00 ` 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
` (10 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Add support for writing back zswap-backed vswap entries to physical
swap. The mechanism mirrors the existing zswap writeback path, except
the backing physical slot is allocated on demand at writeback time
rather than already being pinned by the PTE.
Zswap shrinker only skips vswap entries when we're out of physical swap
space.
A temporary swapcache folio takes the slot's v1 memsw charge. If
writeback fails, restore the charge and owner of referenced slots under
the cluster lock before removing the folio from swapcache. Keep its
resident charge until the final put.
Link: https://lore.kernel.org/all/sashiko-outbox-181080@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
include/linux/memcontrol.h | 6 ++++
mm/memcontrol.c | 44 +++++++++++++++++++++++
mm/swap.h | 5 +++
mm/swap_state.c | 26 ++++++++++----
mm/zswap.c | 71 ++++++++++++++++++++++++++------------
5 files changed, 123 insertions(+), 29 deletions(-)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index e20183a9c834..338406969510 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1999,6 +1999,7 @@ static inline void mem_cgroup_exit_user_fault(void)
void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci);
void memcg1_swapin(struct folio *folio);
+void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci);
#else
@@ -2010,6 +2011,11 @@ static inline void __memcg1_swapout(struct folio *folio,
static inline void memcg1_swapin(struct folio *folio)
{
}
+
+static inline void memcg1_swapin_abort(struct folio *folio,
+ struct swap_cluster_info *ci)
+{
+}
#endif
#endif /* _LINUX_MEMCONTROL_H */
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index a3d065c594bc..978c47ad4b93 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6040,6 +6040,50 @@ int __init mem_cgroup_init(void)
}
#ifdef CONFIG_SWAP
+#ifdef CONFIG_MEMCG_V1
+/*
+ * memcg1_swapin() moved the slot charge to this temporary folio. Restore
+ * referenced slots before an aborted read drops the folio. The caller must
+ * remove it from swapcache before releasing the cluster lock.
+ */
+void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
+{
+ unsigned int start = swp_cluster_offset(folio->swap);
+ unsigned int end = start + folio_nr_pages(folio);
+ unsigned int i, nr = 0;
+ struct mem_cgroup *memcg;
+ struct obj_cgroup *objcg;
+ unsigned short id;
+
+ if (mem_cgroup_disabled() || !do_memsw_account())
+ return;
+
+ lockdep_assert_held(&ci->lock);
+ for (i = start; i < end; i++)
+ if (__swp_tb_get_count(__swap_table_get(ci, i)))
+ nr++;
+ if (!nr)
+ return;
+
+ objcg = folio_objcg(folio);
+ VM_WARN_ON_ONCE_FOLIO(!objcg, folio);
+ if (!objcg)
+ return;
+
+ guard(rcu)();
+ memcg = obj_cgroup_memcg(objcg);
+ id = mem_cgroup_private_id_get(memcg, nr);
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ if (!mem_cgroup_private_id_is_root(id))
+ page_counter_charge(&memcg->memsw, nr);
+ mod_memcg_state(memcg, MEMCG_SWAP, nr);
+
+ for (i = start; i < end; i++)
+ if (__swp_tb_get_count(__swap_table_get(ci, i)))
+ __swap_cgroup_set(ci, i, 1, id);
+}
+#endif
+
/**
* __mem_cgroup_try_charge_swap - try charging swap space for a folio
* @folio: folio being added to swap
diff --git a/mm/swap.h b/mm/swap.h
index 018250b814bb..0d87de6eaddf 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -377,6 +377,7 @@ bool swap_cache_has_folio(swp_entry_t entry);
struct folio *swap_cache_get_folio(swp_entry_t entry);
void *swap_cache_get_shadow(swp_entry_t entry);
void swap_cache_del_folio(struct folio *folio);
+void swap_cache_del_folio_rollback(struct folio *folio);
struct folio *__swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask,
unsigned long orders, struct vm_fault *vmf,
struct mempolicy *mpol, pgoff_t ilx);
@@ -556,6 +557,10 @@ static inline void swap_cache_del_folio(struct folio *folio)
{
}
+static inline void swap_cache_del_folio_rollback(struct folio *folio)
+{
+}
+
static inline void __swap_cache_del_folio(struct swap_cluster_info *ci,
struct folio *folio, swp_entry_t entry, void *shadow,
bool swapout)
diff --git a/mm/swap_state.c b/mm/swap_state.c
index ee3d8fa5c1f3..f7df517cf66a 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -374,6 +374,20 @@ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio,
__swap_cache_do_del_folio(ci, folio, entry, shadow);
}
+static void swap_cache_del_folio_common(struct folio *folio, bool rollback)
+{
+ struct swap_cluster_info *ci;
+ swp_entry_t entry = folio->swap;
+
+ ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry));
+ if (rollback)
+ memcg1_swapin_abort(folio, ci);
+ __swap_cache_del_folio(ci, folio, entry, NULL, false);
+ swap_cluster_unlock(ci);
+
+ folio_ref_sub(folio, folio_nr_pages(folio));
+}
+
/**
* swap_cache_del_folio - Removes a folio from the swap cache.
* @folio: The folio.
@@ -386,14 +400,12 @@ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio,
*/
void swap_cache_del_folio(struct folio *folio)
{
- struct swap_cluster_info *ci;
- swp_entry_t entry = folio->swap;
-
- ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry));
- __swap_cache_del_folio(ci, folio, entry, NULL, false);
- swap_cluster_unlock(ci);
+ swap_cache_del_folio_common(folio, false);
+}
- folio_ref_sub(folio, folio_nr_pages(folio));
+void swap_cache_del_folio_rollback(struct folio *folio)
+{
+ swap_cache_del_folio_common(folio, true);
}
/**
diff --git a/mm/zswap.c b/mm/zswap.c
index 25d4c3f7785a..54b4ddcf7d73 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1101,11 +1101,12 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct zswap_entry *entry,
swp_entry_t swpentry)
{
- struct xarray *tree;
pgoff_t offset = swp_offset(swpentry);
struct folio *folio;
struct mempolicy *mpol;
struct swap_info_struct *si;
+ swp_entry_t phys = {};
+ bool is_vswap;
int ret = 0;
/* try to allocate swap cache folio */
@@ -1113,12 +1114,7 @@ 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;
- }
-
+ is_vswap = swap_is_vswap(si);
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
NO_INTERLEAVE_INDEX);
@@ -1139,24 +1135,44 @@ 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 zswap tree (or vswap
+ * vtable) 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 != zswap_entry_load(swpentry)) {
ret = -ENOMEM;
goto err;
}
+ if (is_vswap) {
+ /*
+ * Allocate physical backing before decompress so a failure
+ * wastes no work.
+ */
+ phys = folio_realloc_swap(folio);
+ if (!phys.val) {
+ ret = -ENOMEM;
+ goto err;
+ }
+ }
+
if (!zswap_decompress(entry, folio)) {
ret = -EIO;
+ /*
+ * The phys allocation above took the entry out of the vtable.
+ * Restore the zswap entry to the vtable, which also frees the
+ * allocated physical swap space.
+ */
+ if (is_vswap)
+ vswap_zswap_store(swpentry, entry);
goto err;
}
- xa_erase(tree, offset);
+ if (!is_vswap)
+ xa_erase(swap_zswap_tree(swpentry), offset);
count_vm_event(ZSWPWB);
if (entry->objcg)
@@ -1183,12 +1199,15 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
folio_put(folio);
/* start writeback */
- __swap_writeout(ctx, folio, folio->swap);
+ if (is_vswap)
+ __swap_writeout(ctx, folio, phys);
+ else
+ __swap_writeout(ctx, folio, folio->swap);
return 0;
err:
- swap_cache_del_folio(folio);
+ swap_cache_del_folio_rollback(folio);
folio_unlock(folio);
folio_put(folio);
return ret;
@@ -1197,6 +1216,17 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
/*********************************
* shrinker functions
**********************************/
+/*
+ * With vswap disabled, all zswap entries use directly mapped physical swap
+ * entries. With vswap enabled, such entries are rare.
+ *
+ * A zswap-backed vswap entry needs physical swap capacity for writeback.
+ */
+static bool zswap_writeback_possible(void)
+{
+ return !vswap_is_enabled() || get_nr_swap_pages() > 0;
+}
+
/*
* The dynamic shrinker is modulated by the following factors:
*
@@ -1342,7 +1372,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
return 0;
- if (vswap_is_enabled())
+ if (!zswap_writeback_possible())
return 0;
/*
@@ -1428,7 +1458,8 @@ static struct shrinker *zswap_alloc_shrinker(void)
* were scanned but none could be written back, or -ENOENT if @memcg has
* writeback disabled, is a zombie cgroup, or has empty zswap LRUs.
*
- * Also returns -ENOENT when vswap is enabled.
+ * Also returns -ENOENT when vswap is enabled and there is no physical
+ * swap to write back to.
*/
static int shrink_memcg(struct mem_cgroup *memcg)
{
@@ -1439,7 +1470,7 @@ static int shrink_memcg(struct mem_cgroup *memcg)
if (!mem_cgroup_zswap_writeback_enabled(memcg))
return -ENOENT;
- if (vswap_is_enabled())
+ if (!zswap_writeback_possible())
return -ENOENT;
/*
@@ -1474,11 +1505,7 @@ static void shrink_worker(struct work_struct *w)
int ret, failures = 0, attempts = 0;
unsigned long thr;
- /*
- * When vswap is enabled, zswap entries are almost all vswap backed,
- * with no slot to write back to.
- */
- if (vswap_is_enabled())
+ if (!zswap_writeback_possible())
return;
/* Reclaim down to the accept threshold */
@@ -1559,7 +1586,7 @@ static void shrink_worker(struct work_struct *w)
break;
resched:
cond_resched();
- } while (zswap_total_pages() > thr);
+ } while (zswap_total_pages() > thr && zswap_writeback_possible());
}
/*********************************
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: zswap: GFP_NOFS reclaim enters FS swapfile via
shrink_memcg()
- [Medium] mm: zswap: shrinker fails to stop scanning on physical swap
exhaustion
- [Medium] mm: zswap: swap_cache_del_folio_rollback() erases workingset
shadows
--
commit e4671ceb7b90c4ba65b27c95c8ebe7436fb2e260
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: write back vswap zswap entries to physical swap
This commit adds support for writing back zswap-backed vswap entries to
physical swap by allocating physical backing on demand in
zswap_writeback_entry(), updating the zswap shrinkers to run when
physical swap is available, and adding swap_cache_del_folio_rollback()
to restore cgroup v1 swap accounting on writeback failure.
Link: https://lore.kernel.org/all/sashiko-outbox-181080@kernel.org/
[ ... ]
> diff --git a/mm/swap_state.c b/mm/swap_state.c
> index ee3d8fa5c1f3c..f7df517cf66a0 100644
> --- a/mm/swap_state.c
> +++ b/mm/swap_state.c
> @@ -374,6 +374,20 @@ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio,
> __swap_cache_do_del_folio(ci, folio, entry, shadow);
> }
>
> +static void swap_cache_del_folio_common(struct folio *folio, bool rollback)
> +{
> + struct swap_cluster_info *ci;
> + swp_entry_t entry = folio->swap;
> +
> + ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry));
> + if (rollback)
> + memcg1_swapin_abort(folio, ci);
> + __swap_cache_del_folio(ci, folio, entry, NULL, false);
> + swap_cluster_unlock(ci);
> +
> + folio_ref_sub(folio, folio_nr_pages(folio));
> +}
[Severity: Medium]
Does swap_cache_del_folio_rollback() permanently erase the workingset
eviction shadow for entries whose writeback aborts?
zswap_writeback_entry() calls __swap_cache_alloc_folio() before
folio_realloc_swap(), so __swap_cache_alloc() replaces the swap table
shadow with the temporary folio and runs workingset_refault(). When
folio_realloc_swap() then fails, passing NULL as the shadow to
__swap_cache_del_folio() discards the eviction shadow for the entry that
remains in zswap while leaving behind spurious refault accounting, which
degrades thrashing detection and reclaim balance under tight swap.
[ ... ]
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 25d4c3f7785ac..54b4ddcf7d733 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -1342,7 +1372,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
> if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
> return 0;
>
> - if (vswap_is_enabled())
> + if (!zswap_writeback_possible())
> return 0;
[Severity: Medium]
Should zswap_shrinker_scan() also check zswap_writeback_possible() and
return SHRINK_STOP when physical swap is exhausted?
do_shrink_slab() calls count_objects() once and then invokes
scan_objects() in repeated batches, so running out of physical swap
partway through a batch still enters another full scan batch before
returning SHRINK_STOP.
Two related gaps keep the LRU walk running after swap is exhausted:
zswap_writeback_entry() returns -ENOMEM when folio_realloc_swap() fails,
which shrink_memcg_cb() treats as LRU_RETRY rather than stopping the
walk, so remaining entries allocate and roll back swapcache folios,
contend on swap locks in swap_sync_discard(), and rotate cold entries to
the LRU tail. shrink_memcg() also checks zswap_writeback_possible()
only once before its for_each_node_state() loop, continuing to scan
remaining NUMA nodes after swap runs out on an earlier node.
[ ... ]
> @@ -1439,7 +1470,7 @@ static int shrink_memcg(struct mem_cgroup *memcg)
> if (!mem_cgroup_zswap_writeback_enabled(memcg))
> return -ENOENT;
>
> - if (vswap_is_enabled())
> + if (!zswap_writeback_possible())
> return -ENOENT;
[Severity: High]
Can GFP_NOFS direct reclaim now re-enter a filesystem swapfile through
shrink_memcg() and deadlock?
During GFP_NOFS reclaim, may_enter_fs() allows unbacked vswap folios
into swap_writeout(), relying on swap_writeout() to reject
SWAP_OPS_F_REQUIRE_NOFS devices (such as NFS or SMB swapfiles) when
ctx->nofs is set. However, swap_writeout() calls zswap_store() before
that check without propagating ctx->nofs:
shrink_folio_list() [ctx.nofs = true]
-> swap_writeout()
-> zswap_store() [!obj_cgroup_may_zswap()]
-> shrink_memcg() [ctl.io_ctx.nofs = false]
-> zswap_writeback_entry()
-> folio_realloc_swap()
-> __swap_writeout()
Now that shrink_memcg() runs whenever get_nr_swap_pages() > 0, hitting
memory.zswap.max during GFP_NOFS reclaim can allocate a slot on a
SWAP_OPS_F_REQUIRE_NOFS swapfile and submit writeback while the caller
holds filesystem locks.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=10
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (9 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
@ 2026-12-03 22:00 ` 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
` (9 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
A vswap folio can keep physical swapfile space after its swap count
reaches zero, even though its data remains in the swap cache. That space
is scarce and valuable: retaining backing leaves less
capacity for pages that need it. Put-time reclaim is best-effort and
does not reliably recover these slots.
Reclaim such slots from the physical reclaim scanner, once swap is more
than half used (vm_swap_full()), to free physical capacity for new
allocations. Extend the allocation-range scans to reclaim them too.
Drop the physical cluster lock before finding and locking the virtual
folio. Recheck the entry, backing and swap count before freeing the
physical slot. Advance one inspected physical slot at a time so a large
folio cannot skip entries in the requested range.
Link: https://lore.kernel.org/all/sashiko-outbox-181081@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swap_table.h | 12 +++--
mm/swapfile.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++-
mm/vswap.h | 31 ++++++++++++
3 files changed, 165 insertions(+), 5 deletions(-)
diff --git a/mm/swap_table.h b/mm/swap_table.h
index a54cacbc2ade..4f4f3100e5f9 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -31,7 +31,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
- * Vswap: |-------- vswap offset --------|100| - vswap rmap
+ * Vswap: |C|------- vswap offset -------|100| - vswap rmap
* Bad: |------------- 1 -------------|1000| - Bad slot
* HIB: | 0 |------- 1 -------|10000| - Hibernation slot
*
@@ -413,14 +413,18 @@ static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
* On physical clusters, a Vswap-tagged entry stores the offset of the
* vswap entry that owns this physical slot (the reverse map). Only the
* offset is stored; the swap type is implicit (always vswap_si->type,
- * since there is exactly one vswap device).
+ * since there is exactly one vswap device). The top bit is reserved as
+ * a cache-only flag, set when vswap swap_count drops to 0 but the folio
+ * is still in swap cache.
*
- * Vswap: |---- vswap offset ----|100|
+ * Vswap: |C|---- vswap offset ----|100|
+ * C = SWP_RMAP_CACHE_ONLY (the top bit)
*/
#define SWP_TB_VSWAP_MARK_BITS 3
#define SWP_TB_VSWAP_MARK 0b100UL
#define SWP_TB_VSWAP_MARK_MASK ((1UL << SWP_TB_VSWAP_MARK_BITS) - 1)
-#define SWP_RMAP_ENTRY_MASK (~SWP_TB_VSWAP_MARK_MASK)
+#define SWP_RMAP_CACHE_ONLY (1UL << (BITS_PER_LONG - 1))
+#define SWP_RMAP_ENTRY_MASK (~(SWP_RMAP_CACHE_ONLY | SWP_TB_VSWAP_MARK_MASK))
static inline bool swp_tb_is_vswap(unsigned long swp_tb)
{
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 3dca9bd55736..ab65b84a51c7 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -145,6 +145,11 @@ static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = {
.lock = INIT_LOCAL_LOCK(),
};
+static void vswap_mark_cache_only(struct swap_cluster_info *ci,
+ unsigned int ci_off);
+static void vswap_clear_cache_only(struct swap_cluster_info *ci,
+ unsigned int ci_start, int nr);
+
/* May return NULL on invalid type, caller must check for NULL return */
static struct swap_info_struct *swap_type_to_info(int type)
{
@@ -936,6 +941,55 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
return ret;
}
+/*
+ * Try to reclaim a Vswap-tagged physical slot backing a vswap entry.
+ * The physical cluster lock must NOT be held. Returns the backing folio's
+ * page count, negated if the slots could not be reclaimed, or 0 if the
+ * folio could not be shown to own @offset (i.e. there is a race).
+ */
+static int try_to_reclaim_vswap_backing(struct swap_info_struct *si,
+ unsigned long offset,
+ swp_entry_t vswap_entry)
+{
+ swp_entry_t phys_base;
+ struct folio *folio;
+ unsigned int i;
+ int ret;
+
+ folio = swap_cache_get_folio(vswap_entry);
+ if (!folio)
+ return 0;
+
+ if (!folio_trylock(folio)) {
+ /* Unvalidated: the folio may not even own @offset. */
+ folio_put(folio);
+ return 0;
+ }
+
+ if (!folio_matches_swap_entry(folio, vswap_entry)) {
+ folio_unlock(folio);
+ folio_put(folio);
+ return 0;
+ }
+
+ i = vswap_entry.val - folio->swap.val;
+ phys_base = vswap_to_phys(folio->swap);
+ if (!phys_base.val || swp_type(phys_base) != si->type ||
+ swp_offset(phys_base) + i != offset) {
+ folio_unlock(folio);
+ folio_put(folio);
+ return 0;
+ }
+
+ /* The run is ours: skip it all, whether or not the free succeeds. */
+ ret = folio_nr_pages(folio);
+ if (!folio_free_swap(folio))
+ ret = -ret;
+ folio_unlock(folio);
+ folio_put(folio);
+ return ret;
+}
+
/*
* Reclaim drops the ci lock, so the cluster may become unusable (freed or
* stolen by a lower order). @usable will be set to false if that happens.
@@ -948,12 +1002,21 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
unsigned int nr_pages = 1 << order;
unsigned long offset = start, end = start + nr_pages;
unsigned long swp_tb;
+ swp_entry_t vswap_entry;
VM_WARN_ON_ONCE(swap_is_vswap(si));
spin_unlock(&ci->lock);
do {
swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
+ if (swp_tb_is_vswap(swp_tb) &&
+ (swp_tb & SWP_RMAP_CACHE_ONLY)) {
+ vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
+ if (try_to_reclaim_vswap_backing(si, offset,
+ vswap_entry) < 0)
+ break;
+ continue;
+ }
if (swp_tb_get_count(swp_tb))
break;
if (swp_tb_is_folio(swp_tb))
@@ -1002,7 +1065,8 @@ 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;
- if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
+ if ((swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) ||
+ (swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY))) {
/* vswap slots are abundant; never reclaim to reuse one */
if (swap_is_vswap(si) || !vm_swap_full())
return false;
@@ -1233,6 +1297,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
long to_scan = 1;
unsigned long offset, end;
struct swap_cluster_info *ci;
+ swp_entry_t vswap_entry;
unsigned long swp_tb;
int nr_reclaim;
@@ -1255,6 +1320,17 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
offset += abs(nr_reclaim);
continue;
}
+ } else if (swp_tb_is_vswap(swp_tb) &&
+ (swp_tb & SWP_RMAP_CACHE_ONLY)) {
+ vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
+ spin_unlock(&ci->lock);
+ nr_reclaim = try_to_reclaim_vswap_backing(si, offset,
+ vswap_entry);
+ spin_lock(&ci->lock);
+ if (nr_reclaim) {
+ offset += abs(nr_reclaim);
+ continue;
+ }
}
offset++;
}
@@ -1820,6 +1896,8 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
}
/* count will be 0 after put, slot can be reclaimed */
need_reclaim = true;
+ if (swap_is_vswap(si))
+ vswap_mark_cache_only(ci, ci_off);
}
/*
* A count != 1 or cached slot can't be freed. Put its swap
@@ -1926,6 +2004,8 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
goto failed;
}
} while (++ci_off < ci_end);
+ if (swap_is_vswap(si))
+ vswap_clear_cache_only(ci, ci_start, nr);
swap_cluster_unlock(ci);
return 0;
failed:
@@ -2076,6 +2156,51 @@ int folio_alloc_swap(struct folio *folio)
return order ? -E2BIG : -ENOMEM;
}
+static void vswap_mark_cache_only(struct swap_cluster_info *ci,
+ unsigned int ci_off)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ struct swap_cluster_info *pci;
+ swp_entry_t phys;
+ unsigned long vt;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ vt = __vtable_get(ci_dyn, ci_off);
+
+ if (vtable_type(vt) == VSWAP_SWAPFILE) {
+ phys = vtable_to_phys(vt);
+ pci = __swap_entry_to_cluster(phys);
+ swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys));
+ }
+}
+
+/*
+ * Clear the cache-only rmap hint for entries re-referenced from count 0 to 1
+ * (no longer reclaimable), so the physical reclaim scanner skips them.
+ */
+static void vswap_clear_cache_only(struct swap_cluster_info *ci,
+ unsigned int ci_start, int nr)
+{
+ struct swap_cluster_info_dynamic *ci_dyn;
+ struct swap_cluster_info *pci;
+ unsigned long swp_tb, vt;
+ swp_entry_t phys;
+ unsigned int off;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (off = ci_start; off < ci_start + nr; off++) {
+ swp_tb = __swap_table_get(ci, off);
+ if (!swp_tb_is_folio(swp_tb) || swp_tb_get_count(swp_tb) != 1)
+ continue;
+ vt = __vtable_get(ci_dyn, off);
+ if (vtable_type(vt) != VSWAP_SWAPFILE)
+ continue;
+ phys = vtable_to_phys(vt);
+ pci = __swap_entry_to_cluster(phys);
+ swap_rmap_clear_cache_only(pci, swp_cluster_offset(phys));
+ }
+}
+
/**
* __vswap_release_backing - release the backing of a range of vtable slots
* @ci: the locked vswap cluster
diff --git a/mm/vswap.h b/mm/vswap.h
index 080e4bec8280..72b43897faae 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -43,6 +43,37 @@ static inline bool is_vswap_entry(swp_entry_t entry)
return swap_is_vswap(__swap_entry_to_info(entry));
}
+/*
+ * Rmap cache-only helpers for physical cluster Vswap-tagged entries.
+ * SWP_RMAP_CACHE_ONLY records, inline on the physical swap_table entry,
+ * that the backing vswap entry has swap_count == 0 (swap-cache-only, so
+ * reclaimable). The physical reclaim scanner reads it directly instead of
+ * chasing the rmap into the vswap layer and paying the cluster-lookup
+ * indirection.
+ *
+ * Callers hold the vswap cluster lock, not the physical one. The rmap is
+ * only touched while the vtable holds the slot as SWAPFILE, and that
+ * window is opened and closed under the vswap cluster lock, so the
+ * allocator has finished writing the entry by then.
+ */
+static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *ci,
+ unsigned int off)
+{
+ atomic_long_t *table;
+
+ table = rcu_dereference_protected(ci->table, true);
+ atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]);
+}
+
+static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
+ unsigned int off)
+{
+ atomic_long_t *table;
+
+ table = rcu_dereference_protected(ci->table, true);
+ atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]);
+}
+
/*
* Virtual table entry encoding for vswap clusters.
*
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm: folio_realloc_swap() omits SWP_RMAP_CACHE_ONLY on
zero-count slots
- [Medium] mm, swap: try_to_reclaim_vswap_backing() overshoots physical
run when i > 0
- [Medium] mm: unconditional cross-cluster atomic RMWs in swap put/dup
paths
- [Medium] mm: try_to_reclaim_vswap_backing() returns 0 on
folio_trylock() failure
--
commit a8c95eb317dfaa1f58b41d171ec74555600e4670
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: reclaim physical slots backing cache-only vswap entries
This commit adds a SWP_RMAP_CACHE_ONLY flag on physical Vswap-tagged swap
table entries to mark cache-only vswap slots, and extends
swap_reclaim_full_clusters(), cluster_scan_range(), and
cluster_reclaim_range() to reclaim their physical backing when swap is more
than half full.
Link: https://lore.kernel.org/all/sashiko-outbox-181081@kernel.org/
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 3dca9bd557361..ab65b84a51c7d 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -936,6 +941,55 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
> return ret;
> }
>
> +/*
> + * Try to reclaim a Vswap-tagged physical slot backing a vswap entry.
> + * The physical cluster lock must NOT be held. Returns the backing folio's
> + * page count, negated if the slots could not be reclaimed, or 0 if the
> + * folio could not be shown to own @offset (i.e. there is a race).
> + */
> +static int try_to_reclaim_vswap_backing(struct swap_info_struct *si,
> + unsigned long offset,
> + swp_entry_t vswap_entry)
> +{
> + swp_entry_t phys_base;
> + struct folio *folio;
> + unsigned int i;
> + int ret;
> +
> + folio = swap_cache_get_folio(vswap_entry);
> + if (!folio)
> + return 0;
> +
> + if (!folio_trylock(folio)) {
> + /* Unvalidated: the folio may not even own @offset. */
> + folio_put(folio);
> + return 0;
> + }
[Severity: Medium]
Should this return a negative value (such as -1) instead of 0 when
folio_trylock() fails?
Returning 0 conflates a contended locked folio with an already-freed slot.
In cluster_reclaim_range(), the loop only breaks on < 0, so a locked folio
at the start of an order > 0 range does not abort the scan; it retries every
remaining subpage and calls folio_free_swap() on unrelated later folios
(dirtying and evicting them from swap cache) even though the locked slot
causes the final null-check loop to fail.
In swap_reclaim_full_clusters(), returning 0 falls through to offset++,
repeating the unlock, XArray lookup in swap_cache_get_folio(), failed
folio_trylock(), and relock up to SWAPFILE_CLUSTER times on a locked large
folio while holding local_lock(&percpu_swap_cluster.lock).
> + if (!folio_matches_swap_entry(folio, vswap_entry)) {
> + folio_unlock(folio);
> + folio_put(folio);
> + return 0;
> + }
> +
> + i = vswap_entry.val - folio->swap.val;
> + phys_base = vswap_to_phys(folio->swap);
> + if (!phys_base.val || swp_type(phys_base) != si->type ||
> + swp_offset(phys_base) + i != offset) {
> + folio_unlock(folio);
> + folio_put(folio);
> + return 0;
> + }
> +
> + /* The run is ours: skip it all, whether or not the free succeeds. */
> + ret = folio_nr_pages(folio);
> + if (!folio_free_swap(folio))
> + ret = -ret;
> + folio_unlock(folio);
> + folio_put(folio);
> + return ret;
> +}
[Severity: Medium]
Can returning folio_nr_pages(folio) here cause swap_reclaim_full_clusters()
to overshoot past the end of the folio when i > 0?
swap_reclaim_full_clusters() advances by offset += abs(nr_reclaim), which
jumps to swp_offset(phys_base) + i + folio_nr_pages(folio) instead of
swp_offset(phys_base) + folio_nr_pages(folio) and skips i unrelated slots
after the folio. This happens on a partially swapped large vswap folio
where subpages [0, i) still have swap_count > 0 and only subpage i has
SWP_RMAP_CACHE_ONLY set.
Returning folio_nr_pages(folio) - i (negated when folio_free_swap() fails)
would advance to the exact end of the folio's physical run.
[ ... ]
> @@ -2076,6 +2156,51 @@ int folio_alloc_swap(struct folio *folio)
> return order ? -E2BIG : -ENOMEM;
> }
>
> +static void vswap_mark_cache_only(struct swap_cluster_info *ci,
> + unsigned int ci_off)
> +{
> + struct swap_cluster_info_dynamic *ci_dyn;
> + struct swap_cluster_info *pci;
> + swp_entry_t phys;
> + unsigned long vt;
> +
> + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> + vt = __vtable_get(ci_dyn, ci_off);
> +
> + if (vtable_type(vt) == VSWAP_SWAPFILE) {
> + phys = vtable_to_phys(vt);
> + pci = __swap_entry_to_cluster(phys);
> + swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys));
> + }
> +}
[Severity: Medium]
Does folio_realloc_swap() leave SWP_RMAP_CACHE_ONLY unset when attaching
physical backing to vswap slots that already have swap_count == 0?
__swap_cluster_alloc_entries() initializes new physical rmap entries with
SWP_RMAP_CACHE_ONLY cleared, and vswap_mark_cache_only() only sets it on
1-to-0 transitions when virtual_table already holds VSWAP_SWAPFILE.
folio_realloc_swap() installs VSWAP_SWAPFILE under ci->lock without
checking whether any slot is already at count 0.
This happens deterministically for a partially swapped large vswap folio
entering swap_writeout() with some subpages already at swap_count == 0,
and also on order-0 folios when a concurrent swap_put_entries_cluster()
drops swap_count from 1 to 0 before folio_realloc_swap() takes ci->lock:
CPU0 CPU1
swap_writeout()
folio_free_swap() -> false
folio_realloc_swap()
folio_alloc_phys_swap()
swap_put_entries_cluster()
lock(ci->lock)
vswap_mark_cache_only() /* VSWAP_NONE */
count: 1 -> 0
unlock(ci->lock)
lock(ci->lock)
__vtable_set(..., VSWAP_SWAPFILE) <-- SWP_RMAP_CACHE_ONLY left clear
unlock(ci->lock)
Because swap_count is already 0, swap_put_entries_cluster() will not run
on those slots again, leaving their physical backing invisible to the
reclaim scanner while the folio remains in swap cache. Checking for
swap_count == 0 inside the ci->lock loop in folio_realloc_swap() and
calling swap_rmap_mark_cache_only() would close both paths.
[ ... ]
> diff --git a/mm/vswap.h b/mm/vswap.h
> index 080e4bec82802..72b43897faaef 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h
> @@ -43,6 +43,37 @@ static inline bool is_vswap_entry(swp_entry_t entry)
> return swap_is_vswap(__swap_entry_to_info(entry));
> }
>
> +/*
> + * Rmap cache-only helpers for physical cluster Vswap-tagged entries.
> + * SWP_RMAP_CACHE_ONLY records, inline on the physical swap_table entry,
> + * that the backing vswap entry has swap_count == 0 (swap-cache-only, so
> + * reclaimable). The physical reclaim scanner reads it directly instead of
> + * chasing the rmap into the vswap layer and paying the cluster-lookup
> + * indirection.
> + *
> + * Callers hold the vswap cluster lock, not the physical one. The rmap is
> + * only touched while the vtable holds the slot as SWAPFILE, and that
> + * window is opened and closed under the vswap cluster lock, so the
> + * allocator has finished writing the entry by then.
> + */
> +static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *ci,
> + unsigned int off)
> +{
> + atomic_long_t *table;
> +
> + table = rcu_dereference_protected(ci->table, true);
> + atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]);
> +}
> +
> +static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
> + unsigned int off)
> +{
> + atomic_long_t *table;
> +
> + table = rcu_dereference_protected(ci->table, true);
> + atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]);
> +}
[Severity: Medium]
Are unconditional locked atomic RMWs (atomic_long_or() and atomic_long_and())
on remote physical cluster tables needed here?
As noted in the comment above, all writes to a Vswap-tagged slot during its
VSWAP_SWAPFILE window are already serialized by the vswap cluster lock, so
full locked RMWs dirty remote cachelines and extend ci->lock hold times
unnecessarily compared to checking the bit and using plain stores.
On swap-in, shmem_swapin_folio() and do_swap_page() (when
should_try_to_free_swap() is true) call folio_put_swap() and immediately
delete the folio from swap cache, executing up to 512 remote atomic_long_or()
operations per 2MB THP only to overwrite those slots with NULL right after.
On swap-out, vswap_clear_cache_only() adds a second pass in
swap_dup_entries_cluster() and runs atomic_long_and() even when
SWP_RMAP_CACHE_ONLY is already clear. Once commit "mm/swap: make vswap
mandatory" pre-allocates physical backing in folio_alloc_swap(), this hits
every initial swapout page on non-zswap systems.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=11
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 12/20] mm, swap: add debugfs counters for vswap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (10 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
@ 2026-12-03 22:00 ` 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
` (8 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Add /sys/kernel/debug/vswap/ with two counters:
* used: virtual swap slots (pages) currently allocated
* alloc_reject: cumulative pages whose vswap slot allocation failed
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swapfile.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index ab65b84a51c7..afc48d33cc40 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -7,6 +7,7 @@
*/
#include <linux/blkdev.h>
+#include <linux/debugfs.h>
#include <linux/mm.h>
#include <linux/sched/mm.h>
#include <linux/sched/task.h>
@@ -145,6 +146,7 @@ static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = {
.lock = INIT_LOCAL_LOCK(),
};
+static atomic_long_t vswap_alloc_reject = ATOMIC_LONG_INIT(0);
static void vswap_mark_cache_only(struct swap_cluster_info *ci,
unsigned int ci_off);
static void vswap_clear_cache_only(struct swap_cluster_info *ci,
@@ -2072,6 +2074,7 @@ static bool vswap_alloc(struct folio *folio)
this_cpu_write(percpu_vswap_cluster.offset[order], SWAP_ENTRY_INVALID);
local_unlock(&percpu_vswap_cluster.lock);
+ atomic_long_add(folio_nr_pages(folio), &vswap_alloc_reject);
return false;
}
@@ -4847,9 +4850,25 @@ early_param("vswap", early_vswap);
/* vswap does no IO on its own. */
static const struct swap_ops vswap_ops = { };
+static int vswap_used_get(void *data, u64 *val)
+{
+ *val = swap_usage_in_pages(vswap_si);
+ return 0;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(vswap_used_fops, vswap_used_get, NULL, "%llu\n");
+
+static int vswap_alloc_reject_get(void *data, u64 *val)
+{
+ *val = atomic_long_read(&vswap_alloc_reject);
+ return 0;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(vswap_alloc_reject_fops, vswap_alloc_reject_get, NULL,
+ "%llu\n");
+
static int __init vswap_init(void)
{
struct swap_info_struct *si;
+ struct dentry *root;
unsigned long maxpages;
int err;
@@ -4893,6 +4912,12 @@ static int __init vswap_init(void)
mutex_unlock(&swapon_mutex);
vswap_si = si;
+
+ root = debugfs_create_dir("vswap", NULL);
+ debugfs_create_file("used", 0444, root, NULL, &vswap_used_fops);
+ debugfs_create_file("alloc_reject", 0444, root, NULL,
+ &vswap_alloc_reject_fops);
+
pr_info("vswap: created virtual swap device (%lu pages)\n", maxpages);
/* Last: everything above must be visible before routing starts. */
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 12/20] mm, swap: add debugfs counters for vswap
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm, swap: 32-bit signed overflow of vswap_alloc_reject in
debugfs
--
commit 8847f9545c0f863d1bcc1db0a6d34d9021971e3e
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: add debugfs counters for vswap
This commit adds /sys/kernel/debug/vswap/ with "used" and "alloc_reject"
counters tracking currently allocated virtual swap pages and cumulative
failed vswap slot allocations.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index ab65b84a51c7d..afc48d33cc40f 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -145,6 +146,7 @@ static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = {
> .lock = INIT_LOCAL_LOCK(),
> };
>
> +static atomic_long_t vswap_alloc_reject = ATOMIC_LONG_INIT(0);
> static void vswap_mark_cache_only(struct swap_cluster_info *ci,
> unsigned int ci_off);
> static void vswap_clear_cache_only(struct swap_cluster_info *ci,
[ ... ]
> +static int vswap_alloc_reject_get(void *data, u64 *val)
> +{
> + *val = atomic_long_read(&vswap_alloc_reject);
> + return 0;
> +}
> +DEFINE_DEBUGFS_ATTRIBUTE(vswap_alloc_reject_fops, vswap_alloc_reject_get, NULL,
> + "%llu\n");
[Severity: Low]
Can vswap_alloc_reject overflow and sign-extend on 32-bit architectures?
On 32-bit kernels, once vswap_alloc() accumulates 2^31 rejected pages,
atomic_long_read() returns a negative long. Assigning it to u64 *val
sign-extends it (starting at 18446744071562067968 via %llu) before
wrapping to 0 at 2^32.
Using atomic64_t with atomic64_add() and atomic64_read() avoids both the
32-bit wraparound and the sign-extension.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=12
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 13/20] mm, swap: only charge physical swap entries
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (11 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
@ 2026-12-03 22:00 ` 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
` (7 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Zswap-backed and zero-filled pages occupy no swap space, but were charged
against memcg->swap, the memcg's swap cgroup counter, as though they did.
Charge memcg->swap when a vswap entry acquires physical backing rather
than when it is allocated.
With vswap enabled, this changes what the counter means and when the
charge can fail:
* Virtual swap entries count towards memory.swap.current only when
backed by physical swap slots.
* A cgroup can reclaim its anon memory even with memory.swap.max set to 0,
provided zswap is allowed for it.
* The charge can fail at writeback rather than at allocation.
swap_writeout() returns AOP_WRITEPAGE_ACTIVATE and
zswap_writeback_entry() returns -ENOMEM for a cgroup at its limit.
* The zswap shrinker skips such a cgroup rather than walking its LRUs.
Also refactor the swap memcg operations into separate get, record, charge,
uncharge and put helpers, since recording the owner and charging it no
longer happen at the same time.
Direct-mapped physical swap charging is unchanged. In cgroup v1, vswap
entries still count against memsw. MEMCG_SWAP counts physical slots,
including slots backing virtual entries, rather than virtual slots.
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
.../admin-guide/cgroup-v1/memcg_test.rst | 2 +-
Documentation/admin-guide/cgroup-v2.rst | 51 ++---
include/linux/swap.h | 74 ++++++-
mm/memcontrol-v1.c | 43 +++-
mm/memcontrol-v1.h | 19 ++
mm/memcontrol.c | 185 ++++++++++++++----
mm/swapfile.c | 88 +++++++--
mm/zswap.c | 22 ++-
8 files changed, 383 insertions(+), 101 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v1/memcg_test.rst b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
index d9951c319ef5..8d3fcba13450 100644
--- a/Documentation/admin-guide/cgroup-v1/memcg_test.rst
+++ b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
@@ -43,7 +43,7 @@ Please note that implementation details can be changed.
mem_cgroup_uncharge()
Called when a page's refcount goes down to 0.
- mem_cgroup_uncharge_swap()
+ mem_cgroup_dir_phys_swap_free()
Called when swp_entry's refcnt goes down to 0. A charge against swap
disappears.
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index eeeaf635754c..cd965ddeaf9e 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1630,7 +1630,8 @@ The following nested keys are defined.
swapcached
Amount of swap cached in memory. The swapcache is accounted
- against both memory and swap usage.
+ against memory usage, and against swap usage when the swap
+ entry has physical backing.
anon_thp
Amount of memory used in anonymous mappings backed by
@@ -1861,16 +1862,17 @@ The following nested keys are defined.
A read-only single value file which exists on non-root
cgroups.
- The total amount of swap currently being used by the cgroup
- and its descendants.
+ The total amount of physical swap currently being used by the
+ cgroup and its descendants.
memory.swap.high
A read-write single value file which exists on non-root
cgroups. The default is "max".
- Swap usage throttle limit. If a cgroup's swap usage exceeds
- this limit, all its further allocations will be throttled to
- allow userspace to implement custom out-of-memory procedures.
+ Physical swap usage throttle limit. If a cgroup's physical
+ swap usage exceeds this limit, all its further allocations will
+ be throttled to allow userspace to implement custom
+ out-of-memory procedures.
This limit marks a point of no return for the cgroup. It is NOT
designed to manage the amount of swapping a workload does
@@ -1883,8 +1885,9 @@ The following nested keys are defined.
memory.swap.peak
A read-write single value file which exists on non-root cgroups.
- The max swap usage recorded for the cgroup and its descendants since
- the creation of the cgroup or the most recent reset for that FD.
+ The max physical swap usage recorded for the cgroup and its
+ descendants since the creation of the cgroup or the most recent
+ reset for that FD.
A write of any non-empty string to this file resets it to the
current memory usage for subsequent reads through the same
@@ -1894,8 +1897,9 @@ The following nested keys are defined.
A read-write single value file which exists on non-root
cgroups. The default is "max".
- Swap usage hard limit. If a cgroup's swap usage reaches this
- limit, anonymous memory of the cgroup will not be swapped out.
+ Physical swap usage hard limit. If a cgroup's physical swap
+ usage reaches this limit, anonymous memory of the cgroup will
+ not be swapped out to a physical swap device.
memory.swap.events
A read-only flat-keyed file which exists on non-root cgroups.
@@ -1904,23 +1908,24 @@ The following nested keys are defined.
modified event.
high
- The number of times the cgroup's swap usage was over
- the high threshold.
+ The number of times the cgroup's physical swap usage
+ was over the high threshold.
max
- The number of times the cgroup's swap usage was about
- to go over the max boundary and swap allocation
- failed.
+ The number of times the cgroup's physical swap usage
+ was about to go over the max boundary and physical
+ swap allocation failed.
fail
- The number of times swap allocation failed either
- because of running out of swap system-wide or max
- limit.
+ The number of failed physical swap allocation or charge
+ attempts. This includes physical backing allocation for
+ virtual swap entries.
- When reduced under the current usage, the existing swap
- entries are reclaimed gradually and the swap usage may stay
- higher than the limit for an extended period of time. This
- reduces the impact on the workload and memory management.
+ When reduced under the current usage, the existing physical
+ swap entries are reclaimed gradually and the physical swap
+ usage may stay higher than the limit for an extended period of
+ time. This reduces the impact on the workload and memory
+ management.
memory.zswap.current
A read-only single value file which exists on non-root
@@ -1950,8 +1955,6 @@ The following nested keys are defined.
reclaim inefficiency after disabling writeback (because the same
pages might be rejected again and again).
- Note that this is subtly different from setting memory.swap.max to
- 0, as it still allows for pages to be written to the zswap pool.
This setting has no effect if zswap is disabled, and swapping
is allowed unless memory.swap.max is set to 0.
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 0c78121e2887..ae2676627b07 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -535,20 +535,56 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
#endif
#if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP)
-int __mem_cgroup_try_charge_swap(struct folio *folio);
-static inline int mem_cgroup_try_charge_swap(struct folio *folio)
+unsigned short __mem_cgroup_swap_get(struct folio *folio);
+static inline unsigned short mem_cgroup_swap_get(struct folio *folio)
{
if (mem_cgroup_disabled())
return 0;
- return __mem_cgroup_try_charge_swap(folio);
+ return __mem_cgroup_swap_get(folio);
}
-extern void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages);
-static inline void mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
+void __mem_cgroup_swap_fail(unsigned short id);
+static inline void mem_cgroup_swap_fail(unsigned short id)
{
if (mem_cgroup_disabled())
return;
- __mem_cgroup_uncharge_swap(id, nr_pages);
+ __mem_cgroup_swap_fail(id);
+}
+
+int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages);
+static inline int mem_cgroup_swap_charge(unsigned short id,
+ unsigned int nr_pages)
+{
+ if (mem_cgroup_disabled())
+ return 0;
+ return __mem_cgroup_swap_charge(id, nr_pages);
+}
+
+void __mem_cgroup_swap_record(struct folio *folio, unsigned short id);
+static inline void mem_cgroup_swap_record(struct folio *folio,
+ unsigned short id)
+{
+ if (mem_cgroup_disabled())
+ return;
+ __mem_cgroup_swap_record(folio, id);
+}
+
+void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages);
+static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
+ unsigned int nr_pages)
+{
+ if (mem_cgroup_disabled())
+ return;
+ __mem_cgroup_dir_phys_swap_free(id, nr_pages);
+}
+
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages);
+static inline void mem_cgroup_swap_put(unsigned short id,
+ unsigned int nr_pages)
+{
+ if (mem_cgroup_disabled())
+ return;
+ __mem_cgroup_swap_put(id, nr_pages);
}
long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
@@ -556,13 +592,33 @@ long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg);
bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages);
bool mem_cgroup_swap_full(const struct folio *folio);
#else
-static inline int mem_cgroup_try_charge_swap(struct folio *folio)
+static inline unsigned short mem_cgroup_swap_get(struct folio *folio)
+{
+ return 0;
+}
+
+static inline void mem_cgroup_swap_fail(unsigned short id)
+{
+}
+
+static inline int mem_cgroup_swap_charge(unsigned short id,
+ unsigned int nr_pages)
{
return 0;
}
-static inline void mem_cgroup_uncharge_swap(unsigned short id,
- unsigned int nr_pages)
+static inline void mem_cgroup_swap_record(struct folio *folio,
+ unsigned short id)
+{
+}
+
+static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
+ unsigned int nr_pages)
+{
+}
+
+static inline void mem_cgroup_swap_put(unsigned short id,
+ unsigned int nr_pages)
{
}
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index b7f286888507..eb61ab797c38 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -15,6 +15,7 @@
#include "internal.h"
#include "swap.h"
#include "swap_table.h"
+#include "vswap.h"
#include "memcontrol-v1.h"
/* for OOM */
@@ -270,7 +271,9 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
{
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
- unsigned int nr_entries;
+ struct swap_cluster_info_dynamic *ci_dyn;
+ unsigned int nr_entries, nr_swapfile;
+ unsigned int start, i;
unsigned short private_id;
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
@@ -298,10 +301,21 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
* if the ID refers to the root memcg.
*/
nr_entries = folio_nr_pages(folio);
+ start = swp_cluster_offset(folio->swap);
private_id = mem_cgroup_private_id_get(memcg, nr_entries);
- mod_memcg_state(memcg, MEMCG_SWAP, nr_entries);
+ nr_swapfile = nr_entries;
+ if (is_vswap_entry(folio->swap)) {
+ nr_swapfile = 0;
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (i = start; i < start + nr_entries; i++)
+ if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ nr_swapfile++;
+ }
+
+ if (nr_swapfile)
+ mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
- __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, private_id);
+ __swap_cgroup_set(ci, start, nr_entries, private_id);
folio_unqueue_deferred_split(folio);
folio->memcg_data = 0;
@@ -340,8 +354,11 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
void memcg1_swapin(struct folio *folio)
{
struct swap_cluster_info *ci;
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned long nr_pages;
+ unsigned int start, i, nr_swapfile = 0;
unsigned short id;
+ bool is_vswap;
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -366,12 +383,26 @@ void memcg1_swapin(struct folio *folio)
* let's not wait for it. The page already received a
* memory+swap charge, drop the swap entry duplicate.
*/
+ is_vswap = is_vswap_entry(folio->swap);
nr_pages = folio_nr_pages(folio);
+ start = swp_cluster_offset(folio->swap);
ci = swap_cluster_get_and_lock(folio);
- id = __swap_cgroup_clear(ci, swp_cluster_offset(folio->swap),
- nr_pages);
+ id = __swap_cgroup_clear(ci, start, nr_pages);
+ if (id && is_vswap) {
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (i = start; i < start + nr_pages; i++)
+ if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ nr_swapfile++;
+ }
swap_cluster_unlock(ci);
- mem_cgroup_uncharge_swap(id, nr_pages);
+ if (!id)
+ return;
+ if (is_vswap) {
+ mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
+ mem_cgroup_swap_put(id, nr_pages);
+ } else {
+ mem_cgroup_dir_phys_swap_free(id, nr_pages);
+ }
}
#endif
diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h
index 820139657184..3eeac14aeecf 100644
--- a/mm/memcontrol-v1.h
+++ b/mm/memcontrol-v1.h
@@ -124,4 +124,23 @@ static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int
#endif /* CONFIG_MEMCG_V1 */
+#if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP)
+void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
+ unsigned int nr_swapfile);
+static inline void mem_cgroup_swap_uncharge(unsigned short id,
+ unsigned int nr_pages,
+ unsigned int nr_swapfile)
+{
+ if ((!nr_swapfile && !do_memsw_account()) || mem_cgroup_disabled())
+ return;
+ __mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
+}
+#else
+static inline void mem_cgroup_swap_uncharge(unsigned short id,
+ unsigned int nr_pages,
+ unsigned int nr_swapfile)
+{
+}
+#endif
+
#endif /* __MM_MEMCONTROL_V1_H */
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 978c47ad4b93..571f375b2e7f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -66,6 +66,7 @@
#include "internal.h"
#include "swap.h"
#include "swap_table.h"
+#include "vswap.h"
#include <net/sock.h>
#include <net/ip.h>
#include "slab.h"
@@ -6050,18 +6051,27 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
{
unsigned int start = swp_cluster_offset(folio->swap);
unsigned int end = start + folio_nr_pages(folio);
- unsigned int i, nr = 0;
+ unsigned int i, nr = 0, nr_swapfile = 0;
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned short id;
+ bool is_vswap = is_vswap_entry(folio->swap);
if (mem_cgroup_disabled() || !do_memsw_account())
return;
lockdep_assert_held(&ci->lock);
- for (i = start; i < end; i++)
- if (__swp_tb_get_count(__swap_table_get(ci, i)))
- nr++;
+ if (is_vswap)
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (i = start; i < end; i++) {
+ if (!__swp_tb_get_count(__swap_table_get(ci, i)))
+ continue;
+ nr++;
+ if (!is_vswap ||
+ vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ nr_swapfile++;
+ }
if (!nr)
return;
@@ -6076,7 +6086,8 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
if (!mem_cgroup_private_id_is_root(id))
page_counter_charge(&memcg->memsw, nr);
- mod_memcg_state(memcg, MEMCG_SWAP, nr);
+ if (nr_swapfile)
+ mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
for (i = start; i < end; i++)
if (__swp_tb_get_count(__swap_table_get(ci, i)))
@@ -6085,21 +6096,20 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
#endif
/**
- * __mem_cgroup_try_charge_swap - try charging swap space for a folio
+ * __mem_cgroup_swap_get - pin the memcg to account a folio's swap slots to
* @folio: folio being added to swap
*
- * Try to charge @folio's memcg for the swap space at folio->swap.
+ * Pins one live private ID ref per page of @folio. The caller charges and
+ * records using the returned ID, including when its owner is reparented.
*
- * Returns 0 on success, -ENOMEM on failure.
+ * Return: the pinned private ID, or 0 if there is nothing to account. Drop
+ * the pins with __mem_cgroup_swap_put().
*/
-int __mem_cgroup_try_charge_swap(struct folio *folio)
+unsigned short __mem_cgroup_swap_get(struct folio *folio)
{
unsigned int nr_pages = folio_nr_pages(folio);
- struct swap_cluster_info *ci;
- struct page_counter *counter;
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
- unsigned short private_id;
if (do_memsw_account())
return 0;
@@ -6109,38 +6119,85 @@ int __mem_cgroup_try_charge_swap(struct folio *folio)
if (!objcg)
return 0;
- scoped_guard(rcu) {
- memcg = obj_cgroup_memcg(objcg);
- if (!folio_test_swapcache(folio)) {
- memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
- return 0;
- }
+ guard(rcu)();
+ memcg = obj_cgroup_memcg(objcg);
+ if (!folio_test_swapcache(folio)) {
+ memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
+ return 0;
+ }
- private_id = mem_cgroup_private_id_get(memcg, nr_pages);
+ return mem_cgroup_private_id_get(memcg, nr_pages);
+}
- if (!mem_cgroup_private_id_is_root(private_id) &&
- !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
- memcg_memory_event(memcg, MEMCG_SWAP_MAX);
- memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
- mem_cgroup_private_id_put(private_id, nr_pages);
- return -ENOMEM;
- }
- mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
+/**
+ * __mem_cgroup_swap_fail - report a failed physical swap allocation
+ * @id: the pinned private ID of the swap owner (may be 0)
+ */
+void __mem_cgroup_swap_fail(unsigned short id)
+{
+ struct mem_cgroup *memcg;
+
+ if (do_memsw_account() || !id)
+ return;
+
+ guard(rcu)();
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
+}
+
+/**
+ * __mem_cgroup_swap_charge - charge physical swap space
+ * @id: the private ID to charge (may be 0)
+ * @nr_pages: the amount of swap space to charge
+ *
+ * Return: 0 on success, -ENOMEM if memory.swap.max is exceeded.
+ */
+int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages)
+{
+ struct page_counter *counter;
+ struct mem_cgroup *memcg;
+
+ if (do_memsw_account() || !id)
+ return 0;
+
+ guard(rcu)();
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ if (!mem_cgroup_private_id_is_root(id) &&
+ !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
+ memcg_memory_event(memcg, MEMCG_SWAP_MAX);
+ memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
+ return -ENOMEM;
}
+ mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
+ return 0;
+}
+
+/**
+ * __mem_cgroup_swap_record - record the owner of a folio's swap slots
+ * @folio: folio being added to swap
+ * @id: the private ID pinned by __mem_cgroup_swap_get()
+ */
+void __mem_cgroup_swap_record(struct folio *folio, unsigned short id)
+{
+ struct swap_cluster_info *ci;
ci = swap_cluster_get_and_lock(folio);
- __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_pages, private_id);
+ __swap_cgroup_set(ci, swp_cluster_offset(folio->swap),
+ folio_nr_pages(folio), id);
swap_cluster_unlock(ci);
-
- return 0;
}
/**
- * __mem_cgroup_uncharge_swap - uncharge swap space
+ * __mem_cgroup_dir_phys_swap_free - uncharge and drop the reference for
+ * directly mapped physical swap entries
* @id: cgroup id to uncharge
- * @nr_pages: the amount of swap space to uncharge
+ * @nr_pages: number of directly mapped physical swap entries
+ *
+ * This is roughly equivalent to calling mem_cgroup_swap_uncharge()
+ * followed by mem_cgroup_swap_put(). Reuse the resolved owner to avoid
+ * a second lookup.
*/
-void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
+void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages)
{
struct obj_cgroup *objcg;
struct mem_cgroup *memcg;
@@ -6161,6 +6218,61 @@ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
rcu_read_unlock();
}
+/**
+ * __mem_cgroup_swap_uncharge - release cgroup charges for a vswap range
+ * @id: the private ID to uncharge (may be 0)
+ * @nr_pages: total number of pages in the range
+ * @nr_swapfile: number of pages in the range backed by physical swap
+ *
+ * MEMCG_SWAP counts physically backed slots attributed to the swap
+ * owner. Subtract @nr_swapfile from it in both cgroup hierarchies.
+ *
+ * In v1, uncharge @nr_pages from memsw because it covers the whole
+ * range. In v2, uncharge @nr_swapfile from the swap counter because
+ * it covers physical backing.
+ *
+ * In v1, __memcg1_swapout() records the owner when a folio leaves
+ * swapcache, and memcg1_swapin() clears it when the folio returns.
+ * Both changes happen under the cluster lock. Backing release on a
+ * cached folio sees no owner after the swap-in charge transfer.
+ *
+ * In v2, the owner is recorded at swap allocation and remains while
+ * the folio is cached. Backing release balances the charge taken by
+ * folio_realloc_swap().
+ *
+ * The caller releases the private ID references separately.
+ */
+void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
+ unsigned int nr_swapfile)
+{
+ struct mem_cgroup *memcg;
+
+ if (!id)
+ return;
+
+ guard(rcu)();
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ if (!mem_cgroup_private_id_is_root(id)) {
+ if (do_memsw_account())
+ page_counter_uncharge(&memcg->memsw, nr_pages);
+ else if (nr_swapfile)
+ page_counter_uncharge(&memcg->swap, nr_swapfile);
+ }
+ if (nr_swapfile)
+ mod_memcg_state(memcg, MEMCG_SWAP, -nr_swapfile);
+}
+
+/**
+ * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
+ * @id: the pinned private ID
+ * @nr_pages: number of refs to drop
+ */
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
+{
+ guard(rcu)();
+ mem_cgroup_private_id_put(id, nr_pages);
+}
+
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg)
{
long nr_swap_pages = get_nr_swap_pages();
@@ -6203,11 +6315,8 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio)
*/
bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
{
- if (!mem_cgroup_disabled() && !do_memsw_account() &&
- page_counter_margin(&memcg->swap) < nr_pages)
- return false;
-
- return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages;
+ return mem_cgroup_can_vswap(memcg) ||
+ mem_cgroup_get_nr_swap_pages(memcg) >= nr_pages;
}
bool mem_cgroup_swap_full(const struct folio *folio)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index afc48d33cc40..a1eef85b9eb6 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -47,6 +47,7 @@
#include <asm/tlbflush.h>
#include <linux/leafops.h>
+#include "memcontrol-v1.h"
#include "swap_table.h"
#include "vswap.h"
#include "internal.h"
@@ -2110,6 +2111,7 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
int folio_alloc_swap(struct folio *folio)
{
unsigned int order = folio_order(folio);
+ unsigned short memcg_id;
unsigned int size = 1 << order;
bool may_vswap;
@@ -2139,10 +2141,20 @@ int folio_alloc_swap(struct folio *folio)
if (!may_vswap || !vswap_alloc(folio))
folio_alloc_phys_swap(folio);
- /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
- if (unlikely(mem_cgroup_try_charge_swap(folio))) {
- swap_cache_del_folio(folio);
- goto failed;
+ /*
+ * Need to call this even if allocation failed, for MEMCG_SWAP_FAIL.
+ * A vswap entry has no physical swap yet, so only record the memcg.
+ * folio_realloc_swap() charges it once backing is allocated.
+ */
+ memcg_id = mem_cgroup_swap_get(folio);
+ if (memcg_id) {
+ if (!is_vswap_entry(folio->swap) &&
+ unlikely(mem_cgroup_swap_charge(memcg_id, size))) {
+ mem_cgroup_swap_put(memcg_id, size);
+ swap_cache_del_folio(folio);
+ } else {
+ mem_cgroup_swap_record(folio, memcg_id);
+ }
}
if (unlikely(!folio_test_swapcache(folio)))
@@ -2153,7 +2165,7 @@ int folio_alloc_swap(struct folio *folio)
failed:
if (get_nr_swap_pages() <= 0 && !may_vswap)
return -ENOSPC;
- if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
+ if (!may_vswap && mem_cgroup_get_folio_swap_margin(folio) <= 0)
return -ENOMEM;
return order ? -E2BIG : -ENOMEM;
@@ -2224,12 +2236,25 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
unsigned int ci_off;
unsigned long vt;
swp_entry_t phys_first = {};
+ unsigned short batch_id, cur_id;
+ unsigned int batch_nr = 0, batch_nr_swapfile = 0;
lockdep_assert_held(&ci->lock);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ batch_id = __swap_cgroup_get(ci, ci_start);
for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
vt = __vtable_get(ci_dyn, ci_off);
+ cur_id = __swap_cgroup_get(ci, ci_off);
+
+ if (cur_id != batch_id) {
+ mem_cgroup_swap_uncharge(batch_id, batch_nr,
+ batch_nr_swapfile);
+ batch_id = cur_id;
+ batch_nr = 0;
+ batch_nr_swapfile = 0;
+ }
+ batch_nr++;
/* The free helper takes one contiguous run within one cluster. */
if (phys_off_start != phys_off_end &&
@@ -2247,6 +2272,7 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
switch (vtable_type(vt)) {
case VSWAP_SWAPFILE:
+ batch_nr_swapfile++;
if (phys_off_start == phys_off_end) {
phys_first = vtable_to_phys(vt);
phys_off_start = swp_offset(phys_first);
@@ -2278,6 +2304,8 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
phys_off_start % SWAPFILE_CLUSTER,
phys_off_end - phys_off_start);
}
+
+ mem_cgroup_swap_uncharge(batch_id, batch_nr, batch_nr_swapfile);
}
/**
@@ -2368,6 +2396,8 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
struct swap_cluster_info *ci;
struct swap_cluster_info_dynamic *ci_dyn;
unsigned int voff;
+ unsigned long vt;
+ unsigned short memcg_id;
swp_entry_t phys_entry = {};
swp_entry_t pe;
int i, nr = folio_nr_pages(folio);
@@ -2376,18 +2406,33 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
VM_WARN_ON(!is_vswap_entry(vswap_entry));
- phys_entry = vswap_to_phys(vswap_entry);
- if (phys_entry.val)
- return phys_entry;
+ voff = swp_cluster_offset(vswap_entry);
+ ci = __swap_entry_to_cluster(vswap_entry);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+
+ spin_lock(&ci->lock);
+ vt = __vtable_get(ci_dyn, voff);
+ if (vtable_type(vt) == VSWAP_SWAPFILE) {
+ spin_unlock(&ci->lock);
+ return vtable_to_phys(vt);
+ }
+ memcg_id = __swap_cgroup_get(ci, voff);
+ spin_unlock(&ci->lock);
phys_entry = folio_alloc_phys_swap(folio);
- if (!phys_entry.val)
+ if (!phys_entry.val) {
+ mem_cgroup_swap_fail(memcg_id);
return (swp_entry_t){};
+ }
- voff = swp_cluster_offset(vswap_entry);
+ if (mem_cgroup_swap_charge(memcg_id, nr)) {
+ __swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry),
+ __swap_entry_to_cluster(phys_entry),
+ swp_cluster_offset(phys_entry),
+ nr);
+ return (swp_entry_t){};
+ }
- ci = __swap_entry_to_cluster(vswap_entry);
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
spin_lock(&ci->lock);
/*
* Install PHYS backing without freeing any prior contents of the
@@ -2572,6 +2617,18 @@ static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
swap_cluster_unlock(pci);
}
+/*
+ * Release cgroup accounting for freed swap slots. Physical backing of
+ * vswap entries was already uncharged by __vswap_release_backing().
+ */
+static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap)
+{
+ if (is_vswap)
+ mem_cgroup_swap_put(id, nr);
+ else
+ mem_cgroup_dir_phys_swap_free(id, nr);
+}
+
/*
* Free a set of swap slots after their swap count dropped to zero, or will be
* zero after putting the last ref (saves one __swap_cluster_put_entry call).
@@ -2584,10 +2641,11 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
unsigned short batch_id = 0, id_cur;
unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
unsigned int batch_off = ci_off;
+ bool is_vswap = swap_is_vswap(si);
VM_WARN_ON(ci->count < nr_pages);
- if (swap_is_vswap(si))
+ if (is_vswap)
__vswap_release_backing(ci, ci_start, nr_pages);
ci->count -= nr_pages;
@@ -2613,14 +2671,14 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
id_cur = __swap_cgroup_clear(ci, ci_off, 1);
if (batch_id != id_cur) {
if (batch_id)
- mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off);
+ memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
batch_id = id_cur;
batch_off = ci_off;
}
} while (++ci_off < ci_end);
if (batch_id)
- mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off);
+ memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
__swap_cluster_finish_free(si, ci, ci_start, nr_pages);
}
diff --git a/mm/zswap.c b/mm/zswap.c
index 54b4ddcf7d73..f1f0fdf3945d 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1220,11 +1220,17 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
* With vswap disabled, all zswap entries use directly mapped physical swap
* entries. With vswap enabled, such entries are rare.
*
- * A zswap-backed vswap entry needs physical swap capacity for writeback.
+ * A zswap-backed vswap entry needs physical swap capacity for writeback
+ * and cgroup swap margin where applicable. @memcg may be NULL to check
+ * device capacity alone.
*/
-static bool zswap_writeback_possible(void)
+static bool zswap_writeback_possible(struct mem_cgroup *memcg)
{
- return !vswap_is_enabled() || get_nr_swap_pages() > 0;
+ if (!vswap_is_enabled())
+ return true;
+ if (!memcg)
+ return get_nr_swap_pages() > 0;
+ return mem_cgroup_get_nr_swap_pages(memcg) > 0;
}
/*
@@ -1372,7 +1378,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
return 0;
- if (!zswap_writeback_possible())
+ if (!zswap_writeback_possible(memcg))
return 0;
/*
@@ -1459,7 +1465,7 @@ static struct shrinker *zswap_alloc_shrinker(void)
* writeback disabled, is a zombie cgroup, or has empty zswap LRUs.
*
* Also returns -ENOENT when vswap is enabled and there is no physical
- * swap to write back to.
+ * swap for @memcg to write back to.
*/
static int shrink_memcg(struct mem_cgroup *memcg)
{
@@ -1470,7 +1476,7 @@ static int shrink_memcg(struct mem_cgroup *memcg)
if (!mem_cgroup_zswap_writeback_enabled(memcg))
return -ENOENT;
- if (!zswap_writeback_possible())
+ if (!zswap_writeback_possible(memcg))
return -ENOENT;
/*
@@ -1505,7 +1511,7 @@ static void shrink_worker(struct work_struct *w)
int ret, failures = 0, attempts = 0;
unsigned long thr;
- if (!zswap_writeback_possible())
+ if (!zswap_writeback_possible(NULL))
return;
/* Reclaim down to the accept threshold */
@@ -1586,7 +1592,7 @@ static void shrink_worker(struct work_struct *w)
break;
resched:
cond_resched();
- } while (zswap_total_pages() > thr && zswap_writeback_possible());
+ } while (zswap_total_pages() > thr && zswap_writeback_possible(NULL));
}
/*********************************
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 13/20] mm, swap: only charge physical swap entries
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: uncharged zero-filled vswap entries bypass memcg
swap/memory limits
- [Medium] mm, swap: folio_realloc_swap() allocates physical swap before
memcg charge
- [Medium] mm, zswap: shrinker keeps scanning after cgroup hits
memory.swap.max
--
commit a8ec36ff174da79cccd73f1c07e6ae7736522bb5
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: only charge physical swap entries
This patch defers charging memcg->swap for virtual swap entries until
physical swap backing is allocated, and updates the swap cgroup helpers
and zswap shrinker checks accordingly.
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 978c47ad4b933..571f375b2e7f7 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -6203,11 +6315,8 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio)
> */
> bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
> {
> - if (!mem_cgroup_disabled() && !do_memsw_account() &&
> - page_counter_margin(&memcg->swap) < nr_pages)
> - return false;
> -
> - return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages;
> + return mem_cgroup_can_vswap(memcg) ||
> + mem_cgroup_get_nr_swap_pages(memcg) >= nr_pages;
> }
[Severity: High]
Can an unprivileged cgroup with memory.swap.max set to 0 now exhaust host
memory with uncharged vswap cluster metadata?
With mem_cgroup_can_swap() allowing reclaim when mem_cgroup_can_vswap() is
true and folio_alloc_swap() skipping mem_cgroup_swap_charge() for vswap
entries, zero-filled folios hit swap_zeromap_folio_set() in swap_writeout()
and are evicted with 0 charge to memory, swap, or zswap.
Each 512-slot cluster allocated by vswap_alloc_cluster() takes ~9.3 KiB of
uncharged, unreclaimable kernel memory (GFP_ATOMIC | __GFP_NOWARN without
__GFP_ACCOUNT), capped only at REFCOUNT_MAX pages in vswap_init().
An unprivileged task using memfd_create() (or anonymous mmap() with
MADV_PAGEOUT) to swap out zero-filled pages can pin gigabytes of uncharged
kernel memory, bypassing memory.max, memory.swap.max, and memory.zswap.max
and triggering host OOM kills of unrelated workloads.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index afc48d33cc40f..a1eef85b9eb6c 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -2376,18 +2406,33 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
> VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
> VM_WARN_ON(!is_vswap_entry(vswap_entry));
>
> - phys_entry = vswap_to_phys(vswap_entry);
> - if (phys_entry.val)
> - return phys_entry;
> + voff = swp_cluster_offset(vswap_entry);
> + ci = __swap_entry_to_cluster(vswap_entry);
> + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> +
> + spin_lock(&ci->lock);
> + vt = __vtable_get(ci_dyn, voff);
> + if (vtable_type(vt) == VSWAP_SWAPFILE) {
> + spin_unlock(&ci->lock);
> + return vtable_to_phys(vt);
> + }
> + memcg_id = __swap_cgroup_get(ci, voff);
> + spin_unlock(&ci->lock);
>
> phys_entry = folio_alloc_phys_swap(folio);
> - if (!phys_entry.val)
> + if (!phys_entry.val) {
> + mem_cgroup_swap_fail(memcg_id);
> return (swp_entry_t){};
> + }
>
> - voff = swp_cluster_offset(vswap_entry);
> + if (mem_cgroup_swap_charge(memcg_id, nr)) {
> + __swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry),
> + __swap_entry_to_cluster(phys_entry),
> + swp_cluster_offset(phys_entry),
> + nr);
> + return (swp_entry_t){};
> + }
[Severity: Medium]
Should mem_cgroup_swap_charge() run before folio_alloc_phys_swap() here?
Both memcg_id and nr are already known after dropping ci->lock. When a
cgroup at memory.swap.max fails zswap_store() in swap_writeout(), each
rejected folio allocates physical swap slots and immediately rolls them
back in __swap_cluster_free_phys_backing().
If the allocation isolated an empty cluster, dropping pci->count back to 0
enters free_cluster(), scheduling a 2 MiB discard on SWP_PAGE_DISCARD
devices (or freeing pci->table via RCU) and forcing the next folio back
through swap_alloc_slow().
Charging memcg->swap first avoids touching shared physical swap state when
the cgroup has no swap margin.
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 54b4ddcf7d733..f1f0fdf3945dc 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -1372,7 +1378,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
> if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
> return 0;
>
> - if (!zswap_writeback_possible())
> + if (!zswap_writeback_possible(memcg))
> return 0;
>
> /*
> @@ -1459,7 +1465,7 @@ static struct shrinker *zswap_alloc_shrinker(void)
> * writeback disabled, is a zombie cgroup, or has empty zswap LRUs.
> *
> * Also returns -ENOENT when vswap is enabled and there is no physical
> - * swap to write back to.
> + * swap for @memcg to write back to.
> */
> static int shrink_memcg(struct mem_cgroup *memcg)
> {
> @@ -1470,7 +1476,7 @@ static int shrink_memcg(struct mem_cgroup *memcg)
> if (!mem_cgroup_zswap_writeback_enabled(memcg))
> return -ENOENT;
>
> - if (!zswap_writeback_possible())
> + if (!zswap_writeback_possible(memcg))
> return -ENOENT;
[Severity: Medium]
Can the zswap shrinker keep scanning entries after a cgroup exhausts its
swap limit?
do_shrink_slab() calls zswap_shrinker_count() once and then calls
zswap_shrinker_scan() in SHRINK_BATCH chunks until SHRINK_STOP is returned,
but zswap_shrinker_scan() does not check zswap_writeback_possible(sc->memcg).
In addition, shrink_memcg_cb() returns LRU_RETRY rather than LRU_STOP when
zswap_writeback_entry() fails with -ENOMEM, and shrink_memcg() only checks
zswap_writeback_possible(memcg) once before iterating over all online NUMA
nodes.
Once a cgroup hits memory.swap.max partway through shrinking (or has it
lowered between count and scan), the shrinker keeps walking remaining
batches or nodes, rotating cold entries to the LRU tail and inflating
MEMCG_SWAP_MAX, MEMCG_SWAP_FAIL, and zswap_reject_reclaim_fail.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=13
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (12 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
@ 2026-12-03 22:00 ` Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
` (6 subsequent siblings)
20 siblings, 0 replies; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Stop allocating a memcg table for every physical swap cluster that only
ever holds vswap backings. The table costs SWAPFILE_CLUSTER *
sizeof(unsigned short) per cluster, 1 KB per 2 MB of swap on a 64-bit
kernel with 4 KB pages.
Such clusters never have their memcg_table read or written: vswap-layer
charging records on the vswap cluster's table, not the physical one.
Allocate eagerly for each vswap cluster and each new direct-use physical
cluster. A physical cluster otherwise allocates its owner table on the
first direct-use slot and skips it if all slots hold vswap backings.
If the existing cluster has no owner table, its locked swapcache folio
pins the new slots while allocation sleeps. Free the reservation if
allocation fails. Hibernation prepares the table before publishing a
raw slot.
Link: https://lore.kernel.org/all/sashiko-outbox-181084@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swapfile.c | 127 +++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 104 insertions(+), 23 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index a1eef85b9eb6..30c1eed8adfe 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -7,6 +7,7 @@
*/
#include <linux/blkdev.h>
+#include <linux/cleanup.h>
#include <linux/debugfs.h>
#include <linux/mm.h>
#include <linux/sched/mm.h>
@@ -481,12 +482,13 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
* 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,
+ struct swap_cluster_info *ci, bool direct,
gfp_t gfp, bool locked)
{
struct swap_table *table = NULL;
#ifdef CONFIG_MEMCG
struct swap_memcg_table *memcg_table = NULL;
+ bool vswap = si && swap_is_vswap(si);
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
unsigned long *zero_bitmap = NULL;
@@ -505,8 +507,19 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
goto lock;
#ifdef CONFIG_MEMCG
- if (!mem_cgroup_disabled()) {
- memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+ /*
+ * Backing-only clusters keep quota owners on the virtual cluster.
+ * Physical owner metadata is prepared on demand for direct use
+ * and physical-slot statistics.
+ */
+ if ((!vswap_is_enabled() || vswap || direct) &&
+ !mem_cgroup_disabled()) {
+ gfp_t memcg_gfp = gfp;
+
+ if (direct && !vswap)
+ memcg_gfp &= ~__GFP_NOMEMALLOC;
+
+ memcg_table = kzalloc_obj(*ci->memcg_table, memcg_gfp);
if (!memcg_table)
goto lock;
}
@@ -601,7 +614,7 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
*/
static struct swap_cluster_info *
swap_cluster_populate(struct swap_info_struct *si,
- struct swap_cluster_info *ci)
+ struct swap_cluster_info *ci, bool direct)
{
int ret;
@@ -616,7 +629,7 @@ swap_cluster_populate(struct swap_info_struct *si,
lockdep_assert_held(&si->global_cluster_lock);
lockdep_assert_held(&ci->lock);
- if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ if (!swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
__GFP_NOWARN, true))
return ci;
@@ -630,7 +643,7 @@ swap_cluster_populate(struct swap_info_struct *si,
spin_unlock(&si->global_cluster_lock);
local_unlock(&percpu_swap_cluster.lock);
- ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ ret = swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
GFP_KERNEL, false);
/*
@@ -713,7 +726,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
* this returns NULL for an non-empty list.
*/
static struct swap_cluster_info *isolate_lock_cluster(
- struct swap_info_struct *si, struct list_head *list)
+ struct swap_info_struct *si, struct list_head *list, bool direct)
{
struct swap_cluster_info *ci, *found = NULL;
u8 flags = CLUSTER_FLAG_NONE;
@@ -745,7 +758,7 @@ static struct swap_cluster_info *isolate_lock_cluster(
if (found && flags == CLUSTER_FLAG_FREE) {
VM_WARN_ON_ONCE(list != &si->free_clusters);
VM_WARN_ON_ONCE(cluster_table_is_alloced(found));
- return swap_cluster_populate(si, found);
+ return swap_cluster_populate(si, found, direct);
}
return found;
@@ -920,7 +933,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
need_table = !ci->count;
/* Need to allocate swap table first for initial bad slot marking. */
if (need_table) {
- ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
+ ret = swap_cluster_alloc_table(alloc_si, ci, false, GFP_KERNEL, false);
if (ret)
goto out;
} else {
@@ -1173,7 +1186,9 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
/* Try use a new cluster for current CPU and allocate from it. */
static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
struct swap_cluster_info *ci,
- struct folio *folio, unsigned long offset)
+ struct folio *folio,
+ unsigned long offset,
+ bool *nomem)
{
unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
@@ -1202,6 +1217,19 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
if (!ret)
continue;
}
+#ifdef CONFIG_MEMCG
+ /* Hibernation needs owner metadata before publishing a raw slot. */
+ if (vswap_is_enabled() && !folio &&
+ !mem_cgroup_disabled() && !ci->memcg_table) {
+ ci->memcg_table = kzalloc_obj(*ci->memcg_table,
+ GFP_ATOMIC | __GFP_NOWARN);
+ if (!ci->memcg_table) {
+ if (nomem)
+ *nomem = true;
+ goto out;
+ }
+ }
+#endif
if (!__swap_cluster_alloc_entries(si, ci, folio, offset % SWAPFILE_CLUSTER))
break;
found = offset;
@@ -1211,7 +1239,15 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
break;
}
out:
- relocate_cluster(si, ci);
+ /*
+ * On a discard-capable device, relocating a cluster whose memcg_table
+ * allocation failed queues a discard for slots that were never used,
+ * which folio_alloc_phys_swap() reads as progress and retries on.
+ */
+ if (nomem && *nomem && !ci->count)
+ __free_cluster(si, ci);
+ else
+ relocate_cluster(si, ci);
swap_cluster_unlock(ci);
if (swap_is_vswap(si)) {
this_cpu_write(percpu_vswap_cluster.offset[order], next);
@@ -1232,18 +1268,25 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
bool scan_all)
{
unsigned int found = SWAP_ENTRY_INVALID;
+ bool nomem = false;
+ bool direct = !folio || !folio_test_swapcache(folio);
+ /*
+ * In rare cases alloc_swap_scan_cluster() can fail due to
+ * memcg_table allocation failure. Short-circuit to avoid looping
+ * over the list indefinitely.
+ */
do {
- struct swap_cluster_info *ci = isolate_lock_cluster(si, list);
+ struct swap_cluster_info *ci = isolate_lock_cluster(si, list, direct);
unsigned long offset;
if (!ci)
break;
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset, &nomem);
if (found)
break;
- } while (scan_all);
+ } while (scan_all && !nomem);
return found;
}
@@ -1268,7 +1311,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
- if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
+ if (swap_cluster_alloc_table(si, ci, false, GFP_ATOMIC | __GFP_NOWARN, true)) {
spin_unlock(&ci->lock);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1292,7 +1335,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
}
offset = cluster_offset(si, ci);
- return alloc_swap_scan_cluster(si, ci, folio, offset);
+ return alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
}
static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
@@ -1307,7 +1350,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
if (force)
to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER;
- while ((ci = isolate_lock_cluster(si, &si->full_clusters))) {
+ while ((ci = isolate_lock_cluster(si, &si->full_clusters, false))) {
offset = cluster_offset(si, ci);
end = min(si->max, offset + SWAPFILE_CLUSTER);
to_scan--;
@@ -1394,7 +1437,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset,
+ NULL);
} else {
swap_cluster_unlock(ci);
}
@@ -1434,7 +1478,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (order < PMD_ORDER) {
/*
* Scan only one fragment cluster is good enough. Order 0
- * allocation will surely success, and large allocation
+ * allocation rarely fails, and large allocation
* failure is not critical. Scanning one cluster still
* keeps the list rotated and reclaimed (for clean swap cache).
*/
@@ -1449,7 +1493,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
/* Order 0 stealing from higher order */
for (int o = 1; o < SWAP_NR_ORDERS; o++) {
/*
- * Clusters here have at least one usable slots and can't fail order 0
+ * Clusters here have at least one usable slots and rarely fail order 0
* allocation, but reclaim may drop si->lock and race with another user.
*/
found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true);
@@ -1670,7 +1714,7 @@ static swp_entry_t swap_alloc_fast(struct folio *folio)
if (ci && cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
} else if (ci) {
swap_cluster_unlock(ci);
}
@@ -2058,7 +2102,8 @@ static bool vswap_alloc(struct folio *folio)
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);
+ alloc_swap_scan_cluster(vswap_si, ci, folio, offset,
+ NULL);
} else if (ci) {
swap_cluster_unlock(ci);
}
@@ -2079,8 +2124,37 @@ static bool vswap_alloc(struct folio *folio)
return false;
}
+#ifdef CONFIG_MEMCG
+static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
+{
+ struct swap_cluster_info *ci = __swap_entry_to_cluster(entry);
+ struct swap_memcg_table *table __free(kfree) = NULL;
+
+ if (mem_cgroup_disabled() || READ_ONCE(ci->memcg_table))
+ return 0;
+
+ /* The caller's locked folio pins the slots while allocation sleeps. */
+ table = kzalloc_obj(*table, GFP_ATOMIC | __GFP_NOWARN);
+ if (!table)
+ table = kzalloc_obj(*table, __GFP_HIGH | GFP_KERNEL);
+ guard(spinlock)(&ci->lock);
+ if (ci->memcg_table)
+ return 0;
+ if (!table)
+ return -ENOMEM;
+ WRITE_ONCE(ci->memcg_table, no_free_ptr(table));
+ return 0;
+}
+#else
+static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
+{
+ return 0;
+}
+#endif
+
static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
{
+ bool direct = !folio_test_swapcache(folio);
swp_entry_t entry;
again:
@@ -2092,7 +2166,13 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
if (!entry.val && !folio_order(folio) && swap_sync_discard())
goto again;
+ if (!entry.val)
+ return entry;
+ if (direct && swap_cluster_alloc_memcg_table(entry)) {
+ swap_cache_del_folio(folio);
+ return (swp_entry_t){};
+ }
return entry;
}
@@ -2895,7 +2975,8 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
if (pcp_si == si && pcp_offset) {
ci = swap_cluster_lock(si, pcp_offset);
if (cluster_is_usable(ci, 0))
- offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
+ offset = alloc_swap_scan_cluster(si, ci, NULL,
+ pcp_offset, NULL);
else
swap_cluster_unlock(ci);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (13 preceding siblings ...)
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 ` 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
` (5 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Add nr_vswap to /proc/vmstat and vswap to cgroup's memory.stat to report
virtual swap usage.
These counters help diagnose swapping issues. Userspace can also use
vswap as a component of a workload's logical memory footprint when
making policy decisions.
Count cgroup virtual slots while they hold the owner ID. Restore the
statistic with that owner when failed writeback rolls back a swap-in.
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
.../admin-guide/cgroup-v1/memory.rst | 1 +
Documentation/admin-guide/cgroup-v2.rst | 3 +++
include/linux/memcontrol.h | 1 +
include/linux/swap.h | 10 ++++----
include/linux/vmstat.h | 3 +++
mm/memcontrol-v1.c | 5 +++-
mm/memcontrol.c | 24 +++++++++++++++++--
mm/swapfile.c | 9 +++++--
mm/vmstat.c | 6 +++++
9 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v1/memory.rst b/Documentation/admin-guide/cgroup-v1/memory.rst
index 7d2a44af52c9..732b379f2612 100644
--- a/Documentation/admin-guide/cgroup-v1/memory.rst
+++ b/Documentation/admin-guide/cgroup-v1/memory.rst
@@ -555,6 +555,7 @@ memory.stat file includes following statistics:
cgroup.
swap # of bytes of swap usage
swapcached # of bytes of swap cached in memory
+ vswap # of bytes of virtual swap usage
dirty # of bytes that are waiting to get written back to the disk.
writeback # of bytes of file/anon cache that are queued for syncing to
disk.
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index cd965ddeaf9e..b17789de7557 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1633,6 +1633,9 @@ The following nested keys are defined.
against memory usage, and against swap usage when the swap
entry has physical backing.
+ vswap
+ Amount of virtual swap usage.
+
anon_thp
Amount of memory used in anonymous mappings backed by
transparent hugepages
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 338406969510..3b43f20c9005 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -41,6 +41,7 @@ enum memcg_stat_item {
MEMCG_ZSWAP_B,
MEMCG_ZSWAPPED,
MEMCG_ZSWAP_INCOMP,
+ MEMCG_VSWAP,
MEMCG_NR_STAT,
};
diff --git a/include/linux/swap.h b/include/linux/swap.h
index ae2676627b07..ae635d5d0bab 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -371,6 +371,7 @@ bool current_is_kswapd(void);
#ifdef CONFIG_SWAP
bool mem_cgroup_can_vswap(struct mem_cgroup *memcg);
+unsigned long vswap_usage_in_pages(void);
int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
unsigned long nr_pages, sector_t start_block);
@@ -578,13 +579,14 @@ static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
__mem_cgroup_dir_phys_swap_free(id, nr_pages);
}
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages);
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
+ bool is_vswap);
static inline void mem_cgroup_swap_put(unsigned short id,
- unsigned int nr_pages)
+ unsigned int nr_pages, bool is_vswap)
{
if (mem_cgroup_disabled())
return;
- __mem_cgroup_swap_put(id, nr_pages);
+ __mem_cgroup_swap_put(id, nr_pages, is_vswap);
}
long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
@@ -618,7 +620,7 @@ static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
}
static inline void mem_cgroup_swap_put(unsigned short id,
- unsigned int nr_pages)
+ unsigned int nr_pages, bool is_vswap)
{
}
diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
index 9df3587a2968..93f0e4a5f076 100644
--- a/include/linux/vmstat.h
+++ b/include/linux/vmstat.h
@@ -33,6 +33,9 @@ enum vm_stat_item {
NR_DIRTY_BG_THRESHOLD,
NR_MEMMAP_PAGES, /* page metadata allocated through buddy allocator */
NR_MEMMAP_BOOT_PAGES, /* page metadata allocated through boot allocator */
+#ifdef CONFIG_SWAP
+ NR_VSWAP, /* pages occupying a virtual swap slot */
+#endif
NR_VM_STAT_ITEMS,
};
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index eb61ab797c38..1a55c168c3eb 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -305,6 +305,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
private_id = mem_cgroup_private_id_get(memcg, nr_entries);
nr_swapfile = nr_entries;
if (is_vswap_entry(folio->swap)) {
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr_entries);
nr_swapfile = 0;
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
for (i = start; i < start + nr_entries; i++)
@@ -399,7 +400,7 @@ void memcg1_swapin(struct folio *folio)
return;
if (is_vswap) {
mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
- mem_cgroup_swap_put(id, nr_pages);
+ mem_cgroup_swap_put(id, nr_pages, true);
} else {
mem_cgroup_dir_phys_swap_free(id, nr_pages);
}
@@ -1868,6 +1869,7 @@ static const unsigned int memcg1_stats[] = {
#ifdef CONFIG_SWAP
MEMCG_SWAP,
NR_SWAPCACHE,
+ MEMCG_VSWAP,
#endif
};
@@ -1886,6 +1888,7 @@ static const char *const memcg1_stat_names[] = {
#ifdef CONFIG_SWAP
"swap",
"swapcached",
+ "vswap",
#endif
};
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 571f375b2e7f..5811ad3889df 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -458,6 +458,9 @@ static const unsigned int memcg_stat_items[] = {
MEMCG_ZSWAP_B,
MEMCG_ZSWAPPED,
MEMCG_ZSWAP_INCOMP,
+#ifdef CONFIG_SWAP
+ MEMCG_VSWAP,
+#endif
};
#define NR_MEMCG_NODE_STAT_ITEMS ARRAY_SIZE(memcg_node_stat_items)
@@ -1620,6 +1623,7 @@ static const struct memory_stat memory_stats[] = {
{ "file_writeback", NR_WRITEBACK },
#ifdef CONFIG_SWAP
{ "swapcached", NR_SWAPCACHE },
+ { "vswap", MEMCG_VSWAP },
#endif
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
{ "anon_thp", NR_ANON_THPS },
@@ -6088,6 +6092,8 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
page_counter_charge(&memcg->memsw, nr);
if (nr_swapfile)
mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
+ if (is_vswap_entry(folio->swap))
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr);
for (i = start; i < end; i++)
if (__swp_tb_get_count(__swap_table_get(ci, i)))
@@ -6110,6 +6116,7 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
unsigned int nr_pages = folio_nr_pages(folio);
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
+ unsigned short id;
if (do_memsw_account())
return 0;
@@ -6126,7 +6133,12 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
return 0;
}
- return mem_cgroup_private_id_get(memcg, nr_pages);
+ id = mem_cgroup_private_id_get(memcg, nr_pages);
+ if (is_vswap_entry(folio->swap)) {
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
+ }
+ return id;
}
/**
@@ -6266,10 +6278,18 @@ void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
* __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
* @id: the pinned private ID
* @nr_pages: number of refs to drop
+ * @is_vswap: release virtual-swap accounting along with the refs
*/
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
+ bool is_vswap)
{
+ struct mem_cgroup *memcg;
+
guard(rcu)();
+ if (is_vswap) {
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
+ }
mem_cgroup_private_id_put(id, nr_pages);
}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 30c1eed8adfe..b0865d617512 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2230,7 +2230,7 @@ int folio_alloc_swap(struct folio *folio)
if (memcg_id) {
if (!is_vswap_entry(folio->swap) &&
unlikely(mem_cgroup_swap_charge(memcg_id, size))) {
- mem_cgroup_swap_put(memcg_id, size);
+ mem_cgroup_swap_put(memcg_id, size, false);
swap_cache_del_folio(folio);
} else {
mem_cgroup_swap_record(folio, memcg_id);
@@ -2704,7 +2704,7 @@ static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap)
{
if (is_vswap)
- mem_cgroup_swap_put(id, nr);
+ mem_cgroup_swap_put(id, nr, true);
else
mem_cgroup_dir_phys_swap_free(id, nr);
}
@@ -4989,6 +4989,11 @@ early_param("vswap", early_vswap);
/* vswap does no IO on its own. */
static const struct swap_ops vswap_ops = { };
+unsigned long vswap_usage_in_pages(void)
+{
+ return vswap_is_enabled() ? swap_usage_in_pages(vswap_si) : 0;
+}
+
static int vswap_used_get(void *data, u64 *val)
{
*val = swap_usage_in_pages(vswap_si);
diff --git a/mm/vmstat.c b/mm/vmstat.c
index c6beac6be2bc..73fcd38e09d7 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1320,6 +1320,9 @@ const char * const vmstat_text[] = {
[I(NR_DIRTY_BG_THRESHOLD)] = "nr_dirty_background_threshold",
[I(NR_MEMMAP_PAGES)] = "nr_memmap_pages",
[I(NR_MEMMAP_BOOT_PAGES)] = "nr_memmap_boot_pages",
+#ifdef CONFIG_SWAP
+ [I(NR_VSWAP)] = "nr_vswap",
+#endif
#undef I
#if defined(CONFIG_VM_EVENT_COUNTERS)
@@ -1936,6 +1939,9 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
v + NR_DIRTY_THRESHOLD);
v[NR_MEMMAP_PAGES] = atomic_long_read(&nr_memmap_pages);
v[NR_MEMMAP_BOOT_PAGES] = atomic_long_read(&nr_memmap_boot_pages);
+#ifdef CONFIG_SWAP
+ v[NR_VSWAP] = vswap_usage_in_pages();
+#endif
v += NR_VM_STAT_ITEMS;
#ifdef CONFIG_VM_EVENT_COUNTERS
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: missing cgroup v1 swap-in rollback in
swap_cache_del_folio() callers
- [Low] mm: redundant mem_cgroup_private_ids XArray lookup in
__mem_cgroup_swap_put()
- [Low] mm: unnecessary mem_cgroup_private_ids XArray lookup in
__mem_cgroup_swap_get()
--
commit d0f68463326459f383de74db00757c571c68f75f
Author: Nhat Pham <nphamcs@gmail.com>
mm, swap: add vmstat and memcg counters for vswap
This commit adds nr_vswap to /proc/vmstat and vswap to cgroup v1 and v2
memory.stat to report virtual swap usage. It tracks MEMCG_VSWAP while
virtual swap slots hold a cgroup owner ID and restores it in
memcg1_swapin_abort().
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 571f375b2e7f7..5811ad3889dff 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
[ ... ]
> @@ -6088,6 +6092,8 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
> page_counter_charge(&memcg->memsw, nr);
> if (nr_swapfile)
> mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
> + if (is_vswap_entry(folio->swap))
> + mod_memcg_state(memcg, MEMCG_VSWAP, nr);
>
> for (i = start; i < end; i++)
> if (__swp_tb_get_count(__swap_table_get(ci, i)))
[Severity: High]
Does memcg1_swapin_abort() miss the other paths that discard a clean
swapcache folio via swap_cache_del_folio()?
memcg1_swapin_abort() only runs from swap_cache_del_folio_rollback(), whose
sole caller is zswap_writeback_entry(). When do_swap_page() drops a freshly
swapped-in large folio after swap_cache_folio_mappable() fails (for example,
racing with MADV_DONTNEED), it calls swap_cache_del_folio() instead.
Because memcg1_swapin() already cleared the owner IDs and uncharged memsw,
MEMCG_SWAP, and MEMCG_VSWAP for the whole folio, skipping rollback leaves
remaining referenced slots with owner ID 0 and permanently drops their
cgroup v1 accounting while they stay counted in nr_vswap.
Repeating this lets an unprivileged cgroup v1 workload bypass
memory.memsw.limit_in_bytes and consume unbounded global swap or vswap
space. unuse_pte(), shmem_swapin_folio(), and me_swapcache_clean() have the
same issue and should also use swap_cache_del_folio_rollback().
[ ... ]
> @@ -6126,7 +6133,12 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
> return 0;
> }
>
> - return mem_cgroup_private_id_get(memcg, nr_pages);
> + id = mem_cgroup_private_id_get(memcg, nr_pages);
> + if (is_vswap_entry(folio->swap)) {
> + memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
> + mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
> + }
> + return id;
> }
[Severity: Low]
Is the obj_cgroup_from_private_id() lookup needed here?
__mem_cgroup_swap_get() already has memcg, and mod_memcg_state() walks past
dying cgroups via get_non_dying_memcg_start() to the same non-dying
ancestor. Calling mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages) directly
avoids a global XArray lookup on every vswap allocation during reclaim.
[ ... ]
> @@ -6266,10 +6278,18 @@ void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
> * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
> * @id: the pinned private ID
> * @nr_pages: number of refs to drop
> + * @is_vswap: release virtual-swap accounting along with the refs
> */
> -void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
> +void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
> + bool is_vswap)
> {
> + struct mem_cgroup *memcg;
> +
> guard(rcu)();
> + if (is_vswap) {
> + memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
> + mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
> + }
> mem_cgroup_private_id_put(id, nr_pages);
> }
[Severity: Low]
Can this avoid looking up id in mem_cgroup_private_ids twice when is_vswap
is true?
mem_cgroup_private_id_put() repeats obj_cgroup_from_private_id(id) under
ci->lock on every vswap free batch via memcg_swap_free(). Saving objcg and
calling __mem_cgroup_private_id_put(objcg, id, nr_pages) directly, as
__mem_cgroup_dir_phys_swap_free() does, avoids the second XArray lookup.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=15
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 16/20] mm/swap: make vswap mandatory
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (14 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
@ 2026-12-03 22:00 ` 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
` (4 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Optional vswap uses physical entries directly when a folio cannot use
zswap or virtual slot allocation fails. Give ordinary swapout a virtual
slot even when it needs disk. For folios ineligible for zswap, reserve
disk backing before publishing the entry in a PTE or shmem mapping.
This lets reclaim split a large folio if a physical run is unavailable.
Hibernation continues to reserve physical slots directly.
Initialize the virtual device whenever CONFIG_SWAP is enabled and remove
the separate vswap boot controls, so ordinary swapping can use it with
the default boot settings.
The virtual device now limits reclaim admission and usable swap
capacity for memory commitments. Check the loss of usable swap capacity
when removing a physical device.
Use the physical backing's synchronous I/O flag when choosing shmem
readahead. Preserve the shmem entry's order on a synchronous read so
zram can still read a large folio. Keep base-page reads during physical
swapoff and retain the existing order-zero retry.
Keep shmem's capacity check on active swap areas. Swapoff can
temporarily undercount free slots on other devices.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
.../admin-guide/kernel-parameters.txt | 7 --
include/linux/swap.h | 10 +-
mm/Kconfig | 20 ----
mm/memcontrol.c | 13 --
mm/shmem.c | 11 +-
mm/swapfile.c | 113 +++++++++++-------
mm/util.c | 31 +++--
mm/vmscan.c | 25 +---
mm/vswap.h | 7 ++
mm/zswap.c | 3 -
10 files changed, 108 insertions(+), 132 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 68b2d751439b..dce9b8f3ae98 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -8421,13 +8421,6 @@ Kernel parameters
force - force vulnerability detection even on
unaffected processors
- vswap= [MM,EARLY]
- Route swapouts through the virtual swap layer, which
- allows zswap and zero-filled pages to be used without
- a physical swap device.
- Format: { on | off }
- Default: on if CONFIG_VSWAP_DEFAULT_ON=y, else off.
-
vsyscall= [X86-64,EARLY]
Controls the behavior of vsyscalls (i.e. calls to
fixed addresses of 0xffffffffff600x00 from legacy
diff --git a/include/linux/swap.h b/include/linux/swap.h
index ae635d5d0bab..4fe3cabe6a39 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -370,7 +370,7 @@ extern void __meminit kswapd_stop(int nid);
bool current_is_kswapd(void);
#ifdef CONFIG_SWAP
-bool mem_cgroup_can_vswap(struct mem_cgroup *memcg);
+bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages);
unsigned long vswap_usage_in_pages(void);
int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
@@ -451,7 +451,7 @@ static inline void put_swap_device(struct swap_info_struct *si)
#else /* CONFIG_SWAP */
static inline void swap_writeback_dropbehind_folio(struct folio *folio) {}
-static inline bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
+static inline bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
{
return false;
}
@@ -591,7 +591,6 @@ static inline void mem_cgroup_swap_put(unsigned short id,
long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg);
-bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages);
bool mem_cgroup_swap_full(const struct folio *folio);
#else
static inline unsigned short mem_cgroup_swap_get(struct folio *folio)
@@ -634,11 +633,6 @@ static inline long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg)
return get_nr_swap_pages();
}
-static inline bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
-{
- return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages;
-}
-
static inline bool mem_cgroup_swap_full(const struct folio *folio)
{
return vm_swap_full();
diff --git a/mm/Kconfig b/mm/Kconfig
index 704d43fcd92b..18925ccb4adc 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -19,26 +19,6 @@ menuconfig SWAP
used to provide more virtual memory than the actual RAM present
in your computer. If unsure say Y.
-config VSWAP_DEFAULT_ON
- bool "Route swapouts through virtual swap by default"
- depends on SWAP
- default n
- help
- Virtual swap allows zswap and zero-filled pages to be used
- without swapping on a physical device first, and lets a page
- move between zswap and a swapfile without invalidating the page
- table entries that refer to it.
-
- Swap entries are handed out by a virtual swap device instead of
- naming a slot on a real one, so the backing can be chosen and
- changed after the entry exists.
-
- Say Y to make "vswap=on" the default, routing swapouts through
- the virtual swap layer from boot.
-
- Say N (default) to leave vswap off unless "vswap=on" is passed
- on the kernel command line.
-
config ZSWAP
bool "Compressed cache for swap pages"
depends on SWAP
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 5811ad3889df..2a87f33bc56d 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6326,19 +6326,6 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio)
return margin;
}
-/**
- * mem_cgroup_can_swap - can @memcg swap out at least @nr_pages more pages?
- * @memcg: the memcg to query
- * @nr_pages: the number of pages the caller wants to swap out
- *
- * Return: true if @memcg can swap out at least @nr_pages more pages.
- */
-bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
-{
- return mem_cgroup_can_vswap(memcg) ||
- mem_cgroup_get_nr_swap_pages(memcg) >= nr_pages;
-}
-
bool mem_cgroup_swap_full(const struct folio *folio)
{
struct mem_cgroup *memcg;
diff --git a/mm/shmem.c b/mm/shmem.c
index 5d3695886ecd..a9000a443674 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1914,8 +1914,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap)
goto redirty;
- /* vswap doesn't contribute to total_swap_pages */
- if (!total_swap_pages && !(vswap_is_enabled() && zswap_is_enabled()))
+ if (!vswap_is_enabled() || (!total_swap_pages && !zswap_is_enabled()))
goto redirty;
/*
@@ -2609,10 +2608,12 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
/* Look it up and read it in.. */
folio = swap_cache_get_folio(swap);
if (!folio) {
- if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
- /* Direct swapin skipping swap cache & readahead */
+ if (vswap_unuse ||
+ swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) {
folio = shmem_swap_alloc_folio(inode, vmf, index,
- swap, vswap_unuse ? 0 : order, gfp);
+ swap,
+ vswap_unuse ? 0 : order,
+ gfp);
} else {
/* Cached swapin only supports order 0 folio */
folio = shmem_swapin_cluster(swap, gfp, info, index);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index b0865d617512..72652a2099e9 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2062,25 +2062,37 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return err;
}
-bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
+/**
+ * mem_cgroup_can_swap - check backing headroom for ordinary swapout
+ * @memcg: the cgroup to query, or NULL for global reclaim
+ * @nr_pages: the number of pages the caller wants to swap out
+ *
+ * Return: true if compression or physical capacity and quota permit swapout.
+ */
+bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
{
- return vswap_is_enabled() && zswap_is_enabled() &&
- (!memcg || mem_cgroup_may_zswap(memcg, false));
+ long nr_swap_pages;
+
+ if (!vswap_is_enabled())
+ return false;
+
+ if (zswap_is_enabled() &&
+ (!memcg || mem_cgroup_may_zswap(memcg, false)))
+ return true;
+
+ nr_swap_pages = memcg ? mem_cgroup_get_nr_swap_pages(memcg) :
+ get_nr_swap_pages();
+ return nr_swap_pages >= nr_pages;
}
-static bool folio_may_vswap(struct folio *folio)
+static bool folio_may_zswap(struct folio *folio)
{
struct obj_cgroup *objcg;
bool may_zswap;
- if (!vswap_is_enabled() || !zswap_is_enabled())
+ if (!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)
@@ -2193,12 +2205,14 @@ int folio_alloc_swap(struct folio *folio)
unsigned int order = folio_order(folio);
unsigned short memcg_id;
unsigned int size = 1 << order;
- bool may_vswap;
+ bool may_zswap;
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 (!vswap_is_enabled())
+ return -ENOSPC;
+ may_zswap = folio_may_zswap(folio);
if (order) {
/*
@@ -2218,8 +2232,7 @@ int folio_alloc_swap(struct folio *folio)
}
}
- if (!may_vswap || !vswap_alloc(folio))
- folio_alloc_phys_swap(folio);
+ vswap_alloc(folio);
/*
* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL.
@@ -2227,27 +2240,25 @@ int folio_alloc_swap(struct folio *folio)
* folio_realloc_swap() charges it once backing is allocated.
*/
memcg_id = mem_cgroup_swap_get(folio);
- if (memcg_id) {
- if (!is_vswap_entry(folio->swap) &&
- unlikely(mem_cgroup_swap_charge(memcg_id, size))) {
- mem_cgroup_swap_put(memcg_id, size, false);
- swap_cache_del_folio(folio);
- } else {
- mem_cgroup_swap_record(folio, memcg_id);
- }
- }
+ if (memcg_id)
+ mem_cgroup_swap_record(folio, memcg_id);
if (unlikely(!folio_test_swapcache(folio)))
goto failed;
+ /* Reserve disk-only backing here so large allocation failures can split. */
+ if (!may_zswap && !folio_realloc_swap(folio).val) {
+ swap_cache_del_folio(folio);
+ goto failed;
+ }
+
return 0;
failed:
- if (get_nr_swap_pages() <= 0 && !may_vswap)
+ if (!may_zswap && get_nr_swap_pages() <= 0)
return -ENOSPC;
- if (!may_vswap && mem_cgroup_get_folio_swap_margin(folio) <= 0)
+ if (!may_zswap && mem_cgroup_get_folio_swap_margin(folio) <= 0)
return -ENOMEM;
-
return order ? -E2BIG : -ENOMEM;
}
@@ -4054,6 +4065,24 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)
}
+static int swapoff_check_commit(struct swap_info_struct *si)
+{
+ long capacity = vswap_capacity_in_pages();
+ long pages;
+ int ret;
+
+ if (!capacity)
+ return 0;
+ pages = min(total_swap_pages, capacity) -
+ min(total_swap_pages - (long)si->pages, capacity);
+ if (!pages)
+ return 0;
+ ret = security_vm_enough_memory_mm(current->mm, pages);
+ if (!ret)
+ vm_unacct_memory(pages);
+ return ret;
+}
+
SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
{
struct swap_info_struct *p = NULL;
@@ -4097,10 +4126,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
goto out_dput;
}
- if (!security_vm_enough_memory_mm(current->mm, p->pages))
- vm_unacct_memory(p->pages);
- else {
- err = -ENOMEM;
+ err = swapoff_check_commit(p);
+ if (err) {
spin_unlock(&swap_lock);
goto out_dput;
}
@@ -4978,16 +5005,19 @@ subsys_initcall(swapfile_init);
struct swap_info_struct *vswap_si;
DEFINE_STATIC_KEY_FALSE(vswap_key);
-static bool vswap_enabled_early __initdata = IS_ENABLED(CONFIG_VSWAP_DEFAULT_ON);
+/* vswap does no IO on its own. */
+static const struct swap_ops vswap_ops = { };
-static int __init early_vswap(char *buf)
+long vswap_capacity_in_pages(void)
{
- return kstrtobool(buf, &vswap_enabled_early);
-}
-early_param("vswap", early_vswap);
+ struct swap_info_struct *si;
-/* vswap does no IO on its own. */
-static const struct swap_ops vswap_ops = { };
+ if (!vswap_is_enabled())
+ return 0;
+ /* Pairs with publication in vswap_init(), before allocation is enabled. */
+ si = smp_load_acquire(&vswap_si);
+ return si->pages;
+}
unsigned long vswap_usage_in_pages(void)
{
@@ -5016,12 +5046,9 @@ static int __init vswap_init(void)
unsigned long maxpages;
int err;
- if (!vswap_enabled_early)
- return 0;
-
si = alloc_swap_info();
if (IS_ERR(si)) {
- pr_warn("vswap: alloc_swap_info failed (%ld); vswap disabled, swapout falls back to direct physical swap\n",
+ pr_warn("vswap: alloc_swap_info failed (%ld); ordinary swapping is disabled\n",
PTR_ERR(si));
return 0;
}
@@ -5055,7 +5082,7 @@ static int __init vswap_init(void)
enable_swap_info(si);
mutex_unlock(&swapon_mutex);
- vswap_si = si;
+ smp_store_release(&vswap_si, si);
root = debugfs_create_dir("vswap", NULL);
debugfs_create_file("used", 0444, root, NULL, &vswap_used_fops);
@@ -5069,7 +5096,7 @@ static int __init vswap_init(void)
return 0;
fail:
- pr_warn("vswap: setup_swap_clusters_info failed (%d); vswap disabled, swapout falls back to direct physical swap\n",
+ pr_warn("vswap: setup_swap_clusters_info failed (%d); ordinary swapping is disabled\n",
err);
spin_lock(&swap_lock);
si->flags = 0;
diff --git a/mm/util.c b/mm/util.c
index 47c4cf7400cf..d0dbe4de593f 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -924,7 +924,7 @@ unsigned long vm_commit_limit(void)
else
allowed = ((totalram_pages() - hugetlb_total_pages())
* sysctl_overcommit_ratio / 100);
- allowed += total_swap_pages;
+ allowed += min(total_swap_pages, vswap_capacity_in_pages());
return allowed;
}
@@ -954,6 +954,23 @@ unsigned long vm_memory_committed(void)
}
EXPORT_SYMBOL_GPL(vm_memory_committed);
+static long vm_overcommit_guess_limit(void)
+{
+ long capacity = vswap_capacity_in_pages();
+ long swap_pages = total_swap_pages;
+
+ /*
+ * With no physical swap the bound
+ * would collapse to RAM alone even though zswap can absorb
+ * more. Compression swap is backed by memory, so budget a 3x
+ * compression ratio, typical of most workloads. Anything that
+ * compresses better can use OVERCOMMIT_ALWAYS.
+ */
+ if (capacity && zswap_is_enabled())
+ swap_pages += 2 * totalram_pages();
+ return totalram_pages() + min(swap_pages, capacity);
+}
+
/*
* Check that a process has enough memory to allocate a new virtual
* mapping. 0 means there is enough memory for the allocation to
@@ -984,17 +1001,7 @@ int __vm_enough_memory(const struct mm_struct *mm, long pages, int cap_sys_admin
return 0;
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
- allowed = totalram_pages() + total_swap_pages;
- /*
- * Vswap is in neither term, so with no swapfile the bound
- * would collapse to RAM alone even though zswap can absorb
- * more. Compression swap is backed by memory, so budget a 3x
- * compression ratio, typical of most workloads. Anything that
- * compresses better can use OVERCOMMIT_ALWAYS.
- */
- if (vswap_is_enabled() && zswap_is_enabled())
- allowed += 2 * totalram_pages();
- if (pages > allowed)
+ if (pages > vm_overcommit_guess_limit())
goto error;
return 0;
}
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 77aefadc24f9..42ab70acdf5c 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -299,26 +299,9 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
int nid,
struct scan_control *sc)
{
- if (memcg == NULL) {
- /*
- * For non-memcg reclaim, is there space in any swap device?
- * vswap does not contribute to nr_swap_pages. And under
- * GFP_NOIO, is there enough swapcached anon to make scanning
- * anon worthwhile?
- */
- if ((get_nr_swap_pages() > 0 ||
- (vswap_is_enabled() && zswap_is_enabled())) &&
- !reclaimable_anon_is_low(memcg, nid, sc))
- return true;
- } else {
- /*
- * Is the memcg below its swap limit, and under GFP_NOIO does
- * it have enough swapcached anon to make scanning worthwhile?
- */
- if (mem_cgroup_can_swap(memcg, 1) &&
- !reclaimable_anon_is_low(memcg, nid, sc))
- return true;
- }
+ if (mem_cgroup_can_swap(memcg, 1) &&
+ !reclaimable_anon_is_low(memcg, nid, sc))
+ return true;
/*
* The page can not be swapped.
@@ -2754,7 +2737,7 @@ static bool can_age_anon_pages(struct lruvec *lruvec,
struct scan_control *sc)
{
/* Aging the anon LRU is valuable if swap is present: */
- if (total_swap_pages > 0 || (vswap_is_enabled() && zswap_is_enabled()))
+ if (vswap_is_enabled() && (total_swap_pages > 0 || zswap_is_enabled()))
return true;
/* Also valuable if anon pages can be demoted: */
diff --git a/mm/vswap.h b/mm/vswap.h
index 72b43897faae..c02aa8f6da57 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -29,6 +29,8 @@ enum vswap_backing_type {
#include "swap_table.h"
DECLARE_STATIC_KEY_FALSE(vswap_key);
+long vswap_capacity_in_pages(void);
+
/*
* Only true once vswap_init() has published vswap_si, so callers never
* see the device half built.
@@ -372,6 +374,11 @@ static inline void vswap_cluster_free_vtable(struct swap_cluster_info *ci)
#else /* !CONFIG_SWAP */
+static inline long vswap_capacity_in_pages(void)
+{
+ return 0;
+}
+
static inline bool vswap_is_enabled(void)
{
return false;
diff --git a/mm/zswap.c b/mm/zswap.c
index f1f0fdf3945d..bab1d52aa24c 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1217,9 +1217,6 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
* shrinker functions
**********************************/
/*
- * With vswap disabled, all zswap entries use directly mapped physical swap
- * entries. With vswap enabled, such entries are rare.
- *
* A zswap-backed vswap entry needs physical swap capacity for writeback
* and cgroup swap margin where applicable. @memcg may be NULL to check
* device capacity alone.
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 16/20] mm/swap: make vswap mandatory
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
@ 2026-12-04 22:09 ` sashiko-bot
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] mm/swap: missing SWP_RMAP_CACHE_ONLY on pre-reserved physical
swap slots
- [Medium] mm/swap: zero-filled and zswap shmem entries lose synchronous
swapin fast path
- [Medium] mm/swap: mem_cgroup_can_swap() and folio_alloc_swap() ignore
vswap exhaustion
- [Medium] mm/swap: mandatory vswap relies on GFP_ATOMIC-only cluster
allocations
- [Medium] mm/swap: swap reporting and OOM accounting ignore mandatory
vswap capacity cap
- [Medium] mm/swap: cacheline false sharing and barrier in
vswap_capacity_in_pages()
- [Medium] mm/swap: redundant vswap cluster locking and barriers on
swap-in faults
- [Medium] mm/swap: missing NULL check or acquire ordering in
vswap_capacity_in_pages()
- [Medium] mm/swap: redundant cluster locks and zero-page slot churn on
disk swapout
--
commit 4d9278f9d6b32ab62d490caafb93e1e6851a00d3
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: make vswap mandatory
This commit initializes the virtual swap device unconditionally when
CONFIG_SWAP is enabled, removes the vswap Kconfig and boot controls, and
routes all ordinary swapout through virtual swap while reserving physical
backing upfront for folios ineligible for zswap. It also caps overcommit
and swapoff swap capacity checks at vswap_capacity_in_pages() and uses
swap_entry_backend_has_flag() for shmem synchronous swap-in checks.
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 5d3695886ecd1..a9000a4436746 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
[ ... ]
> @@ -2609,10 +2608,12 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
> /* Look it up and read it in.. */
> folio = swap_cache_get_folio(swap);
> if (!folio) {
> - if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) {
> - /* Direct swapin skipping swap cache & readahead */
> + if (vswap_unuse ||
> + swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) {
> folio = shmem_swap_alloc_folio(inode, vmf, index,
> - swap, vswap_unuse ? 0 : order, gfp);
> + swap,
> + vswap_unuse ? 0 : order,
> + gfp);
> } else {
> /* Cached swapin only supports order 0 folio */
> folio = shmem_swapin_cluster(swap, gfp, info, index);
[Severity: Medium]
Does this demote zero-filled and zswap-backed shmem entries to order-0
readahead swap-in?
For a zero-filled folio, swap_zeromap_folio_set() releases any physical
backing via __vswap_release_backing(), and zswap-backed entries have no
physical slot either. In both cases vswap_to_phys() returns 0, so
swap_entry_backend_has_flag() returns false and shmem_swapin_folio() falls
back to shmem_swapin_cluster(), forcing shmem_split_large_entry() on large
entries and triggering speculative readahead of neighboring slots.
Zero-filled anonymous entries hit the same regression in do_swap_page(),
where both swap_entry_synchronous() and swap_entry_backend_has_flag()
return false and send the fault to order-0 swapin_readahead() instead of
swapin_sync().
[Severity: Medium]
Can mandatory vswap amplify cluster spinlock and barrier overhead on the
swap-in fault path?
Replacing the unlocked si->flags check here with
swap_entry_backend_has_flag() adds an XArray lookup, a ci->lock round-trip,
and a get_swap_device()/smp_rmb()/put_swap_device() pair on every shmem
swap-in miss.
On anonymous swap-in faults, do_swap_page() and should_try_to_free_swap()
now perform up to five separate vswap_lock_cluster() round-trips and two
get_swap_device()/smp_rmb() pairs for a single fault. In particular,
zswap_is_present() checks is_vswap_entry() before zswap_never_enabled(), so
swap_entry_synchronous() locks the vswap cluster on every fault even when
zswap has never been enabled.
Checking zswap_never_enabled() first in zswap_is_present() and resolving
the backing type and flags once across do_swap_page() and
should_try_to_free_swap() would avoid these redundant lock cycles.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index b0865d617512a..72652a2099e9d 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -2062,25 +2062,37 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
> return err;
> }
>
> -bool mem_cgroup_can_vswap(struct mem_cgroup *memcg)
> +/**
> + * mem_cgroup_can_swap - check backing headroom for ordinary swapout
> + * @memcg: the cgroup to query, or NULL for global reclaim
> + * @nr_pages: the number of pages the caller wants to swap out
> + *
> + * Return: true if compression or physical capacity and quota permit swapout.
> + */
> +bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages)
> {
> - return vswap_is_enabled() && zswap_is_enabled() &&
> - (!memcg || mem_cgroup_may_zswap(memcg, false));
> + long nr_swap_pages;
> +
> + if (!vswap_is_enabled())
> + return false;
> +
> + if (zswap_is_enabled() &&
> + (!memcg || mem_cgroup_may_zswap(memcg, false)))
> + return true;
> +
> + nr_swap_pages = memcg ? mem_cgroup_get_nr_swap_pages(memcg) :
> + get_nr_swap_pages();
> + return nr_swap_pages >= nr_pages;
> }
[Severity: Medium]
Should mem_cgroup_can_swap() also check whether vswap_si has at least
nr_pages of free virtual slots left?
Once vswap_usage_in_pages() reaches vswap_capacity_in_pages(), this still
returns true whenever zswap is enabled or physical swap has free slots, so
reclaim keeps scanning and isolating anonymous folios. In addition,
folio_alloc_swap()'s failed: path returns -E2BIG for large folios even when
vswap_si is exhausted, causing shrink_folio_list() and shmem_writeout() to
split THPs/mTHPs and retry order-0 allocation only to fail again.
[ ... ]
> @@ -2218,8 +2232,7 @@ int folio_alloc_swap(struct folio *folio)
> }
> }
>
> - if (!may_vswap || !vswap_alloc(folio))
> - folio_alloc_phys_swap(folio);
> + vswap_alloc(folio);
[Severity: Medium]
Can removing the physical swap fallback here cause swapout failures under
memory pressure because vswap_alloc_cluster() only uses GFP_ATOMIC?
Unlike swap_cluster_populate(), which drops its locks and retries with
__GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL, vswap_alloc_cluster() performs
several GFP_ATOMIC | __GFP_NOWARN allocations (ci_dyn, swap_table, vtable,
and xa_alloc()) under percpu_vswap_cluster.lock and ci->lock with no
sleeping fallback and without __GFP_NOMEMALLOC. When atomic reserves are
depleted, vswap_alloc() fails even with abundant physical swap, triggering
futile large-folio splits on -E2BIG, reclaim stalls, or premature OOMs.
[ ... ]
> if (unlikely(!folio_test_swapcache(folio)))
> goto failed;
>
> + /* Reserve disk-only backing here so large allocation failures can split. */
> + if (!may_zswap && !folio_realloc_swap(folio).val) {
> + swap_cache_del_folio(folio);
> + goto failed;
> + }
> +
> return 0;
[Severity: High]
Does reserving physical backing via folio_realloc_swap() here leave
SWP_RMAP_CACHE_ONLY clear on physical swap slots whose vswap swap_count is
still 0?
vswap_alloc() enters the folio into swapcache with swap_count == 0, and
folio_realloc_swap() allocates physical slots via
__swap_cluster_alloc_entries(), which initializes the physical swap_table
rmap entries without SWP_RMAP_CACHE_ONLY. folio_realloc_swap() then
installs vtable_mk_phys() under ci->lock without checking for
swap_count == 0, while vswap_mark_cache_only() only runs on a 1 -> 0
transition in swap_put_entries_cluster().
If try_to_unmap() fails in shrink_folio_list() (or the folio is deferred
while !mem_cgroup_swap_full() and !folio_test_mlocked()), folio_free_swap()
is skipped and the folio returns to the LRU in swapcache with
swap_count == 0 and physical backing lacking SWP_RMAP_CACHE_ONLY. A
concurrent swap_put_entries_cluster() dropping swap_count from 1 to 0 while
folio_realloc_swap() has dropped ci->lock for folio_alloc_phys_swap() hits
the same state:
CPU0 CPU1
folio_realloc_swap()
spin_unlock(&ci->lock)
folio_alloc_phys_swap()
swap_put_entries_cluster()
swap_count 1 -> 0
vswap_mark_cache_only()
(vtable still NONE/ZSWAP; no-op)
spin_lock(&ci->lock)
__vtable_set(..., vtable_mk_phys())
spin_unlock(&ci->lock) <-- SWP_RMAP_CACHE_ONLY never set
Once swap fills past 50%, cluster_scan_range(), cluster_reclaim_range(),
and swap_reclaim_full_clusters() skip Vswap-tagged physical slots without
SWP_RMAP_CACHE_ONLY, pinning physical swap space until the folio is freed
or swapoff runs and risking premature swap exhaustion. Checking
swap_count == 0 under ci->lock in folio_realloc_swap() and calling
swap_rmap_mark_cache_only() fixes both paths.
[Severity: Medium]
Does calling folio_realloc_swap() eagerly for every !may_zswap folio add
avoidable cluster lock round-trips and zero-page slot churn on disk
swapout?
For a non-zero disk-backed folio, folio_alloc_swap() and swap_writeout()
now perform 8 cluster spinlock cycles and 7 XArray lookups (compared to 3
cluster locks and 0 XArray lookups before): folio_realloc_swap() locks
ci->lock just to re-check vtable_type and re-read memcg_id that
folio_alloc_swap() already holds, and swap_writeout() locks ci->lock three
more times for swap_zeromap_folio_clear(),
folio_release_non_phys_swap_backing(), and folio_realloc_swap() again.
For zero-filled !may_zswap folios, folio_alloc_swap() allocates physical
slots and charges memcg->swap only for swap_zeromap_folio_set() to
immediately free the unwritten slots via __vswap_release_backing(),
bouncing nr_swap_pages and psi->inuse_pages, uncharging memcg->swap, and
invoking swap_slot_free_notify() and potential cluster discards.
[ ... ]
> +long vswap_capacity_in_pages(void)
> {
> - return kstrtobool(buf, &vswap_enabled_early);
> -}
> -early_param("vswap", early_vswap);
> + struct swap_info_struct *si;
>
> -/* vswap does no IO on its own. */
> -static const struct swap_ops vswap_ops = { };
> + if (!vswap_is_enabled())
> + return 0;
> + /* Pairs with publication in vswap_init(), before allocation is enabled. */
> + si = smp_load_acquire(&vswap_si);
> + return si->pages;
> +}
[Severity: Medium]
Can vswap_capacity_in_pages() dereference a NULL vswap_si during boot when
CONFIG_JUMP_LABEL=n?
Async initramfs unpacking (populate_rootfs() -> do_populate_rootfs() ->
shmem_acct_blocks() -> __vm_enough_memory()) runs concurrently with
late_initcall(vswap_init). With CONFIG_JUMP_LABEL=n, static_key_enable()
uses a relaxed atomic_set() and vswap_is_enabled() uses a relaxed
raw_atomic_read(). The conditional branch after vswap_is_enabled() does
not order the subsequent smp_load_acquire(&vswap_si) after the load of
vswap_key.key.enabled on weakly-ordered CPUs:
CPU0 (vswap_init) CPU1 (do_populate_rootfs)
vswap_capacity_in_pages()
si = smp_load_acquire(&vswap_si)
(reordered early; reads NULL)
smp_store_release(&vswap_si, si)
static_branch_enable(&vswap_key)
atomic_set(&key->enabled, 1)
vswap_is_enabled() -> true
return si->pages; <-- NULL deref
Checking if (!si) return 0; (or reading si = smp_load_acquire(&vswap_si)
without relying on the static key for ordering) prevents the NULL pointer
dereference.
[Severity: Medium]
Does calling vswap_capacity_in_pages() from __vm_enough_memory() introduce
cacheline false sharing and an acquire barrier on the mmap/brk hot path?
Under both OVERCOMMIT_GUESS (vm_overcommit_guess_limit()) and
OVERCOMMIT_NEVER (vm_commit_limit()), every __vm_enough_memory() call now
invokes vswap_capacity_in_pages(), executing smp_load_acquire(&vswap_si)
and reading si->pages. In struct swap_info_struct, pages sits adjacent to
frag_clusters[] and atomic_long_t inuse_pages, which swap_usage_add() and
swap_usage_sub() modify globally on every swap allocation and free.
Since vswap_si->pages is a boot-time constant initialized before
static_branch_enable(&vswap_key), caching it in a __ro_after_init scalar
avoids both the false sharing and the acquire barrier.
> diff --git a/mm/util.c b/mm/util.c
> index 47c4cf7400cf7..d0dbe4de593fb 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -924,7 +924,7 @@ unsigned long vm_commit_limit(void)
> else
> allowed = ((totalram_pages() - hugetlb_total_pages())
> * sysctl_overcommit_ratio / 100);
> - allowed += total_swap_pages;
> + allowed += min(total_swap_pages, vswap_capacity_in_pages());
>
> return allowed;
> }
[Severity: Medium]
Should swap reporting and OOM accounting also be capped at
vswap_capacity_in_pages() now that a single virtual swap device is
mandatory?
vswap_init() caps the sole virtual swap device at
min(swapfile_maximum_size, REFCOUNT_MAX) (~8 TiB on 64-bit 4 KiB systems,
or 512 MiB on 32-bit MIPS), and this commit caps vm_commit_limit(),
vm_overcommit_guess_limit(), and swapoff_check_commit() there. However,
si_swapinfo() (used by /proc/meminfo and sysinfo(2)), constrained_alloc()
in mm/oom_kill.c, and mem_cgroup_get_max() in mm/memcontrol.c still read
uncapped total_swap_pages and nr_swap_pages, causing reported swap totals
and OOM badness calculations to disagree with actual usable swap capacity
when total physical swap exceeds vswap_capacity_in_pages() or vswap_init()
fails.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=16
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (15 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
@ 2026-12-03 22:00 ` 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
` (3 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Ordinary swapcache folios and swap PTEs already use virtual
slots. Physical slots store reverse mappings or hibernation
reservations. Follow the virtual entry for swapin, writeout,
swapoff and cgroup accounting.
Release physical reservations independently of swapcache.
Store cgroup owners only on virtual ordinary swap slots.
Keep the physical device check when draining a swap area, and
check large-folio backing under the virtual cluster lock.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
.../admin-guide/cgroup-v1/memcg_test.rst | 5 +-
include/linux/swap.h | 23 +-
mm/memcontrol-v1.c | 39 +--
mm/memcontrol.c | 61 +----
mm/memory.c | 41 +--
mm/page_io.c | 53 ++--
mm/shmem.c | 8 +-
mm/swap_state.c | 33 ++-
mm/swapfile.c | 254 ++++++------------
mm/vmscan.c | 9 +-
mm/vswap.h | 16 +-
mm/zswap.c | 61 +----
12 files changed, 172 insertions(+), 431 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v1/memcg_test.rst b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
index 8d3fcba13450..f16098f32796 100644
--- a/Documentation/admin-guide/cgroup-v1/memcg_test.rst
+++ b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
@@ -43,9 +43,8 @@ Please note that implementation details can be changed.
mem_cgroup_uncharge()
Called when a page's refcount goes down to 0.
- mem_cgroup_dir_phys_swap_free()
- Called when swp_entry's refcnt goes down to 0. A charge against swap
- disappears.
+ mem_cgroup_swap_uncharge()
+ Releases the swap-side charge on swap-in or slot free.
3. charge-commit
=======================
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 4fe3cabe6a39..99b63fa65853 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -570,23 +570,13 @@ static inline void mem_cgroup_swap_record(struct folio *folio,
__mem_cgroup_swap_record(folio, id);
}
-void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages);
-static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
- unsigned int nr_pages)
-{
- if (mem_cgroup_disabled())
- return;
- __mem_cgroup_dir_phys_swap_free(id, nr_pages);
-}
-
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
- bool is_vswap);
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages);
static inline void mem_cgroup_swap_put(unsigned short id,
- unsigned int nr_pages, bool is_vswap)
+ unsigned int nr_pages)
{
if (mem_cgroup_disabled())
return;
- __mem_cgroup_swap_put(id, nr_pages, is_vswap);
+ __mem_cgroup_swap_put(id, nr_pages);
}
long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
@@ -613,13 +603,8 @@ static inline void mem_cgroup_swap_record(struct folio *folio,
{
}
-static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
- unsigned int nr_pages)
-{
-}
-
static inline void mem_cgroup_swap_put(unsigned short id,
- unsigned int nr_pages, bool is_vswap)
+ unsigned int nr_pages)
{
}
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 1a55c168c3eb..570bb5006328 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -269,9 +269,9 @@ void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
*/
void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
- struct swap_cluster_info_dynamic *ci_dyn;
unsigned int nr_entries, nr_swapfile;
unsigned int start, i;
unsigned short private_id;
@@ -303,15 +303,12 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
nr_entries = folio_nr_pages(folio);
start = swp_cluster_offset(folio->swap);
private_id = mem_cgroup_private_id_get(memcg, nr_entries);
- nr_swapfile = nr_entries;
- if (is_vswap_entry(folio->swap)) {
- mod_memcg_state(memcg, MEMCG_VSWAP, nr_entries);
- nr_swapfile = 0;
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
- for (i = start; i < start + nr_entries; i++)
- if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
- nr_swapfile++;
- }
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr_entries);
+ nr_swapfile = 0;
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (i = start; i < start + nr_entries; i++)
+ if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ nr_swapfile++;
if (nr_swapfile)
mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
@@ -354,12 +351,11 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
*/
void memcg1_swapin(struct folio *folio)
{
- struct swap_cluster_info *ci;
struct swap_cluster_info_dynamic *ci_dyn;
+ struct swap_cluster_info *ci;
unsigned long nr_pages;
unsigned int start, i, nr_swapfile = 0;
unsigned short id;
- bool is_vswap;
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -384,26 +380,19 @@ void memcg1_swapin(struct folio *folio)
* let's not wait for it. The page already received a
* memory+swap charge, drop the swap entry duplicate.
*/
- is_vswap = is_vswap_entry(folio->swap);
nr_pages = folio_nr_pages(folio);
start = swp_cluster_offset(folio->swap);
ci = swap_cluster_get_and_lock(folio);
id = __swap_cgroup_clear(ci, start, nr_pages);
- if (id && is_vswap) {
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
- for (i = start; i < start + nr_pages; i++)
- if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
- nr_swapfile++;
- }
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ for (i = start; i < start + nr_pages; i++)
+ if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ nr_swapfile++;
swap_cluster_unlock(ci);
if (!id)
return;
- if (is_vswap) {
- mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
- mem_cgroup_swap_put(id, nr_pages, true);
- } else {
- mem_cgroup_dir_phys_swap_free(id, nr_pages);
- }
+ mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
+ mem_cgroup_swap_put(id, nr_pages);
}
#endif
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 2a87f33bc56d..03f78a11138f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6053,27 +6053,24 @@ int __init mem_cgroup_init(void)
*/
void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned int start = swp_cluster_offset(folio->swap);
unsigned int end = start + folio_nr_pages(folio);
unsigned int i, nr = 0, nr_swapfile = 0;
struct mem_cgroup *memcg;
struct obj_cgroup *objcg;
- struct swap_cluster_info_dynamic *ci_dyn;
unsigned short id;
- bool is_vswap = is_vswap_entry(folio->swap);
if (mem_cgroup_disabled() || !do_memsw_account())
return;
lockdep_assert_held(&ci->lock);
- if (is_vswap)
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
for (i = start; i < end; i++) {
if (!__swp_tb_get_count(__swap_table_get(ci, i)))
continue;
nr++;
- if (!is_vswap ||
- vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+ if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
nr_swapfile++;
}
if (!nr)
@@ -6092,8 +6089,7 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
page_counter_charge(&memcg->memsw, nr);
if (nr_swapfile)
mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
- if (is_vswap_entry(folio->swap))
- mod_memcg_state(memcg, MEMCG_VSWAP, nr);
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr);
for (i = start; i < end; i++)
if (__swp_tb_get_count(__swap_table_get(ci, i)))
@@ -6134,10 +6130,8 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
}
id = mem_cgroup_private_id_get(memcg, nr_pages);
- if (is_vswap_entry(folio->swap)) {
- memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
- mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
- }
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
return id;
}
@@ -6199,37 +6193,6 @@ void __mem_cgroup_swap_record(struct folio *folio, unsigned short id)
swap_cluster_unlock(ci);
}
-/**
- * __mem_cgroup_dir_phys_swap_free - uncharge and drop the reference for
- * directly mapped physical swap entries
- * @id: cgroup id to uncharge
- * @nr_pages: number of directly mapped physical swap entries
- *
- * This is roughly equivalent to calling mem_cgroup_swap_uncharge()
- * followed by mem_cgroup_swap_put(). Reuse the resolved owner to avoid
- * a second lookup.
- */
-void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages)
-{
- struct obj_cgroup *objcg;
- struct mem_cgroup *memcg;
-
- rcu_read_lock();
- objcg = obj_cgroup_from_private_id(id);
- memcg = obj_cgroup_memcg(objcg);
- if (memcg) {
- if (!mem_cgroup_private_id_is_root(id)) {
- if (do_memsw_account())
- page_counter_uncharge(&memcg->memsw, nr_pages);
- else
- page_counter_uncharge(&memcg->swap, nr_pages);
- }
- mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
- __mem_cgroup_private_id_put(objcg, id, nr_pages);
- }
- rcu_read_unlock();
-}
-
/**
* __mem_cgroup_swap_uncharge - release cgroup charges for a vswap range
* @id: the private ID to uncharge (may be 0)
@@ -6275,21 +6238,17 @@ void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
}
/**
- * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
+ * __mem_cgroup_swap_put - drop accounting and private ID refs for virtual slots
* @id: the pinned private ID
* @nr_pages: number of refs to drop
- * @is_vswap: release virtual-swap accounting along with the refs
*/
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
- bool is_vswap)
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
{
struct mem_cgroup *memcg;
guard(rcu)();
- if (is_vswap) {
- memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
- mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
- }
+ memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+ mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
mem_cgroup_private_id_put(id, nr_pages);
}
diff --git a/mm/memory.c b/mm/memory.c
index ae9d5e69622f..ef443d569419 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4746,8 +4746,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
* folio_free_swap only frees the swap cache to release the slot if swap
* count is zero, so we don't need to check the swap count here.
*/
-static inline bool should_try_to_free_swap(struct swap_info_struct *si,
- struct folio *folio,
+static inline bool should_try_to_free_swap(struct folio *folio,
struct vm_area_struct *vma,
bool exclusive,
unsigned int fault_flags)
@@ -4766,7 +4765,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
* are fast, and meanwhile, swap cache pinning the slot deferring the
* release of metadata or fragmentation is a more critical issue.
*/
- if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO))
+ if (swap_entry_backend_has_flag(folio->swap, SWP_SYNCHRONOUS_IO))
return true;
if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) ||
folio_test_mlocked(folio))
@@ -4899,8 +4898,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
return orders;
}
-static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
- struct vm_fault *vmf)
+static unsigned long thp_swapin_suitable_orders(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;
unsigned long orders;
@@ -4919,25 +4917,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
entry = softleaf_from_pte(vmf->orig_pte);
- /*
- * A large swapped out folio could be partially or fully in zswap. We
- * lack handling for such cases, so fallback to swapping in order-0
- * folio.
- *
- * Vswap entries are checked later, under the cluster lock in
- * __swap_cache_add_check().
- */
- if (!is_vswap_entry(entry) && !zswap_never_enabled())
- return 0;
-
- /*
- * Neighbouring slots of a swap file can lie in two extents, and a folio
- * is read with one bio. The flag is set in swapon() and stays, so the
- * race is benign.
- */
- if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
- return 0;
-
/*
* Get a list of all the (large) orders below PMD_ORDER that are enabled
* and suitable for swapping THP.
@@ -4973,8 +4952,7 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
return orders;
}
#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
-static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
- struct vm_fault *vmf)
+static unsigned long thp_swapin_suitable_orders(struct vm_fault *vmf)
{
return 0;
}
@@ -5114,10 +5092,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (!folio) {
/* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */
if (swap_entry_synchronous(si, entry) ||
- (is_vswap_entry(entry) &&
- swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO)))
+ swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO))
folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
- thp_swapin_suitable_orders(si, vmf) | BIT(0),
+ thp_swapin_suitable_orders(vmf) | BIT(0),
vmf, NULL, 0);
else
folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf);
@@ -5251,7 +5228,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
* large folio is fully applicable before use.
*/
if (!swap_cache_folio_mappable(folio, nr_pages)) {
- if (!is_vswap_entry(entry) || !folio_test_dirty(folio))
+ if (!folio_test_dirty(folio))
swap_cache_del_folio(folio);
goto out_nomap;
}
@@ -5275,7 +5252,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
*/
exclusive = true;
} else if (exclusive && folio_test_writeback(folio) &&
- swap_entry_backend_has_flag(si, entry, SWP_STABLE_WRITES)) {
+ swap_entry_backend_has_flag(entry, SWP_STABLE_WRITES)) {
/*
* This is tricky: not all swap backends support
* concurrent page modifications while under writeback.
@@ -5378,7 +5355,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
* Do it after mapping, so raced page faults will likely see the folio
* in swap cache and wait on the folio lock.
*/
- if (should_try_to_free_swap(si, folio, vma, exclusive, vmf->flags))
+ if (should_try_to_free_swap(folio, vma, exclusive, vmf->flags))
folio_free_swap(folio);
folio_unlock(folio);
diff --git a/mm/page_io.c b/mm/page_io.c
index 854b0b71581d..0e90acc57c87 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -168,11 +168,9 @@ static void swap_zeromap_folio_set(struct folio *folio)
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
ci = swap_cluster_get_and_lock(folio);
- if (is_vswap_entry(folio->swap)) {
- /* Free any prior backing (e.g. ZSWAP entry from earlier swapout) */
- voff = swp_cluster_offset(folio->swap);
- __vswap_release_backing(ci, voff, nr_pages);
- }
+ /* Drop any backing from an earlier swapout. */
+ voff = swp_cluster_offset(entry);
+ __vswap_release_backing(ci, voff, nr_pages);
for (i = 0; i < nr_pages; i++) {
__swap_table_set_zero(ci, swp_cluster_offset(entry));
entry.val++;
@@ -209,7 +207,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
*/
int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
{
- swp_entry_t phys = folio->swap;
+ swp_entry_t phys;
int ret = 0;
if (folio_free_swap(folio))
@@ -231,18 +229,7 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
* acquired internally by swap_zeromap_folio_set/clear.
*/
if (is_folio_zero_filled(folio)) {
- swp_entry_t entry = folio->swap;
-
swap_zeromap_folio_set(folio);
-
- /*
- * The folio may have been stored in zswap before and dirtied
- * again while it stayed in the swap cache. Drop the old copy,
- * or zswap writeback would bring it back into the swap cache.
- */
- if (!is_vswap_entry(entry))
- zswap_invalidate(swp_type(entry), swp_offset(entry),
- folio_nr_pages(folio));
goto out_unlock;
}
@@ -254,13 +241,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
swap_zeromap_folio_clear(folio);
/*
- * For vswap: release stale non-swapfile backings (e.g. ZSWAP from a
+ * Release stale non-swapfile backings (e.g. ZSWAP from a
* previous swapout cycle) so zswap_store or folio_realloc_swap
* starts on clean slots. Contiguous PHYS backing is preserved for
* reuse by folio_realloc_swap.
*/
- if (is_vswap_entry(folio->swap))
- folio_release_non_phys_swap_backing(folio);
+ folio_release_non_phys_swap_backing(folio);
if (zswap_store(folio)) {
count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
@@ -275,12 +261,10 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
}
rcu_read_unlock();
- if (is_vswap_entry(folio->swap)) {
- phys = folio_realloc_swap(folio);
- if (!phys.val) {
- folio_mark_dirty(folio);
- return AOP_WRITEPAGE_ACTIVATE;
- }
+ phys = folio_realloc_swap(folio);
+ if (!phys.val) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
}
if (ctx->nofs &&
@@ -505,12 +489,11 @@ static bool swap_read_folio_zeromap(struct folio *folio)
void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
{
- struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
- bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
+ bool synchronous = false;
bool workingset = folio_test_workingset(folio);
swp_entry_t phys;
- VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
+ VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
@@ -519,14 +502,10 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
* folio->swap virtual, so map it to its physical backing; a folio with
* no backing has nothing to read.
*/
- if (swap_is_vswap(sis)) {
- phys = vswap_to_phys(folio->swap);
- if (phys.val)
- synchronous = __swap_entry_to_info(phys)->flags &
- SWP_SYNCHRONOUS_IO;
- } else {
- phys = folio->swap;
- }
+ phys = vswap_to_phys(folio->swap);
+ if (phys.val)
+ synchronous = __swap_entry_to_info(phys)->flags &
+ SWP_SYNCHRONOUS_IO;
/*
* A batch this folio can't join is submitted before the pressure
diff --git a/mm/shmem.c b/mm/shmem.c
index a9000a443674..8edbb84b1e4a 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2574,7 +2574,6 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
struct folio *folio = NULL;
int error, nr_pages, order;
pgoff_t offset;
- bool vswap_unuse;
VM_BUG_ON(!*foliop || !xa_is_value(*foliop));
index_entry = radix_to_swp_entry(*foliop);
@@ -2603,16 +2602,15 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
swap = swp_entry(swp_type(swap), swp_offset(swap) + offset);
}
target_swap = swap;
- vswap_unuse = swapoff_type >= 0 && swap_is_vswap(si);
/* Look it up and read it in.. */
folio = swap_cache_get_folio(swap);
if (!folio) {
- if (vswap_unuse ||
- swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) {
+ if (swapoff_type >= 0 ||
+ swap_entry_backend_has_flag(swap, SWP_SYNCHRONOUS_IO)) {
folio = shmem_swap_alloc_folio(inode, vmf, index,
swap,
- vswap_unuse ? 0 : order,
+ swapoff_type >= 0 ? 0 : order,
gfp);
} else {
/* Cached swapin only supports order 0 folio */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index f7df517cf66a..b03871d63408 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -210,28 +210,25 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
return 0;
/*
- * For a vswap entry batch, reject if the backing is not THP-amenable
+ * Reject a batch if its backing is not THP-amenable
* (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in
* __swap_cache_alloc_folio will retry with a smaller order on -EBUSY.
*/
- if (is_vswap_entry(targ_entry)) {
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
- ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr),
- nr, &type);
- if (ret != nr || type == VSWAP_ZSWAP)
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr), nr, &type);
+ if (ret != nr || type == VSWAP_ZSWAP)
+ return -EBUSY;
+ if (type == VSWAP_SWAPFILE) {
+ struct swap_info_struct *si;
+ swp_entry_t phys;
+ unsigned long vt;
+
+ vt = __vtable_get(ci_dyn, round_down(ci_off, nr));
+ phys = vtable_to_phys(vt);
+ si = __swap_entry_to_info(phys);
+ /* Swapfile extents can end inside a large folio. */
+ if (!data_race(si->flags & SWP_BLKDEV))
return -EBUSY;
- if (type == VSWAP_SWAPFILE) {
- struct swap_info_struct *si;
- swp_entry_t phys;
- unsigned long vt;
-
- vt = __vtable_get(ci_dyn, round_down(ci_off, nr));
- phys = vtable_to_phys(vt);
- si = __swap_entry_to_info(phys);
- /* Swapfile extents can end inside a large folio. */
- if (!data_race(si->flags & SWP_BLKDEV))
- return -EBUSY;
- }
}
is_zero = __swap_table_test_zero(ci, ci_off);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 72652a2099e9..8f38737d8aa6 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -482,7 +482,7 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
* 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, bool direct,
+ struct swap_cluster_info *ci,
gfp_t gfp, bool locked)
{
struct swap_table *table = NULL;
@@ -507,19 +507,8 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
goto lock;
#ifdef CONFIG_MEMCG
- /*
- * Backing-only clusters keep quota owners on the virtual cluster.
- * Physical owner metadata is prepared on demand for direct use
- * and physical-slot statistics.
- */
- if ((!vswap_is_enabled() || vswap || direct) &&
- !mem_cgroup_disabled()) {
- gfp_t memcg_gfp = gfp;
-
- if (direct && !vswap)
- memcg_gfp &= ~__GFP_NOMEMALLOC;
-
- memcg_table = kzalloc_obj(*ci->memcg_table, memcg_gfp);
+ if (vswap && !mem_cgroup_disabled()) {
+ memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
if (!memcg_table)
goto lock;
}
@@ -614,7 +603,7 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
*/
static struct swap_cluster_info *
swap_cluster_populate(struct swap_info_struct *si,
- struct swap_cluster_info *ci, bool direct)
+ struct swap_cluster_info *ci)
{
int ret;
@@ -629,7 +618,7 @@ swap_cluster_populate(struct swap_info_struct *si,
lockdep_assert_held(&si->global_cluster_lock);
lockdep_assert_held(&ci->lock);
- if (!swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
+ if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
__GFP_NOWARN, true))
return ci;
@@ -643,8 +632,8 @@ swap_cluster_populate(struct swap_info_struct *si,
spin_unlock(&si->global_cluster_lock);
local_unlock(&percpu_swap_cluster.lock);
- ret = swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
- GFP_KERNEL, false);
+ ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ GFP_KERNEL, false);
/*
* Back to atomic context. We might have migrated to a new CPU with a
@@ -726,7 +715,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
* this returns NULL for an non-empty list.
*/
static struct swap_cluster_info *isolate_lock_cluster(
- struct swap_info_struct *si, struct list_head *list, bool direct)
+ struct swap_info_struct *si, struct list_head *list)
{
struct swap_cluster_info *ci, *found = NULL;
u8 flags = CLUSTER_FLAG_NONE;
@@ -758,7 +747,7 @@ static struct swap_cluster_info *isolate_lock_cluster(
if (found && flags == CLUSTER_FLAG_FREE) {
VM_WARN_ON_ONCE(list != &si->free_clusters);
VM_WARN_ON_ONCE(cluster_table_is_alloced(found));
- return swap_cluster_populate(si, found, direct);
+ return swap_cluster_populate(si, found);
}
return found;
@@ -933,7 +922,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
need_table = !ci->count;
/* Need to allocate swap table first for initial bad slot marking. */
if (need_table) {
- ret = swap_cluster_alloc_table(alloc_si, ci, false, GFP_KERNEL, false);
+ ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
if (ret)
goto out;
} else {
@@ -1125,13 +1114,14 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
order = folio_order(folio);
nr_pages = 1 << order;
swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
- if (folio_test_swapcache(folio)) {
+ if (!swap_is_vswap(si)) {
/*
* Folio already in the swap cache: we are allocating
* physical backing for its vswap entry. Point each
* physical slot back at its own vswap entry
* (Vswap-tagged rmap).
*/
+ VM_WARN_ON(!folio_test_swapcache(folio));
VM_WARN_ON(!is_vswap_entry(folio->swap));
vswap_entry = folio->swap;
for (i = 0; i < nr_pages; i++) {
@@ -1187,8 +1177,7 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
struct swap_cluster_info *ci,
struct folio *folio,
- unsigned long offset,
- bool *nomem)
+ unsigned long offset)
{
unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
@@ -1217,19 +1206,6 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
if (!ret)
continue;
}
-#ifdef CONFIG_MEMCG
- /* Hibernation needs owner metadata before publishing a raw slot. */
- if (vswap_is_enabled() && !folio &&
- !mem_cgroup_disabled() && !ci->memcg_table) {
- ci->memcg_table = kzalloc_obj(*ci->memcg_table,
- GFP_ATOMIC | __GFP_NOWARN);
- if (!ci->memcg_table) {
- if (nomem)
- *nomem = true;
- goto out;
- }
- }
-#endif
if (!__swap_cluster_alloc_entries(si, ci, folio, offset % SWAPFILE_CLUSTER))
break;
found = offset;
@@ -1239,15 +1215,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
break;
}
out:
- /*
- * On a discard-capable device, relocating a cluster whose memcg_table
- * allocation failed queues a discard for slots that were never used,
- * which folio_alloc_phys_swap() reads as progress and retries on.
- */
- if (nomem && *nomem && !ci->count)
- __free_cluster(si, ci);
- else
- relocate_cluster(si, ci);
+ relocate_cluster(si, ci);
swap_cluster_unlock(ci);
if (swap_is_vswap(si)) {
this_cpu_write(percpu_vswap_cluster.offset[order], next);
@@ -1268,25 +1236,18 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
bool scan_all)
{
unsigned int found = SWAP_ENTRY_INVALID;
- bool nomem = false;
- bool direct = !folio || !folio_test_swapcache(folio);
- /*
- * In rare cases alloc_swap_scan_cluster() can fail due to
- * memcg_table allocation failure. Short-circuit to avoid looping
- * over the list indefinitely.
- */
do {
- struct swap_cluster_info *ci = isolate_lock_cluster(si, list, direct);
+ struct swap_cluster_info *ci = isolate_lock_cluster(si, list);
unsigned long offset;
if (!ci)
break;
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset, &nomem);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
if (found)
break;
- } while (scan_all && !nomem);
+ } while (scan_all);
return found;
}
@@ -1311,7 +1272,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
- if (swap_cluster_alloc_table(si, ci, false, GFP_ATOMIC | __GFP_NOWARN, true)) {
+ if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
spin_unlock(&ci->lock);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1335,7 +1296,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
}
offset = cluster_offset(si, ci);
- return alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
+ return alloc_swap_scan_cluster(si, ci, folio, offset);
}
static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
@@ -1350,7 +1311,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
if (force)
to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER;
- while ((ci = isolate_lock_cluster(si, &si->full_clusters, false))) {
+ while ((ci = isolate_lock_cluster(si, &si->full_clusters))) {
offset = cluster_offset(si, ci);
end = min(si->max, offset + SWAPFILE_CLUSTER);
to_scan--;
@@ -1437,8 +1398,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset,
- NULL);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
} else {
swap_cluster_unlock(ci);
}
@@ -1714,7 +1674,7 @@ static swp_entry_t swap_alloc_fast(struct folio *folio)
if (ci && cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset);
} else if (ci) {
swap_cluster_unlock(ci);
}
@@ -1943,8 +1903,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
}
/* count will be 0 after put, slot can be reclaimed */
need_reclaim = true;
- if (swap_is_vswap(si))
- vswap_mark_cache_only(ci, ci_off);
+ vswap_mark_cache_only(ci, ci_off);
}
/*
* A count != 1 or cached slot can't be freed. Put its swap
@@ -2051,8 +2010,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
goto failed;
}
} while (++ci_off < ci_end);
- if (swap_is_vswap(si))
- vswap_clear_cache_only(ci, ci_start, nr);
+ vswap_clear_cache_only(ci, ci_start, nr);
swap_cluster_unlock(ci);
return 0;
failed:
@@ -2114,8 +2072,7 @@ static bool vswap_alloc(struct folio *folio)
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,
- NULL);
+ alloc_swap_scan_cluster(vswap_si, ci, folio, offset);
} else if (ci) {
swap_cluster_unlock(ci);
}
@@ -2136,37 +2093,8 @@ static bool vswap_alloc(struct folio *folio)
return false;
}
-#ifdef CONFIG_MEMCG
-static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
-{
- struct swap_cluster_info *ci = __swap_entry_to_cluster(entry);
- struct swap_memcg_table *table __free(kfree) = NULL;
-
- if (mem_cgroup_disabled() || READ_ONCE(ci->memcg_table))
- return 0;
-
- /* The caller's locked folio pins the slots while allocation sleeps. */
- table = kzalloc_obj(*table, GFP_ATOMIC | __GFP_NOWARN);
- if (!table)
- table = kzalloc_obj(*table, __GFP_HIGH | GFP_KERNEL);
- guard(spinlock)(&ci->lock);
- if (ci->memcg_table)
- return 0;
- if (!table)
- return -ENOMEM;
- WRITE_ONCE(ci->memcg_table, no_free_ptr(table));
- return 0;
-}
-#else
-static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
-{
- return 0;
-}
-#endif
-
static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
{
- bool direct = !folio_test_swapcache(folio);
swp_entry_t entry;
again:
@@ -2178,13 +2106,6 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
if (!entry.val && !folio_order(folio) && swap_sync_discard())
goto again;
- if (!entry.val)
- return entry;
-
- if (direct && swap_cluster_alloc_memcg_table(entry)) {
- swap_cache_del_folio(folio);
- return (swp_entry_t){};
- }
return entry;
}
@@ -2516,6 +2437,7 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
return (swp_entry_t){};
}
+
if (mem_cgroup_swap_charge(memcg_id, nr)) {
__swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry),
__swap_entry_to_cluster(phys_entry),
@@ -2686,42 +2608,40 @@ static void __swap_cluster_finish_free(struct swap_info_struct *si,
partial_free_cluster(si, ci);
}
-/*
- * Free physical swap slots that were backing vswap entries (Vswap-tagged).
- */
-static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
- struct swap_cluster_info *pci,
- unsigned int ci_start,
- unsigned int nr_pages)
+/* Backing owners live on virtual entries; raw hibernation is root-owned. */
+static void __swap_cluster_free_phys_slots(struct swap_info_struct *si,
+ struct swap_cluster_info *ci,
+ unsigned int ci_start,
+ unsigned int nr_pages)
{
unsigned int ci_off;
- spin_lock_nested(&pci->lock, SINGLE_DEPTH_NESTING);
- VM_WARN_ON(pci->count < nr_pages);
- pci->count -= nr_pages;
+ lockdep_assert_held(&ci->lock);
+ VM_WARN_ON(ci->count < nr_pages);
+ ci->count -= nr_pages;
for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) {
- __swap_table_set(pci, ci_off, null_to_swp_tb());
+ __swap_table_set(ci, ci_off, null_to_swp_tb());
if (!SWAP_TABLE_HAS_ZEROFLAG)
- __swap_table_clear_zero(pci, ci_off);
+ __swap_table_clear_zero(ci, ci_off);
}
- __swap_cluster_finish_free(psi, pci, ci_start, nr_pages);
- swap_cluster_unlock(pci);
+ __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
}
/*
- * Release cgroup accounting for freed swap slots. Physical backing of
- * vswap entries was already uncharged by __vswap_release_backing().
+ * Free physical swap slots that were backing vswap entries (Vswap-tagged).
*/
-static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap)
+static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
+ struct swap_cluster_info *pci,
+ unsigned int ci_start,
+ unsigned int nr_pages)
{
- if (is_vswap)
- mem_cgroup_swap_put(id, nr, true);
- else
- mem_cgroup_dir_phys_swap_free(id, nr);
+ spin_lock_nested(&pci->lock, SINGLE_DEPTH_NESTING);
+ __swap_cluster_free_phys_slots(psi, pci, ci_start, nr_pages);
+ swap_cluster_unlock(pci);
}
/*
- * Free a set of swap slots after their swap count dropped to zero, or will be
+ * Free virtual swap slots after their swap count dropped to zero, or will be
* zero after putting the last ref (saves one __swap_cluster_put_entry call).
*/
void __swap_cluster_free_entries(struct swap_info_struct *si,
@@ -2732,23 +2652,20 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
unsigned short batch_id = 0, id_cur;
unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
unsigned int batch_off = ci_off;
- bool is_vswap = swap_is_vswap(si);
VM_WARN_ON(ci->count < nr_pages);
- if (is_vswap)
- __vswap_release_backing(ci, ci_start, nr_pages);
+ __vswap_release_backing(ci, ci_start, nr_pages);
ci->count -= nr_pages;
do {
old_tb = __swap_table_get(ci, ci_off);
/*
* Freeing is done after release of the last swap count
- * ref, or after swap cache is dropped. A hibernation slot
- * has no count and is freed directly by its owner.
+ * ref, or after swap cache is dropped.
*/
- VM_WARN_ON(!swp_tb_is_hibernation(old_tb) &&
- (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
+ VM_WARN_ON(!swp_tb_is_shadow(old_tb) ||
+ __swp_tb_get_count(old_tb) > 1);
/* Resetting the slot to NULL also clears the inline flags. */
__swap_table_set(ci, ci_off, null_to_swp_tb());
@@ -2756,20 +2673,22 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
__swap_table_clear_zero(ci, ci_off);
/*
- * Uncharge swap slots by memcg in batches. Consecutive
- * slots with the same cgroup id are uncharged together.
+ * Release cgroup accounting for freed swap slots. Physical
+ * backing of vswap entries was already uncharged by
+ * __vswap_release_backing().
*/
id_cur = __swap_cgroup_clear(ci, ci_off, 1);
if (batch_id != id_cur) {
if (batch_id)
- memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
+ mem_cgroup_swap_put(batch_id,
+ ci_off - batch_off);
batch_id = id_cur;
batch_off = ci_off;
}
} while (++ci_off < ci_end);
if (batch_id)
- memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
+ mem_cgroup_swap_put(batch_id, ci_off - batch_off);
__swap_cluster_finish_free(si, ci, ci_start, nr_pages);
}
@@ -2986,16 +2905,17 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
if (pcp_si == si && pcp_offset) {
ci = swap_cluster_lock(si, pcp_offset);
if (cluster_is_usable(ci, 0))
- offset = alloc_swap_scan_cluster(si, ci, NULL,
- pcp_offset, NULL);
+ offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
else
swap_cluster_unlock(ci);
}
if (!offset)
offset = cluster_alloc_swap_entry(si, NULL);
local_unlock(&percpu_swap_cluster.lock);
- if (offset)
+ if (offset) {
entry = swp_entry(si->type, offset);
+ mod_memcg_state(root_mem_cgroup, MEMCG_SWAP, 1);
+ }
fail:
return entry;
@@ -3012,10 +2932,13 @@ void swap_free_hibernation_slot(swp_entry_t entry)
struct swap_info_struct *si = __swap_entry_to_info(entry);
struct swap_cluster_info *ci;
pgoff_t offset = swp_offset(entry);
+ unsigned int ci_off = offset % SWAPFILE_CLUSTER;
ci = swap_cluster_lock(si, offset);
- __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
+ VM_WARN_ON(!swp_tb_is_hibernation(__swap_table_get(ci, ci_off)));
+ __swap_cluster_free_phys_slots(si, ci, ci_off, 1);
swap_cluster_unlock(ci);
+ mod_memcg_state(root_mem_cgroup, MEMCG_SWAP, -1);
}
static int __find_hibernation_swap_type(dev_t device, sector_t offset)
@@ -3288,16 +3211,11 @@ bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type)
if (!softleaf_is_swap(entry))
return false;
- /* Direct entries retain their physical slots while zswapped. */
- if (swp_type(entry) == type)
- return true;
si = get_swap_device(entry);
if (IS_ERR_OR_NULL(si))
return false;
- if (swap_is_vswap(si)) {
- phys = vswap_to_phys(entry);
- match = phys.val && swp_type(phys) == type;
- }
+ phys = vswap_to_phys(entry);
+ match = phys.val && swp_type(phys) == type;
put_swap_device(si);
return match;
}
@@ -3326,16 +3244,6 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
!swap_entry_is_backed_by(entry, type))
return 0;
- /*
- * A large folio that a fault didn't map can't be mapped one PTE at a
- * time here, so drop it and swap in small folios instead.
- */
- if (unlikely(!is_vswap_entry(entry) &&
- !swap_cache_folio_mappable(folio, 1))) {
- swap_cache_del_folio(folio);
- return 0;
- }
-
swapcache = folio;
folio = ksm_might_need_to_copy(folio,
folio_file_page(folio, swp_offset(entry)), vma, addr);
@@ -3373,8 +3281,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
goto setpte;
}
- if (is_vswap_entry(entry) && !folio_test_anon(folio) &&
- folio_test_large(folio)) {
+ if (!folio_test_anon(folio) && folio_test_large(folio)) {
if (folio_test_dirty(folio)) {
ret = -EBUSY;
goto out;
@@ -3484,15 +3391,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
.pmd = pmd,
};
- if (is_vswap_entry(entry)) {
- folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
- BIT(0), &vmf, NULL, 0);
- if (IS_ERR(folio))
- folio = NULL;
- } else {
- folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
- &vmf);
- }
+ folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, BIT(0),
+ &vmf, NULL, 0);
+ if (IS_ERR(folio))
+ folio = NULL;
}
if (!folio) {
swp_tb = swap_table_lookup(entry);
@@ -3750,14 +3652,12 @@ static int try_to_unuse(unsigned int type)
*/
folio_lock(folio);
folio_wait_writeback(folio);
- if (is_vswap_entry(entry)) {
- if (!folio_matches_swap_entry(folio, entry))
- goto unlock_cache;
- phys = vswap_to_phys(entry);
- if (!phys.val || swp_type(phys) != type ||
- swp_offset(phys) != i)
- goto unlock_cache;
- }
+ if (!folio_matches_swap_entry(folio, entry))
+ goto unlock_cache;
+ phys = vswap_to_phys(entry);
+ if (!phys.val || swp_type(phys) != type ||
+ swp_offset(phys) != i)
+ goto unlock_cache;
folio_free_swap(folio);
unlock_cache:
folio_unlock(folio);
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 42ab70acdf5c..ffe2795ce495 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1026,12 +1026,9 @@ static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
return false;
- entry = folio->swap;
- if (is_vswap_entry(entry)) {
- entry = vswap_to_phys(entry);
- if (!entry.val)
- return true;
- }
+ entry = vswap_to_phys(folio->swap);
+ if (!entry.val)
+ return true;
return !(__swap_entry_to_info(entry)->ops->flags &
SWAP_OPS_F_REQUIRE_NOFS);
diff --git a/mm/vswap.h b/mm/vswap.h
index c02aa8f6da57..f3a55414c6b7 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -341,8 +341,7 @@ static inline int vswap_check_backing(swp_entry_t entry, int nr,
* range of physical swap slots.
* @folio: a swap-cache resident folio
*
- * Return: %true if @folio->swap is not a vswap entry, or if these vswap
- * entries are backed by a contiguous range of physical slots.
+ * Return: %true if the folio has a contiguous range of physical slots.
*/
static inline bool folio_phys_swap_backed(struct folio *folio)
{
@@ -350,9 +349,8 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
int nr = folio_nr_pages(folio);
enum vswap_backing_type type;
- return !is_vswap_entry(entry) ||
- (vswap_check_backing(entry, nr, &type) == nr &&
- type == VSWAP_SWAPFILE);
+ return vswap_check_backing(entry, nr, &type) == nr &&
+ type == VSWAP_SWAPFILE;
}
static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
@@ -403,22 +401,18 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
/*
* Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...)
- * for @entry. For a vswap entry the property belongs to the current
+ * for the virtual @entry. The property belongs to the current
* physical backing rather than vswap_si itself; resolve to the backing
* and test there. Returns false for zswap/zero/unbacked vswap entries
* as they don't have a backing bdev.
*/
-static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
- swp_entry_t entry,
+static inline bool swap_entry_backend_has_flag(swp_entry_t entry,
unsigned long flag)
{
struct swap_info_struct *phys_si;
swp_entry_t phys;
bool has_flag;
- if (!swap_is_vswap(si))
- return data_race(si->flags & flag);
-
phys = vswap_to_phys(entry);
if (!phys.val)
return false;
diff --git a/mm/zswap.c b/mm/zswap.c
index bab1d52aa24c..e527a34e52a8 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1101,12 +1101,10 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct zswap_entry *entry,
swp_entry_t swpentry)
{
- pgoff_t offset = swp_offset(swpentry);
struct folio *folio;
struct mempolicy *mpol;
struct swap_info_struct *si;
swp_entry_t phys = {};
- bool is_vswap;
int ret = 0;
/* try to allocate swap cache folio */
@@ -1114,7 +1112,6 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
if (IS_ERR_OR_NULL(si))
return -ENOENT;
- is_vswap = swap_is_vswap(si);
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
NO_INTERLEAVE_INDEX);
@@ -1135,8 +1132,7 @@ 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 (or vswap
- * vtable) here.
+ * swapoff so we can safely dereference the vswap vtable 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
@@ -1147,16 +1143,14 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
goto err;
}
- if (is_vswap) {
- /*
- * Allocate physical backing before decompress so a failure
- * wastes no work.
- */
- phys = folio_realloc_swap(folio);
- if (!phys.val) {
- ret = -ENOMEM;
- goto err;
- }
+ /*
+ * Allocate physical backing before decompress so a failure
+ * wastes no work.
+ */
+ phys = folio_realloc_swap(folio);
+ if (!phys.val) {
+ ret = -ENOMEM;
+ goto err;
}
if (!zswap_decompress(entry, folio)) {
@@ -1166,14 +1160,10 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
* Restore the zswap entry to the vtable, which also frees the
* allocated physical swap space.
*/
- if (is_vswap)
- vswap_zswap_store(swpentry, entry);
+ vswap_zswap_store(swpentry, entry);
goto err;
}
- if (!is_vswap)
- xa_erase(swap_zswap_tree(swpentry), offset);
-
count_vm_event(ZSWPWB);
if (entry->objcg)
count_objcg_events(entry->objcg, ZSWPWB, 1);
@@ -1199,10 +1189,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
folio_put(folio);
/* start writeback */
- if (is_vswap)
- __swap_writeout(ctx, folio, phys);
- else
- __swap_writeout(ctx, folio, folio->swap);
+ __swap_writeout(ctx, folio, phys);
return 0;
@@ -1805,9 +1792,7 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr)
* of the following error codes:
*
* -EIO: if the swapped out content was in zswap but could not be handed
- * back, either because decompression failed or because a slot in a
- * large-folio range is still in zswap and zswap cannot reconstruct a large
- * folio from per-page entries. The folio is unlocked, but NOT marked
+ * back because decompression failed. The folio is unlocked, but NOT marked
* up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
* SIGBUS).
*
@@ -1817,7 +1802,6 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr)
int zswap_load(struct folio *folio)
{
swp_entry_t swp = folio->swap;
- struct swap_info_struct *si = __swap_entry_to_info(swp);
struct zswap_entry *entry;
VM_WARN_ON_ONCE(!folio_test_locked(folio));
@@ -1827,23 +1811,11 @@ int zswap_load(struct folio *folio)
return -ENOENT;
/*
- * A large folio can legitimately reach zswap_load() with its whole
- * range on the backing device, so scan the range rather than rejecting
- * it outright. The caller has pinned every slot, so zswap cannot start
- * a store or a writeback into the range while we look.
- *
* A vswap batch is checked when the folio enters the swap cache, and
* its backing cannot change after that.
*/
- if (folio_test_large(folio)) {
- if (WARN_ON_ONCE(!swap_is_vswap(si) &&
- zswap_is_present(swp,
- folio_nr_pages(folio)))) {
- folio_unlock(folio);
- return -EIO;
- }
+ if (folio_test_large(folio))
return -ENOENT;
- }
entry = zswap_entry_load(swp);
if (!entry)
@@ -1869,12 +1841,7 @@ int zswap_load(struct folio *folio)
*/
folio_mark_dirty(folio);
- if (swap_is_vswap(si)) {
- folio_release_vswap_backing(folio);
- } else {
- xa_erase(swap_zswap_tree(swp), swp_offset(swp));
- zswap_entry_free(entry);
- }
+ folio_release_vswap_backing(folio);
folio_unlock(folio);
return 0;
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm/swap: repeated cluster spinlocks and XArray lookups per
swap-in fault
- [Medium] mm: zswap swap-in faults run unnecessary mTHP PTE and cluster
scans
- [Medium] mm/swap: swapoff disables readahead, plugging, and large
shmem swap-in
--
commit 3f2d2334395a7ef2f69d9d945528dc972a0fe190
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: drop direct physical swapcache handling
This commit removes the direct physical swapcache paths across swap-in,
writeout, swapoff, zswap, and memcg accounting so ordinary swapcache folios
and swap PTEs always go through virtual swap slots. Physical slots are now
used only for reverse mappings and hibernation reservations.
[ ... ]
> diff --git a/mm/memory.c b/mm/memory.c
> index ae9d5e69622f9..ef443d569419b 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
[ ... ]
> @@ -4919,25 +4917,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
>
> entry = softleaf_from_pte(vmf->orig_pte);
>
> - /*
> - * A large swapped out folio could be partially or fully in zswap. We
> - * lack handling for such cases, so fallback to swapping in order-0
> - * folio.
> - *
> - * Vswap entries are checked later, under the cluster lock in
> - * __swap_cache_add_check().
> - */
> - if (!is_vswap_entry(entry) && !zswap_never_enabled())
> - return 0;
> -
> - /*
> - * Neighbouring slots of a swap file can lie in two extents, and a folio
> - * is read with one bio. The flag is set in swapon() and stays, so the
> - * race is benign.
> - */
> - if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
> - return 0;
> -
> /*
> * Get a list of all the (large) orders below PMD_ORDER that are enabled
> * and suitable for swapping THP.
[ ... ]
> @@ -5114,10 +5092,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> if (!folio) {
> /* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */
> if (swap_entry_synchronous(si, entry) ||
> - (is_vswap_entry(entry) &&
> - swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO)))
> + swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO))
> folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
> - thp_swapin_suitable_orders(si, vmf) | BIT(0),
> + thp_swapin_suitable_orders(vmf) | BIT(0),
> vmf, NULL, 0);
> else
> folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf);
[Severity: Medium]
Does do_swap_page() now run mTHP PTE and cluster scans on every zswap swap-in
fault when mTHP orders are enabled?
With the !zswap_never_enabled() check removed from
thp_swapin_suitable_orders(), a fault where swap_entry_synchronous() matched
via zswap still takes the page table lock to scan neighboring PTEs and passes
a multi-order mask to swapin_sync(). __swap_cache_alloc_folio() then iterates
through each non-zero order, taking swap_cluster_lock() and scanning all
1 << order vtable slots in __vswap_check_backing() only to get -EBUSY for
VSWAP_ZSWAP and increment MTHP_STAT_SWPIN_FALLBACK before falling back to
order 0.
Passing BIT(0) directly when zswap matched in do_swap_page() would avoid the
extra lock contention and false fallback counts.
[ ... ]
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 72652a2099e9d..8f38737d8aa60 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -3484,15 +3391,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
> .pmd = pmd,
> };
>
> - if (is_vswap_entry(entry)) {
> - folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
> - BIT(0), &vmf, NULL, 0);
> - if (IS_ERR(folio))
> - folio = NULL;
> - } else {
> - folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
> - &vmf);
> - }
> + folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, BIT(0),
> + &vmf, NULL, 0);
> + if (IS_ERR(folio))
> + folio = NULL;
> }
[Severity: Medium]
Does switching swapoff unconditionally to swapin_sync() serialize all swapoff
I/O to unplugged, queue-depth-1 4 KiB reads?
Previously unuse_pte_range() called swapin_readahead() with block plugging
and readahead, while swapin_sync() submits a single order-0 BIO immediately
and the caller waits on folio_lock() before moving to the next PTE.
shmem_swapin_folio() has the same regression when swapoff_type >= 0,
bypassing shmem_swapin_cluster() and forcing order 0 so large shmem swap
entries are also split via shmem_split_large_entry().
On asynchronous swap devices (HDDs, SSDs, NVMe), this can severely degrade
swapoff throughput and latency.
[ ... ]
> diff --git a/mm/vswap.h b/mm/vswap.h
> index c02aa8f6da579..f3a55414c6b71 100644
> --- a/mm/vswap.h
> +++ b/mm/vswap.h
> @@ -341,8 +341,7 @@ static inline int vswap_check_backing(swp_entry_t entry, int nr,
> * range of physical swap slots.
> * @folio: a swap-cache resident folio
> *
> - * Return: %true if @folio->swap is not a vswap entry, or if these vswap
> - * entries are backed by a contiguous range of physical slots.
> + * Return: %true if the folio has a contiguous range of physical slots.
> */
> static inline bool folio_phys_swap_backed(struct folio *folio)
> {
> @@ -350,9 +349,8 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
> int nr = folio_nr_pages(folio);
> enum vswap_backing_type type;
>
> - return !is_vswap_entry(entry) ||
> - (vswap_check_backing(entry, nr, &type) == nr &&
> - type == VSWAP_SWAPFILE);
> + return vswap_check_backing(entry, nr, &type) == nr &&
> + type == VSWAP_SWAPFILE;
> }
>
> static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
> @@ -403,22 +401,18 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
>
> /*
> * Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...)
> - * for @entry. For a vswap entry the property belongs to the current
> + * for the virtual @entry. The property belongs to the current
> * physical backing rather than vswap_si itself; resolve to the backing
> * and test there. Returns false for zswap/zero/unbacked vswap entries
> * as they don't have a backing bdev.
> */
> -static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
> - swp_entry_t entry,
> +static inline bool swap_entry_backend_has_flag(swp_entry_t entry,
> unsigned long flag)
> {
> struct swap_info_struct *phys_si;
> swp_entry_t phys;
> bool has_flag;
>
> - if (!swap_is_vswap(si))
> - return data_race(si->flags & flag);
> -
> phys = vswap_to_phys(entry);
> if (!phys.val)
> return false;
[Severity: High]
Can dropping the lockless fast paths here add several virtual cluster
spinlock roundtrips to every swap-in fault?
With all ordinary swap entries now virtual, a single swap-in miss in
do_swap_page() calls vswap_lock_cluster() (an XArray lookup plus ci->lock
acquire/release) repeatedly on the same slot:
- swap_entry_synchronous() -> zswap_is_present(), which checks
is_vswap_entry() before zswap_never_enabled() and takes ci->lock even when
zswap was never enabled;
- swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO) on the next line,
plus get_swap_device()/put_swap_device() on the backing device;
- swap_read_folio() via vswap_to_phys(), even for zero-mapped or zswap
folios;
- should_try_to_free_swap(), which runs on both hits and misses and locks the
cluster twice back-to-back in folio_phys_swap_backed() and
swap_entry_backend_has_flag(folio->swap, SWP_SYNCHRONOUS_IO) while holding
the folio lock.
On fast NVMe or zram swap devices, this serializes concurrent faults within
the same swap cluster. Coalescing the backing lookup in do_swap_page() and
should_try_to_free_swap(), and checking zswap_never_enabled() first in
zswap_is_present(), would eliminate most of these redundant lock roundtrips.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=17
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (16 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
@ 2026-12-03 22:00 ` 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
` (2 subsequent siblings)
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Vswap is mandatory and supports 32-bit systems, so the vtable supplies
every zswap lookup for decompression and writeback.
Remove the redundant per-device XArrays and call the virtual entry
helpers directly.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
Documentation/admin-guide/mm/zswap.rst | 12 +-
include/linux/zswap.h | 13 --
mm/swapfile.c | 12 +-
mm/zswap.c | 185 +++----------------------
4 files changed, 28 insertions(+), 194 deletions(-)
diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/admin-guide/mm/zswap.rst
index 2464425c783d..6dbe6ed7112a 100644
--- a/Documentation/admin-guide/mm/zswap.rst
+++ b/Documentation/admin-guide/mm/zswap.rst
@@ -59,19 +59,15 @@ returned by the allocation routine and that handle must be mapped before being
accessed. The compressed memory pool grows on demand and shrinks as compressed
pages are freed. The pool is not preallocated.
-When a swap page is passed from swapout to zswap, zswap maintains a mapping of
-the swap entry, a combination of the swap type and swap offset, to the zsmalloc
-handle that references that compressed swap page. This mapping is achieved
-with an xarray per swap type. The swap offset is the search key for the xarray
-nodes.
+When zswap compresses a swap page, it stores the zsmalloc handle in a zswap
+entry. The corresponding virtual swap slot points to that entry in its vtable.
During a page fault on a PTE that is a swap entry, the swapin code calls the
zswap load function to decompress the page into the page allocated by the page
fault handler.
-Once there are no PTEs referencing a swap page stored in zswap (i.e. the count
-in the swap_map goes to 0) the swap code calls the zswap invalidate function
-to free the compressed entry.
+When the swap code releases the virtual swap slot, it frees any remaining
+compressed entry.
Zswap seeks to be simple in its policies. Sysfs attributes allow for one user
controlled policy:
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index b90f330f87cd..08bbb567b83e 100644
--- a/include/linux/zswap.h
+++ b/include/linux/zswap.h
@@ -28,10 +28,7 @@ struct zswap_lruvec_state {
unsigned long zswap_total_pages(void);
bool zswap_store(struct folio *folio);
int zswap_load(struct folio *folio);
-void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries);
void zswap_entry_free(struct zswap_entry *entry);
-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);
@@ -52,18 +49,8 @@ static inline int zswap_load(struct folio *folio)
return -ENOENT;
}
-static inline void zswap_invalidate(int type, pgoff_t offset,
- unsigned long nr_entries)
-{
-}
-
static inline void zswap_entry_free(struct zswap_entry *entry) {}
-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 8f38737d8aa6..d71fccce5ae3 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1603,9 +1603,6 @@ 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);
- if (!swap_is_vswap(si))
- zswap_invalidate(si->type, offset, nr_entries);
-
if (si->flags & SWP_BLKDEV)
swap_slot_free_notify =
si->bdev->bd_disk->fops->swap_slot_free_notify;
@@ -4086,7 +4083,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
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;
@@ -4715,10 +4711,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.
@@ -4727,7 +4719,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);
@@ -4761,8 +4753,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 e527a34e52a8..c4fb6af8b300 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -158,12 +158,6 @@ struct crypto_acomp_ctx {
struct zswap_acomp_req decomp; /* unused with on-stack requests */
};
-/*
- * 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.
- */
struct zswap_pool {
struct zs_pool *zs_pool;
struct crypto_acomp_ctx __percpu *acomp_ctx;
@@ -185,7 +179,11 @@ struct zswap_pool {
static DEFINE_XARRAY_FLAGS(zswap_pools, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH);
static struct zswap_pool __rcu *zswap_current_pool;
-/* Global LRU lists shared by all zswap pools. */
+/*
+ * Global LRU lists shared by all zswap pools. Releasing an entry from the
+ * vtable takes the cluster lock before the LRU lock. Writeback drops the
+ * LRU lock before revalidating the entry under the cluster lock.
+ */
static struct list_lru zswap_list_lru;
/* The lock protects zswap_next_shrink updates. */
@@ -200,7 +198,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 - virtual swap entry whose vtable slot holds this zswap entry
* 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
@@ -232,9 +230,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);
@@ -256,39 +251,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));
-}
-
-static struct zswap_entry *zswap_entry_load(swp_entry_t swp)
-{
- if (is_vswap_entry(swp))
- return vswap_zswap_load(swp);
- return xa_load(swap_zswap_tree(swp), swp_offset(swp));
-}
-
-static struct zswap_entry *zswap_entry_store(swp_entry_t swp,
- struct zswap_entry *entry)
-{
- if (is_vswap_entry(swp)) {
- vswap_zswap_store(swp, entry);
- return NULL;
- }
-
- return xa_store(swap_zswap_tree(swp), swp_offset(swp), entry,
- GFP_KERNEL);
-}
-
#define zswap_pool_debug(msg, p) \
pr_debug("%s pool %s\n", msg, (p)->tfm_name)
@@ -1138,7 +1100,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
* old compressed data. Only when this is successful can the entry
* be dereferenced.
*/
- if (entry != zswap_entry_load(swpentry)) {
+ if (entry != vswap_zswap_load(swpentry)) {
ret = -ENOMEM;
goto err;
}
@@ -1264,8 +1226,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o
*
* 1. We extract the swp_entry_t to the stack, allowing
* zswap_writeback_entry() to pin the swap entry and
- * then validate the zswap entry against that swap entry's
- * tree using pointer value comparison. Only when that
+ * then validate the pointer against the vtable slot. Only when that
* is successful can the entry be dereferenced.
*
* 2. Usually, objects are taken off the LRU for reclaim. In
@@ -1288,7 +1249,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 writeback verifies it against the vtable.
*/
swpentry = entry->swpentry;
@@ -1588,7 +1549,7 @@ static bool zswap_store_page(struct folio *folio, long index,
struct zswap_pool *pool)
{
swp_entry_t page_swpentry = folio_swap_entry(folio, index);
- struct zswap_entry *entry, *old;
+ struct zswap_entry *entry;
/* allocate entry */
entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));
@@ -1607,29 +1568,14 @@ static bool zswap_store_page(struct folio *folio, long index,
*/
entry->pool_idx = pool->idx;
- old = zswap_entry_store(page_swpentry, entry);
- if (xa_is_err(old)) {
- int err = xa_err(old);
-
- WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err);
- zswap_reject_alloc_fail++;
- goto store_failed;
- }
+ vswap_zswap_store(page_swpentry, entry);
/*
- * We may have had an existing entry that became stale when
- * the folio was redirtied and now the new version is being
- * swapped out. Get rid of the old.
- */
- if (old)
- zswap_entry_free(old);
-
- /*
- * The entry is successfully compressed and stored in the tree, there is
+ * The entry is successfully compressed and stored, so 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.
+ * when the entry is removed from the vtable.
*/
zswap_pool_get(pool);
if (objcg) {
@@ -1641,7 +1587,7 @@ 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.
+ * We finish initializing the entry while it's already in the vtable.
* This is safe because:
*
* 1. Concurrent stores and invalidations are excluded by folio lock.
@@ -1658,8 +1604,6 @@ static bool zswap_store_page(struct folio *folio, long index,
return true;
-store_failed:
- zs_free(pool->zs_pool, &entry->handle);
compress_failed:
zswap_entry_cache_free(entry);
return false;
@@ -1668,7 +1612,6 @@ static bool zswap_store_page(struct folio *folio, long index,
bool zswap_store(struct folio *folio)
{
long nr_pages = folio_nr_pages(folio);
- swp_entry_t swp = folio->swap;
struct obj_cgroup *objcg = NULL;
struct mem_cgroup *memcg = NULL;
struct zswap_pool *pool;
@@ -1730,17 +1673,10 @@ bool zswap_store(struct folio *folio)
return ret;
/*
- * If the zswap store fails or zswap is disabled, we must invalidate
- * the possibly stale entries which were previously stored at the
- * offsets corresponding to each page of the folio. Otherwise,
- * writeback could overwrite the new data in the swapfile.
+ * Drop any entries installed before a partial store failed.
*/
- if (is_vswap_entry(swp)) {
- if (index > 0)
- folio_release_non_phys_swap_backing(folio);
- } else {
- zswap_invalidate(swp_type(swp), swp_offset(swp), nr_pages);
- }
+ if (index > 0)
+ folio_release_non_phys_swap_backing(folio);
return ret;
}
@@ -1756,32 +1692,15 @@ bool zswap_store(struct folio *folio)
*/
bool zswap_is_present(swp_entry_t entry, unsigned int nr)
{
- pgoff_t offset = swp_offset(entry);
- struct xarray *tree;
- unsigned long index = offset;
unsigned int i;
- /* Virtual entries have no native zswap tree. */
- if (is_vswap_entry(entry)) {
- for (i = 0; i < nr; i++)
- if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i)))
- return true;
- return false;
+ for (i = 0; i < nr; i++) {
+ if (vswap_zswap_load(entry))
+ return true;
+ entry.val++;
}
- tree = swap_zswap_tree(entry);
-
- 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
- * "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 false;
}
/**
@@ -1817,7 +1736,7 @@ int zswap_load(struct folio *folio)
if (folio_test_large(folio))
return -ENOENT;
- entry = zswap_entry_load(swp);
+ entry = vswap_zswap_load(swp);
if (!entry)
return -ENOENT;
@@ -1847,64 +1766,6 @@ int zswap_load(struct folio *folio)
return 0;
}
-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;
-
- for (i = 0; i < nr_entries; i++) {
- tree = zswap_tree(type, offset + i);
-
- if (xa_empty(tree))
- continue;
-
- entry = xa_erase(tree, 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.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/zswap: zswap_is_present() drops zswap_never_enabled() fast
path
--
commit e9187c725f7b2b69c83eb8d861f7957d165a02ed
Author: Nhat Pham <nphamcs@gmail.com>
mm/zswap: store compressed entries only in the vtable
This commit removes the per-device zswap XArrays and switches zswap
lookups, stores, and writeback validation to use the vswap vtable helpers
directly.
> diff --git a/mm/zswap.c b/mm/zswap.c
> index e527a34e52a80..c4fb6af8b3008 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -1756,32 +1692,15 @@ bool zswap_store(struct folio *folio)
> */
> bool zswap_is_present(swp_entry_t entry, unsigned int nr)
> {
> - pgoff_t offset = swp_offset(entry);
> - struct xarray *tree;
> - unsigned long index = offset;
> unsigned int i;
>
> - /* Virtual entries have no native zswap tree. */
> - if (is_vswap_entry(entry)) {
> - for (i = 0; i < nr; i++)
> - if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i)))
> - return true;
> - return false;
> + for (i = 0; i < nr; i++) {
> + if (vswap_zswap_load(entry))
> + return true;
> + entry.val++;
> }
> - tree = swap_zswap_tree(entry);
> -
> - 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
> - * "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 false;
> }
[Severity: Medium]
Should zswap_is_present() keep the zswap_never_enabled() early return
before the loop, like zswap_load() does?
Because vswap_si does not set SWP_SYNCHRONOUS_IO, every swap-in cache
miss in do_swap_page() calls swap_entry_synchronous() ->
zswap_is_present(entry, 1).
Without the static-branch check, kernels with CONFIG_ZSWAP=y and zswap
disabled still walk si->cluster_info_pool and take ci->lock in
vswap_zswap_load() on every fault, right before
swap_entry_backend_has_flag() locks the same cluster again.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=18
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (17 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
@ 2026-12-03 22:00 ` 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-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
The physical allocator can reuse slots backing a cache-only
virtual entry. Recognize those slots from the reverse mapping
in both the full-cluster and range scans.
The last remaining caller of __try_to_reclaim_swap() drops a
virtual swap count. It already asks for unmapped folios or
physical backing held by a cgroup under swap pressure. Keep
that test directly and remove the flags.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swap_table.h | 5 ++++
mm/swapfile.c | 64 ++++++++++++-------------------------------------
2 files changed, 20 insertions(+), 49 deletions(-)
diff --git a/mm/swap_table.h b/mm/swap_table.h
index 4f4f3100e5f9..f723ce4f2fca 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -431,6 +431,11 @@ static inline bool swp_tb_is_vswap(unsigned long swp_tb)
return (swp_tb & SWP_TB_VSWAP_MARK_MASK) == SWP_TB_VSWAP_MARK;
}
+static inline bool swp_tb_is_vswap_cache_only(unsigned long swp_tb)
+{
+ return swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY);
+}
+
#ifdef CONFIG_SWAP
static inline unsigned long swp_entry_to_swp_tb_vswap(swp_entry_t entry)
{
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d71fccce5ae3..3f6c56c51c2c 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -188,16 +188,6 @@ static long swap_usage_in_pages(struct swap_info_struct *si)
return atomic_long_read(&si->inuse_pages) & SWAP_USAGE_COUNTER_MASK;
}
-/* Reclaim the swap entry anyway if possible */
-#define TTRS_ANYWAY 0x1
-/*
- * Reclaim the swap entry if there are no more mappings of the
- * corresponding page
- */
-#define TTRS_UNMAPPED 0x2
-/* Reclaim the swap entry if swap is getting full */
-#define TTRS_FULL 0x4
-
static bool swap_only_has_cache(struct swap_cluster_info *ci,
unsigned long offset, int nr_pages)
{
@@ -221,7 +211,7 @@ static bool swap_only_has_cache(struct swap_cluster_info *ci,
* folio was associated with the swap entry.
*/
static int __try_to_reclaim_swap(struct swap_info_struct *si,
- unsigned long offset, unsigned long flags)
+ unsigned long offset)
{
const swp_entry_t entry = swp_entry(si->type, offset);
struct swap_cluster_info *ci;
@@ -257,10 +247,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,
}
offset = swp_offset(folio->swap);
- need_reclaim = ((flags & TTRS_ANYWAY) ||
- ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) ||
- ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) &&
- folio_phys_swap_backed(folio)));
+ need_reclaim = !folio_mapped(folio) ||
+ (mem_cgroup_swap_full(folio) && folio_phys_swap_backed(folio));
if (!need_reclaim || !folio_swapcache_freeable(folio))
goto out_unlock;
@@ -1014,19 +1002,13 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
spin_unlock(&ci->lock);
do {
swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
- if (swp_tb_is_vswap(swp_tb) &&
- (swp_tb & SWP_RMAP_CACHE_ONLY)) {
- vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
- if (try_to_reclaim_vswap_backing(si, offset,
- vswap_entry) < 0)
- break;
+ if (swp_tb_is_null(swp_tb))
continue;
- }
- if (swp_tb_get_count(swp_tb))
+ if (!swp_tb_is_vswap_cache_only(swp_tb))
+ break;
+ vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
+ if (try_to_reclaim_vswap_backing(si, offset, vswap_entry) < 0)
break;
- if (swp_tb_is_folio(swp_tb))
- if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0)
- break;
} while (++offset < end);
spin_lock(&ci->lock);
@@ -1057,8 +1039,7 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
return true;
}
-static bool cluster_scan_range(struct swap_info_struct *si,
- struct swap_cluster_info *ci,
+static bool cluster_scan_range(struct swap_cluster_info *ci,
unsigned long offset, unsigned int nr_pages,
bool *need_reclaim)
{
@@ -1070,16 +1051,12 @@ 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;
- if ((swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) ||
- (swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY))) {
- /* vswap slots are abundant; never reclaim to reuse one */
- if (swap_is_vswap(si) || !vm_swap_full())
- return false;
+ if (swp_tb_is_vswap_cache_only(swp_tb) && vm_swap_full()) {
*need_reclaim = true;
continue;
}
/* Slot with zero count can only be NULL or folio */
- VM_WARN_ON(!swp_tb_get_count(swp_tb));
+ VM_WARN_ON(swp_tb_is_shadow(swp_tb) && !__swp_tb_get_count(swp_tb));
return false;
} while (++ci_off < ci_end);
@@ -1194,7 +1171,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
for (end -= nr_pages; offset <= end; offset += nr_pages) {
need_reclaim = false;
- if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim))
+ if (!cluster_scan_range(ci, offset, nr_pages, &need_reclaim))
continue;
if (need_reclaim) {
ret = cluster_reclaim_range(si, ci, offset, order, &usable);
@@ -1318,17 +1295,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
while (offset < end) {
swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
- if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
- spin_unlock(&ci->lock);
- nr_reclaim = __try_to_reclaim_swap(si, offset,
- TTRS_ANYWAY);
- spin_lock(&ci->lock);
- if (nr_reclaim) {
- offset += abs(nr_reclaim);
- continue;
- }
- } else if (swp_tb_is_vswap(swp_tb) &&
- (swp_tb & SWP_RMAP_CACHE_ONLY)) {
+ if (swp_tb_is_vswap_cache_only(swp_tb)) {
vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
spin_unlock(&ci->lock);
nr_reclaim = try_to_reclaim_vswap_backing(si, offset,
@@ -1923,8 +1890,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
return;
do {
- nr_reclaimed = __try_to_reclaim_swap(si, offset,
- TTRS_UNMAPPED | TTRS_FULL);
+ nr_reclaimed = __try_to_reclaim_swap(si, offset);
offset++;
if (nr_reclaimed)
offset = round_up(offset, abs(nr_reclaimed));
@@ -2795,7 +2761,7 @@ static bool folio_swapcache_freeable(struct folio *folio)
/*
* Once hibernation has begun to create its image of memory,
* there's a danger that one of the calls to folio_free_swap()
- * - most probably a call from __try_to_reclaim_swap() while
+ * - most probably a call from try_to_reclaim_vswap_backing() while
* hibernation is allocating its own swap pages for the image,
* but conceivably even a call from memory reclaim - will free
* the swap from a folio which has already been recorded in the
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm: folio_realloc_swap() omits SWP_RMAP_CACHE_ONLY on
zero-count vswap slots
- [Medium] mm: try_to_reclaim_vswap_backing() returns 0 on trylock
failure causing retries
--
commit 52440fcbb69abc8d4e57c8cf9a12927331e6d4fd
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: reclaim physical backing through reverse mappings
This commit switches physical swap range and full-cluster reclaim to
identify cache-only slots via the vswap reverse mapping
(SWP_RMAP_CACHE_ONLY) and removes the unused TTRS_* flags from
__try_to_reclaim_swap().
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d71fccce5ae31..3f6c56c51c2c0 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1014,19 +1002,13 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
> spin_unlock(&ci->lock);
> do {
> swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
> - if (swp_tb_is_vswap(swp_tb) &&
> - (swp_tb & SWP_RMAP_CACHE_ONLY)) {
> - vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
> - if (try_to_reclaim_vswap_backing(si, offset,
> - vswap_entry) < 0)
> - break;
> + if (swp_tb_is_null(swp_tb))
> continue;
> - }
> - if (swp_tb_get_count(swp_tb))
> + if (!swp_tb_is_vswap_cache_only(swp_tb))
> + break;
> + vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
> + if (try_to_reclaim_vswap_backing(si, offset, vswap_entry) < 0)
> break;
> - if (swp_tb_is_folio(swp_tb))
> - if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0)
> - break;
> } while (++offset < end);
> spin_lock(&ci->lock);
[Severity: Medium]
Can a concurrently locked folio cause cluster_reclaim_range() to keep
scanning and reclaiming slots in an unallocatable range?
Unlike __try_to_reclaim_swap(), try_to_reclaim_vswap_backing() returns 0
on folio_trylock() failure:
mm/swapfile.c:try_to_reclaim_vswap_backing() {
...
if (!folio_trylock(folio)) {
/* Unvalidated: the folio may not even own @offset. */
folio_put(folio);
return 0;
}
...
}
Because the return value is not negative, cluster_reclaim_range() does
not break. It repeats swap_cache_get_folio() XArray lookups and trylock
attempts on every subpage of a locked large folio, or frees and
redirties later folios in a range whose post-loop check will fail.
swap_reclaim_full_clusters() hits the same 0 return and advances one
slot at a time with full ci->lock unlock/lock cycles instead of skipping
the folio. Returning -1 on trylock failure would at least let
cluster_reclaim_range() break immediately without relying on the
unvalidated folio size.
[ ... ]
> @@ -1070,16 +1051,12 @@ 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;
> - if ((swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) ||
> - (swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY))) {
> - /* vswap slots are abundant; never reclaim to reuse one */
> - if (swap_is_vswap(si) || !vm_swap_full())
> - return false;
> + if (swp_tb_is_vswap_cache_only(swp_tb) && vm_swap_full()) {
> *need_reclaim = true;
> continue;
> }
> /* Slot with zero count can only be NULL or folio */
> - VM_WARN_ON(!swp_tb_get_count(swp_tb));
> + VM_WARN_ON(swp_tb_is_shadow(swp_tb) && !__swp_tb_get_count(swp_tb));
> return false;
> } while (++ci_off < ci_end);
[Severity: Medium]
Can physical swap slots backing a zero-count vswap entry end up missing
SWP_RMAP_CACHE_ONLY and become invisible to physical reclaim?
SWP_RMAP_CACHE_ONLY is only set by swap_put_entries_cluster() ->
vswap_mark_cache_only() when swap count drops from 1 to 0 while vtable
already holds VSWAP_SWAPFILE. However, folio_realloc_swap() installs
physical backing (allocated with SWP_RMAP_CACHE_ONLY clear) without
checking whether any virtual slot already has swap count 0:
mm/swapfile.c:folio_realloc_swap() {
...
for (i = 0; i < nr; i++) {
pe.val = phys_entry.val + i;
__vtable_set(ci_dyn, voff + i, vtable_mk_phys(pe));
}
spin_unlock(&ci->lock);
...
}
On the disk-only (!may_zswap) path, folio_alloc_swap() calls
folio_realloc_swap() right after vswap_alloc() while swap count is 0.
If try_to_unmap() fails in shrink_folio_list() while swap is not full,
the folio stays cached with count 0 and SWP_RMAP_CACHE_ONLY clear.
Because swap count is already 0, swap_put_entries_cluster() will not run
again to mark the rmap. Those physical slots are then skipped by
cluster_scan_range() and swap_reclaim_full_clusters(), stranding
physical swap capacity and blocking large-order reuse.
Checking !__swp_tb_get_count(__swap_table_get(ci, voff + i)) under
ci->lock in folio_realloc_swap() and calling swap_rmap_mark_cache_only()
would keep the rmap flag in sync.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=19
^ permalink raw reply [flat|nested] 43+ messages in thread
* [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (18 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
@ 2026-12-03 22:00 ` Nhat Pham
2026-12-04 22:09 ` sashiko-bot
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
20 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 22:00 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, linux-doc, cgroups, Tejun Heo, Michal Koutný
Physical swap tables hold reverse mappings and marks for bad and raw
hibernation slots. Ordinary swap uses the virtual slot for its cgroup
owner, swap count and zero state. Hibernation accounts its slots to the
root cgroup directly.
Move the cgroup table, overflow count table and zero bitmap pointers
into the vswap cluster descriptor. This saves 16 bytes per physical
cluster on x86-64 with CONFIG_MEMCG without growing the virtual
descriptor. Where the zero flag does not fit in the swap table, allocate
the separate bitmap only for virtual clusters.
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/memcontrol-v1.c | 4 +-
mm/memcontrol.c | 6 +-
mm/swap.h | 14 ++--
mm/swap_state.c | 6 +-
mm/swap_table.h | 51 +++++++-----
mm/swapfile.c | 194 ++++++++++++++++++++++++++-------------------
mm/vswap.h | 6 +-
7 files changed, 161 insertions(+), 120 deletions(-)
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 570bb5006328..cd365873861f 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -313,7 +313,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
if (nr_swapfile)
mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
- __swap_cgroup_set(ci, start, nr_entries, private_id);
+ __swap_cgroup_set(ci_dyn, start, nr_entries, private_id);
folio_unqueue_deferred_split(folio);
folio->memcg_data = 0;
@@ -383,8 +383,8 @@ void memcg1_swapin(struct folio *folio)
nr_pages = folio_nr_pages(folio);
start = swp_cluster_offset(folio->swap);
ci = swap_cluster_get_and_lock(folio);
- id = __swap_cgroup_clear(ci, start, nr_pages);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ id = __swap_cgroup_clear(ci_dyn, start, nr_pages);
for (i = start; i < start + nr_pages; i++)
if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
nr_swapfile++;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 03f78a11138f..58478af5696b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6093,7 +6093,7 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
for (i = start; i < end; i++)
if (__swp_tb_get_count(__swap_table_get(ci, i)))
- __swap_cgroup_set(ci, i, 1, id);
+ __swap_cgroup_set(ci_dyn, i, 1, id);
}
#endif
@@ -6185,10 +6185,12 @@ int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages)
*/
void __mem_cgroup_swap_record(struct folio *folio, unsigned short id)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
ci = swap_cluster_get_and_lock(folio);
- __swap_cgroup_set(ci, swp_cluster_offset(folio->swap),
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ __swap_cgroup_set(ci_dyn, swp_cluster_offset(folio->swap),
folio_nr_pages(folio), id);
swap_cluster_unlock(ci);
}
diff --git a/mm/swap.h b/mm/swap.h
index 0d87de6eaddf..1bf4821cdfef 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -57,13 +57,6 @@ struct swap_cluster_info {
u8 flags;
u8 order;
atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */
- unsigned int *extend_table; /* For large swap count, protected by ci->lock */
-#ifdef CONFIG_MEMCG
- struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
-#endif
-#if !SWAP_TABLE_HAS_ZEROFLAG
- unsigned long *zero_bitmap;
-#endif
struct list_head list;
};
@@ -72,6 +65,13 @@ struct swap_cluster_info_dynamic {
unsigned int index; /* for cluster_index() */
struct rcu_head rcu;
atomic_long_t *virtual_table; /* Backing pointers for vswap slots */
+ unsigned int *extend_table; /* For large swap count, protected by ci->lock */
+#ifdef CONFIG_MEMCG
+ struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
+#endif
+#if !SWAP_TABLE_HAS_ZEROFLAG
+ unsigned long *zero_bitmap;
+#endif
};
/* All on-list cluster must have a non-zero flag. */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index b03871d63408..003147bd7ac4 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -201,10 +201,11 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
*/
if (!swp_tb_is_shadow(old_tb) || !__swp_tb_get_count(old_tb))
return -ENOENT;
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
if (shadowp)
*shadowp = swp_tb_to_shadow(old_tb);
if (memcg_id)
- *memcg_id = __swap_cgroup_get(ci, ci_off);
+ *memcg_id = __swap_cgroup_get(ci_dyn, ci_off);
if (nr == 1)
return 0;
@@ -214,7 +215,6 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
* (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in
* __swap_cache_alloc_folio will retry with a smaller order on -EBUSY.
*/
- ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr), nr, &type);
if (ret != nr || type == VSWAP_ZSWAP)
return -EBUSY;
@@ -240,7 +240,7 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
!swp_tb_is_shadow(old_tb) ||
!__swp_tb_get_count(old_tb) ||
is_zero != __swap_table_test_zero(ci, ci_off) ||
- (memcg_id && *memcg_id != __swap_cgroup_get(ci, ci_off))))
+ (memcg_id && *memcg_id != __swap_cgroup_get(ci_dyn, ci_off))))
return -EBUSY;
} while (++ci_off < ci_end);
diff --git a/mm/swap_table.h b/mm/swap_table.h
index f723ce4f2fca..bd1d9909113c 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -316,8 +316,11 @@ static inline void __swap_table_set_zero(struct swap_cluster_info *ci,
swp_tb |= SWP_TB_ZERO_FLAG;
__swap_table_set(ci, ci_off, swp_tb);
#else
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
lockdep_assert_held(&ci->lock);
- __set_bit(ci_off, ci->zero_bitmap);
+ __set_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
@@ -330,7 +333,10 @@ static inline bool __swap_table_test_zero(struct swap_cluster_info *ci,
VM_WARN_ON(!swp_tb_is_countable(swp_tb));
return !!(swp_tb & SWP_TB_ZERO_FLAG);
#else
- return test_bit(ci_off, ci->zero_bitmap);
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ return test_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
@@ -344,62 +350,67 @@ static inline void __swap_table_clear_zero(struct swap_cluster_info *ci,
swp_tb &= ~SWP_TB_ZERO_FLAG;
__swap_table_set(ci, ci_off, swp_tb);
#else
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
lockdep_assert_held(&ci->lock);
- __clear_bit(ci_off, ci->zero_bitmap);
+ __clear_bit(ci_off, ci_dyn->zero_bitmap);
#endif
}
#ifdef CONFIG_MEMCG
-static inline void __swap_cgroup_set(struct swap_cluster_info *ci,
- unsigned int ci_off, unsigned long nr, unsigned short id)
+static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off, unsigned long nr,
+ unsigned short id)
{
- lockdep_assert_held(&ci->lock);
+ lockdep_assert_held(&ci_dyn->ci.lock);
VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER);
- if (WARN_ON_ONCE(!ci->memcg_table))
+ if (WARN_ON_ONCE(!ci_dyn->memcg_table))
return;
do {
- ci->memcg_table->id[ci_off++] = id;
+ ci_dyn->memcg_table->id[ci_off++] = id;
} while (--nr);
}
-static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
- lockdep_assert_held(&ci->lock);
+ lockdep_assert_held(&ci_dyn->ci.lock);
VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER);
- if (unlikely(!ci->memcg_table))
+ if (unlikely(!ci_dyn->memcg_table))
return 0;
- return ci->memcg_table->id[ci_off];
+ return ci_dyn->memcg_table->id[ci_off];
}
-static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off,
unsigned long nr)
{
- unsigned short old = __swap_cgroup_get(ci, ci_off);
+ unsigned short old = __swap_cgroup_get(ci_dyn, ci_off);
if (!old)
return 0;
do {
- VM_WARN_ON_ONCE(ci->memcg_table->id[ci_off] != old);
- ci->memcg_table->id[ci_off++] = 0;
+ VM_WARN_ON_ONCE(ci_dyn->memcg_table->id[ci_off] != old);
+ ci_dyn->memcg_table->id[ci_off++] = 0;
} while (--nr);
return old;
}
#else
-static inline void __swap_cgroup_set(struct swap_cluster_info *ci,
- unsigned int ci_off, unsigned long nr, unsigned short id)
+static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off, unsigned long nr,
+ unsigned short id)
{
}
-static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
return 0;
}
-static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci,
+static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off,
unsigned long nr)
{
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 3f6c56c51c2c..a9ee5bd1f86b 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -443,19 +443,21 @@ static void swap_cluster_free_count_table(struct swap_table *table)
swap_cluster_free_table_folio_rcu_cb);
}
-static void swap_cluster_free_table(struct swap_cluster_info *ci)
+static void swap_cluster_free_table(struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn)
{
struct swap_table *table;
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- kfree(ci->memcg_table);
- ci->memcg_table = NULL;
+ kfree(ci_dyn->memcg_table);
+ ci_dyn->memcg_table = NULL;
#endif
-
#if !SWAP_TABLE_HAS_ZEROFLAG
- kfree(ci->zero_bitmap);
- ci->zero_bitmap = NULL;
+ bitmap_free(ci_dyn->zero_bitmap);
+ ci_dyn->zero_bitmap = NULL;
#endif
+ }
table = (struct swap_table *)rcu_access_pointer(ci->table);
if (!table)
@@ -471,12 +473,12 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
*/
static int swap_cluster_alloc_table(struct swap_info_struct *si,
struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
gfp_t gfp, bool locked)
{
struct swap_table *table = NULL;
#ifdef CONFIG_MEMCG
struct swap_memcg_table *memcg_table = NULL;
- bool vswap = si && swap_is_vswap(si);
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
unsigned long *zero_bitmap = NULL;
@@ -494,19 +496,20 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
if (!table)
goto lock;
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- if (vswap && !mem_cgroup_disabled()) {
- memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
- if (!memcg_table)
- goto lock;
- }
+ if (!mem_cgroup_disabled()) {
+ memcg_table = kzalloc_obj(*memcg_table, gfp);
+ if (!memcg_table)
+ goto lock;
+ }
#endif
-
#if !SWAP_TABLE_HAS_ZEROFLAG
- zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
- if (!zero_bitmap)
- goto lock;
+ zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
+ if (!zero_bitmap)
+ goto lock;
#endif
+ }
ret = 0;
lock:
@@ -528,14 +531,16 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
/* The cluster must be empty and not on any list during allocation. */
VM_WARN_ON_ONCE(!cluster_is_empty(ci));
+ if (ci_dyn) {
#ifdef CONFIG_MEMCG
- VM_WARN_ON_ONCE(ci->memcg_table);
- ci->memcg_table = memcg_table;
+ VM_WARN_ON_ONCE(ci_dyn->memcg_table);
+ ci_dyn->memcg_table = memcg_table;
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
- VM_WARN_ON_ONCE(ci->zero_bitmap);
- ci->zero_bitmap = zero_bitmap;
+ VM_WARN_ON_ONCE(ci_dyn->zero_bitmap);
+ ci_dyn->zero_bitmap = zero_bitmap;
#endif
+ }
/*
* Make tables visible to cluster_is_usable() after everything is
@@ -562,6 +567,7 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
* bad slots for swapoff, and non-swapoff path must never free bad slots.
*/
static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off, unsigned int nr,
bool swapoff)
{
@@ -578,11 +584,12 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
bad_slots++;
else
WARN_ON_ONCE(!swp_tb_is_null(swp_tb));
- WARN_ON_ONCE(__swap_cgroup_get(ci, ci_off));
+ if (ci_dyn)
+ WARN_ON_ONCE(__swap_cgroup_get(ci_dyn, ci_off));
} while (++ci_off < ci_end);
WARN_ON_ONCE(bad_slots != (swapoff ? ci->count : 0));
- WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci->extend_table);
+ WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci_dyn && ci_dyn->extend_table);
}
/*
@@ -606,7 +613,7 @@ swap_cluster_populate(struct swap_info_struct *si,
lockdep_assert_held(&si->global_cluster_lock);
lockdep_assert_held(&ci->lock);
- if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ if (!swap_cluster_alloc_table(si, ci, NULL, __GFP_HIGH | __GFP_NOMEMALLOC |
__GFP_NOWARN, true))
return ci;
@@ -620,7 +627,7 @@ swap_cluster_populate(struct swap_info_struct *si,
spin_unlock(&si->global_cluster_lock);
local_unlock(&percpu_swap_cluster.lock);
- ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+ ret = swap_cluster_alloc_table(si, ci, NULL, __GFP_HIGH | __GFP_NOMEMALLOC |
GFP_KERNEL, false);
/*
@@ -674,14 +681,15 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si,
static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci)
{
- swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false);
- swap_cluster_free_table(ci);
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
- if (swap_is_vswap(si)) {
- struct swap_cluster_info_dynamic *ci_dyn;
+ if (swap_is_vswap(si))
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ swap_cluster_assert_empty(ci, ci_dyn, 0, SWAPFILE_CLUSTER, false);
+ swap_cluster_free_table(ci, ci_dyn);
+ if (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);
vswap_cluster_free_vtable(ci);
@@ -910,7 +918,11 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
need_table = !ci->count;
/* Need to allocate swap table first for initial bad slot marking. */
if (need_table) {
- ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
+
+ if (alloc_si)
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = swap_cluster_alloc_table(alloc_si, ci, ci_dyn, GFP_KERNEL, false);
if (ret)
goto out;
} else {
@@ -1088,10 +1100,14 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
* The slot carries no swap count and is freed by offset.
*/
if (likely(folio)) {
+ struct swap_cluster_info_dynamic *ci_dyn = NULL;
+
order = folio_order(folio);
nr_pages = 1 << order;
- swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
- if (!swap_is_vswap(si)) {
+ if (swap_is_vswap(si))
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ swap_cluster_assert_empty(ci, ci_dyn, ci_off, nr_pages, false);
+ if (!ci_dyn) {
/*
* Folio already in the swap cache: we are allocating
* physical backing for its vswap entry. Point each
@@ -1115,7 +1131,7 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
} else if (IS_ENABLED(CONFIG_HIBERNATION)) {
order = 0;
nr_pages = 1;
- swap_cluster_assert_empty(ci, ci_off, 1, false);
+ swap_cluster_assert_empty(ci, NULL, ci_off, 1, false);
/* Exclusively owned by hibernation, must never enter the swap cache */
__swap_table_set(ci, ci_off, SWP_TB_HIB);
} else {
@@ -1249,7 +1265,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
- if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
+ if (swap_cluster_alloc_table(si, ci, ci_dyn, GFP_ATOMIC | __GFP_NOWARN, true)) {
spin_unlock(&ci->lock);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1257,7 +1273,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
if (vswap_cluster_alloc_vtable(ci_dyn, GFP_ATOMIC | __GFP_NOWARN)) {
spin_unlock(&ci->lock);
- swap_cluster_free_table(ci);
+ swap_cluster_free_table(ci, ci_dyn);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
}
@@ -1266,7 +1282,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
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);
+ swap_cluster_free_table(ci, ci_dyn);
vswap_cluster_free_vtable(&ci_dyn->ci);
kfree(ci_dyn);
return SWAP_ENTRY_INVALID;
@@ -1718,14 +1734,14 @@ static bool swap_sync_discard(void)
return false;
}
-static int swap_extend_table_alloc(struct swap_info_struct *si,
- struct swap_cluster_info *ci,
+static int swap_extend_table_alloc(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off, gfp_t gfp)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
void *table;
- table = kzalloc(sizeof(ci->extend_table[0]) * SWAPFILE_CLUSTER, gfp);
+ table = kzalloc(sizeof(ci_dyn->extend_table[0]) * SWAPFILE_CLUSTER, gfp);
if (!table)
return -ENOMEM;
@@ -1741,10 +1757,10 @@ static int swap_extend_table_alloc(struct swap_info_struct *si,
count = swp_tb_get_count(__swap_table_get(ci, ci_off));
if (count < (SWP_TB_COUNT_MAX - 1))
goto out_free;
- if (ci->extend_table)
+ if (ci_dyn->extend_table)
goto out_free;
- ci->extend_table = table;
+ ci_dyn->extend_table = table;
spin_unlock(&ci->lock);
return 0;
@@ -1759,6 +1775,7 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
int ret;
struct swap_info_struct *si;
struct swap_cluster_info *ci;
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned long offset = swp_offset(entry);
si = get_swap_device(entry);
@@ -1767,32 +1784,34 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
/* 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);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ ret = swap_extend_table_alloc(ci_dyn, swp_cluster_offset(entry), gfp);
put_swap_device(si);
return ret;
}
-static void swap_extend_table_try_free(struct swap_cluster_info *ci)
+static void swap_extend_table_try_free(struct swap_cluster_info_dynamic *ci_dyn)
{
unsigned long i;
- if (!ci->extend_table)
+ if (!ci_dyn->extend_table)
return;
for (i = 0; i < SWAPFILE_CLUSTER; i++) {
- if (ci->extend_table[i])
+ if (ci_dyn->extend_table[i])
return;
}
- kfree(ci->extend_table);
- ci->extend_table = NULL;
+ kfree(ci_dyn->extend_table);
+ ci_dyn->extend_table = NULL;
}
/* Decrease the swap count of one slot, without freeing it */
-static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
- unsigned int ci_off)
+static void __swap_cluster_put_entry(struct swap_cluster_info_dynamic *ci_dyn,
+ unsigned int ci_off)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
unsigned long swp_tb;
@@ -1804,15 +1823,15 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
VM_WARN_ON_ONCE(count > SWP_TB_COUNT_MAX);
if (count == SWP_TB_COUNT_MAX) {
- count = ci->extend_table[ci_off];
+ count = ci_dyn->extend_table[ci_off];
/* Overflow starts with SWP_TB_COUNT_MAX */
VM_WARN_ON_ONCE(count < SWP_TB_COUNT_MAX);
count--;
if (count == (SWP_TB_COUNT_MAX - 1)) {
- ci->extend_table[ci_off] = 0;
+ ci_dyn->extend_table[ci_off] = 0;
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count));
} else {
- ci->extend_table[ci_off] = count;
+ ci_dyn->extend_table[ci_off] = count;
}
} else {
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, --count));
@@ -1825,7 +1844,7 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci,
* also possible that a pending dup just attached an extend table.
*/
if (unlikely(count == SWP_TB_COUNT_MAX - 2 || count == SWP_TB_COUNT_MAX - 1))
- swap_extend_table_try_free(ci);
+ swap_extend_table_try_free(ci_dyn);
}
/**
@@ -1845,6 +1864,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
pgoff_t offset, int nr,
bool reclaim_cache)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
unsigned int ci_off, ci_end;
pgoff_t end = offset + nr;
@@ -1854,6 +1874,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
int ci_batch = -1;
ci = swap_cluster_lock(si, offset);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
ci_off = offset % SWAPFILE_CLUSTER;
ci_end = ci_off + nr;
do {
@@ -1875,7 +1896,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
* slots will be freed when folio is removed from swap cache
* (__swap_cache_del_folio).
*/
- __swap_cluster_put_entry(ci, ci_off);
+ __swap_cluster_put_entry(ci_dyn, ci_off);
if (ci_batch != -1) {
__swap_cluster_free_entries(si, ci, ci_batch, ci_off - ci_batch);
ci_batch = -1;
@@ -1898,16 +1919,17 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
}
/* Increase the swap count of one slot. */
-static int __swap_cluster_dup_entry(struct swap_cluster_info *ci,
+static int __swap_cluster_dup_entry(struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_off)
{
+ struct swap_cluster_info *ci = &ci_dyn->ci;
int count;
unsigned long swp_tb;
lockdep_assert_held(&ci->lock);
swp_tb = __swap_table_get(ci, ci_off);
/* Bad or special slots can't be handled */
- if (WARN_ON_ONCE(swp_tb_is_bad(swp_tb)))
+ if (WARN_ON_ONCE(!swp_tb_is_countable(swp_tb)))
return -EINVAL;
count = __swp_tb_get_count(swp_tb);
/* Must be either cached or have a count already */
@@ -1916,19 +1938,19 @@ static int __swap_cluster_dup_entry(struct swap_cluster_info *ci,
if (likely(count < (SWP_TB_COUNT_MAX - 1))) {
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count + 1));
- VM_WARN_ON_ONCE(ci->extend_table && ci->extend_table[ci_off]);
+ VM_WARN_ON_ONCE(ci_dyn->extend_table && ci_dyn->extend_table[ci_off]);
} else if (count == (SWP_TB_COUNT_MAX - 1)) {
- if (ci->extend_table) {
- VM_WARN_ON_ONCE(ci->extend_table[ci_off]);
- ci->extend_table[ci_off] = SWP_TB_COUNT_MAX;
+ if (ci_dyn->extend_table) {
+ VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off]);
+ ci_dyn->extend_table[ci_off] = SWP_TB_COUNT_MAX;
__swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, SWP_TB_COUNT_MAX));
} else {
return -ENOMEM;
}
} else if (count == SWP_TB_COUNT_MAX) {
- VM_WARN_ON_ONCE(ci->extend_table[ci_off] >=
- type_max(typeof(ci->extend_table[0])));
- ++ci->extend_table[ci_off];
+ VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off] >=
+ type_max(typeof(ci_dyn->extend_table[0])));
+ ++ci_dyn->extend_table[ci_off];
} else {
/* Never happens unless counting went wrong */
WARN_ON_ONCE(1);
@@ -1952,6 +1974,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
pgoff_t offset, int nr)
{
int err;
+ struct swap_cluster_info_dynamic *ci_dyn;
struct swap_cluster_info *ci;
unsigned int ci_start, ci_off, ci_end;
@@ -1959,13 +1982,14 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
ci_end = ci_start + nr;
ci_off = ci_start;
ci = swap_cluster_lock(si, offset);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
restart:
do {
- err = __swap_cluster_dup_entry(ci, ci_off);
+ err = __swap_cluster_dup_entry(ci_dyn, ci_off);
if (unlikely(err)) {
if (err == -ENOMEM) {
spin_unlock(&ci->lock);
- err = swap_extend_table_alloc(si, ci, ci_off, GFP_ATOMIC);
+ err = swap_extend_table_alloc(ci_dyn, ci_off, GFP_ATOMIC);
spin_lock(&ci->lock);
if (!err)
goto restart;
@@ -1978,7 +2002,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return 0;
failed:
while (ci_off-- > ci_start)
- __swap_cluster_put_entry(ci, ci_off);
+ __swap_cluster_put_entry(ci_dyn, ci_off);
swap_cluster_unlock(ci);
return err;
}
@@ -2216,11 +2240,11 @@ void __vswap_release_backing(struct swap_cluster_info *ci,
lockdep_assert_held(&ci->lock);
ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
- batch_id = __swap_cgroup_get(ci, ci_start);
+ batch_id = __swap_cgroup_get(ci_dyn, ci_start);
for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) {
vt = __vtable_get(ci_dyn, ci_off);
- cur_id = __swap_cgroup_get(ci, ci_off);
+ cur_id = __swap_cgroup_get(ci_dyn, ci_off);
if (cur_id != batch_id) {
mem_cgroup_swap_uncharge(batch_id, batch_nr,
@@ -2391,7 +2415,7 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
spin_unlock(&ci->lock);
return vtable_to_phys(vt);
}
- memcg_id = __swap_cgroup_get(ci, voff);
+ memcg_id = __swap_cgroup_get(ci_dyn, voff);
spin_unlock(&ci->lock);
phys_entry = folio_alloc_phys_swap(folio);
@@ -2558,12 +2582,13 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
*/
static void __swap_cluster_finish_free(struct swap_info_struct *si,
struct swap_cluster_info *ci,
+ struct swap_cluster_info_dynamic *ci_dyn,
unsigned int ci_start,
unsigned int nr_pages)
{
lockdep_assert_held(&ci->lock);
swap_range_free(si, cluster_offset(si, ci) + ci_start, nr_pages);
- swap_cluster_assert_empty(ci, ci_start, nr_pages, false);
+ swap_cluster_assert_empty(ci, ci_dyn, ci_start, nr_pages, false);
if (!ci->count)
free_cluster(si, ci);
@@ -2582,12 +2607,9 @@ static void __swap_cluster_free_phys_slots(struct swap_info_struct *si,
lockdep_assert_held(&ci->lock);
VM_WARN_ON(ci->count < nr_pages);
ci->count -= nr_pages;
- for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) {
+ for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++)
__swap_table_set(ci, ci_off, null_to_swp_tb());
- if (!SWAP_TABLE_HAS_ZEROFLAG)
- __swap_table_clear_zero(ci, ci_off);
- }
- __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
+ __swap_cluster_finish_free(si, ci, NULL, ci_start, nr_pages);
}
/*
@@ -2611,6 +2633,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
struct swap_cluster_info *ci,
unsigned int ci_start, unsigned int nr_pages)
{
+ struct swap_cluster_info_dynamic *ci_dyn;
unsigned long old_tb;
unsigned short batch_id = 0, id_cur;
unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
@@ -2618,6 +2641,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
VM_WARN_ON(ci->count < nr_pages);
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
__vswap_release_backing(ci, ci_start, nr_pages);
ci->count -= nr_pages;
@@ -2640,7 +2664,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
* backing of vswap entries was already uncharged by
* __vswap_release_backing().
*/
- id_cur = __swap_cgroup_clear(ci, ci_off, 1);
+ id_cur = __swap_cgroup_clear(ci_dyn, ci_off, 1);
if (batch_id != id_cur) {
if (batch_id)
mem_cgroup_swap_put(batch_id,
@@ -2653,7 +2677,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
if (batch_id)
mem_cgroup_swap_put(batch_id, ci_off - batch_off);
- __swap_cluster_finish_free(si, ci, ci_start, nr_pages);
+ __swap_cluster_finish_free(si, ci, ci_dyn, ci_start, nr_pages);
}
int __swap_count(swp_entry_t entry)
@@ -2700,8 +2724,12 @@ int swp_swapcount(swp_entry_t entry)
ci = swap_cluster_lock(si, swp_offset(entry));
swp_tb = __swap_table_get(ci, swp_cluster_offset(entry));
count = swp_tb_get_count(swp_tb);
- if (count == SWP_TB_COUNT_MAX)
- count = ci->extend_table[swp_cluster_offset(entry)];
+ if (count == SWP_TB_COUNT_MAX) {
+ struct swap_cluster_info_dynamic *ci_dyn;
+
+ ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+ count = ci_dyn->extend_table[swp_cluster_offset(entry)];
+ }
swap_cluster_unlock(ci);
put_swap_device(si);
@@ -3879,9 +3907,9 @@ static void free_swap_cluster_info(struct swap_info_struct *si,
ci = &ci_dyn->ci;
spin_lock(&ci->lock);
if (cluster_table_is_alloced(ci)) {
- swap_cluster_assert_empty(ci, 0,
+ swap_cluster_assert_empty(ci, ci_dyn, 0,
SWAPFILE_CLUSTER, true);
- swap_cluster_free_table(ci);
+ swap_cluster_free_table(ci, ci_dyn);
}
spin_unlock(&ci->lock);
vswap_cluster_free_vtable(ci);
@@ -3898,8 +3926,8 @@ static void free_swap_cluster_info(struct swap_info_struct *si,
/* Cluster with bad marks count will have a remaining table */
spin_lock(&ci->lock);
if (cluster_table_is_alloced(ci)) {
- swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true);
- swap_cluster_free_table(ci);
+ swap_cluster_assert_empty(ci, NULL, 0, SWAPFILE_CLUSTER, true);
+ swap_cluster_free_table(ci, NULL);
}
spin_unlock(&ci->lock);
}
diff --git a/mm/vswap.h b/mm/vswap.h
index f3a55414c6b7..3af7d73b7b1c 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -107,9 +107,9 @@ static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci,
* reads atomic; it does not permit backend changes. Revalidate speculative
* reads under the folio lock before using them.
*
- * Zero-backed slots use the swap_table per-slot zero flag (same as
- * direct-mapped physical swap), via __swap_table_test_zero() and friends,
- * which fall back to ci->zero_bitmap where the flag does not fit. Cached
+ * Zero-backed slots use the swap_table per-slot zero flag via
+ * __swap_table_test_zero() and friends, which fall back to
+ * ci_dyn->zero_bitmap where the flag does not fit. Cached
* folios are read out of the swap_table PFN entry; there is no separate FOLIO
* vtable type because the folio pointer would duplicate that PFN and
* would go stale on folio migration / split.
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 43+ messages in thread* Re: [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters
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
0 siblings, 0 replies; 43+ messages in thread
From: sashiko-bot @ 2026-12-04 22:09 UTC (permalink / raw)
To: Nhat Pham, linux-mm
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, Youngjun Park, Johannes Weiner, Yosry Ahmed,
Chengming Zhou, Qi Zheng, Shakeel Butt, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko, David Hildenbrand,
Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Hugh Dickins,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Roman Gushchin,
Muchun Song, Arnd Bergmann, Karl Mehltretter, Dapeng Mi,
Marco Elver, Ethan Nelson-Moore, Borislav Petkov (AMD),
Jeff Layton, Tejun Heo, Michal Koutný
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm/swap: swap_cluster_info_dynamic places hot pointers after
rcu_head
- [Low] mm/swap: vestigial alloc_si and lock in
swap_cluster_setup_bad_slot()
--
commit 7eb961b95873a58401088b4f4bf51cf685713031
Author: Nhat Pham <nphamcs@gmail.com>
mm/swap: move virtual swap metadata into vswap clusters
This commit moves the cgroup table, overflow count table, and zero bitmap
pointers from struct swap_cluster_info into struct swap_cluster_info_dynamic
so physical clusters do not carry virtual swap metadata.
> diff --git a/mm/swap.h b/mm/swap.h
> index 0d87de6eaddfc..1bf4821cdfefd 100644
> --- a/mm/swap.h
> +++ b/mm/swap.h
[ ... ]
> @@ -72,6 +65,13 @@ struct swap_cluster_info_dynamic {
> unsigned int index; /* for cluster_index() */
> struct rcu_head rcu;
> atomic_long_t *virtual_table; /* Backing pointers for vswap slots */
> + unsigned int *extend_table; /* For large swap count, protected by ci->lock */
> +#ifdef CONFIG_MEMCG
> + struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
> +#endif
> +#if !SWAP_TABLE_HAS_ZEROFLAG
> + unsigned long *zero_bitmap;
> +#endif
> };
[Severity: Medium]
Should rcu_head move to the end of struct swap_cluster_info_dynamic to avoid
pushing these pointers into a second cacheline?
On 64-bit SMP with CONFIG_MEMCG, placing extend_table, memcg_table, and
zero_bitmap after the 16-byte rcu_head puts them at offsets 64, 72, and 80.
Hot paths like __mem_cgroup_swap_record() and __swap_cache_add_check() now
always cross a 64-byte cacheline boundary between ci.lock and memcg_table.
Since rcu is only used on teardown in __free_cluster(), moving it to the end
keeps the struct at 80 bytes while packing the hot pointers into the first
64 bytes.
[ ... ]
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 3f6c56c51c2c0..a9ee5bd1f86b9 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -910,7 +918,11 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
> need_table = !ci->count;
> /* Need to allocate swap table first for initial bad slot marking. */
> if (need_table) {
> - ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
> + struct swap_cluster_info_dynamic *ci_dyn = NULL;
> +
> + if (alloc_si)
> + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> + ret = swap_cluster_alloc_table(alloc_si, ci, ci_dyn, GFP_KERNEL, false);
> if (ret)
> goto out;
> } else {
[Severity: Low]
Can alloc_si and the matching local_unlock(&percpu_swap_cluster.lock) be
dropped from swap_cluster_setup_bad_slot() now?
swap_cluster_alloc_table() only uses si to take outer allocator locks when
locked is false. Since vswap_init() calls setup_swap_clusters_info() before
publishing vswap_si, bad-slot setup can pass NULL for si in both cases and
compute ci_dyn from swap_is_vswap(si).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=20
^ permalink raw reply [flat|nested] 43+ messages in thread
* Re: [PATCH v7 00/20] mm: mandatory virtual swap
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
` (19 preceding siblings ...)
2026-12-03 22:00 ` [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters Nhat Pham
@ 2026-12-03 22:14 ` Yosry Ahmed
2026-12-03 23:00 ` Nhat Pham
20 siblings, 1 reply; 43+ messages in thread
From: Yosry Ahmed @ 2026-12-03 22:14 UTC (permalink / raw)
To: Nhat Pham
Cc: linux-mm, linux-kernel, Andrew Morton, Chris Li, Kairui Song,
Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Johannes Weiner, Chengming Zhou, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko,
David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Hugh Dickins, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Roman Gushchin, Muchun Song, Arnd Bergmann,
Karl Mehltretter, Dapeng Mi, Marco Elver, Ethan Nelson-Moore,
Borislav Petkov (AMD), Jeff Layton, linux-doc, cgroups, Tejun Heo,
Michal Koutný
On Thu, Dec 3, 2026 at 2:00 PM Nhat Pham <nphamcs@gmail.com> wrote:
>
> Changelog
>
> * v6 [v6] -> v7
> * Append five patches that make vswap mandatory after the optional
> implementation. Keep all swap counts, cache entries and ordinary
> cgroup ownership at the virtual slot.
> * The mandatory conversion removes 437 lines net across 19 files.
> Calls to swap_is_vswap() or is_vswap_entry() fall from 61 to 25,
> excluding definitions and assertions. Physical cluster metadata
> shrinks by 16 bytes on x86-64 with CONFIG_MEMCG.
> * Rebase onto mm-unstable. Keep the cluster XArray in this series;
> the v7-compatible VM_SPARSE variant is evaluated separately.
> * Restructure the patches for review. Keep I/O preparation ahead
> of virtual-entry storage and allocation, and split allocation
> from the zswap/zero backends.
> * Split physical backing into allocator, backing-lookup, swapoff
> preparation and final wiring patches. Register the physical
> backend with the virtual layer.
> * Walk PTEs and shmem for physical swapoff. Extend cache-only
> backing reclaim to allocation-range scans.
> * Fix cgroup v1 accounting and failed zswap writeback rollback.
> Register the vswap statistic only in swap builds.
> * Enable 32-bit with a one-word vtable.
> * Expand the zswap shrinker benchmarks and add MariaDB read/write
> results.
Not sure what others prefer, but I usually put the changelog at the
end, and only the most recent one with a link to the previous version.
This avoids the cover letter getting too large.
[..]
> II. Design
> ==========
>
> Patches 1-15 build the optional vswap implementation, roughly equivalent
> to v6 in functionality. Patches 16-20 then make it the common layer for
> ordinary swapping.
I assume that if we decide to go this route we will fold these patches
into the series (i.e. start with always-on vswap to begin with)?
[..]
> III. Mandatory vswap
> ====================
>
> In the v6 version, vswap keeps separate virtual and direct physical swap
> paths.
>
> At the last vswap sync, we discussed making vswap mandatory to simplify
> the implementation. Patches 16-20 apply this conversion after the
> optional vswap implementation.
> Anonymous and shmem swapping use virtual entries throughout, with swap
> counts, swapcache and cgroup ownership held at the virtual slot.
>
> Physical swap supplies disk backing and raw hibernation slots. Its swap
> table records reverse mappings to virtual slots, so backing reclaim can
> find the owner directly. Compressed zswap entries live in the vtable,
> removing per-device zswap trees. Initialize vswap automatically when
> CONFIG_SWAP is enabled, and move virtual-only fields out of physical
> clusters.
This section should call out the extra memory overhead when zswap is
not used. More generally, I think the cover letter should call out the
memory overhead for zswap and non-zswap cases (I believe the former is
0?).
>
> IV. Benchmarks
> ==============
>
> Highlights
> ----------
>
> The largest gain is with zswap and its shrinker. Host usemem throughput
> rises about 47% and wall time falls about 32%; kernel build wall time
> falls 3.6%. With zswap, usemem free time falls about 10-11%. MariaDB TPS
> rises 11.7% with the shrinker and 6.8% without it. Mean p95 latency
> falls 12.0% and 6.2%, respectively.
>
> Overall wall-time and throughput regressions are mostly a few percent in
> tight-memory microbenchmarks. Some separate costs are larger: cgrouped
> SSD usemem free time rises 3.8% and host-zram free time rises 7.0%.
> These overheads are a tradeoff for simplification: ordinary swapping
> shares the virtual layer, removing separate direct physical paths.
Do we know what the specific cause of regression is? Is it something
that can be optimized later while keeping vswap always-on?
^ permalink raw reply [flat|nested] 43+ messages in thread* Re: [PATCH v7 00/20] mm: mandatory virtual swap
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
0 siblings, 1 reply; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 23:00 UTC (permalink / raw)
To: Yosry Ahmed
Cc: linux-mm, linux-kernel, Andrew Morton, Chris Li, Kairui Song,
Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Johannes Weiner, Chengming Zhou, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko,
David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Hugh Dickins, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Roman Gushchin, Muchun Song, Arnd Bergmann,
Karl Mehltretter, Dapeng Mi, Marco Elver, Ethan Nelson-Moore,
Borislav Petkov (AMD), Jeff Layton, linux-doc, cgroups, Tejun Heo,
Michal Koutný
On Thu, Dec 3, 2026 at 3:14 PM Yosry Ahmed <yosry@kernel.org> wrote:
>
> On Thu, Dec 3, 2026 at 2:00 PM Nhat Pham <nphamcs@gmail.com> wrote:
> >
> > Changelog
> >
> > * v6 [v6] -> v7
> > * Append five patches that make vswap mandatory after the optional
> > implementation. Keep all swap counts, cache entries and ordinary
> > cgroup ownership at the virtual slot.
> > * The mandatory conversion removes 437 lines net across 19 files.
> > Calls to swap_is_vswap() or is_vswap_entry() fall from 61 to 25,
> > excluding definitions and assertions. Physical cluster metadata
> > shrinks by 16 bytes on x86-64 with CONFIG_MEMCG.
> > * Rebase onto mm-unstable. Keep the cluster XArray in this series;
> > the v7-compatible VM_SPARSE variant is evaluated separately.
> > * Restructure the patches for review. Keep I/O preparation ahead
> > of virtual-entry storage and allocation, and split allocation
> > from the zswap/zero backends.
> > * Split physical backing into allocator, backing-lookup, swapoff
> > preparation and final wiring patches. Register the physical
> > backend with the virtual layer.
> > * Walk PTEs and shmem for physical swapoff. Extend cache-only
> > backing reclaim to allocation-range scans.
> > * Fix cgroup v1 accounting and failed zswap writeback rollback.
> > Register the vswap statistic only in swap builds.
> > * Enable 32-bit with a one-word vtable.
> > * Expand the zswap shrinker benchmarks and add MariaDB read/write
> > results.
>
> Not sure what others prefer, but I usually put the changelog at the
> end, and only the most recent one with a link to the previous version.
> This avoids the cover letter getting too large.
Acked. Will move this to the bottom from now on.
>
> [..]
>
> > II. Design
> > ==========
> >
> > Patches 1-15 build the optional vswap implementation, roughly equivalent
> > to v6 in functionality. Patches 16-20 then make it the common layer for
> > ordinary swapping.
>
> I assume that if we decide to go this route we will fold these patches
> into the series (i.e. start with always-on vswap to begin with)?
Let me try that one first. If it's easier to review then I certainly
can give it a shot.
>
> [..]
>
> > III. Mandatory vswap
> > ====================
> >
> > In the v6 version, vswap keeps separate virtual and direct physical swap
> > paths.
> >
> > At the last vswap sync, we discussed making vswap mandatory to simplify
> > the implementation. Patches 16-20 apply this conversion after the
> > optional vswap implementation.
> > Anonymous and shmem swapping use virtual entries throughout, with swap
> > counts, swapcache and cgroup ownership held at the virtual slot.
> >
> > Physical swap supplies disk backing and raw hibernation slots. Its swap
> > table records reverse mappings to virtual slots, so backing reclaim can
> > find the owner directly. Compressed zswap entries live in the vtable,
> > removing per-device zswap trees. Initialize vswap automatically when
> > CONFIG_SWAP is enabled, and move virtual-only fields out of physical
> > clusters.
>
> This section should call out the extra memory overhead when zswap is
> not used. More generally, I think the cover letter should call out the
> memory overhead for zswap and non-zswap cases (I believe the former is
> 0?).
Yeah the former is 0.
For non-zswap backend, it's 16 bytes per entry! For the forward and
reverse map. We can get rid of the reverse map, if we can get rid of
cluster readahead, and implement a different scheme to do
swap-cache-only freeing. We can do it as a follow-up.
>
> >
> > IV. Benchmarks
> > ==============
> >
> > Highlights
> > ----------
> >
> > The largest gain is with zswap and its shrinker. Host usemem throughput
> > rises about 47% and wall time falls about 32%; kernel build wall time
> > falls 3.6%. With zswap, usemem free time falls about 10-11%. MariaDB TPS
> > rises 11.7% with the shrinker and 6.8% without it. Mean p95 latency
> > falls 12.0% and 6.2%, respectively.
> >
> > Overall wall-time and throughput regressions are mostly a few percent in
> > tight-memory microbenchmarks. Some separate costs are larger: cgrouped
> > SSD usemem free time rises 3.8% and host-zram free time rises 7.0%.
> > These overheads are a tradeoff for simplification: ordinary swapping
> > shares the virtual layer, removing separate direct physical paths.
>
> Do we know what the specific cause of regression is? Is it something
> that can be optimized later while keeping vswap always-on?
There's a couple more places where I can optimize. For e.g, on the
free path right now I'm separating the reference dropping and
uncharging options, but those can be merged to save memcg lookup work.
I think I might also be clearing the zeromap bits twice - a simple
check might be able to shave that one off too.
I can perf around a bit to see how much we can reduce it even further,
but FWIW though it feels like I will just be overindexing on some
microbenchmarks :)
^ permalink raw reply [flat|nested] 43+ messages in thread
* Re: [PATCH v7 00/20] mm: mandatory virtual swap
2026-12-03 23:00 ` Nhat Pham
@ 2026-12-03 23:05 ` Yosry Ahmed
2026-12-03 23:14 ` Nhat Pham
0 siblings, 1 reply; 43+ messages in thread
From: Yosry Ahmed @ 2026-12-03 23:05 UTC (permalink / raw)
To: Nhat Pham
Cc: linux-mm, linux-kernel, Andrew Morton, Chris Li, Kairui Song,
Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Johannes Weiner, Chengming Zhou, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko,
David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Hugh Dickins, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Roman Gushchin, Muchun Song, Arnd Bergmann,
Karl Mehltretter, Dapeng Mi, Marco Elver, Ethan Nelson-Moore,
Borislav Petkov (AMD), Jeff Layton, linux-doc, cgroups, Tejun Heo,
Michal Koutný
> > > II. Design
> > > ==========
> > >
> > > Patches 1-15 build the optional vswap implementation, roughly equivalent
> > > to v6 in functionality. Patches 16-20 then make it the common layer for
> > > ordinary swapping.
> >
> > I assume that if we decide to go this route we will fold these patches
> > into the series (i.e. start with always-on vswap to begin with)?
>
>
> Let me try that one first. If it's easier to review then I certainly
> can give it a shot.
I would expect/hope so. Thanks!
> > > III. Mandatory vswap
> > > ====================
> > >
> > > In the v6 version, vswap keeps separate virtual and direct physical swap
> > > paths.
> > >
> > > At the last vswap sync, we discussed making vswap mandatory to simplify
> > > the implementation. Patches 16-20 apply this conversion after the
> > > optional vswap implementation.
> > > Anonymous and shmem swapping use virtual entries throughout, with swap
> > > counts, swapcache and cgroup ownership held at the virtual slot.
> > >
> > > Physical swap supplies disk backing and raw hibernation slots. Its swap
> > > table records reverse mappings to virtual slots, so backing reclaim can
> > > find the owner directly. Compressed zswap entries live in the vtable,
> > > removing per-device zswap trees. Initialize vswap automatically when
> > > CONFIG_SWAP is enabled, and move virtual-only fields out of physical
> > > clusters.
> >
> > This section should call out the extra memory overhead when zswap is
> > not used. More generally, I think the cover letter should call out the
> > memory overhead for zswap and non-zswap cases (I believe the former is
> > 0?).
>
> Yeah the former is 0.
>
> For non-zswap backend, it's 16 bytes per entry! For the forward and
> reverse map. We can get rid of the reverse map, if we can get rid of
> cluster readahead, and implement a different scheme to do
> swap-cache-only freeing. We can do it as a follow-up.
This is per used swapped out entry, not for the entire swapfile size, right?
> > > IV. Benchmarks
> > > ==============
> > >
> > > Highlights
> > > ----------
> > >
> > > The largest gain is with zswap and its shrinker. Host usemem throughput
> > > rises about 47% and wall time falls about 32%; kernel build wall time
> > > falls 3.6%. With zswap, usemem free time falls about 10-11%. MariaDB TPS
> > > rises 11.7% with the shrinker and 6.8% without it. Mean p95 latency
> > > falls 12.0% and 6.2%, respectively.
> > >
> > > Overall wall-time and throughput regressions are mostly a few percent in
> > > tight-memory microbenchmarks. Some separate costs are larger: cgrouped
> > > SSD usemem free time rises 3.8% and host-zram free time rises 7.0%.
> > > These overheads are a tradeoff for simplification: ordinary swapping
> > > shares the virtual layer, removing separate direct physical paths.
> >
> > Do we know what the specific cause of regression is? Is it something
> > that can be optimized later while keeping vswap always-on?
>
> There's a couple more places where I can optimize. For e.g, on the
> free path right now I'm separating the reference dropping and
> uncharging options, but those can be merged to save memcg lookup work.
> I think I might also be clearing the zeromap bits twice - a simple
> check might be able to shave that one off too.
>
> I can perf around a bit to see how much we can reduce it even further,
> but FWIW though it feels like I will just be overindexing on some
> microbenchmarks :)
Yeah I am not saying we need to do this, just wondering what the
options are if it becomes a real problem. Thanks!
^ permalink raw reply [flat|nested] 43+ messages in thread
* Re: [PATCH v7 00/20] mm: mandatory virtual swap
2026-12-03 23:05 ` Yosry Ahmed
@ 2026-12-03 23:14 ` Nhat Pham
0 siblings, 0 replies; 43+ messages in thread
From: Nhat Pham @ 2026-12-03 23:14 UTC (permalink / raw)
To: Yosry Ahmed
Cc: linux-mm, linux-kernel, Andrew Morton, Chris Li, Kairui Song,
Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Johannes Weiner, Chengming Zhou, Qi Zheng, Shakeel Butt,
Axel Rasmussen, Yuanchu Xie, Wei Xu, Baolin Wang, Michal Hocko,
David Hildenbrand, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Hugh Dickins, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Roman Gushchin, Muchun Song, Arnd Bergmann,
Karl Mehltretter, Dapeng Mi, Marco Elver, Ethan Nelson-Moore,
Borislav Petkov (AMD), Jeff Layton, linux-doc, cgroups, Tejun Heo,
Michal Koutný
On Thu, Dec 3, 2026 at 4:05 PM Yosry Ahmed <yosry@kernel.org> wrote:
>
> > > > II. Design
> > > > ==========
> > > >
> > > > Patches 1-15 build the optional vswap implementation, roughly equivalent
> > > > to v6 in functionality. Patches 16-20 then make it the common layer for
> > > > ordinary swapping.
> > >
> > > I assume that if we decide to go this route we will fold these patches
> > > into the series (i.e. start with always-on vswap to begin with)?
> >
> >
> > Let me try that one first. If it's easier to review then I certainly
> > can give it a shot.
>
> I would expect/hope so. Thanks!
>
> > > > III. Mandatory vswap
> > > > ====================
> > > >
> > > > In the v6 version, vswap keeps separate virtual and direct physical swap
> > > > paths.
> > > >
> > > > At the last vswap sync, we discussed making vswap mandatory to simplify
> > > > the implementation. Patches 16-20 apply this conversion after the
> > > > optional vswap implementation.
> > > > Anonymous and shmem swapping use virtual entries throughout, with swap
> > > > counts, swapcache and cgroup ownership held at the virtual slot.
> > > >
> > > > Physical swap supplies disk backing and raw hibernation slots. Its swap
> > > > table records reverse mappings to virtual slots, so backing reclaim can
> > > > find the owner directly. Compressed zswap entries live in the vtable,
> > > > removing per-device zswap trees. Initialize vswap automatically when
> > > > CONFIG_SWAP is enabled, and move virtual-only fields out of physical
> > > > clusters.
> > >
> > > This section should call out the extra memory overhead when zswap is
> > > not used. More generally, I think the cover letter should call out the
> > > memory overhead for zswap and non-zswap cases (I believe the former is
> > > 0?).
> >
> > Yeah the former is 0.
> >
> > For non-zswap backend, it's 16 bytes per entry! For the forward and
> > reverse map. We can get rid of the reverse map, if we can get rid of
> > cluster readahead, and implement a different scheme to do
> > swap-cache-only freeing. We can do it as a follow-up.
>
> This is per used swapped out entry, not for the entire swapfile size, right?
Yup. Per-used entry only. The reverse map uses swap table
infrastructure, which is dynamically allocated (i.e on demand).
^ permalink raw reply [flat|nested] 43+ messages in thread