2 * Copyright (C) 2009 Red Hat, Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
9 #include <linux/sched.h>
10 #include <linux/highmem.h>
11 #include <linux/hugetlb.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/rmap.h>
14 #include <linux/swap.h>
15 #include <linux/shrinker.h>
16 #include <linux/mm_inline.h>
17 #include <linux/kthread.h>
18 #include <linux/khugepaged.h>
19 #include <linux/freezer.h>
20 #include <linux/mman.h>
21 #include <linux/pagemap.h>
22 #include <linux/migrate.h>
25 #include <asm/pgalloc.h>
29 * By default transparent hugepage support is enabled for all mappings
30 * and khugepaged scans all mappings. Defrag is only invoked by
31 * khugepaged hugepage allocations and by page faults inside
32 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
35 unsigned long transparent_hugepage_flags __read_mostly
=
36 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
37 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
39 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
40 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
42 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
43 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
44 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
46 /* default scan 8*512 pte (or vmas) every 30 second */
47 static unsigned int khugepaged_pages_to_scan __read_mostly
= HPAGE_PMD_NR
*8;
48 static unsigned int khugepaged_pages_collapsed
;
49 static unsigned int khugepaged_full_scans
;
50 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
51 /* during fragmentation poll the hugepage allocator once every minute */
52 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
53 static struct task_struct
*khugepaged_thread __read_mostly
;
54 static DEFINE_MUTEX(khugepaged_mutex
);
55 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
56 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
58 * default collapse hugepages if there is at least one pte mapped like
59 * it would have happened if the vma was large enough during page
62 static unsigned int khugepaged_max_ptes_none __read_mostly
= HPAGE_PMD_NR
-1;
64 static int khugepaged(void *none
);
65 static int mm_slots_hash_init(void);
66 static int khugepaged_slab_init(void);
67 static void khugepaged_slab_free(void);
69 #define MM_SLOTS_HASH_HEADS 1024
70 static struct hlist_head
*mm_slots_hash __read_mostly
;
71 static struct kmem_cache
*mm_slot_cache __read_mostly
;
74 * struct mm_slot - hash lookup from mm to mm_slot
75 * @hash: hash collision list
76 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
77 * @mm: the mm that this information is valid for
80 struct hlist_node hash
;
81 struct list_head mm_node
;
86 * struct khugepaged_scan - cursor for scanning
87 * @mm_head: the head of the mm list to scan
88 * @mm_slot: the current mm_slot we are scanning
89 * @address: the next address inside that to be scanned
91 * There is only the one khugepaged_scan instance of this cursor structure.
93 struct khugepaged_scan
{
94 struct list_head mm_head
;
95 struct mm_slot
*mm_slot
;
96 unsigned long address
;
98 static struct khugepaged_scan khugepaged_scan
= {
99 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
103 static int set_recommended_min_free_kbytes(void)
107 unsigned long recommended_min
;
108 extern int min_free_kbytes
;
110 if (!khugepaged_enabled())
113 for_each_populated_zone(zone
)
116 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
117 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
120 * Make sure that on average at least two pageblocks are almost free
121 * of another type, one for a migratetype to fall back to and a
122 * second to avoid subsequent fallbacks of other types There are 3
123 * MIGRATE_TYPES we care about.
125 recommended_min
+= pageblock_nr_pages
* nr_zones
*
126 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
128 /* don't ever allow to reserve more than 5% of the lowmem */
129 recommended_min
= min(recommended_min
,
130 (unsigned long) nr_free_buffer_pages() / 20);
131 recommended_min
<<= (PAGE_SHIFT
-10);
133 if (recommended_min
> min_free_kbytes
)
134 min_free_kbytes
= recommended_min
;
135 setup_per_zone_wmarks();
138 late_initcall(set_recommended_min_free_kbytes
);
140 static int start_khugepaged(void)
143 if (khugepaged_enabled()) {
144 if (!khugepaged_thread
)
145 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
147 if (unlikely(IS_ERR(khugepaged_thread
))) {
149 "khugepaged: kthread_run(khugepaged) failed\n");
150 err
= PTR_ERR(khugepaged_thread
);
151 khugepaged_thread
= NULL
;
154 if (!list_empty(&khugepaged_scan
.mm_head
))
155 wake_up_interruptible(&khugepaged_wait
);
157 set_recommended_min_free_kbytes();
158 } else if (khugepaged_thread
) {
159 kthread_stop(khugepaged_thread
);
160 khugepaged_thread
= NULL
;
166 static atomic_t huge_zero_refcount
;
167 static unsigned long huge_zero_pfn __read_mostly
;
169 static inline bool is_huge_zero_pfn(unsigned long pfn
)
171 unsigned long zero_pfn
= ACCESS_ONCE(huge_zero_pfn
);
172 return zero_pfn
&& pfn
== zero_pfn
;
175 static inline bool is_huge_zero_pmd(pmd_t pmd
)
177 return is_huge_zero_pfn(pmd_pfn(pmd
));
180 static unsigned long get_huge_zero_page(void)
182 struct page
*zero_page
;
184 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
185 return ACCESS_ONCE(huge_zero_pfn
);
187 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
190 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
193 count_vm_event(THP_ZERO_PAGE_ALLOC
);
195 if (cmpxchg(&huge_zero_pfn
, 0, page_to_pfn(zero_page
))) {
197 __free_page(zero_page
);
201 /* We take additional reference here. It will be put back by shrinker */
202 atomic_set(&huge_zero_refcount
, 2);
204 return ACCESS_ONCE(huge_zero_pfn
);
207 static void put_huge_zero_page(void)
210 * Counter should never go to zero here. Only shrinker can put
213 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
216 static int shrink_huge_zero_page(struct shrinker
*shrink
,
217 struct shrink_control
*sc
)
220 /* we can free zero page only if last reference remains */
221 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
223 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
224 unsigned long zero_pfn
= xchg(&huge_zero_pfn
, 0);
225 BUG_ON(zero_pfn
== 0);
226 __free_page(__pfn_to_page(zero_pfn
));
232 static struct shrinker huge_zero_page_shrinker
= {
233 .shrink
= shrink_huge_zero_page
,
234 .seeks
= DEFAULT_SEEKS
,
239 static ssize_t
double_flag_show(struct kobject
*kobj
,
240 struct kobj_attribute
*attr
, char *buf
,
241 enum transparent_hugepage_flag enabled
,
242 enum transparent_hugepage_flag req_madv
)
244 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
245 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
246 return sprintf(buf
, "[always] madvise never\n");
247 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
248 return sprintf(buf
, "always [madvise] never\n");
250 return sprintf(buf
, "always madvise [never]\n");
252 static ssize_t
double_flag_store(struct kobject
*kobj
,
253 struct kobj_attribute
*attr
,
254 const char *buf
, size_t count
,
255 enum transparent_hugepage_flag enabled
,
256 enum transparent_hugepage_flag req_madv
)
258 if (!memcmp("always", buf
,
259 min(sizeof("always")-1, count
))) {
260 set_bit(enabled
, &transparent_hugepage_flags
);
261 clear_bit(req_madv
, &transparent_hugepage_flags
);
262 } else if (!memcmp("madvise", buf
,
263 min(sizeof("madvise")-1, count
))) {
264 clear_bit(enabled
, &transparent_hugepage_flags
);
265 set_bit(req_madv
, &transparent_hugepage_flags
);
266 } else if (!memcmp("never", buf
,
267 min(sizeof("never")-1, count
))) {
268 clear_bit(enabled
, &transparent_hugepage_flags
);
269 clear_bit(req_madv
, &transparent_hugepage_flags
);
276 static ssize_t
enabled_show(struct kobject
*kobj
,
277 struct kobj_attribute
*attr
, char *buf
)
279 return double_flag_show(kobj
, attr
, buf
,
280 TRANSPARENT_HUGEPAGE_FLAG
,
281 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
283 static ssize_t
enabled_store(struct kobject
*kobj
,
284 struct kobj_attribute
*attr
,
285 const char *buf
, size_t count
)
289 ret
= double_flag_store(kobj
, attr
, buf
, count
,
290 TRANSPARENT_HUGEPAGE_FLAG
,
291 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
296 mutex_lock(&khugepaged_mutex
);
297 err
= start_khugepaged();
298 mutex_unlock(&khugepaged_mutex
);
306 static struct kobj_attribute enabled_attr
=
307 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
309 static ssize_t
single_flag_show(struct kobject
*kobj
,
310 struct kobj_attribute
*attr
, char *buf
,
311 enum transparent_hugepage_flag flag
)
313 return sprintf(buf
, "%d\n",
314 !!test_bit(flag
, &transparent_hugepage_flags
));
317 static ssize_t
single_flag_store(struct kobject
*kobj
,
318 struct kobj_attribute
*attr
,
319 const char *buf
, size_t count
,
320 enum transparent_hugepage_flag flag
)
325 ret
= kstrtoul(buf
, 10, &value
);
332 set_bit(flag
, &transparent_hugepage_flags
);
334 clear_bit(flag
, &transparent_hugepage_flags
);
340 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
341 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
342 * memory just to allocate one more hugepage.
344 static ssize_t
defrag_show(struct kobject
*kobj
,
345 struct kobj_attribute
*attr
, char *buf
)
347 return double_flag_show(kobj
, attr
, buf
,
348 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
349 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
351 static ssize_t
defrag_store(struct kobject
*kobj
,
352 struct kobj_attribute
*attr
,
353 const char *buf
, size_t count
)
355 return double_flag_store(kobj
, attr
, buf
, count
,
356 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
357 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
359 static struct kobj_attribute defrag_attr
=
360 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
362 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
363 struct kobj_attribute
*attr
, char *buf
)
365 return single_flag_show(kobj
, attr
, buf
,
366 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
368 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
369 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
371 return single_flag_store(kobj
, attr
, buf
, count
,
372 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
374 static struct kobj_attribute use_zero_page_attr
=
375 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
376 #ifdef CONFIG_DEBUG_VM
377 static ssize_t
debug_cow_show(struct kobject
*kobj
,
378 struct kobj_attribute
*attr
, char *buf
)
380 return single_flag_show(kobj
, attr
, buf
,
381 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
383 static ssize_t
debug_cow_store(struct kobject
*kobj
,
384 struct kobj_attribute
*attr
,
385 const char *buf
, size_t count
)
387 return single_flag_store(kobj
, attr
, buf
, count
,
388 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
390 static struct kobj_attribute debug_cow_attr
=
391 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
392 #endif /* CONFIG_DEBUG_VM */
394 static struct attribute
*hugepage_attr
[] = {
397 &use_zero_page_attr
.attr
,
398 #ifdef CONFIG_DEBUG_VM
399 &debug_cow_attr
.attr
,
404 static struct attribute_group hugepage_attr_group
= {
405 .attrs
= hugepage_attr
,
408 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
409 struct kobj_attribute
*attr
,
412 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
415 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
416 struct kobj_attribute
*attr
,
417 const char *buf
, size_t count
)
422 err
= strict_strtoul(buf
, 10, &msecs
);
423 if (err
|| msecs
> UINT_MAX
)
426 khugepaged_scan_sleep_millisecs
= msecs
;
427 wake_up_interruptible(&khugepaged_wait
);
431 static struct kobj_attribute scan_sleep_millisecs_attr
=
432 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
433 scan_sleep_millisecs_store
);
435 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
436 struct kobj_attribute
*attr
,
439 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
442 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
443 struct kobj_attribute
*attr
,
444 const char *buf
, size_t count
)
449 err
= strict_strtoul(buf
, 10, &msecs
);
450 if (err
|| msecs
> UINT_MAX
)
453 khugepaged_alloc_sleep_millisecs
= msecs
;
454 wake_up_interruptible(&khugepaged_wait
);
458 static struct kobj_attribute alloc_sleep_millisecs_attr
=
459 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
460 alloc_sleep_millisecs_store
);
462 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
463 struct kobj_attribute
*attr
,
466 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
468 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
469 struct kobj_attribute
*attr
,
470 const char *buf
, size_t count
)
475 err
= strict_strtoul(buf
, 10, &pages
);
476 if (err
|| !pages
|| pages
> UINT_MAX
)
479 khugepaged_pages_to_scan
= pages
;
483 static struct kobj_attribute pages_to_scan_attr
=
484 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
485 pages_to_scan_store
);
487 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
488 struct kobj_attribute
*attr
,
491 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
493 static struct kobj_attribute pages_collapsed_attr
=
494 __ATTR_RO(pages_collapsed
);
496 static ssize_t
full_scans_show(struct kobject
*kobj
,
497 struct kobj_attribute
*attr
,
500 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
502 static struct kobj_attribute full_scans_attr
=
503 __ATTR_RO(full_scans
);
505 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
506 struct kobj_attribute
*attr
, char *buf
)
508 return single_flag_show(kobj
, attr
, buf
,
509 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
511 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
512 struct kobj_attribute
*attr
,
513 const char *buf
, size_t count
)
515 return single_flag_store(kobj
, attr
, buf
, count
,
516 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
518 static struct kobj_attribute khugepaged_defrag_attr
=
519 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
520 khugepaged_defrag_store
);
523 * max_ptes_none controls if khugepaged should collapse hugepages over
524 * any unmapped ptes in turn potentially increasing the memory
525 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
526 * reduce the available free memory in the system as it
527 * runs. Increasing max_ptes_none will instead potentially reduce the
528 * free memory in the system during the khugepaged scan.
530 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
531 struct kobj_attribute
*attr
,
534 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
536 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
537 struct kobj_attribute
*attr
,
538 const char *buf
, size_t count
)
541 unsigned long max_ptes_none
;
543 err
= strict_strtoul(buf
, 10, &max_ptes_none
);
544 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
547 khugepaged_max_ptes_none
= max_ptes_none
;
551 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
552 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
553 khugepaged_max_ptes_none_store
);
555 static struct attribute
*khugepaged_attr
[] = {
556 &khugepaged_defrag_attr
.attr
,
557 &khugepaged_max_ptes_none_attr
.attr
,
558 &pages_to_scan_attr
.attr
,
559 &pages_collapsed_attr
.attr
,
560 &full_scans_attr
.attr
,
561 &scan_sleep_millisecs_attr
.attr
,
562 &alloc_sleep_millisecs_attr
.attr
,
566 static struct attribute_group khugepaged_attr_group
= {
567 .attrs
= khugepaged_attr
,
568 .name
= "khugepaged",
571 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
575 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
576 if (unlikely(!*hugepage_kobj
)) {
577 printk(KERN_ERR
"hugepage: failed to create transparent hugepage kobject\n");
581 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
583 printk(KERN_ERR
"hugepage: failed to register transparent hugepage group\n");
587 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
589 printk(KERN_ERR
"hugepage: failed to register transparent hugepage group\n");
590 goto remove_hp_group
;
596 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
598 kobject_put(*hugepage_kobj
);
602 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
604 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
605 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
606 kobject_put(hugepage_kobj
);
609 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
614 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
617 #endif /* CONFIG_SYSFS */
619 static int __init
hugepage_init(void)
622 struct kobject
*hugepage_kobj
;
624 if (!has_transparent_hugepage()) {
625 transparent_hugepage_flags
= 0;
629 err
= hugepage_init_sysfs(&hugepage_kobj
);
633 err
= khugepaged_slab_init();
637 err
= mm_slots_hash_init();
639 khugepaged_slab_free();
643 register_shrinker(&huge_zero_page_shrinker
);
646 * By default disable transparent hugepages on smaller systems,
647 * where the extra memory used could hurt more than TLB overhead
648 * is likely to save. The admin can still enable it through /sys.
650 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
)))
651 transparent_hugepage_flags
= 0;
657 hugepage_exit_sysfs(hugepage_kobj
);
660 module_init(hugepage_init
)
662 static int __init
setup_transparent_hugepage(char *str
)
667 if (!strcmp(str
, "always")) {
668 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
669 &transparent_hugepage_flags
);
670 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
671 &transparent_hugepage_flags
);
673 } else if (!strcmp(str
, "madvise")) {
674 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
675 &transparent_hugepage_flags
);
676 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
677 &transparent_hugepage_flags
);
679 } else if (!strcmp(str
, "never")) {
680 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
681 &transparent_hugepage_flags
);
682 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
683 &transparent_hugepage_flags
);
689 "transparent_hugepage= cannot parse, ignored\n");
692 __setup("transparent_hugepage=", setup_transparent_hugepage
);
694 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
696 if (likely(vma
->vm_flags
& VM_WRITE
))
697 pmd
= pmd_mkwrite(pmd
);
701 static inline pmd_t
mk_huge_pmd(struct page
*page
, struct vm_area_struct
*vma
)
704 entry
= mk_pmd(page
, vma
->vm_page_prot
);
705 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
706 entry
= pmd_mkhuge(entry
);
710 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
711 struct vm_area_struct
*vma
,
712 unsigned long haddr
, pmd_t
*pmd
,
717 VM_BUG_ON(!PageCompound(page
));
718 pgtable
= pte_alloc_one(mm
, haddr
);
719 if (unlikely(!pgtable
))
722 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
723 __SetPageUptodate(page
);
725 spin_lock(&mm
->page_table_lock
);
726 if (unlikely(!pmd_none(*pmd
))) {
727 spin_unlock(&mm
->page_table_lock
);
728 mem_cgroup_uncharge_page(page
);
730 pte_free(mm
, pgtable
);
733 entry
= mk_huge_pmd(page
, vma
);
735 * The spinlocking to take the lru_lock inside
736 * page_add_new_anon_rmap() acts as a full memory
737 * barrier to be sure clear_huge_page writes become
738 * visible after the set_pmd_at() write.
740 page_add_new_anon_rmap(page
, vma
, haddr
);
741 set_pmd_at(mm
, haddr
, pmd
, entry
);
742 pgtable_trans_huge_deposit(mm
, pgtable
);
743 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
745 spin_unlock(&mm
->page_table_lock
);
751 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
753 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_WAIT
)) | extra_gfp
;
756 static inline struct page
*alloc_hugepage_vma(int defrag
,
757 struct vm_area_struct
*vma
,
758 unsigned long haddr
, int nd
,
761 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag
, extra_gfp
),
762 HPAGE_PMD_ORDER
, vma
, haddr
, nd
);
766 static inline struct page
*alloc_hugepage(int defrag
)
768 return alloc_pages(alloc_hugepage_gfpmask(defrag
, 0),
773 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
774 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
775 unsigned long zero_pfn
)
780 entry
= pfn_pmd(zero_pfn
, vma
->vm_page_prot
);
781 entry
= pmd_wrprotect(entry
);
782 entry
= pmd_mkhuge(entry
);
783 set_pmd_at(mm
, haddr
, pmd
, entry
);
784 pgtable_trans_huge_deposit(mm
, pgtable
);
789 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
790 unsigned long address
, pmd_t
*pmd
,
794 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
797 if (haddr
>= vma
->vm_start
&& haddr
+ HPAGE_PMD_SIZE
<= vma
->vm_end
) {
798 if (unlikely(anon_vma_prepare(vma
)))
800 if (unlikely(khugepaged_enter(vma
)))
802 if (!(flags
& FAULT_FLAG_WRITE
) &&
803 transparent_hugepage_use_zero_page()) {
805 unsigned long zero_pfn
;
807 pgtable
= pte_alloc_one(mm
, haddr
);
808 if (unlikely(!pgtable
))
810 zero_pfn
= get_huge_zero_page();
811 if (unlikely(!zero_pfn
)) {
812 pte_free(mm
, pgtable
);
813 count_vm_event(THP_FAULT_FALLBACK
);
816 spin_lock(&mm
->page_table_lock
);
817 set
= set_huge_zero_page(pgtable
, mm
, vma
, haddr
, pmd
,
819 spin_unlock(&mm
->page_table_lock
);
821 pte_free(mm
, pgtable
);
822 put_huge_zero_page();
826 page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
827 vma
, haddr
, numa_node_id(), 0);
828 if (unlikely(!page
)) {
829 count_vm_event(THP_FAULT_FALLBACK
);
832 count_vm_event(THP_FAULT_ALLOC
);
833 if (unlikely(mem_cgroup_newpage_charge(page
, mm
, GFP_KERNEL
))) {
837 if (unlikely(__do_huge_pmd_anonymous_page(mm
, vma
, haddr
, pmd
,
839 mem_cgroup_uncharge_page(page
);
848 * Use __pte_alloc instead of pte_alloc_map, because we can't
849 * run pte_offset_map on the pmd, if an huge pmd could
850 * materialize from under us from a different thread.
852 if (unlikely(pmd_none(*pmd
)) &&
853 unlikely(__pte_alloc(mm
, vma
, pmd
, address
)))
855 /* if an huge pmd materialized from under us just retry later */
856 if (unlikely(pmd_trans_huge(*pmd
)))
859 * A regular pmd is established and it can't morph into a huge pmd
860 * from under us anymore at this point because we hold the mmap_sem
861 * read mode and khugepaged takes it in write mode. So now it's
862 * safe to run pte_offset_map().
864 pte
= pte_offset_map(pmd
, address
);
865 return handle_pte_fault(mm
, vma
, address
, pte
, pmd
, flags
);
868 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
869 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
870 struct vm_area_struct
*vma
)
872 struct page
*src_page
;
878 pgtable
= pte_alloc_one(dst_mm
, addr
);
879 if (unlikely(!pgtable
))
882 spin_lock(&dst_mm
->page_table_lock
);
883 spin_lock_nested(&src_mm
->page_table_lock
, SINGLE_DEPTH_NESTING
);
887 if (unlikely(!pmd_trans_huge(pmd
))) {
888 pte_free(dst_mm
, pgtable
);
892 * mm->page_table_lock is enough to be sure that huge zero pmd is not
893 * under splitting since we don't split the page itself, only pmd to
896 if (is_huge_zero_pmd(pmd
)) {
897 unsigned long zero_pfn
;
900 * get_huge_zero_page() will never allocate a new page here,
901 * since we already have a zero page to copy. It just takes a
904 zero_pfn
= get_huge_zero_page();
905 set
= set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
907 BUG_ON(!set
); /* unexpected !pmd_none(dst_pmd) */
911 if (unlikely(pmd_trans_splitting(pmd
))) {
912 /* split huge page running from under us */
913 spin_unlock(&src_mm
->page_table_lock
);
914 spin_unlock(&dst_mm
->page_table_lock
);
915 pte_free(dst_mm
, pgtable
);
917 wait_split_huge_page(vma
->anon_vma
, src_pmd
); /* src_vma */
920 src_page
= pmd_page(pmd
);
921 VM_BUG_ON(!PageHead(src_page
));
923 page_dup_rmap(src_page
);
924 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
926 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
927 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
928 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
929 pgtable_trans_huge_deposit(dst_mm
, pgtable
);
934 spin_unlock(&src_mm
->page_table_lock
);
935 spin_unlock(&dst_mm
->page_table_lock
);
940 void huge_pmd_set_accessed(struct mm_struct
*mm
,
941 struct vm_area_struct
*vma
,
942 unsigned long address
,
943 pmd_t
*pmd
, pmd_t orig_pmd
,
949 spin_lock(&mm
->page_table_lock
);
950 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
953 entry
= pmd_mkyoung(orig_pmd
);
954 haddr
= address
& HPAGE_PMD_MASK
;
955 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
956 update_mmu_cache_pmd(vma
, address
, pmd
);
959 spin_unlock(&mm
->page_table_lock
);
962 static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct
*mm
,
963 struct vm_area_struct
*vma
, unsigned long address
,
964 pmd_t
*pmd
, pmd_t orig_pmd
, unsigned long haddr
)
970 unsigned long mmun_start
; /* For mmu_notifiers */
971 unsigned long mmun_end
; /* For mmu_notifiers */
973 page
= alloc_page_vma(GFP_HIGHUSER_MOVABLE
, vma
, address
);
979 if (mem_cgroup_newpage_charge(page
, mm
, GFP_KERNEL
)) {
985 clear_user_highpage(page
, address
);
986 __SetPageUptodate(page
);
989 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
990 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
992 spin_lock(&mm
->page_table_lock
);
993 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
996 pmdp_clear_flush(vma
, haddr
, pmd
);
997 /* leave pmd empty until pte is filled */
999 pgtable
= pgtable_trans_huge_withdraw(mm
);
1000 pmd_populate(mm
, &_pmd
, pgtable
);
1002 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1004 if (haddr
== (address
& PAGE_MASK
)) {
1005 entry
= mk_pte(page
, vma
->vm_page_prot
);
1006 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1007 page_add_new_anon_rmap(page
, vma
, haddr
);
1009 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
1010 entry
= pte_mkspecial(entry
);
1012 pte
= pte_offset_map(&_pmd
, haddr
);
1013 VM_BUG_ON(!pte_none(*pte
));
1014 set_pte_at(mm
, haddr
, pte
, entry
);
1017 smp_wmb(); /* make pte visible before pmd */
1018 pmd_populate(mm
, pmd
, pgtable
);
1019 spin_unlock(&mm
->page_table_lock
);
1020 put_huge_zero_page();
1021 inc_mm_counter(mm
, MM_ANONPAGES
);
1023 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1025 ret
|= VM_FAULT_WRITE
;
1029 spin_unlock(&mm
->page_table_lock
);
1030 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1031 mem_cgroup_uncharge_page(page
);
1036 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
1037 struct vm_area_struct
*vma
,
1038 unsigned long address
,
1039 pmd_t
*pmd
, pmd_t orig_pmd
,
1041 unsigned long haddr
)
1046 struct page
**pages
;
1047 unsigned long mmun_start
; /* For mmu_notifiers */
1048 unsigned long mmun_end
; /* For mmu_notifiers */
1050 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1052 if (unlikely(!pages
)) {
1053 ret
|= VM_FAULT_OOM
;
1057 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1058 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1060 vma
, address
, page_to_nid(page
));
1061 if (unlikely(!pages
[i
] ||
1062 mem_cgroup_newpage_charge(pages
[i
], mm
,
1066 mem_cgroup_uncharge_start();
1068 mem_cgroup_uncharge_page(pages
[i
]);
1071 mem_cgroup_uncharge_end();
1073 ret
|= VM_FAULT_OOM
;
1078 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1079 copy_user_highpage(pages
[i
], page
+ i
,
1080 haddr
+ PAGE_SIZE
* i
, vma
);
1081 __SetPageUptodate(pages
[i
]);
1086 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1087 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1089 spin_lock(&mm
->page_table_lock
);
1090 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1091 goto out_free_pages
;
1092 VM_BUG_ON(!PageHead(page
));
1094 pmdp_clear_flush(vma
, haddr
, pmd
);
1095 /* leave pmd empty until pte is filled */
1097 pgtable
= pgtable_trans_huge_withdraw(mm
);
1098 pmd_populate(mm
, &_pmd
, pgtable
);
1100 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1102 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1103 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1104 page_add_new_anon_rmap(pages
[i
], vma
, haddr
);
1105 pte
= pte_offset_map(&_pmd
, haddr
);
1106 VM_BUG_ON(!pte_none(*pte
));
1107 set_pte_at(mm
, haddr
, pte
, entry
);
1112 smp_wmb(); /* make pte visible before pmd */
1113 pmd_populate(mm
, pmd
, pgtable
);
1114 page_remove_rmap(page
);
1115 spin_unlock(&mm
->page_table_lock
);
1117 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1119 ret
|= VM_FAULT_WRITE
;
1126 spin_unlock(&mm
->page_table_lock
);
1127 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1128 mem_cgroup_uncharge_start();
1129 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1130 mem_cgroup_uncharge_page(pages
[i
]);
1133 mem_cgroup_uncharge_end();
1138 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1139 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
1142 struct page
*page
= NULL
, *new_page
;
1143 unsigned long haddr
;
1144 unsigned long mmun_start
; /* For mmu_notifiers */
1145 unsigned long mmun_end
; /* For mmu_notifiers */
1147 VM_BUG_ON(!vma
->anon_vma
);
1148 haddr
= address
& HPAGE_PMD_MASK
;
1149 if (is_huge_zero_pmd(orig_pmd
))
1151 spin_lock(&mm
->page_table_lock
);
1152 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1155 page
= pmd_page(orig_pmd
);
1156 VM_BUG_ON(!PageCompound(page
) || !PageHead(page
));
1157 if (page_mapcount(page
) == 1) {
1159 entry
= pmd_mkyoung(orig_pmd
);
1160 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1161 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
1162 update_mmu_cache_pmd(vma
, address
, pmd
);
1163 ret
|= VM_FAULT_WRITE
;
1167 spin_unlock(&mm
->page_table_lock
);
1169 if (transparent_hugepage_enabled(vma
) &&
1170 !transparent_hugepage_debug_cow())
1171 new_page
= alloc_hugepage_vma(transparent_hugepage_defrag(vma
),
1172 vma
, haddr
, numa_node_id(), 0);
1176 if (unlikely(!new_page
)) {
1177 count_vm_event(THP_FAULT_FALLBACK
);
1178 if (is_huge_zero_pmd(orig_pmd
)) {
1179 ret
= do_huge_pmd_wp_zero_page_fallback(mm
, vma
,
1180 address
, pmd
, orig_pmd
, haddr
);
1182 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
1183 pmd
, orig_pmd
, page
, haddr
);
1184 if (ret
& VM_FAULT_OOM
)
1185 split_huge_page(page
);
1190 count_vm_event(THP_FAULT_ALLOC
);
1192 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
))) {
1195 split_huge_page(page
);
1198 ret
|= VM_FAULT_OOM
;
1202 if (is_huge_zero_pmd(orig_pmd
))
1203 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1205 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1206 __SetPageUptodate(new_page
);
1209 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1210 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1212 spin_lock(&mm
->page_table_lock
);
1215 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
1216 spin_unlock(&mm
->page_table_lock
);
1217 mem_cgroup_uncharge_page(new_page
);
1222 entry
= mk_huge_pmd(new_page
, vma
);
1223 pmdp_clear_flush(vma
, haddr
, pmd
);
1224 page_add_new_anon_rmap(new_page
, vma
, haddr
);
1225 set_pmd_at(mm
, haddr
, pmd
, entry
);
1226 update_mmu_cache_pmd(vma
, address
, pmd
);
1227 if (is_huge_zero_pmd(orig_pmd
)) {
1228 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1229 put_huge_zero_page();
1231 VM_BUG_ON(!PageHead(page
));
1232 page_remove_rmap(page
);
1235 ret
|= VM_FAULT_WRITE
;
1237 spin_unlock(&mm
->page_table_lock
);
1239 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1243 spin_unlock(&mm
->page_table_lock
);
1247 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1252 struct mm_struct
*mm
= vma
->vm_mm
;
1253 struct page
*page
= NULL
;
1255 assert_spin_locked(&mm
->page_table_lock
);
1257 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1260 /* Avoid dumping huge zero page */
1261 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1262 return ERR_PTR(-EFAULT
);
1264 page
= pmd_page(*pmd
);
1265 VM_BUG_ON(!PageHead(page
));
1266 if (flags
& FOLL_TOUCH
) {
1269 * We should set the dirty bit only for FOLL_WRITE but
1270 * for now the dirty bit in the pmd is meaningless.
1271 * And if the dirty bit will become meaningful and
1272 * we'll only set it with FOLL_WRITE, an atomic
1273 * set_bit will be required on the pmd to set the
1274 * young bit, instead of the current set_pmd_at.
1276 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1277 set_pmd_at(mm
, addr
& HPAGE_PMD_MASK
, pmd
, _pmd
);
1279 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1280 if (page
->mapping
&& trylock_page(page
)) {
1283 mlock_vma_page(page
);
1287 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1288 VM_BUG_ON(!PageCompound(page
));
1289 if (flags
& FOLL_GET
)
1290 get_page_foll(page
);
1296 /* NUMA hinting page fault entry point for trans huge pmds */
1297 int do_huge_pmd_numa_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1298 unsigned long addr
, pmd_t pmd
, pmd_t
*pmdp
)
1301 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1303 int current_nid
= -1;
1305 bool page_locked
= false;
1307 spin_lock(&mm
->page_table_lock
);
1308 if (unlikely(!pmd_same(pmd
, *pmdp
)))
1311 page
= pmd_page(pmd
);
1313 current_nid
= page_to_nid(page
);
1314 count_vm_numa_event(NUMA_HINT_FAULTS
);
1315 if (current_nid
== numa_node_id())
1316 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1318 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1319 if (target_nid
== -1) {
1324 /* Acquire the page lock to serialise THP migrations */
1325 spin_unlock(&mm
->page_table_lock
);
1329 /* Confirm the PTE did not while locked */
1330 spin_lock(&mm
->page_table_lock
);
1331 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1336 spin_unlock(&mm
->page_table_lock
);
1338 /* Migrate the THP to the requested node */
1339 migrated
= migrate_misplaced_transhuge_page(mm
, vma
,
1343 current_nid
= target_nid
;
1345 spin_lock(&mm
->page_table_lock
);
1346 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1353 task_numa_fault(current_nid
, HPAGE_PMD_NR
, migrated
);
1357 pmd
= pmd_mknonnuma(pmd
);
1358 set_pmd_at(mm
, haddr
, pmdp
, pmd
);
1359 VM_BUG_ON(pmd_numa(*pmdp
));
1360 update_mmu_cache_pmd(vma
, addr
, pmdp
);
1365 spin_unlock(&mm
->page_table_lock
);
1366 if (current_nid
!= -1)
1367 task_numa_fault(current_nid
, HPAGE_PMD_NR
, migrated
);
1371 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1372 pmd_t
*pmd
, unsigned long addr
)
1376 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1380 pgtable
= pgtable_trans_huge_withdraw(tlb
->mm
);
1381 orig_pmd
= pmdp_get_and_clear(tlb
->mm
, addr
, pmd
);
1382 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1383 if (is_huge_zero_pmd(orig_pmd
)) {
1385 spin_unlock(&tlb
->mm
->page_table_lock
);
1386 put_huge_zero_page();
1388 page
= pmd_page(orig_pmd
);
1389 page_remove_rmap(page
);
1390 VM_BUG_ON(page_mapcount(page
) < 0);
1391 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1392 VM_BUG_ON(!PageHead(page
));
1394 spin_unlock(&tlb
->mm
->page_table_lock
);
1395 tlb_remove_page(tlb
, page
);
1397 pte_free(tlb
->mm
, pgtable
);
1403 int mincore_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1404 unsigned long addr
, unsigned long end
,
1409 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1411 * All logical pages in the range are present
1412 * if backed by a huge page.
1414 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1415 memset(vec
, 1, (end
- addr
) >> PAGE_SHIFT
);
1422 int move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1423 unsigned long old_addr
,
1424 unsigned long new_addr
, unsigned long old_end
,
1425 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1430 struct mm_struct
*mm
= vma
->vm_mm
;
1432 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1433 (new_addr
& ~HPAGE_PMD_MASK
) ||
1434 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1435 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1439 * The destination pmd shouldn't be established, free_pgtables()
1440 * should have release it.
1442 if (WARN_ON(!pmd_none(*new_pmd
))) {
1443 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1447 ret
= __pmd_trans_huge_lock(old_pmd
, vma
);
1449 pmd
= pmdp_get_and_clear(mm
, old_addr
, old_pmd
);
1450 VM_BUG_ON(!pmd_none(*new_pmd
));
1451 set_pmd_at(mm
, new_addr
, new_pmd
, pmd
);
1452 spin_unlock(&mm
->page_table_lock
);
1458 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1459 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1461 struct mm_struct
*mm
= vma
->vm_mm
;
1464 if (__pmd_trans_huge_lock(pmd
, vma
) == 1) {
1466 entry
= pmdp_get_and_clear(mm
, addr
, pmd
);
1468 entry
= pmd_modify(entry
, newprot
);
1469 BUG_ON(pmd_write(entry
));
1471 struct page
*page
= pmd_page(*pmd
);
1473 /* only check non-shared pages */
1474 if (page_mapcount(page
) == 1 &&
1476 entry
= pmd_mknuma(entry
);
1479 set_pmd_at(mm
, addr
, pmd
, entry
);
1480 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1488 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1489 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1491 * Note that if it returns 1, this routine returns without unlocking page
1492 * table locks. So callers must unlock them.
1494 int __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1496 spin_lock(&vma
->vm_mm
->page_table_lock
);
1497 if (likely(pmd_trans_huge(*pmd
))) {
1498 if (unlikely(pmd_trans_splitting(*pmd
))) {
1499 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1500 wait_split_huge_page(vma
->anon_vma
, pmd
);
1503 /* Thp mapped by 'pmd' is stable, so we can
1504 * handle it as it is. */
1508 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1512 pmd_t
*page_check_address_pmd(struct page
*page
,
1513 struct mm_struct
*mm
,
1514 unsigned long address
,
1515 enum page_check_address_pmd_flag flag
)
1517 pmd_t
*pmd
, *ret
= NULL
;
1519 if (address
& ~HPAGE_PMD_MASK
)
1522 pmd
= mm_find_pmd(mm
, address
);
1527 if (pmd_page(*pmd
) != page
)
1530 * split_vma() may create temporary aliased mappings. There is
1531 * no risk as long as all huge pmd are found and have their
1532 * splitting bit set before __split_huge_page_refcount
1533 * runs. Finding the same huge pmd more than once during the
1534 * same rmap walk is not a problem.
1536 if (flag
== PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
&&
1537 pmd_trans_splitting(*pmd
))
1539 if (pmd_trans_huge(*pmd
)) {
1540 VM_BUG_ON(flag
== PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
&&
1541 !pmd_trans_splitting(*pmd
));
1548 static int __split_huge_page_splitting(struct page
*page
,
1549 struct vm_area_struct
*vma
,
1550 unsigned long address
)
1552 struct mm_struct
*mm
= vma
->vm_mm
;
1555 /* For mmu_notifiers */
1556 const unsigned long mmun_start
= address
;
1557 const unsigned long mmun_end
= address
+ HPAGE_PMD_SIZE
;
1559 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1560 spin_lock(&mm
->page_table_lock
);
1561 pmd
= page_check_address_pmd(page
, mm
, address
,
1562 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
);
1565 * We can't temporarily set the pmd to null in order
1566 * to split it, the pmd must remain marked huge at all
1567 * times or the VM won't take the pmd_trans_huge paths
1568 * and it won't wait on the anon_vma->root->rwsem to
1569 * serialize against split_huge_page*.
1571 pmdp_splitting_flush(vma
, address
, pmd
);
1574 spin_unlock(&mm
->page_table_lock
);
1575 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1580 static void __split_huge_page_refcount(struct page
*page
)
1583 struct zone
*zone
= page_zone(page
);
1584 struct lruvec
*lruvec
;
1587 /* prevent PageLRU to go away from under us, and freeze lru stats */
1588 spin_lock_irq(&zone
->lru_lock
);
1589 lruvec
= mem_cgroup_page_lruvec(page
, zone
);
1591 compound_lock(page
);
1592 /* complete memcg works before add pages to LRU */
1593 mem_cgroup_split_huge_fixup(page
);
1595 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1596 struct page
*page_tail
= page
+ i
;
1598 /* tail_page->_mapcount cannot change */
1599 BUG_ON(page_mapcount(page_tail
) < 0);
1600 tail_count
+= page_mapcount(page_tail
);
1601 /* check for overflow */
1602 BUG_ON(tail_count
< 0);
1603 BUG_ON(atomic_read(&page_tail
->_count
) != 0);
1605 * tail_page->_count is zero and not changing from
1606 * under us. But get_page_unless_zero() may be running
1607 * from under us on the tail_page. If we used
1608 * atomic_set() below instead of atomic_add(), we
1609 * would then run atomic_set() concurrently with
1610 * get_page_unless_zero(), and atomic_set() is
1611 * implemented in C not using locked ops. spin_unlock
1612 * on x86 sometime uses locked ops because of PPro
1613 * errata 66, 92, so unless somebody can guarantee
1614 * atomic_set() here would be safe on all archs (and
1615 * not only on x86), it's safer to use atomic_add().
1617 atomic_add(page_mapcount(page
) + page_mapcount(page_tail
) + 1,
1618 &page_tail
->_count
);
1620 /* after clearing PageTail the gup refcount can be released */
1624 * retain hwpoison flag of the poisoned tail page:
1625 * fix for the unsuitable process killed on Guest Machine(KVM)
1626 * by the memory-failure.
1628 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
| __PG_HWPOISON
;
1629 page_tail
->flags
|= (page
->flags
&
1630 ((1L << PG_referenced
) |
1631 (1L << PG_swapbacked
) |
1632 (1L << PG_mlocked
) |
1633 (1L << PG_uptodate
)));
1634 page_tail
->flags
|= (1L << PG_dirty
);
1636 /* clear PageTail before overwriting first_page */
1640 * __split_huge_page_splitting() already set the
1641 * splitting bit in all pmd that could map this
1642 * hugepage, that will ensure no CPU can alter the
1643 * mapcount on the head page. The mapcount is only
1644 * accounted in the head page and it has to be
1645 * transferred to all tail pages in the below code. So
1646 * for this code to be safe, the split the mapcount
1647 * can't change. But that doesn't mean userland can't
1648 * keep changing and reading the page contents while
1649 * we transfer the mapcount, so the pmd splitting
1650 * status is achieved setting a reserved bit in the
1651 * pmd, not by clearing the present bit.
1653 page_tail
->_mapcount
= page
->_mapcount
;
1655 BUG_ON(page_tail
->mapping
);
1656 page_tail
->mapping
= page
->mapping
;
1658 page_tail
->index
= page
->index
+ i
;
1659 page_xchg_last_nid(page_tail
, page_last_nid(page
));
1661 BUG_ON(!PageAnon(page_tail
));
1662 BUG_ON(!PageUptodate(page_tail
));
1663 BUG_ON(!PageDirty(page_tail
));
1664 BUG_ON(!PageSwapBacked(page_tail
));
1666 lru_add_page_tail(page
, page_tail
, lruvec
);
1668 atomic_sub(tail_count
, &page
->_count
);
1669 BUG_ON(atomic_read(&page
->_count
) <= 0);
1671 __mod_zone_page_state(zone
, NR_ANON_TRANSPARENT_HUGEPAGES
, -1);
1672 __mod_zone_page_state(zone
, NR_ANON_PAGES
, HPAGE_PMD_NR
);
1674 ClearPageCompound(page
);
1675 compound_unlock(page
);
1676 spin_unlock_irq(&zone
->lru_lock
);
1678 for (i
= 1; i
< HPAGE_PMD_NR
; i
++) {
1679 struct page
*page_tail
= page
+ i
;
1680 BUG_ON(page_count(page_tail
) <= 0);
1682 * Tail pages may be freed if there wasn't any mapping
1683 * like if add_to_swap() is running on a lru page that
1684 * had its mapping zapped. And freeing these pages
1685 * requires taking the lru_lock so we do the put_page
1686 * of the tail pages after the split is complete.
1688 put_page(page_tail
);
1692 * Only the head page (now become a regular page) is required
1693 * to be pinned by the caller.
1695 BUG_ON(page_count(page
) <= 0);
1698 static int __split_huge_page_map(struct page
*page
,
1699 struct vm_area_struct
*vma
,
1700 unsigned long address
)
1702 struct mm_struct
*mm
= vma
->vm_mm
;
1706 unsigned long haddr
;
1708 spin_lock(&mm
->page_table_lock
);
1709 pmd
= page_check_address_pmd(page
, mm
, address
,
1710 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
);
1712 pgtable
= pgtable_trans_huge_withdraw(mm
);
1713 pmd_populate(mm
, &_pmd
, pgtable
);
1716 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1718 BUG_ON(PageCompound(page
+i
));
1719 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
1720 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1721 if (!pmd_write(*pmd
))
1722 entry
= pte_wrprotect(entry
);
1724 BUG_ON(page_mapcount(page
) != 1);
1725 if (!pmd_young(*pmd
))
1726 entry
= pte_mkold(entry
);
1728 entry
= pte_mknuma(entry
);
1729 pte
= pte_offset_map(&_pmd
, haddr
);
1730 BUG_ON(!pte_none(*pte
));
1731 set_pte_at(mm
, haddr
, pte
, entry
);
1735 smp_wmb(); /* make pte visible before pmd */
1737 * Up to this point the pmd is present and huge and
1738 * userland has the whole access to the hugepage
1739 * during the split (which happens in place). If we
1740 * overwrite the pmd with the not-huge version
1741 * pointing to the pte here (which of course we could
1742 * if all CPUs were bug free), userland could trigger
1743 * a small page size TLB miss on the small sized TLB
1744 * while the hugepage TLB entry is still established
1745 * in the huge TLB. Some CPU doesn't like that. See
1746 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1747 * Erratum 383 on page 93. Intel should be safe but is
1748 * also warns that it's only safe if the permission
1749 * and cache attributes of the two entries loaded in
1750 * the two TLB is identical (which should be the case
1751 * here). But it is generally safer to never allow
1752 * small and huge TLB entries for the same virtual
1753 * address to be loaded simultaneously. So instead of
1754 * doing "pmd_populate(); flush_tlb_range();" we first
1755 * mark the current pmd notpresent (atomically because
1756 * here the pmd_trans_huge and pmd_trans_splitting
1757 * must remain set at all times on the pmd until the
1758 * split is complete for this pmd), then we flush the
1759 * SMP TLB and finally we write the non-huge version
1760 * of the pmd entry with pmd_populate.
1762 pmdp_invalidate(vma
, address
, pmd
);
1763 pmd_populate(mm
, pmd
, pgtable
);
1766 spin_unlock(&mm
->page_table_lock
);
1771 /* must be called with anon_vma->root->rwsem held */
1772 static void __split_huge_page(struct page
*page
,
1773 struct anon_vma
*anon_vma
)
1775 int mapcount
, mapcount2
;
1776 pgoff_t pgoff
= page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
1777 struct anon_vma_chain
*avc
;
1779 BUG_ON(!PageHead(page
));
1780 BUG_ON(PageTail(page
));
1783 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1784 struct vm_area_struct
*vma
= avc
->vma
;
1785 unsigned long addr
= vma_address(page
, vma
);
1786 BUG_ON(is_vma_temporary_stack(vma
));
1787 mapcount
+= __split_huge_page_splitting(page
, vma
, addr
);
1790 * It is critical that new vmas are added to the tail of the
1791 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1792 * and establishes a child pmd before
1793 * __split_huge_page_splitting() freezes the parent pmd (so if
1794 * we fail to prevent copy_huge_pmd() from running until the
1795 * whole __split_huge_page() is complete), we will still see
1796 * the newly established pmd of the child later during the
1797 * walk, to be able to set it as pmd_trans_splitting too.
1799 if (mapcount
!= page_mapcount(page
))
1800 printk(KERN_ERR
"mapcount %d page_mapcount %d\n",
1801 mapcount
, page_mapcount(page
));
1802 BUG_ON(mapcount
!= page_mapcount(page
));
1804 __split_huge_page_refcount(page
);
1807 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1808 struct vm_area_struct
*vma
= avc
->vma
;
1809 unsigned long addr
= vma_address(page
, vma
);
1810 BUG_ON(is_vma_temporary_stack(vma
));
1811 mapcount2
+= __split_huge_page_map(page
, vma
, addr
);
1813 if (mapcount
!= mapcount2
)
1814 printk(KERN_ERR
"mapcount %d mapcount2 %d page_mapcount %d\n",
1815 mapcount
, mapcount2
, page_mapcount(page
));
1816 BUG_ON(mapcount
!= mapcount2
);
1819 int split_huge_page(struct page
*page
)
1821 struct anon_vma
*anon_vma
;
1824 BUG_ON(is_huge_zero_pfn(page_to_pfn(page
)));
1825 BUG_ON(!PageAnon(page
));
1828 * The caller does not necessarily hold an mmap_sem that would prevent
1829 * the anon_vma disappearing so we first we take a reference to it
1830 * and then lock the anon_vma for write. This is similar to
1831 * page_lock_anon_vma_read except the write lock is taken to serialise
1832 * against parallel split or collapse operations.
1834 anon_vma
= page_get_anon_vma(page
);
1837 anon_vma_lock_write(anon_vma
);
1840 if (!PageCompound(page
))
1843 BUG_ON(!PageSwapBacked(page
));
1844 __split_huge_page(page
, anon_vma
);
1845 count_vm_event(THP_SPLIT
);
1847 BUG_ON(PageCompound(page
));
1849 anon_vma_unlock(anon_vma
);
1850 put_anon_vma(anon_vma
);
1855 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1857 int hugepage_madvise(struct vm_area_struct
*vma
,
1858 unsigned long *vm_flags
, int advice
)
1860 struct mm_struct
*mm
= vma
->vm_mm
;
1865 * Be somewhat over-protective like KSM for now!
1867 if (*vm_flags
& (VM_HUGEPAGE
| VM_NO_THP
))
1869 if (mm
->def_flags
& VM_NOHUGEPAGE
)
1871 *vm_flags
&= ~VM_NOHUGEPAGE
;
1872 *vm_flags
|= VM_HUGEPAGE
;
1874 * If the vma become good for khugepaged to scan,
1875 * register it here without waiting a page fault that
1876 * may not happen any time soon.
1878 if (unlikely(khugepaged_enter_vma_merge(vma
)))
1881 case MADV_NOHUGEPAGE
:
1883 * Be somewhat over-protective like KSM for now!
1885 if (*vm_flags
& (VM_NOHUGEPAGE
| VM_NO_THP
))
1887 *vm_flags
&= ~VM_HUGEPAGE
;
1888 *vm_flags
|= VM_NOHUGEPAGE
;
1890 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1891 * this vma even if we leave the mm registered in khugepaged if
1892 * it got registered before VM_NOHUGEPAGE was set.
1900 static int __init
khugepaged_slab_init(void)
1902 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1903 sizeof(struct mm_slot
),
1904 __alignof__(struct mm_slot
), 0, NULL
);
1911 static void __init
khugepaged_slab_free(void)
1913 kmem_cache_destroy(mm_slot_cache
);
1914 mm_slot_cache
= NULL
;
1917 static inline struct mm_slot
*alloc_mm_slot(void)
1919 if (!mm_slot_cache
) /* initialization failed */
1921 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1924 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1926 kmem_cache_free(mm_slot_cache
, mm_slot
);
1929 static int __init
mm_slots_hash_init(void)
1931 mm_slots_hash
= kzalloc(MM_SLOTS_HASH_HEADS
* sizeof(struct hlist_head
),
1939 static void __init
mm_slots_hash_free(void)
1941 kfree(mm_slots_hash
);
1942 mm_slots_hash
= NULL
;
1946 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1948 struct mm_slot
*mm_slot
;
1949 struct hlist_head
*bucket
;
1950 struct hlist_node
*node
;
1952 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1953 % MM_SLOTS_HASH_HEADS
];
1954 hlist_for_each_entry(mm_slot
, node
, bucket
, hash
) {
1955 if (mm
== mm_slot
->mm
)
1961 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1962 struct mm_slot
*mm_slot
)
1964 struct hlist_head
*bucket
;
1966 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
1967 % MM_SLOTS_HASH_HEADS
];
1969 hlist_add_head(&mm_slot
->hash
, bucket
);
1972 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1974 return atomic_read(&mm
->mm_users
) == 0;
1977 int __khugepaged_enter(struct mm_struct
*mm
)
1979 struct mm_slot
*mm_slot
;
1982 mm_slot
= alloc_mm_slot();
1986 /* __khugepaged_exit() must not run from under us */
1987 VM_BUG_ON(khugepaged_test_exit(mm
));
1988 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1989 free_mm_slot(mm_slot
);
1993 spin_lock(&khugepaged_mm_lock
);
1994 insert_to_mm_slots_hash(mm
, mm_slot
);
1996 * Insert just behind the scanning cursor, to let the area settle
1999 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
2000 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
2001 spin_unlock(&khugepaged_mm_lock
);
2003 atomic_inc(&mm
->mm_count
);
2005 wake_up_interruptible(&khugepaged_wait
);
2010 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
)
2012 unsigned long hstart
, hend
;
2015 * Not yet faulted in so we will register later in the
2016 * page fault if needed.
2020 /* khugepaged not yet working on file or special mappings */
2022 VM_BUG_ON(vma
->vm_flags
& VM_NO_THP
);
2023 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2024 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2026 return khugepaged_enter(vma
);
2030 void __khugepaged_exit(struct mm_struct
*mm
)
2032 struct mm_slot
*mm_slot
;
2035 spin_lock(&khugepaged_mm_lock
);
2036 mm_slot
= get_mm_slot(mm
);
2037 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
2038 hlist_del(&mm_slot
->hash
);
2039 list_del(&mm_slot
->mm_node
);
2042 spin_unlock(&khugepaged_mm_lock
);
2045 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
2046 free_mm_slot(mm_slot
);
2048 } else if (mm_slot
) {
2050 * This is required to serialize against
2051 * khugepaged_test_exit() (which is guaranteed to run
2052 * under mmap sem read mode). Stop here (after we
2053 * return all pagetables will be destroyed) until
2054 * khugepaged has finished working on the pagetables
2055 * under the mmap_sem.
2057 down_write(&mm
->mmap_sem
);
2058 up_write(&mm
->mmap_sem
);
2062 static void release_pte_page(struct page
*page
)
2064 /* 0 stands for page_is_file_cache(page) == false */
2065 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2067 putback_lru_page(page
);
2070 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
2072 while (--_pte
>= pte
) {
2073 pte_t pteval
= *_pte
;
2074 if (!pte_none(pteval
))
2075 release_pte_page(pte_page(pteval
));
2079 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
2080 unsigned long address
,
2085 int referenced
= 0, none
= 0;
2086 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2087 _pte
++, address
+= PAGE_SIZE
) {
2088 pte_t pteval
= *_pte
;
2089 if (pte_none(pteval
)) {
2090 if (++none
<= khugepaged_max_ptes_none
)
2095 if (!pte_present(pteval
) || !pte_write(pteval
))
2097 page
= vm_normal_page(vma
, address
, pteval
);
2098 if (unlikely(!page
))
2101 VM_BUG_ON(PageCompound(page
));
2102 BUG_ON(!PageAnon(page
));
2103 VM_BUG_ON(!PageSwapBacked(page
));
2105 /* cannot use mapcount: can't collapse if there's a gup pin */
2106 if (page_count(page
) != 1)
2109 * We can do it before isolate_lru_page because the
2110 * page can't be freed from under us. NOTE: PG_lock
2111 * is needed to serialize against split_huge_page
2112 * when invoked from the VM.
2114 if (!trylock_page(page
))
2117 * Isolate the page to avoid collapsing an hugepage
2118 * currently in use by the VM.
2120 if (isolate_lru_page(page
)) {
2124 /* 0 stands for page_is_file_cache(page) == false */
2125 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2126 VM_BUG_ON(!PageLocked(page
));
2127 VM_BUG_ON(PageLRU(page
));
2129 /* If there is no mapped pte young don't collapse the page */
2130 if (pte_young(pteval
) || PageReferenced(page
) ||
2131 mmu_notifier_test_young(vma
->vm_mm
, address
))
2134 if (likely(referenced
))
2137 release_pte_pages(pte
, _pte
);
2141 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2142 struct vm_area_struct
*vma
,
2143 unsigned long address
,
2147 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2148 pte_t pteval
= *_pte
;
2149 struct page
*src_page
;
2151 if (pte_none(pteval
)) {
2152 clear_user_highpage(page
, address
);
2153 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2155 src_page
= pte_page(pteval
);
2156 copy_user_highpage(page
, src_page
, address
, vma
);
2157 VM_BUG_ON(page_mapcount(src_page
) != 1);
2158 release_pte_page(src_page
);
2160 * ptl mostly unnecessary, but preempt has to
2161 * be disabled to update the per-cpu stats
2162 * inside page_remove_rmap().
2166 * paravirt calls inside pte_clear here are
2169 pte_clear(vma
->vm_mm
, address
, _pte
);
2170 page_remove_rmap(src_page
);
2172 free_page_and_swap_cache(src_page
);
2175 address
+= PAGE_SIZE
;
2180 static void khugepaged_alloc_sleep(void)
2182 wait_event_freezable_timeout(khugepaged_wait
, false,
2183 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2187 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2189 if (IS_ERR(*hpage
)) {
2195 khugepaged_alloc_sleep();
2196 } else if (*hpage
) {
2205 *khugepaged_alloc_page(struct page
**hpage
, struct mm_struct
*mm
,
2206 struct vm_area_struct
*vma
, unsigned long address
,
2211 * Allocate the page while the vma is still valid and under
2212 * the mmap_sem read mode so there is no memory allocation
2213 * later when we take the mmap_sem in write mode. This is more
2214 * friendly behavior (OTOH it may actually hide bugs) to
2215 * filesystems in userland with daemons allocating memory in
2216 * the userland I/O paths. Allocating memory with the
2217 * mmap_sem in read mode is good idea also to allow greater
2220 *hpage
= alloc_hugepage_vma(khugepaged_defrag(), vma
, address
,
2221 node
, __GFP_OTHER_NODE
);
2224 * After allocating the hugepage, release the mmap_sem read lock in
2225 * preparation for taking it in write mode.
2227 up_read(&mm
->mmap_sem
);
2228 if (unlikely(!*hpage
)) {
2229 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2230 *hpage
= ERR_PTR(-ENOMEM
);
2234 count_vm_event(THP_COLLAPSE_ALLOC
);
2238 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2243 hpage
= alloc_hugepage(khugepaged_defrag());
2245 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2250 khugepaged_alloc_sleep();
2252 count_vm_event(THP_COLLAPSE_ALLOC
);
2253 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2258 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2261 *hpage
= khugepaged_alloc_hugepage(wait
);
2263 if (unlikely(!*hpage
))
2270 *khugepaged_alloc_page(struct page
**hpage
, struct mm_struct
*mm
,
2271 struct vm_area_struct
*vma
, unsigned long address
,
2274 up_read(&mm
->mmap_sem
);
2280 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2282 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2283 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2286 if (!vma
->anon_vma
|| vma
->vm_ops
)
2288 if (is_vma_temporary_stack(vma
))
2290 VM_BUG_ON(vma
->vm_flags
& VM_NO_THP
);
2294 static void collapse_huge_page(struct mm_struct
*mm
,
2295 unsigned long address
,
2296 struct page
**hpage
,
2297 struct vm_area_struct
*vma
,
2303 struct page
*new_page
;
2306 unsigned long hstart
, hend
;
2307 unsigned long mmun_start
; /* For mmu_notifiers */
2308 unsigned long mmun_end
; /* For mmu_notifiers */
2310 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2312 /* release the mmap_sem read lock. */
2313 new_page
= khugepaged_alloc_page(hpage
, mm
, vma
, address
, node
);
2317 if (unlikely(mem_cgroup_newpage_charge(new_page
, mm
, GFP_KERNEL
)))
2321 * Prevent all access to pagetables with the exception of
2322 * gup_fast later hanlded by the ptep_clear_flush and the VM
2323 * handled by the anon_vma lock + PG_lock.
2325 down_write(&mm
->mmap_sem
);
2326 if (unlikely(khugepaged_test_exit(mm
)))
2329 vma
= find_vma(mm
, address
);
2330 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2331 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2332 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
2334 if (!hugepage_vma_check(vma
))
2336 pmd
= mm_find_pmd(mm
, address
);
2339 if (pmd_trans_huge(*pmd
))
2342 anon_vma_lock_write(vma
->anon_vma
);
2344 pte
= pte_offset_map(pmd
, address
);
2345 ptl
= pte_lockptr(mm
, pmd
);
2347 mmun_start
= address
;
2348 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2349 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2350 spin_lock(&mm
->page_table_lock
); /* probably unnecessary */
2352 * After this gup_fast can't run anymore. This also removes
2353 * any huge TLB entry from the CPU so we won't allow
2354 * huge and small TLB entries for the same virtual address
2355 * to avoid the risk of CPU bugs in that area.
2357 _pmd
= pmdp_clear_flush(vma
, address
, pmd
);
2358 spin_unlock(&mm
->page_table_lock
);
2359 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2362 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2365 if (unlikely(!isolated
)) {
2367 spin_lock(&mm
->page_table_lock
);
2368 BUG_ON(!pmd_none(*pmd
));
2369 set_pmd_at(mm
, address
, pmd
, _pmd
);
2370 spin_unlock(&mm
->page_table_lock
);
2371 anon_vma_unlock(vma
->anon_vma
);
2376 * All pages are isolated and locked so anon_vma rmap
2377 * can't run anymore.
2379 anon_vma_unlock(vma
->anon_vma
);
2381 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, ptl
);
2383 __SetPageUptodate(new_page
);
2384 pgtable
= pmd_pgtable(_pmd
);
2386 _pmd
= mk_huge_pmd(new_page
, vma
);
2389 * spin_lock() below is not the equivalent of smp_wmb(), so
2390 * this is needed to avoid the copy_huge_page writes to become
2391 * visible after the set_pmd_at() write.
2395 spin_lock(&mm
->page_table_lock
);
2396 BUG_ON(!pmd_none(*pmd
));
2397 page_add_new_anon_rmap(new_page
, vma
, address
);
2398 set_pmd_at(mm
, address
, pmd
, _pmd
);
2399 update_mmu_cache_pmd(vma
, address
, pmd
);
2400 pgtable_trans_huge_deposit(mm
, pgtable
);
2401 spin_unlock(&mm
->page_table_lock
);
2405 khugepaged_pages_collapsed
++;
2407 up_write(&mm
->mmap_sem
);
2411 mem_cgroup_uncharge_page(new_page
);
2415 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2416 struct vm_area_struct
*vma
,
2417 unsigned long address
,
2418 struct page
**hpage
)
2422 int ret
= 0, referenced
= 0, none
= 0;
2424 unsigned long _address
;
2428 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2430 pmd
= mm_find_pmd(mm
, address
);
2433 if (pmd_trans_huge(*pmd
))
2436 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2437 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2438 _pte
++, _address
+= PAGE_SIZE
) {
2439 pte_t pteval
= *_pte
;
2440 if (pte_none(pteval
)) {
2441 if (++none
<= khugepaged_max_ptes_none
)
2446 if (!pte_present(pteval
) || !pte_write(pteval
))
2448 page
= vm_normal_page(vma
, _address
, pteval
);
2449 if (unlikely(!page
))
2452 * Chose the node of the first page. This could
2453 * be more sophisticated and look at more pages,
2454 * but isn't for now.
2457 node
= page_to_nid(page
);
2458 VM_BUG_ON(PageCompound(page
));
2459 if (!PageLRU(page
) || PageLocked(page
) || !PageAnon(page
))
2461 /* cannot use mapcount: can't collapse if there's a gup pin */
2462 if (page_count(page
) != 1)
2464 if (pte_young(pteval
) || PageReferenced(page
) ||
2465 mmu_notifier_test_young(vma
->vm_mm
, address
))
2471 pte_unmap_unlock(pte
, ptl
);
2473 /* collapse_huge_page will return with the mmap_sem released */
2474 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2479 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2481 struct mm_struct
*mm
= mm_slot
->mm
;
2483 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2485 if (khugepaged_test_exit(mm
)) {
2487 hlist_del(&mm_slot
->hash
);
2488 list_del(&mm_slot
->mm_node
);
2491 * Not strictly needed because the mm exited already.
2493 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2496 /* khugepaged_mm_lock actually not necessary for the below */
2497 free_mm_slot(mm_slot
);
2502 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2503 struct page
**hpage
)
2504 __releases(&khugepaged_mm_lock
)
2505 __acquires(&khugepaged_mm_lock
)
2507 struct mm_slot
*mm_slot
;
2508 struct mm_struct
*mm
;
2509 struct vm_area_struct
*vma
;
2513 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2515 if (khugepaged_scan
.mm_slot
)
2516 mm_slot
= khugepaged_scan
.mm_slot
;
2518 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2519 struct mm_slot
, mm_node
);
2520 khugepaged_scan
.address
= 0;
2521 khugepaged_scan
.mm_slot
= mm_slot
;
2523 spin_unlock(&khugepaged_mm_lock
);
2526 down_read(&mm
->mmap_sem
);
2527 if (unlikely(khugepaged_test_exit(mm
)))
2530 vma
= find_vma(mm
, khugepaged_scan
.address
);
2533 for (; vma
; vma
= vma
->vm_next
) {
2534 unsigned long hstart
, hend
;
2537 if (unlikely(khugepaged_test_exit(mm
))) {
2541 if (!hugepage_vma_check(vma
)) {
2546 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2547 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2550 if (khugepaged_scan
.address
> hend
)
2552 if (khugepaged_scan
.address
< hstart
)
2553 khugepaged_scan
.address
= hstart
;
2554 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2556 while (khugepaged_scan
.address
< hend
) {
2559 if (unlikely(khugepaged_test_exit(mm
)))
2560 goto breakouterloop
;
2562 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2563 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2565 ret
= khugepaged_scan_pmd(mm
, vma
,
2566 khugepaged_scan
.address
,
2568 /* move to next address */
2569 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2570 progress
+= HPAGE_PMD_NR
;
2572 /* we released mmap_sem so break loop */
2573 goto breakouterloop_mmap_sem
;
2574 if (progress
>= pages
)
2575 goto breakouterloop
;
2579 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2580 breakouterloop_mmap_sem
:
2582 spin_lock(&khugepaged_mm_lock
);
2583 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2585 * Release the current mm_slot if this mm is about to die, or
2586 * if we scanned all vmas of this mm.
2588 if (khugepaged_test_exit(mm
) || !vma
) {
2590 * Make sure that if mm_users is reaching zero while
2591 * khugepaged runs here, khugepaged_exit will find
2592 * mm_slot not pointing to the exiting mm.
2594 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2595 khugepaged_scan
.mm_slot
= list_entry(
2596 mm_slot
->mm_node
.next
,
2597 struct mm_slot
, mm_node
);
2598 khugepaged_scan
.address
= 0;
2600 khugepaged_scan
.mm_slot
= NULL
;
2601 khugepaged_full_scans
++;
2604 collect_mm_slot(mm_slot
);
2610 static int khugepaged_has_work(void)
2612 return !list_empty(&khugepaged_scan
.mm_head
) &&
2613 khugepaged_enabled();
2616 static int khugepaged_wait_event(void)
2618 return !list_empty(&khugepaged_scan
.mm_head
) ||
2619 kthread_should_stop();
2622 static void khugepaged_do_scan(void)
2624 struct page
*hpage
= NULL
;
2625 unsigned int progress
= 0, pass_through_head
= 0;
2626 unsigned int pages
= khugepaged_pages_to_scan
;
2629 barrier(); /* write khugepaged_pages_to_scan to local stack */
2631 while (progress
< pages
) {
2632 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2637 if (unlikely(kthread_should_stop() || freezing(current
)))
2640 spin_lock(&khugepaged_mm_lock
);
2641 if (!khugepaged_scan
.mm_slot
)
2642 pass_through_head
++;
2643 if (khugepaged_has_work() &&
2644 pass_through_head
< 2)
2645 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2649 spin_unlock(&khugepaged_mm_lock
);
2652 if (!IS_ERR_OR_NULL(hpage
))
2656 static void khugepaged_wait_work(void)
2660 if (khugepaged_has_work()) {
2661 if (!khugepaged_scan_sleep_millisecs
)
2664 wait_event_freezable_timeout(khugepaged_wait
,
2665 kthread_should_stop(),
2666 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2670 if (khugepaged_enabled())
2671 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2674 static int khugepaged(void *none
)
2676 struct mm_slot
*mm_slot
;
2679 set_user_nice(current
, 19);
2681 while (!kthread_should_stop()) {
2682 khugepaged_do_scan();
2683 khugepaged_wait_work();
2686 spin_lock(&khugepaged_mm_lock
);
2687 mm_slot
= khugepaged_scan
.mm_slot
;
2688 khugepaged_scan
.mm_slot
= NULL
;
2690 collect_mm_slot(mm_slot
);
2691 spin_unlock(&khugepaged_mm_lock
);
2695 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2696 unsigned long haddr
, pmd_t
*pmd
)
2698 struct mm_struct
*mm
= vma
->vm_mm
;
2703 pmdp_clear_flush(vma
, haddr
, pmd
);
2704 /* leave pmd empty until pte is filled */
2706 pgtable
= pgtable_trans_huge_withdraw(mm
);
2707 pmd_populate(mm
, &_pmd
, pgtable
);
2709 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2711 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2712 entry
= pte_mkspecial(entry
);
2713 pte
= pte_offset_map(&_pmd
, haddr
);
2714 VM_BUG_ON(!pte_none(*pte
));
2715 set_pte_at(mm
, haddr
, pte
, entry
);
2718 smp_wmb(); /* make pte visible before pmd */
2719 pmd_populate(mm
, pmd
, pgtable
);
2720 put_huge_zero_page();
2723 void __split_huge_page_pmd(struct vm_area_struct
*vma
, unsigned long address
,
2727 struct mm_struct
*mm
= vma
->vm_mm
;
2728 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
2729 unsigned long mmun_start
; /* For mmu_notifiers */
2730 unsigned long mmun_end
; /* For mmu_notifiers */
2732 BUG_ON(vma
->vm_start
> haddr
|| vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
);
2735 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
2736 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2737 spin_lock(&mm
->page_table_lock
);
2738 if (unlikely(!pmd_trans_huge(*pmd
))) {
2739 spin_unlock(&mm
->page_table_lock
);
2740 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2743 if (is_huge_zero_pmd(*pmd
)) {
2744 __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
2745 spin_unlock(&mm
->page_table_lock
);
2746 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2749 page
= pmd_page(*pmd
);
2750 VM_BUG_ON(!page_count(page
));
2752 spin_unlock(&mm
->page_table_lock
);
2753 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2755 split_huge_page(page
);
2758 BUG_ON(pmd_trans_huge(*pmd
));
2761 void split_huge_page_pmd_mm(struct mm_struct
*mm
, unsigned long address
,
2764 struct vm_area_struct
*vma
;
2766 vma
= find_vma(mm
, address
);
2767 BUG_ON(vma
== NULL
);
2768 split_huge_page_pmd(vma
, address
, pmd
);
2771 static void split_huge_page_address(struct mm_struct
*mm
,
2772 unsigned long address
)
2776 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
2778 pmd
= mm_find_pmd(mm
, address
);
2782 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2783 * materialize from under us.
2785 split_huge_page_pmd_mm(mm
, address
, pmd
);
2788 void __vma_adjust_trans_huge(struct vm_area_struct
*vma
,
2789 unsigned long start
,
2794 * If the new start address isn't hpage aligned and it could
2795 * previously contain an hugepage: check if we need to split
2798 if (start
& ~HPAGE_PMD_MASK
&&
2799 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2800 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2801 split_huge_page_address(vma
->vm_mm
, start
);
2804 * If the new end address isn't hpage aligned and it could
2805 * previously contain an hugepage: check if we need to split
2808 if (end
& ~HPAGE_PMD_MASK
&&
2809 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2810 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2811 split_huge_page_address(vma
->vm_mm
, end
);
2814 * If we're also updating the vma->vm_next->vm_start, if the new
2815 * vm_next->vm_start isn't page aligned and it could previously
2816 * contain an hugepage: check if we need to split an huge pmd.
2818 if (adjust_next
> 0) {
2819 struct vm_area_struct
*next
= vma
->vm_next
;
2820 unsigned long nstart
= next
->vm_start
;
2821 nstart
+= adjust_next
<< PAGE_SHIFT
;
2822 if (nstart
& ~HPAGE_PMD_MASK
&&
2823 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
2824 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
2825 split_huge_page_address(next
->vm_mm
, nstart
);