Re: [PATCH 30/32] mm/migrate: fix KSM oddity by checking expected refcount without mappings
From: Zi Yan
Date: Wed Dec 02 2026 - 12:01:09 EST
On Sun Nov 29, 2026 at 5:07 AM EST, David Hildenbrand (Arm) wrote:
> KSM can add page table mappings to an unmapped folio while it is locked
> and getting migrated. While that is completely inconsistent with
The first sentence is confusing, since I read it as if KSM adds mappings
while holding the folio lock.
How about ?
KSM can add page table mappings to a folio that the migration code has
locked and unmapped.
> 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>
> 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 d09173a94e9cd..a8cb634a227d3 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -3212,6 +3212,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
> @@ -3226,7 +3262,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.,
> @@ -3247,29 +3284,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 bae9b801d7fb2..fbe12c4cd8abd 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -914,7 +914,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)
Sigh, another special case to clean up later. Anyway,
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
--
Best Regards,
Yan, Zi