2 * Copyright (C) 2009 Red Hat, Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/sched.h>
12 #include <linux/highmem.h>
13 #include <linux/hugetlb.h>
14 #include <linux/mmu_notifier.h>
15 #include <linux/rmap.h>
16 #include <linux/swap.h>
17 #include <linux/shrinker.h>
18 #include <linux/mm_inline.h>
19 #include <linux/swapops.h>
20 #include <linux/dax.h>
21 #include <linux/khugepaged.h>
22 #include <linux/freezer.h>
23 #include <linux/pfn_t.h>
24 #include <linux/mman.h>
25 #include <linux/memremap.h>
26 #include <linux/pagemap.h>
27 #include <linux/debugfs.h>
28 #include <linux/migrate.h>
29 #include <linux/hashtable.h>
30 #include <linux/userfaultfd_k.h>
31 #include <linux/page_idle.h>
32 #include <linux/shmem_fs.h>
33 #include <linux/page_owner.h>
36 #include <asm/pgalloc.h>
40 * By default transparent hugepage support is disabled in order that avoid
41 * to risk increase the memory footprint of applications without a guaranteed
42 * benefit. When transparent hugepage support is enabled, is for all mappings,
43 * and khugepaged scans all mappings.
44 * Defrag is invoked by khugepaged hugepage allocations and by page faults
45 * for all hugepage allocations.
47 unsigned long transparent_hugepage_flags __read_mostly
=
48 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
49 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
51 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
52 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
54 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
)|
55 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
56 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
58 static struct shrinker deferred_split_shrinker
;
60 static atomic_t huge_zero_refcount
;
61 struct page
*huge_zero_page __read_mostly
;
63 static struct page
*get_huge_zero_page(void)
65 struct page
*zero_page
;
67 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
68 return READ_ONCE(huge_zero_page
);
70 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
73 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
76 count_vm_event(THP_ZERO_PAGE_ALLOC
);
78 if (cmpxchg(&huge_zero_page
, NULL
, zero_page
)) {
80 __free_pages(zero_page
, compound_order(zero_page
));
84 /* We take additional reference here. It will be put back by shrinker */
85 atomic_set(&huge_zero_refcount
, 2);
87 return READ_ONCE(huge_zero_page
);
90 static void put_huge_zero_page(void)
93 * Counter should never go to zero here. Only shrinker can put
96 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
99 struct page
*mm_get_huge_zero_page(struct mm_struct
*mm
)
101 if (test_bit(MMF_HUGE_ZERO_PAGE
, &mm
->flags
))
102 return READ_ONCE(huge_zero_page
);
104 if (!get_huge_zero_page())
107 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE
, &mm
->flags
))
108 put_huge_zero_page();
110 return READ_ONCE(huge_zero_page
);
113 void mm_put_huge_zero_page(struct mm_struct
*mm
)
115 if (test_bit(MMF_HUGE_ZERO_PAGE
, &mm
->flags
))
116 put_huge_zero_page();
119 static unsigned long shrink_huge_zero_page_count(struct shrinker
*shrink
,
120 struct shrink_control
*sc
)
122 /* we can free zero page only if last reference remains */
123 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
126 static unsigned long shrink_huge_zero_page_scan(struct shrinker
*shrink
,
127 struct shrink_control
*sc
)
129 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
130 struct page
*zero_page
= xchg(&huge_zero_page
, NULL
);
131 BUG_ON(zero_page
== NULL
);
132 __free_pages(zero_page
, compound_order(zero_page
));
139 static struct shrinker huge_zero_page_shrinker
= {
140 .count_objects
= shrink_huge_zero_page_count
,
141 .scan_objects
= shrink_huge_zero_page_scan
,
142 .seeks
= DEFAULT_SEEKS
,
147 static ssize_t
triple_flag_store(struct kobject
*kobj
,
148 struct kobj_attribute
*attr
,
149 const char *buf
, size_t count
,
150 enum transparent_hugepage_flag enabled
,
151 enum transparent_hugepage_flag deferred
,
152 enum transparent_hugepage_flag req_madv
)
154 if (!memcmp("defer", buf
,
155 min(sizeof("defer")-1, count
))) {
156 if (enabled
== deferred
)
158 clear_bit(enabled
, &transparent_hugepage_flags
);
159 clear_bit(req_madv
, &transparent_hugepage_flags
);
160 set_bit(deferred
, &transparent_hugepage_flags
);
161 } else if (!memcmp("always", buf
,
162 min(sizeof("always")-1, count
))) {
163 clear_bit(deferred
, &transparent_hugepage_flags
);
164 clear_bit(req_madv
, &transparent_hugepage_flags
);
165 set_bit(enabled
, &transparent_hugepage_flags
);
166 } else if (!memcmp("madvise", buf
,
167 min(sizeof("madvise")-1, count
))) {
168 clear_bit(enabled
, &transparent_hugepage_flags
);
169 clear_bit(deferred
, &transparent_hugepage_flags
);
170 set_bit(req_madv
, &transparent_hugepage_flags
);
171 } else if (!memcmp("never", buf
,
172 min(sizeof("never")-1, count
))) {
173 clear_bit(enabled
, &transparent_hugepage_flags
);
174 clear_bit(req_madv
, &transparent_hugepage_flags
);
175 clear_bit(deferred
, &transparent_hugepage_flags
);
182 static ssize_t
enabled_show(struct kobject
*kobj
,
183 struct kobj_attribute
*attr
, char *buf
)
185 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG
, &transparent_hugepage_flags
))
186 return sprintf(buf
, "[always] madvise never\n");
187 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
, &transparent_hugepage_flags
))
188 return sprintf(buf
, "always [madvise] never\n");
190 return sprintf(buf
, "always madvise [never]\n");
193 static ssize_t
enabled_store(struct kobject
*kobj
,
194 struct kobj_attribute
*attr
,
195 const char *buf
, size_t count
)
199 ret
= triple_flag_store(kobj
, attr
, buf
, count
,
200 TRANSPARENT_HUGEPAGE_FLAG
,
201 TRANSPARENT_HUGEPAGE_FLAG
,
202 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
205 int err
= start_stop_khugepaged();
212 static struct kobj_attribute enabled_attr
=
213 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
215 ssize_t
single_hugepage_flag_show(struct kobject
*kobj
,
216 struct kobj_attribute
*attr
, char *buf
,
217 enum transparent_hugepage_flag flag
)
219 return sprintf(buf
, "%d\n",
220 !!test_bit(flag
, &transparent_hugepage_flags
));
223 ssize_t
single_hugepage_flag_store(struct kobject
*kobj
,
224 struct kobj_attribute
*attr
,
225 const char *buf
, size_t count
,
226 enum transparent_hugepage_flag flag
)
231 ret
= kstrtoul(buf
, 10, &value
);
238 set_bit(flag
, &transparent_hugepage_flags
);
240 clear_bit(flag
, &transparent_hugepage_flags
);
246 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
247 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
248 * memory just to allocate one more hugepage.
250 static ssize_t
defrag_show(struct kobject
*kobj
,
251 struct kobj_attribute
*attr
, char *buf
)
253 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
, &transparent_hugepage_flags
))
254 return sprintf(buf
, "[always] defer madvise never\n");
255 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
, &transparent_hugepage_flags
))
256 return sprintf(buf
, "always [defer] madvise never\n");
257 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
, &transparent_hugepage_flags
))
258 return sprintf(buf
, "always defer [madvise] never\n");
260 return sprintf(buf
, "always defer madvise [never]\n");
263 static ssize_t
defrag_store(struct kobject
*kobj
,
264 struct kobj_attribute
*attr
,
265 const char *buf
, size_t count
)
267 return triple_flag_store(kobj
, attr
, buf
, count
,
268 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
,
269 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
,
270 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
272 static struct kobj_attribute defrag_attr
=
273 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
275 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
276 struct kobj_attribute
*attr
, char *buf
)
278 return single_hugepage_flag_show(kobj
, attr
, buf
,
279 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
281 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
282 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
284 return single_hugepage_flag_store(kobj
, attr
, buf
, count
,
285 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
287 static struct kobj_attribute use_zero_page_attr
=
288 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
289 #ifdef CONFIG_DEBUG_VM
290 static ssize_t
debug_cow_show(struct kobject
*kobj
,
291 struct kobj_attribute
*attr
, char *buf
)
293 return single_hugepage_flag_show(kobj
, attr
, buf
,
294 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
296 static ssize_t
debug_cow_store(struct kobject
*kobj
,
297 struct kobj_attribute
*attr
,
298 const char *buf
, size_t count
)
300 return single_hugepage_flag_store(kobj
, attr
, buf
, count
,
301 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
303 static struct kobj_attribute debug_cow_attr
=
304 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
305 #endif /* CONFIG_DEBUG_VM */
307 static struct attribute
*hugepage_attr
[] = {
310 &use_zero_page_attr
.attr
,
311 #if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
312 &shmem_enabled_attr
.attr
,
314 #ifdef CONFIG_DEBUG_VM
315 &debug_cow_attr
.attr
,
320 static struct attribute_group hugepage_attr_group
= {
321 .attrs
= hugepage_attr
,
324 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
328 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
329 if (unlikely(!*hugepage_kobj
)) {
330 pr_err("failed to create transparent hugepage kobject\n");
334 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
336 pr_err("failed to register transparent hugepage group\n");
340 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
342 pr_err("failed to register transparent hugepage group\n");
343 goto remove_hp_group
;
349 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
351 kobject_put(*hugepage_kobj
);
355 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
357 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
358 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
359 kobject_put(hugepage_kobj
);
362 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
367 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
370 #endif /* CONFIG_SYSFS */
372 static int __init
hugepage_init(void)
375 struct kobject
*hugepage_kobj
;
377 if (!has_transparent_hugepage()) {
378 transparent_hugepage_flags
= 0;
383 * hugepages can't be allocated by the buddy allocator
385 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
>= MAX_ORDER
);
387 * we use page->mapping and page->index in second tail page
388 * as list_head: assuming THP order >= 2
390 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
< 2);
392 err
= hugepage_init_sysfs(&hugepage_kobj
);
396 err
= khugepaged_init();
400 err
= register_shrinker(&huge_zero_page_shrinker
);
402 goto err_hzp_shrinker
;
403 err
= register_shrinker(&deferred_split_shrinker
);
405 goto err_split_shrinker
;
408 * By default disable transparent hugepages on smaller systems,
409 * where the extra memory used could hurt more than TLB overhead
410 * is likely to save. The admin can still enable it through /sys.
412 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
413 transparent_hugepage_flags
= 0;
417 err
= start_stop_khugepaged();
423 unregister_shrinker(&deferred_split_shrinker
);
425 unregister_shrinker(&huge_zero_page_shrinker
);
427 khugepaged_destroy();
429 hugepage_exit_sysfs(hugepage_kobj
);
433 subsys_initcall(hugepage_init
);
435 static int __init
setup_transparent_hugepage(char *str
)
440 if (!strcmp(str
, "always")) {
441 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
442 &transparent_hugepage_flags
);
443 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
444 &transparent_hugepage_flags
);
446 } else if (!strcmp(str
, "madvise")) {
447 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
448 &transparent_hugepage_flags
);
449 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
450 &transparent_hugepage_flags
);
452 } else if (!strcmp(str
, "never")) {
453 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
454 &transparent_hugepage_flags
);
455 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
456 &transparent_hugepage_flags
);
461 pr_warn("transparent_hugepage= cannot parse, ignored\n");
464 __setup("transparent_hugepage=", setup_transparent_hugepage
);
466 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
468 if (likely(vma
->vm_flags
& VM_WRITE
))
469 pmd
= pmd_mkwrite(pmd
);
473 static inline struct list_head
*page_deferred_list(struct page
*page
)
476 * ->lru in the tail pages is occupied by compound_head.
477 * Let's use ->mapping + ->index in the second tail page as list_head.
479 return (struct list_head
*)&page
[2].mapping
;
482 void prep_transhuge_page(struct page
*page
)
485 * we use page->mapping and page->indexlru in second tail page
486 * as list_head: assuming THP order >= 2
489 INIT_LIST_HEAD(page_deferred_list(page
));
490 set_compound_page_dtor(page
, TRANSHUGE_PAGE_DTOR
);
493 unsigned long __thp_get_unmapped_area(struct file
*filp
, unsigned long len
,
494 loff_t off
, unsigned long flags
, unsigned long size
)
497 loff_t off_end
= off
+ len
;
498 loff_t off_align
= round_up(off
, size
);
499 unsigned long len_pad
;
501 if (off_end
<= off_align
|| (off_end
- off_align
) < size
)
504 len_pad
= len
+ size
;
505 if (len_pad
< len
|| (off
+ len_pad
) < off
)
508 addr
= current
->mm
->get_unmapped_area(filp
, 0, len_pad
,
509 off
>> PAGE_SHIFT
, flags
);
510 if (IS_ERR_VALUE(addr
))
513 addr
+= (off
- addr
) & (size
- 1);
517 unsigned long thp_get_unmapped_area(struct file
*filp
, unsigned long addr
,
518 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
520 loff_t off
= (loff_t
)pgoff
<< PAGE_SHIFT
;
524 if (!IS_DAX(filp
->f_mapping
->host
) || !IS_ENABLED(CONFIG_FS_DAX_PMD
))
527 addr
= __thp_get_unmapped_area(filp
, len
, off
, flags
, PMD_SIZE
);
532 return current
->mm
->get_unmapped_area(filp
, addr
, len
, pgoff
, flags
);
534 EXPORT_SYMBOL_GPL(thp_get_unmapped_area
);
536 static int __do_huge_pmd_anonymous_page(struct fault_env
*fe
, struct page
*page
,
539 struct vm_area_struct
*vma
= fe
->vma
;
540 struct mem_cgroup
*memcg
;
542 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
544 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
546 if (mem_cgroup_try_charge(page
, vma
->vm_mm
, gfp
| __GFP_NORETRY
, &memcg
,
549 count_vm_event(THP_FAULT_FALLBACK
);
550 return VM_FAULT_FALLBACK
;
553 pgtable
= pte_alloc_one(vma
->vm_mm
, haddr
);
554 if (unlikely(!pgtable
)) {
555 mem_cgroup_cancel_charge(page
, memcg
, true);
560 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
562 * The memory barrier inside __SetPageUptodate makes sure that
563 * clear_huge_page writes become visible before the set_pmd_at()
566 __SetPageUptodate(page
);
568 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
569 if (unlikely(!pmd_none(*fe
->pmd
))) {
570 spin_unlock(fe
->ptl
);
571 mem_cgroup_cancel_charge(page
, memcg
, true);
573 pte_free(vma
->vm_mm
, pgtable
);
577 /* Deliver the page fault to userland */
578 if (userfaultfd_missing(vma
)) {
581 spin_unlock(fe
->ptl
);
582 mem_cgroup_cancel_charge(page
, memcg
, true);
584 pte_free(vma
->vm_mm
, pgtable
);
585 ret
= handle_userfault(fe
, VM_UFFD_MISSING
);
586 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
590 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
591 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
592 page_add_new_anon_rmap(page
, vma
, haddr
, true);
593 mem_cgroup_commit_charge(page
, memcg
, false, true);
594 lru_cache_add_active_or_unevictable(page
, vma
);
595 pgtable_trans_huge_deposit(vma
->vm_mm
, fe
->pmd
, pgtable
);
596 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, entry
);
597 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
598 atomic_long_inc(&vma
->vm_mm
->nr_ptes
);
599 spin_unlock(fe
->ptl
);
600 count_vm_event(THP_FAULT_ALLOC
);
607 * If THP defrag is set to always then directly reclaim/compact as necessary
608 * If set to defer then do only background reclaim/compact and defer to khugepaged
609 * If set to madvise and the VMA is flagged then directly reclaim/compact
610 * When direct reclaim/compact is allowed, don't retry except for flagged VMA's
612 static inline gfp_t
alloc_hugepage_direct_gfpmask(struct vm_area_struct
*vma
)
614 bool vma_madvised
= !!(vma
->vm_flags
& VM_HUGEPAGE
);
616 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
,
617 &transparent_hugepage_flags
) && vma_madvised
)
618 return GFP_TRANSHUGE
;
619 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
,
620 &transparent_hugepage_flags
))
621 return GFP_TRANSHUGE_LIGHT
| __GFP_KSWAPD_RECLAIM
;
622 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
,
623 &transparent_hugepage_flags
))
624 return GFP_TRANSHUGE
| (vma_madvised
? 0 : __GFP_NORETRY
);
626 return GFP_TRANSHUGE_LIGHT
;
629 /* Caller must hold page table lock. */
630 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
631 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
632 struct page
*zero_page
)
637 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
638 entry
= pmd_mkhuge(entry
);
640 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
641 set_pmd_at(mm
, haddr
, pmd
, entry
);
642 atomic_long_inc(&mm
->nr_ptes
);
646 int do_huge_pmd_anonymous_page(struct fault_env
*fe
)
648 struct vm_area_struct
*vma
= fe
->vma
;
651 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
653 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
654 return VM_FAULT_FALLBACK
;
655 if (unlikely(anon_vma_prepare(vma
)))
657 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
659 if (!(fe
->flags
& FAULT_FLAG_WRITE
) &&
660 !mm_forbids_zeropage(vma
->vm_mm
) &&
661 transparent_hugepage_use_zero_page()) {
663 struct page
*zero_page
;
666 pgtable
= pte_alloc_one(vma
->vm_mm
, haddr
);
667 if (unlikely(!pgtable
))
669 zero_page
= mm_get_huge_zero_page(vma
->vm_mm
);
670 if (unlikely(!zero_page
)) {
671 pte_free(vma
->vm_mm
, pgtable
);
672 count_vm_event(THP_FAULT_FALLBACK
);
673 return VM_FAULT_FALLBACK
;
675 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
678 if (pmd_none(*fe
->pmd
)) {
679 if (userfaultfd_missing(vma
)) {
680 spin_unlock(fe
->ptl
);
681 ret
= handle_userfault(fe
, VM_UFFD_MISSING
);
682 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
684 set_huge_zero_page(pgtable
, vma
->vm_mm
, vma
,
685 haddr
, fe
->pmd
, zero_page
);
686 spin_unlock(fe
->ptl
);
690 spin_unlock(fe
->ptl
);
692 pte_free(vma
->vm_mm
, pgtable
);
695 gfp
= alloc_hugepage_direct_gfpmask(vma
);
696 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
697 if (unlikely(!page
)) {
698 count_vm_event(THP_FAULT_FALLBACK
);
699 return VM_FAULT_FALLBACK
;
701 prep_transhuge_page(page
);
702 return __do_huge_pmd_anonymous_page(fe
, page
, gfp
);
705 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
706 pmd_t
*pmd
, pfn_t pfn
, pgprot_t prot
, bool write
)
708 struct mm_struct
*mm
= vma
->vm_mm
;
712 ptl
= pmd_lock(mm
, pmd
);
713 entry
= pmd_mkhuge(pfn_t_pmd(pfn
, prot
));
714 if (pfn_t_devmap(pfn
))
715 entry
= pmd_mkdevmap(entry
);
717 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
718 entry
= maybe_pmd_mkwrite(entry
, vma
);
720 set_pmd_at(mm
, addr
, pmd
, entry
);
721 update_mmu_cache_pmd(vma
, addr
, pmd
);
725 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
726 pmd_t
*pmd
, pfn_t pfn
, bool write
)
728 pgprot_t pgprot
= vma
->vm_page_prot
;
730 * If we had pmd_special, we could avoid all these restrictions,
731 * but we need to be consistent with PTEs and architectures that
732 * can't support a 'special' bit.
734 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
735 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
736 (VM_PFNMAP
|VM_MIXEDMAP
));
737 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
738 BUG_ON(!pfn_t_devmap(pfn
));
740 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
741 return VM_FAULT_SIGBUS
;
742 if (track_pfn_insert(vma
, &pgprot
, pfn
))
743 return VM_FAULT_SIGBUS
;
744 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
745 return VM_FAULT_NOPAGE
;
747 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd
);
749 static void touch_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
750 pmd_t
*pmd
, int flags
)
754 _pmd
= pmd_mkyoung(*pmd
);
755 if (flags
& FOLL_WRITE
)
756 _pmd
= pmd_mkdirty(_pmd
);
757 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
758 pmd
, _pmd
, flags
& FOLL_WRITE
))
759 update_mmu_cache_pmd(vma
, addr
, pmd
);
762 struct page
*follow_devmap_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
763 pmd_t
*pmd
, int flags
)
765 unsigned long pfn
= pmd_pfn(*pmd
);
766 struct mm_struct
*mm
= vma
->vm_mm
;
767 struct dev_pagemap
*pgmap
;
770 assert_spin_locked(pmd_lockptr(mm
, pmd
));
773 * When we COW a devmap PMD entry, we split it into PTEs, so we should
774 * not be in this function with `flags & FOLL_COW` set.
776 WARN_ONCE(flags
& FOLL_COW
, "mm: In follow_devmap_pmd with FOLL_COW set");
778 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
781 if (pmd_present(*pmd
) && pmd_devmap(*pmd
))
786 if (flags
& FOLL_TOUCH
)
787 touch_pmd(vma
, addr
, pmd
, flags
);
790 * device mapped pages can only be returned if the
791 * caller will manage the page reference count.
793 if (!(flags
& FOLL_GET
))
794 return ERR_PTR(-EEXIST
);
796 pfn
+= (addr
& ~PMD_MASK
) >> PAGE_SHIFT
;
797 pgmap
= get_dev_pagemap(pfn
, NULL
);
799 return ERR_PTR(-EFAULT
);
800 page
= pfn_to_page(pfn
);
802 put_dev_pagemap(pgmap
);
807 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
808 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
809 struct vm_area_struct
*vma
)
811 spinlock_t
*dst_ptl
, *src_ptl
;
812 struct page
*src_page
;
814 pgtable_t pgtable
= NULL
;
817 /* Skip if can be re-fill on fault */
818 if (!vma_is_anonymous(vma
))
821 pgtable
= pte_alloc_one(dst_mm
, addr
);
822 if (unlikely(!pgtable
))
825 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
826 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
827 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
831 if (unlikely(!pmd_trans_huge(pmd
))) {
832 pte_free(dst_mm
, pgtable
);
836 * When page table lock is held, the huge zero pmd should not be
837 * under splitting since we don't split the page itself, only pmd to
840 if (is_huge_zero_pmd(pmd
)) {
841 struct page
*zero_page
;
843 * get_huge_zero_page() will never allocate a new page here,
844 * since we already have a zero page to copy. It just takes a
847 zero_page
= mm_get_huge_zero_page(dst_mm
);
848 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
854 src_page
= pmd_page(pmd
);
855 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
857 page_dup_rmap(src_page
, true);
858 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
859 atomic_long_inc(&dst_mm
->nr_ptes
);
860 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
862 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
863 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
864 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
868 spin_unlock(src_ptl
);
869 spin_unlock(dst_ptl
);
874 void huge_pmd_set_accessed(struct fault_env
*fe
, pmd_t orig_pmd
)
878 bool write
= fe
->flags
& FAULT_FLAG_WRITE
;
880 fe
->ptl
= pmd_lock(fe
->vma
->vm_mm
, fe
->pmd
);
881 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
884 entry
= pmd_mkyoung(orig_pmd
);
886 entry
= pmd_mkdirty(entry
);
887 haddr
= fe
->address
& HPAGE_PMD_MASK
;
888 if (pmdp_set_access_flags(fe
->vma
, haddr
, fe
->pmd
, entry
, write
))
889 update_mmu_cache_pmd(fe
->vma
, fe
->address
, fe
->pmd
);
892 spin_unlock(fe
->ptl
);
895 static int do_huge_pmd_wp_page_fallback(struct fault_env
*fe
, pmd_t orig_pmd
,
898 struct vm_area_struct
*vma
= fe
->vma
;
899 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
900 struct mem_cgroup
*memcg
;
905 unsigned long mmun_start
; /* For mmu_notifiers */
906 unsigned long mmun_end
; /* For mmu_notifiers */
908 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
910 if (unlikely(!pages
)) {
915 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
916 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
917 __GFP_OTHER_NODE
, vma
,
918 fe
->address
, page_to_nid(page
));
919 if (unlikely(!pages
[i
] ||
920 mem_cgroup_try_charge(pages
[i
], vma
->vm_mm
,
921 GFP_KERNEL
, &memcg
, false))) {
925 memcg
= (void *)page_private(pages
[i
]);
926 set_page_private(pages
[i
], 0);
927 mem_cgroup_cancel_charge(pages
[i
], memcg
,
935 set_page_private(pages
[i
], (unsigned long)memcg
);
938 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
939 copy_user_highpage(pages
[i
], page
+ i
,
940 haddr
+ PAGE_SIZE
* i
, vma
);
941 __SetPageUptodate(pages
[i
]);
946 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
947 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
949 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
950 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
952 VM_BUG_ON_PAGE(!PageHead(page
), page
);
954 pmdp_huge_clear_flush_notify(vma
, haddr
, fe
->pmd
);
955 /* leave pmd empty until pte is filled */
957 pgtable
= pgtable_trans_huge_withdraw(vma
->vm_mm
, fe
->pmd
);
958 pmd_populate(vma
->vm_mm
, &_pmd
, pgtable
);
960 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
962 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
963 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
964 memcg
= (void *)page_private(pages
[i
]);
965 set_page_private(pages
[i
], 0);
966 page_add_new_anon_rmap(pages
[i
], fe
->vma
, haddr
, false);
967 mem_cgroup_commit_charge(pages
[i
], memcg
, false, false);
968 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
969 fe
->pte
= pte_offset_map(&_pmd
, haddr
);
970 VM_BUG_ON(!pte_none(*fe
->pte
));
971 set_pte_at(vma
->vm_mm
, haddr
, fe
->pte
, entry
);
976 smp_wmb(); /* make pte visible before pmd */
977 pmd_populate(vma
->vm_mm
, fe
->pmd
, pgtable
);
978 page_remove_rmap(page
, true);
979 spin_unlock(fe
->ptl
);
981 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
983 ret
|= VM_FAULT_WRITE
;
990 spin_unlock(fe
->ptl
);
991 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
992 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
993 memcg
= (void *)page_private(pages
[i
]);
994 set_page_private(pages
[i
], 0);
995 mem_cgroup_cancel_charge(pages
[i
], memcg
, false);
1002 int do_huge_pmd_wp_page(struct fault_env
*fe
, pmd_t orig_pmd
)
1004 struct vm_area_struct
*vma
= fe
->vma
;
1005 struct page
*page
= NULL
, *new_page
;
1006 struct mem_cgroup
*memcg
;
1007 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
1008 unsigned long mmun_start
; /* For mmu_notifiers */
1009 unsigned long mmun_end
; /* For mmu_notifiers */
1010 gfp_t huge_gfp
; /* for allocation and charge */
1013 fe
->ptl
= pmd_lockptr(vma
->vm_mm
, fe
->pmd
);
1014 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1015 if (is_huge_zero_pmd(orig_pmd
))
1018 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
1021 page
= pmd_page(orig_pmd
);
1022 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1024 * We can only reuse the page if nobody else maps the huge page or it's
1027 if (page_trans_huge_mapcount(page
, NULL
) == 1) {
1029 entry
= pmd_mkyoung(orig_pmd
);
1030 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1031 if (pmdp_set_access_flags(vma
, haddr
, fe
->pmd
, entry
, 1))
1032 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1033 ret
|= VM_FAULT_WRITE
;
1037 spin_unlock(fe
->ptl
);
1039 if (transparent_hugepage_enabled(vma
) &&
1040 !transparent_hugepage_debug_cow()) {
1041 huge_gfp
= alloc_hugepage_direct_gfpmask(vma
);
1042 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1046 if (likely(new_page
)) {
1047 prep_transhuge_page(new_page
);
1050 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1051 ret
|= VM_FAULT_FALLBACK
;
1053 ret
= do_huge_pmd_wp_page_fallback(fe
, orig_pmd
, page
);
1054 if (ret
& VM_FAULT_OOM
) {
1055 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1056 ret
|= VM_FAULT_FALLBACK
;
1060 count_vm_event(THP_FAULT_FALLBACK
);
1064 if (unlikely(mem_cgroup_try_charge(new_page
, vma
->vm_mm
,
1065 huge_gfp
| __GFP_NORETRY
, &memcg
, true))) {
1067 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1070 ret
|= VM_FAULT_FALLBACK
;
1071 count_vm_event(THP_FAULT_FALLBACK
);
1075 count_vm_event(THP_FAULT_ALLOC
);
1078 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1080 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1081 __SetPageUptodate(new_page
);
1084 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1085 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
1090 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
))) {
1091 spin_unlock(fe
->ptl
);
1092 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1097 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1098 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1099 pmdp_huge_clear_flush_notify(vma
, haddr
, fe
->pmd
);
1100 page_add_new_anon_rmap(new_page
, vma
, haddr
, true);
1101 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1102 lru_cache_add_active_or_unevictable(new_page
, vma
);
1103 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, entry
);
1104 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1106 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1108 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1109 page_remove_rmap(page
, true);
1112 ret
|= VM_FAULT_WRITE
;
1114 spin_unlock(fe
->ptl
);
1116 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
1120 spin_unlock(fe
->ptl
);
1125 * FOLL_FORCE can write to even unwritable pmd's, but only
1126 * after we've gone through a COW cycle and they are dirty.
1128 static inline bool can_follow_write_pmd(pmd_t pmd
, unsigned int flags
)
1130 return pmd_write(pmd
) ||
1131 ((flags
& FOLL_FORCE
) && (flags
& FOLL_COW
) && pmd_dirty(pmd
));
1134 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1139 struct mm_struct
*mm
= vma
->vm_mm
;
1140 struct page
*page
= NULL
;
1142 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1144 if (flags
& FOLL_WRITE
&& !can_follow_write_pmd(*pmd
, flags
))
1147 /* Avoid dumping huge zero page */
1148 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1149 return ERR_PTR(-EFAULT
);
1151 /* Full NUMA hinting faults to serialise migration in fault paths */
1152 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1155 page
= pmd_page(*pmd
);
1156 VM_BUG_ON_PAGE(!PageHead(page
) && !is_zone_device_page(page
), page
);
1157 if (flags
& FOLL_TOUCH
)
1158 touch_pmd(vma
, addr
, pmd
, flags
);
1159 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1161 * We don't mlock() pte-mapped THPs. This way we can avoid
1162 * leaking mlocked pages into non-VM_LOCKED VMAs.
1166 * In most cases the pmd is the only mapping of the page as we
1167 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1168 * writable private mappings in populate_vma_page_range().
1170 * The only scenario when we have the page shared here is if we
1171 * mlocking read-only mapping shared over fork(). We skip
1172 * mlocking such pages.
1176 * We can expect PageDoubleMap() to be stable under page lock:
1177 * for file pages we set it in page_add_file_rmap(), which
1178 * requires page to be locked.
1181 if (PageAnon(page
) && compound_mapcount(page
) != 1)
1183 if (PageDoubleMap(page
) || !page
->mapping
)
1185 if (!trylock_page(page
))
1188 if (page
->mapping
&& !PageDoubleMap(page
))
1189 mlock_vma_page(page
);
1193 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1194 VM_BUG_ON_PAGE(!PageCompound(page
) && !is_zone_device_page(page
), page
);
1195 if (flags
& FOLL_GET
)
1202 /* NUMA hinting page fault entry point for trans huge pmds */
1203 int do_huge_pmd_numa_page(struct fault_env
*fe
, pmd_t pmd
)
1205 struct vm_area_struct
*vma
= fe
->vma
;
1206 struct anon_vma
*anon_vma
= NULL
;
1208 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
1209 int page_nid
= -1, this_nid
= numa_node_id();
1210 int target_nid
, last_cpupid
= -1;
1212 bool migrated
= false;
1216 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
1217 if (unlikely(!pmd_same(pmd
, *fe
->pmd
)))
1221 * If there are potential migrations, wait for completion and retry
1222 * without disrupting NUMA hinting information. Do not relock and
1223 * check_same as the page may no longer be mapped.
1225 if (unlikely(pmd_trans_migrating(*fe
->pmd
))) {
1226 page
= pmd_page(*fe
->pmd
);
1227 if (!get_page_unless_zero(page
))
1229 spin_unlock(fe
->ptl
);
1230 wait_on_page_locked(page
);
1235 page
= pmd_page(pmd
);
1236 BUG_ON(is_huge_zero_page(page
));
1237 page_nid
= page_to_nid(page
);
1238 last_cpupid
= page_cpupid_last(page
);
1239 count_vm_numa_event(NUMA_HINT_FAULTS
);
1240 if (page_nid
== this_nid
) {
1241 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1242 flags
|= TNF_FAULT_LOCAL
;
1245 /* See similar comment in do_numa_page for explanation */
1246 if (!pmd_write(pmd
))
1247 flags
|= TNF_NO_GROUP
;
1250 * Acquire the page lock to serialise THP migrations but avoid dropping
1251 * page_table_lock if at all possible
1253 page_locked
= trylock_page(page
);
1254 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1255 if (target_nid
== -1) {
1256 /* If the page was locked, there are no parallel migrations */
1261 /* Migration could have started since the pmd_trans_migrating check */
1264 if (!get_page_unless_zero(page
))
1266 spin_unlock(fe
->ptl
);
1267 wait_on_page_locked(page
);
1273 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1274 * to serialises splits
1277 spin_unlock(fe
->ptl
);
1278 anon_vma
= page_lock_anon_vma_read(page
);
1280 /* Confirm the PMD did not change while page_table_lock was released */
1282 if (unlikely(!pmd_same(pmd
, *fe
->pmd
))) {
1289 /* Bail if we fail to protect against THP splits for any reason */
1290 if (unlikely(!anon_vma
)) {
1297 * Migrate the THP to the requested node, returns with page unlocked
1298 * and access rights restored.
1300 spin_unlock(fe
->ptl
);
1301 migrated
= migrate_misplaced_transhuge_page(vma
->vm_mm
, vma
,
1302 fe
->pmd
, pmd
, fe
->address
, page
, target_nid
);
1304 flags
|= TNF_MIGRATED
;
1305 page_nid
= target_nid
;
1307 flags
|= TNF_MIGRATE_FAIL
;
1311 BUG_ON(!PageLocked(page
));
1312 was_writable
= pmd_write(pmd
);
1313 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1314 pmd
= pmd_mkyoung(pmd
);
1316 pmd
= pmd_mkwrite(pmd
);
1317 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, pmd
);
1318 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1321 spin_unlock(fe
->ptl
);
1325 page_unlock_anon_vma_read(anon_vma
);
1328 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, fe
->flags
);
1334 * Return true if we do MADV_FREE successfully on entire pmd page.
1335 * Otherwise, return false.
1337 bool madvise_free_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1338 pmd_t
*pmd
, unsigned long addr
, unsigned long next
)
1343 struct mm_struct
*mm
= tlb
->mm
;
1346 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1351 if (is_huge_zero_pmd(orig_pmd
))
1354 page
= pmd_page(orig_pmd
);
1356 * If other processes are mapping this page, we couldn't discard
1357 * the page unless they all do MADV_FREE so let's skip the page.
1359 if (page_mapcount(page
) != 1)
1362 if (!trylock_page(page
))
1366 * If user want to discard part-pages of THP, split it so MADV_FREE
1367 * will deactivate only them.
1369 if (next
- addr
!= HPAGE_PMD_SIZE
) {
1372 split_huge_page(page
);
1378 if (PageDirty(page
))
1379 ClearPageDirty(page
);
1382 if (PageActive(page
))
1383 deactivate_page(page
);
1385 if (pmd_young(orig_pmd
) || pmd_dirty(orig_pmd
)) {
1386 pmdp_invalidate(vma
, addr
, pmd
);
1387 orig_pmd
= pmd_mkold(orig_pmd
);
1388 orig_pmd
= pmd_mkclean(orig_pmd
);
1390 set_pmd_at(mm
, addr
, pmd
, orig_pmd
);
1391 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1400 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1401 pmd_t
*pmd
, unsigned long addr
)
1406 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1410 * For architectures like ppc64 we look at deposited pgtable
1411 * when calling pmdp_huge_get_and_clear. So do the
1412 * pgtable_trans_huge_withdraw after finishing pmdp related
1415 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1417 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1418 if (vma_is_dax(vma
)) {
1420 if (is_huge_zero_pmd(orig_pmd
))
1421 tlb_remove_page(tlb
, pmd_page(orig_pmd
));
1422 } else if (is_huge_zero_pmd(orig_pmd
)) {
1423 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1424 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1426 tlb_remove_page(tlb
, pmd_page(orig_pmd
));
1428 struct page
*page
= pmd_page(orig_pmd
);
1429 page_remove_rmap(page
, true);
1430 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1431 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1432 if (PageAnon(page
)) {
1434 pgtable
= pgtable_trans_huge_withdraw(tlb
->mm
, pmd
);
1435 pte_free(tlb
->mm
, pgtable
);
1436 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1437 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1439 add_mm_counter(tlb
->mm
, MM_FILEPAGES
, -HPAGE_PMD_NR
);
1442 tlb_remove_page_size(tlb
, page
, HPAGE_PMD_SIZE
);
1447 bool move_huge_pmd(struct vm_area_struct
*vma
, unsigned long old_addr
,
1448 unsigned long new_addr
, unsigned long old_end
,
1449 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1451 spinlock_t
*old_ptl
, *new_ptl
;
1453 struct mm_struct
*mm
= vma
->vm_mm
;
1454 bool force_flush
= false;
1456 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1457 (new_addr
& ~HPAGE_PMD_MASK
) ||
1458 old_end
- old_addr
< HPAGE_PMD_SIZE
)
1462 * The destination pmd shouldn't be established, free_pgtables()
1463 * should have release it.
1465 if (WARN_ON(!pmd_none(*new_pmd
))) {
1466 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1471 * We don't have to worry about the ordering of src and dst
1472 * ptlocks because exclusive mmap_sem prevents deadlock.
1474 old_ptl
= __pmd_trans_huge_lock(old_pmd
, vma
);
1476 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1477 if (new_ptl
!= old_ptl
)
1478 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1479 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1480 if (pmd_present(pmd
))
1482 VM_BUG_ON(!pmd_none(*new_pmd
));
1484 if (pmd_move_must_withdraw(new_ptl
, old_ptl
) &&
1485 vma_is_anonymous(vma
)) {
1487 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1488 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1490 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1492 flush_tlb_range(vma
, old_addr
, old_addr
+ PMD_SIZE
);
1493 if (new_ptl
!= old_ptl
)
1494 spin_unlock(new_ptl
);
1495 spin_unlock(old_ptl
);
1503 * - 0 if PMD could not be locked
1504 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1505 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1507 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1508 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1510 struct mm_struct
*mm
= vma
->vm_mm
;
1513 bool preserve_write
;
1516 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1520 preserve_write
= prot_numa
&& pmd_write(*pmd
);
1524 * Avoid trapping faults against the zero page. The read-only
1525 * data is likely to be read-cached on the local CPU and
1526 * local/remote hits to the zero page are not interesting.
1528 if (prot_numa
&& is_huge_zero_pmd(*pmd
))
1531 if (prot_numa
&& pmd_protnone(*pmd
))
1535 * In case prot_numa, we are under down_read(mmap_sem). It's critical
1536 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
1537 * which is also under down_read(mmap_sem):
1540 * change_huge_pmd(prot_numa=1)
1541 * pmdp_huge_get_and_clear_notify()
1542 * madvise_dontneed()
1544 * pmd_trans_huge(*pmd) == 0 (without ptl)
1547 * // pmd is re-established
1549 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1550 * which may break userspace.
1552 * pmdp_invalidate() is required to make sure we don't miss
1553 * dirty/young flags set by hardware.
1556 pmdp_invalidate(vma
, addr
, pmd
);
1559 * Recover dirty/young flags. It relies on pmdp_invalidate to not
1562 if (pmd_dirty(*pmd
))
1563 entry
= pmd_mkdirty(entry
);
1564 if (pmd_young(*pmd
))
1565 entry
= pmd_mkyoung(entry
);
1567 entry
= pmd_modify(entry
, newprot
);
1569 entry
= pmd_mkwrite(entry
);
1571 set_pmd_at(mm
, addr
, pmd
, entry
);
1572 BUG_ON(vma_is_anonymous(vma
) && !preserve_write
&& pmd_write(entry
));
1579 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
1581 * Note that if it returns page table lock pointer, this routine returns without
1582 * unlocking page table lock. So callers must unlock it.
1584 spinlock_t
*__pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1587 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1588 if (likely(pmd_trans_huge(*pmd
) || pmd_devmap(*pmd
)))
1594 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
1595 unsigned long haddr
, pmd_t
*pmd
)
1597 struct mm_struct
*mm
= vma
->vm_mm
;
1602 /* leave pmd empty until pte is filled */
1603 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1605 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1606 pmd_populate(mm
, &_pmd
, pgtable
);
1608 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1610 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
1611 entry
= pte_mkspecial(entry
);
1612 pte
= pte_offset_map(&_pmd
, haddr
);
1613 VM_BUG_ON(!pte_none(*pte
));
1614 set_pte_at(mm
, haddr
, pte
, entry
);
1617 smp_wmb(); /* make pte visible before pmd */
1618 pmd_populate(mm
, pmd
, pgtable
);
1621 static void __split_huge_pmd_locked(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1622 unsigned long haddr
, bool freeze
)
1624 struct mm_struct
*mm
= vma
->vm_mm
;
1628 bool young
, write
, dirty
, soft_dirty
;
1632 VM_BUG_ON(haddr
& ~HPAGE_PMD_MASK
);
1633 VM_BUG_ON_VMA(vma
->vm_start
> haddr
, vma
);
1634 VM_BUG_ON_VMA(vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
, vma
);
1635 VM_BUG_ON(!pmd_trans_huge(*pmd
) && !pmd_devmap(*pmd
));
1637 count_vm_event(THP_SPLIT_PMD
);
1639 if (!vma_is_anonymous(vma
)) {
1640 _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1641 if (vma_is_dax(vma
))
1643 page
= pmd_page(_pmd
);
1644 if (!PageDirty(page
) && pmd_dirty(_pmd
))
1645 set_page_dirty(page
);
1646 if (!PageReferenced(page
) && pmd_young(_pmd
))
1647 SetPageReferenced(page
);
1648 page_remove_rmap(page
, true);
1650 add_mm_counter(mm
, MM_FILEPAGES
, -HPAGE_PMD_NR
);
1652 } else if (is_huge_zero_pmd(*pmd
)) {
1653 return __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
1656 page
= pmd_page(*pmd
);
1657 VM_BUG_ON_PAGE(!page_count(page
), page
);
1658 page_ref_add(page
, HPAGE_PMD_NR
- 1);
1659 write
= pmd_write(*pmd
);
1660 young
= pmd_young(*pmd
);
1661 dirty
= pmd_dirty(*pmd
);
1662 soft_dirty
= pmd_soft_dirty(*pmd
);
1664 pmdp_huge_split_prepare(vma
, haddr
, pmd
);
1665 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1666 pmd_populate(mm
, &_pmd
, pgtable
);
1668 for (i
= 0, addr
= haddr
; i
< HPAGE_PMD_NR
; i
++, addr
+= PAGE_SIZE
) {
1671 * Note that NUMA hinting access restrictions are not
1672 * transferred to avoid any possibility of altering
1673 * permissions across VMAs.
1676 swp_entry_t swp_entry
;
1677 swp_entry
= make_migration_entry(page
+ i
, write
);
1678 entry
= swp_entry_to_pte(swp_entry
);
1680 entry
= pte_swp_mksoft_dirty(entry
);
1682 entry
= mk_pte(page
+ i
, READ_ONCE(vma
->vm_page_prot
));
1683 entry
= maybe_mkwrite(entry
, vma
);
1685 entry
= pte_wrprotect(entry
);
1687 entry
= pte_mkold(entry
);
1689 entry
= pte_mksoft_dirty(entry
);
1692 SetPageDirty(page
+ i
);
1693 pte
= pte_offset_map(&_pmd
, addr
);
1694 BUG_ON(!pte_none(*pte
));
1695 set_pte_at(mm
, addr
, pte
, entry
);
1696 atomic_inc(&page
[i
]._mapcount
);
1701 * Set PG_double_map before dropping compound_mapcount to avoid
1702 * false-negative page_mapped().
1704 if (compound_mapcount(page
) > 1 && !TestSetPageDoubleMap(page
)) {
1705 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
1706 atomic_inc(&page
[i
]._mapcount
);
1709 if (atomic_add_negative(-1, compound_mapcount_ptr(page
))) {
1710 /* Last compound_mapcount is gone. */
1711 __dec_node_page_state(page
, NR_ANON_THPS
);
1712 if (TestClearPageDoubleMap(page
)) {
1713 /* No need in mapcount reference anymore */
1714 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
1715 atomic_dec(&page
[i
]._mapcount
);
1719 smp_wmb(); /* make pte visible before pmd */
1721 * Up to this point the pmd is present and huge and userland has the
1722 * whole access to the hugepage during the split (which happens in
1723 * place). If we overwrite the pmd with the not-huge version pointing
1724 * to the pte here (which of course we could if all CPUs were bug
1725 * free), userland could trigger a small page size TLB miss on the
1726 * small sized TLB while the hugepage TLB entry is still established in
1727 * the huge TLB. Some CPU doesn't like that.
1728 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
1729 * 383 on page 93. Intel should be safe but is also warns that it's
1730 * only safe if the permission and cache attributes of the two entries
1731 * loaded in the two TLB is identical (which should be the case here).
1732 * But it is generally safer to never allow small and huge TLB entries
1733 * for the same virtual address to be loaded simultaneously. So instead
1734 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
1735 * current pmd notpresent (atomically because here the pmd_trans_huge
1736 * and pmd_trans_splitting must remain set at all times on the pmd
1737 * until the split is complete for this pmd), then we flush the SMP TLB
1738 * and finally we write the non-huge version of the pmd entry with
1741 pmdp_invalidate(vma
, haddr
, pmd
);
1742 pmd_populate(mm
, pmd
, pgtable
);
1745 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1746 page_remove_rmap(page
+ i
, false);
1752 void __split_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1753 unsigned long address
, bool freeze
, struct page
*page
)
1756 struct mm_struct
*mm
= vma
->vm_mm
;
1757 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
1759 mmu_notifier_invalidate_range_start(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
1760 ptl
= pmd_lock(mm
, pmd
);
1763 * If caller asks to setup a migration entries, we need a page to check
1764 * pmd against. Otherwise we can end up replacing wrong page.
1766 VM_BUG_ON(freeze
&& !page
);
1767 if (page
&& page
!= pmd_page(*pmd
))
1770 if (pmd_trans_huge(*pmd
)) {
1771 page
= pmd_page(*pmd
);
1772 if (PageMlocked(page
))
1773 clear_page_mlock(page
);
1774 } else if (!pmd_devmap(*pmd
))
1776 __split_huge_pmd_locked(vma
, pmd
, haddr
, freeze
);
1779 mmu_notifier_invalidate_range_end(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
1782 void split_huge_pmd_address(struct vm_area_struct
*vma
, unsigned long address
,
1783 bool freeze
, struct page
*page
)
1789 pgd
= pgd_offset(vma
->vm_mm
, address
);
1790 if (!pgd_present(*pgd
))
1793 pud
= pud_offset(pgd
, address
);
1794 if (!pud_present(*pud
))
1797 pmd
= pmd_offset(pud
, address
);
1799 __split_huge_pmd(vma
, pmd
, address
, freeze
, page
);
1802 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
1803 unsigned long start
,
1808 * If the new start address isn't hpage aligned and it could
1809 * previously contain an hugepage: check if we need to split
1812 if (start
& ~HPAGE_PMD_MASK
&&
1813 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
1814 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
1815 split_huge_pmd_address(vma
, start
, false, NULL
);
1818 * If the new end address isn't hpage aligned and it could
1819 * previously contain an hugepage: check if we need to split
1822 if (end
& ~HPAGE_PMD_MASK
&&
1823 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
1824 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
1825 split_huge_pmd_address(vma
, end
, false, NULL
);
1828 * If we're also updating the vma->vm_next->vm_start, if the new
1829 * vm_next->vm_start isn't page aligned and it could previously
1830 * contain an hugepage: check if we need to split an huge pmd.
1832 if (adjust_next
> 0) {
1833 struct vm_area_struct
*next
= vma
->vm_next
;
1834 unsigned long nstart
= next
->vm_start
;
1835 nstart
+= adjust_next
<< PAGE_SHIFT
;
1836 if (nstart
& ~HPAGE_PMD_MASK
&&
1837 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
1838 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
1839 split_huge_pmd_address(next
, nstart
, false, NULL
);
1843 static void unmap_page(struct page
*page
)
1845 enum ttu_flags ttu_flags
= TTU_IGNORE_MLOCK
| TTU_IGNORE_ACCESS
|
1849 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1852 ttu_flags
|= TTU_MIGRATION
;
1854 /* We only need TTU_SPLIT_HUGE_PMD once */
1855 ret
= try_to_unmap(page
, ttu_flags
| TTU_SPLIT_HUGE_PMD
);
1856 for (i
= 1; !ret
&& i
< HPAGE_PMD_NR
; i
++) {
1857 /* Cut short if the page is unmapped */
1858 if (page_count(page
) == 1)
1861 ret
= try_to_unmap(page
+ i
, ttu_flags
);
1863 VM_BUG_ON_PAGE(ret
, page
+ i
- 1);
1866 static void remap_page(struct page
*page
)
1870 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
1871 remove_migration_ptes(page
+ i
, page
+ i
, true);
1874 static void __split_huge_page_tail(struct page
*head
, int tail
,
1875 struct lruvec
*lruvec
, struct list_head
*list
)
1877 struct page
*page_tail
= head
+ tail
;
1879 VM_BUG_ON_PAGE(atomic_read(&page_tail
->_mapcount
) != -1, page_tail
);
1882 * Clone page flags before unfreezing refcount.
1884 * After successful get_page_unless_zero() might follow flags change,
1885 * for exmaple lock_page() which set PG_waiters.
1887 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
1888 page_tail
->flags
|= (head
->flags
&
1889 ((1L << PG_referenced
) |
1890 (1L << PG_swapbacked
) |
1891 (1L << PG_mlocked
) |
1892 (1L << PG_uptodate
) |
1895 (1L << PG_unevictable
) |
1898 /* ->mapping in first tail page is compound_mapcount */
1899 VM_BUG_ON_PAGE(tail
> 2 && page_tail
->mapping
!= TAIL_MAPPING
,
1901 page_tail
->mapping
= head
->mapping
;
1902 page_tail
->index
= head
->index
+ tail
;
1904 /* Page flags must be visible before we make the page non-compound. */
1908 * Clear PageTail before unfreezing page refcount.
1910 * After successful get_page_unless_zero() might follow put_page()
1911 * which needs correct compound_head().
1913 clear_compound_head(page_tail
);
1915 /* Finally unfreeze refcount. Additional reference from page cache. */
1916 page_ref_unfreeze(page_tail
, 1 + (!PageAnon(head
) ||
1917 PageSwapCache(head
)));
1919 if (page_is_young(head
))
1920 set_page_young(page_tail
);
1921 if (page_is_idle(head
))
1922 set_page_idle(page_tail
);
1924 page_cpupid_xchg_last(page_tail
, page_cpupid_last(head
));
1925 lru_add_page_tail(head
, page_tail
, lruvec
, list
);
1928 static void __split_huge_page(struct page
*page
, struct list_head
*list
,
1929 pgoff_t end
, unsigned long flags
)
1931 struct page
*head
= compound_head(page
);
1932 struct zone
*zone
= page_zone(head
);
1933 struct lruvec
*lruvec
;
1936 lruvec
= mem_cgroup_page_lruvec(head
, zone
->zone_pgdat
);
1938 /* complete memcg works before add pages to LRU */
1939 mem_cgroup_split_huge_fixup(head
);
1941 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1942 __split_huge_page_tail(head
, i
, lruvec
, list
);
1943 /* Some pages can be beyond i_size: drop them from page cache */
1944 if (head
[i
].index
>= end
) {
1945 __ClearPageDirty(head
+ i
);
1946 __delete_from_page_cache(head
+ i
, NULL
);
1947 if (IS_ENABLED(CONFIG_SHMEM
) && PageSwapBacked(head
))
1948 shmem_uncharge(head
->mapping
->host
, 1);
1953 ClearPageCompound(head
);
1955 split_page_owner(head
, HPAGE_PMD_ORDER
);
1957 /* See comment in __split_huge_page_tail() */
1958 if (PageAnon(head
)) {
1961 /* Additional pin to radix tree */
1962 page_ref_add(head
, 2);
1963 spin_unlock(&head
->mapping
->tree_lock
);
1966 spin_unlock_irqrestore(zone_lru_lock(page_zone(head
)), flags
);
1970 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1971 struct page
*subpage
= head
+ i
;
1972 if (subpage
== page
)
1974 unlock_page(subpage
);
1977 * Subpages may be freed if there wasn't any mapping
1978 * like if add_to_swap() is running on a lru page that
1979 * had its mapping zapped. And freeing these pages
1980 * requires taking the lru_lock so we do the put_page
1981 * of the tail pages after the split is complete.
1987 int total_mapcount(struct page
*page
)
1989 int i
, compound
, ret
;
1991 VM_BUG_ON_PAGE(PageTail(page
), page
);
1993 if (likely(!PageCompound(page
)))
1994 return atomic_read(&page
->_mapcount
) + 1;
1996 compound
= compound_mapcount(page
);
2000 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
2001 ret
+= atomic_read(&page
[i
]._mapcount
) + 1;
2002 /* File pages has compound_mapcount included in _mapcount */
2003 if (!PageAnon(page
))
2004 return ret
- compound
* HPAGE_PMD_NR
;
2005 if (PageDoubleMap(page
))
2006 ret
-= HPAGE_PMD_NR
;
2011 * This calculates accurately how many mappings a transparent hugepage
2012 * has (unlike page_mapcount() which isn't fully accurate). This full
2013 * accuracy is primarily needed to know if copy-on-write faults can
2014 * reuse the page and change the mapping to read-write instead of
2015 * copying them. At the same time this returns the total_mapcount too.
2017 * The function returns the highest mapcount any one of the subpages
2018 * has. If the return value is one, even if different processes are
2019 * mapping different subpages of the transparent hugepage, they can
2020 * all reuse it, because each process is reusing a different subpage.
2022 * The total_mapcount is instead counting all virtual mappings of the
2023 * subpages. If the total_mapcount is equal to "one", it tells the
2024 * caller all mappings belong to the same "mm" and in turn the
2025 * anon_vma of the transparent hugepage can become the vma->anon_vma
2026 * local one as no other process may be mapping any of the subpages.
2028 * It would be more accurate to replace page_mapcount() with
2029 * page_trans_huge_mapcount(), however we only use
2030 * page_trans_huge_mapcount() in the copy-on-write faults where we
2031 * need full accuracy to avoid breaking page pinning, because
2032 * page_trans_huge_mapcount() is slower than page_mapcount().
2034 int page_trans_huge_mapcount(struct page
*page
, int *total_mapcount
)
2036 int i
, ret
, _total_mapcount
, mapcount
;
2038 /* hugetlbfs shouldn't call it */
2039 VM_BUG_ON_PAGE(PageHuge(page
), page
);
2041 if (likely(!PageTransCompound(page
))) {
2042 mapcount
= atomic_read(&page
->_mapcount
) + 1;
2044 *total_mapcount
= mapcount
;
2048 page
= compound_head(page
);
2050 _total_mapcount
= ret
= 0;
2051 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
2052 mapcount
= atomic_read(&page
[i
]._mapcount
) + 1;
2053 ret
= max(ret
, mapcount
);
2054 _total_mapcount
+= mapcount
;
2056 if (PageDoubleMap(page
)) {
2058 _total_mapcount
-= HPAGE_PMD_NR
;
2060 mapcount
= compound_mapcount(page
);
2062 _total_mapcount
+= mapcount
;
2064 *total_mapcount
= _total_mapcount
;
2069 * This function splits huge page into normal pages. @page can point to any
2070 * subpage of huge page to split. Split doesn't change the position of @page.
2072 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
2073 * The huge page must be locked.
2075 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
2077 * Both head page and tail pages will inherit mapping, flags, and so on from
2080 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
2081 * they are not mapped.
2083 * Returns 0 if the hugepage is split successfully.
2084 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
2087 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
2089 struct page
*head
= compound_head(page
);
2090 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(head
));
2091 struct anon_vma
*anon_vma
= NULL
;
2092 struct address_space
*mapping
= NULL
;
2093 int count
, mapcount
, extra_pins
, ret
;
2095 unsigned long flags
;
2098 VM_BUG_ON_PAGE(is_huge_zero_page(page
), page
);
2099 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2100 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
2101 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
2103 if (PageAnon(head
)) {
2105 * The caller does not necessarily hold an mmap_sem that would
2106 * prevent the anon_vma disappearing so we first we take a
2107 * reference to it and then lock the anon_vma for write. This
2108 * is similar to page_lock_anon_vma_read except the write lock
2109 * is taken to serialise against parallel split or collapse
2112 anon_vma
= page_get_anon_vma(head
);
2120 anon_vma_lock_write(anon_vma
);
2122 mapping
= head
->mapping
;
2130 /* Addidional pins from radix tree */
2131 extra_pins
= HPAGE_PMD_NR
;
2133 i_mmap_lock_read(mapping
);
2136 *__split_huge_page() may need to trim off pages beyond EOF:
2137 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2138 * which cannot be nested inside the page tree lock. So note
2139 * end now: i_size itself may be changed at any moment, but
2140 * head page lock is good enough to serialize the trimming.
2142 end
= DIV_ROUND_UP(i_size_read(mapping
->host
), PAGE_SIZE
);
2146 * Racy check if we can split the page, before unmap_page() will
2149 if (total_mapcount(head
) != page_count(head
) - extra_pins
- 1) {
2154 mlocked
= PageMlocked(page
);
2156 VM_BUG_ON_PAGE(compound_mapcount(head
), head
);
2158 /* Make sure the page is not on per-CPU pagevec as it takes pin */
2162 /* prevent PageLRU to go away from under us, and freeze lru stats */
2163 spin_lock_irqsave(zone_lru_lock(page_zone(head
)), flags
);
2168 spin_lock(&mapping
->tree_lock
);
2169 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
2172 * Check if the head page is present in radix tree.
2173 * We assume all tail are present too, if head is there.
2175 if (radix_tree_deref_slot_protected(pslot
,
2176 &mapping
->tree_lock
) != head
)
2180 /* Prevent deferred_split_scan() touching ->_refcount */
2181 spin_lock(&pgdata
->split_queue_lock
);
2182 count
= page_count(head
);
2183 mapcount
= total_mapcount(head
);
2184 if (!mapcount
&& page_ref_freeze(head
, 1 + extra_pins
)) {
2185 if (!list_empty(page_deferred_list(head
))) {
2186 pgdata
->split_queue_len
--;
2187 list_del(page_deferred_list(head
));
2190 __dec_node_page_state(page
, NR_SHMEM_THPS
);
2191 spin_unlock(&pgdata
->split_queue_lock
);
2192 __split_huge_page(page
, list
, end
, flags
);
2195 if (IS_ENABLED(CONFIG_DEBUG_VM
) && mapcount
) {
2196 pr_alert("total_mapcount: %u, page_count(): %u\n",
2199 dump_page(head
, NULL
);
2200 dump_page(page
, "total_mapcount(head) > 0");
2203 spin_unlock(&pgdata
->split_queue_lock
);
2205 spin_unlock(&mapping
->tree_lock
);
2206 spin_unlock_irqrestore(zone_lru_lock(page_zone(head
)), flags
);
2213 anon_vma_unlock_write(anon_vma
);
2214 put_anon_vma(anon_vma
);
2217 i_mmap_unlock_read(mapping
);
2219 count_vm_event(!ret
? THP_SPLIT_PAGE
: THP_SPLIT_PAGE_FAILED
);
2223 void free_transhuge_page(struct page
*page
)
2225 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
2226 unsigned long flags
;
2228 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
2229 if (!list_empty(page_deferred_list(page
))) {
2230 pgdata
->split_queue_len
--;
2231 list_del(page_deferred_list(page
));
2233 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
2234 free_compound_page(page
);
2237 void deferred_split_huge_page(struct page
*page
)
2239 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
2240 unsigned long flags
;
2242 VM_BUG_ON_PAGE(!PageTransHuge(page
), page
);
2244 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
2245 if (list_empty(page_deferred_list(page
))) {
2246 count_vm_event(THP_DEFERRED_SPLIT_PAGE
);
2247 list_add_tail(page_deferred_list(page
), &pgdata
->split_queue
);
2248 pgdata
->split_queue_len
++;
2250 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
2253 static unsigned long deferred_split_count(struct shrinker
*shrink
,
2254 struct shrink_control
*sc
)
2256 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
2257 return ACCESS_ONCE(pgdata
->split_queue_len
);
2260 static unsigned long deferred_split_scan(struct shrinker
*shrink
,
2261 struct shrink_control
*sc
)
2263 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
2264 unsigned long flags
;
2265 LIST_HEAD(list
), *pos
, *next
;
2269 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
2270 /* Take pin on all head pages to avoid freeing them under us */
2271 list_for_each_safe(pos
, next
, &pgdata
->split_queue
) {
2272 page
= list_entry((void *)pos
, struct page
, mapping
);
2273 page
= compound_head(page
);
2274 if (get_page_unless_zero(page
)) {
2275 list_move(page_deferred_list(page
), &list
);
2277 /* We lost race with put_compound_page() */
2278 list_del_init(page_deferred_list(page
));
2279 pgdata
->split_queue_len
--;
2281 if (!--sc
->nr_to_scan
)
2284 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
2286 list_for_each_safe(pos
, next
, &list
) {
2287 page
= list_entry((void *)pos
, struct page
, mapping
);
2288 if (!trylock_page(page
))
2290 /* split_huge_page() removes page from list on success */
2291 if (!split_huge_page(page
))
2298 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
2299 list_splice_tail(&list
, &pgdata
->split_queue
);
2300 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
2303 * Stop shrinker if we didn't split any page, but the queue is empty.
2304 * This can happen if pages were freed under us.
2306 if (!split
&& list_empty(&pgdata
->split_queue
))
2311 static struct shrinker deferred_split_shrinker
= {
2312 .count_objects
= deferred_split_count
,
2313 .scan_objects
= deferred_split_scan
,
2314 .seeks
= DEFAULT_SEEKS
,
2315 .flags
= SHRINKER_NUMA_AWARE
,
2318 #ifdef CONFIG_DEBUG_FS
2319 static int split_huge_pages_set(void *data
, u64 val
)
2323 unsigned long pfn
, max_zone_pfn
;
2324 unsigned long total
= 0, split
= 0;
2329 for_each_populated_zone(zone
) {
2330 max_zone_pfn
= zone_end_pfn(zone
);
2331 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++) {
2332 if (!pfn_valid(pfn
))
2335 page
= pfn_to_page(pfn
);
2336 if (!get_page_unless_zero(page
))
2339 if (zone
!= page_zone(page
))
2342 if (!PageHead(page
) || PageHuge(page
) || !PageLRU(page
))
2347 if (!split_huge_page(page
))
2355 pr_info("%lu of %lu THP split\n", split
, total
);
2359 DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops
, NULL
, split_huge_pages_set
,
2362 static int __init
split_huge_pages_debugfs(void)
2366 ret
= debugfs_create_file("split_huge_pages", 0200, NULL
, NULL
,
2367 &split_huge_pages_fops
);
2369 pr_warn("Failed to create split_huge_pages in debugfs");
2372 late_initcall(split_huge_pages_debugfs
);