mm/thp: collapse_file() do try_to_unmap(TTU_BATCH_FLUSH)
authorHugh Dickins <hughd@google.com>
Tue, 15 Feb 2022 02:40:55 +0000 (18:40 -0800)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Thu, 17 Feb 2022 16:59:45 +0000 (11:59 -0500)
collapse_file() is using unmap_mapping_pages(1) on each small page found
mapped, unlike others (reclaim, migration, splitting, memory-failure) who
use try_to_unmap().  There are four advantages to try_to_unmap(): first,
its TTU_IGNORE_MLOCK option now avoids leaving mlocked page in pagevec;
second, its vma lookup uses i_mmap_lock_read() not i_mmap_lock_write();
third, it breaks out early if page is not mapped everywhere it might be;
fourth, its TTU_BATCH_FLUSH option can be used, as in page reclaim, to
save up all the TLB flushing until all of the pages have been unmapped.

Wild guess: perhaps it was originally written to use try_to_unmap(),
but hit the VM_BUG_ON_PAGE(page_mapped) after unmapping, because without
TTU_SYNC it may skip page table locks; but unmap_mapping_pages() never
skips them, so fixed the issue.  I did once hit that VM_BUG_ON_PAGE()
since making this change: we could pass TTU_SYNC here, but I think just
delete the check - the race is very rare, this is an ordinary small page
so we don't need to be so paranoid about mapcount surprises, and the
page_ref_freeze() just below already handles the case adequately.

Signed-off-by: Hugh Dickins <hughd@google.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
mm/khugepaged.c

index 52add1825525eaa13674a3be317ddf30b44ab1e8..fa05e6d397833bd1f2910ca07f008679a16c48a3 100644 (file)
@@ -1834,13 +1834,12 @@ static void collapse_file(struct mm_struct *mm,
                }
 
                if (page_mapped(page))
-                       unmap_mapping_pages(mapping, index, 1, false);
+                       try_to_unmap(page, TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
 
                xas_lock_irq(&xas);
                xas_set(&xas, index);
 
                VM_BUG_ON_PAGE(page != xas_load(&xas), page);
-               VM_BUG_ON_PAGE(page_mapped(page), page);
 
                /*
                 * The page is expected to have page_count() == 3:
@@ -1904,6 +1903,13 @@ xa_locked:
        xas_unlock_irq(&xas);
 xa_unlocked:
 
+       /*
+        * If collapse is successful, flush must be done now before copying.
+        * If collapse is unsuccessful, does flush actually need to be done?
+        * Do it anyway, to clear the state.
+        */
+       try_to_unmap_flush();
+
        if (result == SCAN_SUCCEED) {
                struct page *page, *tmp;