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.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/sched.h>
12 #include <linux/highmem.h>
13 #include <linux/hugetlb.h>
14 #include <linux/mmu_notifier.h>
15 #include <linux/rmap.h>
16 #include <linux/swap.h>
17 #include <linux/shrinker.h>
18 #include <linux/mm_inline.h>
19 #include <linux/swapops.h>
20 #include <linux/dax.h>
21 #include <linux/kthread.h>
22 #include <linux/khugepaged.h>
23 #include <linux/freezer.h>
24 #include <linux/pfn_t.h>
25 #include <linux/mman.h>
26 #include <linux/memremap.h>
27 #include <linux/pagemap.h>
28 #include <linux/debugfs.h>
29 #include <linux/migrate.h>
30 #include <linux/hashtable.h>
31 #include <linux/userfaultfd_k.h>
32 #include <linux/page_idle.h>
35 #include <asm/pgalloc.h>
45 SCAN_NO_REFERENCED_PAGE
,
59 SCAN_ALLOC_HUGE_PAGE_FAIL
,
60 SCAN_CGROUP_CHARGE_FAIL
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/huge_memory.h>
67 * By default transparent hugepage support is disabled in order that avoid
68 * to risk increase the memory footprint of applications without a guaranteed
69 * benefit. When transparent hugepage support is enabled, is for all mappings,
70 * and khugepaged scans all mappings.
71 * Defrag is invoked by khugepaged hugepage allocations and by page faults
72 * for all hugepage allocations.
74 unsigned long transparent_hugepage_flags __read_mostly
=
75 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
76 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
78 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
79 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
81 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
82 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
83 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
85 /* default scan 8*512 pte (or vmas) every 30 second */
86 static unsigned int khugepaged_pages_to_scan __read_mostly
;
87 static unsigned int khugepaged_pages_collapsed
;
88 static unsigned int khugepaged_full_scans
;
89 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
90 /* during fragmentation poll the hugepage allocator once every minute */
91 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
92 static struct task_struct
*khugepaged_thread __read_mostly
;
93 static DEFINE_MUTEX(khugepaged_mutex
);
94 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
95 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
97 * default collapse hugepages if there is at least one pte mapped like
98 * it would have happened if the vma was large enough during page
101 static unsigned int khugepaged_max_ptes_none __read_mostly
;
103 static int khugepaged(void *none
);
104 static int khugepaged_slab_init(void);
105 static void khugepaged_slab_exit(void);
107 #define MM_SLOTS_HASH_BITS 10
108 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
110 static struct kmem_cache
*mm_slot_cache __read_mostly
;
113 * struct mm_slot - hash lookup from mm to mm_slot
114 * @hash: hash collision list
115 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
116 * @mm: the mm that this information is valid for
119 struct hlist_node hash
;
120 struct list_head mm_node
;
121 struct mm_struct
*mm
;
125 * struct khugepaged_scan - cursor for scanning
126 * @mm_head: the head of the mm list to scan
127 * @mm_slot: the current mm_slot we are scanning
128 * @address: the next address inside that to be scanned
130 * There is only the one khugepaged_scan instance of this cursor structure.
132 struct khugepaged_scan
{
133 struct list_head mm_head
;
134 struct mm_slot
*mm_slot
;
135 unsigned long address
;
137 static struct khugepaged_scan khugepaged_scan
= {
138 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
141 static struct shrinker deferred_split_shrinker
;
143 static void set_recommended_min_free_kbytes(void)
147 unsigned long recommended_min
;
149 for_each_populated_zone(zone
)
152 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
153 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
156 * Make sure that on average at least two pageblocks are almost free
157 * of another type, one for a migratetype to fall back to and a
158 * second to avoid subsequent fallbacks of other types There are 3
159 * MIGRATE_TYPES we care about.
161 recommended_min
+= pageblock_nr_pages
* nr_zones
*
162 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
164 /* don't ever allow to reserve more than 5% of the lowmem */
165 recommended_min
= min(recommended_min
,
166 (unsigned long) nr_free_buffer_pages() / 20);
167 recommended_min
<<= (PAGE_SHIFT
-10);
169 if (recommended_min
> min_free_kbytes
) {
170 if (user_min_free_kbytes
>= 0)
171 pr_info("raising min_free_kbytes from %d to %lu "
172 "to help transparent hugepage allocations\n",
173 min_free_kbytes
, recommended_min
);
175 min_free_kbytes
= recommended_min
;
177 setup_per_zone_wmarks();
180 static int start_stop_khugepaged(void)
183 if (khugepaged_enabled()) {
184 if (!khugepaged_thread
)
185 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
187 if (IS_ERR(khugepaged_thread
)) {
188 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
189 err
= PTR_ERR(khugepaged_thread
);
190 khugepaged_thread
= NULL
;
194 if (!list_empty(&khugepaged_scan
.mm_head
))
195 wake_up_interruptible(&khugepaged_wait
);
197 set_recommended_min_free_kbytes();
198 } else if (khugepaged_thread
) {
199 kthread_stop(khugepaged_thread
);
200 khugepaged_thread
= NULL
;
206 static atomic_t huge_zero_refcount
;
207 struct page
*huge_zero_page __read_mostly
;
209 struct page
*get_huge_zero_page(void)
211 struct page
*zero_page
;
213 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
214 return READ_ONCE(huge_zero_page
);
216 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
219 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
222 count_vm_event(THP_ZERO_PAGE_ALLOC
);
224 if (cmpxchg(&huge_zero_page
, NULL
, zero_page
)) {
226 __free_pages(zero_page
, compound_order(zero_page
));
230 /* We take additional reference here. It will be put back by shrinker */
231 atomic_set(&huge_zero_refcount
, 2);
233 return READ_ONCE(huge_zero_page
);
236 static void put_huge_zero_page(void)
239 * Counter should never go to zero here. Only shrinker can put
242 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
245 static unsigned long shrink_huge_zero_page_count(struct shrinker
*shrink
,
246 struct shrink_control
*sc
)
248 /* we can free zero page only if last reference remains */
249 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
252 static unsigned long shrink_huge_zero_page_scan(struct shrinker
*shrink
,
253 struct shrink_control
*sc
)
255 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
256 struct page
*zero_page
= xchg(&huge_zero_page
, NULL
);
257 BUG_ON(zero_page
== NULL
);
258 __free_pages(zero_page
, compound_order(zero_page
));
265 static struct shrinker huge_zero_page_shrinker
= {
266 .count_objects
= shrink_huge_zero_page_count
,
267 .scan_objects
= shrink_huge_zero_page_scan
,
268 .seeks
= DEFAULT_SEEKS
,
273 static ssize_t
double_flag_show(struct kobject
*kobj
,
274 struct kobj_attribute
*attr
, char *buf
,
275 enum transparent_hugepage_flag enabled
,
276 enum transparent_hugepage_flag req_madv
)
278 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
279 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
280 return sprintf(buf
, "[always] madvise never\n");
281 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
282 return sprintf(buf
, "always [madvise] never\n");
284 return sprintf(buf
, "always madvise [never]\n");
286 static ssize_t
double_flag_store(struct kobject
*kobj
,
287 struct kobj_attribute
*attr
,
288 const char *buf
, size_t count
,
289 enum transparent_hugepage_flag enabled
,
290 enum transparent_hugepage_flag req_madv
)
292 if (!memcmp("always", buf
,
293 min(sizeof("always")-1, count
))) {
294 set_bit(enabled
, &transparent_hugepage_flags
);
295 clear_bit(req_madv
, &transparent_hugepage_flags
);
296 } else if (!memcmp("madvise", buf
,
297 min(sizeof("madvise")-1, count
))) {
298 clear_bit(enabled
, &transparent_hugepage_flags
);
299 set_bit(req_madv
, &transparent_hugepage_flags
);
300 } else if (!memcmp("never", buf
,
301 min(sizeof("never")-1, count
))) {
302 clear_bit(enabled
, &transparent_hugepage_flags
);
303 clear_bit(req_madv
, &transparent_hugepage_flags
);
310 static ssize_t
enabled_show(struct kobject
*kobj
,
311 struct kobj_attribute
*attr
, char *buf
)
313 return double_flag_show(kobj
, attr
, buf
,
314 TRANSPARENT_HUGEPAGE_FLAG
,
315 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
317 static ssize_t
enabled_store(struct kobject
*kobj
,
318 struct kobj_attribute
*attr
,
319 const char *buf
, size_t count
)
323 ret
= double_flag_store(kobj
, attr
, buf
, count
,
324 TRANSPARENT_HUGEPAGE_FLAG
,
325 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
330 mutex_lock(&khugepaged_mutex
);
331 err
= start_stop_khugepaged();
332 mutex_unlock(&khugepaged_mutex
);
340 static struct kobj_attribute enabled_attr
=
341 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
343 static ssize_t
single_flag_show(struct kobject
*kobj
,
344 struct kobj_attribute
*attr
, char *buf
,
345 enum transparent_hugepage_flag flag
)
347 return sprintf(buf
, "%d\n",
348 !!test_bit(flag
, &transparent_hugepage_flags
));
351 static ssize_t
single_flag_store(struct kobject
*kobj
,
352 struct kobj_attribute
*attr
,
353 const char *buf
, size_t count
,
354 enum transparent_hugepage_flag flag
)
359 ret
= kstrtoul(buf
, 10, &value
);
366 set_bit(flag
, &transparent_hugepage_flags
);
368 clear_bit(flag
, &transparent_hugepage_flags
);
374 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
375 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
376 * memory just to allocate one more hugepage.
378 static ssize_t
defrag_show(struct kobject
*kobj
,
379 struct kobj_attribute
*attr
, char *buf
)
381 return double_flag_show(kobj
, attr
, buf
,
382 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
383 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
385 static ssize_t
defrag_store(struct kobject
*kobj
,
386 struct kobj_attribute
*attr
,
387 const char *buf
, size_t count
)
389 return double_flag_store(kobj
, attr
, buf
, count
,
390 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
391 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
393 static struct kobj_attribute defrag_attr
=
394 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
396 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
397 struct kobj_attribute
*attr
, char *buf
)
399 return single_flag_show(kobj
, attr
, buf
,
400 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
402 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
403 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
405 return single_flag_store(kobj
, attr
, buf
, count
,
406 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
408 static struct kobj_attribute use_zero_page_attr
=
409 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
410 #ifdef CONFIG_DEBUG_VM
411 static ssize_t
debug_cow_show(struct kobject
*kobj
,
412 struct kobj_attribute
*attr
, char *buf
)
414 return single_flag_show(kobj
, attr
, buf
,
415 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
417 static ssize_t
debug_cow_store(struct kobject
*kobj
,
418 struct kobj_attribute
*attr
,
419 const char *buf
, size_t count
)
421 return single_flag_store(kobj
, attr
, buf
, count
,
422 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
424 static struct kobj_attribute debug_cow_attr
=
425 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
426 #endif /* CONFIG_DEBUG_VM */
428 static struct attribute
*hugepage_attr
[] = {
431 &use_zero_page_attr
.attr
,
432 #ifdef CONFIG_DEBUG_VM
433 &debug_cow_attr
.attr
,
438 static struct attribute_group hugepage_attr_group
= {
439 .attrs
= hugepage_attr
,
442 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
443 struct kobj_attribute
*attr
,
446 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
449 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
450 struct kobj_attribute
*attr
,
451 const char *buf
, size_t count
)
456 err
= kstrtoul(buf
, 10, &msecs
);
457 if (err
|| msecs
> UINT_MAX
)
460 khugepaged_scan_sleep_millisecs
= msecs
;
461 wake_up_interruptible(&khugepaged_wait
);
465 static struct kobj_attribute scan_sleep_millisecs_attr
=
466 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
467 scan_sleep_millisecs_store
);
469 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
470 struct kobj_attribute
*attr
,
473 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
476 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
477 struct kobj_attribute
*attr
,
478 const char *buf
, size_t count
)
483 err
= kstrtoul(buf
, 10, &msecs
);
484 if (err
|| msecs
> UINT_MAX
)
487 khugepaged_alloc_sleep_millisecs
= msecs
;
488 wake_up_interruptible(&khugepaged_wait
);
492 static struct kobj_attribute alloc_sleep_millisecs_attr
=
493 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
494 alloc_sleep_millisecs_store
);
496 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
497 struct kobj_attribute
*attr
,
500 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
502 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
503 struct kobj_attribute
*attr
,
504 const char *buf
, size_t count
)
509 err
= kstrtoul(buf
, 10, &pages
);
510 if (err
|| !pages
|| pages
> UINT_MAX
)
513 khugepaged_pages_to_scan
= pages
;
517 static struct kobj_attribute pages_to_scan_attr
=
518 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
519 pages_to_scan_store
);
521 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
522 struct kobj_attribute
*attr
,
525 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
527 static struct kobj_attribute pages_collapsed_attr
=
528 __ATTR_RO(pages_collapsed
);
530 static ssize_t
full_scans_show(struct kobject
*kobj
,
531 struct kobj_attribute
*attr
,
534 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
536 static struct kobj_attribute full_scans_attr
=
537 __ATTR_RO(full_scans
);
539 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
540 struct kobj_attribute
*attr
, char *buf
)
542 return single_flag_show(kobj
, attr
, buf
,
543 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
545 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
546 struct kobj_attribute
*attr
,
547 const char *buf
, size_t count
)
549 return single_flag_store(kobj
, attr
, buf
, count
,
550 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
552 static struct kobj_attribute khugepaged_defrag_attr
=
553 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
554 khugepaged_defrag_store
);
557 * max_ptes_none controls if khugepaged should collapse hugepages over
558 * any unmapped ptes in turn potentially increasing the memory
559 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
560 * reduce the available free memory in the system as it
561 * runs. Increasing max_ptes_none will instead potentially reduce the
562 * free memory in the system during the khugepaged scan.
564 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
565 struct kobj_attribute
*attr
,
568 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
570 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
571 struct kobj_attribute
*attr
,
572 const char *buf
, size_t count
)
575 unsigned long max_ptes_none
;
577 err
= kstrtoul(buf
, 10, &max_ptes_none
);
578 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
581 khugepaged_max_ptes_none
= max_ptes_none
;
585 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
586 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
587 khugepaged_max_ptes_none_store
);
589 static struct attribute
*khugepaged_attr
[] = {
590 &khugepaged_defrag_attr
.attr
,
591 &khugepaged_max_ptes_none_attr
.attr
,
592 &pages_to_scan_attr
.attr
,
593 &pages_collapsed_attr
.attr
,
594 &full_scans_attr
.attr
,
595 &scan_sleep_millisecs_attr
.attr
,
596 &alloc_sleep_millisecs_attr
.attr
,
600 static struct attribute_group khugepaged_attr_group
= {
601 .attrs
= khugepaged_attr
,
602 .name
= "khugepaged",
605 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
609 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
610 if (unlikely(!*hugepage_kobj
)) {
611 pr_err("failed to create transparent hugepage kobject\n");
615 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
617 pr_err("failed to register transparent hugepage group\n");
621 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
623 pr_err("failed to register transparent hugepage group\n");
624 goto remove_hp_group
;
630 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
632 kobject_put(*hugepage_kobj
);
636 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
638 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
639 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
640 kobject_put(hugepage_kobj
);
643 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
648 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
651 #endif /* CONFIG_SYSFS */
653 static int __init
hugepage_init(void)
656 struct kobject
*hugepage_kobj
;
658 if (!has_transparent_hugepage()) {
659 transparent_hugepage_flags
= 0;
663 khugepaged_pages_to_scan
= HPAGE_PMD_NR
* 8;
664 khugepaged_max_ptes_none
= HPAGE_PMD_NR
- 1;
666 * hugepages can't be allocated by the buddy allocator
668 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
>= MAX_ORDER
);
670 * we use page->mapping and page->index in second tail page
671 * as list_head: assuming THP order >= 2
673 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
< 2);
675 err
= hugepage_init_sysfs(&hugepage_kobj
);
679 err
= khugepaged_slab_init();
683 err
= register_shrinker(&huge_zero_page_shrinker
);
685 goto err_hzp_shrinker
;
686 err
= register_shrinker(&deferred_split_shrinker
);
688 goto err_split_shrinker
;
691 * By default disable transparent hugepages on smaller systems,
692 * where the extra memory used could hurt more than TLB overhead
693 * is likely to save. The admin can still enable it through /sys.
695 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
696 transparent_hugepage_flags
= 0;
700 err
= start_stop_khugepaged();
706 unregister_shrinker(&deferred_split_shrinker
);
708 unregister_shrinker(&huge_zero_page_shrinker
);
710 khugepaged_slab_exit();
712 hugepage_exit_sysfs(hugepage_kobj
);
716 subsys_initcall(hugepage_init
);
718 static int __init
setup_transparent_hugepage(char *str
)
723 if (!strcmp(str
, "always")) {
724 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
725 &transparent_hugepage_flags
);
726 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
727 &transparent_hugepage_flags
);
729 } else if (!strcmp(str
, "madvise")) {
730 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
731 &transparent_hugepage_flags
);
732 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
733 &transparent_hugepage_flags
);
735 } else if (!strcmp(str
, "never")) {
736 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
737 &transparent_hugepage_flags
);
738 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
739 &transparent_hugepage_flags
);
744 pr_warn("transparent_hugepage= cannot parse, ignored\n");
747 __setup("transparent_hugepage=", setup_transparent_hugepage
);
749 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
751 if (likely(vma
->vm_flags
& VM_WRITE
))
752 pmd
= pmd_mkwrite(pmd
);
756 static inline pmd_t
mk_huge_pmd(struct page
*page
, pgprot_t prot
)
759 entry
= mk_pmd(page
, prot
);
760 entry
= pmd_mkhuge(entry
);
764 static inline struct list_head
*page_deferred_list(struct page
*page
)
767 * ->lru in the tail pages is occupied by compound_head.
768 * Let's use ->mapping + ->index in the second tail page as list_head.
770 return (struct list_head
*)&page
[2].mapping
;
773 void prep_transhuge_page(struct page
*page
)
776 * we use page->mapping and page->indexlru in second tail page
777 * as list_head: assuming THP order >= 2
780 INIT_LIST_HEAD(page_deferred_list(page
));
781 set_compound_page_dtor(page
, TRANSHUGE_PAGE_DTOR
);
784 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
785 struct vm_area_struct
*vma
,
786 unsigned long address
, pmd_t
*pmd
,
787 struct page
*page
, gfp_t gfp
,
790 struct mem_cgroup
*memcg
;
793 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
795 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
797 if (mem_cgroup_try_charge(page
, mm
, gfp
, &memcg
, true)) {
799 count_vm_event(THP_FAULT_FALLBACK
);
800 return VM_FAULT_FALLBACK
;
803 pgtable
= pte_alloc_one(mm
, haddr
);
804 if (unlikely(!pgtable
)) {
805 mem_cgroup_cancel_charge(page
, memcg
, true);
810 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
812 * The memory barrier inside __SetPageUptodate makes sure that
813 * clear_huge_page writes become visible before the set_pmd_at()
816 __SetPageUptodate(page
);
818 ptl
= pmd_lock(mm
, pmd
);
819 if (unlikely(!pmd_none(*pmd
))) {
821 mem_cgroup_cancel_charge(page
, memcg
, true);
823 pte_free(mm
, pgtable
);
827 /* Deliver the page fault to userland */
828 if (userfaultfd_missing(vma
)) {
832 mem_cgroup_cancel_charge(page
, memcg
, true);
834 pte_free(mm
, pgtable
);
835 ret
= handle_userfault(vma
, address
, flags
,
837 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
841 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
842 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
843 page_add_new_anon_rmap(page
, vma
, haddr
, true);
844 mem_cgroup_commit_charge(page
, memcg
, false, true);
845 lru_cache_add_active_or_unevictable(page
, vma
);
846 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
847 set_pmd_at(mm
, haddr
, pmd
, entry
);
848 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
849 atomic_long_inc(&mm
->nr_ptes
);
851 count_vm_event(THP_FAULT_ALLOC
);
857 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
859 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_RECLAIM
)) | extra_gfp
;
862 /* Caller must hold page table lock. */
863 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
864 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
865 struct page
*zero_page
)
870 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
871 entry
= pmd_mkhuge(entry
);
873 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
874 set_pmd_at(mm
, haddr
, pmd
, entry
);
875 atomic_long_inc(&mm
->nr_ptes
);
879 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
880 unsigned long address
, pmd_t
*pmd
,
885 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
887 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
888 return VM_FAULT_FALLBACK
;
889 if (unlikely(anon_vma_prepare(vma
)))
891 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
893 if (!(flags
& FAULT_FLAG_WRITE
) && !mm_forbids_zeropage(mm
) &&
894 transparent_hugepage_use_zero_page()) {
897 struct page
*zero_page
;
900 pgtable
= pte_alloc_one(mm
, haddr
);
901 if (unlikely(!pgtable
))
903 zero_page
= get_huge_zero_page();
904 if (unlikely(!zero_page
)) {
905 pte_free(mm
, pgtable
);
906 count_vm_event(THP_FAULT_FALLBACK
);
907 return VM_FAULT_FALLBACK
;
909 ptl
= pmd_lock(mm
, pmd
);
912 if (pmd_none(*pmd
)) {
913 if (userfaultfd_missing(vma
)) {
915 ret
= handle_userfault(vma
, address
, flags
,
917 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
919 set_huge_zero_page(pgtable
, mm
, vma
,
928 pte_free(mm
, pgtable
);
929 put_huge_zero_page();
933 gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
934 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
935 if (unlikely(!page
)) {
936 count_vm_event(THP_FAULT_FALLBACK
);
937 return VM_FAULT_FALLBACK
;
939 prep_transhuge_page(page
);
940 return __do_huge_pmd_anonymous_page(mm
, vma
, address
, pmd
, page
, gfp
,
944 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
945 pmd_t
*pmd
, pfn_t pfn
, pgprot_t prot
, bool write
)
947 struct mm_struct
*mm
= vma
->vm_mm
;
951 ptl
= pmd_lock(mm
, pmd
);
952 entry
= pmd_mkhuge(pfn_t_pmd(pfn
, prot
));
953 if (pfn_t_devmap(pfn
))
954 entry
= pmd_mkdevmap(entry
);
956 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
957 entry
= maybe_pmd_mkwrite(entry
, vma
);
959 set_pmd_at(mm
, addr
, pmd
, entry
);
960 update_mmu_cache_pmd(vma
, addr
, pmd
);
964 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
965 pmd_t
*pmd
, pfn_t pfn
, bool write
)
967 pgprot_t pgprot
= vma
->vm_page_prot
;
969 * If we had pmd_special, we could avoid all these restrictions,
970 * but we need to be consistent with PTEs and architectures that
971 * can't support a 'special' bit.
973 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
974 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
975 (VM_PFNMAP
|VM_MIXEDMAP
));
976 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
977 BUG_ON(!pfn_t_devmap(pfn
));
979 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
980 return VM_FAULT_SIGBUS
;
981 if (track_pfn_insert(vma
, &pgprot
, pfn
))
982 return VM_FAULT_SIGBUS
;
983 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
984 return VM_FAULT_NOPAGE
;
987 static void touch_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
993 * We should set the dirty bit only for FOLL_WRITE but for now
994 * the dirty bit in the pmd is meaningless. And if the dirty
995 * bit will become meaningful and we'll only set it with
996 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
997 * set the young bit, instead of the current set_pmd_at.
999 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1000 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
1002 update_mmu_cache_pmd(vma
, addr
, pmd
);
1005 struct page
*follow_devmap_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
1006 pmd_t
*pmd
, int flags
)
1008 unsigned long pfn
= pmd_pfn(*pmd
);
1009 struct mm_struct
*mm
= vma
->vm_mm
;
1010 struct dev_pagemap
*pgmap
;
1013 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1015 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1018 if (pmd_present(*pmd
) && pmd_devmap(*pmd
))
1023 if (flags
& FOLL_TOUCH
)
1024 touch_pmd(vma
, addr
, pmd
);
1027 * device mapped pages can only be returned if the
1028 * caller will manage the page reference count.
1030 if (!(flags
& FOLL_GET
))
1031 return ERR_PTR(-EEXIST
);
1033 pfn
+= (addr
& ~PMD_MASK
) >> PAGE_SHIFT
;
1034 pgmap
= get_dev_pagemap(pfn
, NULL
);
1036 return ERR_PTR(-EFAULT
);
1037 page
= pfn_to_page(pfn
);
1039 put_dev_pagemap(pgmap
);
1044 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1045 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
1046 struct vm_area_struct
*vma
)
1048 spinlock_t
*dst_ptl
, *src_ptl
;
1049 struct page
*src_page
;
1051 pgtable_t pgtable
= NULL
;
1054 if (!vma_is_dax(vma
)) {
1056 pgtable
= pte_alloc_one(dst_mm
, addr
);
1057 if (unlikely(!pgtable
))
1061 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
1062 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
1063 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
1067 if (unlikely(!pmd_trans_huge(pmd
) && !pmd_devmap(pmd
))) {
1068 pte_free(dst_mm
, pgtable
);
1072 * When page table lock is held, the huge zero pmd should not be
1073 * under splitting since we don't split the page itself, only pmd to
1076 if (is_huge_zero_pmd(pmd
)) {
1077 struct page
*zero_page
;
1079 * get_huge_zero_page() will never allocate a new page here,
1080 * since we already have a zero page to copy. It just takes a
1083 zero_page
= get_huge_zero_page();
1084 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
1090 if (!vma_is_dax(vma
)) {
1091 /* thp accounting separate from pmd_devmap accounting */
1092 src_page
= pmd_page(pmd
);
1093 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
1095 page_dup_rmap(src_page
, true);
1096 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1097 atomic_long_inc(&dst_mm
->nr_ptes
);
1098 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
1101 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
1102 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
1103 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
1107 spin_unlock(src_ptl
);
1108 spin_unlock(dst_ptl
);
1113 void huge_pmd_set_accessed(struct mm_struct
*mm
,
1114 struct vm_area_struct
*vma
,
1115 unsigned long address
,
1116 pmd_t
*pmd
, pmd_t orig_pmd
,
1121 unsigned long haddr
;
1123 ptl
= pmd_lock(mm
, pmd
);
1124 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1127 entry
= pmd_mkyoung(orig_pmd
);
1128 haddr
= address
& HPAGE_PMD_MASK
;
1129 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
1130 update_mmu_cache_pmd(vma
, address
, pmd
);
1136 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
1137 struct vm_area_struct
*vma
,
1138 unsigned long address
,
1139 pmd_t
*pmd
, pmd_t orig_pmd
,
1141 unsigned long haddr
)
1143 struct mem_cgroup
*memcg
;
1148 struct page
**pages
;
1149 unsigned long mmun_start
; /* For mmu_notifiers */
1150 unsigned long mmun_end
; /* For mmu_notifiers */
1152 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1154 if (unlikely(!pages
)) {
1155 ret
|= VM_FAULT_OOM
;
1159 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1160 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1162 vma
, address
, page_to_nid(page
));
1163 if (unlikely(!pages
[i
] ||
1164 mem_cgroup_try_charge(pages
[i
], mm
, GFP_KERNEL
,
1169 memcg
= (void *)page_private(pages
[i
]);
1170 set_page_private(pages
[i
], 0);
1171 mem_cgroup_cancel_charge(pages
[i
], memcg
,
1176 ret
|= VM_FAULT_OOM
;
1179 set_page_private(pages
[i
], (unsigned long)memcg
);
1182 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1183 copy_user_highpage(pages
[i
], page
+ i
,
1184 haddr
+ PAGE_SIZE
* i
, vma
);
1185 __SetPageUptodate(pages
[i
]);
1190 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1191 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1193 ptl
= pmd_lock(mm
, pmd
);
1194 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1195 goto out_free_pages
;
1196 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1198 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1199 /* leave pmd empty until pte is filled */
1201 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1202 pmd_populate(mm
, &_pmd
, pgtable
);
1204 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1206 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1207 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1208 memcg
= (void *)page_private(pages
[i
]);
1209 set_page_private(pages
[i
], 0);
1210 page_add_new_anon_rmap(pages
[i
], vma
, haddr
, false);
1211 mem_cgroup_commit_charge(pages
[i
], memcg
, false, false);
1212 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
1213 pte
= pte_offset_map(&_pmd
, haddr
);
1214 VM_BUG_ON(!pte_none(*pte
));
1215 set_pte_at(mm
, haddr
, pte
, entry
);
1220 smp_wmb(); /* make pte visible before pmd */
1221 pmd_populate(mm
, pmd
, pgtable
);
1222 page_remove_rmap(page
, true);
1225 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1227 ret
|= VM_FAULT_WRITE
;
1235 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1236 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1237 memcg
= (void *)page_private(pages
[i
]);
1238 set_page_private(pages
[i
], 0);
1239 mem_cgroup_cancel_charge(pages
[i
], memcg
, false);
1246 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1247 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
1251 struct page
*page
= NULL
, *new_page
;
1252 struct mem_cgroup
*memcg
;
1253 unsigned long haddr
;
1254 unsigned long mmun_start
; /* For mmu_notifiers */
1255 unsigned long mmun_end
; /* For mmu_notifiers */
1256 gfp_t huge_gfp
; /* for allocation and charge */
1258 ptl
= pmd_lockptr(mm
, pmd
);
1259 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1260 haddr
= address
& HPAGE_PMD_MASK
;
1261 if (is_huge_zero_pmd(orig_pmd
))
1264 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1267 page
= pmd_page(orig_pmd
);
1268 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1270 * We can only reuse the page if nobody else maps the huge page or it's
1271 * part. We can do it by checking page_mapcount() on each sub-page, but
1273 * The cheaper way is to check page_count() to be equal 1: every
1274 * mapcount takes page reference reference, so this way we can
1275 * guarantee, that the PMD is the only mapping.
1276 * This can give false negative if somebody pinned the page, but that's
1279 if (page_mapcount(page
) == 1 && page_count(page
) == 1) {
1281 entry
= pmd_mkyoung(orig_pmd
);
1282 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1283 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
1284 update_mmu_cache_pmd(vma
, address
, pmd
);
1285 ret
|= VM_FAULT_WRITE
;
1291 if (transparent_hugepage_enabled(vma
) &&
1292 !transparent_hugepage_debug_cow()) {
1293 huge_gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
1294 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1298 if (likely(new_page
)) {
1299 prep_transhuge_page(new_page
);
1302 split_huge_pmd(vma
, pmd
, address
);
1303 ret
|= VM_FAULT_FALLBACK
;
1305 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
1306 pmd
, orig_pmd
, page
, haddr
);
1307 if (ret
& VM_FAULT_OOM
) {
1308 split_huge_pmd(vma
, pmd
, address
);
1309 ret
|= VM_FAULT_FALLBACK
;
1313 count_vm_event(THP_FAULT_FALLBACK
);
1317 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, huge_gfp
, &memcg
,
1321 split_huge_pmd(vma
, pmd
, address
);
1324 split_huge_pmd(vma
, pmd
, address
);
1325 ret
|= VM_FAULT_FALLBACK
;
1326 count_vm_event(THP_FAULT_FALLBACK
);
1330 count_vm_event(THP_FAULT_ALLOC
);
1333 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1335 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1336 __SetPageUptodate(new_page
);
1339 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1340 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1345 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
1347 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1352 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1353 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1354 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1355 page_add_new_anon_rmap(new_page
, vma
, haddr
, true);
1356 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1357 lru_cache_add_active_or_unevictable(new_page
, vma
);
1358 set_pmd_at(mm
, haddr
, pmd
, entry
);
1359 update_mmu_cache_pmd(vma
, address
, pmd
);
1361 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1362 put_huge_zero_page();
1364 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1365 page_remove_rmap(page
, true);
1368 ret
|= VM_FAULT_WRITE
;
1372 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1380 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1385 struct mm_struct
*mm
= vma
->vm_mm
;
1386 struct page
*page
= NULL
;
1388 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1390 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1393 /* Avoid dumping huge zero page */
1394 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1395 return ERR_PTR(-EFAULT
);
1397 /* Full NUMA hinting faults to serialise migration in fault paths */
1398 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1401 page
= pmd_page(*pmd
);
1402 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1403 if (flags
& FOLL_TOUCH
)
1404 touch_pmd(vma
, addr
, pmd
);
1405 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1407 * We don't mlock() pte-mapped THPs. This way we can avoid
1408 * leaking mlocked pages into non-VM_LOCKED VMAs.
1410 * In most cases the pmd is the only mapping of the page as we
1411 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1412 * writable private mappings in populate_vma_page_range().
1414 * The only scenario when we have the page shared here is if we
1415 * mlocking read-only mapping shared over fork(). We skip
1416 * mlocking such pages.
1418 if (compound_mapcount(page
) == 1 && !PageDoubleMap(page
) &&
1419 page
->mapping
&& trylock_page(page
)) {
1422 mlock_vma_page(page
);
1426 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1427 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
1428 if (flags
& FOLL_GET
)
1435 /* NUMA hinting page fault entry point for trans huge pmds */
1436 int do_huge_pmd_numa_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1437 unsigned long addr
, pmd_t pmd
, pmd_t
*pmdp
)
1440 struct anon_vma
*anon_vma
= NULL
;
1442 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1443 int page_nid
= -1, this_nid
= numa_node_id();
1444 int target_nid
, last_cpupid
= -1;
1446 bool migrated
= false;
1450 /* A PROT_NONE fault should not end up here */
1451 BUG_ON(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)));
1453 ptl
= pmd_lock(mm
, pmdp
);
1454 if (unlikely(!pmd_same(pmd
, *pmdp
)))
1458 * If there are potential migrations, wait for completion and retry
1459 * without disrupting NUMA hinting information. Do not relock and
1460 * check_same as the page may no longer be mapped.
1462 if (unlikely(pmd_trans_migrating(*pmdp
))) {
1463 page
= pmd_page(*pmdp
);
1465 wait_on_page_locked(page
);
1469 page
= pmd_page(pmd
);
1470 BUG_ON(is_huge_zero_page(page
));
1471 page_nid
= page_to_nid(page
);
1472 last_cpupid
= page_cpupid_last(page
);
1473 count_vm_numa_event(NUMA_HINT_FAULTS
);
1474 if (page_nid
== this_nid
) {
1475 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1476 flags
|= TNF_FAULT_LOCAL
;
1479 /* See similar comment in do_numa_page for explanation */
1480 if (!(vma
->vm_flags
& VM_WRITE
))
1481 flags
|= TNF_NO_GROUP
;
1484 * Acquire the page lock to serialise THP migrations but avoid dropping
1485 * page_table_lock if at all possible
1487 page_locked
= trylock_page(page
);
1488 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1489 if (target_nid
== -1) {
1490 /* If the page was locked, there are no parallel migrations */
1495 /* Migration could have started since the pmd_trans_migrating check */
1498 wait_on_page_locked(page
);
1504 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1505 * to serialises splits
1509 anon_vma
= page_lock_anon_vma_read(page
);
1511 /* Confirm the PMD did not change while page_table_lock was released */
1513 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1520 /* Bail if we fail to protect against THP splits for any reason */
1521 if (unlikely(!anon_vma
)) {
1528 * Migrate the THP to the requested node, returns with page unlocked
1529 * and access rights restored.
1532 migrated
= migrate_misplaced_transhuge_page(mm
, vma
,
1533 pmdp
, pmd
, addr
, page
, target_nid
);
1535 flags
|= TNF_MIGRATED
;
1536 page_nid
= target_nid
;
1538 flags
|= TNF_MIGRATE_FAIL
;
1542 BUG_ON(!PageLocked(page
));
1543 was_writable
= pmd_write(pmd
);
1544 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1545 pmd
= pmd_mkyoung(pmd
);
1547 pmd
= pmd_mkwrite(pmd
);
1548 set_pmd_at(mm
, haddr
, pmdp
, pmd
);
1549 update_mmu_cache_pmd(vma
, addr
, pmdp
);
1556 page_unlock_anon_vma_read(anon_vma
);
1559 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, flags
);
1564 int madvise_free_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1565 pmd_t
*pmd
, unsigned long addr
, unsigned long next
)
1571 struct mm_struct
*mm
= tlb
->mm
;
1574 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1579 if (is_huge_zero_pmd(orig_pmd
)) {
1584 page
= pmd_page(orig_pmd
);
1586 * If other processes are mapping this page, we couldn't discard
1587 * the page unless they all do MADV_FREE so let's skip the page.
1589 if (page_mapcount(page
) != 1)
1592 if (!trylock_page(page
))
1596 * If user want to discard part-pages of THP, split it so MADV_FREE
1597 * will deactivate only them.
1599 if (next
- addr
!= HPAGE_PMD_SIZE
) {
1602 if (split_huge_page(page
)) {
1613 if (PageDirty(page
))
1614 ClearPageDirty(page
);
1617 if (PageActive(page
))
1618 deactivate_page(page
);
1620 if (pmd_young(orig_pmd
) || pmd_dirty(orig_pmd
)) {
1621 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1623 orig_pmd
= pmd_mkold(orig_pmd
);
1624 orig_pmd
= pmd_mkclean(orig_pmd
);
1626 set_pmd_at(mm
, addr
, pmd
, orig_pmd
);
1627 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1636 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1637 pmd_t
*pmd
, unsigned long addr
)
1642 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1646 * For architectures like ppc64 we look at deposited pgtable
1647 * when calling pmdp_huge_get_and_clear. So do the
1648 * pgtable_trans_huge_withdraw after finishing pmdp related
1651 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1653 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1654 if (vma_is_dax(vma
)) {
1656 if (is_huge_zero_pmd(orig_pmd
))
1657 put_huge_zero_page();
1658 } else if (is_huge_zero_pmd(orig_pmd
)) {
1659 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1660 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1662 put_huge_zero_page();
1664 struct page
*page
= pmd_page(orig_pmd
);
1665 page_remove_rmap(page
, true);
1666 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1667 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1668 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1669 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1670 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1672 tlb_remove_page(tlb
, page
);
1677 bool move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1678 unsigned long old_addr
,
1679 unsigned long new_addr
, unsigned long old_end
,
1680 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1682 spinlock_t
*old_ptl
, *new_ptl
;
1685 struct mm_struct
*mm
= vma
->vm_mm
;
1687 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1688 (new_addr
& ~HPAGE_PMD_MASK
) ||
1689 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1690 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1694 * The destination pmd shouldn't be established, free_pgtables()
1695 * should have release it.
1697 if (WARN_ON(!pmd_none(*new_pmd
))) {
1698 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1703 * We don't have to worry about the ordering of src and dst
1704 * ptlocks because exclusive mmap_sem prevents deadlock.
1706 old_ptl
= __pmd_trans_huge_lock(old_pmd
, vma
);
1708 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1709 if (new_ptl
!= old_ptl
)
1710 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1711 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1712 VM_BUG_ON(!pmd_none(*new_pmd
));
1714 if (pmd_move_must_withdraw(new_ptl
, old_ptl
)) {
1716 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1717 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1719 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1720 if (new_ptl
!= old_ptl
)
1721 spin_unlock(new_ptl
);
1722 spin_unlock(old_ptl
);
1730 * - 0 if PMD could not be locked
1731 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1732 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1734 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1735 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1737 struct mm_struct
*mm
= vma
->vm_mm
;
1741 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1744 bool preserve_write
= prot_numa
&& pmd_write(*pmd
);
1748 * Avoid trapping faults against the zero page. The read-only
1749 * data is likely to be read-cached on the local CPU and
1750 * local/remote hits to the zero page are not interesting.
1752 if (prot_numa
&& is_huge_zero_pmd(*pmd
)) {
1757 if (!prot_numa
|| !pmd_protnone(*pmd
)) {
1758 entry
= pmdp_huge_get_and_clear_notify(mm
, addr
, pmd
);
1759 entry
= pmd_modify(entry
, newprot
);
1761 entry
= pmd_mkwrite(entry
);
1763 set_pmd_at(mm
, addr
, pmd
, entry
);
1764 BUG_ON(!preserve_write
&& pmd_write(entry
));
1773 * Returns true if a given pmd maps a thp, false otherwise.
1775 * Note that if it returns true, this routine returns without unlocking page
1776 * table lock. So callers must unlock it.
1778 spinlock_t
*__pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1781 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1782 if (likely(pmd_trans_huge(*pmd
) || pmd_devmap(*pmd
)))
1788 #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1790 int hugepage_madvise(struct vm_area_struct
*vma
,
1791 unsigned long *vm_flags
, int advice
)
1797 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1798 * can't handle this properly after s390_enable_sie, so we simply
1799 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1801 if (mm_has_pgste(vma
->vm_mm
))
1805 * Be somewhat over-protective like KSM for now!
1807 if (*vm_flags
& VM_NO_THP
)
1809 *vm_flags
&= ~VM_NOHUGEPAGE
;
1810 *vm_flags
|= VM_HUGEPAGE
;
1812 * If the vma become good for khugepaged to scan,
1813 * register it here without waiting a page fault that
1814 * may not happen any time soon.
1816 if (unlikely(khugepaged_enter_vma_merge(vma
, *vm_flags
)))
1819 case MADV_NOHUGEPAGE
:
1821 * Be somewhat over-protective like KSM for now!
1823 if (*vm_flags
& VM_NO_THP
)
1825 *vm_flags
&= ~VM_HUGEPAGE
;
1826 *vm_flags
|= VM_NOHUGEPAGE
;
1828 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1829 * this vma even if we leave the mm registered in khugepaged if
1830 * it got registered before VM_NOHUGEPAGE was set.
1838 static int __init
khugepaged_slab_init(void)
1840 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1841 sizeof(struct mm_slot
),
1842 __alignof__(struct mm_slot
), 0, NULL
);
1849 static void __init
khugepaged_slab_exit(void)
1851 kmem_cache_destroy(mm_slot_cache
);
1854 static inline struct mm_slot
*alloc_mm_slot(void)
1856 if (!mm_slot_cache
) /* initialization failed */
1858 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1861 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1863 kmem_cache_free(mm_slot_cache
, mm_slot
);
1866 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1868 struct mm_slot
*mm_slot
;
1870 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
1871 if (mm
== mm_slot
->mm
)
1877 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1878 struct mm_slot
*mm_slot
)
1881 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
1884 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1886 return atomic_read(&mm
->mm_users
) == 0;
1889 int __khugepaged_enter(struct mm_struct
*mm
)
1891 struct mm_slot
*mm_slot
;
1894 mm_slot
= alloc_mm_slot();
1898 /* __khugepaged_exit() must not run from under us */
1899 VM_BUG_ON_MM(khugepaged_test_exit(mm
), mm
);
1900 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1901 free_mm_slot(mm_slot
);
1905 spin_lock(&khugepaged_mm_lock
);
1906 insert_to_mm_slots_hash(mm
, mm_slot
);
1908 * Insert just behind the scanning cursor, to let the area settle
1911 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
1912 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
1913 spin_unlock(&khugepaged_mm_lock
);
1915 atomic_inc(&mm
->mm_count
);
1917 wake_up_interruptible(&khugepaged_wait
);
1922 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
1923 unsigned long vm_flags
)
1925 unsigned long hstart
, hend
;
1928 * Not yet faulted in so we will register later in the
1929 * page fault if needed.
1933 /* khugepaged not yet working on file or special mappings */
1935 VM_BUG_ON_VMA(vm_flags
& VM_NO_THP
, vma
);
1936 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1937 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1939 return khugepaged_enter(vma
, vm_flags
);
1943 void __khugepaged_exit(struct mm_struct
*mm
)
1945 struct mm_slot
*mm_slot
;
1948 spin_lock(&khugepaged_mm_lock
);
1949 mm_slot
= get_mm_slot(mm
);
1950 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
1951 hash_del(&mm_slot
->hash
);
1952 list_del(&mm_slot
->mm_node
);
1955 spin_unlock(&khugepaged_mm_lock
);
1958 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
1959 free_mm_slot(mm_slot
);
1961 } else if (mm_slot
) {
1963 * This is required to serialize against
1964 * khugepaged_test_exit() (which is guaranteed to run
1965 * under mmap sem read mode). Stop here (after we
1966 * return all pagetables will be destroyed) until
1967 * khugepaged has finished working on the pagetables
1968 * under the mmap_sem.
1970 down_write(&mm
->mmap_sem
);
1971 up_write(&mm
->mmap_sem
);
1975 static void release_pte_page(struct page
*page
)
1977 /* 0 stands for page_is_file_cache(page) == false */
1978 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1980 putback_lru_page(page
);
1983 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
1985 while (--_pte
>= pte
) {
1986 pte_t pteval
= *_pte
;
1987 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
1988 release_pte_page(pte_page(pteval
));
1992 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
1993 unsigned long address
,
1996 struct page
*page
= NULL
;
1998 int none_or_zero
= 0, result
= 0;
1999 bool referenced
= false, writable
= false;
2001 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2002 _pte
++, address
+= PAGE_SIZE
) {
2003 pte_t pteval
= *_pte
;
2004 if (pte_none(pteval
) || (pte_present(pteval
) &&
2005 is_zero_pfn(pte_pfn(pteval
)))) {
2006 if (!userfaultfd_armed(vma
) &&
2007 ++none_or_zero
<= khugepaged_max_ptes_none
) {
2010 result
= SCAN_EXCEED_NONE_PTE
;
2014 if (!pte_present(pteval
)) {
2015 result
= SCAN_PTE_NON_PRESENT
;
2018 page
= vm_normal_page(vma
, address
, pteval
);
2019 if (unlikely(!page
)) {
2020 result
= SCAN_PAGE_NULL
;
2024 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2025 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
2026 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
2029 * We can do it before isolate_lru_page because the
2030 * page can't be freed from under us. NOTE: PG_lock
2031 * is needed to serialize against split_huge_page
2032 * when invoked from the VM.
2034 if (!trylock_page(page
)) {
2035 result
= SCAN_PAGE_LOCK
;
2040 * cannot use mapcount: can't collapse if there's a gup pin.
2041 * The page must only be referenced by the scanned process
2042 * and page swap cache.
2044 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2046 result
= SCAN_PAGE_COUNT
;
2049 if (pte_write(pteval
)) {
2052 if (PageSwapCache(page
) && !reuse_swap_page(page
)) {
2054 result
= SCAN_SWAP_CACHE_PAGE
;
2058 * Page is not in the swap cache. It can be collapsed
2064 * Isolate the page to avoid collapsing an hugepage
2065 * currently in use by the VM.
2067 if (isolate_lru_page(page
)) {
2069 result
= SCAN_DEL_PAGE_LRU
;
2072 /* 0 stands for page_is_file_cache(page) == false */
2073 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2074 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2075 VM_BUG_ON_PAGE(PageLRU(page
), page
);
2077 /* If there is no mapped pte young don't collapse the page */
2078 if (pte_young(pteval
) ||
2079 page_is_young(page
) || PageReferenced(page
) ||
2080 mmu_notifier_test_young(vma
->vm_mm
, address
))
2083 if (likely(writable
)) {
2084 if (likely(referenced
)) {
2085 result
= SCAN_SUCCEED
;
2086 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
2087 referenced
, writable
, result
);
2091 result
= SCAN_PAGE_RO
;
2095 release_pte_pages(pte
, _pte
);
2096 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
2097 referenced
, writable
, result
);
2101 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2102 struct vm_area_struct
*vma
,
2103 unsigned long address
,
2107 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2108 pte_t pteval
= *_pte
;
2109 struct page
*src_page
;
2111 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2112 clear_user_highpage(page
, address
);
2113 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2114 if (is_zero_pfn(pte_pfn(pteval
))) {
2116 * ptl mostly unnecessary.
2120 * paravirt calls inside pte_clear here are
2123 pte_clear(vma
->vm_mm
, address
, _pte
);
2127 src_page
= pte_page(pteval
);
2128 copy_user_highpage(page
, src_page
, address
, vma
);
2129 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
2130 release_pte_page(src_page
);
2132 * ptl mostly unnecessary, but preempt has to
2133 * be disabled to update the per-cpu stats
2134 * inside page_remove_rmap().
2138 * paravirt calls inside pte_clear here are
2141 pte_clear(vma
->vm_mm
, address
, _pte
);
2142 page_remove_rmap(src_page
, false);
2144 free_page_and_swap_cache(src_page
);
2147 address
+= PAGE_SIZE
;
2152 static void khugepaged_alloc_sleep(void)
2156 add_wait_queue(&khugepaged_wait
, &wait
);
2157 freezable_schedule_timeout_interruptible(
2158 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2159 remove_wait_queue(&khugepaged_wait
, &wait
);
2162 static int khugepaged_node_load
[MAX_NUMNODES
];
2164 static bool khugepaged_scan_abort(int nid
)
2169 * If zone_reclaim_mode is disabled, then no extra effort is made to
2170 * allocate memory locally.
2172 if (!zone_reclaim_mode
)
2175 /* If there is a count for this node already, it must be acceptable */
2176 if (khugepaged_node_load
[nid
])
2179 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
2180 if (!khugepaged_node_load
[i
])
2182 if (node_distance(nid
, i
) > RECLAIM_DISTANCE
)
2189 static int khugepaged_find_target_node(void)
2191 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
2192 int nid
, target_node
= 0, max_value
= 0;
2194 /* find first node with max normal pages hit */
2195 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
2196 if (khugepaged_node_load
[nid
] > max_value
) {
2197 max_value
= khugepaged_node_load
[nid
];
2201 /* do some balance if several nodes have the same hit record */
2202 if (target_node
<= last_khugepaged_target_node
)
2203 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
2205 if (max_value
== khugepaged_node_load
[nid
]) {
2210 last_khugepaged_target_node
= target_node
;
2214 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2216 if (IS_ERR(*hpage
)) {
2222 khugepaged_alloc_sleep();
2223 } else if (*hpage
) {
2231 static struct page
*
2232 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2233 unsigned long address
, int node
)
2235 VM_BUG_ON_PAGE(*hpage
, *hpage
);
2238 * Before allocating the hugepage, release the mmap_sem read lock.
2239 * The allocation can take potentially a long time if it involves
2240 * sync compaction, and we do not need to hold the mmap_sem during
2241 * that. We will recheck the vma after taking it again in write mode.
2243 up_read(&mm
->mmap_sem
);
2245 *hpage
= __alloc_pages_node(node
, gfp
, HPAGE_PMD_ORDER
);
2246 if (unlikely(!*hpage
)) {
2247 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2248 *hpage
= ERR_PTR(-ENOMEM
);
2252 prep_transhuge_page(*hpage
);
2253 count_vm_event(THP_COLLAPSE_ALLOC
);
2257 static int khugepaged_find_target_node(void)
2262 static inline struct page
*alloc_hugepage(int defrag
)
2266 page
= alloc_pages(alloc_hugepage_gfpmask(defrag
, 0), HPAGE_PMD_ORDER
);
2268 prep_transhuge_page(page
);
2272 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2277 hpage
= alloc_hugepage(khugepaged_defrag());
2279 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2284 khugepaged_alloc_sleep();
2286 count_vm_event(THP_COLLAPSE_ALLOC
);
2287 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2292 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2295 *hpage
= khugepaged_alloc_hugepage(wait
);
2297 if (unlikely(!*hpage
))
2303 static struct page
*
2304 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2305 unsigned long address
, int node
)
2307 up_read(&mm
->mmap_sem
);
2314 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2316 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2317 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2319 if (!vma
->anon_vma
|| vma
->vm_ops
)
2321 if (is_vma_temporary_stack(vma
))
2323 VM_BUG_ON_VMA(vma
->vm_flags
& VM_NO_THP
, vma
);
2327 static void collapse_huge_page(struct mm_struct
*mm
,
2328 unsigned long address
,
2329 struct page
**hpage
,
2330 struct vm_area_struct
*vma
,
2336 struct page
*new_page
;
2337 spinlock_t
*pmd_ptl
, *pte_ptl
;
2338 int isolated
= 0, result
= 0;
2339 unsigned long hstart
, hend
;
2340 struct mem_cgroup
*memcg
;
2341 unsigned long mmun_start
; /* For mmu_notifiers */
2342 unsigned long mmun_end
; /* For mmu_notifiers */
2345 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2347 /* Only allocate from the target node */
2348 gfp
= alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE
) |
2351 /* release the mmap_sem read lock. */
2352 new_page
= khugepaged_alloc_page(hpage
, gfp
, mm
, address
, node
);
2354 result
= SCAN_ALLOC_HUGE_PAGE_FAIL
;
2358 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, gfp
, &memcg
, true))) {
2359 result
= SCAN_CGROUP_CHARGE_FAIL
;
2364 * Prevent all access to pagetables with the exception of
2365 * gup_fast later hanlded by the ptep_clear_flush and the VM
2366 * handled by the anon_vma lock + PG_lock.
2368 down_write(&mm
->mmap_sem
);
2369 if (unlikely(khugepaged_test_exit(mm
))) {
2370 result
= SCAN_ANY_PROCESS
;
2374 vma
= find_vma(mm
, address
);
2376 result
= SCAN_VMA_NULL
;
2379 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2380 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2381 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
) {
2382 result
= SCAN_ADDRESS_RANGE
;
2385 if (!hugepage_vma_check(vma
)) {
2386 result
= SCAN_VMA_CHECK
;
2389 pmd
= mm_find_pmd(mm
, address
);
2391 result
= SCAN_PMD_NULL
;
2395 anon_vma_lock_write(vma
->anon_vma
);
2397 pte
= pte_offset_map(pmd
, address
);
2398 pte_ptl
= pte_lockptr(mm
, pmd
);
2400 mmun_start
= address
;
2401 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2402 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2403 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
2405 * After this gup_fast can't run anymore. This also removes
2406 * any huge TLB entry from the CPU so we won't allow
2407 * huge and small TLB entries for the same virtual address
2408 * to avoid the risk of CPU bugs in that area.
2410 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
2411 spin_unlock(pmd_ptl
);
2412 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2415 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2416 spin_unlock(pte_ptl
);
2418 if (unlikely(!isolated
)) {
2421 BUG_ON(!pmd_none(*pmd
));
2423 * We can only use set_pmd_at when establishing
2424 * hugepmds and never for establishing regular pmds that
2425 * points to regular pagetables. Use pmd_populate for that
2427 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
2428 spin_unlock(pmd_ptl
);
2429 anon_vma_unlock_write(vma
->anon_vma
);
2435 * All pages are isolated and locked so anon_vma rmap
2436 * can't run anymore.
2438 anon_vma_unlock_write(vma
->anon_vma
);
2440 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
2442 __SetPageUptodate(new_page
);
2443 pgtable
= pmd_pgtable(_pmd
);
2445 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2446 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
2449 * spin_lock() below is not the equivalent of smp_wmb(), so
2450 * this is needed to avoid the copy_huge_page writes to become
2451 * visible after the set_pmd_at() write.
2456 BUG_ON(!pmd_none(*pmd
));
2457 page_add_new_anon_rmap(new_page
, vma
, address
, true);
2458 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
2459 lru_cache_add_active_or_unevictable(new_page
, vma
);
2460 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
2461 set_pmd_at(mm
, address
, pmd
, _pmd
);
2462 update_mmu_cache_pmd(vma
, address
, pmd
);
2463 spin_unlock(pmd_ptl
);
2467 khugepaged_pages_collapsed
++;
2468 result
= SCAN_SUCCEED
;
2470 up_write(&mm
->mmap_sem
);
2471 trace_mm_collapse_huge_page(mm
, isolated
, result
);
2475 trace_mm_collapse_huge_page(mm
, isolated
, result
);
2478 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2482 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2483 struct vm_area_struct
*vma
,
2484 unsigned long address
,
2485 struct page
**hpage
)
2489 int ret
= 0, none_or_zero
= 0, result
= 0;
2490 struct page
*page
= NULL
;
2491 unsigned long _address
;
2493 int node
= NUMA_NO_NODE
;
2494 bool writable
= false, referenced
= false;
2496 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2498 pmd
= mm_find_pmd(mm
, address
);
2500 result
= SCAN_PMD_NULL
;
2504 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
2505 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2506 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2507 _pte
++, _address
+= PAGE_SIZE
) {
2508 pte_t pteval
= *_pte
;
2509 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2510 if (!userfaultfd_armed(vma
) &&
2511 ++none_or_zero
<= khugepaged_max_ptes_none
) {
2514 result
= SCAN_EXCEED_NONE_PTE
;
2518 if (!pte_present(pteval
)) {
2519 result
= SCAN_PTE_NON_PRESENT
;
2522 if (pte_write(pteval
))
2525 page
= vm_normal_page(vma
, _address
, pteval
);
2526 if (unlikely(!page
)) {
2527 result
= SCAN_PAGE_NULL
;
2531 /* TODO: teach khugepaged to collapse THP mapped with pte */
2532 if (PageCompound(page
)) {
2533 result
= SCAN_PAGE_COMPOUND
;
2538 * Record which node the original page is from and save this
2539 * information to khugepaged_node_load[].
2540 * Khupaged will allocate hugepage from the node has the max
2543 node
= page_to_nid(page
);
2544 if (khugepaged_scan_abort(node
)) {
2545 result
= SCAN_SCAN_ABORT
;
2548 khugepaged_node_load
[node
]++;
2549 if (!PageLRU(page
)) {
2550 result
= SCAN_SCAN_ABORT
;
2553 if (PageLocked(page
)) {
2554 result
= SCAN_PAGE_LOCK
;
2557 if (!PageAnon(page
)) {
2558 result
= SCAN_PAGE_ANON
;
2563 * cannot use mapcount: can't collapse if there's a gup pin.
2564 * The page must only be referenced by the scanned process
2565 * and page swap cache.
2567 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2568 result
= SCAN_PAGE_COUNT
;
2571 if (pte_young(pteval
) ||
2572 page_is_young(page
) || PageReferenced(page
) ||
2573 mmu_notifier_test_young(vma
->vm_mm
, address
))
2578 result
= SCAN_SUCCEED
;
2581 result
= SCAN_NO_REFERENCED_PAGE
;
2584 result
= SCAN_PAGE_RO
;
2587 pte_unmap_unlock(pte
, ptl
);
2589 node
= khugepaged_find_target_node();
2590 /* collapse_huge_page will return with the mmap_sem released */
2591 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2594 trace_mm_khugepaged_scan_pmd(mm
, page
, writable
, referenced
,
2595 none_or_zero
, result
);
2599 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2601 struct mm_struct
*mm
= mm_slot
->mm
;
2603 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2605 if (khugepaged_test_exit(mm
)) {
2607 hash_del(&mm_slot
->hash
);
2608 list_del(&mm_slot
->mm_node
);
2611 * Not strictly needed because the mm exited already.
2613 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2616 /* khugepaged_mm_lock actually not necessary for the below */
2617 free_mm_slot(mm_slot
);
2622 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2623 struct page
**hpage
)
2624 __releases(&khugepaged_mm_lock
)
2625 __acquires(&khugepaged_mm_lock
)
2627 struct mm_slot
*mm_slot
;
2628 struct mm_struct
*mm
;
2629 struct vm_area_struct
*vma
;
2633 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2635 if (khugepaged_scan
.mm_slot
)
2636 mm_slot
= khugepaged_scan
.mm_slot
;
2638 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2639 struct mm_slot
, mm_node
);
2640 khugepaged_scan
.address
= 0;
2641 khugepaged_scan
.mm_slot
= mm_slot
;
2643 spin_unlock(&khugepaged_mm_lock
);
2646 down_read(&mm
->mmap_sem
);
2647 if (unlikely(khugepaged_test_exit(mm
)))
2650 vma
= find_vma(mm
, khugepaged_scan
.address
);
2653 for (; vma
; vma
= vma
->vm_next
) {
2654 unsigned long hstart
, hend
;
2657 if (unlikely(khugepaged_test_exit(mm
))) {
2661 if (!hugepage_vma_check(vma
)) {
2666 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2667 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2670 if (khugepaged_scan
.address
> hend
)
2672 if (khugepaged_scan
.address
< hstart
)
2673 khugepaged_scan
.address
= hstart
;
2674 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2676 while (khugepaged_scan
.address
< hend
) {
2679 if (unlikely(khugepaged_test_exit(mm
)))
2680 goto breakouterloop
;
2682 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2683 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2685 ret
= khugepaged_scan_pmd(mm
, vma
,
2686 khugepaged_scan
.address
,
2688 /* move to next address */
2689 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2690 progress
+= HPAGE_PMD_NR
;
2692 /* we released mmap_sem so break loop */
2693 goto breakouterloop_mmap_sem
;
2694 if (progress
>= pages
)
2695 goto breakouterloop
;
2699 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2700 breakouterloop_mmap_sem
:
2702 spin_lock(&khugepaged_mm_lock
);
2703 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2705 * Release the current mm_slot if this mm is about to die, or
2706 * if we scanned all vmas of this mm.
2708 if (khugepaged_test_exit(mm
) || !vma
) {
2710 * Make sure that if mm_users is reaching zero while
2711 * khugepaged runs here, khugepaged_exit will find
2712 * mm_slot not pointing to the exiting mm.
2714 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2715 khugepaged_scan
.mm_slot
= list_entry(
2716 mm_slot
->mm_node
.next
,
2717 struct mm_slot
, mm_node
);
2718 khugepaged_scan
.address
= 0;
2720 khugepaged_scan
.mm_slot
= NULL
;
2721 khugepaged_full_scans
++;
2724 collect_mm_slot(mm_slot
);
2730 static int khugepaged_has_work(void)
2732 return !list_empty(&khugepaged_scan
.mm_head
) &&
2733 khugepaged_enabled();
2736 static int khugepaged_wait_event(void)
2738 return !list_empty(&khugepaged_scan
.mm_head
) ||
2739 kthread_should_stop();
2742 static void khugepaged_do_scan(void)
2744 struct page
*hpage
= NULL
;
2745 unsigned int progress
= 0, pass_through_head
= 0;
2746 unsigned int pages
= khugepaged_pages_to_scan
;
2749 barrier(); /* write khugepaged_pages_to_scan to local stack */
2751 while (progress
< pages
) {
2752 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2757 if (unlikely(kthread_should_stop() || try_to_freeze()))
2760 spin_lock(&khugepaged_mm_lock
);
2761 if (!khugepaged_scan
.mm_slot
)
2762 pass_through_head
++;
2763 if (khugepaged_has_work() &&
2764 pass_through_head
< 2)
2765 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2769 spin_unlock(&khugepaged_mm_lock
);
2772 if (!IS_ERR_OR_NULL(hpage
))
2776 static void khugepaged_wait_work(void)
2778 if (khugepaged_has_work()) {
2779 if (!khugepaged_scan_sleep_millisecs
)
2782 wait_event_freezable_timeout(khugepaged_wait
,
2783 kthread_should_stop(),
2784 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2788 if (khugepaged_enabled())
2789 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2792 static int khugepaged(void *none
)
2794 struct mm_slot
*mm_slot
;
2797 set_user_nice(current
, MAX_NICE
);
2799 while (!kthread_should_stop()) {
2800 khugepaged_do_scan();
2801 khugepaged_wait_work();
2804 spin_lock(&khugepaged_mm_lock
);
2805 mm_slot
= khugepaged_scan
.mm_slot
;
2806 khugepaged_scan
.mm_slot
= NULL
;
2808 collect_mm_slot(mm_slot
);
2809 spin_unlock(&khugepaged_mm_lock
);
2813 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2814 unsigned long haddr
, pmd_t
*pmd
)
2816 struct mm_struct
*mm
= vma
->vm_mm
;
2821 /* leave pmd empty until pte is filled */
2822 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2824 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2825 pmd_populate(mm
, &_pmd
, pgtable
);
2827 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2829 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2830 entry
= pte_mkspecial(entry
);
2831 pte
= pte_offset_map(&_pmd
, haddr
);
2832 VM_BUG_ON(!pte_none(*pte
));
2833 set_pte_at(mm
, haddr
, pte
, entry
);
2836 smp_wmb(); /* make pte visible before pmd */
2837 pmd_populate(mm
, pmd
, pgtable
);
2838 put_huge_zero_page();
2841 static void __split_huge_pmd_locked(struct vm_area_struct
*vma
, pmd_t
*pmd
,
2842 unsigned long haddr
, bool freeze
)
2844 struct mm_struct
*mm
= vma
->vm_mm
;
2848 bool young
, write
, dirty
;
2851 VM_BUG_ON(haddr
& ~HPAGE_PMD_MASK
);
2852 VM_BUG_ON_VMA(vma
->vm_start
> haddr
, vma
);
2853 VM_BUG_ON_VMA(vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
, vma
);
2854 VM_BUG_ON(!pmd_trans_huge(*pmd
) && !pmd_devmap(*pmd
));
2856 count_vm_event(THP_SPLIT_PMD
);
2858 if (vma_is_dax(vma
)) {
2859 pmd_t _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2860 if (is_huge_zero_pmd(_pmd
))
2861 put_huge_zero_page();
2863 } else if (is_huge_zero_pmd(*pmd
)) {
2864 return __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
2867 page
= pmd_page(*pmd
);
2868 VM_BUG_ON_PAGE(!page_count(page
), page
);
2869 atomic_add(HPAGE_PMD_NR
- 1, &page
->_count
);
2870 write
= pmd_write(*pmd
);
2871 young
= pmd_young(*pmd
);
2872 dirty
= pmd_dirty(*pmd
);
2874 pmdp_huge_split_prepare(vma
, haddr
, pmd
);
2875 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2876 pmd_populate(mm
, &_pmd
, pgtable
);
2878 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2881 * Note that NUMA hinting access restrictions are not
2882 * transferred to avoid any possibility of altering
2883 * permissions across VMAs.
2886 swp_entry_t swp_entry
;
2887 swp_entry
= make_migration_entry(page
+ i
, write
);
2888 entry
= swp_entry_to_pte(swp_entry
);
2890 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
2891 entry
= maybe_mkwrite(entry
, vma
);
2893 entry
= pte_wrprotect(entry
);
2895 entry
= pte_mkold(entry
);
2898 SetPageDirty(page
+ i
);
2899 pte
= pte_offset_map(&_pmd
, haddr
);
2900 BUG_ON(!pte_none(*pte
));
2901 set_pte_at(mm
, haddr
, pte
, entry
);
2902 atomic_inc(&page
[i
]._mapcount
);
2907 * Set PG_double_map before dropping compound_mapcount to avoid
2908 * false-negative page_mapped().
2910 if (compound_mapcount(page
) > 1 && !TestSetPageDoubleMap(page
)) {
2911 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
2912 atomic_inc(&page
[i
]._mapcount
);
2915 if (atomic_add_negative(-1, compound_mapcount_ptr(page
))) {
2916 /* Last compound_mapcount is gone. */
2917 __dec_zone_page_state(page
, NR_ANON_TRANSPARENT_HUGEPAGES
);
2918 if (TestClearPageDoubleMap(page
)) {
2919 /* No need in mapcount reference anymore */
2920 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
2921 atomic_dec(&page
[i
]._mapcount
);
2925 smp_wmb(); /* make pte visible before pmd */
2927 * Up to this point the pmd is present and huge and userland has the
2928 * whole access to the hugepage during the split (which happens in
2929 * place). If we overwrite the pmd with the not-huge version pointing
2930 * to the pte here (which of course we could if all CPUs were bug
2931 * free), userland could trigger a small page size TLB miss on the
2932 * small sized TLB while the hugepage TLB entry is still established in
2933 * the huge TLB. Some CPU doesn't like that.
2934 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2935 * 383 on page 93. Intel should be safe but is also warns that it's
2936 * only safe if the permission and cache attributes of the two entries
2937 * loaded in the two TLB is identical (which should be the case here).
2938 * But it is generally safer to never allow small and huge TLB entries
2939 * for the same virtual address to be loaded simultaneously. So instead
2940 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2941 * current pmd notpresent (atomically because here the pmd_trans_huge
2942 * and pmd_trans_splitting must remain set at all times on the pmd
2943 * until the split is complete for this pmd), then we flush the SMP TLB
2944 * and finally we write the non-huge version of the pmd entry with
2947 pmdp_invalidate(vma
, haddr
, pmd
);
2948 pmd_populate(mm
, pmd
, pgtable
);
2951 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2952 page_remove_rmap(page
+ i
, false);
2958 void __split_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
2959 unsigned long address
)
2962 struct mm_struct
*mm
= vma
->vm_mm
;
2963 struct page
*page
= NULL
;
2964 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
2966 mmu_notifier_invalidate_range_start(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
2967 ptl
= pmd_lock(mm
, pmd
);
2968 if (pmd_trans_huge(*pmd
)) {
2969 page
= pmd_page(*pmd
);
2970 if (PageMlocked(page
))
2974 } else if (!pmd_devmap(*pmd
))
2976 __split_huge_pmd_locked(vma
, pmd
, haddr
, false);
2979 mmu_notifier_invalidate_range_end(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
2982 munlock_vma_page(page
);
2988 static void split_huge_pmd_address(struct vm_area_struct
*vma
,
2989 unsigned long address
)
2995 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
2997 pgd
= pgd_offset(vma
->vm_mm
, address
);
2998 if (!pgd_present(*pgd
))
3001 pud
= pud_offset(pgd
, address
);
3002 if (!pud_present(*pud
))
3005 pmd
= pmd_offset(pud
, address
);
3006 if (!pmd_present(*pmd
) || (!pmd_trans_huge(*pmd
) && !pmd_devmap(*pmd
)))
3009 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3010 * materialize from under us.
3012 split_huge_pmd(vma
, pmd
, address
);
3015 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
3016 unsigned long start
,
3021 * If the new start address isn't hpage aligned and it could
3022 * previously contain an hugepage: check if we need to split
3025 if (start
& ~HPAGE_PMD_MASK
&&
3026 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3027 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3028 split_huge_pmd_address(vma
, start
);
3031 * If the new end address isn't hpage aligned and it could
3032 * previously contain an hugepage: check if we need to split
3035 if (end
& ~HPAGE_PMD_MASK
&&
3036 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3037 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3038 split_huge_pmd_address(vma
, end
);
3041 * If we're also updating the vma->vm_next->vm_start, if the new
3042 * vm_next->vm_start isn't page aligned and it could previously
3043 * contain an hugepage: check if we need to split an huge pmd.
3045 if (adjust_next
> 0) {
3046 struct vm_area_struct
*next
= vma
->vm_next
;
3047 unsigned long nstart
= next
->vm_start
;
3048 nstart
+= adjust_next
<< PAGE_SHIFT
;
3049 if (nstart
& ~HPAGE_PMD_MASK
&&
3050 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
3051 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
3052 split_huge_pmd_address(next
, nstart
);
3056 static void freeze_page_vma(struct vm_area_struct
*vma
, struct page
*page
,
3057 unsigned long address
)
3059 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
3065 int i
, nr
= HPAGE_PMD_NR
;
3067 /* Skip pages which doesn't belong to the VMA */
3068 if (address
< vma
->vm_start
) {
3069 int off
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
3072 address
= vma
->vm_start
;
3075 pgd
= pgd_offset(vma
->vm_mm
, address
);
3076 if (!pgd_present(*pgd
))
3078 pud
= pud_offset(pgd
, address
);
3079 if (!pud_present(*pud
))
3081 pmd
= pmd_offset(pud
, address
);
3082 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
3083 if (!pmd_present(*pmd
)) {
3087 if (pmd_trans_huge(*pmd
)) {
3088 if (page
== pmd_page(*pmd
))
3089 __split_huge_pmd_locked(vma
, pmd
, haddr
, true);
3095 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, address
, &ptl
);
3096 for (i
= 0; i
< nr
; i
++, address
+= PAGE_SIZE
, page
++, pte
++) {
3097 pte_t entry
, swp_pte
;
3098 swp_entry_t swp_entry
;
3101 * We've just crossed page table boundary: need to map next one.
3102 * It can happen if THP was mremaped to non PMD-aligned address.
3104 if (unlikely(address
== haddr
+ HPAGE_PMD_SIZE
)) {
3105 pte_unmap_unlock(pte
- 1, ptl
);
3106 pmd
= mm_find_pmd(vma
->vm_mm
, address
);
3109 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
,
3113 if (!pte_present(*pte
))
3115 if (page_to_pfn(page
) != pte_pfn(*pte
))
3117 flush_cache_page(vma
, address
, page_to_pfn(page
));
3118 entry
= ptep_clear_flush(vma
, address
, pte
);
3119 if (pte_dirty(entry
))
3121 swp_entry
= make_migration_entry(page
, pte_write(entry
));
3122 swp_pte
= swp_entry_to_pte(swp_entry
);
3123 if (pte_soft_dirty(entry
))
3124 swp_pte
= pte_swp_mksoft_dirty(swp_pte
);
3125 set_pte_at(vma
->vm_mm
, address
, pte
, swp_pte
);
3126 page_remove_rmap(page
, false);
3129 pte_unmap_unlock(pte
- 1, ptl
);
3132 static void freeze_page(struct anon_vma
*anon_vma
, struct page
*page
)
3134 struct anon_vma_chain
*avc
;
3135 pgoff_t pgoff
= page_to_pgoff(page
);
3137 VM_BUG_ON_PAGE(!PageHead(page
), page
);
3139 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
,
3140 pgoff
+ HPAGE_PMD_NR
- 1) {
3141 unsigned long address
= __vma_address(page
, avc
->vma
);
3143 mmu_notifier_invalidate_range_start(avc
->vma
->vm_mm
,
3144 address
, address
+ HPAGE_PMD_SIZE
);
3145 freeze_page_vma(avc
->vma
, page
, address
);
3146 mmu_notifier_invalidate_range_end(avc
->vma
->vm_mm
,
3147 address
, address
+ HPAGE_PMD_SIZE
);
3151 static void unfreeze_page_vma(struct vm_area_struct
*vma
, struct page
*page
,
3152 unsigned long address
)
3157 swp_entry_t swp_entry
;
3158 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
3159 int i
, nr
= HPAGE_PMD_NR
;
3161 /* Skip pages which doesn't belong to the VMA */
3162 if (address
< vma
->vm_start
) {
3163 int off
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
3166 address
= vma
->vm_start
;
3169 pmd
= mm_find_pmd(vma
->vm_mm
, address
);
3173 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, address
, &ptl
);
3174 for (i
= 0; i
< nr
; i
++, address
+= PAGE_SIZE
, page
++, pte
++) {
3176 * We've just crossed page table boundary: need to map next one.
3177 * It can happen if THP was mremaped to non-PMD aligned address.
3179 if (unlikely(address
== haddr
+ HPAGE_PMD_SIZE
)) {
3180 pte_unmap_unlock(pte
- 1, ptl
);
3181 pmd
= mm_find_pmd(vma
->vm_mm
, address
);
3184 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
,
3188 if (!is_swap_pte(*pte
))
3191 swp_entry
= pte_to_swp_entry(*pte
);
3192 if (!is_migration_entry(swp_entry
))
3194 if (migration_entry_to_page(swp_entry
) != page
)
3198 page_add_anon_rmap(page
, vma
, address
, false);
3200 entry
= pte_mkold(mk_pte(page
, vma
->vm_page_prot
));
3201 if (PageDirty(page
))
3202 entry
= pte_mkdirty(entry
);
3203 if (is_write_migration_entry(swp_entry
))
3204 entry
= maybe_mkwrite(entry
, vma
);
3206 flush_dcache_page(page
);
3207 set_pte_at(vma
->vm_mm
, address
, pte
, entry
);
3209 /* No need to invalidate - it was non-present before */
3210 update_mmu_cache(vma
, address
, pte
);
3212 pte_unmap_unlock(pte
- 1, ptl
);
3215 static void unfreeze_page(struct anon_vma
*anon_vma
, struct page
*page
)
3217 struct anon_vma_chain
*avc
;
3218 pgoff_t pgoff
= page_to_pgoff(page
);
3220 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
,
3221 pgoff
, pgoff
+ HPAGE_PMD_NR
- 1) {
3222 unsigned long address
= __vma_address(page
, avc
->vma
);
3224 mmu_notifier_invalidate_range_start(avc
->vma
->vm_mm
,
3225 address
, address
+ HPAGE_PMD_SIZE
);
3226 unfreeze_page_vma(avc
->vma
, page
, address
);
3227 mmu_notifier_invalidate_range_end(avc
->vma
->vm_mm
,
3228 address
, address
+ HPAGE_PMD_SIZE
);
3232 static int __split_huge_page_tail(struct page
*head
, int tail
,
3233 struct lruvec
*lruvec
, struct list_head
*list
)
3236 struct page
*page_tail
= head
+ tail
;
3238 mapcount
= atomic_read(&page_tail
->_mapcount
) + 1;
3239 VM_BUG_ON_PAGE(atomic_read(&page_tail
->_count
) != 0, page_tail
);
3242 * tail_page->_count is zero and not changing from under us. But
3243 * get_page_unless_zero() may be running from under us on the
3244 * tail_page. If we used atomic_set() below instead of atomic_add(), we
3245 * would then run atomic_set() concurrently with
3246 * get_page_unless_zero(), and atomic_set() is implemented in C not
3247 * using locked ops. spin_unlock on x86 sometime uses locked ops
3248 * because of PPro errata 66, 92, so unless somebody can guarantee
3249 * atomic_set() here would be safe on all archs (and not only on x86),
3250 * it's safer to use atomic_add().
3252 atomic_add(mapcount
+ 1, &page_tail
->_count
);
3255 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
3256 page_tail
->flags
|= (head
->flags
&
3257 ((1L << PG_referenced
) |
3258 (1L << PG_swapbacked
) |
3259 (1L << PG_mlocked
) |
3260 (1L << PG_uptodate
) |
3263 (1L << PG_unevictable
) |
3267 * After clearing PageTail the gup refcount can be released.
3268 * Page flags also must be visible before we make the page non-compound.
3272 clear_compound_head(page_tail
);
3274 if (page_is_young(head
))
3275 set_page_young(page_tail
);
3276 if (page_is_idle(head
))
3277 set_page_idle(page_tail
);
3279 /* ->mapping in first tail page is compound_mapcount */
3280 VM_BUG_ON_PAGE(tail
> 2 && page_tail
->mapping
!= TAIL_MAPPING
,
3282 page_tail
->mapping
= head
->mapping
;
3284 page_tail
->index
= head
->index
+ tail
;
3285 page_cpupid_xchg_last(page_tail
, page_cpupid_last(head
));
3286 lru_add_page_tail(head
, page_tail
, lruvec
, list
);
3291 static void __split_huge_page(struct page
*page
, struct list_head
*list
)
3293 struct page
*head
= compound_head(page
);
3294 struct zone
*zone
= page_zone(head
);
3295 struct lruvec
*lruvec
;
3296 int i
, tail_mapcount
;
3298 /* prevent PageLRU to go away from under us, and freeze lru stats */
3299 spin_lock_irq(&zone
->lru_lock
);
3300 lruvec
= mem_cgroup_page_lruvec(head
, zone
);
3302 /* complete memcg works before add pages to LRU */
3303 mem_cgroup_split_huge_fixup(head
);
3306 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--)
3307 tail_mapcount
+= __split_huge_page_tail(head
, i
, lruvec
, list
);
3308 atomic_sub(tail_mapcount
, &head
->_count
);
3310 ClearPageCompound(head
);
3311 spin_unlock_irq(&zone
->lru_lock
);
3313 unfreeze_page(page_anon_vma(head
), head
);
3315 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
3316 struct page
*subpage
= head
+ i
;
3317 if (subpage
== page
)
3319 unlock_page(subpage
);
3322 * Subpages may be freed if there wasn't any mapping
3323 * like if add_to_swap() is running on a lru page that
3324 * had its mapping zapped. And freeing these pages
3325 * requires taking the lru_lock so we do the put_page
3326 * of the tail pages after the split is complete.
3332 int total_mapcount(struct page
*page
)
3336 VM_BUG_ON_PAGE(PageTail(page
), page
);
3338 if (likely(!PageCompound(page
)))
3339 return atomic_read(&page
->_mapcount
) + 1;
3341 ret
= compound_mapcount(page
);
3344 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3345 ret
+= atomic_read(&page
[i
]._mapcount
) + 1;
3346 if (PageDoubleMap(page
))
3347 ret
-= HPAGE_PMD_NR
;
3352 * This function splits huge page into normal pages. @page can point to any
3353 * subpage of huge page to split. Split doesn't change the position of @page.
3355 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3356 * The huge page must be locked.
3358 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3360 * Both head page and tail pages will inherit mapping, flags, and so on from
3363 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3364 * they are not mapped.
3366 * Returns 0 if the hugepage is split successfully.
3367 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3370 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
3372 struct page
*head
= compound_head(page
);
3373 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(head
));
3374 struct anon_vma
*anon_vma
;
3375 int count
, mapcount
, ret
;
3377 unsigned long flags
;
3379 VM_BUG_ON_PAGE(is_huge_zero_page(page
), page
);
3380 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
3381 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
3382 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
3383 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
3386 * The caller does not necessarily hold an mmap_sem that would prevent
3387 * the anon_vma disappearing so we first we take a reference to it
3388 * and then lock the anon_vma for write. This is similar to
3389 * page_lock_anon_vma_read except the write lock is taken to serialise
3390 * against parallel split or collapse operations.
3392 anon_vma
= page_get_anon_vma(head
);
3397 anon_vma_lock_write(anon_vma
);
3400 * Racy check if we can split the page, before freeze_page() will
3403 if (total_mapcount(head
) != page_count(head
) - 1) {
3408 mlocked
= PageMlocked(page
);
3409 freeze_page(anon_vma
, head
);
3410 VM_BUG_ON_PAGE(compound_mapcount(head
), head
);
3412 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3416 /* Prevent deferred_split_scan() touching ->_count */
3417 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3418 count
= page_count(head
);
3419 mapcount
= total_mapcount(head
);
3420 if (!mapcount
&& count
== 1) {
3421 if (!list_empty(page_deferred_list(head
))) {
3422 pgdata
->split_queue_len
--;
3423 list_del(page_deferred_list(head
));
3425 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3426 __split_huge_page(page
, list
);
3428 } else if (IS_ENABLED(CONFIG_DEBUG_VM
) && mapcount
) {
3429 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3430 pr_alert("total_mapcount: %u, page_count(): %u\n",
3433 dump_page(head
, NULL
);
3434 dump_page(page
, "total_mapcount(head) > 0");
3437 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3438 unfreeze_page(anon_vma
, head
);
3443 anon_vma_unlock_write(anon_vma
);
3444 put_anon_vma(anon_vma
);
3446 count_vm_event(!ret
? THP_SPLIT_PAGE
: THP_SPLIT_PAGE_FAILED
);
3450 void free_transhuge_page(struct page
*page
)
3452 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
3453 unsigned long flags
;
3455 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3456 if (!list_empty(page_deferred_list(page
))) {
3457 pgdata
->split_queue_len
--;
3458 list_del(page_deferred_list(page
));
3460 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3461 free_compound_page(page
);
3464 void deferred_split_huge_page(struct page
*page
)
3466 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
3467 unsigned long flags
;
3469 VM_BUG_ON_PAGE(!PageTransHuge(page
), page
);
3471 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3472 if (list_empty(page_deferred_list(page
))) {
3473 list_add_tail(page_deferred_list(page
), &pgdata
->split_queue
);
3474 pgdata
->split_queue_len
++;
3476 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3479 static unsigned long deferred_split_count(struct shrinker
*shrink
,
3480 struct shrink_control
*sc
)
3482 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
3483 return ACCESS_ONCE(pgdata
->split_queue_len
);
3486 static unsigned long deferred_split_scan(struct shrinker
*shrink
,
3487 struct shrink_control
*sc
)
3489 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
3490 unsigned long flags
;
3491 LIST_HEAD(list
), *pos
, *next
;
3495 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3496 /* Take pin on all head pages to avoid freeing them under us */
3497 list_for_each_safe(pos
, next
, &pgdata
->split_queue
) {
3498 page
= list_entry((void *)pos
, struct page
, mapping
);
3499 page
= compound_head(page
);
3500 if (get_page_unless_zero(page
)) {
3501 list_move(page_deferred_list(page
), &list
);
3503 /* We lost race with put_compound_page() */
3504 list_del_init(page_deferred_list(page
));
3505 pgdata
->split_queue_len
--;
3507 if (!--sc
->nr_to_scan
)
3510 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3512 list_for_each_safe(pos
, next
, &list
) {
3513 page
= list_entry((void *)pos
, struct page
, mapping
);
3515 /* split_huge_page() removes page from list on success */
3516 if (!split_huge_page(page
))
3522 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3523 list_splice_tail(&list
, &pgdata
->split_queue
);
3524 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3527 * Stop shrinker if we didn't split any page, but the queue is empty.
3528 * This can happen if pages were freed under us.
3530 if (!split
&& list_empty(&pgdata
->split_queue
))
3535 static struct shrinker deferred_split_shrinker
= {
3536 .count_objects
= deferred_split_count
,
3537 .scan_objects
= deferred_split_scan
,
3538 .seeks
= DEFAULT_SEEKS
,
3539 .flags
= SHRINKER_NUMA_AWARE
,
3542 #ifdef CONFIG_DEBUG_FS
3543 static int split_huge_pages_set(void *data
, u64 val
)
3547 unsigned long pfn
, max_zone_pfn
;
3548 unsigned long total
= 0, split
= 0;
3553 for_each_populated_zone(zone
) {
3554 max_zone_pfn
= zone_end_pfn(zone
);
3555 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++) {
3556 if (!pfn_valid(pfn
))
3559 page
= pfn_to_page(pfn
);
3560 if (!get_page_unless_zero(page
))
3563 if (zone
!= page_zone(page
))
3566 if (!PageHead(page
) || !PageAnon(page
) ||
3572 if (!split_huge_page(page
))
3580 pr_info("%lu of %lu THP split", split
, total
);
3584 DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops
, NULL
, split_huge_pages_set
,
3587 static int __init
split_huge_pages_debugfs(void)
3591 ret
= debugfs_create_file("split_huge_pages", 0644, NULL
, NULL
,
3592 &split_huge_pages_fops
);
3594 pr_warn("Failed to create split_huge_pages in debugfs");
3597 late_initcall(split_huge_pages_debugfs
);