mirror of https://lore.kernel.org/lkml/
 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 2/4] mm: free swap cache that migration or a split leaves unmapped
Date: Mon, 30 Nov 2026 10:27:53 -0800	[thread overview]
Message-ID: <20261130182755.666042-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130182755.666042-1-shakeel.butt@linux.dev>

Migration and large folio splits replace the PTEs of a folio with
migration entries, and remove_migration_ptes() restores them afterwards.
Zapping a migration entry neither drops a swap count nor calls
free_swap_cache(). So if the process exits in between, the folio comes
back unmapped. If it is in the swap cache and its slots have no
references, it is an orphan that only LRU reclaim frees. This goes back
to when migration switched from swap entries to migration entries.

Free the swap of such a folio at the end of remove_migration_ptes(),
with folio_free_orphan_swap(). Every caller holds the folio lock, and
the folio is not under writeback: migration waits for writeback or gives
up, and a folio under writeback is not split. So the swap is normally
freed right away. The rmap walk also sees the PTEs that fork copied, so
nothing can map the folio again after it, except KSM for a KSM folio,
which is safe.

Splits have one more gap. __folio_split() frees the swap of unmapped
after-split folios, but not of the one it hands back to the caller. And
a folio that was already unmapped, such as a THP swapped out whole, is
split without remove_migration_ptes(). So if part of such a THP was
unmapped while its other slots were still in use, the folio the caller
gets back can be an orphan. Run the same helper on it in
__folio_split(). It has no slot references, and no caller needs it to
stay in the swap cache.

In a VM with 1,200 short-lived cgroups under global memory pressure, the
migration check found 0 to 12 orphans per run. A THP that was swapped
out whole, then partly unmapped and split through debugfs, left an
orphan before this patch and none after.

Fixes: 6c5240ae7f48 ("[PATCH] Swapless page migration: modify core logic")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 mm/huge_memory.c | 10 ++++++++--
 mm/migrate.c     |  9 +++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 4fc888ddfb69..f024289930ac 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4443,6 +4443,14 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	 */
 	for (new_folio = folio; new_folio != end_folio; new_folio = next) {
 		next = folio_next(new_folio);
+		/*
+		 * Free the swap of an after-split folio that nothing maps and
+		 * whose slots have no references, the one kept for the caller
+		 * included: an unmapped folio is split without
+		 * remove_migration_ptes().
+		 */
+		if (!ret && is_swapcache)
+			folio_free_orphan_swap(new_folio);
 		if (new_folio == page_folio(lock_at))
 			continue;
 
@@ -4452,8 +4460,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 		 * lru_lock, so we defer folio_put() on tail pages until
 		 * after the split completes.
 		 */
-		if (is_swapcache && !folio_mapped(new_folio))
-			folio_free_swap(new_folio);
 		folio_unlock(new_folio);
 		folio_put(new_folio);
 	}
diff --git a/mm/migrate.c b/mm/migrate.c
index 96b66a272379..fbd69f871291 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -494,6 +494,15 @@ void remove_migration_ptes(struct folio *src, struct folio *dst,
 		rmap_walk_locked(dst, &rwc);
 	else
 		rmap_walk(dst, &rwc);
+
+	/*
+	 * If the process exited while the folio was unmapped for migration
+	 * or a split, no PTE points to it now: zapping a migration entry
+	 * neither drops a swap count nor calls free_swap_cache(). Every
+	 * caller holds the folio lock, and the folio is not under writeback,
+	 * so this normally frees the swap right away.
+	 */
+	folio_free_orphan_swap(dst);
 }
 
 /*
-- 
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 ` Shakeel Butt [this message]
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 ` [PATCH 4/4] mm/vmscan: don't give swap to an anon folio that nothing maps Shakeel Butt

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-3-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

all inboxes | Powered by JetHome®