virtio: console: Enable call to hvc_remove() on console port remove
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / huge_memory.c
blob8f7656194417821e97e0882e1d611d6721d3e688
1 /*
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.
6 */
8 #include <linux/mm.h>
9 #include <linux/sched.h>
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/mm_inline.h>
16 #include <linux/kthread.h>
17 #include <linux/khugepaged.h>
18 #include <linux/freezer.h>
19 #include <linux/mman.h>
20 #include <asm/tlb.h>
21 #include <asm/pgalloc.h>
22 #include "internal.h"
25 * By default transparent hugepage support is enabled for all mappings
26 * and khugepaged scans all mappings. Defrag is only invoked by
27 * khugepaged hugepage allocations and by page faults inside
28 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
29 * allocations.
31 unsigned long transparent_hugepage_flags __read_mostly =
32 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
33 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
34 #endif
35 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
36 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
37 #endif
38 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
39 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
41 /* default scan 8*512 pte (or vmas) every 30 second */
42 static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
43 static unsigned int khugepaged_pages_collapsed;
44 static unsigned int khugepaged_full_scans;
45 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
46 /* during fragmentation poll the hugepage allocator once every minute */
47 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
48 static struct task_struct *khugepaged_thread __read_mostly;
49 static DEFINE_MUTEX(khugepaged_mutex);
50 static DEFINE_SPINLOCK(khugepaged_mm_lock);
51 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
53 * default collapse hugepages if there is at least one pte mapped like
54 * it would have happened if the vma was large enough during page
55 * fault.
57 static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
59 static int khugepaged(void *none);
60 static int mm_slots_hash_init(void);
61 static int khugepaged_slab_init(void);
62 static void khugepaged_slab_free(void);
64 #define MM_SLOTS_HASH_HEADS 1024
65 static struct hlist_head *mm_slots_hash __read_mostly;
66 static struct kmem_cache *mm_slot_cache __read_mostly;
68 /**
69 * struct mm_slot - hash lookup from mm to mm_slot
70 * @hash: hash collision list
71 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
72 * @mm: the mm that this information is valid for
74 struct mm_slot {
75 struct hlist_node hash;
76 struct list_head mm_node;
77 struct mm_struct *mm;
80 /**
81 * struct khugepaged_scan - cursor for scanning
82 * @mm_head: the head of the mm list to scan
83 * @mm_slot: the current mm_slot we are scanning
84 * @address: the next address inside that to be scanned
86 * There is only the one khugepaged_scan instance of this cursor structure.
88 struct khugepaged_scan {
89 struct list_head mm_head;
90 struct mm_slot *mm_slot;
91 unsigned long address;
92 } khugepaged_scan = {
93 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
97 static int set_recommended_min_free_kbytes(void)
99 struct zone *zone;
100 int nr_zones = 0;
101 unsigned long recommended_min;
102 extern int min_free_kbytes;
104 if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG,
105 &transparent_hugepage_flags) &&
106 !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
107 &transparent_hugepage_flags))
108 return 0;
110 for_each_populated_zone(zone)
111 nr_zones++;
113 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
114 recommended_min = pageblock_nr_pages * nr_zones * 2;
117 * Make sure that on average at least two pageblocks are almost free
118 * of another type, one for a migratetype to fall back to and a
119 * second to avoid subsequent fallbacks of other types There are 3
120 * MIGRATE_TYPES we care about.
122 recommended_min += pageblock_nr_pages * nr_zones *
123 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
125 /* don't ever allow to reserve more than 5% of the lowmem */
126 recommended_min = min(recommended_min,
127 (unsigned long) nr_free_buffer_pages() / 20);
128 recommended_min <<= (PAGE_SHIFT-10);
130 if (recommended_min > min_free_kbytes)
131 min_free_kbytes = recommended_min;
132 setup_per_zone_wmarks();
133 return 0;
135 late_initcall(set_recommended_min_free_kbytes);
137 static int start_khugepaged(void)
139 int err = 0;
140 if (khugepaged_enabled()) {
141 int wakeup;
142 if (unlikely(!mm_slot_cache || !mm_slots_hash)) {
143 err = -ENOMEM;
144 goto out;
146 mutex_lock(&khugepaged_mutex);
147 if (!khugepaged_thread)
148 khugepaged_thread = kthread_run(khugepaged, NULL,
149 "khugepaged");
150 if (unlikely(IS_ERR(khugepaged_thread))) {
151 printk(KERN_ERR
152 "khugepaged: kthread_run(khugepaged) failed\n");
153 err = PTR_ERR(khugepaged_thread);
154 khugepaged_thread = NULL;
156 wakeup = !list_empty(&khugepaged_scan.mm_head);
157 mutex_unlock(&khugepaged_mutex);
158 if (wakeup)
159 wake_up_interruptible(&khugepaged_wait);
161 set_recommended_min_free_kbytes();
162 } else
163 /* wakeup to exit */
164 wake_up_interruptible(&khugepaged_wait);
165 out:
166 return err;
169 #ifdef CONFIG_SYSFS
171 static ssize_t double_flag_show(struct kobject *kobj,
172 struct kobj_attribute *attr, char *buf,
173 enum transparent_hugepage_flag enabled,
174 enum transparent_hugepage_flag req_madv)
176 if (test_bit(enabled, &transparent_hugepage_flags)) {
177 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
178 return sprintf(buf, "[always] madvise never\n");
179 } else if (test_bit(req_madv, &transparent_hugepage_flags))
180 return sprintf(buf, "always [madvise] never\n");
181 else
182 return sprintf(buf, "always madvise [never]\n");
184 static ssize_t double_flag_store(struct kobject *kobj,
185 struct kobj_attribute *attr,
186 const char *buf, size_t count,
187 enum transparent_hugepage_flag enabled,
188 enum transparent_hugepage_flag req_madv)
190 if (!memcmp("always", buf,
191 min(sizeof("always")-1, count))) {
192 set_bit(enabled, &transparent_hugepage_flags);
193 clear_bit(req_madv, &transparent_hugepage_flags);
194 } else if (!memcmp("madvise", buf,
195 min(sizeof("madvise")-1, count))) {
196 clear_bit(enabled, &transparent_hugepage_flags);
197 set_bit(req_madv, &transparent_hugepage_flags);
198 } else if (!memcmp("never", buf,
199 min(sizeof("never")-1, count))) {
200 clear_bit(enabled, &transparent_hugepage_flags);
201 clear_bit(req_madv, &transparent_hugepage_flags);
202 } else
203 return -EINVAL;
205 return count;
208 static ssize_t enabled_show(struct kobject *kobj,
209 struct kobj_attribute *attr, char *buf)
211 return double_flag_show(kobj, attr, buf,
212 TRANSPARENT_HUGEPAGE_FLAG,
213 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
215 static ssize_t enabled_store(struct kobject *kobj,
216 struct kobj_attribute *attr,
217 const char *buf, size_t count)
219 ssize_t ret;
221 ret = double_flag_store(kobj, attr, buf, count,
222 TRANSPARENT_HUGEPAGE_FLAG,
223 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
225 if (ret > 0) {
226 int err = start_khugepaged();
227 if (err)
228 ret = err;
231 if (ret > 0 &&
232 (test_bit(TRANSPARENT_HUGEPAGE_FLAG,
233 &transparent_hugepage_flags) ||
234 test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
235 &transparent_hugepage_flags)))
236 set_recommended_min_free_kbytes();
238 return ret;
240 static struct kobj_attribute enabled_attr =
241 __ATTR(enabled, 0644, enabled_show, enabled_store);
243 static ssize_t single_flag_show(struct kobject *kobj,
244 struct kobj_attribute *attr, char *buf,
245 enum transparent_hugepage_flag flag)
247 return sprintf(buf, "%d\n",
248 !!test_bit(flag, &transparent_hugepage_flags));
251 static ssize_t single_flag_store(struct kobject *kobj,
252 struct kobj_attribute *attr,
253 const char *buf, size_t count,
254 enum transparent_hugepage_flag flag)
256 unsigned long value;
257 int ret;
259 ret = kstrtoul(buf, 10, &value);
260 if (ret < 0)
261 return ret;
262 if (value > 1)
263 return -EINVAL;
265 if (value)
266 set_bit(flag, &transparent_hugepage_flags);
267 else
268 clear_bit(flag, &transparent_hugepage_flags);
270 return count;
274 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
275 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
276 * memory just to allocate one more hugepage.
278 static ssize_t defrag_show(struct kobject *kobj,
279 struct kobj_attribute *attr, char *buf)
281 return double_flag_show(kobj, attr, buf,
282 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
283 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
285 static ssize_t defrag_store(struct kobject *kobj,
286 struct kobj_attribute *attr,
287 const char *buf, size_t count)
289 return double_flag_store(kobj, attr, buf, count,
290 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
291 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
293 static struct kobj_attribute defrag_attr =
294 __ATTR(defrag, 0644, defrag_show, defrag_store);
296 #ifdef CONFIG_DEBUG_VM
297 static ssize_t debug_cow_show(struct kobject *kobj,
298 struct kobj_attribute *attr, char *buf)
300 return single_flag_show(kobj, attr, buf,
301 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
303 static ssize_t debug_cow_store(struct kobject *kobj,
304 struct kobj_attribute *attr,
305 const char *buf, size_t count)
307 return single_flag_store(kobj, attr, buf, count,
308 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
310 static struct kobj_attribute debug_cow_attr =
311 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
312 #endif /* CONFIG_DEBUG_VM */
314 static struct attribute *hugepage_attr[] = {
315 &enabled_attr.attr,
316 &defrag_attr.attr,
317 #ifdef CONFIG_DEBUG_VM
318 &debug_cow_attr.attr,
319 #endif
320 NULL,
323 static struct attribute_group hugepage_attr_group = {
324 .attrs = hugepage_attr,
327 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
328 struct kobj_attribute *attr,
329 char *buf)
331 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
334 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
335 struct kobj_attribute *attr,
336 const char *buf, size_t count)
338 unsigned long msecs;
339 int err;
341 err = strict_strtoul(buf, 10, &msecs);
342 if (err || msecs > UINT_MAX)
343 return -EINVAL;
345 khugepaged_scan_sleep_millisecs = msecs;
346 wake_up_interruptible(&khugepaged_wait);
348 return count;
350 static struct kobj_attribute scan_sleep_millisecs_attr =
351 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
352 scan_sleep_millisecs_store);
354 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
355 struct kobj_attribute *attr,
356 char *buf)
358 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
361 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
362 struct kobj_attribute *attr,
363 const char *buf, size_t count)
365 unsigned long msecs;
366 int err;
368 err = strict_strtoul(buf, 10, &msecs);
369 if (err || msecs > UINT_MAX)
370 return -EINVAL;
372 khugepaged_alloc_sleep_millisecs = msecs;
373 wake_up_interruptible(&khugepaged_wait);
375 return count;
377 static struct kobj_attribute alloc_sleep_millisecs_attr =
378 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
379 alloc_sleep_millisecs_store);
381 static ssize_t pages_to_scan_show(struct kobject *kobj,
382 struct kobj_attribute *attr,
383 char *buf)
385 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
387 static ssize_t pages_to_scan_store(struct kobject *kobj,
388 struct kobj_attribute *attr,
389 const char *buf, size_t count)
391 int err;
392 unsigned long pages;
394 err = strict_strtoul(buf, 10, &pages);
395 if (err || !pages || pages > UINT_MAX)
396 return -EINVAL;
398 khugepaged_pages_to_scan = pages;
400 return count;
402 static struct kobj_attribute pages_to_scan_attr =
403 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
404 pages_to_scan_store);
406 static ssize_t pages_collapsed_show(struct kobject *kobj,
407 struct kobj_attribute *attr,
408 char *buf)
410 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
412 static struct kobj_attribute pages_collapsed_attr =
413 __ATTR_RO(pages_collapsed);
415 static ssize_t full_scans_show(struct kobject *kobj,
416 struct kobj_attribute *attr,
417 char *buf)
419 return sprintf(buf, "%u\n", khugepaged_full_scans);
421 static struct kobj_attribute full_scans_attr =
422 __ATTR_RO(full_scans);
424 static ssize_t khugepaged_defrag_show(struct kobject *kobj,
425 struct kobj_attribute *attr, char *buf)
427 return single_flag_show(kobj, attr, buf,
428 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
430 static ssize_t khugepaged_defrag_store(struct kobject *kobj,
431 struct kobj_attribute *attr,
432 const char *buf, size_t count)
434 return single_flag_store(kobj, attr, buf, count,
435 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
437 static struct kobj_attribute khugepaged_defrag_attr =
438 __ATTR(defrag, 0644, khugepaged_defrag_show,
439 khugepaged_defrag_store);
442 * max_ptes_none controls if khugepaged should collapse hugepages over
443 * any unmapped ptes in turn potentially increasing the memory
444 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
445 * reduce the available free memory in the system as it
446 * runs. Increasing max_ptes_none will instead potentially reduce the
447 * free memory in the system during the khugepaged scan.
449 static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
450 struct kobj_attribute *attr,
451 char *buf)
453 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
455 static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
456 struct kobj_attribute *attr,
457 const char *buf, size_t count)
459 int err;
460 unsigned long max_ptes_none;
462 err = strict_strtoul(buf, 10, &max_ptes_none);
463 if (err || max_ptes_none > HPAGE_PMD_NR-1)
464 return -EINVAL;
466 khugepaged_max_ptes_none = max_ptes_none;
468 return count;
470 static struct kobj_attribute khugepaged_max_ptes_none_attr =
471 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
472 khugepaged_max_ptes_none_store);
474 static struct attribute *khugepaged_attr[] = {
475 &khugepaged_defrag_attr.attr,
476 &khugepaged_max_ptes_none_attr.attr,
477 &pages_to_scan_attr.attr,
478 &pages_collapsed_attr.attr,
479 &full_scans_attr.attr,
480 &scan_sleep_millisecs_attr.attr,
481 &alloc_sleep_millisecs_attr.attr,
482 NULL,
485 static struct attribute_group khugepaged_attr_group = {
486 .attrs = khugepaged_attr,
487 .name = "khugepaged",
489 #endif /* CONFIG_SYSFS */
491 static int __init hugepage_init(void)
493 int err;
494 #ifdef CONFIG_SYSFS
495 static struct kobject *hugepage_kobj;
496 #endif
498 err = -EINVAL;
499 if (!has_transparent_hugepage()) {
500 transparent_hugepage_flags = 0;
501 goto out;
504 #ifdef CONFIG_SYSFS
505 err = -ENOMEM;
506 hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
507 if (unlikely(!hugepage_kobj)) {
508 printk(KERN_ERR "hugepage: failed kobject create\n");
509 goto out;
512 err = sysfs_create_group(hugepage_kobj, &hugepage_attr_group);
513 if (err) {
514 printk(KERN_ERR "hugepage: failed register hugeage group\n");
515 goto out;
518 err = sysfs_create_group(hugepage_kobj, &khugepaged_attr_group);
519 if (err) {
520 printk(KERN_ERR "hugepage: failed register hugeage group\n");
521 goto out;
523 #endif
525 err = khugepaged_slab_init();
526 if (err)
527 goto out;
529 err = mm_slots_hash_init();
530 if (err) {
531 khugepaged_slab_free();
532 goto out;
536 * By default disable transparent hugepages on smaller systems,
537 * where the extra memory used could hurt more than TLB overhead
538 * is likely to save. The admin can still enable it through /sys.
540 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
541 transparent_hugepage_flags = 0;
543 start_khugepaged();
545 set_recommended_min_free_kbytes();
547 out:
548 return err;
550 module_init(hugepage_init)
552 static int __init setup_transparent_hugepage(char *str)
554 int ret = 0;
555 if (!str)
556 goto out;
557 if (!strcmp(str, "always")) {
558 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
559 &transparent_hugepage_flags);
560 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
561 &transparent_hugepage_flags);
562 ret = 1;
563 } else if (!strcmp(str, "madvise")) {
564 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
565 &transparent_hugepage_flags);
566 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
567 &transparent_hugepage_flags);
568 ret = 1;
569 } else if (!strcmp(str, "never")) {
570 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
571 &transparent_hugepage_flags);
572 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
573 &transparent_hugepage_flags);
574 ret = 1;
576 out:
577 if (!ret)
578 printk(KERN_WARNING
579 "transparent_hugepage= cannot parse, ignored\n");
580 return ret;
582 __setup("transparent_hugepage=", setup_transparent_hugepage);
584 static void prepare_pmd_huge_pte(pgtable_t pgtable,
585 struct mm_struct *mm)
587 assert_spin_locked(&mm->page_table_lock);
589 /* FIFO */
590 if (!mm->pmd_huge_pte)
591 INIT_LIST_HEAD(&pgtable->lru);
592 else
593 list_add(&pgtable->lru, &mm->pmd_huge_pte->lru);
594 mm->pmd_huge_pte = pgtable;
597 static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
599 if (likely(vma->vm_flags & VM_WRITE))
600 pmd = pmd_mkwrite(pmd);
601 return pmd;
604 static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
605 struct vm_area_struct *vma,
606 unsigned long haddr, pmd_t *pmd,
607 struct page *page)
609 int ret = 0;
610 pgtable_t pgtable;
612 VM_BUG_ON(!PageCompound(page));
613 pgtable = pte_alloc_one(mm, haddr);
614 if (unlikely(!pgtable)) {
615 mem_cgroup_uncharge_page(page);
616 put_page(page);
617 return VM_FAULT_OOM;
620 clear_huge_page(page, haddr, HPAGE_PMD_NR);
621 __SetPageUptodate(page);
623 spin_lock(&mm->page_table_lock);
624 if (unlikely(!pmd_none(*pmd))) {
625 spin_unlock(&mm->page_table_lock);
626 mem_cgroup_uncharge_page(page);
627 put_page(page);
628 pte_free(mm, pgtable);
629 } else {
630 pmd_t entry;
631 entry = mk_pmd(page, vma->vm_page_prot);
632 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
633 entry = pmd_mkhuge(entry);
635 * The spinlocking to take the lru_lock inside
636 * page_add_new_anon_rmap() acts as a full memory
637 * barrier to be sure clear_huge_page writes become
638 * visible after the set_pmd_at() write.
640 page_add_new_anon_rmap(page, vma, haddr);
641 set_pmd_at(mm, haddr, pmd, entry);
642 prepare_pmd_huge_pte(pgtable, mm);
643 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
644 spin_unlock(&mm->page_table_lock);
647 return ret;
650 static inline gfp_t alloc_hugepage_gfpmask(int defrag)
652 return GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT);
655 static inline struct page *alloc_hugepage_vma(int defrag,
656 struct vm_area_struct *vma,
657 unsigned long haddr, int nd)
659 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag),
660 HPAGE_PMD_ORDER, vma, haddr, nd);
663 #ifndef CONFIG_NUMA
664 static inline struct page *alloc_hugepage(int defrag)
666 return alloc_pages(alloc_hugepage_gfpmask(defrag),
667 HPAGE_PMD_ORDER);
669 #endif
671 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
672 unsigned long address, pmd_t *pmd,
673 unsigned int flags)
675 struct page *page;
676 unsigned long haddr = address & HPAGE_PMD_MASK;
677 pte_t *pte;
679 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
680 if (unlikely(anon_vma_prepare(vma)))
681 return VM_FAULT_OOM;
682 if (unlikely(khugepaged_enter(vma)))
683 return VM_FAULT_OOM;
684 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
685 vma, haddr, numa_node_id());
686 if (unlikely(!page))
687 goto out;
688 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
689 put_page(page);
690 goto out;
693 return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
695 out:
697 * Use __pte_alloc instead of pte_alloc_map, because we can't
698 * run pte_offset_map on the pmd, if an huge pmd could
699 * materialize from under us from a different thread.
701 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
702 return VM_FAULT_OOM;
703 /* if an huge pmd materialized from under us just retry later */
704 if (unlikely(pmd_trans_huge(*pmd)))
705 return 0;
707 * A regular pmd is established and it can't morph into a huge pmd
708 * from under us anymore at this point because we hold the mmap_sem
709 * read mode and khugepaged takes it in write mode. So now it's
710 * safe to run pte_offset_map().
712 pte = pte_offset_map(pmd, address);
713 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
716 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
717 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
718 struct vm_area_struct *vma)
720 struct page *src_page;
721 pmd_t pmd;
722 pgtable_t pgtable;
723 int ret;
725 ret = -ENOMEM;
726 pgtable = pte_alloc_one(dst_mm, addr);
727 if (unlikely(!pgtable))
728 goto out;
730 spin_lock(&dst_mm->page_table_lock);
731 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
733 ret = -EAGAIN;
734 pmd = *src_pmd;
735 if (unlikely(!pmd_trans_huge(pmd))) {
736 pte_free(dst_mm, pgtable);
737 goto out_unlock;
739 if (unlikely(pmd_trans_splitting(pmd))) {
740 /* split huge page running from under us */
741 spin_unlock(&src_mm->page_table_lock);
742 spin_unlock(&dst_mm->page_table_lock);
743 pte_free(dst_mm, pgtable);
745 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
746 goto out;
748 src_page = pmd_page(pmd);
749 VM_BUG_ON(!PageHead(src_page));
750 get_page(src_page);
751 page_dup_rmap(src_page);
752 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
754 pmdp_set_wrprotect(src_mm, addr, src_pmd);
755 pmd = pmd_mkold(pmd_wrprotect(pmd));
756 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
757 prepare_pmd_huge_pte(pgtable, dst_mm);
759 ret = 0;
760 out_unlock:
761 spin_unlock(&src_mm->page_table_lock);
762 spin_unlock(&dst_mm->page_table_lock);
763 out:
764 return ret;
767 /* no "address" argument so destroys page coloring of some arch */
768 pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
770 pgtable_t pgtable;
772 assert_spin_locked(&mm->page_table_lock);
774 /* FIFO */
775 pgtable = mm->pmd_huge_pte;
776 if (list_empty(&pgtable->lru))
777 mm->pmd_huge_pte = NULL;
778 else {
779 mm->pmd_huge_pte = list_entry(pgtable->lru.next,
780 struct page, lru);
781 list_del(&pgtable->lru);
783 return pgtable;
786 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
787 struct vm_area_struct *vma,
788 unsigned long address,
789 pmd_t *pmd, pmd_t orig_pmd,
790 struct page *page,
791 unsigned long haddr)
793 pgtable_t pgtable;
794 pmd_t _pmd;
795 int ret = 0, i;
796 struct page **pages;
798 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
799 GFP_KERNEL);
800 if (unlikely(!pages)) {
801 ret |= VM_FAULT_OOM;
802 goto out;
805 for (i = 0; i < HPAGE_PMD_NR; i++) {
806 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE,
807 vma, address, page_to_nid(page));
808 if (unlikely(!pages[i] ||
809 mem_cgroup_newpage_charge(pages[i], mm,
810 GFP_KERNEL))) {
811 if (pages[i])
812 put_page(pages[i]);
813 mem_cgroup_uncharge_start();
814 while (--i >= 0) {
815 mem_cgroup_uncharge_page(pages[i]);
816 put_page(pages[i]);
818 mem_cgroup_uncharge_end();
819 kfree(pages);
820 ret |= VM_FAULT_OOM;
821 goto out;
825 for (i = 0; i < HPAGE_PMD_NR; i++) {
826 copy_user_highpage(pages[i], page + i,
827 haddr + PAGE_SHIFT*i, vma);
828 __SetPageUptodate(pages[i]);
829 cond_resched();
832 spin_lock(&mm->page_table_lock);
833 if (unlikely(!pmd_same(*pmd, orig_pmd)))
834 goto out_free_pages;
835 VM_BUG_ON(!PageHead(page));
837 pmdp_clear_flush_notify(vma, haddr, pmd);
838 /* leave pmd empty until pte is filled */
840 pgtable = get_pmd_huge_pte(mm);
841 pmd_populate(mm, &_pmd, pgtable);
843 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
844 pte_t *pte, entry;
845 entry = mk_pte(pages[i], vma->vm_page_prot);
846 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
847 page_add_new_anon_rmap(pages[i], vma, haddr);
848 pte = pte_offset_map(&_pmd, haddr);
849 VM_BUG_ON(!pte_none(*pte));
850 set_pte_at(mm, haddr, pte, entry);
851 pte_unmap(pte);
853 kfree(pages);
855 mm->nr_ptes++;
856 smp_wmb(); /* make pte visible before pmd */
857 pmd_populate(mm, pmd, pgtable);
858 page_remove_rmap(page);
859 spin_unlock(&mm->page_table_lock);
861 ret |= VM_FAULT_WRITE;
862 put_page(page);
864 out:
865 return ret;
867 out_free_pages:
868 spin_unlock(&mm->page_table_lock);
869 mem_cgroup_uncharge_start();
870 for (i = 0; i < HPAGE_PMD_NR; i++) {
871 mem_cgroup_uncharge_page(pages[i]);
872 put_page(pages[i]);
874 mem_cgroup_uncharge_end();
875 kfree(pages);
876 goto out;
879 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
880 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
882 int ret = 0;
883 struct page *page, *new_page;
884 unsigned long haddr;
886 VM_BUG_ON(!vma->anon_vma);
887 spin_lock(&mm->page_table_lock);
888 if (unlikely(!pmd_same(*pmd, orig_pmd)))
889 goto out_unlock;
891 page = pmd_page(orig_pmd);
892 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
893 haddr = address & HPAGE_PMD_MASK;
894 if (page_mapcount(page) == 1) {
895 pmd_t entry;
896 entry = pmd_mkyoung(orig_pmd);
897 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
898 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
899 update_mmu_cache(vma, address, entry);
900 ret |= VM_FAULT_WRITE;
901 goto out_unlock;
903 get_page(page);
904 spin_unlock(&mm->page_table_lock);
906 if (transparent_hugepage_enabled(vma) &&
907 !transparent_hugepage_debug_cow())
908 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
909 vma, haddr, numa_node_id());
910 else
911 new_page = NULL;
913 if (unlikely(!new_page)) {
914 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
915 pmd, orig_pmd, page, haddr);
916 put_page(page);
917 goto out;
920 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
921 put_page(new_page);
922 put_page(page);
923 ret |= VM_FAULT_OOM;
924 goto out;
927 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
928 __SetPageUptodate(new_page);
930 spin_lock(&mm->page_table_lock);
931 put_page(page);
932 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
933 mem_cgroup_uncharge_page(new_page);
934 put_page(new_page);
935 } else {
936 pmd_t entry;
937 VM_BUG_ON(!PageHead(page));
938 entry = mk_pmd(new_page, vma->vm_page_prot);
939 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
940 entry = pmd_mkhuge(entry);
941 pmdp_clear_flush_notify(vma, haddr, pmd);
942 page_add_new_anon_rmap(new_page, vma, haddr);
943 set_pmd_at(mm, haddr, pmd, entry);
944 update_mmu_cache(vma, address, entry);
945 page_remove_rmap(page);
946 put_page(page);
947 ret |= VM_FAULT_WRITE;
949 out_unlock:
950 spin_unlock(&mm->page_table_lock);
951 out:
952 return ret;
955 struct page *follow_trans_huge_pmd(struct mm_struct *mm,
956 unsigned long addr,
957 pmd_t *pmd,
958 unsigned int flags)
960 struct page *page = NULL;
962 assert_spin_locked(&mm->page_table_lock);
964 if (flags & FOLL_WRITE && !pmd_write(*pmd))
965 goto out;
967 page = pmd_page(*pmd);
968 VM_BUG_ON(!PageHead(page));
969 if (flags & FOLL_TOUCH) {
970 pmd_t _pmd;
972 * We should set the dirty bit only for FOLL_WRITE but
973 * for now the dirty bit in the pmd is meaningless.
974 * And if the dirty bit will become meaningful and
975 * we'll only set it with FOLL_WRITE, an atomic
976 * set_bit will be required on the pmd to set the
977 * young bit, instead of the current set_pmd_at.
979 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
980 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
982 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
983 VM_BUG_ON(!PageCompound(page));
984 if (flags & FOLL_GET)
985 get_page(page);
987 out:
988 return page;
991 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
992 pmd_t *pmd)
994 int ret = 0;
996 spin_lock(&tlb->mm->page_table_lock);
997 if (likely(pmd_trans_huge(*pmd))) {
998 if (unlikely(pmd_trans_splitting(*pmd))) {
999 spin_unlock(&tlb->mm->page_table_lock);
1000 wait_split_huge_page(vma->anon_vma,
1001 pmd);
1002 } else {
1003 struct page *page;
1004 pgtable_t pgtable;
1005 pgtable = get_pmd_huge_pte(tlb->mm);
1006 page = pmd_page(*pmd);
1007 pmd_clear(pmd);
1008 page_remove_rmap(page);
1009 VM_BUG_ON(page_mapcount(page) < 0);
1010 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1011 VM_BUG_ON(!PageHead(page));
1012 spin_unlock(&tlb->mm->page_table_lock);
1013 tlb_remove_page(tlb, page);
1014 pte_free(tlb->mm, pgtable);
1015 ret = 1;
1017 } else
1018 spin_unlock(&tlb->mm->page_table_lock);
1020 return ret;
1023 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1024 unsigned long addr, unsigned long end,
1025 unsigned char *vec)
1027 int ret = 0;
1029 spin_lock(&vma->vm_mm->page_table_lock);
1030 if (likely(pmd_trans_huge(*pmd))) {
1031 ret = !pmd_trans_splitting(*pmd);
1032 spin_unlock(&vma->vm_mm->page_table_lock);
1033 if (unlikely(!ret))
1034 wait_split_huge_page(vma->anon_vma, pmd);
1035 else {
1037 * All logical pages in the range are present
1038 * if backed by a huge page.
1040 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1042 } else
1043 spin_unlock(&vma->vm_mm->page_table_lock);
1045 return ret;
1048 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1049 unsigned long addr, pgprot_t newprot)
1051 struct mm_struct *mm = vma->vm_mm;
1052 int ret = 0;
1054 spin_lock(&mm->page_table_lock);
1055 if (likely(pmd_trans_huge(*pmd))) {
1056 if (unlikely(pmd_trans_splitting(*pmd))) {
1057 spin_unlock(&mm->page_table_lock);
1058 wait_split_huge_page(vma->anon_vma, pmd);
1059 } else {
1060 pmd_t entry;
1062 entry = pmdp_get_and_clear(mm, addr, pmd);
1063 entry = pmd_modify(entry, newprot);
1064 set_pmd_at(mm, addr, pmd, entry);
1065 spin_unlock(&vma->vm_mm->page_table_lock);
1066 flush_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
1067 ret = 1;
1069 } else
1070 spin_unlock(&vma->vm_mm->page_table_lock);
1072 return ret;
1075 pmd_t *page_check_address_pmd(struct page *page,
1076 struct mm_struct *mm,
1077 unsigned long address,
1078 enum page_check_address_pmd_flag flag)
1080 pgd_t *pgd;
1081 pud_t *pud;
1082 pmd_t *pmd, *ret = NULL;
1084 if (address & ~HPAGE_PMD_MASK)
1085 goto out;
1087 pgd = pgd_offset(mm, address);
1088 if (!pgd_present(*pgd))
1089 goto out;
1091 pud = pud_offset(pgd, address);
1092 if (!pud_present(*pud))
1093 goto out;
1095 pmd = pmd_offset(pud, address);
1096 if (pmd_none(*pmd))
1097 goto out;
1098 if (pmd_page(*pmd) != page)
1099 goto out;
1101 * split_vma() may create temporary aliased mappings. There is
1102 * no risk as long as all huge pmd are found and have their
1103 * splitting bit set before __split_huge_page_refcount
1104 * runs. Finding the same huge pmd more than once during the
1105 * same rmap walk is not a problem.
1107 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1108 pmd_trans_splitting(*pmd))
1109 goto out;
1110 if (pmd_trans_huge(*pmd)) {
1111 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1112 !pmd_trans_splitting(*pmd));
1113 ret = pmd;
1115 out:
1116 return ret;
1119 static int __split_huge_page_splitting(struct page *page,
1120 struct vm_area_struct *vma,
1121 unsigned long address)
1123 struct mm_struct *mm = vma->vm_mm;
1124 pmd_t *pmd;
1125 int ret = 0;
1127 spin_lock(&mm->page_table_lock);
1128 pmd = page_check_address_pmd(page, mm, address,
1129 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1130 if (pmd) {
1132 * We can't temporarily set the pmd to null in order
1133 * to split it, the pmd must remain marked huge at all
1134 * times or the VM won't take the pmd_trans_huge paths
1135 * and it won't wait on the anon_vma->root->lock to
1136 * serialize against split_huge_page*.
1138 pmdp_splitting_flush_notify(vma, address, pmd);
1139 ret = 1;
1141 spin_unlock(&mm->page_table_lock);
1143 return ret;
1146 static void __split_huge_page_refcount(struct page *page)
1148 int i;
1149 unsigned long head_index = page->index;
1150 struct zone *zone = page_zone(page);
1151 int zonestat;
1153 /* prevent PageLRU to go away from under us, and freeze lru stats */
1154 spin_lock_irq(&zone->lru_lock);
1155 compound_lock(page);
1157 for (i = 1; i < HPAGE_PMD_NR; i++) {
1158 struct page *page_tail = page + i;
1160 /* tail_page->_count cannot change */
1161 atomic_sub(atomic_read(&page_tail->_count), &page->_count);
1162 BUG_ON(page_count(page) <= 0);
1163 atomic_add(page_mapcount(page) + 1, &page_tail->_count);
1164 BUG_ON(atomic_read(&page_tail->_count) <= 0);
1166 /* after clearing PageTail the gup refcount can be released */
1167 smp_mb();
1170 * retain hwpoison flag of the poisoned tail page:
1171 * fix for the unsuitable process killed on Guest Machine(KVM)
1172 * by the memory-failure.
1174 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1175 page_tail->flags |= (page->flags &
1176 ((1L << PG_referenced) |
1177 (1L << PG_swapbacked) |
1178 (1L << PG_mlocked) |
1179 (1L << PG_uptodate)));
1180 page_tail->flags |= (1L << PG_dirty);
1183 * 1) clear PageTail before overwriting first_page
1184 * 2) clear PageTail before clearing PageHead for VM_BUG_ON
1186 smp_wmb();
1189 * __split_huge_page_splitting() already set the
1190 * splitting bit in all pmd that could map this
1191 * hugepage, that will ensure no CPU can alter the
1192 * mapcount on the head page. The mapcount is only
1193 * accounted in the head page and it has to be
1194 * transferred to all tail pages in the below code. So
1195 * for this code to be safe, the split the mapcount
1196 * can't change. But that doesn't mean userland can't
1197 * keep changing and reading the page contents while
1198 * we transfer the mapcount, so the pmd splitting
1199 * status is achieved setting a reserved bit in the
1200 * pmd, not by clearing the present bit.
1202 BUG_ON(page_mapcount(page_tail));
1203 page_tail->_mapcount = page->_mapcount;
1205 BUG_ON(page_tail->mapping);
1206 page_tail->mapping = page->mapping;
1208 page_tail->index = ++head_index;
1210 BUG_ON(!PageAnon(page_tail));
1211 BUG_ON(!PageUptodate(page_tail));
1212 BUG_ON(!PageDirty(page_tail));
1213 BUG_ON(!PageSwapBacked(page_tail));
1215 mem_cgroup_split_huge_fixup(page, page_tail);
1217 lru_add_page_tail(zone, page, page_tail);
1220 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1221 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1224 * A hugepage counts for HPAGE_PMD_NR pages on the LRU statistics,
1225 * so adjust those appropriately if this page is on the LRU.
1227 if (PageLRU(page)) {
1228 zonestat = NR_LRU_BASE + page_lru(page);
1229 __mod_zone_page_state(zone, zonestat, -(HPAGE_PMD_NR-1));
1232 ClearPageCompound(page);
1233 compound_unlock(page);
1234 spin_unlock_irq(&zone->lru_lock);
1236 for (i = 1; i < HPAGE_PMD_NR; i++) {
1237 struct page *page_tail = page + i;
1238 BUG_ON(page_count(page_tail) <= 0);
1240 * Tail pages may be freed if there wasn't any mapping
1241 * like if add_to_swap() is running on a lru page that
1242 * had its mapping zapped. And freeing these pages
1243 * requires taking the lru_lock so we do the put_page
1244 * of the tail pages after the split is complete.
1246 put_page(page_tail);
1250 * Only the head page (now become a regular page) is required
1251 * to be pinned by the caller.
1253 BUG_ON(page_count(page) <= 0);
1256 static int __split_huge_page_map(struct page *page,
1257 struct vm_area_struct *vma,
1258 unsigned long address)
1260 struct mm_struct *mm = vma->vm_mm;
1261 pmd_t *pmd, _pmd;
1262 int ret = 0, i;
1263 pgtable_t pgtable;
1264 unsigned long haddr;
1266 spin_lock(&mm->page_table_lock);
1267 pmd = page_check_address_pmd(page, mm, address,
1268 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1269 if (pmd) {
1270 pgtable = get_pmd_huge_pte(mm);
1271 pmd_populate(mm, &_pmd, pgtable);
1273 for (i = 0, haddr = address; i < HPAGE_PMD_NR;
1274 i++, haddr += PAGE_SIZE) {
1275 pte_t *pte, entry;
1276 BUG_ON(PageCompound(page+i));
1277 entry = mk_pte(page + i, vma->vm_page_prot);
1278 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1279 if (!pmd_write(*pmd))
1280 entry = pte_wrprotect(entry);
1281 else
1282 BUG_ON(page_mapcount(page) != 1);
1283 if (!pmd_young(*pmd))
1284 entry = pte_mkold(entry);
1285 pte = pte_offset_map(&_pmd, haddr);
1286 BUG_ON(!pte_none(*pte));
1287 set_pte_at(mm, haddr, pte, entry);
1288 pte_unmap(pte);
1291 mm->nr_ptes++;
1292 smp_wmb(); /* make pte visible before pmd */
1294 * Up to this point the pmd is present and huge and
1295 * userland has the whole access to the hugepage
1296 * during the split (which happens in place). If we
1297 * overwrite the pmd with the not-huge version
1298 * pointing to the pte here (which of course we could
1299 * if all CPUs were bug free), userland could trigger
1300 * a small page size TLB miss on the small sized TLB
1301 * while the hugepage TLB entry is still established
1302 * in the huge TLB. Some CPU doesn't like that. See
1303 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1304 * Erratum 383 on page 93. Intel should be safe but is
1305 * also warns that it's only safe if the permission
1306 * and cache attributes of the two entries loaded in
1307 * the two TLB is identical (which should be the case
1308 * here). But it is generally safer to never allow
1309 * small and huge TLB entries for the same virtual
1310 * address to be loaded simultaneously. So instead of
1311 * doing "pmd_populate(); flush_tlb_range();" we first
1312 * mark the current pmd notpresent (atomically because
1313 * here the pmd_trans_huge and pmd_trans_splitting
1314 * must remain set at all times on the pmd until the
1315 * split is complete for this pmd), then we flush the
1316 * SMP TLB and finally we write the non-huge version
1317 * of the pmd entry with pmd_populate.
1319 set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
1320 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
1321 pmd_populate(mm, pmd, pgtable);
1322 ret = 1;
1324 spin_unlock(&mm->page_table_lock);
1326 return ret;
1329 /* must be called with anon_vma->root->lock hold */
1330 static void __split_huge_page(struct page *page,
1331 struct anon_vma *anon_vma)
1333 int mapcount, mapcount2;
1334 struct anon_vma_chain *avc;
1336 BUG_ON(!PageHead(page));
1337 BUG_ON(PageTail(page));
1339 mapcount = 0;
1340 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1341 struct vm_area_struct *vma = avc->vma;
1342 unsigned long addr = vma_address(page, vma);
1343 BUG_ON(is_vma_temporary_stack(vma));
1344 if (addr == -EFAULT)
1345 continue;
1346 mapcount += __split_huge_page_splitting(page, vma, addr);
1349 * It is critical that new vmas are added to the tail of the
1350 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1351 * and establishes a child pmd before
1352 * __split_huge_page_splitting() freezes the parent pmd (so if
1353 * we fail to prevent copy_huge_pmd() from running until the
1354 * whole __split_huge_page() is complete), we will still see
1355 * the newly established pmd of the child later during the
1356 * walk, to be able to set it as pmd_trans_splitting too.
1358 if (mapcount != page_mapcount(page))
1359 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1360 mapcount, page_mapcount(page));
1361 BUG_ON(mapcount != page_mapcount(page));
1363 __split_huge_page_refcount(page);
1365 mapcount2 = 0;
1366 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1367 struct vm_area_struct *vma = avc->vma;
1368 unsigned long addr = vma_address(page, vma);
1369 BUG_ON(is_vma_temporary_stack(vma));
1370 if (addr == -EFAULT)
1371 continue;
1372 mapcount2 += __split_huge_page_map(page, vma, addr);
1374 if (mapcount != mapcount2)
1375 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1376 mapcount, mapcount2, page_mapcount(page));
1377 BUG_ON(mapcount != mapcount2);
1380 int split_huge_page(struct page *page)
1382 struct anon_vma *anon_vma;
1383 int ret = 1;
1385 BUG_ON(!PageAnon(page));
1386 anon_vma = page_lock_anon_vma(page);
1387 if (!anon_vma)
1388 goto out;
1389 ret = 0;
1390 if (!PageCompound(page))
1391 goto out_unlock;
1393 BUG_ON(!PageSwapBacked(page));
1394 __split_huge_page(page, anon_vma);
1396 BUG_ON(PageCompound(page));
1397 out_unlock:
1398 page_unlock_anon_vma(anon_vma);
1399 out:
1400 return ret;
1403 int hugepage_madvise(struct vm_area_struct *vma,
1404 unsigned long *vm_flags, int advice)
1406 switch (advice) {
1407 case MADV_HUGEPAGE:
1409 * Be somewhat over-protective like KSM for now!
1411 if (*vm_flags & (VM_HUGEPAGE |
1412 VM_SHARED | VM_MAYSHARE |
1413 VM_PFNMAP | VM_IO | VM_DONTEXPAND |
1414 VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
1415 VM_MIXEDMAP | VM_SAO))
1416 return -EINVAL;
1417 *vm_flags &= ~VM_NOHUGEPAGE;
1418 *vm_flags |= VM_HUGEPAGE;
1420 * If the vma become good for khugepaged to scan,
1421 * register it here without waiting a page fault that
1422 * may not happen any time soon.
1424 if (unlikely(khugepaged_enter_vma_merge(vma)))
1425 return -ENOMEM;
1426 break;
1427 case MADV_NOHUGEPAGE:
1429 * Be somewhat over-protective like KSM for now!
1431 if (*vm_flags & (VM_NOHUGEPAGE |
1432 VM_SHARED | VM_MAYSHARE |
1433 VM_PFNMAP | VM_IO | VM_DONTEXPAND |
1434 VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
1435 VM_MIXEDMAP | VM_SAO))
1436 return -EINVAL;
1437 *vm_flags &= ~VM_HUGEPAGE;
1438 *vm_flags |= VM_NOHUGEPAGE;
1440 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1441 * this vma even if we leave the mm registered in khugepaged if
1442 * it got registered before VM_NOHUGEPAGE was set.
1444 break;
1447 return 0;
1450 static int __init khugepaged_slab_init(void)
1452 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1453 sizeof(struct mm_slot),
1454 __alignof__(struct mm_slot), 0, NULL);
1455 if (!mm_slot_cache)
1456 return -ENOMEM;
1458 return 0;
1461 static void __init khugepaged_slab_free(void)
1463 kmem_cache_destroy(mm_slot_cache);
1464 mm_slot_cache = NULL;
1467 static inline struct mm_slot *alloc_mm_slot(void)
1469 if (!mm_slot_cache) /* initialization failed */
1470 return NULL;
1471 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1474 static inline void free_mm_slot(struct mm_slot *mm_slot)
1476 kmem_cache_free(mm_slot_cache, mm_slot);
1479 static int __init mm_slots_hash_init(void)
1481 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1482 GFP_KERNEL);
1483 if (!mm_slots_hash)
1484 return -ENOMEM;
1485 return 0;
1488 #if 0
1489 static void __init mm_slots_hash_free(void)
1491 kfree(mm_slots_hash);
1492 mm_slots_hash = NULL;
1494 #endif
1496 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1498 struct mm_slot *mm_slot;
1499 struct hlist_head *bucket;
1500 struct hlist_node *node;
1502 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1503 % MM_SLOTS_HASH_HEADS];
1504 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1505 if (mm == mm_slot->mm)
1506 return mm_slot;
1508 return NULL;
1511 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1512 struct mm_slot *mm_slot)
1514 struct hlist_head *bucket;
1516 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1517 % MM_SLOTS_HASH_HEADS];
1518 mm_slot->mm = mm;
1519 hlist_add_head(&mm_slot->hash, bucket);
1522 static inline int khugepaged_test_exit(struct mm_struct *mm)
1524 return atomic_read(&mm->mm_users) == 0;
1527 int __khugepaged_enter(struct mm_struct *mm)
1529 struct mm_slot *mm_slot;
1530 int wakeup;
1532 mm_slot = alloc_mm_slot();
1533 if (!mm_slot)
1534 return -ENOMEM;
1536 /* __khugepaged_exit() must not run from under us */
1537 VM_BUG_ON(khugepaged_test_exit(mm));
1538 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1539 free_mm_slot(mm_slot);
1540 return 0;
1543 spin_lock(&khugepaged_mm_lock);
1544 insert_to_mm_slots_hash(mm, mm_slot);
1546 * Insert just behind the scanning cursor, to let the area settle
1547 * down a little.
1549 wakeup = list_empty(&khugepaged_scan.mm_head);
1550 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1551 spin_unlock(&khugepaged_mm_lock);
1553 atomic_inc(&mm->mm_count);
1554 if (wakeup)
1555 wake_up_interruptible(&khugepaged_wait);
1557 return 0;
1560 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1562 unsigned long hstart, hend;
1563 if (!vma->anon_vma)
1565 * Not yet faulted in so we will register later in the
1566 * page fault if needed.
1568 return 0;
1569 if (vma->vm_file || vma->vm_ops)
1570 /* khugepaged not yet working on file or special mappings */
1571 return 0;
1572 VM_BUG_ON(is_linear_pfn_mapping(vma) || is_pfn_mapping(vma));
1573 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1574 hend = vma->vm_end & HPAGE_PMD_MASK;
1575 if (hstart < hend)
1576 return khugepaged_enter(vma);
1577 return 0;
1580 void __khugepaged_exit(struct mm_struct *mm)
1582 struct mm_slot *mm_slot;
1583 int free = 0;
1585 spin_lock(&khugepaged_mm_lock);
1586 mm_slot = get_mm_slot(mm);
1587 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1588 hlist_del(&mm_slot->hash);
1589 list_del(&mm_slot->mm_node);
1590 free = 1;
1593 if (free) {
1594 spin_unlock(&khugepaged_mm_lock);
1595 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1596 free_mm_slot(mm_slot);
1597 mmdrop(mm);
1598 } else if (mm_slot) {
1599 spin_unlock(&khugepaged_mm_lock);
1601 * This is required to serialize against
1602 * khugepaged_test_exit() (which is guaranteed to run
1603 * under mmap sem read mode). Stop here (after we
1604 * return all pagetables will be destroyed) until
1605 * khugepaged has finished working on the pagetables
1606 * under the mmap_sem.
1608 down_write(&mm->mmap_sem);
1609 up_write(&mm->mmap_sem);
1610 } else
1611 spin_unlock(&khugepaged_mm_lock);
1614 static void release_pte_page(struct page *page)
1616 /* 0 stands for page_is_file_cache(page) == false */
1617 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1618 unlock_page(page);
1619 putback_lru_page(page);
1622 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1624 while (--_pte >= pte) {
1625 pte_t pteval = *_pte;
1626 if (!pte_none(pteval))
1627 release_pte_page(pte_page(pteval));
1631 static void release_all_pte_pages(pte_t *pte)
1633 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1636 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1637 unsigned long address,
1638 pte_t *pte)
1640 struct page *page;
1641 pte_t *_pte;
1642 int referenced = 0, isolated = 0, none = 0;
1643 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1644 _pte++, address += PAGE_SIZE) {
1645 pte_t pteval = *_pte;
1646 if (pte_none(pteval)) {
1647 if (++none <= khugepaged_max_ptes_none)
1648 continue;
1649 else {
1650 release_pte_pages(pte, _pte);
1651 goto out;
1654 if (!pte_present(pteval) || !pte_write(pteval)) {
1655 release_pte_pages(pte, _pte);
1656 goto out;
1658 page = vm_normal_page(vma, address, pteval);
1659 if (unlikely(!page)) {
1660 release_pte_pages(pte, _pte);
1661 goto out;
1663 VM_BUG_ON(PageCompound(page));
1664 BUG_ON(!PageAnon(page));
1665 VM_BUG_ON(!PageSwapBacked(page));
1667 /* cannot use mapcount: can't collapse if there's a gup pin */
1668 if (page_count(page) != 1) {
1669 release_pte_pages(pte, _pte);
1670 goto out;
1673 * We can do it before isolate_lru_page because the
1674 * page can't be freed from under us. NOTE: PG_lock
1675 * is needed to serialize against split_huge_page
1676 * when invoked from the VM.
1678 if (!trylock_page(page)) {
1679 release_pte_pages(pte, _pte);
1680 goto out;
1683 * Isolate the page to avoid collapsing an hugepage
1684 * currently in use by the VM.
1686 if (isolate_lru_page(page)) {
1687 unlock_page(page);
1688 release_pte_pages(pte, _pte);
1689 goto out;
1691 /* 0 stands for page_is_file_cache(page) == false */
1692 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1693 VM_BUG_ON(!PageLocked(page));
1694 VM_BUG_ON(PageLRU(page));
1696 /* If there is no mapped pte young don't collapse the page */
1697 if (pte_young(pteval) || PageReferenced(page) ||
1698 mmu_notifier_test_young(vma->vm_mm, address))
1699 referenced = 1;
1701 if (unlikely(!referenced))
1702 release_all_pte_pages(pte);
1703 else
1704 isolated = 1;
1705 out:
1706 return isolated;
1709 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1710 struct vm_area_struct *vma,
1711 unsigned long address,
1712 spinlock_t *ptl)
1714 pte_t *_pte;
1715 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1716 pte_t pteval = *_pte;
1717 struct page *src_page;
1719 if (pte_none(pteval)) {
1720 clear_user_highpage(page, address);
1721 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1722 } else {
1723 src_page = pte_page(pteval);
1724 copy_user_highpage(page, src_page, address, vma);
1725 VM_BUG_ON(page_mapcount(src_page) != 1);
1726 VM_BUG_ON(page_count(src_page) != 2);
1727 release_pte_page(src_page);
1729 * ptl mostly unnecessary, but preempt has to
1730 * be disabled to update the per-cpu stats
1731 * inside page_remove_rmap().
1733 spin_lock(ptl);
1735 * paravirt calls inside pte_clear here are
1736 * superfluous.
1738 pte_clear(vma->vm_mm, address, _pte);
1739 page_remove_rmap(src_page);
1740 spin_unlock(ptl);
1741 free_page_and_swap_cache(src_page);
1744 address += PAGE_SIZE;
1745 page++;
1749 static void collapse_huge_page(struct mm_struct *mm,
1750 unsigned long address,
1751 struct page **hpage,
1752 struct vm_area_struct *vma,
1753 int node)
1755 pgd_t *pgd;
1756 pud_t *pud;
1757 pmd_t *pmd, _pmd;
1758 pte_t *pte;
1759 pgtable_t pgtable;
1760 struct page *new_page;
1761 spinlock_t *ptl;
1762 int isolated;
1763 unsigned long hstart, hend;
1765 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1766 #ifndef CONFIG_NUMA
1767 VM_BUG_ON(!*hpage);
1768 new_page = *hpage;
1769 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1770 up_read(&mm->mmap_sem);
1771 return;
1773 #else
1774 VM_BUG_ON(*hpage);
1776 * Allocate the page while the vma is still valid and under
1777 * the mmap_sem read mode so there is no memory allocation
1778 * later when we take the mmap_sem in write mode. This is more
1779 * friendly behavior (OTOH it may actually hide bugs) to
1780 * filesystems in userland with daemons allocating memory in
1781 * the userland I/O paths. Allocating memory with the
1782 * mmap_sem in read mode is good idea also to allow greater
1783 * scalability.
1785 new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1786 node);
1787 if (unlikely(!new_page)) {
1788 up_read(&mm->mmap_sem);
1789 *hpage = ERR_PTR(-ENOMEM);
1790 return;
1792 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1793 up_read(&mm->mmap_sem);
1794 put_page(new_page);
1795 return;
1797 #endif
1799 /* after allocating the hugepage upgrade to mmap_sem write mode */
1800 up_read(&mm->mmap_sem);
1803 * Prevent all access to pagetables with the exception of
1804 * gup_fast later hanlded by the ptep_clear_flush and the VM
1805 * handled by the anon_vma lock + PG_lock.
1807 down_write(&mm->mmap_sem);
1808 if (unlikely(khugepaged_test_exit(mm)))
1809 goto out;
1811 vma = find_vma(mm, address);
1812 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1813 hend = vma->vm_end & HPAGE_PMD_MASK;
1814 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1815 goto out;
1817 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1818 (vma->vm_flags & VM_NOHUGEPAGE))
1819 goto out;
1821 /* VM_PFNMAP vmas may have vm_ops null but vm_file set */
1822 if (!vma->anon_vma || vma->vm_ops || vma->vm_file)
1823 goto out;
1824 if (is_vma_temporary_stack(vma))
1825 goto out;
1826 VM_BUG_ON(is_linear_pfn_mapping(vma) || is_pfn_mapping(vma));
1828 pgd = pgd_offset(mm, address);
1829 if (!pgd_present(*pgd))
1830 goto out;
1832 pud = pud_offset(pgd, address);
1833 if (!pud_present(*pud))
1834 goto out;
1836 pmd = pmd_offset(pud, address);
1837 /* pmd can't go away or become huge under us */
1838 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1839 goto out;
1841 anon_vma_lock(vma->anon_vma);
1843 pte = pte_offset_map(pmd, address);
1844 ptl = pte_lockptr(mm, pmd);
1846 spin_lock(&mm->page_table_lock); /* probably unnecessary */
1848 * After this gup_fast can't run anymore. This also removes
1849 * any huge TLB entry from the CPU so we won't allow
1850 * huge and small TLB entries for the same virtual address
1851 * to avoid the risk of CPU bugs in that area.
1853 _pmd = pmdp_clear_flush_notify(vma, address, pmd);
1854 spin_unlock(&mm->page_table_lock);
1856 spin_lock(ptl);
1857 isolated = __collapse_huge_page_isolate(vma, address, pte);
1858 spin_unlock(ptl);
1860 if (unlikely(!isolated)) {
1861 pte_unmap(pte);
1862 spin_lock(&mm->page_table_lock);
1863 BUG_ON(!pmd_none(*pmd));
1864 set_pmd_at(mm, address, pmd, _pmd);
1865 spin_unlock(&mm->page_table_lock);
1866 anon_vma_unlock(vma->anon_vma);
1867 goto out;
1871 * All pages are isolated and locked so anon_vma rmap
1872 * can't run anymore.
1874 anon_vma_unlock(vma->anon_vma);
1876 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
1877 pte_unmap(pte);
1878 __SetPageUptodate(new_page);
1879 pgtable = pmd_pgtable(_pmd);
1880 VM_BUG_ON(page_count(pgtable) != 1);
1881 VM_BUG_ON(page_mapcount(pgtable) != 0);
1883 _pmd = mk_pmd(new_page, vma->vm_page_prot);
1884 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1885 _pmd = pmd_mkhuge(_pmd);
1888 * spin_lock() below is not the equivalent of smp_wmb(), so
1889 * this is needed to avoid the copy_huge_page writes to become
1890 * visible after the set_pmd_at() write.
1892 smp_wmb();
1894 spin_lock(&mm->page_table_lock);
1895 BUG_ON(!pmd_none(*pmd));
1896 page_add_new_anon_rmap(new_page, vma, address);
1897 set_pmd_at(mm, address, pmd, _pmd);
1898 update_mmu_cache(vma, address, entry);
1899 prepare_pmd_huge_pte(pgtable, mm);
1900 mm->nr_ptes--;
1901 spin_unlock(&mm->page_table_lock);
1903 #ifndef CONFIG_NUMA
1904 *hpage = NULL;
1905 #endif
1906 khugepaged_pages_collapsed++;
1907 out_up_write:
1908 up_write(&mm->mmap_sem);
1909 return;
1911 out:
1912 mem_cgroup_uncharge_page(new_page);
1913 #ifdef CONFIG_NUMA
1914 put_page(new_page);
1915 #endif
1916 goto out_up_write;
1919 static int khugepaged_scan_pmd(struct mm_struct *mm,
1920 struct vm_area_struct *vma,
1921 unsigned long address,
1922 struct page **hpage)
1924 pgd_t *pgd;
1925 pud_t *pud;
1926 pmd_t *pmd;
1927 pte_t *pte, *_pte;
1928 int ret = 0, referenced = 0, none = 0;
1929 struct page *page;
1930 unsigned long _address;
1931 spinlock_t *ptl;
1932 int node = -1;
1934 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1936 pgd = pgd_offset(mm, address);
1937 if (!pgd_present(*pgd))
1938 goto out;
1940 pud = pud_offset(pgd, address);
1941 if (!pud_present(*pud))
1942 goto out;
1944 pmd = pmd_offset(pud, address);
1945 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1946 goto out;
1948 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1949 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
1950 _pte++, _address += PAGE_SIZE) {
1951 pte_t pteval = *_pte;
1952 if (pte_none(pteval)) {
1953 if (++none <= khugepaged_max_ptes_none)
1954 continue;
1955 else
1956 goto out_unmap;
1958 if (!pte_present(pteval) || !pte_write(pteval))
1959 goto out_unmap;
1960 page = vm_normal_page(vma, _address, pteval);
1961 if (unlikely(!page))
1962 goto out_unmap;
1964 * Chose the node of the first page. This could
1965 * be more sophisticated and look at more pages,
1966 * but isn't for now.
1968 if (node == -1)
1969 node = page_to_nid(page);
1970 VM_BUG_ON(PageCompound(page));
1971 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
1972 goto out_unmap;
1973 /* cannot use mapcount: can't collapse if there's a gup pin */
1974 if (page_count(page) != 1)
1975 goto out_unmap;
1976 if (pte_young(pteval) || PageReferenced(page) ||
1977 mmu_notifier_test_young(vma->vm_mm, address))
1978 referenced = 1;
1980 if (referenced)
1981 ret = 1;
1982 out_unmap:
1983 pte_unmap_unlock(pte, ptl);
1984 if (ret)
1985 /* collapse_huge_page will return with the mmap_sem released */
1986 collapse_huge_page(mm, address, hpage, vma, node);
1987 out:
1988 return ret;
1991 static void collect_mm_slot(struct mm_slot *mm_slot)
1993 struct mm_struct *mm = mm_slot->mm;
1995 VM_BUG_ON(!spin_is_locked(&khugepaged_mm_lock));
1997 if (khugepaged_test_exit(mm)) {
1998 /* free mm_slot */
1999 hlist_del(&mm_slot->hash);
2000 list_del(&mm_slot->mm_node);
2003 * Not strictly needed because the mm exited already.
2005 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2008 /* khugepaged_mm_lock actually not necessary for the below */
2009 free_mm_slot(mm_slot);
2010 mmdrop(mm);
2014 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2015 struct page **hpage)
2017 struct mm_slot *mm_slot;
2018 struct mm_struct *mm;
2019 struct vm_area_struct *vma;
2020 int progress = 0;
2022 VM_BUG_ON(!pages);
2023 VM_BUG_ON(!spin_is_locked(&khugepaged_mm_lock));
2025 if (khugepaged_scan.mm_slot)
2026 mm_slot = khugepaged_scan.mm_slot;
2027 else {
2028 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2029 struct mm_slot, mm_node);
2030 khugepaged_scan.address = 0;
2031 khugepaged_scan.mm_slot = mm_slot;
2033 spin_unlock(&khugepaged_mm_lock);
2035 mm = mm_slot->mm;
2036 down_read(&mm->mmap_sem);
2037 if (unlikely(khugepaged_test_exit(mm)))
2038 vma = NULL;
2039 else
2040 vma = find_vma(mm, khugepaged_scan.address);
2042 progress++;
2043 for (; vma; vma = vma->vm_next) {
2044 unsigned long hstart, hend;
2046 cond_resched();
2047 if (unlikely(khugepaged_test_exit(mm))) {
2048 progress++;
2049 break;
2052 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2053 !khugepaged_always()) ||
2054 (vma->vm_flags & VM_NOHUGEPAGE)) {
2055 skip:
2056 progress++;
2057 continue;
2059 /* VM_PFNMAP vmas may have vm_ops null but vm_file set */
2060 if (!vma->anon_vma || vma->vm_ops || vma->vm_file)
2061 goto skip;
2062 if (is_vma_temporary_stack(vma))
2063 goto skip;
2065 VM_BUG_ON(is_linear_pfn_mapping(vma) || is_pfn_mapping(vma));
2067 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2068 hend = vma->vm_end & HPAGE_PMD_MASK;
2069 if (hstart >= hend)
2070 goto skip;
2071 if (khugepaged_scan.address > hend)
2072 goto skip;
2073 if (khugepaged_scan.address < hstart)
2074 khugepaged_scan.address = hstart;
2075 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2077 while (khugepaged_scan.address < hend) {
2078 int ret;
2079 cond_resched();
2080 if (unlikely(khugepaged_test_exit(mm)))
2081 goto breakouterloop;
2083 VM_BUG_ON(khugepaged_scan.address < hstart ||
2084 khugepaged_scan.address + HPAGE_PMD_SIZE >
2085 hend);
2086 ret = khugepaged_scan_pmd(mm, vma,
2087 khugepaged_scan.address,
2088 hpage);
2089 /* move to next address */
2090 khugepaged_scan.address += HPAGE_PMD_SIZE;
2091 progress += HPAGE_PMD_NR;
2092 if (ret)
2093 /* we released mmap_sem so break loop */
2094 goto breakouterloop_mmap_sem;
2095 if (progress >= pages)
2096 goto breakouterloop;
2099 breakouterloop:
2100 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2101 breakouterloop_mmap_sem:
2103 spin_lock(&khugepaged_mm_lock);
2104 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2106 * Release the current mm_slot if this mm is about to die, or
2107 * if we scanned all vmas of this mm.
2109 if (khugepaged_test_exit(mm) || !vma) {
2111 * Make sure that if mm_users is reaching zero while
2112 * khugepaged runs here, khugepaged_exit will find
2113 * mm_slot not pointing to the exiting mm.
2115 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2116 khugepaged_scan.mm_slot = list_entry(
2117 mm_slot->mm_node.next,
2118 struct mm_slot, mm_node);
2119 khugepaged_scan.address = 0;
2120 } else {
2121 khugepaged_scan.mm_slot = NULL;
2122 khugepaged_full_scans++;
2125 collect_mm_slot(mm_slot);
2128 return progress;
2131 static int khugepaged_has_work(void)
2133 return !list_empty(&khugepaged_scan.mm_head) &&
2134 khugepaged_enabled();
2137 static int khugepaged_wait_event(void)
2139 return !list_empty(&khugepaged_scan.mm_head) ||
2140 !khugepaged_enabled();
2143 static void khugepaged_do_scan(struct page **hpage)
2145 unsigned int progress = 0, pass_through_head = 0;
2146 unsigned int pages = khugepaged_pages_to_scan;
2148 barrier(); /* write khugepaged_pages_to_scan to local stack */
2150 while (progress < pages) {
2151 cond_resched();
2153 #ifndef CONFIG_NUMA
2154 if (!*hpage) {
2155 *hpage = alloc_hugepage(khugepaged_defrag());
2156 if (unlikely(!*hpage))
2157 break;
2159 #else
2160 if (IS_ERR(*hpage))
2161 break;
2162 #endif
2164 if (unlikely(kthread_should_stop() || freezing(current)))
2165 break;
2167 spin_lock(&khugepaged_mm_lock);
2168 if (!khugepaged_scan.mm_slot)
2169 pass_through_head++;
2170 if (khugepaged_has_work() &&
2171 pass_through_head < 2)
2172 progress += khugepaged_scan_mm_slot(pages - progress,
2173 hpage);
2174 else
2175 progress = pages;
2176 spin_unlock(&khugepaged_mm_lock);
2180 static void khugepaged_alloc_sleep(void)
2182 DEFINE_WAIT(wait);
2183 add_wait_queue(&khugepaged_wait, &wait);
2184 schedule_timeout_interruptible(
2185 msecs_to_jiffies(
2186 khugepaged_alloc_sleep_millisecs));
2187 remove_wait_queue(&khugepaged_wait, &wait);
2190 #ifndef CONFIG_NUMA
2191 static struct page *khugepaged_alloc_hugepage(void)
2193 struct page *hpage;
2195 do {
2196 hpage = alloc_hugepage(khugepaged_defrag());
2197 if (!hpage)
2198 khugepaged_alloc_sleep();
2199 } while (unlikely(!hpage) &&
2200 likely(khugepaged_enabled()));
2201 return hpage;
2203 #endif
2205 static void khugepaged_loop(void)
2207 struct page *hpage;
2209 #ifdef CONFIG_NUMA
2210 hpage = NULL;
2211 #endif
2212 while (likely(khugepaged_enabled())) {
2213 #ifndef CONFIG_NUMA
2214 hpage = khugepaged_alloc_hugepage();
2215 if (unlikely(!hpage))
2216 break;
2217 #else
2218 if (IS_ERR(hpage)) {
2219 khugepaged_alloc_sleep();
2220 hpage = NULL;
2222 #endif
2224 khugepaged_do_scan(&hpage);
2225 #ifndef CONFIG_NUMA
2226 if (hpage)
2227 put_page(hpage);
2228 #endif
2229 try_to_freeze();
2230 if (unlikely(kthread_should_stop()))
2231 break;
2232 if (khugepaged_has_work()) {
2233 DEFINE_WAIT(wait);
2234 if (!khugepaged_scan_sleep_millisecs)
2235 continue;
2236 add_wait_queue(&khugepaged_wait, &wait);
2237 schedule_timeout_interruptible(
2238 msecs_to_jiffies(
2239 khugepaged_scan_sleep_millisecs));
2240 remove_wait_queue(&khugepaged_wait, &wait);
2241 } else if (khugepaged_enabled())
2242 wait_event_freezable(khugepaged_wait,
2243 khugepaged_wait_event());
2247 static int khugepaged(void *none)
2249 struct mm_slot *mm_slot;
2251 set_freezable();
2252 set_user_nice(current, 19);
2254 /* serialize with start_khugepaged() */
2255 mutex_lock(&khugepaged_mutex);
2257 for (;;) {
2258 mutex_unlock(&khugepaged_mutex);
2259 VM_BUG_ON(khugepaged_thread != current);
2260 khugepaged_loop();
2261 VM_BUG_ON(khugepaged_thread != current);
2263 mutex_lock(&khugepaged_mutex);
2264 if (!khugepaged_enabled())
2265 break;
2266 if (unlikely(kthread_should_stop()))
2267 break;
2270 spin_lock(&khugepaged_mm_lock);
2271 mm_slot = khugepaged_scan.mm_slot;
2272 khugepaged_scan.mm_slot = NULL;
2273 if (mm_slot)
2274 collect_mm_slot(mm_slot);
2275 spin_unlock(&khugepaged_mm_lock);
2277 khugepaged_thread = NULL;
2278 mutex_unlock(&khugepaged_mutex);
2280 return 0;
2283 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2285 struct page *page;
2287 spin_lock(&mm->page_table_lock);
2288 if (unlikely(!pmd_trans_huge(*pmd))) {
2289 spin_unlock(&mm->page_table_lock);
2290 return;
2292 page = pmd_page(*pmd);
2293 VM_BUG_ON(!page_count(page));
2294 get_page(page);
2295 spin_unlock(&mm->page_table_lock);
2297 split_huge_page(page);
2299 put_page(page);
2300 BUG_ON(pmd_trans_huge(*pmd));
2303 static void split_huge_page_address(struct mm_struct *mm,
2304 unsigned long address)
2306 pgd_t *pgd;
2307 pud_t *pud;
2308 pmd_t *pmd;
2310 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2312 pgd = pgd_offset(mm, address);
2313 if (!pgd_present(*pgd))
2314 return;
2316 pud = pud_offset(pgd, address);
2317 if (!pud_present(*pud))
2318 return;
2320 pmd = pmd_offset(pud, address);
2321 if (!pmd_present(*pmd))
2322 return;
2324 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2325 * materialize from under us.
2327 split_huge_page_pmd(mm, pmd);
2330 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2331 unsigned long start,
2332 unsigned long end,
2333 long adjust_next)
2336 * If the new start address isn't hpage aligned and it could
2337 * previously contain an hugepage: check if we need to split
2338 * an huge pmd.
2340 if (start & ~HPAGE_PMD_MASK &&
2341 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2342 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2343 split_huge_page_address(vma->vm_mm, start);
2346 * If the new end address isn't hpage aligned and it could
2347 * previously contain an hugepage: check if we need to split
2348 * an huge pmd.
2350 if (end & ~HPAGE_PMD_MASK &&
2351 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2352 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2353 split_huge_page_address(vma->vm_mm, end);
2356 * If we're also updating the vma->vm_next->vm_start, if the new
2357 * vm_next->vm_start isn't page aligned and it could previously
2358 * contain an hugepage: check if we need to split an huge pmd.
2360 if (adjust_next > 0) {
2361 struct vm_area_struct *next = vma->vm_next;
2362 unsigned long nstart = next->vm_start;
2363 nstart += adjust_next << PAGE_SHIFT;
2364 if (nstart & ~HPAGE_PMD_MASK &&
2365 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2366 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2367 split_huge_page_address(next->vm_mm, nstart);