Re: [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio
From: sashiko-bot
Date: Sat Dec 05 2026 - 08:00:08 EST
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/ksm: cmp_and_merge_page() splits shared large folios after
-EBUSY
--
commit 999d85f121ae9110b9d07446c1254f5c0a633c39
Author: David Hildenbrand (Arm) <david@xxxxxxxxxx>
mm/ksm: don't convert a shared anon folio into a KSM folio
This commit refuses to convert an anonymous folio into a KSM folio when it
may be shared with another mm or still has unfreeable swap entries, returning
-EBUSY before locking or splitting. When merging two non-KSM pages in
cmp_and_merge_page(), it retries in the reverse direction if the first
attempt returns ERR_PTR(-EBUSY).
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 509bd78156583..fe7b3b60540db 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
[ ... ]
> @@ -2359,8 +2384,19 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
> struct folio *tree_folio;
> bool split;
>
> + /*
> + * Try merging in both directions: if one folio is shared but
> + * the other isn't, one direction will work. Merging will fail
> + * if both folios are shared.
> + */
> kfolio = try_to_merge_two_pages(rmap_item, page,
> tree_rmap_item, tree_page);
> + if (kfolio == ERR_PTR(-EBUSY))
> + kfolio = try_to_merge_two_pages(tree_rmap_item, tree_page,
> + rmap_item, page);
> + if (IS_ERR(kfolio))
> + kfolio = NULL;
> +
> /* We might have split the folio. */
> folio = page_folio(page);
> tree_folio = page_folio(tree_page);
> @@ -2375,7 +2411,6 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
> * split_huge_page should succeed.
> */
> split = folio == tree_folio;
> - folio_put(tree_folio);
> if (kfolio) {
> /*
> * The pages were successfully merged: insert new
> @@ -2401,7 +2436,9 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
> break_cow(tree_rmap_item);
> break_cow(rmap_item);
> }
> - } else if (split) {
> + }
> + folio_put(tree_folio);
> + if (!kfolio && split) {
> /*
> * We are here if we tried to merge two pages and
> * failed because they both belonged to the same
[Severity: Medium]
Does flattening ERR_PTR(-EBUSY) to NULL cause cmp_and_merge_page() to split a
shared large folio anyway?
When page and tree_page are two identical subpages of the same fork-shared
large folio, try_to_merge_one_page() returns -EBUSY early to avoid locking or
splitting it. Overwriting ERR_PTR(-EBUSY) with NULL leaves split set to true
(folio == tree_folio), so if (!kfolio && split) still calls split_huge_page().
That breaks up the shared THP across all processes mapping it for no
deduplication gain, since the resulting order-0 folios remain shared and
still return -EBUSY on later scans.
Retrying try_to_merge_two_pages() in the reverse direction is also redundant
when page and tree_page belong to the same folio.
Setting split = !IS_ERR(kfolio) && folio == tree_folio before clearing the
error pointer, and skipping the reverse retry when
page_folio(page) == page_folio(tree_page), would avoid both.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-fixes_core_3-v2-0-9aad65727632@xxxxxxxxxx?part=27