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 (!khugepaged_enabled())
108 for_each_populated_zone(zone)
111 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
112 recommended_min = pageblock_nr_pages * nr_zones * 2;
115 * Make sure that on average at least two pageblocks are almost free
116 * of another type, one for a migratetype to fall back to and a
117 * second to avoid subsequent fallbacks of other types There are 3
118 * MIGRATE_TYPES we care about.
120 recommended_min += pageblock_nr_pages * nr_zones *
121 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
123 /* don't ever allow to reserve more than 5% of the lowmem */
124 recommended_min = min(recommended_min,
125 (unsigned long) nr_free_buffer_pages() / 20);
126 recommended_min <<= (PAGE_SHIFT-10);
128 if (recommended_min > min_free_kbytes)
129 min_free_kbytes = recommended_min;
130 setup_per_zone_wmarks();
133 late_initcall(set_recommended_min_free_kbytes);
135 static int start_khugepaged(void)
138 if (khugepaged_enabled()) {
139 if (!khugepaged_thread)
140 khugepaged_thread = kthread_run(khugepaged, NULL,
142 if (unlikely(IS_ERR(khugepaged_thread))) {
144 "khugepaged: kthread_run(khugepaged) failed\n");
145 err = PTR_ERR(khugepaged_thread);
146 khugepaged_thread = NULL;
149 if (!list_empty(&khugepaged_scan.mm_head))
150 wake_up_interruptible(&khugepaged_wait);
152 set_recommended_min_free_kbytes();
153 } else if (khugepaged_thread) {
154 kthread_stop(khugepaged_thread);
155 khugepaged_thread = NULL;
163 static ssize_t double_flag_show(struct kobject *kobj,
164 struct kobj_attribute *attr, char *buf,
165 enum transparent_hugepage_flag enabled,
166 enum transparent_hugepage_flag req_madv)
168 if (test_bit(enabled, &transparent_hugepage_flags)) {
169 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
170 return sprintf(buf, "[always] madvise never\n");
171 } else if (test_bit(req_madv, &transparent_hugepage_flags))
172 return sprintf(buf, "always [madvise] never\n");
174 return sprintf(buf, "always madvise [never]\n");
176 static ssize_t double_flag_store(struct kobject *kobj,
177 struct kobj_attribute *attr,
178 const char *buf, size_t count,
179 enum transparent_hugepage_flag enabled,
180 enum transparent_hugepage_flag req_madv)
182 if (!memcmp("always", buf,
183 min(sizeof("always")-1, count))) {
184 set_bit(enabled, &transparent_hugepage_flags);
185 clear_bit(req_madv, &transparent_hugepage_flags);
186 } else if (!memcmp("madvise", buf,
187 min(sizeof("madvise")-1, count))) {
188 clear_bit(enabled, &transparent_hugepage_flags);
189 set_bit(req_madv, &transparent_hugepage_flags);
190 } else if (!memcmp("never", buf,
191 min(sizeof("never")-1, count))) {
192 clear_bit(enabled, &transparent_hugepage_flags);
193 clear_bit(req_madv, &transparent_hugepage_flags);
200 static ssize_t enabled_show(struct kobject *kobj,
201 struct kobj_attribute *attr, char *buf)
203 return double_flag_show(kobj, attr, buf,
204 TRANSPARENT_HUGEPAGE_FLAG,
205 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
207 static ssize_t enabled_store(struct kobject *kobj,
208 struct kobj_attribute *attr,
209 const char *buf, size_t count)
213 ret = double_flag_store(kobj, attr, buf, count,
214 TRANSPARENT_HUGEPAGE_FLAG,
215 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
220 mutex_lock(&khugepaged_mutex);
221 err = start_khugepaged();
222 mutex_unlock(&khugepaged_mutex);
230 static struct kobj_attribute enabled_attr =
231 __ATTR(enabled, 0644, enabled_show, enabled_store);
233 static ssize_t single_flag_show(struct kobject *kobj,
234 struct kobj_attribute *attr, char *buf,
235 enum transparent_hugepage_flag flag)
237 return sprintf(buf, "%d\n",
238 !!test_bit(flag, &transparent_hugepage_flags));
241 static ssize_t single_flag_store(struct kobject *kobj,
242 struct kobj_attribute *attr,
243 const char *buf, size_t count,
244 enum transparent_hugepage_flag flag)
249 ret = kstrtoul(buf, 10, &value);
256 set_bit(flag, &transparent_hugepage_flags);
258 clear_bit(flag, &transparent_hugepage_flags);
264 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
265 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
266 * memory just to allocate one more hugepage.
268 static ssize_t defrag_show(struct kobject *kobj,
269 struct kobj_attribute *attr, char *buf)
271 return double_flag_show(kobj, attr, buf,
272 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
273 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
275 static ssize_t defrag_store(struct kobject *kobj,
276 struct kobj_attribute *attr,
277 const char *buf, size_t count)
279 return double_flag_store(kobj, attr, buf, count,
280 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
281 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
283 static struct kobj_attribute defrag_attr =
284 __ATTR(defrag, 0644, defrag_show, defrag_store);
286 #ifdef CONFIG_DEBUG_VM
287 static ssize_t debug_cow_show(struct kobject *kobj,
288 struct kobj_attribute *attr, char *buf)
290 return single_flag_show(kobj, attr, buf,
291 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
293 static ssize_t debug_cow_store(struct kobject *kobj,
294 struct kobj_attribute *attr,
295 const char *buf, size_t count)
297 return single_flag_store(kobj, attr, buf, count,
298 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
300 static struct kobj_attribute debug_cow_attr =
301 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
302 #endif /* CONFIG_DEBUG_VM */
304 static struct attribute *hugepage_attr[] = {
307 #ifdef CONFIG_DEBUG_VM
308 &debug_cow_attr.attr,
313 static struct attribute_group hugepage_attr_group = {
314 .attrs = hugepage_attr,
317 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
318 struct kobj_attribute *attr,
321 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
324 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
325 struct kobj_attribute *attr,
326 const char *buf, size_t count)
331 err = strict_strtoul(buf, 10, &msecs);
332 if (err || msecs > UINT_MAX)
335 khugepaged_scan_sleep_millisecs = msecs;
336 wake_up_interruptible(&khugepaged_wait);
340 static struct kobj_attribute scan_sleep_millisecs_attr =
341 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
342 scan_sleep_millisecs_store);
344 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
345 struct kobj_attribute *attr,
348 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
351 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
352 struct kobj_attribute *attr,
353 const char *buf, size_t count)
358 err = strict_strtoul(buf, 10, &msecs);
359 if (err || msecs > UINT_MAX)
362 khugepaged_alloc_sleep_millisecs = msecs;
363 wake_up_interruptible(&khugepaged_wait);
367 static struct kobj_attribute alloc_sleep_millisecs_attr =
368 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
369 alloc_sleep_millisecs_store);
371 static ssize_t pages_to_scan_show(struct kobject *kobj,
372 struct kobj_attribute *attr,
375 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
377 static ssize_t pages_to_scan_store(struct kobject *kobj,
378 struct kobj_attribute *attr,
379 const char *buf, size_t count)
384 err = strict_strtoul(buf, 10, &pages);
385 if (err || !pages || pages > UINT_MAX)
388 khugepaged_pages_to_scan = pages;
392 static struct kobj_attribute pages_to_scan_attr =
393 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
394 pages_to_scan_store);
396 static ssize_t pages_collapsed_show(struct kobject *kobj,
397 struct kobj_attribute *attr,
400 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
402 static struct kobj_attribute pages_collapsed_attr =
403 __ATTR_RO(pages_collapsed);
405 static ssize_t full_scans_show(struct kobject *kobj,
406 struct kobj_attribute *attr,
409 return sprintf(buf, "%u\n", khugepaged_full_scans);
411 static struct kobj_attribute full_scans_attr =
412 __ATTR_RO(full_scans);
414 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
415 struct kobj_attribute *attr, char *buf)
417 return single_flag_show(kobj, attr, buf,
418 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
420 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
421 struct kobj_attribute *attr,
422 const char *buf, size_t count)
424 return single_flag_store(kobj, attr, buf, count,
425 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
427 static struct kobj_attribute khugepaged_defrag_attr =
428 __ATTR(defrag, 0644, khugepaged_defrag_show,
429 khugepaged_defrag_store);
432 * max_ptes_none controls if khugepaged should collapse hugepages over
433 * any unmapped ptes in turn potentially increasing the memory
434 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
435 * reduce the available free memory in the system as it
436 * runs. Increasing max_ptes_none will instead potentially reduce the
437 * free memory in the system during the khugepaged scan.
439 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
440 struct kobj_attribute *attr,
443 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
445 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
446 struct kobj_attribute *attr,
447 const char *buf, size_t count)
450 unsigned long max_ptes_none;
452 err = strict_strtoul(buf, 10, &max_ptes_none);
453 if (err || max_ptes_none > HPAGE_PMD_NR-1)
456 khugepaged_max_ptes_none = max_ptes_none;
460 static struct kobj_attribute khugepaged_max_ptes_none_attr =
461 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
462 khugepaged_max_ptes_none_store);
464 static struct attribute *khugepaged_attr[] = {
465 &khugepaged_defrag_attr.attr,
466 &khugepaged_max_ptes_none_attr.attr,
467 &pages_to_scan_attr.attr,
468 &pages_collapsed_attr.attr,
469 &full_scans_attr.attr,
470 &scan_sleep_millisecs_attr.attr,
471 &alloc_sleep_millisecs_attr.attr,
475 static struct attribute_group khugepaged_attr_group = {
476 .attrs = khugepaged_attr,
477 .name = "khugepaged",
480 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
484 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
485 if (unlikely(!*hugepage_kobj)) {
486 printk(KERN_ERR "hugepage: failed kobject create\n");
490 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
492 printk(KERN_ERR "hugepage: failed register hugeage group\n");
496 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
498 printk(KERN_ERR "hugepage: failed register hugeage group\n");
499 goto remove_hp_group;
505 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
507 kobject_put(*hugepage_kobj);
511 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
513 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
514 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
515 kobject_put(hugepage_kobj);
518 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
523 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
526 #endif /* CONFIG_SYSFS */
528 static int __init hugepage_init(void)
531 struct kobject *hugepage_kobj;
533 if (!has_transparent_hugepage()) {
534 transparent_hugepage_flags = 0;
538 err = hugepage_init_sysfs(&hugepage_kobj);
542 err = khugepaged_slab_init();
546 err = mm_slots_hash_init();
548 khugepaged_slab_free();
553 * By default disable transparent hugepages on smaller systems,
554 * where the extra memory used could hurt more than TLB overhead
555 * is likely to save. The admin can still enable it through /sys.
557 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
558 transparent_hugepage_flags = 0;
564 hugepage_exit_sysfs(hugepage_kobj);
567 module_init(hugepage_init)
569 static int __init setup_transparent_hugepage(char *str)
574 if (!strcmp(str, "always")) {
575 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
576 &transparent_hugepage_flags);
577 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
578 &transparent_hugepage_flags);
580 } else if (!strcmp(str, "madvise")) {
581 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
582 &transparent_hugepage_flags);
583 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
584 &transparent_hugepage_flags);
586 } else if (!strcmp(str, "never")) {
587 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
588 &transparent_hugepage_flags);
589 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
590 &transparent_hugepage_flags);
596 "transparent_hugepage= cannot parse, ignored\n");
599 __setup("transparent_hugepage=", setup_transparent_hugepage);
601 static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
603 if (likely(vma->vm_flags & VM_WRITE))
604 pmd = pmd_mkwrite(pmd);
608 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
609 struct vm_area_struct *vma,
610 unsigned long haddr, pmd_t *pmd,
615 VM_BUG_ON(!PageCompound(page));
616 pgtable = pte_alloc_one(mm, haddr);
617 if (unlikely(!pgtable))
620 clear_huge_page(page, haddr, HPAGE_PMD_NR);
621 __SetPageUptodate(page);
623 spin_lock(&mm->page_table_lock);
624 if (unlikely(!pmd_none(*pmd))) {
625 spin_unlock(&mm->page_table_lock);
626 mem_cgroup_uncharge_page(page);
628 pte_free(mm, pgtable);
631 entry = mk_pmd(page, vma->vm_page_prot);
632 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
633 entry = pmd_mkhuge(entry);
635 * The spinlocking to take the lru_lock inside
636 * page_add_new_anon_rmap() acts as a full memory
637 * barrier to be sure clear_huge_page writes become
638 * visible after the set_pmd_at() write.
640 page_add_new_anon_rmap(page, vma, haddr);
641 set_pmd_at(mm, haddr, pmd, entry);
642 pgtable_trans_huge_deposit(mm, pgtable);
643 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
645 spin_unlock(&mm->page_table_lock);
651 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
653 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
656 static inline struct page *alloc_hugepage_vma(int defrag,
657 struct vm_area_struct *vma,
658 unsigned long haddr, int nd,
661 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
662 HPAGE_PMD_ORDER, vma, haddr, nd);
666 static inline struct page *alloc_hugepage(int defrag)
668 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
673 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
674 unsigned long address, pmd_t *pmd,
678 unsigned long haddr = address & HPAGE_PMD_MASK;
681 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
682 if (unlikely(anon_vma_prepare(vma)))
684 if (unlikely(khugepaged_enter(vma)))
686 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
687 vma, haddr, numa_node_id(), 0);
688 if (unlikely(!page)) {
689 count_vm_event(THP_FAULT_FALLBACK);
692 count_vm_event(THP_FAULT_ALLOC);
693 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
697 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
699 mem_cgroup_uncharge_page(page);
708 * Use __pte_alloc instead of pte_alloc_map, because we can't
709 * run pte_offset_map on the pmd, if an huge pmd could
710 * materialize from under us from a different thread.
712 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
714 /* if an huge pmd materialized from under us just retry later */
715 if (unlikely(pmd_trans_huge(*pmd)))
718 * A regular pmd is established and it can't morph into a huge pmd
719 * from under us anymore at this point because we hold the mmap_sem
720 * read mode and khugepaged takes it in write mode. So now it's
721 * safe to run pte_offset_map().
723 pte = pte_offset_map(pmd, address);
724 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
727 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
728 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
729 struct vm_area_struct *vma)
731 struct page *src_page;
737 pgtable = pte_alloc_one(dst_mm, addr);
738 if (unlikely(!pgtable))
741 spin_lock(&dst_mm->page_table_lock);
742 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
746 if (unlikely(!pmd_trans_huge(pmd))) {
747 pte_free(dst_mm, pgtable);
750 if (unlikely(pmd_trans_splitting(pmd))) {
751 /* split huge page running from under us */
752 spin_unlock(&src_mm->page_table_lock);
753 spin_unlock(&dst_mm->page_table_lock);
754 pte_free(dst_mm, pgtable);
756 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
759 src_page = pmd_page(pmd);
760 VM_BUG_ON(!PageHead(src_page));
762 page_dup_rmap(src_page);
763 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
765 pmdp_set_wrprotect(src_mm, addr, src_pmd);
766 pmd = pmd_mkold(pmd_wrprotect(pmd));
767 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
768 pgtable_trans_huge_deposit(dst_mm, pgtable);
773 spin_unlock(&src_mm->page_table_lock);
774 spin_unlock(&dst_mm->page_table_lock);
779 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
780 struct vm_area_struct *vma,
781 unsigned long address,
782 pmd_t *pmd, pmd_t orig_pmd,
790 unsigned long mmun_start; /* For mmu_notifiers */
791 unsigned long mmun_end; /* For mmu_notifiers */
793 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
795 if (unlikely(!pages)) {
800 for (i = 0; i < HPAGE_PMD_NR; i++) {
801 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
803 vma, address, page_to_nid(page));
804 if (unlikely(!pages[i] ||
805 mem_cgroup_newpage_charge(pages[i], mm,
809 mem_cgroup_uncharge_start();
811 mem_cgroup_uncharge_page(pages[i]);
814 mem_cgroup_uncharge_end();
821 for (i = 0; i < HPAGE_PMD_NR; i++) {
822 copy_user_highpage(pages[i], page + i,
823 haddr + PAGE_SIZE * i, vma);
824 __SetPageUptodate(pages[i]);
829 mmun_end = haddr + HPAGE_PMD_SIZE;
830 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
832 spin_lock(&mm->page_table_lock);
833 if (unlikely(!pmd_same(*pmd, orig_pmd)))
835 VM_BUG_ON(!PageHead(page));
837 pmdp_clear_flush(vma, haddr, pmd);
838 /* leave pmd empty until pte is filled */
840 pgtable = pgtable_trans_huge_withdraw(mm);
841 pmd_populate(mm, &_pmd, pgtable);
843 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
845 entry = mk_pte(pages[i], vma->vm_page_prot);
846 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
847 page_add_new_anon_rmap(pages[i], vma, haddr);
848 pte = pte_offset_map(&_pmd, haddr);
849 VM_BUG_ON(!pte_none(*pte));
850 set_pte_at(mm, haddr, pte, entry);
855 smp_wmb(); /* make pte visible before pmd */
856 pmd_populate(mm, pmd, pgtable);
857 page_remove_rmap(page);
858 spin_unlock(&mm->page_table_lock);
860 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
862 ret |= VM_FAULT_WRITE;
869 spin_unlock(&mm->page_table_lock);
870 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
871 mem_cgroup_uncharge_start();
872 for (i = 0; i < HPAGE_PMD_NR; i++) {
873 mem_cgroup_uncharge_page(pages[i]);
876 mem_cgroup_uncharge_end();
881 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
882 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
885 struct page *page, *new_page;
887 unsigned long mmun_start; /* For mmu_notifiers */
888 unsigned long mmun_end; /* For mmu_notifiers */
890 VM_BUG_ON(!vma->anon_vma);
891 spin_lock(&mm->page_table_lock);
892 if (unlikely(!pmd_same(*pmd, orig_pmd)))
895 page = pmd_page(orig_pmd);
896 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
897 haddr = address & HPAGE_PMD_MASK;
898 if (page_mapcount(page) == 1) {
900 entry = pmd_mkyoung(orig_pmd);
901 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
902 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
903 update_mmu_cache(vma, address, entry);
904 ret |= VM_FAULT_WRITE;
908 spin_unlock(&mm->page_table_lock);
910 if (transparent_hugepage_enabled(vma) &&
911 !transparent_hugepage_debug_cow())
912 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
913 vma, haddr, numa_node_id(), 0);
917 if (unlikely(!new_page)) {
918 count_vm_event(THP_FAULT_FALLBACK);
919 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
920 pmd, orig_pmd, page, haddr);
921 if (ret & VM_FAULT_OOM)
922 split_huge_page(page);
926 count_vm_event(THP_FAULT_ALLOC);
928 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
930 split_huge_page(page);
936 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
937 __SetPageUptodate(new_page);
940 mmun_end = haddr + HPAGE_PMD_SIZE;
941 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
943 spin_lock(&mm->page_table_lock);
945 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
946 spin_unlock(&mm->page_table_lock);
947 mem_cgroup_uncharge_page(new_page);
952 VM_BUG_ON(!PageHead(page));
953 entry = mk_pmd(new_page, vma->vm_page_prot);
954 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
955 entry = pmd_mkhuge(entry);
956 pmdp_clear_flush(vma, haddr, pmd);
957 page_add_new_anon_rmap(new_page, vma, haddr);
958 set_pmd_at(mm, haddr, pmd, entry);
959 update_mmu_cache(vma, address, entry);
960 page_remove_rmap(page);
962 ret |= VM_FAULT_WRITE;
964 spin_unlock(&mm->page_table_lock);
966 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
970 spin_unlock(&mm->page_table_lock);
974 struct page *follow_trans_huge_pmd(struct mm_struct *mm,
979 struct page *page = NULL;
981 assert_spin_locked(&mm->page_table_lock);
983 if (flags & FOLL_WRITE && !pmd_write(*pmd))
986 page = pmd_page(*pmd);
987 VM_BUG_ON(!PageHead(page));
988 if (flags & FOLL_TOUCH) {
991 * We should set the dirty bit only for FOLL_WRITE but
992 * for now the dirty bit in the pmd is meaningless.
993 * And if the dirty bit will become meaningful and
994 * we'll only set it with FOLL_WRITE, an atomic
995 * set_bit will be required on the pmd to set the
996 * young bit, instead of the current set_pmd_at.
998 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
999 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1001 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1002 VM_BUG_ON(!PageCompound(page));
1003 if (flags & FOLL_GET)
1004 get_page_foll(page);
1010 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1011 pmd_t *pmd, unsigned long addr)
1015 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1018 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1019 page = pmd_page(*pmd);
1021 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1022 page_remove_rmap(page);
1023 VM_BUG_ON(page_mapcount(page) < 0);
1024 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1025 VM_BUG_ON(!PageHead(page));
1027 spin_unlock(&tlb->mm->page_table_lock);
1028 tlb_remove_page(tlb, page);
1029 pte_free(tlb->mm, pgtable);
1035 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1036 unsigned long addr, unsigned long end,
1041 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1043 * All logical pages in the range are present
1044 * if backed by a huge page.
1046 spin_unlock(&vma->vm_mm->page_table_lock);
1047 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1054 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1055 unsigned long old_addr,
1056 unsigned long new_addr, unsigned long old_end,
1057 pmd_t *old_pmd, pmd_t *new_pmd)
1062 struct mm_struct *mm = vma->vm_mm;
1064 if ((old_addr & ~HPAGE_PMD_MASK) ||
1065 (new_addr & ~HPAGE_PMD_MASK) ||
1066 old_end - old_addr < HPAGE_PMD_SIZE ||
1067 (new_vma->vm_flags & VM_NOHUGEPAGE))
1071 * The destination pmd shouldn't be established, free_pgtables()
1072 * should have release it.
1074 if (WARN_ON(!pmd_none(*new_pmd))) {
1075 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1079 ret = __pmd_trans_huge_lock(old_pmd, vma);
1081 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1082 VM_BUG_ON(!pmd_none(*new_pmd));
1083 set_pmd_at(mm, new_addr, new_pmd, pmd);
1084 spin_unlock(&mm->page_table_lock);
1090 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1091 unsigned long addr, pgprot_t newprot)
1093 struct mm_struct *mm = vma->vm_mm;
1096 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1098 entry = pmdp_get_and_clear(mm, addr, pmd);
1099 entry = pmd_modify(entry, newprot);
1100 set_pmd_at(mm, addr, pmd, entry);
1101 spin_unlock(&vma->vm_mm->page_table_lock);
1109 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1110 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1112 * Note that if it returns 1, this routine returns without unlocking page
1113 * table locks. So callers must unlock them.
1115 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1117 spin_lock(&vma->vm_mm->page_table_lock);
1118 if (likely(pmd_trans_huge(*pmd))) {
1119 if (unlikely(pmd_trans_splitting(*pmd))) {
1120 spin_unlock(&vma->vm_mm->page_table_lock);
1121 wait_split_huge_page(vma->anon_vma, pmd);
1124 /* Thp mapped by 'pmd' is stable, so we can
1125 * handle it as it is. */
1129 spin_unlock(&vma->vm_mm->page_table_lock);
1133 pmd_t *page_check_address_pmd(struct page *page,
1134 struct mm_struct *mm,
1135 unsigned long address,
1136 enum page_check_address_pmd_flag flag)
1140 pmd_t *pmd, *ret = NULL;
1142 if (address & ~HPAGE_PMD_MASK)
1145 pgd = pgd_offset(mm, address);
1146 if (!pgd_present(*pgd))
1149 pud = pud_offset(pgd, address);
1150 if (!pud_present(*pud))
1153 pmd = pmd_offset(pud, address);
1156 if (pmd_page(*pmd) != page)
1159 * split_vma() may create temporary aliased mappings. There is
1160 * no risk as long as all huge pmd are found and have their
1161 * splitting bit set before __split_huge_page_refcount
1162 * runs. Finding the same huge pmd more than once during the
1163 * same rmap walk is not a problem.
1165 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1166 pmd_trans_splitting(*pmd))
1168 if (pmd_trans_huge(*pmd)) {
1169 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1170 !pmd_trans_splitting(*pmd));
1177 static int __split_huge_page_splitting(struct page *page,
1178 struct vm_area_struct *vma,
1179 unsigned long address)
1181 struct mm_struct *mm = vma->vm_mm;
1184 /* For mmu_notifiers */
1185 const unsigned long mmun_start = address;
1186 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1188 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1189 spin_lock(&mm->page_table_lock);
1190 pmd = page_check_address_pmd(page, mm, address,
1191 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1194 * We can't temporarily set the pmd to null in order
1195 * to split it, the pmd must remain marked huge at all
1196 * times or the VM won't take the pmd_trans_huge paths
1197 * and it won't wait on the anon_vma->root->mutex to
1198 * serialize against split_huge_page*.
1200 pmdp_splitting_flush(vma, address, pmd);
1203 spin_unlock(&mm->page_table_lock);
1204 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1209 static void __split_huge_page_refcount(struct page *page)
1212 struct zone *zone = page_zone(page);
1213 struct lruvec *lruvec;
1216 /* prevent PageLRU to go away from under us, and freeze lru stats */
1217 spin_lock_irq(&zone->lru_lock);
1218 lruvec = mem_cgroup_page_lruvec(page, zone);
1220 compound_lock(page);
1221 /* complete memcg works before add pages to LRU */
1222 mem_cgroup_split_huge_fixup(page);
1224 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1225 struct page *page_tail = page + i;
1227 /* tail_page->_mapcount cannot change */
1228 BUG_ON(page_mapcount(page_tail) < 0);
1229 tail_count += page_mapcount(page_tail);
1230 /* check for overflow */
1231 BUG_ON(tail_count < 0);
1232 BUG_ON(atomic_read(&page_tail->_count) != 0);
1234 * tail_page->_count is zero and not changing from
1235 * under us. But get_page_unless_zero() may be running
1236 * from under us on the tail_page. If we used
1237 * atomic_set() below instead of atomic_add(), we
1238 * would then run atomic_set() concurrently with
1239 * get_page_unless_zero(), and atomic_set() is
1240 * implemented in C not using locked ops. spin_unlock
1241 * on x86 sometime uses locked ops because of PPro
1242 * errata 66, 92, so unless somebody can guarantee
1243 * atomic_set() here would be safe on all archs (and
1244 * not only on x86), it's safer to use atomic_add().
1246 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1247 &page_tail->_count);
1249 /* after clearing PageTail the gup refcount can be released */
1253 * retain hwpoison flag of the poisoned tail page:
1254 * fix for the unsuitable process killed on Guest Machine(KVM)
1255 * by the memory-failure.
1257 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1258 page_tail->flags |= (page->flags &
1259 ((1L << PG_referenced) |
1260 (1L << PG_swapbacked) |
1261 (1L << PG_mlocked) |
1262 (1L << PG_uptodate)));
1263 page_tail->flags |= (1L << PG_dirty);
1265 /* clear PageTail before overwriting first_page */
1269 * __split_huge_page_splitting() already set the
1270 * splitting bit in all pmd that could map this
1271 * hugepage, that will ensure no CPU can alter the
1272 * mapcount on the head page. The mapcount is only
1273 * accounted in the head page and it has to be
1274 * transferred to all tail pages in the below code. So
1275 * for this code to be safe, the split the mapcount
1276 * can't change. But that doesn't mean userland can't
1277 * keep changing and reading the page contents while
1278 * we transfer the mapcount, so the pmd splitting
1279 * status is achieved setting a reserved bit in the
1280 * pmd, not by clearing the present bit.
1282 page_tail->_mapcount = page->_mapcount;
1284 BUG_ON(page_tail->mapping);
1285 page_tail->mapping = page->mapping;
1287 page_tail->index = page->index + i;
1289 BUG_ON(!PageAnon(page_tail));
1290 BUG_ON(!PageUptodate(page_tail));
1291 BUG_ON(!PageDirty(page_tail));
1292 BUG_ON(!PageSwapBacked(page_tail));
1294 lru_add_page_tail(page, page_tail, lruvec);
1296 atomic_sub(tail_count, &page->_count);
1297 BUG_ON(atomic_read(&page->_count) <= 0);
1299 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1300 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1302 ClearPageCompound(page);
1303 compound_unlock(page);
1304 spin_unlock_irq(&zone->lru_lock);
1306 for (i = 1; i < HPAGE_PMD_NR; i++) {
1307 struct page *page_tail = page + i;
1308 BUG_ON(page_count(page_tail) <= 0);
1310 * Tail pages may be freed if there wasn't any mapping
1311 * like if add_to_swap() is running on a lru page that
1312 * had its mapping zapped. And freeing these pages
1313 * requires taking the lru_lock so we do the put_page
1314 * of the tail pages after the split is complete.
1316 put_page(page_tail);
1320 * Only the head page (now become a regular page) is required
1321 * to be pinned by the caller.
1323 BUG_ON(page_count(page) <= 0);
1326 static int __split_huge_page_map(struct page *page,
1327 struct vm_area_struct *vma,
1328 unsigned long address)
1330 struct mm_struct *mm = vma->vm_mm;
1334 unsigned long haddr;
1336 spin_lock(&mm->page_table_lock);
1337 pmd = page_check_address_pmd(page, mm, address,
1338 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1340 pgtable = pgtable_trans_huge_withdraw(mm);
1341 pmd_populate(mm, &_pmd, pgtable);
1343 for (i = 0, haddr = address; i < HPAGE_PMD_NR;
1344 i++, haddr += PAGE_SIZE) {
1346 BUG_ON(PageCompound(page+i));
1347 entry = mk_pte(page + i, vma->vm_page_prot);
1348 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1349 if (!pmd_write(*pmd))
1350 entry = pte_wrprotect(entry);
1352 BUG_ON(page_mapcount(page) != 1);
1353 if (!pmd_young(*pmd))
1354 entry = pte_mkold(entry);
1355 pte = pte_offset_map(&_pmd, haddr);
1356 BUG_ON(!pte_none(*pte));
1357 set_pte_at(mm, haddr, pte, entry);
1361 smp_wmb(); /* make pte visible before pmd */
1363 * Up to this point the pmd is present and huge and
1364 * userland has the whole access to the hugepage
1365 * during the split (which happens in place). If we
1366 * overwrite the pmd with the not-huge version
1367 * pointing to the pte here (which of course we could
1368 * if all CPUs were bug free), userland could trigger
1369 * a small page size TLB miss on the small sized TLB
1370 * while the hugepage TLB entry is still established
1371 * in the huge TLB. Some CPU doesn't like that. See
1372 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1373 * Erratum 383 on page 93. Intel should be safe but is
1374 * also warns that it's only safe if the permission
1375 * and cache attributes of the two entries loaded in
1376 * the two TLB is identical (which should be the case
1377 * here). But it is generally safer to never allow
1378 * small and huge TLB entries for the same virtual
1379 * address to be loaded simultaneously. So instead of
1380 * doing "pmd_populate(); flush_tlb_range();" we first
1381 * mark the current pmd notpresent (atomically because
1382 * here the pmd_trans_huge and pmd_trans_splitting
1383 * must remain set at all times on the pmd until the
1384 * split is complete for this pmd), then we flush the
1385 * SMP TLB and finally we write the non-huge version
1386 * of the pmd entry with pmd_populate.
1388 pmdp_invalidate(vma, address, pmd);
1389 pmd_populate(mm, pmd, pgtable);
1392 spin_unlock(&mm->page_table_lock);
1397 /* must be called with anon_vma->root->mutex hold */
1398 static void __split_huge_page(struct page *page,
1399 struct anon_vma *anon_vma)
1401 int mapcount, mapcount2;
1402 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1403 struct anon_vma_chain *avc;
1405 BUG_ON(!PageHead(page));
1406 BUG_ON(PageTail(page));
1409 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1410 struct vm_area_struct *vma = avc->vma;
1411 unsigned long addr = vma_address(page, vma);
1412 BUG_ON(is_vma_temporary_stack(vma));
1413 mapcount += __split_huge_page_splitting(page, vma, addr);
1416 * It is critical that new vmas are added to the tail of the
1417 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1418 * and establishes a child pmd before
1419 * __split_huge_page_splitting() freezes the parent pmd (so if
1420 * we fail to prevent copy_huge_pmd() from running until the
1421 * whole __split_huge_page() is complete), we will still see
1422 * the newly established pmd of the child later during the
1423 * walk, to be able to set it as pmd_trans_splitting too.
1425 if (mapcount != page_mapcount(page))
1426 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1427 mapcount, page_mapcount(page));
1428 BUG_ON(mapcount != page_mapcount(page));
1430 __split_huge_page_refcount(page);
1433 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1434 struct vm_area_struct *vma = avc->vma;
1435 unsigned long addr = vma_address(page, vma);
1436 BUG_ON(is_vma_temporary_stack(vma));
1437 mapcount2 += __split_huge_page_map(page, vma, addr);
1439 if (mapcount != mapcount2)
1440 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1441 mapcount, mapcount2, page_mapcount(page));
1442 BUG_ON(mapcount != mapcount2);
1445 int split_huge_page(struct page *page)
1447 struct anon_vma *anon_vma;
1450 BUG_ON(!PageAnon(page));
1451 anon_vma = page_lock_anon_vma(page);
1455 if (!PageCompound(page))
1458 BUG_ON(!PageSwapBacked(page));
1459 __split_huge_page(page, anon_vma);
1460 count_vm_event(THP_SPLIT);
1462 BUG_ON(PageCompound(page));
1464 page_unlock_anon_vma(anon_vma);
1469 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1471 int hugepage_madvise(struct vm_area_struct *vma,
1472 unsigned long *vm_flags, int advice)
1474 struct mm_struct *mm = vma->vm_mm;
1479 * Be somewhat over-protective like KSM for now!
1481 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1483 if (mm->def_flags & VM_NOHUGEPAGE)
1485 *vm_flags &= ~VM_NOHUGEPAGE;
1486 *vm_flags |= VM_HUGEPAGE;
1488 * If the vma become good for khugepaged to scan,
1489 * register it here without waiting a page fault that
1490 * may not happen any time soon.
1492 if (unlikely(khugepaged_enter_vma_merge(vma)))
1495 case MADV_NOHUGEPAGE:
1497 * Be somewhat over-protective like KSM for now!
1499 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1501 *vm_flags &= ~VM_HUGEPAGE;
1502 *vm_flags |= VM_NOHUGEPAGE;
1504 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1505 * this vma even if we leave the mm registered in khugepaged if
1506 * it got registered before VM_NOHUGEPAGE was set.
1514 static int __init khugepaged_slab_init(void)
1516 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1517 sizeof(struct mm_slot),
1518 __alignof__(struct mm_slot), 0, NULL);
1525 static void __init khugepaged_slab_free(void)
1527 kmem_cache_destroy(mm_slot_cache);
1528 mm_slot_cache = NULL;
1531 static inline struct mm_slot *alloc_mm_slot(void)
1533 if (!mm_slot_cache) /* initialization failed */
1535 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1538 static inline void free_mm_slot(struct mm_slot *mm_slot)
1540 kmem_cache_free(mm_slot_cache, mm_slot);
1543 static int __init mm_slots_hash_init(void)
1545 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1553 static void __init mm_slots_hash_free(void)
1555 kfree(mm_slots_hash);
1556 mm_slots_hash = NULL;
1560 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1562 struct mm_slot *mm_slot;
1563 struct hlist_head *bucket;
1564 struct hlist_node *node;
1566 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1567 % MM_SLOTS_HASH_HEADS];
1568 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1569 if (mm == mm_slot->mm)
1575 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1576 struct mm_slot *mm_slot)
1578 struct hlist_head *bucket;
1580 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1581 % MM_SLOTS_HASH_HEADS];
1583 hlist_add_head(&mm_slot->hash, bucket);
1586 static inline int khugepaged_test_exit(struct mm_struct *mm)
1588 return atomic_read(&mm->mm_users) == 0;
1591 int __khugepaged_enter(struct mm_struct *mm)
1593 struct mm_slot *mm_slot;
1596 mm_slot = alloc_mm_slot();
1600 /* __khugepaged_exit() must not run from under us */
1601 VM_BUG_ON(khugepaged_test_exit(mm));
1602 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1603 free_mm_slot(mm_slot);
1607 spin_lock(&khugepaged_mm_lock);
1608 insert_to_mm_slots_hash(mm, mm_slot);
1610 * Insert just behind the scanning cursor, to let the area settle
1613 wakeup = list_empty(&khugepaged_scan.mm_head);
1614 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1615 spin_unlock(&khugepaged_mm_lock);
1617 atomic_inc(&mm->mm_count);
1619 wake_up_interruptible(&khugepaged_wait);
1624 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1626 unsigned long hstart, hend;
1629 * Not yet faulted in so we will register later in the
1630 * page fault if needed.
1634 /* khugepaged not yet working on file or special mappings */
1636 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1637 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1638 hend = vma->vm_end & HPAGE_PMD_MASK;
1640 return khugepaged_enter(vma);
1644 void __khugepaged_exit(struct mm_struct *mm)
1646 struct mm_slot *mm_slot;
1649 spin_lock(&khugepaged_mm_lock);
1650 mm_slot = get_mm_slot(mm);
1651 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1652 hlist_del(&mm_slot->hash);
1653 list_del(&mm_slot->mm_node);
1656 spin_unlock(&khugepaged_mm_lock);
1659 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1660 free_mm_slot(mm_slot);
1662 } else if (mm_slot) {
1664 * This is required to serialize against
1665 * khugepaged_test_exit() (which is guaranteed to run
1666 * under mmap sem read mode). Stop here (after we
1667 * return all pagetables will be destroyed) until
1668 * khugepaged has finished working on the pagetables
1669 * under the mmap_sem.
1671 down_write(&mm->mmap_sem);
1672 up_write(&mm->mmap_sem);
1676 static void release_pte_page(struct page *page)
1678 /* 0 stands for page_is_file_cache(page) == false */
1679 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1681 putback_lru_page(page);
1684 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1686 while (--_pte >= pte) {
1687 pte_t pteval = *_pte;
1688 if (!pte_none(pteval))
1689 release_pte_page(pte_page(pteval));
1693 static void release_all_pte_pages(pte_t *pte)
1695 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1698 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1699 unsigned long address,
1704 int referenced = 0, isolated = 0, none = 0;
1705 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1706 _pte++, address += PAGE_SIZE) {
1707 pte_t pteval = *_pte;
1708 if (pte_none(pteval)) {
1709 if (++none <= khugepaged_max_ptes_none)
1712 release_pte_pages(pte, _pte);
1716 if (!pte_present(pteval) || !pte_write(pteval)) {
1717 release_pte_pages(pte, _pte);
1720 page = vm_normal_page(vma, address, pteval);
1721 if (unlikely(!page)) {
1722 release_pte_pages(pte, _pte);
1725 VM_BUG_ON(PageCompound(page));
1726 BUG_ON(!PageAnon(page));
1727 VM_BUG_ON(!PageSwapBacked(page));
1729 /* cannot use mapcount: can't collapse if there's a gup pin */
1730 if (page_count(page) != 1) {
1731 release_pte_pages(pte, _pte);
1735 * We can do it before isolate_lru_page because the
1736 * page can't be freed from under us. NOTE: PG_lock
1737 * is needed to serialize against split_huge_page
1738 * when invoked from the VM.
1740 if (!trylock_page(page)) {
1741 release_pte_pages(pte, _pte);
1745 * Isolate the page to avoid collapsing an hugepage
1746 * currently in use by the VM.
1748 if (isolate_lru_page(page)) {
1750 release_pte_pages(pte, _pte);
1753 /* 0 stands for page_is_file_cache(page) == false */
1754 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1755 VM_BUG_ON(!PageLocked(page));
1756 VM_BUG_ON(PageLRU(page));
1758 /* If there is no mapped pte young don't collapse the page */
1759 if (pte_young(pteval) || PageReferenced(page) ||
1760 mmu_notifier_test_young(vma->vm_mm, address))
1763 if (unlikely(!referenced))
1764 release_all_pte_pages(pte);
1771 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1772 struct vm_area_struct *vma,
1773 unsigned long address,
1777 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1778 pte_t pteval = *_pte;
1779 struct page *src_page;
1781 if (pte_none(pteval)) {
1782 clear_user_highpage(page, address);
1783 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1785 src_page = pte_page(pteval);
1786 copy_user_highpage(page, src_page, address, vma);
1787 VM_BUG_ON(page_mapcount(src_page) != 1);
1788 VM_BUG_ON(page_count(src_page) != 2);
1789 release_pte_page(src_page);
1791 * ptl mostly unnecessary, but preempt has to
1792 * be disabled to update the per-cpu stats
1793 * inside page_remove_rmap().
1797 * paravirt calls inside pte_clear here are
1800 pte_clear(vma->vm_mm, address, _pte);
1801 page_remove_rmap(src_page);
1803 free_page_and_swap_cache(src_page);
1806 address += PAGE_SIZE;
1811 static void khugepaged_alloc_sleep(void)
1813 wait_event_freezable_timeout(khugepaged_wait, false,
1814 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
1818 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1820 if (IS_ERR(*hpage)) {
1826 khugepaged_alloc_sleep();
1827 } else if (*hpage) {
1836 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1837 struct vm_area_struct *vma, unsigned long address,
1842 * Allocate the page while the vma is still valid and under
1843 * the mmap_sem read mode so there is no memory allocation
1844 * later when we take the mmap_sem in write mode. This is more
1845 * friendly behavior (OTOH it may actually hide bugs) to
1846 * filesystems in userland with daemons allocating memory in
1847 * the userland I/O paths. Allocating memory with the
1848 * mmap_sem in read mode is good idea also to allow greater
1851 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1852 node, __GFP_OTHER_NODE);
1855 * After allocating the hugepage, release the mmap_sem read lock in
1856 * preparation for taking it in write mode.
1858 up_read(&mm->mmap_sem);
1859 if (unlikely(!*hpage)) {
1860 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1861 *hpage = ERR_PTR(-ENOMEM);
1865 count_vm_event(THP_COLLAPSE_ALLOC);
1869 static struct page *khugepaged_alloc_hugepage(bool *wait)
1874 hpage = alloc_hugepage(khugepaged_defrag());
1876 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1881 khugepaged_alloc_sleep();
1883 count_vm_event(THP_COLLAPSE_ALLOC);
1884 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
1889 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1892 *hpage = khugepaged_alloc_hugepage(wait);
1894 if (unlikely(!*hpage))
1901 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1902 struct vm_area_struct *vma, unsigned long address,
1905 up_read(&mm->mmap_sem);
1911 static void collapse_huge_page(struct mm_struct *mm,
1912 unsigned long address,
1913 struct page **hpage,
1914 struct vm_area_struct *vma,
1922 struct page *new_page;
1925 unsigned long hstart, hend;
1926 unsigned long mmun_start; /* For mmu_notifiers */
1927 unsigned long mmun_end; /* For mmu_notifiers */
1929 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1931 /* release the mmap_sem read lock. */
1932 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
1936 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
1940 * Prevent all access to pagetables with the exception of
1941 * gup_fast later hanlded by the ptep_clear_flush and the VM
1942 * handled by the anon_vma lock + PG_lock.
1944 down_write(&mm->mmap_sem);
1945 if (unlikely(khugepaged_test_exit(mm)))
1948 vma = find_vma(mm, address);
1949 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1950 hend = vma->vm_end & HPAGE_PMD_MASK;
1951 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1954 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1955 (vma->vm_flags & VM_NOHUGEPAGE))
1958 if (!vma->anon_vma || vma->vm_ops)
1960 if (is_vma_temporary_stack(vma))
1962 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1964 pgd = pgd_offset(mm, address);
1965 if (!pgd_present(*pgd))
1968 pud = pud_offset(pgd, address);
1969 if (!pud_present(*pud))
1972 pmd = pmd_offset(pud, address);
1973 /* pmd can't go away or become huge under us */
1974 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1977 anon_vma_lock(vma->anon_vma);
1979 pte = pte_offset_map(pmd, address);
1980 ptl = pte_lockptr(mm, pmd);
1982 mmun_start = address;
1983 mmun_end = address + HPAGE_PMD_SIZE;
1984 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1985 spin_lock(&mm->page_table_lock); /* probably unnecessary */
1987 * After this gup_fast can't run anymore. This also removes
1988 * any huge TLB entry from the CPU so we won't allow
1989 * huge and small TLB entries for the same virtual address
1990 * to avoid the risk of CPU bugs in that area.
1992 _pmd = pmdp_clear_flush(vma, address, pmd);
1993 spin_unlock(&mm->page_table_lock);
1994 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1997 isolated = __collapse_huge_page_isolate(vma, address, pte);
2000 if (unlikely(!isolated)) {
2002 spin_lock(&mm->page_table_lock);
2003 BUG_ON(!pmd_none(*pmd));
2004 set_pmd_at(mm, address, pmd, _pmd);
2005 spin_unlock(&mm->page_table_lock);
2006 anon_vma_unlock(vma->anon_vma);
2011 * All pages are isolated and locked so anon_vma rmap
2012 * can't run anymore.
2014 anon_vma_unlock(vma->anon_vma);
2016 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2018 __SetPageUptodate(new_page);
2019 pgtable = pmd_pgtable(_pmd);
2021 _pmd = mk_pmd(new_page, vma->vm_page_prot);
2022 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
2023 _pmd = pmd_mkhuge(_pmd);
2026 * spin_lock() below is not the equivalent of smp_wmb(), so
2027 * this is needed to avoid the copy_huge_page writes to become
2028 * visible after the set_pmd_at() write.
2032 spin_lock(&mm->page_table_lock);
2033 BUG_ON(!pmd_none(*pmd));
2034 page_add_new_anon_rmap(new_page, vma, address);
2035 set_pmd_at(mm, address, pmd, _pmd);
2036 update_mmu_cache(vma, address, _pmd);
2037 pgtable_trans_huge_deposit(mm, pgtable);
2038 spin_unlock(&mm->page_table_lock);
2042 khugepaged_pages_collapsed++;
2044 up_write(&mm->mmap_sem);
2048 mem_cgroup_uncharge_page(new_page);
2052 static int khugepaged_scan_pmd(struct mm_struct *mm,
2053 struct vm_area_struct *vma,
2054 unsigned long address,
2055 struct page **hpage)
2061 int ret = 0, referenced = 0, none = 0;
2063 unsigned long _address;
2067 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2069 pgd = pgd_offset(mm, address);
2070 if (!pgd_present(*pgd))
2073 pud = pud_offset(pgd, address);
2074 if (!pud_present(*pud))
2077 pmd = pmd_offset(pud, address);
2078 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2081 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2082 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2083 _pte++, _address += PAGE_SIZE) {
2084 pte_t pteval = *_pte;
2085 if (pte_none(pteval)) {
2086 if (++none <= khugepaged_max_ptes_none)
2091 if (!pte_present(pteval) || !pte_write(pteval))
2093 page = vm_normal_page(vma, _address, pteval);
2094 if (unlikely(!page))
2097 * Chose the node of the first page. This could
2098 * be more sophisticated and look at more pages,
2099 * but isn't for now.
2102 node = page_to_nid(page);
2103 VM_BUG_ON(PageCompound(page));
2104 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2106 /* cannot use mapcount: can't collapse if there's a gup pin */
2107 if (page_count(page) != 1)
2109 if (pte_young(pteval) || PageReferenced(page) ||
2110 mmu_notifier_test_young(vma->vm_mm, address))
2116 pte_unmap_unlock(pte, ptl);
2118 /* collapse_huge_page will return with the mmap_sem released */
2119 collapse_huge_page(mm, address, hpage, vma, node);
2124 static void collect_mm_slot(struct mm_slot *mm_slot)
2126 struct mm_struct *mm = mm_slot->mm;
2128 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2130 if (khugepaged_test_exit(mm)) {
2132 hlist_del(&mm_slot->hash);
2133 list_del(&mm_slot->mm_node);
2136 * Not strictly needed because the mm exited already.
2138 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2141 /* khugepaged_mm_lock actually not necessary for the below */
2142 free_mm_slot(mm_slot);
2147 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2148 struct page **hpage)
2149 __releases(&khugepaged_mm_lock)
2150 __acquires(&khugepaged_mm_lock)
2152 struct mm_slot *mm_slot;
2153 struct mm_struct *mm;
2154 struct vm_area_struct *vma;
2158 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2160 if (khugepaged_scan.mm_slot)
2161 mm_slot = khugepaged_scan.mm_slot;
2163 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2164 struct mm_slot, mm_node);
2165 khugepaged_scan.address = 0;
2166 khugepaged_scan.mm_slot = mm_slot;
2168 spin_unlock(&khugepaged_mm_lock);
2171 down_read(&mm->mmap_sem);
2172 if (unlikely(khugepaged_test_exit(mm)))
2175 vma = find_vma(mm, khugepaged_scan.address);
2178 for (; vma; vma = vma->vm_next) {
2179 unsigned long hstart, hend;
2182 if (unlikely(khugepaged_test_exit(mm))) {
2187 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2188 !khugepaged_always()) ||
2189 (vma->vm_flags & VM_NOHUGEPAGE)) {
2194 if (!vma->anon_vma || vma->vm_ops)
2196 if (is_vma_temporary_stack(vma))
2198 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2200 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2201 hend = vma->vm_end & HPAGE_PMD_MASK;
2204 if (khugepaged_scan.address > hend)
2206 if (khugepaged_scan.address < hstart)
2207 khugepaged_scan.address = hstart;
2208 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2210 while (khugepaged_scan.address < hend) {
2213 if (unlikely(khugepaged_test_exit(mm)))
2214 goto breakouterloop;
2216 VM_BUG_ON(khugepaged_scan.address < hstart ||
2217 khugepaged_scan.address + HPAGE_PMD_SIZE >
2219 ret = khugepaged_scan_pmd(mm, vma,
2220 khugepaged_scan.address,
2222 /* move to next address */
2223 khugepaged_scan.address += HPAGE_PMD_SIZE;
2224 progress += HPAGE_PMD_NR;
2226 /* we released mmap_sem so break loop */
2227 goto breakouterloop_mmap_sem;
2228 if (progress >= pages)
2229 goto breakouterloop;
2233 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2234 breakouterloop_mmap_sem:
2236 spin_lock(&khugepaged_mm_lock);
2237 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2239 * Release the current mm_slot if this mm is about to die, or
2240 * if we scanned all vmas of this mm.
2242 if (khugepaged_test_exit(mm) || !vma) {
2244 * Make sure that if mm_users is reaching zero while
2245 * khugepaged runs here, khugepaged_exit will find
2246 * mm_slot not pointing to the exiting mm.
2248 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2249 khugepaged_scan.mm_slot = list_entry(
2250 mm_slot->mm_node.next,
2251 struct mm_slot, mm_node);
2252 khugepaged_scan.address = 0;
2254 khugepaged_scan.mm_slot = NULL;
2255 khugepaged_full_scans++;
2258 collect_mm_slot(mm_slot);
2264 static int khugepaged_has_work(void)
2266 return !list_empty(&khugepaged_scan.mm_head) &&
2267 khugepaged_enabled();
2270 static int khugepaged_wait_event(void)
2272 return !list_empty(&khugepaged_scan.mm_head) ||
2273 kthread_should_stop();
2276 static void khugepaged_do_scan(void)
2278 struct page *hpage = NULL;
2279 unsigned int progress = 0, pass_through_head = 0;
2280 unsigned int pages = khugepaged_pages_to_scan;
2283 barrier(); /* write khugepaged_pages_to_scan to local stack */
2285 while (progress < pages) {
2286 if (!khugepaged_prealloc_page(&hpage, &wait))
2291 if (unlikely(kthread_should_stop() || freezing(current)))
2294 spin_lock(&khugepaged_mm_lock);
2295 if (!khugepaged_scan.mm_slot)
2296 pass_through_head++;
2297 if (khugepaged_has_work() &&
2298 pass_through_head < 2)
2299 progress += khugepaged_scan_mm_slot(pages - progress,
2303 spin_unlock(&khugepaged_mm_lock);
2306 if (!IS_ERR_OR_NULL(hpage))
2310 static void khugepaged_wait_work(void)
2314 if (khugepaged_has_work()) {
2315 if (!khugepaged_scan_sleep_millisecs)
2318 wait_event_freezable_timeout(khugepaged_wait,
2319 kthread_should_stop(),
2320 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2324 if (khugepaged_enabled())
2325 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2328 static int khugepaged(void *none)
2330 struct mm_slot *mm_slot;
2333 set_user_nice(current, 19);
2335 while (!kthread_should_stop()) {
2336 khugepaged_do_scan();
2337 khugepaged_wait_work();
2340 spin_lock(&khugepaged_mm_lock);
2341 mm_slot = khugepaged_scan.mm_slot;
2342 khugepaged_scan.mm_slot = NULL;
2344 collect_mm_slot(mm_slot);
2345 spin_unlock(&khugepaged_mm_lock);
2349 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2353 spin_lock(&mm->page_table_lock);
2354 if (unlikely(!pmd_trans_huge(*pmd))) {
2355 spin_unlock(&mm->page_table_lock);
2358 page = pmd_page(*pmd);
2359 VM_BUG_ON(!page_count(page));
2361 spin_unlock(&mm->page_table_lock);
2363 split_huge_page(page);
2366 BUG_ON(pmd_trans_huge(*pmd));
2369 static void split_huge_page_address(struct mm_struct *mm,
2370 unsigned long address)
2376 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2378 pgd = pgd_offset(mm, address);
2379 if (!pgd_present(*pgd))
2382 pud = pud_offset(pgd, address);
2383 if (!pud_present(*pud))
2386 pmd = pmd_offset(pud, address);
2387 if (!pmd_present(*pmd))
2390 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2391 * materialize from under us.
2393 split_huge_page_pmd(mm, pmd);
2396 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2397 unsigned long start,
2402 * If the new start address isn't hpage aligned and it could
2403 * previously contain an hugepage: check if we need to split
2406 if (start & ~HPAGE_PMD_MASK &&
2407 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2408 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2409 split_huge_page_address(vma->vm_mm, start);
2412 * If the new end address isn't hpage aligned and it could
2413 * previously contain an hugepage: check if we need to split
2416 if (end & ~HPAGE_PMD_MASK &&
2417 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2418 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2419 split_huge_page_address(vma->vm_mm, end);
2422 * If we're also updating the vma->vm_next->vm_start, if the new
2423 * vm_next->vm_start isn't page aligned and it could previously
2424 * contain an hugepage: check if we need to split an huge pmd.
2426 if (adjust_next > 0) {
2427 struct vm_area_struct *next = vma->vm_next;
2428 unsigned long nstart = next->vm_start;
2429 nstart += adjust_next << PAGE_SHIFT;
2430 if (nstart & ~HPAGE_PMD_MASK &&
2431 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2432 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2433 split_huge_page_address(next->vm_mm, nstart);