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 unsigned long huge_zero_pfn __read_mostly;
168 static inline bool is_huge_zero_pfn(unsigned long pfn)
170 unsigned long zero_pfn = ACCESS_ONCE(huge_zero_pfn);
171 return zero_pfn && pfn == zero_pfn;
174 static inline bool is_huge_zero_pmd(pmd_t pmd)
176 return is_huge_zero_pfn(pmd_pfn(pmd));
179 static unsigned long get_huge_zero_page(void)
181 struct page *zero_page;
183 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
184 return ACCESS_ONCE(huge_zero_pfn);
186 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
189 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
192 count_vm_event(THP_ZERO_PAGE_ALLOC);
194 if (cmpxchg(&huge_zero_pfn, 0, page_to_pfn(zero_page))) {
196 __free_page(zero_page);
200 /* We take additional reference here. It will be put back by shrinker */
201 atomic_set(&huge_zero_refcount, 2);
203 return ACCESS_ONCE(huge_zero_pfn);
206 static void put_huge_zero_page(void)
209 * Counter should never go to zero here. Only shrinker can put
212 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
215 static int shrink_huge_zero_page(struct shrinker *shrink,
216 struct shrink_control *sc)
219 /* we can free zero page only if last reference remains */
220 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
222 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
223 unsigned long zero_pfn = xchg(&huge_zero_pfn, 0);
224 BUG_ON(zero_pfn == 0);
225 __free_page(__pfn_to_page(zero_pfn));
231 static struct shrinker huge_zero_page_shrinker = {
232 .shrink = shrink_huge_zero_page,
233 .seeks = DEFAULT_SEEKS,
238 static ssize_t double_flag_show(struct kobject *kobj,
239 struct kobj_attribute *attr, char *buf,
240 enum transparent_hugepage_flag enabled,
241 enum transparent_hugepage_flag req_madv)
243 if (test_bit(enabled, &transparent_hugepage_flags)) {
244 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
245 return sprintf(buf, "[always] madvise never\n");
246 } else if (test_bit(req_madv, &transparent_hugepage_flags))
247 return sprintf(buf, "always [madvise] never\n");
249 return sprintf(buf, "always madvise [never]\n");
251 static ssize_t double_flag_store(struct kobject *kobj,
252 struct kobj_attribute *attr,
253 const char *buf, size_t count,
254 enum transparent_hugepage_flag enabled,
255 enum transparent_hugepage_flag req_madv)
257 if (!memcmp("always", buf,
258 min(sizeof("always")-1, count))) {
259 set_bit(enabled, &transparent_hugepage_flags);
260 clear_bit(req_madv, &transparent_hugepage_flags);
261 } else if (!memcmp("madvise", buf,
262 min(sizeof("madvise")-1, count))) {
263 clear_bit(enabled, &transparent_hugepage_flags);
264 set_bit(req_madv, &transparent_hugepage_flags);
265 } else if (!memcmp("never", buf,
266 min(sizeof("never")-1, count))) {
267 clear_bit(enabled, &transparent_hugepage_flags);
268 clear_bit(req_madv, &transparent_hugepage_flags);
275 static ssize_t enabled_show(struct kobject *kobj,
276 struct kobj_attribute *attr, char *buf)
278 return double_flag_show(kobj, attr, buf,
279 TRANSPARENT_HUGEPAGE_FLAG,
280 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
282 static ssize_t enabled_store(struct kobject *kobj,
283 struct kobj_attribute *attr,
284 const char *buf, size_t count)
288 ret = double_flag_store(kobj, attr, buf, count,
289 TRANSPARENT_HUGEPAGE_FLAG,
290 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
295 mutex_lock(&khugepaged_mutex);
296 err = start_khugepaged();
297 mutex_unlock(&khugepaged_mutex);
305 static struct kobj_attribute enabled_attr =
306 __ATTR(enabled, 0644, enabled_show, enabled_store);
308 static ssize_t single_flag_show(struct kobject *kobj,
309 struct kobj_attribute *attr, char *buf,
310 enum transparent_hugepage_flag flag)
312 return sprintf(buf, "%d\n",
313 !!test_bit(flag, &transparent_hugepage_flags));
316 static ssize_t single_flag_store(struct kobject *kobj,
317 struct kobj_attribute *attr,
318 const char *buf, size_t count,
319 enum transparent_hugepage_flag flag)
324 ret = kstrtoul(buf, 10, &value);
331 set_bit(flag, &transparent_hugepage_flags);
333 clear_bit(flag, &transparent_hugepage_flags);
339 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
340 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
341 * memory just to allocate one more hugepage.
343 static ssize_t defrag_show(struct kobject *kobj,
344 struct kobj_attribute *attr, char *buf)
346 return double_flag_show(kobj, attr, buf,
347 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
348 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
350 static ssize_t defrag_store(struct kobject *kobj,
351 struct kobj_attribute *attr,
352 const char *buf, size_t count)
354 return double_flag_store(kobj, attr, buf, count,
355 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
356 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
358 static struct kobj_attribute defrag_attr =
359 __ATTR(defrag, 0644, defrag_show, defrag_store);
361 static ssize_t use_zero_page_show(struct kobject *kobj,
362 struct kobj_attribute *attr, char *buf)
364 return single_flag_show(kobj, attr, buf,
365 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
367 static ssize_t use_zero_page_store(struct kobject *kobj,
368 struct kobj_attribute *attr, const char *buf, size_t count)
370 return single_flag_store(kobj, attr, buf, count,
371 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
373 static struct kobj_attribute use_zero_page_attr =
374 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
375 #ifdef CONFIG_DEBUG_VM
376 static ssize_t debug_cow_show(struct kobject *kobj,
377 struct kobj_attribute *attr, char *buf)
379 return single_flag_show(kobj, attr, buf,
380 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
382 static ssize_t debug_cow_store(struct kobject *kobj,
383 struct kobj_attribute *attr,
384 const char *buf, size_t count)
386 return single_flag_store(kobj, attr, buf, count,
387 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
389 static struct kobj_attribute debug_cow_attr =
390 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
391 #endif /* CONFIG_DEBUG_VM */
393 static struct attribute *hugepage_attr[] = {
396 &use_zero_page_attr.attr,
397 #ifdef CONFIG_DEBUG_VM
398 &debug_cow_attr.attr,
403 static struct attribute_group hugepage_attr_group = {
404 .attrs = hugepage_attr,
407 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
408 struct kobj_attribute *attr,
411 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
414 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
415 struct kobj_attribute *attr,
416 const char *buf, size_t count)
421 err = strict_strtoul(buf, 10, &msecs);
422 if (err || msecs > UINT_MAX)
425 khugepaged_scan_sleep_millisecs = msecs;
426 wake_up_interruptible(&khugepaged_wait);
430 static struct kobj_attribute scan_sleep_millisecs_attr =
431 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
432 scan_sleep_millisecs_store);
434 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
435 struct kobj_attribute *attr,
438 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
441 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
442 struct kobj_attribute *attr,
443 const char *buf, size_t count)
448 err = strict_strtoul(buf, 10, &msecs);
449 if (err || msecs > UINT_MAX)
452 khugepaged_alloc_sleep_millisecs = msecs;
453 wake_up_interruptible(&khugepaged_wait);
457 static struct kobj_attribute alloc_sleep_millisecs_attr =
458 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
459 alloc_sleep_millisecs_store);
461 static ssize_t pages_to_scan_show(struct kobject *kobj,
462 struct kobj_attribute *attr,
465 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
467 static ssize_t pages_to_scan_store(struct kobject *kobj,
468 struct kobj_attribute *attr,
469 const char *buf, size_t count)
474 err = strict_strtoul(buf, 10, &pages);
475 if (err || !pages || pages > UINT_MAX)
478 khugepaged_pages_to_scan = pages;
482 static struct kobj_attribute pages_to_scan_attr =
483 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
484 pages_to_scan_store);
486 static ssize_t pages_collapsed_show(struct kobject *kobj,
487 struct kobj_attribute *attr,
490 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
492 static struct kobj_attribute pages_collapsed_attr =
493 __ATTR_RO(pages_collapsed);
495 static ssize_t full_scans_show(struct kobject *kobj,
496 struct kobj_attribute *attr,
499 return sprintf(buf, "%u\n", khugepaged_full_scans);
501 static struct kobj_attribute full_scans_attr =
502 __ATTR_RO(full_scans);
504 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
505 struct kobj_attribute *attr, char *buf)
507 return single_flag_show(kobj, attr, buf,
508 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
510 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 const char *buf, size_t count)
514 return single_flag_store(kobj, attr, buf, count,
515 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
517 static struct kobj_attribute khugepaged_defrag_attr =
518 __ATTR(defrag, 0644, khugepaged_defrag_show,
519 khugepaged_defrag_store);
522 * max_ptes_none controls if khugepaged should collapse hugepages over
523 * any unmapped ptes in turn potentially increasing the memory
524 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
525 * reduce the available free memory in the system as it
526 * runs. Increasing max_ptes_none will instead potentially reduce the
527 * free memory in the system during the khugepaged scan.
529 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
530 struct kobj_attribute *attr,
533 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
535 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 const char *buf, size_t count)
540 unsigned long max_ptes_none;
542 err = strict_strtoul(buf, 10, &max_ptes_none);
543 if (err || max_ptes_none > HPAGE_PMD_NR-1)
546 khugepaged_max_ptes_none = max_ptes_none;
550 static struct kobj_attribute khugepaged_max_ptes_none_attr =
551 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
552 khugepaged_max_ptes_none_store);
554 static struct attribute *khugepaged_attr[] = {
555 &khugepaged_defrag_attr.attr,
556 &khugepaged_max_ptes_none_attr.attr,
557 &pages_to_scan_attr.attr,
558 &pages_collapsed_attr.attr,
559 &full_scans_attr.attr,
560 &scan_sleep_millisecs_attr.attr,
561 &alloc_sleep_millisecs_attr.attr,
565 static struct attribute_group khugepaged_attr_group = {
566 .attrs = khugepaged_attr,
567 .name = "khugepaged",
570 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
574 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
575 if (unlikely(!*hugepage_kobj)) {
576 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
580 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
582 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
586 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
588 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
589 goto remove_hp_group;
595 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
597 kobject_put(*hugepage_kobj);
601 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
603 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
604 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
605 kobject_put(hugepage_kobj);
608 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
613 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
616 #endif /* CONFIG_SYSFS */
618 static int __init hugepage_init(void)
621 struct kobject *hugepage_kobj;
623 if (!has_transparent_hugepage()) {
624 transparent_hugepage_flags = 0;
628 err = hugepage_init_sysfs(&hugepage_kobj);
632 err = khugepaged_slab_init();
636 register_shrinker(&huge_zero_page_shrinker);
639 * By default disable transparent hugepages on smaller systems,
640 * where the extra memory used could hurt more than TLB overhead
641 * is likely to save. The admin can still enable it through /sys.
643 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
644 transparent_hugepage_flags = 0;
650 hugepage_exit_sysfs(hugepage_kobj);
653 module_init(hugepage_init)
655 static int __init setup_transparent_hugepage(char *str)
660 if (!strcmp(str, "always")) {
661 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
662 &transparent_hugepage_flags);
663 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
664 &transparent_hugepage_flags);
666 } else if (!strcmp(str, "madvise")) {
667 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
668 &transparent_hugepage_flags);
669 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
670 &transparent_hugepage_flags);
672 } else if (!strcmp(str, "never")) {
673 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
674 &transparent_hugepage_flags);
675 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
676 &transparent_hugepage_flags);
682 "transparent_hugepage= cannot parse, ignored\n");
685 __setup("transparent_hugepage=", setup_transparent_hugepage);
687 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
689 if (likely(vma->vm_flags & VM_WRITE))
690 pmd = pmd_mkwrite(pmd);
694 static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
697 entry = mk_pmd(page, vma->vm_page_prot);
698 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
699 entry = pmd_mkhuge(entry);
703 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
704 struct vm_area_struct *vma,
705 unsigned long haddr, pmd_t *pmd,
710 VM_BUG_ON(!PageCompound(page));
711 pgtable = pte_alloc_one(mm, haddr);
712 if (unlikely(!pgtable))
715 clear_huge_page(page, haddr, HPAGE_PMD_NR);
716 __SetPageUptodate(page);
718 spin_lock(&mm->page_table_lock);
719 if (unlikely(!pmd_none(*pmd))) {
720 spin_unlock(&mm->page_table_lock);
721 mem_cgroup_uncharge_page(page);
723 pte_free(mm, pgtable);
726 entry = mk_huge_pmd(page, vma);
728 * The spinlocking to take the lru_lock inside
729 * page_add_new_anon_rmap() acts as a full memory
730 * barrier to be sure clear_huge_page writes become
731 * visible after the set_pmd_at() write.
733 page_add_new_anon_rmap(page, vma, haddr);
734 set_pmd_at(mm, haddr, pmd, entry);
735 pgtable_trans_huge_deposit(mm, pgtable);
736 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
738 spin_unlock(&mm->page_table_lock);
744 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
746 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
749 static inline struct page *alloc_hugepage_vma(int defrag,
750 struct vm_area_struct *vma,
751 unsigned long haddr, int nd,
754 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
755 HPAGE_PMD_ORDER, vma, haddr, nd);
759 static inline struct page *alloc_hugepage(int defrag)
761 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
766 static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
767 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
768 unsigned long zero_pfn)
773 entry = pfn_pmd(zero_pfn, vma->vm_page_prot);
774 entry = pmd_wrprotect(entry);
775 entry = pmd_mkhuge(entry);
776 set_pmd_at(mm, haddr, pmd, entry);
777 pgtable_trans_huge_deposit(mm, pgtable);
782 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
783 unsigned long address, pmd_t *pmd,
787 unsigned long haddr = address & HPAGE_PMD_MASK;
790 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
791 if (unlikely(anon_vma_prepare(vma)))
793 if (unlikely(khugepaged_enter(vma)))
795 if (!(flags & FAULT_FLAG_WRITE) &&
796 transparent_hugepage_use_zero_page()) {
798 unsigned long zero_pfn;
800 pgtable = pte_alloc_one(mm, haddr);
801 if (unlikely(!pgtable))
803 zero_pfn = get_huge_zero_page();
804 if (unlikely(!zero_pfn)) {
805 pte_free(mm, pgtable);
806 count_vm_event(THP_FAULT_FALLBACK);
809 spin_lock(&mm->page_table_lock);
810 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
812 spin_unlock(&mm->page_table_lock);
814 pte_free(mm, pgtable);
815 put_huge_zero_page();
819 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
820 vma, haddr, numa_node_id(), 0);
821 if (unlikely(!page)) {
822 count_vm_event(THP_FAULT_FALLBACK);
825 count_vm_event(THP_FAULT_ALLOC);
826 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
830 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
832 mem_cgroup_uncharge_page(page);
841 * Use __pte_alloc instead of pte_alloc_map, because we can't
842 * run pte_offset_map on the pmd, if an huge pmd could
843 * materialize from under us from a different thread.
845 if (unlikely(pmd_none(*pmd)) &&
846 unlikely(__pte_alloc(mm, vma, pmd, address)))
848 /* if an huge pmd materialized from under us just retry later */
849 if (unlikely(pmd_trans_huge(*pmd)))
852 * A regular pmd is established and it can't morph into a huge pmd
853 * from under us anymore at this point because we hold the mmap_sem
854 * read mode and khugepaged takes it in write mode. So now it's
855 * safe to run pte_offset_map().
857 pte = pte_offset_map(pmd, address);
858 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
861 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
862 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
863 struct vm_area_struct *vma)
865 struct page *src_page;
871 pgtable = pte_alloc_one(dst_mm, addr);
872 if (unlikely(!pgtable))
875 spin_lock(&dst_mm->page_table_lock);
876 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
880 if (unlikely(!pmd_trans_huge(pmd))) {
881 pte_free(dst_mm, pgtable);
885 * mm->page_table_lock is enough to be sure that huge zero pmd is not
886 * under splitting since we don't split the page itself, only pmd to
889 if (is_huge_zero_pmd(pmd)) {
890 unsigned long zero_pfn;
893 * get_huge_zero_page() will never allocate a new page here,
894 * since we already have a zero page to copy. It just takes a
897 zero_pfn = get_huge_zero_page();
898 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
900 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
904 if (unlikely(pmd_trans_splitting(pmd))) {
905 /* split huge page running from under us */
906 spin_unlock(&src_mm->page_table_lock);
907 spin_unlock(&dst_mm->page_table_lock);
908 pte_free(dst_mm, pgtable);
910 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
913 src_page = pmd_page(pmd);
914 VM_BUG_ON(!PageHead(src_page));
916 page_dup_rmap(src_page);
917 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
919 pmdp_set_wrprotect(src_mm, addr, src_pmd);
920 pmd = pmd_mkold(pmd_wrprotect(pmd));
921 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
922 pgtable_trans_huge_deposit(dst_mm, pgtable);
927 spin_unlock(&src_mm->page_table_lock);
928 spin_unlock(&dst_mm->page_table_lock);
933 void huge_pmd_set_accessed(struct mm_struct *mm,
934 struct vm_area_struct *vma,
935 unsigned long address,
936 pmd_t *pmd, pmd_t orig_pmd,
942 spin_lock(&mm->page_table_lock);
943 if (unlikely(!pmd_same(*pmd, orig_pmd)))
946 entry = pmd_mkyoung(orig_pmd);
947 haddr = address & HPAGE_PMD_MASK;
948 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
949 update_mmu_cache_pmd(vma, address, pmd);
952 spin_unlock(&mm->page_table_lock);
955 static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
956 struct vm_area_struct *vma, unsigned long address,
957 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
963 unsigned long mmun_start; /* For mmu_notifiers */
964 unsigned long mmun_end; /* For mmu_notifiers */
966 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
972 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
978 clear_user_highpage(page, address);
979 __SetPageUptodate(page);
982 mmun_end = haddr + HPAGE_PMD_SIZE;
983 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
985 spin_lock(&mm->page_table_lock);
986 if (unlikely(!pmd_same(*pmd, orig_pmd)))
989 pmdp_clear_flush(vma, haddr, pmd);
990 /* leave pmd empty until pte is filled */
992 pgtable = pgtable_trans_huge_withdraw(mm);
993 pmd_populate(mm, &_pmd, pgtable);
995 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
997 if (haddr == (address & PAGE_MASK)) {
998 entry = mk_pte(page, vma->vm_page_prot);
999 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1000 page_add_new_anon_rmap(page, vma, haddr);
1002 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1003 entry = pte_mkspecial(entry);
1005 pte = pte_offset_map(&_pmd, haddr);
1006 VM_BUG_ON(!pte_none(*pte));
1007 set_pte_at(mm, haddr, pte, entry);
1010 smp_wmb(); /* make pte visible before pmd */
1011 pmd_populate(mm, pmd, pgtable);
1012 spin_unlock(&mm->page_table_lock);
1013 put_huge_zero_page();
1014 inc_mm_counter(mm, MM_ANONPAGES);
1016 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1018 ret |= VM_FAULT_WRITE;
1022 spin_unlock(&mm->page_table_lock);
1023 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1024 mem_cgroup_uncharge_page(page);
1029 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1030 struct vm_area_struct *vma,
1031 unsigned long address,
1032 pmd_t *pmd, pmd_t orig_pmd,
1034 unsigned long haddr)
1039 struct page **pages;
1040 unsigned long mmun_start; /* For mmu_notifiers */
1041 unsigned long mmun_end; /* For mmu_notifiers */
1043 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1045 if (unlikely(!pages)) {
1046 ret |= VM_FAULT_OOM;
1050 for (i = 0; i < HPAGE_PMD_NR; i++) {
1051 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1053 vma, address, page_to_nid(page));
1054 if (unlikely(!pages[i] ||
1055 mem_cgroup_newpage_charge(pages[i], mm,
1059 mem_cgroup_uncharge_start();
1061 mem_cgroup_uncharge_page(pages[i]);
1064 mem_cgroup_uncharge_end();
1066 ret |= VM_FAULT_OOM;
1071 for (i = 0; i < HPAGE_PMD_NR; i++) {
1072 copy_user_highpage(pages[i], page + i,
1073 haddr + PAGE_SIZE * i, vma);
1074 __SetPageUptodate(pages[i]);
1079 mmun_end = haddr + HPAGE_PMD_SIZE;
1080 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1082 spin_lock(&mm->page_table_lock);
1083 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1084 goto out_free_pages;
1085 VM_BUG_ON(!PageHead(page));
1087 pmdp_clear_flush(vma, haddr, pmd);
1088 /* leave pmd empty until pte is filled */
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(mm, mmun_start, mmun_end);
1112 ret |= VM_FAULT_WRITE;
1119 spin_unlock(&mm->page_table_lock);
1120 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1121 mem_cgroup_uncharge_start();
1122 for (i = 0; i < HPAGE_PMD_NR; i++) {
1123 mem_cgroup_uncharge_page(pages[i]);
1126 mem_cgroup_uncharge_end();
1131 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1132 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1135 struct page *page = NULL, *new_page;
1136 unsigned long haddr;
1137 unsigned long mmun_start; /* For mmu_notifiers */
1138 unsigned long mmun_end; /* For mmu_notifiers */
1140 VM_BUG_ON(!vma->anon_vma);
1141 haddr = address & HPAGE_PMD_MASK;
1142 if (is_huge_zero_pmd(orig_pmd))
1144 spin_lock(&mm->page_table_lock);
1145 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1148 page = pmd_page(orig_pmd);
1149 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
1150 if (page_mapcount(page) == 1) {
1152 entry = pmd_mkyoung(orig_pmd);
1153 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1154 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
1155 update_mmu_cache_pmd(vma, address, pmd);
1156 ret |= VM_FAULT_WRITE;
1160 spin_unlock(&mm->page_table_lock);
1162 if (transparent_hugepage_enabled(vma) &&
1163 !transparent_hugepage_debug_cow())
1164 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
1165 vma, haddr, numa_node_id(), 0);
1169 if (unlikely(!new_page)) {
1170 count_vm_event(THP_FAULT_FALLBACK);
1171 if (is_huge_zero_pmd(orig_pmd)) {
1172 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
1173 address, pmd, orig_pmd, haddr);
1175 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1176 pmd, orig_pmd, page, haddr);
1177 if (ret & VM_FAULT_OOM)
1178 split_huge_page(page);
1183 count_vm_event(THP_FAULT_ALLOC);
1185 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1188 split_huge_page(page);
1191 ret |= VM_FAULT_OOM;
1195 if (is_huge_zero_pmd(orig_pmd))
1196 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1198 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
1199 __SetPageUptodate(new_page);
1202 mmun_end = haddr + HPAGE_PMD_SIZE;
1203 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1205 spin_lock(&mm->page_table_lock);
1208 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
1209 spin_unlock(&mm->page_table_lock);
1210 mem_cgroup_uncharge_page(new_page);
1215 entry = mk_huge_pmd(new_page, vma);
1216 pmdp_clear_flush(vma, haddr, pmd);
1217 page_add_new_anon_rmap(new_page, vma, haddr);
1218 set_pmd_at(mm, haddr, pmd, entry);
1219 update_mmu_cache_pmd(vma, address, pmd);
1220 if (is_huge_zero_pmd(orig_pmd)) {
1221 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
1222 put_huge_zero_page();
1224 VM_BUG_ON(!PageHead(page));
1225 page_remove_rmap(page);
1228 ret |= VM_FAULT_WRITE;
1230 spin_unlock(&mm->page_table_lock);
1232 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1236 spin_unlock(&mm->page_table_lock);
1240 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1245 struct mm_struct *mm = vma->vm_mm;
1246 struct page *page = NULL;
1248 assert_spin_locked(&mm->page_table_lock);
1250 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1253 /* Avoid dumping huge zero page */
1254 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1255 return ERR_PTR(-EFAULT);
1257 page = pmd_page(*pmd);
1258 VM_BUG_ON(!PageHead(page));
1259 if (flags & FOLL_TOUCH) {
1262 * We should set the dirty bit only for FOLL_WRITE but
1263 * for now the dirty bit in the pmd is meaningless.
1264 * And if the dirty bit will become meaningful and
1265 * we'll only set it with FOLL_WRITE, an atomic
1266 * set_bit will be required on the pmd to set the
1267 * young bit, instead of the current set_pmd_at.
1269 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1270 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1272 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1273 if (page->mapping && trylock_page(page)) {
1276 mlock_vma_page(page);
1280 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1281 VM_BUG_ON(!PageCompound(page));
1282 if (flags & FOLL_GET)
1283 get_page_foll(page);
1289 /* NUMA hinting page fault entry point for trans huge pmds */
1290 int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1291 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
1294 unsigned long haddr = addr & HPAGE_PMD_MASK;
1296 int current_nid = -1;
1299 spin_lock(&mm->page_table_lock);
1300 if (unlikely(!pmd_same(pmd, *pmdp)))
1303 page = pmd_page(pmd);
1305 current_nid = page_to_nid(page);
1306 count_vm_numa_event(NUMA_HINT_FAULTS);
1307 if (current_nid == numa_node_id())
1308 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
1310 target_nid = mpol_misplaced(page, vma, haddr);
1311 if (target_nid == -1) {
1316 /* Acquire the page lock to serialise THP migrations */
1317 spin_unlock(&mm->page_table_lock);
1320 /* Confirm the PTE did not while locked */
1321 spin_lock(&mm->page_table_lock);
1322 if (unlikely(!pmd_same(pmd, *pmdp))) {
1327 spin_unlock(&mm->page_table_lock);
1329 /* Migrate the THP to the requested node */
1330 migrated = migrate_misplaced_transhuge_page(mm, vma,
1331 pmdp, pmd, addr, page, target_nid);
1335 task_numa_fault(target_nid, HPAGE_PMD_NR, true);
1339 spin_lock(&mm->page_table_lock);
1340 if (unlikely(!pmd_same(pmd, *pmdp)))
1343 pmd = pmd_mknonnuma(pmd);
1344 set_pmd_at(mm, haddr, pmdp, pmd);
1345 VM_BUG_ON(pmd_numa(*pmdp));
1346 update_mmu_cache_pmd(vma, addr, pmdp);
1348 spin_unlock(&mm->page_table_lock);
1349 if (current_nid != -1)
1350 task_numa_fault(current_nid, HPAGE_PMD_NR, false);
1354 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1355 pmd_t *pmd, unsigned long addr)
1359 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1363 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1364 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1365 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1366 if (is_huge_zero_pmd(orig_pmd)) {
1368 spin_unlock(&tlb->mm->page_table_lock);
1369 put_huge_zero_page();
1371 page = pmd_page(orig_pmd);
1372 page_remove_rmap(page);
1373 VM_BUG_ON(page_mapcount(page) < 0);
1374 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1375 VM_BUG_ON(!PageHead(page));
1377 spin_unlock(&tlb->mm->page_table_lock);
1378 tlb_remove_page(tlb, page);
1380 pte_free(tlb->mm, pgtable);
1386 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1387 unsigned long addr, unsigned long end,
1392 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1394 * All logical pages in the range are present
1395 * if backed by a huge page.
1397 spin_unlock(&vma->vm_mm->page_table_lock);
1398 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1405 int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1406 unsigned long old_addr,
1407 unsigned long new_addr, unsigned long old_end,
1408 pmd_t *old_pmd, pmd_t *new_pmd)
1413 struct mm_struct *mm = vma->vm_mm;
1415 if ((old_addr & ~HPAGE_PMD_MASK) ||
1416 (new_addr & ~HPAGE_PMD_MASK) ||
1417 old_end - old_addr < HPAGE_PMD_SIZE ||
1418 (new_vma->vm_flags & VM_NOHUGEPAGE))
1422 * The destination pmd shouldn't be established, free_pgtables()
1423 * should have release it.
1425 if (WARN_ON(!pmd_none(*new_pmd))) {
1426 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1430 ret = __pmd_trans_huge_lock(old_pmd, vma);
1432 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1433 VM_BUG_ON(!pmd_none(*new_pmd));
1434 set_pmd_at(mm, new_addr, new_pmd, pmd);
1435 spin_unlock(&mm->page_table_lock);
1441 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1442 unsigned long addr, pgprot_t newprot, int prot_numa)
1444 struct mm_struct *mm = vma->vm_mm;
1447 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1449 entry = pmdp_get_and_clear(mm, addr, pmd);
1451 entry = pmd_modify(entry, newprot);
1452 BUG_ON(pmd_write(entry));
1454 struct page *page = pmd_page(*pmd);
1456 /* only check non-shared pages */
1457 if (page_mapcount(page) == 1 &&
1459 entry = pmd_mknuma(entry);
1462 set_pmd_at(mm, addr, pmd, entry);
1463 spin_unlock(&vma->vm_mm->page_table_lock);
1471 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1472 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1474 * Note that if it returns 1, this routine returns without unlocking page
1475 * table locks. So callers must unlock them.
1477 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1479 spin_lock(&vma->vm_mm->page_table_lock);
1480 if (likely(pmd_trans_huge(*pmd))) {
1481 if (unlikely(pmd_trans_splitting(*pmd))) {
1482 spin_unlock(&vma->vm_mm->page_table_lock);
1483 wait_split_huge_page(vma->anon_vma, pmd);
1486 /* Thp mapped by 'pmd' is stable, so we can
1487 * handle it as it is. */
1491 spin_unlock(&vma->vm_mm->page_table_lock);
1495 pmd_t *page_check_address_pmd(struct page *page,
1496 struct mm_struct *mm,
1497 unsigned long address,
1498 enum page_check_address_pmd_flag flag)
1500 pmd_t *pmd, *ret = NULL;
1502 if (address & ~HPAGE_PMD_MASK)
1505 pmd = mm_find_pmd(mm, address);
1510 if (pmd_page(*pmd) != page)
1513 * split_vma() may create temporary aliased mappings. There is
1514 * no risk as long as all huge pmd are found and have their
1515 * splitting bit set before __split_huge_page_refcount
1516 * runs. Finding the same huge pmd more than once during the
1517 * same rmap walk is not a problem.
1519 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1520 pmd_trans_splitting(*pmd))
1522 if (pmd_trans_huge(*pmd)) {
1523 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1524 !pmd_trans_splitting(*pmd));
1531 static int __split_huge_page_splitting(struct page *page,
1532 struct vm_area_struct *vma,
1533 unsigned long address)
1535 struct mm_struct *mm = vma->vm_mm;
1538 /* For mmu_notifiers */
1539 const unsigned long mmun_start = address;
1540 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
1542 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1543 spin_lock(&mm->page_table_lock);
1544 pmd = page_check_address_pmd(page, mm, address,
1545 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1548 * We can't temporarily set the pmd to null in order
1549 * to split it, the pmd must remain marked huge at all
1550 * times or the VM won't take the pmd_trans_huge paths
1551 * and it won't wait on the anon_vma->root->rwsem to
1552 * serialize against split_huge_page*.
1554 pmdp_splitting_flush(vma, address, pmd);
1557 spin_unlock(&mm->page_table_lock);
1558 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1563 static void __split_huge_page_refcount(struct page *page)
1566 struct zone *zone = page_zone(page);
1567 struct lruvec *lruvec;
1570 /* prevent PageLRU to go away from under us, and freeze lru stats */
1571 spin_lock_irq(&zone->lru_lock);
1572 lruvec = mem_cgroup_page_lruvec(page, zone);
1574 compound_lock(page);
1575 /* complete memcg works before add pages to LRU */
1576 mem_cgroup_split_huge_fixup(page);
1578 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1579 struct page *page_tail = page + i;
1581 /* tail_page->_mapcount cannot change */
1582 BUG_ON(page_mapcount(page_tail) < 0);
1583 tail_count += page_mapcount(page_tail);
1584 /* check for overflow */
1585 BUG_ON(tail_count < 0);
1586 BUG_ON(atomic_read(&page_tail->_count) != 0);
1588 * tail_page->_count is zero and not changing from
1589 * under us. But get_page_unless_zero() may be running
1590 * from under us on the tail_page. If we used
1591 * atomic_set() below instead of atomic_add(), we
1592 * would then run atomic_set() concurrently with
1593 * get_page_unless_zero(), and atomic_set() is
1594 * implemented in C not using locked ops. spin_unlock
1595 * on x86 sometime uses locked ops because of PPro
1596 * errata 66, 92, so unless somebody can guarantee
1597 * atomic_set() here would be safe on all archs (and
1598 * not only on x86), it's safer to use atomic_add().
1600 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1601 &page_tail->_count);
1603 /* after clearing PageTail the gup refcount can be released */
1607 * retain hwpoison flag of the poisoned tail page:
1608 * fix for the unsuitable process killed on Guest Machine(KVM)
1609 * by the memory-failure.
1611 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1612 page_tail->flags |= (page->flags &
1613 ((1L << PG_referenced) |
1614 (1L << PG_swapbacked) |
1615 (1L << PG_mlocked) |
1616 (1L << PG_uptodate)));
1617 page_tail->flags |= (1L << PG_dirty);
1619 /* clear PageTail before overwriting first_page */
1623 * __split_huge_page_splitting() already set the
1624 * splitting bit in all pmd that could map this
1625 * hugepage, that will ensure no CPU can alter the
1626 * mapcount on the head page. The mapcount is only
1627 * accounted in the head page and it has to be
1628 * transferred to all tail pages in the below code. So
1629 * for this code to be safe, the split the mapcount
1630 * can't change. But that doesn't mean userland can't
1631 * keep changing and reading the page contents while
1632 * we transfer the mapcount, so the pmd splitting
1633 * status is achieved setting a reserved bit in the
1634 * pmd, not by clearing the present bit.
1636 page_tail->_mapcount = page->_mapcount;
1638 BUG_ON(page_tail->mapping);
1639 page_tail->mapping = page->mapping;
1641 page_tail->index = page->index + i;
1642 page_nid_xchg_last(page_tail, page_nid_last(page));
1644 BUG_ON(!PageAnon(page_tail));
1645 BUG_ON(!PageUptodate(page_tail));
1646 BUG_ON(!PageDirty(page_tail));
1647 BUG_ON(!PageSwapBacked(page_tail));
1649 lru_add_page_tail(page, page_tail, lruvec);
1651 atomic_sub(tail_count, &page->_count);
1652 BUG_ON(atomic_read(&page->_count) <= 0);
1654 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1655 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1657 ClearPageCompound(page);
1658 compound_unlock(page);
1659 spin_unlock_irq(&zone->lru_lock);
1661 for (i = 1; i < HPAGE_PMD_NR; i++) {
1662 struct page *page_tail = page + i;
1663 BUG_ON(page_count(page_tail) <= 0);
1665 * Tail pages may be freed if there wasn't any mapping
1666 * like if add_to_swap() is running on a lru page that
1667 * had its mapping zapped. And freeing these pages
1668 * requires taking the lru_lock so we do the put_page
1669 * of the tail pages after the split is complete.
1671 put_page(page_tail);
1675 * Only the head page (now become a regular page) is required
1676 * to be pinned by the caller.
1678 BUG_ON(page_count(page) <= 0);
1681 static int __split_huge_page_map(struct page *page,
1682 struct vm_area_struct *vma,
1683 unsigned long address)
1685 struct mm_struct *mm = vma->vm_mm;
1689 unsigned long haddr;
1691 spin_lock(&mm->page_table_lock);
1692 pmd = page_check_address_pmd(page, mm, address,
1693 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1695 pgtable = pgtable_trans_huge_withdraw(mm);
1696 pmd_populate(mm, &_pmd, pgtable);
1699 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1701 BUG_ON(PageCompound(page+i));
1702 entry = mk_pte(page + i, vma->vm_page_prot);
1703 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1704 if (!pmd_write(*pmd))
1705 entry = pte_wrprotect(entry);
1707 BUG_ON(page_mapcount(page) != 1);
1708 if (!pmd_young(*pmd))
1709 entry = pte_mkold(entry);
1711 entry = pte_mknuma(entry);
1712 pte = pte_offset_map(&_pmd, haddr);
1713 BUG_ON(!pte_none(*pte));
1714 set_pte_at(mm, haddr, pte, entry);
1718 smp_wmb(); /* make pte visible before pmd */
1720 * Up to this point the pmd is present and huge and
1721 * userland has the whole access to the hugepage
1722 * during the split (which happens in place). If we
1723 * overwrite the pmd with the not-huge version
1724 * pointing to the pte here (which of course we could
1725 * if all CPUs were bug free), userland could trigger
1726 * a small page size TLB miss on the small sized TLB
1727 * while the hugepage TLB entry is still established
1728 * in the huge TLB. Some CPU doesn't like that. See
1729 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1730 * Erratum 383 on page 93. Intel should be safe but is
1731 * also warns that it's only safe if the permission
1732 * and cache attributes of the two entries loaded in
1733 * the two TLB is identical (which should be the case
1734 * here). But it is generally safer to never allow
1735 * small and huge TLB entries for the same virtual
1736 * address to be loaded simultaneously. So instead of
1737 * doing "pmd_populate(); flush_tlb_range();" we first
1738 * mark the current pmd notpresent (atomically because
1739 * here the pmd_trans_huge and pmd_trans_splitting
1740 * must remain set at all times on the pmd until the
1741 * split is complete for this pmd), then we flush the
1742 * SMP TLB and finally we write the non-huge version
1743 * of the pmd entry with pmd_populate.
1745 pmdp_invalidate(vma, address, pmd);
1746 pmd_populate(mm, pmd, pgtable);
1749 spin_unlock(&mm->page_table_lock);
1754 /* must be called with anon_vma->root->rwsem held */
1755 static void __split_huge_page(struct page *page,
1756 struct anon_vma *anon_vma)
1758 int mapcount, mapcount2;
1759 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1760 struct anon_vma_chain *avc;
1762 BUG_ON(!PageHead(page));
1763 BUG_ON(PageTail(page));
1766 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1767 struct vm_area_struct *vma = avc->vma;
1768 unsigned long addr = vma_address(page, vma);
1769 BUG_ON(is_vma_temporary_stack(vma));
1770 mapcount += __split_huge_page_splitting(page, vma, addr);
1773 * It is critical that new vmas are added to the tail of the
1774 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1775 * and establishes a child pmd before
1776 * __split_huge_page_splitting() freezes the parent pmd (so if
1777 * we fail to prevent copy_huge_pmd() from running until the
1778 * whole __split_huge_page() is complete), we will still see
1779 * the newly established pmd of the child later during the
1780 * walk, to be able to set it as pmd_trans_splitting too.
1782 if (mapcount != page_mapcount(page))
1783 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1784 mapcount, page_mapcount(page));
1785 BUG_ON(mapcount != page_mapcount(page));
1787 __split_huge_page_refcount(page);
1790 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1791 struct vm_area_struct *vma = avc->vma;
1792 unsigned long addr = vma_address(page, vma);
1793 BUG_ON(is_vma_temporary_stack(vma));
1794 mapcount2 += __split_huge_page_map(page, vma, addr);
1796 if (mapcount != mapcount2)
1797 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1798 mapcount, mapcount2, page_mapcount(page));
1799 BUG_ON(mapcount != mapcount2);
1802 int split_huge_page(struct page *page)
1804 struct anon_vma *anon_vma;
1807 BUG_ON(is_huge_zero_pfn(page_to_pfn(page)));
1808 BUG_ON(!PageAnon(page));
1811 * The caller does not necessarily hold an mmap_sem that would prevent
1812 * the anon_vma disappearing so we first we take a reference to it
1813 * and then lock the anon_vma for write. This is similar to
1814 * page_lock_anon_vma_read except the write lock is taken to serialise
1815 * against parallel split or collapse operations.
1817 anon_vma = page_get_anon_vma(page);
1820 anon_vma_lock_write(anon_vma);
1823 if (!PageCompound(page))
1826 BUG_ON(!PageSwapBacked(page));
1827 __split_huge_page(page, anon_vma);
1828 count_vm_event(THP_SPLIT);
1830 BUG_ON(PageCompound(page));
1832 anon_vma_unlock_write(anon_vma);
1833 put_anon_vma(anon_vma);
1838 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1840 int hugepage_madvise(struct vm_area_struct *vma,
1841 unsigned long *vm_flags, int advice)
1843 struct mm_struct *mm = vma->vm_mm;
1848 * Be somewhat over-protective like KSM for now!
1850 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1852 if (mm->def_flags & VM_NOHUGEPAGE)
1854 *vm_flags &= ~VM_NOHUGEPAGE;
1855 *vm_flags |= VM_HUGEPAGE;
1857 * If the vma become good for khugepaged to scan,
1858 * register it here without waiting a page fault that
1859 * may not happen any time soon.
1861 if (unlikely(khugepaged_enter_vma_merge(vma)))
1864 case MADV_NOHUGEPAGE:
1866 * Be somewhat over-protective like KSM for now!
1868 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1870 *vm_flags &= ~VM_HUGEPAGE;
1871 *vm_flags |= VM_NOHUGEPAGE;
1873 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1874 * this vma even if we leave the mm registered in khugepaged if
1875 * it got registered before VM_NOHUGEPAGE was set.
1883 static int __init khugepaged_slab_init(void)
1885 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1886 sizeof(struct mm_slot),
1887 __alignof__(struct mm_slot), 0, NULL);
1894 static inline struct mm_slot *alloc_mm_slot(void)
1896 if (!mm_slot_cache) /* initialization failed */
1898 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1901 static inline void free_mm_slot(struct mm_slot *mm_slot)
1903 kmem_cache_free(mm_slot_cache, mm_slot);
1906 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1908 struct mm_slot *mm_slot;
1909 struct hlist_node *node;
1911 hash_for_each_possible(mm_slots_hash, mm_slot, node, hash, (unsigned long)mm)
1912 if (mm == mm_slot->mm)
1918 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1919 struct mm_slot *mm_slot)
1922 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
1925 static inline int khugepaged_test_exit(struct mm_struct *mm)
1927 return atomic_read(&mm->mm_users) == 0;
1930 int __khugepaged_enter(struct mm_struct *mm)
1932 struct mm_slot *mm_slot;
1935 mm_slot = alloc_mm_slot();
1939 /* __khugepaged_exit() must not run from under us */
1940 VM_BUG_ON(khugepaged_test_exit(mm));
1941 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1942 free_mm_slot(mm_slot);
1946 spin_lock(&khugepaged_mm_lock);
1947 insert_to_mm_slots_hash(mm, mm_slot);
1949 * Insert just behind the scanning cursor, to let the area settle
1952 wakeup = list_empty(&khugepaged_scan.mm_head);
1953 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1954 spin_unlock(&khugepaged_mm_lock);
1956 atomic_inc(&mm->mm_count);
1958 wake_up_interruptible(&khugepaged_wait);
1963 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1965 unsigned long hstart, hend;
1968 * Not yet faulted in so we will register later in the
1969 * page fault if needed.
1973 /* khugepaged not yet working on file or special mappings */
1975 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1976 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1977 hend = vma->vm_end & HPAGE_PMD_MASK;
1979 return khugepaged_enter(vma);
1983 void __khugepaged_exit(struct mm_struct *mm)
1985 struct mm_slot *mm_slot;
1988 spin_lock(&khugepaged_mm_lock);
1989 mm_slot = get_mm_slot(mm);
1990 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1991 hash_del(&mm_slot->hash);
1992 list_del(&mm_slot->mm_node);
1995 spin_unlock(&khugepaged_mm_lock);
1998 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1999 free_mm_slot(mm_slot);
2001 } else if (mm_slot) {
2003 * This is required to serialize against
2004 * khugepaged_test_exit() (which is guaranteed to run
2005 * under mmap sem read mode). Stop here (after we
2006 * return all pagetables will be destroyed) until
2007 * khugepaged has finished working on the pagetables
2008 * under the mmap_sem.
2010 down_write(&mm->mmap_sem);
2011 up_write(&mm->mmap_sem);
2015 static void release_pte_page(struct page *page)
2017 /* 0 stands for page_is_file_cache(page) == false */
2018 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2020 putback_lru_page(page);
2023 static void release_pte_pages(pte_t *pte, pte_t *_pte)
2025 while (--_pte >= pte) {
2026 pte_t pteval = *_pte;
2027 if (!pte_none(pteval))
2028 release_pte_page(pte_page(pteval));
2032 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2033 unsigned long address,
2038 int referenced = 0, none = 0;
2039 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2040 _pte++, address += PAGE_SIZE) {
2041 pte_t pteval = *_pte;
2042 if (pte_none(pteval)) {
2043 if (++none <= khugepaged_max_ptes_none)
2048 if (!pte_present(pteval) || !pte_write(pteval))
2050 page = vm_normal_page(vma, address, pteval);
2051 if (unlikely(!page))
2054 VM_BUG_ON(PageCompound(page));
2055 BUG_ON(!PageAnon(page));
2056 VM_BUG_ON(!PageSwapBacked(page));
2058 /* cannot use mapcount: can't collapse if there's a gup pin */
2059 if (page_count(page) != 1)
2062 * We can do it before isolate_lru_page because the
2063 * page can't be freed from under us. NOTE: PG_lock
2064 * is needed to serialize against split_huge_page
2065 * when invoked from the VM.
2067 if (!trylock_page(page))
2070 * Isolate the page to avoid collapsing an hugepage
2071 * currently in use by the VM.
2073 if (isolate_lru_page(page)) {
2077 /* 0 stands for page_is_file_cache(page) == false */
2078 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2079 VM_BUG_ON(!PageLocked(page));
2080 VM_BUG_ON(PageLRU(page));
2082 /* If there is no mapped pte young don't collapse the page */
2083 if (pte_young(pteval) || PageReferenced(page) ||
2084 mmu_notifier_test_young(vma->vm_mm, address))
2087 if (likely(referenced))
2090 release_pte_pages(pte, _pte);
2094 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2095 struct vm_area_struct *vma,
2096 unsigned long address,
2100 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2101 pte_t pteval = *_pte;
2102 struct page *src_page;
2104 if (pte_none(pteval)) {
2105 clear_user_highpage(page, address);
2106 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2108 src_page = pte_page(pteval);
2109 copy_user_highpage(page, src_page, address, vma);
2110 VM_BUG_ON(page_mapcount(src_page) != 1);
2111 release_pte_page(src_page);
2113 * ptl mostly unnecessary, but preempt has to
2114 * be disabled to update the per-cpu stats
2115 * inside page_remove_rmap().
2119 * paravirt calls inside pte_clear here are
2122 pte_clear(vma->vm_mm, address, _pte);
2123 page_remove_rmap(src_page);
2125 free_page_and_swap_cache(src_page);
2128 address += PAGE_SIZE;
2133 static void khugepaged_alloc_sleep(void)
2135 wait_event_freezable_timeout(khugepaged_wait, false,
2136 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2140 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2142 if (IS_ERR(*hpage)) {
2148 khugepaged_alloc_sleep();
2149 } else if (*hpage) {
2158 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2159 struct vm_area_struct *vma, unsigned long address,
2164 * Allocate the page while the vma is still valid and under
2165 * the mmap_sem read mode so there is no memory allocation
2166 * later when we take the mmap_sem in write mode. This is more
2167 * friendly behavior (OTOH it may actually hide bugs) to
2168 * filesystems in userland with daemons allocating memory in
2169 * the userland I/O paths. Allocating memory with the
2170 * mmap_sem in read mode is good idea also to allow greater
2173 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2174 node, __GFP_OTHER_NODE);
2177 * After allocating the hugepage, release the mmap_sem read lock in
2178 * preparation for taking it in write mode.
2180 up_read(&mm->mmap_sem);
2181 if (unlikely(!*hpage)) {
2182 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2183 *hpage = ERR_PTR(-ENOMEM);
2187 count_vm_event(THP_COLLAPSE_ALLOC);
2191 static struct page *khugepaged_alloc_hugepage(bool *wait)
2196 hpage = alloc_hugepage(khugepaged_defrag());
2198 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2203 khugepaged_alloc_sleep();
2205 count_vm_event(THP_COLLAPSE_ALLOC);
2206 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2211 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2214 *hpage = khugepaged_alloc_hugepage(wait);
2216 if (unlikely(!*hpage))
2223 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2224 struct vm_area_struct *vma, unsigned long address,
2227 up_read(&mm->mmap_sem);
2233 static bool hugepage_vma_check(struct vm_area_struct *vma)
2235 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2236 (vma->vm_flags & VM_NOHUGEPAGE))
2239 if (!vma->anon_vma || vma->vm_ops)
2241 if (is_vma_temporary_stack(vma))
2243 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2247 static void collapse_huge_page(struct mm_struct *mm,
2248 unsigned long address,
2249 struct page **hpage,
2250 struct vm_area_struct *vma,
2256 struct page *new_page;
2259 unsigned long hstart, hend;
2260 unsigned long mmun_start; /* For mmu_notifiers */
2261 unsigned long mmun_end; /* For mmu_notifiers */
2263 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2265 /* release the mmap_sem read lock. */
2266 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2270 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
2274 * Prevent all access to pagetables with the exception of
2275 * gup_fast later hanlded by the ptep_clear_flush and the VM
2276 * handled by the anon_vma lock + PG_lock.
2278 down_write(&mm->mmap_sem);
2279 if (unlikely(khugepaged_test_exit(mm)))
2282 vma = find_vma(mm, address);
2283 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2284 hend = vma->vm_end & HPAGE_PMD_MASK;
2285 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2287 if (!hugepage_vma_check(vma))
2289 pmd = mm_find_pmd(mm, address);
2292 if (pmd_trans_huge(*pmd))
2295 anon_vma_lock_write(vma->anon_vma);
2297 pte = pte_offset_map(pmd, address);
2298 ptl = pte_lockptr(mm, pmd);
2300 mmun_start = address;
2301 mmun_end = address + HPAGE_PMD_SIZE;
2302 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2303 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2305 * After this gup_fast can't run anymore. This also removes
2306 * any huge TLB entry from the CPU so we won't allow
2307 * huge and small TLB entries for the same virtual address
2308 * to avoid the risk of CPU bugs in that area.
2310 _pmd = pmdp_clear_flush(vma, address, pmd);
2311 spin_unlock(&mm->page_table_lock);
2312 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2315 isolated = __collapse_huge_page_isolate(vma, address, pte);
2318 if (unlikely(!isolated)) {
2320 spin_lock(&mm->page_table_lock);
2321 BUG_ON(!pmd_none(*pmd));
2322 set_pmd_at(mm, address, pmd, _pmd);
2323 spin_unlock(&mm->page_table_lock);
2324 anon_vma_unlock_write(vma->anon_vma);
2329 * All pages are isolated and locked so anon_vma rmap
2330 * can't run anymore.
2332 anon_vma_unlock_write(vma->anon_vma);
2334 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2336 __SetPageUptodate(new_page);
2337 pgtable = pmd_pgtable(_pmd);
2339 _pmd = mk_huge_pmd(new_page, vma);
2342 * spin_lock() below is not the equivalent of smp_wmb(), so
2343 * this is needed to avoid the copy_huge_page writes to become
2344 * visible after the set_pmd_at() write.
2348 spin_lock(&mm->page_table_lock);
2349 BUG_ON(!pmd_none(*pmd));
2350 page_add_new_anon_rmap(new_page, vma, address);
2351 set_pmd_at(mm, address, pmd, _pmd);
2352 update_mmu_cache_pmd(vma, address, pmd);
2353 pgtable_trans_huge_deposit(mm, pgtable);
2354 spin_unlock(&mm->page_table_lock);
2358 khugepaged_pages_collapsed++;
2360 up_write(&mm->mmap_sem);
2364 mem_cgroup_uncharge_page(new_page);
2368 static int khugepaged_scan_pmd(struct mm_struct *mm,
2369 struct vm_area_struct *vma,
2370 unsigned long address,
2371 struct page **hpage)
2375 int ret = 0, referenced = 0, none = 0;
2377 unsigned long _address;
2379 int node = NUMA_NO_NODE;
2381 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2383 pmd = mm_find_pmd(mm, address);
2386 if (pmd_trans_huge(*pmd))
2389 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2390 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2391 _pte++, _address += PAGE_SIZE) {
2392 pte_t pteval = *_pte;
2393 if (pte_none(pteval)) {
2394 if (++none <= khugepaged_max_ptes_none)
2399 if (!pte_present(pteval) || !pte_write(pteval))
2401 page = vm_normal_page(vma, _address, pteval);
2402 if (unlikely(!page))
2405 * Chose the node of the first page. This could
2406 * be more sophisticated and look at more pages,
2407 * but isn't for now.
2409 if (node == NUMA_NO_NODE)
2410 node = page_to_nid(page);
2411 VM_BUG_ON(PageCompound(page));
2412 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2414 /* cannot use mapcount: can't collapse if there's a gup pin */
2415 if (page_count(page) != 1)
2417 if (pte_young(pteval) || PageReferenced(page) ||
2418 mmu_notifier_test_young(vma->vm_mm, address))
2424 pte_unmap_unlock(pte, ptl);
2426 /* collapse_huge_page will return with the mmap_sem released */
2427 collapse_huge_page(mm, address, hpage, vma, node);
2432 static void collect_mm_slot(struct mm_slot *mm_slot)
2434 struct mm_struct *mm = mm_slot->mm;
2436 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2438 if (khugepaged_test_exit(mm)) {
2440 hash_del(&mm_slot->hash);
2441 list_del(&mm_slot->mm_node);
2444 * Not strictly needed because the mm exited already.
2446 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2449 /* khugepaged_mm_lock actually not necessary for the below */
2450 free_mm_slot(mm_slot);
2455 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2456 struct page **hpage)
2457 __releases(&khugepaged_mm_lock)
2458 __acquires(&khugepaged_mm_lock)
2460 struct mm_slot *mm_slot;
2461 struct mm_struct *mm;
2462 struct vm_area_struct *vma;
2466 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2468 if (khugepaged_scan.mm_slot)
2469 mm_slot = khugepaged_scan.mm_slot;
2471 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2472 struct mm_slot, mm_node);
2473 khugepaged_scan.address = 0;
2474 khugepaged_scan.mm_slot = mm_slot;
2476 spin_unlock(&khugepaged_mm_lock);
2479 down_read(&mm->mmap_sem);
2480 if (unlikely(khugepaged_test_exit(mm)))
2483 vma = find_vma(mm, khugepaged_scan.address);
2486 for (; vma; vma = vma->vm_next) {
2487 unsigned long hstart, hend;
2490 if (unlikely(khugepaged_test_exit(mm))) {
2494 if (!hugepage_vma_check(vma)) {
2499 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2500 hend = vma->vm_end & HPAGE_PMD_MASK;
2503 if (khugepaged_scan.address > hend)
2505 if (khugepaged_scan.address < hstart)
2506 khugepaged_scan.address = hstart;
2507 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2509 while (khugepaged_scan.address < hend) {
2512 if (unlikely(khugepaged_test_exit(mm)))
2513 goto breakouterloop;
2515 VM_BUG_ON(khugepaged_scan.address < hstart ||
2516 khugepaged_scan.address + HPAGE_PMD_SIZE >
2518 ret = khugepaged_scan_pmd(mm, vma,
2519 khugepaged_scan.address,
2521 /* move to next address */
2522 khugepaged_scan.address += HPAGE_PMD_SIZE;
2523 progress += HPAGE_PMD_NR;
2525 /* we released mmap_sem so break loop */
2526 goto breakouterloop_mmap_sem;
2527 if (progress >= pages)
2528 goto breakouterloop;
2532 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2533 breakouterloop_mmap_sem:
2535 spin_lock(&khugepaged_mm_lock);
2536 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2538 * Release the current mm_slot if this mm is about to die, or
2539 * if we scanned all vmas of this mm.
2541 if (khugepaged_test_exit(mm) || !vma) {
2543 * Make sure that if mm_users is reaching zero while
2544 * khugepaged runs here, khugepaged_exit will find
2545 * mm_slot not pointing to the exiting mm.
2547 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2548 khugepaged_scan.mm_slot = list_entry(
2549 mm_slot->mm_node.next,
2550 struct mm_slot, mm_node);
2551 khugepaged_scan.address = 0;
2553 khugepaged_scan.mm_slot = NULL;
2554 khugepaged_full_scans++;
2557 collect_mm_slot(mm_slot);
2563 static int khugepaged_has_work(void)
2565 return !list_empty(&khugepaged_scan.mm_head) &&
2566 khugepaged_enabled();
2569 static int khugepaged_wait_event(void)
2571 return !list_empty(&khugepaged_scan.mm_head) ||
2572 kthread_should_stop();
2575 static void khugepaged_do_scan(void)
2577 struct page *hpage = NULL;
2578 unsigned int progress = 0, pass_through_head = 0;
2579 unsigned int pages = khugepaged_pages_to_scan;
2582 barrier(); /* write khugepaged_pages_to_scan to local stack */
2584 while (progress < pages) {
2585 if (!khugepaged_prealloc_page(&hpage, &wait))
2590 if (unlikely(kthread_should_stop() || freezing(current)))
2593 spin_lock(&khugepaged_mm_lock);
2594 if (!khugepaged_scan.mm_slot)
2595 pass_through_head++;
2596 if (khugepaged_has_work() &&
2597 pass_through_head < 2)
2598 progress += khugepaged_scan_mm_slot(pages - progress,
2602 spin_unlock(&khugepaged_mm_lock);
2605 if (!IS_ERR_OR_NULL(hpage))
2609 static void khugepaged_wait_work(void)
2613 if (khugepaged_has_work()) {
2614 if (!khugepaged_scan_sleep_millisecs)
2617 wait_event_freezable_timeout(khugepaged_wait,
2618 kthread_should_stop(),
2619 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2623 if (khugepaged_enabled())
2624 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2627 static int khugepaged(void *none)
2629 struct mm_slot *mm_slot;
2632 set_user_nice(current, 19);
2634 while (!kthread_should_stop()) {
2635 khugepaged_do_scan();
2636 khugepaged_wait_work();
2639 spin_lock(&khugepaged_mm_lock);
2640 mm_slot = khugepaged_scan.mm_slot;
2641 khugepaged_scan.mm_slot = NULL;
2643 collect_mm_slot(mm_slot);
2644 spin_unlock(&khugepaged_mm_lock);
2648 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2649 unsigned long haddr, pmd_t *pmd)
2651 struct mm_struct *mm = vma->vm_mm;
2656 pmdp_clear_flush(vma, haddr, pmd);
2657 /* leave pmd empty until pte is filled */
2659 pgtable = pgtable_trans_huge_withdraw(mm);
2660 pmd_populate(mm, &_pmd, pgtable);
2662 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2664 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2665 entry = pte_mkspecial(entry);
2666 pte = pte_offset_map(&_pmd, haddr);
2667 VM_BUG_ON(!pte_none(*pte));
2668 set_pte_at(mm, haddr, pte, entry);
2671 smp_wmb(); /* make pte visible before pmd */
2672 pmd_populate(mm, pmd, pgtable);
2673 put_huge_zero_page();
2676 void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2680 struct mm_struct *mm = vma->vm_mm;
2681 unsigned long haddr = address & HPAGE_PMD_MASK;
2682 unsigned long mmun_start; /* For mmu_notifiers */
2683 unsigned long mmun_end; /* For mmu_notifiers */
2685 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
2688 mmun_end = haddr + HPAGE_PMD_SIZE;
2689 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2690 spin_lock(&mm->page_table_lock);
2691 if (unlikely(!pmd_trans_huge(*pmd))) {
2692 spin_unlock(&mm->page_table_lock);
2693 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2696 if (is_huge_zero_pmd(*pmd)) {
2697 __split_huge_zero_page_pmd(vma, haddr, pmd);
2698 spin_unlock(&mm->page_table_lock);
2699 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2702 page = pmd_page(*pmd);
2703 VM_BUG_ON(!page_count(page));
2705 spin_unlock(&mm->page_table_lock);
2706 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2708 split_huge_page(page);
2711 BUG_ON(pmd_trans_huge(*pmd));
2714 void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2717 struct vm_area_struct *vma;
2719 vma = find_vma(mm, address);
2720 BUG_ON(vma == NULL);
2721 split_huge_page_pmd(vma, address, pmd);
2724 static void split_huge_page_address(struct mm_struct *mm,
2725 unsigned long address)
2729 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2731 pmd = mm_find_pmd(mm, address);
2735 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2736 * materialize from under us.
2738 split_huge_page_pmd_mm(mm, address, pmd);
2741 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2742 unsigned long start,
2747 * If the new start address isn't hpage aligned and it could
2748 * previously contain an hugepage: check if we need to split
2751 if (start & ~HPAGE_PMD_MASK &&
2752 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2753 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2754 split_huge_page_address(vma->vm_mm, start);
2757 * If the new end address isn't hpage aligned and it could
2758 * previously contain an hugepage: check if we need to split
2761 if (end & ~HPAGE_PMD_MASK &&
2762 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2763 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2764 split_huge_page_address(vma->vm_mm, end);
2767 * If we're also updating the vma->vm_next->vm_start, if the new
2768 * vm_next->vm_start isn't page aligned and it could previously
2769 * contain an hugepage: check if we need to split an huge pmd.
2771 if (adjust_next > 0) {
2772 struct vm_area_struct *next = vma->vm_next;
2773 unsigned long nstart = next->vm_start;
2774 nstart += adjust_next << PAGE_SHIFT;
2775 if (nstart & ~HPAGE_PMD_MASK &&
2776 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2777 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2778 split_huge_page_address(next->vm_mm, nstart);