Skip to content

Commit d58a361

Browse files
cmzxoakpm00
authored andcommitted
mm/ksm: don't waste time searching stable tree for fast changing page
The code flow in cmp_and_merge_page() is suboptimal for handling the ksm page and non-ksm page at the same time. For example: - ksm page 1. Mostly just return if this ksm page is not migrated and this rmap_item has been on the rmap hlist. Or we have to fix this rmap_item mapping. 2. But we absolutely don't need to checksum for this ksm page, since it can't change. - non-ksm page 1. First don't need to waste time searching stable tree if fast changing. 2. Should try to merge with zero page before search the stable tree. 3. Then search stable tree to find mergeable ksm page. This patch optimizes the code flow so the handling differences between ksm page and non-ksm page become clearer and more efficient too. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Chengming Zhou <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Stefan Roesch <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent ac90c56 commit d58a361

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

mm/ksm.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,23 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
23662366
*/
23672367
if (!is_page_sharing_candidate(stable_node))
23682368
max_page_sharing_bypass = true;
2369+
} else {
2370+
remove_rmap_item_from_tree(rmap_item);
2371+
2372+
/*
2373+
* If the hash value of the page has changed from the last time
2374+
* we calculated it, this page is changing frequently: therefore we
2375+
* don't want to insert it in the unstable tree, and we don't want
2376+
* to waste our time searching for something identical to it there.
2377+
*/
2378+
checksum = calc_checksum(page);
2379+
if (rmap_item->oldchecksum != checksum) {
2380+
rmap_item->oldchecksum = checksum;
2381+
return;
2382+
}
2383+
2384+
if (!try_to_merge_with_zero_page(rmap_item, page))
2385+
return;
23692386
}
23702387

23712388
/* We first start with searching the page inside the stable tree */
@@ -2396,21 +2413,6 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
23962413
return;
23972414
}
23982415

2399-
/*
2400-
* If the hash value of the page has changed from the last time
2401-
* we calculated it, this page is changing frequently: therefore we
2402-
* don't want to insert it in the unstable tree, and we don't want
2403-
* to waste our time searching for something identical to it there.
2404-
*/
2405-
checksum = calc_checksum(page);
2406-
if (rmap_item->oldchecksum != checksum) {
2407-
rmap_item->oldchecksum = checksum;
2408-
return;
2409-
}
2410-
2411-
if (!try_to_merge_with_zero_page(rmap_item, page))
2412-
return;
2413-
24142416
tree_rmap_item =
24152417
unstable_tree_search_insert(rmap_item, page, &tree_page);
24162418
if (tree_rmap_item) {

0 commit comments

Comments
 (0)