2 * Copyright (C) 2009 Red Hat, Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
9 #include <linux/sched.h>
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/mm_inline.h>
16 #include <linux/kthread.h>
17 #include <linux/khugepaged.h>
18 #include <linux/freezer.h>
19 #include <linux/mman.h>
20 #include <linux/pagemap.h>
21 #include <linux/migrate.h>
23 #include <asm/pgalloc.h>
27 * By default transparent hugepage support is enabled for all mappings
28 * and khugepaged scans all mappings. Defrag is only invoked by
29 * khugepaged hugepage allocations and by page faults inside
30 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
33 unsigned long transparent_hugepage_flags __read_mostly =
34 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
35 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
37 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
38 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
40 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
41 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
43 /* default scan 8*512 pte (or vmas) every 30 second */
44 static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
45 static unsigned int khugepaged_pages_collapsed;
46 static unsigned int khugepaged_full_scans;
47 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
48 /* during fragmentation poll the hugepage allocator once every minute */
49 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
50 static struct task_struct *khugepaged_thread __read_mostly;
51 static DEFINE_MUTEX(khugepaged_mutex);
52 static DEFINE_SPINLOCK(khugepaged_mm_lock);
53 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
55 * default collapse hugepages if there is at least one pte mapped like
56 * it would have happened if the vma was large enough during page
59 static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
61 static int khugepaged(void *none);
62 static int mm_slots_hash_init(void);
63 static int khugepaged_slab_init(void);
64 static void khugepaged_slab_free(void);
66 #define MM_SLOTS_HASH_HEADS 1024
67 static struct hlist_head *mm_slots_hash __read_mostly;
68 static struct kmem_cache *mm_slot_cache __read_mostly;
71 * struct mm_slot - hash lookup from mm to mm_slot
72 * @hash: hash collision list
73 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
74 * @mm: the mm that this information is valid for
77 struct hlist_node hash;
78 struct list_head mm_node;
83 * struct khugepaged_scan - cursor for scanning
84 * @mm_head: the head of the mm list to scan
85 * @mm_slot: the current mm_slot we are scanning
86 * @address: the next address inside that to be scanned
88 * There is only the one khugepaged_scan instance of this cursor structure.
90 struct khugepaged_scan {
91 struct list_head mm_head;
92 struct mm_slot *mm_slot;
93 unsigned long address;
95 static struct khugepaged_scan khugepaged_scan = {
96 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
100 static int set_recommended_min_free_kbytes(void)
104 unsigned long recommended_min;
105 extern int min_free_kbytes;
107 if (!khugepaged_enabled())
110 for_each_populated_zone(zone)
113 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
114 recommended_min = pageblock_nr_pages * nr_zones * 2;
117 * Make sure that on average at least two pageblocks are almost free
118 * of another type, one for a migratetype to fall back to and a
119 * second to avoid subsequent fallbacks of other types There are 3
120 * MIGRATE_TYPES we care about.
122 recommended_min += pageblock_nr_pages * nr_zones *
123 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
125 /* don't ever allow to reserve more than 5% of the lowmem */
126 recommended_min = min(recommended_min,
127 (unsigned long) nr_free_buffer_pages() / 20);
128 recommended_min <<= (PAGE_SHIFT-10);
130 if (recommended_min > min_free_kbytes)
131 min_free_kbytes = recommended_min;
132 setup_per_zone_wmarks();
135 late_initcall(set_recommended_min_free_kbytes);
137 static int start_khugepaged(void)
140 if (khugepaged_enabled()) {
141 if (!khugepaged_thread)
142 khugepaged_thread = kthread_run(khugepaged, NULL,
144 if (unlikely(IS_ERR(khugepaged_thread))) {
146 "khugepaged: kthread_run(khugepaged) failed\n");
147 err = PTR_ERR(khugepaged_thread);
148 khugepaged_thread = NULL;
151 if (!list_empty(&khugepaged_scan.mm_head))
152 wake_up_interruptible(&khugepaged_wait);
154 set_recommended_min_free_kbytes();
155 } else if (khugepaged_thread) {
156 kthread_stop(khugepaged_thread);
157 khugepaged_thread = NULL;
165 static ssize_t double_flag_show(struct kobject *kobj,
166 struct kobj_attribute *attr, char *buf,
167 enum transparent_hugepage_flag enabled,
168 enum transparent_hugepage_flag req_madv)
170 if (test_bit(enabled, &transparent_hugepage_flags)) {
171 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
172 return sprintf(buf, "[always] madvise never\n");
173 } else if (test_bit(req_madv, &transparent_hugepage_flags))
174 return sprintf(buf, "always [madvise] never\n");
176 return sprintf(buf, "always madvise [never]\n");
178 static ssize_t double_flag_store(struct kobject *kobj,
179 struct kobj_attribute *attr,
180 const char *buf, size_t count,
181 enum transparent_hugepage_flag enabled,
182 enum transparent_hugepage_flag req_madv)
184 if (!memcmp("always", buf,
185 min(sizeof("always")-1, count))) {
186 set_bit(enabled, &transparent_hugepage_flags);
187 clear_bit(req_madv, &transparent_hugepage_flags);
188 } else if (!memcmp("madvise", buf,
189 min(sizeof("madvise")-1, count))) {
190 clear_bit(enabled, &transparent_hugepage_flags);
191 set_bit(req_madv, &transparent_hugepage_flags);
192 } else if (!memcmp("never", buf,
193 min(sizeof("never")-1, count))) {
194 clear_bit(enabled, &transparent_hugepage_flags);
195 clear_bit(req_madv, &transparent_hugepage_flags);
202 static ssize_t enabled_show(struct kobject *kobj,
203 struct kobj_attribute *attr, char *buf)
205 return double_flag_show(kobj, attr, buf,
206 TRANSPARENT_HUGEPAGE_FLAG,
207 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
209 static ssize_t enabled_store(struct kobject *kobj,
210 struct kobj_attribute *attr,
211 const char *buf, size_t count)
215 ret = double_flag_store(kobj, attr, buf, count,
216 TRANSPARENT_HUGEPAGE_FLAG,
217 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
222 mutex_lock(&khugepaged_mutex);
223 err = start_khugepaged();
224 mutex_unlock(&khugepaged_mutex);
232 static struct kobj_attribute enabled_attr =
233 __ATTR(enabled, 0644, enabled_show, enabled_store);
235 static ssize_t single_flag_show(struct kobject *kobj,
236 struct kobj_attribute *attr, char *buf,
237 enum transparent_hugepage_flag flag)
239 return sprintf(buf, "%d\n",
240 !!test_bit(flag, &transparent_hugepage_flags));
243 static ssize_t single_flag_store(struct kobject *kobj,
244 struct kobj_attribute *attr,
245 const char *buf, size_t count,
246 enum transparent_hugepage_flag flag)
251 ret = kstrtoul(buf, 10, &value);
258 set_bit(flag, &transparent_hugepage_flags);
260 clear_bit(flag, &transparent_hugepage_flags);
266 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
267 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
268 * memory just to allocate one more hugepage.
270 static ssize_t defrag_show(struct kobject *kobj,
271 struct kobj_attribute *attr, char *buf)
273 return double_flag_show(kobj, attr, buf,
274 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
275 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
277 static ssize_t defrag_store(struct kobject *kobj,
278 struct kobj_attribute *attr,
279 const char *buf, size_t count)
281 return double_flag_store(kobj, attr, buf, count,
282 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
283 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
285 static struct kobj_attribute defrag_attr =
286 __ATTR(defrag, 0644, defrag_show, defrag_store);
288 #ifdef CONFIG_DEBUG_VM
289 static ssize_t debug_cow_show(struct kobject *kobj,
290 struct kobj_attribute *attr, char *buf)
292 return single_flag_show(kobj, attr, buf,
293 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
295 static ssize_t debug_cow_store(struct kobject *kobj,
296 struct kobj_attribute *attr,
297 const char *buf, size_t count)
299 return single_flag_store(kobj, attr, buf, count,
300 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
302 static struct kobj_attribute debug_cow_attr =
303 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
304 #endif /* CONFIG_DEBUG_VM */
306 static struct attribute *hugepage_attr[] = {
309 #ifdef CONFIG_DEBUG_VM
310 &debug_cow_attr.attr,
315 static struct attribute_group hugepage_attr_group = {
316 .attrs = hugepage_attr,
319 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
320 struct kobj_attribute *attr,
323 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
326 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
327 struct kobj_attribute *attr,
328 const char *buf, size_t count)
333 err = strict_strtoul(buf, 10, &msecs);
334 if (err || msecs > UINT_MAX)
337 khugepaged_scan_sleep_millisecs = msecs;
338 wake_up_interruptible(&khugepaged_wait);
342 static struct kobj_attribute scan_sleep_millisecs_attr =
343 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
344 scan_sleep_millisecs_store);
346 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
347 struct kobj_attribute *attr,
350 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
353 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
354 struct kobj_attribute *attr,
355 const char *buf, size_t count)
360 err = strict_strtoul(buf, 10, &msecs);
361 if (err || msecs > UINT_MAX)
364 khugepaged_alloc_sleep_millisecs = msecs;
365 wake_up_interruptible(&khugepaged_wait);
369 static struct kobj_attribute alloc_sleep_millisecs_attr =
370 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
371 alloc_sleep_millisecs_store);
373 static ssize_t pages_to_scan_show(struct kobject *kobj,
374 struct kobj_attribute *attr,
377 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
379 static ssize_t pages_to_scan_store(struct kobject *kobj,
380 struct kobj_attribute *attr,
381 const char *buf, size_t count)
386 err = strict_strtoul(buf, 10, &pages);
387 if (err || !pages || pages > UINT_MAX)
390 khugepaged_pages_to_scan = pages;
394 static struct kobj_attribute pages_to_scan_attr =
395 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
396 pages_to_scan_store);
398 static ssize_t pages_collapsed_show(struct kobject *kobj,
399 struct kobj_attribute *attr,
402 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
404 static struct kobj_attribute pages_collapsed_attr =
405 __ATTR_RO(pages_collapsed);
407 static ssize_t full_scans_show(struct kobject *kobj,
408 struct kobj_attribute *attr,
411 return sprintf(buf, "%u\n", khugepaged_full_scans);
413 static struct kobj_attribute full_scans_attr =
414 __ATTR_RO(full_scans);
416 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
417 struct kobj_attribute *attr, char *buf)
419 return single_flag_show(kobj, attr, buf,
420 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
422 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
423 struct kobj_attribute *attr,
424 const char *buf, size_t count)
426 return single_flag_store(kobj, attr, buf, count,
427 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
429 static struct kobj_attribute khugepaged_defrag_attr =
430 __ATTR(defrag, 0644, khugepaged_defrag_show,
431 khugepaged_defrag_store);
434 * max_ptes_none controls if khugepaged should collapse hugepages over
435 * any unmapped ptes in turn potentially increasing the memory
436 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
437 * reduce the available free memory in the system as it
438 * runs. Increasing max_ptes_none will instead potentially reduce the
439 * free memory in the system during the khugepaged scan.
441 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
442 struct kobj_attribute *attr,
445 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
447 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
448 struct kobj_attribute *attr,
449 const char *buf, size_t count)
452 unsigned long max_ptes_none;
454 err = strict_strtoul(buf, 10, &max_ptes_none);
455 if (err || max_ptes_none > HPAGE_PMD_NR-1)
458 khugepaged_max_ptes_none = max_ptes_none;
462 static struct kobj_attribute khugepaged_max_ptes_none_attr =
463 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
464 khugepaged_max_ptes_none_store);
466 static struct attribute *khugepaged_attr[] = {
467 &khugepaged_defrag_attr.attr,
468 &khugepaged_max_ptes_none_attr.attr,
469 &pages_to_scan_attr.attr,
470 &pages_collapsed_attr.attr,
471 &full_scans_attr.attr,
472 &scan_sleep_millisecs_attr.attr,
473 &alloc_sleep_millisecs_attr.attr,
477 static struct attribute_group khugepaged_attr_group = {
478 .attrs = khugepaged_attr,
479 .name = "khugepaged",
482 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
486 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
487 if (unlikely(!*hugepage_kobj)) {
488 printk(KERN_ERR "hugepage: failed kobject create\n");
492 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
494 printk(KERN_ERR "hugepage: failed register hugeage group\n");
498 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
500 printk(KERN_ERR "hugepage: failed register hugeage group\n");
501 goto remove_hp_group;
507 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
509 kobject_put(*hugepage_kobj);
513 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
515 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
516 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
517 kobject_put(hugepage_kobj);
520 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
525 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
528 #endif /* CONFIG_SYSFS */
530 static int __init hugepage_init(void)
533 struct kobject *hugepage_kobj;
535 if (!has_transparent_hugepage()) {
536 transparent_hugepage_flags = 0;
540 err = hugepage_init_sysfs(&hugepage_kobj);
544 err = khugepaged_slab_init();
548 err = mm_slots_hash_init();
550 khugepaged_slab_free();
555 * By default disable transparent hugepages on smaller systems,
556 * where the extra memory used could hurt more than TLB overhead
557 * is likely to save. The admin can still enable it through /sys.
559 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
560 transparent_hugepage_flags = 0;
566 hugepage_exit_sysfs(hugepage_kobj);
569 module_init(hugepage_init)
571 static int __init setup_transparent_hugepage(char *str)
576 if (!strcmp(str, "always")) {
577 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
578 &transparent_hugepage_flags);
579 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
580 &transparent_hugepage_flags);
582 } else if (!strcmp(str, "madvise")) {
583 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
584 &transparent_hugepage_flags);
585 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
586 &transparent_hugepage_flags);
588 } else if (!strcmp(str, "never")) {
589 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
590 &transparent_hugepage_flags);
591 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
592 &transparent_hugepage_flags);
598 "transparent_hugepage= cannot parse, ignored\n");
601 __setup("transparent_hugepage=", setup_transparent_hugepage);
603 static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
605 if (likely(vma->vm_flags & VM_WRITE))
606 pmd = pmd_mkwrite(pmd);
610 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
611 struct vm_area_struct *vma,
612 unsigned long haddr, pmd_t *pmd,
617 VM_BUG_ON(!PageCompound(page));
618 pgtable = pte_alloc_one(mm, haddr);
619 if (unlikely(!pgtable))
622 clear_huge_page(page, haddr, HPAGE_PMD_NR);
623 __SetPageUptodate(page);
625 spin_lock(&mm->page_table_lock);
626 if (unlikely(!pmd_none(*pmd))) {
627 spin_unlock(&mm->page_table_lock);
628 mem_cgroup_uncharge_page(page);
630 pte_free(mm, pgtable);
633 entry = mk_pmd(page, vma->vm_page_prot);
634 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
635 entry = pmd_mkhuge(entry);
637 * The spinlocking to take the lru_lock inside
638 * page_add_new_anon_rmap() acts as a full memory
639 * barrier to be sure clear_huge_page writes become
640 * visible after the set_pmd_at() write.
642 page_add_new_anon_rmap(page, vma, haddr);
643 set_pmd_at(mm, haddr, pmd, entry);
644 pgtable_trans_huge_deposit(mm, pgtable);
645 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
647 spin_unlock(&mm->page_table_lock);
653 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
655 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
658 static inline struct page *alloc_hugepage_vma(int defrag,
659 struct vm_area_struct *vma,
660 unsigned long haddr, int nd,
663 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
664 HPAGE_PMD_ORDER, vma, haddr, nd);
668 static inline struct page *alloc_hugepage(int defrag)
670 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
675 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
676 unsigned long address, pmd_t *pmd,
680 unsigned long haddr = address & HPAGE_PMD_MASK;
683 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
684 if (unlikely(anon_vma_prepare(vma)))
686 if (unlikely(khugepaged_enter(vma)))
688 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
689 vma, haddr, numa_node_id(), 0);
690 if (unlikely(!page)) {
691 count_vm_event(THP_FAULT_FALLBACK);
694 count_vm_event(THP_FAULT_ALLOC);
695 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
699 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
701 mem_cgroup_uncharge_page(page);
710 * Use __pte_alloc instead of pte_alloc_map, because we can't
711 * run pte_offset_map on the pmd, if an huge pmd could
712 * materialize from under us from a different thread.
714 if (unlikely(pmd_none(*pmd)) &&
715 unlikely(__pte_alloc(mm, vma, pmd, address)))
717 /* if an huge pmd materialized from under us just retry later */
718 if (unlikely(pmd_trans_huge(*pmd)))
721 * A regular pmd is established and it can't morph into a huge pmd
722 * from under us anymore at this point because we hold the mmap_sem
723 * read mode and khugepaged takes it in write mode. So now it's
724 * safe to run pte_offset_map().
726 pte = pte_offset_map(pmd, address);
727 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
730 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
731 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
732 struct vm_area_struct *vma)
734 struct page *src_page;
740 pgtable = pte_alloc_one(dst_mm, addr);
741 if (unlikely(!pgtable))
744 spin_lock(&dst_mm->page_table_lock);
745 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
749 if (unlikely(!pmd_trans_huge(pmd))) {
750 pte_free(dst_mm, pgtable);
753 if (unlikely(pmd_trans_splitting(pmd))) {
754 /* split huge page running from under us */
755 spin_unlock(&src_mm->page_table_lock);
756 spin_unlock(&dst_mm->page_table_lock);
757 pte_free(dst_mm, pgtable);
759 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
762 src_page = pmd_page(pmd);
763 VM_BUG_ON(!PageHead(src_page));
765 page_dup_rmap(src_page);
766 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
768 pmdp_set_wrprotect(src_mm, addr, src_pmd);
769 pmd = pmd_mkold(pmd_wrprotect(pmd));
770 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
771 pgtable_trans_huge_deposit(dst_mm, pgtable);
776 spin_unlock(&src_mm->page_table_lock);
777 spin_unlock(&dst_mm->page_table_lock);
782 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
783 struct vm_area_struct *vma,
784 unsigned long address,
785 pmd_t *pmd, pmd_t orig_pmd,
793 unsigned long mmun_start; /* For mmu_notifiers */
794 unsigned long mmun_end; /* For mmu_notifiers */
796 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
798 if (unlikely(!pages)) {
803 for (i = 0; i < HPAGE_PMD_NR; i++) {
804 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
806 vma, address, page_to_nid(page));
807 if (unlikely(!pages[i] ||
808 mem_cgroup_newpage_charge(pages[i], mm,
812 mem_cgroup_uncharge_start();
814 mem_cgroup_uncharge_page(pages[i]);
817 mem_cgroup_uncharge_end();
824 for (i = 0; i < HPAGE_PMD_NR; i++) {
825 copy_user_highpage(pages[i], page + i,
826 haddr + PAGE_SIZE * i, vma);
827 __SetPageUptodate(pages[i]);
832 mmun_end = haddr + HPAGE_PMD_SIZE;
833 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
835 spin_lock(&mm->page_table_lock);
836 if (unlikely(!pmd_same(*pmd, orig_pmd)))
838 VM_BUG_ON(!PageHead(page));
840 pmdp_clear_flush(vma, haddr, pmd);
841 /* leave pmd empty until pte is filled */
843 pgtable = pgtable_trans_huge_withdraw(mm);
844 pmd_populate(mm, &_pmd, pgtable);
846 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
848 entry = mk_pte(pages[i], vma->vm_page_prot);
849 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
850 page_add_new_anon_rmap(pages[i], vma, haddr);
851 pte = pte_offset_map(&_pmd, haddr);
852 VM_BUG_ON(!pte_none(*pte));
853 set_pte_at(mm, haddr, pte, entry);
858 smp_wmb(); /* make pte visible before pmd */
859 pmd_populate(mm, pmd, pgtable);
860 page_remove_rmap(page);
861 spin_unlock(&mm->page_table_lock);
863 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
865 ret |= VM_FAULT_WRITE;
872 spin_unlock(&mm->page_table_lock);
873 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
874 mem_cgroup_uncharge_start();
875 for (i = 0; i < HPAGE_PMD_NR; i++) {
876 mem_cgroup_uncharge_page(pages[i]);
879 mem_cgroup_uncharge_end();
884 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
885 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
888 struct page *page, *new_page;
890 unsigned long mmun_start; /* For mmu_notifiers */
891 unsigned long mmun_end; /* For mmu_notifiers */
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_pmd(vma, address, pmd);
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);
943 mmun_end = haddr + HPAGE_PMD_SIZE;
944 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
946 spin_lock(&mm->page_table_lock);
948 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
949 spin_unlock(&mm->page_table_lock);
950 mem_cgroup_uncharge_page(new_page);
955 VM_BUG_ON(!PageHead(page));
956 entry = mk_pmd(new_page, vma->vm_page_prot);
957 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
958 entry = pmd_mkhuge(entry);
959 pmdp_clear_flush(vma, haddr, pmd);
960 page_add_new_anon_rmap(new_page, vma, haddr);
961 set_pmd_at(mm, haddr, pmd, entry);
962 update_mmu_cache_pmd(vma, address, pmd);
963 page_remove_rmap(page);
965 ret |= VM_FAULT_WRITE;
967 spin_unlock(&mm->page_table_lock);
969 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
973 spin_unlock(&mm->page_table_lock);
977 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
982 struct mm_struct *mm = vma->vm_mm;
983 struct page *page = NULL;
985 assert_spin_locked(&mm->page_table_lock);
987 if (flags & FOLL_WRITE && !pmd_write(*pmd))
990 page = pmd_page(*pmd);
991 VM_BUG_ON(!PageHead(page));
992 if (flags & FOLL_TOUCH) {
995 * We should set the dirty bit only for FOLL_WRITE but
996 * for now the dirty bit in the pmd is meaningless.
997 * And if the dirty bit will become meaningful and
998 * we'll only set it with FOLL_WRITE, an atomic
999 * set_bit will be required on the pmd to set the
1000 * young bit, instead of the current set_pmd_at.
1002 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1003 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1005 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1006 if (page->mapping && trylock_page(page)) {
1009 mlock_vma_page(page);
1013 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1014 VM_BUG_ON(!PageCompound(page));
1015 if (flags & FOLL_GET)
1016 get_page_foll(page);
1022 /* NUMA hinting page fault entry point for trans huge pmds */
1023 int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1024 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
1026 struct page *page = NULL;
1027 unsigned long haddr = addr & HPAGE_PMD_MASK;
1029 int current_nid = -1;
1031 spin_lock(&mm->page_table_lock);
1032 if (unlikely(!pmd_same(pmd, *pmdp)))
1035 page = pmd_page(pmd);
1037 spin_unlock(&mm->page_table_lock);
1038 current_nid = page_to_nid(page);
1039 count_vm_numa_event(NUMA_HINT_FAULTS);
1040 if (current_nid == numa_node_id())
1041 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
1043 target_nid = mpol_misplaced(page, vma, haddr);
1044 if (target_nid == -1)
1048 * Due to lacking code to migrate thp pages, we'll split
1049 * (which preserves the special PROT_NONE) and re-take the
1050 * fault on the normal pages.
1052 split_huge_page(page);
1058 spin_lock(&mm->page_table_lock);
1059 if (unlikely(!pmd_same(pmd, *pmdp)))
1062 pmd = pmd_mknonnuma(pmd);
1063 set_pmd_at(mm, haddr, pmdp, pmd);
1064 VM_BUG_ON(pmd_numa(*pmdp));
1065 update_mmu_cache_pmd(vma, addr, pmdp);
1068 spin_unlock(&mm->page_table_lock);
1071 task_numa_fault(numa_node_id(), HPAGE_PMD_NR, false);
1076 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1077 pmd_t *pmd, unsigned long addr)
1081 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1085 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1086 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1087 page = pmd_page(orig_pmd);
1088 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1089 page_remove_rmap(page);
1090 VM_BUG_ON(page_mapcount(page) < 0);
1091 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1092 VM_BUG_ON(!PageHead(page));
1094 spin_unlock(&tlb->mm->page_table_lock);
1095 tlb_remove_page(tlb, page);
1096 pte_free(tlb->mm, pgtable);
1102 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1103 unsigned long addr, unsigned long end,
1108 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1110 * All logical pages in the range are present
1111 * if backed by a huge page.
1113 spin_unlock(&vma->vm_mm->page_table_lock);
1114 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1121 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1122 unsigned long old_addr,
1123 unsigned long new_addr, unsigned long old_end,
1124 pmd_t *old_pmd, pmd_t *new_pmd)
1129 struct mm_struct *mm = vma->vm_mm;
1131 if ((old_addr & ~HPAGE_PMD_MASK) ||
1132 (new_addr & ~HPAGE_PMD_MASK) ||
1133 old_end - old_addr < HPAGE_PMD_SIZE ||
1134 (new_vma->vm_flags & VM_NOHUGEPAGE))
1138 * The destination pmd shouldn't be established, free_pgtables()
1139 * should have release it.
1141 if (WARN_ON(!pmd_none(*new_pmd))) {
1142 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1146 ret = __pmd_trans_huge_lock(old_pmd, vma);
1148 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1149 VM_BUG_ON(!pmd_none(*new_pmd));
1150 set_pmd_at(mm, new_addr, new_pmd, pmd);
1151 spin_unlock(&mm->page_table_lock);
1157 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1158 unsigned long addr, pgprot_t newprot, int prot_numa)
1160 struct mm_struct *mm = vma->vm_mm;
1163 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1165 entry = pmdp_get_and_clear(mm, addr, pmd);
1167 entry = pmd_modify(entry, newprot);
1169 struct page *page = pmd_page(*pmd);
1171 /* only check non-shared pages */
1172 if (page_mapcount(page) == 1 &&
1174 entry = pmd_mknuma(entry);
1177 set_pmd_at(mm, addr, pmd, entry);
1178 spin_unlock(&vma->vm_mm->page_table_lock);
1186 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1187 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1189 * Note that if it returns 1, this routine returns without unlocking page
1190 * table locks. So callers must unlock them.
1192 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1194 spin_lock(&vma->vm_mm->page_table_lock);
1195 if (likely(pmd_trans_huge(*pmd))) {
1196 if (unlikely(pmd_trans_splitting(*pmd))) {
1197 spin_unlock(&vma->vm_mm->page_table_lock);
1198 wait_split_huge_page(vma->anon_vma, pmd);
1201 /* Thp mapped by 'pmd' is stable, so we can
1202 * handle it as it is. */
1206 spin_unlock(&vma->vm_mm->page_table_lock);
1210 pmd_t *page_check_address_pmd(struct page *page,
1211 struct mm_struct *mm,
1212 unsigned long address,
1213 enum page_check_address_pmd_flag flag)
1217 pmd_t *pmd, *ret = NULL;
1219 if (address & ~HPAGE_PMD_MASK)
1222 pgd = pgd_offset(mm, address);
1223 if (!pgd_present(*pgd))
1226 pud = pud_offset(pgd, address);
1227 if (!pud_present(*pud))
1230 pmd = pmd_offset(pud, address);
1233 if (pmd_page(*pmd) != page)
1236 * split_vma() may create temporary aliased mappings. There is
1237 * no risk as long as all huge pmd are found and have their
1238 * splitting bit set before __split_huge_page_refcount
1239 * runs. Finding the same huge pmd more than once during the
1240 * same rmap walk is not a problem.
1242 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1243 pmd_trans_splitting(*pmd))
1245 if (pmd_trans_huge(*pmd)) {
1246 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1247 !pmd_trans_splitting(*pmd));
1254 static int __split_huge_page_splitting(struct page *page,
1255 struct vm_area_struct *vma,
1256 unsigned long address)
1258 struct mm_struct *mm = vma->vm_mm;
1261 /* For mmu_notifiers */
1262 const unsigned long mmun_start = address;
1263 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1265 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1266 spin_lock(&mm->page_table_lock);
1267 pmd = page_check_address_pmd(page, mm, address,
1268 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1271 * We can't temporarily set the pmd to null in order
1272 * to split it, the pmd must remain marked huge at all
1273 * times or the VM won't take the pmd_trans_huge paths
1274 * and it won't wait on the anon_vma->root->mutex to
1275 * serialize against split_huge_page*.
1277 pmdp_splitting_flush(vma, address, pmd);
1280 spin_unlock(&mm->page_table_lock);
1281 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1286 static void __split_huge_page_refcount(struct page *page)
1289 struct zone *zone = page_zone(page);
1290 struct lruvec *lruvec;
1293 /* prevent PageLRU to go away from under us, and freeze lru stats */
1294 spin_lock_irq(&zone->lru_lock);
1295 lruvec = mem_cgroup_page_lruvec(page, zone);
1297 compound_lock(page);
1298 /* complete memcg works before add pages to LRU */
1299 mem_cgroup_split_huge_fixup(page);
1301 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1302 struct page *page_tail = page + i;
1304 /* tail_page->_mapcount cannot change */
1305 BUG_ON(page_mapcount(page_tail) < 0);
1306 tail_count += page_mapcount(page_tail);
1307 /* check for overflow */
1308 BUG_ON(tail_count < 0);
1309 BUG_ON(atomic_read(&page_tail->_count) != 0);
1311 * tail_page->_count is zero and not changing from
1312 * under us. But get_page_unless_zero() may be running
1313 * from under us on the tail_page. If we used
1314 * atomic_set() below instead of atomic_add(), we
1315 * would then run atomic_set() concurrently with
1316 * get_page_unless_zero(), and atomic_set() is
1317 * implemented in C not using locked ops. spin_unlock
1318 * on x86 sometime uses locked ops because of PPro
1319 * errata 66, 92, so unless somebody can guarantee
1320 * atomic_set() here would be safe on all archs (and
1321 * not only on x86), it's safer to use atomic_add().
1323 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1324 &page_tail->_count);
1326 /* after clearing PageTail the gup refcount can be released */
1330 * retain hwpoison flag of the poisoned tail page:
1331 * fix for the unsuitable process killed on Guest Machine(KVM)
1332 * by the memory-failure.
1334 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1335 page_tail->flags |= (page->flags &
1336 ((1L << PG_referenced) |
1337 (1L << PG_swapbacked) |
1338 (1L << PG_mlocked) |
1339 (1L << PG_uptodate)));
1340 page_tail->flags |= (1L << PG_dirty);
1342 /* clear PageTail before overwriting first_page */
1346 * __split_huge_page_splitting() already set the
1347 * splitting bit in all pmd that could map this
1348 * hugepage, that will ensure no CPU can alter the
1349 * mapcount on the head page. The mapcount is only
1350 * accounted in the head page and it has to be
1351 * transferred to all tail pages in the below code. So
1352 * for this code to be safe, the split the mapcount
1353 * can't change. But that doesn't mean userland can't
1354 * keep changing and reading the page contents while
1355 * we transfer the mapcount, so the pmd splitting
1356 * status is achieved setting a reserved bit in the
1357 * pmd, not by clearing the present bit.
1359 page_tail->_mapcount = page->_mapcount;
1361 BUG_ON(page_tail->mapping);
1362 page_tail->mapping = page->mapping;
1364 page_tail->index = page->index + i;
1365 page_xchg_last_nid(page_tail, page_last_nid(page));
1367 BUG_ON(!PageAnon(page_tail));
1368 BUG_ON(!PageUptodate(page_tail));
1369 BUG_ON(!PageDirty(page_tail));
1370 BUG_ON(!PageSwapBacked(page_tail));
1372 lru_add_page_tail(page, page_tail, lruvec);
1374 atomic_sub(tail_count, &page->_count);
1375 BUG_ON(atomic_read(&page->_count) <= 0);
1377 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1378 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1380 ClearPageCompound(page);
1381 compound_unlock(page);
1382 spin_unlock_irq(&zone->lru_lock);
1384 for (i = 1; i < HPAGE_PMD_NR; i++) {
1385 struct page *page_tail = page + i;
1386 BUG_ON(page_count(page_tail) <= 0);
1388 * Tail pages may be freed if there wasn't any mapping
1389 * like if add_to_swap() is running on a lru page that
1390 * had its mapping zapped. And freeing these pages
1391 * requires taking the lru_lock so we do the put_page
1392 * of the tail pages after the split is complete.
1394 put_page(page_tail);
1398 * Only the head page (now become a regular page) is required
1399 * to be pinned by the caller.
1401 BUG_ON(page_count(page) <= 0);
1404 static int __split_huge_page_map(struct page *page,
1405 struct vm_area_struct *vma,
1406 unsigned long address)
1408 struct mm_struct *mm = vma->vm_mm;
1412 unsigned long haddr;
1414 spin_lock(&mm->page_table_lock);
1415 pmd = page_check_address_pmd(page, mm, address,
1416 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1418 pgtable = pgtable_trans_huge_withdraw(mm);
1419 pmd_populate(mm, &_pmd, pgtable);
1422 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1424 BUG_ON(PageCompound(page+i));
1425 entry = mk_pte(page + i, vma->vm_page_prot);
1426 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1427 if (!pmd_write(*pmd))
1428 entry = pte_wrprotect(entry);
1430 BUG_ON(page_mapcount(page) != 1);
1431 if (!pmd_young(*pmd))
1432 entry = pte_mkold(entry);
1434 entry = pte_mknuma(entry);
1435 pte = pte_offset_map(&_pmd, haddr);
1436 BUG_ON(!pte_none(*pte));
1437 set_pte_at(mm, haddr, pte, entry);
1441 smp_wmb(); /* make pte visible before pmd */
1443 * Up to this point the pmd is present and huge and
1444 * userland has the whole access to the hugepage
1445 * during the split (which happens in place). If we
1446 * overwrite the pmd with the not-huge version
1447 * pointing to the pte here (which of course we could
1448 * if all CPUs were bug free), userland could trigger
1449 * a small page size TLB miss on the small sized TLB
1450 * while the hugepage TLB entry is still established
1451 * in the huge TLB. Some CPU doesn't like that. See
1452 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1453 * Erratum 383 on page 93. Intel should be safe but is
1454 * also warns that it's only safe if the permission
1455 * and cache attributes of the two entries loaded in
1456 * the two TLB is identical (which should be the case
1457 * here). But it is generally safer to never allow
1458 * small and huge TLB entries for the same virtual
1459 * address to be loaded simultaneously. So instead of
1460 * doing "pmd_populate(); flush_tlb_range();" we first
1461 * mark the current pmd notpresent (atomically because
1462 * here the pmd_trans_huge and pmd_trans_splitting
1463 * must remain set at all times on the pmd until the
1464 * split is complete for this pmd), then we flush the
1465 * SMP TLB and finally we write the non-huge version
1466 * of the pmd entry with pmd_populate.
1468 pmdp_invalidate(vma, address, pmd);
1469 pmd_populate(mm, pmd, pgtable);
1472 spin_unlock(&mm->page_table_lock);
1477 /* must be called with anon_vma->root->mutex hold */
1478 static void __split_huge_page(struct page *page,
1479 struct anon_vma *anon_vma)
1481 int mapcount, mapcount2;
1482 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1483 struct anon_vma_chain *avc;
1485 BUG_ON(!PageHead(page));
1486 BUG_ON(PageTail(page));
1489 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1490 struct vm_area_struct *vma = avc->vma;
1491 unsigned long addr = vma_address(page, vma);
1492 BUG_ON(is_vma_temporary_stack(vma));
1493 mapcount += __split_huge_page_splitting(page, vma, addr);
1496 * It is critical that new vmas are added to the tail of the
1497 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1498 * and establishes a child pmd before
1499 * __split_huge_page_splitting() freezes the parent pmd (so if
1500 * we fail to prevent copy_huge_pmd() from running until the
1501 * whole __split_huge_page() is complete), we will still see
1502 * the newly established pmd of the child later during the
1503 * walk, to be able to set it as pmd_trans_splitting too.
1505 if (mapcount != page_mapcount(page))
1506 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1507 mapcount, page_mapcount(page));
1508 BUG_ON(mapcount != page_mapcount(page));
1510 __split_huge_page_refcount(page);
1513 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1514 struct vm_area_struct *vma = avc->vma;
1515 unsigned long addr = vma_address(page, vma);
1516 BUG_ON(is_vma_temporary_stack(vma));
1517 mapcount2 += __split_huge_page_map(page, vma, addr);
1519 if (mapcount != mapcount2)
1520 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1521 mapcount, mapcount2, page_mapcount(page));
1522 BUG_ON(mapcount != mapcount2);
1525 int split_huge_page(struct page *page)
1527 struct anon_vma *anon_vma;
1530 BUG_ON(!PageAnon(page));
1531 anon_vma = page_lock_anon_vma(page);
1535 if (!PageCompound(page))
1538 BUG_ON(!PageSwapBacked(page));
1539 __split_huge_page(page, anon_vma);
1540 count_vm_event(THP_SPLIT);
1542 BUG_ON(PageCompound(page));
1544 page_unlock_anon_vma(anon_vma);
1549 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1551 int hugepage_madvise(struct vm_area_struct *vma,
1552 unsigned long *vm_flags, int advice)
1554 struct mm_struct *mm = vma->vm_mm;
1559 * Be somewhat over-protective like KSM for now!
1561 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1563 if (mm->def_flags & VM_NOHUGEPAGE)
1565 *vm_flags &= ~VM_NOHUGEPAGE;
1566 *vm_flags |= VM_HUGEPAGE;
1568 * If the vma become good for khugepaged to scan,
1569 * register it here without waiting a page fault that
1570 * may not happen any time soon.
1572 if (unlikely(khugepaged_enter_vma_merge(vma)))
1575 case MADV_NOHUGEPAGE:
1577 * Be somewhat over-protective like KSM for now!
1579 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1581 *vm_flags &= ~VM_HUGEPAGE;
1582 *vm_flags |= VM_NOHUGEPAGE;
1584 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1585 * this vma even if we leave the mm registered in khugepaged if
1586 * it got registered before VM_NOHUGEPAGE was set.
1594 static int __init khugepaged_slab_init(void)
1596 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1597 sizeof(struct mm_slot),
1598 __alignof__(struct mm_slot), 0, NULL);
1605 static void __init khugepaged_slab_free(void)
1607 kmem_cache_destroy(mm_slot_cache);
1608 mm_slot_cache = NULL;
1611 static inline struct mm_slot *alloc_mm_slot(void)
1613 if (!mm_slot_cache) /* initialization failed */
1615 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1618 static inline void free_mm_slot(struct mm_slot *mm_slot)
1620 kmem_cache_free(mm_slot_cache, mm_slot);
1623 static int __init mm_slots_hash_init(void)
1625 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1633 static void __init mm_slots_hash_free(void)
1635 kfree(mm_slots_hash);
1636 mm_slots_hash = NULL;
1640 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1642 struct mm_slot *mm_slot;
1643 struct hlist_head *bucket;
1644 struct hlist_node *node;
1646 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1647 % MM_SLOTS_HASH_HEADS];
1648 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1649 if (mm == mm_slot->mm)
1655 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1656 struct mm_slot *mm_slot)
1658 struct hlist_head *bucket;
1660 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1661 % MM_SLOTS_HASH_HEADS];
1663 hlist_add_head(&mm_slot->hash, bucket);
1666 static inline int khugepaged_test_exit(struct mm_struct *mm)
1668 return atomic_read(&mm->mm_users) == 0;
1671 int __khugepaged_enter(struct mm_struct *mm)
1673 struct mm_slot *mm_slot;
1676 mm_slot = alloc_mm_slot();
1680 /* __khugepaged_exit() must not run from under us */
1681 VM_BUG_ON(khugepaged_test_exit(mm));
1682 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1683 free_mm_slot(mm_slot);
1687 spin_lock(&khugepaged_mm_lock);
1688 insert_to_mm_slots_hash(mm, mm_slot);
1690 * Insert just behind the scanning cursor, to let the area settle
1693 wakeup = list_empty(&khugepaged_scan.mm_head);
1694 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1695 spin_unlock(&khugepaged_mm_lock);
1697 atomic_inc(&mm->mm_count);
1699 wake_up_interruptible(&khugepaged_wait);
1704 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1706 unsigned long hstart, hend;
1709 * Not yet faulted in so we will register later in the
1710 * page fault if needed.
1714 /* khugepaged not yet working on file or special mappings */
1716 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1717 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1718 hend = vma->vm_end & HPAGE_PMD_MASK;
1720 return khugepaged_enter(vma);
1724 void __khugepaged_exit(struct mm_struct *mm)
1726 struct mm_slot *mm_slot;
1729 spin_lock(&khugepaged_mm_lock);
1730 mm_slot = get_mm_slot(mm);
1731 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1732 hlist_del(&mm_slot->hash);
1733 list_del(&mm_slot->mm_node);
1736 spin_unlock(&khugepaged_mm_lock);
1739 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1740 free_mm_slot(mm_slot);
1742 } else if (mm_slot) {
1744 * This is required to serialize against
1745 * khugepaged_test_exit() (which is guaranteed to run
1746 * under mmap sem read mode). Stop here (after we
1747 * return all pagetables will be destroyed) until
1748 * khugepaged has finished working on the pagetables
1749 * under the mmap_sem.
1751 down_write(&mm->mmap_sem);
1752 up_write(&mm->mmap_sem);
1756 static void release_pte_page(struct page *page)
1758 /* 0 stands for page_is_file_cache(page) == false */
1759 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1761 putback_lru_page(page);
1764 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1766 while (--_pte >= pte) {
1767 pte_t pteval = *_pte;
1768 if (!pte_none(pteval))
1769 release_pte_page(pte_page(pteval));
1773 static void release_all_pte_pages(pte_t *pte)
1775 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1778 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1779 unsigned long address,
1784 int referenced = 0, isolated = 0, none = 0;
1785 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1786 _pte++, address += PAGE_SIZE) {
1787 pte_t pteval = *_pte;
1788 if (pte_none(pteval)) {
1789 if (++none <= khugepaged_max_ptes_none)
1792 release_pte_pages(pte, _pte);
1796 if (!pte_present(pteval) || !pte_write(pteval)) {
1797 release_pte_pages(pte, _pte);
1800 page = vm_normal_page(vma, address, pteval);
1801 if (unlikely(!page)) {
1802 release_pte_pages(pte, _pte);
1805 VM_BUG_ON(PageCompound(page));
1806 BUG_ON(!PageAnon(page));
1807 VM_BUG_ON(!PageSwapBacked(page));
1809 /* cannot use mapcount: can't collapse if there's a gup pin */
1810 if (page_count(page) != 1) {
1811 release_pte_pages(pte, _pte);
1815 * We can do it before isolate_lru_page because the
1816 * page can't be freed from under us. NOTE: PG_lock
1817 * is needed to serialize against split_huge_page
1818 * when invoked from the VM.
1820 if (!trylock_page(page)) {
1821 release_pte_pages(pte, _pte);
1825 * Isolate the page to avoid collapsing an hugepage
1826 * currently in use by the VM.
1828 if (isolate_lru_page(page)) {
1830 release_pte_pages(pte, _pte);
1833 /* 0 stands for page_is_file_cache(page) == false */
1834 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1835 VM_BUG_ON(!PageLocked(page));
1836 VM_BUG_ON(PageLRU(page));
1838 /* If there is no mapped pte young don't collapse the page */
1839 if (pte_young(pteval) || PageReferenced(page) ||
1840 mmu_notifier_test_young(vma->vm_mm, address))
1843 if (unlikely(!referenced))
1844 release_all_pte_pages(pte);
1851 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1852 struct vm_area_struct *vma,
1853 unsigned long address,
1857 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1858 pte_t pteval = *_pte;
1859 struct page *src_page;
1861 if (pte_none(pteval)) {
1862 clear_user_highpage(page, address);
1863 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1865 src_page = pte_page(pteval);
1866 copy_user_highpage(page, src_page, address, vma);
1867 VM_BUG_ON(page_mapcount(src_page) != 1);
1868 release_pte_page(src_page);
1870 * ptl mostly unnecessary, but preempt has to
1871 * be disabled to update the per-cpu stats
1872 * inside page_remove_rmap().
1876 * paravirt calls inside pte_clear here are
1879 pte_clear(vma->vm_mm, address, _pte);
1880 page_remove_rmap(src_page);
1882 free_page_and_swap_cache(src_page);
1885 address += PAGE_SIZE;
1890 static void khugepaged_alloc_sleep(void)
1892 wait_event_freezable_timeout(khugepaged_wait, false,
1893 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
1897 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1899 if (IS_ERR(*hpage)) {
1905 khugepaged_alloc_sleep();
1906 } else if (*hpage) {
1915 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1916 struct vm_area_struct *vma, unsigned long address,
1921 * Allocate the page while the vma is still valid and under
1922 * the mmap_sem read mode so there is no memory allocation
1923 * later when we take the mmap_sem in write mode. This is more
1924 * friendly behavior (OTOH it may actually hide bugs) to
1925 * filesystems in userland with daemons allocating memory in
1926 * the userland I/O paths. Allocating memory with the
1927 * mmap_sem in read mode is good idea also to allow greater
1930 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1931 node, __GFP_OTHER_NODE);
1934 * After allocating the hugepage, release the mmap_sem read lock in
1935 * preparation for taking it in write mode.
1937 up_read(&mm->mmap_sem);
1938 if (unlikely(!*hpage)) {
1939 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1940 *hpage = ERR_PTR(-ENOMEM);
1944 count_vm_event(THP_COLLAPSE_ALLOC);
1948 static struct page *khugepaged_alloc_hugepage(bool *wait)
1953 hpage = alloc_hugepage(khugepaged_defrag());
1955 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1960 khugepaged_alloc_sleep();
1962 count_vm_event(THP_COLLAPSE_ALLOC);
1963 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
1968 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1971 *hpage = khugepaged_alloc_hugepage(wait);
1973 if (unlikely(!*hpage))
1980 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1981 struct vm_area_struct *vma, unsigned long address,
1984 up_read(&mm->mmap_sem);
1990 static void collapse_huge_page(struct mm_struct *mm,
1991 unsigned long address,
1992 struct page **hpage,
1993 struct vm_area_struct *vma,
2001 struct page *new_page;
2004 unsigned long hstart, hend;
2005 unsigned long mmun_start; /* For mmu_notifiers */
2006 unsigned long mmun_end; /* For mmu_notifiers */
2008 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2010 /* release the mmap_sem read lock. */
2011 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2015 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
2019 * Prevent all access to pagetables with the exception of
2020 * gup_fast later hanlded by the ptep_clear_flush and the VM
2021 * handled by the anon_vma lock + PG_lock.
2023 down_write(&mm->mmap_sem);
2024 if (unlikely(khugepaged_test_exit(mm)))
2027 vma = find_vma(mm, address);
2028 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2029 hend = vma->vm_end & HPAGE_PMD_MASK;
2030 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2033 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2034 (vma->vm_flags & VM_NOHUGEPAGE))
2037 if (!vma->anon_vma || vma->vm_ops)
2039 if (is_vma_temporary_stack(vma))
2041 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2043 pgd = pgd_offset(mm, address);
2044 if (!pgd_present(*pgd))
2047 pud = pud_offset(pgd, address);
2048 if (!pud_present(*pud))
2051 pmd = pmd_offset(pud, address);
2052 /* pmd can't go away or become huge under us */
2053 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2056 anon_vma_lock(vma->anon_vma);
2058 pte = pte_offset_map(pmd, address);
2059 ptl = pte_lockptr(mm, pmd);
2061 mmun_start = address;
2062 mmun_end = address + HPAGE_PMD_SIZE;
2063 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2064 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2066 * After this gup_fast can't run anymore. This also removes
2067 * any huge TLB entry from the CPU so we won't allow
2068 * huge and small TLB entries for the same virtual address
2069 * to avoid the risk of CPU bugs in that area.
2071 _pmd = pmdp_clear_flush(vma, address, pmd);
2072 spin_unlock(&mm->page_table_lock);
2073 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2076 isolated = __collapse_huge_page_isolate(vma, address, pte);
2079 if (unlikely(!isolated)) {
2081 spin_lock(&mm->page_table_lock);
2082 BUG_ON(!pmd_none(*pmd));
2083 set_pmd_at(mm, address, pmd, _pmd);
2084 spin_unlock(&mm->page_table_lock);
2085 anon_vma_unlock(vma->anon_vma);
2090 * All pages are isolated and locked so anon_vma rmap
2091 * can't run anymore.
2093 anon_vma_unlock(vma->anon_vma);
2095 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2097 __SetPageUptodate(new_page);
2098 pgtable = pmd_pgtable(_pmd);
2100 _pmd = mk_pmd(new_page, vma->vm_page_prot);
2101 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
2102 _pmd = pmd_mkhuge(_pmd);
2105 * spin_lock() below is not the equivalent of smp_wmb(), so
2106 * this is needed to avoid the copy_huge_page writes to become
2107 * visible after the set_pmd_at() write.
2111 spin_lock(&mm->page_table_lock);
2112 BUG_ON(!pmd_none(*pmd));
2113 page_add_new_anon_rmap(new_page, vma, address);
2114 set_pmd_at(mm, address, pmd, _pmd);
2115 update_mmu_cache_pmd(vma, address, pmd);
2116 pgtable_trans_huge_deposit(mm, pgtable);
2117 spin_unlock(&mm->page_table_lock);
2121 khugepaged_pages_collapsed++;
2123 up_write(&mm->mmap_sem);
2127 mem_cgroup_uncharge_page(new_page);
2131 static int khugepaged_scan_pmd(struct mm_struct *mm,
2132 struct vm_area_struct *vma,
2133 unsigned long address,
2134 struct page **hpage)
2140 int ret = 0, referenced = 0, none = 0;
2142 unsigned long _address;
2146 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2148 pgd = pgd_offset(mm, address);
2149 if (!pgd_present(*pgd))
2152 pud = pud_offset(pgd, address);
2153 if (!pud_present(*pud))
2156 pmd = pmd_offset(pud, address);
2157 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2160 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2161 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2162 _pte++, _address += PAGE_SIZE) {
2163 pte_t pteval = *_pte;
2164 if (pte_none(pteval)) {
2165 if (++none <= khugepaged_max_ptes_none)
2170 if (!pte_present(pteval) || !pte_write(pteval))
2172 page = vm_normal_page(vma, _address, pteval);
2173 if (unlikely(!page))
2176 * Chose the node of the first page. This could
2177 * be more sophisticated and look at more pages,
2178 * but isn't for now.
2181 node = page_to_nid(page);
2182 VM_BUG_ON(PageCompound(page));
2183 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2185 /* cannot use mapcount: can't collapse if there's a gup pin */
2186 if (page_count(page) != 1)
2188 if (pte_young(pteval) || PageReferenced(page) ||
2189 mmu_notifier_test_young(vma->vm_mm, address))
2195 pte_unmap_unlock(pte, ptl);
2197 /* collapse_huge_page will return with the mmap_sem released */
2198 collapse_huge_page(mm, address, hpage, vma, node);
2203 static void collect_mm_slot(struct mm_slot *mm_slot)
2205 struct mm_struct *mm = mm_slot->mm;
2207 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2209 if (khugepaged_test_exit(mm)) {
2211 hlist_del(&mm_slot->hash);
2212 list_del(&mm_slot->mm_node);
2215 * Not strictly needed because the mm exited already.
2217 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2220 /* khugepaged_mm_lock actually not necessary for the below */
2221 free_mm_slot(mm_slot);
2226 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2227 struct page **hpage)
2228 __releases(&khugepaged_mm_lock)
2229 __acquires(&khugepaged_mm_lock)
2231 struct mm_slot *mm_slot;
2232 struct mm_struct *mm;
2233 struct vm_area_struct *vma;
2237 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2239 if (khugepaged_scan.mm_slot)
2240 mm_slot = khugepaged_scan.mm_slot;
2242 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2243 struct mm_slot, mm_node);
2244 khugepaged_scan.address = 0;
2245 khugepaged_scan.mm_slot = mm_slot;
2247 spin_unlock(&khugepaged_mm_lock);
2250 down_read(&mm->mmap_sem);
2251 if (unlikely(khugepaged_test_exit(mm)))
2254 vma = find_vma(mm, khugepaged_scan.address);
2257 for (; vma; vma = vma->vm_next) {
2258 unsigned long hstart, hend;
2261 if (unlikely(khugepaged_test_exit(mm))) {
2266 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2267 !khugepaged_always()) ||
2268 (vma->vm_flags & VM_NOHUGEPAGE)) {
2273 if (!vma->anon_vma || vma->vm_ops)
2275 if (is_vma_temporary_stack(vma))
2277 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2279 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2280 hend = vma->vm_end & HPAGE_PMD_MASK;
2283 if (khugepaged_scan.address > hend)
2285 if (khugepaged_scan.address < hstart)
2286 khugepaged_scan.address = hstart;
2287 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2289 while (khugepaged_scan.address < hend) {
2292 if (unlikely(khugepaged_test_exit(mm)))
2293 goto breakouterloop;
2295 VM_BUG_ON(khugepaged_scan.address < hstart ||
2296 khugepaged_scan.address + HPAGE_PMD_SIZE >
2298 ret = khugepaged_scan_pmd(mm, vma,
2299 khugepaged_scan.address,
2301 /* move to next address */
2302 khugepaged_scan.address += HPAGE_PMD_SIZE;
2303 progress += HPAGE_PMD_NR;
2305 /* we released mmap_sem so break loop */
2306 goto breakouterloop_mmap_sem;
2307 if (progress >= pages)
2308 goto breakouterloop;
2312 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2313 breakouterloop_mmap_sem:
2315 spin_lock(&khugepaged_mm_lock);
2316 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2318 * Release the current mm_slot if this mm is about to die, or
2319 * if we scanned all vmas of this mm.
2321 if (khugepaged_test_exit(mm) || !vma) {
2323 * Make sure that if mm_users is reaching zero while
2324 * khugepaged runs here, khugepaged_exit will find
2325 * mm_slot not pointing to the exiting mm.
2327 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2328 khugepaged_scan.mm_slot = list_entry(
2329 mm_slot->mm_node.next,
2330 struct mm_slot, mm_node);
2331 khugepaged_scan.address = 0;
2333 khugepaged_scan.mm_slot = NULL;
2334 khugepaged_full_scans++;
2337 collect_mm_slot(mm_slot);
2343 static int khugepaged_has_work(void)
2345 return !list_empty(&khugepaged_scan.mm_head) &&
2346 khugepaged_enabled();
2349 static int khugepaged_wait_event(void)
2351 return !list_empty(&khugepaged_scan.mm_head) ||
2352 kthread_should_stop();
2355 static void khugepaged_do_scan(void)
2357 struct page *hpage = NULL;
2358 unsigned int progress = 0, pass_through_head = 0;
2359 unsigned int pages = khugepaged_pages_to_scan;
2362 barrier(); /* write khugepaged_pages_to_scan to local stack */
2364 while (progress < pages) {
2365 if (!khugepaged_prealloc_page(&hpage, &wait))
2370 if (unlikely(kthread_should_stop() || freezing(current)))
2373 spin_lock(&khugepaged_mm_lock);
2374 if (!khugepaged_scan.mm_slot)
2375 pass_through_head++;
2376 if (khugepaged_has_work() &&
2377 pass_through_head < 2)
2378 progress += khugepaged_scan_mm_slot(pages - progress,
2382 spin_unlock(&khugepaged_mm_lock);
2385 if (!IS_ERR_OR_NULL(hpage))
2389 static void khugepaged_wait_work(void)
2393 if (khugepaged_has_work()) {
2394 if (!khugepaged_scan_sleep_millisecs)
2397 wait_event_freezable_timeout(khugepaged_wait,
2398 kthread_should_stop(),
2399 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2403 if (khugepaged_enabled())
2404 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2407 static int khugepaged(void *none)
2409 struct mm_slot *mm_slot;
2412 set_user_nice(current, 19);
2414 while (!kthread_should_stop()) {
2415 khugepaged_do_scan();
2416 khugepaged_wait_work();
2419 spin_lock(&khugepaged_mm_lock);
2420 mm_slot = khugepaged_scan.mm_slot;
2421 khugepaged_scan.mm_slot = NULL;
2423 collect_mm_slot(mm_slot);
2424 spin_unlock(&khugepaged_mm_lock);
2428 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2432 spin_lock(&mm->page_table_lock);
2433 if (unlikely(!pmd_trans_huge(*pmd))) {
2434 spin_unlock(&mm->page_table_lock);
2437 page = pmd_page(*pmd);
2438 VM_BUG_ON(!page_count(page));
2440 spin_unlock(&mm->page_table_lock);
2442 split_huge_page(page);
2445 BUG_ON(pmd_trans_huge(*pmd));
2448 static void split_huge_page_address(struct mm_struct *mm,
2449 unsigned long address)
2455 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2457 pgd = pgd_offset(mm, address);
2458 if (!pgd_present(*pgd))
2461 pud = pud_offset(pgd, address);
2462 if (!pud_present(*pud))
2465 pmd = pmd_offset(pud, address);
2466 if (!pmd_present(*pmd))
2469 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2470 * materialize from under us.
2472 split_huge_page_pmd(mm, pmd);
2475 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2476 unsigned long start,
2481 * If the new start address isn't hpage aligned and it could
2482 * previously contain an hugepage: check if we need to split
2485 if (start & ~HPAGE_PMD_MASK &&
2486 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2487 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2488 split_huge_page_address(vma->vm_mm, start);
2491 * If the new end address isn't hpage aligned and it could
2492 * previously contain an hugepage: check if we need to split
2495 if (end & ~HPAGE_PMD_MASK &&
2496 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2497 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2498 split_huge_page_address(vma->vm_mm, end);
2501 * If we're also updating the vma->vm_next->vm_start, if the new
2502 * vm_next->vm_start isn't page aligned and it could previously
2503 * contain an hugepage: check if we need to split an huge pmd.
2505 if (adjust_next > 0) {
2506 struct vm_area_struct *next = vma->vm_next;
2507 unsigned long nstart = next->vm_start;
2508 nstart += adjust_next << PAGE_SHIFT;
2509 if (nstart & ~HPAGE_PMD_MASK &&
2510 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2511 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2512 split_huge_page_address(next->vm_mm, nstart);