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/mman.h>
25 #include <linux/pagemap.h>
26 #include <linux/migrate.h>
27 #include <linux/hashtable.h>
28 #include <linux/userfaultfd_k.h>
29 #include <linux/page_idle.h>
32 #include <asm/pgalloc.h>
42 SCAN_NO_REFERENCED_PAGE
,
56 SCAN_ALLOC_HUGE_PAGE_FAIL
,
57 SCAN_CGROUP_CHARGE_FAIL
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/huge_memory.h>
64 * By default transparent hugepage support is disabled in order that avoid
65 * to risk increase the memory footprint of applications without a guaranteed
66 * benefit. When transparent hugepage support is enabled, is for all mappings,
67 * and khugepaged scans all mappings.
68 * Defrag is invoked by khugepaged hugepage allocations and by page faults
69 * for all hugepage allocations.
71 unsigned long transparent_hugepage_flags __read_mostly
=
72 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
73 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
75 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
76 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
78 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
79 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
80 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
82 /* default scan 8*512 pte (or vmas) every 30 second */
83 static unsigned int khugepaged_pages_to_scan __read_mostly
= HPAGE_PMD_NR
*8;
84 static unsigned int khugepaged_pages_collapsed
;
85 static unsigned int khugepaged_full_scans
;
86 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
87 /* during fragmentation poll the hugepage allocator once every minute */
88 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
89 static struct task_struct
*khugepaged_thread __read_mostly
;
90 static DEFINE_MUTEX(khugepaged_mutex
);
91 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
92 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
94 * default collapse hugepages if there is at least one pte mapped like
95 * it would have happened if the vma was large enough during page
98 static unsigned int khugepaged_max_ptes_none __read_mostly
= HPAGE_PMD_NR
-1;
100 static int khugepaged(void *none
);
101 static int khugepaged_slab_init(void);
102 static void khugepaged_slab_exit(void);
104 #define MM_SLOTS_HASH_BITS 10
105 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
107 static struct kmem_cache
*mm_slot_cache __read_mostly
;
110 * struct mm_slot - hash lookup from mm to mm_slot
111 * @hash: hash collision list
112 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
113 * @mm: the mm that this information is valid for
116 struct hlist_node hash
;
117 struct list_head mm_node
;
118 struct mm_struct
*mm
;
122 * struct khugepaged_scan - cursor for scanning
123 * @mm_head: the head of the mm list to scan
124 * @mm_slot: the current mm_slot we are scanning
125 * @address: the next address inside that to be scanned
127 * There is only the one khugepaged_scan instance of this cursor structure.
129 struct khugepaged_scan
{
130 struct list_head mm_head
;
131 struct mm_slot
*mm_slot
;
132 unsigned long address
;
134 static struct khugepaged_scan khugepaged_scan
= {
135 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
138 static DEFINE_SPINLOCK(split_queue_lock
);
139 static LIST_HEAD(split_queue
);
140 static unsigned long split_queue_len
;
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 err
= hugepage_init_sysfs(&hugepage_kobj
);
667 err
= khugepaged_slab_init();
671 err
= register_shrinker(&huge_zero_page_shrinker
);
673 goto err_hzp_shrinker
;
674 err
= register_shrinker(&deferred_split_shrinker
);
676 goto err_split_shrinker
;
679 * By default disable transparent hugepages on smaller systems,
680 * where the extra memory used could hurt more than TLB overhead
681 * is likely to save. The admin can still enable it through /sys.
683 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
684 transparent_hugepage_flags
= 0;
688 err
= start_stop_khugepaged();
694 unregister_shrinker(&deferred_split_shrinker
);
696 unregister_shrinker(&huge_zero_page_shrinker
);
698 khugepaged_slab_exit();
700 hugepage_exit_sysfs(hugepage_kobj
);
704 subsys_initcall(hugepage_init
);
706 static int __init
setup_transparent_hugepage(char *str
)
711 if (!strcmp(str
, "always")) {
712 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
713 &transparent_hugepage_flags
);
714 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
715 &transparent_hugepage_flags
);
717 } else if (!strcmp(str
, "madvise")) {
718 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
719 &transparent_hugepage_flags
);
720 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
721 &transparent_hugepage_flags
);
723 } else if (!strcmp(str
, "never")) {
724 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
725 &transparent_hugepage_flags
);
726 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
727 &transparent_hugepage_flags
);
732 pr_warn("transparent_hugepage= cannot parse, ignored\n");
735 __setup("transparent_hugepage=", setup_transparent_hugepage
);
737 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
739 if (likely(vma
->vm_flags
& VM_WRITE
))
740 pmd
= pmd_mkwrite(pmd
);
744 static inline pmd_t
mk_huge_pmd(struct page
*page
, pgprot_t prot
)
747 entry
= mk_pmd(page
, prot
);
748 entry
= pmd_mkhuge(entry
);
752 static inline struct list_head
*page_deferred_list(struct page
*page
)
755 * ->lru in the tail pages is occupied by compound_head.
756 * Let's use ->mapping + ->index in the second tail page as list_head.
758 return (struct list_head
*)&page
[2].mapping
;
761 void prep_transhuge_page(struct page
*page
)
764 * we use page->mapping and page->indexlru in second tail page
765 * as list_head: assuming THP order >= 2
767 BUILD_BUG_ON(HPAGE_PMD_ORDER
< 2);
769 INIT_LIST_HEAD(page_deferred_list(page
));
770 set_compound_page_dtor(page
, TRANSHUGE_PAGE_DTOR
);
773 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
774 struct vm_area_struct
*vma
,
775 unsigned long address
, pmd_t
*pmd
,
776 struct page
*page
, gfp_t gfp
,
779 struct mem_cgroup
*memcg
;
782 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
784 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
786 if (mem_cgroup_try_charge(page
, mm
, gfp
, &memcg
, true)) {
788 count_vm_event(THP_FAULT_FALLBACK
);
789 return VM_FAULT_FALLBACK
;
792 pgtable
= pte_alloc_one(mm
, haddr
);
793 if (unlikely(!pgtable
)) {
794 mem_cgroup_cancel_charge(page
, memcg
, true);
799 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
801 * The memory barrier inside __SetPageUptodate makes sure that
802 * clear_huge_page writes become visible before the set_pmd_at()
805 __SetPageUptodate(page
);
807 ptl
= pmd_lock(mm
, pmd
);
808 if (unlikely(!pmd_none(*pmd
))) {
810 mem_cgroup_cancel_charge(page
, memcg
, true);
812 pte_free(mm
, pgtable
);
816 /* Deliver the page fault to userland */
817 if (userfaultfd_missing(vma
)) {
821 mem_cgroup_cancel_charge(page
, memcg
, true);
823 pte_free(mm
, pgtable
);
824 ret
= handle_userfault(vma
, address
, flags
,
826 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
830 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
831 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
832 page_add_new_anon_rmap(page
, vma
, haddr
, true);
833 mem_cgroup_commit_charge(page
, memcg
, false, true);
834 lru_cache_add_active_or_unevictable(page
, vma
);
835 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
836 set_pmd_at(mm
, haddr
, pmd
, entry
);
837 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
838 atomic_long_inc(&mm
->nr_ptes
);
840 count_vm_event(THP_FAULT_ALLOC
);
846 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
848 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_RECLAIM
)) | extra_gfp
;
851 /* Caller must hold page table lock. */
852 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
853 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
854 struct page
*zero_page
)
859 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
860 entry
= pmd_mkhuge(entry
);
861 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
862 set_pmd_at(mm
, haddr
, pmd
, entry
);
863 atomic_long_inc(&mm
->nr_ptes
);
867 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
868 unsigned long address
, pmd_t
*pmd
,
873 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
875 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
876 return VM_FAULT_FALLBACK
;
877 if (unlikely(anon_vma_prepare(vma
)))
879 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
881 if (!(flags
& FAULT_FLAG_WRITE
) && !mm_forbids_zeropage(mm
) &&
882 transparent_hugepage_use_zero_page()) {
885 struct page
*zero_page
;
888 pgtable
= pte_alloc_one(mm
, haddr
);
889 if (unlikely(!pgtable
))
891 zero_page
= get_huge_zero_page();
892 if (unlikely(!zero_page
)) {
893 pte_free(mm
, pgtable
);
894 count_vm_event(THP_FAULT_FALLBACK
);
895 return VM_FAULT_FALLBACK
;
897 ptl
= pmd_lock(mm
, pmd
);
900 if (pmd_none(*pmd
)) {
901 if (userfaultfd_missing(vma
)) {
903 ret
= handle_userfault(vma
, address
, flags
,
905 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
907 set_huge_zero_page(pgtable
, mm
, vma
,
916 pte_free(mm
, pgtable
);
917 put_huge_zero_page();
921 gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
922 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
923 if (unlikely(!page
)) {
924 count_vm_event(THP_FAULT_FALLBACK
);
925 return VM_FAULT_FALLBACK
;
927 prep_transhuge_page(page
);
928 return __do_huge_pmd_anonymous_page(mm
, vma
, address
, pmd
, page
, gfp
,
932 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
933 pmd_t
*pmd
, unsigned long pfn
, pgprot_t prot
, bool write
)
935 struct mm_struct
*mm
= vma
->vm_mm
;
939 ptl
= pmd_lock(mm
, pmd
);
940 if (pmd_none(*pmd
)) {
941 entry
= pmd_mkhuge(pfn_pmd(pfn
, prot
));
943 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
944 entry
= maybe_pmd_mkwrite(entry
, vma
);
946 set_pmd_at(mm
, addr
, pmd
, entry
);
947 update_mmu_cache_pmd(vma
, addr
, pmd
);
952 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
953 pmd_t
*pmd
, unsigned long pfn
, bool write
)
955 pgprot_t pgprot
= vma
->vm_page_prot
;
957 * If we had pmd_special, we could avoid all these restrictions,
958 * but we need to be consistent with PTEs and architectures that
959 * can't support a 'special' bit.
961 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
962 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
963 (VM_PFNMAP
|VM_MIXEDMAP
));
964 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
965 BUG_ON((vma
->vm_flags
& VM_MIXEDMAP
) && pfn_valid(pfn
));
967 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
968 return VM_FAULT_SIGBUS
;
969 if (track_pfn_insert(vma
, &pgprot
, pfn
))
970 return VM_FAULT_SIGBUS
;
971 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
972 return VM_FAULT_NOPAGE
;
975 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
976 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
977 struct vm_area_struct
*vma
)
979 spinlock_t
*dst_ptl
, *src_ptl
;
980 struct page
*src_page
;
986 pgtable
= pte_alloc_one(dst_mm
, addr
);
987 if (unlikely(!pgtable
))
990 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
991 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
992 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
996 if (unlikely(!pmd_trans_huge(pmd
))) {
997 pte_free(dst_mm
, pgtable
);
1001 * When page table lock is held, the huge zero pmd should not be
1002 * under splitting since we don't split the page itself, only pmd to
1005 if (is_huge_zero_pmd(pmd
)) {
1006 struct page
*zero_page
;
1008 * get_huge_zero_page() will never allocate a new page here,
1009 * since we already have a zero page to copy. It just takes a
1012 zero_page
= get_huge_zero_page();
1013 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
1019 src_page
= pmd_page(pmd
);
1020 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
1022 page_dup_rmap(src_page
, true);
1023 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1025 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
1026 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
1027 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
1028 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
1029 atomic_long_inc(&dst_mm
->nr_ptes
);
1033 spin_unlock(src_ptl
);
1034 spin_unlock(dst_ptl
);
1039 void huge_pmd_set_accessed(struct mm_struct
*mm
,
1040 struct vm_area_struct
*vma
,
1041 unsigned long address
,
1042 pmd_t
*pmd
, pmd_t orig_pmd
,
1047 unsigned long haddr
;
1049 ptl
= pmd_lock(mm
, pmd
);
1050 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1053 entry
= pmd_mkyoung(orig_pmd
);
1054 haddr
= address
& HPAGE_PMD_MASK
;
1055 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
1056 update_mmu_cache_pmd(vma
, address
, pmd
);
1062 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
1063 struct vm_area_struct
*vma
,
1064 unsigned long address
,
1065 pmd_t
*pmd
, pmd_t orig_pmd
,
1067 unsigned long haddr
)
1069 struct mem_cgroup
*memcg
;
1074 struct page
**pages
;
1075 unsigned long mmun_start
; /* For mmu_notifiers */
1076 unsigned long mmun_end
; /* For mmu_notifiers */
1078 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1080 if (unlikely(!pages
)) {
1081 ret
|= VM_FAULT_OOM
;
1085 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1086 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1088 vma
, address
, page_to_nid(page
));
1089 if (unlikely(!pages
[i
] ||
1090 mem_cgroup_try_charge(pages
[i
], mm
, GFP_KERNEL
,
1095 memcg
= (void *)page_private(pages
[i
]);
1096 set_page_private(pages
[i
], 0);
1097 mem_cgroup_cancel_charge(pages
[i
], memcg
,
1102 ret
|= VM_FAULT_OOM
;
1105 set_page_private(pages
[i
], (unsigned long)memcg
);
1108 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1109 copy_user_highpage(pages
[i
], page
+ i
,
1110 haddr
+ PAGE_SIZE
* i
, vma
);
1111 __SetPageUptodate(pages
[i
]);
1116 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1117 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1119 ptl
= pmd_lock(mm
, pmd
);
1120 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1121 goto out_free_pages
;
1122 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1124 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1125 /* leave pmd empty until pte is filled */
1127 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1128 pmd_populate(mm
, &_pmd
, pgtable
);
1130 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1132 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1133 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1134 memcg
= (void *)page_private(pages
[i
]);
1135 set_page_private(pages
[i
], 0);
1136 page_add_new_anon_rmap(pages
[i
], vma
, haddr
, false);
1137 mem_cgroup_commit_charge(pages
[i
], memcg
, false, false);
1138 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
1139 pte
= pte_offset_map(&_pmd
, haddr
);
1140 VM_BUG_ON(!pte_none(*pte
));
1141 set_pte_at(mm
, haddr
, pte
, entry
);
1146 smp_wmb(); /* make pte visible before pmd */
1147 pmd_populate(mm
, pmd
, pgtable
);
1148 page_remove_rmap(page
, true);
1151 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1153 ret
|= VM_FAULT_WRITE
;
1161 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1162 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1163 memcg
= (void *)page_private(pages
[i
]);
1164 set_page_private(pages
[i
], 0);
1165 mem_cgroup_cancel_charge(pages
[i
], memcg
, false);
1172 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1173 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
1177 struct page
*page
= NULL
, *new_page
;
1178 struct mem_cgroup
*memcg
;
1179 unsigned long haddr
;
1180 unsigned long mmun_start
; /* For mmu_notifiers */
1181 unsigned long mmun_end
; /* For mmu_notifiers */
1182 gfp_t huge_gfp
; /* for allocation and charge */
1184 ptl
= pmd_lockptr(mm
, pmd
);
1185 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1186 haddr
= address
& HPAGE_PMD_MASK
;
1187 if (is_huge_zero_pmd(orig_pmd
))
1190 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1193 page
= pmd_page(orig_pmd
);
1194 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1196 * We can only reuse the page if nobody else maps the huge page or it's
1197 * part. We can do it by checking page_mapcount() on each sub-page, but
1199 * The cheaper way is to check page_count() to be equal 1: every
1200 * mapcount takes page reference reference, so this way we can
1201 * guarantee, that the PMD is the only mapping.
1202 * This can give false negative if somebody pinned the page, but that's
1205 if (page_mapcount(page
) == 1 && page_count(page
) == 1) {
1207 entry
= pmd_mkyoung(orig_pmd
);
1208 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1209 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
1210 update_mmu_cache_pmd(vma
, address
, pmd
);
1211 ret
|= VM_FAULT_WRITE
;
1217 if (transparent_hugepage_enabled(vma
) &&
1218 !transparent_hugepage_debug_cow()) {
1219 huge_gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
1220 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1224 if (likely(new_page
)) {
1225 prep_transhuge_page(new_page
);
1228 split_huge_pmd(vma
, pmd
, address
);
1229 ret
|= VM_FAULT_FALLBACK
;
1231 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
1232 pmd
, orig_pmd
, page
, haddr
);
1233 if (ret
& VM_FAULT_OOM
) {
1234 split_huge_pmd(vma
, pmd
, address
);
1235 ret
|= VM_FAULT_FALLBACK
;
1239 count_vm_event(THP_FAULT_FALLBACK
);
1243 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, huge_gfp
, &memcg
,
1247 split_huge_pmd(vma
, pmd
, address
);
1250 split_huge_pmd(vma
, pmd
, address
);
1251 ret
|= VM_FAULT_FALLBACK
;
1252 count_vm_event(THP_FAULT_FALLBACK
);
1256 count_vm_event(THP_FAULT_ALLOC
);
1259 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1261 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1262 __SetPageUptodate(new_page
);
1265 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1266 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1271 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
1273 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1278 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1279 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1280 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1281 page_add_new_anon_rmap(new_page
, vma
, haddr
, true);
1282 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1283 lru_cache_add_active_or_unevictable(new_page
, vma
);
1284 set_pmd_at(mm
, haddr
, pmd
, entry
);
1285 update_mmu_cache_pmd(vma
, address
, pmd
);
1287 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1288 put_huge_zero_page();
1290 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1291 page_remove_rmap(page
, true);
1294 ret
|= VM_FAULT_WRITE
;
1298 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1306 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1311 struct mm_struct
*mm
= vma
->vm_mm
;
1312 struct page
*page
= NULL
;
1314 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1316 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1319 /* Avoid dumping huge zero page */
1320 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1321 return ERR_PTR(-EFAULT
);
1323 /* Full NUMA hinting faults to serialise migration in fault paths */
1324 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1327 page
= pmd_page(*pmd
);
1328 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1329 if (flags
& FOLL_TOUCH
) {
1332 * We should set the dirty bit only for FOLL_WRITE but
1333 * for now the dirty bit in the pmd is meaningless.
1334 * And if the dirty bit will become meaningful and
1335 * we'll only set it with FOLL_WRITE, an atomic
1336 * set_bit will be required on the pmd to set the
1337 * young bit, instead of the current set_pmd_at.
1339 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1340 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
1342 update_mmu_cache_pmd(vma
, addr
, pmd
);
1344 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1346 * We don't mlock() pte-mapped THPs. This way we can avoid
1347 * leaking mlocked pages into non-VM_LOCKED VMAs.
1349 * In most cases the pmd is the only mapping of the page as we
1350 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1351 * writable private mappings in populate_vma_page_range().
1353 * The only scenario when we have the page shared here is if we
1354 * mlocking read-only mapping shared over fork(). We skip
1355 * mlocking such pages.
1357 if (compound_mapcount(page
) == 1 && !PageDoubleMap(page
) &&
1358 page
->mapping
&& trylock_page(page
)) {
1361 mlock_vma_page(page
);
1365 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1366 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
1367 if (flags
& FOLL_GET
)
1374 /* NUMA hinting page fault entry point for trans huge pmds */
1375 int do_huge_pmd_numa_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1376 unsigned long addr
, pmd_t pmd
, pmd_t
*pmdp
)
1379 struct anon_vma
*anon_vma
= NULL
;
1381 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1382 int page_nid
= -1, this_nid
= numa_node_id();
1383 int target_nid
, last_cpupid
= -1;
1385 bool migrated
= false;
1389 /* A PROT_NONE fault should not end up here */
1390 BUG_ON(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)));
1392 ptl
= pmd_lock(mm
, pmdp
);
1393 if (unlikely(!pmd_same(pmd
, *pmdp
)))
1397 * If there are potential migrations, wait for completion and retry
1398 * without disrupting NUMA hinting information. Do not relock and
1399 * check_same as the page may no longer be mapped.
1401 if (unlikely(pmd_trans_migrating(*pmdp
))) {
1402 page
= pmd_page(*pmdp
);
1404 wait_on_page_locked(page
);
1408 page
= pmd_page(pmd
);
1409 BUG_ON(is_huge_zero_page(page
));
1410 page_nid
= page_to_nid(page
);
1411 last_cpupid
= page_cpupid_last(page
);
1412 count_vm_numa_event(NUMA_HINT_FAULTS
);
1413 if (page_nid
== this_nid
) {
1414 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1415 flags
|= TNF_FAULT_LOCAL
;
1418 /* See similar comment in do_numa_page for explanation */
1419 if (!(vma
->vm_flags
& VM_WRITE
))
1420 flags
|= TNF_NO_GROUP
;
1423 * Acquire the page lock to serialise THP migrations but avoid dropping
1424 * page_table_lock if at all possible
1426 page_locked
= trylock_page(page
);
1427 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1428 if (target_nid
== -1) {
1429 /* If the page was locked, there are no parallel migrations */
1434 /* Migration could have started since the pmd_trans_migrating check */
1437 wait_on_page_locked(page
);
1443 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1444 * to serialises splits
1448 anon_vma
= page_lock_anon_vma_read(page
);
1450 /* Confirm the PMD did not change while page_table_lock was released */
1452 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1459 /* Bail if we fail to protect against THP splits for any reason */
1460 if (unlikely(!anon_vma
)) {
1467 * Migrate the THP to the requested node, returns with page unlocked
1468 * and access rights restored.
1471 migrated
= migrate_misplaced_transhuge_page(mm
, vma
,
1472 pmdp
, pmd
, addr
, page
, target_nid
);
1474 flags
|= TNF_MIGRATED
;
1475 page_nid
= target_nid
;
1477 flags
|= TNF_MIGRATE_FAIL
;
1481 BUG_ON(!PageLocked(page
));
1482 was_writable
= pmd_write(pmd
);
1483 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1484 pmd
= pmd_mkyoung(pmd
);
1486 pmd
= pmd_mkwrite(pmd
);
1487 set_pmd_at(mm
, haddr
, pmdp
, pmd
);
1488 update_mmu_cache_pmd(vma
, addr
, pmdp
);
1495 page_unlock_anon_vma_read(anon_vma
);
1498 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, flags
);
1503 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1504 pmd_t
*pmd
, unsigned long addr
)
1509 if (!__pmd_trans_huge_lock(pmd
, vma
, &ptl
))
1512 * For architectures like ppc64 we look at deposited pgtable
1513 * when calling pmdp_huge_get_and_clear. So do the
1514 * pgtable_trans_huge_withdraw after finishing pmdp related
1517 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1519 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1520 if (vma_is_dax(vma
)) {
1522 if (is_huge_zero_pmd(orig_pmd
))
1523 put_huge_zero_page();
1524 } else if (is_huge_zero_pmd(orig_pmd
)) {
1525 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1526 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1528 put_huge_zero_page();
1530 struct page
*page
= pmd_page(orig_pmd
);
1531 page_remove_rmap(page
, true);
1532 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1533 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1534 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1535 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1536 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1538 tlb_remove_page(tlb
, page
);
1543 bool move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1544 unsigned long old_addr
,
1545 unsigned long new_addr
, unsigned long old_end
,
1546 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1548 spinlock_t
*old_ptl
, *new_ptl
;
1551 struct mm_struct
*mm
= vma
->vm_mm
;
1553 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1554 (new_addr
& ~HPAGE_PMD_MASK
) ||
1555 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1556 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1560 * The destination pmd shouldn't be established, free_pgtables()
1561 * should have release it.
1563 if (WARN_ON(!pmd_none(*new_pmd
))) {
1564 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1569 * We don't have to worry about the ordering of src and dst
1570 * ptlocks because exclusive mmap_sem prevents deadlock.
1572 if (__pmd_trans_huge_lock(old_pmd
, vma
, &old_ptl
)) {
1573 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1574 if (new_ptl
!= old_ptl
)
1575 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1576 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1577 VM_BUG_ON(!pmd_none(*new_pmd
));
1579 if (pmd_move_must_withdraw(new_ptl
, old_ptl
)) {
1581 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1582 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1584 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1585 if (new_ptl
!= old_ptl
)
1586 spin_unlock(new_ptl
);
1587 spin_unlock(old_ptl
);
1595 * - 0 if PMD could not be locked
1596 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1597 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1599 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1600 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1602 struct mm_struct
*mm
= vma
->vm_mm
;
1606 if (__pmd_trans_huge_lock(pmd
, vma
, &ptl
)) {
1608 bool preserve_write
= prot_numa
&& pmd_write(*pmd
);
1612 * Avoid trapping faults against the zero page. The read-only
1613 * data is likely to be read-cached on the local CPU and
1614 * local/remote hits to the zero page are not interesting.
1616 if (prot_numa
&& is_huge_zero_pmd(*pmd
)) {
1621 if (!prot_numa
|| !pmd_protnone(*pmd
)) {
1622 entry
= pmdp_huge_get_and_clear_notify(mm
, addr
, pmd
);
1623 entry
= pmd_modify(entry
, newprot
);
1625 entry
= pmd_mkwrite(entry
);
1627 set_pmd_at(mm
, addr
, pmd
, entry
);
1628 BUG_ON(!preserve_write
&& pmd_write(entry
));
1637 * Returns true if a given pmd maps a thp, false otherwise.
1639 * Note that if it returns true, this routine returns without unlocking page
1640 * table lock. So callers must unlock it.
1642 bool __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
,
1645 *ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1646 if (likely(pmd_trans_huge(*pmd
)))
1653 * This function returns whether a given @page is mapped onto the @address
1654 * in the virtual space of @mm.
1656 * When it's true, this function returns *pmd with holding the page table lock
1657 * and passing it back to the caller via @ptl.
1658 * If it's false, returns NULL without holding the page table lock.
1660 pmd_t
*page_check_address_pmd(struct page
*page
,
1661 struct mm_struct
*mm
,
1662 unsigned long address
,
1669 if (address
& ~HPAGE_PMD_MASK
)
1672 pgd
= pgd_offset(mm
, address
);
1673 if (!pgd_present(*pgd
))
1675 pud
= pud_offset(pgd
, address
);
1676 if (!pud_present(*pud
))
1678 pmd
= pmd_offset(pud
, address
);
1680 *ptl
= pmd_lock(mm
, pmd
);
1681 if (!pmd_present(*pmd
))
1683 if (pmd_page(*pmd
) != page
)
1685 if (pmd_trans_huge(*pmd
))
1692 #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1694 int hugepage_madvise(struct vm_area_struct
*vma
,
1695 unsigned long *vm_flags
, int advice
)
1701 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1702 * can't handle this properly after s390_enable_sie, so we simply
1703 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1705 if (mm_has_pgste(vma
->vm_mm
))
1709 * Be somewhat over-protective like KSM for now!
1711 if (*vm_flags
& VM_NO_THP
)
1713 *vm_flags
&= ~VM_NOHUGEPAGE
;
1714 *vm_flags
|= VM_HUGEPAGE
;
1716 * If the vma become good for khugepaged to scan,
1717 * register it here without waiting a page fault that
1718 * may not happen any time soon.
1720 if (unlikely(khugepaged_enter_vma_merge(vma
, *vm_flags
)))
1723 case MADV_NOHUGEPAGE
:
1725 * Be somewhat over-protective like KSM for now!
1727 if (*vm_flags
& VM_NO_THP
)
1729 *vm_flags
&= ~VM_HUGEPAGE
;
1730 *vm_flags
|= VM_NOHUGEPAGE
;
1732 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1733 * this vma even if we leave the mm registered in khugepaged if
1734 * it got registered before VM_NOHUGEPAGE was set.
1742 static int __init
khugepaged_slab_init(void)
1744 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1745 sizeof(struct mm_slot
),
1746 __alignof__(struct mm_slot
), 0, NULL
);
1753 static void __init
khugepaged_slab_exit(void)
1755 kmem_cache_destroy(mm_slot_cache
);
1758 static inline struct mm_slot
*alloc_mm_slot(void)
1760 if (!mm_slot_cache
) /* initialization failed */
1762 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1765 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1767 kmem_cache_free(mm_slot_cache
, mm_slot
);
1770 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1772 struct mm_slot
*mm_slot
;
1774 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
1775 if (mm
== mm_slot
->mm
)
1781 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1782 struct mm_slot
*mm_slot
)
1785 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
1788 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1790 return atomic_read(&mm
->mm_users
) == 0;
1793 int __khugepaged_enter(struct mm_struct
*mm
)
1795 struct mm_slot
*mm_slot
;
1798 mm_slot
= alloc_mm_slot();
1802 /* __khugepaged_exit() must not run from under us */
1803 VM_BUG_ON_MM(khugepaged_test_exit(mm
), mm
);
1804 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1805 free_mm_slot(mm_slot
);
1809 spin_lock(&khugepaged_mm_lock
);
1810 insert_to_mm_slots_hash(mm
, mm_slot
);
1812 * Insert just behind the scanning cursor, to let the area settle
1815 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
1816 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
1817 spin_unlock(&khugepaged_mm_lock
);
1819 atomic_inc(&mm
->mm_count
);
1821 wake_up_interruptible(&khugepaged_wait
);
1826 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
1827 unsigned long vm_flags
)
1829 unsigned long hstart
, hend
;
1832 * Not yet faulted in so we will register later in the
1833 * page fault if needed.
1837 /* khugepaged not yet working on file or special mappings */
1839 VM_BUG_ON_VMA(vm_flags
& VM_NO_THP
, vma
);
1840 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1841 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1843 return khugepaged_enter(vma
, vm_flags
);
1847 void __khugepaged_exit(struct mm_struct
*mm
)
1849 struct mm_slot
*mm_slot
;
1852 spin_lock(&khugepaged_mm_lock
);
1853 mm_slot
= get_mm_slot(mm
);
1854 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
1855 hash_del(&mm_slot
->hash
);
1856 list_del(&mm_slot
->mm_node
);
1859 spin_unlock(&khugepaged_mm_lock
);
1862 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
1863 free_mm_slot(mm_slot
);
1865 } else if (mm_slot
) {
1867 * This is required to serialize against
1868 * khugepaged_test_exit() (which is guaranteed to run
1869 * under mmap sem read mode). Stop here (after we
1870 * return all pagetables will be destroyed) until
1871 * khugepaged has finished working on the pagetables
1872 * under the mmap_sem.
1874 down_write(&mm
->mmap_sem
);
1875 up_write(&mm
->mmap_sem
);
1879 static void release_pte_page(struct page
*page
)
1881 /* 0 stands for page_is_file_cache(page) == false */
1882 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1884 putback_lru_page(page
);
1887 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
1889 while (--_pte
>= pte
) {
1890 pte_t pteval
= *_pte
;
1891 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
1892 release_pte_page(pte_page(pteval
));
1896 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
1897 unsigned long address
,
1900 struct page
*page
= NULL
;
1902 int none_or_zero
= 0, result
= 0;
1903 bool referenced
= false, writable
= false;
1905 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
1906 _pte
++, address
+= PAGE_SIZE
) {
1907 pte_t pteval
= *_pte
;
1908 if (pte_none(pteval
) || (pte_present(pteval
) &&
1909 is_zero_pfn(pte_pfn(pteval
)))) {
1910 if (!userfaultfd_armed(vma
) &&
1911 ++none_or_zero
<= khugepaged_max_ptes_none
) {
1914 result
= SCAN_EXCEED_NONE_PTE
;
1918 if (!pte_present(pteval
)) {
1919 result
= SCAN_PTE_NON_PRESENT
;
1922 page
= vm_normal_page(vma
, address
, pteval
);
1923 if (unlikely(!page
)) {
1924 result
= SCAN_PAGE_NULL
;
1928 VM_BUG_ON_PAGE(PageCompound(page
), page
);
1929 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
1930 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
1933 * We can do it before isolate_lru_page because the
1934 * page can't be freed from under us. NOTE: PG_lock
1935 * is needed to serialize against split_huge_page
1936 * when invoked from the VM.
1938 if (!trylock_page(page
)) {
1939 result
= SCAN_PAGE_LOCK
;
1944 * cannot use mapcount: can't collapse if there's a gup pin.
1945 * The page must only be referenced by the scanned process
1946 * and page swap cache.
1948 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
1950 result
= SCAN_PAGE_COUNT
;
1953 if (pte_write(pteval
)) {
1956 if (PageSwapCache(page
) && !reuse_swap_page(page
)) {
1958 result
= SCAN_SWAP_CACHE_PAGE
;
1962 * Page is not in the swap cache. It can be collapsed
1968 * Isolate the page to avoid collapsing an hugepage
1969 * currently in use by the VM.
1971 if (isolate_lru_page(page
)) {
1973 result
= SCAN_DEL_PAGE_LRU
;
1976 /* 0 stands for page_is_file_cache(page) == false */
1977 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
1978 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
1979 VM_BUG_ON_PAGE(PageLRU(page
), page
);
1981 /* If there is no mapped pte young don't collapse the page */
1982 if (pte_young(pteval
) ||
1983 page_is_young(page
) || PageReferenced(page
) ||
1984 mmu_notifier_test_young(vma
->vm_mm
, address
))
1987 if (likely(writable
)) {
1988 if (likely(referenced
)) {
1989 result
= SCAN_SUCCEED
;
1990 trace_mm_collapse_huge_page_isolate(page_to_pfn(page
), none_or_zero
,
1991 referenced
, writable
, result
);
1995 result
= SCAN_PAGE_RO
;
1999 release_pte_pages(pte
, _pte
);
2000 trace_mm_collapse_huge_page_isolate(page_to_pfn(page
), none_or_zero
,
2001 referenced
, writable
, result
);
2005 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2006 struct vm_area_struct
*vma
,
2007 unsigned long address
,
2011 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2012 pte_t pteval
= *_pte
;
2013 struct page
*src_page
;
2015 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2016 clear_user_highpage(page
, address
);
2017 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2018 if (is_zero_pfn(pte_pfn(pteval
))) {
2020 * ptl mostly unnecessary.
2024 * paravirt calls inside pte_clear here are
2027 pte_clear(vma
->vm_mm
, address
, _pte
);
2031 src_page
= pte_page(pteval
);
2032 copy_user_highpage(page
, src_page
, address
, vma
);
2033 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
2034 release_pte_page(src_page
);
2036 * ptl mostly unnecessary, but preempt has to
2037 * be disabled to update the per-cpu stats
2038 * inside page_remove_rmap().
2042 * paravirt calls inside pte_clear here are
2045 pte_clear(vma
->vm_mm
, address
, _pte
);
2046 page_remove_rmap(src_page
, false);
2048 free_page_and_swap_cache(src_page
);
2051 address
+= PAGE_SIZE
;
2056 static void khugepaged_alloc_sleep(void)
2060 add_wait_queue(&khugepaged_wait
, &wait
);
2061 freezable_schedule_timeout_interruptible(
2062 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2063 remove_wait_queue(&khugepaged_wait
, &wait
);
2066 static int khugepaged_node_load
[MAX_NUMNODES
];
2068 static bool khugepaged_scan_abort(int nid
)
2073 * If zone_reclaim_mode is disabled, then no extra effort is made to
2074 * allocate memory locally.
2076 if (!zone_reclaim_mode
)
2079 /* If there is a count for this node already, it must be acceptable */
2080 if (khugepaged_node_load
[nid
])
2083 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
2084 if (!khugepaged_node_load
[i
])
2086 if (node_distance(nid
, i
) > RECLAIM_DISTANCE
)
2093 static int khugepaged_find_target_node(void)
2095 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
2096 int nid
, target_node
= 0, max_value
= 0;
2098 /* find first node with max normal pages hit */
2099 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
2100 if (khugepaged_node_load
[nid
] > max_value
) {
2101 max_value
= khugepaged_node_load
[nid
];
2105 /* do some balance if several nodes have the same hit record */
2106 if (target_node
<= last_khugepaged_target_node
)
2107 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
2109 if (max_value
== khugepaged_node_load
[nid
]) {
2114 last_khugepaged_target_node
= target_node
;
2118 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2120 if (IS_ERR(*hpage
)) {
2126 khugepaged_alloc_sleep();
2127 } else if (*hpage
) {
2135 static struct page
*
2136 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2137 unsigned long address
, int node
)
2139 VM_BUG_ON_PAGE(*hpage
, *hpage
);
2142 * Before allocating the hugepage, release the mmap_sem read lock.
2143 * The allocation can take potentially a long time if it involves
2144 * sync compaction, and we do not need to hold the mmap_sem during
2145 * that. We will recheck the vma after taking it again in write mode.
2147 up_read(&mm
->mmap_sem
);
2149 *hpage
= __alloc_pages_node(node
, gfp
, HPAGE_PMD_ORDER
);
2150 if (unlikely(!*hpage
)) {
2151 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2152 *hpage
= ERR_PTR(-ENOMEM
);
2156 prep_transhuge_page(*hpage
);
2157 count_vm_event(THP_COLLAPSE_ALLOC
);
2161 static int khugepaged_find_target_node(void)
2166 static inline struct page
*alloc_hugepage(int defrag
)
2170 page
= alloc_pages(alloc_hugepage_gfpmask(defrag
, 0), HPAGE_PMD_ORDER
);
2172 prep_transhuge_page(page
);
2176 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2181 hpage
= alloc_hugepage(khugepaged_defrag());
2183 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2188 khugepaged_alloc_sleep();
2190 count_vm_event(THP_COLLAPSE_ALLOC
);
2191 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2196 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2199 *hpage
= khugepaged_alloc_hugepage(wait
);
2201 if (unlikely(!*hpage
))
2207 static struct page
*
2208 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2209 unsigned long address
, int node
)
2211 up_read(&mm
->mmap_sem
);
2218 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2220 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2221 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2223 if (!vma
->anon_vma
|| vma
->vm_ops
)
2225 if (is_vma_temporary_stack(vma
))
2227 VM_BUG_ON_VMA(vma
->vm_flags
& VM_NO_THP
, vma
);
2231 static void collapse_huge_page(struct mm_struct
*mm
,
2232 unsigned long address
,
2233 struct page
**hpage
,
2234 struct vm_area_struct
*vma
,
2240 struct page
*new_page
;
2241 spinlock_t
*pmd_ptl
, *pte_ptl
;
2242 int isolated
, result
= 0;
2243 unsigned long hstart
, hend
;
2244 struct mem_cgroup
*memcg
;
2245 unsigned long mmun_start
; /* For mmu_notifiers */
2246 unsigned long mmun_end
; /* For mmu_notifiers */
2249 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2251 /* Only allocate from the target node */
2252 gfp
= alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE
) |
2255 /* release the mmap_sem read lock. */
2256 new_page
= khugepaged_alloc_page(hpage
, gfp
, mm
, address
, node
);
2258 result
= SCAN_ALLOC_HUGE_PAGE_FAIL
;
2262 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, gfp
, &memcg
, true))) {
2263 result
= SCAN_CGROUP_CHARGE_FAIL
;
2268 * Prevent all access to pagetables with the exception of
2269 * gup_fast later hanlded by the ptep_clear_flush and the VM
2270 * handled by the anon_vma lock + PG_lock.
2272 down_write(&mm
->mmap_sem
);
2273 if (unlikely(khugepaged_test_exit(mm
))) {
2274 result
= SCAN_ANY_PROCESS
;
2278 vma
= find_vma(mm
, address
);
2280 result
= SCAN_VMA_NULL
;
2283 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2284 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2285 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
) {
2286 result
= SCAN_ADDRESS_RANGE
;
2289 if (!hugepage_vma_check(vma
)) {
2290 result
= SCAN_VMA_CHECK
;
2293 pmd
= mm_find_pmd(mm
, address
);
2295 result
= SCAN_PMD_NULL
;
2299 anon_vma_lock_write(vma
->anon_vma
);
2301 pte
= pte_offset_map(pmd
, address
);
2302 pte_ptl
= pte_lockptr(mm
, pmd
);
2304 mmun_start
= address
;
2305 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2306 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2307 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
2309 * After this gup_fast can't run anymore. This also removes
2310 * any huge TLB entry from the CPU so we won't allow
2311 * huge and small TLB entries for the same virtual address
2312 * to avoid the risk of CPU bugs in that area.
2314 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
2315 spin_unlock(pmd_ptl
);
2316 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2319 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2320 spin_unlock(pte_ptl
);
2322 if (unlikely(!isolated
)) {
2325 BUG_ON(!pmd_none(*pmd
));
2327 * We can only use set_pmd_at when establishing
2328 * hugepmds and never for establishing regular pmds that
2329 * points to regular pagetables. Use pmd_populate for that
2331 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
2332 spin_unlock(pmd_ptl
);
2333 anon_vma_unlock_write(vma
->anon_vma
);
2339 * All pages are isolated and locked so anon_vma rmap
2340 * can't run anymore.
2342 anon_vma_unlock_write(vma
->anon_vma
);
2344 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
2346 __SetPageUptodate(new_page
);
2347 pgtable
= pmd_pgtable(_pmd
);
2349 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2350 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
2353 * spin_lock() below is not the equivalent of smp_wmb(), so
2354 * this is needed to avoid the copy_huge_page writes to become
2355 * visible after the set_pmd_at() write.
2360 BUG_ON(!pmd_none(*pmd
));
2361 page_add_new_anon_rmap(new_page
, vma
, address
, true);
2362 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
2363 lru_cache_add_active_or_unevictable(new_page
, vma
);
2364 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
2365 set_pmd_at(mm
, address
, pmd
, _pmd
);
2366 update_mmu_cache_pmd(vma
, address
, pmd
);
2367 spin_unlock(pmd_ptl
);
2371 khugepaged_pages_collapsed
++;
2372 result
= SCAN_SUCCEED
;
2374 up_write(&mm
->mmap_sem
);
2375 trace_mm_collapse_huge_page(mm
, isolated
, result
);
2379 trace_mm_collapse_huge_page(mm
, isolated
, result
);
2382 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2386 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2387 struct vm_area_struct
*vma
,
2388 unsigned long address
,
2389 struct page
**hpage
)
2393 int ret
= 0, none_or_zero
= 0, result
= 0;
2394 struct page
*page
= NULL
;
2395 unsigned long _address
;
2397 int node
= NUMA_NO_NODE
;
2398 bool writable
= false, referenced
= false;
2400 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2402 pmd
= mm_find_pmd(mm
, address
);
2404 result
= SCAN_PMD_NULL
;
2408 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
2409 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2410 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2411 _pte
++, _address
+= PAGE_SIZE
) {
2412 pte_t pteval
= *_pte
;
2413 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2414 if (!userfaultfd_armed(vma
) &&
2415 ++none_or_zero
<= khugepaged_max_ptes_none
) {
2418 result
= SCAN_EXCEED_NONE_PTE
;
2422 if (!pte_present(pteval
)) {
2423 result
= SCAN_PTE_NON_PRESENT
;
2426 if (pte_write(pteval
))
2429 page
= vm_normal_page(vma
, _address
, pteval
);
2430 if (unlikely(!page
)) {
2431 result
= SCAN_PAGE_NULL
;
2435 /* TODO: teach khugepaged to collapse THP mapped with pte */
2436 if (PageCompound(page
)) {
2437 result
= SCAN_PAGE_COMPOUND
;
2442 * Record which node the original page is from and save this
2443 * information to khugepaged_node_load[].
2444 * Khupaged will allocate hugepage from the node has the max
2447 node
= page_to_nid(page
);
2448 if (khugepaged_scan_abort(node
)) {
2449 result
= SCAN_SCAN_ABORT
;
2452 khugepaged_node_load
[node
]++;
2453 if (!PageLRU(page
)) {
2454 result
= SCAN_SCAN_ABORT
;
2457 if (PageLocked(page
)) {
2458 result
= SCAN_PAGE_LOCK
;
2461 if (!PageAnon(page
)) {
2462 result
= SCAN_PAGE_ANON
;
2467 * cannot use mapcount: can't collapse if there's a gup pin.
2468 * The page must only be referenced by the scanned process
2469 * and page swap cache.
2471 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2472 result
= SCAN_PAGE_COUNT
;
2475 if (pte_young(pteval
) ||
2476 page_is_young(page
) || PageReferenced(page
) ||
2477 mmu_notifier_test_young(vma
->vm_mm
, address
))
2482 result
= SCAN_SUCCEED
;
2485 result
= SCAN_NO_REFERENCED_PAGE
;
2488 result
= SCAN_PAGE_RO
;
2491 pte_unmap_unlock(pte
, ptl
);
2493 node
= khugepaged_find_target_node();
2494 /* collapse_huge_page will return with the mmap_sem released */
2495 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2498 trace_mm_khugepaged_scan_pmd(mm
, page_to_pfn(page
), writable
, referenced
,
2499 none_or_zero
, result
);
2503 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2505 struct mm_struct
*mm
= mm_slot
->mm
;
2507 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2509 if (khugepaged_test_exit(mm
)) {
2511 hash_del(&mm_slot
->hash
);
2512 list_del(&mm_slot
->mm_node
);
2515 * Not strictly needed because the mm exited already.
2517 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2520 /* khugepaged_mm_lock actually not necessary for the below */
2521 free_mm_slot(mm_slot
);
2526 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2527 struct page
**hpage
)
2528 __releases(&khugepaged_mm_lock
)
2529 __acquires(&khugepaged_mm_lock
)
2531 struct mm_slot
*mm_slot
;
2532 struct mm_struct
*mm
;
2533 struct vm_area_struct
*vma
;
2537 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2539 if (khugepaged_scan
.mm_slot
)
2540 mm_slot
= khugepaged_scan
.mm_slot
;
2542 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2543 struct mm_slot
, mm_node
);
2544 khugepaged_scan
.address
= 0;
2545 khugepaged_scan
.mm_slot
= mm_slot
;
2547 spin_unlock(&khugepaged_mm_lock
);
2550 down_read(&mm
->mmap_sem
);
2551 if (unlikely(khugepaged_test_exit(mm
)))
2554 vma
= find_vma(mm
, khugepaged_scan
.address
);
2557 for (; vma
; vma
= vma
->vm_next
) {
2558 unsigned long hstart
, hend
;
2561 if (unlikely(khugepaged_test_exit(mm
))) {
2565 if (!hugepage_vma_check(vma
)) {
2570 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2571 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2574 if (khugepaged_scan
.address
> hend
)
2576 if (khugepaged_scan
.address
< hstart
)
2577 khugepaged_scan
.address
= hstart
;
2578 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2580 while (khugepaged_scan
.address
< hend
) {
2583 if (unlikely(khugepaged_test_exit(mm
)))
2584 goto breakouterloop
;
2586 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2587 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2589 ret
= khugepaged_scan_pmd(mm
, vma
,
2590 khugepaged_scan
.address
,
2592 /* move to next address */
2593 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2594 progress
+= HPAGE_PMD_NR
;
2596 /* we released mmap_sem so break loop */
2597 goto breakouterloop_mmap_sem
;
2598 if (progress
>= pages
)
2599 goto breakouterloop
;
2603 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2604 breakouterloop_mmap_sem
:
2606 spin_lock(&khugepaged_mm_lock
);
2607 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2609 * Release the current mm_slot if this mm is about to die, or
2610 * if we scanned all vmas of this mm.
2612 if (khugepaged_test_exit(mm
) || !vma
) {
2614 * Make sure that if mm_users is reaching zero while
2615 * khugepaged runs here, khugepaged_exit will find
2616 * mm_slot not pointing to the exiting mm.
2618 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2619 khugepaged_scan
.mm_slot
= list_entry(
2620 mm_slot
->mm_node
.next
,
2621 struct mm_slot
, mm_node
);
2622 khugepaged_scan
.address
= 0;
2624 khugepaged_scan
.mm_slot
= NULL
;
2625 khugepaged_full_scans
++;
2628 collect_mm_slot(mm_slot
);
2634 static int khugepaged_has_work(void)
2636 return !list_empty(&khugepaged_scan
.mm_head
) &&
2637 khugepaged_enabled();
2640 static int khugepaged_wait_event(void)
2642 return !list_empty(&khugepaged_scan
.mm_head
) ||
2643 kthread_should_stop();
2646 static void khugepaged_do_scan(void)
2648 struct page
*hpage
= NULL
;
2649 unsigned int progress
= 0, pass_through_head
= 0;
2650 unsigned int pages
= khugepaged_pages_to_scan
;
2653 barrier(); /* write khugepaged_pages_to_scan to local stack */
2655 while (progress
< pages
) {
2656 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2661 if (unlikely(kthread_should_stop() || try_to_freeze()))
2664 spin_lock(&khugepaged_mm_lock
);
2665 if (!khugepaged_scan
.mm_slot
)
2666 pass_through_head
++;
2667 if (khugepaged_has_work() &&
2668 pass_through_head
< 2)
2669 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2673 spin_unlock(&khugepaged_mm_lock
);
2676 if (!IS_ERR_OR_NULL(hpage
))
2680 static void khugepaged_wait_work(void)
2682 if (khugepaged_has_work()) {
2683 if (!khugepaged_scan_sleep_millisecs
)
2686 wait_event_freezable_timeout(khugepaged_wait
,
2687 kthread_should_stop(),
2688 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2692 if (khugepaged_enabled())
2693 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2696 static int khugepaged(void *none
)
2698 struct mm_slot
*mm_slot
;
2701 set_user_nice(current
, MAX_NICE
);
2703 while (!kthread_should_stop()) {
2704 khugepaged_do_scan();
2705 khugepaged_wait_work();
2708 spin_lock(&khugepaged_mm_lock
);
2709 mm_slot
= khugepaged_scan
.mm_slot
;
2710 khugepaged_scan
.mm_slot
= NULL
;
2712 collect_mm_slot(mm_slot
);
2713 spin_unlock(&khugepaged_mm_lock
);
2717 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2718 unsigned long haddr
, pmd_t
*pmd
)
2720 struct mm_struct
*mm
= vma
->vm_mm
;
2725 /* leave pmd empty until pte is filled */
2726 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2728 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2729 pmd_populate(mm
, &_pmd
, pgtable
);
2731 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2733 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2734 entry
= pte_mkspecial(entry
);
2735 pte
= pte_offset_map(&_pmd
, haddr
);
2736 VM_BUG_ON(!pte_none(*pte
));
2737 set_pte_at(mm
, haddr
, pte
, entry
);
2740 smp_wmb(); /* make pte visible before pmd */
2741 pmd_populate(mm
, pmd
, pgtable
);
2742 put_huge_zero_page();
2745 static void __split_huge_pmd_locked(struct vm_area_struct
*vma
, pmd_t
*pmd
,
2746 unsigned long haddr
, bool freeze
)
2748 struct mm_struct
*mm
= vma
->vm_mm
;
2755 VM_BUG_ON(haddr
& ~HPAGE_PMD_MASK
);
2756 VM_BUG_ON_VMA(vma
->vm_start
> haddr
, vma
);
2757 VM_BUG_ON_VMA(vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
, vma
);
2758 VM_BUG_ON(!pmd_trans_huge(*pmd
));
2760 count_vm_event(THP_SPLIT_PMD
);
2762 if (vma_is_dax(vma
)) {
2763 pmd_t _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2764 if (is_huge_zero_pmd(_pmd
))
2765 put_huge_zero_page();
2767 } else if (is_huge_zero_pmd(*pmd
)) {
2768 return __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
2771 page
= pmd_page(*pmd
);
2772 VM_BUG_ON_PAGE(!page_count(page
), page
);
2773 atomic_add(HPAGE_PMD_NR
- 1, &page
->_count
);
2774 write
= pmd_write(*pmd
);
2775 young
= pmd_young(*pmd
);
2777 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2778 pmd_populate(mm
, &_pmd
, pgtable
);
2780 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2783 * Note that NUMA hinting access restrictions are not
2784 * transferred to avoid any possibility of altering
2785 * permissions across VMAs.
2788 swp_entry_t swp_entry
;
2789 swp_entry
= make_migration_entry(page
+ i
, write
);
2790 entry
= swp_entry_to_pte(swp_entry
);
2792 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
2793 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
2795 entry
= pte_wrprotect(entry
);
2797 entry
= pte_mkold(entry
);
2799 pte
= pte_offset_map(&_pmd
, haddr
);
2800 BUG_ON(!pte_none(*pte
));
2801 set_pte_at(mm
, haddr
, pte
, entry
);
2802 atomic_inc(&page
[i
]._mapcount
);
2807 * Set PG_double_map before dropping compound_mapcount to avoid
2808 * false-negative page_mapped().
2810 if (compound_mapcount(page
) > 1 && !TestSetPageDoubleMap(page
)) {
2811 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
2812 atomic_inc(&page
[i
]._mapcount
);
2815 if (atomic_add_negative(-1, compound_mapcount_ptr(page
))) {
2816 /* Last compound_mapcount is gone. */
2817 __dec_zone_page_state(page
, NR_ANON_TRANSPARENT_HUGEPAGES
);
2818 if (TestClearPageDoubleMap(page
)) {
2819 /* No need in mapcount reference anymore */
2820 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
2821 atomic_dec(&page
[i
]._mapcount
);
2825 smp_wmb(); /* make pte visible before pmd */
2827 * Up to this point the pmd is present and huge and userland has the
2828 * whole access to the hugepage during the split (which happens in
2829 * place). If we overwrite the pmd with the not-huge version pointing
2830 * to the pte here (which of course we could if all CPUs were bug
2831 * free), userland could trigger a small page size TLB miss on the
2832 * small sized TLB while the hugepage TLB entry is still established in
2833 * the huge TLB. Some CPU doesn't like that.
2834 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2835 * 383 on page 93. Intel should be safe but is also warns that it's
2836 * only safe if the permission and cache attributes of the two entries
2837 * loaded in the two TLB is identical (which should be the case here).
2838 * But it is generally safer to never allow small and huge TLB entries
2839 * for the same virtual address to be loaded simultaneously. So instead
2840 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2841 * current pmd notpresent (atomically because here the pmd_trans_huge
2842 * and pmd_trans_splitting must remain set at all times on the pmd
2843 * until the split is complete for this pmd), then we flush the SMP TLB
2844 * and finally we write the non-huge version of the pmd entry with
2847 pmdp_invalidate(vma
, haddr
, pmd
);
2848 pmd_populate(mm
, pmd
, pgtable
);
2851 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2852 page_remove_rmap(page
+ i
, false);
2858 void __split_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
2859 unsigned long address
)
2862 struct mm_struct
*mm
= vma
->vm_mm
;
2863 struct page
*page
= NULL
;
2864 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
2866 mmu_notifier_invalidate_range_start(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
2867 ptl
= pmd_lock(mm
, pmd
);
2868 if (unlikely(!pmd_trans_huge(*pmd
)))
2870 page
= pmd_page(*pmd
);
2871 __split_huge_pmd_locked(vma
, pmd
, haddr
, false);
2872 if (PageMlocked(page
))
2878 mmu_notifier_invalidate_range_end(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
2881 munlock_vma_page(page
);
2887 static void split_huge_pmd_address(struct vm_area_struct
*vma
,
2888 unsigned long address
)
2894 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
2896 pgd
= pgd_offset(vma
->vm_mm
, address
);
2897 if (!pgd_present(*pgd
))
2900 pud
= pud_offset(pgd
, address
);
2901 if (!pud_present(*pud
))
2904 pmd
= pmd_offset(pud
, address
);
2905 if (!pmd_present(*pmd
) || !pmd_trans_huge(*pmd
))
2908 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2909 * materialize from under us.
2911 split_huge_pmd(vma
, pmd
, address
);
2914 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
2915 unsigned long start
,
2920 * If the new start address isn't hpage aligned and it could
2921 * previously contain an hugepage: check if we need to split
2924 if (start
& ~HPAGE_PMD_MASK
&&
2925 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2926 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2927 split_huge_pmd_address(vma
, start
);
2930 * If the new end address isn't hpage aligned and it could
2931 * previously contain an hugepage: check if we need to split
2934 if (end
& ~HPAGE_PMD_MASK
&&
2935 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
2936 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
2937 split_huge_pmd_address(vma
, end
);
2940 * If we're also updating the vma->vm_next->vm_start, if the new
2941 * vm_next->vm_start isn't page aligned and it could previously
2942 * contain an hugepage: check if we need to split an huge pmd.
2944 if (adjust_next
> 0) {
2945 struct vm_area_struct
*next
= vma
->vm_next
;
2946 unsigned long nstart
= next
->vm_start
;
2947 nstart
+= adjust_next
<< PAGE_SHIFT
;
2948 if (nstart
& ~HPAGE_PMD_MASK
&&
2949 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
2950 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
2951 split_huge_pmd_address(next
, nstart
);
2955 static void freeze_page_vma(struct vm_area_struct
*vma
, struct page
*page
,
2956 unsigned long address
)
2963 int i
, nr
= HPAGE_PMD_NR
;
2965 /* Skip pages which doesn't belong to the VMA */
2966 if (address
< vma
->vm_start
) {
2967 int off
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
2970 address
= vma
->vm_start
;
2973 pgd
= pgd_offset(vma
->vm_mm
, address
);
2974 if (!pgd_present(*pgd
))
2976 pud
= pud_offset(pgd
, address
);
2977 if (!pud_present(*pud
))
2979 pmd
= pmd_offset(pud
, address
);
2980 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
2981 if (!pmd_present(*pmd
)) {
2985 if (pmd_trans_huge(*pmd
)) {
2986 if (page
== pmd_page(*pmd
))
2987 __split_huge_pmd_locked(vma
, pmd
, address
, true);
2993 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, address
, &ptl
);
2994 for (i
= 0; i
< nr
; i
++, address
+= PAGE_SIZE
, page
++) {
2995 pte_t entry
, swp_pte
;
2996 swp_entry_t swp_entry
;
2998 if (!pte_present(pte
[i
]))
3000 if (page_to_pfn(page
) != pte_pfn(pte
[i
]))
3002 flush_cache_page(vma
, address
, page_to_pfn(page
));
3003 entry
= ptep_clear_flush(vma
, address
, pte
+ i
);
3004 swp_entry
= make_migration_entry(page
, pte_write(entry
));
3005 swp_pte
= swp_entry_to_pte(swp_entry
);
3006 if (pte_soft_dirty(entry
))
3007 swp_pte
= pte_swp_mksoft_dirty(swp_pte
);
3008 set_pte_at(vma
->vm_mm
, address
, pte
+ i
, swp_pte
);
3009 page_remove_rmap(page
, false);
3012 pte_unmap_unlock(pte
, ptl
);
3015 static void freeze_page(struct anon_vma
*anon_vma
, struct page
*page
)
3017 struct anon_vma_chain
*avc
;
3018 pgoff_t pgoff
= page_to_pgoff(page
);
3020 VM_BUG_ON_PAGE(!PageHead(page
), page
);
3022 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
,
3023 pgoff
+ HPAGE_PMD_NR
- 1) {
3024 unsigned long haddr
;
3026 haddr
= __vma_address(page
, avc
->vma
) & HPAGE_PMD_MASK
;
3027 mmu_notifier_invalidate_range_start(avc
->vma
->vm_mm
,
3028 haddr
, haddr
+ HPAGE_PMD_SIZE
);
3029 freeze_page_vma(avc
->vma
, page
, haddr
);
3030 mmu_notifier_invalidate_range_end(avc
->vma
->vm_mm
,
3031 haddr
, haddr
+ HPAGE_PMD_SIZE
);
3035 static void unfreeze_page_vma(struct vm_area_struct
*vma
, struct page
*page
,
3036 unsigned long address
)
3041 swp_entry_t swp_entry
;
3042 int i
, nr
= HPAGE_PMD_NR
;
3044 /* Skip pages which doesn't belong to the VMA */
3045 if (address
< vma
->vm_start
) {
3046 int off
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
3049 address
= vma
->vm_start
;
3052 pmd
= mm_find_pmd(vma
->vm_mm
, address
);
3055 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, address
, &ptl
);
3056 for (i
= 0; i
< nr
; i
++, address
+= PAGE_SIZE
, page
++) {
3057 if (!is_swap_pte(pte
[i
]))
3060 swp_entry
= pte_to_swp_entry(pte
[i
]);
3061 if (!is_migration_entry(swp_entry
))
3063 if (migration_entry_to_page(swp_entry
) != page
)
3067 page_add_anon_rmap(page
, vma
, address
, false);
3069 entry
= pte_mkold(mk_pte(page
, vma
->vm_page_prot
));
3070 entry
= pte_mkdirty(entry
);
3071 if (is_write_migration_entry(swp_entry
))
3072 entry
= maybe_mkwrite(entry
, vma
);
3074 flush_dcache_page(page
);
3075 set_pte_at(vma
->vm_mm
, address
, pte
+ i
, entry
);
3077 /* No need to invalidate - it was non-present before */
3078 update_mmu_cache(vma
, address
, pte
+ i
);
3080 pte_unmap_unlock(pte
, ptl
);
3083 static void unfreeze_page(struct anon_vma
*anon_vma
, struct page
*page
)
3085 struct anon_vma_chain
*avc
;
3086 pgoff_t pgoff
= page_to_pgoff(page
);
3088 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
,
3089 pgoff
, pgoff
+ HPAGE_PMD_NR
- 1) {
3090 unsigned long address
= __vma_address(page
, avc
->vma
);
3092 mmu_notifier_invalidate_range_start(avc
->vma
->vm_mm
,
3093 address
, address
+ HPAGE_PMD_SIZE
);
3094 unfreeze_page_vma(avc
->vma
, page
, address
);
3095 mmu_notifier_invalidate_range_end(avc
->vma
->vm_mm
,
3096 address
, address
+ HPAGE_PMD_SIZE
);
3100 static int total_mapcount(struct page
*page
)
3104 ret
= compound_mapcount(page
);
3105 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3106 ret
+= atomic_read(&page
[i
]._mapcount
) + 1;
3108 if (PageDoubleMap(page
))
3109 ret
-= HPAGE_PMD_NR
;
3114 static int __split_huge_page_tail(struct page
*head
, int tail
,
3115 struct lruvec
*lruvec
, struct list_head
*list
)
3118 struct page
*page_tail
= head
+ tail
;
3120 mapcount
= atomic_read(&page_tail
->_mapcount
) + 1;
3121 VM_BUG_ON_PAGE(atomic_read(&page_tail
->_count
) != 0, page_tail
);
3124 * tail_page->_count is zero and not changing from under us. But
3125 * get_page_unless_zero() may be running from under us on the
3126 * tail_page. If we used atomic_set() below instead of atomic_add(), we
3127 * would then run atomic_set() concurrently with
3128 * get_page_unless_zero(), and atomic_set() is implemented in C not
3129 * using locked ops. spin_unlock on x86 sometime uses locked ops
3130 * because of PPro errata 66, 92, so unless somebody can guarantee
3131 * atomic_set() here would be safe on all archs (and not only on x86),
3132 * it's safer to use atomic_add().
3134 atomic_add(mapcount
+ 1, &page_tail
->_count
);
3137 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
3138 page_tail
->flags
|= (head
->flags
&
3139 ((1L << PG_referenced
) |
3140 (1L << PG_swapbacked
) |
3141 (1L << PG_mlocked
) |
3142 (1L << PG_uptodate
) |
3145 (1L << PG_unevictable
)));
3146 page_tail
->flags
|= (1L << PG_dirty
);
3149 * After clearing PageTail the gup refcount can be released.
3150 * Page flags also must be visible before we make the page non-compound.
3154 clear_compound_head(page_tail
);
3156 if (page_is_young(head
))
3157 set_page_young(page_tail
);
3158 if (page_is_idle(head
))
3159 set_page_idle(page_tail
);
3161 /* ->mapping in first tail page is compound_mapcount */
3162 VM_BUG_ON_PAGE(tail
> 2 && page_tail
->mapping
!= TAIL_MAPPING
,
3164 page_tail
->mapping
= head
->mapping
;
3166 page_tail
->index
= head
->index
+ tail
;
3167 page_cpupid_xchg_last(page_tail
, page_cpupid_last(head
));
3168 lru_add_page_tail(head
, page_tail
, lruvec
, list
);
3173 static void __split_huge_page(struct page
*page
, struct list_head
*list
)
3175 struct page
*head
= compound_head(page
);
3176 struct zone
*zone
= page_zone(head
);
3177 struct lruvec
*lruvec
;
3178 int i
, tail_mapcount
;
3180 /* prevent PageLRU to go away from under us, and freeze lru stats */
3181 spin_lock_irq(&zone
->lru_lock
);
3182 lruvec
= mem_cgroup_page_lruvec(head
, zone
);
3184 /* complete memcg works before add pages to LRU */
3185 mem_cgroup_split_huge_fixup(head
);
3188 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--)
3189 tail_mapcount
+= __split_huge_page_tail(head
, i
, lruvec
, list
);
3190 atomic_sub(tail_mapcount
, &head
->_count
);
3192 ClearPageCompound(head
);
3193 spin_unlock_irq(&zone
->lru_lock
);
3195 unfreeze_page(page_anon_vma(head
), head
);
3197 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
3198 struct page
*subpage
= head
+ i
;
3199 if (subpage
== page
)
3201 unlock_page(subpage
);
3204 * Subpages may be freed if there wasn't any mapping
3205 * like if add_to_swap() is running on a lru page that
3206 * had its mapping zapped. And freeing these pages
3207 * requires taking the lru_lock so we do the put_page
3208 * of the tail pages after the split is complete.
3215 * This function splits huge page into normal pages. @page can point to any
3216 * subpage of huge page to split. Split doesn't change the position of @page.
3218 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3219 * The huge page must be locked.
3221 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3223 * Both head page and tail pages will inherit mapping, flags, and so on from
3226 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3227 * they are not mapped.
3229 * Returns 0 if the hugepage is split successfully.
3230 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3233 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
3235 struct page
*head
= compound_head(page
);
3236 struct anon_vma
*anon_vma
;
3237 int count
, mapcount
, ret
;
3239 VM_BUG_ON_PAGE(is_huge_zero_page(page
), page
);
3240 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
3241 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
3242 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
3243 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
3246 * The caller does not necessarily hold an mmap_sem that would prevent
3247 * the anon_vma disappearing so we first we take a reference to it
3248 * and then lock the anon_vma for write. This is similar to
3249 * page_lock_anon_vma_read except the write lock is taken to serialise
3250 * against parallel split or collapse operations.
3252 anon_vma
= page_get_anon_vma(head
);
3257 anon_vma_lock_write(anon_vma
);
3260 * Racy check if we can split the page, before freeze_page() will
3263 if (total_mapcount(head
) != page_count(head
) - 1) {
3268 freeze_page(anon_vma
, head
);
3269 VM_BUG_ON_PAGE(compound_mapcount(head
), head
);
3271 /* Prevent deferred_split_scan() touching ->_count */
3272 spin_lock(&split_queue_lock
);
3273 count
= page_count(head
);
3274 mapcount
= total_mapcount(head
);
3275 if (mapcount
== count
- 1) {
3276 if (!list_empty(page_deferred_list(head
))) {
3278 list_del(page_deferred_list(head
));
3280 spin_unlock(&split_queue_lock
);
3281 __split_huge_page(page
, list
);
3283 } else if (IS_ENABLED(CONFIG_DEBUG_VM
) && mapcount
> count
- 1) {
3284 spin_unlock(&split_queue_lock
);
3285 pr_alert("total_mapcount: %u, page_count(): %u\n",
3288 dump_page(head
, NULL
);
3289 dump_page(page
, "total_mapcount(head) > page_count(head) - 1");
3292 spin_unlock(&split_queue_lock
);
3293 unfreeze_page(anon_vma
, head
);
3298 anon_vma_unlock_write(anon_vma
);
3299 put_anon_vma(anon_vma
);
3301 count_vm_event(!ret
? THP_SPLIT_PAGE
: THP_SPLIT_PAGE_FAILED
);
3305 void free_transhuge_page(struct page
*page
)
3307 unsigned long flags
;
3309 spin_lock_irqsave(&split_queue_lock
, flags
);
3310 if (!list_empty(page_deferred_list(page
))) {
3312 list_del(page_deferred_list(page
));
3314 spin_unlock_irqrestore(&split_queue_lock
, flags
);
3315 free_compound_page(page
);
3318 void deferred_split_huge_page(struct page
*page
)
3320 unsigned long flags
;
3322 VM_BUG_ON_PAGE(!PageTransHuge(page
), page
);
3324 spin_lock_irqsave(&split_queue_lock
, flags
);
3325 if (list_empty(page_deferred_list(page
))) {
3326 list_add_tail(page_deferred_list(page
), &split_queue
);
3329 spin_unlock_irqrestore(&split_queue_lock
, flags
);
3332 static unsigned long deferred_split_count(struct shrinker
*shrink
,
3333 struct shrink_control
*sc
)
3336 * Split a page from split_queue will free up at least one page,
3337 * at most HPAGE_PMD_NR - 1. We don't track exact number.
3338 * Let's use HPAGE_PMD_NR / 2 as ballpark.
3340 return ACCESS_ONCE(split_queue_len
) * HPAGE_PMD_NR
/ 2;
3343 static unsigned long deferred_split_scan(struct shrinker
*shrink
,
3344 struct shrink_control
*sc
)
3346 unsigned long flags
;
3347 LIST_HEAD(list
), *pos
, *next
;
3351 spin_lock_irqsave(&split_queue_lock
, flags
);
3352 list_splice_init(&split_queue
, &list
);
3354 /* Take pin on all head pages to avoid freeing them under us */
3355 list_for_each_safe(pos
, next
, &list
) {
3356 page
= list_entry((void *)pos
, struct page
, mapping
);
3357 page
= compound_head(page
);
3358 /* race with put_compound_page() */
3359 if (!get_page_unless_zero(page
)) {
3360 list_del_init(page_deferred_list(page
));
3364 spin_unlock_irqrestore(&split_queue_lock
, flags
);
3366 list_for_each_safe(pos
, next
, &list
) {
3367 page
= list_entry((void *)pos
, struct page
, mapping
);
3369 /* split_huge_page() removes page from list on success */
3370 if (!split_huge_page(page
))
3376 spin_lock_irqsave(&split_queue_lock
, flags
);
3377 list_splice_tail(&list
, &split_queue
);
3378 spin_unlock_irqrestore(&split_queue_lock
, flags
);
3380 return split
* HPAGE_PMD_NR
/ 2;
3383 static struct shrinker deferred_split_shrinker
= {
3384 .count_objects
= deferred_split_count
,
3385 .scan_objects
= deferred_split_scan
,
3386 .seeks
= DEFAULT_SEEKS
,