So far we were under the assumption that only VM_MERGEABLE VMAs may contain KSM folios, because we unmerge when clearing VM_MERGEABLE, and KSM only merges within VM_MERGEABLE VMAs. Various infrastructure (e.g., rmap walks) relies on related properties: for example that a KSM folio is always mapped at rmap_item->address, also in child processes. We ignored one scenario: turning a COW-shared folio into a KSM folio in one process will make all processes mapping it suddenly see a KSM folio, even if VM_MERGEABLE is not set for them. Consequently, rmap walks can miss such folio mappings, resulting in the KSM folio no longer being reclaimable or migratable. Worse, if such a process calls mremap() while the kernel is migrating a KSM folio, mremap() moves the migration entry that try_to_migrate() installed, and remove_migration_ptes() will not find it at its new address. Once migration frees the source folio, the moved PTE still holds a migration entry that refers to the freed, and possibly already reused, folio: the next access through that PTE will hang, or worse. Further, if ksmd scans the child, rmap walks will not even find the parent's VMA: the child's rmap_item references the child's anon_vma, which the parent's VMA is not linked into. Let's fix it all by refusing to convert a folio into a KSM folio if it might be shared with another mm. When merging two folios that are not KSM folios yet, try both directions: as long as one folio is not shared, we can merge. Shared folios can still be merged into an existing KSM folio, the shared zeropage or an identical folio that is not shared. Only identical folios that are all shared will no longer be merged, which should be fine for the known use cases. Note that other processes might only hold a swap entry for the folio, and map it after swapping it in. As the previous commit only copies KSM folios at swap-in into VMAs without VM_MERGEABLE, also refuse to convert a folio that is in the swap cache, unless we can free the swap entry. This has medium urgency, as an unprivileged process can trigger a use-after-free if KSM is enabled. Fixes: 80e148226028 ("ksm: share anon page without allocating") Fixes: 5beb49305251 ("mm: change anon_vma linking to fix multi-process server scalability issue") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/ksm.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 509bd78156583..fe7b3b60540db 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1494,7 +1494,9 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, * @kpage: the KSM page that we want to map instead of page, * or NULL the first time when we want to use page as kpage. * - * This function returns 0 if the pages were merged, -EFAULT otherwise. + * This function returns 0 if the pages were merged, -EBUSY if page must not be + * turned into a KSM folio because another mm maps it or can still swap it in, + * -EFAULT otherwise. */ static int try_to_merge_one_page(struct vm_area_struct *vma, unsigned long addr, struct page *page, struct page *kpage) @@ -1509,6 +1511,12 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, if (!folio_test_anon(folio) || folio_test_ksm(folio)) goto out; + /* Avoid lock+split if the folio is obviously shared. */ + if (!kpage && folio_maybe_mapped_shared(folio)) { + err = -EBUSY; + goto out; + } + /* * We need the folio lock to read a stable swapcache flag in * write_protect_page(). We trylock because we don't want to wait @@ -1524,6 +1532,18 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, folio = page_folio(page); } + /* + * Turning a folio that is shared with other processes, or that other + * processes could still swap in, into a KSM folio can result in KSM + * folios showing up in VMAs that never had KSM enabled, or in VMAs that + * rmap walks cannot find. So just deny on any sign of sharing. + */ + if (!kpage && (folio_maybe_mapped_shared(folio) || + (folio_test_swapcache(folio) && !folio_free_swap(folio)))) { + err = -EBUSY; + goto out_unlock; + } + /* * If this anonymous page is mapped only here, its pte may need * to be write-protected. If it's mapped elsewhere, all of its @@ -1598,7 +1618,9 @@ static int try_to_merge_with_zero_page(struct ksm_rmap_item *rmap_item, * try_to_merge_with_ksm_page - like try_to_merge_two_pages, * but no new kernel page is allocated: kpage must already be a ksm page. * - * This function returns 0 if the pages were merged, -EFAULT otherwise. + * This function returns 0 if the pages were merged, -EBUSY if page must not be + * turned into a KSM folio because another mm maps it or can still swap it in, + * -EFAULT otherwise. */ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, struct page *page, struct page *kpage) @@ -1642,7 +1664,8 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, * to be merged into one page. * * This function returns the kpage if we successfully merged two identical - * pages into one ksm page, NULL otherwise. + * pages into one ksm page, ERR_PTR(-EBUSY) if page must not be turned into a + * ksm page, NULL otherwise. * * Note that this function upgrades page to ksm page: if one of the pages * is already a ksm page, try_to_merge_with_ksm_page should be used. @@ -1655,6 +1678,8 @@ static struct folio *try_to_merge_two_pages(struct ksm_rmap_item *rmap_item, int err; err = try_to_merge_with_ksm_page(rmap_item, page, NULL); + if (err == -EBUSY) + return ERR_PTR(-EBUSY); if (!err) { err = try_to_merge_with_ksm_page(tree_rmap_item, tree_page, page); @@ -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 -- 2.56.1