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/shrinker.h>
16 #include <linux/mm_inline.h>
17 #include <linux/kthread.h>
18 #include <linux/khugepaged.h>
19 #include <linux/freezer.h>
20 #include <linux/mman.h>
21 #include <linux/pagemap.h>
22 #include <linux/migrate.h>
23 #include <linux/hashtable.h>
26 #include <asm/pgalloc.h>
30 * By default transparent hugepage support is enabled for all mappings
31 * and khugepaged scans all mappings. Defrag is only invoked by
32 * khugepaged hugepage allocations and by page faults inside
33 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
36 unsigned long transparent_hugepage_flags __read_mostly =
37 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
38 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
40 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
41 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
43 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
44 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
45 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
47 /* default scan 8*512 pte (or vmas) every 30 second */
48 static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
49 static unsigned int khugepaged_pages_collapsed;
50 static unsigned int khugepaged_full_scans;
51 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
52 /* during fragmentation poll the hugepage allocator once every minute */
53 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
54 static struct task_struct *khugepaged_thread __read_mostly;
55 static DEFINE_MUTEX(khugepaged_mutex);
56 static DEFINE_SPINLOCK(khugepaged_mm_lock);
57 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
59 * default collapse hugepages if there is at least one pte mapped like
60 * it would have happened if the vma was large enough during page
63 static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
65 static int khugepaged(void *none);
66 static int khugepaged_slab_init(void);
68 #define MM_SLOTS_HASH_BITS 10
69 static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
71 static struct kmem_cache *mm_slot_cache __read_mostly;
74 * struct mm_slot - hash lookup from mm to mm_slot
75 * @hash: hash collision list
76 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
77 * @mm: the mm that this information is valid for
80 struct hlist_node hash;
81 struct list_head mm_node;
86 * struct khugepaged_scan - cursor for scanning
87 * @mm_head: the head of the mm list to scan
88 * @mm_slot: the current mm_slot we are scanning
89 * @address: the next address inside that to be scanned
91 * There is only the one khugepaged_scan instance of this cursor structure.
93 struct khugepaged_scan {
94 struct list_head mm_head;
95 struct mm_slot *mm_slot;
96 unsigned long address;
98 static struct khugepaged_scan khugepaged_scan = {
99 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
103 static int set_recommended_min_free_kbytes(void)
107 unsigned long recommended_min;
109 if (!khugepaged_enabled())
112 for_each_populated_zone(zone)
115 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
116 recommended_min = pageblock_nr_pages * nr_zones * 2;
119 * Make sure that on average at least two pageblocks are almost free
120 * of another type, one for a migratetype to fall back to and a
121 * second to avoid subsequent fallbacks of other types There are 3
122 * MIGRATE_TYPES we care about.
124 recommended_min += pageblock_nr_pages * nr_zones *
125 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
127 /* don't ever allow to reserve more than 5% of the lowmem */
128 recommended_min = min(recommended_min,
129 (unsigned long) nr_free_buffer_pages() / 20);
130 recommended_min <<= (PAGE_SHIFT-10);
132 if (recommended_min > min_free_kbytes)
133 min_free_kbytes = recommended_min;
134 setup_per_zone_wmarks();
137 late_initcall(set_recommended_min_free_kbytes);
139 static int start_khugepaged(void)
142 if (khugepaged_enabled()) {
143 if (!khugepaged_thread)
144 khugepaged_thread = kthread_run(khugepaged, NULL,
146 if (unlikely(IS_ERR(khugepaged_thread))) {
148 "khugepaged: kthread_run(khugepaged) failed\n");
149 err = PTR_ERR(khugepaged_thread);
150 khugepaged_thread = NULL;
153 if (!list_empty(&khugepaged_scan.mm_head))
154 wake_up_interruptible(&khugepaged_wait);
156 set_recommended_min_free_kbytes();
157 } else if (khugepaged_thread) {
158 kthread_stop(khugepaged_thread);
159 khugepaged_thread = NULL;
165 static atomic_t huge_zero_refcount;
166 static struct page *huge_zero_page __read_mostly;
168 static inline bool is_huge_zero_page(struct page *page)
170 return ACCESS_ONCE(huge_zero_page) == page;
173 static inline bool is_huge_zero_pmd(pmd_t pmd)
175 return is_huge_zero_page(pmd_page(pmd));
178 static struct page *get_huge_zero_page(void)
180 struct page *zero_page;
182 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
183 return ACCESS_ONCE(huge_zero_page);
185 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
188 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
191 count_vm_event(THP_ZERO_PAGE_ALLOC);
193 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
195 __free_page(zero_page);
199 /* We take additional reference here. It will be put back by shrinker */
200 atomic_set(&huge_zero_refcount, 2);
202 return ACCESS_ONCE(huge_zero_page);
205 static void put_huge_zero_page(void)
208 * Counter should never go to zero here. Only shrinker can put
211 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
214 static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
215 struct shrink_control *sc)
217 /* we can free zero page only if last reference remains */
218 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
221 static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
222 struct shrink_control *sc)
224 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
225 struct page *zero_page = xchg(&huge_zero_page, NULL);
226 BUG_ON(zero_page == NULL);
227 __free_page(zero_page);
234 static struct shrinker huge_zero_page_shrinker = {
235 .count_objects = shrink_huge_zero_page_count,
236 .scan_objects = shrink_huge_zero_page_scan,
237 .seeks = DEFAULT_SEEKS,
242 static ssize_t double_flag_show(struct kobject *kobj,
243 struct kobj_attribute *attr, char *buf,
244 enum transparent_hugepage_flag enabled,
245 enum transparent_hugepage_flag req_madv)
247 if (test_bit(enabled, &transparent_hugepage_flags)) {
248 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
249 return sprintf(buf, "[always] madvise never\n");
250 } else if (test_bit(req_madv, &transparent_hugepage_flags))
251 return sprintf(buf, "always [madvise] never\n");
253 return sprintf(buf, "always madvise [never]\n");
255 static ssize_t double_flag_store(struct kobject *kobj,
256 struct kobj_attribute *attr,
257 const char *buf, size_t count,
258 enum transparent_hugepage_flag enabled,
259 enum transparent_hugepage_flag req_madv)
261 if (!memcmp("always", buf,
262 min(sizeof("always")-1, count))) {
263 set_bit(enabled, &transparent_hugepage_flags);
264 clear_bit(req_madv, &transparent_hugepage_flags);
265 } else if (!memcmp("madvise", buf,
266 min(sizeof("madvise")-1, count))) {
267 clear_bit(enabled, &transparent_hugepage_flags);
268 set_bit(req_madv, &transparent_hugepage_flags);
269 } else if (!memcmp("never", buf,
270 min(sizeof("never")-1, count))) {
271 clear_bit(enabled, &transparent_hugepage_flags);
272 clear_bit(req_madv, &transparent_hugepage_flags);
279 static ssize_t enabled_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf)
282 return double_flag_show(kobj, attr, buf,
283 TRANSPARENT_HUGEPAGE_FLAG,
284 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
286 static ssize_t enabled_store(struct kobject *kobj,
287 struct kobj_attribute *attr,
288 const char *buf, size_t count)
292 ret = double_flag_store(kobj, attr, buf, count,
293 TRANSPARENT_HUGEPAGE_FLAG,
294 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
299 mutex_lock(&khugepaged_mutex);
300 err = start_khugepaged();
301 mutex_unlock(&khugepaged_mutex);
309 static struct kobj_attribute enabled_attr =
310 __ATTR(enabled, 0644, enabled_show, enabled_store);
312 static ssize_t single_flag_show(struct kobject *kobj,
313 struct kobj_attribute *attr, char *buf,
314 enum transparent_hugepage_flag flag)
316 return sprintf(buf, "%d\n",
317 !!test_bit(flag, &transparent_hugepage_flags));
320 static ssize_t single_flag_store(struct kobject *kobj,
321 struct kobj_attribute *attr,
322 const char *buf, size_t count,
323 enum transparent_hugepage_flag flag)
328 ret = kstrtoul(buf, 10, &value);
335 set_bit(flag, &transparent_hugepage_flags);
337 clear_bit(flag, &transparent_hugepage_flags);
343 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
344 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
345 * memory just to allocate one more hugepage.
347 static ssize_t defrag_show(struct kobject *kobj,
348 struct kobj_attribute *attr, char *buf)
350 return double_flag_show(kobj, attr, buf,
351 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
352 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
354 static ssize_t defrag_store(struct kobject *kobj,
355 struct kobj_attribute *attr,
356 const char *buf, size_t count)
358 return double_flag_store(kobj, attr, buf, count,
359 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
360 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
362 static struct kobj_attribute defrag_attr =
363 __ATTR(defrag, 0644, defrag_show, defrag_store);
365 static ssize_t use_zero_page_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf)
368 return single_flag_show(kobj, attr, buf,
369 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
371 static ssize_t use_zero_page_store(struct kobject *kobj,
372 struct kobj_attribute *attr, const char *buf, size_t count)
374 return single_flag_store(kobj, attr, buf, count,
375 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
377 static struct kobj_attribute use_zero_page_attr =
378 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
379 #ifdef CONFIG_DEBUG_VM
380 static ssize_t debug_cow_show(struct kobject *kobj,
381 struct kobj_attribute *attr, char *buf)
383 return single_flag_show(kobj, attr, buf,
384 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
386 static ssize_t debug_cow_store(struct kobject *kobj,
387 struct kobj_attribute *attr,
388 const char *buf, size_t count)
390 return single_flag_store(kobj, attr, buf, count,
391 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
393 static struct kobj_attribute debug_cow_attr =
394 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
395 #endif /* CONFIG_DEBUG_VM */
397 static struct attribute *hugepage_attr[] = {
400 &use_zero_page_attr.attr,
401 #ifdef CONFIG_DEBUG_VM
402 &debug_cow_attr.attr,
407 static struct attribute_group hugepage_attr_group = {
408 .attrs = hugepage_attr,
411 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
412 struct kobj_attribute *attr,
415 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
418 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
419 struct kobj_attribute *attr,
420 const char *buf, size_t count)
425 err = kstrtoul(buf, 10, &msecs);
426 if (err || msecs > UINT_MAX)
429 khugepaged_scan_sleep_millisecs = msecs;
430 wake_up_interruptible(&khugepaged_wait);
434 static struct kobj_attribute scan_sleep_millisecs_attr =
435 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
436 scan_sleep_millisecs_store);
438 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
439 struct kobj_attribute *attr,
442 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
445 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
446 struct kobj_attribute *attr,
447 const char *buf, size_t count)
452 err = kstrtoul(buf, 10, &msecs);
453 if (err || msecs > UINT_MAX)
456 khugepaged_alloc_sleep_millisecs = msecs;
457 wake_up_interruptible(&khugepaged_wait);
461 static struct kobj_attribute alloc_sleep_millisecs_attr =
462 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
463 alloc_sleep_millisecs_store);
465 static ssize_t pages_to_scan_show(struct kobject *kobj,
466 struct kobj_attribute *attr,
469 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
471 static ssize_t pages_to_scan_store(struct kobject *kobj,
472 struct kobj_attribute *attr,
473 const char *buf, size_t count)
478 err = kstrtoul(buf, 10, &pages);
479 if (err || !pages || pages > UINT_MAX)
482 khugepaged_pages_to_scan = pages;
486 static struct kobj_attribute pages_to_scan_attr =
487 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
488 pages_to_scan_store);
490 static ssize_t pages_collapsed_show(struct kobject *kobj,
491 struct kobj_attribute *attr,
494 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
496 static struct kobj_attribute pages_collapsed_attr =
497 __ATTR_RO(pages_collapsed);
499 static ssize_t full_scans_show(struct kobject *kobj,
500 struct kobj_attribute *attr,
503 return sprintf(buf, "%u\n", khugepaged_full_scans);
505 static struct kobj_attribute full_scans_attr =
506 __ATTR_RO(full_scans);
508 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
509 struct kobj_attribute *attr, char *buf)
511 return single_flag_show(kobj, attr, buf,
512 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
514 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
515 struct kobj_attribute *attr,
516 const char *buf, size_t count)
518 return single_flag_store(kobj, attr, buf, count,
519 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
521 static struct kobj_attribute khugepaged_defrag_attr =
522 __ATTR(defrag, 0644, khugepaged_defrag_show,
523 khugepaged_defrag_store);
526 * max_ptes_none controls if khugepaged should collapse hugepages over
527 * any unmapped ptes in turn potentially increasing the memory
528 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
529 * reduce the available free memory in the system as it
530 * runs. Increasing max_ptes_none will instead potentially reduce the
531 * free memory in the system during the khugepaged scan.
533 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
534 struct kobj_attribute *attr,
537 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
539 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
540 struct kobj_attribute *attr,
541 const char *buf, size_t count)
544 unsigned long max_ptes_none;
546 err = kstrtoul(buf, 10, &max_ptes_none);
547 if (err || max_ptes_none > HPAGE_PMD_NR-1)
550 khugepaged_max_ptes_none = max_ptes_none;
554 static struct kobj_attribute khugepaged_max_ptes_none_attr =
555 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
556 khugepaged_max_ptes_none_store);
558 static struct attribute *khugepaged_attr[] = {
559 &khugepaged_defrag_attr.attr,
560 &khugepaged_max_ptes_none_attr.attr,
561 &pages_to_scan_attr.attr,
562 &pages_collapsed_attr.attr,
563 &full_scans_attr.attr,
564 &scan_sleep_millisecs_attr.attr,
565 &alloc_sleep_millisecs_attr.attr,
569 static struct attribute_group khugepaged_attr_group = {
570 .attrs = khugepaged_attr,
571 .name = "khugepaged",
574 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
578 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
579 if (unlikely(!*hugepage_kobj)) {
580 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
584 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
586 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
590 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
592 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
593 goto remove_hp_group;
599 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
601 kobject_put(*hugepage_kobj);
605 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
607 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
608 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
609 kobject_put(hugepage_kobj);
612 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
617 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
620 #endif /* CONFIG_SYSFS */
622 static int __init hugepage_init(void)
625 struct kobject *hugepage_kobj;
627 if (!has_transparent_hugepage()) {
628 transparent_hugepage_flags = 0;
632 err = hugepage_init_sysfs(&hugepage_kobj);
636 err = khugepaged_slab_init();
640 register_shrinker(&huge_zero_page_shrinker);
643 * By default disable transparent hugepages on smaller systems,
644 * where the extra memory used could hurt more than TLB overhead
645 * is likely to save. The admin can still enable it through /sys.
647 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
648 transparent_hugepage_flags = 0;
654 hugepage_exit_sysfs(hugepage_kobj);
657 module_init(hugepage_init)
659 static int __init setup_transparent_hugepage(char *str)
664 if (!strcmp(str, "always")) {
665 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
666 &transparent_hugepage_flags);
667 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
668 &transparent_hugepage_flags);
670 } else if (!strcmp(str, "madvise")) {
671 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
672 &transparent_hugepage_flags);
673 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
674 &transparent_hugepage_flags);
676 } else if (!strcmp(str, "never")) {
677 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
678 &transparent_hugepage_flags);
679 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
680 &transparent_hugepage_flags);
686 "transparent_hugepage= cannot parse, ignored\n");
689 __setup("transparent_hugepage=", setup_transparent_hugepage);
691 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
693 if (likely(vma->vm_flags & VM_WRITE))
694 pmd = pmd_mkwrite(pmd);
698 static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
701 entry = mk_pmd(page, prot);
702 entry = pmd_mkhuge(entry);
706 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
707 struct vm_area_struct *vma,
708 unsigned long haddr, pmd_t *pmd,
713 VM_BUG_ON(!PageCompound(page));
714 pgtable = pte_alloc_one(mm, haddr);
715 if (unlikely(!pgtable))
718 clear_huge_page(page, haddr, HPAGE_PMD_NR);
720 * The memory barrier inside __SetPageUptodate makes sure that
721 * clear_huge_page writes become visible before the set_pmd_at()
724 __SetPageUptodate(page);
726 spin_lock(&mm->page_table_lock);
727 if (unlikely(!pmd_none(*pmd))) {
728 spin_unlock(&mm->page_table_lock);
729 mem_cgroup_uncharge_page(page);
731 pte_free(mm, pgtable);
734 entry = mk_huge_pmd(page, vma->vm_page_prot);
735 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
736 page_add_new_anon_rmap(page, vma, haddr);
737 pgtable_trans_huge_deposit(mm, pmd, pgtable);
738 set_pmd_at(mm, haddr, pmd, entry);
739 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
741 spin_unlock(&mm->page_table_lock);
747 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
749 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
752 static inline struct page *alloc_hugepage_vma(int defrag,
753 struct vm_area_struct *vma,
754 unsigned long haddr, int nd,
757 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
758 HPAGE_PMD_ORDER, vma, haddr, nd);
762 static inline struct page *alloc_hugepage(int defrag)
764 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
769 static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
770 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
771 struct page *zero_page)
776 entry = mk_pmd(zero_page, vma->vm_page_prot);
777 entry = pmd_wrprotect(entry);
778 entry = pmd_mkhuge(entry);
779 pgtable_trans_huge_deposit(mm, pmd, pgtable);
780 set_pmd_at(mm, haddr, pmd, entry);
785 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
786 unsigned long address, pmd_t *pmd,
790 unsigned long haddr = address & HPAGE_PMD_MASK;
792 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
793 return VM_FAULT_FALLBACK;
794 if (unlikely(anon_vma_prepare(vma)))
796 if (unlikely(khugepaged_enter(vma)))
798 if (!(flags & FAULT_FLAG_WRITE) &&
799 transparent_hugepage_use_zero_page()) {
801 struct page *zero_page;
803 pgtable = pte_alloc_one(mm, haddr);
804 if (unlikely(!pgtable))
806 zero_page = get_huge_zero_page();
807 if (unlikely(!zero_page)) {
808 pte_free(mm, pgtable);
809 count_vm_event(THP_FAULT_FALLBACK);
810 return VM_FAULT_FALLBACK;
812 spin_lock(&mm->page_table_lock);
813 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
815 spin_unlock(&mm->page_table_lock);
817 pte_free(mm, pgtable);
818 put_huge_zero_page();
822 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
823 vma, haddr, numa_node_id(), 0);
824 if (unlikely(!page)) {
825 count_vm_event(THP_FAULT_FALLBACK);
826 return VM_FAULT_FALLBACK;
828 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
830 count_vm_event(THP_FAULT_FALLBACK);
831 return VM_FAULT_FALLBACK;
833 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page))) {
834 mem_cgroup_uncharge_page(page);
836 count_vm_event(THP_FAULT_FALLBACK);
837 return VM_FAULT_FALLBACK;
840 count_vm_event(THP_FAULT_ALLOC);
844 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
845 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
846 struct vm_area_struct *vma)
848 struct page *src_page;
854 pgtable = pte_alloc_one(dst_mm, addr);
855 if (unlikely(!pgtable))
858 spin_lock(&dst_mm->page_table_lock);
859 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
863 if (unlikely(!pmd_trans_huge(pmd))) {
864 pte_free(dst_mm, pgtable);
868 * mm->page_table_lock is enough to be sure that huge zero pmd is not
869 * under splitting since we don't split the page itself, only pmd to
872 if (is_huge_zero_pmd(pmd)) {
873 struct page *zero_page;
876 * get_huge_zero_page() will never allocate a new page here,
877 * since we already have a zero page to copy. It just takes a
880 zero_page = get_huge_zero_page();
881 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
883 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
887 if (unlikely(pmd_trans_splitting(pmd))) {
888 /* split huge page running from under us */
889 spin_unlock(&src_mm->page_table_lock);
890 spin_unlock(&dst_mm->page_table_lock);
891 pte_free(dst_mm, pgtable);
893 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
896 src_page = pmd_page(pmd);
897 VM_BUG_ON(!PageHead(src_page));
899 page_dup_rmap(src_page);
900 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
902 pmdp_set_wrprotect(src_mm, addr, src_pmd);
903 pmd = pmd_mkold(pmd_wrprotect(pmd));
904 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
905 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
910 spin_unlock(&src_mm->page_table_lock);
911 spin_unlock(&dst_mm->page_table_lock);
916 void huge_pmd_set_accessed(struct mm_struct *mm,
917 struct vm_area_struct *vma,
918 unsigned long address,
919 pmd_t *pmd, pmd_t orig_pmd,
925 spin_lock(&mm->page_table_lock);
926 if (unlikely(!pmd_same(*pmd, orig_pmd)))
929 entry = pmd_mkyoung(orig_pmd);
930 haddr = address & HPAGE_PMD_MASK;
931 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
932 update_mmu_cache_pmd(vma, address, pmd);
935 spin_unlock(&mm->page_table_lock);
938 static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
939 struct vm_area_struct *vma, unsigned long address,
940 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
946 unsigned long mmun_start; /* For mmu_notifiers */
947 unsigned long mmun_end; /* For mmu_notifiers */
949 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
955 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
961 clear_user_highpage(page, address);
962 __SetPageUptodate(page);
965 mmun_end = haddr + HPAGE_PMD_SIZE;
966 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
968 spin_lock(&mm->page_table_lock);
969 if (unlikely(!pmd_same(*pmd, orig_pmd)))
972 pmdp_clear_flush(vma, haddr, pmd);
973 /* leave pmd empty until pte is filled */
975 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
976 pmd_populate(mm, &_pmd, pgtable);
978 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
980 if (haddr == (address & PAGE_MASK)) {
981 entry = mk_pte(page, vma->vm_page_prot);
982 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
983 page_add_new_anon_rmap(page, vma, haddr);
985 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
986 entry = pte_mkspecial(entry);
988 pte = pte_offset_map(&_pmd, haddr);
989 VM_BUG_ON(!pte_none(*pte));
990 set_pte_at(mm, haddr, pte, entry);
993 smp_wmb(); /* make pte visible before pmd */
994 pmd_populate(mm, pmd, pgtable);
995 spin_unlock(&mm->page_table_lock);
996 put_huge_zero_page();
997 inc_mm_counter(mm, MM_ANONPAGES);
999 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1001 ret |= VM_FAULT_WRITE;
1005 spin_unlock(&mm->page_table_lock);
1006 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1007 mem_cgroup_uncharge_page(page);
1012 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1013 struct vm_area_struct *vma,
1014 unsigned long address,
1015 pmd_t *pmd, pmd_t orig_pmd,
1017 unsigned long haddr)
1022 struct page **pages;
1023 unsigned long mmun_start; /* For mmu_notifiers */
1024 unsigned long mmun_end; /* For mmu_notifiers */
1026 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1028 if (unlikely(!pages)) {
1029 ret |= VM_FAULT_OOM;
1033 for (i = 0; i < HPAGE_PMD_NR; i++) {
1034 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1036 vma, address, page_to_nid(page));
1037 if (unlikely(!pages[i] ||
1038 mem_cgroup_newpage_charge(pages[i], mm,
1042 mem_cgroup_uncharge_start();
1044 mem_cgroup_uncharge_page(pages[i]);
1047 mem_cgroup_uncharge_end();
1049 ret |= VM_FAULT_OOM;
1054 for (i = 0; i < HPAGE_PMD_NR; i++) {
1055 copy_user_highpage(pages[i], page + i,
1056 haddr + PAGE_SIZE * i, vma);
1057 __SetPageUptodate(pages[i]);
1062 mmun_end = haddr + HPAGE_PMD_SIZE;
1063 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1065 spin_lock(&mm->page_table_lock);
1066 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1067 goto out_free_pages;
1068 VM_BUG_ON(!PageHead(page));
1070 pmdp_clear_flush(vma, haddr, pmd);
1071 /* leave pmd empty until pte is filled */
1073 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1074 pmd_populate(mm, &_pmd, pgtable);
1076 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1078 entry = mk_pte(pages[i], vma->vm_page_prot);
1079 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1080 page_add_new_anon_rmap(pages[i], vma, haddr);
1081 pte = pte_offset_map(&_pmd, haddr);
1082 VM_BUG_ON(!pte_none(*pte));
1083 set_pte_at(mm, haddr, pte, entry);
1088 smp_wmb(); /* make pte visible before pmd */
1089 pmd_populate(mm, pmd, pgtable);
1090 page_remove_rmap(page);
1091 spin_unlock(&mm->page_table_lock);
1093 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1095 ret |= VM_FAULT_WRITE;
1102 spin_unlock(&mm->page_table_lock);
1103 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1104 mem_cgroup_uncharge_start();
1105 for (i = 0; i < HPAGE_PMD_NR; i++) {
1106 mem_cgroup_uncharge_page(pages[i]);
1109 mem_cgroup_uncharge_end();
1114 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1115 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1118 struct page *page = NULL, *new_page;
1119 unsigned long haddr;
1120 unsigned long mmun_start; /* For mmu_notifiers */
1121 unsigned long mmun_end; /* For mmu_notifiers */
1123 VM_BUG_ON(!vma->anon_vma);
1124 haddr = address & HPAGE_PMD_MASK;
1125 if (is_huge_zero_pmd(orig_pmd))
1127 spin_lock(&mm->page_table_lock);
1128 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1131 page = pmd_page(orig_pmd);
1132 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
1133 if (page_mapcount(page) == 1) {
1135 entry = pmd_mkyoung(orig_pmd);
1136 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1137 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
1138 update_mmu_cache_pmd(vma, address, pmd);
1139 ret |= VM_FAULT_WRITE;
1143 spin_unlock(&mm->page_table_lock);
1145 if (transparent_hugepage_enabled(vma) &&
1146 !transparent_hugepage_debug_cow())
1147 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
1148 vma, haddr, numa_node_id(), 0);
1152 if (unlikely(!new_page)) {
1153 if (is_huge_zero_pmd(orig_pmd)) {
1154 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
1155 address, pmd, orig_pmd, haddr);
1157 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1158 pmd, orig_pmd, page, haddr);
1159 if (ret & VM_FAULT_OOM)
1160 split_huge_page(page);
1163 count_vm_event(THP_FAULT_FALLBACK);
1167 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1170 split_huge_page(page);
1173 count_vm_event(THP_FAULT_FALLBACK);
1174 ret |= VM_FAULT_OOM;
1178 count_vm_event(THP_FAULT_ALLOC);
1180 if (is_huge_zero_pmd(orig_pmd))
1181 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1183 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
1184 __SetPageUptodate(new_page);
1187 mmun_end = haddr + HPAGE_PMD_SIZE;
1188 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1190 spin_lock(&mm->page_table_lock);
1193 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
1194 spin_unlock(&mm->page_table_lock);
1195 mem_cgroup_uncharge_page(new_page);
1200 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1201 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1202 pmdp_clear_flush(vma, haddr, pmd);
1203 page_add_new_anon_rmap(new_page, vma, haddr);
1204 set_pmd_at(mm, haddr, pmd, entry);
1205 update_mmu_cache_pmd(vma, address, pmd);
1206 if (is_huge_zero_pmd(orig_pmd)) {
1207 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
1208 put_huge_zero_page();
1210 VM_BUG_ON(!PageHead(page));
1211 page_remove_rmap(page);
1214 ret |= VM_FAULT_WRITE;
1216 spin_unlock(&mm->page_table_lock);
1218 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1222 spin_unlock(&mm->page_table_lock);
1226 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1231 struct mm_struct *mm = vma->vm_mm;
1232 struct page *page = NULL;
1234 assert_spin_locked(&mm->page_table_lock);
1236 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1239 /* Avoid dumping huge zero page */
1240 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1241 return ERR_PTR(-EFAULT);
1243 page = pmd_page(*pmd);
1244 VM_BUG_ON(!PageHead(page));
1245 if (flags & FOLL_TOUCH) {
1248 * We should set the dirty bit only for FOLL_WRITE but
1249 * for now the dirty bit in the pmd is meaningless.
1250 * And if the dirty bit will become meaningful and
1251 * we'll only set it with FOLL_WRITE, an atomic
1252 * set_bit will be required on the pmd to set the
1253 * young bit, instead of the current set_pmd_at.
1255 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1256 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1258 update_mmu_cache_pmd(vma, addr, pmd);
1260 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1261 if (page->mapping && trylock_page(page)) {
1264 mlock_vma_page(page);
1268 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1269 VM_BUG_ON(!PageCompound(page));
1270 if (flags & FOLL_GET)
1271 get_page_foll(page);
1277 /* NUMA hinting page fault entry point for trans huge pmds */
1278 int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1279 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
1281 struct anon_vma *anon_vma = NULL;
1283 unsigned long haddr = addr & HPAGE_PMD_MASK;
1284 int page_nid = -1, this_nid = numa_node_id();
1287 bool migrated = false;
1289 spin_lock(&mm->page_table_lock);
1290 if (unlikely(!pmd_same(pmd, *pmdp)))
1293 page = pmd_page(pmd);
1294 page_nid = page_to_nid(page);
1295 count_vm_numa_event(NUMA_HINT_FAULTS);
1296 if (page_nid == this_nid)
1297 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
1300 * Acquire the page lock to serialise THP migrations but avoid dropping
1301 * page_table_lock if at all possible
1303 page_locked = trylock_page(page);
1304 target_nid = mpol_misplaced(page, vma, haddr);
1305 if (target_nid == -1) {
1306 /* If the page was locked, there are no parallel migrations */
1312 /* Otherwise wait for potential migrations and retry fault */
1313 spin_unlock(&mm->page_table_lock);
1314 wait_on_page_locked(page);
1318 /* Page is misplaced, serialise migrations and parallel THP splits */
1320 spin_unlock(&mm->page_table_lock);
1325 anon_vma = page_lock_anon_vma_read(page);
1327 /* Confirm the PMD did not change while page_table_lock was released */
1328 spin_lock(&mm->page_table_lock);
1329 if (unlikely(!pmd_same(pmd, *pmdp))) {
1335 /* Migrate the THP to the requested node */
1336 spin_unlock(&mm->page_table_lock);
1337 migrated = migrate_misplaced_transhuge_page(mm, vma,
1338 pmdp, pmd, addr, page, target_nid);
1340 page_nid = target_nid;
1347 spin_lock(&mm->page_table_lock);
1348 if (unlikely(!pmd_same(pmd, *pmdp))) {
1349 /* Someone else took our fault */
1354 pmd = pmd_mknonnuma(pmd);
1355 set_pmd_at(mm, haddr, pmdp, pmd);
1356 VM_BUG_ON(pmd_numa(*pmdp));
1357 update_mmu_cache_pmd(vma, addr, pmdp);
1359 spin_unlock(&mm->page_table_lock);
1363 page_unlock_anon_vma_read(anon_vma);
1366 task_numa_fault(page_nid, HPAGE_PMD_NR, migrated);
1371 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1372 pmd_t *pmd, unsigned long addr)
1376 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1381 * For architectures like ppc64 we look at deposited pgtable
1382 * when calling pmdp_get_and_clear. So do the
1383 * pgtable_trans_huge_withdraw after finishing pmdp related
1386 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1387 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1388 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
1389 if (is_huge_zero_pmd(orig_pmd)) {
1391 spin_unlock(&tlb->mm->page_table_lock);
1392 put_huge_zero_page();
1394 page = pmd_page(orig_pmd);
1395 page_remove_rmap(page);
1396 VM_BUG_ON(page_mapcount(page) < 0);
1397 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1398 VM_BUG_ON(!PageHead(page));
1400 spin_unlock(&tlb->mm->page_table_lock);
1401 tlb_remove_page(tlb, page);
1403 pte_free(tlb->mm, pgtable);
1409 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1410 unsigned long addr, unsigned long end,
1415 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1417 * All logical pages in the range are present
1418 * if backed by a huge page.
1420 spin_unlock(&vma->vm_mm->page_table_lock);
1421 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1428 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1429 unsigned long old_addr,
1430 unsigned long new_addr, unsigned long old_end,
1431 pmd_t *old_pmd, pmd_t *new_pmd)
1436 struct mm_struct *mm = vma->vm_mm;
1438 if ((old_addr & ~HPAGE_PMD_MASK) ||
1439 (new_addr & ~HPAGE_PMD_MASK) ||
1440 old_end - old_addr < HPAGE_PMD_SIZE ||
1441 (new_vma->vm_flags & VM_NOHUGEPAGE))
1445 * The destination pmd shouldn't be established, free_pgtables()
1446 * should have release it.
1448 if (WARN_ON(!pmd_none(*new_pmd))) {
1449 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1453 ret = __pmd_trans_huge_lock(old_pmd, vma);
1455 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1456 VM_BUG_ON(!pmd_none(*new_pmd));
1457 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1458 spin_unlock(&mm->page_table_lock);
1464 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1465 unsigned long addr, pgprot_t newprot, int prot_numa)
1467 struct mm_struct *mm = vma->vm_mm;
1470 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1472 entry = pmdp_get_and_clear(mm, addr, pmd);
1474 entry = pmd_modify(entry, newprot);
1475 BUG_ON(pmd_write(entry));
1477 struct page *page = pmd_page(*pmd);
1479 /* only check non-shared pages */
1480 if (page_mapcount(page) == 1 &&
1482 entry = pmd_mknuma(entry);
1485 set_pmd_at(mm, addr, pmd, entry);
1486 spin_unlock(&vma->vm_mm->page_table_lock);
1494 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1495 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1497 * Note that if it returns 1, this routine returns without unlocking page
1498 * table locks. So callers must unlock them.
1500 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1502 spin_lock(&vma->vm_mm->page_table_lock);
1503 if (likely(pmd_trans_huge(*pmd))) {
1504 if (unlikely(pmd_trans_splitting(*pmd))) {
1505 spin_unlock(&vma->vm_mm->page_table_lock);
1506 wait_split_huge_page(vma->anon_vma, pmd);
1509 /* Thp mapped by 'pmd' is stable, so we can
1510 * handle it as it is. */
1514 spin_unlock(&vma->vm_mm->page_table_lock);
1518 pmd_t *page_check_address_pmd(struct page *page,
1519 struct mm_struct *mm,
1520 unsigned long address,
1521 enum page_check_address_pmd_flag flag)
1523 pmd_t *pmd, *ret = NULL;
1525 if (address & ~HPAGE_PMD_MASK)
1528 pmd = mm_find_pmd(mm, address);
1533 if (pmd_page(*pmd) != page)
1536 * split_vma() may create temporary aliased mappings. There is
1537 * no risk as long as all huge pmd are found and have their
1538 * splitting bit set before __split_huge_page_refcount
1539 * runs. Finding the same huge pmd more than once during the
1540 * same rmap walk is not a problem.
1542 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1543 pmd_trans_splitting(*pmd))
1545 if (pmd_trans_huge(*pmd)) {
1546 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1547 !pmd_trans_splitting(*pmd));
1554 static int __split_huge_page_splitting(struct page *page,
1555 struct vm_area_struct *vma,
1556 unsigned long address)
1558 struct mm_struct *mm = vma->vm_mm;
1561 /* For mmu_notifiers */
1562 const unsigned long mmun_start = address;
1563 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1565 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1566 spin_lock(&mm->page_table_lock);
1567 pmd = page_check_address_pmd(page, mm, address,
1568 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1571 * We can't temporarily set the pmd to null in order
1572 * to split it, the pmd must remain marked huge at all
1573 * times or the VM won't take the pmd_trans_huge paths
1574 * and it won't wait on the anon_vma->root->rwsem to
1575 * serialize against split_huge_page*.
1577 pmdp_splitting_flush(vma, address, pmd);
1580 spin_unlock(&mm->page_table_lock);
1581 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1586 static void __split_huge_page_refcount(struct page *page,
1587 struct list_head *list)
1590 struct zone *zone = page_zone(page);
1591 struct lruvec *lruvec;
1594 /* prevent PageLRU to go away from under us, and freeze lru stats */
1595 spin_lock_irq(&zone->lru_lock);
1596 lruvec = mem_cgroup_page_lruvec(page, zone);
1598 compound_lock(page);
1599 /* complete memcg works before add pages to LRU */
1600 mem_cgroup_split_huge_fixup(page);
1602 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1603 struct page *page_tail = page + i;
1605 /* tail_page->_mapcount cannot change */
1606 BUG_ON(page_mapcount(page_tail) < 0);
1607 tail_count += page_mapcount(page_tail);
1608 /* check for overflow */
1609 BUG_ON(tail_count < 0);
1610 BUG_ON(atomic_read(&page_tail->_count) != 0);
1612 * tail_page->_count is zero and not changing from
1613 * under us. But get_page_unless_zero() may be running
1614 * from under us on the tail_page. If we used
1615 * atomic_set() below instead of atomic_add(), we
1616 * would then run atomic_set() concurrently with
1617 * get_page_unless_zero(), and atomic_set() is
1618 * implemented in C not using locked ops. spin_unlock
1619 * on x86 sometime uses locked ops because of PPro
1620 * errata 66, 92, so unless somebody can guarantee
1621 * atomic_set() here would be safe on all archs (and
1622 * not only on x86), it's safer to use atomic_add().
1624 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1625 &page_tail->_count);
1627 /* after clearing PageTail the gup refcount can be released */
1631 * retain hwpoison flag of the poisoned tail page:
1632 * fix for the unsuitable process killed on Guest Machine(KVM)
1633 * by the memory-failure.
1635 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1636 page_tail->flags |= (page->flags &
1637 ((1L << PG_referenced) |
1638 (1L << PG_swapbacked) |
1639 (1L << PG_mlocked) |
1640 (1L << PG_uptodate) |
1642 (1L << PG_unevictable)));
1643 page_tail->flags |= (1L << PG_dirty);
1645 /* clear PageTail before overwriting first_page */
1649 * __split_huge_page_splitting() already set the
1650 * splitting bit in all pmd that could map this
1651 * hugepage, that will ensure no CPU can alter the
1652 * mapcount on the head page. The mapcount is only
1653 * accounted in the head page and it has to be
1654 * transferred to all tail pages in the below code. So
1655 * for this code to be safe, the split the mapcount
1656 * can't change. But that doesn't mean userland can't
1657 * keep changing and reading the page contents while
1658 * we transfer the mapcount, so the pmd splitting
1659 * status is achieved setting a reserved bit in the
1660 * pmd, not by clearing the present bit.
1662 page_tail->_mapcount = page->_mapcount;
1664 BUG_ON(page_tail->mapping);
1665 page_tail->mapping = page->mapping;
1667 page_tail->index = page->index + i;
1668 page_nid_xchg_last(page_tail, page_nid_last(page));
1670 BUG_ON(!PageAnon(page_tail));
1671 BUG_ON(!PageUptodate(page_tail));
1672 BUG_ON(!PageDirty(page_tail));
1673 BUG_ON(!PageSwapBacked(page_tail));
1675 lru_add_page_tail(page, page_tail, lruvec, list);
1677 atomic_sub(tail_count, &page->_count);
1678 BUG_ON(atomic_read(&page->_count) <= 0);
1680 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1682 ClearPageCompound(page);
1683 compound_unlock(page);
1684 spin_unlock_irq(&zone->lru_lock);
1686 for (i = 1; i < HPAGE_PMD_NR; i++) {
1687 struct page *page_tail = page + i;
1688 BUG_ON(page_count(page_tail) <= 0);
1690 * Tail pages may be freed if there wasn't any mapping
1691 * like if add_to_swap() is running on a lru page that
1692 * had its mapping zapped. And freeing these pages
1693 * requires taking the lru_lock so we do the put_page
1694 * of the tail pages after the split is complete.
1696 put_page(page_tail);
1700 * Only the head page (now become a regular page) is required
1701 * to be pinned by the caller.
1703 BUG_ON(page_count(page) <= 0);
1706 static int __split_huge_page_map(struct page *page,
1707 struct vm_area_struct *vma,
1708 unsigned long address)
1710 struct mm_struct *mm = vma->vm_mm;
1714 unsigned long haddr;
1716 spin_lock(&mm->page_table_lock);
1717 pmd = page_check_address_pmd(page, mm, address,
1718 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1720 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1721 pmd_populate(mm, &_pmd, pgtable);
1724 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1726 BUG_ON(PageCompound(page+i));
1727 entry = mk_pte(page + i, vma->vm_page_prot);
1728 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1729 if (!pmd_write(*pmd))
1730 entry = pte_wrprotect(entry);
1732 BUG_ON(page_mapcount(page) != 1);
1733 if (!pmd_young(*pmd))
1734 entry = pte_mkold(entry);
1736 entry = pte_mknuma(entry);
1737 pte = pte_offset_map(&_pmd, haddr);
1738 BUG_ON(!pte_none(*pte));
1739 set_pte_at(mm, haddr, pte, entry);
1743 smp_wmb(); /* make pte visible before pmd */
1745 * Up to this point the pmd is present and huge and
1746 * userland has the whole access to the hugepage
1747 * during the split (which happens in place). If we
1748 * overwrite the pmd with the not-huge version
1749 * pointing to the pte here (which of course we could
1750 * if all CPUs were bug free), userland could trigger
1751 * a small page size TLB miss on the small sized TLB
1752 * while the hugepage TLB entry is still established
1753 * in the huge TLB. Some CPU doesn't like that. See
1754 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1755 * Erratum 383 on page 93. Intel should be safe but is
1756 * also warns that it's only safe if the permission
1757 * and cache attributes of the two entries loaded in
1758 * the two TLB is identical (which should be the case
1759 * here). But it is generally safer to never allow
1760 * small and huge TLB entries for the same virtual
1761 * address to be loaded simultaneously. So instead of
1762 * doing "pmd_populate(); flush_tlb_range();" we first
1763 * mark the current pmd notpresent (atomically because
1764 * here the pmd_trans_huge and pmd_trans_splitting
1765 * must remain set at all times on the pmd until the
1766 * split is complete for this pmd), then we flush the
1767 * SMP TLB and finally we write the non-huge version
1768 * of the pmd entry with pmd_populate.
1770 pmdp_invalidate(vma, address, pmd);
1771 pmd_populate(mm, pmd, pgtable);
1774 spin_unlock(&mm->page_table_lock);
1779 /* must be called with anon_vma->root->rwsem held */
1780 static void __split_huge_page(struct page *page,
1781 struct anon_vma *anon_vma,
1782 struct list_head *list)
1784 int mapcount, mapcount2;
1785 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1786 struct anon_vma_chain *avc;
1788 BUG_ON(!PageHead(page));
1789 BUG_ON(PageTail(page));
1792 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1793 struct vm_area_struct *vma = avc->vma;
1794 unsigned long addr = vma_address(page, vma);
1795 BUG_ON(is_vma_temporary_stack(vma));
1796 mapcount += __split_huge_page_splitting(page, vma, addr);
1799 * It is critical that new vmas are added to the tail of the
1800 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1801 * and establishes a child pmd before
1802 * __split_huge_page_splitting() freezes the parent pmd (so if
1803 * we fail to prevent copy_huge_pmd() from running until the
1804 * whole __split_huge_page() is complete), we will still see
1805 * the newly established pmd of the child later during the
1806 * walk, to be able to set it as pmd_trans_splitting too.
1808 if (mapcount != page_mapcount(page))
1809 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1810 mapcount, page_mapcount(page));
1811 BUG_ON(mapcount != page_mapcount(page));
1813 __split_huge_page_refcount(page, list);
1816 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1817 struct vm_area_struct *vma = avc->vma;
1818 unsigned long addr = vma_address(page, vma);
1819 BUG_ON(is_vma_temporary_stack(vma));
1820 mapcount2 += __split_huge_page_map(page, vma, addr);
1822 if (mapcount != mapcount2)
1823 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1824 mapcount, mapcount2, page_mapcount(page));
1825 BUG_ON(mapcount != mapcount2);
1829 * Split a hugepage into normal pages. This doesn't change the position of head
1830 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1831 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1832 * from the hugepage.
1833 * Return 0 if the hugepage is split successfully otherwise return 1.
1835 int split_huge_page_to_list(struct page *page, struct list_head *list)
1837 struct anon_vma *anon_vma;
1840 BUG_ON(is_huge_zero_page(page));
1841 BUG_ON(!PageAnon(page));
1844 * The caller does not necessarily hold an mmap_sem that would prevent
1845 * the anon_vma disappearing so we first we take a reference to it
1846 * and then lock the anon_vma for write. This is similar to
1847 * page_lock_anon_vma_read except the write lock is taken to serialise
1848 * against parallel split or collapse operations.
1850 anon_vma = page_get_anon_vma(page);
1853 anon_vma_lock_write(anon_vma);
1856 if (!PageCompound(page))
1859 BUG_ON(!PageSwapBacked(page));
1860 __split_huge_page(page, anon_vma, list);
1861 count_vm_event(THP_SPLIT);
1863 BUG_ON(PageCompound(page));
1865 anon_vma_unlock_write(anon_vma);
1866 put_anon_vma(anon_vma);
1871 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1873 int hugepage_madvise(struct vm_area_struct *vma,
1874 unsigned long *vm_flags, int advice)
1876 struct mm_struct *mm = vma->vm_mm;
1881 * Be somewhat over-protective like KSM for now!
1883 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1885 if (mm->def_flags & VM_NOHUGEPAGE)
1887 *vm_flags &= ~VM_NOHUGEPAGE;
1888 *vm_flags |= VM_HUGEPAGE;
1890 * If the vma become good for khugepaged to scan,
1891 * register it here without waiting a page fault that
1892 * may not happen any time soon.
1894 if (unlikely(khugepaged_enter_vma_merge(vma)))
1897 case MADV_NOHUGEPAGE:
1899 * Be somewhat over-protective like KSM for now!
1901 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1903 *vm_flags &= ~VM_HUGEPAGE;
1904 *vm_flags |= VM_NOHUGEPAGE;
1906 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1907 * this vma even if we leave the mm registered in khugepaged if
1908 * it got registered before VM_NOHUGEPAGE was set.
1916 static int __init khugepaged_slab_init(void)
1918 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1919 sizeof(struct mm_slot),
1920 __alignof__(struct mm_slot), 0, NULL);
1927 static inline struct mm_slot *alloc_mm_slot(void)
1929 if (!mm_slot_cache) /* initialization failed */
1931 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1934 static inline void free_mm_slot(struct mm_slot *mm_slot)
1936 kmem_cache_free(mm_slot_cache, mm_slot);
1939 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1941 struct mm_slot *mm_slot;
1943 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
1944 if (mm == mm_slot->mm)
1950 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1951 struct mm_slot *mm_slot)
1954 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
1957 static inline int khugepaged_test_exit(struct mm_struct *mm)
1959 return atomic_read(&mm->mm_users) == 0;
1962 int __khugepaged_enter(struct mm_struct *mm)
1964 struct mm_slot *mm_slot;
1967 mm_slot = alloc_mm_slot();
1971 /* __khugepaged_exit() must not run from under us */
1972 VM_BUG_ON(khugepaged_test_exit(mm));
1973 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1974 free_mm_slot(mm_slot);
1978 spin_lock(&khugepaged_mm_lock);
1979 insert_to_mm_slots_hash(mm, mm_slot);
1981 * Insert just behind the scanning cursor, to let the area settle
1984 wakeup = list_empty(&khugepaged_scan.mm_head);
1985 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1986 spin_unlock(&khugepaged_mm_lock);
1988 atomic_inc(&mm->mm_count);
1990 wake_up_interruptible(&khugepaged_wait);
1995 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1997 unsigned long hstart, hend;
2000 * Not yet faulted in so we will register later in the
2001 * page fault if needed.
2005 /* khugepaged not yet working on file or special mappings */
2007 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2008 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2009 hend = vma->vm_end & HPAGE_PMD_MASK;
2011 return khugepaged_enter(vma);
2015 void __khugepaged_exit(struct mm_struct *mm)
2017 struct mm_slot *mm_slot;
2020 spin_lock(&khugepaged_mm_lock);
2021 mm_slot = get_mm_slot(mm);
2022 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
2023 hash_del(&mm_slot->hash);
2024 list_del(&mm_slot->mm_node);
2027 spin_unlock(&khugepaged_mm_lock);
2030 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2031 free_mm_slot(mm_slot);
2033 } else if (mm_slot) {
2035 * This is required to serialize against
2036 * khugepaged_test_exit() (which is guaranteed to run
2037 * under mmap sem read mode). Stop here (after we
2038 * return all pagetables will be destroyed) until
2039 * khugepaged has finished working on the pagetables
2040 * under the mmap_sem.
2042 down_write(&mm->mmap_sem);
2043 up_write(&mm->mmap_sem);
2047 static void release_pte_page(struct page *page)
2049 /* 0 stands for page_is_file_cache(page) == false */
2050 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2052 putback_lru_page(page);
2055 static void release_pte_pages(pte_t *pte, pte_t *_pte)
2057 while (--_pte >= pte) {
2058 pte_t pteval = *_pte;
2059 if (!pte_none(pteval))
2060 release_pte_page(pte_page(pteval));
2064 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2065 unsigned long address,
2070 int referenced = 0, none = 0;
2071 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2072 _pte++, address += PAGE_SIZE) {
2073 pte_t pteval = *_pte;
2074 if (pte_none(pteval)) {
2075 if (++none <= khugepaged_max_ptes_none)
2080 if (!pte_present(pteval) || !pte_write(pteval))
2082 page = vm_normal_page(vma, address, pteval);
2083 if (unlikely(!page))
2086 VM_BUG_ON(PageCompound(page));
2087 BUG_ON(!PageAnon(page));
2088 VM_BUG_ON(!PageSwapBacked(page));
2090 /* cannot use mapcount: can't collapse if there's a gup pin */
2091 if (page_count(page) != 1)
2094 * We can do it before isolate_lru_page because the
2095 * page can't be freed from under us. NOTE: PG_lock
2096 * is needed to serialize against split_huge_page
2097 * when invoked from the VM.
2099 if (!trylock_page(page))
2102 * Isolate the page to avoid collapsing an hugepage
2103 * currently in use by the VM.
2105 if (isolate_lru_page(page)) {
2109 /* 0 stands for page_is_file_cache(page) == false */
2110 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2111 VM_BUG_ON(!PageLocked(page));
2112 VM_BUG_ON(PageLRU(page));
2114 /* If there is no mapped pte young don't collapse the page */
2115 if (pte_young(pteval) || PageReferenced(page) ||
2116 mmu_notifier_test_young(vma->vm_mm, address))
2119 if (likely(referenced))
2122 release_pte_pages(pte, _pte);
2126 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2127 struct vm_area_struct *vma,
2128 unsigned long address,
2132 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2133 pte_t pteval = *_pte;
2134 struct page *src_page;
2136 if (pte_none(pteval)) {
2137 clear_user_highpage(page, address);
2138 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2140 src_page = pte_page(pteval);
2141 copy_user_highpage(page, src_page, address, vma);
2142 VM_BUG_ON(page_mapcount(src_page) != 1);
2143 release_pte_page(src_page);
2145 * ptl mostly unnecessary, but preempt has to
2146 * be disabled to update the per-cpu stats
2147 * inside page_remove_rmap().
2151 * paravirt calls inside pte_clear here are
2154 pte_clear(vma->vm_mm, address, _pte);
2155 page_remove_rmap(src_page);
2157 free_page_and_swap_cache(src_page);
2160 address += PAGE_SIZE;
2165 static void khugepaged_alloc_sleep(void)
2167 wait_event_freezable_timeout(khugepaged_wait, false,
2168 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2172 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2174 if (IS_ERR(*hpage)) {
2180 khugepaged_alloc_sleep();
2181 } else if (*hpage) {
2190 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2191 struct vm_area_struct *vma, unsigned long address,
2196 * Allocate the page while the vma is still valid and under
2197 * the mmap_sem read mode so there is no memory allocation
2198 * later when we take the mmap_sem in write mode. This is more
2199 * friendly behavior (OTOH it may actually hide bugs) to
2200 * filesystems in userland with daemons allocating memory in
2201 * the userland I/O paths. Allocating memory with the
2202 * mmap_sem in read mode is good idea also to allow greater
2205 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2206 node, __GFP_OTHER_NODE);
2209 * After allocating the hugepage, release the mmap_sem read lock in
2210 * preparation for taking it in write mode.
2212 up_read(&mm->mmap_sem);
2213 if (unlikely(!*hpage)) {
2214 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2215 *hpage = ERR_PTR(-ENOMEM);
2219 count_vm_event(THP_COLLAPSE_ALLOC);
2223 static struct page *khugepaged_alloc_hugepage(bool *wait)
2228 hpage = alloc_hugepage(khugepaged_defrag());
2230 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2235 khugepaged_alloc_sleep();
2237 count_vm_event(THP_COLLAPSE_ALLOC);
2238 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2243 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2246 *hpage = khugepaged_alloc_hugepage(wait);
2248 if (unlikely(!*hpage))
2255 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2256 struct vm_area_struct *vma, unsigned long address,
2259 up_read(&mm->mmap_sem);
2265 static bool hugepage_vma_check(struct vm_area_struct *vma)
2267 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2268 (vma->vm_flags & VM_NOHUGEPAGE))
2271 if (!vma->anon_vma || vma->vm_ops)
2273 if (is_vma_temporary_stack(vma))
2275 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2279 static void collapse_huge_page(struct mm_struct *mm,
2280 unsigned long address,
2281 struct page **hpage,
2282 struct vm_area_struct *vma,
2288 struct page *new_page;
2291 unsigned long hstart, hend;
2292 unsigned long mmun_start; /* For mmu_notifiers */
2293 unsigned long mmun_end; /* For mmu_notifiers */
2295 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2297 /* release the mmap_sem read lock. */
2298 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2302 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
2306 * Prevent all access to pagetables with the exception of
2307 * gup_fast later hanlded by the ptep_clear_flush and the VM
2308 * handled by the anon_vma lock + PG_lock.
2310 down_write(&mm->mmap_sem);
2311 if (unlikely(khugepaged_test_exit(mm)))
2314 vma = find_vma(mm, address);
2317 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2318 hend = vma->vm_end & HPAGE_PMD_MASK;
2319 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2321 if (!hugepage_vma_check(vma))
2323 pmd = mm_find_pmd(mm, address);
2326 if (pmd_trans_huge(*pmd))
2329 anon_vma_lock_write(vma->anon_vma);
2331 pte = pte_offset_map(pmd, address);
2332 ptl = pte_lockptr(mm, pmd);
2334 mmun_start = address;
2335 mmun_end = address + HPAGE_PMD_SIZE;
2336 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2337 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2339 * After this gup_fast can't run anymore. This also removes
2340 * any huge TLB entry from the CPU so we won't allow
2341 * huge and small TLB entries for the same virtual address
2342 * to avoid the risk of CPU bugs in that area.
2344 _pmd = pmdp_clear_flush(vma, address, pmd);
2345 spin_unlock(&mm->page_table_lock);
2346 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2349 isolated = __collapse_huge_page_isolate(vma, address, pte);
2352 if (unlikely(!isolated)) {
2354 spin_lock(&mm->page_table_lock);
2355 BUG_ON(!pmd_none(*pmd));
2357 * We can only use set_pmd_at when establishing
2358 * hugepmds and never for establishing regular pmds that
2359 * points to regular pagetables. Use pmd_populate for that
2361 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
2362 spin_unlock(&mm->page_table_lock);
2363 anon_vma_unlock_write(vma->anon_vma);
2368 * All pages are isolated and locked so anon_vma rmap
2369 * can't run anymore.
2371 anon_vma_unlock_write(vma->anon_vma);
2373 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2375 __SetPageUptodate(new_page);
2376 pgtable = pmd_pgtable(_pmd);
2378 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2379 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
2382 * spin_lock() below is not the equivalent of smp_wmb(), so
2383 * this is needed to avoid the copy_huge_page writes to become
2384 * visible after the set_pmd_at() write.
2388 spin_lock(&mm->page_table_lock);
2389 BUG_ON(!pmd_none(*pmd));
2390 page_add_new_anon_rmap(new_page, vma, address);
2391 pgtable_trans_huge_deposit(mm, pmd, pgtable);
2392 set_pmd_at(mm, address, pmd, _pmd);
2393 update_mmu_cache_pmd(vma, address, pmd);
2394 spin_unlock(&mm->page_table_lock);
2398 khugepaged_pages_collapsed++;
2400 up_write(&mm->mmap_sem);
2404 mem_cgroup_uncharge_page(new_page);
2408 static int khugepaged_scan_pmd(struct mm_struct *mm,
2409 struct vm_area_struct *vma,
2410 unsigned long address,
2411 struct page **hpage)
2415 int ret = 0, referenced = 0, none = 0;
2417 unsigned long _address;
2419 int node = NUMA_NO_NODE;
2421 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2423 pmd = mm_find_pmd(mm, address);
2426 if (pmd_trans_huge(*pmd))
2429 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2430 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2431 _pte++, _address += PAGE_SIZE) {
2432 pte_t pteval = *_pte;
2433 if (pte_none(pteval)) {
2434 if (++none <= khugepaged_max_ptes_none)
2439 if (!pte_present(pteval) || !pte_write(pteval))
2441 page = vm_normal_page(vma, _address, pteval);
2442 if (unlikely(!page))
2445 * Chose the node of the first page. This could
2446 * be more sophisticated and look at more pages,
2447 * but isn't for now.
2449 if (node == NUMA_NO_NODE)
2450 node = page_to_nid(page);
2451 VM_BUG_ON(PageCompound(page));
2452 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2454 /* cannot use mapcount: can't collapse if there's a gup pin */
2455 if (page_count(page) != 1)
2457 if (pte_young(pteval) || PageReferenced(page) ||
2458 mmu_notifier_test_young(vma->vm_mm, address))
2464 pte_unmap_unlock(pte, ptl);
2466 /* collapse_huge_page will return with the mmap_sem released */
2467 collapse_huge_page(mm, address, hpage, vma, node);
2472 static void collect_mm_slot(struct mm_slot *mm_slot)
2474 struct mm_struct *mm = mm_slot->mm;
2476 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2478 if (khugepaged_test_exit(mm)) {
2480 hash_del(&mm_slot->hash);
2481 list_del(&mm_slot->mm_node);
2484 * Not strictly needed because the mm exited already.
2486 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2489 /* khugepaged_mm_lock actually not necessary for the below */
2490 free_mm_slot(mm_slot);
2495 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2496 struct page **hpage)
2497 __releases(&khugepaged_mm_lock)
2498 __acquires(&khugepaged_mm_lock)
2500 struct mm_slot *mm_slot;
2501 struct mm_struct *mm;
2502 struct vm_area_struct *vma;
2506 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2508 if (khugepaged_scan.mm_slot)
2509 mm_slot = khugepaged_scan.mm_slot;
2511 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2512 struct mm_slot, mm_node);
2513 khugepaged_scan.address = 0;
2514 khugepaged_scan.mm_slot = mm_slot;
2516 spin_unlock(&khugepaged_mm_lock);
2519 down_read(&mm->mmap_sem);
2520 if (unlikely(khugepaged_test_exit(mm)))
2523 vma = find_vma(mm, khugepaged_scan.address);
2526 for (; vma; vma = vma->vm_next) {
2527 unsigned long hstart, hend;
2530 if (unlikely(khugepaged_test_exit(mm))) {
2534 if (!hugepage_vma_check(vma)) {
2539 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2540 hend = vma->vm_end & HPAGE_PMD_MASK;
2543 if (khugepaged_scan.address > hend)
2545 if (khugepaged_scan.address < hstart)
2546 khugepaged_scan.address = hstart;
2547 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2549 while (khugepaged_scan.address < hend) {
2552 if (unlikely(khugepaged_test_exit(mm)))
2553 goto breakouterloop;
2555 VM_BUG_ON(khugepaged_scan.address < hstart ||
2556 khugepaged_scan.address + HPAGE_PMD_SIZE >
2558 ret = khugepaged_scan_pmd(mm, vma,
2559 khugepaged_scan.address,
2561 /* move to next address */
2562 khugepaged_scan.address += HPAGE_PMD_SIZE;
2563 progress += HPAGE_PMD_NR;
2565 /* we released mmap_sem so break loop */
2566 goto breakouterloop_mmap_sem;
2567 if (progress >= pages)
2568 goto breakouterloop;
2572 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2573 breakouterloop_mmap_sem:
2575 spin_lock(&khugepaged_mm_lock);
2576 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2578 * Release the current mm_slot if this mm is about to die, or
2579 * if we scanned all vmas of this mm.
2581 if (khugepaged_test_exit(mm) || !vma) {
2583 * Make sure that if mm_users is reaching zero while
2584 * khugepaged runs here, khugepaged_exit will find
2585 * mm_slot not pointing to the exiting mm.
2587 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2588 khugepaged_scan.mm_slot = list_entry(
2589 mm_slot->mm_node.next,
2590 struct mm_slot, mm_node);
2591 khugepaged_scan.address = 0;
2593 khugepaged_scan.mm_slot = NULL;
2594 khugepaged_full_scans++;
2597 collect_mm_slot(mm_slot);
2603 static int khugepaged_has_work(void)
2605 return !list_empty(&khugepaged_scan.mm_head) &&
2606 khugepaged_enabled();
2609 static int khugepaged_wait_event(void)
2611 return !list_empty(&khugepaged_scan.mm_head) ||
2612 kthread_should_stop();
2615 static void khugepaged_do_scan(void)
2617 struct page *hpage = NULL;
2618 unsigned int progress = 0, pass_through_head = 0;
2619 unsigned int pages = khugepaged_pages_to_scan;
2622 barrier(); /* write khugepaged_pages_to_scan to local stack */
2624 while (progress < pages) {
2625 if (!khugepaged_prealloc_page(&hpage, &wait))
2630 if (unlikely(kthread_should_stop() || freezing(current)))
2633 spin_lock(&khugepaged_mm_lock);
2634 if (!khugepaged_scan.mm_slot)
2635 pass_through_head++;
2636 if (khugepaged_has_work() &&
2637 pass_through_head < 2)
2638 progress += khugepaged_scan_mm_slot(pages - progress,
2642 spin_unlock(&khugepaged_mm_lock);
2645 if (!IS_ERR_OR_NULL(hpage))
2649 static void khugepaged_wait_work(void)
2653 if (khugepaged_has_work()) {
2654 if (!khugepaged_scan_sleep_millisecs)
2657 wait_event_freezable_timeout(khugepaged_wait,
2658 kthread_should_stop(),
2659 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2663 if (khugepaged_enabled())
2664 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2667 static int khugepaged(void *none)
2669 struct mm_slot *mm_slot;
2672 set_user_nice(current, 19);
2674 while (!kthread_should_stop()) {
2675 khugepaged_do_scan();
2676 khugepaged_wait_work();
2679 spin_lock(&khugepaged_mm_lock);
2680 mm_slot = khugepaged_scan.mm_slot;
2681 khugepaged_scan.mm_slot = NULL;
2683 collect_mm_slot(mm_slot);
2684 spin_unlock(&khugepaged_mm_lock);
2688 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2689 unsigned long haddr, pmd_t *pmd)
2691 struct mm_struct *mm = vma->vm_mm;
2696 pmdp_clear_flush(vma, haddr, pmd);
2697 /* leave pmd empty until pte is filled */
2699 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2700 pmd_populate(mm, &_pmd, pgtable);
2702 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2704 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2705 entry = pte_mkspecial(entry);
2706 pte = pte_offset_map(&_pmd, haddr);
2707 VM_BUG_ON(!pte_none(*pte));
2708 set_pte_at(mm, haddr, pte, entry);
2711 smp_wmb(); /* make pte visible before pmd */
2712 pmd_populate(mm, pmd, pgtable);
2713 put_huge_zero_page();
2716 void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2720 struct mm_struct *mm = vma->vm_mm;
2721 unsigned long haddr = address & HPAGE_PMD_MASK;
2722 unsigned long mmun_start; /* For mmu_notifiers */
2723 unsigned long mmun_end; /* For mmu_notifiers */
2725 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
2728 mmun_end = haddr + HPAGE_PMD_SIZE;
2729 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2730 spin_lock(&mm->page_table_lock);
2731 if (unlikely(!pmd_trans_huge(*pmd))) {
2732 spin_unlock(&mm->page_table_lock);
2733 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2736 if (is_huge_zero_pmd(*pmd)) {
2737 __split_huge_zero_page_pmd(vma, haddr, pmd);
2738 spin_unlock(&mm->page_table_lock);
2739 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2742 page = pmd_page(*pmd);
2743 VM_BUG_ON(!page_count(page));
2745 spin_unlock(&mm->page_table_lock);
2746 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2748 split_huge_page(page);
2751 BUG_ON(pmd_trans_huge(*pmd));
2754 void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2757 struct vm_area_struct *vma;
2759 vma = find_vma(mm, address);
2760 BUG_ON(vma == NULL);
2761 split_huge_page_pmd(vma, address, pmd);
2764 static void split_huge_page_address(struct mm_struct *mm,
2765 unsigned long address)
2769 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2771 pmd = mm_find_pmd(mm, address);
2775 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2776 * materialize from under us.
2778 split_huge_page_pmd_mm(mm, address, pmd);
2781 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2782 unsigned long start,
2787 * If the new start address isn't hpage aligned and it could
2788 * previously contain an hugepage: check if we need to split
2791 if (start & ~HPAGE_PMD_MASK &&
2792 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2793 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2794 split_huge_page_address(vma->vm_mm, start);
2797 * If the new end address isn't hpage aligned and it could
2798 * previously contain an hugepage: check if we need to split
2801 if (end & ~HPAGE_PMD_MASK &&
2802 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2803 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2804 split_huge_page_address(vma->vm_mm, end);
2807 * If we're also updating the vma->vm_next->vm_start, if the new
2808 * vm_next->vm_start isn't page aligned and it could previously
2809 * contain an hugepage: check if we need to split an huge pmd.
2811 if (adjust_next > 0) {
2812 struct vm_area_struct *next = vma->vm_next;
2813 unsigned long nstart = next->vm_start;
2814 nstart += adjust_next << PAGE_SHIFT;
2815 if (nstart & ~HPAGE_PMD_MASK &&
2816 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2817 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2818 split_huge_page_address(next->vm_mm, nstart);