path: root/mm/ksm.c
diff options
authorKirill Tkhai <>2019-03-05 15:43:06 -0800
committerLinus Torvalds <>2019-03-05 21:07:15 -0800
commit52d1e606ee733921e984770d47539a6bb91e8506 (patch)
tree766f0493a901bb74bea0bf3d0bc01f7754e0bd4d /mm/ksm.c
parent7c9eefe82ca1efec5890678c33e66d5d520c06f4 (diff)
mm: reuse only-pte-mapped KSM page in do_wp_page()
Add an optimization for KSM pages almost in the same way that we have for ordinary anonymous pages. If there is a write fault in a page, which is mapped to an only pte, and it is not related to swap cache; the page may be reused without copying its content. [ Note that we do not consider PageSwapCache() pages at least for now, since we don't want to complicate __get_ksm_page(), which has nice optimization based on this (for the migration case). Currenly it is spinning on PageSwapCache() pages, waiting for when they have unfreezed counters (i.e., for the migration finish). But we don't want to make it also spinning on swap cache pages, which we try to reuse, since there is not a very high probability to reuse them. So, for now we do not consider PageSwapCache() pages at all. ] So in reuse_ksm_page() we check for 1) PageSwapCache() and 2) page_stable_node(), to skip a page, which KSM is currently trying to link to stable tree. Then we do page_ref_freeze() to prohibit KSM to merge one more page into the page, we are reusing. After that, nobody can refer to the reusing page: KSM skips !PageSwapCache() pages with zero refcount; and the protection against of all other participants is the same as for reused ordinary anon pages pte lock, page lock and mmap_sem. [ replace BUG_ON()s with WARN_ON()s] Link: Signed-off-by: Kirill Tkhai <> Reviewed-by: Yang Shi <> Cc: "Kirill A. Shutemov" <> Cc: Hugh Dickins <> Cc: Andrea Arcangeli <> Cc: Christian Koenig <> Cc: Claudio Imbrenda <> Cc: Rik van Riel <> Cc: Huang Ying <> Cc: Minchan Kim <> Cc: Kirill Tkhai <> Signed-off-by: Andrew Morton <> Signed-off-by: Linus Torvalds <>
Diffstat (limited to 'mm/ksm.c')
1 files changed, 28 insertions, 2 deletions
diff --git a/mm/ksm.c b/mm/ksm.c
index fd2db6a74d3c..983fbac24bda 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -706,8 +706,9 @@ again:
* case this node is no longer referenced, and should be freed;
* however, it might mean that the page is under page_ref_freeze().
* The __remove_mapping() case is easy, again the node is now stale;
- * but if page is swapcache in migrate_page_move_mapping(), it might
- * still be our page, in which case it's essential to keep the node.
+ * the same is in reuse_ksm_page() case; but if page is swapcache
+ * in migrate_page_move_mapping(), it might still be our page,
+ * in which case it's essential to keep the node.
while (!get_page_unless_zero(page)) {
@@ -2642,6 +2643,31 @@ again:
goto again;
+bool reuse_ksm_page(struct page *page,
+ struct vm_area_struct *vma,
+ unsigned long address)
+ if (WARN_ON(is_zero_pfn(page_to_pfn(page))) ||
+ WARN_ON(!page_mapped(page)) ||
+ WARN_ON(!PageLocked(page))) {
+ dump_page(page, "reuse_ksm_page");
+ return false;
+ }
+ if (PageSwapCache(page) || !page_stable_node(page))
+ return false;
+ /* Prohibit parallel get_ksm_page() */
+ if (!page_ref_freeze(page, 1))
+ return false;
+ page_move_anon_rmap(page, vma);
+ page->index = linear_page_index(vma, address);
+ page_ref_unfreeze(page, 1);
+ return true;
void ksm_migrate_page(struct page *newpage, struct page *oldpage)