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 int shrink_huge_zero_page(struct shrinker *shrink,
215 struct shrink_control *sc)
218 /* we can free zero page only if last reference remains */
219 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
221 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
222 struct page *zero_page = xchg(&huge_zero_page, NULL);
223 BUG_ON(zero_page == NULL);
224 __free_page(zero_page);
230 static struct shrinker huge_zero_page_shrinker = {
231 .shrink = shrink_huge_zero_page,
232 .seeks = DEFAULT_SEEKS,
237 static ssize_t double_flag_show(struct kobject *kobj,
238 struct kobj_attribute *attr, char *buf,
239 enum transparent_hugepage_flag enabled,
240 enum transparent_hugepage_flag req_madv)
242 if (test_bit(enabled, &transparent_hugepage_flags)) {
243 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
244 return sprintf(buf, "[always] madvise never\n");
245 } else if (test_bit(req_madv, &transparent_hugepage_flags))
246 return sprintf(buf, "always [madvise] never\n");
248 return sprintf(buf, "always madvise [never]\n");
250 static ssize_t double_flag_store(struct kobject *kobj,
251 struct kobj_attribute *attr,
252 const char *buf, size_t count,
253 enum transparent_hugepage_flag enabled,
254 enum transparent_hugepage_flag req_madv)
256 if (!memcmp("always", buf,
257 min(sizeof("always")-1, count))) {
258 set_bit(enabled, &transparent_hugepage_flags);
259 clear_bit(req_madv, &transparent_hugepage_flags);
260 } else if (!memcmp("madvise", buf,
261 min(sizeof("madvise")-1, count))) {
262 clear_bit(enabled, &transparent_hugepage_flags);
263 set_bit(req_madv, &transparent_hugepage_flags);
264 } else if (!memcmp("never", buf,
265 min(sizeof("never")-1, count))) {
266 clear_bit(enabled, &transparent_hugepage_flags);
267 clear_bit(req_madv, &transparent_hugepage_flags);
274 static ssize_t enabled_show(struct kobject *kobj,
275 struct kobj_attribute *attr, char *buf)
277 return double_flag_show(kobj, attr, buf,
278 TRANSPARENT_HUGEPAGE_FLAG,
279 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
281 static ssize_t enabled_store(struct kobject *kobj,
282 struct kobj_attribute *attr,
283 const char *buf, size_t count)
287 ret = double_flag_store(kobj, attr, buf, count,
288 TRANSPARENT_HUGEPAGE_FLAG,
289 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
294 mutex_lock(&khugepaged_mutex);
295 err = start_khugepaged();
296 mutex_unlock(&khugepaged_mutex);
304 static struct kobj_attribute enabled_attr =
305 __ATTR(enabled, 0644, enabled_show, enabled_store);
307 static ssize_t single_flag_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *buf,
309 enum transparent_hugepage_flag flag)
311 return sprintf(buf, "%d\n",
312 !!test_bit(flag, &transparent_hugepage_flags));
315 static ssize_t single_flag_store(struct kobject *kobj,
316 struct kobj_attribute *attr,
317 const char *buf, size_t count,
318 enum transparent_hugepage_flag flag)
323 ret = kstrtoul(buf, 10, &value);
330 set_bit(flag, &transparent_hugepage_flags);
332 clear_bit(flag, &transparent_hugepage_flags);
338 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
339 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
340 * memory just to allocate one more hugepage.
342 static ssize_t defrag_show(struct kobject *kobj,
343 struct kobj_attribute *attr, char *buf)
345 return double_flag_show(kobj, attr, buf,
346 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
347 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
349 static ssize_t defrag_store(struct kobject *kobj,
350 struct kobj_attribute *attr,
351 const char *buf, size_t count)
353 return double_flag_store(kobj, attr, buf, count,
354 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
355 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
357 static struct kobj_attribute defrag_attr =
358 __ATTR(defrag, 0644, defrag_show, defrag_store);
360 static ssize_t use_zero_page_show(struct kobject *kobj,
361 struct kobj_attribute *attr, char *buf)
363 return single_flag_show(kobj, attr, buf,
364 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
366 static ssize_t use_zero_page_store(struct kobject *kobj,
367 struct kobj_attribute *attr, const char *buf, size_t count)
369 return single_flag_store(kobj, attr, buf, count,
370 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
372 static struct kobj_attribute use_zero_page_attr =
373 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
374 #ifdef CONFIG_DEBUG_VM
375 static ssize_t debug_cow_show(struct kobject *kobj,
376 struct kobj_attribute *attr, char *buf)
378 return single_flag_show(kobj, attr, buf,
379 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
381 static ssize_t debug_cow_store(struct kobject *kobj,
382 struct kobj_attribute *attr,
383 const char *buf, size_t count)
385 return single_flag_store(kobj, attr, buf, count,
386 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
388 static struct kobj_attribute debug_cow_attr =
389 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
390 #endif /* CONFIG_DEBUG_VM */
392 static struct attribute *hugepage_attr[] = {
395 &use_zero_page_attr.attr,
396 #ifdef CONFIG_DEBUG_VM
397 &debug_cow_attr.attr,
402 static struct attribute_group hugepage_attr_group = {
403 .attrs = hugepage_attr,
406 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
407 struct kobj_attribute *attr,
410 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
413 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
414 struct kobj_attribute *attr,
415 const char *buf, size_t count)
420 err = kstrtoul(buf, 10, &msecs);
421 if (err || msecs > UINT_MAX)
424 khugepaged_scan_sleep_millisecs = msecs;
425 wake_up_interruptible(&khugepaged_wait);
429 static struct kobj_attribute scan_sleep_millisecs_attr =
430 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
431 scan_sleep_millisecs_store);
433 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
434 struct kobj_attribute *attr,
437 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
440 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
441 struct kobj_attribute *attr,
442 const char *buf, size_t count)
447 err = kstrtoul(buf, 10, &msecs);
448 if (err || msecs > UINT_MAX)
451 khugepaged_alloc_sleep_millisecs = msecs;
452 wake_up_interruptible(&khugepaged_wait);
456 static struct kobj_attribute alloc_sleep_millisecs_attr =
457 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
458 alloc_sleep_millisecs_store);
460 static ssize_t pages_to_scan_show(struct kobject *kobj,
461 struct kobj_attribute *attr,
464 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
466 static ssize_t pages_to_scan_store(struct kobject *kobj,
467 struct kobj_attribute *attr,
468 const char *buf, size_t count)
473 err = kstrtoul(buf, 10, &pages);
474 if (err || !pages || pages > UINT_MAX)
477 khugepaged_pages_to_scan = pages;
481 static struct kobj_attribute pages_to_scan_attr =
482 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
483 pages_to_scan_store);
485 static ssize_t pages_collapsed_show(struct kobject *kobj,
486 struct kobj_attribute *attr,
489 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
491 static struct kobj_attribute pages_collapsed_attr =
492 __ATTR_RO(pages_collapsed);
494 static ssize_t full_scans_show(struct kobject *kobj,
495 struct kobj_attribute *attr,
498 return sprintf(buf, "%u\n", khugepaged_full_scans);
500 static struct kobj_attribute full_scans_attr =
501 __ATTR_RO(full_scans);
503 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
504 struct kobj_attribute *attr, char *buf)
506 return single_flag_show(kobj, attr, buf,
507 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
509 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
510 struct kobj_attribute *attr,
511 const char *buf, size_t count)
513 return single_flag_store(kobj, attr, buf, count,
514 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
516 static struct kobj_attribute khugepaged_defrag_attr =
517 __ATTR(defrag, 0644, khugepaged_defrag_show,
518 khugepaged_defrag_store);
521 * max_ptes_none controls if khugepaged should collapse hugepages over
522 * any unmapped ptes in turn potentially increasing the memory
523 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
524 * reduce the available free memory in the system as it
525 * runs. Increasing max_ptes_none will instead potentially reduce the
526 * free memory in the system during the khugepaged scan.
528 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
529 struct kobj_attribute *attr,
532 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
534 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
535 struct kobj_attribute *attr,
536 const char *buf, size_t count)
539 unsigned long max_ptes_none;
541 err = kstrtoul(buf, 10, &max_ptes_none);
542 if (err || max_ptes_none > HPAGE_PMD_NR-1)
545 khugepaged_max_ptes_none = max_ptes_none;
549 static struct kobj_attribute khugepaged_max_ptes_none_attr =
550 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
551 khugepaged_max_ptes_none_store);
553 static struct attribute *khugepaged_attr[] = {
554 &khugepaged_defrag_attr.attr,
555 &khugepaged_max_ptes_none_attr.attr,
556 &pages_to_scan_attr.attr,
557 &pages_collapsed_attr.attr,
558 &full_scans_attr.attr,
559 &scan_sleep_millisecs_attr.attr,
560 &alloc_sleep_millisecs_attr.attr,
564 static struct attribute_group khugepaged_attr_group = {
565 .attrs = khugepaged_attr,
566 .name = "khugepaged",
569 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
573 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
574 if (unlikely(!*hugepage_kobj)) {
575 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
579 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
581 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
585 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
587 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
588 goto remove_hp_group;
594 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
596 kobject_put(*hugepage_kobj);
600 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
602 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
603 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
604 kobject_put(hugepage_kobj);
607 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
612 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
615 #endif /* CONFIG_SYSFS */
617 static int __init hugepage_init(void)
620 struct kobject *hugepage_kobj;
622 if (!has_transparent_hugepage()) {
623 transparent_hugepage_flags = 0;
627 err = hugepage_init_sysfs(&hugepage_kobj);
631 err = khugepaged_slab_init();
635 register_shrinker(&huge_zero_page_shrinker);
638 * By default disable transparent hugepages on smaller systems,
639 * where the extra memory used could hurt more than TLB overhead
640 * is likely to save. The admin can still enable it through /sys.
642 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
643 transparent_hugepage_flags = 0;
649 hugepage_exit_sysfs(hugepage_kobj);
652 module_init(hugepage_init)
654 static int __init setup_transparent_hugepage(char *str)
659 if (!strcmp(str, "always")) {
660 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
661 &transparent_hugepage_flags);
662 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
663 &transparent_hugepage_flags);
665 } else if (!strcmp(str, "madvise")) {
666 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
667 &transparent_hugepage_flags);
668 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
669 &transparent_hugepage_flags);
671 } else if (!strcmp(str, "never")) {
672 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
673 &transparent_hugepage_flags);
674 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
675 &transparent_hugepage_flags);
681 "transparent_hugepage= cannot parse, ignored\n");
684 __setup("transparent_hugepage=", setup_transparent_hugepage);
686 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
688 if (likely(vma->vm_flags & VM_WRITE))
689 pmd = pmd_mkwrite(pmd);
693 static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
696 entry = mk_pmd(page, vma->vm_page_prot);
697 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
698 entry = pmd_mkhuge(entry);
702 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
703 struct vm_area_struct *vma,
704 unsigned long haddr, pmd_t *pmd,
709 VM_BUG_ON(!PageCompound(page));
710 pgtable = pte_alloc_one(mm, haddr);
711 if (unlikely(!pgtable))
714 clear_huge_page(page, haddr, HPAGE_PMD_NR);
716 * The memory barrier inside __SetPageUptodate makes sure that
717 * clear_huge_page writes become visible before the set_pmd_at()
720 __SetPageUptodate(page);
722 spin_lock(&mm->page_table_lock);
723 if (unlikely(!pmd_none(*pmd))) {
724 spin_unlock(&mm->page_table_lock);
725 mem_cgroup_uncharge_page(page);
727 pte_free(mm, pgtable);
730 entry = mk_huge_pmd(page, vma);
731 page_add_new_anon_rmap(page, vma, haddr);
732 set_pmd_at(mm, haddr, pmd, entry);
733 pgtable_trans_huge_deposit(mm, pgtable);
734 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
736 spin_unlock(&mm->page_table_lock);
742 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
744 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
747 static inline struct page *alloc_hugepage_vma(int defrag,
748 struct vm_area_struct *vma,
749 unsigned long haddr, int nd,
752 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
753 HPAGE_PMD_ORDER, vma, haddr, nd);
757 static inline struct page *alloc_hugepage(int defrag)
759 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
764 static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
765 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
766 struct page *zero_page)
771 entry = mk_pmd(zero_page, vma->vm_page_prot);
772 entry = pmd_wrprotect(entry);
773 entry = pmd_mkhuge(entry);
774 set_pmd_at(mm, haddr, pmd, entry);
775 pgtable_trans_huge_deposit(mm, pgtable);
780 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
781 unsigned long address, pmd_t *pmd,
785 unsigned long haddr = address & HPAGE_PMD_MASK;
788 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
789 if (unlikely(anon_vma_prepare(vma)))
791 if (unlikely(khugepaged_enter(vma)))
793 if (!(flags & FAULT_FLAG_WRITE) &&
794 transparent_hugepage_use_zero_page()) {
796 struct page *zero_page;
798 pgtable = pte_alloc_one(mm, haddr);
799 if (unlikely(!pgtable))
801 zero_page = get_huge_zero_page();
802 if (unlikely(!zero_page)) {
803 pte_free(mm, pgtable);
804 count_vm_event(THP_FAULT_FALLBACK);
807 spin_lock(&mm->page_table_lock);
808 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
810 spin_unlock(&mm->page_table_lock);
812 pte_free(mm, pgtable);
813 put_huge_zero_page();
817 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
818 vma, haddr, numa_node_id(), 0);
819 if (unlikely(!page)) {
820 count_vm_event(THP_FAULT_FALLBACK);
823 count_vm_event(THP_FAULT_ALLOC);
824 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
828 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
830 mem_cgroup_uncharge_page(page);
839 * Use __pte_alloc instead of pte_alloc_map, because we can't
840 * run pte_offset_map on the pmd, if an huge pmd could
841 * materialize from under us from a different thread.
843 if (unlikely(pmd_none(*pmd)) &&
844 unlikely(__pte_alloc(mm, vma, pmd, address)))
846 /* if an huge pmd materialized from under us just retry later */
847 if (unlikely(pmd_trans_huge(*pmd)))
850 * A regular pmd is established and it can't morph into a huge pmd
851 * from under us anymore at this point because we hold the mmap_sem
852 * read mode and khugepaged takes it in write mode. So now it's
853 * safe to run pte_offset_map().
855 pte = pte_offset_map(pmd, address);
856 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
859 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
860 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
861 struct vm_area_struct *vma)
863 struct page *src_page;
869 pgtable = pte_alloc_one(dst_mm, addr);
870 if (unlikely(!pgtable))
873 spin_lock(&dst_mm->page_table_lock);
874 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
878 if (unlikely(!pmd_trans_huge(pmd))) {
879 pte_free(dst_mm, pgtable);
883 * mm->page_table_lock is enough to be sure that huge zero pmd is not
884 * under splitting since we don't split the page itself, only pmd to
887 if (is_huge_zero_pmd(pmd)) {
888 struct page *zero_page;
891 * get_huge_zero_page() will never allocate a new page here,
892 * since we already have a zero page to copy. It just takes a
895 zero_page = get_huge_zero_page();
896 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
898 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
902 if (unlikely(pmd_trans_splitting(pmd))) {
903 /* split huge page running from under us */
904 spin_unlock(&src_mm->page_table_lock);
905 spin_unlock(&dst_mm->page_table_lock);
906 pte_free(dst_mm, pgtable);
908 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
911 src_page = pmd_page(pmd);
912 VM_BUG_ON(!PageHead(src_page));
914 page_dup_rmap(src_page);
915 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
917 pmdp_set_wrprotect(src_mm, addr, src_pmd);
918 pmd = pmd_mkold(pmd_wrprotect(pmd));
919 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
920 pgtable_trans_huge_deposit(dst_mm, pgtable);
925 spin_unlock(&src_mm->page_table_lock);
926 spin_unlock(&dst_mm->page_table_lock);
931 void huge_pmd_set_accessed(struct mm_struct *mm,
932 struct vm_area_struct *vma,
933 unsigned long address,
934 pmd_t *pmd, pmd_t orig_pmd,
940 spin_lock(&mm->page_table_lock);
941 if (unlikely(!pmd_same(*pmd, orig_pmd)))
944 entry = pmd_mkyoung(orig_pmd);
945 haddr = address & HPAGE_PMD_MASK;
946 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
947 update_mmu_cache_pmd(vma, address, pmd);
950 spin_unlock(&mm->page_table_lock);
953 static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
954 struct vm_area_struct *vma, unsigned long address,
955 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
961 unsigned long mmun_start; /* For mmu_notifiers */
962 unsigned long mmun_end; /* For mmu_notifiers */
964 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
970 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
976 clear_user_highpage(page, address);
977 __SetPageUptodate(page);
980 mmun_end = haddr + HPAGE_PMD_SIZE;
981 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
983 spin_lock(&mm->page_table_lock);
984 if (unlikely(!pmd_same(*pmd, orig_pmd)))
987 pmdp_clear_flush(vma, haddr, pmd);
988 /* leave pmd empty until pte is filled */
990 pgtable = pgtable_trans_huge_withdraw(mm);
991 pmd_populate(mm, &_pmd, pgtable);
993 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
995 if (haddr == (address & PAGE_MASK)) {
996 entry = mk_pte(page, vma->vm_page_prot);
997 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
998 page_add_new_anon_rmap(page, vma, haddr);
1000 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1001 entry = pte_mkspecial(entry);
1003 pte = pte_offset_map(&_pmd, haddr);
1004 VM_BUG_ON(!pte_none(*pte));
1005 set_pte_at(mm, haddr, pte, entry);
1008 smp_wmb(); /* make pte visible before pmd */
1009 pmd_populate(mm, pmd, pgtable);
1010 spin_unlock(&mm->page_table_lock);
1011 put_huge_zero_page();
1012 inc_mm_counter(mm, MM_ANONPAGES);
1014 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1016 ret |= VM_FAULT_WRITE;
1020 spin_unlock(&mm->page_table_lock);
1021 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1022 mem_cgroup_uncharge_page(page);
1027 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1028 struct vm_area_struct *vma,
1029 unsigned long address,
1030 pmd_t *pmd, pmd_t orig_pmd,
1032 unsigned long haddr)
1037 struct page **pages;
1038 unsigned long mmun_start; /* For mmu_notifiers */
1039 unsigned long mmun_end; /* For mmu_notifiers */
1041 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1043 if (unlikely(!pages)) {
1044 ret |= VM_FAULT_OOM;
1048 for (i = 0; i < HPAGE_PMD_NR; i++) {
1049 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1051 vma, address, page_to_nid(page));
1052 if (unlikely(!pages[i] ||
1053 mem_cgroup_newpage_charge(pages[i], mm,
1057 mem_cgroup_uncharge_start();
1059 mem_cgroup_uncharge_page(pages[i]);
1062 mem_cgroup_uncharge_end();
1064 ret |= VM_FAULT_OOM;
1069 for (i = 0; i < HPAGE_PMD_NR; i++) {
1070 copy_user_highpage(pages[i], page + i,
1071 haddr + PAGE_SIZE * i, vma);
1072 __SetPageUptodate(pages[i]);
1077 mmun_end = haddr + HPAGE_PMD_SIZE;
1078 mmu_notifier_invalidate_range_start(vma, mmun_start, mmun_end,
1081 spin_lock(&mm->page_table_lock);
1082 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1083 goto out_free_pages;
1084 VM_BUG_ON(!PageHead(page));
1086 pmdp_clear_flush_notify(vma, haddr, pmd);
1087 /* leave pmd empty until pte is filled */
1088 mmu_notifier_invalidate_range_free_pages(vma, mmun_start, mmun_end);
1090 pgtable = pgtable_trans_huge_withdraw(mm);
1091 pmd_populate(mm, &_pmd, pgtable);
1093 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1095 entry = mk_pte(pages[i], vma->vm_page_prot);
1096 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1097 page_add_new_anon_rmap(pages[i], vma, haddr);
1098 pte = pte_offset_map(&_pmd, haddr);
1099 VM_BUG_ON(!pte_none(*pte));
1100 set_pte_at(mm, haddr, pte, entry);
1105 smp_wmb(); /* make pte visible before pmd */
1106 pmd_populate(mm, pmd, pgtable);
1107 page_remove_rmap(page);
1108 spin_unlock(&mm->page_table_lock);
1110 mmu_notifier_invalidate_range_end(vma, mmun_start,
1111 mmun_end, MMU_MIGRATE);
1113 ret |= VM_FAULT_WRITE;
1120 spin_unlock(&mm->page_table_lock);
1121 mmu_notifier_invalidate_range_end(vma, mmun_start,
1122 mmun_end, MMU_MIGRATE);
1123 mem_cgroup_uncharge_start();
1124 for (i = 0; i < HPAGE_PMD_NR; i++) {
1125 mem_cgroup_uncharge_page(pages[i]);
1128 mem_cgroup_uncharge_end();
1133 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1134 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1137 struct page *page = NULL, *new_page;
1138 unsigned long haddr;
1139 unsigned long mmun_start; /* For mmu_notifiers */
1140 unsigned long mmun_end; /* For mmu_notifiers */
1142 VM_BUG_ON(!vma->anon_vma);
1143 haddr = address & HPAGE_PMD_MASK;
1144 if (is_huge_zero_pmd(orig_pmd))
1146 spin_lock(&mm->page_table_lock);
1147 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1150 page = pmd_page(orig_pmd);
1151 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
1152 if (page_mapcount(page) == 1) {
1154 entry = pmd_mkyoung(orig_pmd);
1155 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1156 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
1157 update_mmu_cache_pmd(vma, address, pmd);
1158 ret |= VM_FAULT_WRITE;
1162 spin_unlock(&mm->page_table_lock);
1164 if (transparent_hugepage_enabled(vma) &&
1165 !transparent_hugepage_debug_cow())
1166 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
1167 vma, haddr, numa_node_id(), 0);
1171 if (unlikely(!new_page)) {
1172 count_vm_event(THP_FAULT_FALLBACK);
1174 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
1175 address, pmd, orig_pmd, haddr);
1177 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1178 pmd, orig_pmd, page, haddr);
1179 if (ret & VM_FAULT_OOM)
1180 split_huge_page(page);
1185 count_vm_event(THP_FAULT_ALLOC);
1187 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1190 split_huge_page(page);
1193 ret |= VM_FAULT_OOM;
1198 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1200 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
1201 __SetPageUptodate(new_page);
1204 mmun_end = haddr + HPAGE_PMD_SIZE;
1205 mmu_notifier_invalidate_range_start(vma, mmun_start, mmun_end,
1208 spin_lock(&mm->page_table_lock);
1211 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
1212 spin_unlock(&mm->page_table_lock);
1213 mem_cgroup_uncharge_page(new_page);
1218 entry = mk_huge_pmd(new_page, vma);
1219 pmdp_clear_flush_notify(vma, haddr, pmd);
1220 mmu_notifier_invalidate_range_free_pages(vma, mmun_start,
1222 page_add_new_anon_rmap(new_page, vma, haddr);
1223 set_pmd_at(mm, haddr, pmd, entry);
1224 update_mmu_cache_pmd(vma, address, pmd);
1226 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
1227 put_huge_zero_page();
1229 VM_BUG_ON(!PageHead(page));
1230 page_remove_rmap(page);
1233 ret |= VM_FAULT_WRITE;
1235 spin_unlock(&mm->page_table_lock);
1237 mmu_notifier_invalidate_range_end(vma, mmun_start,
1238 mmun_end, MMU_MIGRATE);
1242 spin_unlock(&mm->page_table_lock);
1246 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1251 struct mm_struct *mm = vma->vm_mm;
1252 struct page *page = NULL;
1254 assert_spin_locked(&mm->page_table_lock);
1256 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1259 /* Avoid dumping huge zero page */
1260 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1261 return ERR_PTR(-EFAULT);
1263 page = pmd_page(*pmd);
1264 VM_BUG_ON(!PageHead(page));
1265 if (flags & FOLL_TOUCH) {
1268 * We should set the dirty bit only for FOLL_WRITE but
1269 * for now the dirty bit in the pmd is meaningless.
1270 * And if the dirty bit will become meaningful and
1271 * we'll only set it with FOLL_WRITE, an atomic
1272 * set_bit will be required on the pmd to set the
1273 * young bit, instead of the current set_pmd_at.
1275 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1276 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1278 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1279 if (page->mapping && trylock_page(page)) {
1282 mlock_vma_page(page);
1286 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1287 VM_BUG_ON(!PageCompound(page));
1288 if (flags & FOLL_GET)
1289 get_page_foll(page);
1295 /* NUMA hinting page fault entry point for trans huge pmds */
1296 int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1297 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
1299 struct anon_vma *anon_vma = NULL;
1301 unsigned long haddr = addr & HPAGE_PMD_MASK;
1302 int page_nid = -1, this_nid = numa_node_id();
1305 bool migrated = false;
1307 spin_lock(&mm->page_table_lock);
1308 if (unlikely(!pmd_same(pmd, *pmdp)))
1311 page = pmd_page(pmd);
1312 page_nid = page_to_nid(page);
1313 count_vm_numa_event(NUMA_HINT_FAULTS);
1314 if (page_nid == this_nid)
1315 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
1318 * Acquire the page lock to serialise THP migrations but avoid dropping
1319 * page_table_lock if at all possible
1321 page_locked = trylock_page(page);
1322 target_nid = mpol_misplaced(page, vma, haddr);
1323 if (target_nid == -1) {
1324 /* If the page was locked, there are no parallel migrations */
1329 * Otherwise wait for potential migrations and retry. We do
1330 * relock and check_same as the page may no longer be mapped.
1331 * As the fault is being retried, do not account for it.
1333 spin_unlock(&mm->page_table_lock);
1334 wait_on_page_locked(page);
1339 /* Page is misplaced, serialise migrations and parallel THP splits */
1341 spin_unlock(&mm->page_table_lock);
1344 anon_vma = page_lock_anon_vma_read(page);
1346 /* Confirm the PTE did not while locked */
1347 spin_lock(&mm->page_table_lock);
1348 if (unlikely(!pmd_same(pmd, *pmdp))) {
1355 /* Bail if we fail to protect against THP splits for any reason */
1356 if (unlikely(!anon_vma)) {
1363 * The page_table_lock above provides a memory barrier
1364 * with change_protection_range.
1366 if (mm_tlb_flush_pending(mm))
1367 flush_tlb_range(vma, haddr, haddr + HPAGE_PMD_SIZE);
1370 * Migrate the THP to the requested node, returns with page unlocked
1371 * and pmd_numa cleared.
1373 spin_unlock(&mm->page_table_lock);
1374 migrated = migrate_misplaced_transhuge_page(mm, vma,
1375 pmdp, pmd, addr, page, target_nid);
1377 page_nid = target_nid;
1381 BUG_ON(!PageLocked(page));
1382 pmd = pmd_mknonnuma(pmd);
1383 set_pmd_at(mm, haddr, pmdp, pmd);
1384 VM_BUG_ON(pmd_numa(*pmdp));
1385 update_mmu_cache_pmd(vma, addr, pmdp);
1388 spin_unlock(&mm->page_table_lock);
1392 page_unlock_anon_vma_read(anon_vma);
1395 task_numa_fault(page_nid, HPAGE_PMD_NR, migrated);
1400 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1401 pmd_t *pmd, unsigned long addr)
1405 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1409 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1410 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1411 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1412 if (is_huge_zero_pmd(orig_pmd)) {
1414 spin_unlock(&tlb->mm->page_table_lock);
1415 put_huge_zero_page();
1417 page = pmd_page(orig_pmd);
1418 page_remove_rmap(page);
1419 VM_BUG_ON(page_mapcount(page) < 0);
1420 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1421 VM_BUG_ON(!PageHead(page));
1423 spin_unlock(&tlb->mm->page_table_lock);
1424 tlb_remove_page(tlb, page);
1426 pte_free(tlb->mm, pgtable);
1432 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1433 unsigned long addr, unsigned long end,
1438 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1440 * All logical pages in the range are present
1441 * if backed by a huge page.
1443 spin_unlock(&vma->vm_mm->page_table_lock);
1444 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1451 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1452 unsigned long old_addr,
1453 unsigned long new_addr, unsigned long old_end,
1454 pmd_t *old_pmd, pmd_t *new_pmd)
1459 struct mm_struct *mm = vma->vm_mm;
1461 if ((old_addr & ~HPAGE_PMD_MASK) ||
1462 (new_addr & ~HPAGE_PMD_MASK) ||
1463 old_end - old_addr < HPAGE_PMD_SIZE ||
1464 (new_vma->vm_flags & VM_NOHUGEPAGE))
1468 * The destination pmd shouldn't be established, free_pgtables()
1469 * should have release it.
1471 if (WARN_ON(!pmd_none(*new_pmd))) {
1472 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1476 ret = __pmd_trans_huge_lock(old_pmd, vma);
1478 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1479 VM_BUG_ON(!pmd_none(*new_pmd));
1480 set_pmd_at(mm, new_addr, new_pmd, pmd);
1481 spin_unlock(&mm->page_table_lock);
1487 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1488 unsigned long addr, pgprot_t newprot, int prot_numa)
1490 struct mm_struct *mm = vma->vm_mm;
1493 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1495 entry = pmdp_get_and_clear_notify(mm, addr, pmd);
1497 entry = pmd_modify(entry, newprot);
1498 BUG_ON(pmd_write(entry));
1500 struct page *page = pmd_page(*pmd);
1502 /* only check non-shared pages */
1503 if (page_mapcount(page) == 1 &&
1505 entry = pmd_mknuma(entry);
1508 set_pmd_at(mm, addr, pmd, entry);
1509 spin_unlock(&vma->vm_mm->page_table_lock);
1517 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1518 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1520 * Note that if it returns 1, this routine returns without unlocking page
1521 * table locks. So callers must unlock them.
1523 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1525 spin_lock(&vma->vm_mm->page_table_lock);
1526 if (likely(pmd_trans_huge(*pmd))) {
1527 if (unlikely(pmd_trans_splitting(*pmd))) {
1528 spin_unlock(&vma->vm_mm->page_table_lock);
1529 wait_split_huge_page(vma->anon_vma, pmd);
1532 /* Thp mapped by 'pmd' is stable, so we can
1533 * handle it as it is. */
1537 spin_unlock(&vma->vm_mm->page_table_lock);
1541 pmd_t *page_check_address_pmd(struct page *page,
1542 struct mm_struct *mm,
1543 unsigned long address,
1544 enum page_check_address_pmd_flag flag)
1546 pmd_t *pmd, *ret = NULL;
1548 if (address & ~HPAGE_PMD_MASK)
1551 pmd = mm_find_pmd(mm, address);
1556 if (pmd_page(*pmd) != page)
1559 * split_vma() may create temporary aliased mappings. There is
1560 * no risk as long as all huge pmd are found and have their
1561 * splitting bit set before __split_huge_page_refcount
1562 * runs. Finding the same huge pmd more than once during the
1563 * same rmap walk is not a problem.
1565 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1566 pmd_trans_splitting(*pmd))
1568 if (pmd_trans_huge(*pmd)) {
1569 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1570 !pmd_trans_splitting(*pmd));
1577 static int __split_huge_page_splitting(struct page *page,
1578 struct vm_area_struct *vma,
1579 unsigned long address)
1581 struct mm_struct *mm = vma->vm_mm;
1584 /* For mmu_notifiers */
1585 const unsigned long mmun_start = address;
1586 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1588 mmu_notifier_invalidate_range_start(vma, mmun_start,
1589 mmun_end, MMU_STATUS);
1590 spin_lock(&mm->page_table_lock);
1591 pmd = page_check_address_pmd(page, mm, address,
1592 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1595 * We can't temporarily set the pmd to null in order
1596 * to split it, the pmd must remain marked huge at all
1597 * times or the VM won't take the pmd_trans_huge paths
1598 * and it won't wait on the anon_vma->root->rwsem to
1599 * serialize against split_huge_page*.
1601 pmdp_splitting_flush(vma, address, pmd);
1605 spin_unlock(&mm->page_table_lock);
1606 mmu_notifier_invalidate_range_end(vma, mmun_start,
1607 mmun_end, MMU_STATUS);
1612 static void __split_huge_page_refcount(struct page *page,
1613 struct list_head *list)
1616 struct zone *zone = page_zone(page);
1617 struct lruvec *lruvec;
1620 /* prevent PageLRU to go away from under us, and freeze lru stats */
1621 spin_lock_irq(&zone->lru_lock);
1622 lruvec = mem_cgroup_page_lruvec(page, zone);
1624 compound_lock(page);
1625 /* complete memcg works before add pages to LRU */
1626 mem_cgroup_split_huge_fixup(page);
1628 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1629 struct page *page_tail = page + i;
1631 /* tail_page->_mapcount cannot change */
1632 BUG_ON(page_mapcount(page_tail) < 0);
1633 tail_count += page_mapcount(page_tail);
1634 /* check for overflow */
1635 BUG_ON(tail_count < 0);
1636 BUG_ON(atomic_read(&page_tail->_count) != 0);
1638 * tail_page->_count is zero and not changing from
1639 * under us. But get_page_unless_zero() may be running
1640 * from under us on the tail_page. If we used
1641 * atomic_set() below instead of atomic_add(), we
1642 * would then run atomic_set() concurrently with
1643 * get_page_unless_zero(), and atomic_set() is
1644 * implemented in C not using locked ops. spin_unlock
1645 * on x86 sometime uses locked ops because of PPro
1646 * errata 66, 92, so unless somebody can guarantee
1647 * atomic_set() here would be safe on all archs (and
1648 * not only on x86), it's safer to use atomic_add().
1650 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1651 &page_tail->_count);
1653 /* after clearing PageTail the gup refcount can be released */
1657 * retain hwpoison flag of the poisoned tail page:
1658 * fix for the unsuitable process killed on Guest Machine(KVM)
1659 * by the memory-failure.
1661 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1662 page_tail->flags |= (page->flags &
1663 ((1L << PG_referenced) |
1664 (1L << PG_swapbacked) |
1665 (1L << PG_mlocked) |
1666 (1L << PG_uptodate)));
1667 page_tail->flags |= (1L << PG_dirty);
1669 /* clear PageTail before overwriting first_page */
1673 * __split_huge_page_splitting() already set the
1674 * splitting bit in all pmd that could map this
1675 * hugepage, that will ensure no CPU can alter the
1676 * mapcount on the head page. The mapcount is only
1677 * accounted in the head page and it has to be
1678 * transferred to all tail pages in the below code. So
1679 * for this code to be safe, the split the mapcount
1680 * can't change. But that doesn't mean userland can't
1681 * keep changing and reading the page contents while
1682 * we transfer the mapcount, so the pmd splitting
1683 * status is achieved setting a reserved bit in the
1684 * pmd, not by clearing the present bit.
1686 page_tail->_mapcount = page->_mapcount;
1688 BUG_ON(page_tail->mapping);
1689 page_tail->mapping = page->mapping;
1691 page_tail->index = page->index + i;
1692 page_nid_xchg_last(page_tail, page_nid_last(page));
1694 BUG_ON(!PageAnon(page_tail));
1695 BUG_ON(!PageUptodate(page_tail));
1696 BUG_ON(!PageDirty(page_tail));
1697 BUG_ON(!PageSwapBacked(page_tail));
1699 lru_add_page_tail(page, page_tail, lruvec, list);
1701 atomic_sub(tail_count, &page->_count);
1702 BUG_ON(atomic_read(&page->_count) <= 0);
1704 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1705 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1707 ClearPageCompound(page);
1708 compound_unlock(page);
1709 spin_unlock_irq(&zone->lru_lock);
1711 for (i = 1; i < HPAGE_PMD_NR; i++) {
1712 struct page *page_tail = page + i;
1713 BUG_ON(page_count(page_tail) <= 0);
1715 * Tail pages may be freed if there wasn't any mapping
1716 * like if add_to_swap() is running on a lru page that
1717 * had its mapping zapped. And freeing these pages
1718 * requires taking the lru_lock so we do the put_page
1719 * of the tail pages after the split is complete.
1721 put_page(page_tail);
1725 * Only the head page (now become a regular page) is required
1726 * to be pinned by the caller.
1728 BUG_ON(page_count(page) <= 0);
1731 static int __split_huge_page_map(struct page *page,
1732 struct vm_area_struct *vma,
1733 unsigned long address)
1735 struct mm_struct *mm = vma->vm_mm;
1739 unsigned long haddr;
1741 spin_lock(&mm->page_table_lock);
1742 pmd = page_check_address_pmd(page, mm, address,
1743 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1745 pgtable = pgtable_trans_huge_withdraw(mm);
1746 pmd_populate(mm, &_pmd, pgtable);
1747 if (pmd_write(*pmd))
1748 BUG_ON(page_mapcount(page) != 1);
1751 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1753 BUG_ON(PageCompound(page+i));
1755 * Note that pmd_numa is not transferred deliberately
1756 * to avoid any possibility that pte_numa leaks to
1757 * a PROT_NONE VMA by accident.
1759 entry = mk_pte(page + i, vma->vm_page_prot);
1760 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1761 if (!pmd_write(*pmd))
1762 entry = pte_wrprotect(entry);
1763 if (!pmd_young(*pmd))
1764 entry = pte_mkold(entry);
1765 pte = pte_offset_map(&_pmd, haddr);
1766 BUG_ON(!pte_none(*pte));
1767 set_pte_at(mm, haddr, pte, entry);
1771 smp_wmb(); /* make pte visible before pmd */
1773 * Up to this point the pmd is present and huge and
1774 * userland has the whole access to the hugepage
1775 * during the split (which happens in place). If we
1776 * overwrite the pmd with the not-huge version
1777 * pointing to the pte here (which of course we could
1778 * if all CPUs were bug free), userland could trigger
1779 * a small page size TLB miss on the small sized TLB
1780 * while the hugepage TLB entry is still established
1781 * in the huge TLB. Some CPU doesn't like that. See
1782 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1783 * Erratum 383 on page 93. Intel should be safe but is
1784 * also warns that it's only safe if the permission
1785 * and cache attributes of the two entries loaded in
1786 * the two TLB is identical (which should be the case
1787 * here). But it is generally safer to never allow
1788 * small and huge TLB entries for the same virtual
1789 * address to be loaded simultaneously. So instead of
1790 * doing "pmd_populate(); flush_tlb_range();" we first
1791 * mark the current pmd notpresent (atomically because
1792 * here the pmd_trans_huge and pmd_trans_splitting
1793 * must remain set at all times on the pmd until the
1794 * split is complete for this pmd), then we flush the
1795 * SMP TLB and finally we write the non-huge version
1796 * of the pmd entry with pmd_populate.
1798 pmdp_invalidate(vma, address, pmd);
1799 pmd_populate(mm, pmd, pgtable);
1802 spin_unlock(&mm->page_table_lock);
1807 /* must be called with anon_vma->root->rwsem held */
1808 static void __split_huge_page(struct page *page,
1809 struct anon_vma *anon_vma,
1810 struct list_head *list)
1812 int mapcount, mapcount2;
1813 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1814 struct anon_vma_chain *avc;
1816 BUG_ON(!PageHead(page));
1817 BUG_ON(PageTail(page));
1820 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1821 struct vm_area_struct *vma = avc->vma;
1822 unsigned long addr = vma_address(page, vma);
1823 BUG_ON(is_vma_temporary_stack(vma));
1824 mapcount += __split_huge_page_splitting(page, vma, addr);
1827 * It is critical that new vmas are added to the tail of the
1828 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1829 * and establishes a child pmd before
1830 * __split_huge_page_splitting() freezes the parent pmd (so if
1831 * we fail to prevent copy_huge_pmd() from running until the
1832 * whole __split_huge_page() is complete), we will still see
1833 * the newly established pmd of the child later during the
1834 * walk, to be able to set it as pmd_trans_splitting too.
1836 if (mapcount != page_mapcount(page))
1837 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1838 mapcount, page_mapcount(page));
1839 BUG_ON(mapcount != page_mapcount(page));
1841 __split_huge_page_refcount(page, list);
1844 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1845 struct vm_area_struct *vma = avc->vma;
1846 unsigned long addr = vma_address(page, vma);
1847 BUG_ON(is_vma_temporary_stack(vma));
1848 mapcount2 += __split_huge_page_map(page, vma, addr);
1850 if (mapcount != mapcount2)
1851 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1852 mapcount, mapcount2, page_mapcount(page));
1853 BUG_ON(mapcount != mapcount2);
1857 * Split a hugepage into normal pages. This doesn't change the position of head
1858 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1859 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1860 * from the hugepage.
1861 * Return 0 if the hugepage is split successfully otherwise return 1.
1863 int split_huge_page_to_list(struct page *page, struct list_head *list)
1865 struct anon_vma *anon_vma;
1868 BUG_ON(is_huge_zero_page(page));
1869 BUG_ON(!PageAnon(page));
1872 * The caller does not necessarily hold an mmap_sem that would prevent
1873 * the anon_vma disappearing so we first we take a reference to it
1874 * and then lock the anon_vma for write. This is similar to
1875 * page_lock_anon_vma_read except the write lock is taken to serialise
1876 * against parallel split or collapse operations.
1878 anon_vma = page_get_anon_vma(page);
1881 anon_vma_lock_write(anon_vma);
1884 if (!PageCompound(page))
1887 BUG_ON(!PageSwapBacked(page));
1888 __split_huge_page(page, anon_vma, list);
1889 count_vm_event(THP_SPLIT);
1891 BUG_ON(PageCompound(page));
1893 anon_vma_unlock_write(anon_vma);
1894 put_anon_vma(anon_vma);
1899 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1901 int hugepage_madvise(struct vm_area_struct *vma,
1902 unsigned long *vm_flags, int advice)
1904 struct mm_struct *mm = vma->vm_mm;
1909 * Be somewhat over-protective like KSM for now!
1911 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1913 if (mm->def_flags & VM_NOHUGEPAGE)
1915 *vm_flags &= ~VM_NOHUGEPAGE;
1916 *vm_flags |= VM_HUGEPAGE;
1918 * If the vma become good for khugepaged to scan,
1919 * register it here without waiting a page fault that
1920 * may not happen any time soon.
1922 if (unlikely(khugepaged_enter_vma_merge(vma)))
1925 case MADV_NOHUGEPAGE:
1927 * Be somewhat over-protective like KSM for now!
1929 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1931 *vm_flags &= ~VM_HUGEPAGE;
1932 *vm_flags |= VM_NOHUGEPAGE;
1934 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1935 * this vma even if we leave the mm registered in khugepaged if
1936 * it got registered before VM_NOHUGEPAGE was set.
1944 static int __init khugepaged_slab_init(void)
1946 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1947 sizeof(struct mm_slot),
1948 __alignof__(struct mm_slot), 0, NULL);
1955 static inline struct mm_slot *alloc_mm_slot(void)
1957 if (!mm_slot_cache) /* initialization failed */
1959 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1962 static inline void free_mm_slot(struct mm_slot *mm_slot)
1964 kmem_cache_free(mm_slot_cache, mm_slot);
1967 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1969 struct mm_slot *mm_slot;
1971 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
1972 if (mm == mm_slot->mm)
1978 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1979 struct mm_slot *mm_slot)
1982 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
1985 static inline int khugepaged_test_exit(struct mm_struct *mm)
1987 return atomic_read(&mm->mm_users) == 0;
1990 int __khugepaged_enter(struct mm_struct *mm)
1992 struct mm_slot *mm_slot;
1995 mm_slot = alloc_mm_slot();
1999 /* __khugepaged_exit() must not run from under us */
2000 VM_BUG_ON(khugepaged_test_exit(mm));
2001 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
2002 free_mm_slot(mm_slot);
2006 spin_lock(&khugepaged_mm_lock);
2007 insert_to_mm_slots_hash(mm, mm_slot);
2009 * Insert just behind the scanning cursor, to let the area settle
2012 wakeup = list_empty(&khugepaged_scan.mm_head);
2013 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
2014 spin_unlock(&khugepaged_mm_lock);
2016 atomic_inc(&mm->mm_count);
2018 wake_up_interruptible(&khugepaged_wait);
2023 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
2025 unsigned long hstart, hend;
2028 * Not yet faulted in so we will register later in the
2029 * page fault if needed.
2033 /* khugepaged not yet working on file or special mappings */
2035 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2036 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2037 hend = vma->vm_end & HPAGE_PMD_MASK;
2039 return khugepaged_enter(vma);
2043 void __khugepaged_exit(struct mm_struct *mm)
2045 struct mm_slot *mm_slot;
2048 spin_lock(&khugepaged_mm_lock);
2049 mm_slot = get_mm_slot(mm);
2050 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
2051 hash_del(&mm_slot->hash);
2052 list_del(&mm_slot->mm_node);
2055 spin_unlock(&khugepaged_mm_lock);
2058 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2059 free_mm_slot(mm_slot);
2061 } else if (mm_slot) {
2063 * This is required to serialize against
2064 * khugepaged_test_exit() (which is guaranteed to run
2065 * under mmap sem read mode). Stop here (after we
2066 * return all pagetables will be destroyed) until
2067 * khugepaged has finished working on the pagetables
2068 * under the mmap_sem.
2070 down_write(&mm->mmap_sem);
2071 up_write(&mm->mmap_sem);
2075 static void release_pte_page(struct page *page)
2077 /* 0 stands for page_is_file_cache(page) == false */
2078 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2080 putback_lru_page(page);
2083 static void release_pte_pages(pte_t *pte, pte_t *_pte)
2085 while (--_pte >= pte) {
2086 pte_t pteval = *_pte;
2087 if (!pte_none(pteval))
2088 release_pte_page(pte_page(pteval));
2092 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2093 unsigned long address,
2098 int referenced = 0, none = 0;
2099 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2100 _pte++, address += PAGE_SIZE) {
2101 pte_t pteval = *_pte;
2102 if (pte_none(pteval)) {
2103 if (++none <= khugepaged_max_ptes_none)
2108 if (!pte_present(pteval) || !pte_write(pteval))
2110 page = vm_normal_page(vma, address, pteval);
2111 if (unlikely(!page))
2114 VM_BUG_ON(PageCompound(page));
2115 BUG_ON(!PageAnon(page));
2116 VM_BUG_ON(!PageSwapBacked(page));
2118 /* cannot use mapcount: can't collapse if there's a gup pin */
2119 if (page_count(page) != 1)
2122 * We can do it before isolate_lru_page because the
2123 * page can't be freed from under us. NOTE: PG_lock
2124 * is needed to serialize against split_huge_page
2125 * when invoked from the VM.
2127 if (!trylock_page(page))
2130 * Isolate the page to avoid collapsing an hugepage
2131 * currently in use by the VM.
2133 if (isolate_lru_page(page)) {
2137 /* 0 stands for page_is_file_cache(page) == false */
2138 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2139 VM_BUG_ON(!PageLocked(page));
2140 VM_BUG_ON(PageLRU(page));
2142 /* If there is no mapped pte young don't collapse the page */
2143 if (pte_young(pteval) || PageReferenced(page) ||
2144 mmu_notifier_test_young(vma->vm_mm, address))
2147 if (likely(referenced))
2150 release_pte_pages(pte, _pte);
2154 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2155 struct vm_area_struct *vma,
2156 unsigned long address,
2160 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2161 pte_t pteval = *_pte;
2162 struct page *src_page;
2164 if (pte_none(pteval)) {
2165 clear_user_highpage(page, address);
2166 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2168 src_page = pte_page(pteval);
2169 copy_user_highpage(page, src_page, address, vma);
2170 VM_BUG_ON(page_mapcount(src_page) != 1);
2171 release_pte_page(src_page);
2173 * ptl mostly unnecessary, but preempt has to
2174 * be disabled to update the per-cpu stats
2175 * inside page_remove_rmap().
2179 * paravirt calls inside pte_clear here are
2182 pte_clear(vma->vm_mm, address, _pte);
2183 page_remove_rmap(src_page);
2185 free_page_and_swap_cache(src_page);
2188 address += PAGE_SIZE;
2193 static void khugepaged_alloc_sleep(void)
2195 wait_event_freezable_timeout(khugepaged_wait, false,
2196 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2200 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2202 if (IS_ERR(*hpage)) {
2208 khugepaged_alloc_sleep();
2209 } else if (*hpage) {
2218 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2219 struct vm_area_struct *vma, unsigned long address,
2224 * Allocate the page while the vma is still valid and under
2225 * the mmap_sem read mode so there is no memory allocation
2226 * later when we take the mmap_sem in write mode. This is more
2227 * friendly behavior (OTOH it may actually hide bugs) to
2228 * filesystems in userland with daemons allocating memory in
2229 * the userland I/O paths. Allocating memory with the
2230 * mmap_sem in read mode is good idea also to allow greater
2233 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2234 node, __GFP_OTHER_NODE);
2237 * After allocating the hugepage, release the mmap_sem read lock in
2238 * preparation for taking it in write mode.
2240 up_read(&mm->mmap_sem);
2241 if (unlikely(!*hpage)) {
2242 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2243 *hpage = ERR_PTR(-ENOMEM);
2247 count_vm_event(THP_COLLAPSE_ALLOC);
2251 static struct page *khugepaged_alloc_hugepage(bool *wait)
2256 hpage = alloc_hugepage(khugepaged_defrag());
2258 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2263 khugepaged_alloc_sleep();
2265 count_vm_event(THP_COLLAPSE_ALLOC);
2266 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2271 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2274 *hpage = khugepaged_alloc_hugepage(wait);
2276 if (unlikely(!*hpage))
2283 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2284 struct vm_area_struct *vma, unsigned long address,
2287 up_read(&mm->mmap_sem);
2293 static bool hugepage_vma_check(struct vm_area_struct *vma)
2295 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2296 (vma->vm_flags & VM_NOHUGEPAGE))
2299 if (!vma->anon_vma || vma->vm_ops)
2301 if (is_vma_temporary_stack(vma))
2303 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2307 static void collapse_huge_page(struct mm_struct *mm,
2308 unsigned long address,
2309 struct page **hpage,
2310 struct vm_area_struct *vma,
2316 struct page *new_page;
2319 unsigned long hstart, hend;
2320 unsigned long mmun_start; /* For mmu_notifiers */
2321 unsigned long mmun_end; /* For mmu_notifiers */
2323 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2325 /* release the mmap_sem read lock. */
2326 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2330 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
2334 * Prevent all access to pagetables with the exception of
2335 * gup_fast later hanlded by the ptep_clear_flush and the VM
2336 * handled by the anon_vma lock + PG_lock.
2338 down_write(&mm->mmap_sem);
2339 if (unlikely(khugepaged_test_exit(mm)))
2342 vma = find_vma(mm, address);
2345 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2346 hend = vma->vm_end & HPAGE_PMD_MASK;
2347 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2349 if (!hugepage_vma_check(vma))
2351 pmd = mm_find_pmd(mm, address);
2354 if (pmd_trans_huge(*pmd))
2357 anon_vma_lock_write(vma->anon_vma);
2359 pte = pte_offset_map(pmd, address);
2360 ptl = pte_lockptr(mm, pmd);
2362 mmun_start = address;
2363 mmun_end = address + HPAGE_PMD_SIZE;
2364 mmu_notifier_invalidate_range_start(vma, mmun_start,
2365 mmun_end, MMU_MIGRATE);
2366 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2368 * After this gup_fast can't run anymore. This also removes
2369 * any huge TLB entry from the CPU so we won't allow
2370 * huge and small TLB entries for the same virtual address
2371 * to avoid the risk of CPU bugs in that area.
2373 _pmd = pmdp_clear_flush(vma, address, pmd);
2374 spin_unlock(&mm->page_table_lock);
2375 mmu_notifier_invalidate_range_end(vma, mmun_start,
2376 mmun_end, MMU_MIGRATE);
2379 isolated = __collapse_huge_page_isolate(vma, address, pte);
2382 if (unlikely(!isolated)) {
2384 spin_lock(&mm->page_table_lock);
2385 BUG_ON(!pmd_none(*pmd));
2387 * We can only use set_pmd_at when establishing
2388 * hugepmds and never for establishing regular pmds that
2389 * points to regular pagetables. Use pmd_populate for that
2391 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
2392 spin_unlock(&mm->page_table_lock);
2393 anon_vma_unlock_write(vma->anon_vma);
2398 * All pages are isolated and locked so anon_vma rmap
2399 * can't run anymore.
2401 anon_vma_unlock_write(vma->anon_vma);
2403 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2405 __SetPageUptodate(new_page);
2406 pgtable = pmd_pgtable(_pmd);
2408 _pmd = mk_huge_pmd(new_page, vma);
2411 * spin_lock() below is not the equivalent of smp_wmb(), so
2412 * this is needed to avoid the copy_huge_page writes to become
2413 * visible after the set_pmd_at() write.
2417 spin_lock(&mm->page_table_lock);
2418 BUG_ON(!pmd_none(*pmd));
2419 page_add_new_anon_rmap(new_page, vma, address);
2420 set_pmd_at(mm, address, pmd, _pmd);
2421 update_mmu_cache_pmd(vma, address, pmd);
2422 pgtable_trans_huge_deposit(mm, pgtable);
2423 spin_unlock(&mm->page_table_lock);
2427 khugepaged_pages_collapsed++;
2429 up_write(&mm->mmap_sem);
2433 mem_cgroup_uncharge_page(new_page);
2437 static int khugepaged_scan_pmd(struct mm_struct *mm,
2438 struct vm_area_struct *vma,
2439 unsigned long address,
2440 struct page **hpage)
2444 int ret = 0, referenced = 0, none = 0;
2446 unsigned long _address;
2448 int node = NUMA_NO_NODE;
2450 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2452 pmd = mm_find_pmd(mm, address);
2455 if (pmd_trans_huge(*pmd))
2458 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2459 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2460 _pte++, _address += PAGE_SIZE) {
2461 pte_t pteval = *_pte;
2462 if (pte_none(pteval)) {
2463 if (++none <= khugepaged_max_ptes_none)
2468 if (!pte_present(pteval) || !pte_write(pteval))
2470 page = vm_normal_page(vma, _address, pteval);
2471 if (unlikely(!page))
2474 * Chose the node of the first page. This could
2475 * be more sophisticated and look at more pages,
2476 * but isn't for now.
2478 if (node == NUMA_NO_NODE)
2479 node = page_to_nid(page);
2480 VM_BUG_ON(PageCompound(page));
2481 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2483 /* cannot use mapcount: can't collapse if there's a gup pin */
2484 if (page_count(page) != 1)
2486 if (pte_young(pteval) || PageReferenced(page) ||
2487 mmu_notifier_test_young(vma->vm_mm, address))
2493 pte_unmap_unlock(pte, ptl);
2495 /* collapse_huge_page will return with the mmap_sem released */
2496 collapse_huge_page(mm, address, hpage, vma, node);
2501 static void collect_mm_slot(struct mm_slot *mm_slot)
2503 struct mm_struct *mm = mm_slot->mm;
2505 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2507 if (khugepaged_test_exit(mm)) {
2509 hash_del(&mm_slot->hash);
2510 list_del(&mm_slot->mm_node);
2513 * Not strictly needed because the mm exited already.
2515 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2518 /* khugepaged_mm_lock actually not necessary for the below */
2519 free_mm_slot(mm_slot);
2524 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2525 struct page **hpage)
2526 __releases(&khugepaged_mm_lock)
2527 __acquires(&khugepaged_mm_lock)
2529 struct mm_slot *mm_slot;
2530 struct mm_struct *mm;
2531 struct vm_area_struct *vma;
2535 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2537 if (khugepaged_scan.mm_slot)
2538 mm_slot = khugepaged_scan.mm_slot;
2540 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2541 struct mm_slot, mm_node);
2542 khugepaged_scan.address = 0;
2543 khugepaged_scan.mm_slot = mm_slot;
2545 spin_unlock(&khugepaged_mm_lock);
2548 down_read(&mm->mmap_sem);
2549 if (unlikely(khugepaged_test_exit(mm)))
2552 vma = find_vma(mm, khugepaged_scan.address);
2555 for (; vma; vma = vma->vm_next) {
2556 unsigned long hstart, hend;
2559 if (unlikely(khugepaged_test_exit(mm))) {
2563 if (!hugepage_vma_check(vma)) {
2568 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2569 hend = vma->vm_end & HPAGE_PMD_MASK;
2572 if (khugepaged_scan.address > hend)
2574 if (khugepaged_scan.address < hstart)
2575 khugepaged_scan.address = hstart;
2576 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2578 while (khugepaged_scan.address < hend) {
2581 if (unlikely(khugepaged_test_exit(mm)))
2582 goto breakouterloop;
2584 VM_BUG_ON(khugepaged_scan.address < hstart ||
2585 khugepaged_scan.address + HPAGE_PMD_SIZE >
2587 ret = khugepaged_scan_pmd(mm, vma,
2588 khugepaged_scan.address,
2590 /* move to next address */
2591 khugepaged_scan.address += HPAGE_PMD_SIZE;
2592 progress += HPAGE_PMD_NR;
2594 /* we released mmap_sem so break loop */
2595 goto breakouterloop_mmap_sem;
2596 if (progress >= pages)
2597 goto breakouterloop;
2601 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2602 breakouterloop_mmap_sem:
2604 spin_lock(&khugepaged_mm_lock);
2605 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2607 * Release the current mm_slot if this mm is about to die, or
2608 * if we scanned all vmas of this mm.
2610 if (khugepaged_test_exit(mm) || !vma) {
2612 * Make sure that if mm_users is reaching zero while
2613 * khugepaged runs here, khugepaged_exit will find
2614 * mm_slot not pointing to the exiting mm.
2616 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2617 khugepaged_scan.mm_slot = list_entry(
2618 mm_slot->mm_node.next,
2619 struct mm_slot, mm_node);
2620 khugepaged_scan.address = 0;
2622 khugepaged_scan.mm_slot = NULL;
2623 khugepaged_full_scans++;
2626 collect_mm_slot(mm_slot);
2632 static int khugepaged_has_work(void)
2634 return !list_empty(&khugepaged_scan.mm_head) &&
2635 khugepaged_enabled();
2638 static int khugepaged_wait_event(void)
2640 return !list_empty(&khugepaged_scan.mm_head) ||
2641 kthread_should_stop();
2644 static void khugepaged_do_scan(void)
2646 struct page *hpage = NULL;
2647 unsigned int progress = 0, pass_through_head = 0;
2648 unsigned int pages = khugepaged_pages_to_scan;
2651 barrier(); /* write khugepaged_pages_to_scan to local stack */
2653 while (progress < pages) {
2654 if (!khugepaged_prealloc_page(&hpage, &wait))
2659 if (unlikely(kthread_should_stop() || freezing(current)))
2662 spin_lock(&khugepaged_mm_lock);
2663 if (!khugepaged_scan.mm_slot)
2664 pass_through_head++;
2665 if (khugepaged_has_work() &&
2666 pass_through_head < 2)
2667 progress += khugepaged_scan_mm_slot(pages - progress,
2671 spin_unlock(&khugepaged_mm_lock);
2674 if (!IS_ERR_OR_NULL(hpage))
2678 static void khugepaged_wait_work(void)
2682 if (khugepaged_has_work()) {
2683 if (!khugepaged_scan_sleep_millisecs)
2686 wait_event_freezable_timeout(khugepaged_wait,
2687 kthread_should_stop(),
2688 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2692 if (khugepaged_enabled())
2693 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2696 static int khugepaged(void *none)
2698 struct mm_slot *mm_slot;
2701 set_user_nice(current, 19);
2703 while (!kthread_should_stop()) {
2704 khugepaged_do_scan();
2705 khugepaged_wait_work();
2708 spin_lock(&khugepaged_mm_lock);
2709 mm_slot = khugepaged_scan.mm_slot;
2710 khugepaged_scan.mm_slot = NULL;
2712 collect_mm_slot(mm_slot);
2713 spin_unlock(&khugepaged_mm_lock);
2717 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2718 unsigned long haddr, pmd_t *pmd)
2720 struct mm_struct *mm = vma->vm_mm;
2725 pmdp_clear_flush_notify(vma, haddr, pmd);
2726 /* leave pmd empty until pte is filled */
2728 pgtable = pgtable_trans_huge_withdraw(mm);
2729 pmd_populate(mm, &_pmd, pgtable);
2731 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2733 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2734 entry = pte_mkspecial(entry);
2735 pte = pte_offset_map(&_pmd, haddr);
2736 VM_BUG_ON(!pte_none(*pte));
2737 set_pte_at(mm, haddr, pte, entry);
2740 smp_wmb(); /* make pte visible before pmd */
2741 pmd_populate(mm, pmd, pgtable);
2742 put_huge_zero_page();
2745 void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2749 struct mm_struct *mm = vma->vm_mm;
2750 unsigned long haddr = address & HPAGE_PMD_MASK;
2751 unsigned long mmun_start; /* For mmu_notifiers */
2752 unsigned long mmun_end; /* For mmu_notifiers */
2754 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
2757 mmun_end = haddr + HPAGE_PMD_SIZE;
2759 mmu_notifier_invalidate_range_start(vma, mmun_start,
2760 mmun_end, MMU_MIGRATE);
2761 spin_lock(&mm->page_table_lock);
2762 if (unlikely(!pmd_trans_huge(*pmd))) {
2763 spin_unlock(&mm->page_table_lock);
2764 mmu_notifier_invalidate_range_end(vma, mmun_start,
2765 mmun_end, MMU_MIGRATE);
2768 if (is_huge_zero_pmd(*pmd)) {
2769 __split_huge_zero_page_pmd(vma, haddr, pmd);
2770 spin_unlock(&mm->page_table_lock);
2771 mmu_notifier_invalidate_range_end(vma, mmun_start,
2772 mmun_end, MMU_MIGRATE);
2775 page = pmd_page(*pmd);
2776 VM_BUG_ON(!page_count(page));
2778 spin_unlock(&mm->page_table_lock);
2779 mmu_notifier_invalidate_range_end(vma, mmun_start,
2780 mmun_end, MMU_MIGRATE);
2782 split_huge_page(page);
2787 * We don't always have down_write of mmap_sem here: a racing
2788 * do_huge_pmd_wp_page() might have copied-on-write to another
2789 * huge page before our split_huge_page() got the anon_vma lock.
2791 if (unlikely(pmd_trans_huge(*pmd)))
2795 void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2798 struct vm_area_struct *vma;
2800 vma = find_vma(mm, address);
2801 BUG_ON(vma == NULL);
2802 split_huge_page_pmd(vma, address, pmd);
2805 static void split_huge_page_address(struct mm_struct *mm,
2806 unsigned long address)
2810 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2812 pmd = mm_find_pmd(mm, address);
2816 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2817 * materialize from under us.
2819 split_huge_page_pmd_mm(mm, address, pmd);
2822 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2823 unsigned long start,
2828 * If the new start address isn't hpage aligned and it could
2829 * previously contain an hugepage: check if we need to split
2832 if (start & ~HPAGE_PMD_MASK &&
2833 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2834 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2835 split_huge_page_address(vma->vm_mm, start);
2838 * If the new end address isn't hpage aligned and it could
2839 * previously contain an hugepage: check if we need to split
2842 if (end & ~HPAGE_PMD_MASK &&
2843 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2844 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2845 split_huge_page_address(vma->vm_mm, end);
2848 * If we're also updating the vma->vm_next->vm_start, if the new
2849 * vm_next->vm_start isn't page aligned and it could previously
2850 * contain an hugepage: check if we need to split an huge pmd.
2852 if (adjust_next > 0) {
2853 struct vm_area_struct *next = vma->vm_next;
2854 unsigned long nstart = next->vm_start;
2855 nstart += adjust_next << PAGE_SHIFT;
2856 if (nstart & ~HPAGE_PMD_MASK &&
2857 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2858 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2859 split_huge_page_address(next->vm_mm, nstart);