From: Chris Mason 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@vger.kernel.org Signed-off-by: Chris Mason [ david: simplify comment and description ] Signed-off-by: David Hildenbrand (Arm) --- 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