Re: [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio
From: David Hildenbrand (Arm)
Date: Sat Dec 05 2026 - 11:48:55 EST
On 12/5/26 13:59, sashiko-bot@xxxxxxxxxx wrote:
> 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.
We could throw in a
diff --git a/mm/ksm.c b/mm/ksm.c
index 33c270aae3285..330b06175d509 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2478,7 +2478,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
}
}
folio_put(tree_folio);
- if (!kfolio && split) {
+ if (!kfolio && split && !folio_maybe_mapped_shared(folio)) {
/*
* We are here if we tried to merge two pages and
* failed because they both belonged to the same
But it raises the question how to handle folios that are "maybe shared" but have some
unshared pages that we could merge after a split.
We could also drop the early folio_maybe_mapped_shared() before the split this patch adds.
But ... I don't think we care about COW-shared folios all that much.
So I'll just fold the above check in when applying.
--
Cheers,
David