ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / mm / huge_memory.c
blob8cc11dda6a747e4e9e823c8f988fd441a4b43ad9
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 mm->nr_ptes++;
645 spin_unlock(&mm->page_table_lock);
648 return ret;
651 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
653 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
656 static inline struct page *alloc_hugepage_vma(int defrag,
657 struct vm_area_struct *vma,
658 unsigned long haddr, int nd,
659 gfp_t extra_gfp)
661 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
662 HPAGE_PMD_ORDER, vma, haddr, nd);
665 #ifndef CONFIG_NUMA
666 static inline struct page *alloc_hugepage(int defrag)
668 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
669 HPAGE_PMD_ORDER);
671 #endif
673 int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
674 unsigned long address, pmd_t *pmd,
675 unsigned int flags)
677 struct page *page;
678 unsigned long haddr = address & HPAGE_PMD_MASK;
679 pte_t *pte;
681 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
682 if (unlikely(anon_vma_prepare(vma)))
683 return VM_FAULT_OOM;
684 if (unlikely(khugepaged_enter(vma)))
685 return VM_FAULT_OOM;
686 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
687 vma, haddr, numa_node_id(), 0);
688 if (unlikely(!page)) {
689 count_vm_event(THP_FAULT_FALLBACK);
690 goto out;
692 count_vm_event(THP_FAULT_ALLOC);
693 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
694 put_page(page);
695 goto out;
698 return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
700 out:
702 * Use __pte_alloc instead of pte_alloc_map, because we can't
703 * run pte_offset_map on the pmd, if an huge pmd could
704 * materialize from under us from a different thread.
706 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
707 return VM_FAULT_OOM;
708 /* if an huge pmd materialized from under us just retry later */
709 if (unlikely(pmd_trans_huge(*pmd)))
710 return 0;
712 * A regular pmd is established and it can't morph into a huge pmd
713 * from under us anymore at this point because we hold the mmap_sem
714 * read mode and khugepaged takes it in write mode. So now it's
715 * safe to run pte_offset_map().
717 pte = pte_offset_map(pmd, address);
718 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
721 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
722 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
723 struct vm_area_struct *vma)
725 struct page *src_page;
726 pmd_t pmd;
727 pgtable_t pgtable;
728 int ret;
730 ret = -ENOMEM;
731 pgtable = pte_alloc_one(dst_mm, addr);
732 if (unlikely(!pgtable))
733 goto out;
735 spin_lock(&dst_mm->page_table_lock);
736 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
738 ret = -EAGAIN;
739 pmd = *src_pmd;
740 if (unlikely(!pmd_trans_huge(pmd))) {
741 pte_free(dst_mm, pgtable);
742 goto out_unlock;
744 if (unlikely(pmd_trans_splitting(pmd))) {
745 /* split huge page running from under us */
746 spin_unlock(&src_mm->page_table_lock);
747 spin_unlock(&dst_mm->page_table_lock);
748 pte_free(dst_mm, pgtable);
750 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
751 goto out;
753 src_page = pmd_page(pmd);
754 VM_BUG_ON(!PageHead(src_page));
755 get_page(src_page);
756 page_dup_rmap(src_page);
757 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
759 pmdp_set_wrprotect(src_mm, addr, src_pmd);
760 pmd = pmd_mkold(pmd_wrprotect(pmd));
761 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
762 prepare_pmd_huge_pte(pgtable, dst_mm);
763 dst_mm->nr_ptes++;
765 ret = 0;
766 out_unlock:
767 spin_unlock(&src_mm->page_table_lock);
768 spin_unlock(&dst_mm->page_table_lock);
769 out:
770 return ret;
773 /* no "address" argument so destroys page coloring of some arch */
774 pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
776 pgtable_t pgtable;
778 assert_spin_locked(&mm->page_table_lock);
780 /* FIFO */
781 pgtable = mm->pmd_huge_pte;
782 if (list_empty(&pgtable->lru))
783 mm->pmd_huge_pte = NULL;
784 else {
785 mm->pmd_huge_pte = list_entry(pgtable->lru.next,
786 struct page, lru);
787 list_del(&pgtable->lru);
789 return pgtable;
792 static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
793 struct vm_area_struct *vma,
794 unsigned long address,
795 pmd_t *pmd, pmd_t orig_pmd,
796 struct page *page,
797 unsigned long haddr)
799 pgtable_t pgtable;
800 pmd_t _pmd;
801 int ret = 0, i;
802 struct page **pages;
804 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
805 GFP_KERNEL);
806 if (unlikely(!pages)) {
807 ret |= VM_FAULT_OOM;
808 goto out;
811 for (i = 0; i < HPAGE_PMD_NR; i++) {
812 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
813 __GFP_OTHER_NODE,
814 vma, address, page_to_nid(page));
815 if (unlikely(!pages[i] ||
816 mem_cgroup_newpage_charge(pages[i], mm,
817 GFP_KERNEL))) {
818 if (pages[i])
819 put_page(pages[i]);
820 mem_cgroup_uncharge_start();
821 while (--i >= 0) {
822 mem_cgroup_uncharge_page(pages[i]);
823 put_page(pages[i]);
825 mem_cgroup_uncharge_end();
826 kfree(pages);
827 ret |= VM_FAULT_OOM;
828 goto out;
832 for (i = 0; i < HPAGE_PMD_NR; i++) {
833 copy_user_highpage(pages[i], page + i,
834 haddr + PAGE_SHIFT*i, vma);
835 __SetPageUptodate(pages[i]);
836 cond_resched();
839 spin_lock(&mm->page_table_lock);
840 if (unlikely(!pmd_same(*pmd, orig_pmd)))
841 goto out_free_pages;
842 VM_BUG_ON(!PageHead(page));
844 pmdp_clear_flush_notify(vma, haddr, pmd);
845 /* leave pmd empty until pte is filled */
847 pgtable = get_pmd_huge_pte(mm);
848 pmd_populate(mm, &_pmd, pgtable);
850 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
851 pte_t *pte, entry;
852 entry = mk_pte(pages[i], vma->vm_page_prot);
853 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
854 page_add_new_anon_rmap(pages[i], vma, haddr);
855 pte = pte_offset_map(&_pmd, haddr);
856 VM_BUG_ON(!pte_none(*pte));
857 set_pte_at(mm, haddr, pte, entry);
858 pte_unmap(pte);
860 kfree(pages);
862 smp_wmb(); /* make pte visible before pmd */
863 pmd_populate(mm, pmd, pgtable);
864 page_remove_rmap(page);
865 spin_unlock(&mm->page_table_lock);
867 ret |= VM_FAULT_WRITE;
868 put_page(page);
870 out:
871 return ret;
873 out_free_pages:
874 spin_unlock(&mm->page_table_lock);
875 mem_cgroup_uncharge_start();
876 for (i = 0; i < HPAGE_PMD_NR; i++) {
877 mem_cgroup_uncharge_page(pages[i]);
878 put_page(pages[i]);
880 mem_cgroup_uncharge_end();
881 kfree(pages);
882 goto out;
885 int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
886 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
888 int ret = 0;
889 struct page *page, *new_page;
890 unsigned long haddr;
892 VM_BUG_ON(!vma->anon_vma);
893 spin_lock(&mm->page_table_lock);
894 if (unlikely(!pmd_same(*pmd, orig_pmd)))
895 goto out_unlock;
897 page = pmd_page(orig_pmd);
898 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
899 haddr = address & HPAGE_PMD_MASK;
900 if (page_mapcount(page) == 1) {
901 pmd_t entry;
902 entry = pmd_mkyoung(orig_pmd);
903 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
904 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
905 update_mmu_cache(vma, address, entry);
906 ret |= VM_FAULT_WRITE;
907 goto out_unlock;
909 get_page(page);
910 spin_unlock(&mm->page_table_lock);
912 if (transparent_hugepage_enabled(vma) &&
913 !transparent_hugepage_debug_cow())
914 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
915 vma, haddr, numa_node_id(), 0);
916 else
917 new_page = NULL;
919 if (unlikely(!new_page)) {
920 count_vm_event(THP_FAULT_FALLBACK);
921 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
922 pmd, orig_pmd, page, haddr);
923 put_page(page);
924 goto out;
926 count_vm_event(THP_FAULT_ALLOC);
928 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
929 put_page(new_page);
930 put_page(page);
931 ret |= VM_FAULT_OOM;
932 goto out;
935 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
936 __SetPageUptodate(new_page);
938 spin_lock(&mm->page_table_lock);
939 put_page(page);
940 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
941 mem_cgroup_uncharge_page(new_page);
942 put_page(new_page);
943 } else {
944 pmd_t entry;
945 VM_BUG_ON(!PageHead(page));
946 entry = mk_pmd(new_page, vma->vm_page_prot);
947 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
948 entry = pmd_mkhuge(entry);
949 pmdp_clear_flush_notify(vma, haddr, pmd);
950 page_add_new_anon_rmap(new_page, vma, haddr);
951 set_pmd_at(mm, haddr, pmd, entry);
952 update_mmu_cache(vma, address, entry);
953 page_remove_rmap(page);
954 put_page(page);
955 ret |= VM_FAULT_WRITE;
957 out_unlock:
958 spin_unlock(&mm->page_table_lock);
959 out:
960 return ret;
963 struct page *follow_trans_huge_pmd(struct mm_struct *mm,
964 unsigned long addr,
965 pmd_t *pmd,
966 unsigned int flags)
968 struct page *page = NULL;
970 assert_spin_locked(&mm->page_table_lock);
972 if (flags & FOLL_WRITE && !pmd_write(*pmd))
973 goto out;
975 page = pmd_page(*pmd);
976 VM_BUG_ON(!PageHead(page));
977 if (flags & FOLL_TOUCH) {
978 pmd_t _pmd;
980 * We should set the dirty bit only for FOLL_WRITE but
981 * for now the dirty bit in the pmd is meaningless.
982 * And if the dirty bit will become meaningful and
983 * we'll only set it with FOLL_WRITE, an atomic
984 * set_bit will be required on the pmd to set the
985 * young bit, instead of the current set_pmd_at.
987 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
988 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
990 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
991 VM_BUG_ON(!PageCompound(page));
992 if (flags & FOLL_GET)
993 get_page_foll(page);
995 out:
996 return page;
999 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1000 pmd_t *pmd)
1002 int ret = 0;
1004 spin_lock(&tlb->mm->page_table_lock);
1005 if (likely(pmd_trans_huge(*pmd))) {
1006 if (unlikely(pmd_trans_splitting(*pmd))) {
1007 spin_unlock(&tlb->mm->page_table_lock);
1008 wait_split_huge_page(vma->anon_vma,
1009 pmd);
1010 } else {
1011 struct page *page;
1012 pgtable_t pgtable;
1013 pgtable = get_pmd_huge_pte(tlb->mm);
1014 page = pmd_page(*pmd);
1015 pmd_clear(pmd);
1016 page_remove_rmap(page);
1017 VM_BUG_ON(page_mapcount(page) < 0);
1018 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1019 VM_BUG_ON(!PageHead(page));
1020 tlb->mm->nr_ptes--;
1021 spin_unlock(&tlb->mm->page_table_lock);
1022 tlb_remove_page(tlb, page);
1023 pte_free(tlb->mm, pgtable);
1024 ret = 1;
1026 } else
1027 spin_unlock(&tlb->mm->page_table_lock);
1029 return ret;
1032 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1033 unsigned long addr, unsigned long end,
1034 unsigned char *vec)
1036 int ret = 0;
1038 spin_lock(&vma->vm_mm->page_table_lock);
1039 if (likely(pmd_trans_huge(*pmd))) {
1040 ret = !pmd_trans_splitting(*pmd);
1041 spin_unlock(&vma->vm_mm->page_table_lock);
1042 if (unlikely(!ret))
1043 wait_split_huge_page(vma->anon_vma, pmd);
1044 else {
1046 * All logical pages in the range are present
1047 * if backed by a huge page.
1049 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1051 } else
1052 spin_unlock(&vma->vm_mm->page_table_lock);
1054 return ret;
1057 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1058 unsigned long addr, pgprot_t newprot)
1060 struct mm_struct *mm = vma->vm_mm;
1061 int ret = 0;
1063 spin_lock(&mm->page_table_lock);
1064 if (likely(pmd_trans_huge(*pmd))) {
1065 if (unlikely(pmd_trans_splitting(*pmd))) {
1066 spin_unlock(&mm->page_table_lock);
1067 wait_split_huge_page(vma->anon_vma, pmd);
1068 } else {
1069 pmd_t entry;
1071 entry = pmdp_get_and_clear(mm, addr, pmd);
1072 entry = pmd_modify(entry, newprot);
1073 set_pmd_at(mm, addr, pmd, entry);
1074 spin_unlock(&vma->vm_mm->page_table_lock);
1075 flush_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
1076 ret = 1;
1078 } else
1079 spin_unlock(&vma->vm_mm->page_table_lock);
1081 return ret;
1084 pmd_t *page_check_address_pmd(struct page *page,
1085 struct mm_struct *mm,
1086 unsigned long address,
1087 enum page_check_address_pmd_flag flag)
1089 pgd_t *pgd;
1090 pud_t *pud;
1091 pmd_t *pmd, *ret = NULL;
1093 if (address & ~HPAGE_PMD_MASK)
1094 goto out;
1096 pgd = pgd_offset(mm, address);
1097 if (!pgd_present(*pgd))
1098 goto out;
1100 pud = pud_offset(pgd, address);
1101 if (!pud_present(*pud))
1102 goto out;
1104 pmd = pmd_offset(pud, address);
1105 if (pmd_none(*pmd))
1106 goto out;
1107 if (pmd_page(*pmd) != page)
1108 goto out;
1110 * split_vma() may create temporary aliased mappings. There is
1111 * no risk as long as all huge pmd are found and have their
1112 * splitting bit set before __split_huge_page_refcount
1113 * runs. Finding the same huge pmd more than once during the
1114 * same rmap walk is not a problem.
1116 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1117 pmd_trans_splitting(*pmd))
1118 goto out;
1119 if (pmd_trans_huge(*pmd)) {
1120 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1121 !pmd_trans_splitting(*pmd));
1122 ret = pmd;
1124 out:
1125 return ret;
1128 static int __split_huge_page_splitting(struct page *page,
1129 struct vm_area_struct *vma,
1130 unsigned long address)
1132 struct mm_struct *mm = vma->vm_mm;
1133 pmd_t *pmd;
1134 int ret = 0;
1136 spin_lock(&mm->page_table_lock);
1137 pmd = page_check_address_pmd(page, mm, address,
1138 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1139 if (pmd) {
1141 * We can't temporarily set the pmd to null in order
1142 * to split it, the pmd must remain marked huge at all
1143 * times or the VM won't take the pmd_trans_huge paths
1144 * and it won't wait on the anon_vma->root->mutex to
1145 * serialize against split_huge_page*.
1147 pmdp_splitting_flush_notify(vma, address, pmd);
1148 ret = 1;
1150 spin_unlock(&mm->page_table_lock);
1152 return ret;
1155 static void __split_huge_page_refcount(struct page *page)
1157 int i;
1158 unsigned long head_index = page->index;
1159 struct zone *zone = page_zone(page);
1160 int zonestat;
1161 int tail_count = 0;
1163 /* prevent PageLRU to go away from under us, and freeze lru stats */
1164 spin_lock_irq(&zone->lru_lock);
1165 compound_lock(page);
1167 for (i = 1; i < HPAGE_PMD_NR; i++) {
1168 struct page *page_tail = page + i;
1170 /* tail_page->_mapcount cannot change */
1171 BUG_ON(page_mapcount(page_tail) < 0);
1172 tail_count += page_mapcount(page_tail);
1173 /* check for overflow */
1174 BUG_ON(tail_count < 0);
1175 BUG_ON(atomic_read(&page_tail->_count) != 0);
1177 * tail_page->_count is zero and not changing from
1178 * under us. But get_page_unless_zero() may be running
1179 * from under us on the tail_page. If we used
1180 * atomic_set() below instead of atomic_add(), we
1181 * would then run atomic_set() concurrently with
1182 * get_page_unless_zero(), and atomic_set() is
1183 * implemented in C not using locked ops. spin_unlock
1184 * on x86 sometime uses locked ops because of PPro
1185 * errata 66, 92, so unless somebody can guarantee
1186 * atomic_set() here would be safe on all archs (and
1187 * not only on x86), it's safer to use atomic_add().
1189 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1190 &page_tail->_count);
1192 /* after clearing PageTail the gup refcount can be released */
1193 smp_mb();
1196 * retain hwpoison flag of the poisoned tail page:
1197 * fix for the unsuitable process killed on Guest Machine(KVM)
1198 * by the memory-failure.
1200 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1201 page_tail->flags |= (page->flags &
1202 ((1L << PG_referenced) |
1203 (1L << PG_swapbacked) |
1204 (1L << PG_mlocked) |
1205 (1L << PG_uptodate)));
1206 page_tail->flags |= (1L << PG_dirty);
1208 /* clear PageTail before overwriting first_page */
1209 smp_wmb();
1212 * __split_huge_page_splitting() already set the
1213 * splitting bit in all pmd that could map this
1214 * hugepage, that will ensure no CPU can alter the
1215 * mapcount on the head page. The mapcount is only
1216 * accounted in the head page and it has to be
1217 * transferred to all tail pages in the below code. So
1218 * for this code to be safe, the split the mapcount
1219 * can't change. But that doesn't mean userland can't
1220 * keep changing and reading the page contents while
1221 * we transfer the mapcount, so the pmd splitting
1222 * status is achieved setting a reserved bit in the
1223 * pmd, not by clearing the present bit.
1225 page_tail->_mapcount = page->_mapcount;
1227 BUG_ON(page_tail->mapping);
1228 page_tail->mapping = page->mapping;
1230 page_tail->index = ++head_index;
1232 BUG_ON(!PageAnon(page_tail));
1233 BUG_ON(!PageUptodate(page_tail));
1234 BUG_ON(!PageDirty(page_tail));
1235 BUG_ON(!PageSwapBacked(page_tail));
1237 mem_cgroup_split_huge_fixup(page, page_tail);
1239 lru_add_page_tail(zone, page, page_tail);
1241 atomic_sub(tail_count, &page->_count);
1242 BUG_ON(atomic_read(&page->_count) <= 0);
1244 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1245 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1248 * A hugepage counts for HPAGE_PMD_NR pages on the LRU statistics,
1249 * so adjust those appropriately if this page is on the LRU.
1251 if (PageLRU(page)) {
1252 zonestat = NR_LRU_BASE + page_lru(page);
1253 __mod_zone_page_state(zone, zonestat, -(HPAGE_PMD_NR-1));
1256 ClearPageCompound(page);
1257 compound_unlock(page);
1258 spin_unlock_irq(&zone->lru_lock);
1260 for (i = 1; i < HPAGE_PMD_NR; i++) {
1261 struct page *page_tail = page + i;
1262 BUG_ON(page_count(page_tail) <= 0);
1264 * Tail pages may be freed if there wasn't any mapping
1265 * like if add_to_swap() is running on a lru page that
1266 * had its mapping zapped. And freeing these pages
1267 * requires taking the lru_lock so we do the put_page
1268 * of the tail pages after the split is complete.
1270 put_page(page_tail);
1274 * Only the head page (now become a regular page) is required
1275 * to be pinned by the caller.
1277 BUG_ON(page_count(page) <= 0);
1280 static int __split_huge_page_map(struct page *page,
1281 struct vm_area_struct *vma,
1282 unsigned long address)
1284 struct mm_struct *mm = vma->vm_mm;
1285 pmd_t *pmd, _pmd;
1286 int ret = 0, i;
1287 pgtable_t pgtable;
1288 unsigned long haddr;
1290 spin_lock(&mm->page_table_lock);
1291 pmd = page_check_address_pmd(page, mm, address,
1292 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1293 if (pmd) {
1294 pgtable = get_pmd_huge_pte(mm);
1295 pmd_populate(mm, &_pmd, pgtable);
1297 for (i = 0, haddr = address; i < HPAGE_PMD_NR;
1298 i++, haddr += PAGE_SIZE) {
1299 pte_t *pte, entry;
1300 BUG_ON(PageCompound(page+i));
1301 entry = mk_pte(page + i, vma->vm_page_prot);
1302 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1303 if (!pmd_write(*pmd))
1304 entry = pte_wrprotect(entry);
1305 else
1306 BUG_ON(page_mapcount(page) != 1);
1307 if (!pmd_young(*pmd))
1308 entry = pte_mkold(entry);
1309 pte = pte_offset_map(&_pmd, haddr);
1310 BUG_ON(!pte_none(*pte));
1311 set_pte_at(mm, haddr, pte, entry);
1312 pte_unmap(pte);
1315 smp_wmb(); /* make pte visible before pmd */
1317 * Up to this point the pmd is present and huge and
1318 * userland has the whole access to the hugepage
1319 * during the split (which happens in place). If we
1320 * overwrite the pmd with the not-huge version
1321 * pointing to the pte here (which of course we could
1322 * if all CPUs were bug free), userland could trigger
1323 * a small page size TLB miss on the small sized TLB
1324 * while the hugepage TLB entry is still established
1325 * in the huge TLB. Some CPU doesn't like that. See
1326 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1327 * Erratum 383 on page 93. Intel should be safe but is
1328 * also warns that it's only safe if the permission
1329 * and cache attributes of the two entries loaded in
1330 * the two TLB is identical (which should be the case
1331 * here). But it is generally safer to never allow
1332 * small and huge TLB entries for the same virtual
1333 * address to be loaded simultaneously. So instead of
1334 * doing "pmd_populate(); flush_tlb_range();" we first
1335 * mark the current pmd notpresent (atomically because
1336 * here the pmd_trans_huge and pmd_trans_splitting
1337 * must remain set at all times on the pmd until the
1338 * split is complete for this pmd), then we flush the
1339 * SMP TLB and finally we write the non-huge version
1340 * of the pmd entry with pmd_populate.
1342 set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
1343 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
1344 pmd_populate(mm, pmd, pgtable);
1345 ret = 1;
1347 spin_unlock(&mm->page_table_lock);
1349 return ret;
1352 /* must be called with anon_vma->root->mutex hold */
1353 static void __split_huge_page(struct page *page,
1354 struct anon_vma *anon_vma)
1356 int mapcount, mapcount2;
1357 struct anon_vma_chain *avc;
1359 BUG_ON(!PageHead(page));
1360 BUG_ON(PageTail(page));
1362 mapcount = 0;
1363 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1364 struct vm_area_struct *vma = avc->vma;
1365 unsigned long addr = vma_address(page, vma);
1366 BUG_ON(is_vma_temporary_stack(vma));
1367 if (addr == -EFAULT)
1368 continue;
1369 mapcount += __split_huge_page_splitting(page, vma, addr);
1372 * It is critical that new vmas are added to the tail of the
1373 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1374 * and establishes a child pmd before
1375 * __split_huge_page_splitting() freezes the parent pmd (so if
1376 * we fail to prevent copy_huge_pmd() from running until the
1377 * whole __split_huge_page() is complete), we will still see
1378 * the newly established pmd of the child later during the
1379 * walk, to be able to set it as pmd_trans_splitting too.
1381 if (mapcount != page_mapcount(page))
1382 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1383 mapcount, page_mapcount(page));
1384 BUG_ON(mapcount != page_mapcount(page));
1386 __split_huge_page_refcount(page);
1388 mapcount2 = 0;
1389 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1390 struct vm_area_struct *vma = avc->vma;
1391 unsigned long addr = vma_address(page, vma);
1392 BUG_ON(is_vma_temporary_stack(vma));
1393 if (addr == -EFAULT)
1394 continue;
1395 mapcount2 += __split_huge_page_map(page, vma, addr);
1397 if (mapcount != mapcount2)
1398 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1399 mapcount, mapcount2, page_mapcount(page));
1400 BUG_ON(mapcount != mapcount2);
1403 int split_huge_page(struct page *page)
1405 struct anon_vma *anon_vma;
1406 int ret = 1;
1408 BUG_ON(!PageAnon(page));
1409 anon_vma = page_lock_anon_vma(page);
1410 if (!anon_vma)
1411 goto out;
1412 ret = 0;
1413 if (!PageCompound(page))
1414 goto out_unlock;
1416 BUG_ON(!PageSwapBacked(page));
1417 __split_huge_page(page, anon_vma);
1418 count_vm_event(THP_SPLIT);
1420 BUG_ON(PageCompound(page));
1421 out_unlock:
1422 page_unlock_anon_vma(anon_vma);
1423 out:
1424 return ret;
1427 #define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
1428 VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1430 int hugepage_madvise(struct vm_area_struct *vma,
1431 unsigned long *vm_flags, int advice)
1433 switch (advice) {
1434 case MADV_HUGEPAGE:
1436 * Be somewhat over-protective like KSM for now!
1438 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1439 return -EINVAL;
1440 *vm_flags &= ~VM_NOHUGEPAGE;
1441 *vm_flags |= VM_HUGEPAGE;
1443 * If the vma become good for khugepaged to scan,
1444 * register it here without waiting a page fault that
1445 * may not happen any time soon.
1447 if (unlikely(khugepaged_enter_vma_merge(vma)))
1448 return -ENOMEM;
1449 break;
1450 case MADV_NOHUGEPAGE:
1452 * Be somewhat over-protective like KSM for now!
1454 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1455 return -EINVAL;
1456 *vm_flags &= ~VM_HUGEPAGE;
1457 *vm_flags |= VM_NOHUGEPAGE;
1459 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1460 * this vma even if we leave the mm registered in khugepaged if
1461 * it got registered before VM_NOHUGEPAGE was set.
1463 break;
1466 return 0;
1469 static int __init khugepaged_slab_init(void)
1471 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1472 sizeof(struct mm_slot),
1473 __alignof__(struct mm_slot), 0, NULL);
1474 if (!mm_slot_cache)
1475 return -ENOMEM;
1477 return 0;
1480 static void __init khugepaged_slab_free(void)
1482 kmem_cache_destroy(mm_slot_cache);
1483 mm_slot_cache = NULL;
1486 static inline struct mm_slot *alloc_mm_slot(void)
1488 if (!mm_slot_cache) /* initialization failed */
1489 return NULL;
1490 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1493 static inline void free_mm_slot(struct mm_slot *mm_slot)
1495 kmem_cache_free(mm_slot_cache, mm_slot);
1498 static int __init mm_slots_hash_init(void)
1500 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1501 GFP_KERNEL);
1502 if (!mm_slots_hash)
1503 return -ENOMEM;
1504 return 0;
1507 #if 0
1508 static void __init mm_slots_hash_free(void)
1510 kfree(mm_slots_hash);
1511 mm_slots_hash = NULL;
1513 #endif
1515 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1517 struct mm_slot *mm_slot;
1518 struct hlist_head *bucket;
1519 struct hlist_node *node;
1521 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1522 % MM_SLOTS_HASH_HEADS];
1523 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1524 if (mm == mm_slot->mm)
1525 return mm_slot;
1527 return NULL;
1530 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1531 struct mm_slot *mm_slot)
1533 struct hlist_head *bucket;
1535 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1536 % MM_SLOTS_HASH_HEADS];
1537 mm_slot->mm = mm;
1538 hlist_add_head(&mm_slot->hash, bucket);
1541 static inline int khugepaged_test_exit(struct mm_struct *mm)
1543 return atomic_read(&mm->mm_users) == 0;
1546 int __khugepaged_enter(struct mm_struct *mm)
1548 struct mm_slot *mm_slot;
1549 int wakeup;
1551 mm_slot = alloc_mm_slot();
1552 if (!mm_slot)
1553 return -ENOMEM;
1555 /* __khugepaged_exit() must not run from under us */
1556 VM_BUG_ON(khugepaged_test_exit(mm));
1557 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1558 free_mm_slot(mm_slot);
1559 return 0;
1562 spin_lock(&khugepaged_mm_lock);
1563 insert_to_mm_slots_hash(mm, mm_slot);
1565 * Insert just behind the scanning cursor, to let the area settle
1566 * down a little.
1568 wakeup = list_empty(&khugepaged_scan.mm_head);
1569 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1570 spin_unlock(&khugepaged_mm_lock);
1572 atomic_inc(&mm->mm_count);
1573 if (wakeup)
1574 wake_up_interruptible(&khugepaged_wait);
1576 return 0;
1579 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1581 unsigned long hstart, hend;
1582 if (!vma->anon_vma)
1584 * Not yet faulted in so we will register later in the
1585 * page fault if needed.
1587 return 0;
1588 if (vma->vm_ops)
1589 /* khugepaged not yet working on file or special mappings */
1590 return 0;
1592 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1593 * true too, verify it here.
1595 VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
1596 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1597 hend = vma->vm_end & HPAGE_PMD_MASK;
1598 if (hstart < hend)
1599 return khugepaged_enter(vma);
1600 return 0;
1603 void __khugepaged_exit(struct mm_struct *mm)
1605 struct mm_slot *mm_slot;
1606 int free = 0;
1608 spin_lock(&khugepaged_mm_lock);
1609 mm_slot = get_mm_slot(mm);
1610 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1611 hlist_del(&mm_slot->hash);
1612 list_del(&mm_slot->mm_node);
1613 free = 1;
1616 if (free) {
1617 spin_unlock(&khugepaged_mm_lock);
1618 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1619 free_mm_slot(mm_slot);
1620 mmdrop(mm);
1621 } else if (mm_slot) {
1622 spin_unlock(&khugepaged_mm_lock);
1624 * This is required to serialize against
1625 * khugepaged_test_exit() (which is guaranteed to run
1626 * under mmap sem read mode). Stop here (after we
1627 * return all pagetables will be destroyed) until
1628 * khugepaged has finished working on the pagetables
1629 * under the mmap_sem.
1631 down_write(&mm->mmap_sem);
1632 up_write(&mm->mmap_sem);
1633 } else
1634 spin_unlock(&khugepaged_mm_lock);
1637 static void release_pte_page(struct page *page)
1639 /* 0 stands for page_is_file_cache(page) == false */
1640 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1641 unlock_page(page);
1642 putback_lru_page(page);
1645 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1647 while (--_pte >= pte) {
1648 pte_t pteval = *_pte;
1649 if (!pte_none(pteval))
1650 release_pte_page(pte_page(pteval));
1654 static void release_all_pte_pages(pte_t *pte)
1656 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1659 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1660 unsigned long address,
1661 pte_t *pte)
1663 struct page *page;
1664 pte_t *_pte;
1665 int referenced = 0, isolated = 0, none = 0;
1666 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1667 _pte++, address += PAGE_SIZE) {
1668 pte_t pteval = *_pte;
1669 if (pte_none(pteval)) {
1670 if (++none <= khugepaged_max_ptes_none)
1671 continue;
1672 else {
1673 release_pte_pages(pte, _pte);
1674 goto out;
1677 if (!pte_present(pteval) || !pte_write(pteval)) {
1678 release_pte_pages(pte, _pte);
1679 goto out;
1681 page = vm_normal_page(vma, address, pteval);
1682 if (unlikely(!page)) {
1683 release_pte_pages(pte, _pte);
1684 goto out;
1686 VM_BUG_ON(PageCompound(page));
1687 BUG_ON(!PageAnon(page));
1688 VM_BUG_ON(!PageSwapBacked(page));
1690 /* cannot use mapcount: can't collapse if there's a gup pin */
1691 if (page_count(page) != 1) {
1692 release_pte_pages(pte, _pte);
1693 goto out;
1696 * We can do it before isolate_lru_page because the
1697 * page can't be freed from under us. NOTE: PG_lock
1698 * is needed to serialize against split_huge_page
1699 * when invoked from the VM.
1701 if (!trylock_page(page)) {
1702 release_pte_pages(pte, _pte);
1703 goto out;
1706 * Isolate the page to avoid collapsing an hugepage
1707 * currently in use by the VM.
1709 if (isolate_lru_page(page)) {
1710 unlock_page(page);
1711 release_pte_pages(pte, _pte);
1712 goto out;
1714 /* 0 stands for page_is_file_cache(page) == false */
1715 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1716 VM_BUG_ON(!PageLocked(page));
1717 VM_BUG_ON(PageLRU(page));
1719 /* If there is no mapped pte young don't collapse the page */
1720 if (pte_young(pteval) || PageReferenced(page) ||
1721 mmu_notifier_test_young(vma->vm_mm, address))
1722 referenced = 1;
1724 if (unlikely(!referenced))
1725 release_all_pte_pages(pte);
1726 else
1727 isolated = 1;
1728 out:
1729 return isolated;
1732 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1733 struct vm_area_struct *vma,
1734 unsigned long address,
1735 spinlock_t *ptl)
1737 pte_t *_pte;
1738 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1739 pte_t pteval = *_pte;
1740 struct page *src_page;
1742 if (pte_none(pteval)) {
1743 clear_user_highpage(page, address);
1744 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1745 } else {
1746 src_page = pte_page(pteval);
1747 copy_user_highpage(page, src_page, address, vma);
1748 VM_BUG_ON(page_mapcount(src_page) != 1);
1749 VM_BUG_ON(page_count(src_page) != 2);
1750 release_pte_page(src_page);
1752 * ptl mostly unnecessary, but preempt has to
1753 * be disabled to update the per-cpu stats
1754 * inside page_remove_rmap().
1756 spin_lock(ptl);
1758 * paravirt calls inside pte_clear here are
1759 * superfluous.
1761 pte_clear(vma->vm_mm, address, _pte);
1762 page_remove_rmap(src_page);
1763 spin_unlock(ptl);
1764 free_page_and_swap_cache(src_page);
1767 address += PAGE_SIZE;
1768 page++;
1772 static void collapse_huge_page(struct mm_struct *mm,
1773 unsigned long address,
1774 struct page **hpage,
1775 struct vm_area_struct *vma,
1776 int node)
1778 pgd_t *pgd;
1779 pud_t *pud;
1780 pmd_t *pmd, _pmd;
1781 pte_t *pte;
1782 pgtable_t pgtable;
1783 struct page *new_page;
1784 spinlock_t *ptl;
1785 int isolated;
1786 unsigned long hstart, hend;
1788 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1789 #ifndef CONFIG_NUMA
1790 up_read(&mm->mmap_sem);
1791 VM_BUG_ON(!*hpage);
1792 new_page = *hpage;
1793 #else
1794 VM_BUG_ON(*hpage);
1796 * Allocate the page while the vma is still valid and under
1797 * the mmap_sem read mode so there is no memory allocation
1798 * later when we take the mmap_sem in write mode. This is more
1799 * friendly behavior (OTOH it may actually hide bugs) to
1800 * filesystems in userland with daemons allocating memory in
1801 * the userland I/O paths. Allocating memory with the
1802 * mmap_sem in read mode is good idea also to allow greater
1803 * scalability.
1805 new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1806 node, __GFP_OTHER_NODE);
1809 * After allocating the hugepage, release the mmap_sem read lock in
1810 * preparation for taking it in write mode.
1812 up_read(&mm->mmap_sem);
1813 if (unlikely(!new_page)) {
1814 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1815 *hpage = ERR_PTR(-ENOMEM);
1816 return;
1818 #endif
1820 count_vm_event(THP_COLLAPSE_ALLOC);
1821 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1822 #ifdef CONFIG_NUMA
1823 put_page(new_page);
1824 #endif
1825 return;
1829 * Prevent all access to pagetables with the exception of
1830 * gup_fast later hanlded by the ptep_clear_flush and the VM
1831 * handled by the anon_vma lock + PG_lock.
1833 down_write(&mm->mmap_sem);
1834 if (unlikely(khugepaged_test_exit(mm)))
1835 goto out;
1837 vma = find_vma(mm, address);
1838 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1839 hend = vma->vm_end & HPAGE_PMD_MASK;
1840 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1841 goto out;
1843 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1844 (vma->vm_flags & VM_NOHUGEPAGE))
1845 goto out;
1847 if (!vma->anon_vma || vma->vm_ops)
1848 goto out;
1849 if (is_vma_temporary_stack(vma))
1850 goto out;
1852 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1853 * true too, verify it here.
1855 VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
1857 pgd = pgd_offset(mm, address);
1858 if (!pgd_present(*pgd))
1859 goto out;
1861 pud = pud_offset(pgd, address);
1862 if (!pud_present(*pud))
1863 goto out;
1865 pmd = pmd_offset(pud, address);
1866 /* pmd can't go away or become huge under us */
1867 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1868 goto out;
1870 anon_vma_lock(vma->anon_vma);
1872 pte = pte_offset_map(pmd, address);
1873 ptl = pte_lockptr(mm, pmd);
1875 spin_lock(&mm->page_table_lock); /* probably unnecessary */
1877 * After this gup_fast can't run anymore. This also removes
1878 * any huge TLB entry from the CPU so we won't allow
1879 * huge and small TLB entries for the same virtual address
1880 * to avoid the risk of CPU bugs in that area.
1882 _pmd = pmdp_clear_flush_notify(vma, address, pmd);
1883 spin_unlock(&mm->page_table_lock);
1885 spin_lock(ptl);
1886 isolated = __collapse_huge_page_isolate(vma, address, pte);
1887 spin_unlock(ptl);
1889 if (unlikely(!isolated)) {
1890 pte_unmap(pte);
1891 spin_lock(&mm->page_table_lock);
1892 BUG_ON(!pmd_none(*pmd));
1893 set_pmd_at(mm, address, pmd, _pmd);
1894 spin_unlock(&mm->page_table_lock);
1895 anon_vma_unlock(vma->anon_vma);
1896 goto out;
1900 * All pages are isolated and locked so anon_vma rmap
1901 * can't run anymore.
1903 anon_vma_unlock(vma->anon_vma);
1905 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
1906 pte_unmap(pte);
1907 __SetPageUptodate(new_page);
1908 pgtable = pmd_pgtable(_pmd);
1909 VM_BUG_ON(page_count(pgtable) != 1);
1910 VM_BUG_ON(page_mapcount(pgtable) != 0);
1912 _pmd = mk_pmd(new_page, vma->vm_page_prot);
1913 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1914 _pmd = pmd_mkhuge(_pmd);
1917 * spin_lock() below is not the equivalent of smp_wmb(), so
1918 * this is needed to avoid the copy_huge_page writes to become
1919 * visible after the set_pmd_at() write.
1921 smp_wmb();
1923 spin_lock(&mm->page_table_lock);
1924 BUG_ON(!pmd_none(*pmd));
1925 page_add_new_anon_rmap(new_page, vma, address);
1926 set_pmd_at(mm, address, pmd, _pmd);
1927 update_mmu_cache(vma, address, entry);
1928 prepare_pmd_huge_pte(pgtable, mm);
1929 spin_unlock(&mm->page_table_lock);
1931 #ifndef CONFIG_NUMA
1932 *hpage = NULL;
1933 #endif
1934 khugepaged_pages_collapsed++;
1935 out_up_write:
1936 up_write(&mm->mmap_sem);
1937 return;
1939 out:
1940 mem_cgroup_uncharge_page(new_page);
1941 #ifdef CONFIG_NUMA
1942 put_page(new_page);
1943 #endif
1944 goto out_up_write;
1947 static int khugepaged_scan_pmd(struct mm_struct *mm,
1948 struct vm_area_struct *vma,
1949 unsigned long address,
1950 struct page **hpage)
1952 pgd_t *pgd;
1953 pud_t *pud;
1954 pmd_t *pmd;
1955 pte_t *pte, *_pte;
1956 int ret = 0, referenced = 0, none = 0;
1957 struct page *page;
1958 unsigned long _address;
1959 spinlock_t *ptl;
1960 int node = -1;
1962 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1964 pgd = pgd_offset(mm, address);
1965 if (!pgd_present(*pgd))
1966 goto out;
1968 pud = pud_offset(pgd, address);
1969 if (!pud_present(*pud))
1970 goto out;
1972 pmd = pmd_offset(pud, address);
1973 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1974 goto out;
1976 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1977 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
1978 _pte++, _address += PAGE_SIZE) {
1979 pte_t pteval = *_pte;
1980 if (pte_none(pteval)) {
1981 if (++none <= khugepaged_max_ptes_none)
1982 continue;
1983 else
1984 goto out_unmap;
1986 if (!pte_present(pteval) || !pte_write(pteval))
1987 goto out_unmap;
1988 page = vm_normal_page(vma, _address, pteval);
1989 if (unlikely(!page))
1990 goto out_unmap;
1992 * Chose the node of the first page. This could
1993 * be more sophisticated and look at more pages,
1994 * but isn't for now.
1996 if (node == -1)
1997 node = page_to_nid(page);
1998 VM_BUG_ON(PageCompound(page));
1999 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2000 goto out_unmap;
2001 /* cannot use mapcount: can't collapse if there's a gup pin */
2002 if (page_count(page) != 1)
2003 goto out_unmap;
2004 if (pte_young(pteval) || PageReferenced(page) ||
2005 mmu_notifier_test_young(vma->vm_mm, address))
2006 referenced = 1;
2008 if (referenced)
2009 ret = 1;
2010 out_unmap:
2011 pte_unmap_unlock(pte, ptl);
2012 if (ret)
2013 /* collapse_huge_page will return with the mmap_sem released */
2014 collapse_huge_page(mm, address, hpage, vma, node);
2015 out:
2016 return ret;
2019 static void collect_mm_slot(struct mm_slot *mm_slot)
2021 struct mm_struct *mm = mm_slot->mm;
2023 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2025 if (khugepaged_test_exit(mm)) {
2026 /* free mm_slot */
2027 hlist_del(&mm_slot->hash);
2028 list_del(&mm_slot->mm_node);
2031 * Not strictly needed because the mm exited already.
2033 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2036 /* khugepaged_mm_lock actually not necessary for the below */
2037 free_mm_slot(mm_slot);
2038 mmdrop(mm);
2042 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2043 struct page **hpage)
2045 struct mm_slot *mm_slot;
2046 struct mm_struct *mm;
2047 struct vm_area_struct *vma;
2048 int progress = 0;
2050 VM_BUG_ON(!pages);
2051 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2053 if (khugepaged_scan.mm_slot)
2054 mm_slot = khugepaged_scan.mm_slot;
2055 else {
2056 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2057 struct mm_slot, mm_node);
2058 khugepaged_scan.address = 0;
2059 khugepaged_scan.mm_slot = mm_slot;
2061 spin_unlock(&khugepaged_mm_lock);
2063 mm = mm_slot->mm;
2064 down_read(&mm->mmap_sem);
2065 if (unlikely(khugepaged_test_exit(mm)))
2066 vma = NULL;
2067 else
2068 vma = find_vma(mm, khugepaged_scan.address);
2070 progress++;
2071 for (; vma; vma = vma->vm_next) {
2072 unsigned long hstart, hend;
2074 cond_resched();
2075 if (unlikely(khugepaged_test_exit(mm))) {
2076 progress++;
2077 break;
2080 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2081 !khugepaged_always()) ||
2082 (vma->vm_flags & VM_NOHUGEPAGE)) {
2083 skip:
2084 progress++;
2085 continue;
2087 if (!vma->anon_vma || vma->vm_ops)
2088 goto skip;
2089 if (is_vma_temporary_stack(vma))
2090 goto skip;
2092 * If is_pfn_mapping() is true is_learn_pfn_mapping()
2093 * must be true too, verify it here.
2095 VM_BUG_ON(is_linear_pfn_mapping(vma) ||
2096 vma->vm_flags & VM_NO_THP);
2098 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2099 hend = vma->vm_end & HPAGE_PMD_MASK;
2100 if (hstart >= hend)
2101 goto skip;
2102 if (khugepaged_scan.address > hend)
2103 goto skip;
2104 if (khugepaged_scan.address < hstart)
2105 khugepaged_scan.address = hstart;
2106 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2108 while (khugepaged_scan.address < hend) {
2109 int ret;
2110 cond_resched();
2111 if (unlikely(khugepaged_test_exit(mm)))
2112 goto breakouterloop;
2114 VM_BUG_ON(khugepaged_scan.address < hstart ||
2115 khugepaged_scan.address + HPAGE_PMD_SIZE >
2116 hend);
2117 ret = khugepaged_scan_pmd(mm, vma,
2118 khugepaged_scan.address,
2119 hpage);
2120 /* move to next address */
2121 khugepaged_scan.address += HPAGE_PMD_SIZE;
2122 progress += HPAGE_PMD_NR;
2123 if (ret)
2124 /* we released mmap_sem so break loop */
2125 goto breakouterloop_mmap_sem;
2126 if (progress >= pages)
2127 goto breakouterloop;
2130 breakouterloop:
2131 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2132 breakouterloop_mmap_sem:
2134 spin_lock(&khugepaged_mm_lock);
2135 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2137 * Release the current mm_slot if this mm is about to die, or
2138 * if we scanned all vmas of this mm.
2140 if (khugepaged_test_exit(mm) || !vma) {
2142 * Make sure that if mm_users is reaching zero while
2143 * khugepaged runs here, khugepaged_exit will find
2144 * mm_slot not pointing to the exiting mm.
2146 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2147 khugepaged_scan.mm_slot = list_entry(
2148 mm_slot->mm_node.next,
2149 struct mm_slot, mm_node);
2150 khugepaged_scan.address = 0;
2151 } else {
2152 khugepaged_scan.mm_slot = NULL;
2153 khugepaged_full_scans++;
2156 collect_mm_slot(mm_slot);
2159 return progress;
2162 static int khugepaged_has_work(void)
2164 return !list_empty(&khugepaged_scan.mm_head) &&
2165 khugepaged_enabled();
2168 static int khugepaged_wait_event(void)
2170 return !list_empty(&khugepaged_scan.mm_head) ||
2171 !khugepaged_enabled();
2174 static void khugepaged_do_scan(struct page **hpage)
2176 unsigned int progress = 0, pass_through_head = 0;
2177 unsigned int pages = khugepaged_pages_to_scan;
2179 barrier(); /* write khugepaged_pages_to_scan to local stack */
2181 while (progress < pages) {
2182 cond_resched();
2184 #ifndef CONFIG_NUMA
2185 if (!*hpage) {
2186 *hpage = alloc_hugepage(khugepaged_defrag());
2187 if (unlikely(!*hpage)) {
2188 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2189 break;
2191 count_vm_event(THP_COLLAPSE_ALLOC);
2193 #else
2194 if (IS_ERR(*hpage))
2195 break;
2196 #endif
2198 if (unlikely(kthread_should_stop() || freezing(current)))
2199 break;
2201 spin_lock(&khugepaged_mm_lock);
2202 if (!khugepaged_scan.mm_slot)
2203 pass_through_head++;
2204 if (khugepaged_has_work() &&
2205 pass_through_head < 2)
2206 progress += khugepaged_scan_mm_slot(pages - progress,
2207 hpage);
2208 else
2209 progress = pages;
2210 spin_unlock(&khugepaged_mm_lock);
2214 static void khugepaged_alloc_sleep(void)
2216 DEFINE_WAIT(wait);
2217 add_wait_queue(&khugepaged_wait, &wait);
2218 schedule_timeout_interruptible(
2219 msecs_to_jiffies(
2220 khugepaged_alloc_sleep_millisecs));
2221 remove_wait_queue(&khugepaged_wait, &wait);
2224 #ifndef CONFIG_NUMA
2225 static struct page *khugepaged_alloc_hugepage(void)
2227 struct page *hpage;
2229 do {
2230 hpage = alloc_hugepage(khugepaged_defrag());
2231 if (!hpage) {
2232 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2233 khugepaged_alloc_sleep();
2234 } else
2235 count_vm_event(THP_COLLAPSE_ALLOC);
2236 } while (unlikely(!hpage) &&
2237 likely(khugepaged_enabled()));
2238 return hpage;
2240 #endif
2242 static void khugepaged_loop(void)
2244 struct page *hpage;
2246 #ifdef CONFIG_NUMA
2247 hpage = NULL;
2248 #endif
2249 while (likely(khugepaged_enabled())) {
2250 #ifndef CONFIG_NUMA
2251 hpage = khugepaged_alloc_hugepage();
2252 if (unlikely(!hpage))
2253 break;
2254 #else
2255 if (IS_ERR(hpage)) {
2256 khugepaged_alloc_sleep();
2257 hpage = NULL;
2259 #endif
2261 khugepaged_do_scan(&hpage);
2262 #ifndef CONFIG_NUMA
2263 if (hpage)
2264 put_page(hpage);
2265 #endif
2266 try_to_freeze();
2267 if (unlikely(kthread_should_stop()))
2268 break;
2269 if (khugepaged_has_work()) {
2270 DEFINE_WAIT(wait);
2271 if (!khugepaged_scan_sleep_millisecs)
2272 continue;
2273 add_wait_queue(&khugepaged_wait, &wait);
2274 schedule_timeout_interruptible(
2275 msecs_to_jiffies(
2276 khugepaged_scan_sleep_millisecs));
2277 remove_wait_queue(&khugepaged_wait, &wait);
2278 } else if (khugepaged_enabled())
2279 wait_event_freezable(khugepaged_wait,
2280 khugepaged_wait_event());
2284 static int khugepaged(void *none)
2286 struct mm_slot *mm_slot;
2288 set_freezable();
2289 set_user_nice(current, 19);
2291 /* serialize with start_khugepaged() */
2292 mutex_lock(&khugepaged_mutex);
2294 for (;;) {
2295 mutex_unlock(&khugepaged_mutex);
2296 VM_BUG_ON(khugepaged_thread != current);
2297 khugepaged_loop();
2298 VM_BUG_ON(khugepaged_thread != current);
2300 mutex_lock(&khugepaged_mutex);
2301 if (!khugepaged_enabled())
2302 break;
2303 if (unlikely(kthread_should_stop()))
2304 break;
2307 spin_lock(&khugepaged_mm_lock);
2308 mm_slot = khugepaged_scan.mm_slot;
2309 khugepaged_scan.mm_slot = NULL;
2310 if (mm_slot)
2311 collect_mm_slot(mm_slot);
2312 spin_unlock(&khugepaged_mm_lock);
2314 khugepaged_thread = NULL;
2315 mutex_unlock(&khugepaged_mutex);
2317 return 0;
2320 void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2322 struct page *page;
2324 spin_lock(&mm->page_table_lock);
2325 if (unlikely(!pmd_trans_huge(*pmd))) {
2326 spin_unlock(&mm->page_table_lock);
2327 return;
2329 page = pmd_page(*pmd);
2330 VM_BUG_ON(!page_count(page));
2331 get_page(page);
2332 spin_unlock(&mm->page_table_lock);
2334 split_huge_page(page);
2336 put_page(page);
2337 BUG_ON(pmd_trans_huge(*pmd));
2340 static void split_huge_page_address(struct mm_struct *mm,
2341 unsigned long address)
2343 pgd_t *pgd;
2344 pud_t *pud;
2345 pmd_t *pmd;
2347 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2349 pgd = pgd_offset(mm, address);
2350 if (!pgd_present(*pgd))
2351 return;
2353 pud = pud_offset(pgd, address);
2354 if (!pud_present(*pud))
2355 return;
2357 pmd = pmd_offset(pud, address);
2358 if (!pmd_present(*pmd))
2359 return;
2361 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2362 * materialize from under us.
2364 split_huge_page_pmd(mm, pmd);
2367 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2368 unsigned long start,
2369 unsigned long end,
2370 long adjust_next)
2373 * If the new start address isn't hpage aligned and it could
2374 * previously contain an hugepage: check if we need to split
2375 * an huge pmd.
2377 if (start & ~HPAGE_PMD_MASK &&
2378 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2379 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2380 split_huge_page_address(vma->vm_mm, start);
2383 * If the new end address isn't hpage aligned and it could
2384 * previously contain an hugepage: check if we need to split
2385 * an huge pmd.
2387 if (end & ~HPAGE_PMD_MASK &&
2388 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2389 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2390 split_huge_page_address(vma->vm_mm, end);
2393 * If we're also updating the vma->vm_next->vm_start, if the new
2394 * vm_next->vm_start isn't page aligned and it could previously
2395 * contain an hugepage: check if we need to split an huge pmd.
2397 if (adjust_next > 0) {
2398 struct vm_area_struct *next = vma->vm_next;
2399 unsigned long nstart = next->vm_start;
2400 nstart += adjust_next << PAGE_SHIFT;
2401 if (nstart & ~HPAGE_PMD_MASK &&
2402 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2403 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2404 split_huge_page_address(next->vm_mm, nstart);