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>
20 #include <linux/pagemap.h>
22 #include <asm/pgalloc.h>
26 * By default transparent hugepage support is enabled for all mappings
27 * and khugepaged scans all mappings. Defrag is only invoked by
28 * khugepaged hugepage allocations and by page faults inside
29 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
32 unsigned long transparent_hugepage_flags __read_mostly
=
33 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
34 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
36 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
37 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
39 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
40 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
42 /* default scan 8*512 pte (or vmas) every 30 second */
43 static unsigned int khugepaged_pages_to_scan __read_mostly
= HPAGE_PMD_NR
*8;
44 static unsigned int khugepaged_pages_collapsed
;
45 static unsigned int khugepaged_full_scans
;
46 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
47 /* during fragmentation poll the hugepage allocator once every minute */
48 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
49 static struct task_struct
*khugepaged_thread __read_mostly
;
50 static DEFINE_MUTEX(khugepaged_mutex
);
51 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
52 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
54 * default collapse hugepages if there is at least one pte mapped like
55 * it would have happened if the vma was large enough during page
58 static unsigned int khugepaged_max_ptes_none __read_mostly
= HPAGE_PMD_NR
-1;
60 static int khugepaged(void *none
);
61 static int mm_slots_hash_init(void);
62 static int khugepaged_slab_init(void);
63 static void khugepaged_slab_free(void);
65 #define MM_SLOTS_HASH_HEADS 1024
66 static struct hlist_head
*mm_slots_hash __read_mostly
;
67 static struct kmem_cache
*mm_slot_cache __read_mostly
;
70 * struct mm_slot - hash lookup from mm to mm_slot
71 * @hash: hash collision list
72 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
73 * @mm: the mm that this information is valid for
76 struct hlist_node hash
;
77 struct list_head mm_node
;
82 * struct khugepaged_scan - cursor for scanning
83 * @mm_head: the head of the mm list to scan
84 * @mm_slot: the current mm_slot we are scanning
85 * @address: the next address inside that to be scanned
87 * There is only the one khugepaged_scan instance of this cursor structure.
89 struct khugepaged_scan
{
90 struct list_head mm_head
;
91 struct mm_slot
*mm_slot
;
92 unsigned long address
;
94 static struct khugepaged_scan khugepaged_scan
= {
95 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
99 static int set_recommended_min_free_kbytes(void)
103 unsigned long recommended_min
;
104 extern int min_free_kbytes
;
106 if (!khugepaged_enabled())
109 for_each_populated_zone(zone
)
112 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
113 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
116 * Make sure that on average at least two pageblocks are almost free
117 * of another type, one for a migratetype to fall back to and a
118 * second to avoid subsequent fallbacks of other types There are 3
119 * MIGRATE_TYPES we care about.
121 recommended_min
+= pageblock_nr_pages
* nr_zones
*
122 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
124 /* don't ever allow to reserve more than 5% of the lowmem */
125 recommended_min
= min(recommended_min
,
126 (unsigned long) nr_free_buffer_pages() / 20);
127 recommended_min
<<= (PAGE_SHIFT
-10);
129 if (recommended_min
> min_free_kbytes
)
130 min_free_kbytes
= recommended_min
;
131 setup_per_zone_wmarks();
134 late_initcall(set_recommended_min_free_kbytes
);
136 static int start_khugepaged(void)
139 if (khugepaged_enabled()) {
140 if (!khugepaged_thread
)
141 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
143 if (unlikely(IS_ERR(khugepaged_thread
))) {
145 "khugepaged: kthread_run(khugepaged) failed\n");
146 err
= PTR_ERR(khugepaged_thread
);
147 khugepaged_thread
= NULL
;
150 if (!list_empty(&khugepaged_scan
.mm_head
))
151 wake_up_interruptible(&khugepaged_wait
);
153 set_recommended_min_free_kbytes();
154 } else if (khugepaged_thread
) {
155 kthread_stop(khugepaged_thread
);
156 khugepaged_thread
= NULL
;
164 static ssize_t
double_flag_show(struct kobject
*kobj
,
165 struct kobj_attribute
*attr
, char *buf
,
166 enum transparent_hugepage_flag enabled
,
167 enum transparent_hugepage_flag req_madv
)
169 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
170 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
171 return sprintf(buf
, "[always] madvise never\n");
172 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
173 return sprintf(buf
, "always [madvise] never\n");
175 return sprintf(buf
, "always madvise [never]\n");
177 static ssize_t
double_flag_store(struct kobject
*kobj
,
178 struct kobj_attribute
*attr
,
179 const char *buf
, size_t count
,
180 enum transparent_hugepage_flag enabled
,
181 enum transparent_hugepage_flag req_madv
)
183 if (!memcmp("always", buf
,
184 min(sizeof("always")-1, count
))) {
185 set_bit(enabled
, &transparent_hugepage_flags
);
186 clear_bit(req_madv
, &transparent_hugepage_flags
);
187 } else if (!memcmp("madvise", buf
,
188 min(sizeof("madvise")-1, count
))) {
189 clear_bit(enabled
, &transparent_hugepage_flags
);
190 set_bit(req_madv
, &transparent_hugepage_flags
);
191 } else if (!memcmp("never", buf
,
192 min(sizeof("never")-1, count
))) {
193 clear_bit(enabled
, &transparent_hugepage_flags
);
194 clear_bit(req_madv
, &transparent_hugepage_flags
);
201 static ssize_t
enabled_show(struct kobject
*kobj
,
202 struct kobj_attribute
*attr
, char *buf
)
204 return double_flag_show(kobj
, attr
, buf
,
205 TRANSPARENT_HUGEPAGE_FLAG
,
206 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
208 static ssize_t
enabled_store(struct kobject
*kobj
,
209 struct kobj_attribute
*attr
,
210 const char *buf
, size_t count
)
214 ret
= double_flag_store(kobj
, attr
, buf
, count
,
215 TRANSPARENT_HUGEPAGE_FLAG
,
216 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
221 mutex_lock(&khugepaged_mutex
);
222 err
= start_khugepaged();
223 mutex_unlock(&khugepaged_mutex
);
231 static struct kobj_attribute enabled_attr
=
232 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
234 static ssize_t
single_flag_show(struct kobject
*kobj
,
235 struct kobj_attribute
*attr
, char *buf
,
236 enum transparent_hugepage_flag flag
)
238 return sprintf(buf
, "%d\n",
239 !!test_bit(flag
, &transparent_hugepage_flags
));
242 static ssize_t
single_flag_store(struct kobject
*kobj
,
243 struct kobj_attribute
*attr
,
244 const char *buf
, size_t count
,
245 enum transparent_hugepage_flag flag
)
250 ret
= kstrtoul(buf
, 10, &value
);
257 set_bit(flag
, &transparent_hugepage_flags
);
259 clear_bit(flag
, &transparent_hugepage_flags
);
265 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
266 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
267 * memory just to allocate one more hugepage.
269 static ssize_t
defrag_show(struct kobject
*kobj
,
270 struct kobj_attribute
*attr
, char *buf
)
272 return double_flag_show(kobj
, attr
, buf
,
273 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
274 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
276 static ssize_t
defrag_store(struct kobject
*kobj
,
277 struct kobj_attribute
*attr
,
278 const char *buf
, size_t count
)
280 return double_flag_store(kobj
, attr
, buf
, count
,
281 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
282 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
284 static struct kobj_attribute defrag_attr
=
285 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
287 #ifdef CONFIG_DEBUG_VM
288 static ssize_t
debug_cow_show(struct kobject
*kobj
,
289 struct kobj_attribute
*attr
, char *buf
)
291 return single_flag_show(kobj
, attr
, buf
,
292 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
294 static ssize_t
debug_cow_store(struct kobject
*kobj
,
295 struct kobj_attribute
*attr
,
296 const char *buf
, size_t count
)
298 return single_flag_store(kobj
, attr
, buf
, count
,
299 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
301 static struct kobj_attribute debug_cow_attr
=
302 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
303 #endif /* CONFIG_DEBUG_VM */
305 static struct attribute
*hugepage_attr
[] = {
308 #ifdef CONFIG_DEBUG_VM
309 &debug_cow_attr
.attr
,
314 static struct attribute_group hugepage_attr_group
= {
315 .attrs
= hugepage_attr
,
318 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
319 struct kobj_attribute
*attr
,
322 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
325 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
326 struct kobj_attribute
*attr
,
327 const char *buf
, size_t count
)
332 err
= strict_strtoul(buf
, 10, &msecs
);
333 if (err
|| msecs
> UINT_MAX
)
336 khugepaged_scan_sleep_millisecs
= msecs
;
337 wake_up_interruptible(&khugepaged_wait
);
341 static struct kobj_attribute scan_sleep_millisecs_attr
=
342 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
343 scan_sleep_millisecs_store
);
345 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
346 struct kobj_attribute
*attr
,
349 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
352 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
353 struct kobj_attribute
*attr
,
354 const char *buf
, size_t count
)
359 err
= strict_strtoul(buf
, 10, &msecs
);
360 if (err
|| msecs
> UINT_MAX
)
363 khugepaged_alloc_sleep_millisecs
= msecs
;
364 wake_up_interruptible(&khugepaged_wait
);
368 static struct kobj_attribute alloc_sleep_millisecs_attr
=
369 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
370 alloc_sleep_millisecs_store
);
372 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
373 struct kobj_attribute
*attr
,
376 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
378 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
379 struct kobj_attribute
*attr
,
380 const char *buf
, size_t count
)
385 err
= strict_strtoul(buf
, 10, &pages
);
386 if (err
|| !pages
|| pages
> UINT_MAX
)
389 khugepaged_pages_to_scan
= pages
;
393 static struct kobj_attribute pages_to_scan_attr
=
394 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
395 pages_to_scan_store
);
397 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
398 struct kobj_attribute
*attr
,
401 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
403 static struct kobj_attribute pages_collapsed_attr
=
404 __ATTR_RO(pages_collapsed
);
406 static ssize_t
full_scans_show(struct kobject
*kobj
,
407 struct kobj_attribute
*attr
,
410 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
412 static struct kobj_attribute full_scans_attr
=
413 __ATTR_RO(full_scans
);
415 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
416 struct kobj_attribute
*attr
, char *buf
)
418 return single_flag_show(kobj
, attr
, buf
,
419 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
421 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
422 struct kobj_attribute
*attr
,
423 const char *buf
, size_t count
)
425 return single_flag_store(kobj
, attr
, buf
, count
,
426 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
428 static struct kobj_attribute khugepaged_defrag_attr
=
429 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
430 khugepaged_defrag_store
);
433 * max_ptes_none controls if khugepaged should collapse hugepages over
434 * any unmapped ptes in turn potentially increasing the memory
435 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
436 * reduce the available free memory in the system as it
437 * runs. Increasing max_ptes_none will instead potentially reduce the
438 * free memory in the system during the khugepaged scan.
440 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
441 struct kobj_attribute
*attr
,
444 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
446 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
447 struct kobj_attribute
*attr
,
448 const char *buf
, size_t count
)
451 unsigned long max_ptes_none
;
453 err
= strict_strtoul(buf
, 10, &max_ptes_none
);
454 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
457 khugepaged_max_ptes_none
= max_ptes_none
;
461 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
462 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
463 khugepaged_max_ptes_none_store
);
465 static struct attribute
*khugepaged_attr
[] = {
466 &khugepaged_defrag_attr
.attr
,
467 &khugepaged_max_ptes_none_attr
.attr
,
468 &pages_to_scan_attr
.attr
,
469 &pages_collapsed_attr
.attr
,
470 &full_scans_attr
.attr
,
471 &scan_sleep_millisecs_attr
.attr
,
472 &alloc_sleep_millisecs_attr
.attr
,
476 static struct attribute_group khugepaged_attr_group
= {
477 .attrs
= khugepaged_attr
,
478 .name
= "khugepaged",
481 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
485 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
486 if (unlikely(!*hugepage_kobj
)) {
487 printk(KERN_ERR
"hugepage: failed kobject create\n");
491 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
493 printk(KERN_ERR
"hugepage: failed register hugeage group\n");
497 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
499 printk(KERN_ERR
"hugepage: failed register hugeage group\n");
500 goto remove_hp_group
;
506 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
508 kobject_put(*hugepage_kobj
);
512 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
514 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
515 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
516 kobject_put(hugepage_kobj
);
519 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
524 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
527 #endif /* CONFIG_SYSFS */
529 static int __init
hugepage_init(void)
532 struct kobject
*hugepage_kobj
;
534 if (!has_transparent_hugepage()) {
535 transparent_hugepage_flags
= 0;
539 err
= hugepage_init_sysfs(&hugepage_kobj
);
543 err
= khugepaged_slab_init();
547 err
= mm_slots_hash_init();
549 khugepaged_slab_free();
554 * By default disable transparent hugepages on smaller systems,
555 * where the extra memory used could hurt more than TLB overhead
556 * is likely to save. The admin can still enable it through /sys.
558 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
)))
559 transparent_hugepage_flags
= 0;
565 hugepage_exit_sysfs(hugepage_kobj
);
568 module_init(hugepage_init
)
570 static int __init
setup_transparent_hugepage(char *str
)
575 if (!strcmp(str
, "always")) {
576 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
577 &transparent_hugepage_flags
);
578 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
579 &transparent_hugepage_flags
);
581 } else if (!strcmp(str
, "madvise")) {
582 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
583 &transparent_hugepage_flags
);
584 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
585 &transparent_hugepage_flags
);
587 } else if (!strcmp(str
, "never")) {
588 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
589 &transparent_hugepage_flags
);
590 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
591 &transparent_hugepage_flags
);
597 "transparent_hugepage= cannot parse, ignored\n");
600 __setup("transparent_hugepage=", setup_transparent_hugepage
);
602 static inline pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
604 if (likely(vma
->vm_flags
& VM_WRITE
))
605 pmd
= pmd_mkwrite(pmd
);
609 static inline pmd_t
mk_huge_pmd(struct page
*page
, struct vm_area_struct
*vma
)
612 entry
= mk_pmd(page
, vma
->vm_page_prot
);
613 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
614 entry
= pmd_mkhuge(entry
);
618 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
619 struct vm_area_struct
*vma
,
620 unsigned long haddr
, pmd_t
*pmd
,
625 VM_BUG_ON(!PageCompound(page
));
626 pgtable
= pte_alloc_one(mm
, haddr
);
627 if (unlikely(!pgtable
))
630 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
631 __SetPageUptodate(page
);
633 spin_lock(&mm
->page_table_lock
);
634 if (unlikely(!pmd_none(*pmd
))) {
635 spin_unlock(&mm
->page_table_lock
);
636 mem_cgroup_uncharge_page(page
);
638 pte_free(mm
, pgtable
);
641 entry
= mk_huge_pmd(page
, vma
);
643 * The spinlocking to take the lru_lock inside
644 * page_add_new_anon_rmap() acts as a full memory
645 * barrier to be sure clear_huge_page writes become
646 * visible after the set_pmd_at() write.
648 page_add_new_anon_rmap(page
, vma
, haddr
);
649 set_pmd_at(mm
, haddr
, pmd
, entry
);
650 pgtable_trans_huge_deposit(mm
, pgtable
);
651 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
653 spin_unlock(&mm
->page_table_lock
);
659 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
661 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_WAIT
)) | extra_gfp
;
664 static inline struct page
*alloc_hugepage_vma(int defrag
,
665 struct vm_area_struct
*vma
,
666 unsigned long haddr
, int nd
,
669 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag
, extra_gfp
),
670 HPAGE_PMD_ORDER
, vma
, haddr
, nd
);
674 static inline struct page
*alloc_hugepage(int defrag
)
676 return alloc_pages(alloc_hugepage_gfpmask(defrag
, 0),
681 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
682 unsigned long address
, pmd_t
*pmd
,
686 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
689 if (haddr
>= vma
->vm_start
&& haddr
+ HPAGE_PMD_SIZE
<= vma
->vm_end
) {
690 if (unlikely(anon_vma_prepare(vma
)))
692 if (unlikely(khugepaged_enter(vma
)))
694 page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
695 vma
, haddr
, numa_node_id(), 0);
696 if (unlikely(!page
)) {
697 count_vm_event(THP_FAULT_FALLBACK
);
700 count_vm_event(THP_FAULT_ALLOC
);
701 if (unlikely(mem_cgroup_newpage_charge(page
, mm
, GFP_KERNEL
))) {
705 if (unlikely(__do_huge_pmd_anonymous_page(mm
, vma
, haddr
, pmd
,
707 mem_cgroup_uncharge_page(page
);
716 * Use __pte_alloc instead of pte_alloc_map, because we can't
717 * run pte_offset_map on the pmd, if an huge pmd could
718 * materialize from under us from a different thread.
720 if (unlikely(__pte_alloc(mm
, vma
, pmd
, address
)))
722 /* if an huge pmd materialized from under us just retry later */
723 if (unlikely(pmd_trans_huge(*pmd
)))
726 * A regular pmd is established and it can't morph into a huge pmd
727 * from under us anymore at this point because we hold the mmap_sem
728 * read mode and khugepaged takes it in write mode. So now it's
729 * safe to run pte_offset_map().
731 pte
= pte_offset_map(pmd
, address
);
732 return handle_pte_fault(mm
, vma
, address
, pte
, pmd
, flags
);
735 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
736 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
737 struct vm_area_struct
*vma
)
739 struct page
*src_page
;
745 pgtable
= pte_alloc_one(dst_mm
, addr
);
746 if (unlikely(!pgtable
))
749 spin_lock(&dst_mm
->page_table_lock
);
750 spin_lock_nested(&src_mm
->page_table_lock
, SINGLE_DEPTH_NESTING
);
754 if (unlikely(!pmd_trans_huge(pmd
))) {
755 pte_free(dst_mm
, pgtable
);
758 if (unlikely(pmd_trans_splitting(pmd
))) {
759 /* split huge page running from under us */
760 spin_unlock(&src_mm
->page_table_lock
);
761 spin_unlock(&dst_mm
->page_table_lock
);
762 pte_free(dst_mm
, pgtable
);
764 wait_split_huge_page(vma
->anon_vma
, src_pmd
); /* src_vma */
767 src_page
= pmd_page(pmd
);
768 VM_BUG_ON(!PageHead(src_page
));
770 page_dup_rmap(src_page
);
771 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
773 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
774 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
775 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
776 pgtable_trans_huge_deposit(dst_mm
, pgtable
);
781 spin_unlock(&src_mm
->page_table_lock
);
782 spin_unlock(&dst_mm
->page_table_lock
);
787 void huge_pmd_set_accessed(struct mm_struct
*mm
,
788 struct vm_area_struct
*vma
,
789 unsigned long address
,
790 pmd_t
*pmd
, pmd_t orig_pmd
,
796 spin_lock(&mm
->page_table_lock
);
797 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
800 entry
= pmd_mkyoung(orig_pmd
);
801 haddr
= address
& HPAGE_PMD_MASK
;
802 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
803 update_mmu_cache_pmd(vma
, address
, pmd
);
806 spin_unlock(&mm
->page_table_lock
);
809 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
810 struct vm_area_struct
*vma
,
811 unsigned long address
,
812 pmd_t
*pmd
, pmd_t orig_pmd
,
820 unsigned long mmun_start
; /* For mmu_notifiers */
821 unsigned long mmun_end
; /* For mmu_notifiers */
823 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
825 if (unlikely(!pages
)) {
830 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
831 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
833 vma
, address
, page_to_nid(page
));
834 if (unlikely(!pages
[i
] ||
835 mem_cgroup_newpage_charge(pages
[i
], mm
,
839 mem_cgroup_uncharge_start();
841 mem_cgroup_uncharge_page(pages
[i
]);
844 mem_cgroup_uncharge_end();
851 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
852 copy_user_highpage(pages
[i
], page
+ i
,
853 haddr
+ PAGE_SIZE
* i
, vma
);
854 __SetPageUptodate(pages
[i
]);
859 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
860 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
862 spin_lock(&mm
->page_table_lock
);
863 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
865 VM_BUG_ON(!PageHead(page
));
867 pmdp_clear_flush(vma
, haddr
, pmd
);
868 /* leave pmd empty until pte is filled */
870 pgtable
= pgtable_trans_huge_withdraw(mm
);
871 pmd_populate(mm
, &_pmd
, pgtable
);
873 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
875 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
876 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
877 page_add_new_anon_rmap(pages
[i
], vma
, haddr
);
878 pte
= pte_offset_map(&_pmd
, haddr
);
879 VM_BUG_ON(!pte_none(*pte
));
880 set_pte_at(mm
, haddr
, pte
, entry
);
885 smp_wmb(); /* make pte visible before pmd */
886 pmd_populate(mm
, pmd
, pgtable
);
887 page_remove_rmap(page
);
888 spin_unlock(&mm
->page_table_lock
);
890 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
892 ret
|= VM_FAULT_WRITE
;
899 spin_unlock(&mm
->page_table_lock
);
900 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
901 mem_cgroup_uncharge_start();
902 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
903 mem_cgroup_uncharge_page(pages
[i
]);
906 mem_cgroup_uncharge_end();
911 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
912 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
915 struct page
*page
, *new_page
;
917 unsigned long mmun_start
; /* For mmu_notifiers */
918 unsigned long mmun_end
; /* For mmu_notifiers */
920 VM_BUG_ON(!vma
->anon_vma
);
921 spin_lock(&mm
->page_table_lock
);
922 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
925 page
= pmd_page(orig_pmd
);
926 VM_BUG_ON(!PageCompound(page
) || !PageHead(page
));
927 haddr
= address
& HPAGE_PMD_MASK
;
928 if (page_mapcount(page
) == 1) {
930 entry
= pmd_mkyoung(orig_pmd
);
931 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
932 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
933 update_mmu_cache_pmd(vma
, address
, pmd
);
934 ret
|= VM_FAULT_WRITE
;
938 spin_unlock(&mm
->page_table_lock
);
940 if (transparent_hugepage_enabled(vma
) &&
941 !transparent_hugepage_debug_cow())
942 new_page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
943 vma
, haddr
, numa_node_id(), 0);
947 if (unlikely(!new_page
)) {
948 count_vm_event(THP_FAULT_FALLBACK
);
949 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
950 pmd
, orig_pmd
, page
, haddr
);
951 if (ret
& VM_FAULT_OOM
)
952 split_huge_page(page
);
956 count_vm_event(THP_FAULT_ALLOC
);
958 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
))) {
960 split_huge_page(page
);
966 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
967 __SetPageUptodate(new_page
);
970 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
971 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
973 spin_lock(&mm
->page_table_lock
);
975 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
976 spin_unlock(&mm
->page_table_lock
);
977 mem_cgroup_uncharge_page(new_page
);
982 VM_BUG_ON(!PageHead(page
));
983 entry
= mk_huge_pmd(new_page
, vma
);
984 pmdp_clear_flush(vma
, haddr
, pmd
);
985 page_add_new_anon_rmap(new_page
, vma
, haddr
);
986 set_pmd_at(mm
, haddr
, pmd
, entry
);
987 update_mmu_cache_pmd(vma
, address
, pmd
);
988 page_remove_rmap(page
);
990 ret
|= VM_FAULT_WRITE
;
992 spin_unlock(&mm
->page_table_lock
);
994 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
998 spin_unlock(&mm
->page_table_lock
);
1002 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1007 struct mm_struct
*mm
= vma
->vm_mm
;
1008 struct page
*page
= NULL
;
1010 assert_spin_locked(&mm
->page_table_lock
);
1012 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1015 page
= pmd_page(*pmd
);
1016 VM_BUG_ON(!PageHead(page
));
1017 if (flags
& FOLL_TOUCH
) {
1020 * We should set the dirty bit only for FOLL_WRITE but
1021 * for now the dirty bit in the pmd is meaningless.
1022 * And if the dirty bit will become meaningful and
1023 * we'll only set it with FOLL_WRITE, an atomic
1024 * set_bit will be required on the pmd to set the
1025 * young bit, instead of the current set_pmd_at.
1027 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1028 set_pmd_at(mm
, addr
& HPAGE_PMD_MASK
, pmd
, _pmd
);
1030 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1031 if (page
->mapping
&& trylock_page(page
)) {
1034 mlock_vma_page(page
);
1038 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1039 VM_BUG_ON(!PageCompound(page
));
1040 if (flags
& FOLL_GET
)
1041 get_page_foll(page
);
1047 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1048 pmd_t
*pmd
, unsigned long addr
)
1052 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1056 pgtable
= pgtable_trans_huge_withdraw(tlb
->mm
);
1057 orig_pmd
= pmdp_get_and_clear(tlb
->mm
, addr
, pmd
);
1058 page
= pmd_page(orig_pmd
);
1059 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1060 page_remove_rmap(page
);
1061 VM_BUG_ON(page_mapcount(page
) < 0);
1062 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1063 VM_BUG_ON(!PageHead(page
));
1065 spin_unlock(&tlb
->mm
->page_table_lock
);
1066 tlb_remove_page(tlb
, page
);
1067 pte_free(tlb
->mm
, pgtable
);
1073 int mincore_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1074 unsigned long addr
, unsigned long end
,
1079 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1081 * All logical pages in the range are present
1082 * if backed by a huge page.
1084 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1085 memset(vec
, 1, (end
- addr
) >> PAGE_SHIFT
);
1092 int move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1093 unsigned long old_addr
,
1094 unsigned long new_addr
, unsigned long old_end
,
1095 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1100 struct mm_struct
*mm
= vma
->vm_mm
;
1102 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1103 (new_addr
& ~HPAGE_PMD_MASK
) ||
1104 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1105 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1109 * The destination pmd shouldn't be established, free_pgtables()
1110 * should have release it.
1112 if (WARN_ON(!pmd_none(*new_pmd
))) {
1113 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1117 ret
= __pmd_trans_huge_lock(old_pmd
, vma
);
1119 pmd
= pmdp_get_and_clear(mm
, old_addr
, old_pmd
);
1120 VM_BUG_ON(!pmd_none(*new_pmd
));
1121 set_pmd_at(mm
, new_addr
, new_pmd
, pmd
);
1122 spin_unlock(&mm
->page_table_lock
);
1128 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1129 unsigned long addr
, pgprot_t newprot
)
1131 struct mm_struct
*mm
= vma
->vm_mm
;
1134 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1136 entry
= pmdp_get_and_clear(mm
, addr
, pmd
);
1137 entry
= pmd_modify(entry
, newprot
);
1138 set_pmd_at(mm
, addr
, pmd
, entry
);
1139 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1147 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1148 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1150 * Note that if it returns 1, this routine returns without unlocking page
1151 * table locks. So callers must unlock them.
1153 int __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1155 spin_lock(&vma
->vm_mm
->page_table_lock
);
1156 if (likely(pmd_trans_huge(*pmd
))) {
1157 if (unlikely(pmd_trans_splitting(*pmd
))) {
1158 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1159 wait_split_huge_page(vma
->anon_vma
, pmd
);
1162 /* Thp mapped by 'pmd' is stable, so we can
1163 * handle it as it is. */
1167 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1171 pmd_t
*page_check_address_pmd(struct page
*page
,
1172 struct mm_struct
*mm
,
1173 unsigned long address
,
1174 enum page_check_address_pmd_flag flag
)
1176 pmd_t
*pmd
, *ret
= NULL
;
1178 if (address
& ~HPAGE_PMD_MASK
)
1181 pmd
= mm_find_pmd(mm
, address
);
1186 if (pmd_page(*pmd
) != page
)
1189 * split_vma() may create temporary aliased mappings. There is
1190 * no risk as long as all huge pmd are found and have their
1191 * splitting bit set before __split_huge_page_refcount
1192 * runs. Finding the same huge pmd more than once during the
1193 * same rmap walk is not a problem.
1195 if (flag
== PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
&&
1196 pmd_trans_splitting(*pmd
))
1198 if (pmd_trans_huge(*pmd
)) {
1199 VM_BUG_ON(flag
== PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
&&
1200 !pmd_trans_splitting(*pmd
));
1207 static int __split_huge_page_splitting(struct page
*page
,
1208 struct vm_area_struct
*vma
,
1209 unsigned long address
)
1211 struct mm_struct
*mm
= vma
->vm_mm
;
1214 /* For mmu_notifiers */
1215 const unsigned long mmun_start
= address
;
1216 const unsigned long mmun_end
= address
+ HPAGE_PMD_SIZE
;
1218 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1219 spin_lock(&mm
->page_table_lock
);
1220 pmd
= page_check_address_pmd(page
, mm
, address
,
1221 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
);
1224 * We can't temporarily set the pmd to null in order
1225 * to split it, the pmd must remain marked huge at all
1226 * times or the VM won't take the pmd_trans_huge paths
1227 * and it won't wait on the anon_vma->root->mutex to
1228 * serialize against split_huge_page*.
1230 pmdp_splitting_flush(vma
, address
, pmd
);
1233 spin_unlock(&mm
->page_table_lock
);
1234 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1239 static void __split_huge_page_refcount(struct page
*page
)
1242 struct zone
*zone
= page_zone(page
);
1243 struct lruvec
*lruvec
;
1246 /* prevent PageLRU to go away from under us, and freeze lru stats */
1247 spin_lock_irq(&zone
->lru_lock
);
1248 lruvec
= mem_cgroup_page_lruvec(page
, zone
);
1250 compound_lock(page
);
1251 /* complete memcg works before add pages to LRU */
1252 mem_cgroup_split_huge_fixup(page
);
1254 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1255 struct page
*page_tail
= page
+ i
;
1257 /* tail_page->_mapcount cannot change */
1258 BUG_ON(page_mapcount(page_tail
) < 0);
1259 tail_count
+= page_mapcount(page_tail
);
1260 /* check for overflow */
1261 BUG_ON(tail_count
< 0);
1262 BUG_ON(atomic_read(&page_tail
->_count
) != 0);
1264 * tail_page->_count is zero and not changing from
1265 * under us. But get_page_unless_zero() may be running
1266 * from under us on the tail_page. If we used
1267 * atomic_set() below instead of atomic_add(), we
1268 * would then run atomic_set() concurrently with
1269 * get_page_unless_zero(), and atomic_set() is
1270 * implemented in C not using locked ops. spin_unlock
1271 * on x86 sometime uses locked ops because of PPro
1272 * errata 66, 92, so unless somebody can guarantee
1273 * atomic_set() here would be safe on all archs (and
1274 * not only on x86), it's safer to use atomic_add().
1276 atomic_add(page_mapcount(page
) + page_mapcount(page_tail
) + 1,
1277 &page_tail
->_count
);
1279 /* after clearing PageTail the gup refcount can be released */
1283 * retain hwpoison flag of the poisoned tail page:
1284 * fix for the unsuitable process killed on Guest Machine(KVM)
1285 * by the memory-failure.
1287 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
| __PG_HWPOISON
;
1288 page_tail
->flags
|= (page
->flags
&
1289 ((1L << PG_referenced
) |
1290 (1L << PG_swapbacked
) |
1291 (1L << PG_mlocked
) |
1292 (1L << PG_uptodate
)));
1293 page_tail
->flags
|= (1L << PG_dirty
);
1295 /* clear PageTail before overwriting first_page */
1299 * __split_huge_page_splitting() already set the
1300 * splitting bit in all pmd that could map this
1301 * hugepage, that will ensure no CPU can alter the
1302 * mapcount on the head page. The mapcount is only
1303 * accounted in the head page and it has to be
1304 * transferred to all tail pages in the below code. So
1305 * for this code to be safe, the split the mapcount
1306 * can't change. But that doesn't mean userland can't
1307 * keep changing and reading the page contents while
1308 * we transfer the mapcount, so the pmd splitting
1309 * status is achieved setting a reserved bit in the
1310 * pmd, not by clearing the present bit.
1312 page_tail
->_mapcount
= page
->_mapcount
;
1314 BUG_ON(page_tail
->mapping
);
1315 page_tail
->mapping
= page
->mapping
;
1317 page_tail
->index
= page
->index
+ i
;
1319 BUG_ON(!PageAnon(page_tail
));
1320 BUG_ON(!PageUptodate(page_tail
));
1321 BUG_ON(!PageDirty(page_tail
));
1322 BUG_ON(!PageSwapBacked(page_tail
));
1324 lru_add_page_tail(page
, page_tail
, lruvec
);
1326 atomic_sub(tail_count
, &page
->_count
);
1327 BUG_ON(atomic_read(&page
->_count
) <= 0);
1329 __mod_zone_page_state(zone
, NR_ANON_TRANSPARENT_HUGEPAGES
, -1);
1330 __mod_zone_page_state(zone
, NR_ANON_PAGES
, HPAGE_PMD_NR
);
1332 ClearPageCompound(page
);
1333 compound_unlock(page
);
1334 spin_unlock_irq(&zone
->lru_lock
);
1336 for (i
= 1; i
< HPAGE_PMD_NR
; i
++) {
1337 struct page
*page_tail
= page
+ i
;
1338 BUG_ON(page_count(page_tail
) <= 0);
1340 * Tail pages may be freed if there wasn't any mapping
1341 * like if add_to_swap() is running on a lru page that
1342 * had its mapping zapped. And freeing these pages
1343 * requires taking the lru_lock so we do the put_page
1344 * of the tail pages after the split is complete.
1346 put_page(page_tail
);
1350 * Only the head page (now become a regular page) is required
1351 * to be pinned by the caller.
1353 BUG_ON(page_count(page
) <= 0);
1356 static int __split_huge_page_map(struct page
*page
,
1357 struct vm_area_struct
*vma
,
1358 unsigned long address
)
1360 struct mm_struct
*mm
= vma
->vm_mm
;
1364 unsigned long haddr
;
1366 spin_lock(&mm
->page_table_lock
);
1367 pmd
= page_check_address_pmd(page
, mm
, address
,
1368 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
);
1370 pgtable
= pgtable_trans_huge_withdraw(mm
);
1371 pmd_populate(mm
, &_pmd
, pgtable
);
1374 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1376 BUG_ON(PageCompound(page
+i
));
1377 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
1378 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1379 if (!pmd_write(*pmd
))
1380 entry
= pte_wrprotect(entry
);
1382 BUG_ON(page_mapcount(page
) != 1);
1383 if (!pmd_young(*pmd
))
1384 entry
= pte_mkold(entry
);
1385 pte
= pte_offset_map(&_pmd
, haddr
);
1386 BUG_ON(!pte_none(*pte
));
1387 set_pte_at(mm
, haddr
, pte
, entry
);
1391 smp_wmb(); /* make pte visible before pmd */
1393 * Up to this point the pmd is present and huge and
1394 * userland has the whole access to the hugepage
1395 * during the split (which happens in place). If we
1396 * overwrite the pmd with the not-huge version
1397 * pointing to the pte here (which of course we could
1398 * if all CPUs were bug free), userland could trigger
1399 * a small page size TLB miss on the small sized TLB
1400 * while the hugepage TLB entry is still established
1401 * in the huge TLB. Some CPU doesn't like that. See
1402 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1403 * Erratum 383 on page 93. Intel should be safe but is
1404 * also warns that it's only safe if the permission
1405 * and cache attributes of the two entries loaded in
1406 * the two TLB is identical (which should be the case
1407 * here). But it is generally safer to never allow
1408 * small and huge TLB entries for the same virtual
1409 * address to be loaded simultaneously. So instead of
1410 * doing "pmd_populate(); flush_tlb_range();" we first
1411 * mark the current pmd notpresent (atomically because
1412 * here the pmd_trans_huge and pmd_trans_splitting
1413 * must remain set at all times on the pmd until the
1414 * split is complete for this pmd), then we flush the
1415 * SMP TLB and finally we write the non-huge version
1416 * of the pmd entry with pmd_populate.
1418 pmdp_invalidate(vma
, address
, pmd
);
1419 pmd_populate(mm
, pmd
, pgtable
);
1422 spin_unlock(&mm
->page_table_lock
);
1427 /* must be called with anon_vma->root->mutex hold */
1428 static void __split_huge_page(struct page
*page
,
1429 struct anon_vma
*anon_vma
)
1431 int mapcount
, mapcount2
;
1432 pgoff_t pgoff
= page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
1433 struct anon_vma_chain
*avc
;
1435 BUG_ON(!PageHead(page
));
1436 BUG_ON(PageTail(page
));
1439 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1440 struct vm_area_struct
*vma
= avc
->vma
;
1441 unsigned long addr
= vma_address(page
, vma
);
1442 BUG_ON(is_vma_temporary_stack(vma
));
1443 mapcount
+= __split_huge_page_splitting(page
, vma
, addr
);
1446 * It is critical that new vmas are added to the tail of the
1447 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1448 * and establishes a child pmd before
1449 * __split_huge_page_splitting() freezes the parent pmd (so if
1450 * we fail to prevent copy_huge_pmd() from running until the
1451 * whole __split_huge_page() is complete), we will still see
1452 * the newly established pmd of the child later during the
1453 * walk, to be able to set it as pmd_trans_splitting too.
1455 if (mapcount
!= page_mapcount(page
))
1456 printk(KERN_ERR
"mapcount %d page_mapcount %d\n",
1457 mapcount
, page_mapcount(page
));
1458 BUG_ON(mapcount
!= page_mapcount(page
));
1460 __split_huge_page_refcount(page
);
1463 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1464 struct vm_area_struct
*vma
= avc
->vma
;
1465 unsigned long addr
= vma_address(page
, vma
);
1466 BUG_ON(is_vma_temporary_stack(vma
));
1467 mapcount2
+= __split_huge_page_map(page
, vma
, addr
);
1469 if (mapcount
!= mapcount2
)
1470 printk(KERN_ERR
"mapcount %d mapcount2 %d page_mapcount %d\n",
1471 mapcount
, mapcount2
, page_mapcount(page
));
1472 BUG_ON(mapcount
!= mapcount2
);
1475 int split_huge_page(struct page
*page
)
1477 struct anon_vma
*anon_vma
;
1480 BUG_ON(!PageAnon(page
));
1481 anon_vma
= page_lock_anon_vma(page
);
1485 if (!PageCompound(page
))
1488 BUG_ON(!PageSwapBacked(page
));
1489 __split_huge_page(page
, anon_vma
);
1490 count_vm_event(THP_SPLIT
);
1492 BUG_ON(PageCompound(page
));
1494 page_unlock_anon_vma(anon_vma
);
1499 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1501 int hugepage_madvise(struct vm_area_struct
*vma
,
1502 unsigned long *vm_flags
, int advice
)
1504 struct mm_struct
*mm
= vma
->vm_mm
;
1509 * Be somewhat over-protective like KSM for now!
1511 if (*vm_flags
& (VM_HUGEPAGE
| VM_NO_THP
))
1513 if (mm
->def_flags
& VM_NOHUGEPAGE
)
1515 *vm_flags
&= ~VM_NOHUGEPAGE
;
1516 *vm_flags
|= VM_HUGEPAGE
;
1518 * If the vma become good for khugepaged to scan,
1519 * register it here without waiting a page fault that
1520 * may not happen any time soon.
1522 if (unlikely(khugepaged_enter_vma_merge(vma
)))
1525 case MADV_NOHUGEPAGE
:
1527 * Be somewhat over-protective like KSM for now!
1529 if (*vm_flags
& (VM_NOHUGEPAGE
| VM_NO_THP
))
1531 *vm_flags
&= ~VM_HUGEPAGE
;
1532 *vm_flags
|= VM_NOHUGEPAGE
;
1534 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1535 * this vma even if we leave the mm registered in khugepaged if
1536 * it got registered before VM_NOHUGEPAGE was set.
1544 static int __init
khugepaged_slab_init(void)
1546 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1547 sizeof(struct mm_slot
),
1548 __alignof__(struct mm_slot
), 0, NULL
);
1555 static void __init
khugepaged_slab_free(void)
1557 kmem_cache_destroy(mm_slot_cache
);
1558 mm_slot_cache
= NULL
;
1561 static inline struct mm_slot
*alloc_mm_slot(void)
1563 if (!mm_slot_cache
) /* initialization failed */
1565 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1568 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1570 kmem_cache_free(mm_slot_cache
, mm_slot
);
1573 static int __init
mm_slots_hash_init(void)
1575 mm_slots_hash
= kzalloc(MM_SLOTS_HASH_HEADS
* sizeof(struct hlist_head
),
1583 static void __init
mm_slots_hash_free(void)
1585 kfree(mm_slots_hash
);
1586 mm_slots_hash
= NULL
;
1590 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1592 struct mm_slot
*mm_slot
;
1593 struct hlist_head
*bucket
;
1594 struct hlist_node
*node
;
1596 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1597 % MM_SLOTS_HASH_HEADS
];
1598 hlist_for_each_entry(mm_slot
, node
, bucket
, hash
) {
1599 if (mm
== mm_slot
->mm
)
1605 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1606 struct mm_slot
*mm_slot
)
1608 struct hlist_head
*bucket
;
1610 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1611 % MM_SLOTS_HASH_HEADS
];
1613 hlist_add_head(&mm_slot
->hash
, bucket
);
1616 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1618 return atomic_read(&mm
->mm_users
) == 0;
1621 int __khugepaged_enter(struct mm_struct
*mm
)
1623 struct mm_slot
*mm_slot
;
1626 mm_slot
= alloc_mm_slot();
1630 /* __khugepaged_exit() must not run from under us */
1631 VM_BUG_ON(khugepaged_test_exit(mm
));
1632 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1633 free_mm_slot(mm_slot
);
1637 spin_lock(&khugepaged_mm_lock
);
1638 insert_to_mm_slots_hash(mm
, mm_slot
);
1640 * Insert just behind the scanning cursor, to let the area settle
1643 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
1644 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
1645 spin_unlock(&khugepaged_mm_lock
);
1647 atomic_inc(&mm
->mm_count
);
1649 wake_up_interruptible(&khugepaged_wait
);
1654 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
)
1656 unsigned long hstart
, hend
;
1659 * Not yet faulted in so we will register later in the
1660 * page fault if needed.
1664 /* khugepaged not yet working on file or special mappings */
1666 VM_BUG_ON(vma
->vm_flags
& VM_NO_THP
);
1667 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1668 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1670 return khugepaged_enter(vma
);
1674 void __khugepaged_exit(struct mm_struct
*mm
)
1676 struct mm_slot
*mm_slot
;
1679 spin_lock(&khugepaged_mm_lock
);
1680 mm_slot
= get_mm_slot(mm
);
1681 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
1682 hlist_del(&mm_slot
->hash
);
1683 list_del(&mm_slot
->mm_node
);
1686 spin_unlock(&khugepaged_mm_lock
);
1689 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
1690 free_mm_slot(mm_slot
);
1692 } else if (mm_slot
) {
1694 * This is required to serialize against
1695 * khugepaged_test_exit() (which is guaranteed to run
1696 * under mmap sem read mode). Stop here (after we
1697 * return all pagetables will be destroyed) until
1698 * khugepaged has finished working on the pagetables
1699 * under the mmap_sem.
1701 down_write(&mm
->mmap_sem
);
1702 up_write(&mm
->mmap_sem
);
1706 static void release_pte_page(struct page
*page
)
1708 /* 0 stands for page_is_file_cache(page) == false */
1709 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1711 putback_lru_page(page
);
1714 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
1716 while (--_pte
>= pte
) {
1717 pte_t pteval
= *_pte
;
1718 if (!pte_none(pteval
))
1719 release_pte_page(pte_page(pteval
));
1723 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
1724 unsigned long address
,
1729 int referenced
= 0, none
= 0;
1730 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
1731 _pte
++, address
+= PAGE_SIZE
) {
1732 pte_t pteval
= *_pte
;
1733 if (pte_none(pteval
)) {
1734 if (++none
<= khugepaged_max_ptes_none
)
1739 if (!pte_present(pteval
) || !pte_write(pteval
))
1741 page
= vm_normal_page(vma
, address
, pteval
);
1742 if (unlikely(!page
))
1745 VM_BUG_ON(PageCompound(page
));
1746 BUG_ON(!PageAnon(page
));
1747 VM_BUG_ON(!PageSwapBacked(page
));
1749 /* cannot use mapcount: can't collapse if there's a gup pin */
1750 if (page_count(page
) != 1)
1753 * We can do it before isolate_lru_page because the
1754 * page can't be freed from under us. NOTE: PG_lock
1755 * is needed to serialize against split_huge_page
1756 * when invoked from the VM.
1758 if (!trylock_page(page
))
1761 * Isolate the page to avoid collapsing an hugepage
1762 * currently in use by the VM.
1764 if (isolate_lru_page(page
)) {
1768 /* 0 stands for page_is_file_cache(page) == false */
1769 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1770 VM_BUG_ON(!PageLocked(page
));
1771 VM_BUG_ON(PageLRU(page
));
1773 /* If there is no mapped pte young don't collapse the page */
1774 if (pte_young(pteval
) || PageReferenced(page
) ||
1775 mmu_notifier_test_young(vma
->vm_mm
, address
))
1778 if (likely(referenced
))
1781 release_pte_pages(pte
, _pte
);
1785 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
1786 struct vm_area_struct
*vma
,
1787 unsigned long address
,
1791 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
1792 pte_t pteval
= *_pte
;
1793 struct page
*src_page
;
1795 if (pte_none(pteval
)) {
1796 clear_user_highpage(page
, address
);
1797 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
1799 src_page
= pte_page(pteval
);
1800 copy_user_highpage(page
, src_page
, address
, vma
);
1801 VM_BUG_ON(page_mapcount(src_page
) != 1);
1802 release_pte_page(src_page
);
1804 * ptl mostly unnecessary, but preempt has to
1805 * be disabled to update the per-cpu stats
1806 * inside page_remove_rmap().
1810 * paravirt calls inside pte_clear here are
1813 pte_clear(vma
->vm_mm
, address
, _pte
);
1814 page_remove_rmap(src_page
);
1816 free_page_and_swap_cache(src_page
);
1819 address
+= PAGE_SIZE
;
1824 static void khugepaged_alloc_sleep(void)
1826 wait_event_freezable_timeout(khugepaged_wait
, false,
1827 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
1831 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
1833 if (IS_ERR(*hpage
)) {
1839 khugepaged_alloc_sleep();
1840 } else if (*hpage
) {
1849 *khugepaged_alloc_page(struct page
**hpage
, struct mm_struct
*mm
,
1850 struct vm_area_struct
*vma
, unsigned long address
,
1855 * Allocate the page while the vma is still valid and under
1856 * the mmap_sem read mode so there is no memory allocation
1857 * later when we take the mmap_sem in write mode. This is more
1858 * friendly behavior (OTOH it may actually hide bugs) to
1859 * filesystems in userland with daemons allocating memory in
1860 * the userland I/O paths. Allocating memory with the
1861 * mmap_sem in read mode is good idea also to allow greater
1864 *hpage
= alloc_hugepage_vma(khugepaged_defrag(), vma
, address
,
1865 node
, __GFP_OTHER_NODE
);
1868 * After allocating the hugepage, release the mmap_sem read lock in
1869 * preparation for taking it in write mode.
1871 up_read(&mm
->mmap_sem
);
1872 if (unlikely(!*hpage
)) {
1873 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
1874 *hpage
= ERR_PTR(-ENOMEM
);
1878 count_vm_event(THP_COLLAPSE_ALLOC
);
1882 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
1887 hpage
= alloc_hugepage(khugepaged_defrag());
1889 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
1894 khugepaged_alloc_sleep();
1896 count_vm_event(THP_COLLAPSE_ALLOC
);
1897 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
1902 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
1905 *hpage
= khugepaged_alloc_hugepage(wait
);
1907 if (unlikely(!*hpage
))
1914 *khugepaged_alloc_page(struct page
**hpage
, struct mm_struct
*mm
,
1915 struct vm_area_struct
*vma
, unsigned long address
,
1918 up_read(&mm
->mmap_sem
);
1924 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
1926 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
1927 (vma
->vm_flags
& VM_NOHUGEPAGE
))
1930 if (!vma
->anon_vma
|| vma
->vm_ops
)
1932 if (is_vma_temporary_stack(vma
))
1934 VM_BUG_ON(vma
->vm_flags
& VM_NO_THP
);
1938 static void collapse_huge_page(struct mm_struct
*mm
,
1939 unsigned long address
,
1940 struct page
**hpage
,
1941 struct vm_area_struct
*vma
,
1947 struct page
*new_page
;
1950 unsigned long hstart
, hend
;
1951 unsigned long mmun_start
; /* For mmu_notifiers */
1952 unsigned long mmun_end
; /* For mmu_notifiers */
1954 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
1956 /* release the mmap_sem read lock. */
1957 new_page
= khugepaged_alloc_page(hpage
, mm
, vma
, address
, node
);
1961 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
)))
1965 * Prevent all access to pagetables with the exception of
1966 * gup_fast later hanlded by the ptep_clear_flush and the VM
1967 * handled by the anon_vma lock + PG_lock.
1969 down_write(&mm
->mmap_sem
);
1970 if (unlikely(khugepaged_test_exit(mm
)))
1973 vma
= find_vma(mm
, address
);
1974 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1975 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1976 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
1978 if (!hugepage_vma_check(vma
))
1980 pmd
= mm_find_pmd(mm
, address
);
1983 if (pmd_trans_huge(*pmd
))
1986 anon_vma_lock(vma
->anon_vma
);
1988 pte
= pte_offset_map(pmd
, address
);
1989 ptl
= pte_lockptr(mm
, pmd
);
1991 mmun_start
= address
;
1992 mmun_end
= address
+ HPAGE_PMD_SIZE
;
1993 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1994 spin_lock(&mm
->page_table_lock
); /* probably unnecessary */
1996 * After this gup_fast can't run anymore. This also removes
1997 * any huge TLB entry from the CPU so we won't allow
1998 * huge and small TLB entries for the same virtual address
1999 * to avoid the risk of CPU bugs in that area.
2001 _pmd
= pmdp_clear_flush(vma
, address
, pmd
);
2002 spin_unlock(&mm
->page_table_lock
);
2003 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2006 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2009 if (unlikely(!isolated
)) {
2011 spin_lock(&mm
->page_table_lock
);
2012 BUG_ON(!pmd_none(*pmd
));
2013 set_pmd_at(mm
, address
, pmd
, _pmd
);
2014 spin_unlock(&mm
->page_table_lock
);
2015 anon_vma_unlock(vma
->anon_vma
);
2020 * All pages are isolated and locked so anon_vma rmap
2021 * can't run anymore.
2023 anon_vma_unlock(vma
->anon_vma
);
2025 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, ptl
);
2027 __SetPageUptodate(new_page
);
2028 pgtable
= pmd_pgtable(_pmd
);
2030 _pmd
= mk_huge_pmd(new_page
, vma
);
2033 * spin_lock() below is not the equivalent of smp_wmb(), so
2034 * this is needed to avoid the copy_huge_page writes to become
2035 * visible after the set_pmd_at() write.
2039 spin_lock(&mm
->page_table_lock
);
2040 BUG_ON(!pmd_none(*pmd
));
2041 page_add_new_anon_rmap(new_page
, vma
, address
);
2042 set_pmd_at(mm
, address
, pmd
, _pmd
);
2043 update_mmu_cache_pmd(vma
, address
, pmd
);
2044 pgtable_trans_huge_deposit(mm
, pgtable
);
2045 spin_unlock(&mm
->page_table_lock
);
2049 khugepaged_pages_collapsed
++;
2051 up_write(&mm
->mmap_sem
);
2055 mem_cgroup_uncharge_page(new_page
);
2059 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2060 struct vm_area_struct
*vma
,
2061 unsigned long address
,
2062 struct page
**hpage
)
2066 int ret
= 0, referenced
= 0, none
= 0;
2068 unsigned long _address
;
2072 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2074 pmd
= mm_find_pmd(mm
, address
);
2077 if (pmd_trans_huge(*pmd
))
2080 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2081 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2082 _pte
++, _address
+= PAGE_SIZE
) {
2083 pte_t pteval
= *_pte
;
2084 if (pte_none(pteval
)) {
2085 if (++none
<= khugepaged_max_ptes_none
)
2090 if (!pte_present(pteval
) || !pte_write(pteval
))
2092 page
= vm_normal_page(vma
, _address
, pteval
);
2093 if (unlikely(!page
))
2096 * Chose the node of the first page. This could
2097 * be more sophisticated and look at more pages,
2098 * but isn't for now.
2101 node
= page_to_nid(page
);
2102 VM_BUG_ON(PageCompound(page
));
2103 if (!PageLRU(page
) || PageLocked(page
) || !PageAnon(page
))
2105 /* cannot use mapcount: can't collapse if there's a gup pin */
2106 if (page_count(page
) != 1)
2108 if (pte_young(pteval
) || PageReferenced(page
) ||
2109 mmu_notifier_test_young(vma
->vm_mm
, address
))
2115 pte_unmap_unlock(pte
, ptl
);
2117 /* collapse_huge_page will return with the mmap_sem released */
2118 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2123 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2125 struct mm_struct
*mm
= mm_slot
->mm
;
2127 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2129 if (khugepaged_test_exit(mm
)) {
2131 hlist_del(&mm_slot
->hash
);
2132 list_del(&mm_slot
->mm_node
);
2135 * Not strictly needed because the mm exited already.
2137 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2140 /* khugepaged_mm_lock actually not necessary for the below */
2141 free_mm_slot(mm_slot
);
2146 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2147 struct page
**hpage
)
2148 __releases(&khugepaged_mm_lock
)
2149 __acquires(&khugepaged_mm_lock
)
2151 struct mm_slot
*mm_slot
;
2152 struct mm_struct
*mm
;
2153 struct vm_area_struct
*vma
;
2157 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2159 if (khugepaged_scan
.mm_slot
)
2160 mm_slot
= khugepaged_scan
.mm_slot
;
2162 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2163 struct mm_slot
, mm_node
);
2164 khugepaged_scan
.address
= 0;
2165 khugepaged_scan
.mm_slot
= mm_slot
;
2167 spin_unlock(&khugepaged_mm_lock
);
2170 down_read(&mm
->mmap_sem
);
2171 if (unlikely(khugepaged_test_exit(mm
)))
2174 vma
= find_vma(mm
, khugepaged_scan
.address
);
2177 for (; vma
; vma
= vma
->vm_next
) {
2178 unsigned long hstart
, hend
;
2181 if (unlikely(khugepaged_test_exit(mm
))) {
2185 if (!hugepage_vma_check(vma
)) {
2190 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2191 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2194 if (khugepaged_scan
.address
> hend
)
2196 if (khugepaged_scan
.address
< hstart
)
2197 khugepaged_scan
.address
= hstart
;
2198 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2200 while (khugepaged_scan
.address
< hend
) {
2203 if (unlikely(khugepaged_test_exit(mm
)))
2204 goto breakouterloop
;
2206 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2207 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2209 ret
= khugepaged_scan_pmd(mm
, vma
,
2210 khugepaged_scan
.address
,
2212 /* move to next address */
2213 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2214 progress
+= HPAGE_PMD_NR
;
2216 /* we released mmap_sem so break loop */
2217 goto breakouterloop_mmap_sem
;
2218 if (progress
>= pages
)
2219 goto breakouterloop
;
2223 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2224 breakouterloop_mmap_sem
:
2226 spin_lock(&khugepaged_mm_lock
);
2227 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2229 * Release the current mm_slot if this mm is about to die, or
2230 * if we scanned all vmas of this mm.
2232 if (khugepaged_test_exit(mm
) || !vma
) {
2234 * Make sure that if mm_users is reaching zero while
2235 * khugepaged runs here, khugepaged_exit will find
2236 * mm_slot not pointing to the exiting mm.
2238 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2239 khugepaged_scan
.mm_slot
= list_entry(
2240 mm_slot
->mm_node
.next
,
2241 struct mm_slot
, mm_node
);
2242 khugepaged_scan
.address
= 0;
2244 khugepaged_scan
.mm_slot
= NULL
;
2245 khugepaged_full_scans
++;
2248 collect_mm_slot(mm_slot
);
2254 static int khugepaged_has_work(void)
2256 return !list_empty(&khugepaged_scan
.mm_head
) &&
2257 khugepaged_enabled();
2260 static int khugepaged_wait_event(void)
2262 return !list_empty(&khugepaged_scan
.mm_head
) ||
2263 kthread_should_stop();
2266 static void khugepaged_do_scan(void)
2268 struct page
*hpage
= NULL
;
2269 unsigned int progress
= 0, pass_through_head
= 0;
2270 unsigned int pages
= khugepaged_pages_to_scan
;
2273 barrier(); /* write khugepaged_pages_to_scan to local stack */
2275 while (progress
< pages
) {
2276 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2281 if (unlikely(kthread_should_stop() || freezing(current
)))
2284 spin_lock(&khugepaged_mm_lock
);
2285 if (!khugepaged_scan
.mm_slot
)
2286 pass_through_head
++;
2287 if (khugepaged_has_work() &&
2288 pass_through_head
< 2)
2289 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2293 spin_unlock(&khugepaged_mm_lock
);
2296 if (!IS_ERR_OR_NULL(hpage
))
2300 static void khugepaged_wait_work(void)
2304 if (khugepaged_has_work()) {
2305 if (!khugepaged_scan_sleep_millisecs
)
2308 wait_event_freezable_timeout(khugepaged_wait
,
2309 kthread_should_stop(),
2310 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2314 if (khugepaged_enabled())
2315 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2318 static int khugepaged(void *none
)
2320 struct mm_slot
*mm_slot
;
2323 set_user_nice(current
, 19);
2325 while (!kthread_should_stop()) {
2326 khugepaged_do_scan();
2327 khugepaged_wait_work();
2330 spin_lock(&khugepaged_mm_lock
);
2331 mm_slot
= khugepaged_scan
.mm_slot
;
2332 khugepaged_scan
.mm_slot
= NULL
;
2334 collect_mm_slot(mm_slot
);
2335 spin_unlock(&khugepaged_mm_lock
);
2339 void __split_huge_page_pmd(struct mm_struct
*mm
, pmd_t
*pmd
)
2343 spin_lock(&mm
->page_table_lock
);
2344 if (unlikely(!pmd_trans_huge(*pmd
))) {
2345 spin_unlock(&mm
->page_table_lock
);
2348 page
= pmd_page(*pmd
);
2349 VM_BUG_ON(!page_count(page
));
2351 spin_unlock(&mm
->page_table_lock
);
2353 split_huge_page(page
);
2356 BUG_ON(pmd_trans_huge(*pmd
));
2359 static void split_huge_page_address(struct mm_struct
*mm
,
2360 unsigned long address
)
2364 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
2366 pmd
= mm_find_pmd(mm
, address
);
2370 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2371 * materialize from under us.
2373 split_huge_page_pmd(mm
, pmd
);
2376 void __vma_adjust_trans_huge(struct vm_area_struct
*vma
,
2377 unsigned long start
,
2382 * If the new start address isn't hpage aligned and it could
2383 * previously contain an hugepage: check if we need to split
2386 if (start
& ~HPAGE_PMD_MASK
&&
2387 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2388 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2389 split_huge_page_address(vma
->vm_mm
, start
);
2392 * If the new end address isn't hpage aligned and it could
2393 * previously contain an hugepage: check if we need to split
2396 if (end
& ~HPAGE_PMD_MASK
&&
2397 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2398 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2399 split_huge_page_address(vma
->vm_mm
, end
);
2402 * If we're also updating the vma->vm_next->vm_start, if the new
2403 * vm_next->vm_start isn't page aligned and it could previously
2404 * contain an hugepage: check if we need to split an huge pmd.
2406 if (adjust_next
> 0) {
2407 struct vm_area_struct
*next
= vma
->vm_next
;
2408 unsigned long nstart
= next
->vm_start
;
2409 nstart
+= adjust_next
<< PAGE_SHIFT
;
2410 if (nstart
& ~HPAGE_PMD_MASK
&&
2411 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
2412 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
2413 split_huge_page_address(next
->vm_mm
, nstart
);