hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/mm/khugepaged.c
....@@ -28,7 +28,10 @@
2828 SCAN_SUCCEED,
2929 SCAN_PMD_NULL,
3030 SCAN_EXCEED_NONE_PTE,
31
+ SCAN_EXCEED_SWAP_PTE,
32
+ SCAN_EXCEED_SHARED_PTE,
3133 SCAN_PTE_NON_PRESENT,
34
+ SCAN_PTE_UFFD_WP,
3235 SCAN_PAGE_RO,
3336 SCAN_LACK_REFERENCED_PAGE,
3437 SCAN_PAGE_NULL,
....@@ -46,8 +49,8 @@
4649 SCAN_DEL_PAGE_LRU,
4750 SCAN_ALLOC_HUGE_PAGE_FAIL,
4851 SCAN_CGROUP_CHARGE_FAIL,
49
- SCAN_EXCEED_SWAP_PTE,
5052 SCAN_TRUNCATED,
53
+ SCAN_PAGE_HAS_PRIVATE,
5154 };
5255
5356 #define CREATE_TRACE_POINTS
....@@ -73,11 +76,14 @@
7376 */
7477 static unsigned int khugepaged_max_ptes_none __read_mostly;
7578 static unsigned int khugepaged_max_ptes_swap __read_mostly;
79
+static unsigned int khugepaged_max_ptes_shared __read_mostly;
7680
7781 #define MM_SLOTS_HASH_BITS 10
7882 static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
7983
8084 static struct kmem_cache *mm_slot_cache __read_mostly;
85
+
86
+#define MAX_PTE_MAPPED_THP 8
8187
8288 /**
8389 * struct mm_slot - hash lookup from mm to mm_slot
....@@ -89,6 +95,10 @@
8995 struct hlist_node hash;
9096 struct list_head mm_node;
9197 struct mm_struct *mm;
98
+
99
+ /* pte-mapped THP in this mm */
100
+ int nr_pte_mapped_thp;
101
+ unsigned long pte_mapped_thp[MAX_PTE_MAPPED_THP];
92102 };
93103
94104 /**
....@@ -286,15 +296,43 @@
286296 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
287297 khugepaged_max_ptes_swap_store);
288298
299
+static ssize_t khugepaged_max_ptes_shared_show(struct kobject *kobj,
300
+ struct kobj_attribute *attr,
301
+ char *buf)
302
+{
303
+ return sprintf(buf, "%u\n", khugepaged_max_ptes_shared);
304
+}
305
+
306
+static ssize_t khugepaged_max_ptes_shared_store(struct kobject *kobj,
307
+ struct kobj_attribute *attr,
308
+ const char *buf, size_t count)
309
+{
310
+ int err;
311
+ unsigned long max_ptes_shared;
312
+
313
+ err = kstrtoul(buf, 10, &max_ptes_shared);
314
+ if (err || max_ptes_shared > HPAGE_PMD_NR-1)
315
+ return -EINVAL;
316
+
317
+ khugepaged_max_ptes_shared = max_ptes_shared;
318
+
319
+ return count;
320
+}
321
+
322
+static struct kobj_attribute khugepaged_max_ptes_shared_attr =
323
+ __ATTR(max_ptes_shared, 0644, khugepaged_max_ptes_shared_show,
324
+ khugepaged_max_ptes_shared_store);
325
+
289326 static struct attribute *khugepaged_attr[] = {
290327 &khugepaged_defrag_attr.attr,
291328 &khugepaged_max_ptes_none_attr.attr,
329
+ &khugepaged_max_ptes_swap_attr.attr,
330
+ &khugepaged_max_ptes_shared_attr.attr,
292331 &pages_to_scan_attr.attr,
293332 &pages_collapsed_attr.attr,
294333 &full_scans_attr.attr,
295334 &scan_sleep_millisecs_attr.attr,
296335 &alloc_sleep_millisecs_attr.attr,
297
- &khugepaged_max_ptes_swap_attr.attr,
298336 NULL,
299337 };
300338
....@@ -303,8 +341,6 @@
303341 .name = "khugepaged",
304342 };
305343 #endif /* CONFIG_SYSFS */
306
-
307
-#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
308344
309345 int hugepage_madvise(struct vm_area_struct *vma,
310346 unsigned long *vm_flags, int advice)
....@@ -356,6 +392,7 @@
356392 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
357393 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
358394 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
395
+ khugepaged_max_ptes_shared = HPAGE_PMD_NR / 2;
359396
360397 return 0;
361398 }
....@@ -397,25 +434,39 @@
397434
398435 static inline int khugepaged_test_exit(struct mm_struct *mm)
399436 {
400
- return atomic_read(&mm->mm_users) == 0 || !mmget_still_valid(mm);
437
+ return atomic_read(&mm->mm_users) == 0;
401438 }
402439
403440 static bool hugepage_vma_check(struct vm_area_struct *vma,
404441 unsigned long vm_flags)
405442 {
406
- if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
407
- (vm_flags & VM_NOHUGEPAGE) ||
408
- test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
443
+ if (!transhuge_vma_enabled(vma, vm_flags))
409444 return false;
410
- if (shmem_file(vma->vm_file)) {
411
- if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
412
- return false;
413
- return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
414
- HPAGE_PMD_NR);
445
+
446
+ if (vma->vm_file && !IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) -
447
+ vma->vm_pgoff, HPAGE_PMD_NR))
448
+ return false;
449
+
450
+ /* Enabled via shmem mount options or sysfs settings. */
451
+ if (shmem_file(vma->vm_file))
452
+ return shmem_huge_enabled(vma);
453
+
454
+ /* THP settings require madvise. */
455
+ if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
456
+ return false;
457
+
458
+ /* Only regular file is valid */
459
+ if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
460
+ !inode_is_open_for_write(vma->vm_file->f_inode) &&
461
+ (vm_flags & VM_EXEC)) {
462
+ struct inode *inode = vma->vm_file->f_inode;
463
+
464
+ return S_ISREG(inode->i_mode);
415465 }
466
+
416467 if (!vma->anon_vma || vma->vm_ops)
417468 return false;
418
- if (is_vma_temporary_stack(vma))
469
+ if (vma_is_temporary_stack(vma))
419470 return false;
420471 return !(vm_flags & VM_NO_KHUGEPAGED);
421472 }
....@@ -459,8 +510,9 @@
459510 unsigned long hstart, hend;
460511
461512 /*
462
- * khugepaged does not yet work on non-shmem files or special
463
- * mappings. And file-private shmem THP is not supported.
513
+ * khugepaged only supports read-only files for non-shmem files.
514
+ * khugepaged does not yet work on special mappings. And
515
+ * file-private shmem THP is not supported.
464516 */
465517 if (!hugepage_vma_check(vma, vm_flags))
466518 return 0;
....@@ -497,36 +549,61 @@
497549 * under mmap sem read mode). Stop here (after we
498550 * return all pagetables will be destroyed) until
499551 * khugepaged has finished working on the pagetables
500
- * under the mmap_sem.
552
+ * under the mmap_lock.
501553 */
502
- down_write(&mm->mmap_sem);
503
- up_write(&mm->mmap_sem);
554
+ mmap_write_lock(mm);
555
+ mmap_write_unlock(mm);
504556 }
505557 }
506558
507559 static void release_pte_page(struct page *page)
508560 {
509
- dec_node_page_state(page, NR_ISOLATED_ANON + page_is_file_cache(page));
561
+ mod_node_page_state(page_pgdat(page),
562
+ NR_ISOLATED_ANON + page_is_file_lru(page),
563
+ -compound_nr(page));
510564 unlock_page(page);
511565 putback_lru_page(page);
512566 }
513567
514
-static void release_pte_pages(pte_t *pte, pte_t *_pte)
568
+static void release_pte_pages(pte_t *pte, pte_t *_pte,
569
+ struct list_head *compound_pagelist)
515570 {
571
+ struct page *page, *tmp;
572
+
516573 while (--_pte >= pte) {
517574 pte_t pteval = *_pte;
518
- if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
519
- release_pte_page(pte_page(pteval));
575
+
576
+ page = pte_page(pteval);
577
+ if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)) &&
578
+ !PageCompound(page))
579
+ release_pte_page(page);
520580 }
581
+
582
+ list_for_each_entry_safe(page, tmp, compound_pagelist, lru) {
583
+ list_del(&page->lru);
584
+ release_pte_page(page);
585
+ }
586
+}
587
+
588
+static bool is_refcount_suitable(struct page *page)
589
+{
590
+ int expected_refcount;
591
+
592
+ expected_refcount = total_mapcount(page);
593
+ if (PageSwapCache(page))
594
+ expected_refcount += compound_nr(page);
595
+
596
+ return page_count(page) == expected_refcount;
521597 }
522598
523599 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
524600 unsigned long address,
525
- pte_t *pte)
601
+ pte_t *pte,
602
+ struct list_head *compound_pagelist)
526603 {
527604 struct page *page = NULL;
528605 pte_t *_pte;
529
- int none_or_zero = 0, result = 0, referenced = 0;
606
+ int none_or_zero = 0, shared = 0, result = 0, referenced = 0;
530607 bool writable = false;
531608
532609 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
....@@ -552,13 +629,27 @@
552629 goto out;
553630 }
554631
555
- /* TODO: teach khugepaged to collapse THP mapped with pte */
556
- if (PageCompound(page)) {
557
- result = SCAN_PAGE_COMPOUND;
632
+ VM_BUG_ON_PAGE(!PageAnon(page), page);
633
+
634
+ if (page_mapcount(page) > 1 &&
635
+ ++shared > khugepaged_max_ptes_shared) {
636
+ result = SCAN_EXCEED_SHARED_PTE;
558637 goto out;
559638 }
560639
561
- VM_BUG_ON_PAGE(!PageAnon(page), page);
640
+ if (PageCompound(page)) {
641
+ struct page *p;
642
+ page = compound_head(page);
643
+
644
+ /*
645
+ * Check if we have dealt with the compound page
646
+ * already
647
+ */
648
+ list_for_each_entry(p, compound_pagelist, lru) {
649
+ if (page == p)
650
+ goto next;
651
+ }
652
+ }
562653
563654 /*
564655 * We can do it before isolate_lru_page because the
....@@ -572,28 +663,30 @@
572663 }
573664
574665 /*
575
- * cannot use mapcount: can't collapse if there's a gup pin.
576
- * The page must only be referenced by the scanned process
577
- * and page swap cache.
666
+ * Check if the page has any GUP (or other external) pins.
667
+ *
668
+ * The page table that maps the page has been already unlinked
669
+ * from the page table tree and this process cannot get
670
+ * an additinal pin on the page.
671
+ *
672
+ * New pins can come later if the page is shared across fork,
673
+ * but not from this process. The other process cannot write to
674
+ * the page, only trigger CoW.
578675 */
579
- if (page_count(page) != 1 + PageSwapCache(page)) {
676
+ if (!is_refcount_suitable(page)) {
580677 unlock_page(page);
581678 result = SCAN_PAGE_COUNT;
582679 goto out;
583680 }
584
- if (pte_write(pteval)) {
585
- writable = true;
586
- } else {
587
- if (PageSwapCache(page) &&
588
- !reuse_swap_page(page, NULL)) {
589
- unlock_page(page);
590
- result = SCAN_SWAP_CACHE_PAGE;
591
- goto out;
592
- }
681
+ if (!pte_write(pteval) && PageSwapCache(page) &&
682
+ !reuse_swap_page(page, NULL)) {
593683 /*
594
- * Page is not in the swap cache. It can be collapsed
595
- * into a THP.
684
+ * Page is in the swap cache and cannot be re-used.
685
+ * It cannot be collapsed into a THP.
596686 */
687
+ unlock_page(page);
688
+ result = SCAN_SWAP_CACHE_PAGE;
689
+ goto out;
597690 }
598691
599692 /*
....@@ -605,16 +698,23 @@
605698 result = SCAN_DEL_PAGE_LRU;
606699 goto out;
607700 }
608
- inc_node_page_state(page,
609
- NR_ISOLATED_ANON + page_is_file_cache(page));
701
+ mod_node_page_state(page_pgdat(page),
702
+ NR_ISOLATED_ANON + page_is_file_lru(page),
703
+ compound_nr(page));
610704 VM_BUG_ON_PAGE(!PageLocked(page), page);
611705 VM_BUG_ON_PAGE(PageLRU(page), page);
612706
707
+ if (PageCompound(page))
708
+ list_add_tail(&page->lru, compound_pagelist);
709
+next:
613710 /* There should be enough young pte to collapse the page */
614711 if (pte_young(pteval) ||
615712 page_is_young(page) || PageReferenced(page) ||
616713 mmu_notifier_test_young(vma->vm_mm, address))
617714 referenced++;
715
+
716
+ if (pte_write(pteval))
717
+ writable = true;
618718 }
619719
620720 if (unlikely(!writable)) {
....@@ -628,7 +728,7 @@
628728 return 1;
629729 }
630730 out:
631
- release_pte_pages(pte, _pte);
731
+ release_pte_pages(pte, _pte, compound_pagelist);
632732 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
633733 referenced, writable, result);
634734 return 0;
....@@ -637,13 +737,14 @@
637737 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
638738 struct vm_area_struct *vma,
639739 unsigned long address,
640
- spinlock_t *ptl)
740
+ spinlock_t *ptl,
741
+ struct list_head *compound_pagelist)
641742 {
743
+ struct page *src_page, *tmp;
642744 pte_t *_pte;
643745 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
644746 _pte++, page++, address += PAGE_SIZE) {
645747 pte_t pteval = *_pte;
646
- struct page *src_page;
647748
648749 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
649750 clear_user_highpage(page, address);
....@@ -663,8 +764,8 @@
663764 } else {
664765 src_page = pte_page(pteval);
665766 copy_user_highpage(page, src_page, address, vma);
666
- VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
667
- release_pte_page(src_page);
767
+ if (!PageCompound(src_page))
768
+ release_pte_page(src_page);
668769 /*
669770 * ptl mostly unnecessary, but preempt has to
670771 * be disabled to update the per-cpu stats
....@@ -680,6 +781,11 @@
680781 spin_unlock(ptl);
681782 free_page_and_swap_cache(src_page);
682783 }
784
+ }
785
+
786
+ list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) {
787
+ list_del(&src_page->lru);
788
+ release_pte_page(src_page);
683789 }
684790 }
685791
....@@ -713,7 +819,7 @@
713819 for (i = 0; i < MAX_NUMNODES; i++) {
714820 if (!khugepaged_node_load[i])
715821 continue;
716
- if (node_distance(nid, i) > RECLAIM_DISTANCE)
822
+ if (node_distance(nid, i) > node_reclaim_distance)
717823 return true;
718824 }
719825 return false;
....@@ -854,8 +960,8 @@
854960 #endif
855961
856962 /*
857
- * If mmap_sem temporarily dropped, revalidate vma
858
- * before taking mmap_sem.
963
+ * If mmap_lock temporarily dropped, revalidate vma
964
+ * before taking mmap_lock.
859965 * Return 0 if succeeds, otherwise return none-zero
860966 * value (scan code).
861967 */
....@@ -879,6 +985,9 @@
879985 return SCAN_ADDRESS_RANGE;
880986 if (!hugepage_vma_check(vma, vma->vm_flags))
881987 return SCAN_VMA_CHECK;
988
+ /* Anon VMA expected */
989
+ if (!vma->anon_vma || vma->vm_ops)
990
+ return SCAN_VMA_CHECK;
882991 return 0;
883992 }
884993
....@@ -887,48 +996,48 @@
887996 * Only done if khugepaged_scan_pmd believes it is worthwhile.
888997 *
889998 * Called and returns without pte mapped or spinlocks held,
890
- * but with mmap_sem held to protect against vma changes.
999
+ * but with mmap_lock held to protect against vma changes.
8911000 */
8921001
8931002 static bool __collapse_huge_page_swapin(struct mm_struct *mm,
8941003 struct vm_area_struct *vma,
895
- unsigned long address, pmd_t *pmd,
1004
+ unsigned long haddr, pmd_t *pmd,
8961005 int referenced)
8971006 {
8981007 int swapped_in = 0;
8991008 vm_fault_t ret = 0;
900
- struct vm_fault vmf = {
901
- .vma = vma,
902
- .address = address,
903
- .flags = FAULT_FLAG_ALLOW_RETRY,
904
- .pmd = pmd,
905
- .pgoff = linear_page_index(vma, address),
906
- };
1009
+ unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
9071010
908
- /* we only decide to swapin, if there is enough young ptes */
909
- if (referenced < HPAGE_PMD_NR/2) {
910
- trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
911
- return false;
912
- }
913
- vmf.pte = pte_offset_map(pmd, address);
914
- for (; vmf.address < address + HPAGE_PMD_NR*PAGE_SIZE;
915
- vmf.pte++, vmf.address += PAGE_SIZE) {
1011
+ for (address = haddr; address < end; address += PAGE_SIZE) {
1012
+ struct vm_fault vmf = {
1013
+ .vma = vma,
1014
+ .address = address,
1015
+ .pgoff = linear_page_index(vma, haddr),
1016
+ .flags = FAULT_FLAG_ALLOW_RETRY,
1017
+ .pmd = pmd,
1018
+ .vma_flags = vma->vm_flags,
1019
+ .vma_page_prot = vma->vm_page_prot,
1020
+ };
1021
+
1022
+ vmf.pte = pte_offset_map(pmd, address);
9161023 vmf.orig_pte = *vmf.pte;
917
- if (!is_swap_pte(vmf.orig_pte))
1024
+ if (!is_swap_pte(vmf.orig_pte)) {
1025
+ pte_unmap(vmf.pte);
9181026 continue;
1027
+ }
9191028 swapped_in++;
9201029 ret = do_swap_page(&vmf);
9211030
922
- /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
1031
+ /* do_swap_page returns VM_FAULT_RETRY with released mmap_lock */
9231032 if (ret & VM_FAULT_RETRY) {
924
- down_read(&mm->mmap_sem);
925
- if (hugepage_vma_revalidate(mm, address, &vmf.vma)) {
1033
+ mmap_read_lock(mm);
1034
+ if (hugepage_vma_revalidate(mm, haddr, &vma)) {
9261035 /* vma is no longer available, don't continue to swapin */
9271036 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9281037 return false;
9291038 }
9301039 /* check if the pmd is still valid */
931
- if (mm_find_pmd(mm, address) != pmd) {
1040
+ if (mm_find_pmd(mm, haddr) != pmd) {
9321041 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9331042 return false;
9341043 }
....@@ -937,11 +1046,12 @@
9371046 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9381047 return false;
9391048 }
940
- /* pte is unmapped now, we need to map it */
941
- vmf.pte = pte_offset_map(pmd, vmf.address);
9421049 }
943
- vmf.pte--;
944
- pte_unmap(vmf.pte);
1050
+
1051
+ /* Drain LRU add pagevec to remove extra pin on the swapped in pages */
1052
+ if (swapped_in)
1053
+ lru_add_drain();
1054
+
9451055 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
9461056 return true;
9471057 }
....@@ -949,18 +1059,17 @@
9491059 static void collapse_huge_page(struct mm_struct *mm,
9501060 unsigned long address,
9511061 struct page **hpage,
952
- int node, int referenced)
1062
+ int node, int referenced, int unmapped)
9531063 {
1064
+ LIST_HEAD(compound_pagelist);
9541065 pmd_t *pmd, _pmd;
9551066 pte_t *pte;
9561067 pgtable_t pgtable;
9571068 struct page *new_page;
9581069 spinlock_t *pmd_ptl, *pte_ptl;
9591070 int isolated = 0, result = 0;
960
- struct mem_cgroup *memcg;
9611071 struct vm_area_struct *vma;
962
- unsigned long mmun_start; /* For mmu_notifiers */
963
- unsigned long mmun_end; /* For mmu_notifiers */
1072
+ struct mmu_notifier_range range;
9641073 gfp_t gfp;
9651074
9661075 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
....@@ -969,57 +1078,56 @@
9691078 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
9701079
9711080 /*
972
- * Before allocating the hugepage, release the mmap_sem read lock.
1081
+ * Before allocating the hugepage, release the mmap_lock read lock.
9731082 * The allocation can take potentially a long time if it involves
974
- * sync compaction, and we do not need to hold the mmap_sem during
1083
+ * sync compaction, and we do not need to hold the mmap_lock during
9751084 * that. We will recheck the vma after taking it again in write mode.
9761085 */
977
- up_read(&mm->mmap_sem);
1086
+ mmap_read_unlock(mm);
9781087 new_page = khugepaged_alloc_page(hpage, gfp, node);
9791088 if (!new_page) {
9801089 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
9811090 goto out_nolock;
9821091 }
9831092
984
- if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
1093
+ if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
9851094 result = SCAN_CGROUP_CHARGE_FAIL;
9861095 goto out_nolock;
9871096 }
1097
+ count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
9881098
989
- down_read(&mm->mmap_sem);
1099
+ mmap_read_lock(mm);
9901100 result = hugepage_vma_revalidate(mm, address, &vma);
9911101 if (result) {
992
- mem_cgroup_cancel_charge(new_page, memcg, true);
993
- up_read(&mm->mmap_sem);
1102
+ mmap_read_unlock(mm);
9941103 goto out_nolock;
9951104 }
9961105
9971106 pmd = mm_find_pmd(mm, address);
9981107 if (!pmd) {
9991108 result = SCAN_PMD_NULL;
1000
- mem_cgroup_cancel_charge(new_page, memcg, true);
1001
- up_read(&mm->mmap_sem);
1109
+ mmap_read_unlock(mm);
10021110 goto out_nolock;
10031111 }
10041112
10051113 /*
1006
- * __collapse_huge_page_swapin always returns with mmap_sem locked.
1007
- * If it fails, we release mmap_sem and jump out_nolock.
1114
+ * __collapse_huge_page_swapin always returns with mmap_lock locked.
1115
+ * If it fails, we release mmap_lock and jump out_nolock.
10081116 * Continuing to collapse causes inconsistency.
10091117 */
1010
- if (!__collapse_huge_page_swapin(mm, vma, address, pmd, referenced)) {
1011
- mem_cgroup_cancel_charge(new_page, memcg, true);
1012
- up_read(&mm->mmap_sem);
1118
+ if (unmapped && !__collapse_huge_page_swapin(mm, vma, address,
1119
+ pmd, referenced)) {
1120
+ mmap_read_unlock(mm);
10131121 goto out_nolock;
10141122 }
10151123
1016
- up_read(&mm->mmap_sem);
1124
+ mmap_read_unlock(mm);
10171125 /*
10181126 * Prevent all access to pagetables with the exception of
10191127 * gup_fast later handled by the ptep_clear_flush and the VM
10201128 * handled by the anon_vma lock + PG_lock.
10211129 */
1022
- down_write(&mm->mmap_sem);
1130
+ mmap_write_lock(mm);
10231131 result = hugepage_vma_revalidate(mm, address, &vma);
10241132 if (result)
10251133 goto out;
....@@ -1027,27 +1135,33 @@
10271135 if (mm_find_pmd(mm, address) != pmd)
10281136 goto out;
10291137
1138
+ vm_write_begin(vma);
10301139 anon_vma_lock_write(vma->anon_vma);
1140
+
1141
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
1142
+ address, address + HPAGE_PMD_SIZE);
1143
+ mmu_notifier_invalidate_range_start(&range);
10311144
10321145 pte = pte_offset_map(pmd, address);
10331146 pte_ptl = pte_lockptr(mm, pmd);
10341147
1035
- mmun_start = address;
1036
- mmun_end = address + HPAGE_PMD_SIZE;
1037
- mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
10381148 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
10391149 /*
1040
- * After this gup_fast can't run anymore. This also removes
1041
- * any huge TLB entry from the CPU so we won't allow
1042
- * huge and small TLB entries for the same virtual address
1043
- * to avoid the risk of CPU bugs in that area.
1150
+ * This removes any huge TLB entry from the CPU so we won't allow
1151
+ * huge and small TLB entries for the same virtual address to
1152
+ * avoid the risk of CPU bugs in that area.
1153
+ *
1154
+ * Parallel fast GUP is fine since fast GUP will back off when
1155
+ * it detects PMD is changed.
10441156 */
10451157 _pmd = pmdp_collapse_flush(vma, address, pmd);
10461158 spin_unlock(pmd_ptl);
1047
- mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1159
+ mmu_notifier_invalidate_range_end(&range);
1160
+ tlb_remove_table_sync_one();
10481161
10491162 spin_lock(pte_ptl);
1050
- isolated = __collapse_huge_page_isolate(vma, address, pte);
1163
+ isolated = __collapse_huge_page_isolate(vma, address, pte,
1164
+ &compound_pagelist);
10511165 spin_unlock(pte_ptl);
10521166
10531167 if (unlikely(!isolated)) {
....@@ -1062,6 +1176,7 @@
10621176 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
10631177 spin_unlock(pmd_ptl);
10641178 anon_vma_unlock_write(vma->anon_vma);
1179
+ vm_write_end(vma);
10651180 result = SCAN_FAIL;
10661181 goto out;
10671182 }
....@@ -1072,7 +1187,8 @@
10721187 */
10731188 anon_vma_unlock_write(vma->anon_vma);
10741189
1075
- __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
1190
+ __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
1191
+ &compound_pagelist);
10761192 pte_unmap(pte);
10771193 __SetPageUptodate(new_page);
10781194 pgtable = pmd_pgtable(_pmd);
....@@ -1090,24 +1206,25 @@
10901206 spin_lock(pmd_ptl);
10911207 BUG_ON(!pmd_none(*pmd));
10921208 page_add_new_anon_rmap(new_page, vma, address, true);
1093
- mem_cgroup_commit_charge(new_page, memcg, false, true);
1094
- lru_cache_add_active_or_unevictable(new_page, vma);
1209
+ lru_cache_add_inactive_or_unevictable(new_page, vma);
10951210 pgtable_trans_huge_deposit(mm, pmd, pgtable);
10961211 set_pmd_at(mm, address, pmd, _pmd);
10971212 update_mmu_cache_pmd(vma, address, pmd);
10981213 spin_unlock(pmd_ptl);
1214
+ vm_write_end(vma);
10991215
11001216 *hpage = NULL;
11011217
11021218 khugepaged_pages_collapsed++;
11031219 result = SCAN_SUCCEED;
11041220 out_up_write:
1105
- up_write(&mm->mmap_sem);
1221
+ mmap_write_unlock(mm);
11061222 out_nolock:
1223
+ if (!IS_ERR_OR_NULL(*hpage))
1224
+ mem_cgroup_uncharge(*hpage);
11071225 trace_mm_collapse_huge_page(mm, isolated, result);
11081226 return;
11091227 out:
1110
- mem_cgroup_cancel_charge(new_page, memcg, true);
11111228 goto out_up_write;
11121229 }
11131230
....@@ -1118,7 +1235,8 @@
11181235 {
11191236 pmd_t *pmd;
11201237 pte_t *pte, *_pte;
1121
- int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
1238
+ int ret = 0, result = 0, referenced = 0;
1239
+ int none_or_zero = 0, shared = 0;
11221240 struct page *page = NULL;
11231241 unsigned long _address;
11241242 spinlock_t *ptl;
....@@ -1140,6 +1258,15 @@
11401258 pte_t pteval = *_pte;
11411259 if (is_swap_pte(pteval)) {
11421260 if (++unmapped <= khugepaged_max_ptes_swap) {
1261
+ /*
1262
+ * Always be strict with uffd-wp
1263
+ * enabled swap entries. Please see
1264
+ * comment below for pte_uffd_wp().
1265
+ */
1266
+ if (pte_swp_uffd_wp(pteval)) {
1267
+ result = SCAN_PTE_UFFD_WP;
1268
+ goto out_unmap;
1269
+ }
11431270 continue;
11441271 } else {
11451272 result = SCAN_EXCEED_SWAP_PTE;
....@@ -1159,6 +1286,19 @@
11591286 result = SCAN_PTE_NON_PRESENT;
11601287 goto out_unmap;
11611288 }
1289
+ if (pte_uffd_wp(pteval)) {
1290
+ /*
1291
+ * Don't collapse the page if any of the small
1292
+ * PTEs are armed with uffd write protection.
1293
+ * Here we can also mark the new huge pmd as
1294
+ * write protected if any of the small ones is
1295
+ * marked but that could bring uknown
1296
+ * userfault messages that falls outside of
1297
+ * the registered range. So, just be simple.
1298
+ */
1299
+ result = SCAN_PTE_UFFD_WP;
1300
+ goto out_unmap;
1301
+ }
11621302 if (pte_write(pteval))
11631303 writable = true;
11641304
....@@ -1168,11 +1308,13 @@
11681308 goto out_unmap;
11691309 }
11701310
1171
- /* TODO: teach khugepaged to collapse THP mapped with pte */
1172
- if (PageCompound(page)) {
1173
- result = SCAN_PAGE_COMPOUND;
1311
+ if (page_mapcount(page) > 1 &&
1312
+ ++shared > khugepaged_max_ptes_shared) {
1313
+ result = SCAN_EXCEED_SHARED_PTE;
11741314 goto out_unmap;
11751315 }
1316
+
1317
+ page = compound_head(page);
11761318
11771319 /*
11781320 * Record which node the original page is from and save this
....@@ -1200,11 +1342,23 @@
12001342 }
12011343
12021344 /*
1203
- * cannot use mapcount: can't collapse if there's a gup pin.
1204
- * The page must only be referenced by the scanned process
1205
- * and page swap cache.
1345
+ * Check if the page has any GUP (or other external) pins.
1346
+ *
1347
+ * Here the check is racy it may see totmal_mapcount > refcount
1348
+ * in some cases.
1349
+ * For example, one process with one forked child process.
1350
+ * The parent has the PMD split due to MADV_DONTNEED, then
1351
+ * the child is trying unmap the whole PMD, but khugepaged
1352
+ * may be scanning the parent between the child has
1353
+ * PageDoubleMap flag cleared and dec the mapcount. So
1354
+ * khugepaged may see total_mapcount > refcount.
1355
+ *
1356
+ * But such case is ephemeral we could always retry collapse
1357
+ * later. However it may report false positive if the page
1358
+ * has excessive GUP pins (i.e. 512). Anyway the same check
1359
+ * will be done again later the risk seems low.
12061360 */
1207
- if (page_count(page) != 1 + PageSwapCache(page)) {
1361
+ if (!is_refcount_suitable(page)) {
12081362 result = SCAN_PAGE_COUNT;
12091363 goto out_unmap;
12101364 }
....@@ -1213,22 +1367,21 @@
12131367 mmu_notifier_test_young(vma->vm_mm, address))
12141368 referenced++;
12151369 }
1216
- if (writable) {
1217
- if (referenced) {
1218
- result = SCAN_SUCCEED;
1219
- ret = 1;
1220
- } else {
1221
- result = SCAN_LACK_REFERENCED_PAGE;
1222
- }
1223
- } else {
1370
+ if (!writable) {
12241371 result = SCAN_PAGE_RO;
1372
+ } else if (!referenced || (unmapped && referenced < HPAGE_PMD_NR/2)) {
1373
+ result = SCAN_LACK_REFERENCED_PAGE;
1374
+ } else {
1375
+ result = SCAN_SUCCEED;
1376
+ ret = 1;
12251377 }
12261378 out_unmap:
12271379 pte_unmap_unlock(pte, ptl);
12281380 if (ret) {
12291381 node = khugepaged_find_target_node();
1230
- /* collapse_huge_page will return with the mmap_sem released */
1231
- collapse_huge_page(mm, address, hpage, node, referenced);
1382
+ /* collapse_huge_page will return with the mmap_lock released */
1383
+ collapse_huge_page(mm, address, hpage, node,
1384
+ referenced, unmapped);
12321385 }
12331386 out:
12341387 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
....@@ -1240,7 +1393,7 @@
12401393 {
12411394 struct mm_struct *mm = mm_slot->mm;
12421395
1243
- VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1396
+ lockdep_assert_held(&khugepaged_mm_lock);
12441397
12451398 if (khugepaged_test_exit(mm)) {
12461399 /* free mm_slot */
....@@ -1259,7 +1412,186 @@
12591412 }
12601413 }
12611414
1262
-#if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
1415
+#ifdef CONFIG_SHMEM
1416
+/*
1417
+ * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1418
+ * khugepaged should try to collapse the page table.
1419
+ */
1420
+static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
1421
+ unsigned long addr)
1422
+{
1423
+ struct mm_slot *mm_slot;
1424
+
1425
+ VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1426
+
1427
+ spin_lock(&khugepaged_mm_lock);
1428
+ mm_slot = get_mm_slot(mm);
1429
+ if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP))
1430
+ mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
1431
+ spin_unlock(&khugepaged_mm_lock);
1432
+ return 0;
1433
+}
1434
+
1435
+/**
1436
+ * Try to collapse a pte-mapped THP for mm at address haddr.
1437
+ *
1438
+ * This function checks whether all the PTEs in the PMD are pointing to the
1439
+ * right THP. If so, retract the page table so the THP can refault in with
1440
+ * as pmd-mapped.
1441
+ */
1442
+void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
1443
+{
1444
+ unsigned long haddr = addr & HPAGE_PMD_MASK;
1445
+ struct vm_area_struct *vma = find_vma(mm, haddr);
1446
+ struct page *hpage;
1447
+ pte_t *start_pte, *pte;
1448
+ pmd_t *pmd, _pmd;
1449
+ spinlock_t *ptl;
1450
+ int count = 0;
1451
+ int i;
1452
+ struct mmu_notifier_range range;
1453
+
1454
+ if (!vma || !vma->vm_file ||
1455
+ vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE)
1456
+ return;
1457
+
1458
+ /*
1459
+ * This vm_flags may not have VM_HUGEPAGE if the page was not
1460
+ * collapsed by this mm. But we can still collapse if the page is
1461
+ * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
1462
+ * will not fail the vma for missing VM_HUGEPAGE
1463
+ */
1464
+ if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
1465
+ return;
1466
+
1467
+ /*
1468
+ * Symmetry with retract_page_tables(): Exclude MAP_PRIVATE mappings
1469
+ * that got written to. Without this, we'd have to also lock the
1470
+ * anon_vma if one exists.
1471
+ */
1472
+ if (vma->anon_vma)
1473
+ return;
1474
+
1475
+ hpage = find_lock_page(vma->vm_file->f_mapping,
1476
+ linear_page_index(vma, haddr));
1477
+ if (!hpage)
1478
+ return;
1479
+
1480
+ if (!PageHead(hpage))
1481
+ goto drop_hpage;
1482
+
1483
+ pmd = mm_find_pmd(mm, haddr);
1484
+ if (!pmd)
1485
+ goto drop_hpage;
1486
+
1487
+ vm_write_begin(vma);
1488
+
1489
+ /*
1490
+ * We need to lock the mapping so that from here on, only GUP-fast and
1491
+ * hardware page walks can access the parts of the page tables that
1492
+ * we're operating on.
1493
+ */
1494
+ i_mmap_lock_write(vma->vm_file->f_mapping);
1495
+
1496
+ /*
1497
+ * This spinlock should be unnecessary: Nobody else should be accessing
1498
+ * the page tables under spinlock protection here, only
1499
+ * lockless_pages_from_mm() and the hardware page walker can access page
1500
+ * tables while all the high-level locks are held in write mode.
1501
+ */
1502
+ start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
1503
+
1504
+ /* step 1: check all mapped PTEs are to the right huge page */
1505
+ for (i = 0, addr = haddr, pte = start_pte;
1506
+ i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1507
+ struct page *page;
1508
+
1509
+ /* empty pte, skip */
1510
+ if (pte_none(*pte))
1511
+ continue;
1512
+
1513
+ /* page swapped out, abort */
1514
+ if (!pte_present(*pte))
1515
+ goto abort;
1516
+
1517
+ page = vm_normal_page(vma, addr, *pte);
1518
+
1519
+ /*
1520
+ * Note that uprobe, debugger, or MAP_PRIVATE may change the
1521
+ * page table, but the new page will not be a subpage of hpage.
1522
+ */
1523
+ if (hpage + i != page)
1524
+ goto abort;
1525
+ count++;
1526
+ }
1527
+
1528
+ /* step 2: adjust rmap */
1529
+ for (i = 0, addr = haddr, pte = start_pte;
1530
+ i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1531
+ struct page *page;
1532
+
1533
+ if (pte_none(*pte))
1534
+ continue;
1535
+ page = vm_normal_page(vma, addr, *pte);
1536
+ page_remove_rmap(page, false);
1537
+ }
1538
+
1539
+ pte_unmap_unlock(start_pte, ptl);
1540
+
1541
+ /* step 3: set proper refcount and mm_counters. */
1542
+ if (count) {
1543
+ page_ref_sub(hpage, count);
1544
+ add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1545
+ }
1546
+
1547
+ /* step 4: collapse pmd */
1548
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm, haddr,
1549
+ haddr + HPAGE_PMD_SIZE);
1550
+ mmu_notifier_invalidate_range_start(&range);
1551
+ _pmd = pmdp_collapse_flush(vma, haddr, pmd);
1552
+ vm_write_end(vma);
1553
+ mm_dec_nr_ptes(mm);
1554
+ tlb_remove_table_sync_one();
1555
+ mmu_notifier_invalidate_range_end(&range);
1556
+ pte_free(mm, pmd_pgtable(_pmd));
1557
+
1558
+ i_mmap_unlock_write(vma->vm_file->f_mapping);
1559
+
1560
+drop_hpage:
1561
+ unlock_page(hpage);
1562
+ put_page(hpage);
1563
+ return;
1564
+
1565
+abort:
1566
+ pte_unmap_unlock(start_pte, ptl);
1567
+ vm_write_end(vma);
1568
+ i_mmap_unlock_write(vma->vm_file->f_mapping);
1569
+ goto drop_hpage;
1570
+}
1571
+
1572
+static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
1573
+{
1574
+ struct mm_struct *mm = mm_slot->mm;
1575
+ int i;
1576
+
1577
+ if (likely(mm_slot->nr_pte_mapped_thp == 0))
1578
+ return 0;
1579
+
1580
+ if (!mmap_write_trylock(mm))
1581
+ return -EBUSY;
1582
+
1583
+ if (unlikely(khugepaged_test_exit(mm)))
1584
+ goto out;
1585
+
1586
+ for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
1587
+ collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i]);
1588
+
1589
+out:
1590
+ mm_slot->nr_pte_mapped_thp = 0;
1591
+ mmap_write_unlock(mm);
1592
+ return 0;
1593
+}
1594
+
12631595 static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
12641596 {
12651597 struct vm_area_struct *vma;
....@@ -1269,7 +1601,23 @@
12691601
12701602 i_mmap_lock_write(mapping);
12711603 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1272
- /* probably overkill */
1604
+ /*
1605
+ * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1606
+ * got written to. These VMAs are likely not worth investing
1607
+ * mmap_write_lock(mm) as PMD-mapping is likely to be split
1608
+ * later.
1609
+ *
1610
+ * Not that vma->anon_vma check is racy: it can be set up after
1611
+ * the check but before we took mmap_lock by the fault path.
1612
+ * But page lock would prevent establishing any new ptes of the
1613
+ * page, so we are safe.
1614
+ *
1615
+ * An alternative would be drop the check, but check that page
1616
+ * table is clear before calling pmdp_collapse_flush() under
1617
+ * ptl. It has higher chance to recover THP for the VMA, but
1618
+ * has higher cost too. It would also probably require locking
1619
+ * the anon_vma.
1620
+ */
12731621 if (vma->anon_vma)
12741622 continue;
12751623 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
....@@ -1282,57 +1630,71 @@
12821630 if (!pmd)
12831631 continue;
12841632 /*
1285
- * We need exclusive mmap_sem to retract page table.
1286
- * If trylock fails we would end up with pte-mapped THP after
1287
- * re-fault. Not ideal, but it's more important to not disturb
1288
- * the system too much.
1633
+ * We need exclusive mmap_lock to retract page table.
1634
+ *
1635
+ * We use trylock due to lock inversion: we need to acquire
1636
+ * mmap_lock while holding page lock. Fault path does it in
1637
+ * reverse order. Trylock is a way to avoid deadlock.
12891638 */
1290
- if (down_write_trylock(&mm->mmap_sem)) {
1639
+ if (mmap_write_trylock(mm)) {
12911640 if (!khugepaged_test_exit(mm)) {
1292
- spinlock_t *ptl = pmd_lock(mm, pmd);
1641
+ struct mmu_notifier_range range;
1642
+
1643
+ vm_write_begin(vma);
1644
+ mmu_notifier_range_init(&range,
1645
+ MMU_NOTIFY_CLEAR, 0,
1646
+ NULL, mm, addr,
1647
+ addr + HPAGE_PMD_SIZE);
1648
+ mmu_notifier_invalidate_range_start(&range);
12931649 /* assume page table is clear */
12941650 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1295
- spin_unlock(ptl);
1651
+ vm_write_end(vma);
12961652 mm_dec_nr_ptes(mm);
1653
+ tlb_remove_table_sync_one();
12971654 pte_free(mm, pmd_pgtable(_pmd));
1655
+ mmu_notifier_invalidate_range_end(&range);
12981656 }
1299
- up_write(&mm->mmap_sem);
1657
+ mmap_write_unlock(mm);
1658
+ } else {
1659
+ /* Try again later */
1660
+ khugepaged_add_pte_mapped_thp(mm, addr);
13001661 }
13011662 }
13021663 i_mmap_unlock_write(mapping);
13031664 }
13041665
13051666 /**
1306
- * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
1667
+ * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
13071668 *
13081669 * Basic scheme is simple, details are more complex:
13091670 * - allocate and lock a new huge page;
1310
- * - scan over radix tree replacing old pages the new one
1311
- * + swap in pages if necessary;
1671
+ * - scan page cache replacing old pages with the new one
1672
+ * + swap/gup in pages if necessary;
13121673 * + fill in gaps;
1313
- * + keep old pages around in case if rollback is required;
1314
- * - if replacing succeed:
1674
+ * + keep old pages around in case rollback is required;
1675
+ * - if replacing succeeds:
13151676 * + copy data over;
13161677 * + free old pages;
13171678 * + unlock huge page;
13181679 * - if replacing failed;
13191680 * + put all pages back and unfreeze them;
1320
- * + restore gaps in the radix-tree;
1681
+ * + restore gaps in the page cache;
13211682 * + unlock and free huge page;
13221683 */
1323
-static void collapse_shmem(struct mm_struct *mm,
1324
- struct address_space *mapping, pgoff_t start,
1684
+static void collapse_file(struct mm_struct *mm,
1685
+ struct file *file, pgoff_t start,
13251686 struct page **hpage, int node)
13261687 {
1688
+ struct address_space *mapping = file->f_mapping;
13271689 gfp_t gfp;
1328
- struct page *page, *new_page, *tmp;
1329
- struct mem_cgroup *memcg;
1690
+ struct page *new_page;
13301691 pgoff_t index, end = start + HPAGE_PMD_NR;
13311692 LIST_HEAD(pagelist);
1332
- struct radix_tree_iter iter;
1333
- void **slot;
1693
+ XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
13341694 int nr_none = 0, result = SCAN_SUCCEED;
1695
+ bool is_shmem = shmem_file(file);
13351696
1697
+ VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
13361698 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
13371699
13381700 /* Only allocate from the target node */
....@@ -1344,13 +1706,28 @@
13441706 goto out;
13451707 }
13461708
1347
- if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
1709
+ if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
13481710 result = SCAN_CGROUP_CHARGE_FAIL;
13491711 goto out;
13501712 }
1713
+ count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
1714
+
1715
+ /* This will be less messy when we use multi-index entries */
1716
+ do {
1717
+ xas_lock_irq(&xas);
1718
+ xas_create_range(&xas);
1719
+ if (!xas_error(&xas))
1720
+ break;
1721
+ xas_unlock_irq(&xas);
1722
+ if (!xas_nomem(&xas, GFP_KERNEL)) {
1723
+ result = SCAN_FAIL;
1724
+ goto out;
1725
+ }
1726
+ } while (1);
13511727
13521728 __SetPageLocked(new_page);
1353
- __SetPageSwapBacked(new_page);
1729
+ if (is_shmem)
1730
+ __SetPageSwapBacked(new_page);
13541731 new_page->index = start;
13551732 new_page->mapping = mapping;
13561733
....@@ -1360,55 +1737,91 @@
13601737 * be able to map it or use it in another way until we unlock it.
13611738 */
13621739
1363
- index = start;
1364
- xa_lock_irq(&mapping->i_pages);
1365
- radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
1366
- int n = min(iter.index, end) - index;
1740
+ xas_set(&xas, start);
1741
+ for (index = start; index < end; index++) {
1742
+ struct page *page = xas_next(&xas);
13671743
1368
- /*
1369
- * Stop if extent has been hole-punched, and is now completely
1370
- * empty (the more obvious i_size_read() check would take an
1371
- * irq-unsafe seqlock on 32-bit).
1372
- */
1373
- if (n >= HPAGE_PMD_NR) {
1374
- result = SCAN_TRUNCATED;
1375
- goto tree_locked;
1376
- }
1377
-
1378
- /*
1379
- * Handle holes in the radix tree: charge it from shmem and
1380
- * insert relevant subpage of new_page into the radix-tree.
1381
- */
1382
- if (n && !shmem_charge(mapping->host, n)) {
1383
- result = SCAN_FAIL;
1384
- goto tree_locked;
1385
- }
1386
- for (; index < min(iter.index, end); index++) {
1387
- radix_tree_insert(&mapping->i_pages, index,
1388
- new_page + (index % HPAGE_PMD_NR));
1389
- }
1390
- nr_none += n;
1391
-
1392
- /* We are done. */
1393
- if (index >= end)
1394
- break;
1395
-
1396
- page = radix_tree_deref_slot_protected(slot,
1397
- &mapping->i_pages.xa_lock);
1398
- if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) {
1399
- xa_unlock_irq(&mapping->i_pages);
1400
- /* swap in or instantiate fallocated page */
1401
- if (shmem_getpage(mapping->host, index, &page,
1402
- SGP_NOHUGE)) {
1403
- result = SCAN_FAIL;
1404
- goto tree_unlocked;
1744
+ VM_BUG_ON(index != xas.xa_index);
1745
+ if (is_shmem) {
1746
+ if (!page) {
1747
+ /*
1748
+ * Stop if extent has been truncated or
1749
+ * hole-punched, and is now completely
1750
+ * empty.
1751
+ */
1752
+ if (index == start) {
1753
+ if (!xas_next_entry(&xas, end - 1)) {
1754
+ result = SCAN_TRUNCATED;
1755
+ goto xa_locked;
1756
+ }
1757
+ xas_set(&xas, index);
1758
+ }
1759
+ if (!shmem_charge(mapping->host, 1)) {
1760
+ result = SCAN_FAIL;
1761
+ goto xa_locked;
1762
+ }
1763
+ xas_store(&xas, new_page);
1764
+ nr_none++;
1765
+ continue;
14051766 }
1406
- } else if (trylock_page(page)) {
1407
- get_page(page);
1408
- xa_unlock_irq(&mapping->i_pages);
1409
- } else {
1410
- result = SCAN_PAGE_LOCK;
1411
- goto tree_locked;
1767
+
1768
+ if (xa_is_value(page) || !PageUptodate(page)) {
1769
+ xas_unlock_irq(&xas);
1770
+ /* swap in or instantiate fallocated page */
1771
+ if (shmem_getpage(mapping->host, index, &page,
1772
+ SGP_NOHUGE)) {
1773
+ result = SCAN_FAIL;
1774
+ goto xa_unlocked;
1775
+ }
1776
+ } else if (trylock_page(page)) {
1777
+ get_page(page);
1778
+ xas_unlock_irq(&xas);
1779
+ } else {
1780
+ result = SCAN_PAGE_LOCK;
1781
+ goto xa_locked;
1782
+ }
1783
+ } else { /* !is_shmem */
1784
+ if (!page || xa_is_value(page)) {
1785
+ xas_unlock_irq(&xas);
1786
+ page_cache_sync_readahead(mapping, &file->f_ra,
1787
+ file, index,
1788
+ end - index);
1789
+ /* drain pagevecs to help isolate_lru_page() */
1790
+ lru_add_drain();
1791
+ page = find_lock_page(mapping, index);
1792
+ if (unlikely(page == NULL)) {
1793
+ result = SCAN_FAIL;
1794
+ goto xa_unlocked;
1795
+ }
1796
+ } else if (PageDirty(page)) {
1797
+ /*
1798
+ * khugepaged only works on read-only fd,
1799
+ * so this page is dirty because it hasn't
1800
+ * been flushed since first write. There
1801
+ * won't be new dirty pages.
1802
+ *
1803
+ * Trigger async flush here and hope the
1804
+ * writeback is done when khugepaged
1805
+ * revisits this page.
1806
+ *
1807
+ * This is a one-off situation. We are not
1808
+ * forcing writeback in loop.
1809
+ */
1810
+ xas_unlock_irq(&xas);
1811
+ filemap_flush(mapping);
1812
+ result = SCAN_FAIL;
1813
+ goto xa_unlocked;
1814
+ } else if (PageWriteback(page)) {
1815
+ xas_unlock_irq(&xas);
1816
+ result = SCAN_FAIL;
1817
+ goto xa_unlocked;
1818
+ } else if (trylock_page(page)) {
1819
+ get_page(page);
1820
+ xas_unlock_irq(&xas);
1821
+ } else {
1822
+ result = SCAN_PAGE_LOCK;
1823
+ goto xa_locked;
1824
+ }
14121825 }
14131826
14141827 /*
....@@ -1416,7 +1829,12 @@
14161829 * without racing with truncate.
14171830 */
14181831 VM_BUG_ON_PAGE(!PageLocked(page), page);
1419
- VM_BUG_ON_PAGE(!PageUptodate(page), page);
1832
+
1833
+ /* make sure the page is up to date */
1834
+ if (unlikely(!PageUptodate(page))) {
1835
+ result = SCAN_FAIL;
1836
+ goto out_unlock;
1837
+ }
14201838
14211839 /*
14221840 * If file was truncated then extended, or hole-punched, before
....@@ -1432,30 +1850,47 @@
14321850 goto out_unlock;
14331851 }
14341852
1853
+ if (!is_shmem && (PageDirty(page) ||
1854
+ PageWriteback(page))) {
1855
+ /*
1856
+ * khugepaged only works on read-only fd, so this
1857
+ * page is dirty because it hasn't been flushed
1858
+ * since first write.
1859
+ */
1860
+ result = SCAN_FAIL;
1861
+ goto out_unlock;
1862
+ }
1863
+
14351864 if (isolate_lru_page(page)) {
14361865 result = SCAN_DEL_PAGE_LRU;
1866
+ goto out_unlock;
1867
+ }
1868
+
1869
+ if (page_has_private(page) &&
1870
+ !try_to_release_page(page, GFP_KERNEL)) {
1871
+ result = SCAN_PAGE_HAS_PRIVATE;
1872
+ putback_lru_page(page);
14371873 goto out_unlock;
14381874 }
14391875
14401876 if (page_mapped(page))
14411877 unmap_mapping_pages(mapping, index, 1, false);
14421878
1443
- xa_lock_irq(&mapping->i_pages);
1879
+ xas_lock_irq(&xas);
1880
+ xas_set(&xas, index);
14441881
1445
- slot = radix_tree_lookup_slot(&mapping->i_pages, index);
1446
- VM_BUG_ON_PAGE(page != radix_tree_deref_slot_protected(slot,
1447
- &mapping->i_pages.xa_lock), page);
1882
+ VM_BUG_ON_PAGE(page != xas_load(&xas), page);
14481883 VM_BUG_ON_PAGE(page_mapped(page), page);
14491884
14501885 /*
14511886 * The page is expected to have page_count() == 3:
14521887 * - we hold a pin on it;
1453
- * - one reference from radix tree;
1888
+ * - one reference from page cache;
14541889 * - one from isolate_lru_page;
14551890 */
14561891 if (!page_ref_freeze(page, 3)) {
14571892 result = SCAN_PAGE_COUNT;
1458
- xa_unlock_irq(&mapping->i_pages);
1893
+ xas_unlock_irq(&xas);
14591894 putback_lru_page(page);
14601895 goto out_unlock;
14611896 }
....@@ -1467,58 +1902,50 @@
14671902 list_add_tail(&page->lru, &pagelist);
14681903
14691904 /* Finally, replace with the new page. */
1470
- radix_tree_replace_slot(&mapping->i_pages, slot,
1471
- new_page + (index % HPAGE_PMD_NR));
1472
-
1473
- slot = radix_tree_iter_resume(slot, &iter);
1474
- index++;
1905
+ xas_store(&xas, new_page);
14751906 continue;
14761907 out_unlock:
14771908 unlock_page(page);
14781909 put_page(page);
1479
- goto tree_unlocked;
1910
+ goto xa_unlocked;
14801911 }
14811912
1482
- /*
1483
- * Handle hole in radix tree at the end of the range.
1484
- * This code only triggers if there's nothing in radix tree
1485
- * beyond 'end'.
1486
- */
1487
- if (index < end) {
1488
- int n = end - index;
1489
-
1490
- /* Stop if extent has been truncated, and is now empty */
1491
- if (n >= HPAGE_PMD_NR) {
1492
- result = SCAN_TRUNCATED;
1493
- goto tree_locked;
1494
- }
1495
- if (!shmem_charge(mapping->host, n)) {
1913
+ if (is_shmem)
1914
+ __inc_node_page_state(new_page, NR_SHMEM_THPS);
1915
+ else {
1916
+ __inc_node_page_state(new_page, NR_FILE_THPS);
1917
+ filemap_nr_thps_inc(mapping);
1918
+ /*
1919
+ * Paired with smp_mb() in do_dentry_open() to ensure
1920
+ * i_writecount is up to date and the update to nr_thps is
1921
+ * visible. Ensures the page cache will be truncated if the
1922
+ * file is opened writable.
1923
+ */
1924
+ smp_mb();
1925
+ if (inode_is_open_for_write(mapping->host)) {
14961926 result = SCAN_FAIL;
1497
- goto tree_locked;
1927
+ __dec_node_page_state(new_page, NR_FILE_THPS);
1928
+ filemap_nr_thps_dec(mapping);
1929
+ goto xa_locked;
14981930 }
1499
- for (; index < end; index++) {
1500
- radix_tree_insert(&mapping->i_pages, index,
1501
- new_page + (index % HPAGE_PMD_NR));
1502
- }
1503
- nr_none += n;
15041931 }
15051932
1506
- __inc_node_page_state(new_page, NR_SHMEM_THPS);
15071933 if (nr_none) {
1508
- struct zone *zone = page_zone(new_page);
1509
-
1510
- __mod_node_page_state(zone->zone_pgdat, NR_FILE_PAGES, nr_none);
1511
- __mod_node_page_state(zone->zone_pgdat, NR_SHMEM, nr_none);
1934
+ __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
1935
+ if (is_shmem)
1936
+ __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
15121937 }
15131938
1514
-tree_locked:
1515
- xa_unlock_irq(&mapping->i_pages);
1516
-tree_unlocked:
1939
+xa_locked:
1940
+ xas_unlock_irq(&xas);
1941
+xa_unlocked:
15171942
15181943 if (result == SCAN_SUCCEED) {
1944
+ struct page *page, *tmp;
1945
+
15191946 /*
1520
- * Replacing old pages with new one has succeed, now we need to
1521
- * copy the content and free old pages.
1947
+ * Replacing old pages with new one has succeeded, now we
1948
+ * need to copy the content and free the old pages.
15221949 */
15231950 index = start;
15241951 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
....@@ -1544,9 +1971,9 @@
15441971
15451972 SetPageUptodate(new_page);
15461973 page_ref_add(new_page, HPAGE_PMD_NR - 1);
1547
- set_page_dirty(new_page);
1548
- mem_cgroup_commit_charge(new_page, memcg, false, true);
1549
- lru_cache_add_anon(new_page);
1974
+ if (is_shmem)
1975
+ set_page_dirty(new_page);
1976
+ lru_cache_add(new_page);
15501977
15511978 /*
15521979 * Remove pte page tables, so we can re-fault the page as huge.
....@@ -1556,57 +1983,60 @@
15561983
15571984 khugepaged_pages_collapsed++;
15581985 } else {
1559
- /* Something went wrong: rollback changes to the radix-tree */
1560
- xa_lock_irq(&mapping->i_pages);
1561
- mapping->nrpages -= nr_none;
1562
- shmem_uncharge(mapping->host, nr_none);
1986
+ struct page *page;
15631987
1564
- radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
1565
- if (iter.index >= end)
1566
- break;
1988
+ /* Something went wrong: roll back page cache changes */
1989
+ xas_lock_irq(&xas);
1990
+ mapping->nrpages -= nr_none;
1991
+
1992
+ if (is_shmem)
1993
+ shmem_uncharge(mapping->host, nr_none);
1994
+
1995
+ xas_set(&xas, start);
1996
+ xas_for_each(&xas, page, end - 1) {
15671997 page = list_first_entry_or_null(&pagelist,
15681998 struct page, lru);
1569
- if (!page || iter.index < page->index) {
1999
+ if (!page || xas.xa_index < page->index) {
15702000 if (!nr_none)
15712001 break;
15722002 nr_none--;
15732003 /* Put holes back where they were */
1574
- radix_tree_delete(&mapping->i_pages, iter.index);
2004
+ xas_store(&xas, NULL);
15752005 continue;
15762006 }
15772007
1578
- VM_BUG_ON_PAGE(page->index != iter.index, page);
2008
+ VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
15792009
15802010 /* Unfreeze the page. */
15812011 list_del(&page->lru);
15822012 page_ref_unfreeze(page, 2);
1583
- radix_tree_replace_slot(&mapping->i_pages, slot, page);
1584
- slot = radix_tree_iter_resume(slot, &iter);
1585
- xa_unlock_irq(&mapping->i_pages);
2013
+ xas_store(&xas, page);
2014
+ xas_pause(&xas);
2015
+ xas_unlock_irq(&xas);
15862016 unlock_page(page);
15872017 putback_lru_page(page);
1588
- xa_lock_irq(&mapping->i_pages);
2018
+ xas_lock_irq(&xas);
15892019 }
15902020 VM_BUG_ON(nr_none);
1591
- xa_unlock_irq(&mapping->i_pages);
2021
+ xas_unlock_irq(&xas);
15922022
1593
- mem_cgroup_cancel_charge(new_page, memcg, true);
15942023 new_page->mapping = NULL;
15952024 }
15962025
15972026 unlock_page(new_page);
15982027 out:
15992028 VM_BUG_ON(!list_empty(&pagelist));
2029
+ if (!IS_ERR_OR_NULL(*hpage))
2030
+ mem_cgroup_uncharge(*hpage);
16002031 /* TODO: tracepoints */
16012032 }
16022033
1603
-static void khugepaged_scan_shmem(struct mm_struct *mm,
1604
- struct address_space *mapping,
1605
- pgoff_t start, struct page **hpage)
2034
+static void khugepaged_scan_file(struct mm_struct *mm,
2035
+ struct file *file, pgoff_t start, struct page **hpage)
16062036 {
16072037 struct page *page = NULL;
1608
- struct radix_tree_iter iter;
1609
- void **slot;
2038
+ struct address_space *mapping = file->f_mapping;
2039
+ XA_STATE(xas, &mapping->i_pages, start);
16102040 int present, swap;
16112041 int node = NUMA_NO_NODE;
16122042 int result = SCAN_SUCCEED;
....@@ -1615,17 +2045,11 @@
16152045 swap = 0;
16162046 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
16172047 rcu_read_lock();
1618
- radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
1619
- if (iter.index >= start + HPAGE_PMD_NR)
1620
- break;
1621
-
1622
- page = radix_tree_deref_slot(slot);
1623
- if (radix_tree_deref_retry(page)) {
1624
- slot = radix_tree_iter_retry(&iter);
2048
+ xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
2049
+ if (xas_retry(&xas, page))
16252050 continue;
1626
- }
16272051
1628
- if (radix_tree_exception(page)) {
2052
+ if (xa_is_value(page)) {
16292053 if (++swap > khugepaged_max_ptes_swap) {
16302054 result = SCAN_EXCEED_SWAP_PTE;
16312055 break;
....@@ -1650,7 +2074,8 @@
16502074 break;
16512075 }
16522076
1653
- if (page_count(page) != 1 + page_mapcount(page)) {
2077
+ if (page_count(page) !=
2078
+ 1 + page_mapcount(page) + page_has_private(page)) {
16542079 result = SCAN_PAGE_COUNT;
16552080 break;
16562081 }
....@@ -1664,7 +2089,7 @@
16642089 present++;
16652090
16662091 if (need_resched()) {
1667
- slot = radix_tree_iter_resume(slot, &iter);
2092
+ xas_pause(&xas);
16682093 cond_resched_rcu();
16692094 }
16702095 }
....@@ -1675,18 +2100,22 @@
16752100 result = SCAN_EXCEED_NONE_PTE;
16762101 } else {
16772102 node = khugepaged_find_target_node();
1678
- collapse_shmem(mm, mapping, start, hpage, node);
2103
+ collapse_file(mm, file, start, hpage, node);
16792104 }
16802105 }
16812106
16822107 /* TODO: tracepoints */
16832108 }
16842109 #else
1685
-static void khugepaged_scan_shmem(struct mm_struct *mm,
1686
- struct address_space *mapping,
1687
- pgoff_t start, struct page **hpage)
2110
+static void khugepaged_scan_file(struct mm_struct *mm,
2111
+ struct file *file, pgoff_t start, struct page **hpage)
16882112 {
16892113 BUILD_BUG();
2114
+}
2115
+
2116
+static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
2117
+{
2118
+ return 0;
16902119 }
16912120 #endif
16922121
....@@ -1701,7 +2130,7 @@
17012130 int progress = 0;
17022131
17032132 VM_BUG_ON(!pages);
1704
- VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2133
+ lockdep_assert_held(&khugepaged_mm_lock);
17052134
17062135 if (khugepaged_scan.mm_slot)
17072136 mm_slot = khugepaged_scan.mm_slot;
....@@ -1712,6 +2141,7 @@
17122141 khugepaged_scan.mm_slot = mm_slot;
17132142 }
17142143 spin_unlock(&khugepaged_mm_lock);
2144
+ khugepaged_collapse_pte_mapped_thps(mm_slot);
17152145
17162146 mm = mm_slot->mm;
17172147 /*
....@@ -1719,8 +2149,8 @@
17192149 * the next mm on the list.
17202150 */
17212151 vma = NULL;
1722
- if (unlikely(!down_read_trylock(&mm->mmap_sem)))
1723
- goto breakouterloop_mmap_sem;
2152
+ if (unlikely(!mmap_read_trylock(mm)))
2153
+ goto breakouterloop_mmap_lock;
17242154 if (likely(!khugepaged_test_exit(mm)))
17252155 vma = find_vma(mm, khugepaged_scan.address);
17262156
....@@ -1747,6 +2177,8 @@
17472177 if (khugepaged_scan.address < hstart)
17482178 khugepaged_scan.address = hstart;
17492179 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2180
+ if (shmem_file(vma->vm_file) && !shmem_huge_enabled(vma))
2181
+ goto skip;
17502182
17512183 while (khugepaged_scan.address < hend) {
17522184 int ret;
....@@ -1757,17 +2189,14 @@
17572189 VM_BUG_ON(khugepaged_scan.address < hstart ||
17582190 khugepaged_scan.address + HPAGE_PMD_SIZE >
17592191 hend);
1760
- if (shmem_file(vma->vm_file)) {
1761
- struct file *file;
2192
+ if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
2193
+ struct file *file = get_file(vma->vm_file);
17622194 pgoff_t pgoff = linear_page_index(vma,
17632195 khugepaged_scan.address);
1764
- if (!shmem_huge_enabled(vma))
1765
- goto skip;
1766
- file = get_file(vma->vm_file);
1767
- up_read(&mm->mmap_sem);
2196
+
2197
+ mmap_read_unlock(mm);
17682198 ret = 1;
1769
- khugepaged_scan_shmem(mm, file->f_mapping,
1770
- pgoff, hpage);
2199
+ khugepaged_scan_file(mm, file, pgoff, hpage);
17712200 fput(file);
17722201 } else {
17732202 ret = khugepaged_scan_pmd(mm, vma,
....@@ -1778,15 +2207,15 @@
17782207 khugepaged_scan.address += HPAGE_PMD_SIZE;
17792208 progress += HPAGE_PMD_NR;
17802209 if (ret)
1781
- /* we released mmap_sem so break loop */
1782
- goto breakouterloop_mmap_sem;
2210
+ /* we released mmap_lock so break loop */
2211
+ goto breakouterloop_mmap_lock;
17832212 if (progress >= pages)
17842213 goto breakouterloop;
17852214 }
17862215 }
17872216 breakouterloop:
1788
- up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
1789
-breakouterloop_mmap_sem:
2217
+ mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */
2218
+breakouterloop_mmap_lock:
17902219
17912220 spin_lock(&khugepaged_mm_lock);
17922221 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
....@@ -1837,6 +2266,8 @@
18372266
18382267 barrier(); /* write khugepaged_pages_to_scan to local stack */
18392268
2269
+ lru_add_drain_all();
2270
+
18402271 while (progress < pages) {
18412272 if (!khugepaged_prealloc_page(&hpage, &wait))
18422273 break;