hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
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;
....@@ -546,19 +623,37 @@
546623 result = SCAN_PTE_NON_PRESENT;
547624 goto out;
548625 }
626
+ if (pte_uffd_wp(pteval)) {
627
+ result = SCAN_PTE_UFFD_WP;
628
+ goto out;
629
+ }
549630 page = vm_normal_page(vma, address, pteval);
550631 if (unlikely(!page)) {
551632 result = SCAN_PAGE_NULL;
552633 goto out;
553634 }
554635
555
- /* TODO: teach khugepaged to collapse THP mapped with pte */
556
- if (PageCompound(page)) {
557
- result = SCAN_PAGE_COMPOUND;
636
+ VM_BUG_ON_PAGE(!PageAnon(page), page);
637
+
638
+ if (page_mapcount(page) > 1 &&
639
+ ++shared > khugepaged_max_ptes_shared) {
640
+ result = SCAN_EXCEED_SHARED_PTE;
558641 goto out;
559642 }
560643
561
- VM_BUG_ON_PAGE(!PageAnon(page), page);
644
+ if (PageCompound(page)) {
645
+ struct page *p;
646
+ page = compound_head(page);
647
+
648
+ /*
649
+ * Check if we have dealt with the compound page
650
+ * already
651
+ */
652
+ list_for_each_entry(p, compound_pagelist, lru) {
653
+ if (page == p)
654
+ goto next;
655
+ }
656
+ }
562657
563658 /*
564659 * We can do it before isolate_lru_page because the
....@@ -572,28 +667,30 @@
572667 }
573668
574669 /*
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.
670
+ * Check if the page has any GUP (or other external) pins.
671
+ *
672
+ * The page table that maps the page has been already unlinked
673
+ * from the page table tree and this process cannot get
674
+ * an additinal pin on the page.
675
+ *
676
+ * New pins can come later if the page is shared across fork,
677
+ * but not from this process. The other process cannot write to
678
+ * the page, only trigger CoW.
578679 */
579
- if (page_count(page) != 1 + PageSwapCache(page)) {
680
+ if (!is_refcount_suitable(page)) {
580681 unlock_page(page);
581682 result = SCAN_PAGE_COUNT;
582683 goto out;
583684 }
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
- }
685
+ if (!pte_write(pteval) && PageSwapCache(page) &&
686
+ !reuse_swap_page(page, NULL)) {
593687 /*
594
- * Page is not in the swap cache. It can be collapsed
595
- * into a THP.
688
+ * Page is in the swap cache and cannot be re-used.
689
+ * It cannot be collapsed into a THP.
596690 */
691
+ unlock_page(page);
692
+ result = SCAN_SWAP_CACHE_PAGE;
693
+ goto out;
597694 }
598695
599696 /*
....@@ -605,16 +702,23 @@
605702 result = SCAN_DEL_PAGE_LRU;
606703 goto out;
607704 }
608
- inc_node_page_state(page,
609
- NR_ISOLATED_ANON + page_is_file_cache(page));
705
+ mod_node_page_state(page_pgdat(page),
706
+ NR_ISOLATED_ANON + page_is_file_lru(page),
707
+ compound_nr(page));
610708 VM_BUG_ON_PAGE(!PageLocked(page), page);
611709 VM_BUG_ON_PAGE(PageLRU(page), page);
612710
711
+ if (PageCompound(page))
712
+ list_add_tail(&page->lru, compound_pagelist);
713
+next:
613714 /* There should be enough young pte to collapse the page */
614715 if (pte_young(pteval) ||
615716 page_is_young(page) || PageReferenced(page) ||
616717 mmu_notifier_test_young(vma->vm_mm, address))
617718 referenced++;
719
+
720
+ if (pte_write(pteval))
721
+ writable = true;
618722 }
619723
620724 if (unlikely(!writable)) {
....@@ -628,7 +732,7 @@
628732 return 1;
629733 }
630734 out:
631
- release_pte_pages(pte, _pte);
735
+ release_pte_pages(pte, _pte, compound_pagelist);
632736 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
633737 referenced, writable, result);
634738 return 0;
....@@ -637,13 +741,14 @@
637741 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
638742 struct vm_area_struct *vma,
639743 unsigned long address,
640
- spinlock_t *ptl)
744
+ spinlock_t *ptl,
745
+ struct list_head *compound_pagelist)
641746 {
747
+ struct page *src_page, *tmp;
642748 pte_t *_pte;
643749 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
644750 _pte++, page++, address += PAGE_SIZE) {
645751 pte_t pteval = *_pte;
646
- struct page *src_page;
647752
648753 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
649754 clear_user_highpage(page, address);
....@@ -663,8 +768,8 @@
663768 } else {
664769 src_page = pte_page(pteval);
665770 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);
771
+ if (!PageCompound(src_page))
772
+ release_pte_page(src_page);
668773 /*
669774 * ptl mostly unnecessary, but preempt has to
670775 * be disabled to update the per-cpu stats
....@@ -680,6 +785,11 @@
680785 spin_unlock(ptl);
681786 free_page_and_swap_cache(src_page);
682787 }
788
+ }
789
+
790
+ list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) {
791
+ list_del(&src_page->lru);
792
+ release_pte_page(src_page);
683793 }
684794 }
685795
....@@ -713,7 +823,7 @@
713823 for (i = 0; i < MAX_NUMNODES; i++) {
714824 if (!khugepaged_node_load[i])
715825 continue;
716
- if (node_distance(nid, i) > RECLAIM_DISTANCE)
826
+ if (node_distance(nid, i) > node_reclaim_distance)
717827 return true;
718828 }
719829 return false;
....@@ -854,8 +964,8 @@
854964 #endif
855965
856966 /*
857
- * If mmap_sem temporarily dropped, revalidate vma
858
- * before taking mmap_sem.
967
+ * If mmap_lock temporarily dropped, revalidate vma
968
+ * before taking mmap_lock.
859969 * Return 0 if succeeds, otherwise return none-zero
860970 * value (scan code).
861971 */
....@@ -879,6 +989,9 @@
879989 return SCAN_ADDRESS_RANGE;
880990 if (!hugepage_vma_check(vma, vma->vm_flags))
881991 return SCAN_VMA_CHECK;
992
+ /* Anon VMA expected */
993
+ if (!vma->anon_vma || vma->vm_ops)
994
+ return SCAN_VMA_CHECK;
882995 return 0;
883996 }
884997
....@@ -887,48 +1000,48 @@
8871000 * Only done if khugepaged_scan_pmd believes it is worthwhile.
8881001 *
8891002 * Called and returns without pte mapped or spinlocks held,
890
- * but with mmap_sem held to protect against vma changes.
1003
+ * but with mmap_lock held to protect against vma changes.
8911004 */
8921005
8931006 static bool __collapse_huge_page_swapin(struct mm_struct *mm,
8941007 struct vm_area_struct *vma,
895
- unsigned long address, pmd_t *pmd,
1008
+ unsigned long haddr, pmd_t *pmd,
8961009 int referenced)
8971010 {
8981011 int swapped_in = 0;
8991012 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
- };
1013
+ unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
9071014
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) {
1015
+ for (address = haddr; address < end; address += PAGE_SIZE) {
1016
+ struct vm_fault vmf = {
1017
+ .vma = vma,
1018
+ .address = address,
1019
+ .pgoff = linear_page_index(vma, haddr),
1020
+ .flags = FAULT_FLAG_ALLOW_RETRY,
1021
+ .pmd = pmd,
1022
+ .vma_flags = vma->vm_flags,
1023
+ .vma_page_prot = vma->vm_page_prot,
1024
+ };
1025
+
1026
+ vmf.pte = pte_offset_map(pmd, address);
9161027 vmf.orig_pte = *vmf.pte;
917
- if (!is_swap_pte(vmf.orig_pte))
1028
+ if (!is_swap_pte(vmf.orig_pte)) {
1029
+ pte_unmap(vmf.pte);
9181030 continue;
1031
+ }
9191032 swapped_in++;
9201033 ret = do_swap_page(&vmf);
9211034
922
- /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
1035
+ /* do_swap_page returns VM_FAULT_RETRY with released mmap_lock */
9231036 if (ret & VM_FAULT_RETRY) {
924
- down_read(&mm->mmap_sem);
925
- if (hugepage_vma_revalidate(mm, address, &vmf.vma)) {
1037
+ mmap_read_lock(mm);
1038
+ if (hugepage_vma_revalidate(mm, haddr, &vma)) {
9261039 /* vma is no longer available, don't continue to swapin */
9271040 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9281041 return false;
9291042 }
9301043 /* check if the pmd is still valid */
931
- if (mm_find_pmd(mm, address) != pmd) {
1044
+ if (mm_find_pmd(mm, haddr) != pmd) {
9321045 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9331046 return false;
9341047 }
....@@ -937,11 +1050,12 @@
9371050 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
9381051 return false;
9391052 }
940
- /* pte is unmapped now, we need to map it */
941
- vmf.pte = pte_offset_map(pmd, vmf.address);
9421053 }
943
- vmf.pte--;
944
- pte_unmap(vmf.pte);
1054
+
1055
+ /* Drain LRU add pagevec to remove extra pin on the swapped in pages */
1056
+ if (swapped_in)
1057
+ lru_add_drain();
1058
+
9451059 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
9461060 return true;
9471061 }
....@@ -949,18 +1063,17 @@
9491063 static void collapse_huge_page(struct mm_struct *mm,
9501064 unsigned long address,
9511065 struct page **hpage,
952
- int node, int referenced)
1066
+ int node, int referenced, int unmapped)
9531067 {
1068
+ LIST_HEAD(compound_pagelist);
9541069 pmd_t *pmd, _pmd;
9551070 pte_t *pte;
9561071 pgtable_t pgtable;
9571072 struct page *new_page;
9581073 spinlock_t *pmd_ptl, *pte_ptl;
9591074 int isolated = 0, result = 0;
960
- struct mem_cgroup *memcg;
9611075 struct vm_area_struct *vma;
962
- unsigned long mmun_start; /* For mmu_notifiers */
963
- unsigned long mmun_end; /* For mmu_notifiers */
1076
+ struct mmu_notifier_range range;
9641077 gfp_t gfp;
9651078
9661079 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
....@@ -969,57 +1082,56 @@
9691082 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
9701083
9711084 /*
972
- * Before allocating the hugepage, release the mmap_sem read lock.
1085
+ * Before allocating the hugepage, release the mmap_lock read lock.
9731086 * 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
1087
+ * sync compaction, and we do not need to hold the mmap_lock during
9751088 * that. We will recheck the vma after taking it again in write mode.
9761089 */
977
- up_read(&mm->mmap_sem);
1090
+ mmap_read_unlock(mm);
9781091 new_page = khugepaged_alloc_page(hpage, gfp, node);
9791092 if (!new_page) {
9801093 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
9811094 goto out_nolock;
9821095 }
9831096
984
- if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
1097
+ if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
9851098 result = SCAN_CGROUP_CHARGE_FAIL;
9861099 goto out_nolock;
9871100 }
1101
+ count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
9881102
989
- down_read(&mm->mmap_sem);
1103
+ mmap_read_lock(mm);
9901104 result = hugepage_vma_revalidate(mm, address, &vma);
9911105 if (result) {
992
- mem_cgroup_cancel_charge(new_page, memcg, true);
993
- up_read(&mm->mmap_sem);
1106
+ mmap_read_unlock(mm);
9941107 goto out_nolock;
9951108 }
9961109
9971110 pmd = mm_find_pmd(mm, address);
9981111 if (!pmd) {
9991112 result = SCAN_PMD_NULL;
1000
- mem_cgroup_cancel_charge(new_page, memcg, true);
1001
- up_read(&mm->mmap_sem);
1113
+ mmap_read_unlock(mm);
10021114 goto out_nolock;
10031115 }
10041116
10051117 /*
1006
- * __collapse_huge_page_swapin always returns with mmap_sem locked.
1007
- * If it fails, we release mmap_sem and jump out_nolock.
1118
+ * __collapse_huge_page_swapin always returns with mmap_lock locked.
1119
+ * If it fails, we release mmap_lock and jump out_nolock.
10081120 * Continuing to collapse causes inconsistency.
10091121 */
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);
1122
+ if (unmapped && !__collapse_huge_page_swapin(mm, vma, address,
1123
+ pmd, referenced)) {
1124
+ mmap_read_unlock(mm);
10131125 goto out_nolock;
10141126 }
10151127
1016
- up_read(&mm->mmap_sem);
1128
+ mmap_read_unlock(mm);
10171129 /*
10181130 * Prevent all access to pagetables with the exception of
10191131 * gup_fast later handled by the ptep_clear_flush and the VM
10201132 * handled by the anon_vma lock + PG_lock.
10211133 */
1022
- down_write(&mm->mmap_sem);
1134
+ mmap_write_lock(mm);
10231135 result = hugepage_vma_revalidate(mm, address, &vma);
10241136 if (result)
10251137 goto out;
....@@ -1027,27 +1139,33 @@
10271139 if (mm_find_pmd(mm, address) != pmd)
10281140 goto out;
10291141
1142
+ vm_write_begin(vma);
10301143 anon_vma_lock_write(vma->anon_vma);
1144
+
1145
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
1146
+ address, address + HPAGE_PMD_SIZE);
1147
+ mmu_notifier_invalidate_range_start(&range);
10311148
10321149 pte = pte_offset_map(pmd, address);
10331150 pte_ptl = pte_lockptr(mm, pmd);
10341151
1035
- mmun_start = address;
1036
- mmun_end = address + HPAGE_PMD_SIZE;
1037
- mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
10381152 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
10391153 /*
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.
1154
+ * This removes any huge TLB entry from the CPU so we won't allow
1155
+ * huge and small TLB entries for the same virtual address to
1156
+ * avoid the risk of CPU bugs in that area.
1157
+ *
1158
+ * Parallel fast GUP is fine since fast GUP will back off when
1159
+ * it detects PMD is changed.
10441160 */
10451161 _pmd = pmdp_collapse_flush(vma, address, pmd);
10461162 spin_unlock(pmd_ptl);
1047
- mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1163
+ mmu_notifier_invalidate_range_end(&range);
1164
+ tlb_remove_table_sync_one();
10481165
10491166 spin_lock(pte_ptl);
1050
- isolated = __collapse_huge_page_isolate(vma, address, pte);
1167
+ isolated = __collapse_huge_page_isolate(vma, address, pte,
1168
+ &compound_pagelist);
10511169 spin_unlock(pte_ptl);
10521170
10531171 if (unlikely(!isolated)) {
....@@ -1062,6 +1180,7 @@
10621180 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
10631181 spin_unlock(pmd_ptl);
10641182 anon_vma_unlock_write(vma->anon_vma);
1183
+ vm_write_end(vma);
10651184 result = SCAN_FAIL;
10661185 goto out;
10671186 }
....@@ -1072,7 +1191,8 @@
10721191 */
10731192 anon_vma_unlock_write(vma->anon_vma);
10741193
1075
- __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
1194
+ __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
1195
+ &compound_pagelist);
10761196 pte_unmap(pte);
10771197 __SetPageUptodate(new_page);
10781198 pgtable = pmd_pgtable(_pmd);
....@@ -1090,24 +1210,25 @@
10901210 spin_lock(pmd_ptl);
10911211 BUG_ON(!pmd_none(*pmd));
10921212 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);
1213
+ lru_cache_add_inactive_or_unevictable(new_page, vma);
10951214 pgtable_trans_huge_deposit(mm, pmd, pgtable);
10961215 set_pmd_at(mm, address, pmd, _pmd);
10971216 update_mmu_cache_pmd(vma, address, pmd);
10981217 spin_unlock(pmd_ptl);
1218
+ vm_write_end(vma);
10991219
11001220 *hpage = NULL;
11011221
11021222 khugepaged_pages_collapsed++;
11031223 result = SCAN_SUCCEED;
11041224 out_up_write:
1105
- up_write(&mm->mmap_sem);
1225
+ mmap_write_unlock(mm);
11061226 out_nolock:
1227
+ if (!IS_ERR_OR_NULL(*hpage))
1228
+ mem_cgroup_uncharge(*hpage);
11071229 trace_mm_collapse_huge_page(mm, isolated, result);
11081230 return;
11091231 out:
1110
- mem_cgroup_cancel_charge(new_page, memcg, true);
11111232 goto out_up_write;
11121233 }
11131234
....@@ -1118,7 +1239,8 @@
11181239 {
11191240 pmd_t *pmd;
11201241 pte_t *pte, *_pte;
1121
- int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
1242
+ int ret = 0, result = 0, referenced = 0;
1243
+ int none_or_zero = 0, shared = 0;
11221244 struct page *page = NULL;
11231245 unsigned long _address;
11241246 spinlock_t *ptl;
....@@ -1140,6 +1262,15 @@
11401262 pte_t pteval = *_pte;
11411263 if (is_swap_pte(pteval)) {
11421264 if (++unmapped <= khugepaged_max_ptes_swap) {
1265
+ /*
1266
+ * Always be strict with uffd-wp
1267
+ * enabled swap entries. Please see
1268
+ * comment below for pte_uffd_wp().
1269
+ */
1270
+ if (pte_swp_uffd_wp(pteval)) {
1271
+ result = SCAN_PTE_UFFD_WP;
1272
+ goto out_unmap;
1273
+ }
11431274 continue;
11441275 } else {
11451276 result = SCAN_EXCEED_SWAP_PTE;
....@@ -1159,6 +1290,19 @@
11591290 result = SCAN_PTE_NON_PRESENT;
11601291 goto out_unmap;
11611292 }
1293
+ if (pte_uffd_wp(pteval)) {
1294
+ /*
1295
+ * Don't collapse the page if any of the small
1296
+ * PTEs are armed with uffd write protection.
1297
+ * Here we can also mark the new huge pmd as
1298
+ * write protected if any of the small ones is
1299
+ * marked but that could bring uknown
1300
+ * userfault messages that falls outside of
1301
+ * the registered range. So, just be simple.
1302
+ */
1303
+ result = SCAN_PTE_UFFD_WP;
1304
+ goto out_unmap;
1305
+ }
11621306 if (pte_write(pteval))
11631307 writable = true;
11641308
....@@ -1168,11 +1312,13 @@
11681312 goto out_unmap;
11691313 }
11701314
1171
- /* TODO: teach khugepaged to collapse THP mapped with pte */
1172
- if (PageCompound(page)) {
1173
- result = SCAN_PAGE_COMPOUND;
1315
+ if (page_mapcount(page) > 1 &&
1316
+ ++shared > khugepaged_max_ptes_shared) {
1317
+ result = SCAN_EXCEED_SHARED_PTE;
11741318 goto out_unmap;
11751319 }
1320
+
1321
+ page = compound_head(page);
11761322
11771323 /*
11781324 * Record which node the original page is from and save this
....@@ -1200,11 +1346,23 @@
12001346 }
12011347
12021348 /*
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.
1349
+ * Check if the page has any GUP (or other external) pins.
1350
+ *
1351
+ * Here the check is racy it may see totmal_mapcount > refcount
1352
+ * in some cases.
1353
+ * For example, one process with one forked child process.
1354
+ * The parent has the PMD split due to MADV_DONTNEED, then
1355
+ * the child is trying unmap the whole PMD, but khugepaged
1356
+ * may be scanning the parent between the child has
1357
+ * PageDoubleMap flag cleared and dec the mapcount. So
1358
+ * khugepaged may see total_mapcount > refcount.
1359
+ *
1360
+ * But such case is ephemeral we could always retry collapse
1361
+ * later. However it may report false positive if the page
1362
+ * has excessive GUP pins (i.e. 512). Anyway the same check
1363
+ * will be done again later the risk seems low.
12061364 */
1207
- if (page_count(page) != 1 + PageSwapCache(page)) {
1365
+ if (!is_refcount_suitable(page)) {
12081366 result = SCAN_PAGE_COUNT;
12091367 goto out_unmap;
12101368 }
....@@ -1213,22 +1371,21 @@
12131371 mmu_notifier_test_young(vma->vm_mm, address))
12141372 referenced++;
12151373 }
1216
- if (writable) {
1217
- if (referenced) {
1218
- result = SCAN_SUCCEED;
1219
- ret = 1;
1220
- } else {
1221
- result = SCAN_LACK_REFERENCED_PAGE;
1222
- }
1223
- } else {
1374
+ if (!writable) {
12241375 result = SCAN_PAGE_RO;
1376
+ } else if (!referenced || (unmapped && referenced < HPAGE_PMD_NR/2)) {
1377
+ result = SCAN_LACK_REFERENCED_PAGE;
1378
+ } else {
1379
+ result = SCAN_SUCCEED;
1380
+ ret = 1;
12251381 }
12261382 out_unmap:
12271383 pte_unmap_unlock(pte, ptl);
12281384 if (ret) {
12291385 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);
1386
+ /* collapse_huge_page will return with the mmap_lock released */
1387
+ collapse_huge_page(mm, address, hpage, node,
1388
+ referenced, unmapped);
12321389 }
12331390 out:
12341391 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
....@@ -1240,7 +1397,7 @@
12401397 {
12411398 struct mm_struct *mm = mm_slot->mm;
12421399
1243
- VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1400
+ lockdep_assert_held(&khugepaged_mm_lock);
12441401
12451402 if (khugepaged_test_exit(mm)) {
12461403 /* free mm_slot */
....@@ -1259,7 +1416,184 @@
12591416 }
12601417 }
12611418
1262
-#if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
1419
+#ifdef CONFIG_SHMEM
1420
+/*
1421
+ * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1422
+ * khugepaged should try to collapse the page table.
1423
+ */
1424
+static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
1425
+ unsigned long addr)
1426
+{
1427
+ struct mm_slot *mm_slot;
1428
+
1429
+ VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1430
+
1431
+ spin_lock(&khugepaged_mm_lock);
1432
+ mm_slot = get_mm_slot(mm);
1433
+ if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP))
1434
+ mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
1435
+ spin_unlock(&khugepaged_mm_lock);
1436
+ return 0;
1437
+}
1438
+
1439
+/**
1440
+ * Try to collapse a pte-mapped THP for mm at address haddr.
1441
+ *
1442
+ * This function checks whether all the PTEs in the PMD are pointing to the
1443
+ * right THP. If so, retract the page table so the THP can refault in with
1444
+ * as pmd-mapped.
1445
+ */
1446
+void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
1447
+{
1448
+ unsigned long haddr = addr & HPAGE_PMD_MASK;
1449
+ struct vm_area_struct *vma = find_vma(mm, haddr);
1450
+ struct page *hpage;
1451
+ pte_t *start_pte, *pte;
1452
+ pmd_t *pmd, _pmd;
1453
+ spinlock_t *ptl;
1454
+ int count = 0;
1455
+ int i;
1456
+ struct mmu_notifier_range range;
1457
+
1458
+ if (!vma || !vma->vm_file ||
1459
+ vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE)
1460
+ return;
1461
+
1462
+ /*
1463
+ * This vm_flags may not have VM_HUGEPAGE if the page was not
1464
+ * collapsed by this mm. But we can still collapse if the page is
1465
+ * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
1466
+ * will not fail the vma for missing VM_HUGEPAGE
1467
+ */
1468
+ if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
1469
+ return;
1470
+
1471
+ hpage = find_lock_page(vma->vm_file->f_mapping,
1472
+ linear_page_index(vma, haddr));
1473
+ if (!hpage)
1474
+ return;
1475
+
1476
+ if (!PageHead(hpage))
1477
+ goto drop_hpage;
1478
+
1479
+ pmd = mm_find_pmd(mm, haddr);
1480
+ if (!pmd)
1481
+ goto drop_hpage;
1482
+
1483
+ vm_write_begin(vma);
1484
+
1485
+ /*
1486
+ * We need to lock the mapping so that from here on, only GUP-fast and
1487
+ * hardware page walks can access the parts of the page tables that
1488
+ * we're operating on.
1489
+ */
1490
+ i_mmap_lock_write(vma->vm_file->f_mapping);
1491
+
1492
+ /*
1493
+ * This spinlock should be unnecessary: Nobody else should be accessing
1494
+ * the page tables under spinlock protection here, only
1495
+ * lockless_pages_from_mm() and the hardware page walker can access page
1496
+ * tables while all the high-level locks are held in write mode.
1497
+ */
1498
+ start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
1499
+
1500
+ /* step 1: check all mapped PTEs are to the right huge page */
1501
+ for (i = 0, addr = haddr, pte = start_pte;
1502
+ i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1503
+ struct page *page;
1504
+
1505
+ /* empty pte, skip */
1506
+ if (pte_none(*pte))
1507
+ continue;
1508
+
1509
+ /* page swapped out, abort */
1510
+ if (!pte_present(*pte))
1511
+ goto abort;
1512
+
1513
+ page = vm_normal_page(vma, addr, *pte);
1514
+
1515
+ /*
1516
+ * Note that uprobe, debugger, or MAP_PRIVATE may change the
1517
+ * page table, but the new page will not be a subpage of hpage.
1518
+ */
1519
+ if (hpage + i != page)
1520
+ goto abort;
1521
+ count++;
1522
+ }
1523
+
1524
+ /* step 2: adjust rmap */
1525
+ for (i = 0, addr = haddr, pte = start_pte;
1526
+ i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1527
+ struct page *page;
1528
+
1529
+ if (pte_none(*pte))
1530
+ continue;
1531
+ page = vm_normal_page(vma, addr, *pte);
1532
+ page_remove_rmap(page, false);
1533
+ }
1534
+
1535
+ pte_unmap_unlock(start_pte, ptl);
1536
+
1537
+ /* step 3: set proper refcount and mm_counters. */
1538
+ if (count) {
1539
+ page_ref_sub(hpage, count);
1540
+ add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1541
+ }
1542
+
1543
+ /* step 4: collapse pmd */
1544
+ /* we make no change to anon, but protect concurrent anon page lookup */
1545
+ if (vma->anon_vma)
1546
+ anon_vma_lock_write(vma->anon_vma);
1547
+
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
+ if (vma->anon_vma)
1559
+ anon_vma_unlock_write(vma->anon_vma);
1560
+ i_mmap_unlock_write(vma->vm_file->f_mapping);
1561
+
1562
+drop_hpage:
1563
+ unlock_page(hpage);
1564
+ put_page(hpage);
1565
+ return;
1566
+
1567
+abort:
1568
+ pte_unmap_unlock(start_pte, ptl);
1569
+ vm_write_end(vma);
1570
+ i_mmap_unlock_write(vma->vm_file->f_mapping);
1571
+ goto drop_hpage;
1572
+}
1573
+
1574
+static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
1575
+{
1576
+ struct mm_struct *mm = mm_slot->mm;
1577
+ int i;
1578
+
1579
+ if (likely(mm_slot->nr_pte_mapped_thp == 0))
1580
+ return 0;
1581
+
1582
+ if (!mmap_write_trylock(mm))
1583
+ return -EBUSY;
1584
+
1585
+ if (unlikely(khugepaged_test_exit(mm)))
1586
+ goto out;
1587
+
1588
+ for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
1589
+ collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i]);
1590
+
1591
+out:
1592
+ mm_slot->nr_pte_mapped_thp = 0;
1593
+ mmap_write_unlock(mm);
1594
+ return 0;
1595
+}
1596
+
12631597 static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
12641598 {
12651599 struct vm_area_struct *vma;
....@@ -1269,7 +1603,23 @@
12691603
12701604 i_mmap_lock_write(mapping);
12711605 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1272
- /* probably overkill */
1606
+ /*
1607
+ * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1608
+ * got written to. These VMAs are likely not worth investing
1609
+ * mmap_write_lock(mm) as PMD-mapping is likely to be split
1610
+ * later.
1611
+ *
1612
+ * Not that vma->anon_vma check is racy: it can be set up after
1613
+ * the check but before we took mmap_lock by the fault path.
1614
+ * But page lock would prevent establishing any new ptes of the
1615
+ * page, so we are safe.
1616
+ *
1617
+ * An alternative would be drop the check, but check that page
1618
+ * table is clear before calling pmdp_collapse_flush() under
1619
+ * ptl. It has higher chance to recover THP for the VMA, but
1620
+ * has higher cost too. It would also probably require locking
1621
+ * the anon_vma.
1622
+ */
12731623 if (vma->anon_vma)
12741624 continue;
12751625 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
....@@ -1282,57 +1632,71 @@
12821632 if (!pmd)
12831633 continue;
12841634 /*
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.
1635
+ * We need exclusive mmap_lock to retract page table.
1636
+ *
1637
+ * We use trylock due to lock inversion: we need to acquire
1638
+ * mmap_lock while holding page lock. Fault path does it in
1639
+ * reverse order. Trylock is a way to avoid deadlock.
12891640 */
1290
- if (down_write_trylock(&mm->mmap_sem)) {
1641
+ if (mmap_write_trylock(mm)) {
12911642 if (!khugepaged_test_exit(mm)) {
1292
- spinlock_t *ptl = pmd_lock(mm, pmd);
1643
+ struct mmu_notifier_range range;
1644
+
1645
+ vm_write_begin(vma);
1646
+ mmu_notifier_range_init(&range,
1647
+ MMU_NOTIFY_CLEAR, 0,
1648
+ NULL, mm, addr,
1649
+ addr + HPAGE_PMD_SIZE);
1650
+ mmu_notifier_invalidate_range_start(&range);
12931651 /* assume page table is clear */
12941652 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1295
- spin_unlock(ptl);
1653
+ vm_write_end(vma);
12961654 mm_dec_nr_ptes(mm);
1655
+ tlb_remove_table_sync_one();
12971656 pte_free(mm, pmd_pgtable(_pmd));
1657
+ mmu_notifier_invalidate_range_end(&range);
12981658 }
1299
- up_write(&mm->mmap_sem);
1659
+ mmap_write_unlock(mm);
1660
+ } else {
1661
+ /* Try again later */
1662
+ khugepaged_add_pte_mapped_thp(mm, addr);
13001663 }
13011664 }
13021665 i_mmap_unlock_write(mapping);
13031666 }
13041667
13051668 /**
1306
- * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
1669
+ * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
13071670 *
13081671 * Basic scheme is simple, details are more complex:
13091672 * - allocate and lock a new huge page;
1310
- * - scan over radix tree replacing old pages the new one
1311
- * + swap in pages if necessary;
1673
+ * - scan page cache replacing old pages with the new one
1674
+ * + swap/gup in pages if necessary;
13121675 * + fill in gaps;
1313
- * + keep old pages around in case if rollback is required;
1314
- * - if replacing succeed:
1676
+ * + keep old pages around in case rollback is required;
1677
+ * - if replacing succeeds:
13151678 * + copy data over;
13161679 * + free old pages;
13171680 * + unlock huge page;
13181681 * - if replacing failed;
13191682 * + put all pages back and unfreeze them;
1320
- * + restore gaps in the radix-tree;
1683
+ * + restore gaps in the page cache;
13211684 * + unlock and free huge page;
13221685 */
1323
-static void collapse_shmem(struct mm_struct *mm,
1324
- struct address_space *mapping, pgoff_t start,
1686
+static void collapse_file(struct mm_struct *mm,
1687
+ struct file *file, pgoff_t start,
13251688 struct page **hpage, int node)
13261689 {
1690
+ struct address_space *mapping = file->f_mapping;
13271691 gfp_t gfp;
1328
- struct page *page, *new_page, *tmp;
1329
- struct mem_cgroup *memcg;
1692
+ struct page *new_page;
13301693 pgoff_t index, end = start + HPAGE_PMD_NR;
13311694 LIST_HEAD(pagelist);
1332
- struct radix_tree_iter iter;
1333
- void **slot;
1695
+ XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
13341696 int nr_none = 0, result = SCAN_SUCCEED;
1697
+ bool is_shmem = shmem_file(file);
13351698
1699
+ VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
13361700 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
13371701
13381702 /* Only allocate from the target node */
....@@ -1344,13 +1708,28 @@
13441708 goto out;
13451709 }
13461710
1347
- if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
1711
+ if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
13481712 result = SCAN_CGROUP_CHARGE_FAIL;
13491713 goto out;
13501714 }
1715
+ count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
1716
+
1717
+ /* This will be less messy when we use multi-index entries */
1718
+ do {
1719
+ xas_lock_irq(&xas);
1720
+ xas_create_range(&xas);
1721
+ if (!xas_error(&xas))
1722
+ break;
1723
+ xas_unlock_irq(&xas);
1724
+ if (!xas_nomem(&xas, GFP_KERNEL)) {
1725
+ result = SCAN_FAIL;
1726
+ goto out;
1727
+ }
1728
+ } while (1);
13511729
13521730 __SetPageLocked(new_page);
1353
- __SetPageSwapBacked(new_page);
1731
+ if (is_shmem)
1732
+ __SetPageSwapBacked(new_page);
13541733 new_page->index = start;
13551734 new_page->mapping = mapping;
13561735
....@@ -1360,55 +1739,91 @@
13601739 * be able to map it or use it in another way until we unlock it.
13611740 */
13621741
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;
1742
+ xas_set(&xas, start);
1743
+ for (index = start; index < end; index++) {
1744
+ struct page *page = xas_next(&xas);
13671745
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;
1746
+ VM_BUG_ON(index != xas.xa_index);
1747
+ if (is_shmem) {
1748
+ if (!page) {
1749
+ /*
1750
+ * Stop if extent has been truncated or
1751
+ * hole-punched, and is now completely
1752
+ * empty.
1753
+ */
1754
+ if (index == start) {
1755
+ if (!xas_next_entry(&xas, end - 1)) {
1756
+ result = SCAN_TRUNCATED;
1757
+ goto xa_locked;
1758
+ }
1759
+ xas_set(&xas, index);
1760
+ }
1761
+ if (!shmem_charge(mapping->host, 1)) {
1762
+ result = SCAN_FAIL;
1763
+ goto xa_locked;
1764
+ }
1765
+ xas_store(&xas, new_page);
1766
+ nr_none++;
1767
+ continue;
14051768 }
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;
1769
+
1770
+ if (xa_is_value(page) || !PageUptodate(page)) {
1771
+ xas_unlock_irq(&xas);
1772
+ /* swap in or instantiate fallocated page */
1773
+ if (shmem_getpage(mapping->host, index, &page,
1774
+ SGP_NOHUGE)) {
1775
+ result = SCAN_FAIL;
1776
+ goto xa_unlocked;
1777
+ }
1778
+ } else if (trylock_page(page)) {
1779
+ get_page(page);
1780
+ xas_unlock_irq(&xas);
1781
+ } else {
1782
+ result = SCAN_PAGE_LOCK;
1783
+ goto xa_locked;
1784
+ }
1785
+ } else { /* !is_shmem */
1786
+ if (!page || xa_is_value(page)) {
1787
+ xas_unlock_irq(&xas);
1788
+ page_cache_sync_readahead(mapping, &file->f_ra,
1789
+ file, index,
1790
+ end - index);
1791
+ /* drain pagevecs to help isolate_lru_page() */
1792
+ lru_add_drain();
1793
+ page = find_lock_page(mapping, index);
1794
+ if (unlikely(page == NULL)) {
1795
+ result = SCAN_FAIL;
1796
+ goto xa_unlocked;
1797
+ }
1798
+ } else if (PageDirty(page)) {
1799
+ /*
1800
+ * khugepaged only works on read-only fd,
1801
+ * so this page is dirty because it hasn't
1802
+ * been flushed since first write. There
1803
+ * won't be new dirty pages.
1804
+ *
1805
+ * Trigger async flush here and hope the
1806
+ * writeback is done when khugepaged
1807
+ * revisits this page.
1808
+ *
1809
+ * This is a one-off situation. We are not
1810
+ * forcing writeback in loop.
1811
+ */
1812
+ xas_unlock_irq(&xas);
1813
+ filemap_flush(mapping);
1814
+ result = SCAN_FAIL;
1815
+ goto xa_unlocked;
1816
+ } else if (PageWriteback(page)) {
1817
+ xas_unlock_irq(&xas);
1818
+ result = SCAN_FAIL;
1819
+ goto xa_unlocked;
1820
+ } else if (trylock_page(page)) {
1821
+ get_page(page);
1822
+ xas_unlock_irq(&xas);
1823
+ } else {
1824
+ result = SCAN_PAGE_LOCK;
1825
+ goto xa_locked;
1826
+ }
14121827 }
14131828
14141829 /*
....@@ -1416,7 +1831,12 @@
14161831 * without racing with truncate.
14171832 */
14181833 VM_BUG_ON_PAGE(!PageLocked(page), page);
1419
- VM_BUG_ON_PAGE(!PageUptodate(page), page);
1834
+
1835
+ /* make sure the page is up to date */
1836
+ if (unlikely(!PageUptodate(page))) {
1837
+ result = SCAN_FAIL;
1838
+ goto out_unlock;
1839
+ }
14201840
14211841 /*
14221842 * If file was truncated then extended, or hole-punched, before
....@@ -1432,30 +1852,47 @@
14321852 goto out_unlock;
14331853 }
14341854
1855
+ if (!is_shmem && (PageDirty(page) ||
1856
+ PageWriteback(page))) {
1857
+ /*
1858
+ * khugepaged only works on read-only fd, so this
1859
+ * page is dirty because it hasn't been flushed
1860
+ * since first write.
1861
+ */
1862
+ result = SCAN_FAIL;
1863
+ goto out_unlock;
1864
+ }
1865
+
14351866 if (isolate_lru_page(page)) {
14361867 result = SCAN_DEL_PAGE_LRU;
1868
+ goto out_unlock;
1869
+ }
1870
+
1871
+ if (page_has_private(page) &&
1872
+ !try_to_release_page(page, GFP_KERNEL)) {
1873
+ result = SCAN_PAGE_HAS_PRIVATE;
1874
+ putback_lru_page(page);
14371875 goto out_unlock;
14381876 }
14391877
14401878 if (page_mapped(page))
14411879 unmap_mapping_pages(mapping, index, 1, false);
14421880
1443
- xa_lock_irq(&mapping->i_pages);
1881
+ xas_lock_irq(&xas);
1882
+ xas_set(&xas, index);
14441883
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);
1884
+ VM_BUG_ON_PAGE(page != xas_load(&xas), page);
14481885 VM_BUG_ON_PAGE(page_mapped(page), page);
14491886
14501887 /*
14511888 * The page is expected to have page_count() == 3:
14521889 * - we hold a pin on it;
1453
- * - one reference from radix tree;
1890
+ * - one reference from page cache;
14541891 * - one from isolate_lru_page;
14551892 */
14561893 if (!page_ref_freeze(page, 3)) {
14571894 result = SCAN_PAGE_COUNT;
1458
- xa_unlock_irq(&mapping->i_pages);
1895
+ xas_unlock_irq(&xas);
14591896 putback_lru_page(page);
14601897 goto out_unlock;
14611898 }
....@@ -1467,58 +1904,50 @@
14671904 list_add_tail(&page->lru, &pagelist);
14681905
14691906 /* 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++;
1907
+ xas_store(&xas, new_page);
14751908 continue;
14761909 out_unlock:
14771910 unlock_page(page);
14781911 put_page(page);
1479
- goto tree_unlocked;
1912
+ goto xa_unlocked;
14801913 }
14811914
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)) {
1915
+ if (is_shmem)
1916
+ __inc_node_page_state(new_page, NR_SHMEM_THPS);
1917
+ else {
1918
+ __inc_node_page_state(new_page, NR_FILE_THPS);
1919
+ filemap_nr_thps_inc(mapping);
1920
+ /*
1921
+ * Paired with smp_mb() in do_dentry_open() to ensure
1922
+ * i_writecount is up to date and the update to nr_thps is
1923
+ * visible. Ensures the page cache will be truncated if the
1924
+ * file is opened writable.
1925
+ */
1926
+ smp_mb();
1927
+ if (inode_is_open_for_write(mapping->host)) {
14961928 result = SCAN_FAIL;
1497
- goto tree_locked;
1929
+ __dec_node_page_state(new_page, NR_FILE_THPS);
1930
+ filemap_nr_thps_dec(mapping);
1931
+ goto xa_locked;
14981932 }
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;
15041933 }
15051934
1506
- __inc_node_page_state(new_page, NR_SHMEM_THPS);
15071935 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);
1936
+ __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
1937
+ if (is_shmem)
1938
+ __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
15121939 }
15131940
1514
-tree_locked:
1515
- xa_unlock_irq(&mapping->i_pages);
1516
-tree_unlocked:
1941
+xa_locked:
1942
+ xas_unlock_irq(&xas);
1943
+xa_unlocked:
15171944
15181945 if (result == SCAN_SUCCEED) {
1946
+ struct page *page, *tmp;
1947
+
15191948 /*
1520
- * Replacing old pages with new one has succeed, now we need to
1521
- * copy the content and free old pages.
1949
+ * Replacing old pages with new one has succeeded, now we
1950
+ * need to copy the content and free the old pages.
15221951 */
15231952 index = start;
15241953 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
....@@ -1544,9 +1973,9 @@
15441973
15451974 SetPageUptodate(new_page);
15461975 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);
1976
+ if (is_shmem)
1977
+ set_page_dirty(new_page);
1978
+ lru_cache_add(new_page);
15501979
15511980 /*
15521981 * Remove pte page tables, so we can re-fault the page as huge.
....@@ -1556,57 +1985,60 @@
15561985
15571986 khugepaged_pages_collapsed++;
15581987 } 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);
1988
+ struct page *page;
15631989
1564
- radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
1565
- if (iter.index >= end)
1566
- break;
1990
+ /* Something went wrong: roll back page cache changes */
1991
+ xas_lock_irq(&xas);
1992
+ mapping->nrpages -= nr_none;
1993
+
1994
+ if (is_shmem)
1995
+ shmem_uncharge(mapping->host, nr_none);
1996
+
1997
+ xas_set(&xas, start);
1998
+ xas_for_each(&xas, page, end - 1) {
15671999 page = list_first_entry_or_null(&pagelist,
15682000 struct page, lru);
1569
- if (!page || iter.index < page->index) {
2001
+ if (!page || xas.xa_index < page->index) {
15702002 if (!nr_none)
15712003 break;
15722004 nr_none--;
15732005 /* Put holes back where they were */
1574
- radix_tree_delete(&mapping->i_pages, iter.index);
2006
+ xas_store(&xas, NULL);
15752007 continue;
15762008 }
15772009
1578
- VM_BUG_ON_PAGE(page->index != iter.index, page);
2010
+ VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
15792011
15802012 /* Unfreeze the page. */
15812013 list_del(&page->lru);
15822014 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);
2015
+ xas_store(&xas, page);
2016
+ xas_pause(&xas);
2017
+ xas_unlock_irq(&xas);
15862018 unlock_page(page);
15872019 putback_lru_page(page);
1588
- xa_lock_irq(&mapping->i_pages);
2020
+ xas_lock_irq(&xas);
15892021 }
15902022 VM_BUG_ON(nr_none);
1591
- xa_unlock_irq(&mapping->i_pages);
2023
+ xas_unlock_irq(&xas);
15922024
1593
- mem_cgroup_cancel_charge(new_page, memcg, true);
15942025 new_page->mapping = NULL;
15952026 }
15962027
15972028 unlock_page(new_page);
15982029 out:
15992030 VM_BUG_ON(!list_empty(&pagelist));
2031
+ if (!IS_ERR_OR_NULL(*hpage))
2032
+ mem_cgroup_uncharge(*hpage);
16002033 /* TODO: tracepoints */
16012034 }
16022035
1603
-static void khugepaged_scan_shmem(struct mm_struct *mm,
1604
- struct address_space *mapping,
1605
- pgoff_t start, struct page **hpage)
2036
+static void khugepaged_scan_file(struct mm_struct *mm,
2037
+ struct file *file, pgoff_t start, struct page **hpage)
16062038 {
16072039 struct page *page = NULL;
1608
- struct radix_tree_iter iter;
1609
- void **slot;
2040
+ struct address_space *mapping = file->f_mapping;
2041
+ XA_STATE(xas, &mapping->i_pages, start);
16102042 int present, swap;
16112043 int node = NUMA_NO_NODE;
16122044 int result = SCAN_SUCCEED;
....@@ -1615,17 +2047,11 @@
16152047 swap = 0;
16162048 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
16172049 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);
2050
+ xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
2051
+ if (xas_retry(&xas, page))
16252052 continue;
1626
- }
16272053
1628
- if (radix_tree_exception(page)) {
2054
+ if (xa_is_value(page)) {
16292055 if (++swap > khugepaged_max_ptes_swap) {
16302056 result = SCAN_EXCEED_SWAP_PTE;
16312057 break;
....@@ -1650,7 +2076,8 @@
16502076 break;
16512077 }
16522078
1653
- if (page_count(page) != 1 + page_mapcount(page)) {
2079
+ if (page_count(page) !=
2080
+ 1 + page_mapcount(page) + page_has_private(page)) {
16542081 result = SCAN_PAGE_COUNT;
16552082 break;
16562083 }
....@@ -1664,7 +2091,7 @@
16642091 present++;
16652092
16662093 if (need_resched()) {
1667
- slot = radix_tree_iter_resume(slot, &iter);
2094
+ xas_pause(&xas);
16682095 cond_resched_rcu();
16692096 }
16702097 }
....@@ -1675,18 +2102,22 @@
16752102 result = SCAN_EXCEED_NONE_PTE;
16762103 } else {
16772104 node = khugepaged_find_target_node();
1678
- collapse_shmem(mm, mapping, start, hpage, node);
2105
+ collapse_file(mm, file, start, hpage, node);
16792106 }
16802107 }
16812108
16822109 /* TODO: tracepoints */
16832110 }
16842111 #else
1685
-static void khugepaged_scan_shmem(struct mm_struct *mm,
1686
- struct address_space *mapping,
1687
- pgoff_t start, struct page **hpage)
2112
+static void khugepaged_scan_file(struct mm_struct *mm,
2113
+ struct file *file, pgoff_t start, struct page **hpage)
16882114 {
16892115 BUILD_BUG();
2116
+}
2117
+
2118
+static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
2119
+{
2120
+ return 0;
16902121 }
16912122 #endif
16922123
....@@ -1701,7 +2132,7 @@
17012132 int progress = 0;
17022133
17032134 VM_BUG_ON(!pages);
1704
- VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2135
+ lockdep_assert_held(&khugepaged_mm_lock);
17052136
17062137 if (khugepaged_scan.mm_slot)
17072138 mm_slot = khugepaged_scan.mm_slot;
....@@ -1712,6 +2143,7 @@
17122143 khugepaged_scan.mm_slot = mm_slot;
17132144 }
17142145 spin_unlock(&khugepaged_mm_lock);
2146
+ khugepaged_collapse_pte_mapped_thps(mm_slot);
17152147
17162148 mm = mm_slot->mm;
17172149 /*
....@@ -1719,8 +2151,8 @@
17192151 * the next mm on the list.
17202152 */
17212153 vma = NULL;
1722
- if (unlikely(!down_read_trylock(&mm->mmap_sem)))
1723
- goto breakouterloop_mmap_sem;
2154
+ if (unlikely(!mmap_read_trylock(mm)))
2155
+ goto breakouterloop_mmap_lock;
17242156 if (likely(!khugepaged_test_exit(mm)))
17252157 vma = find_vma(mm, khugepaged_scan.address);
17262158
....@@ -1747,6 +2179,8 @@
17472179 if (khugepaged_scan.address < hstart)
17482180 khugepaged_scan.address = hstart;
17492181 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2182
+ if (shmem_file(vma->vm_file) && !shmem_huge_enabled(vma))
2183
+ goto skip;
17502184
17512185 while (khugepaged_scan.address < hend) {
17522186 int ret;
....@@ -1757,17 +2191,14 @@
17572191 VM_BUG_ON(khugepaged_scan.address < hstart ||
17582192 khugepaged_scan.address + HPAGE_PMD_SIZE >
17592193 hend);
1760
- if (shmem_file(vma->vm_file)) {
1761
- struct file *file;
2194
+ if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
2195
+ struct file *file = get_file(vma->vm_file);
17622196 pgoff_t pgoff = linear_page_index(vma,
17632197 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);
2198
+
2199
+ mmap_read_unlock(mm);
17682200 ret = 1;
1769
- khugepaged_scan_shmem(mm, file->f_mapping,
1770
- pgoff, hpage);
2201
+ khugepaged_scan_file(mm, file, pgoff, hpage);
17712202 fput(file);
17722203 } else {
17732204 ret = khugepaged_scan_pmd(mm, vma,
....@@ -1778,15 +2209,15 @@
17782209 khugepaged_scan.address += HPAGE_PMD_SIZE;
17792210 progress += HPAGE_PMD_NR;
17802211 if (ret)
1781
- /* we released mmap_sem so break loop */
1782
- goto breakouterloop_mmap_sem;
2212
+ /* we released mmap_lock so break loop */
2213
+ goto breakouterloop_mmap_lock;
17832214 if (progress >= pages)
17842215 goto breakouterloop;
17852216 }
17862217 }
17872218 breakouterloop:
1788
- up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
1789
-breakouterloop_mmap_sem:
2219
+ mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */
2220
+breakouterloop_mmap_lock:
17902221
17912222 spin_lock(&khugepaged_mm_lock);
17922223 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
....@@ -1837,6 +2268,8 @@
18372268
18382269 barrier(); /* write khugepaged_pages_to_scan to local stack */
18392270
2271
+ lru_add_drain_all();
2272
+
18402273 while (progress < pages) {
18412274 if (!khugepaged_prealloc_page(&hpage, &wait))
18422275 break;