1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 #include <linux/sched.h>
6 #include <linux/sched/mm.h>
7 #include <linux/sched/coredump.h>
8 #include <linux/mmu_notifier.h>
9 #include <linux/rmap.h>
10 #include <linux/swap.h>
11 #include <linux/mm_inline.h>
12 #include <linux/kthread.h>
13 #include <linux/khugepaged.h>
14 #include <linux/freezer.h>
15 #include <linux/mman.h>
16 #include <linux/hashtable.h>
17 #include <linux/userfaultfd_k.h>
18 #include <linux/page_idle.h>
19 #include <linux/swapops.h>
20 #include <linux/shmem_fs.h>
23 #include <asm/pgalloc.h>
34 SCAN_LACK_REFERENCED_PAGE
,
48 SCAN_ALLOC_HUGE_PAGE_FAIL
,
49 SCAN_CGROUP_CHARGE_FAIL
,
52 SCAN_PAGE_HAS_PRIVATE
,
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/huge_memory.h>
58 /* default scan 8*512 pte (or vmas) every 30 second */
59 static unsigned int khugepaged_pages_to_scan __read_mostly
;
60 static unsigned int khugepaged_pages_collapsed
;
61 static unsigned int khugepaged_full_scans
;
62 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
63 /* during fragmentation poll the hugepage allocator once every minute */
64 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
65 static unsigned long khugepaged_sleep_expire
;
66 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
67 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
69 * default collapse hugepages if there is at least one pte mapped like
70 * it would have happened if the vma was large enough during page
73 static unsigned int khugepaged_max_ptes_none __read_mostly
;
74 static unsigned int khugepaged_max_ptes_swap __read_mostly
;
76 #define MM_SLOTS_HASH_BITS 10
77 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
79 static struct kmem_cache
*mm_slot_cache __read_mostly
;
81 #define MAX_PTE_MAPPED_THP 8
84 * struct mm_slot - hash lookup from mm to mm_slot
85 * @hash: hash collision list
86 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
87 * @mm: the mm that this information is valid for
90 struct hlist_node hash
;
91 struct list_head mm_node
;
94 /* pte-mapped THP in this mm */
95 int nr_pte_mapped_thp
;
96 unsigned long pte_mapped_thp
[MAX_PTE_MAPPED_THP
];
100 * struct khugepaged_scan - cursor for scanning
101 * @mm_head: the head of the mm list to scan
102 * @mm_slot: the current mm_slot we are scanning
103 * @address: the next address inside that to be scanned
105 * There is only the one khugepaged_scan instance of this cursor structure.
107 struct khugepaged_scan
{
108 struct list_head mm_head
;
109 struct mm_slot
*mm_slot
;
110 unsigned long address
;
113 static struct khugepaged_scan khugepaged_scan
= {
114 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
118 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
119 struct kobj_attribute
*attr
,
122 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
125 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
126 struct kobj_attribute
*attr
,
127 const char *buf
, size_t count
)
132 err
= kstrtoul(buf
, 10, &msecs
);
133 if (err
|| msecs
> UINT_MAX
)
136 khugepaged_scan_sleep_millisecs
= msecs
;
137 khugepaged_sleep_expire
= 0;
138 wake_up_interruptible(&khugepaged_wait
);
142 static struct kobj_attribute scan_sleep_millisecs_attr
=
143 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
144 scan_sleep_millisecs_store
);
146 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
147 struct kobj_attribute
*attr
,
150 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
153 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
154 struct kobj_attribute
*attr
,
155 const char *buf
, size_t count
)
160 err
= kstrtoul(buf
, 10, &msecs
);
161 if (err
|| msecs
> UINT_MAX
)
164 khugepaged_alloc_sleep_millisecs
= msecs
;
165 khugepaged_sleep_expire
= 0;
166 wake_up_interruptible(&khugepaged_wait
);
170 static struct kobj_attribute alloc_sleep_millisecs_attr
=
171 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
172 alloc_sleep_millisecs_store
);
174 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
175 struct kobj_attribute
*attr
,
178 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
180 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
181 struct kobj_attribute
*attr
,
182 const char *buf
, size_t count
)
187 err
= kstrtoul(buf
, 10, &pages
);
188 if (err
|| !pages
|| pages
> UINT_MAX
)
191 khugepaged_pages_to_scan
= pages
;
195 static struct kobj_attribute pages_to_scan_attr
=
196 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
197 pages_to_scan_store
);
199 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
200 struct kobj_attribute
*attr
,
203 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
205 static struct kobj_attribute pages_collapsed_attr
=
206 __ATTR_RO(pages_collapsed
);
208 static ssize_t
full_scans_show(struct kobject
*kobj
,
209 struct kobj_attribute
*attr
,
212 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
214 static struct kobj_attribute full_scans_attr
=
215 __ATTR_RO(full_scans
);
217 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
218 struct kobj_attribute
*attr
, char *buf
)
220 return single_hugepage_flag_show(kobj
, attr
, buf
,
221 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
223 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
224 struct kobj_attribute
*attr
,
225 const char *buf
, size_t count
)
227 return single_hugepage_flag_store(kobj
, attr
, buf
, count
,
228 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
230 static struct kobj_attribute khugepaged_defrag_attr
=
231 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
232 khugepaged_defrag_store
);
235 * max_ptes_none controls if khugepaged should collapse hugepages over
236 * any unmapped ptes in turn potentially increasing the memory
237 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
238 * reduce the available free memory in the system as it
239 * runs. Increasing max_ptes_none will instead potentially reduce the
240 * free memory in the system during the khugepaged scan.
242 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
243 struct kobj_attribute
*attr
,
246 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
248 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
249 struct kobj_attribute
*attr
,
250 const char *buf
, size_t count
)
253 unsigned long max_ptes_none
;
255 err
= kstrtoul(buf
, 10, &max_ptes_none
);
256 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
259 khugepaged_max_ptes_none
= max_ptes_none
;
263 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
264 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
265 khugepaged_max_ptes_none_store
);
267 static ssize_t
khugepaged_max_ptes_swap_show(struct kobject
*kobj
,
268 struct kobj_attribute
*attr
,
271 return sprintf(buf
, "%u\n", khugepaged_max_ptes_swap
);
274 static ssize_t
khugepaged_max_ptes_swap_store(struct kobject
*kobj
,
275 struct kobj_attribute
*attr
,
276 const char *buf
, size_t count
)
279 unsigned long max_ptes_swap
;
281 err
= kstrtoul(buf
, 10, &max_ptes_swap
);
282 if (err
|| max_ptes_swap
> HPAGE_PMD_NR
-1)
285 khugepaged_max_ptes_swap
= max_ptes_swap
;
290 static struct kobj_attribute khugepaged_max_ptes_swap_attr
=
291 __ATTR(max_ptes_swap
, 0644, khugepaged_max_ptes_swap_show
,
292 khugepaged_max_ptes_swap_store
);
294 static struct attribute
*khugepaged_attr
[] = {
295 &khugepaged_defrag_attr
.attr
,
296 &khugepaged_max_ptes_none_attr
.attr
,
297 &pages_to_scan_attr
.attr
,
298 &pages_collapsed_attr
.attr
,
299 &full_scans_attr
.attr
,
300 &scan_sleep_millisecs_attr
.attr
,
301 &alloc_sleep_millisecs_attr
.attr
,
302 &khugepaged_max_ptes_swap_attr
.attr
,
306 struct attribute_group khugepaged_attr_group
= {
307 .attrs
= khugepaged_attr
,
308 .name
= "khugepaged",
310 #endif /* CONFIG_SYSFS */
312 int hugepage_madvise(struct vm_area_struct
*vma
,
313 unsigned long *vm_flags
, int advice
)
319 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
320 * can't handle this properly after s390_enable_sie, so we simply
321 * ignore the madvise to prevent qemu from causing a SIGSEGV.
323 if (mm_has_pgste(vma
->vm_mm
))
326 *vm_flags
&= ~VM_NOHUGEPAGE
;
327 *vm_flags
|= VM_HUGEPAGE
;
329 * If the vma become good for khugepaged to scan,
330 * register it here without waiting a page fault that
331 * may not happen any time soon.
333 if (!(*vm_flags
& VM_NO_KHUGEPAGED
) &&
334 khugepaged_enter_vma_merge(vma
, *vm_flags
))
337 case MADV_NOHUGEPAGE
:
338 *vm_flags
&= ~VM_HUGEPAGE
;
339 *vm_flags
|= VM_NOHUGEPAGE
;
341 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
342 * this vma even if we leave the mm registered in khugepaged if
343 * it got registered before VM_NOHUGEPAGE was set.
351 int __init
khugepaged_init(void)
353 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
354 sizeof(struct mm_slot
),
355 __alignof__(struct mm_slot
), 0, NULL
);
359 khugepaged_pages_to_scan
= HPAGE_PMD_NR
* 8;
360 khugepaged_max_ptes_none
= HPAGE_PMD_NR
- 1;
361 khugepaged_max_ptes_swap
= HPAGE_PMD_NR
/ 8;
366 void __init
khugepaged_destroy(void)
368 kmem_cache_destroy(mm_slot_cache
);
371 static inline struct mm_slot
*alloc_mm_slot(void)
373 if (!mm_slot_cache
) /* initialization failed */
375 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
378 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
380 kmem_cache_free(mm_slot_cache
, mm_slot
);
383 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
385 struct mm_slot
*mm_slot
;
387 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
388 if (mm
== mm_slot
->mm
)
394 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
395 struct mm_slot
*mm_slot
)
398 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
401 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
403 return atomic_read(&mm
->mm_users
) == 0;
406 static bool hugepage_vma_check(struct vm_area_struct
*vma
,
407 unsigned long vm_flags
)
409 if ((!(vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
410 (vm_flags
& VM_NOHUGEPAGE
) ||
411 test_bit(MMF_DISABLE_THP
, &vma
->vm_mm
->flags
))
414 if (shmem_file(vma
->vm_file
) ||
415 (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS
) &&
417 (vm_flags
& VM_DENYWRITE
))) {
418 return IS_ALIGNED((vma
->vm_start
>> PAGE_SHIFT
) - vma
->vm_pgoff
,
421 if (!vma
->anon_vma
|| vma
->vm_ops
)
423 if (vma_is_temporary_stack(vma
))
425 return !(vm_flags
& VM_NO_KHUGEPAGED
);
428 int __khugepaged_enter(struct mm_struct
*mm
)
430 struct mm_slot
*mm_slot
;
433 mm_slot
= alloc_mm_slot();
437 /* __khugepaged_exit() must not run from under us */
438 VM_BUG_ON_MM(khugepaged_test_exit(mm
), mm
);
439 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
440 free_mm_slot(mm_slot
);
444 spin_lock(&khugepaged_mm_lock
);
445 insert_to_mm_slots_hash(mm
, mm_slot
);
447 * Insert just behind the scanning cursor, to let the area settle
450 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
451 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
452 spin_unlock(&khugepaged_mm_lock
);
456 wake_up_interruptible(&khugepaged_wait
);
461 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
462 unsigned long vm_flags
)
464 unsigned long hstart
, hend
;
467 * khugepaged only supports read-only files for non-shmem files.
468 * khugepaged does not yet work on special mappings. And
469 * file-private shmem THP is not supported.
471 if (!hugepage_vma_check(vma
, vm_flags
))
474 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
475 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
477 return khugepaged_enter(vma
, vm_flags
);
481 void __khugepaged_exit(struct mm_struct
*mm
)
483 struct mm_slot
*mm_slot
;
486 spin_lock(&khugepaged_mm_lock
);
487 mm_slot
= get_mm_slot(mm
);
488 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
489 hash_del(&mm_slot
->hash
);
490 list_del(&mm_slot
->mm_node
);
493 spin_unlock(&khugepaged_mm_lock
);
496 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
497 free_mm_slot(mm_slot
);
499 } else if (mm_slot
) {
501 * This is required to serialize against
502 * khugepaged_test_exit() (which is guaranteed to run
503 * under mmap sem read mode). Stop here (after we
504 * return all pagetables will be destroyed) until
505 * khugepaged has finished working on the pagetables
506 * under the mmap_sem.
508 down_write(&mm
->mmap_sem
);
509 up_write(&mm
->mmap_sem
);
513 static void release_pte_page(struct page
*page
)
515 dec_node_page_state(page
, NR_ISOLATED_ANON
+ page_is_file_lru(page
));
517 putback_lru_page(page
);
520 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
522 while (--_pte
>= pte
) {
523 pte_t pteval
= *_pte
;
524 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
525 release_pte_page(pte_page(pteval
));
529 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
530 unsigned long address
,
533 struct page
*page
= NULL
;
535 int none_or_zero
= 0, result
= 0, referenced
= 0;
536 bool writable
= false;
538 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
539 _pte
++, address
+= PAGE_SIZE
) {
540 pte_t pteval
= *_pte
;
541 if (pte_none(pteval
) || (pte_present(pteval
) &&
542 is_zero_pfn(pte_pfn(pteval
)))) {
543 if (!userfaultfd_armed(vma
) &&
544 ++none_or_zero
<= khugepaged_max_ptes_none
) {
547 result
= SCAN_EXCEED_NONE_PTE
;
551 if (!pte_present(pteval
)) {
552 result
= SCAN_PTE_NON_PRESENT
;
555 page
= vm_normal_page(vma
, address
, pteval
);
556 if (unlikely(!page
)) {
557 result
= SCAN_PAGE_NULL
;
561 /* TODO: teach khugepaged to collapse THP mapped with pte */
562 if (PageCompound(page
)) {
563 result
= SCAN_PAGE_COMPOUND
;
567 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
570 * We can do it before isolate_lru_page because the
571 * page can't be freed from under us. NOTE: PG_lock
572 * is needed to serialize against split_huge_page
573 * when invoked from the VM.
575 if (!trylock_page(page
)) {
576 result
= SCAN_PAGE_LOCK
;
581 * cannot use mapcount: can't collapse if there's a gup pin.
582 * The page must only be referenced by the scanned process
583 * and page swap cache.
585 if (page_count(page
) != 1 + PageSwapCache(page
)) {
587 result
= SCAN_PAGE_COUNT
;
590 if (pte_write(pteval
)) {
593 if (PageSwapCache(page
) &&
594 !reuse_swap_page(page
, NULL
)) {
596 result
= SCAN_SWAP_CACHE_PAGE
;
600 * Page is not in the swap cache. It can be collapsed
606 * Isolate the page to avoid collapsing an hugepage
607 * currently in use by the VM.
609 if (isolate_lru_page(page
)) {
611 result
= SCAN_DEL_PAGE_LRU
;
614 inc_node_page_state(page
,
615 NR_ISOLATED_ANON
+ page_is_file_lru(page
));
616 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
617 VM_BUG_ON_PAGE(PageLRU(page
), page
);
619 /* There should be enough young pte to collapse the page */
620 if (pte_young(pteval
) ||
621 page_is_young(page
) || PageReferenced(page
) ||
622 mmu_notifier_test_young(vma
->vm_mm
, address
))
625 if (likely(writable
)) {
626 if (likely(referenced
)) {
627 result
= SCAN_SUCCEED
;
628 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
629 referenced
, writable
, result
);
633 result
= SCAN_PAGE_RO
;
637 release_pte_pages(pte
, _pte
);
638 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
639 referenced
, writable
, result
);
643 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
644 struct vm_area_struct
*vma
,
645 unsigned long address
,
649 for (_pte
= pte
; _pte
< pte
+ HPAGE_PMD_NR
;
650 _pte
++, page
++, address
+= PAGE_SIZE
) {
651 pte_t pteval
= *_pte
;
652 struct page
*src_page
;
654 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
655 clear_user_highpage(page
, address
);
656 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
657 if (is_zero_pfn(pte_pfn(pteval
))) {
659 * ptl mostly unnecessary.
663 * paravirt calls inside pte_clear here are
666 pte_clear(vma
->vm_mm
, address
, _pte
);
670 src_page
= pte_page(pteval
);
671 copy_user_highpage(page
, src_page
, address
, vma
);
672 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
673 release_pte_page(src_page
);
675 * ptl mostly unnecessary, but preempt has to
676 * be disabled to update the per-cpu stats
677 * inside page_remove_rmap().
681 * paravirt calls inside pte_clear here are
684 pte_clear(vma
->vm_mm
, address
, _pte
);
685 page_remove_rmap(src_page
, false);
687 free_page_and_swap_cache(src_page
);
692 static void khugepaged_alloc_sleep(void)
696 add_wait_queue(&khugepaged_wait
, &wait
);
697 freezable_schedule_timeout_interruptible(
698 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
699 remove_wait_queue(&khugepaged_wait
, &wait
);
702 static int khugepaged_node_load
[MAX_NUMNODES
];
704 static bool khugepaged_scan_abort(int nid
)
709 * If node_reclaim_mode is disabled, then no extra effort is made to
710 * allocate memory locally.
712 if (!node_reclaim_mode
)
715 /* If there is a count for this node already, it must be acceptable */
716 if (khugepaged_node_load
[nid
])
719 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
720 if (!khugepaged_node_load
[i
])
722 if (node_distance(nid
, i
) > node_reclaim_distance
)
728 /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
729 static inline gfp_t
alloc_hugepage_khugepaged_gfpmask(void)
731 return khugepaged_defrag() ? GFP_TRANSHUGE
: GFP_TRANSHUGE_LIGHT
;
735 static int khugepaged_find_target_node(void)
737 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
738 int nid
, target_node
= 0, max_value
= 0;
740 /* find first node with max normal pages hit */
741 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
742 if (khugepaged_node_load
[nid
] > max_value
) {
743 max_value
= khugepaged_node_load
[nid
];
747 /* do some balance if several nodes have the same hit record */
748 if (target_node
<= last_khugepaged_target_node
)
749 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
751 if (max_value
== khugepaged_node_load
[nid
]) {
756 last_khugepaged_target_node
= target_node
;
760 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
762 if (IS_ERR(*hpage
)) {
768 khugepaged_alloc_sleep();
778 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, int node
)
780 VM_BUG_ON_PAGE(*hpage
, *hpage
);
782 *hpage
= __alloc_pages_node(node
, gfp
, HPAGE_PMD_ORDER
);
783 if (unlikely(!*hpage
)) {
784 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
785 *hpage
= ERR_PTR(-ENOMEM
);
789 prep_transhuge_page(*hpage
);
790 count_vm_event(THP_COLLAPSE_ALLOC
);
794 static int khugepaged_find_target_node(void)
799 static inline struct page
*alloc_khugepaged_hugepage(void)
803 page
= alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
806 prep_transhuge_page(page
);
810 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
815 hpage
= alloc_khugepaged_hugepage();
817 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
822 khugepaged_alloc_sleep();
824 count_vm_event(THP_COLLAPSE_ALLOC
);
825 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
830 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
833 *hpage
= khugepaged_alloc_hugepage(wait
);
835 if (unlikely(!*hpage
))
842 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, int node
)
851 * If mmap_sem temporarily dropped, revalidate vma
852 * before taking mmap_sem.
853 * Return 0 if succeeds, otherwise return none-zero
857 static int hugepage_vma_revalidate(struct mm_struct
*mm
, unsigned long address
,
858 struct vm_area_struct
**vmap
)
860 struct vm_area_struct
*vma
;
861 unsigned long hstart
, hend
;
863 if (unlikely(khugepaged_test_exit(mm
)))
864 return SCAN_ANY_PROCESS
;
866 *vmap
= vma
= find_vma(mm
, address
);
868 return SCAN_VMA_NULL
;
870 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
871 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
872 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
873 return SCAN_ADDRESS_RANGE
;
874 if (!hugepage_vma_check(vma
, vma
->vm_flags
))
875 return SCAN_VMA_CHECK
;
880 * Bring missing pages in from swap, to complete THP collapse.
881 * Only done if khugepaged_scan_pmd believes it is worthwhile.
883 * Called and returns without pte mapped or spinlocks held,
884 * but with mmap_sem held to protect against vma changes.
887 static bool __collapse_huge_page_swapin(struct mm_struct
*mm
,
888 struct vm_area_struct
*vma
,
889 unsigned long address
, pmd_t
*pmd
,
894 struct vm_fault vmf
= {
897 .flags
= FAULT_FLAG_ALLOW_RETRY
,
899 .pgoff
= linear_page_index(vma
, address
),
902 /* we only decide to swapin, if there is enough young ptes */
903 if (referenced
< HPAGE_PMD_NR
/2) {
904 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, referenced
, 0);
907 vmf
.pte
= pte_offset_map(pmd
, address
);
908 for (; vmf
.address
< address
+ HPAGE_PMD_NR
*PAGE_SIZE
;
909 vmf
.pte
++, vmf
.address
+= PAGE_SIZE
) {
910 vmf
.orig_pte
= *vmf
.pte
;
911 if (!is_swap_pte(vmf
.orig_pte
))
914 ret
= do_swap_page(&vmf
);
916 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
917 if (ret
& VM_FAULT_RETRY
) {
918 down_read(&mm
->mmap_sem
);
919 if (hugepage_vma_revalidate(mm
, address
, &vmf
.vma
)) {
920 /* vma is no longer available, don't continue to swapin */
921 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, referenced
, 0);
924 /* check if the pmd is still valid */
925 if (mm_find_pmd(mm
, address
) != pmd
) {
926 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, referenced
, 0);
930 if (ret
& VM_FAULT_ERROR
) {
931 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, referenced
, 0);
934 /* pte is unmapped now, we need to map it */
935 vmf
.pte
= pte_offset_map(pmd
, vmf
.address
);
939 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, referenced
, 1);
943 static void collapse_huge_page(struct mm_struct
*mm
,
944 unsigned long address
,
946 int node
, int referenced
)
951 struct page
*new_page
;
952 spinlock_t
*pmd_ptl
, *pte_ptl
;
953 int isolated
= 0, result
= 0;
954 struct mem_cgroup
*memcg
;
955 struct vm_area_struct
*vma
;
956 struct mmu_notifier_range range
;
959 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
961 /* Only allocate from the target node */
962 gfp
= alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE
;
965 * Before allocating the hugepage, release the mmap_sem read lock.
966 * The allocation can take potentially a long time if it involves
967 * sync compaction, and we do not need to hold the mmap_sem during
968 * that. We will recheck the vma after taking it again in write mode.
970 up_read(&mm
->mmap_sem
);
971 new_page
= khugepaged_alloc_page(hpage
, gfp
, node
);
973 result
= SCAN_ALLOC_HUGE_PAGE_FAIL
;
977 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, gfp
, &memcg
, true))) {
978 result
= SCAN_CGROUP_CHARGE_FAIL
;
982 down_read(&mm
->mmap_sem
);
983 result
= hugepage_vma_revalidate(mm
, address
, &vma
);
985 mem_cgroup_cancel_charge(new_page
, memcg
, true);
986 up_read(&mm
->mmap_sem
);
990 pmd
= mm_find_pmd(mm
, address
);
992 result
= SCAN_PMD_NULL
;
993 mem_cgroup_cancel_charge(new_page
, memcg
, true);
994 up_read(&mm
->mmap_sem
);
999 * __collapse_huge_page_swapin always returns with mmap_sem locked.
1000 * If it fails, we release mmap_sem and jump out_nolock.
1001 * Continuing to collapse causes inconsistency.
1003 if (!__collapse_huge_page_swapin(mm
, vma
, address
, pmd
, referenced
)) {
1004 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1005 up_read(&mm
->mmap_sem
);
1009 up_read(&mm
->mmap_sem
);
1011 * Prevent all access to pagetables with the exception of
1012 * gup_fast later handled by the ptep_clear_flush and the VM
1013 * handled by the anon_vma lock + PG_lock.
1015 down_write(&mm
->mmap_sem
);
1016 result
= SCAN_ANY_PROCESS
;
1017 if (!mmget_still_valid(mm
))
1019 result
= hugepage_vma_revalidate(mm
, address
, &vma
);
1022 /* check if the pmd is still valid */
1023 if (mm_find_pmd(mm
, address
) != pmd
)
1026 anon_vma_lock_write(vma
->anon_vma
);
1028 mmu_notifier_range_init(&range
, MMU_NOTIFY_CLEAR
, 0, NULL
, mm
,
1029 address
, address
+ HPAGE_PMD_SIZE
);
1030 mmu_notifier_invalidate_range_start(&range
);
1032 pte
= pte_offset_map(pmd
, address
);
1033 pte_ptl
= pte_lockptr(mm
, pmd
);
1035 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
1037 * After this gup_fast can't run anymore. This also removes
1038 * any huge TLB entry from the CPU so we won't allow
1039 * huge and small TLB entries for the same virtual address
1040 * to avoid the risk of CPU bugs in that area.
1042 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
1043 spin_unlock(pmd_ptl
);
1044 mmu_notifier_invalidate_range_end(&range
);
1047 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
1048 spin_unlock(pte_ptl
);
1050 if (unlikely(!isolated
)) {
1053 BUG_ON(!pmd_none(*pmd
));
1055 * We can only use set_pmd_at when establishing
1056 * hugepmds and never for establishing regular pmds that
1057 * points to regular pagetables. Use pmd_populate for that
1059 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
1060 spin_unlock(pmd_ptl
);
1061 anon_vma_unlock_write(vma
->anon_vma
);
1067 * All pages are isolated and locked so anon_vma rmap
1068 * can't run anymore.
1070 anon_vma_unlock_write(vma
->anon_vma
);
1072 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
1074 __SetPageUptodate(new_page
);
1075 pgtable
= pmd_pgtable(_pmd
);
1077 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1078 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
1081 * spin_lock() below is not the equivalent of smp_wmb(), so
1082 * this is needed to avoid the copy_huge_page writes to become
1083 * visible after the set_pmd_at() write.
1088 BUG_ON(!pmd_none(*pmd
));
1089 page_add_new_anon_rmap(new_page
, vma
, address
, true);
1090 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1091 count_memcg_events(memcg
, THP_COLLAPSE_ALLOC
, 1);
1092 lru_cache_add_active_or_unevictable(new_page
, vma
);
1093 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
1094 set_pmd_at(mm
, address
, pmd
, _pmd
);
1095 update_mmu_cache_pmd(vma
, address
, pmd
);
1096 spin_unlock(pmd_ptl
);
1100 khugepaged_pages_collapsed
++;
1101 result
= SCAN_SUCCEED
;
1103 up_write(&mm
->mmap_sem
);
1105 trace_mm_collapse_huge_page(mm
, isolated
, result
);
1108 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1112 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
1113 struct vm_area_struct
*vma
,
1114 unsigned long address
,
1115 struct page
**hpage
)
1119 int ret
= 0, none_or_zero
= 0, result
= 0, referenced
= 0;
1120 struct page
*page
= NULL
;
1121 unsigned long _address
;
1123 int node
= NUMA_NO_NODE
, unmapped
= 0;
1124 bool writable
= false;
1126 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
1128 pmd
= mm_find_pmd(mm
, address
);
1130 result
= SCAN_PMD_NULL
;
1134 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
1135 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
1136 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
1137 _pte
++, _address
+= PAGE_SIZE
) {
1138 pte_t pteval
= *_pte
;
1139 if (is_swap_pte(pteval
)) {
1140 if (++unmapped
<= khugepaged_max_ptes_swap
) {
1142 * Always be strict with uffd-wp
1143 * enabled swap entries. Please see
1144 * comment below for pte_uffd_wp().
1146 if (pte_swp_uffd_wp(pteval
)) {
1147 result
= SCAN_PTE_UFFD_WP
;
1152 result
= SCAN_EXCEED_SWAP_PTE
;
1156 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
1157 if (!userfaultfd_armed(vma
) &&
1158 ++none_or_zero
<= khugepaged_max_ptes_none
) {
1161 result
= SCAN_EXCEED_NONE_PTE
;
1165 if (!pte_present(pteval
)) {
1166 result
= SCAN_PTE_NON_PRESENT
;
1169 if (pte_uffd_wp(pteval
)) {
1171 * Don't collapse the page if any of the small
1172 * PTEs are armed with uffd write protection.
1173 * Here we can also mark the new huge pmd as
1174 * write protected if any of the small ones is
1175 * marked but that could bring uknown
1176 * userfault messages that falls outside of
1177 * the registered range. So, just be simple.
1179 result
= SCAN_PTE_UFFD_WP
;
1182 if (pte_write(pteval
))
1185 page
= vm_normal_page(vma
, _address
, pteval
);
1186 if (unlikely(!page
)) {
1187 result
= SCAN_PAGE_NULL
;
1191 /* TODO: teach khugepaged to collapse THP mapped with pte */
1192 if (PageCompound(page
)) {
1193 result
= SCAN_PAGE_COMPOUND
;
1198 * Record which node the original page is from and save this
1199 * information to khugepaged_node_load[].
1200 * Khupaged will allocate hugepage from the node has the max
1203 node
= page_to_nid(page
);
1204 if (khugepaged_scan_abort(node
)) {
1205 result
= SCAN_SCAN_ABORT
;
1208 khugepaged_node_load
[node
]++;
1209 if (!PageLRU(page
)) {
1210 result
= SCAN_PAGE_LRU
;
1213 if (PageLocked(page
)) {
1214 result
= SCAN_PAGE_LOCK
;
1217 if (!PageAnon(page
)) {
1218 result
= SCAN_PAGE_ANON
;
1223 * cannot use mapcount: can't collapse if there's a gup pin.
1224 * The page must only be referenced by the scanned process
1225 * and page swap cache.
1227 if (page_count(page
) != 1 + PageSwapCache(page
)) {
1228 result
= SCAN_PAGE_COUNT
;
1231 if (pte_young(pteval
) ||
1232 page_is_young(page
) || PageReferenced(page
) ||
1233 mmu_notifier_test_young(vma
->vm_mm
, address
))
1238 result
= SCAN_SUCCEED
;
1241 result
= SCAN_LACK_REFERENCED_PAGE
;
1244 result
= SCAN_PAGE_RO
;
1247 pte_unmap_unlock(pte
, ptl
);
1249 node
= khugepaged_find_target_node();
1250 /* collapse_huge_page will return with the mmap_sem released */
1251 collapse_huge_page(mm
, address
, hpage
, node
, referenced
);
1254 trace_mm_khugepaged_scan_pmd(mm
, page
, writable
, referenced
,
1255 none_or_zero
, result
, unmapped
);
1259 static void collect_mm_slot(struct mm_slot
*mm_slot
)
1261 struct mm_struct
*mm
= mm_slot
->mm
;
1263 lockdep_assert_held(&khugepaged_mm_lock
);
1265 if (khugepaged_test_exit(mm
)) {
1267 hash_del(&mm_slot
->hash
);
1268 list_del(&mm_slot
->mm_node
);
1271 * Not strictly needed because the mm exited already.
1273 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1276 /* khugepaged_mm_lock actually not necessary for the below */
1277 free_mm_slot(mm_slot
);
1284 * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1285 * khugepaged should try to collapse the page table.
1287 static int khugepaged_add_pte_mapped_thp(struct mm_struct
*mm
,
1290 struct mm_slot
*mm_slot
;
1292 VM_BUG_ON(addr
& ~HPAGE_PMD_MASK
);
1294 spin_lock(&khugepaged_mm_lock
);
1295 mm_slot
= get_mm_slot(mm
);
1296 if (likely(mm_slot
&& mm_slot
->nr_pte_mapped_thp
< MAX_PTE_MAPPED_THP
))
1297 mm_slot
->pte_mapped_thp
[mm_slot
->nr_pte_mapped_thp
++] = addr
;
1298 spin_unlock(&khugepaged_mm_lock
);
1303 * Try to collapse a pte-mapped THP for mm at address haddr.
1305 * This function checks whether all the PTEs in the PMD are pointing to the
1306 * right THP. If so, retract the page table so the THP can refault in with
1309 void collapse_pte_mapped_thp(struct mm_struct
*mm
, unsigned long addr
)
1311 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1312 struct vm_area_struct
*vma
= find_vma(mm
, haddr
);
1313 struct page
*hpage
= NULL
;
1314 pte_t
*start_pte
, *pte
;
1320 if (!vma
|| !vma
->vm_file
||
1321 vma
->vm_start
> haddr
|| vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
)
1325 * This vm_flags may not have VM_HUGEPAGE if the page was not
1326 * collapsed by this mm. But we can still collapse if the page is
1327 * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
1328 * will not fail the vma for missing VM_HUGEPAGE
1330 if (!hugepage_vma_check(vma
, vma
->vm_flags
| VM_HUGEPAGE
))
1333 pmd
= mm_find_pmd(mm
, haddr
);
1337 start_pte
= pte_offset_map_lock(mm
, pmd
, haddr
, &ptl
);
1339 /* step 1: check all mapped PTEs are to the right huge page */
1340 for (i
= 0, addr
= haddr
, pte
= start_pte
;
1341 i
< HPAGE_PMD_NR
; i
++, addr
+= PAGE_SIZE
, pte
++) {
1344 /* empty pte, skip */
1348 /* page swapped out, abort */
1349 if (!pte_present(*pte
))
1352 page
= vm_normal_page(vma
, addr
, *pte
);
1354 if (!page
|| !PageCompound(page
))
1358 hpage
= compound_head(page
);
1360 * The mapping of the THP should not change.
1362 * Note that uprobe, debugger, or MAP_PRIVATE may
1363 * change the page table, but the new page will
1364 * not pass PageCompound() check.
1366 if (WARN_ON(hpage
->mapping
!= vma
->vm_file
->f_mapping
))
1371 * Confirm the page maps to the correct subpage.
1373 * Note that uprobe, debugger, or MAP_PRIVATE may change
1374 * the page table, but the new page will not pass
1375 * PageCompound() check.
1377 if (WARN_ON(hpage
+ i
!= page
))
1382 /* step 2: adjust rmap */
1383 for (i
= 0, addr
= haddr
, pte
= start_pte
;
1384 i
< HPAGE_PMD_NR
; i
++, addr
+= PAGE_SIZE
, pte
++) {
1389 page
= vm_normal_page(vma
, addr
, *pte
);
1390 page_remove_rmap(page
, false);
1393 pte_unmap_unlock(start_pte
, ptl
);
1395 /* step 3: set proper refcount and mm_counters. */
1397 page_ref_sub(hpage
, count
);
1398 add_mm_counter(vma
->vm_mm
, mm_counter_file(hpage
), -count
);
1401 /* step 4: collapse pmd */
1402 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1403 _pmd
= pmdp_collapse_flush(vma
, addr
, pmd
);
1406 pte_free(mm
, pmd_pgtable(_pmd
));
1410 pte_unmap_unlock(start_pte
, ptl
);
1413 static int khugepaged_collapse_pte_mapped_thps(struct mm_slot
*mm_slot
)
1415 struct mm_struct
*mm
= mm_slot
->mm
;
1418 if (likely(mm_slot
->nr_pte_mapped_thp
== 0))
1421 if (!down_write_trylock(&mm
->mmap_sem
))
1424 if (unlikely(khugepaged_test_exit(mm
)))
1427 for (i
= 0; i
< mm_slot
->nr_pte_mapped_thp
; i
++)
1428 collapse_pte_mapped_thp(mm
, mm_slot
->pte_mapped_thp
[i
]);
1431 mm_slot
->nr_pte_mapped_thp
= 0;
1432 up_write(&mm
->mmap_sem
);
1436 static void retract_page_tables(struct address_space
*mapping
, pgoff_t pgoff
)
1438 struct vm_area_struct
*vma
;
1442 i_mmap_lock_write(mapping
);
1443 vma_interval_tree_foreach(vma
, &mapping
->i_mmap
, pgoff
, pgoff
) {
1445 * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1446 * got written to. These VMAs are likely not worth investing
1447 * down_write(mmap_sem) as PMD-mapping is likely to be split
1450 * Not that vma->anon_vma check is racy: it can be set up after
1451 * the check but before we took mmap_sem by the fault path.
1452 * But page lock would prevent establishing any new ptes of the
1453 * page, so we are safe.
1455 * An alternative would be drop the check, but check that page
1456 * table is clear before calling pmdp_collapse_flush() under
1457 * ptl. It has higher chance to recover THP for the VMA, but
1458 * has higher cost too.
1462 addr
= vma
->vm_start
+ ((pgoff
- vma
->vm_pgoff
) << PAGE_SHIFT
);
1463 if (addr
& ~HPAGE_PMD_MASK
)
1465 if (vma
->vm_end
< addr
+ HPAGE_PMD_SIZE
)
1467 pmd
= mm_find_pmd(vma
->vm_mm
, addr
);
1471 * We need exclusive mmap_sem to retract page table.
1473 * We use trylock due to lock inversion: we need to acquire
1474 * mmap_sem while holding page lock. Fault path does it in
1475 * reverse order. Trylock is a way to avoid deadlock.
1477 if (down_write_trylock(&vma
->vm_mm
->mmap_sem
)) {
1478 spinlock_t
*ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1479 /* assume page table is clear */
1480 _pmd
= pmdp_collapse_flush(vma
, addr
, pmd
);
1482 up_write(&vma
->vm_mm
->mmap_sem
);
1483 mm_dec_nr_ptes(vma
->vm_mm
);
1484 pte_free(vma
->vm_mm
, pmd_pgtable(_pmd
));
1486 /* Try again later */
1487 khugepaged_add_pte_mapped_thp(vma
->vm_mm
, addr
);
1490 i_mmap_unlock_write(mapping
);
1494 * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
1496 * Basic scheme is simple, details are more complex:
1497 * - allocate and lock a new huge page;
1498 * - scan page cache replacing old pages with the new one
1499 * + swap/gup in pages if necessary;
1501 * + keep old pages around in case rollback is required;
1502 * - if replacing succeeds:
1505 * + unlock huge page;
1506 * - if replacing failed;
1507 * + put all pages back and unfreeze them;
1508 * + restore gaps in the page cache;
1509 * + unlock and free huge page;
1511 static void collapse_file(struct mm_struct
*mm
,
1512 struct file
*file
, pgoff_t start
,
1513 struct page
**hpage
, int node
)
1515 struct address_space
*mapping
= file
->f_mapping
;
1517 struct page
*new_page
;
1518 struct mem_cgroup
*memcg
;
1519 pgoff_t index
, end
= start
+ HPAGE_PMD_NR
;
1520 LIST_HEAD(pagelist
);
1521 XA_STATE_ORDER(xas
, &mapping
->i_pages
, start
, HPAGE_PMD_ORDER
);
1522 int nr_none
= 0, result
= SCAN_SUCCEED
;
1523 bool is_shmem
= shmem_file(file
);
1525 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS
) && !is_shmem
);
1526 VM_BUG_ON(start
& (HPAGE_PMD_NR
- 1));
1528 /* Only allocate from the target node */
1529 gfp
= alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE
;
1531 new_page
= khugepaged_alloc_page(hpage
, gfp
, node
);
1533 result
= SCAN_ALLOC_HUGE_PAGE_FAIL
;
1537 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, gfp
, &memcg
, true))) {
1538 result
= SCAN_CGROUP_CHARGE_FAIL
;
1542 /* This will be less messy when we use multi-index entries */
1545 xas_create_range(&xas
);
1546 if (!xas_error(&xas
))
1548 xas_unlock_irq(&xas
);
1549 if (!xas_nomem(&xas
, GFP_KERNEL
)) {
1550 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1556 __SetPageLocked(new_page
);
1558 __SetPageSwapBacked(new_page
);
1559 new_page
->index
= start
;
1560 new_page
->mapping
= mapping
;
1563 * At this point the new_page is locked and not up-to-date.
1564 * It's safe to insert it into the page cache, because nobody would
1565 * be able to map it or use it in another way until we unlock it.
1568 xas_set(&xas
, start
);
1569 for (index
= start
; index
< end
; index
++) {
1570 struct page
*page
= xas_next(&xas
);
1572 VM_BUG_ON(index
!= xas
.xa_index
);
1576 * Stop if extent has been truncated or
1577 * hole-punched, and is now completely
1580 if (index
== start
) {
1581 if (!xas_next_entry(&xas
, end
- 1)) {
1582 result
= SCAN_TRUNCATED
;
1585 xas_set(&xas
, index
);
1587 if (!shmem_charge(mapping
->host
, 1)) {
1591 xas_store(&xas
, new_page
);
1596 if (xa_is_value(page
) || !PageUptodate(page
)) {
1597 xas_unlock_irq(&xas
);
1598 /* swap in or instantiate fallocated page */
1599 if (shmem_getpage(mapping
->host
, index
, &page
,
1604 } else if (trylock_page(page
)) {
1606 xas_unlock_irq(&xas
);
1608 result
= SCAN_PAGE_LOCK
;
1611 } else { /* !is_shmem */
1612 if (!page
|| xa_is_value(page
)) {
1613 xas_unlock_irq(&xas
);
1614 page_cache_sync_readahead(mapping
, &file
->f_ra
,
1617 /* drain pagevecs to help isolate_lru_page() */
1619 page
= find_lock_page(mapping
, index
);
1620 if (unlikely(page
== NULL
)) {
1624 } else if (PageDirty(page
)) {
1626 * khugepaged only works on read-only fd,
1627 * so this page is dirty because it hasn't
1628 * been flushed since first write. There
1629 * won't be new dirty pages.
1631 * Trigger async flush here and hope the
1632 * writeback is done when khugepaged
1633 * revisits this page.
1635 * This is a one-off situation. We are not
1636 * forcing writeback in loop.
1638 xas_unlock_irq(&xas
);
1639 filemap_flush(mapping
);
1642 } else if (trylock_page(page
)) {
1644 xas_unlock_irq(&xas
);
1646 result
= SCAN_PAGE_LOCK
;
1652 * The page must be locked, so we can drop the i_pages lock
1653 * without racing with truncate.
1655 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
1657 /* make sure the page is up to date */
1658 if (unlikely(!PageUptodate(page
))) {
1664 * If file was truncated then extended, or hole-punched, before
1665 * we locked the first page, then a THP might be there already.
1667 if (PageTransCompound(page
)) {
1668 result
= SCAN_PAGE_COMPOUND
;
1672 if (page_mapping(page
) != mapping
) {
1673 result
= SCAN_TRUNCATED
;
1677 if (!is_shmem
&& PageDirty(page
)) {
1679 * khugepaged only works on read-only fd, so this
1680 * page is dirty because it hasn't been flushed
1681 * since first write.
1687 if (isolate_lru_page(page
)) {
1688 result
= SCAN_DEL_PAGE_LRU
;
1692 if (page_has_private(page
) &&
1693 !try_to_release_page(page
, GFP_KERNEL
)) {
1694 result
= SCAN_PAGE_HAS_PRIVATE
;
1695 putback_lru_page(page
);
1699 if (page_mapped(page
))
1700 unmap_mapping_pages(mapping
, index
, 1, false);
1703 xas_set(&xas
, index
);
1705 VM_BUG_ON_PAGE(page
!= xas_load(&xas
), page
);
1706 VM_BUG_ON_PAGE(page_mapped(page
), page
);
1709 * The page is expected to have page_count() == 3:
1710 * - we hold a pin on it;
1711 * - one reference from page cache;
1712 * - one from isolate_lru_page;
1714 if (!page_ref_freeze(page
, 3)) {
1715 result
= SCAN_PAGE_COUNT
;
1716 xas_unlock_irq(&xas
);
1717 putback_lru_page(page
);
1722 * Add the page to the list to be able to undo the collapse if
1723 * something go wrong.
1725 list_add_tail(&page
->lru
, &pagelist
);
1727 /* Finally, replace with the new page. */
1728 xas_store(&xas
, new_page
);
1737 __inc_node_page_state(new_page
, NR_SHMEM_THPS
);
1739 __inc_node_page_state(new_page
, NR_FILE_THPS
);
1740 filemap_nr_thps_inc(mapping
);
1744 struct zone
*zone
= page_zone(new_page
);
1746 __mod_node_page_state(zone
->zone_pgdat
, NR_FILE_PAGES
, nr_none
);
1748 __mod_node_page_state(zone
->zone_pgdat
,
1753 xas_unlock_irq(&xas
);
1756 if (result
== SCAN_SUCCEED
) {
1757 struct page
*page
, *tmp
;
1760 * Replacing old pages with new one has succeeded, now we
1761 * need to copy the content and free the old pages.
1764 list_for_each_entry_safe(page
, tmp
, &pagelist
, lru
) {
1765 while (index
< page
->index
) {
1766 clear_highpage(new_page
+ (index
% HPAGE_PMD_NR
));
1769 copy_highpage(new_page
+ (page
->index
% HPAGE_PMD_NR
),
1771 list_del(&page
->lru
);
1772 page
->mapping
= NULL
;
1773 page_ref_unfreeze(page
, 1);
1774 ClearPageActive(page
);
1775 ClearPageUnevictable(page
);
1780 while (index
< end
) {
1781 clear_highpage(new_page
+ (index
% HPAGE_PMD_NR
));
1785 SetPageUptodate(new_page
);
1786 page_ref_add(new_page
, HPAGE_PMD_NR
- 1);
1787 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1790 set_page_dirty(new_page
);
1791 lru_cache_add_anon(new_page
);
1793 lru_cache_add_file(new_page
);
1795 count_memcg_events(memcg
, THP_COLLAPSE_ALLOC
, 1);
1798 * Remove pte page tables, so we can re-fault the page as huge.
1800 retract_page_tables(mapping
, start
);
1803 khugepaged_pages_collapsed
++;
1807 /* Something went wrong: roll back page cache changes */
1809 mapping
->nrpages
-= nr_none
;
1812 shmem_uncharge(mapping
->host
, nr_none
);
1814 xas_set(&xas
, start
);
1815 xas_for_each(&xas
, page
, end
- 1) {
1816 page
= list_first_entry_or_null(&pagelist
,
1818 if (!page
|| xas
.xa_index
< page
->index
) {
1822 /* Put holes back where they were */
1823 xas_store(&xas
, NULL
);
1827 VM_BUG_ON_PAGE(page
->index
!= xas
.xa_index
, page
);
1829 /* Unfreeze the page. */
1830 list_del(&page
->lru
);
1831 page_ref_unfreeze(page
, 2);
1832 xas_store(&xas
, page
);
1834 xas_unlock_irq(&xas
);
1836 putback_lru_page(page
);
1840 xas_unlock_irq(&xas
);
1842 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1843 new_page
->mapping
= NULL
;
1846 unlock_page(new_page
);
1848 VM_BUG_ON(!list_empty(&pagelist
));
1849 /* TODO: tracepoints */
1852 static void khugepaged_scan_file(struct mm_struct
*mm
,
1853 struct file
*file
, pgoff_t start
, struct page
**hpage
)
1855 struct page
*page
= NULL
;
1856 struct address_space
*mapping
= file
->f_mapping
;
1857 XA_STATE(xas
, &mapping
->i_pages
, start
);
1859 int node
= NUMA_NO_NODE
;
1860 int result
= SCAN_SUCCEED
;
1864 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
1866 xas_for_each(&xas
, page
, start
+ HPAGE_PMD_NR
- 1) {
1867 if (xas_retry(&xas
, page
))
1870 if (xa_is_value(page
)) {
1871 if (++swap
> khugepaged_max_ptes_swap
) {
1872 result
= SCAN_EXCEED_SWAP_PTE
;
1878 if (PageTransCompound(page
)) {
1879 result
= SCAN_PAGE_COMPOUND
;
1883 node
= page_to_nid(page
);
1884 if (khugepaged_scan_abort(node
)) {
1885 result
= SCAN_SCAN_ABORT
;
1888 khugepaged_node_load
[node
]++;
1890 if (!PageLRU(page
)) {
1891 result
= SCAN_PAGE_LRU
;
1895 if (page_count(page
) !=
1896 1 + page_mapcount(page
) + page_has_private(page
)) {
1897 result
= SCAN_PAGE_COUNT
;
1902 * We probably should check if the page is referenced here, but
1903 * nobody would transfer pte_young() to PageReferenced() for us.
1904 * And rmap walk here is just too costly...
1909 if (need_resched()) {
1916 if (result
== SCAN_SUCCEED
) {
1917 if (present
< HPAGE_PMD_NR
- khugepaged_max_ptes_none
) {
1918 result
= SCAN_EXCEED_NONE_PTE
;
1920 node
= khugepaged_find_target_node();
1921 collapse_file(mm
, file
, start
, hpage
, node
);
1925 /* TODO: tracepoints */
1928 static void khugepaged_scan_file(struct mm_struct
*mm
,
1929 struct file
*file
, pgoff_t start
, struct page
**hpage
)
1934 static int khugepaged_collapse_pte_mapped_thps(struct mm_slot
*mm_slot
)
1940 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
1941 struct page
**hpage
)
1942 __releases(&khugepaged_mm_lock
)
1943 __acquires(&khugepaged_mm_lock
)
1945 struct mm_slot
*mm_slot
;
1946 struct mm_struct
*mm
;
1947 struct vm_area_struct
*vma
;
1951 lockdep_assert_held(&khugepaged_mm_lock
);
1953 if (khugepaged_scan
.mm_slot
)
1954 mm_slot
= khugepaged_scan
.mm_slot
;
1956 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
1957 struct mm_slot
, mm_node
);
1958 khugepaged_scan
.address
= 0;
1959 khugepaged_scan
.mm_slot
= mm_slot
;
1961 spin_unlock(&khugepaged_mm_lock
);
1962 khugepaged_collapse_pte_mapped_thps(mm_slot
);
1966 * Don't wait for semaphore (to avoid long wait times). Just move to
1967 * the next mm on the list.
1970 if (unlikely(!down_read_trylock(&mm
->mmap_sem
)))
1971 goto breakouterloop_mmap_sem
;
1972 if (likely(!khugepaged_test_exit(mm
)))
1973 vma
= find_vma(mm
, khugepaged_scan
.address
);
1976 for (; vma
; vma
= vma
->vm_next
) {
1977 unsigned long hstart
, hend
;
1980 if (unlikely(khugepaged_test_exit(mm
))) {
1984 if (!hugepage_vma_check(vma
, vma
->vm_flags
)) {
1989 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1990 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1993 if (khugepaged_scan
.address
> hend
)
1995 if (khugepaged_scan
.address
< hstart
)
1996 khugepaged_scan
.address
= hstart
;
1997 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
1998 if (shmem_file(vma
->vm_file
) && !shmem_huge_enabled(vma
))
2001 while (khugepaged_scan
.address
< hend
) {
2004 if (unlikely(khugepaged_test_exit(mm
)))
2005 goto breakouterloop
;
2007 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2008 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2010 if (IS_ENABLED(CONFIG_SHMEM
) && vma
->vm_file
) {
2011 struct file
*file
= get_file(vma
->vm_file
);
2012 pgoff_t pgoff
= linear_page_index(vma
,
2013 khugepaged_scan
.address
);
2015 up_read(&mm
->mmap_sem
);
2017 khugepaged_scan_file(mm
, file
, pgoff
, hpage
);
2020 ret
= khugepaged_scan_pmd(mm
, vma
,
2021 khugepaged_scan
.address
,
2024 /* move to next address */
2025 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2026 progress
+= HPAGE_PMD_NR
;
2028 /* we released mmap_sem so break loop */
2029 goto breakouterloop_mmap_sem
;
2030 if (progress
>= pages
)
2031 goto breakouterloop
;
2035 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2036 breakouterloop_mmap_sem
:
2038 spin_lock(&khugepaged_mm_lock
);
2039 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2041 * Release the current mm_slot if this mm is about to die, or
2042 * if we scanned all vmas of this mm.
2044 if (khugepaged_test_exit(mm
) || !vma
) {
2046 * Make sure that if mm_users is reaching zero while
2047 * khugepaged runs here, khugepaged_exit will find
2048 * mm_slot not pointing to the exiting mm.
2050 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2051 khugepaged_scan
.mm_slot
= list_entry(
2052 mm_slot
->mm_node
.next
,
2053 struct mm_slot
, mm_node
);
2054 khugepaged_scan
.address
= 0;
2056 khugepaged_scan
.mm_slot
= NULL
;
2057 khugepaged_full_scans
++;
2060 collect_mm_slot(mm_slot
);
2066 static int khugepaged_has_work(void)
2068 return !list_empty(&khugepaged_scan
.mm_head
) &&
2069 khugepaged_enabled();
2072 static int khugepaged_wait_event(void)
2074 return !list_empty(&khugepaged_scan
.mm_head
) ||
2075 kthread_should_stop();
2078 static void khugepaged_do_scan(void)
2080 struct page
*hpage
= NULL
;
2081 unsigned int progress
= 0, pass_through_head
= 0;
2082 unsigned int pages
= khugepaged_pages_to_scan
;
2085 barrier(); /* write khugepaged_pages_to_scan to local stack */
2087 while (progress
< pages
) {
2088 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2093 if (unlikely(kthread_should_stop() || try_to_freeze()))
2096 spin_lock(&khugepaged_mm_lock
);
2097 if (!khugepaged_scan
.mm_slot
)
2098 pass_through_head
++;
2099 if (khugepaged_has_work() &&
2100 pass_through_head
< 2)
2101 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2105 spin_unlock(&khugepaged_mm_lock
);
2108 if (!IS_ERR_OR_NULL(hpage
))
2112 static bool khugepaged_should_wakeup(void)
2114 return kthread_should_stop() ||
2115 time_after_eq(jiffies
, khugepaged_sleep_expire
);
2118 static void khugepaged_wait_work(void)
2120 if (khugepaged_has_work()) {
2121 const unsigned long scan_sleep_jiffies
=
2122 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
);
2124 if (!scan_sleep_jiffies
)
2127 khugepaged_sleep_expire
= jiffies
+ scan_sleep_jiffies
;
2128 wait_event_freezable_timeout(khugepaged_wait
,
2129 khugepaged_should_wakeup(),
2130 scan_sleep_jiffies
);
2134 if (khugepaged_enabled())
2135 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2138 static int khugepaged(void *none
)
2140 struct mm_slot
*mm_slot
;
2143 set_user_nice(current
, MAX_NICE
);
2145 while (!kthread_should_stop()) {
2146 khugepaged_do_scan();
2147 khugepaged_wait_work();
2150 spin_lock(&khugepaged_mm_lock
);
2151 mm_slot
= khugepaged_scan
.mm_slot
;
2152 khugepaged_scan
.mm_slot
= NULL
;
2154 collect_mm_slot(mm_slot
);
2155 spin_unlock(&khugepaged_mm_lock
);
2159 static void set_recommended_min_free_kbytes(void)
2163 unsigned long recommended_min
;
2165 for_each_populated_zone(zone
) {
2167 * We don't need to worry about fragmentation of
2168 * ZONE_MOVABLE since it only has movable pages.
2170 if (zone_idx(zone
) > gfp_zone(GFP_USER
))
2176 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
2177 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
2180 * Make sure that on average at least two pageblocks are almost free
2181 * of another type, one for a migratetype to fall back to and a
2182 * second to avoid subsequent fallbacks of other types There are 3
2183 * MIGRATE_TYPES we care about.
2185 recommended_min
+= pageblock_nr_pages
* nr_zones
*
2186 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
2188 /* don't ever allow to reserve more than 5% of the lowmem */
2189 recommended_min
= min(recommended_min
,
2190 (unsigned long) nr_free_buffer_pages() / 20);
2191 recommended_min
<<= (PAGE_SHIFT
-10);
2193 if (recommended_min
> min_free_kbytes
) {
2194 if (user_min_free_kbytes
>= 0)
2195 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
2196 min_free_kbytes
, recommended_min
);
2198 min_free_kbytes
= recommended_min
;
2200 setup_per_zone_wmarks();
2203 int start_stop_khugepaged(void)
2205 static struct task_struct
*khugepaged_thread __read_mostly
;
2206 static DEFINE_MUTEX(khugepaged_mutex
);
2209 mutex_lock(&khugepaged_mutex
);
2210 if (khugepaged_enabled()) {
2211 if (!khugepaged_thread
)
2212 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
2214 if (IS_ERR(khugepaged_thread
)) {
2215 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
2216 err
= PTR_ERR(khugepaged_thread
);
2217 khugepaged_thread
= NULL
;
2221 if (!list_empty(&khugepaged_scan
.mm_head
))
2222 wake_up_interruptible(&khugepaged_wait
);
2224 set_recommended_min_free_kbytes();
2225 } else if (khugepaged_thread
) {
2226 kthread_stop(khugepaged_thread
);
2227 khugepaged_thread
= NULL
;
2230 mutex_unlock(&khugepaged_mutex
);