From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
Michal Hocko <mhocko@suse.com>, Kiryl Shutsemau <kas@kernel.org>,
Manfred Spraul <manfred@colorfullife.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Cyrill Gorcunov <gorcunov@gmail.com>, Zi Yan <ziy@nvidia.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Lorenzo Stoakes <ljs@kernel.org>,
Rik van Riel <riel@surriel.com>,
Vlastimil Babka <vbabka@kernel.org>,
Harry Yoo <harry@kernel.org>, Jann Horn <jannh@google.com>,
Yang Shi <shy828301@gmail.com>,
Zach O'Keefe <zokeefe@google.com>, Song Liu <song@kernel.org>,
David Stevens <stevensd@chromium.org>,
Kefeng Wang <wangkefeng.wang@huawei.com>,
Alex Williamson <alex@shazbot.org>,
Suren Baghdasaryan <surenb@google.com>,
Dave Jiang <dave.jiang@intel.com>,
Matthew Wilcox <willy@infradead.org>,
Miaohe Lin <linmiaohe@huawei.com>,
David Rientjes <rientjes@google.com>,
Shakeel Butt <shakeel.butt@linux.dev>, Jan Kara <jack@suse.cz>,
Mel Gorman <mgorman@suse.de>, Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>,
Gerd Hoffmann <kraxel@redhat.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Dave Airlie <airlied@redhat.com>,
Matthew Brost <matthew.brost@intel.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Byungchul Park <byungchul@sk.com>,
Gregory Price <gourry@gourry.net>,
Ying Huang <ying.huang@linux.alibaba.com>,
Alistair Popple <apopple@nvidia.com>,
Hagen Paul Pfeifer <hagen@jauu.net>,
James Bottomley <James.Bottomley@HansenPartnership.com>,
Felix Kuehling <Felix.Kuehling@amd.com>,
Alex Sierra <alex.sierra@amd.com>,
Balbir Singh <balbirs@nvidia.com>,
Kairui Song <kasong@tencent.com>, Qi Zheng <qi.zheng@linux.dev>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
Muchun Song <muchun.song@linux.dev>,
Dan Williams <djbw@kernel.org>, Rakie Kim <rakie.kim@sk.com>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Alexei Starovoitov <ast@kernel.org>, Xu Xin <xu.xin@linux.dev>,
Chengming Zhou <chengming.zhou@linux.dev>,
Shivank Garg <shivankg@amd.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org,
linux-rt-devel@lists.linux.dev, SJ Park <sj@kernel.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
stable@vger.kernel.org, Chris Mason <mason@kernel.org>
Subject: [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio
Date: Fri, 04 Dec 2026 13:50:18 +0100 [thread overview]
Message-ID: <20261204-fixes_core_3-v2-27-9aad65727632@kernel.org> (raw)
In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org>
So far we were under the assumption that only VM_MERGEABLE VMAs may
contain KSM folios, because we unmerge when clearing VM_MERGEABLE, and
KSM only merges within VM_MERGEABLE VMAs. Various infrastructure (e.g.,
rmap walks) relies on related properties: for example that a KSM folio
is always mapped at rmap_item->address, also in child processes.
We ignored one scenario: turning a COW-shared folio into a KSM folio in
one process will make all processes mapping it suddenly see a KSM
folio, even if VM_MERGEABLE is not set for them.
Consequently, rmap walks can miss such folio mappings, resulting in
the KSM folio no longer being reclaimable or migratable.
Worse, if such a process calls mremap() while the kernel is migrating a
KSM folio, mremap() moves the migration entry that try_to_migrate()
installed, and remove_migration_ptes() will not find it at its new
address. Once migration frees the source folio, the moved PTE still
holds a migration entry that refers to the freed, and possibly already
reused, folio: the next access through that PTE will hang, or worse.
Further, if ksmd scans the child, rmap walks will not even find the
parent's VMA: the child's rmap_item references the child's anon_vma,
which the parent's VMA is not linked into.
Let's fix it all by refusing to convert a folio into a KSM folio if it
might be shared with another mm. When merging two folios that are not
KSM folios yet, try both directions: as long as one folio is not shared,
we can merge.
Shared folios can still be merged into an existing KSM folio, the shared
zeropage or an identical folio that is not shared. Only identical folios
that are all shared will no longer be merged, which should be fine for
the known use cases.
Note that other processes might only hold a swap entry for the folio,
and map it after swapping it in. As the previous commit only copies KSM
folios at swap-in into VMAs without VM_MERGEABLE, also refuse to convert
a folio that is in the swap cache, unless we can free the swap entry.
This has medium urgency, as an unprivileged process can trigger a
use-after-free if KSM is enabled.
Fixes: 80e148226028 ("ksm: share anon page without allocating")
Fixes: 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue")
Cc: stable@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
Documentation/admin-guide/mm/ksm.rst | 3 +++
mm/ksm.c | 47 ++++++++++++++++++++++++++++++++----
2 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst
index 8968ae3f55c0a..079db28bfc5b4 100644
--- a/Documentation/admin-guide/mm/ksm.rst
+++ b/Documentation/admin-guide/mm/ksm.rst
@@ -27,6 +27,9 @@ KSM's merged pages were originally locked into kernel memory, but can now
be swapped out just like other user pages (but sharing is broken when they
are swapped back in: ksmd must rediscover their identity and merge again).
+Note that KSM might not merge identical pages if each of them is already
+shared between processes as a result of fork().
+
Controlling KSM with madvise
============================
diff --git a/mm/ksm.c b/mm/ksm.c
index 509bd78156583..fe7b3b60540db 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1494,7 +1494,9 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
* @kpage: the KSM page that we want to map instead of page,
* or NULL the first time when we want to use page as kpage.
*
- * This function returns 0 if the pages were merged, -EFAULT otherwise.
+ * This function returns 0 if the pages were merged, -EBUSY if page must not be
+ * turned into a KSM folio because another mm maps it or can still swap it in,
+ * -EFAULT otherwise.
*/
static int try_to_merge_one_page(struct vm_area_struct *vma,
unsigned long addr, struct page *page, struct page *kpage)
@@ -1509,6 +1511,12 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
if (!folio_test_anon(folio) || folio_test_ksm(folio))
goto out;
+ /* Avoid lock+split if the folio is obviously shared. */
+ if (!kpage && folio_maybe_mapped_shared(folio)) {
+ err = -EBUSY;
+ goto out;
+ }
+
/*
* We need the folio lock to read a stable swapcache flag in
* write_protect_page(). We trylock because we don't want to wait
@@ -1524,6 +1532,18 @@ static int try_to_merge_one_page(struct vm_area_struct *vma,
folio = page_folio(page);
}
+ /*
+ * Turning a folio that is shared with other processes, or that other
+ * processes could still swap in, into a KSM folio can result in KSM
+ * folios showing up in VMAs that never had KSM enabled, or in VMAs that
+ * rmap walks cannot find. So just deny on any sign of sharing.
+ */
+ if (!kpage && (folio_maybe_mapped_shared(folio) ||
+ (folio_test_swapcache(folio) && !folio_free_swap(folio)))) {
+ err = -EBUSY;
+ goto out_unlock;
+ }
+
/*
* If this anonymous page is mapped only here, its pte may need
* to be write-protected. If it's mapped elsewhere, all of its
@@ -1598,7 +1618,9 @@ static int try_to_merge_with_zero_page(struct ksm_rmap_item *rmap_item,
* try_to_merge_with_ksm_page - like try_to_merge_two_pages,
* but no new kernel page is allocated: kpage must already be a ksm page.
*
- * This function returns 0 if the pages were merged, -EFAULT otherwise.
+ * This function returns 0 if the pages were merged, -EBUSY if page must not be
+ * turned into a KSM folio because another mm maps it or can still swap it in,
+ * -EFAULT otherwise.
*/
static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item,
struct page *page, struct page *kpage)
@@ -1642,7 +1664,8 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item,
* to be merged into one page.
*
* This function returns the kpage if we successfully merged two identical
- * pages into one ksm page, NULL otherwise.
+ * pages into one ksm page, ERR_PTR(-EBUSY) if page must not be turned into a
+ * ksm page, NULL otherwise.
*
* Note that this function upgrades page to ksm page: if one of the pages
* is already a ksm page, try_to_merge_with_ksm_page should be used.
@@ -1655,6 +1678,8 @@ static struct folio *try_to_merge_two_pages(struct ksm_rmap_item *rmap_item,
int err;
err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
+ if (err == -EBUSY)
+ return ERR_PTR(-EBUSY);
if (!err) {
err = try_to_merge_with_ksm_page(tree_rmap_item,
tree_page, page);
@@ -2359,8 +2384,19 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
struct folio *tree_folio;
bool split;
+ /*
+ * Try merging in both directions: if one folio is shared but
+ * the other isn't, one direction will work. Merging will fail
+ * if both folios are shared.
+ */
kfolio = try_to_merge_two_pages(rmap_item, page,
tree_rmap_item, tree_page);
+ if (kfolio == ERR_PTR(-EBUSY))
+ kfolio = try_to_merge_two_pages(tree_rmap_item, tree_page,
+ rmap_item, page);
+ if (IS_ERR(kfolio))
+ kfolio = NULL;
+
/* We might have split the folio. */
folio = page_folio(page);
tree_folio = page_folio(tree_page);
@@ -2375,7 +2411,6 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
* split_huge_page should succeed.
*/
split = folio == tree_folio;
- folio_put(tree_folio);
if (kfolio) {
/*
* The pages were successfully merged: insert new
@@ -2401,7 +2436,9 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
break_cow(tree_rmap_item);
break_cow(rmap_item);
}
- } else if (split) {
+ }
+ folio_put(tree_folio);
+ if (!kfolio && split) {
/*
* We are here if we tried to merge two pages and
* failed because they both belonged to the same
--
2.56.1
next prev parent reply other threads:[~2026-12-04 12:58 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 12:49 [PATCH v2 00/31] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 02/31] mm/shmem: don't skip parts of a split swap entry when punching holes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 03/31] mm/shmem: reject userfaultfd fills during hole punch David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 04/31] mm/shmem: fix commit limit bypass with very large shmem sizes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 05/31] mm/shmem: don't pin inodes with eviction held off in the huge shrinker David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 06/31] mm/shmem: seal the size of SysV and shared anon files David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 07/31] mm/khugepaged: check the VMA before walking page tables when collapsing David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 08/31] mm/khugepaged: flush file after unlocking folios in collapse_file() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 09/31] mm/khugepaged: check for uffd outside the i_pages lock David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 10/31] mm/khugepaged: don't wait for userfaultfd David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 12/31] mm/khugepaged: check PMD support with the folios locked David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 15/31] mm/huge_memory: don't map huge PFN entries if the arch restricts PFNs David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 16/31] mm/memory: don't clear a PUD leaf as bad in a zap David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 17/31] mm/memory: drop the swap device ref before do_wp_page() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 19/31] mm/gup: check for fatal signals in memfd_pin_folios() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 20/31] mm/migrate_device: don't collect secretmem pages David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 21/31] mm/migrate_device: mark folios leaving device memory dirty David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 22/31] mm: taint device sections before setting subsections valid David Hildenbrand (Arm)
2026-12-04 17:53 ` Rik van Riel
2026-12-04 12:50 ` [PATCH v2 24/31] mm/mempolicy: reject an empty set of allowed nodes David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 25/31] mm/ksm: look up the folio again in cmp_and_merge_page() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 26/31] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE David Hildenbrand (Arm)
2026-12-04 13:00 ` Lorenzo Stoakes (ARM)
2026-12-04 13:01 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` David Hildenbrand (Arm) [this message]
2026-12-04 12:50 ` [PATCH v2 29/31] mm/migrate: fix KSM oddity by checking expected refcount without mappings David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 30/31] mm/migrate_device: don't collect KSM folios David Hildenbrand (Arm)
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=20261204-fixes_core_3-v2-27-9aad65727632@kernel.org \
--to=david@kernel.org \
--cc=Felix.Kuehling@amd.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=airlied@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.sierra@amd.com \
--cc=alex@shazbot.org \
--cc=apopple@nvidia.com \
--cc=ast@kernel.org \
--cc=axelrasmussen@google.com \
--cc=balbirs@nvidia.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bigeasy@linutronix.de \
--cc=byungchul@sk.com \
--cc=chengming.zhou@linux.dev \
--cc=cl@gentwo.org \
--cc=clrkwllms@kernel.org \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=dev.jain@arm.com \
--cc=djbw@kernel.org \
--cc=gorcunov@gmail.com \
--cc=gourry@gourry.net \
--cc=hagen@jauu.net \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kraxel@redhat.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linmiaohe@huawei.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=ljs@kernel.org \
--cc=manfred@colorfullife.com \
--cc=mason@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=osalvador@suse.de \
--cc=peterx@redhat.com \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shakeel.butt@linux.dev \
--cc=shivankg@amd.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=stevensd@chromium.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=vivek.kasireddy@intel.com \
--cc=wangkefeng.wang@huawei.com \
--cc=weixugc@google.com \
--cc=willy@infradead.org \
--cc=xu.xin@linux.dev \
--cc=ying.huang@linux.alibaba.com \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
--cc=zokeefe@google.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