Re: [PATCH 26/32] mm/ksm: look up the folio again in cmp_and_merge_page()

From: Gregory Price (Meta)

Date: Wed Dec 02 2026 - 16:51:09 EST


On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
> From: Chris Mason <mason@xxxxxxxxxx>
>
> try_to_merge_two_pages() gets a page (not a folio) passed and can end
> up splitting a large folio in try_to_merge_one_page().
>
> However, cmp_and_merge_page() does not look up the page's folio again
> after possibly splitting it, but compares the stale folio against
> tree_folio since commit a35d8872dae7 ("mm: remove PageTransCompound()").
>
> Consequently, we can end up locking a folio we hold no reference on and
> never unlocking it, but unlocking another folio that someone else might
> have locked instead.
>
> Let's fix it by looking up the folio after calling
> try_to_merge_two_pages().
>
> This has medium urgency, as ksmd can leave a folio locked.
>
> Fixes: a35d8872dae7 ("mm: remove PageTransCompound()")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
> [ david: simplify comment and description ]
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

was able to prove and observe the folio changing with a race
and showed this fixed it.

Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>

--
~Gregory