1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Red Hat, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/sched.h>
10 #include <linux/sched/mm.h>
11 #include <linux/sched/coredump.h>
12 #include <linux/sched/numa_balancing.h>
13 #include <linux/highmem.h>
14 #include <linux/hugetlb.h>
15 #include <linux/mmu_notifier.h>
16 #include <linux/rmap.h>
17 #include <linux/swap.h>
18 #include <linux/shrinker.h>
19 #include <linux/mm_inline.h>
20 #include <linux/swapops.h>
21 #include <linux/dax.h>
22 #include <linux/khugepaged.h>
23 #include <linux/freezer.h>
24 #include <linux/pfn_t.h>
25 #include <linux/mman.h>
26 #include <linux/memremap.h>
27 #include <linux/pagemap.h>
28 #include <linux/debugfs.h>
29 #include <linux/migrate.h>
30 #include <linux/hashtable.h>
31 #include <linux/userfaultfd_k.h>
32 #include <linux/page_idle.h>
33 #include <linux/shmem_fs.h>
34 #include <linux/oom.h>
35 #include <linux/numa.h>
36 #include <linux/page_owner.h>
37 #include <linux/sched/sysctl.h>
40 #include <asm/pgalloc.h>
44 * By default, transparent hugepage support is disabled in order to avoid
45 * risking an increased memory footprint for applications that are not
46 * guaranteed to benefit from it. When transparent hugepage support is
47 * enabled, it is for all mappings, and khugepaged scans all mappings.
48 * Defrag is invoked by khugepaged hugepage allocations and by page faults
49 * for all hugepage allocations.
51 unsigned long transparent_hugepage_flags __read_mostly =
52 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
53 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
55 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
56 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
58 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
59 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
60 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
62 static struct shrinker deferred_split_shrinker;
64 static atomic_t huge_zero_refcount;
65 struct page *huge_zero_page __read_mostly;
66 unsigned long huge_zero_pfn __read_mostly = ~0UL;
68 static inline bool file_thp_enabled(struct vm_area_struct *vma)
70 return transhuge_vma_enabled(vma, vma->vm_flags) && vma->vm_file &&
71 !inode_is_open_for_write(vma->vm_file->f_inode) &&
72 (vma->vm_flags & VM_EXEC);
75 bool transparent_hugepage_active(struct vm_area_struct *vma)
77 /* The addr is used to check if the vma size fits */
78 unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
80 if (!transhuge_vma_suitable(vma, addr))
82 if (vma_is_anonymous(vma))
83 return __transparent_hugepage_enabled(vma);
84 if (vma_is_shmem(vma))
85 return shmem_huge_enabled(vma);
86 if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS))
87 return file_thp_enabled(vma);
92 static bool get_huge_zero_page(void)
94 struct page *zero_page;
96 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
99 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
102 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
105 count_vm_event(THP_ZERO_PAGE_ALLOC);
107 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
109 __free_pages(zero_page, compound_order(zero_page));
112 WRITE_ONCE(huge_zero_pfn, page_to_pfn(zero_page));
114 /* We take additional reference here. It will be put back by shrinker */
115 atomic_set(&huge_zero_refcount, 2);
120 static void put_huge_zero_page(void)
123 * Counter should never go to zero here. Only shrinker can put
126 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
129 struct page *mm_get_huge_zero_page(struct mm_struct *mm)
131 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
132 return READ_ONCE(huge_zero_page);
134 if (!get_huge_zero_page())
137 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
138 put_huge_zero_page();
140 return READ_ONCE(huge_zero_page);
143 void mm_put_huge_zero_page(struct mm_struct *mm)
145 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
146 put_huge_zero_page();
149 static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
150 struct shrink_control *sc)
152 /* we can free zero page only if last reference remains */
153 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
156 static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
157 struct shrink_control *sc)
159 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
160 struct page *zero_page = xchg(&huge_zero_page, NULL);
161 BUG_ON(zero_page == NULL);
162 WRITE_ONCE(huge_zero_pfn, ~0UL);
163 __free_pages(zero_page, compound_order(zero_page));
170 static struct shrinker huge_zero_page_shrinker = {
171 .count_objects = shrink_huge_zero_page_count,
172 .scan_objects = shrink_huge_zero_page_scan,
173 .seeks = DEFAULT_SEEKS,
177 static ssize_t enabled_show(struct kobject *kobj,
178 struct kobj_attribute *attr, char *buf)
182 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
183 output = "[always] madvise never";
184 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
185 &transparent_hugepage_flags))
186 output = "always [madvise] never";
188 output = "always madvise [never]";
190 return sysfs_emit(buf, "%s\n", output);
193 static ssize_t enabled_store(struct kobject *kobj,
194 struct kobj_attribute *attr,
195 const char *buf, size_t count)
199 if (sysfs_streq(buf, "always")) {
200 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
201 set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
202 } else if (sysfs_streq(buf, "madvise")) {
203 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
204 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
205 } else if (sysfs_streq(buf, "never")) {
206 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
207 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
212 int err = start_stop_khugepaged();
218 static struct kobj_attribute enabled_attr =
219 __ATTR(enabled, 0644, enabled_show, enabled_store);
221 ssize_t single_hugepage_flag_show(struct kobject *kobj,
222 struct kobj_attribute *attr, char *buf,
223 enum transparent_hugepage_flag flag)
225 return sysfs_emit(buf, "%d\n",
226 !!test_bit(flag, &transparent_hugepage_flags));
229 ssize_t single_hugepage_flag_store(struct kobject *kobj,
230 struct kobj_attribute *attr,
231 const char *buf, size_t count,
232 enum transparent_hugepage_flag flag)
237 ret = kstrtoul(buf, 10, &value);
244 set_bit(flag, &transparent_hugepage_flags);
246 clear_bit(flag, &transparent_hugepage_flags);
251 static ssize_t defrag_show(struct kobject *kobj,
252 struct kobj_attribute *attr, char *buf)
256 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
257 &transparent_hugepage_flags))
258 output = "[always] defer defer+madvise madvise never";
259 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
260 &transparent_hugepage_flags))
261 output = "always [defer] defer+madvise madvise never";
262 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
263 &transparent_hugepage_flags))
264 output = "always defer [defer+madvise] madvise never";
265 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
266 &transparent_hugepage_flags))
267 output = "always defer defer+madvise [madvise] never";
269 output = "always defer defer+madvise madvise [never]";
271 return sysfs_emit(buf, "%s\n", output);
274 static ssize_t defrag_store(struct kobject *kobj,
275 struct kobj_attribute *attr,
276 const char *buf, size_t count)
278 if (sysfs_streq(buf, "always")) {
279 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
280 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
281 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
282 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
283 } else if (sysfs_streq(buf, "defer+madvise")) {
284 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
285 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
286 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
287 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
288 } else if (sysfs_streq(buf, "defer")) {
289 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
290 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
291 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
292 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
293 } else if (sysfs_streq(buf, "madvise")) {
294 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
295 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
296 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
297 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
298 } else if (sysfs_streq(buf, "never")) {
299 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
300 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
301 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
302 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
308 static struct kobj_attribute defrag_attr =
309 __ATTR(defrag, 0644, defrag_show, defrag_store);
311 static ssize_t use_zero_page_show(struct kobject *kobj,
312 struct kobj_attribute *attr, char *buf)
314 return single_hugepage_flag_show(kobj, attr, buf,
315 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
317 static ssize_t use_zero_page_store(struct kobject *kobj,
318 struct kobj_attribute *attr, const char *buf, size_t count)
320 return single_hugepage_flag_store(kobj, attr, buf, count,
321 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
323 static struct kobj_attribute use_zero_page_attr =
324 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
326 static ssize_t hpage_pmd_size_show(struct kobject *kobj,
327 struct kobj_attribute *attr, char *buf)
329 return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE);
331 static struct kobj_attribute hpage_pmd_size_attr =
332 __ATTR_RO(hpage_pmd_size);
334 static struct attribute *hugepage_attr[] = {
337 &use_zero_page_attr.attr,
338 &hpage_pmd_size_attr.attr,
340 &shmem_enabled_attr.attr,
345 static const struct attribute_group hugepage_attr_group = {
346 .attrs = hugepage_attr,
349 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
353 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
354 if (unlikely(!*hugepage_kobj)) {
355 pr_err("failed to create transparent hugepage kobject\n");
359 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
361 pr_err("failed to register transparent hugepage group\n");
365 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
367 pr_err("failed to register transparent hugepage group\n");
368 goto remove_hp_group;
374 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
376 kobject_put(*hugepage_kobj);
380 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
382 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
383 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
384 kobject_put(hugepage_kobj);
387 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
392 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
395 #endif /* CONFIG_SYSFS */
397 static int __init hugepage_init(void)
400 struct kobject *hugepage_kobj;
402 if (!has_transparent_hugepage()) {
404 * Hardware doesn't support hugepages, hence disable
407 transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_NEVER_DAX;
412 * hugepages can't be allocated by the buddy allocator
414 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
416 * we use page->mapping and page->index in second tail page
417 * as list_head: assuming THP order >= 2
419 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
421 err = hugepage_init_sysfs(&hugepage_kobj);
425 err = khugepaged_init();
429 err = register_shrinker(&huge_zero_page_shrinker);
431 goto err_hzp_shrinker;
432 err = register_shrinker(&deferred_split_shrinker);
434 goto err_split_shrinker;
437 * By default disable transparent hugepages on smaller systems,
438 * where the extra memory used could hurt more than TLB overhead
439 * is likely to save. The admin can still enable it through /sys.
441 if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
442 transparent_hugepage_flags = 0;
446 err = start_stop_khugepaged();
452 unregister_shrinker(&deferred_split_shrinker);
454 unregister_shrinker(&huge_zero_page_shrinker);
456 khugepaged_destroy();
458 hugepage_exit_sysfs(hugepage_kobj);
462 subsys_initcall(hugepage_init);
464 static int __init setup_transparent_hugepage(char *str)
469 if (!strcmp(str, "always")) {
470 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
471 &transparent_hugepage_flags);
472 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
473 &transparent_hugepage_flags);
475 } else if (!strcmp(str, "madvise")) {
476 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
477 &transparent_hugepage_flags);
478 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
479 &transparent_hugepage_flags);
481 } else if (!strcmp(str, "never")) {
482 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
483 &transparent_hugepage_flags);
484 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
485 &transparent_hugepage_flags);
490 pr_warn("transparent_hugepage= cannot parse, ignored\n");
493 __setup("transparent_hugepage=", setup_transparent_hugepage);
495 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
497 if (likely(vma->vm_flags & VM_WRITE))
498 pmd = pmd_mkwrite(pmd);
503 static inline struct deferred_split *get_deferred_split_queue(struct page *page)
505 struct mem_cgroup *memcg = page_memcg(compound_head(page));
506 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
509 return &memcg->deferred_split_queue;
511 return &pgdat->deferred_split_queue;
514 static inline struct deferred_split *get_deferred_split_queue(struct page *page)
516 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
518 return &pgdat->deferred_split_queue;
522 void prep_transhuge_page(struct page *page)
525 * we use page->mapping and page->indexlru in second tail page
526 * as list_head: assuming THP order >= 2
529 INIT_LIST_HEAD(page_deferred_list(page));
530 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
533 bool is_transparent_hugepage(struct page *page)
535 if (!PageCompound(page))
538 page = compound_head(page);
539 return is_huge_zero_page(page) ||
540 page[1].compound_dtor == TRANSHUGE_PAGE_DTOR;
542 EXPORT_SYMBOL_GPL(is_transparent_hugepage);
544 static unsigned long __thp_get_unmapped_area(struct file *filp,
545 unsigned long addr, unsigned long len,
546 loff_t off, unsigned long flags, unsigned long size)
548 loff_t off_end = off + len;
549 loff_t off_align = round_up(off, size);
550 unsigned long len_pad, ret;
552 if (off_end <= off_align || (off_end - off_align) < size)
555 len_pad = len + size;
556 if (len_pad < len || (off + len_pad) < off)
559 ret = current->mm->get_unmapped_area(filp, addr, len_pad,
560 off >> PAGE_SHIFT, flags);
563 * The failure might be due to length padding. The caller will retry
564 * without the padding.
566 if (IS_ERR_VALUE(ret))
570 * Do not try to align to THP boundary if allocation at the address
576 ret += (off - ret) & (size - 1);
580 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
581 unsigned long len, unsigned long pgoff, unsigned long flags)
584 loff_t off = (loff_t)pgoff << PAGE_SHIFT;
586 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
590 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
592 EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
594 static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
595 struct page *page, gfp_t gfp)
597 struct vm_area_struct *vma = vmf->vma;
599 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
602 VM_BUG_ON_PAGE(!PageCompound(page), page);
604 if (mem_cgroup_charge(page_folio(page), vma->vm_mm, gfp)) {
606 count_vm_event(THP_FAULT_FALLBACK);
607 count_vm_event(THP_FAULT_FALLBACK_CHARGE);
608 return VM_FAULT_FALLBACK;
610 cgroup_throttle_swaprate(page, gfp);
612 pgtable = pte_alloc_one(vma->vm_mm);
613 if (unlikely(!pgtable)) {
618 clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
620 * The memory barrier inside __SetPageUptodate makes sure that
621 * clear_huge_page writes become visible before the set_pmd_at()
624 __SetPageUptodate(page);
626 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
627 if (unlikely(!pmd_none(*vmf->pmd))) {
632 ret = check_stable_address_space(vma->vm_mm);
636 /* Deliver the page fault to userland */
637 if (userfaultfd_missing(vma)) {
638 spin_unlock(vmf->ptl);
640 pte_free(vma->vm_mm, pgtable);
641 ret = handle_userfault(vmf, VM_UFFD_MISSING);
642 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
646 entry = mk_huge_pmd(page, vma->vm_page_prot);
647 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
648 page_add_new_anon_rmap(page, vma, haddr, true);
649 lru_cache_add_inactive_or_unevictable(page, vma);
650 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
651 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
652 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
653 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
654 mm_inc_nr_ptes(vma->vm_mm);
655 spin_unlock(vmf->ptl);
656 count_vm_event(THP_FAULT_ALLOC);
657 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
662 spin_unlock(vmf->ptl);
665 pte_free(vma->vm_mm, pgtable);
672 * always: directly stall for all thp allocations
673 * defer: wake kswapd and fail if not immediately available
674 * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
675 * fail if not immediately available
676 * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
678 * never: never stall for any thp allocation
680 gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
682 const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
684 /* Always do synchronous compaction */
685 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
686 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
688 /* Kick kcompactd and fail quickly */
689 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
690 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
692 /* Synchronous compaction if madvised, otherwise kick kcompactd */
693 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
694 return GFP_TRANSHUGE_LIGHT |
695 (vma_madvised ? __GFP_DIRECT_RECLAIM :
696 __GFP_KSWAPD_RECLAIM);
698 /* Only do synchronous compaction if madvised */
699 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
700 return GFP_TRANSHUGE_LIGHT |
701 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
703 return GFP_TRANSHUGE_LIGHT;
706 /* Caller must hold page table lock. */
707 static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
708 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
709 struct page *zero_page)
714 entry = mk_pmd(zero_page, vma->vm_page_prot);
715 entry = pmd_mkhuge(entry);
717 pgtable_trans_huge_deposit(mm, pmd, pgtable);
718 set_pmd_at(mm, haddr, pmd, entry);
722 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
724 struct vm_area_struct *vma = vmf->vma;
727 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
729 if (!transhuge_vma_suitable(vma, haddr))
730 return VM_FAULT_FALLBACK;
731 if (unlikely(anon_vma_prepare(vma)))
733 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
735 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
736 !mm_forbids_zeropage(vma->vm_mm) &&
737 transparent_hugepage_use_zero_page()) {
739 struct page *zero_page;
741 pgtable = pte_alloc_one(vma->vm_mm);
742 if (unlikely(!pgtable))
744 zero_page = mm_get_huge_zero_page(vma->vm_mm);
745 if (unlikely(!zero_page)) {
746 pte_free(vma->vm_mm, pgtable);
747 count_vm_event(THP_FAULT_FALLBACK);
748 return VM_FAULT_FALLBACK;
750 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
752 if (pmd_none(*vmf->pmd)) {
753 ret = check_stable_address_space(vma->vm_mm);
755 spin_unlock(vmf->ptl);
756 pte_free(vma->vm_mm, pgtable);
757 } else if (userfaultfd_missing(vma)) {
758 spin_unlock(vmf->ptl);
759 pte_free(vma->vm_mm, pgtable);
760 ret = handle_userfault(vmf, VM_UFFD_MISSING);
761 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
763 set_huge_zero_page(pgtable, vma->vm_mm, vma,
764 haddr, vmf->pmd, zero_page);
765 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
766 spin_unlock(vmf->ptl);
769 spin_unlock(vmf->ptl);
770 pte_free(vma->vm_mm, pgtable);
774 gfp = vma_thp_gfp_mask(vma);
775 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
776 if (unlikely(!page)) {
777 count_vm_event(THP_FAULT_FALLBACK);
778 return VM_FAULT_FALLBACK;
780 prep_transhuge_page(page);
781 return __do_huge_pmd_anonymous_page(vmf, page, gfp);
784 static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
785 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
788 struct mm_struct *mm = vma->vm_mm;
792 ptl = pmd_lock(mm, pmd);
793 if (!pmd_none(*pmd)) {
795 if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
796 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
799 entry = pmd_mkyoung(*pmd);
800 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
801 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
802 update_mmu_cache_pmd(vma, addr, pmd);
808 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
809 if (pfn_t_devmap(pfn))
810 entry = pmd_mkdevmap(entry);
812 entry = pmd_mkyoung(pmd_mkdirty(entry));
813 entry = maybe_pmd_mkwrite(entry, vma);
817 pgtable_trans_huge_deposit(mm, pmd, pgtable);
822 set_pmd_at(mm, addr, pmd, entry);
823 update_mmu_cache_pmd(vma, addr, pmd);
828 pte_free(mm, pgtable);
832 * vmf_insert_pfn_pmd_prot - insert a pmd size pfn
833 * @vmf: Structure describing the fault
834 * @pfn: pfn to insert
835 * @pgprot: page protection to use
836 * @write: whether it's a write fault
838 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info and
839 * also consult the vmf_insert_mixed_prot() documentation when
840 * @pgprot != @vmf->vma->vm_page_prot.
842 * Return: vm_fault_t value.
844 vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
845 pgprot_t pgprot, bool write)
847 unsigned long addr = vmf->address & PMD_MASK;
848 struct vm_area_struct *vma = vmf->vma;
849 pgtable_t pgtable = NULL;
852 * If we had pmd_special, we could avoid all these restrictions,
853 * but we need to be consistent with PTEs and architectures that
854 * can't support a 'special' bit.
856 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
858 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
859 (VM_PFNMAP|VM_MIXEDMAP));
860 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
862 if (addr < vma->vm_start || addr >= vma->vm_end)
863 return VM_FAULT_SIGBUS;
865 if (arch_needs_pgtable_deposit()) {
866 pgtable = pte_alloc_one(vma->vm_mm);
871 track_pfn_insert(vma, &pgprot, pfn);
873 insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
874 return VM_FAULT_NOPAGE;
876 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd_prot);
878 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
879 static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
881 if (likely(vma->vm_flags & VM_WRITE))
882 pud = pud_mkwrite(pud);
886 static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
887 pud_t *pud, pfn_t pfn, pgprot_t prot, bool write)
889 struct mm_struct *mm = vma->vm_mm;
893 ptl = pud_lock(mm, pud);
894 if (!pud_none(*pud)) {
896 if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
897 WARN_ON_ONCE(!is_huge_zero_pud(*pud));
900 entry = pud_mkyoung(*pud);
901 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
902 if (pudp_set_access_flags(vma, addr, pud, entry, 1))
903 update_mmu_cache_pud(vma, addr, pud);
908 entry = pud_mkhuge(pfn_t_pud(pfn, prot));
909 if (pfn_t_devmap(pfn))
910 entry = pud_mkdevmap(entry);
912 entry = pud_mkyoung(pud_mkdirty(entry));
913 entry = maybe_pud_mkwrite(entry, vma);
915 set_pud_at(mm, addr, pud, entry);
916 update_mmu_cache_pud(vma, addr, pud);
923 * vmf_insert_pfn_pud_prot - insert a pud size pfn
924 * @vmf: Structure describing the fault
925 * @pfn: pfn to insert
926 * @pgprot: page protection to use
927 * @write: whether it's a write fault
929 * Insert a pud size pfn. See vmf_insert_pfn() for additional info and
930 * also consult the vmf_insert_mixed_prot() documentation when
931 * @pgprot != @vmf->vma->vm_page_prot.
933 * Return: vm_fault_t value.
935 vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
936 pgprot_t pgprot, bool write)
938 unsigned long addr = vmf->address & PUD_MASK;
939 struct vm_area_struct *vma = vmf->vma;
942 * If we had pud_special, we could avoid all these restrictions,
943 * but we need to be consistent with PTEs and architectures that
944 * can't support a 'special' bit.
946 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
948 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
949 (VM_PFNMAP|VM_MIXEDMAP));
950 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
952 if (addr < vma->vm_start || addr >= vma->vm_end)
953 return VM_FAULT_SIGBUS;
955 track_pfn_insert(vma, &pgprot, pfn);
957 insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write);
958 return VM_FAULT_NOPAGE;
960 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud_prot);
961 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
963 static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
964 pmd_t *pmd, int flags)
968 _pmd = pmd_mkyoung(*pmd);
969 if (flags & FOLL_WRITE)
970 _pmd = pmd_mkdirty(_pmd);
971 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
972 pmd, _pmd, flags & FOLL_WRITE))
973 update_mmu_cache_pmd(vma, addr, pmd);
976 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
977 pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
979 unsigned long pfn = pmd_pfn(*pmd);
980 struct mm_struct *mm = vma->vm_mm;
983 assert_spin_locked(pmd_lockptr(mm, pmd));
986 * When we COW a devmap PMD entry, we split it into PTEs, so we should
987 * not be in this function with `flags & FOLL_COW` set.
989 WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
991 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
992 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
993 (FOLL_PIN | FOLL_GET)))
996 if (flags & FOLL_WRITE && !pmd_write(*pmd))
999 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1004 if (flags & FOLL_TOUCH)
1005 touch_pmd(vma, addr, pmd, flags);
1008 * device mapped pages can only be returned if the
1009 * caller will manage the page reference count.
1011 if (!(flags & (FOLL_GET | FOLL_PIN)))
1012 return ERR_PTR(-EEXIST);
1014 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1015 *pgmap = get_dev_pagemap(pfn, *pgmap);
1017 return ERR_PTR(-EFAULT);
1018 page = pfn_to_page(pfn);
1019 if (!try_grab_page(page, flags))
1020 page = ERR_PTR(-ENOMEM);
1025 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1026 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1027 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
1029 spinlock_t *dst_ptl, *src_ptl;
1030 struct page *src_page;
1032 pgtable_t pgtable = NULL;
1035 /* Skip if can be re-fill on fault */
1036 if (!vma_is_anonymous(dst_vma))
1039 pgtable = pte_alloc_one(dst_mm);
1040 if (unlikely(!pgtable))
1043 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1044 src_ptl = pmd_lockptr(src_mm, src_pmd);
1045 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1050 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1051 if (unlikely(is_swap_pmd(pmd))) {
1052 swp_entry_t entry = pmd_to_swp_entry(pmd);
1054 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1055 if (is_writable_migration_entry(entry)) {
1056 entry = make_readable_migration_entry(
1058 pmd = swp_entry_to_pmd(entry);
1059 if (pmd_swp_soft_dirty(*src_pmd))
1060 pmd = pmd_swp_mksoft_dirty(pmd);
1061 if (pmd_swp_uffd_wp(*src_pmd))
1062 pmd = pmd_swp_mkuffd_wp(pmd);
1063 set_pmd_at(src_mm, addr, src_pmd, pmd);
1065 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1066 mm_inc_nr_ptes(dst_mm);
1067 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1068 if (!userfaultfd_wp(dst_vma))
1069 pmd = pmd_swp_clear_uffd_wp(pmd);
1070 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1076 if (unlikely(!pmd_trans_huge(pmd))) {
1077 pte_free(dst_mm, pgtable);
1081 * When page table lock is held, the huge zero pmd should not be
1082 * under splitting since we don't split the page itself, only pmd to
1085 if (is_huge_zero_pmd(pmd)) {
1087 * get_huge_zero_page() will never allocate a new page here,
1088 * since we already have a zero page to copy. It just takes a
1091 mm_get_huge_zero_page(dst_mm);
1095 src_page = pmd_page(pmd);
1096 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1099 * If this page is a potentially pinned page, split and retry the fault
1100 * with smaller page size. Normally this should not happen because the
1101 * userspace should use MADV_DONTFORK upon pinned regions. This is a
1102 * best effort that the pinned pages won't be replaced by another
1103 * random page during the coming copy-on-write.
1105 if (unlikely(page_needs_cow_for_dma(src_vma, src_page))) {
1106 pte_free(dst_mm, pgtable);
1107 spin_unlock(src_ptl);
1108 spin_unlock(dst_ptl);
1109 __split_huge_pmd(src_vma, src_pmd, addr, false, NULL);
1114 page_dup_rmap(src_page, true);
1115 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1117 mm_inc_nr_ptes(dst_mm);
1118 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1119 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1120 if (!userfaultfd_wp(dst_vma))
1121 pmd = pmd_clear_uffd_wp(pmd);
1122 pmd = pmd_mkold(pmd_wrprotect(pmd));
1123 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1127 spin_unlock(src_ptl);
1128 spin_unlock(dst_ptl);
1133 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1134 static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
1135 pud_t *pud, int flags)
1139 _pud = pud_mkyoung(*pud);
1140 if (flags & FOLL_WRITE)
1141 _pud = pud_mkdirty(_pud);
1142 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
1143 pud, _pud, flags & FOLL_WRITE))
1144 update_mmu_cache_pud(vma, addr, pud);
1147 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
1148 pud_t *pud, int flags, struct dev_pagemap **pgmap)
1150 unsigned long pfn = pud_pfn(*pud);
1151 struct mm_struct *mm = vma->vm_mm;
1154 assert_spin_locked(pud_lockptr(mm, pud));
1156 if (flags & FOLL_WRITE && !pud_write(*pud))
1159 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
1160 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
1161 (FOLL_PIN | FOLL_GET)))
1164 if (pud_present(*pud) && pud_devmap(*pud))
1169 if (flags & FOLL_TOUCH)
1170 touch_pud(vma, addr, pud, flags);
1173 * device mapped pages can only be returned if the
1174 * caller will manage the page reference count.
1176 * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here:
1178 if (!(flags & (FOLL_GET | FOLL_PIN)))
1179 return ERR_PTR(-EEXIST);
1181 pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
1182 *pgmap = get_dev_pagemap(pfn, *pgmap);
1184 return ERR_PTR(-EFAULT);
1185 page = pfn_to_page(pfn);
1186 if (!try_grab_page(page, flags))
1187 page = ERR_PTR(-ENOMEM);
1192 int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1193 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1194 struct vm_area_struct *vma)
1196 spinlock_t *dst_ptl, *src_ptl;
1200 dst_ptl = pud_lock(dst_mm, dst_pud);
1201 src_ptl = pud_lockptr(src_mm, src_pud);
1202 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1206 if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
1210 * When page table lock is held, the huge zero pud should not be
1211 * under splitting since we don't split the page itself, only pud to
1214 if (is_huge_zero_pud(pud)) {
1215 /* No huge zero pud yet */
1218 /* Please refer to comments in copy_huge_pmd() */
1219 if (unlikely(page_needs_cow_for_dma(vma, pud_page(pud)))) {
1220 spin_unlock(src_ptl);
1221 spin_unlock(dst_ptl);
1222 __split_huge_pud(vma, src_pud, addr);
1226 pudp_set_wrprotect(src_mm, addr, src_pud);
1227 pud = pud_mkold(pud_wrprotect(pud));
1228 set_pud_at(dst_mm, addr, dst_pud, pud);
1232 spin_unlock(src_ptl);
1233 spin_unlock(dst_ptl);
1237 void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
1240 unsigned long haddr;
1241 bool write = vmf->flags & FAULT_FLAG_WRITE;
1243 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1244 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1247 entry = pud_mkyoung(orig_pud);
1249 entry = pud_mkdirty(entry);
1250 haddr = vmf->address & HPAGE_PUD_MASK;
1251 if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write))
1252 update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud);
1255 spin_unlock(vmf->ptl);
1257 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1259 void huge_pmd_set_accessed(struct vm_fault *vmf)
1262 unsigned long haddr;
1263 bool write = vmf->flags & FAULT_FLAG_WRITE;
1264 pmd_t orig_pmd = vmf->orig_pmd;
1266 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1267 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
1270 entry = pmd_mkyoung(orig_pmd);
1272 entry = pmd_mkdirty(entry);
1273 haddr = vmf->address & HPAGE_PMD_MASK;
1274 if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write))
1275 update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd);
1278 spin_unlock(vmf->ptl);
1281 vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
1283 struct vm_area_struct *vma = vmf->vma;
1285 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
1286 pmd_t orig_pmd = vmf->orig_pmd;
1288 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
1289 VM_BUG_ON_VMA(!vma->anon_vma, vma);
1291 if (is_huge_zero_pmd(orig_pmd))
1294 spin_lock(vmf->ptl);
1296 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1297 spin_unlock(vmf->ptl);
1301 page = pmd_page(orig_pmd);
1302 VM_BUG_ON_PAGE(!PageHead(page), page);
1304 /* Lock page for reuse_swap_page() */
1305 if (!trylock_page(page)) {
1307 spin_unlock(vmf->ptl);
1309 spin_lock(vmf->ptl);
1310 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1311 spin_unlock(vmf->ptl);
1320 * We can only reuse the page if nobody else maps the huge page or it's
1323 if (reuse_swap_page(page)) {
1325 entry = pmd_mkyoung(orig_pmd);
1326 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1327 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
1328 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1330 spin_unlock(vmf->ptl);
1331 return VM_FAULT_WRITE;
1335 spin_unlock(vmf->ptl);
1337 __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
1338 return VM_FAULT_FALLBACK;
1342 * FOLL_FORCE can write to even unwritable pmd's, but only
1343 * after we've gone through a COW cycle and they are dirty.
1345 static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
1347 return pmd_write(pmd) ||
1348 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
1351 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1356 struct mm_struct *mm = vma->vm_mm;
1357 struct page *page = NULL;
1359 assert_spin_locked(pmd_lockptr(mm, pmd));
1361 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
1364 /* Avoid dumping huge zero page */
1365 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1366 return ERR_PTR(-EFAULT);
1368 /* Full NUMA hinting faults to serialise migration in fault paths */
1369 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
1372 page = pmd_page(*pmd);
1373 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
1375 if (!try_grab_page(page, flags))
1376 return ERR_PTR(-ENOMEM);
1378 if (flags & FOLL_TOUCH)
1379 touch_pmd(vma, addr, pmd, flags);
1381 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1382 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
1388 /* NUMA hinting page fault entry point for trans huge pmds */
1389 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
1391 struct vm_area_struct *vma = vmf->vma;
1392 pmd_t oldpmd = vmf->orig_pmd;
1395 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
1396 int page_nid = NUMA_NO_NODE;
1397 int target_nid, last_cpupid = -1;
1398 bool migrated = false;
1399 bool was_writable = pmd_savedwrite(oldpmd);
1402 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1403 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
1404 spin_unlock(vmf->ptl);
1408 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1409 page = vm_normal_page_pmd(vma, haddr, pmd);
1413 /* See similar comment in do_numa_page for explanation */
1415 flags |= TNF_NO_GROUP;
1417 page_nid = page_to_nid(page);
1418 last_cpupid = page_cpupid_last(page);
1419 target_nid = numa_migrate_prep(page, vma, haddr, page_nid,
1422 if (target_nid == NUMA_NO_NODE) {
1427 spin_unlock(vmf->ptl);
1429 migrated = migrate_misplaced_page(page, vma, target_nid);
1431 flags |= TNF_MIGRATED;
1432 page_nid = target_nid;
1434 flags |= TNF_MIGRATE_FAIL;
1435 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1436 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
1437 spin_unlock(vmf->ptl);
1444 if (page_nid != NUMA_NO_NODE)
1445 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
1451 /* Restore the PMD */
1452 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1453 pmd = pmd_mkyoung(pmd);
1455 pmd = pmd_mkwrite(pmd);
1456 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1457 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1458 spin_unlock(vmf->ptl);
1463 * Return true if we do MADV_FREE successfully on entire pmd page.
1464 * Otherwise, return false.
1466 bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1467 pmd_t *pmd, unsigned long addr, unsigned long next)
1472 struct mm_struct *mm = tlb->mm;
1475 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1477 ptl = pmd_trans_huge_lock(pmd, vma);
1482 if (is_huge_zero_pmd(orig_pmd))
1485 if (unlikely(!pmd_present(orig_pmd))) {
1486 VM_BUG_ON(thp_migration_supported() &&
1487 !is_pmd_migration_entry(orig_pmd));
1491 page = pmd_page(orig_pmd);
1493 * If other processes are mapping this page, we couldn't discard
1494 * the page unless they all do MADV_FREE so let's skip the page.
1496 if (total_mapcount(page) != 1)
1499 if (!trylock_page(page))
1503 * If user want to discard part-pages of THP, split it so MADV_FREE
1504 * will deactivate only them.
1506 if (next - addr != HPAGE_PMD_SIZE) {
1509 split_huge_page(page);
1515 if (PageDirty(page))
1516 ClearPageDirty(page);
1519 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1520 pmdp_invalidate(vma, addr, pmd);
1521 orig_pmd = pmd_mkold(orig_pmd);
1522 orig_pmd = pmd_mkclean(orig_pmd);
1524 set_pmd_at(mm, addr, pmd, orig_pmd);
1525 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1528 mark_page_lazyfree(page);
1536 static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1540 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1541 pte_free(mm, pgtable);
1545 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1546 pmd_t *pmd, unsigned long addr)
1551 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1553 ptl = __pmd_trans_huge_lock(pmd, vma);
1557 * For architectures like ppc64 we look at deposited pgtable
1558 * when calling pmdp_huge_get_and_clear. So do the
1559 * pgtable_trans_huge_withdraw after finishing pmdp related
1562 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
1564 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1565 if (vma_is_special_huge(vma)) {
1566 if (arch_needs_pgtable_deposit())
1567 zap_deposited_table(tlb->mm, pmd);
1569 } else if (is_huge_zero_pmd(orig_pmd)) {
1570 zap_deposited_table(tlb->mm, pmd);
1573 struct page *page = NULL;
1574 int flush_needed = 1;
1576 if (pmd_present(orig_pmd)) {
1577 page = pmd_page(orig_pmd);
1578 page_remove_rmap(page, vma, true);
1579 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1580 VM_BUG_ON_PAGE(!PageHead(page), page);
1581 } else if (thp_migration_supported()) {
1584 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1585 entry = pmd_to_swp_entry(orig_pmd);
1586 page = pfn_swap_entry_to_page(entry);
1589 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1591 if (PageAnon(page)) {
1592 zap_deposited_table(tlb->mm, pmd);
1593 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1595 if (arch_needs_pgtable_deposit())
1596 zap_deposited_table(tlb->mm, pmd);
1597 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
1602 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
1607 #ifndef pmd_move_must_withdraw
1608 static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1609 spinlock_t *old_pmd_ptl,
1610 struct vm_area_struct *vma)
1613 * With split pmd lock we also need to move preallocated
1614 * PTE page table if new_pmd is on different PMD page table.
1616 * We also don't deposit and withdraw tables for file pages.
1618 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1622 static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1624 #ifdef CONFIG_MEM_SOFT_DIRTY
1625 if (unlikely(is_pmd_migration_entry(pmd)))
1626 pmd = pmd_swp_mksoft_dirty(pmd);
1627 else if (pmd_present(pmd))
1628 pmd = pmd_mksoft_dirty(pmd);
1633 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
1634 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
1636 spinlock_t *old_ptl, *new_ptl;
1638 struct mm_struct *mm = vma->vm_mm;
1639 bool force_flush = false;
1642 * The destination pmd shouldn't be established, free_pgtables()
1643 * should have release it.
1645 if (WARN_ON(!pmd_none(*new_pmd))) {
1646 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1651 * We don't have to worry about the ordering of src and dst
1652 * ptlocks because exclusive mmap_lock prevents deadlock.
1654 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1656 new_ptl = pmd_lockptr(mm, new_pmd);
1657 if (new_ptl != old_ptl)
1658 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
1659 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
1660 if (pmd_present(pmd))
1662 VM_BUG_ON(!pmd_none(*new_pmd));
1664 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
1666 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1667 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
1669 pmd = move_soft_dirty_pmd(pmd);
1670 set_pmd_at(mm, new_addr, new_pmd, pmd);
1672 flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
1673 if (new_ptl != old_ptl)
1674 spin_unlock(new_ptl);
1675 spin_unlock(old_ptl);
1683 * - 0 if PMD could not be locked
1684 * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary
1685 * or if prot_numa but THP migration is not supported
1686 * - HPAGE_PMD_NR if protections changed and TLB flush necessary
1688 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1689 unsigned long addr, pgprot_t newprot, unsigned long cp_flags)
1691 struct mm_struct *mm = vma->vm_mm;
1694 bool preserve_write;
1696 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
1697 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
1698 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
1700 if (prot_numa && !thp_migration_supported())
1703 ptl = __pmd_trans_huge_lock(pmd, vma);
1707 preserve_write = prot_numa && pmd_write(*pmd);
1710 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1711 if (is_swap_pmd(*pmd)) {
1712 swp_entry_t entry = pmd_to_swp_entry(*pmd);
1714 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
1715 if (is_writable_migration_entry(entry)) {
1718 * A protection check is difficult so
1719 * just be safe and disable write
1721 entry = make_readable_migration_entry(
1723 newpmd = swp_entry_to_pmd(entry);
1724 if (pmd_swp_soft_dirty(*pmd))
1725 newpmd = pmd_swp_mksoft_dirty(newpmd);
1726 if (pmd_swp_uffd_wp(*pmd))
1727 newpmd = pmd_swp_mkuffd_wp(newpmd);
1728 set_pmd_at(mm, addr, pmd, newpmd);
1737 * Avoid trapping faults against the zero page. The read-only
1738 * data is likely to be read-cached on the local CPU and
1739 * local/remote hits to the zero page are not interesting.
1741 if (is_huge_zero_pmd(*pmd))
1744 if (pmd_protnone(*pmd))
1747 page = pmd_page(*pmd);
1749 * Skip scanning top tier node if normal numa
1750 * balancing is disabled
1752 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
1753 node_is_toptier(page_to_nid(page)))
1757 * In case prot_numa, we are under mmap_read_lock(mm). It's critical
1758 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
1759 * which is also under mmap_read_lock(mm):
1762 * change_huge_pmd(prot_numa=1)
1763 * pmdp_huge_get_and_clear_notify()
1764 * madvise_dontneed()
1766 * pmd_trans_huge(*pmd) == 0 (without ptl)
1769 * // pmd is re-established
1771 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1772 * which may break userspace.
1774 * pmdp_invalidate() is required to make sure we don't miss
1775 * dirty/young flags set by hardware.
1777 entry = pmdp_invalidate(vma, addr, pmd);
1779 entry = pmd_modify(entry, newprot);
1781 entry = pmd_mk_savedwrite(entry);
1783 entry = pmd_wrprotect(entry);
1784 entry = pmd_mkuffd_wp(entry);
1785 } else if (uffd_wp_resolve) {
1787 * Leave the write bit to be handled by PF interrupt
1788 * handler, then things like COW could be properly
1791 entry = pmd_clear_uffd_wp(entry);
1794 set_pmd_at(mm, addr, pmd, entry);
1795 BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
1802 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
1804 * Note that if it returns page table lock pointer, this routine returns without
1805 * unlocking page table lock. So callers must unlock it.
1807 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1810 ptl = pmd_lock(vma->vm_mm, pmd);
1811 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
1819 * Returns true if a given pud maps a thp, false otherwise.
1821 * Note that if it returns true, this routine returns without unlocking page
1822 * table lock. So callers must unlock it.
1824 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
1828 ptl = pud_lock(vma->vm_mm, pud);
1829 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
1835 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1836 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
1837 pud_t *pud, unsigned long addr)
1841 ptl = __pud_trans_huge_lock(pud, vma);
1845 * For architectures like ppc64 we look at deposited pgtable
1846 * when calling pudp_huge_get_and_clear. So do the
1847 * pgtable_trans_huge_withdraw after finishing pudp related
1850 pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
1851 tlb_remove_pud_tlb_entry(tlb, pud, addr);
1852 if (vma_is_special_huge(vma)) {
1854 /* No zero page support yet */
1856 /* No support for anonymous PUD pages yet */
1862 static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
1863 unsigned long haddr)
1865 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
1866 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1867 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
1868 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
1870 count_vm_event(THP_SPLIT_PUD);
1872 pudp_huge_clear_flush_notify(vma, haddr, pud);
1875 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
1876 unsigned long address)
1879 struct mmu_notifier_range range;
1881 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1882 address & HPAGE_PUD_MASK,
1883 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
1884 mmu_notifier_invalidate_range_start(&range);
1885 ptl = pud_lock(vma->vm_mm, pud);
1886 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
1888 __split_huge_pud_locked(vma, pud, range.start);
1893 * No need to double call mmu_notifier->invalidate_range() callback as
1894 * the above pudp_huge_clear_flush_notify() did already call it.
1896 mmu_notifier_invalidate_range_only_end(&range);
1898 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1900 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
1901 unsigned long haddr, pmd_t *pmd)
1903 struct mm_struct *mm = vma->vm_mm;
1909 * Leave pmd empty until pte is filled note that it is fine to delay
1910 * notification until mmu_notifier_invalidate_range_end() as we are
1911 * replacing a zero pmd write protected page with a zero pte write
1914 * See Documentation/vm/mmu_notifier.rst
1916 pmdp_huge_clear_flush(vma, haddr, pmd);
1918 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1919 pmd_populate(mm, &_pmd, pgtable);
1921 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1923 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1924 entry = pte_mkspecial(entry);
1925 pte = pte_offset_map(&_pmd, haddr);
1926 VM_BUG_ON(!pte_none(*pte));
1927 set_pte_at(mm, haddr, pte, entry);
1930 smp_wmb(); /* make pte visible before pmd */
1931 pmd_populate(mm, pmd, pgtable);
1934 static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
1935 unsigned long haddr, bool freeze)
1937 struct mm_struct *mm = vma->vm_mm;
1940 pmd_t old_pmd, _pmd;
1941 bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
1945 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
1946 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1947 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
1948 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
1949 && !pmd_devmap(*pmd));
1951 count_vm_event(THP_SPLIT_PMD);
1953 if (!vma_is_anonymous(vma)) {
1954 old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
1956 * We are going to unmap this huge page. So
1957 * just go ahead and zap it
1959 if (arch_needs_pgtable_deposit())
1960 zap_deposited_table(mm, pmd);
1961 if (vma_is_special_huge(vma))
1963 if (unlikely(is_pmd_migration_entry(old_pmd))) {
1966 entry = pmd_to_swp_entry(old_pmd);
1967 page = pfn_swap_entry_to_page(entry);
1969 page = pmd_page(old_pmd);
1970 if (!PageDirty(page) && pmd_dirty(old_pmd))
1971 set_page_dirty(page);
1972 if (!PageReferenced(page) && pmd_young(old_pmd))
1973 SetPageReferenced(page);
1974 page_remove_rmap(page, vma, true);
1977 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
1981 if (is_huge_zero_pmd(*pmd)) {
1983 * FIXME: Do we want to invalidate secondary mmu by calling
1984 * mmu_notifier_invalidate_range() see comments below inside
1985 * __split_huge_pmd() ?
1987 * We are going from a zero huge page write protected to zero
1988 * small page also write protected so it does not seems useful
1989 * to invalidate secondary mmu at this time.
1991 return __split_huge_zero_page_pmd(vma, haddr, pmd);
1995 * Up to this point the pmd is present and huge and userland has the
1996 * whole access to the hugepage during the split (which happens in
1997 * place). If we overwrite the pmd with the not-huge version pointing
1998 * to the pte here (which of course we could if all CPUs were bug
1999 * free), userland could trigger a small page size TLB miss on the
2000 * small sized TLB while the hugepage TLB entry is still established in
2001 * the huge TLB. Some CPU doesn't like that.
2002 * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
2003 * 383 on page 105. Intel should be safe but is also warns that it's
2004 * only safe if the permission and cache attributes of the two entries
2005 * loaded in the two TLB is identical (which should be the case here).
2006 * But it is generally safer to never allow small and huge TLB entries
2007 * for the same virtual address to be loaded simultaneously. So instead
2008 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2009 * current pmd notpresent (atomically because here the pmd_trans_huge
2010 * must remain set at all times on the pmd until the split is complete
2011 * for this pmd), then we flush the SMP TLB and finally we write the
2012 * non-huge version of the pmd entry with pmd_populate.
2014 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2016 pmd_migration = is_pmd_migration_entry(old_pmd);
2017 if (unlikely(pmd_migration)) {
2020 entry = pmd_to_swp_entry(old_pmd);
2021 page = pfn_swap_entry_to_page(entry);
2022 write = is_writable_migration_entry(entry);
2024 soft_dirty = pmd_swp_soft_dirty(old_pmd);
2025 uffd_wp = pmd_swp_uffd_wp(old_pmd);
2027 page = pmd_page(old_pmd);
2028 if (pmd_dirty(old_pmd))
2030 write = pmd_write(old_pmd);
2031 young = pmd_young(old_pmd);
2032 soft_dirty = pmd_soft_dirty(old_pmd);
2033 uffd_wp = pmd_uffd_wp(old_pmd);
2034 VM_BUG_ON_PAGE(!page_count(page), page);
2035 page_ref_add(page, HPAGE_PMD_NR - 1);
2039 * Withdraw the table only after we mark the pmd entry invalid.
2040 * This's critical for some architectures (Power).
2042 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2043 pmd_populate(mm, &_pmd, pgtable);
2045 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
2048 * Note that NUMA hinting access restrictions are not
2049 * transferred to avoid any possibility of altering
2050 * permissions across VMAs.
2052 if (freeze || pmd_migration) {
2053 swp_entry_t swp_entry;
2055 swp_entry = make_writable_migration_entry(
2056 page_to_pfn(page + i));
2058 swp_entry = make_readable_migration_entry(
2059 page_to_pfn(page + i));
2060 entry = swp_entry_to_pte(swp_entry);
2062 entry = pte_swp_mksoft_dirty(entry);
2064 entry = pte_swp_mkuffd_wp(entry);
2066 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
2067 entry = maybe_mkwrite(entry, vma);
2069 entry = pte_wrprotect(entry);
2071 entry = pte_mkold(entry);
2073 entry = pte_mksoft_dirty(entry);
2075 entry = pte_mkuffd_wp(entry);
2077 pte = pte_offset_map(&_pmd, addr);
2078 BUG_ON(!pte_none(*pte));
2079 set_pte_at(mm, addr, pte, entry);
2081 atomic_inc(&page[i]._mapcount);
2085 if (!pmd_migration) {
2087 * Set PG_double_map before dropping compound_mapcount to avoid
2088 * false-negative page_mapped().
2090 if (compound_mapcount(page) > 1 &&
2091 !TestSetPageDoubleMap(page)) {
2092 for (i = 0; i < HPAGE_PMD_NR; i++)
2093 atomic_inc(&page[i]._mapcount);
2096 lock_page_memcg(page);
2097 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2098 /* Last compound_mapcount is gone. */
2099 __mod_lruvec_page_state(page, NR_ANON_THPS,
2101 if (TestClearPageDoubleMap(page)) {
2102 /* No need in mapcount reference anymore */
2103 for (i = 0; i < HPAGE_PMD_NR; i++)
2104 atomic_dec(&page[i]._mapcount);
2107 unlock_page_memcg(page);
2109 /* Above is effectively page_remove_rmap(page, vma, true) */
2110 munlock_vma_page(page, vma, true);
2113 smp_wmb(); /* make pte visible before pmd */
2114 pmd_populate(mm, pmd, pgtable);
2117 for (i = 0; i < HPAGE_PMD_NR; i++) {
2118 page_remove_rmap(page + i, vma, false);
2124 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
2125 unsigned long address, bool freeze, struct folio *folio)
2128 struct mmu_notifier_range range;
2129 bool do_unlock_folio = false;
2132 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
2133 address & HPAGE_PMD_MASK,
2134 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2135 mmu_notifier_invalidate_range_start(&range);
2136 ptl = pmd_lock(vma->vm_mm, pmd);
2139 * If caller asks to setup a migration entry, we need a folio to check
2140 * pmd against. Otherwise we can end up replacing wrong folio.
2142 VM_BUG_ON(freeze && !folio);
2144 VM_WARN_ON_ONCE(!folio_test_locked(folio));
2145 if (folio != page_folio(pmd_page(*pmd)))
2150 if (pmd_trans_huge(*pmd)) {
2152 folio = page_folio(pmd_page(*pmd));
2154 * An anonymous page must be locked, to ensure that a
2155 * concurrent reuse_swap_page() sees stable mapcount;
2156 * but reuse_swap_page() is not used on shmem or file,
2157 * and page lock must not be taken when zap_pmd_range()
2158 * calls __split_huge_pmd() while i_mmap_lock is held.
2160 if (folio_test_anon(folio)) {
2161 if (unlikely(!folio_trylock(folio))) {
2167 if (unlikely(!pmd_same(*pmd, _pmd))) {
2168 folio_unlock(folio);
2175 do_unlock_folio = true;
2178 } else if (!(pmd_devmap(*pmd) || is_pmd_migration_entry(*pmd)))
2180 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
2183 if (do_unlock_folio)
2184 folio_unlock(folio);
2186 * No need to double call mmu_notifier->invalidate_range() callback.
2187 * They are 3 cases to consider inside __split_huge_pmd_locked():
2188 * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious
2189 * 2) __split_huge_zero_page_pmd() read only zero page and any write
2190 * fault will trigger a flush_notify before pointing to a new page
2191 * (it is fine if the secondary mmu keeps pointing to the old zero
2192 * page in the meantime)
2193 * 3) Split a huge pmd into pte pointing to the same page. No need
2194 * to invalidate secondary tlb entry they are all still valid.
2195 * any further changes to individual pte will notify. So no need
2196 * to call mmu_notifier->invalidate_range()
2198 mmu_notifier_invalidate_range_only_end(&range);
2201 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
2202 bool freeze, struct folio *folio)
2209 pgd = pgd_offset(vma->vm_mm, address);
2210 if (!pgd_present(*pgd))
2213 p4d = p4d_offset(pgd, address);
2214 if (!p4d_present(*p4d))
2217 pud = pud_offset(p4d, address);
2218 if (!pud_present(*pud))
2221 pmd = pmd_offset(pud, address);
2223 __split_huge_pmd(vma, pmd, address, freeze, folio);
2226 static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
2229 * If the new address isn't hpage aligned and it could previously
2230 * contain an hugepage: check if we need to split an huge pmd.
2232 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
2233 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
2234 ALIGN(address, HPAGE_PMD_SIZE)))
2235 split_huge_pmd_address(vma, address, false, NULL);
2238 void vma_adjust_trans_huge(struct vm_area_struct *vma,
2239 unsigned long start,
2243 /* Check if we need to split start first. */
2244 split_huge_pmd_if_needed(vma, start);
2246 /* Check if we need to split end next. */
2247 split_huge_pmd_if_needed(vma, end);
2250 * If we're also updating the vma->vm_next->vm_start,
2251 * check if we need to split it.
2253 if (adjust_next > 0) {
2254 struct vm_area_struct *next = vma->vm_next;
2255 unsigned long nstart = next->vm_start;
2256 nstart += adjust_next;
2257 split_huge_pmd_if_needed(next, nstart);
2261 static void unmap_page(struct page *page)
2263 struct folio *folio = page_folio(page);
2264 enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD |
2267 VM_BUG_ON_PAGE(!PageHead(page), page);
2270 * Anon pages need migration entries to preserve them, but file
2271 * pages can simply be left unmapped, then faulted back on demand.
2272 * If that is ever changed (perhaps for mlock), update remap_page().
2274 if (folio_test_anon(folio))
2275 try_to_migrate(folio, ttu_flags);
2277 try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK);
2279 VM_WARN_ON_ONCE_PAGE(page_mapped(page), page);
2282 static void remap_page(struct folio *folio, unsigned long nr)
2286 /* If unmap_page() uses try_to_migrate() on file, remove this check */
2287 if (!folio_test_anon(folio))
2290 remove_migration_ptes(folio, folio, true);
2291 i += folio_nr_pages(folio);
2294 folio = folio_next(folio);
2298 static void lru_add_page_tail(struct page *head, struct page *tail,
2299 struct lruvec *lruvec, struct list_head *list)
2301 VM_BUG_ON_PAGE(!PageHead(head), head);
2302 VM_BUG_ON_PAGE(PageCompound(tail), head);
2303 VM_BUG_ON_PAGE(PageLRU(tail), head);
2304 lockdep_assert_held(&lruvec->lru_lock);
2307 /* page reclaim is reclaiming a huge page */
2308 VM_WARN_ON(PageLRU(head));
2310 list_add_tail(&tail->lru, list);
2312 /* head is still on lru (and we have it frozen) */
2313 VM_WARN_ON(!PageLRU(head));
2314 if (PageUnevictable(tail))
2315 tail->mlock_count = 0;
2317 list_add_tail(&tail->lru, &head->lru);
2322 static void __split_huge_page_tail(struct page *head, int tail,
2323 struct lruvec *lruvec, struct list_head *list)
2325 struct page *page_tail = head + tail;
2327 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
2330 * Clone page flags before unfreezing refcount.
2332 * After successful get_page_unless_zero() might follow flags change,
2333 * for example lock_page() which set PG_waiters.
2335 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2336 page_tail->flags |= (head->flags &
2337 ((1L << PG_referenced) |
2338 (1L << PG_swapbacked) |
2339 (1L << PG_swapcache) |
2340 (1L << PG_mlocked) |
2341 (1L << PG_uptodate) |
2343 (1L << PG_workingset) |
2345 (1L << PG_unevictable) |
2351 /* ->mapping in first tail page is compound_mapcount */
2352 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2354 page_tail->mapping = head->mapping;
2355 page_tail->index = head->index + tail;
2357 /* Page flags must be visible before we make the page non-compound. */
2361 * Clear PageTail before unfreezing page refcount.
2363 * After successful get_page_unless_zero() might follow put_page()
2364 * which needs correct compound_head().
2366 clear_compound_head(page_tail);
2368 /* Finally unfreeze refcount. Additional reference from page cache. */
2369 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2370 PageSwapCache(head)));
2372 if (page_is_young(head))
2373 set_page_young(page_tail);
2374 if (page_is_idle(head))
2375 set_page_idle(page_tail);
2377 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
2380 * always add to the tail because some iterators expect new
2381 * pages to show after the currently processed elements - e.g.
2384 lru_add_page_tail(head, page_tail, lruvec, list);
2387 static void __split_huge_page(struct page *page, struct list_head *list,
2390 struct folio *folio = page_folio(page);
2391 struct page *head = &folio->page;
2392 struct lruvec *lruvec;
2393 struct address_space *swap_cache = NULL;
2394 unsigned long offset = 0;
2395 unsigned int nr = thp_nr_pages(head);
2398 /* complete memcg works before add pages to LRU */
2399 split_page_memcg(head, nr);
2401 if (PageAnon(head) && PageSwapCache(head)) {
2402 swp_entry_t entry = { .val = page_private(head) };
2404 offset = swp_offset(entry);
2405 swap_cache = swap_address_space(entry);
2406 xa_lock(&swap_cache->i_pages);
2409 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */
2410 lruvec = folio_lruvec_lock(folio);
2412 ClearPageHasHWPoisoned(head);
2414 for (i = nr - 1; i >= 1; i--) {
2415 __split_huge_page_tail(head, i, lruvec, list);
2416 /* Some pages can be beyond EOF: drop them from page cache */
2417 if (head[i].index >= end) {
2418 ClearPageDirty(head + i);
2419 __delete_from_page_cache(head + i, NULL);
2420 if (shmem_mapping(head->mapping))
2421 shmem_uncharge(head->mapping->host, 1);
2423 } else if (!PageAnon(page)) {
2424 __xa_store(&head->mapping->i_pages, head[i].index,
2426 } else if (swap_cache) {
2427 __xa_store(&swap_cache->i_pages, offset + i,
2432 ClearPageCompound(head);
2433 unlock_page_lruvec(lruvec);
2434 /* Caller disabled irqs, so they are still disabled here */
2436 split_page_owner(head, nr);
2438 /* See comment in __split_huge_page_tail() */
2439 if (PageAnon(head)) {
2440 /* Additional pin to swap cache */
2441 if (PageSwapCache(head)) {
2442 page_ref_add(head, 2);
2443 xa_unlock(&swap_cache->i_pages);
2448 /* Additional pin to page cache */
2449 page_ref_add(head, 2);
2450 xa_unlock(&head->mapping->i_pages);
2454 remap_page(folio, nr);
2456 if (PageSwapCache(head)) {
2457 swp_entry_t entry = { .val = page_private(head) };
2459 split_swap_cluster(entry);
2462 for (i = 0; i < nr; i++) {
2463 struct page *subpage = head + i;
2464 if (subpage == page)
2466 unlock_page(subpage);
2469 * Subpages may be freed if there wasn't any mapping
2470 * like if add_to_swap() is running on a lru page that
2471 * had its mapping zapped. And freeing these pages
2472 * requires taking the lru_lock so we do the put_page
2473 * of the tail pages after the split is complete.
2480 * This calculates accurately how many mappings a transparent hugepage
2481 * has (unlike page_mapcount() which isn't fully accurate). This full
2482 * accuracy is primarily needed to know if copy-on-write faults can
2483 * reuse the page and change the mapping to read-write instead of
2484 * copying them. At the same time this returns the total_mapcount too.
2486 * The function returns the highest mapcount any one of the subpages
2487 * has. If the return value is one, even if different processes are
2488 * mapping different subpages of the transparent hugepage, they can
2489 * all reuse it, because each process is reusing a different subpage.
2491 * The total_mapcount is instead counting all virtual mappings of the
2492 * subpages. If the total_mapcount is equal to "one", it tells the
2493 * caller all mappings belong to the same "mm" and in turn the
2494 * anon_vma of the transparent hugepage can become the vma->anon_vma
2495 * local one as no other process may be mapping any of the subpages.
2497 * It would be more accurate to replace page_mapcount() with
2498 * page_trans_huge_mapcount(), however we only use
2499 * page_trans_huge_mapcount() in the copy-on-write faults where we
2500 * need full accuracy to avoid breaking page pinning, because
2501 * page_trans_huge_mapcount() is slower than page_mapcount().
2503 int page_trans_huge_mapcount(struct page *page)
2507 /* hugetlbfs shouldn't call it */
2508 VM_BUG_ON_PAGE(PageHuge(page), page);
2510 if (likely(!PageTransCompound(page)))
2511 return atomic_read(&page->_mapcount) + 1;
2513 page = compound_head(page);
2516 for (i = 0; i < thp_nr_pages(page); i++) {
2517 int mapcount = atomic_read(&page[i]._mapcount) + 1;
2518 ret = max(ret, mapcount);
2521 if (PageDoubleMap(page))
2524 return ret + compound_mapcount(page);
2527 /* Racy check whether the huge page can be split */
2528 bool can_split_folio(struct folio *folio, int *pextra_pins)
2532 /* Additional pins from page cache */
2533 if (folio_test_anon(folio))
2534 extra_pins = folio_test_swapcache(folio) ?
2535 folio_nr_pages(folio) : 0;
2537 extra_pins = folio_nr_pages(folio);
2539 *pextra_pins = extra_pins;
2540 return folio_mapcount(folio) == folio_ref_count(folio) - extra_pins - 1;
2544 * This function splits huge page into normal pages. @page can point to any
2545 * subpage of huge page to split. Split doesn't change the position of @page.
2547 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
2548 * The huge page must be locked.
2550 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
2552 * Both head page and tail pages will inherit mapping, flags, and so on from
2555 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
2556 * they are not mapped.
2558 * Returns 0 if the hugepage is split successfully.
2559 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
2562 int split_huge_page_to_list(struct page *page, struct list_head *list)
2564 struct folio *folio = page_folio(page);
2565 struct page *head = &folio->page;
2566 struct deferred_split *ds_queue = get_deferred_split_queue(head);
2567 XA_STATE(xas, &head->mapping->i_pages, head->index);
2568 struct anon_vma *anon_vma = NULL;
2569 struct address_space *mapping = NULL;
2570 int extra_pins, ret;
2573 VM_BUG_ON_PAGE(is_huge_zero_page(head), head);
2574 VM_BUG_ON_PAGE(!PageLocked(head), head);
2575 VM_BUG_ON_PAGE(!PageCompound(head), head);
2577 if (PageWriteback(head))
2580 if (PageAnon(head)) {
2582 * The caller does not necessarily hold an mmap_lock that would
2583 * prevent the anon_vma disappearing so we first we take a
2584 * reference to it and then lock the anon_vma for write. This
2585 * is similar to folio_lock_anon_vma_read except the write lock
2586 * is taken to serialise against parallel split or collapse
2589 anon_vma = page_get_anon_vma(head);
2596 anon_vma_lock_write(anon_vma);
2598 mapping = head->mapping;
2606 xas_split_alloc(&xas, head, compound_order(head),
2607 mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK);
2608 if (xas_error(&xas)) {
2609 ret = xas_error(&xas);
2614 i_mmap_lock_read(mapping);
2617 *__split_huge_page() may need to trim off pages beyond EOF:
2618 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2619 * which cannot be nested inside the page tree lock. So note
2620 * end now: i_size itself may be changed at any moment, but
2621 * head page lock is good enough to serialize the trimming.
2623 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2624 if (shmem_mapping(mapping))
2625 end = shmem_fallocend(mapping->host, end);
2629 * Racy check if we can split the page, before unmap_page() will
2632 if (!can_split_folio(folio, &extra_pins)) {
2639 /* block interrupt reentry in xa_lock and spinlock */
2640 local_irq_disable();
2643 * Check if the head page is present in page cache.
2644 * We assume all tail are present too, if head is there.
2648 if (xas_load(&xas) != head)
2652 /* Prevent deferred_split_scan() touching ->_refcount */
2653 spin_lock(&ds_queue->split_queue_lock);
2654 if (page_ref_freeze(head, 1 + extra_pins)) {
2655 if (!list_empty(page_deferred_list(head))) {
2656 ds_queue->split_queue_len--;
2657 list_del(page_deferred_list(head));
2659 spin_unlock(&ds_queue->split_queue_lock);
2661 int nr = thp_nr_pages(head);
2663 xas_split(&xas, head, thp_order(head));
2664 if (PageSwapBacked(head)) {
2665 __mod_lruvec_page_state(head, NR_SHMEM_THPS,
2668 __mod_lruvec_page_state(head, NR_FILE_THPS,
2670 filemap_nr_thps_dec(mapping);
2674 __split_huge_page(page, list, end);
2677 spin_unlock(&ds_queue->split_queue_lock);
2682 remap_page(folio, folio_nr_pages(folio));
2688 anon_vma_unlock_write(anon_vma);
2689 put_anon_vma(anon_vma);
2692 i_mmap_unlock_read(mapping);
2694 /* Free any memory we didn't use */
2696 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2700 void free_transhuge_page(struct page *page)
2702 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2703 unsigned long flags;
2705 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2706 if (!list_empty(page_deferred_list(page))) {
2707 ds_queue->split_queue_len--;
2708 list_del(page_deferred_list(page));
2710 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2711 free_compound_page(page);
2714 void deferred_split_huge_page(struct page *page)
2716 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2718 struct mem_cgroup *memcg = page_memcg(compound_head(page));
2720 unsigned long flags;
2722 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
2725 * The try_to_unmap() in page reclaim path might reach here too,
2726 * this may cause a race condition to corrupt deferred split queue.
2727 * And, if page reclaim is already handling the same page, it is
2728 * unnecessary to handle it again in shrinker.
2730 * Check PageSwapCache to determine if the page is being
2731 * handled by page reclaim since THP swap would add the page into
2732 * swap cache before calling try_to_unmap().
2734 if (PageSwapCache(page))
2737 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2738 if (list_empty(page_deferred_list(page))) {
2739 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
2740 list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2741 ds_queue->split_queue_len++;
2744 set_shrinker_bit(memcg, page_to_nid(page),
2745 deferred_split_shrinker.id);
2748 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2751 static unsigned long deferred_split_count(struct shrinker *shrink,
2752 struct shrink_control *sc)
2754 struct pglist_data *pgdata = NODE_DATA(sc->nid);
2755 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
2759 ds_queue = &sc->memcg->deferred_split_queue;
2761 return READ_ONCE(ds_queue->split_queue_len);
2764 static unsigned long deferred_split_scan(struct shrinker *shrink,
2765 struct shrink_control *sc)
2767 struct pglist_data *pgdata = NODE_DATA(sc->nid);
2768 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
2769 unsigned long flags;
2770 LIST_HEAD(list), *pos, *next;
2776 ds_queue = &sc->memcg->deferred_split_queue;
2779 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2780 /* Take pin on all head pages to avoid freeing them under us */
2781 list_for_each_safe(pos, next, &ds_queue->split_queue) {
2782 page = list_entry((void *)pos, struct page, deferred_list);
2783 page = compound_head(page);
2784 if (get_page_unless_zero(page)) {
2785 list_move(page_deferred_list(page), &list);
2787 /* We lost race with put_compound_page() */
2788 list_del_init(page_deferred_list(page));
2789 ds_queue->split_queue_len--;
2791 if (!--sc->nr_to_scan)
2794 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2796 list_for_each_safe(pos, next, &list) {
2797 page = list_entry((void *)pos, struct page, deferred_list);
2798 if (!trylock_page(page))
2800 /* split_huge_page() removes page from list on success */
2801 if (!split_huge_page(page))
2808 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2809 list_splice_tail(&list, &ds_queue->split_queue);
2810 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2813 * Stop shrinker if we didn't split any page, but the queue is empty.
2814 * This can happen if pages were freed under us.
2816 if (!split && list_empty(&ds_queue->split_queue))
2821 static struct shrinker deferred_split_shrinker = {
2822 .count_objects = deferred_split_count,
2823 .scan_objects = deferred_split_scan,
2824 .seeks = DEFAULT_SEEKS,
2825 .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
2829 #ifdef CONFIG_DEBUG_FS
2830 static void split_huge_pages_all(void)
2834 unsigned long pfn, max_zone_pfn;
2835 unsigned long total = 0, split = 0;
2837 pr_debug("Split all THPs\n");
2838 for_each_populated_zone(zone) {
2839 max_zone_pfn = zone_end_pfn(zone);
2840 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
2841 if (!pfn_valid(pfn))
2844 page = pfn_to_page(pfn);
2845 if (!get_page_unless_zero(page))
2848 if (zone != page_zone(page))
2851 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
2856 if (!split_huge_page(page))
2865 pr_debug("%lu of %lu THP split\n", split, total);
2868 static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
2870 return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) ||
2871 is_vm_hugetlb_page(vma);
2874 static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
2875 unsigned long vaddr_end)
2878 struct task_struct *task;
2879 struct mm_struct *mm;
2880 unsigned long total = 0, split = 0;
2883 vaddr_start &= PAGE_MASK;
2884 vaddr_end &= PAGE_MASK;
2886 /* Find the task_struct from pid */
2888 task = find_task_by_vpid(pid);
2894 get_task_struct(task);
2897 /* Find the mm_struct */
2898 mm = get_task_mm(task);
2899 put_task_struct(task);
2906 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n",
2907 pid, vaddr_start, vaddr_end);
2911 * always increase addr by PAGE_SIZE, since we could have a PTE page
2912 * table filled with PTE-mapped THPs, each of which is distinct.
2914 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
2915 struct vm_area_struct *vma = find_vma(mm, addr);
2918 if (!vma || addr < vma->vm_start)
2921 /* skip special VMA and hugetlb VMA */
2922 if (vma_not_suitable_for_thp_split(vma)) {
2927 /* FOLL_DUMP to ignore special (like zero) pages */
2928 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
2935 if (!is_transparent_hugepage(page))
2939 if (!can_split_folio(page_folio(page), NULL))
2942 if (!trylock_page(page))
2945 if (!split_huge_page(page))
2953 mmap_read_unlock(mm);
2956 pr_debug("%lu of %lu THP split\n", split, total);
2962 static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
2965 struct filename *file;
2966 struct file *candidate;
2967 struct address_space *mapping;
2971 unsigned long total = 0, split = 0;
2973 file = getname_kernel(file_path);
2977 candidate = file_open_name(file, O_RDONLY, 0);
2978 if (IS_ERR(candidate))
2981 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n",
2982 file_path, off_start, off_end);
2984 mapping = candidate->f_mapping;
2986 for (index = off_start; index < off_end; index += nr_pages) {
2987 struct page *fpage = pagecache_get_page(mapping, index,
2988 FGP_ENTRY | FGP_HEAD, 0);
2991 if (xa_is_value(fpage) || !fpage)
2994 if (!is_transparent_hugepage(fpage))
2998 nr_pages = thp_nr_pages(fpage);
3000 if (!trylock_page(fpage))
3003 if (!split_huge_page(fpage))
3012 filp_close(candidate, NULL);
3015 pr_debug("%lu of %lu file-backed THP split\n", split, total);
3021 #define MAX_INPUT_BUF_SZ 255
3023 static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
3024 size_t count, loff_t *ppops)
3026 static DEFINE_MUTEX(split_debug_mutex);
3028 /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3029 char input_buf[MAX_INPUT_BUF_SZ];
3031 unsigned long vaddr_start, vaddr_end;
3033 ret = mutex_lock_interruptible(&split_debug_mutex);
3039 memset(input_buf, 0, MAX_INPUT_BUF_SZ);
3040 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
3043 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
3045 if (input_buf[0] == '/') {
3047 char *buf = input_buf;
3048 char file_path[MAX_INPUT_BUF_SZ];
3049 pgoff_t off_start = 0, off_end = 0;
3050 size_t input_len = strlen(input_buf);
3052 tok = strsep(&buf, ",");
3054 strcpy(file_path, tok);
3060 ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3065 ret = split_huge_pages_in_file(file_path, off_start, off_end);
3072 ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3073 if (ret == 1 && pid == 1) {
3074 split_huge_pages_all();
3075 ret = strlen(input_buf);
3077 } else if (ret != 3) {
3082 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3084 ret = strlen(input_buf);
3086 mutex_unlock(&split_debug_mutex);
3091 static const struct file_operations split_huge_pages_fops = {
3092 .owner = THIS_MODULE,
3093 .write = split_huge_pages_write,
3094 .llseek = no_llseek,
3097 static int __init split_huge_pages_debugfs(void)
3099 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3100 &split_huge_pages_fops);
3103 late_initcall(split_huge_pages_debugfs);
3106 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
3107 void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
3110 struct vm_area_struct *vma = pvmw->vma;
3111 struct mm_struct *mm = vma->vm_mm;
3112 unsigned long address = pvmw->address;
3117 if (!(pvmw->pmd && !pvmw->pte))
3120 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
3121 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
3122 if (pmd_dirty(pmdval))
3123 set_page_dirty(page);
3124 if (pmd_write(pmdval))
3125 entry = make_writable_migration_entry(page_to_pfn(page));
3127 entry = make_readable_migration_entry(page_to_pfn(page));
3128 pmdswp = swp_entry_to_pmd(entry);
3129 if (pmd_soft_dirty(pmdval))
3130 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3131 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
3132 page_remove_rmap(page, vma, true);
3136 void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3138 struct vm_area_struct *vma = pvmw->vma;
3139 struct mm_struct *mm = vma->vm_mm;
3140 unsigned long address = pvmw->address;
3141 unsigned long mmun_start = address & HPAGE_PMD_MASK;
3145 if (!(pvmw->pmd && !pvmw->pte))
3148 entry = pmd_to_swp_entry(*pvmw->pmd);
3150 pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot));
3151 if (pmd_swp_soft_dirty(*pvmw->pmd))
3152 pmde = pmd_mksoft_dirty(pmde);
3153 if (is_writable_migration_entry(entry))
3154 pmde = maybe_pmd_mkwrite(pmde, vma);
3155 if (pmd_swp_uffd_wp(*pvmw->pmd))
3156 pmde = pmd_wrprotect(pmd_mkuffd_wp(pmde));
3159 page_add_anon_rmap(new, vma, mmun_start, true);
3161 page_add_file_rmap(new, vma, true);
3162 set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
3164 /* No need to invalidate - it was non-present before */
3165 update_mmu_cache_pmd(vma, address, pvmw->pmd);