[PATCH v4 03/21] mm: swap: add an abstract API for locking out swapoff

Nhat Pham
2026-03-18T22:30:05Z

Currently, we get a reference to the backing swap device in order to
prevent swapoff from freeing the metadata of a swap entry. This does not
make sense in the new virtual swap design, especially after the swap
backends are decoupled - a swap entry might not have any backing swap
device at all, and its backend might change at any time during its
lifetime.

In preparation for this, abstract away the swapoff locking out behavior
into a generic API.

Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
include/linux/swap.h | 17 +++++++++++++++++
mm/memory.c | 13 +++++++------
mm/mincore.c | 15 +++------------
mm/shmem.c | 12 ++++++------
mm/swap_state.c | 14 +++++++-------
mm/userfaultfd.c | 15 +++++++++------
mm/zswap.c | 5 ++---
7 files changed, 51 insertions(+), 40 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index aa29d8ac542d1..3da637b218baf 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -659,5 +659,22 @@ static inline bool mem_cgroup_swap_full(struct folio *folio)
}
#endif

+static inline bool tryget_swap_entry(swp_entry_t entry,
+ struct swap_info_struct **sip)
+{
+ struct swap_info_struct *si = get_swap_device(entry);
+
+ if (sip)
+ *sip = si;
+
+ return si;
+}
+
+static inline void put_swap_entry(swp_entry_t entry,
+ struct swap_info_struct *si)
+{
+ put_swap_device(si);
+}
+
#endif /* __KERNEL__*/
#endif /* _LINUX_SWAP_H */
diff --git a/mm/memory.c b/mm/memory.c
index da360a6eb8a48..90031f833f52e 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4630,6 +4630,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
struct swap_info_struct *si = NULL;
rmap_t rmap_flags = RMAP_NONE;
bool need_clear_cache = false;
+ bool swapoff_locked = false;
bool exclusive = false;
softleaf_t entry;
pte_t pte;
@@ -4698,8 +4699,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
}

/* Prevent swapoff from happening to us. */
- si = get_swap_device(entry);
- if (unlikely(!si))
+ swapoff_locked = tryget_swap_entry(entry, &si);
+ if (unlikely(!swapoff_locked))
goto out;

folio = swap_cache_get_folio(entry);
@@ -5047,8 +5048,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (waitqueue_active(&swapcache_wq))
wake_up(&swapcache_wq);
}
- if (si)
- put_swap_device(si);
+ if (swapoff_locked)
+ put_swap_entry(entry, si);
return ret;
out_nomap:
if (vmf->pte)
@@ -5066,8 +5067,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (waitqueue_active(&swapcache_wq))
wake_up(&swapcache_wq);
}
- if (si)
- put_swap_device(si);
+ if (swapoff_locked)
+ put_swap_entry(entry, si);
return ret;
}

diff --git a/mm/mincore.c b/mm/mincore.c
index e5d13eea92347..f3eb771249d67 100644
--- a/mm/mincore.c
+++ b/mm/mincore.c
@@ -77,19 +77,10 @@ static unsigned char mincore_swap(swp_entry_t entry, bool shmem)
if (!softleaf_is_swap(entry))
return !shmem;

- /*
- * Shmem mapping lookup is lockless, so we need to grab the swap
- * device. mincore page table walk locks the PTL, and the swap
- * device is stable, avoid touching the si for better performance.
- */
- if (shmem) {
- si = get_swap_device(entry);
- if (!si)
- return 0;
- }
+ if (!tryget_swap_entry(entry, &si))
+ return 0;
folio = swap_cache_get_folio(entry);
- if (shmem)
- put_swap_device(si);
+ put_swap_entry(entry, si);
/* The swap cache space contains either folio, shadow or NULL */
if (folio && !xa_is_value(folio)) {
present = folio_test_uptodate(folio);
diff --git a/mm/shmem.c b/mm/shmem.c
index 1db97ef2d14eb..b40be22fa5f09 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2307,7 +2307,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
softleaf_t index_entry;
struct swap_info_struct *si;
struct folio *folio = NULL;
- bool skip_swapcache = false;
+ bool swapoff_locked, skip_swapcache = false;
int error, nr_pages, order;
pgoff_t offset;

@@ -2319,16 +2319,16 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
if (softleaf_is_poison_marker(index_entry))
return -EIO;

- si = get_swap_device(index_entry);
+ swapoff_locked = tryget_swap_entry(index_entry, &si);
order = shmem_confirm_swap(mapping, index, index_entry);
- if (unlikely(!si)) {
+ if (unlikely(!swapoff_locked)) {
if (order < 0)
return -EEXIST;
else
return -EINVAL;
}
if (unlikely(order < 0)) {
- put_swap_device(si);
+ put_swap_entry(index_entry, si);
return -EEXIST;
}

@@ -2448,7 +2448,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
}
folio_mark_dirty(folio);
swap_free_nr(swap, nr_pages);
- put_swap_device(si);
+ put_swap_entry(swap, si);

*foliop = folio;
return 0;
@@ -2466,7 +2466,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
swapcache_clear(si, folio->swap, folio_nr_pages(folio));
if (folio)
folio_put(folio);
- put_swap_device(si);
+ put_swap_entry(swap, si);

return error;
}
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 34c9d9b243a74..bece18eb540fa 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -538,8 +538,7 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
pgoff_t ilx;
struct folio *folio;

- si = get_swap_device(entry);
- if (!si)
+ if (!tryget_swap_entry(entry, &si))
return NULL;

mpol = get_vma_policy(vma, addr, 0, &ilx);
@@ -550,7 +549,7 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
if (page_allocated)
swap_read_folio(folio, plug);

- put_swap_device(si);
+ put_swap_entry(entry, si);
return folio;
}

@@ -763,6 +762,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
for (addr = start; addr < end; ilx++, addr += PAGE_SIZE) {
struct swap_info_struct *si = NULL;
softleaf_t entry;
+ bool swapoff_locked = false;

if (!pte++) {
pte = pte_offset_map(vmf->pmd, addr);
@@ -781,14 +781,14 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
* holding a reference to, try to grab a reference, or skip.
*/
if (swp_type(entry) != swp_type(targ_entry)) {
- si = get_swap_device(entry);
- if (!si)
+ swapoff_locked = tryget_swap_entry(entry, &si);
+ if (!swapoff_locked)
continue;
}
folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
&page_allocated, false);
- if (si)
- put_swap_device(si);
+ if (swapoff_locked)
+ put_swap_entry(entry, si);
if (!folio)
continue;
if (page_allocated) {
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index e6dfd5f28acd7..25f89eba0438c 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -1262,9 +1262,11 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
pte_t *dst_pte = NULL;
pmd_t dummy_pmdval;
pmd_t dst_pmdval;
+ softleaf_t entry;
struct folio *src_folio = NULL;
struct mmu_notifier_range range;
long ret = 0;
+ bool swapoff_locked = false;

mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
src_addr, src_addr + len);
@@ -1429,7 +1431,7 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
len);
} else { /* !pte_present() */
struct folio *folio = NULL;
- const softleaf_t entry = softleaf_from_pte(orig_src_pte);
+ entry = softleaf_from_pte(orig_src_pte);

if (softleaf_is_migration(entry)) {
pte_unmap(src_pte);
@@ -1449,8 +1451,8 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
goto out;
}

- si = get_swap_device(entry);
- if (unlikely(!si)) {
+ swapoff_locked = tryget_swap_entry(entry, &si);
+ if (unlikely(!swapoff_locked)) {
ret = -EAGAIN;
goto out;
}
@@ -1480,8 +1482,9 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
pte_unmap(src_pte);
pte_unmap(dst_pte);
src_pte = dst_pte = NULL;
- put_swap_device(si);
+ put_swap_entry(entry, si);
si = NULL;
+ swapoff_locked = false;
/* now we can block and wait */
folio_lock(src_folio);
goto retry;
@@ -1507,8 +1510,8 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
if (dst_pte)
pte_unmap(dst_pte);
mmu_notifier_invalidate_range_end(&range);
- if (si)
- put_swap_device(si);
+ if (swapoff_locked)
+ put_swap_entry(entry, si);

return ret;
}
diff --git a/mm/zswap.c b/mm/zswap.c
index ac9b7a60736bc..315e4d0d08311 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1009,14 +1009,13 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
int ret = 0;

/* try to allocate swap cache folio */
- si = get_swap_device(swpentry);
- if (!si)
+ if (!tryget_swap_entry(swpentry, &si))
return -EEXIST;

mpol = get_task_policy(current);
folio = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol,
NO_INTERLEAVE_INDEX, &folio_was_allocated, true);
- put_swap_device(si);
+ put_swap_entry(swpentry, si);
if (!folio)
return -ENOMEM;

--
2.52.0