The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	David Hildenbrand <david@kernel.org>
Cc: Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>,
	Qi Zheng <qi.zheng@linux.dev>, Zi Yan <ziy@nvidia.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Kairui Song <kasong@tencent.com>, Nhat Pham <nphamcs@gmail.com>,
	Usama Arif <usama.arif@linux.dev>,
	Hugh Dickins <hughd@google.com>,
	Meta kernel team <kernel-team@meta.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH 4/4] mm/vmscan: don't give swap to an anon folio that nothing maps
Date: Mon, 30 Nov 2026 10:27:55 -0800	[thread overview]
Message-ID: <20261130182755.666042-5-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130182755.666042-1-shakeel.butt@linux.dev>

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@linux.dev>
---
 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


      parent reply	other threads:[~2026-11-30 18:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:27 [PATCH 0/4] mm, memcg: stop orphaned swap cache from using up memcg IDs Shakeel Butt
2026-11-30 18:27 ` [PATCH 1/4] mm/swap: free orphaned swap cache instead of leaving it to reclaim Shakeel Butt
2026-11-30 18:27 ` [PATCH 2/4] mm: free swap cache that migration or a split leaves unmapped Shakeel Butt
2026-11-30 18:27 ` [PATCH 3/4] mm/memory: free the swap cache folio that a KSM copy leaves behind Shakeel Butt
2026-11-30 18:27 ` Shakeel Butt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130182755.666042-5-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kasong@tencent.com \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=nphamcs@gmail.com \
    --cc=qi.zheng@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox