Skip to contentSkip to conversation

mm, THP, swap: make reuse_swap_page() works for THP swapped out

PATCH In review [email protected] 1 participant 1 message Sep 6, 2017

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 68/119 mm, THP, swap: make reuse_swap_page() works for THP swapped out

From: Huang Ying <[email protected]>
Subject: mm, THP, swap: make reuse_swap_page() works for THP swapped out

After supporting to delay THP (Transparent Huge Page) splitting after swapped out, it is possible that some page table mappings of the THP are turned into swap entries. So reuse_swap_page() need to check the swap count in addition to the map count as before. This patch done that.

In the huge PMD write protect fault handler, in addition to the page map count, the swap count need to be checked too, so the page lock need to be acquired too when calling reuse_swap_page() in addition to the page table lock.

[[email protected]: silence a compiler warning]
Cc Johannes Weiner <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc "Kirill A . Shutemov" <[email protected]>
Cc Dan Williams <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Ross Zwisler <[email protected]> [for brd.c, zram_drv.c, pmem.c]
Cc Vishal L Verma <[email protected]>

---

 include/linux/swap.h |    4 -
 mm/huge_memory.c     |   16 ++++++
 mm/memory.c          |    6 +-
 mm/swapfile.c        |  102 +++++++++++++++++++++++++++++++++++++----
 4 files changed, 113 insertions(+), 15 deletions(-)
include/linux/swap.h renamed from include/linux/swap.h~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out +2 -2
diff -puN include/linux/swap.h~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out include/linux/swap.h
@@ -510,8 +510,8 @@ static inline int swp_swapcount(swp_entr
 	return 0;
 }
 
-#define reuse_swap_page(page, total_mapcount) \
-	(page_trans_huge_mapcount(page, total_mapcount) == 1)
+#define reuse_swap_page(page, total_map_swapcount) \
+	(page_trans_huge_mapcount(page, total_map_swapcount) == 1)
 
 static inline int try_to_free_swap(struct page *page)
 {
mm/huge_memory.c renamed from mm/huge_memory.c~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out +15 -1
diff -puN mm/huge_memory.c~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out mm/huge_memory.c
@@ -1240,15 +1240,29 @@ int do_huge_pmd_wp_page(struct vm_fault
 	 * We can only reuse the page if nobody else maps the huge page or it's
 	 * part.
 	 */
-	if (page_trans_huge_mapcount(page, NULL) == 1) {
+	if (!trylock_page(page)) {
+		get_page(page);
+		spin_unlock(vmf->ptl);
+		lock_page(page);
+		spin_lock(vmf->ptl);
+		if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
+			unlock_page(page);
+			put_page(page);
+			goto out_unlock;
+		}
+		put_page(page);
+	}
+	if (reuse_swap_page(page, NULL)) {
 		pmd_t entry;
 		entry = pmd_mkyoung(orig_pmd);
 		entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
 		if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry,  1))
 			update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
 		ret |= VM_FAULT_WRITE;
+		unlock_page(page);
 		goto out_unlock;
 	}
+	unlock_page(page);
 	get_page(page);
 	spin_unlock(vmf->ptl);
 alloc:
mm/memory.c renamed from mm/memory.c~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out +3 -3
diff -puN mm/memory.c~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out mm/memory.c
@@ -2619,7 +2619,7 @@ static int do_wp_page(struct vm_fault *v
 	 * not dirty accountable.
 	 */
 	if (PageAnon(vmf->page) && !PageKsm(vmf->page)) {
-		int total_mapcount;
+		int total_map_swapcount;
 		if (!trylock_page(vmf->page)) {
 			get_page(vmf->page);
 			pte_unmap_unlock(vmf->pte, vmf->ptl);
@@ -2634,8 +2634,8 @@ static int do_wp_page(struct vm_fault *v
 			}
 			put_page(vmf->page);
 		}
-		if (reuse_swap_page(vmf->page, &total_mapcount)) {
-			if (total_mapcount == 1) {
+		if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
+			if (total_map_swapcount == 1) {
 				/*
 				 * The page is all ours. Move it to
 				 * our anon_vma so the rmap code will
mm/swapfile.c renamed from mm/swapfile.c~mm-thp-swap-make-reuse_swap_page-works-for-thp-swapped-out +93 -9

132 lines not on this page · raw message

_