Re: [PATCH 26/32] mm/ksm: look up the folio again in cmp_and_merge_page()
From: Lorenzo Stoakes (ARM)
Date: Fri Dec 04 2026 - 07:05:30 EST
On Sun, Nov 29, 2026 at 11:06:57AM +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>
LGTM
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> mm/ksm.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 444bb7bb62cfe..90cc83275764d 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2361,6 +2361,8 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
>
> kfolio = try_to_merge_two_pages(rmap_item, page,
> tree_rmap_item, tree_page);
> + /* We might have split the folio. */
> + folio = page_folio(page);
> tree_folio = page_folio(tree_page);
> /*
> * If both pages we tried to merge belong to the same (large)
>
> --
> 2.56.1
>
--
Cheers, Lorenzo