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 3/4] mm/memory: free the swap cache folio that a KSM copy leaves behind
Date: Mon, 30 Nov 2026 10:27:54 -0800 [thread overview]
Message-ID: <20261130182755.666042-4-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130182755.666042-1-shakeel.butt@linux.dev>
When do_swap_page() finds a swap cache folio that it may not map,
ksm_might_need_to_copy() gives it a copy to map instead. This happens
for a KSM folio that lost its stable node or while KSM is unmerging, and
for a folio that another process mapped into an unrelated anon_vma.
do_swap_page() maps the copy and drops the swap count of the original,
which stays in the swap cache. If that was the last reference to its
slots and nothing maps it, the original is an orphan. Only LRU reclaim
would free it, and with it its slots and their memcg ID.
This happens to KSM pages that unrelated processes share. After the
page is swapped out, the first process to fault maps the swap cache
folio into its own anon_vma. If that process exits, every later fault on
the slot by an unrelated process gets a copy, and the last one leaves
the original behind.
Free the original with folio_free_orphan_swap() while it is still
locked; if it is under writeback, that queues it. The check covers all
slots of the folio, because one of the sharing processes can swap the
slot in as part of a large folio, and the copy takes only one page.
Two unrelated processes share 4,096 KSM pages, which are swapped out.
One faults them in and exits, then the other faults them in. That left
4,096 orphans before this patch and none after. The test used only
small folios.
Fixes: 5ad6468801d2 ("ksm: let shared pages be swappable")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
mm/memory.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index 82e1ec8e44e7..67b7564a0623 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5368,6 +5368,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
folio_unlock(folio);
if (unlikely(folio != swapcache)) {
+ /*
+ * The copy is mapped and the slot's count was dropped. If
+ * nothing maps swapcache and its slots have no references
+ * left, only reclaim would remove it from the swap cache.
+ */
+ folio_free_orphan_swap(swapcache);
/*
* Hold the lock to avoid the swap entry to be reused
* until we take the PT lock for the pte_same() check
--
2.53.1-Meta
next prev 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 ` Shakeel Butt [this message]
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-4-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®