It's interesting that a mapped ksm page also need to stable_tree_search(), instead of using stable_tree_insert() directly. The reason is that we have a minor optimization for migrated ksm page that has only one mapcount, in which case we can find another ksm page that already on the stable tree to replace it. But what if we can't find another shareable candidate on the stable tree? Obviously, we should just return the ksm page itself if it has been inserted on the tree. And we shouldn't return NULL if no another ksm page is found on the tree, since we will still map on this ksm page but the rmap_item will be removed out to insert on the unstable tree if we return NULL in this case. We can ignore the is_page_sharing_candidate() check in this case, since max_page_sharing_bypass is set to true in cmp_and_merge_page(). Signed-off-by: Chengming Zhou <chengming.zhou@xxxxxxxxx> --- mm/ksm.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index f923699452ed..6dea83998258 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1940,11 +1940,8 @@ static struct page *stable_tree_search(struct page *page) rb_link_node(&page_node->node, parent, new); rb_insert_color(&page_node->node, root); out: - if (is_page_sharing_candidate(page_node)) { - folio_get(folio); - return &folio->page; - } else - return NULL; + folio_get(folio); + return &folio->page; replace: /* @@ -1966,10 +1963,7 @@ static struct page *stable_tree_search(struct page *page) rb_replace_node(&stable_node_dup->node, &page_node->node, root); - if (is_page_sharing_candidate(page_node)) - folio_get(folio); - else - folio = NULL; + folio_get(folio); } else { rb_erase(&stable_node_dup->node, root); folio = NULL; @@ -1982,10 +1976,7 @@ static struct page *stable_tree_search(struct page *page) list_del(&page_node->list); DO_NUMA(page_node->nid = nid); stable_node_chain_add_dup(page_node, stable_node); - if (is_page_sharing_candidate(page_node)) - folio_get(folio); - else - folio = NULL; + folio_get(folio); } else { folio = NULL; } @@ -2009,7 +2000,7 @@ static struct page *stable_tree_search(struct page *page) stable_node = alloc_stable_node_chain(stable_node_dup, root); if (!stable_node) - return NULL; + goto out; } /* * Add this stable_node dup that was -- 2.45.1