2 * Copyright (C) 2009 Red Hat, Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
9 #include <linux/sched.h>
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/mm_inline.h>
16 #include <linux/kthread.h>
17 #include <linux/khugepaged.h>
18 #include <linux/freezer.h>
19 #include <linux/mman.h>
21 #include <asm/pgalloc.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
31 unsigned long transparent_hugepage_flags __read_mostly
=
32 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
33 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
35 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
36 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
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
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
;
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
75 struct hlist_node hash
;
76 struct list_head mm_node
;
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
;
93 static struct khugepaged_scan khugepaged_scan
= {
94 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
98 static int set_recommended_min_free_kbytes(void)
102 unsigned long recommended_min
;
103 extern int min_free_kbytes
;
105 if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG
,
106 &transparent_hugepage_flags
) &&
107 !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
108 &transparent_hugepage_flags
))
111 for_each_populated_zone(zone
)
114 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
115 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
118 * Make sure that on average at least two pageblocks are almost free
119 * of another type, one for a migratetype to fall back to and a
120 * second to avoid subsequent fallbacks of other types There are 3
121 * MIGRATE_TYPES we care about.
123 recommended_min
+= pageblock_nr_pages
* nr_zones
*
124 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
126 /* don't ever allow to reserve more than 5% of the lowmem */
127 recommended_min
= min(recommended_min
,
128 (unsigned long) nr_free_buffer_pages() / 20);
129 recommended_min
<<= (PAGE_SHIFT
-10);
131 if (recommended_min
> min_free_kbytes
)
132 min_free_kbytes
= recommended_min
;
133 setup_per_zone_wmarks();
136 late_initcall(set_recommended_min_free_kbytes
);
138 static int start_khugepaged(void)
141 if (khugepaged_enabled()) {
143 if (unlikely(!mm_slot_cache
|| !mm_slots_hash
)) {
147 mutex_lock(&khugepaged_mutex
);
148 if (!khugepaged_thread
)
149 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
151 if (unlikely(IS_ERR(khugepaged_thread
))) {
153 "khugepaged: kthread_run(khugepaged) failed\n");
154 err
= PTR_ERR(khugepaged_thread
);
155 khugepaged_thread
= NULL
;
157 wakeup
= !list_empty(&khugepaged_scan
.mm_head
);
158 mutex_unlock(&khugepaged_mutex
);
160 wake_up_interruptible(&khugepaged_wait
);
162 set_recommended_min_free_kbytes();
165 wake_up_interruptible(&khugepaged_wait
);
172 static ssize_t
double_flag_show(struct kobject
*kobj
,
173 struct kobj_attribute
*attr
, char *buf
,
174 enum transparent_hugepage_flag enabled
,
175 enum transparent_hugepage_flag req_madv
)
177 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
178 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
179 return sprintf(buf
, "[always] madvise never\n");
180 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
181 return sprintf(buf
, "always [madvise] never\n");
183 return sprintf(buf
, "always madvise [never]\n");
185 static ssize_t
double_flag_store(struct kobject
*kobj
,
186 struct kobj_attribute
*attr
,
187 const char *buf
, size_t count
,
188 enum transparent_hugepage_flag enabled
,
189 enum transparent_hugepage_flag req_madv
)
191 if (!memcmp("always", buf
,
192 min(sizeof("always")-1, count
))) {
193 set_bit(enabled
, &transparent_hugepage_flags
);
194 clear_bit(req_madv
, &transparent_hugepage_flags
);
195 } else if (!memcmp("madvise", buf
,
196 min(sizeof("madvise")-1, count
))) {
197 clear_bit(enabled
, &transparent_hugepage_flags
);
198 set_bit(req_madv
, &transparent_hugepage_flags
);
199 } else if (!memcmp("never", buf
,
200 min(sizeof("never")-1, count
))) {
201 clear_bit(enabled
, &transparent_hugepage_flags
);
202 clear_bit(req_madv
, &transparent_hugepage_flags
);
209 static ssize_t
enabled_show(struct kobject
*kobj
,
210 struct kobj_attribute
*attr
, char *buf
)
212 return double_flag_show(kobj
, attr
, buf
,
213 TRANSPARENT_HUGEPAGE_FLAG
,
214 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
216 static ssize_t
enabled_store(struct kobject
*kobj
,
217 struct kobj_attribute
*attr
,
218 const char *buf
, size_t count
)
222 ret
= double_flag_store(kobj
, attr
, buf
, count
,
223 TRANSPARENT_HUGEPAGE_FLAG
,
224 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
227 int err
= start_khugepaged();
233 (test_bit(TRANSPARENT_HUGEPAGE_FLAG
,
234 &transparent_hugepage_flags
) ||
235 test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
236 &transparent_hugepage_flags
)))
237 set_recommended_min_free_kbytes();
241 static struct kobj_attribute enabled_attr
=
242 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
244 static ssize_t
single_flag_show(struct kobject
*kobj
,
245 struct kobj_attribute
*attr
, char *buf
,
246 enum transparent_hugepage_flag flag
)
248 return sprintf(buf
, "%d\n",
249 !!test_bit(flag
, &transparent_hugepage_flags
));
252 static ssize_t
single_flag_store(struct kobject
*kobj
,
253 struct kobj_attribute
*attr
,
254 const char *buf
, size_t count
,
255 enum transparent_hugepage_flag flag
)
260 ret
= kstrtoul(buf
, 10, &value
);
267 set_bit(flag
, &transparent_hugepage_flags
);
269 clear_bit(flag
, &transparent_hugepage_flags
);
275 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
276 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
277 * memory just to allocate one more hugepage.
279 static ssize_t
defrag_show(struct kobject
*kobj
,
280 struct kobj_attribute
*attr
, char *buf
)
282 return double_flag_show(kobj
, attr
, buf
,
283 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
284 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
286 static ssize_t
defrag_store(struct kobject
*kobj
,
287 struct kobj_attribute
*attr
,
288 const char *buf
, size_t count
)
290 return double_flag_store(kobj
, attr
, buf
, count
,
291 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
292 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
294 static struct kobj_attribute defrag_attr
=
295 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
297 #ifdef CONFIG_DEBUG_VM
298 static ssize_t
debug_cow_show(struct kobject
*kobj
,
299 struct kobj_attribute
*attr
, char *buf
)
301 return single_flag_show(kobj
, attr
, buf
,
302 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
304 static ssize_t
debug_cow_store(struct kobject
*kobj
,
305 struct kobj_attribute
*attr
,
306 const char *buf
, size_t count
)
308 return single_flag_store(kobj
, attr
, buf
, count
,
309 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
311 static struct kobj_attribute debug_cow_attr
=
312 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
313 #endif /* CONFIG_DEBUG_VM */
315 static struct attribute
*hugepage_attr
[] = {
318 #ifdef CONFIG_DEBUG_VM
319 &debug_cow_attr
.attr
,
324 static struct attribute_group hugepage_attr_group
= {
325 .attrs
= hugepage_attr
,
328 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
329 struct kobj_attribute
*attr
,
332 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
335 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
336 struct kobj_attribute
*attr
,
337 const char *buf
, size_t count
)
342 err
= strict_strtoul(buf
, 10, &msecs
);
343 if (err
|| msecs
> UINT_MAX
)
346 khugepaged_scan_sleep_millisecs
= msecs
;
347 wake_up_interruptible(&khugepaged_wait
);
351 static struct kobj_attribute scan_sleep_millisecs_attr
=
352 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
353 scan_sleep_millisecs_store
);
355 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
356 struct kobj_attribute
*attr
,
359 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
362 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
363 struct kobj_attribute
*attr
,
364 const char *buf
, size_t count
)
369 err
= strict_strtoul(buf
, 10, &msecs
);
370 if (err
|| msecs
> UINT_MAX
)
373 khugepaged_alloc_sleep_millisecs
= msecs
;
374 wake_up_interruptible(&khugepaged_wait
);
378 static struct kobj_attribute alloc_sleep_millisecs_attr
=
379 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
380 alloc_sleep_millisecs_store
);
382 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
383 struct kobj_attribute
*attr
,
386 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
388 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
389 struct kobj_attribute
*attr
,
390 const char *buf
, size_t count
)
395 err
= strict_strtoul(buf
, 10, &pages
);
396 if (err
|| !pages
|| pages
> UINT_MAX
)
399 khugepaged_pages_to_scan
= pages
;
403 static struct kobj_attribute pages_to_scan_attr
=
404 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
405 pages_to_scan_store
);
407 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
408 struct kobj_attribute
*attr
,
411 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
413 static struct kobj_attribute pages_collapsed_attr
=
414 __ATTR_RO(pages_collapsed
);
416 static ssize_t
full_scans_show(struct kobject
*kobj
,
417 struct kobj_attribute
*attr
,
420 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
422 static struct kobj_attribute full_scans_attr
=
423 __ATTR_RO(full_scans
);
425 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
426 struct kobj_attribute
*attr
, char *buf
)
428 return single_flag_show(kobj
, attr
, buf
,
429 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
431 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
432 struct kobj_attribute
*attr
,
433 const char *buf
, size_t count
)
435 return single_flag_store(kobj
, attr
, buf
, count
,
436 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
438 static struct kobj_attribute khugepaged_defrag_attr
=
439 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
440 khugepaged_defrag_store
);
443 * max_ptes_none controls if khugepaged should collapse hugepages over
444 * any unmapped ptes in turn potentially increasing the memory
445 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
446 * reduce the available free memory in the system as it
447 * runs. Increasing max_ptes_none will instead potentially reduce the
448 * free memory in the system during the khugepaged scan.
450 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
451 struct kobj_attribute
*attr
,
454 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
456 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
457 struct kobj_attribute
*attr
,
458 const char *buf
, size_t count
)
461 unsigned long max_ptes_none
;
463 err
= strict_strtoul(buf
, 10, &max_ptes_none
);
464 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
467 khugepaged_max_ptes_none
= max_ptes_none
;
471 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
472 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
473 khugepaged_max_ptes_none_store
);
475 static struct attribute
*khugepaged_attr
[] = {
476 &khugepaged_defrag_attr
.attr
,
477 &khugepaged_max_ptes_none_attr
.attr
,
478 &pages_to_scan_attr
.attr
,
479 &pages_collapsed_attr
.attr
,
480 &full_scans_attr
.attr
,
481 &scan_sleep_millisecs_attr
.attr
,
482 &alloc_sleep_millisecs_attr
.attr
,
486 static struct attribute_group khugepaged_attr_group
= {
487 .attrs
= khugepaged_attr
,
488 .name
= "khugepaged",
491 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
495 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
496 if (unlikely(!*hugepage_kobj
)) {
497 printk(KERN_ERR
"hugepage: failed kobject create\n");
501 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
503 printk(KERN_ERR
"hugepage: failed register hugeage group\n");
507 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
509 printk(KERN_ERR
"hugepage: failed register hugeage group\n");
510 goto remove_hp_group
;
516 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
518 kobject_put(*hugepage_kobj
);
522 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
524 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
525 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
526 kobject_put(hugepage_kobj
);
529 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
534 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
537 #endif /* CONFIG_SYSFS */
539 static int __init
hugepage_init(void)
542 struct kobject
*hugepage_kobj
;
544 if (!has_transparent_hugepage()) {
545 transparent_hugepage_flags
= 0;
549 err
= hugepage_init_sysfs(&hugepage_kobj
);
553 err
= khugepaged_slab_init();
557 err
= mm_slots_hash_init();
559 khugepaged_slab_free();
564 * By default disable transparent hugepages on smaller systems,
565 * where the extra memory used could hurt more than TLB overhead
566 * is likely to save. The admin can still enable it through /sys.
568 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
)))
569 transparent_hugepage_flags
= 0;
573 set_recommended_min_free_kbytes();
577 hugepage_exit_sysfs(hugepage_kobj
);
580 module_init(hugepage_init
)
582 static int __init
setup_transparent_hugepage(char *str
)
587 if (!strcmp(str
, "always")) {
588 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
589 &transparent_hugepage_flags
);
590 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
591 &transparent_hugepage_flags
);
593 } else if (!strcmp(str
, "madvise")) {
594 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
595 &transparent_hugepage_flags
);
596 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
597 &transparent_hugepage_flags
);
599 } else if (!strcmp(str
, "never")) {
600 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
601 &transparent_hugepage_flags
);
602 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
603 &transparent_hugepage_flags
);
609 "transparent_hugepage= cannot parse, ignored\n");
612 __setup("transparent_hugepage=", setup_transparent_hugepage
);
614 static void prepare_pmd_huge_pte(pgtable_t pgtable
,
615 struct mm_struct
*mm
)
617 assert_spin_locked(&mm
->page_table_lock
);
620 if (!mm
->pmd_huge_pte
)
621 INIT_LIST_HEAD(&pgtable
->lru
);
623 list_add(&pgtable
->lru
, &mm
->pmd_huge_pte
->lru
);
624 mm
->pmd_huge_pte
= pgtable
;
627 static inline pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
629 if (likely(vma
->vm_flags
& VM_WRITE
))
630 pmd
= pmd_mkwrite(pmd
);
634 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
635 struct vm_area_struct
*vma
,
636 unsigned long haddr
, pmd_t
*pmd
,
642 VM_BUG_ON(!PageCompound(page
));
643 pgtable
= pte_alloc_one(mm
, haddr
);
644 if (unlikely(!pgtable
)) {
645 mem_cgroup_uncharge_page(page
);
650 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
651 __SetPageUptodate(page
);
653 spin_lock(&mm
->page_table_lock
);
654 if (unlikely(!pmd_none(*pmd
))) {
655 spin_unlock(&mm
->page_table_lock
);
656 mem_cgroup_uncharge_page(page
);
658 pte_free(mm
, pgtable
);
661 entry
= mk_pmd(page
, vma
->vm_page_prot
);
662 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
663 entry
= pmd_mkhuge(entry
);
665 * The spinlocking to take the lru_lock inside
666 * page_add_new_anon_rmap() acts as a full memory
667 * barrier to be sure clear_huge_page writes become
668 * visible after the set_pmd_at() write.
670 page_add_new_anon_rmap(page
, vma
, haddr
);
671 set_pmd_at(mm
, haddr
, pmd
, entry
);
672 prepare_pmd_huge_pte(pgtable
, mm
);
673 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
675 spin_unlock(&mm
->page_table_lock
);
681 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
683 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_WAIT
)) | extra_gfp
;
686 static inline struct page
*alloc_hugepage_vma(int defrag
,
687 struct vm_area_struct
*vma
,
688 unsigned long haddr
, int nd
,
691 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag
, extra_gfp
),
692 HPAGE_PMD_ORDER
, vma
, haddr
, nd
);
696 static inline struct page
*alloc_hugepage(int defrag
)
698 return alloc_pages(alloc_hugepage_gfpmask(defrag
, 0),
703 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
704 unsigned long address
, pmd_t
*pmd
,
708 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
711 if (haddr
>= vma
->vm_start
&& haddr
+ HPAGE_PMD_SIZE
<= vma
->vm_end
) {
712 if (unlikely(anon_vma_prepare(vma
)))
714 if (unlikely(khugepaged_enter(vma
)))
716 page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
717 vma
, haddr
, numa_node_id(), 0);
718 if (unlikely(!page
)) {
719 count_vm_event(THP_FAULT_FALLBACK
);
722 count_vm_event(THP_FAULT_ALLOC
);
723 if (unlikely(mem_cgroup_newpage_charge(page
, mm
, GFP_KERNEL
))) {
728 return __do_huge_pmd_anonymous_page(mm
, vma
, haddr
, pmd
, page
);
732 * Use __pte_alloc instead of pte_alloc_map, because we can't
733 * run pte_offset_map on the pmd, if an huge pmd could
734 * materialize from under us from a different thread.
736 if (unlikely(__pte_alloc(mm
, vma
, pmd
, address
)))
738 /* if an huge pmd materialized from under us just retry later */
739 if (unlikely(pmd_trans_huge(*pmd
)))
742 * A regular pmd is established and it can't morph into a huge pmd
743 * from under us anymore at this point because we hold the mmap_sem
744 * read mode and khugepaged takes it in write mode. So now it's
745 * safe to run pte_offset_map().
747 pte
= pte_offset_map(pmd
, address
);
748 return handle_pte_fault(mm
, vma
, address
, pte
, pmd
, flags
);
751 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
752 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
753 struct vm_area_struct
*vma
)
755 struct page
*src_page
;
761 pgtable
= pte_alloc_one(dst_mm
, addr
);
762 if (unlikely(!pgtable
))
765 spin_lock(&dst_mm
->page_table_lock
);
766 spin_lock_nested(&src_mm
->page_table_lock
, SINGLE_DEPTH_NESTING
);
770 if (unlikely(!pmd_trans_huge(pmd
))) {
771 pte_free(dst_mm
, pgtable
);
774 if (unlikely(pmd_trans_splitting(pmd
))) {
775 /* split huge page running from under us */
776 spin_unlock(&src_mm
->page_table_lock
);
777 spin_unlock(&dst_mm
->page_table_lock
);
778 pte_free(dst_mm
, pgtable
);
780 wait_split_huge_page(vma
->anon_vma
, src_pmd
); /* src_vma */
783 src_page
= pmd_page(pmd
);
784 VM_BUG_ON(!PageHead(src_page
));
786 page_dup_rmap(src_page
);
787 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
789 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
790 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
791 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
792 prepare_pmd_huge_pte(pgtable
, dst_mm
);
797 spin_unlock(&src_mm
->page_table_lock
);
798 spin_unlock(&dst_mm
->page_table_lock
);
803 /* no "address" argument so destroys page coloring of some arch */
804 pgtable_t
get_pmd_huge_pte(struct mm_struct
*mm
)
808 assert_spin_locked(&mm
->page_table_lock
);
811 pgtable
= mm
->pmd_huge_pte
;
812 if (list_empty(&pgtable
->lru
))
813 mm
->pmd_huge_pte
= NULL
;
815 mm
->pmd_huge_pte
= list_entry(pgtable
->lru
.next
,
817 list_del(&pgtable
->lru
);
822 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
823 struct vm_area_struct
*vma
,
824 unsigned long address
,
825 pmd_t
*pmd
, pmd_t orig_pmd
,
834 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
836 if (unlikely(!pages
)) {
841 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
842 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
844 vma
, address
, page_to_nid(page
));
845 if (unlikely(!pages
[i
] ||
846 mem_cgroup_newpage_charge(pages
[i
], mm
,
850 mem_cgroup_uncharge_start();
852 mem_cgroup_uncharge_page(pages
[i
]);
855 mem_cgroup_uncharge_end();
862 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
863 copy_user_highpage(pages
[i
], page
+ i
,
864 haddr
+ PAGE_SIZE
* i
, vma
);
865 __SetPageUptodate(pages
[i
]);
869 spin_lock(&mm
->page_table_lock
);
870 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
872 VM_BUG_ON(!PageHead(page
));
874 pmdp_clear_flush_notify(vma
, haddr
, pmd
);
875 /* leave pmd empty until pte is filled */
877 pgtable
= get_pmd_huge_pte(mm
);
878 pmd_populate(mm
, &_pmd
, pgtable
);
880 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
882 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
883 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
884 page_add_new_anon_rmap(pages
[i
], vma
, haddr
);
885 pte
= pte_offset_map(&_pmd
, haddr
);
886 VM_BUG_ON(!pte_none(*pte
));
887 set_pte_at(mm
, haddr
, pte
, entry
);
892 smp_wmb(); /* make pte visible before pmd */
893 pmd_populate(mm
, pmd
, pgtable
);
894 page_remove_rmap(page
);
895 spin_unlock(&mm
->page_table_lock
);
897 ret
|= VM_FAULT_WRITE
;
904 spin_unlock(&mm
->page_table_lock
);
905 mem_cgroup_uncharge_start();
906 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
907 mem_cgroup_uncharge_page(pages
[i
]);
910 mem_cgroup_uncharge_end();
915 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
916 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
919 struct page
*page
, *new_page
;
922 VM_BUG_ON(!vma
->anon_vma
);
923 spin_lock(&mm
->page_table_lock
);
924 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
927 page
= pmd_page(orig_pmd
);
928 VM_BUG_ON(!PageCompound(page
) || !PageHead(page
));
929 haddr
= address
& HPAGE_PMD_MASK
;
930 if (page_mapcount(page
) == 1) {
932 entry
= pmd_mkyoung(orig_pmd
);
933 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
934 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
935 update_mmu_cache(vma
, address
, entry
);
936 ret
|= VM_FAULT_WRITE
;
940 spin_unlock(&mm
->page_table_lock
);
942 if (transparent_hugepage_enabled(vma
) &&
943 !transparent_hugepage_debug_cow())
944 new_page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
945 vma
, haddr
, numa_node_id(), 0);
949 if (unlikely(!new_page
)) {
950 count_vm_event(THP_FAULT_FALLBACK
);
951 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
952 pmd
, orig_pmd
, page
, haddr
);
956 count_vm_event(THP_FAULT_ALLOC
);
958 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
))) {
965 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
966 __SetPageUptodate(new_page
);
968 spin_lock(&mm
->page_table_lock
);
970 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
971 mem_cgroup_uncharge_page(new_page
);
975 VM_BUG_ON(!PageHead(page
));
976 entry
= mk_pmd(new_page
, vma
->vm_page_prot
);
977 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
978 entry
= pmd_mkhuge(entry
);
979 pmdp_clear_flush_notify(vma
, haddr
, pmd
);
980 page_add_new_anon_rmap(new_page
, vma
, haddr
);
981 set_pmd_at(mm
, haddr
, pmd
, entry
);
982 update_mmu_cache(vma
, address
, entry
);
983 page_remove_rmap(page
);
985 ret
|= VM_FAULT_WRITE
;
988 spin_unlock(&mm
->page_table_lock
);
993 struct page
*follow_trans_huge_pmd(struct mm_struct
*mm
,
998 struct page
*page
= NULL
;
1000 assert_spin_locked(&mm
->page_table_lock
);
1002 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1005 page
= pmd_page(*pmd
);
1006 VM_BUG_ON(!PageHead(page
));
1007 if (flags
& FOLL_TOUCH
) {
1010 * We should set the dirty bit only for FOLL_WRITE but
1011 * for now the dirty bit in the pmd is meaningless.
1012 * And if the dirty bit will become meaningful and
1013 * we'll only set it with FOLL_WRITE, an atomic
1014 * set_bit will be required on the pmd to set the
1015 * young bit, instead of the current set_pmd_at.
1017 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1018 set_pmd_at(mm
, addr
& HPAGE_PMD_MASK
, pmd
, _pmd
);
1020 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1021 VM_BUG_ON(!PageCompound(page
));
1022 if (flags
& FOLL_GET
)
1023 get_page_foll(page
);
1029 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1030 pmd_t
*pmd
, unsigned long addr
)
1034 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1037 pgtable
= get_pmd_huge_pte(tlb
->mm
);
1038 page
= pmd_page(*pmd
);
1040 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1041 page_remove_rmap(page
);
1042 VM_BUG_ON(page_mapcount(page
) < 0);
1043 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1044 VM_BUG_ON(!PageHead(page
));
1046 spin_unlock(&tlb
->mm
->page_table_lock
);
1047 tlb_remove_page(tlb
, page
);
1048 pte_free(tlb
->mm
, pgtable
);
1054 int mincore_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1055 unsigned long addr
, unsigned long end
,
1060 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1062 * All logical pages in the range are present
1063 * if backed by a huge page.
1065 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1066 memset(vec
, 1, (end
- addr
) >> PAGE_SHIFT
);
1073 int move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1074 unsigned long old_addr
,
1075 unsigned long new_addr
, unsigned long old_end
,
1076 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1081 struct mm_struct
*mm
= vma
->vm_mm
;
1083 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1084 (new_addr
& ~HPAGE_PMD_MASK
) ||
1085 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1086 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1090 * The destination pmd shouldn't be established, free_pgtables()
1091 * should have release it.
1093 if (WARN_ON(!pmd_none(*new_pmd
))) {
1094 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1098 ret
= __pmd_trans_huge_lock(old_pmd
, vma
);
1100 pmd
= pmdp_get_and_clear(mm
, old_addr
, old_pmd
);
1101 VM_BUG_ON(!pmd_none(*new_pmd
));
1102 set_pmd_at(mm
, new_addr
, new_pmd
, pmd
);
1103 spin_unlock(&mm
->page_table_lock
);
1109 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1110 unsigned long addr
, pgprot_t newprot
)
1112 struct mm_struct
*mm
= vma
->vm_mm
;
1115 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1117 entry
= pmdp_get_and_clear(mm
, addr
, pmd
);
1118 entry
= pmd_modify(entry
, newprot
);
1119 set_pmd_at(mm
, addr
, pmd
, entry
);
1120 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1128 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1129 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1131 * Note that if it returns 1, this routine returns without unlocking page
1132 * table locks. So callers must unlock them.
1134 int __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1136 spin_lock(&vma
->vm_mm
->page_table_lock
);
1137 if (likely(pmd_trans_huge(*pmd
))) {
1138 if (unlikely(pmd_trans_splitting(*pmd
))) {
1139 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1140 wait_split_huge_page(vma
->anon_vma
, pmd
);
1143 /* Thp mapped by 'pmd' is stable, so we can
1144 * handle it as it is. */
1148 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1152 pmd_t
*page_check_address_pmd(struct page
*page
,
1153 struct mm_struct
*mm
,
1154 unsigned long address
,
1155 enum page_check_address_pmd_flag flag
)
1159 pmd_t
*pmd
, *ret
= NULL
;
1161 if (address
& ~HPAGE_PMD_MASK
)
1164 pgd
= pgd_offset(mm
, address
);
1165 if (!pgd_present(*pgd
))
1168 pud
= pud_offset(pgd
, address
);
1169 if (!pud_present(*pud
))
1172 pmd
= pmd_offset(pud
, address
);
1175 if (pmd_page(*pmd
) != page
)
1178 * split_vma() may create temporary aliased mappings. There is
1179 * no risk as long as all huge pmd are found and have their
1180 * splitting bit set before __split_huge_page_refcount
1181 * runs. Finding the same huge pmd more than once during the
1182 * same rmap walk is not a problem.
1184 if (flag
== PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
&&
1185 pmd_trans_splitting(*pmd
))
1187 if (pmd_trans_huge(*pmd
)) {
1188 VM_BUG_ON(flag
== PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
&&
1189 !pmd_trans_splitting(*pmd
));
1196 static int __split_huge_page_splitting(struct page
*page
,
1197 struct vm_area_struct
*vma
,
1198 unsigned long address
)
1200 struct mm_struct
*mm
= vma
->vm_mm
;
1204 spin_lock(&mm
->page_table_lock
);
1205 pmd
= page_check_address_pmd(page
, mm
, address
,
1206 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
);
1209 * We can't temporarily set the pmd to null in order
1210 * to split it, the pmd must remain marked huge at all
1211 * times or the VM won't take the pmd_trans_huge paths
1212 * and it won't wait on the anon_vma->root->mutex to
1213 * serialize against split_huge_page*.
1215 pmdp_splitting_flush_notify(vma
, address
, pmd
);
1218 spin_unlock(&mm
->page_table_lock
);
1223 static void __split_huge_page_refcount(struct page
*page
)
1226 struct zone
*zone
= page_zone(page
);
1229 /* prevent PageLRU to go away from under us, and freeze lru stats */
1230 spin_lock_irq(&zone
->lru_lock
);
1231 compound_lock(page
);
1232 /* complete memcg works before add pages to LRU */
1233 mem_cgroup_split_huge_fixup(page
);
1235 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1236 struct page
*page_tail
= page
+ i
;
1238 /* tail_page->_mapcount cannot change */
1239 BUG_ON(page_mapcount(page_tail
) < 0);
1240 tail_count
+= page_mapcount(page_tail
);
1241 /* check for overflow */
1242 BUG_ON(tail_count
< 0);
1243 BUG_ON(atomic_read(&page_tail
->_count
) != 0);
1245 * tail_page->_count is zero and not changing from
1246 * under us. But get_page_unless_zero() may be running
1247 * from under us on the tail_page. If we used
1248 * atomic_set() below instead of atomic_add(), we
1249 * would then run atomic_set() concurrently with
1250 * get_page_unless_zero(), and atomic_set() is
1251 * implemented in C not using locked ops. spin_unlock
1252 * on x86 sometime uses locked ops because of PPro
1253 * errata 66, 92, so unless somebody can guarantee
1254 * atomic_set() here would be safe on all archs (and
1255 * not only on x86), it's safer to use atomic_add().
1257 atomic_add(page_mapcount(page
) + page_mapcount(page_tail
) + 1,
1258 &page_tail
->_count
);
1260 /* after clearing PageTail the gup refcount can be released */
1264 * retain hwpoison flag of the poisoned tail page:
1265 * fix for the unsuitable process killed on Guest Machine(KVM)
1266 * by the memory-failure.
1268 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
| __PG_HWPOISON
;
1269 page_tail
->flags
|= (page
->flags
&
1270 ((1L << PG_referenced
) |
1271 (1L << PG_swapbacked
) |
1272 (1L << PG_mlocked
) |
1273 (1L << PG_uptodate
)));
1274 page_tail
->flags
|= (1L << PG_dirty
);
1276 /* clear PageTail before overwriting first_page */
1280 * __split_huge_page_splitting() already set the
1281 * splitting bit in all pmd that could map this
1282 * hugepage, that will ensure no CPU can alter the
1283 * mapcount on the head page. The mapcount is only
1284 * accounted in the head page and it has to be
1285 * transferred to all tail pages in the below code. So
1286 * for this code to be safe, the split the mapcount
1287 * can't change. But that doesn't mean userland can't
1288 * keep changing and reading the page contents while
1289 * we transfer the mapcount, so the pmd splitting
1290 * status is achieved setting a reserved bit in the
1291 * pmd, not by clearing the present bit.
1293 page_tail
->_mapcount
= page
->_mapcount
;
1295 BUG_ON(page_tail
->mapping
);
1296 page_tail
->mapping
= page
->mapping
;
1298 page_tail
->index
= page
->index
+ i
;
1300 BUG_ON(!PageAnon(page_tail
));
1301 BUG_ON(!PageUptodate(page_tail
));
1302 BUG_ON(!PageDirty(page_tail
));
1303 BUG_ON(!PageSwapBacked(page_tail
));
1306 lru_add_page_tail(zone
, page
, page_tail
);
1308 atomic_sub(tail_count
, &page
->_count
);
1309 BUG_ON(atomic_read(&page
->_count
) <= 0);
1311 __dec_zone_page_state(page
, NR_ANON_TRANSPARENT_HUGEPAGES
);
1312 __mod_zone_page_state(zone
, NR_ANON_PAGES
, HPAGE_PMD_NR
);
1314 ClearPageCompound(page
);
1315 compound_unlock(page
);
1316 spin_unlock_irq(&zone
->lru_lock
);
1318 for (i
= 1; i
< HPAGE_PMD_NR
; i
++) {
1319 struct page
*page_tail
= page
+ i
;
1320 BUG_ON(page_count(page_tail
) <= 0);
1322 * Tail pages may be freed if there wasn't any mapping
1323 * like if add_to_swap() is running on a lru page that
1324 * had its mapping zapped. And freeing these pages
1325 * requires taking the lru_lock so we do the put_page
1326 * of the tail pages after the split is complete.
1328 put_page(page_tail
);
1332 * Only the head page (now become a regular page) is required
1333 * to be pinned by the caller.
1335 BUG_ON(page_count(page
) <= 0);
1338 static int __split_huge_page_map(struct page
*page
,
1339 struct vm_area_struct
*vma
,
1340 unsigned long address
)
1342 struct mm_struct
*mm
= vma
->vm_mm
;
1346 unsigned long haddr
;
1348 spin_lock(&mm
->page_table_lock
);
1349 pmd
= page_check_address_pmd(page
, mm
, address
,
1350 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
);
1352 pgtable
= get_pmd_huge_pte(mm
);
1353 pmd_populate(mm
, &_pmd
, pgtable
);
1355 for (i
= 0, haddr
= address
; i
< HPAGE_PMD_NR
;
1356 i
++, haddr
+= PAGE_SIZE
) {
1358 BUG_ON(PageCompound(page
+i
));
1359 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
1360 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1361 if (!pmd_write(*pmd
))
1362 entry
= pte_wrprotect(entry
);
1364 BUG_ON(page_mapcount(page
) != 1);
1365 if (!pmd_young(*pmd
))
1366 entry
= pte_mkold(entry
);
1367 pte
= pte_offset_map(&_pmd
, haddr
);
1368 BUG_ON(!pte_none(*pte
));
1369 set_pte_at(mm
, haddr
, pte
, entry
);
1373 smp_wmb(); /* make pte visible before pmd */
1375 * Up to this point the pmd is present and huge and
1376 * userland has the whole access to the hugepage
1377 * during the split (which happens in place). If we
1378 * overwrite the pmd with the not-huge version
1379 * pointing to the pte here (which of course we could
1380 * if all CPUs were bug free), userland could trigger
1381 * a small page size TLB miss on the small sized TLB
1382 * while the hugepage TLB entry is still established
1383 * in the huge TLB. Some CPU doesn't like that. See
1384 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1385 * Erratum 383 on page 93. Intel should be safe but is
1386 * also warns that it's only safe if the permission
1387 * and cache attributes of the two entries loaded in
1388 * the two TLB is identical (which should be the case
1389 * here). But it is generally safer to never allow
1390 * small and huge TLB entries for the same virtual
1391 * address to be loaded simultaneously. So instead of
1392 * doing "pmd_populate(); flush_tlb_range();" we first
1393 * mark the current pmd notpresent (atomically because
1394 * here the pmd_trans_huge and pmd_trans_splitting
1395 * must remain set at all times on the pmd until the
1396 * split is complete for this pmd), then we flush the
1397 * SMP TLB and finally we write the non-huge version
1398 * of the pmd entry with pmd_populate.
1400 set_pmd_at(mm
, address
, pmd
, pmd_mknotpresent(*pmd
));
1401 flush_tlb_range(vma
, address
, address
+ HPAGE_PMD_SIZE
);
1402 pmd_populate(mm
, pmd
, pgtable
);
1405 spin_unlock(&mm
->page_table_lock
);
1410 /* must be called with anon_vma->root->mutex hold */
1411 static void __split_huge_page(struct page
*page
,
1412 struct anon_vma
*anon_vma
)
1414 int mapcount
, mapcount2
;
1415 struct anon_vma_chain
*avc
;
1417 BUG_ON(!PageHead(page
));
1418 BUG_ON(PageTail(page
));
1421 list_for_each_entry(avc
, &anon_vma
->head
, same_anon_vma
) {
1422 struct vm_area_struct
*vma
= avc
->vma
;
1423 unsigned long addr
= vma_address(page
, vma
);
1424 BUG_ON(is_vma_temporary_stack(vma
));
1425 if (addr
== -EFAULT
)
1427 mapcount
+= __split_huge_page_splitting(page
, vma
, addr
);
1430 * It is critical that new vmas are added to the tail of the
1431 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1432 * and establishes a child pmd before
1433 * __split_huge_page_splitting() freezes the parent pmd (so if
1434 * we fail to prevent copy_huge_pmd() from running until the
1435 * whole __split_huge_page() is complete), we will still see
1436 * the newly established pmd of the child later during the
1437 * walk, to be able to set it as pmd_trans_splitting too.
1439 if (mapcount
!= page_mapcount(page
))
1440 printk(KERN_ERR
"mapcount %d page_mapcount %d\n",
1441 mapcount
, page_mapcount(page
));
1442 BUG_ON(mapcount
!= page_mapcount(page
));
1444 __split_huge_page_refcount(page
);
1447 list_for_each_entry(avc
, &anon_vma
->head
, same_anon_vma
) {
1448 struct vm_area_struct
*vma
= avc
->vma
;
1449 unsigned long addr
= vma_address(page
, vma
);
1450 BUG_ON(is_vma_temporary_stack(vma
));
1451 if (addr
== -EFAULT
)
1453 mapcount2
+= __split_huge_page_map(page
, vma
, addr
);
1455 if (mapcount
!= mapcount2
)
1456 printk(KERN_ERR
"mapcount %d mapcount2 %d page_mapcount %d\n",
1457 mapcount
, mapcount2
, page_mapcount(page
));
1458 BUG_ON(mapcount
!= mapcount2
);
1461 int split_huge_page(struct page
*page
)
1463 struct anon_vma
*anon_vma
;
1466 BUG_ON(!PageAnon(page
));
1467 anon_vma
= page_lock_anon_vma(page
);
1471 if (!PageCompound(page
))
1474 BUG_ON(!PageSwapBacked(page
));
1475 __split_huge_page(page
, anon_vma
);
1476 count_vm_event(THP_SPLIT
);
1478 BUG_ON(PageCompound(page
));
1480 page_unlock_anon_vma(anon_vma
);
1485 #define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
1486 VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1488 int hugepage_madvise(struct vm_area_struct
*vma
,
1489 unsigned long *vm_flags
, int advice
)
1494 * Be somewhat over-protective like KSM for now!
1496 if (*vm_flags
& (VM_HUGEPAGE
| VM_NO_THP
))
1498 *vm_flags
&= ~VM_NOHUGEPAGE
;
1499 *vm_flags
|= VM_HUGEPAGE
;
1501 * If the vma become good for khugepaged to scan,
1502 * register it here without waiting a page fault that
1503 * may not happen any time soon.
1505 if (unlikely(khugepaged_enter_vma_merge(vma
)))
1508 case MADV_NOHUGEPAGE
:
1510 * Be somewhat over-protective like KSM for now!
1512 if (*vm_flags
& (VM_NOHUGEPAGE
| VM_NO_THP
))
1514 *vm_flags
&= ~VM_HUGEPAGE
;
1515 *vm_flags
|= VM_NOHUGEPAGE
;
1517 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1518 * this vma even if we leave the mm registered in khugepaged if
1519 * it got registered before VM_NOHUGEPAGE was set.
1527 static int __init
khugepaged_slab_init(void)
1529 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1530 sizeof(struct mm_slot
),
1531 __alignof__(struct mm_slot
), 0, NULL
);
1538 static void __init
khugepaged_slab_free(void)
1540 kmem_cache_destroy(mm_slot_cache
);
1541 mm_slot_cache
= NULL
;
1544 static inline struct mm_slot
*alloc_mm_slot(void)
1546 if (!mm_slot_cache
) /* initialization failed */
1548 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1551 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1553 kmem_cache_free(mm_slot_cache
, mm_slot
);
1556 static int __init
mm_slots_hash_init(void)
1558 mm_slots_hash
= kzalloc(MM_SLOTS_HASH_HEADS
* sizeof(struct hlist_head
),
1566 static void __init
mm_slots_hash_free(void)
1568 kfree(mm_slots_hash
);
1569 mm_slots_hash
= NULL
;
1573 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1575 struct mm_slot
*mm_slot
;
1576 struct hlist_head
*bucket
;
1577 struct hlist_node
*node
;
1579 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1580 % MM_SLOTS_HASH_HEADS
];
1581 hlist_for_each_entry(mm_slot
, node
, bucket
, hash
) {
1582 if (mm
== mm_slot
->mm
)
1588 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1589 struct mm_slot
*mm_slot
)
1591 struct hlist_head
*bucket
;
1593 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1594 % MM_SLOTS_HASH_HEADS
];
1596 hlist_add_head(&mm_slot
->hash
, bucket
);
1599 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1601 return atomic_read(&mm
->mm_users
) == 0;
1604 int __khugepaged_enter(struct mm_struct
*mm
)
1606 struct mm_slot
*mm_slot
;
1609 mm_slot
= alloc_mm_slot();
1613 /* __khugepaged_exit() must not run from under us */
1614 VM_BUG_ON(khugepaged_test_exit(mm
));
1615 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1616 free_mm_slot(mm_slot
);
1620 spin_lock(&khugepaged_mm_lock
);
1621 insert_to_mm_slots_hash(mm
, mm_slot
);
1623 * Insert just behind the scanning cursor, to let the area settle
1626 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
1627 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
1628 spin_unlock(&khugepaged_mm_lock
);
1630 atomic_inc(&mm
->mm_count
);
1632 wake_up_interruptible(&khugepaged_wait
);
1637 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
)
1639 unsigned long hstart
, hend
;
1642 * Not yet faulted in so we will register later in the
1643 * page fault if needed.
1647 /* khugepaged not yet working on file or special mappings */
1650 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1651 * true too, verify it here.
1653 VM_BUG_ON(is_linear_pfn_mapping(vma
) || vma
->vm_flags
& VM_NO_THP
);
1654 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1655 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1657 return khugepaged_enter(vma
);
1661 void __khugepaged_exit(struct mm_struct
*mm
)
1663 struct mm_slot
*mm_slot
;
1666 spin_lock(&khugepaged_mm_lock
);
1667 mm_slot
= get_mm_slot(mm
);
1668 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
1669 hlist_del(&mm_slot
->hash
);
1670 list_del(&mm_slot
->mm_node
);
1673 spin_unlock(&khugepaged_mm_lock
);
1676 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
1677 free_mm_slot(mm_slot
);
1679 } else if (mm_slot
) {
1681 * This is required to serialize against
1682 * khugepaged_test_exit() (which is guaranteed to run
1683 * under mmap sem read mode). Stop here (after we
1684 * return all pagetables will be destroyed) until
1685 * khugepaged has finished working on the pagetables
1686 * under the mmap_sem.
1688 down_write(&mm
->mmap_sem
);
1689 up_write(&mm
->mmap_sem
);
1693 static void release_pte_page(struct page
*page
)
1695 /* 0 stands for page_is_file_cache(page) == false */
1696 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1698 putback_lru_page(page
);
1701 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
1703 while (--_pte
>= pte
) {
1704 pte_t pteval
= *_pte
;
1705 if (!pte_none(pteval
))
1706 release_pte_page(pte_page(pteval
));
1710 static void release_all_pte_pages(pte_t
*pte
)
1712 release_pte_pages(pte
, pte
+ HPAGE_PMD_NR
);
1715 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
1716 unsigned long address
,
1721 int referenced
= 0, isolated
= 0, none
= 0;
1722 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
1723 _pte
++, address
+= PAGE_SIZE
) {
1724 pte_t pteval
= *_pte
;
1725 if (pte_none(pteval
)) {
1726 if (++none
<= khugepaged_max_ptes_none
)
1729 release_pte_pages(pte
, _pte
);
1733 if (!pte_present(pteval
) || !pte_write(pteval
)) {
1734 release_pte_pages(pte
, _pte
);
1737 page
= vm_normal_page(vma
, address
, pteval
);
1738 if (unlikely(!page
)) {
1739 release_pte_pages(pte
, _pte
);
1742 VM_BUG_ON(PageCompound(page
));
1743 BUG_ON(!PageAnon(page
));
1744 VM_BUG_ON(!PageSwapBacked(page
));
1746 /* cannot use mapcount: can't collapse if there's a gup pin */
1747 if (page_count(page
) != 1) {
1748 release_pte_pages(pte
, _pte
);
1752 * We can do it before isolate_lru_page because the
1753 * page can't be freed from under us. NOTE: PG_lock
1754 * is needed to serialize against split_huge_page
1755 * when invoked from the VM.
1757 if (!trylock_page(page
)) {
1758 release_pte_pages(pte
, _pte
);
1762 * Isolate the page to avoid collapsing an hugepage
1763 * currently in use by the VM.
1765 if (isolate_lru_page(page
)) {
1767 release_pte_pages(pte
, _pte
);
1770 /* 0 stands for page_is_file_cache(page) == false */
1771 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1772 VM_BUG_ON(!PageLocked(page
));
1773 VM_BUG_ON(PageLRU(page
));
1775 /* If there is no mapped pte young don't collapse the page */
1776 if (pte_young(pteval
) || PageReferenced(page
) ||
1777 mmu_notifier_test_young(vma
->vm_mm
, address
))
1780 if (unlikely(!referenced
))
1781 release_all_pte_pages(pte
);
1788 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
1789 struct vm_area_struct
*vma
,
1790 unsigned long address
,
1794 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
1795 pte_t pteval
= *_pte
;
1796 struct page
*src_page
;
1798 if (pte_none(pteval
)) {
1799 clear_user_highpage(page
, address
);
1800 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
1802 src_page
= pte_page(pteval
);
1803 copy_user_highpage(page
, src_page
, address
, vma
);
1804 VM_BUG_ON(page_mapcount(src_page
) != 1);
1805 VM_BUG_ON(page_count(src_page
) != 2);
1806 release_pte_page(src_page
);
1808 * ptl mostly unnecessary, but preempt has to
1809 * be disabled to update the per-cpu stats
1810 * inside page_remove_rmap().
1814 * paravirt calls inside pte_clear here are
1817 pte_clear(vma
->vm_mm
, address
, _pte
);
1818 page_remove_rmap(src_page
);
1820 free_page_and_swap_cache(src_page
);
1823 address
+= PAGE_SIZE
;
1828 static void collapse_huge_page(struct mm_struct
*mm
,
1829 unsigned long address
,
1830 struct page
**hpage
,
1831 struct vm_area_struct
*vma
,
1839 struct page
*new_page
;
1842 unsigned long hstart
, hend
;
1844 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
1846 up_read(&mm
->mmap_sem
);
1852 * Allocate the page while the vma is still valid and under
1853 * the mmap_sem read mode so there is no memory allocation
1854 * later when we take the mmap_sem in write mode. This is more
1855 * friendly behavior (OTOH it may actually hide bugs) to
1856 * filesystems in userland with daemons allocating memory in
1857 * the userland I/O paths. Allocating memory with the
1858 * mmap_sem in read mode is good idea also to allow greater
1861 new_page
= alloc_hugepage_vma(khugepaged_defrag(), vma
, address
,
1862 node
, __GFP_OTHER_NODE
);
1865 * After allocating the hugepage, release the mmap_sem read lock in
1866 * preparation for taking it in write mode.
1868 up_read(&mm
->mmap_sem
);
1869 if (unlikely(!new_page
)) {
1870 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
1871 *hpage
= ERR_PTR(-ENOMEM
);
1876 count_vm_event(THP_COLLAPSE_ALLOC
);
1877 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
))) {
1885 * Prevent all access to pagetables with the exception of
1886 * gup_fast later hanlded by the ptep_clear_flush and the VM
1887 * handled by the anon_vma lock + PG_lock.
1889 down_write(&mm
->mmap_sem
);
1890 if (unlikely(khugepaged_test_exit(mm
)))
1893 vma
= find_vma(mm
, address
);
1894 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1895 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1896 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
1899 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
1900 (vma
->vm_flags
& VM_NOHUGEPAGE
))
1903 if (!vma
->anon_vma
|| vma
->vm_ops
)
1905 if (is_vma_temporary_stack(vma
))
1908 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1909 * true too, verify it here.
1911 VM_BUG_ON(is_linear_pfn_mapping(vma
) || vma
->vm_flags
& VM_NO_THP
);
1913 pgd
= pgd_offset(mm
, address
);
1914 if (!pgd_present(*pgd
))
1917 pud
= pud_offset(pgd
, address
);
1918 if (!pud_present(*pud
))
1921 pmd
= pmd_offset(pud
, address
);
1922 /* pmd can't go away or become huge under us */
1923 if (!pmd_present(*pmd
) || pmd_trans_huge(*pmd
))
1926 anon_vma_lock(vma
->anon_vma
);
1928 pte
= pte_offset_map(pmd
, address
);
1929 ptl
= pte_lockptr(mm
, pmd
);
1931 spin_lock(&mm
->page_table_lock
); /* probably unnecessary */
1933 * After this gup_fast can't run anymore. This also removes
1934 * any huge TLB entry from the CPU so we won't allow
1935 * huge and small TLB entries for the same virtual address
1936 * to avoid the risk of CPU bugs in that area.
1938 _pmd
= pmdp_clear_flush_notify(vma
, address
, pmd
);
1939 spin_unlock(&mm
->page_table_lock
);
1942 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
1945 if (unlikely(!isolated
)) {
1947 spin_lock(&mm
->page_table_lock
);
1948 BUG_ON(!pmd_none(*pmd
));
1949 set_pmd_at(mm
, address
, pmd
, _pmd
);
1950 spin_unlock(&mm
->page_table_lock
);
1951 anon_vma_unlock(vma
->anon_vma
);
1956 * All pages are isolated and locked so anon_vma rmap
1957 * can't run anymore.
1959 anon_vma_unlock(vma
->anon_vma
);
1961 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, ptl
);
1963 __SetPageUptodate(new_page
);
1964 pgtable
= pmd_pgtable(_pmd
);
1965 VM_BUG_ON(page_count(pgtable
) != 1);
1966 VM_BUG_ON(page_mapcount(pgtable
) != 0);
1968 _pmd
= mk_pmd(new_page
, vma
->vm_page_prot
);
1969 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
1970 _pmd
= pmd_mkhuge(_pmd
);
1973 * spin_lock() below is not the equivalent of smp_wmb(), so
1974 * this is needed to avoid the copy_huge_page writes to become
1975 * visible after the set_pmd_at() write.
1979 spin_lock(&mm
->page_table_lock
);
1980 BUG_ON(!pmd_none(*pmd
));
1981 page_add_new_anon_rmap(new_page
, vma
, address
);
1982 set_pmd_at(mm
, address
, pmd
, _pmd
);
1983 update_mmu_cache(vma
, address
, _pmd
);
1984 prepare_pmd_huge_pte(pgtable
, mm
);
1985 spin_unlock(&mm
->page_table_lock
);
1990 khugepaged_pages_collapsed
++;
1992 up_write(&mm
->mmap_sem
);
1996 mem_cgroup_uncharge_page(new_page
);
2003 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2004 struct vm_area_struct
*vma
,
2005 unsigned long address
,
2006 struct page
**hpage
)
2012 int ret
= 0, referenced
= 0, none
= 0;
2014 unsigned long _address
;
2018 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2020 pgd
= pgd_offset(mm
, address
);
2021 if (!pgd_present(*pgd
))
2024 pud
= pud_offset(pgd
, address
);
2025 if (!pud_present(*pud
))
2028 pmd
= pmd_offset(pud
, address
);
2029 if (!pmd_present(*pmd
) || pmd_trans_huge(*pmd
))
2032 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2033 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2034 _pte
++, _address
+= PAGE_SIZE
) {
2035 pte_t pteval
= *_pte
;
2036 if (pte_none(pteval
)) {
2037 if (++none
<= khugepaged_max_ptes_none
)
2042 if (!pte_present(pteval
) || !pte_write(pteval
))
2044 page
= vm_normal_page(vma
, _address
, pteval
);
2045 if (unlikely(!page
))
2048 * Chose the node of the first page. This could
2049 * be more sophisticated and look at more pages,
2050 * but isn't for now.
2053 node
= page_to_nid(page
);
2054 VM_BUG_ON(PageCompound(page
));
2055 if (!PageLRU(page
) || PageLocked(page
) || !PageAnon(page
))
2057 /* cannot use mapcount: can't collapse if there's a gup pin */
2058 if (page_count(page
) != 1)
2060 if (pte_young(pteval
) || PageReferenced(page
) ||
2061 mmu_notifier_test_young(vma
->vm_mm
, address
))
2067 pte_unmap_unlock(pte
, ptl
);
2069 /* collapse_huge_page will return with the mmap_sem released */
2070 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2075 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2077 struct mm_struct
*mm
= mm_slot
->mm
;
2079 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2081 if (khugepaged_test_exit(mm
)) {
2083 hlist_del(&mm_slot
->hash
);
2084 list_del(&mm_slot
->mm_node
);
2087 * Not strictly needed because the mm exited already.
2089 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2092 /* khugepaged_mm_lock actually not necessary for the below */
2093 free_mm_slot(mm_slot
);
2098 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2099 struct page
**hpage
)
2100 __releases(&khugepaged_mm_lock
)
2101 __acquires(&khugepaged_mm_lock
)
2103 struct mm_slot
*mm_slot
;
2104 struct mm_struct
*mm
;
2105 struct vm_area_struct
*vma
;
2109 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2111 if (khugepaged_scan
.mm_slot
)
2112 mm_slot
= khugepaged_scan
.mm_slot
;
2114 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2115 struct mm_slot
, mm_node
);
2116 khugepaged_scan
.address
= 0;
2117 khugepaged_scan
.mm_slot
= mm_slot
;
2119 spin_unlock(&khugepaged_mm_lock
);
2122 down_read(&mm
->mmap_sem
);
2123 if (unlikely(khugepaged_test_exit(mm
)))
2126 vma
= find_vma(mm
, khugepaged_scan
.address
);
2129 for (; vma
; vma
= vma
->vm_next
) {
2130 unsigned long hstart
, hend
;
2133 if (unlikely(khugepaged_test_exit(mm
))) {
2138 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) &&
2139 !khugepaged_always()) ||
2140 (vma
->vm_flags
& VM_NOHUGEPAGE
)) {
2145 if (!vma
->anon_vma
|| vma
->vm_ops
)
2147 if (is_vma_temporary_stack(vma
))
2150 * If is_pfn_mapping() is true is_learn_pfn_mapping()
2151 * must be true too, verify it here.
2153 VM_BUG_ON(is_linear_pfn_mapping(vma
) ||
2154 vma
->vm_flags
& VM_NO_THP
);
2156 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2157 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2160 if (khugepaged_scan
.address
> hend
)
2162 if (khugepaged_scan
.address
< hstart
)
2163 khugepaged_scan
.address
= hstart
;
2164 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2166 while (khugepaged_scan
.address
< hend
) {
2169 if (unlikely(khugepaged_test_exit(mm
)))
2170 goto breakouterloop
;
2172 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2173 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2175 ret
= khugepaged_scan_pmd(mm
, vma
,
2176 khugepaged_scan
.address
,
2178 /* move to next address */
2179 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2180 progress
+= HPAGE_PMD_NR
;
2182 /* we released mmap_sem so break loop */
2183 goto breakouterloop_mmap_sem
;
2184 if (progress
>= pages
)
2185 goto breakouterloop
;
2189 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2190 breakouterloop_mmap_sem
:
2192 spin_lock(&khugepaged_mm_lock
);
2193 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2195 * Release the current mm_slot if this mm is about to die, or
2196 * if we scanned all vmas of this mm.
2198 if (khugepaged_test_exit(mm
) || !vma
) {
2200 * Make sure that if mm_users is reaching zero while
2201 * khugepaged runs here, khugepaged_exit will find
2202 * mm_slot not pointing to the exiting mm.
2204 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2205 khugepaged_scan
.mm_slot
= list_entry(
2206 mm_slot
->mm_node
.next
,
2207 struct mm_slot
, mm_node
);
2208 khugepaged_scan
.address
= 0;
2210 khugepaged_scan
.mm_slot
= NULL
;
2211 khugepaged_full_scans
++;
2214 collect_mm_slot(mm_slot
);
2220 static int khugepaged_has_work(void)
2222 return !list_empty(&khugepaged_scan
.mm_head
) &&
2223 khugepaged_enabled();
2226 static int khugepaged_wait_event(void)
2228 return !list_empty(&khugepaged_scan
.mm_head
) ||
2229 !khugepaged_enabled();
2232 static void khugepaged_do_scan(struct page
**hpage
)
2234 unsigned int progress
= 0, pass_through_head
= 0;
2235 unsigned int pages
= khugepaged_pages_to_scan
;
2237 barrier(); /* write khugepaged_pages_to_scan to local stack */
2239 while (progress
< pages
) {
2244 *hpage
= alloc_hugepage(khugepaged_defrag());
2245 if (unlikely(!*hpage
)) {
2246 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2249 count_vm_event(THP_COLLAPSE_ALLOC
);
2256 if (unlikely(kthread_should_stop() || freezing(current
)))
2259 spin_lock(&khugepaged_mm_lock
);
2260 if (!khugepaged_scan
.mm_slot
)
2261 pass_through_head
++;
2262 if (khugepaged_has_work() &&
2263 pass_through_head
< 2)
2264 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2268 spin_unlock(&khugepaged_mm_lock
);
2272 static void khugepaged_alloc_sleep(void)
2274 wait_event_freezable_timeout(khugepaged_wait
, false,
2275 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2279 static struct page
*khugepaged_alloc_hugepage(void)
2284 hpage
= alloc_hugepage(khugepaged_defrag());
2286 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2287 khugepaged_alloc_sleep();
2289 count_vm_event(THP_COLLAPSE_ALLOC
);
2290 } while (unlikely(!hpage
) &&
2291 likely(khugepaged_enabled()));
2296 static void khugepaged_loop(void)
2303 while (likely(khugepaged_enabled())) {
2305 hpage
= khugepaged_alloc_hugepage();
2306 if (unlikely(!hpage
))
2309 if (IS_ERR(hpage
)) {
2310 khugepaged_alloc_sleep();
2315 khugepaged_do_scan(&hpage
);
2321 if (unlikely(kthread_should_stop()))
2323 if (khugepaged_has_work()) {
2324 if (!khugepaged_scan_sleep_millisecs
)
2326 wait_event_freezable_timeout(khugepaged_wait
, false,
2327 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2328 } else if (khugepaged_enabled())
2329 wait_event_freezable(khugepaged_wait
,
2330 khugepaged_wait_event());
2334 static int khugepaged(void *none
)
2336 struct mm_slot
*mm_slot
;
2339 set_user_nice(current
, 19);
2341 /* serialize with start_khugepaged() */
2342 mutex_lock(&khugepaged_mutex
);
2345 mutex_unlock(&khugepaged_mutex
);
2346 VM_BUG_ON(khugepaged_thread
!= current
);
2348 VM_BUG_ON(khugepaged_thread
!= current
);
2350 mutex_lock(&khugepaged_mutex
);
2351 if (!khugepaged_enabled())
2353 if (unlikely(kthread_should_stop()))
2357 spin_lock(&khugepaged_mm_lock
);
2358 mm_slot
= khugepaged_scan
.mm_slot
;
2359 khugepaged_scan
.mm_slot
= NULL
;
2361 collect_mm_slot(mm_slot
);
2362 spin_unlock(&khugepaged_mm_lock
);
2364 khugepaged_thread
= NULL
;
2365 mutex_unlock(&khugepaged_mutex
);
2370 void __split_huge_page_pmd(struct mm_struct
*mm
, pmd_t
*pmd
)
2374 spin_lock(&mm
->page_table_lock
);
2375 if (unlikely(!pmd_trans_huge(*pmd
))) {
2376 spin_unlock(&mm
->page_table_lock
);
2379 page
= pmd_page(*pmd
);
2380 VM_BUG_ON(!page_count(page
));
2382 spin_unlock(&mm
->page_table_lock
);
2384 split_huge_page(page
);
2387 BUG_ON(pmd_trans_huge(*pmd
));
2390 static void split_huge_page_address(struct mm_struct
*mm
,
2391 unsigned long address
)
2397 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
2399 pgd
= pgd_offset(mm
, address
);
2400 if (!pgd_present(*pgd
))
2403 pud
= pud_offset(pgd
, address
);
2404 if (!pud_present(*pud
))
2407 pmd
= pmd_offset(pud
, address
);
2408 if (!pmd_present(*pmd
))
2411 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2412 * materialize from under us.
2414 split_huge_page_pmd(mm
, pmd
);
2417 void __vma_adjust_trans_huge(struct vm_area_struct
*vma
,
2418 unsigned long start
,
2423 * If the new start address isn't hpage aligned and it could
2424 * previously contain an hugepage: check if we need to split
2427 if (start
& ~HPAGE_PMD_MASK
&&
2428 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2429 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2430 split_huge_page_address(vma
->vm_mm
, start
);
2433 * If the new end address isn't hpage aligned and it could
2434 * previously contain an hugepage: check if we need to split
2437 if (end
& ~HPAGE_PMD_MASK
&&
2438 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2439 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2440 split_huge_page_address(vma
->vm_mm
, end
);
2443 * If we're also updating the vma->vm_next->vm_start, if the new
2444 * vm_next->vm_start isn't page aligned and it could previously
2445 * contain an hugepage: check if we need to split an huge pmd.
2447 if (adjust_next
> 0) {
2448 struct vm_area_struct
*next
= vma
->vm_next
;
2449 unsigned long nstart
= next
->vm_start
;
2450 nstart
+= adjust_next
<< PAGE_SHIFT
;
2451 if (nstart
& ~HPAGE_PMD_MASK
&&
2452 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
2453 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
2454 split_huge_page_address(next
->vm_mm
, nstart
);