[PATCH 4/4] mm/vmscan: don't give swap to an anon folio that nothing maps

From: Shakeel Butt

Date: Mon Nov 30 2026 - 13:28:32 EST


When a process exits while reclaim has some of its folios isolated, the
folios lose their mappings, but reclaim's reference keeps them alive.
shrink_folio_list() still gives such a folio swap. If something else
also holds a reference, such as the exiting task's TLB gather, pageout()
returns PAGE_KEEP. The folio then goes back to the LRU in the swap
cache, unmapped and with no slot references. No swap count ever dropped,
so nothing queues it. It is an orphan from the start, and keeps its
slots and memcg ID until reclaim comes back, which may not happen below
a cgroup protected by memory.low.

In a VM that runs short-lived cgroups under global memory pressure, 53
of the 64 orphans left after three runs got their swap this way, 51 of
them from kswapd.

An unmapped anon folio that is not in the swap cache has no page table
or shmem entry pointing to it, so don't give it swap. It is freed when
the last reference goes away, often reclaim's own. A KSM stable node
holds no reference, but KSM can map a KSM folio again; a later pass
swaps it out then. The check runs right before the swap allocation,
after the split of a partially mapped folio, so it also catches an
unmapped first page of such a folio. An unmapped folio that is still
held elsewhere is kept or activated as before; only the swap allocation
is gone.

The last mapping can also go away after that check, while reclaim holds
the folio lock, so the exiting task's free_swap_cache() can't free the
folio. So check again after the unmap step: if the folio got swap in
this pass and is now an orphan, free its slots with
folio_free_orphan_swap() and keep it.

shrink_list() in Linux 2.6.12-rc2 already allocated swap before checking
page_mapped(), so there is no Fixes tag.

Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
mm/vmscan.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 99cb209797b4..ddc1584d06ed 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1128,6 +1128,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
struct folio *folio;
enum folio_references references = FOLIOREF_RECLAIM;
bool dirty, writeback;
+ bool new_swap = false;
unsigned int nr_pages;

cond_resched();
@@ -1331,6 +1332,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
split_folio_to_list(folio, folio_list))
goto activate_locked;
}
+ /*
+ * If nothing maps the folio, its last mapping went
+ * away, usually because its process exited, and no page
+ * table or shmem entry refers to it. It is freed when
+ * the last reference goes away. Giving it swap would
+ * only leave it in the swap cache if something else
+ * still holds a reference. KSM can map a KSM folio
+ * again; a later pass swaps it out then. This comes
+ * after the split above, so it also skips an unmapped
+ * first page of a partially mapped folio.
+ */
+ if (!folio_mapped(folio))
+ goto keep_locked;
ret = folio_alloc_swap(folio);
if (ret) {
int __maybe_unused order = folio_order(folio);
@@ -1366,6 +1380,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
* flag for the folio solves the problem.
*/
folio_mark_dirty(folio);
+ new_swap = true;
}

/*
@@ -1419,6 +1434,17 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
}
}

+ /*
+ * If the folio got swap above but is an orphan now, its last
+ * mapping went away in the meantime, before try_to_unmap() or
+ * while it ran. No swap count will drop for it, and the
+ * unmapping task's free_swap_cache() found it either not yet
+ * in the swap cache or locked by us; it may have queued it.
+ * Free the slots and keep the folio, as above.
+ */
+ if (new_swap && folio_free_orphan_swap(folio))
+ goto keep_locked;
+
/*
* Folio is unmapped now so it cannot be newly pinned anymore.
* No point in trying to reclaim folio if it is pinned.
--
2.53.1-Meta