[PATCH v2 29/31] mm/migrate: fix KSM oddity by checking expected refcount without mappings

From: David Hildenbrand (Arm)

Date: Fri Dec 04 2026 - 07:59:20 EST


KSM can add page table mappings to a folio that the migration code has
locked and unmapped. While that is completely inconsistent with
everything else in MM and should be reworked, changing that is a bigger
task.

When ksmd establishes a new page table mapping, it holds a reference on
the KSM folio from stable_tree_search() to stable_tree_append(), and
relies on migration to back off due to the raised reference. However,
__migrate_folio() considers the mapcount as well, assuming that it is
simply 0. A mapping that goes away after the mapcount is read and before
the refcount is read can hide ksmd's reference:

refcount mapcount
unmapped, held by migration and ksmd 2 0
replace_page() 3 1
expected_count = mapcount + 1 = 2
write fault, COW 2 0
folio_ref_count() == expected_count

Consequently, folio_migrate_ksm() clears the stable node of the old
folio, crashing ksmd when dereferencing a NULL pointer.

Let's fix it for now by not considering page table mappings in
__migrate_folio().

This has medium urgency, as ksmd can crash due to a NULL pointer
dereference, although the race should be very hard to hit.

Fixes: 86ebd50224c0 ("mm: add folio_expected_ref_count() for reference count calculation")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/mm.h | 62 ++++++++++++++++++++++++++++++++++--------------------
mm/migrate.c | 2 +-
2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 62c720c2d4542..3826e51b1bc59 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3217,6 +3217,42 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)
return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids);
}

+/**
+ * folio_expected_unmapped_ref_count - expected refcount ignoring page table
+ * mappings
+ * @folio: the folio
+ *
+ * Like folio_expected_ref_count(), but without references from page table
+ * mappings: any page table mapping will be detected as an unexpected reference.
+ *
+ * Returns: the expected folio refcount, excluding page table mappings.
+ */
+static inline int folio_expected_unmapped_ref_count(const struct folio *folio)
+{
+ const int order = folio_order(folio);
+ int ref_count = 0;
+
+ if (WARN_ON_ONCE(page_has_type(&folio->page) && !folio_test_hugetlb(folio)))
+ return 0;
+
+ /* One reference per page from the swapcache. */
+ ref_count += folio_test_swapcache(folio) << order;
+
+ if (!folio_test_anon(folio)) {
+ /*
+ * One reference per page from the pagecache.
+ * Use data_race() since folio might not be locked.
+ */
+ ref_count += !!data_race(folio->mapping) << order;
+ /*
+ * One reference from filesystem private data.
+ */
+ ref_count += folio_has_attached_private(folio);
+ }
+
+ return ref_count;
+}
+
/**
* folio_expected_ref_count - calculate the expected folio refcount
* @folio: the folio
@@ -3231,7 +3267,8 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)
* the LRU cache does not apply.
*
* Calling this function on an unmapped folio -- !folio_mapped() -- that is
- * locked will return a stable result.
+ * locked will return a stable result, unless it is a KSM folio: KSM can map
+ * these without the folio lock.
*
* Calling this function on a mapped folio will not result in a stable result,
* because nothing stops additional page table mappings from coming (e.g.,
@@ -3252,29 +3289,8 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)
*/
static inline int folio_expected_ref_count(const struct folio *folio)
{
- const int order = folio_order(folio);
- int ref_count = 0;
-
- if (WARN_ON_ONCE(page_has_type(&folio->page) && !folio_test_hugetlb(folio)))
- return 0;
-
- /* One reference per page from the swapcache. */
- ref_count += folio_test_swapcache(folio) << order;
-
- if (!folio_test_anon(folio)) {
- /*
- * One reference per page from the pagecache.
- * Use data_race() since folio might not be locked.
- */
- ref_count += !!data_race(folio->mapping) << order;
- /*
- * One reference from filesystem private data.
- */
- ref_count += folio_has_attached_private(folio);
- }
-
/* One reference per page table mapping. */
- return ref_count + folio_mapcount(folio);
+ return folio_expected_unmapped_ref_count(folio) + folio_mapcount(folio);
}

#ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE
diff --git a/mm/migrate.c b/mm/migrate.c
index 2fe4cd936e71b..4743bd76e93f4 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -917,7 +917,7 @@ static int __migrate_folio(struct address_space *mapping, struct folio *dst,
struct folio *src, void *src_private,
enum migrate_mode mode)
{
- int rc, expected_count = folio_expected_ref_count(src) + 1;
+ int rc, expected_count = folio_expected_unmapped_ref_count(src) + 1;

/* Check whether src does not have extra refs before we do more work */
if (folio_ref_count(src) != expected_count)

--
2.56.1