[PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing

From: Nhat Pham

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


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@xxxxxxxxx>
---
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