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.
9 #include <linux/sched.h>
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/mm_inline.h>
16 #include <linux/kthread.h>
17 #include <linux/khugepaged.h>
18 #include <linux/freezer.h>
19 #include <linux/mman.h>
20 #include <linux/pagemap.h>
22 #include <asm/pgalloc.h>
26 * By default transparent hugepage support is enabled for all mappings
27 * and khugepaged scans all mappings. Defrag is only invoked by
28 * khugepaged hugepage allocations and by page faults inside
29 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
32 unsigned long transparent_hugepage_flags __read_mostly =
33 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
34 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
36 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
37 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
39 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
40 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
42 /* default scan 8*512 pte (or vmas) every 30 second */
43 static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
44 static unsigned int khugepaged_pages_collapsed;
45 static unsigned int khugepaged_full_scans;
46 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
47 /* during fragmentation poll the hugepage allocator once every minute */
48 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
49 static struct task_struct *khugepaged_thread __read_mostly;
50 static DEFINE_MUTEX(khugepaged_mutex);
51 static DEFINE_SPINLOCK(khugepaged_mm_lock);
52 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
54 * default collapse hugepages if there is at least one pte mapped like
55 * it would have happened if the vma was large enough during page
58 static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
60 static int khugepaged(void *none);
61 static int mm_slots_hash_init(void);
62 static int khugepaged_slab_init(void);
63 static void khugepaged_slab_free(void);
65 #define MM_SLOTS_HASH_HEADS 1024
66 static struct hlist_head *mm_slots_hash __read_mostly;
67 static struct kmem_cache *mm_slot_cache __read_mostly;
70 * struct mm_slot - hash lookup from mm to mm_slot
71 * @hash: hash collision list
72 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
73 * @mm: the mm that this information is valid for
76 struct hlist_node hash;
77 struct list_head mm_node;
82 * struct khugepaged_scan - cursor for scanning
83 * @mm_head: the head of the mm list to scan
84 * @mm_slot: the current mm_slot we are scanning
85 * @address: the next address inside that to be scanned
87 * There is only the one khugepaged_scan instance of this cursor structure.
89 struct khugepaged_scan {
90 struct list_head mm_head;
91 struct mm_slot *mm_slot;
92 unsigned long address;
94 static struct khugepaged_scan khugepaged_scan = {
95 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
99 static int set_recommended_min_free_kbytes(void)
103 unsigned long recommended_min;
104 extern int min_free_kbytes;
106 if (!khugepaged_enabled())
109 for_each_populated_zone(zone)
112 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
113 recommended_min = pageblock_nr_pages * nr_zones * 2;
116 * Make sure that on average at least two pageblocks are almost free
117 * of another type, one for a migratetype to fall back to and a
118 * second to avoid subsequent fallbacks of other types There are 3
119 * MIGRATE_TYPES we care about.
121 recommended_min += pageblock_nr_pages * nr_zones *
122 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
124 /* don't ever allow to reserve more than 5% of the lowmem */
125 recommended_min = min(recommended_min,
126 (unsigned long) nr_free_buffer_pages() / 20);
127 recommended_min <<= (PAGE_SHIFT-10);
129 if (recommended_min > min_free_kbytes)
130 min_free_kbytes = recommended_min;
131 setup_per_zone_wmarks();
134 late_initcall(set_recommended_min_free_kbytes);
136 static int start_khugepaged(void)
139 if (khugepaged_enabled()) {
140 if (!khugepaged_thread)
141 khugepaged_thread = kthread_run(khugepaged, NULL,
143 if (unlikely(IS_ERR(khugepaged_thread))) {
145 "khugepaged: kthread_run(khugepaged) failed\n");
146 err = PTR_ERR(khugepaged_thread);
147 khugepaged_thread = NULL;
150 if (!list_empty(&khugepaged_scan.mm_head))
151 wake_up_interruptible(&khugepaged_wait);
153 set_recommended_min_free_kbytes();
154 } else if (khugepaged_thread) {
155 kthread_stop(khugepaged_thread);
156 khugepaged_thread = NULL;
164 static ssize_t double_flag_show(struct kobject *kobj,
165 struct kobj_attribute *attr, char *buf,
166 enum transparent_hugepage_flag enabled,
167 enum transparent_hugepage_flag req_madv)
169 if (test_bit(enabled, &transparent_hugepage_flags)) {
170 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
171 return sprintf(buf, "[always] madvise never\n");
172 } else if (test_bit(req_madv, &transparent_hugepage_flags))
173 return sprintf(buf, "always [madvise] never\n");
175 return sprintf(buf, "always madvise [never]\n");
177 static ssize_t double_flag_store(struct kobject *kobj,
178 struct kobj_attribute *attr,
179 const char *buf, size_t count,
180 enum transparent_hugepage_flag enabled,
181 enum transparent_hugepage_flag req_madv)
183 if (!memcmp("always", buf,
184 min(sizeof("always")-1, count))) {
185 set_bit(enabled, &transparent_hugepage_flags);
186 clear_bit(req_madv, &transparent_hugepage_flags);
187 } else if (!memcmp("madvise", buf,
188 min(sizeof("madvise")-1, count))) {
189 clear_bit(enabled, &transparent_hugepage_flags);
190 set_bit(req_madv, &transparent_hugepage_flags);
191 } else if (!memcmp("never", buf,
192 min(sizeof("never")-1, count))) {
193 clear_bit(enabled, &transparent_hugepage_flags);
194 clear_bit(req_madv, &transparent_hugepage_flags);
201 static ssize_t enabled_show(struct kobject *kobj,
202 struct kobj_attribute *attr, char *buf)
204 return double_flag_show(kobj, attr, buf,
205 TRANSPARENT_HUGEPAGE_FLAG,
206 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
208 static ssize_t enabled_store(struct kobject *kobj,
209 struct kobj_attribute *attr,
210 const char *buf, size_t count)
214 ret = double_flag_store(kobj, attr, buf, count,
215 TRANSPARENT_HUGEPAGE_FLAG,
216 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
221 mutex_lock(&khugepaged_mutex);
222 err = start_khugepaged();
223 mutex_unlock(&khugepaged_mutex);
231 static struct kobj_attribute enabled_attr =
232 __ATTR(enabled, 0644, enabled_show, enabled_store);
234 static ssize_t single_flag_show(struct kobject *kobj,
235 struct kobj_attribute *attr, char *buf,
236 enum transparent_hugepage_flag flag)
238 return sprintf(buf, "%d\n",
239 !!test_bit(flag, &transparent_hugepage_flags));
242 static ssize_t single_flag_store(struct kobject *kobj,
243 struct kobj_attribute *attr,
244 const char *buf, size_t count,
245 enum transparent_hugepage_flag flag)
250 ret = kstrtoul(buf, 10, &value);
257 set_bit(flag, &transparent_hugepage_flags);
259 clear_bit(flag, &transparent_hugepage_flags);
265 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
266 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
267 * memory just to allocate one more hugepage.
269 static ssize_t defrag_show(struct kobject *kobj,
270 struct kobj_attribute *attr, char *buf)
272 return double_flag_show(kobj, attr, buf,
273 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
274 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
276 static ssize_t defrag_store(struct kobject *kobj,
277 struct kobj_attribute *attr,
278 const char *buf, size_t count)
280 return double_flag_store(kobj, attr, buf, count,
281 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
282 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
284 static struct kobj_attribute defrag_attr =
285 __ATTR(defrag, 0644, defrag_show, defrag_store);
287 #ifdef CONFIG_DEBUG_VM
288 static ssize_t debug_cow_show(struct kobject *kobj,
289 struct kobj_attribute *attr, char *buf)
291 return single_flag_show(kobj, attr, buf,
292 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
294 static ssize_t debug_cow_store(struct kobject *kobj,
295 struct kobj_attribute *attr,
296 const char *buf, size_t count)
298 return single_flag_store(kobj, attr, buf, count,
299 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
301 static struct kobj_attribute debug_cow_attr =
302 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
303 #endif /* CONFIG_DEBUG_VM */
305 static struct attribute *hugepage_attr[] = {
308 #ifdef CONFIG_DEBUG_VM
309 &debug_cow_attr.attr,
314 static struct attribute_group hugepage_attr_group = {
315 .attrs = hugepage_attr,
318 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
319 struct kobj_attribute *attr,
322 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
325 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
326 struct kobj_attribute *attr,
327 const char *buf, size_t count)
332 err = strict_strtoul(buf, 10, &msecs);
333 if (err || msecs > UINT_MAX)
336 khugepaged_scan_sleep_millisecs = msecs;
337 wake_up_interruptible(&khugepaged_wait);
341 static struct kobj_attribute scan_sleep_millisecs_attr =
342 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
343 scan_sleep_millisecs_store);
345 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
346 struct kobj_attribute *attr,
349 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
352 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
353 struct kobj_attribute *attr,
354 const char *buf, size_t count)
359 err = strict_strtoul(buf, 10, &msecs);
360 if (err || msecs > UINT_MAX)
363 khugepaged_alloc_sleep_millisecs = msecs;
364 wake_up_interruptible(&khugepaged_wait);
368 static struct kobj_attribute alloc_sleep_millisecs_attr =
369 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
370 alloc_sleep_millisecs_store);
372 static ssize_t pages_to_scan_show(struct kobject *kobj,
373 struct kobj_attribute *attr,
376 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
378 static ssize_t pages_to_scan_store(struct kobject *kobj,
379 struct kobj_attribute *attr,
380 const char *buf, size_t count)
385 err = strict_strtoul(buf, 10, &pages);
386 if (err || !pages || pages > UINT_MAX)
389 khugepaged_pages_to_scan = pages;
393 static struct kobj_attribute pages_to_scan_attr =
394 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
395 pages_to_scan_store);
397 static ssize_t pages_collapsed_show(struct kobject *kobj,
398 struct kobj_attribute *attr,
401 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
403 static struct kobj_attribute pages_collapsed_attr =
404 __ATTR_RO(pages_collapsed);
406 static ssize_t full_scans_show(struct kobject *kobj,
407 struct kobj_attribute *attr,
410 return sprintf(buf, "%u\n", khugepaged_full_scans);
412 static struct kobj_attribute full_scans_attr =
413 __ATTR_RO(full_scans);
415 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
416 struct kobj_attribute *attr, char *buf)
418 return single_flag_show(kobj, attr, buf,
419 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
421 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
422 struct kobj_attribute *attr,
423 const char *buf, size_t count)
425 return single_flag_store(kobj, attr, buf, count,
426 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
428 static struct kobj_attribute khugepaged_defrag_attr =
429 __ATTR(defrag, 0644, khugepaged_defrag_show,
430 khugepaged_defrag_store);
433 * max_ptes_none controls if khugepaged should collapse hugepages over
434 * any unmapped ptes in turn potentially increasing the memory
435 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
436 * reduce the available free memory in the system as it
437 * runs. Increasing max_ptes_none will instead potentially reduce the
438 * free memory in the system during the khugepaged scan.
440 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
441 struct kobj_attribute *attr,
444 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
446 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
447 struct kobj_attribute *attr,
448 const char *buf, size_t count)
451 unsigned long max_ptes_none;
453 err = strict_strtoul(buf, 10, &max_ptes_none);
454 if (err || max_ptes_none > HPAGE_PMD_NR-1)
457 khugepaged_max_ptes_none = max_ptes_none;
461 static struct kobj_attribute khugepaged_max_ptes_none_attr =
462 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
463 khugepaged_max_ptes_none_store);
465 static struct attribute *khugepaged_attr[] = {
466 &khugepaged_defrag_attr.attr,
467 &khugepaged_max_ptes_none_attr.attr,
468 &pages_to_scan_attr.attr,
469 &pages_collapsed_attr.attr,
470 &full_scans_attr.attr,
471 &scan_sleep_millisecs_attr.attr,
472 &alloc_sleep_millisecs_attr.attr,
476 static struct attribute_group khugepaged_attr_group = {
477 .attrs = khugepaged_attr,
478 .name = "khugepaged",
481 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
485 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
486 if (unlikely(!*hugepage_kobj)) {
487 printk(KERN_ERR "hugepage: failed kobject create\n");
491 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
493 printk(KERN_ERR "hugepage: failed register hugeage group\n");
497 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
499 printk(KERN_ERR "hugepage: failed register hugeage group\n");
500 goto remove_hp_group;
506 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
508 kobject_put(*hugepage_kobj);
512 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
514 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
515 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
516 kobject_put(hugepage_kobj);
519 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
524 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
527 #endif /* CONFIG_SYSFS */
529 static int __init hugepage_init(void)
532 struct kobject *hugepage_kobj;
534 if (!has_transparent_hugepage()) {
535 transparent_hugepage_flags = 0;
539 err = hugepage_init_sysfs(&hugepage_kobj);
543 err = khugepaged_slab_init();
547 err = mm_slots_hash_init();
549 khugepaged_slab_free();
554 * By default disable transparent hugepages on smaller systems,
555 * where the extra memory used could hurt more than TLB overhead
556 * is likely to save. The admin can still enable it through /sys.
558 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
559 transparent_hugepage_flags = 0;
565 hugepage_exit_sysfs(hugepage_kobj);
568 module_init(hugepage_init)
570 static int __init setup_transparent_hugepage(char *str)
575 if (!strcmp(str, "always")) {
576 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
577 &transparent_hugepage_flags);
578 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
579 &transparent_hugepage_flags);
581 } else if (!strcmp(str, "madvise")) {
582 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
583 &transparent_hugepage_flags);
584 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
585 &transparent_hugepage_flags);
587 } else if (!strcmp(str, "never")) {
588 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
589 &transparent_hugepage_flags);
590 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
591 &transparent_hugepage_flags);
597 "transparent_hugepage= cannot parse, ignored\n");
600 __setup("transparent_hugepage=", setup_transparent_hugepage);
602 static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
604 if (likely(vma->vm_flags & VM_WRITE))
605 pmd = pmd_mkwrite(pmd);
609 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
610 struct vm_area_struct *vma,
611 unsigned long haddr, pmd_t *pmd,
616 VM_BUG_ON(!PageCompound(page));
617 pgtable = pte_alloc_one(mm, haddr);
618 if (unlikely(!pgtable))
621 clear_huge_page(page, haddr, HPAGE_PMD_NR);
622 __SetPageUptodate(page);
624 spin_lock(&mm->page_table_lock);
625 if (unlikely(!pmd_none(*pmd))) {
626 spin_unlock(&mm->page_table_lock);
627 mem_cgroup_uncharge_page(page);
629 pte_free(mm, pgtable);
632 entry = mk_pmd(page, vma->vm_page_prot);
633 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
634 entry = pmd_mkhuge(entry);
636 * The spinlocking to take the lru_lock inside
637 * page_add_new_anon_rmap() acts as a full memory
638 * barrier to be sure clear_huge_page writes become
639 * visible after the set_pmd_at() write.
641 page_add_new_anon_rmap(page, vma, haddr);
642 set_pmd_at(mm, haddr, pmd, entry);
643 pgtable_trans_huge_deposit(mm, pgtable);
644 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
646 spin_unlock(&mm->page_table_lock);
652 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
654 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
657 static inline struct page *alloc_hugepage_vma(int defrag,
658 struct vm_area_struct *vma,
659 unsigned long haddr, int nd,
662 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
663 HPAGE_PMD_ORDER, vma, haddr, nd);
667 static inline struct page *alloc_hugepage(int defrag)
669 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
674 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
675 unsigned long address, pmd_t *pmd,
679 unsigned long haddr = address & HPAGE_PMD_MASK;
682 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
683 if (unlikely(anon_vma_prepare(vma)))
685 if (unlikely(khugepaged_enter(vma)))
687 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
688 vma, haddr, numa_node_id(), 0);
689 if (unlikely(!page)) {
690 count_vm_event(THP_FAULT_FALLBACK);
693 count_vm_event(THP_FAULT_ALLOC);
694 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
698 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
700 mem_cgroup_uncharge_page(page);
709 * Use __pte_alloc instead of pte_alloc_map, because we can't
710 * run pte_offset_map on the pmd, if an huge pmd could
711 * materialize from under us from a different thread.
713 if (unlikely(pmd_none(*pmd)) &&
714 unlikely(__pte_alloc(mm, vma, pmd, address)))
716 /* if an huge pmd materialized from under us just retry later */
717 if (unlikely(pmd_trans_huge(*pmd)))
720 * A regular pmd is established and it can't morph into a huge pmd
721 * from under us anymore at this point because we hold the mmap_sem
722 * read mode and khugepaged takes it in write mode. So now it's
723 * safe to run pte_offset_map().
725 pte = pte_offset_map(pmd, address);
726 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
729 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
730 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
731 struct vm_area_struct *vma)
733 struct page *src_page;
739 pgtable = pte_alloc_one(dst_mm, addr);
740 if (unlikely(!pgtable))
743 spin_lock(&dst_mm->page_table_lock);
744 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
748 if (unlikely(!pmd_trans_huge(pmd))) {
749 pte_free(dst_mm, pgtable);
752 if (unlikely(pmd_trans_splitting(pmd))) {
753 /* split huge page running from under us */
754 spin_unlock(&src_mm->page_table_lock);
755 spin_unlock(&dst_mm->page_table_lock);
756 pte_free(dst_mm, pgtable);
758 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
761 src_page = pmd_page(pmd);
762 VM_BUG_ON(!PageHead(src_page));
764 page_dup_rmap(src_page);
765 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
767 pmdp_set_wrprotect(src_mm, addr, src_pmd);
768 pmd = pmd_mkold(pmd_wrprotect(pmd));
769 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
770 pgtable_trans_huge_deposit(dst_mm, pgtable);
775 spin_unlock(&src_mm->page_table_lock);
776 spin_unlock(&dst_mm->page_table_lock);
781 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
782 struct vm_area_struct *vma,
783 unsigned long address,
784 pmd_t *pmd, pmd_t orig_pmd,
792 unsigned long mmun_start; /* For mmu_notifiers */
793 unsigned long mmun_end; /* For mmu_notifiers */
795 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
797 if (unlikely(!pages)) {
802 for (i = 0; i < HPAGE_PMD_NR; i++) {
803 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
805 vma, address, page_to_nid(page));
806 if (unlikely(!pages[i] ||
807 mem_cgroup_newpage_charge(pages[i], mm,
811 mem_cgroup_uncharge_start();
813 mem_cgroup_uncharge_page(pages[i]);
816 mem_cgroup_uncharge_end();
823 for (i = 0; i < HPAGE_PMD_NR; i++) {
824 copy_user_highpage(pages[i], page + i,
825 haddr + PAGE_SIZE * i, vma);
826 __SetPageUptodate(pages[i]);
831 mmun_end = haddr + HPAGE_PMD_SIZE;
832 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
834 spin_lock(&mm->page_table_lock);
835 if (unlikely(!pmd_same(*pmd, orig_pmd)))
837 VM_BUG_ON(!PageHead(page));
839 pmdp_clear_flush(vma, haddr, pmd);
840 /* leave pmd empty until pte is filled */
842 pgtable = pgtable_trans_huge_withdraw(mm);
843 pmd_populate(mm, &_pmd, pgtable);
845 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
847 entry = mk_pte(pages[i], vma->vm_page_prot);
848 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
849 page_add_new_anon_rmap(pages[i], vma, haddr);
850 pte = pte_offset_map(&_pmd, haddr);
851 VM_BUG_ON(!pte_none(*pte));
852 set_pte_at(mm, haddr, pte, entry);
857 smp_wmb(); /* make pte visible before pmd */
858 pmd_populate(mm, pmd, pgtable);
859 page_remove_rmap(page);
860 spin_unlock(&mm->page_table_lock);
862 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
864 ret |= VM_FAULT_WRITE;
871 spin_unlock(&mm->page_table_lock);
872 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
873 mem_cgroup_uncharge_start();
874 for (i = 0; i < HPAGE_PMD_NR; i++) {
875 mem_cgroup_uncharge_page(pages[i]);
878 mem_cgroup_uncharge_end();
883 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
884 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
887 struct page *page, *new_page;
889 unsigned long mmun_start; /* For mmu_notifiers */
890 unsigned long mmun_end; /* For mmu_notifiers */
892 VM_BUG_ON(!vma->anon_vma);
893 spin_lock(&mm->page_table_lock);
894 if (unlikely(!pmd_same(*pmd, orig_pmd)))
897 page = pmd_page(orig_pmd);
898 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
899 haddr = address & HPAGE_PMD_MASK;
900 if (page_mapcount(page) == 1) {
902 entry = pmd_mkyoung(orig_pmd);
903 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
904 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
905 update_mmu_cache_pmd(vma, address, pmd);
906 ret |= VM_FAULT_WRITE;
910 spin_unlock(&mm->page_table_lock);
912 if (transparent_hugepage_enabled(vma) &&
913 !transparent_hugepage_debug_cow())
914 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
915 vma, haddr, numa_node_id(), 0);
919 if (unlikely(!new_page)) {
920 count_vm_event(THP_FAULT_FALLBACK);
921 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
922 pmd, orig_pmd, page, haddr);
923 if (ret & VM_FAULT_OOM)
924 split_huge_page(page);
928 count_vm_event(THP_FAULT_ALLOC);
930 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
932 split_huge_page(page);
938 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
939 __SetPageUptodate(new_page);
942 mmun_end = haddr + HPAGE_PMD_SIZE;
943 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
945 spin_lock(&mm->page_table_lock);
947 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
948 spin_unlock(&mm->page_table_lock);
949 mem_cgroup_uncharge_page(new_page);
954 VM_BUG_ON(!PageHead(page));
955 entry = mk_pmd(new_page, vma->vm_page_prot);
956 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
957 entry = pmd_mkhuge(entry);
958 pmdp_clear_flush(vma, haddr, pmd);
959 page_add_new_anon_rmap(new_page, vma, haddr);
960 set_pmd_at(mm, haddr, pmd, entry);
961 update_mmu_cache_pmd(vma, address, pmd);
962 page_remove_rmap(page);
964 ret |= VM_FAULT_WRITE;
966 spin_unlock(&mm->page_table_lock);
968 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
972 spin_unlock(&mm->page_table_lock);
976 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
981 struct mm_struct *mm = vma->vm_mm;
982 struct page *page = NULL;
984 assert_spin_locked(&mm->page_table_lock);
986 if (flags & FOLL_WRITE && !pmd_write(*pmd))
989 page = pmd_page(*pmd);
990 VM_BUG_ON(!PageHead(page));
991 if (flags & FOLL_TOUCH) {
994 * We should set the dirty bit only for FOLL_WRITE but
995 * for now the dirty bit in the pmd is meaningless.
996 * And if the dirty bit will become meaningful and
997 * we'll only set it with FOLL_WRITE, an atomic
998 * set_bit will be required on the pmd to set the
999 * young bit, instead of the current set_pmd_at.
1001 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1002 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1004 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1005 if (page->mapping && trylock_page(page)) {
1008 mlock_vma_page(page);
1012 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1013 VM_BUG_ON(!PageCompound(page));
1014 if (flags & FOLL_GET)
1015 get_page_foll(page);
1021 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1022 pmd_t *pmd, unsigned long addr)
1026 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1030 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1031 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1032 page = pmd_page(orig_pmd);
1033 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1034 page_remove_rmap(page);
1035 VM_BUG_ON(page_mapcount(page) < 0);
1036 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1037 VM_BUG_ON(!PageHead(page));
1039 spin_unlock(&tlb->mm->page_table_lock);
1040 tlb_remove_page(tlb, page);
1041 pte_free(tlb->mm, pgtable);
1047 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1048 unsigned long addr, unsigned long end,
1053 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1055 * All logical pages in the range are present
1056 * if backed by a huge page.
1058 spin_unlock(&vma->vm_mm->page_table_lock);
1059 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1066 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1067 unsigned long old_addr,
1068 unsigned long new_addr, unsigned long old_end,
1069 pmd_t *old_pmd, pmd_t *new_pmd)
1074 struct mm_struct *mm = vma->vm_mm;
1076 if ((old_addr & ~HPAGE_PMD_MASK) ||
1077 (new_addr & ~HPAGE_PMD_MASK) ||
1078 old_end - old_addr < HPAGE_PMD_SIZE ||
1079 (new_vma->vm_flags & VM_NOHUGEPAGE))
1083 * The destination pmd shouldn't be established, free_pgtables()
1084 * should have release it.
1086 if (WARN_ON(!pmd_none(*new_pmd))) {
1087 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1091 ret = __pmd_trans_huge_lock(old_pmd, vma);
1093 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1094 VM_BUG_ON(!pmd_none(*new_pmd));
1095 set_pmd_at(mm, new_addr, new_pmd, pmd);
1096 spin_unlock(&mm->page_table_lock);
1102 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1103 unsigned long addr, pgprot_t newprot)
1105 struct mm_struct *mm = vma->vm_mm;
1108 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1110 entry = pmdp_get_and_clear(mm, addr, pmd);
1111 entry = pmd_modify(entry, newprot);
1112 set_pmd_at(mm, addr, pmd, entry);
1113 spin_unlock(&vma->vm_mm->page_table_lock);
1121 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1122 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1124 * Note that if it returns 1, this routine returns without unlocking page
1125 * table locks. So callers must unlock them.
1127 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1129 spin_lock(&vma->vm_mm->page_table_lock);
1130 if (likely(pmd_trans_huge(*pmd))) {
1131 if (unlikely(pmd_trans_splitting(*pmd))) {
1132 spin_unlock(&vma->vm_mm->page_table_lock);
1133 wait_split_huge_page(vma->anon_vma, pmd);
1136 /* Thp mapped by 'pmd' is stable, so we can
1137 * handle it as it is. */
1141 spin_unlock(&vma->vm_mm->page_table_lock);
1145 pmd_t *page_check_address_pmd(struct page *page,
1146 struct mm_struct *mm,
1147 unsigned long address,
1148 enum page_check_address_pmd_flag flag)
1152 pmd_t *pmd, *ret = NULL;
1154 if (address & ~HPAGE_PMD_MASK)
1157 pgd = pgd_offset(mm, address);
1158 if (!pgd_present(*pgd))
1161 pud = pud_offset(pgd, address);
1162 if (!pud_present(*pud))
1165 pmd = pmd_offset(pud, address);
1168 if (pmd_page(*pmd) != page)
1171 * split_vma() may create temporary aliased mappings. There is
1172 * no risk as long as all huge pmd are found and have their
1173 * splitting bit set before __split_huge_page_refcount
1174 * runs. Finding the same huge pmd more than once during the
1175 * same rmap walk is not a problem.
1177 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1178 pmd_trans_splitting(*pmd))
1180 if (pmd_trans_huge(*pmd)) {
1181 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1182 !pmd_trans_splitting(*pmd));
1189 static int __split_huge_page_splitting(struct page *page,
1190 struct vm_area_struct *vma,
1191 unsigned long address)
1193 struct mm_struct *mm = vma->vm_mm;
1196 /* For mmu_notifiers */
1197 const unsigned long mmun_start = address;
1198 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1200 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1201 spin_lock(&mm->page_table_lock);
1202 pmd = page_check_address_pmd(page, mm, address,
1203 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1206 * We can't temporarily set the pmd to null in order
1207 * to split it, the pmd must remain marked huge at all
1208 * times or the VM won't take the pmd_trans_huge paths
1209 * and it won't wait on the anon_vma->root->mutex to
1210 * serialize against split_huge_page*.
1212 pmdp_splitting_flush(vma, address, pmd);
1215 spin_unlock(&mm->page_table_lock);
1216 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1221 static void __split_huge_page_refcount(struct page *page)
1224 struct zone *zone = page_zone(page);
1225 struct lruvec *lruvec;
1228 /* prevent PageLRU to go away from under us, and freeze lru stats */
1229 spin_lock_irq(&zone->lru_lock);
1230 lruvec = mem_cgroup_page_lruvec(page, zone);
1232 compound_lock(page);
1233 /* complete memcg works before add pages to LRU */
1234 mem_cgroup_split_huge_fixup(page);
1236 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1237 struct page *page_tail = page + i;
1239 /* tail_page->_mapcount cannot change */
1240 BUG_ON(page_mapcount(page_tail) < 0);
1241 tail_count += page_mapcount(page_tail);
1242 /* check for overflow */
1243 BUG_ON(tail_count < 0);
1244 BUG_ON(atomic_read(&page_tail->_count) != 0);
1246 * tail_page->_count is zero and not changing from
1247 * under us. But get_page_unless_zero() may be running
1248 * from under us on the tail_page. If we used
1249 * atomic_set() below instead of atomic_add(), we
1250 * would then run atomic_set() concurrently with
1251 * get_page_unless_zero(), and atomic_set() is
1252 * implemented in C not using locked ops. spin_unlock
1253 * on x86 sometime uses locked ops because of PPro
1254 * errata 66, 92, so unless somebody can guarantee
1255 * atomic_set() here would be safe on all archs (and
1256 * not only on x86), it's safer to use atomic_add().
1258 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1259 &page_tail->_count);
1261 /* after clearing PageTail the gup refcount can be released */
1265 * retain hwpoison flag of the poisoned tail page:
1266 * fix for the unsuitable process killed on Guest Machine(KVM)
1267 * by the memory-failure.
1269 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1270 page_tail->flags |= (page->flags &
1271 ((1L << PG_referenced) |
1272 (1L << PG_swapbacked) |
1273 (1L << PG_mlocked) |
1274 (1L << PG_uptodate)));
1275 page_tail->flags |= (1L << PG_dirty);
1277 /* clear PageTail before overwriting first_page */
1281 * __split_huge_page_splitting() already set the
1282 * splitting bit in all pmd that could map this
1283 * hugepage, that will ensure no CPU can alter the
1284 * mapcount on the head page. The mapcount is only
1285 * accounted in the head page and it has to be
1286 * transferred to all tail pages in the below code. So
1287 * for this code to be safe, the split the mapcount
1288 * can't change. But that doesn't mean userland can't
1289 * keep changing and reading the page contents while
1290 * we transfer the mapcount, so the pmd splitting
1291 * status is achieved setting a reserved bit in the
1292 * pmd, not by clearing the present bit.
1294 page_tail->_mapcount = page->_mapcount;
1296 BUG_ON(page_tail->mapping);
1297 page_tail->mapping = page->mapping;
1299 page_tail->index = page->index + i;
1301 BUG_ON(!PageAnon(page_tail));
1302 BUG_ON(!PageUptodate(page_tail));
1303 BUG_ON(!PageDirty(page_tail));
1304 BUG_ON(!PageSwapBacked(page_tail));
1306 lru_add_page_tail(page, page_tail, lruvec);
1308 atomic_sub(tail_count, &page->_count);
1309 BUG_ON(atomic_read(&page->_count) <= 0);
1311 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1312 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1314 ClearPageCompound(page);
1315 compound_unlock(page);
1316 spin_unlock_irq(&zone->lru_lock);
1318 for (i = 1; i < HPAGE_PMD_NR; i++) {
1319 struct page *page_tail = page + i;
1320 BUG_ON(page_count(page_tail) <= 0);
1322 * Tail pages may be freed if there wasn't any mapping
1323 * like if add_to_swap() is running on a lru page that
1324 * had its mapping zapped. And freeing these pages
1325 * requires taking the lru_lock so we do the put_page
1326 * of the tail pages after the split is complete.
1328 put_page(page_tail);
1332 * Only the head page (now become a regular page) is required
1333 * to be pinned by the caller.
1335 BUG_ON(page_count(page) <= 0);
1338 static int __split_huge_page_map(struct page *page,
1339 struct vm_area_struct *vma,
1340 unsigned long address)
1342 struct mm_struct *mm = vma->vm_mm;
1346 unsigned long haddr;
1348 spin_lock(&mm->page_table_lock);
1349 pmd = page_check_address_pmd(page, mm, address,
1350 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1352 pgtable = pgtable_trans_huge_withdraw(mm);
1353 pmd_populate(mm, &_pmd, pgtable);
1356 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1358 BUG_ON(PageCompound(page+i));
1359 entry = mk_pte(page + i, vma->vm_page_prot);
1360 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1361 if (!pmd_write(*pmd))
1362 entry = pte_wrprotect(entry);
1364 BUG_ON(page_mapcount(page) != 1);
1365 if (!pmd_young(*pmd))
1366 entry = pte_mkold(entry);
1368 entry = pte_mknuma(entry);
1369 pte = pte_offset_map(&_pmd, haddr);
1370 BUG_ON(!pte_none(*pte));
1371 set_pte_at(mm, haddr, pte, entry);
1375 smp_wmb(); /* make pte visible before pmd */
1377 * Up to this point the pmd is present and huge and
1378 * userland has the whole access to the hugepage
1379 * during the split (which happens in place). If we
1380 * overwrite the pmd with the not-huge version
1381 * pointing to the pte here (which of course we could
1382 * if all CPUs were bug free), userland could trigger
1383 * a small page size TLB miss on the small sized TLB
1384 * while the hugepage TLB entry is still established
1385 * in the huge TLB. Some CPU doesn't like that. See
1386 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1387 * Erratum 383 on page 93. Intel should be safe but is
1388 * also warns that it's only safe if the permission
1389 * and cache attributes of the two entries loaded in
1390 * the two TLB is identical (which should be the case
1391 * here). But it is generally safer to never allow
1392 * small and huge TLB entries for the same virtual
1393 * address to be loaded simultaneously. So instead of
1394 * doing "pmd_populate(); flush_tlb_range();" we first
1395 * mark the current pmd notpresent (atomically because
1396 * here the pmd_trans_huge and pmd_trans_splitting
1397 * must remain set at all times on the pmd until the
1398 * split is complete for this pmd), then we flush the
1399 * SMP TLB and finally we write the non-huge version
1400 * of the pmd entry with pmd_populate.
1402 pmdp_invalidate(vma, address, pmd);
1403 pmd_populate(mm, pmd, pgtable);
1406 spin_unlock(&mm->page_table_lock);
1411 /* must be called with anon_vma->root->mutex hold */
1412 static void __split_huge_page(struct page *page,
1413 struct anon_vma *anon_vma)
1415 int mapcount, mapcount2;
1416 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1417 struct anon_vma_chain *avc;
1419 BUG_ON(!PageHead(page));
1420 BUG_ON(PageTail(page));
1423 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1424 struct vm_area_struct *vma = avc->vma;
1425 unsigned long addr = vma_address(page, vma);
1426 BUG_ON(is_vma_temporary_stack(vma));
1427 mapcount += __split_huge_page_splitting(page, vma, addr);
1430 * It is critical that new vmas are added to the tail of the
1431 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1432 * and establishes a child pmd before
1433 * __split_huge_page_splitting() freezes the parent pmd (so if
1434 * we fail to prevent copy_huge_pmd() from running until the
1435 * whole __split_huge_page() is complete), we will still see
1436 * the newly established pmd of the child later during the
1437 * walk, to be able to set it as pmd_trans_splitting too.
1439 if (mapcount != page_mapcount(page))
1440 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1441 mapcount, page_mapcount(page));
1442 BUG_ON(mapcount != page_mapcount(page));
1444 __split_huge_page_refcount(page);
1447 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1448 struct vm_area_struct *vma = avc->vma;
1449 unsigned long addr = vma_address(page, vma);
1450 BUG_ON(is_vma_temporary_stack(vma));
1451 mapcount2 += __split_huge_page_map(page, vma, addr);
1453 if (mapcount != mapcount2)
1454 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1455 mapcount, mapcount2, page_mapcount(page));
1456 BUG_ON(mapcount != mapcount2);
1459 int split_huge_page(struct page *page)
1461 struct anon_vma *anon_vma;
1464 BUG_ON(!PageAnon(page));
1465 anon_vma = page_lock_anon_vma(page);
1469 if (!PageCompound(page))
1472 BUG_ON(!PageSwapBacked(page));
1473 __split_huge_page(page, anon_vma);
1474 count_vm_event(THP_SPLIT);
1476 BUG_ON(PageCompound(page));
1478 page_unlock_anon_vma(anon_vma);
1483 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1485 int hugepage_madvise(struct vm_area_struct *vma,
1486 unsigned long *vm_flags, int advice)
1488 struct mm_struct *mm = vma->vm_mm;
1493 * Be somewhat over-protective like KSM for now!
1495 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1497 if (mm->def_flags & VM_NOHUGEPAGE)
1499 *vm_flags &= ~VM_NOHUGEPAGE;
1500 *vm_flags |= VM_HUGEPAGE;
1502 * If the vma become good for khugepaged to scan,
1503 * register it here without waiting a page fault that
1504 * may not happen any time soon.
1506 if (unlikely(khugepaged_enter_vma_merge(vma)))
1509 case MADV_NOHUGEPAGE:
1511 * Be somewhat over-protective like KSM for now!
1513 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1515 *vm_flags &= ~VM_HUGEPAGE;
1516 *vm_flags |= VM_NOHUGEPAGE;
1518 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1519 * this vma even if we leave the mm registered in khugepaged if
1520 * it got registered before VM_NOHUGEPAGE was set.
1528 static int __init khugepaged_slab_init(void)
1530 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1531 sizeof(struct mm_slot),
1532 __alignof__(struct mm_slot), 0, NULL);
1539 static void __init khugepaged_slab_free(void)
1541 kmem_cache_destroy(mm_slot_cache);
1542 mm_slot_cache = NULL;
1545 static inline struct mm_slot *alloc_mm_slot(void)
1547 if (!mm_slot_cache) /* initialization failed */
1549 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1552 static inline void free_mm_slot(struct mm_slot *mm_slot)
1554 kmem_cache_free(mm_slot_cache, mm_slot);
1557 static int __init mm_slots_hash_init(void)
1559 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1567 static void __init mm_slots_hash_free(void)
1569 kfree(mm_slots_hash);
1570 mm_slots_hash = NULL;
1574 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1576 struct mm_slot *mm_slot;
1577 struct hlist_head *bucket;
1578 struct hlist_node *node;
1580 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1581 % MM_SLOTS_HASH_HEADS];
1582 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1583 if (mm == mm_slot->mm)
1589 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1590 struct mm_slot *mm_slot)
1592 struct hlist_head *bucket;
1594 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1595 % MM_SLOTS_HASH_HEADS];
1597 hlist_add_head(&mm_slot->hash, bucket);
1600 static inline int khugepaged_test_exit(struct mm_struct *mm)
1602 return atomic_read(&mm->mm_users) == 0;
1605 int __khugepaged_enter(struct mm_struct *mm)
1607 struct mm_slot *mm_slot;
1610 mm_slot = alloc_mm_slot();
1614 /* __khugepaged_exit() must not run from under us */
1615 VM_BUG_ON(khugepaged_test_exit(mm));
1616 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1617 free_mm_slot(mm_slot);
1621 spin_lock(&khugepaged_mm_lock);
1622 insert_to_mm_slots_hash(mm, mm_slot);
1624 * Insert just behind the scanning cursor, to let the area settle
1627 wakeup = list_empty(&khugepaged_scan.mm_head);
1628 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1629 spin_unlock(&khugepaged_mm_lock);
1631 atomic_inc(&mm->mm_count);
1633 wake_up_interruptible(&khugepaged_wait);
1638 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1640 unsigned long hstart, hend;
1643 * Not yet faulted in so we will register later in the
1644 * page fault if needed.
1648 /* khugepaged not yet working on file or special mappings */
1650 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1651 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1652 hend = vma->vm_end & HPAGE_PMD_MASK;
1654 return khugepaged_enter(vma);
1658 void __khugepaged_exit(struct mm_struct *mm)
1660 struct mm_slot *mm_slot;
1663 spin_lock(&khugepaged_mm_lock);
1664 mm_slot = get_mm_slot(mm);
1665 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1666 hlist_del(&mm_slot->hash);
1667 list_del(&mm_slot->mm_node);
1670 spin_unlock(&khugepaged_mm_lock);
1673 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1674 free_mm_slot(mm_slot);
1676 } else if (mm_slot) {
1678 * This is required to serialize against
1679 * khugepaged_test_exit() (which is guaranteed to run
1680 * under mmap sem read mode). Stop here (after we
1681 * return all pagetables will be destroyed) until
1682 * khugepaged has finished working on the pagetables
1683 * under the mmap_sem.
1685 down_write(&mm->mmap_sem);
1686 up_write(&mm->mmap_sem);
1690 static void release_pte_page(struct page *page)
1692 /* 0 stands for page_is_file_cache(page) == false */
1693 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1695 putback_lru_page(page);
1698 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1700 while (--_pte >= pte) {
1701 pte_t pteval = *_pte;
1702 if (!pte_none(pteval))
1703 release_pte_page(pte_page(pteval));
1707 static void release_all_pte_pages(pte_t *pte)
1709 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1712 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1713 unsigned long address,
1718 int referenced = 0, isolated = 0, none = 0;
1719 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1720 _pte++, address += PAGE_SIZE) {
1721 pte_t pteval = *_pte;
1722 if (pte_none(pteval)) {
1723 if (++none <= khugepaged_max_ptes_none)
1726 release_pte_pages(pte, _pte);
1730 if (!pte_present(pteval) || !pte_write(pteval)) {
1731 release_pte_pages(pte, _pte);
1734 page = vm_normal_page(vma, address, pteval);
1735 if (unlikely(!page)) {
1736 release_pte_pages(pte, _pte);
1739 VM_BUG_ON(PageCompound(page));
1740 BUG_ON(!PageAnon(page));
1741 VM_BUG_ON(!PageSwapBacked(page));
1743 /* cannot use mapcount: can't collapse if there's a gup pin */
1744 if (page_count(page) != 1) {
1745 release_pte_pages(pte, _pte);
1749 * We can do it before isolate_lru_page because the
1750 * page can't be freed from under us. NOTE: PG_lock
1751 * is needed to serialize against split_huge_page
1752 * when invoked from the VM.
1754 if (!trylock_page(page)) {
1755 release_pte_pages(pte, _pte);
1759 * Isolate the page to avoid collapsing an hugepage
1760 * currently in use by the VM.
1762 if (isolate_lru_page(page)) {
1764 release_pte_pages(pte, _pte);
1767 /* 0 stands for page_is_file_cache(page) == false */
1768 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1769 VM_BUG_ON(!PageLocked(page));
1770 VM_BUG_ON(PageLRU(page));
1772 /* If there is no mapped pte young don't collapse the page */
1773 if (pte_young(pteval) || PageReferenced(page) ||
1774 mmu_notifier_test_young(vma->vm_mm, address))
1777 if (unlikely(!referenced))
1778 release_all_pte_pages(pte);
1785 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1786 struct vm_area_struct *vma,
1787 unsigned long address,
1791 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1792 pte_t pteval = *_pte;
1793 struct page *src_page;
1795 if (pte_none(pteval)) {
1796 clear_user_highpage(page, address);
1797 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1799 src_page = pte_page(pteval);
1800 copy_user_highpage(page, src_page, address, vma);
1801 VM_BUG_ON(page_mapcount(src_page) != 1);
1802 release_pte_page(src_page);
1804 * ptl mostly unnecessary, but preempt has to
1805 * be disabled to update the per-cpu stats
1806 * inside page_remove_rmap().
1810 * paravirt calls inside pte_clear here are
1813 pte_clear(vma->vm_mm, address, _pte);
1814 page_remove_rmap(src_page);
1816 free_page_and_swap_cache(src_page);
1819 address += PAGE_SIZE;
1824 static void khugepaged_alloc_sleep(void)
1826 wait_event_freezable_timeout(khugepaged_wait, false,
1827 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
1831 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1833 if (IS_ERR(*hpage)) {
1839 khugepaged_alloc_sleep();
1840 } else if (*hpage) {
1849 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1850 struct vm_area_struct *vma, unsigned long address,
1855 * Allocate the page while the vma is still valid and under
1856 * the mmap_sem read mode so there is no memory allocation
1857 * later when we take the mmap_sem in write mode. This is more
1858 * friendly behavior (OTOH it may actually hide bugs) to
1859 * filesystems in userland with daemons allocating memory in
1860 * the userland I/O paths. Allocating memory with the
1861 * mmap_sem in read mode is good idea also to allow greater
1864 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1865 node, __GFP_OTHER_NODE);
1868 * After allocating the hugepage, release the mmap_sem read lock in
1869 * preparation for taking it in write mode.
1871 up_read(&mm->mmap_sem);
1872 if (unlikely(!*hpage)) {
1873 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1874 *hpage = ERR_PTR(-ENOMEM);
1878 count_vm_event(THP_COLLAPSE_ALLOC);
1882 static struct page *khugepaged_alloc_hugepage(bool *wait)
1887 hpage = alloc_hugepage(khugepaged_defrag());
1889 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1894 khugepaged_alloc_sleep();
1896 count_vm_event(THP_COLLAPSE_ALLOC);
1897 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
1902 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1905 *hpage = khugepaged_alloc_hugepage(wait);
1907 if (unlikely(!*hpage))
1914 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1915 struct vm_area_struct *vma, unsigned long address,
1918 up_read(&mm->mmap_sem);
1924 static void collapse_huge_page(struct mm_struct *mm,
1925 unsigned long address,
1926 struct page **hpage,
1927 struct vm_area_struct *vma,
1935 struct page *new_page;
1938 unsigned long hstart, hend;
1939 unsigned long mmun_start; /* For mmu_notifiers */
1940 unsigned long mmun_end; /* For mmu_notifiers */
1942 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1944 /* release the mmap_sem read lock. */
1945 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
1949 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
1953 * Prevent all access to pagetables with the exception of
1954 * gup_fast later hanlded by the ptep_clear_flush and the VM
1955 * handled by the anon_vma lock + PG_lock.
1957 down_write(&mm->mmap_sem);
1958 if (unlikely(khugepaged_test_exit(mm)))
1961 vma = find_vma(mm, address);
1962 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1963 hend = vma->vm_end & HPAGE_PMD_MASK;
1964 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1967 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1968 (vma->vm_flags & VM_NOHUGEPAGE))
1971 if (!vma->anon_vma || vma->vm_ops)
1973 if (is_vma_temporary_stack(vma))
1975 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1977 pgd = pgd_offset(mm, address);
1978 if (!pgd_present(*pgd))
1981 pud = pud_offset(pgd, address);
1982 if (!pud_present(*pud))
1985 pmd = pmd_offset(pud, address);
1986 /* pmd can't go away or become huge under us */
1987 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1990 anon_vma_lock(vma->anon_vma);
1992 pte = pte_offset_map(pmd, address);
1993 ptl = pte_lockptr(mm, pmd);
1995 mmun_start = address;
1996 mmun_end = address + HPAGE_PMD_SIZE;
1997 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1998 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2000 * After this gup_fast can't run anymore. This also removes
2001 * any huge TLB entry from the CPU so we won't allow
2002 * huge and small TLB entries for the same virtual address
2003 * to avoid the risk of CPU bugs in that area.
2005 _pmd = pmdp_clear_flush(vma, address, pmd);
2006 spin_unlock(&mm->page_table_lock);
2007 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2010 isolated = __collapse_huge_page_isolate(vma, address, pte);
2013 if (unlikely(!isolated)) {
2015 spin_lock(&mm->page_table_lock);
2016 BUG_ON(!pmd_none(*pmd));
2017 set_pmd_at(mm, address, pmd, _pmd);
2018 spin_unlock(&mm->page_table_lock);
2019 anon_vma_unlock(vma->anon_vma);
2024 * All pages are isolated and locked so anon_vma rmap
2025 * can't run anymore.
2027 anon_vma_unlock(vma->anon_vma);
2029 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2031 __SetPageUptodate(new_page);
2032 pgtable = pmd_pgtable(_pmd);
2034 _pmd = mk_pmd(new_page, vma->vm_page_prot);
2035 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
2036 _pmd = pmd_mkhuge(_pmd);
2039 * spin_lock() below is not the equivalent of smp_wmb(), so
2040 * this is needed to avoid the copy_huge_page writes to become
2041 * visible after the set_pmd_at() write.
2045 spin_lock(&mm->page_table_lock);
2046 BUG_ON(!pmd_none(*pmd));
2047 page_add_new_anon_rmap(new_page, vma, address);
2048 set_pmd_at(mm, address, pmd, _pmd);
2049 update_mmu_cache_pmd(vma, address, pmd);
2050 pgtable_trans_huge_deposit(mm, pgtable);
2051 spin_unlock(&mm->page_table_lock);
2055 khugepaged_pages_collapsed++;
2057 up_write(&mm->mmap_sem);
2061 mem_cgroup_uncharge_page(new_page);
2065 static int khugepaged_scan_pmd(struct mm_struct *mm,
2066 struct vm_area_struct *vma,
2067 unsigned long address,
2068 struct page **hpage)
2074 int ret = 0, referenced = 0, none = 0;
2076 unsigned long _address;
2080 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2082 pgd = pgd_offset(mm, address);
2083 if (!pgd_present(*pgd))
2086 pud = pud_offset(pgd, address);
2087 if (!pud_present(*pud))
2090 pmd = pmd_offset(pud, address);
2091 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2094 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2095 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2096 _pte++, _address += PAGE_SIZE) {
2097 pte_t pteval = *_pte;
2098 if (pte_none(pteval)) {
2099 if (++none <= khugepaged_max_ptes_none)
2104 if (!pte_present(pteval) || !pte_write(pteval))
2106 page = vm_normal_page(vma, _address, pteval);
2107 if (unlikely(!page))
2110 * Chose the node of the first page. This could
2111 * be more sophisticated and look at more pages,
2112 * but isn't for now.
2115 node = page_to_nid(page);
2116 VM_BUG_ON(PageCompound(page));
2117 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2119 /* cannot use mapcount: can't collapse if there's a gup pin */
2120 if (page_count(page) != 1)
2122 if (pte_young(pteval) || PageReferenced(page) ||
2123 mmu_notifier_test_young(vma->vm_mm, address))
2129 pte_unmap_unlock(pte, ptl);
2131 /* collapse_huge_page will return with the mmap_sem released */
2132 collapse_huge_page(mm, address, hpage, vma, node);
2137 static void collect_mm_slot(struct mm_slot *mm_slot)
2139 struct mm_struct *mm = mm_slot->mm;
2141 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2143 if (khugepaged_test_exit(mm)) {
2145 hlist_del(&mm_slot->hash);
2146 list_del(&mm_slot->mm_node);
2149 * Not strictly needed because the mm exited already.
2151 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2154 /* khugepaged_mm_lock actually not necessary for the below */
2155 free_mm_slot(mm_slot);
2160 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2161 struct page **hpage)
2162 __releases(&khugepaged_mm_lock)
2163 __acquires(&khugepaged_mm_lock)
2165 struct mm_slot *mm_slot;
2166 struct mm_struct *mm;
2167 struct vm_area_struct *vma;
2171 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2173 if (khugepaged_scan.mm_slot)
2174 mm_slot = khugepaged_scan.mm_slot;
2176 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2177 struct mm_slot, mm_node);
2178 khugepaged_scan.address = 0;
2179 khugepaged_scan.mm_slot = mm_slot;
2181 spin_unlock(&khugepaged_mm_lock);
2184 down_read(&mm->mmap_sem);
2185 if (unlikely(khugepaged_test_exit(mm)))
2188 vma = find_vma(mm, khugepaged_scan.address);
2191 for (; vma; vma = vma->vm_next) {
2192 unsigned long hstart, hend;
2195 if (unlikely(khugepaged_test_exit(mm))) {
2200 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2201 !khugepaged_always()) ||
2202 (vma->vm_flags & VM_NOHUGEPAGE)) {
2207 if (!vma->anon_vma || vma->vm_ops)
2209 if (is_vma_temporary_stack(vma))
2211 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2213 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2214 hend = vma->vm_end & HPAGE_PMD_MASK;
2217 if (khugepaged_scan.address > hend)
2219 if (khugepaged_scan.address < hstart)
2220 khugepaged_scan.address = hstart;
2221 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2223 while (khugepaged_scan.address < hend) {
2226 if (unlikely(khugepaged_test_exit(mm)))
2227 goto breakouterloop;
2229 VM_BUG_ON(khugepaged_scan.address < hstart ||
2230 khugepaged_scan.address + HPAGE_PMD_SIZE >
2232 ret = khugepaged_scan_pmd(mm, vma,
2233 khugepaged_scan.address,
2235 /* move to next address */
2236 khugepaged_scan.address += HPAGE_PMD_SIZE;
2237 progress += HPAGE_PMD_NR;
2239 /* we released mmap_sem so break loop */
2240 goto breakouterloop_mmap_sem;
2241 if (progress >= pages)
2242 goto breakouterloop;
2246 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2247 breakouterloop_mmap_sem:
2249 spin_lock(&khugepaged_mm_lock);
2250 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2252 * Release the current mm_slot if this mm is about to die, or
2253 * if we scanned all vmas of this mm.
2255 if (khugepaged_test_exit(mm) || !vma) {
2257 * Make sure that if mm_users is reaching zero while
2258 * khugepaged runs here, khugepaged_exit will find
2259 * mm_slot not pointing to the exiting mm.
2261 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2262 khugepaged_scan.mm_slot = list_entry(
2263 mm_slot->mm_node.next,
2264 struct mm_slot, mm_node);
2265 khugepaged_scan.address = 0;
2267 khugepaged_scan.mm_slot = NULL;
2268 khugepaged_full_scans++;
2271 collect_mm_slot(mm_slot);
2277 static int khugepaged_has_work(void)
2279 return !list_empty(&khugepaged_scan.mm_head) &&
2280 khugepaged_enabled();
2283 static int khugepaged_wait_event(void)
2285 return !list_empty(&khugepaged_scan.mm_head) ||
2286 kthread_should_stop();
2289 static void khugepaged_do_scan(void)
2291 struct page *hpage = NULL;
2292 unsigned int progress = 0, pass_through_head = 0;
2293 unsigned int pages = khugepaged_pages_to_scan;
2296 barrier(); /* write khugepaged_pages_to_scan to local stack */
2298 while (progress < pages) {
2299 if (!khugepaged_prealloc_page(&hpage, &wait))
2304 if (unlikely(kthread_should_stop() || freezing(current)))
2307 spin_lock(&khugepaged_mm_lock);
2308 if (!khugepaged_scan.mm_slot)
2309 pass_through_head++;
2310 if (khugepaged_has_work() &&
2311 pass_through_head < 2)
2312 progress += khugepaged_scan_mm_slot(pages - progress,
2316 spin_unlock(&khugepaged_mm_lock);
2319 if (!IS_ERR_OR_NULL(hpage))
2323 static void khugepaged_wait_work(void)
2327 if (khugepaged_has_work()) {
2328 if (!khugepaged_scan_sleep_millisecs)
2331 wait_event_freezable_timeout(khugepaged_wait,
2332 kthread_should_stop(),
2333 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2337 if (khugepaged_enabled())
2338 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2341 static int khugepaged(void *none)
2343 struct mm_slot *mm_slot;
2346 set_user_nice(current, 19);
2348 while (!kthread_should_stop()) {
2349 khugepaged_do_scan();
2350 khugepaged_wait_work();
2353 spin_lock(&khugepaged_mm_lock);
2354 mm_slot = khugepaged_scan.mm_slot;
2355 khugepaged_scan.mm_slot = NULL;
2357 collect_mm_slot(mm_slot);
2358 spin_unlock(&khugepaged_mm_lock);
2362 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2366 spin_lock(&mm->page_table_lock);
2367 if (unlikely(!pmd_trans_huge(*pmd))) {
2368 spin_unlock(&mm->page_table_lock);
2371 page = pmd_page(*pmd);
2372 VM_BUG_ON(!page_count(page));
2374 spin_unlock(&mm->page_table_lock);
2376 split_huge_page(page);
2379 BUG_ON(pmd_trans_huge(*pmd));
2382 static void split_huge_page_address(struct mm_struct *mm,
2383 unsigned long address)
2389 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2391 pgd = pgd_offset(mm, address);
2392 if (!pgd_present(*pgd))
2395 pud = pud_offset(pgd, address);
2396 if (!pud_present(*pud))
2399 pmd = pmd_offset(pud, address);
2400 if (!pmd_present(*pmd))
2403 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2404 * materialize from under us.
2406 split_huge_page_pmd(mm, pmd);
2409 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2410 unsigned long start,
2415 * If the new start address isn't hpage aligned and it could
2416 * previously contain an hugepage: check if we need to split
2419 if (start & ~HPAGE_PMD_MASK &&
2420 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2421 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2422 split_huge_page_address(vma->vm_mm, start);
2425 * If the new end address isn't hpage aligned and it could
2426 * previously contain an hugepage: check if we need to split
2429 if (end & ~HPAGE_PMD_MASK &&
2430 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2431 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2432 split_huge_page_address(vma->vm_mm, end);
2435 * If we're also updating the vma->vm_next->vm_start, if the new
2436 * vm_next->vm_start isn't page aligned and it could previously
2437 * contain an hugepage: check if we need to split an huge pmd.
2439 if (adjust_next > 0) {
2440 struct vm_area_struct *next = vma->vm_next;
2441 unsigned long nstart = next->vm_start;
2442 nstart += adjust_next << PAGE_SHIFT;
2443 if (nstart & ~HPAGE_PMD_MASK &&
2444 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2445 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2446 split_huge_page_address(next->vm_mm, nstart);