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>
21 #include <asm/pgalloc.h>
25 * By default transparent hugepage support is enabled for all mappings
26 * and khugepaged scans all mappings. Defrag is only invoked by
27 * khugepaged hugepage allocations and by page faults inside
28 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
31 unsigned long transparent_hugepage_flags __read_mostly =
32 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
33 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
35 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
36 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
38 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
39 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
41 /* default scan 8*512 pte (or vmas) every 30 second */
42 static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
43 static unsigned int khugepaged_pages_collapsed;
44 static unsigned int khugepaged_full_scans;
45 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
46 /* during fragmentation poll the hugepage allocator once every minute */
47 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
48 static struct task_struct *khugepaged_thread __read_mostly;
49 static DEFINE_MUTEX(khugepaged_mutex);
50 static DEFINE_SPINLOCK(khugepaged_mm_lock);
51 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
53 * default collapse hugepages if there is at least one pte mapped like
54 * it would have happened if the vma was large enough during page
57 static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
59 static int khugepaged(void *none);
60 static int mm_slots_hash_init(void);
61 static int khugepaged_slab_init(void);
62 static void khugepaged_slab_free(void);
64 #define MM_SLOTS_HASH_HEADS 1024
65 static struct hlist_head *mm_slots_hash __read_mostly;
66 static struct kmem_cache *mm_slot_cache __read_mostly;
69 * struct mm_slot - hash lookup from mm to mm_slot
70 * @hash: hash collision list
71 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
72 * @mm: the mm that this information is valid for
75 struct hlist_node hash;
76 struct list_head mm_node;
81 * struct khugepaged_scan - cursor for scanning
82 * @mm_head: the head of the mm list to scan
83 * @mm_slot: the current mm_slot we are scanning
84 * @address: the next address inside that to be scanned
86 * There is only the one khugepaged_scan instance of this cursor structure.
88 struct khugepaged_scan {
89 struct list_head mm_head;
90 struct mm_slot *mm_slot;
91 unsigned long address;
93 static struct khugepaged_scan khugepaged_scan = {
94 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
98 static int set_recommended_min_free_kbytes(void)
102 unsigned long recommended_min;
103 extern int min_free_kbytes;
105 if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG,
106 &transparent_hugepage_flags) &&
107 !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
108 &transparent_hugepage_flags))
111 for_each_populated_zone(zone)
114 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
115 recommended_min = pageblock_nr_pages * nr_zones * 2;
118 * Make sure that on average at least two pageblocks are almost free
119 * of another type, one for a migratetype to fall back to and a
120 * second to avoid subsequent fallbacks of other types There are 3
121 * MIGRATE_TYPES we care about.
123 recommended_min += pageblock_nr_pages * nr_zones *
124 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
126 /* don't ever allow to reserve more than 5% of the lowmem */
127 recommended_min = min(recommended_min,
128 (unsigned long) nr_free_buffer_pages() / 20);
129 recommended_min <<= (PAGE_SHIFT-10);
131 if (recommended_min > min_free_kbytes)
132 min_free_kbytes = recommended_min;
133 setup_per_zone_wmarks();
136 late_initcall(set_recommended_min_free_kbytes);
138 static int start_khugepaged(void)
141 if (khugepaged_enabled()) {
143 if (unlikely(!mm_slot_cache || !mm_slots_hash)) {
147 mutex_lock(&khugepaged_mutex);
148 if (!khugepaged_thread)
149 khugepaged_thread = kthread_run(khugepaged, NULL,
151 if (unlikely(IS_ERR(khugepaged_thread))) {
153 "khugepaged: kthread_run(khugepaged) failed\n");
154 err = PTR_ERR(khugepaged_thread);
155 khugepaged_thread = NULL;
157 wakeup = !list_empty(&khugepaged_scan.mm_head);
158 mutex_unlock(&khugepaged_mutex);
160 wake_up_interruptible(&khugepaged_wait);
162 set_recommended_min_free_kbytes();
165 wake_up_interruptible(&khugepaged_wait);
172 static ssize_t double_flag_show(struct kobject *kobj,
173 struct kobj_attribute *attr, char *buf,
174 enum transparent_hugepage_flag enabled,
175 enum transparent_hugepage_flag req_madv)
177 if (test_bit(enabled, &transparent_hugepage_flags)) {
178 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
179 return sprintf(buf, "[always] madvise never\n");
180 } else if (test_bit(req_madv, &transparent_hugepage_flags))
181 return sprintf(buf, "always [madvise] never\n");
183 return sprintf(buf, "always madvise [never]\n");
185 static ssize_t double_flag_store(struct kobject *kobj,
186 struct kobj_attribute *attr,
187 const char *buf, size_t count,
188 enum transparent_hugepage_flag enabled,
189 enum transparent_hugepage_flag req_madv)
191 if (!memcmp("always", buf,
192 min(sizeof("always")-1, count))) {
193 set_bit(enabled, &transparent_hugepage_flags);
194 clear_bit(req_madv, &transparent_hugepage_flags);
195 } else if (!memcmp("madvise", buf,
196 min(sizeof("madvise")-1, count))) {
197 clear_bit(enabled, &transparent_hugepage_flags);
198 set_bit(req_madv, &transparent_hugepage_flags);
199 } else if (!memcmp("never", buf,
200 min(sizeof("never")-1, count))) {
201 clear_bit(enabled, &transparent_hugepage_flags);
202 clear_bit(req_madv, &transparent_hugepage_flags);
209 static ssize_t enabled_show(struct kobject *kobj,
210 struct kobj_attribute *attr, char *buf)
212 return double_flag_show(kobj, attr, buf,
213 TRANSPARENT_HUGEPAGE_FLAG,
214 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
216 static ssize_t enabled_store(struct kobject *kobj,
217 struct kobj_attribute *attr,
218 const char *buf, size_t count)
222 ret = double_flag_store(kobj, attr, buf, count,
223 TRANSPARENT_HUGEPAGE_FLAG,
224 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
227 int err = start_khugepaged();
233 (test_bit(TRANSPARENT_HUGEPAGE_FLAG,
234 &transparent_hugepage_flags) ||
235 test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
236 &transparent_hugepage_flags)))
237 set_recommended_min_free_kbytes();
241 static struct kobj_attribute enabled_attr =
242 __ATTR(enabled, 0644, enabled_show, enabled_store);
244 static ssize_t single_flag_show(struct kobject *kobj,
245 struct kobj_attribute *attr, char *buf,
246 enum transparent_hugepage_flag flag)
248 return sprintf(buf, "%d\n",
249 !!test_bit(flag, &transparent_hugepage_flags));
252 static ssize_t single_flag_store(struct kobject *kobj,
253 struct kobj_attribute *attr,
254 const char *buf, size_t count,
255 enum transparent_hugepage_flag flag)
260 ret = kstrtoul(buf, 10, &value);
267 set_bit(flag, &transparent_hugepage_flags);
269 clear_bit(flag, &transparent_hugepage_flags);
275 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
276 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
277 * memory just to allocate one more hugepage.
279 static ssize_t defrag_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf)
282 return double_flag_show(kobj, attr, buf,
283 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
284 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
286 static ssize_t defrag_store(struct kobject *kobj,
287 struct kobj_attribute *attr,
288 const char *buf, size_t count)
290 return double_flag_store(kobj, attr, buf, count,
291 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
292 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
294 static struct kobj_attribute defrag_attr =
295 __ATTR(defrag, 0644, defrag_show, defrag_store);
297 #ifdef CONFIG_DEBUG_VM
298 static ssize_t debug_cow_show(struct kobject *kobj,
299 struct kobj_attribute *attr, char *buf)
301 return single_flag_show(kobj, attr, buf,
302 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
304 static ssize_t debug_cow_store(struct kobject *kobj,
305 struct kobj_attribute *attr,
306 const char *buf, size_t count)
308 return single_flag_store(kobj, attr, buf, count,
309 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
311 static struct kobj_attribute debug_cow_attr =
312 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
313 #endif /* CONFIG_DEBUG_VM */
315 static struct attribute *hugepage_attr[] = {
318 #ifdef CONFIG_DEBUG_VM
319 &debug_cow_attr.attr,
324 static struct attribute_group hugepage_attr_group = {
325 .attrs = hugepage_attr,
328 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
329 struct kobj_attribute *attr,
332 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
335 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
336 struct kobj_attribute *attr,
337 const char *buf, size_t count)
342 err = strict_strtoul(buf, 10, &msecs);
343 if (err || msecs > UINT_MAX)
346 khugepaged_scan_sleep_millisecs = msecs;
347 wake_up_interruptible(&khugepaged_wait);
351 static struct kobj_attribute scan_sleep_millisecs_attr =
352 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
353 scan_sleep_millisecs_store);
355 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
356 struct kobj_attribute *attr,
359 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
362 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
363 struct kobj_attribute *attr,
364 const char *buf, size_t count)
369 err = strict_strtoul(buf, 10, &msecs);
370 if (err || msecs > UINT_MAX)
373 khugepaged_alloc_sleep_millisecs = msecs;
374 wake_up_interruptible(&khugepaged_wait);
378 static struct kobj_attribute alloc_sleep_millisecs_attr =
379 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
380 alloc_sleep_millisecs_store);
382 static ssize_t pages_to_scan_show(struct kobject *kobj,
383 struct kobj_attribute *attr,
386 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
388 static ssize_t pages_to_scan_store(struct kobject *kobj,
389 struct kobj_attribute *attr,
390 const char *buf, size_t count)
395 err = strict_strtoul(buf, 10, &pages);
396 if (err || !pages || pages > UINT_MAX)
399 khugepaged_pages_to_scan = pages;
403 static struct kobj_attribute pages_to_scan_attr =
404 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
405 pages_to_scan_store);
407 static ssize_t pages_collapsed_show(struct kobject *kobj,
408 struct kobj_attribute *attr,
411 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
413 static struct kobj_attribute pages_collapsed_attr =
414 __ATTR_RO(pages_collapsed);
416 static ssize_t full_scans_show(struct kobject *kobj,
417 struct kobj_attribute *attr,
420 return sprintf(buf, "%u\n", khugepaged_full_scans);
422 static struct kobj_attribute full_scans_attr =
423 __ATTR_RO(full_scans);
425 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
426 struct kobj_attribute *attr, char *buf)
428 return single_flag_show(kobj, attr, buf,
429 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
431 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
432 struct kobj_attribute *attr,
433 const char *buf, size_t count)
435 return single_flag_store(kobj, attr, buf, count,
436 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
438 static struct kobj_attribute khugepaged_defrag_attr =
439 __ATTR(defrag, 0644, khugepaged_defrag_show,
440 khugepaged_defrag_store);
443 * max_ptes_none controls if khugepaged should collapse hugepages over
444 * any unmapped ptes in turn potentially increasing the memory
445 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
446 * reduce the available free memory in the system as it
447 * runs. Increasing max_ptes_none will instead potentially reduce the
448 * free memory in the system during the khugepaged scan.
450 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
451 struct kobj_attribute *attr,
454 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
456 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
457 struct kobj_attribute *attr,
458 const char *buf, size_t count)
461 unsigned long max_ptes_none;
463 err = strict_strtoul(buf, 10, &max_ptes_none);
464 if (err || max_ptes_none > HPAGE_PMD_NR-1)
467 khugepaged_max_ptes_none = max_ptes_none;
471 static struct kobj_attribute khugepaged_max_ptes_none_attr =
472 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
473 khugepaged_max_ptes_none_store);
475 static struct attribute *khugepaged_attr[] = {
476 &khugepaged_defrag_attr.attr,
477 &khugepaged_max_ptes_none_attr.attr,
478 &pages_to_scan_attr.attr,
479 &pages_collapsed_attr.attr,
480 &full_scans_attr.attr,
481 &scan_sleep_millisecs_attr.attr,
482 &alloc_sleep_millisecs_attr.attr,
486 static struct attribute_group khugepaged_attr_group = {
487 .attrs = khugepaged_attr,
488 .name = "khugepaged",
490 #endif /* CONFIG_SYSFS */
492 static int __init hugepage_init(void)
496 static struct kobject *hugepage_kobj;
500 if (!has_transparent_hugepage()) {
501 transparent_hugepage_flags = 0;
507 hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
508 if (unlikely(!hugepage_kobj)) {
509 printk(KERN_ERR "hugepage: failed kobject create\n");
513 err = sysfs_create_group(hugepage_kobj, &hugepage_attr_group);
515 printk(KERN_ERR "hugepage: failed register hugeage group\n");
519 err = sysfs_create_group(hugepage_kobj, &khugepaged_attr_group);
521 printk(KERN_ERR "hugepage: failed register hugeage group\n");
526 err = khugepaged_slab_init();
530 err = mm_slots_hash_init();
532 khugepaged_slab_free();
537 * By default disable transparent hugepages on smaller systems,
538 * where the extra memory used could hurt more than TLB overhead
539 * is likely to save. The admin can still enable it through /sys.
541 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
542 transparent_hugepage_flags = 0;
546 set_recommended_min_free_kbytes();
551 module_init(hugepage_init)
553 static int __init setup_transparent_hugepage(char *str)
558 if (!strcmp(str, "always")) {
559 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
560 &transparent_hugepage_flags);
561 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
562 &transparent_hugepage_flags);
564 } else if (!strcmp(str, "madvise")) {
565 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
566 &transparent_hugepage_flags);
567 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
568 &transparent_hugepage_flags);
570 } else if (!strcmp(str, "never")) {
571 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
572 &transparent_hugepage_flags);
573 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
574 &transparent_hugepage_flags);
580 "transparent_hugepage= cannot parse, ignored\n");
583 __setup("transparent_hugepage=", setup_transparent_hugepage);
585 static void prepare_pmd_huge_pte(pgtable_t pgtable,
586 struct mm_struct *mm)
588 assert_spin_locked(&mm->page_table_lock);
591 if (!mm->pmd_huge_pte)
592 INIT_LIST_HEAD(&pgtable->lru);
594 list_add(&pgtable->lru, &mm->pmd_huge_pte->lru);
595 mm->pmd_huge_pte = pgtable;
598 static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
600 if (likely(vma->vm_flags & VM_WRITE))
601 pmd = pmd_mkwrite(pmd);
605 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
606 struct vm_area_struct *vma,
607 unsigned long haddr, pmd_t *pmd,
613 VM_BUG_ON(!PageCompound(page));
614 pgtable = pte_alloc_one(mm, haddr);
615 if (unlikely(!pgtable)) {
616 mem_cgroup_uncharge_page(page);
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 prepare_pmd_huge_pte(pgtable, mm);
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, vma->vm_flags)))
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))) {
699 return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
703 * Use __pte_alloc instead of pte_alloc_map, because we can't
704 * run pte_offset_map on the pmd, if an huge pmd could
705 * materialize from under us from a different thread.
707 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
709 /* if an huge pmd materialized from under us just retry later */
710 if (unlikely(pmd_trans_huge(*pmd)))
713 * A regular pmd is established and it can't morph into a huge pmd
714 * from under us anymore at this point because we hold the mmap_sem
715 * read mode and khugepaged takes it in write mode. So now it's
716 * safe to run pte_offset_map().
718 pte = pte_offset_map(pmd, address);
719 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
722 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
723 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
724 struct vm_area_struct *vma)
726 struct page *src_page;
732 pgtable = pte_alloc_one(dst_mm, addr);
733 if (unlikely(!pgtable))
736 spin_lock(&dst_mm->page_table_lock);
737 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
741 if (unlikely(!pmd_trans_huge(pmd))) {
742 pte_free(dst_mm, pgtable);
745 if (unlikely(pmd_trans_splitting(pmd))) {
746 /* split huge page running from under us */
747 spin_unlock(&src_mm->page_table_lock);
748 spin_unlock(&dst_mm->page_table_lock);
749 pte_free(dst_mm, pgtable);
751 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
754 src_page = pmd_page(pmd);
755 VM_BUG_ON(!PageHead(src_page));
757 page_dup_rmap(src_page);
758 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
760 pmdp_set_wrprotect(src_mm, addr, src_pmd);
761 pmd = pmd_mkold(pmd_wrprotect(pmd));
762 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
763 prepare_pmd_huge_pte(pgtable, dst_mm);
768 spin_unlock(&src_mm->page_table_lock);
769 spin_unlock(&dst_mm->page_table_lock);
774 /* no "address" argument so destroys page coloring of some arch */
775 pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
779 assert_spin_locked(&mm->page_table_lock);
782 pgtable = mm->pmd_huge_pte;
783 if (list_empty(&pgtable->lru))
784 mm->pmd_huge_pte = NULL;
786 mm->pmd_huge_pte = list_entry(pgtable->lru.next,
788 list_del(&pgtable->lru);
793 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
794 struct vm_area_struct *vma,
795 unsigned long address,
796 pmd_t *pmd, pmd_t orig_pmd,
805 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
807 if (unlikely(!pages)) {
812 for (i = 0; i < HPAGE_PMD_NR; i++) {
813 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
815 vma, address, page_to_nid(page));
816 if (unlikely(!pages[i] ||
817 mem_cgroup_newpage_charge(pages[i], mm,
821 mem_cgroup_uncharge_start();
823 mem_cgroup_uncharge_page(pages[i]);
826 mem_cgroup_uncharge_end();
833 for (i = 0; i < HPAGE_PMD_NR; i++) {
834 copy_user_highpage(pages[i], page + i,
835 haddr + PAGE_SIZE * i, vma);
836 __SetPageUptodate(pages[i]);
840 spin_lock(&mm->page_table_lock);
841 if (unlikely(!pmd_same(*pmd, orig_pmd)))
843 VM_BUG_ON(!PageHead(page));
845 pmdp_clear_flush_notify(vma, haddr, pmd);
846 /* leave pmd empty until pte is filled */
848 pgtable = get_pmd_huge_pte(mm);
849 pmd_populate(mm, &_pmd, pgtable);
851 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
853 entry = mk_pte(pages[i], vma->vm_page_prot);
854 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
855 page_add_new_anon_rmap(pages[i], vma, haddr);
856 pte = pte_offset_map(&_pmd, haddr);
857 VM_BUG_ON(!pte_none(*pte));
858 set_pte_at(mm, haddr, pte, entry);
863 smp_wmb(); /* make pte visible before pmd */
864 pmd_populate(mm, pmd, pgtable);
865 page_remove_rmap(page);
866 spin_unlock(&mm->page_table_lock);
868 ret |= VM_FAULT_WRITE;
875 spin_unlock(&mm->page_table_lock);
876 mem_cgroup_uncharge_start();
877 for (i = 0; i < HPAGE_PMD_NR; i++) {
878 mem_cgroup_uncharge_page(pages[i]);
881 mem_cgroup_uncharge_end();
886 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
887 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
890 struct page *page, *new_page;
893 VM_BUG_ON(!vma->anon_vma);
894 spin_lock(&mm->page_table_lock);
895 if (unlikely(!pmd_same(*pmd, orig_pmd)))
898 page = pmd_page(orig_pmd);
899 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
900 haddr = address & HPAGE_PMD_MASK;
901 if (page_mapcount(page) == 1) {
903 entry = pmd_mkyoung(orig_pmd);
904 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
905 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
906 update_mmu_cache(vma, address, entry);
907 ret |= VM_FAULT_WRITE;
911 spin_unlock(&mm->page_table_lock);
913 if (transparent_hugepage_enabled(vma) &&
914 !transparent_hugepage_debug_cow())
915 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
916 vma, haddr, numa_node_id(), 0);
920 if (unlikely(!new_page)) {
921 count_vm_event(THP_FAULT_FALLBACK);
922 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
923 pmd, orig_pmd, page, haddr);
924 if (ret & VM_FAULT_OOM)
925 split_huge_page(page);
929 count_vm_event(THP_FAULT_ALLOC);
931 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
933 split_huge_page(page);
939 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
940 __SetPageUptodate(new_page);
942 spin_lock(&mm->page_table_lock);
944 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
945 mem_cgroup_uncharge_page(new_page);
949 VM_BUG_ON(!PageHead(page));
950 entry = mk_pmd(new_page, vma->vm_page_prot);
951 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
952 entry = pmd_mkhuge(entry);
953 pmdp_clear_flush_notify(vma, haddr, pmd);
954 page_add_new_anon_rmap(new_page, vma, haddr);
955 set_pmd_at(mm, haddr, pmd, entry);
956 update_mmu_cache(vma, address, entry);
957 page_remove_rmap(page);
959 ret |= VM_FAULT_WRITE;
962 spin_unlock(&mm->page_table_lock);
968 * FOLL_FORCE can write to even unwritable pmd's, but only
969 * after we've gone through a COW cycle and they are dirty.
971 static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
974 return pmd_write(pmd) ||
975 ((flags & FOLL_FORCE) && (flags & FOLL_COW) &&
976 page && PageAnon(page));
979 struct page *follow_trans_huge_pmd(struct mm_struct *mm,
984 struct page *page = NULL;
986 assert_spin_locked(&mm->page_table_lock);
988 page = pmd_page(*pmd);
989 VM_BUG_ON(!PageHead(page));
991 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, page, flags))
994 if (flags & FOLL_TOUCH) {
997 * We should set the dirty bit only for FOLL_WRITE but
998 * for now the dirty bit in the pmd is meaningless.
999 * And if the dirty bit will become meaningful and
1000 * we'll only set it with FOLL_WRITE, an atomic
1001 * set_bit will be required on the pmd to set the
1002 * young bit, instead of the current set_pmd_at.
1004 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1005 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1007 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1008 VM_BUG_ON(!PageCompound(page));
1009 if (flags & FOLL_GET)
1010 get_page_foll(page);
1015 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1020 spin_lock(&tlb->mm->page_table_lock);
1021 if (likely(pmd_trans_huge(*pmd))) {
1022 if (unlikely(pmd_trans_splitting(*pmd))) {
1023 spin_unlock(&tlb->mm->page_table_lock);
1024 wait_split_huge_page(vma->anon_vma,
1029 pgtable = get_pmd_huge_pte(tlb->mm);
1030 page = pmd_page(*pmd);
1032 page_remove_rmap(page);
1033 VM_BUG_ON(page_mapcount(page) < 0);
1034 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1035 VM_BUG_ON(!PageHead(page));
1037 spin_unlock(&tlb->mm->page_table_lock);
1038 tlb_remove_page(tlb, page);
1039 pte_free(tlb->mm, pgtable);
1043 spin_unlock(&tlb->mm->page_table_lock);
1048 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1049 unsigned long addr, unsigned long end,
1054 spin_lock(&vma->vm_mm->page_table_lock);
1055 if (likely(pmd_trans_huge(*pmd))) {
1056 ret = !pmd_trans_splitting(*pmd);
1057 spin_unlock(&vma->vm_mm->page_table_lock);
1059 wait_split_huge_page(vma->anon_vma, pmd);
1062 * All logical pages in the range are present
1063 * if backed by a huge page.
1065 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1068 spin_unlock(&vma->vm_mm->page_table_lock);
1073 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1074 unsigned long old_addr,
1075 unsigned long new_addr, unsigned long old_end,
1076 pmd_t *old_pmd, pmd_t *new_pmd)
1081 struct mm_struct *mm = vma->vm_mm;
1083 if ((old_addr & ~HPAGE_PMD_MASK) ||
1084 (new_addr & ~HPAGE_PMD_MASK) ||
1085 old_end - old_addr < HPAGE_PMD_SIZE ||
1086 (new_vma->vm_flags & VM_NOHUGEPAGE))
1090 * The destination pmd shouldn't be established, free_pgtables()
1091 * should have release it.
1093 if (WARN_ON(!pmd_none(*new_pmd))) {
1094 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1098 spin_lock(&mm->page_table_lock);
1099 if (likely(pmd_trans_huge(*old_pmd))) {
1100 if (pmd_trans_splitting(*old_pmd)) {
1101 spin_unlock(&mm->page_table_lock);
1102 wait_split_huge_page(vma->anon_vma, old_pmd);
1105 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1106 VM_BUG_ON(!pmd_none(*new_pmd));
1107 set_pmd_at(mm, new_addr, new_pmd, pmd);
1108 spin_unlock(&mm->page_table_lock);
1112 spin_unlock(&mm->page_table_lock);
1118 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1119 unsigned long addr, pgprot_t newprot)
1121 struct mm_struct *mm = vma->vm_mm;
1124 spin_lock(&mm->page_table_lock);
1125 if (likely(pmd_trans_huge(*pmd))) {
1126 if (unlikely(pmd_trans_splitting(*pmd))) {
1127 spin_unlock(&mm->page_table_lock);
1128 wait_split_huge_page(vma->anon_vma, pmd);
1132 entry = pmdp_get_and_clear(mm, addr, pmd);
1133 entry = pmd_modify(entry, newprot);
1134 set_pmd_at(mm, addr, pmd, entry);
1135 spin_unlock(&vma->vm_mm->page_table_lock);
1136 flush_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
1140 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;
1197 spin_lock(&mm->page_table_lock);
1198 pmd = page_check_address_pmd(page, mm, address,
1199 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1202 * We can't temporarily set the pmd to null in order
1203 * to split it, the pmd must remain marked huge at all
1204 * times or the VM won't take the pmd_trans_huge paths
1205 * and it won't wait on the anon_vma->root->mutex to
1206 * serialize against split_huge_page*.
1208 pmdp_splitting_flush_notify(vma, address, pmd);
1211 spin_unlock(&mm->page_table_lock);
1216 static void __split_huge_page_refcount(struct page *page)
1219 unsigned long head_index = page->index;
1220 struct zone *zone = page_zone(page);
1224 /* prevent PageLRU to go away from under us, and freeze lru stats */
1225 spin_lock_irq(&zone->lru_lock);
1226 compound_lock(page);
1228 for (i = 1; i < HPAGE_PMD_NR; i++) {
1229 struct page *page_tail = page + i;
1231 /* tail_page->_mapcount cannot change */
1232 BUG_ON(page_mapcount(page_tail) < 0);
1233 tail_count += page_mapcount(page_tail);
1234 /* check for overflow */
1235 BUG_ON(tail_count < 0);
1236 BUG_ON(atomic_read(&page_tail->_count) != 0);
1238 * tail_page->_count is zero and not changing from
1239 * under us. But get_page_unless_zero() may be running
1240 * from under us on the tail_page. If we used
1241 * atomic_set() below instead of atomic_add(), we
1242 * would then run atomic_set() concurrently with
1243 * get_page_unless_zero(), and atomic_set() is
1244 * implemented in C not using locked ops. spin_unlock
1245 * on x86 sometime uses locked ops because of PPro
1246 * errata 66, 92, so unless somebody can guarantee
1247 * atomic_set() here would be safe on all archs (and
1248 * not only on x86), it's safer to use atomic_add().
1250 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1251 &page_tail->_count);
1253 /* after clearing PageTail the gup refcount can be released */
1257 * retain hwpoison flag of the poisoned tail page:
1258 * fix for the unsuitable process killed on Guest Machine(KVM)
1259 * by the memory-failure.
1261 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1262 page_tail->flags |= (page->flags &
1263 ((1L << PG_referenced) |
1264 (1L << PG_swapbacked) |
1265 (1L << PG_mlocked) |
1266 (1L << PG_uptodate)));
1267 page_tail->flags |= (1L << PG_dirty);
1269 /* clear PageTail before overwriting first_page */
1273 * __split_huge_page_splitting() already set the
1274 * splitting bit in all pmd that could map this
1275 * hugepage, that will ensure no CPU can alter the
1276 * mapcount on the head page. The mapcount is only
1277 * accounted in the head page and it has to be
1278 * transferred to all tail pages in the below code. So
1279 * for this code to be safe, the split the mapcount
1280 * can't change. But that doesn't mean userland can't
1281 * keep changing and reading the page contents while
1282 * we transfer the mapcount, so the pmd splitting
1283 * status is achieved setting a reserved bit in the
1284 * pmd, not by clearing the present bit.
1286 page_tail->_mapcount = page->_mapcount;
1288 BUG_ON(page_tail->mapping);
1289 page_tail->mapping = page->mapping;
1291 page_tail->index = ++head_index;
1293 BUG_ON(!PageAnon(page_tail));
1294 BUG_ON(!PageUptodate(page_tail));
1295 BUG_ON(!PageDirty(page_tail));
1296 BUG_ON(!PageSwapBacked(page_tail));
1298 mem_cgroup_split_huge_fixup(page, page_tail);
1300 lru_add_page_tail(zone, page, page_tail);
1302 atomic_sub(tail_count, &page->_count);
1303 BUG_ON(atomic_read(&page->_count) <= 0);
1305 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1306 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1309 * A hugepage counts for HPAGE_PMD_NR pages on the LRU statistics,
1310 * so adjust those appropriately if this page is on the LRU.
1312 if (PageLRU(page)) {
1313 zonestat = NR_LRU_BASE + page_lru(page);
1314 __mod_zone_page_state(zone, zonestat, -(HPAGE_PMD_NR-1));
1317 ClearPageCompound(page);
1318 compound_unlock(page);
1319 spin_unlock_irq(&zone->lru_lock);
1321 for (i = 1; i < HPAGE_PMD_NR; i++) {
1322 struct page *page_tail = page + i;
1323 BUG_ON(page_count(page_tail) <= 0);
1325 * Tail pages may be freed if there wasn't any mapping
1326 * like if add_to_swap() is running on a lru page that
1327 * had its mapping zapped. And freeing these pages
1328 * requires taking the lru_lock so we do the put_page
1329 * of the tail pages after the split is complete.
1331 put_page(page_tail);
1335 * Only the head page (now become a regular page) is required
1336 * to be pinned by the caller.
1338 BUG_ON(page_count(page) <= 0);
1341 static int __split_huge_page_map(struct page *page,
1342 struct vm_area_struct *vma,
1343 unsigned long address)
1345 struct mm_struct *mm = vma->vm_mm;
1349 unsigned long haddr;
1351 spin_lock(&mm->page_table_lock);
1352 pmd = page_check_address_pmd(page, mm, address,
1353 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1355 pgtable = get_pmd_huge_pte(mm);
1356 pmd_populate(mm, &_pmd, pgtable);
1358 for (i = 0, haddr = address; i < HPAGE_PMD_NR;
1359 i++, haddr += PAGE_SIZE) {
1361 BUG_ON(PageCompound(page+i));
1362 entry = mk_pte(page + i, vma->vm_page_prot);
1363 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1364 if (!pmd_write(*pmd))
1365 entry = pte_wrprotect(entry);
1367 BUG_ON(page_mapcount(page) != 1);
1368 if (!pmd_young(*pmd))
1369 entry = pte_mkold(entry);
1370 pte = pte_offset_map(&_pmd, haddr);
1371 BUG_ON(!pte_none(*pte));
1372 set_pte_at(mm, haddr, pte, entry);
1376 smp_wmb(); /* make pte visible before pmd */
1378 * Up to this point the pmd is present and huge and
1379 * userland has the whole access to the hugepage
1380 * during the split (which happens in place). If we
1381 * overwrite the pmd with the not-huge version
1382 * pointing to the pte here (which of course we could
1383 * if all CPUs were bug free), userland could trigger
1384 * a small page size TLB miss on the small sized TLB
1385 * while the hugepage TLB entry is still established
1386 * in the huge TLB. Some CPU doesn't like that. See
1387 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1388 * Erratum 383 on page 93. Intel should be safe but is
1389 * also warns that it's only safe if the permission
1390 * and cache attributes of the two entries loaded in
1391 * the two TLB is identical (which should be the case
1392 * here). But it is generally safer to never allow
1393 * small and huge TLB entries for the same virtual
1394 * address to be loaded simultaneously. So instead of
1395 * doing "pmd_populate(); flush_tlb_range();" we first
1396 * mark the current pmd notpresent (atomically because
1397 * here the pmd_trans_huge and pmd_trans_splitting
1398 * must remain set at all times on the pmd until the
1399 * split is complete for this pmd), then we flush the
1400 * SMP TLB and finally we write the non-huge version
1401 * of the pmd entry with pmd_populate.
1403 set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
1404 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
1405 pmd_populate(mm, pmd, pgtable);
1408 spin_unlock(&mm->page_table_lock);
1413 /* must be called with anon_vma->root->mutex hold */
1414 static void __split_huge_page(struct page *page,
1415 struct anon_vma *anon_vma)
1417 int mapcount, mapcount2;
1418 struct anon_vma_chain *avc;
1420 BUG_ON(!PageHead(page));
1421 BUG_ON(PageTail(page));
1424 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1425 struct vm_area_struct *vma = avc->vma;
1426 unsigned long addr = vma_address(page, vma);
1427 BUG_ON(is_vma_temporary_stack(vma));
1428 if (addr == -EFAULT)
1430 mapcount += __split_huge_page_splitting(page, vma, addr);
1433 * It is critical that new vmas are added to the tail of the
1434 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1435 * and establishes a child pmd before
1436 * __split_huge_page_splitting() freezes the parent pmd (so if
1437 * we fail to prevent copy_huge_pmd() from running until the
1438 * whole __split_huge_page() is complete), we will still see
1439 * the newly established pmd of the child later during the
1440 * walk, to be able to set it as pmd_trans_splitting too.
1442 if (mapcount != page_mapcount(page))
1443 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1444 mapcount, page_mapcount(page));
1445 BUG_ON(mapcount != page_mapcount(page));
1447 __split_huge_page_refcount(page);
1450 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1451 struct vm_area_struct *vma = avc->vma;
1452 unsigned long addr = vma_address(page, vma);
1453 BUG_ON(is_vma_temporary_stack(vma));
1454 if (addr == -EFAULT)
1456 mapcount2 += __split_huge_page_map(page, vma, addr);
1458 if (mapcount != mapcount2)
1459 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1460 mapcount, mapcount2, page_mapcount(page));
1461 BUG_ON(mapcount != mapcount2);
1464 int split_huge_page(struct page *page)
1466 struct anon_vma *anon_vma;
1469 BUG_ON(!PageAnon(page));
1470 anon_vma = page_lock_anon_vma(page);
1474 if (!PageCompound(page))
1477 BUG_ON(!PageSwapBacked(page));
1478 __split_huge_page(page, anon_vma);
1479 count_vm_event(THP_SPLIT);
1481 BUG_ON(PageCompound(page));
1483 page_unlock_anon_vma(anon_vma);
1488 #define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
1489 VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1491 int hugepage_madvise(struct vm_area_struct *vma,
1492 unsigned long *vm_flags, int advice)
1497 * Be somewhat over-protective like KSM for now!
1499 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1501 *vm_flags &= ~VM_NOHUGEPAGE;
1502 *vm_flags |= VM_HUGEPAGE;
1504 * If the vma become good for khugepaged to scan,
1505 * register it here without waiting a page fault that
1506 * may not happen any time soon.
1508 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
1511 case MADV_NOHUGEPAGE:
1513 * Be somewhat over-protective like KSM for now!
1515 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1517 *vm_flags &= ~VM_HUGEPAGE;
1518 *vm_flags |= VM_NOHUGEPAGE;
1520 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1521 * this vma even if we leave the mm registered in khugepaged if
1522 * it got registered before VM_NOHUGEPAGE was set.
1530 static int __init khugepaged_slab_init(void)
1532 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1533 sizeof(struct mm_slot),
1534 __alignof__(struct mm_slot), 0, NULL);
1541 static void __init khugepaged_slab_free(void)
1543 kmem_cache_destroy(mm_slot_cache);
1544 mm_slot_cache = NULL;
1547 static inline struct mm_slot *alloc_mm_slot(void)
1549 if (!mm_slot_cache) /* initialization failed */
1551 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1554 static inline void free_mm_slot(struct mm_slot *mm_slot)
1556 kmem_cache_free(mm_slot_cache, mm_slot);
1559 static int __init mm_slots_hash_init(void)
1561 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1569 static void __init mm_slots_hash_free(void)
1571 kfree(mm_slots_hash);
1572 mm_slots_hash = NULL;
1576 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1578 struct mm_slot *mm_slot;
1579 struct hlist_head *bucket;
1580 struct hlist_node *node;
1582 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1583 % MM_SLOTS_HASH_HEADS];
1584 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1585 if (mm == mm_slot->mm)
1591 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1592 struct mm_slot *mm_slot)
1594 struct hlist_head *bucket;
1596 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1597 % MM_SLOTS_HASH_HEADS];
1599 hlist_add_head(&mm_slot->hash, bucket);
1602 static inline int khugepaged_test_exit(struct mm_struct *mm)
1604 return atomic_read(&mm->mm_users) == 0;
1607 int __khugepaged_enter(struct mm_struct *mm)
1609 struct mm_slot *mm_slot;
1612 mm_slot = alloc_mm_slot();
1616 /* __khugepaged_exit() must not run from under us */
1617 VM_BUG_ON(khugepaged_test_exit(mm));
1618 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1619 free_mm_slot(mm_slot);
1623 spin_lock(&khugepaged_mm_lock);
1624 insert_to_mm_slots_hash(mm, mm_slot);
1626 * Insert just behind the scanning cursor, to let the area settle
1629 wakeup = list_empty(&khugepaged_scan.mm_head);
1630 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1631 spin_unlock(&khugepaged_mm_lock);
1633 atomic_inc(&mm->mm_count);
1635 wake_up_interruptible(&khugepaged_wait);
1640 int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1641 unsigned long vm_flags)
1643 unsigned long hstart, hend;
1646 * Not yet faulted in so we will register later in the
1647 * page fault if needed.
1650 if (vma->vm_ops || (vm_flags & VM_NO_THP))
1651 /* khugepaged not yet working on file or special mappings */
1654 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1655 * true too, verify it here.
1657 VM_BUG_ON(is_linear_pfn_mapping(vma));
1658 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1659 hend = vma->vm_end & HPAGE_PMD_MASK;
1661 return khugepaged_enter(vma, vm_flags);
1665 void __khugepaged_exit(struct mm_struct *mm)
1667 struct mm_slot *mm_slot;
1670 spin_lock(&khugepaged_mm_lock);
1671 mm_slot = get_mm_slot(mm);
1672 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1673 hlist_del(&mm_slot->hash);
1674 list_del(&mm_slot->mm_node);
1677 spin_unlock(&khugepaged_mm_lock);
1680 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1681 free_mm_slot(mm_slot);
1683 } else if (mm_slot) {
1685 * This is required to serialize against
1686 * khugepaged_test_exit() (which is guaranteed to run
1687 * under mmap sem read mode). Stop here (after we
1688 * return all pagetables will be destroyed) until
1689 * khugepaged has finished working on the pagetables
1690 * under the mmap_sem.
1692 down_write(&mm->mmap_sem);
1693 up_write(&mm->mmap_sem);
1697 static void release_pte_page(struct page *page)
1699 /* 0 stands for page_is_file_cache(page) == false */
1700 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1702 putback_lru_page(page);
1705 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1707 while (--_pte >= pte) {
1708 pte_t pteval = *_pte;
1709 if (!pte_none(pteval))
1710 release_pte_page(pte_page(pteval));
1714 static void release_all_pte_pages(pte_t *pte)
1716 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1719 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1720 unsigned long address,
1725 int referenced = 0, isolated = 0, none = 0;
1726 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1727 _pte++, address += PAGE_SIZE) {
1728 pte_t pteval = *_pte;
1729 if (pte_none(pteval)) {
1730 if (++none <= khugepaged_max_ptes_none)
1733 release_pte_pages(pte, _pte);
1737 if (!pte_present(pteval) || !pte_write(pteval)) {
1738 release_pte_pages(pte, _pte);
1741 page = vm_normal_page(vma, address, pteval);
1742 if (unlikely(!page)) {
1743 release_pte_pages(pte, _pte);
1746 VM_BUG_ON(PageCompound(page));
1747 BUG_ON(!PageAnon(page));
1748 VM_BUG_ON(!PageSwapBacked(page));
1750 /* cannot use mapcount: can't collapse if there's a gup pin */
1751 if (page_count(page) != 1) {
1752 release_pte_pages(pte, _pte);
1756 * We can do it before isolate_lru_page because the
1757 * page can't be freed from under us. NOTE: PG_lock
1758 * is needed to serialize against split_huge_page
1759 * when invoked from the VM.
1761 if (!trylock_page(page)) {
1762 release_pte_pages(pte, _pte);
1766 * Isolate the page to avoid collapsing an hugepage
1767 * currently in use by the VM.
1769 if (isolate_lru_page(page)) {
1771 release_pte_pages(pte, _pte);
1774 /* 0 stands for page_is_file_cache(page) == false */
1775 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1776 VM_BUG_ON(!PageLocked(page));
1777 VM_BUG_ON(PageLRU(page));
1779 /* If there is no mapped pte young don't collapse the page */
1780 if (pte_young(pteval) || PageReferenced(page) ||
1781 mmu_notifier_test_young(vma->vm_mm, address))
1784 if (unlikely(!referenced))
1785 release_all_pte_pages(pte);
1792 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1793 struct vm_area_struct *vma,
1794 unsigned long address,
1798 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1799 pte_t pteval = *_pte;
1800 struct page *src_page;
1802 if (pte_none(pteval)) {
1803 clear_user_highpage(page, address);
1804 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1806 src_page = pte_page(pteval);
1807 copy_user_highpage(page, src_page, address, vma);
1808 VM_BUG_ON(page_mapcount(src_page) != 1);
1809 VM_BUG_ON(page_count(src_page) != 2);
1810 release_pte_page(src_page);
1812 * ptl mostly unnecessary, but preempt has to
1813 * be disabled to update the per-cpu stats
1814 * inside page_remove_rmap().
1818 * paravirt calls inside pte_clear here are
1821 pte_clear(vma->vm_mm, address, _pte);
1822 page_remove_rmap(src_page);
1824 free_page_and_swap_cache(src_page);
1827 address += PAGE_SIZE;
1832 static bool hugepage_vma_check(struct vm_area_struct *vma)
1834 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1835 (vma->vm_flags & VM_NOHUGEPAGE))
1838 if (!vma->anon_vma || vma->vm_ops)
1840 if (is_vma_temporary_stack(vma))
1843 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1844 * true too, verify it here.
1846 VM_BUG_ON(is_linear_pfn_mapping(vma));
1847 return !(vma->vm_flags & VM_NO_THP);
1850 static void collapse_huge_page(struct mm_struct *mm,
1851 unsigned long address,
1852 struct page **hpage,
1853 struct vm_area_struct *vma,
1861 struct page *new_page;
1864 unsigned long hstart, hend;
1866 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1868 up_read(&mm->mmap_sem);
1874 * Allocate the page while the vma is still valid and under
1875 * the mmap_sem read mode so there is no memory allocation
1876 * later when we take the mmap_sem in write mode. This is more
1877 * friendly behavior (OTOH it may actually hide bugs) to
1878 * filesystems in userland with daemons allocating memory in
1879 * the userland I/O paths. Allocating memory with the
1880 * mmap_sem in read mode is good idea also to allow greater
1883 new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1884 node, __GFP_OTHER_NODE);
1887 * After allocating the hugepage, release the mmap_sem read lock in
1888 * preparation for taking it in write mode.
1890 up_read(&mm->mmap_sem);
1891 if (unlikely(!new_page)) {
1892 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1893 *hpage = ERR_PTR(-ENOMEM);
1898 count_vm_event(THP_COLLAPSE_ALLOC);
1899 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1907 * Prevent all access to pagetables with the exception of
1908 * gup_fast later hanlded by the ptep_clear_flush and the VM
1909 * handled by the anon_vma lock + PG_lock.
1911 down_write(&mm->mmap_sem);
1912 if (unlikely(khugepaged_test_exit(mm)))
1915 vma = find_vma(mm, address);
1918 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1919 hend = vma->vm_end & HPAGE_PMD_MASK;
1920 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1922 if (!hugepage_vma_check(vma))
1924 pgd = pgd_offset(mm, address);
1925 if (!pgd_present(*pgd))
1928 pud = pud_offset(pgd, address);
1929 if (!pud_present(*pud))
1932 pmd = pmd_offset(pud, address);
1933 /* pmd can't go away or become huge under us */
1934 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1937 anon_vma_lock(vma->anon_vma);
1939 pte = pte_offset_map(pmd, address);
1940 ptl = pte_lockptr(mm, pmd);
1942 spin_lock(&mm->page_table_lock); /* probably unnecessary */
1944 * After this gup_fast can't run anymore. This also removes
1945 * any huge TLB entry from the CPU so we won't allow
1946 * huge and small TLB entries for the same virtual address
1947 * to avoid the risk of CPU bugs in that area.
1949 _pmd = pmdp_clear_flush_notify(vma, address, pmd);
1950 spin_unlock(&mm->page_table_lock);
1953 isolated = __collapse_huge_page_isolate(vma, address, pte);
1956 if (unlikely(!isolated)) {
1958 spin_lock(&mm->page_table_lock);
1959 BUG_ON(!pmd_none(*pmd));
1961 * We can only use set_pmd_at when establishing
1962 * hugepmds and never for establishing regular pmds that
1963 * points to regular pagetables. Use pmd_populate for that
1965 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1966 spin_unlock(&mm->page_table_lock);
1967 anon_vma_unlock(vma->anon_vma);
1972 * All pages are isolated and locked so anon_vma rmap
1973 * can't run anymore.
1975 anon_vma_unlock(vma->anon_vma);
1977 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
1979 __SetPageUptodate(new_page);
1980 pgtable = pmd_pgtable(_pmd);
1981 VM_BUG_ON(page_count(pgtable) != 1);
1982 VM_BUG_ON(page_mapcount(pgtable) != 0);
1984 _pmd = mk_pmd(new_page, vma->vm_page_prot);
1985 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1986 _pmd = pmd_mkhuge(_pmd);
1989 * spin_lock() below is not the equivalent of smp_wmb(), so
1990 * this is needed to avoid the copy_huge_page writes to become
1991 * visible after the set_pmd_at() write.
1995 spin_lock(&mm->page_table_lock);
1996 BUG_ON(!pmd_none(*pmd));
1997 page_add_new_anon_rmap(new_page, vma, address);
1998 set_pmd_at(mm, address, pmd, _pmd);
1999 update_mmu_cache(vma, address, _pmd);
2000 prepare_pmd_huge_pte(pgtable, mm);
2001 spin_unlock(&mm->page_table_lock);
2006 khugepaged_pages_collapsed++;
2008 up_write(&mm->mmap_sem);
2012 mem_cgroup_uncharge_page(new_page);
2019 static int khugepaged_scan_pmd(struct mm_struct *mm,
2020 struct vm_area_struct *vma,
2021 unsigned long address,
2022 struct page **hpage)
2028 int ret = 0, referenced = 0, none = 0;
2030 unsigned long _address;
2034 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2036 pgd = pgd_offset(mm, address);
2037 if (!pgd_present(*pgd))
2040 pud = pud_offset(pgd, address);
2041 if (!pud_present(*pud))
2044 pmd = pmd_offset(pud, address);
2045 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2048 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2049 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2050 _pte++, _address += PAGE_SIZE) {
2051 pte_t pteval = *_pte;
2052 if (pte_none(pteval)) {
2053 if (++none <= khugepaged_max_ptes_none)
2058 if (!pte_present(pteval) || !pte_write(pteval))
2060 page = vm_normal_page(vma, _address, pteval);
2061 if (unlikely(!page))
2064 * Chose the node of the first page. This could
2065 * be more sophisticated and look at more pages,
2066 * but isn't for now.
2069 node = page_to_nid(page);
2070 VM_BUG_ON(PageCompound(page));
2071 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2073 /* cannot use mapcount: can't collapse if there's a gup pin */
2074 if (page_count(page) != 1)
2076 if (pte_young(pteval) || PageReferenced(page) ||
2077 mmu_notifier_test_young(vma->vm_mm, address))
2083 pte_unmap_unlock(pte, ptl);
2085 /* collapse_huge_page will return with the mmap_sem released */
2086 collapse_huge_page(mm, address, hpage, vma, node);
2091 static void collect_mm_slot(struct mm_slot *mm_slot)
2093 struct mm_struct *mm = mm_slot->mm;
2095 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2097 if (khugepaged_test_exit(mm)) {
2099 hlist_del(&mm_slot->hash);
2100 list_del(&mm_slot->mm_node);
2103 * Not strictly needed because the mm exited already.
2105 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2108 /* khugepaged_mm_lock actually not necessary for the below */
2109 free_mm_slot(mm_slot);
2114 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2115 struct page **hpage)
2116 __releases(&khugepaged_mm_lock)
2117 __acquires(&khugepaged_mm_lock)
2119 struct mm_slot *mm_slot;
2120 struct mm_struct *mm;
2121 struct vm_area_struct *vma;
2125 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2127 if (khugepaged_scan.mm_slot)
2128 mm_slot = khugepaged_scan.mm_slot;
2130 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2131 struct mm_slot, mm_node);
2132 khugepaged_scan.address = 0;
2133 khugepaged_scan.mm_slot = mm_slot;
2135 spin_unlock(&khugepaged_mm_lock);
2138 down_read(&mm->mmap_sem);
2139 if (unlikely(khugepaged_test_exit(mm)))
2142 vma = find_vma(mm, khugepaged_scan.address);
2145 for (; vma; vma = vma->vm_next) {
2146 unsigned long hstart, hend;
2149 if (unlikely(khugepaged_test_exit(mm))) {
2153 if (!hugepage_vma_check(vma)) {
2158 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2159 hend = vma->vm_end & HPAGE_PMD_MASK;
2162 if (khugepaged_scan.address > hend)
2164 if (khugepaged_scan.address < hstart)
2165 khugepaged_scan.address = hstart;
2166 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2168 while (khugepaged_scan.address < hend) {
2171 if (unlikely(khugepaged_test_exit(mm)))
2172 goto breakouterloop;
2174 VM_BUG_ON(khugepaged_scan.address < hstart ||
2175 khugepaged_scan.address + HPAGE_PMD_SIZE >
2177 ret = khugepaged_scan_pmd(mm, vma,
2178 khugepaged_scan.address,
2180 /* move to next address */
2181 khugepaged_scan.address += HPAGE_PMD_SIZE;
2182 progress += HPAGE_PMD_NR;
2184 /* we released mmap_sem so break loop */
2185 goto breakouterloop_mmap_sem;
2186 if (progress >= pages)
2187 goto breakouterloop;
2191 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2192 breakouterloop_mmap_sem:
2194 spin_lock(&khugepaged_mm_lock);
2195 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2197 * Release the current mm_slot if this mm is about to die, or
2198 * if we scanned all vmas of this mm.
2200 if (khugepaged_test_exit(mm) || !vma) {
2202 * Make sure that if mm_users is reaching zero while
2203 * khugepaged runs here, khugepaged_exit will find
2204 * mm_slot not pointing to the exiting mm.
2206 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2207 khugepaged_scan.mm_slot = list_entry(
2208 mm_slot->mm_node.next,
2209 struct mm_slot, mm_node);
2210 khugepaged_scan.address = 0;
2212 khugepaged_scan.mm_slot = NULL;
2213 khugepaged_full_scans++;
2216 collect_mm_slot(mm_slot);
2222 static int khugepaged_has_work(void)
2224 return !list_empty(&khugepaged_scan.mm_head) &&
2225 khugepaged_enabled();
2228 static int khugepaged_wait_event(void)
2230 return !list_empty(&khugepaged_scan.mm_head) ||
2231 !khugepaged_enabled();
2234 static void khugepaged_do_scan(struct page **hpage)
2236 unsigned int progress = 0, pass_through_head = 0;
2237 unsigned int pages = khugepaged_pages_to_scan;
2239 barrier(); /* write khugepaged_pages_to_scan to local stack */
2241 while (progress < pages) {
2246 *hpage = alloc_hugepage(khugepaged_defrag());
2247 if (unlikely(!*hpage)) {
2248 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2251 count_vm_event(THP_COLLAPSE_ALLOC);
2258 if (unlikely(kthread_should_stop() || freezing(current)))
2261 spin_lock(&khugepaged_mm_lock);
2262 if (!khugepaged_scan.mm_slot)
2263 pass_through_head++;
2264 if (khugepaged_has_work() &&
2265 pass_through_head < 2)
2266 progress += khugepaged_scan_mm_slot(pages - progress,
2270 spin_unlock(&khugepaged_mm_lock);
2274 static void khugepaged_alloc_sleep(void)
2276 wait_event_freezable_timeout(khugepaged_wait, false,
2277 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2281 static struct page *khugepaged_alloc_hugepage(void)
2286 hpage = alloc_hugepage(khugepaged_defrag());
2288 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2289 khugepaged_alloc_sleep();
2291 count_vm_event(THP_COLLAPSE_ALLOC);
2292 } while (unlikely(!hpage) &&
2293 likely(khugepaged_enabled()));
2298 static void khugepaged_loop(void)
2305 while (likely(khugepaged_enabled())) {
2307 hpage = khugepaged_alloc_hugepage();
2308 if (unlikely(!hpage))
2311 if (IS_ERR(hpage)) {
2312 khugepaged_alloc_sleep();
2317 khugepaged_do_scan(&hpage);
2323 if (unlikely(kthread_should_stop()))
2325 if (khugepaged_has_work()) {
2326 if (!khugepaged_scan_sleep_millisecs)
2328 wait_event_freezable_timeout(khugepaged_wait, false,
2329 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2330 } else if (khugepaged_enabled())
2331 wait_event_freezable(khugepaged_wait,
2332 khugepaged_wait_event());
2336 static int khugepaged(void *none)
2338 struct mm_slot *mm_slot;
2341 set_user_nice(current, 19);
2343 /* serialize with start_khugepaged() */
2344 mutex_lock(&khugepaged_mutex);
2347 mutex_unlock(&khugepaged_mutex);
2348 VM_BUG_ON(khugepaged_thread != current);
2350 VM_BUG_ON(khugepaged_thread != current);
2352 mutex_lock(&khugepaged_mutex);
2353 if (!khugepaged_enabled())
2355 if (unlikely(kthread_should_stop()))
2359 spin_lock(&khugepaged_mm_lock);
2360 mm_slot = khugepaged_scan.mm_slot;
2361 khugepaged_scan.mm_slot = NULL;
2363 collect_mm_slot(mm_slot);
2364 spin_unlock(&khugepaged_mm_lock);
2366 khugepaged_thread = NULL;
2367 mutex_unlock(&khugepaged_mutex);
2372 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2376 spin_lock(&mm->page_table_lock);
2377 if (unlikely(!pmd_trans_huge(*pmd))) {
2378 spin_unlock(&mm->page_table_lock);
2381 page = pmd_page(*pmd);
2382 VM_BUG_ON(!page_count(page));
2384 spin_unlock(&mm->page_table_lock);
2386 split_huge_page(page);
2389 BUG_ON(pmd_trans_huge(*pmd));
2392 static void split_huge_page_address(struct mm_struct *mm,
2393 unsigned long address)
2399 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2401 pgd = pgd_offset(mm, address);
2402 if (!pgd_present(*pgd))
2405 pud = pud_offset(pgd, address);
2406 if (!pud_present(*pud))
2409 pmd = pmd_offset(pud, address);
2410 if (!pmd_present(*pmd))
2413 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2414 * materialize from under us.
2416 split_huge_page_pmd(mm, pmd);
2419 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2420 unsigned long start,
2425 * If the new start address isn't hpage aligned and it could
2426 * previously contain an hugepage: check if we need to split
2429 if (start & ~HPAGE_PMD_MASK &&
2430 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2431 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2432 split_huge_page_address(vma->vm_mm, start);
2435 * If the new end address isn't hpage aligned and it could
2436 * previously contain an hugepage: check if we need to split
2439 if (end & ~HPAGE_PMD_MASK &&
2440 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2441 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2442 split_huge_page_address(vma->vm_mm, end);
2445 * If we're also updating the vma->vm_next->vm_start, if the new
2446 * vm_next->vm_start isn't page aligned and it could previously
2447 * contain an hugepage: check if we need to split an huge pmd.
2449 if (adjust_next > 0) {
2450 struct vm_area_struct *next = vma->vm_next;
2451 unsigned long nstart = next->vm_start;
2452 nstart += adjust_next << PAGE_SHIFT;
2453 if (nstart & ~HPAGE_PMD_MASK &&
2454 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2455 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2456 split_huge_page_address(next->vm_mm, nstart);