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>
33 #include <linux/shmem_fs.h>
36 #include <asm/pgalloc.h>
46 SCAN_NO_REFERENCED_PAGE
,
60 SCAN_ALLOC_HUGE_PAGE_FAIL
,
61 SCAN_CGROUP_CHARGE_FAIL
,
65 #define CREATE_TRACE_POINTS
66 #include <trace/events/huge_memory.h>
69 * By default transparent hugepage support is disabled in order that avoid
70 * to risk increase the memory footprint of applications without a guaranteed
71 * benefit. When transparent hugepage support is enabled, is for all mappings,
72 * and khugepaged scans all mappings.
73 * Defrag is invoked by khugepaged hugepage allocations and by page faults
74 * for all hugepage allocations.
76 unsigned long transparent_hugepage_flags __read_mostly
=
77 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
78 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
80 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
81 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
83 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
)|
84 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
85 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
87 /* default scan 8*512 pte (or vmas) every 30 second */
88 static unsigned int khugepaged_pages_to_scan __read_mostly
;
89 static unsigned int khugepaged_pages_collapsed
;
90 static unsigned int khugepaged_full_scans
;
91 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
92 /* during fragmentation poll the hugepage allocator once every minute */
93 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
94 static unsigned long khugepaged_sleep_expire
;
95 static struct task_struct
*khugepaged_thread __read_mostly
;
96 static DEFINE_MUTEX(khugepaged_mutex
);
97 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
98 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
100 * default collapse hugepages if there is at least one pte mapped like
101 * it would have happened if the vma was large enough during page
104 static unsigned int khugepaged_max_ptes_none __read_mostly
;
105 static unsigned int khugepaged_max_ptes_swap __read_mostly
;
107 static int khugepaged(void *none
);
108 static int khugepaged_slab_init(void);
109 static void khugepaged_slab_exit(void);
111 #define MM_SLOTS_HASH_BITS 10
112 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
114 static struct kmem_cache
*mm_slot_cache __read_mostly
;
117 * struct mm_slot - hash lookup from mm to mm_slot
118 * @hash: hash collision list
119 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
120 * @mm: the mm that this information is valid for
123 struct hlist_node hash
;
124 struct list_head mm_node
;
125 struct mm_struct
*mm
;
129 * struct khugepaged_scan - cursor for scanning
130 * @mm_head: the head of the mm list to scan
131 * @mm_slot: the current mm_slot we are scanning
132 * @address: the next address inside that to be scanned
134 * There is only the one khugepaged_scan instance of this cursor structure.
136 struct khugepaged_scan
{
137 struct list_head mm_head
;
138 struct mm_slot
*mm_slot
;
139 unsigned long address
;
141 static struct khugepaged_scan khugepaged_scan
= {
142 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
145 static struct shrinker deferred_split_shrinker
;
147 static void set_recommended_min_free_kbytes(void)
151 unsigned long recommended_min
;
153 for_each_populated_zone(zone
)
156 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
157 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
160 * Make sure that on average at least two pageblocks are almost free
161 * of another type, one for a migratetype to fall back to and a
162 * second to avoid subsequent fallbacks of other types There are 3
163 * MIGRATE_TYPES we care about.
165 recommended_min
+= pageblock_nr_pages
* nr_zones
*
166 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
168 /* don't ever allow to reserve more than 5% of the lowmem */
169 recommended_min
= min(recommended_min
,
170 (unsigned long) nr_free_buffer_pages() / 20);
171 recommended_min
<<= (PAGE_SHIFT
-10);
173 if (recommended_min
> min_free_kbytes
) {
174 if (user_min_free_kbytes
>= 0)
175 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
176 min_free_kbytes
, recommended_min
);
178 min_free_kbytes
= recommended_min
;
180 setup_per_zone_wmarks();
183 static int start_stop_khugepaged(void)
186 if (khugepaged_enabled()) {
187 if (!khugepaged_thread
)
188 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
190 if (IS_ERR(khugepaged_thread
)) {
191 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
192 err
= PTR_ERR(khugepaged_thread
);
193 khugepaged_thread
= NULL
;
197 if (!list_empty(&khugepaged_scan
.mm_head
))
198 wake_up_interruptible(&khugepaged_wait
);
200 set_recommended_min_free_kbytes();
201 } else if (khugepaged_thread
) {
202 kthread_stop(khugepaged_thread
);
203 khugepaged_thread
= NULL
;
209 static atomic_t huge_zero_refcount
;
210 struct page
*huge_zero_page __read_mostly
;
212 struct page
*get_huge_zero_page(void)
214 struct page
*zero_page
;
216 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
217 return READ_ONCE(huge_zero_page
);
219 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
222 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
225 count_vm_event(THP_ZERO_PAGE_ALLOC
);
227 if (cmpxchg(&huge_zero_page
, NULL
, zero_page
)) {
229 __free_pages(zero_page
, compound_order(zero_page
));
233 /* We take additional reference here. It will be put back by shrinker */
234 atomic_set(&huge_zero_refcount
, 2);
236 return READ_ONCE(huge_zero_page
);
239 void put_huge_zero_page(void)
242 * Counter should never go to zero here. Only shrinker can put
245 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
248 static unsigned long shrink_huge_zero_page_count(struct shrinker
*shrink
,
249 struct shrink_control
*sc
)
251 /* we can free zero page only if last reference remains */
252 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
255 static unsigned long shrink_huge_zero_page_scan(struct shrinker
*shrink
,
256 struct shrink_control
*sc
)
258 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
259 struct page
*zero_page
= xchg(&huge_zero_page
, NULL
);
260 BUG_ON(zero_page
== NULL
);
261 __free_pages(zero_page
, compound_order(zero_page
));
268 static struct shrinker huge_zero_page_shrinker
= {
269 .count_objects
= shrink_huge_zero_page_count
,
270 .scan_objects
= shrink_huge_zero_page_scan
,
271 .seeks
= DEFAULT_SEEKS
,
276 static ssize_t
triple_flag_store(struct kobject
*kobj
,
277 struct kobj_attribute
*attr
,
278 const char *buf
, size_t count
,
279 enum transparent_hugepage_flag enabled
,
280 enum transparent_hugepage_flag deferred
,
281 enum transparent_hugepage_flag req_madv
)
283 if (!memcmp("defer", buf
,
284 min(sizeof("defer")-1, count
))) {
285 if (enabled
== deferred
)
287 clear_bit(enabled
, &transparent_hugepage_flags
);
288 clear_bit(req_madv
, &transparent_hugepage_flags
);
289 set_bit(deferred
, &transparent_hugepage_flags
);
290 } else if (!memcmp("always", buf
,
291 min(sizeof("always")-1, count
))) {
292 clear_bit(deferred
, &transparent_hugepage_flags
);
293 clear_bit(req_madv
, &transparent_hugepage_flags
);
294 set_bit(enabled
, &transparent_hugepage_flags
);
295 } else if (!memcmp("madvise", buf
,
296 min(sizeof("madvise")-1, count
))) {
297 clear_bit(enabled
, &transparent_hugepage_flags
);
298 clear_bit(deferred
, &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
);
304 clear_bit(deferred
, &transparent_hugepage_flags
);
311 static ssize_t
enabled_show(struct kobject
*kobj
,
312 struct kobj_attribute
*attr
, char *buf
)
314 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG
, &transparent_hugepage_flags
))
315 return sprintf(buf
, "[always] madvise never\n");
316 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
, &transparent_hugepage_flags
))
317 return sprintf(buf
, "always [madvise] never\n");
319 return sprintf(buf
, "always madvise [never]\n");
322 static ssize_t
enabled_store(struct kobject
*kobj
,
323 struct kobj_attribute
*attr
,
324 const char *buf
, size_t count
)
328 ret
= triple_flag_store(kobj
, attr
, buf
, count
,
329 TRANSPARENT_HUGEPAGE_FLAG
,
330 TRANSPARENT_HUGEPAGE_FLAG
,
331 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
336 mutex_lock(&khugepaged_mutex
);
337 err
= start_stop_khugepaged();
338 mutex_unlock(&khugepaged_mutex
);
346 static struct kobj_attribute enabled_attr
=
347 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
349 static ssize_t
single_flag_show(struct kobject
*kobj
,
350 struct kobj_attribute
*attr
, char *buf
,
351 enum transparent_hugepage_flag flag
)
353 return sprintf(buf
, "%d\n",
354 !!test_bit(flag
, &transparent_hugepage_flags
));
357 static ssize_t
single_flag_store(struct kobject
*kobj
,
358 struct kobj_attribute
*attr
,
359 const char *buf
, size_t count
,
360 enum transparent_hugepage_flag flag
)
365 ret
= kstrtoul(buf
, 10, &value
);
372 set_bit(flag
, &transparent_hugepage_flags
);
374 clear_bit(flag
, &transparent_hugepage_flags
);
380 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
381 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
382 * memory just to allocate one more hugepage.
384 static ssize_t
defrag_show(struct kobject
*kobj
,
385 struct kobj_attribute
*attr
, char *buf
)
387 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
, &transparent_hugepage_flags
))
388 return sprintf(buf
, "[always] defer madvise never\n");
389 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
, &transparent_hugepage_flags
))
390 return sprintf(buf
, "always [defer] madvise never\n");
391 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
, &transparent_hugepage_flags
))
392 return sprintf(buf
, "always defer [madvise] never\n");
394 return sprintf(buf
, "always defer madvise [never]\n");
397 static ssize_t
defrag_store(struct kobject
*kobj
,
398 struct kobj_attribute
*attr
,
399 const char *buf
, size_t count
)
401 return triple_flag_store(kobj
, attr
, buf
, count
,
402 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
,
403 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
,
404 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
406 static struct kobj_attribute defrag_attr
=
407 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
409 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
410 struct kobj_attribute
*attr
, char *buf
)
412 return single_flag_show(kobj
, attr
, buf
,
413 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
415 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
416 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
418 return single_flag_store(kobj
, attr
, buf
, count
,
419 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
421 static struct kobj_attribute use_zero_page_attr
=
422 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
423 #ifdef CONFIG_DEBUG_VM
424 static ssize_t
debug_cow_show(struct kobject
*kobj
,
425 struct kobj_attribute
*attr
, char *buf
)
427 return single_flag_show(kobj
, attr
, buf
,
428 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
430 static ssize_t
debug_cow_store(struct kobject
*kobj
,
431 struct kobj_attribute
*attr
,
432 const char *buf
, size_t count
)
434 return single_flag_store(kobj
, attr
, buf
, count
,
435 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
437 static struct kobj_attribute debug_cow_attr
=
438 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
439 #endif /* CONFIG_DEBUG_VM */
441 static struct attribute
*hugepage_attr
[] = {
444 &use_zero_page_attr
.attr
,
445 #ifdef CONFIG_DEBUG_VM
446 &debug_cow_attr
.attr
,
451 static struct attribute_group hugepage_attr_group
= {
452 .attrs
= hugepage_attr
,
455 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
456 struct kobj_attribute
*attr
,
459 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
462 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
463 struct kobj_attribute
*attr
,
464 const char *buf
, size_t count
)
469 err
= kstrtoul(buf
, 10, &msecs
);
470 if (err
|| msecs
> UINT_MAX
)
473 khugepaged_scan_sleep_millisecs
= msecs
;
474 khugepaged_sleep_expire
= 0;
475 wake_up_interruptible(&khugepaged_wait
);
479 static struct kobj_attribute scan_sleep_millisecs_attr
=
480 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
481 scan_sleep_millisecs_store
);
483 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
484 struct kobj_attribute
*attr
,
487 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
490 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
491 struct kobj_attribute
*attr
,
492 const char *buf
, size_t count
)
497 err
= kstrtoul(buf
, 10, &msecs
);
498 if (err
|| msecs
> UINT_MAX
)
501 khugepaged_alloc_sleep_millisecs
= msecs
;
502 khugepaged_sleep_expire
= 0;
503 wake_up_interruptible(&khugepaged_wait
);
507 static struct kobj_attribute alloc_sleep_millisecs_attr
=
508 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
509 alloc_sleep_millisecs_store
);
511 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
512 struct kobj_attribute
*attr
,
515 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
517 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
518 struct kobj_attribute
*attr
,
519 const char *buf
, size_t count
)
524 err
= kstrtoul(buf
, 10, &pages
);
525 if (err
|| !pages
|| pages
> UINT_MAX
)
528 khugepaged_pages_to_scan
= pages
;
532 static struct kobj_attribute pages_to_scan_attr
=
533 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
534 pages_to_scan_store
);
536 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
537 struct kobj_attribute
*attr
,
540 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
542 static struct kobj_attribute pages_collapsed_attr
=
543 __ATTR_RO(pages_collapsed
);
545 static ssize_t
full_scans_show(struct kobject
*kobj
,
546 struct kobj_attribute
*attr
,
549 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
551 static struct kobj_attribute full_scans_attr
=
552 __ATTR_RO(full_scans
);
554 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
555 struct kobj_attribute
*attr
, char *buf
)
557 return single_flag_show(kobj
, attr
, buf
,
558 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
560 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
561 struct kobj_attribute
*attr
,
562 const char *buf
, size_t count
)
564 return single_flag_store(kobj
, attr
, buf
, count
,
565 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
567 static struct kobj_attribute khugepaged_defrag_attr
=
568 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
569 khugepaged_defrag_store
);
572 * max_ptes_none controls if khugepaged should collapse hugepages over
573 * any unmapped ptes in turn potentially increasing the memory
574 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
575 * reduce the available free memory in the system as it
576 * runs. Increasing max_ptes_none will instead potentially reduce the
577 * free memory in the system during the khugepaged scan.
579 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
580 struct kobj_attribute
*attr
,
583 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
585 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
586 struct kobj_attribute
*attr
,
587 const char *buf
, size_t count
)
590 unsigned long max_ptes_none
;
592 err
= kstrtoul(buf
, 10, &max_ptes_none
);
593 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
596 khugepaged_max_ptes_none
= max_ptes_none
;
600 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
601 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
602 khugepaged_max_ptes_none_store
);
604 static ssize_t
khugepaged_max_ptes_swap_show(struct kobject
*kobj
,
605 struct kobj_attribute
*attr
,
608 return sprintf(buf
, "%u\n", khugepaged_max_ptes_swap
);
611 static ssize_t
khugepaged_max_ptes_swap_store(struct kobject
*kobj
,
612 struct kobj_attribute
*attr
,
613 const char *buf
, size_t count
)
616 unsigned long max_ptes_swap
;
618 err
= kstrtoul(buf
, 10, &max_ptes_swap
);
619 if (err
|| max_ptes_swap
> HPAGE_PMD_NR
-1)
622 khugepaged_max_ptes_swap
= max_ptes_swap
;
627 static struct kobj_attribute khugepaged_max_ptes_swap_attr
=
628 __ATTR(max_ptes_swap
, 0644, khugepaged_max_ptes_swap_show
,
629 khugepaged_max_ptes_swap_store
);
631 static struct attribute
*khugepaged_attr
[] = {
632 &khugepaged_defrag_attr
.attr
,
633 &khugepaged_max_ptes_none_attr
.attr
,
634 &pages_to_scan_attr
.attr
,
635 &pages_collapsed_attr
.attr
,
636 &full_scans_attr
.attr
,
637 &scan_sleep_millisecs_attr
.attr
,
638 &alloc_sleep_millisecs_attr
.attr
,
639 &khugepaged_max_ptes_swap_attr
.attr
,
643 static struct attribute_group khugepaged_attr_group
= {
644 .attrs
= khugepaged_attr
,
645 .name
= "khugepaged",
648 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
652 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
653 if (unlikely(!*hugepage_kobj
)) {
654 pr_err("failed to create transparent hugepage kobject\n");
658 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
660 pr_err("failed to register transparent hugepage group\n");
664 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
666 pr_err("failed to register transparent hugepage group\n");
667 goto remove_hp_group
;
673 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
675 kobject_put(*hugepage_kobj
);
679 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
681 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
682 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
683 kobject_put(hugepage_kobj
);
686 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
691 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
694 #endif /* CONFIG_SYSFS */
696 static int __init
hugepage_init(void)
699 struct kobject
*hugepage_kobj
;
701 if (!has_transparent_hugepage()) {
702 transparent_hugepage_flags
= 0;
706 khugepaged_pages_to_scan
= HPAGE_PMD_NR
* 8;
707 khugepaged_max_ptes_none
= HPAGE_PMD_NR
- 1;
708 khugepaged_max_ptes_swap
= HPAGE_PMD_NR
/ 8;
710 * hugepages can't be allocated by the buddy allocator
712 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
>= MAX_ORDER
);
714 * we use page->mapping and page->index in second tail page
715 * as list_head: assuming THP order >= 2
717 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER
< 2);
719 err
= hugepage_init_sysfs(&hugepage_kobj
);
723 err
= khugepaged_slab_init();
727 err
= register_shrinker(&huge_zero_page_shrinker
);
729 goto err_hzp_shrinker
;
730 err
= register_shrinker(&deferred_split_shrinker
);
732 goto err_split_shrinker
;
735 * By default disable transparent hugepages on smaller systems,
736 * where the extra memory used could hurt more than TLB overhead
737 * is likely to save. The admin can still enable it through /sys.
739 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
740 transparent_hugepage_flags
= 0;
744 err
= start_stop_khugepaged();
750 unregister_shrinker(&deferred_split_shrinker
);
752 unregister_shrinker(&huge_zero_page_shrinker
);
754 khugepaged_slab_exit();
756 hugepage_exit_sysfs(hugepage_kobj
);
760 subsys_initcall(hugepage_init
);
762 static int __init
setup_transparent_hugepage(char *str
)
767 if (!strcmp(str
, "always")) {
768 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
769 &transparent_hugepage_flags
);
770 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
771 &transparent_hugepage_flags
);
773 } else if (!strcmp(str
, "madvise")) {
774 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
775 &transparent_hugepage_flags
);
776 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
777 &transparent_hugepage_flags
);
779 } else if (!strcmp(str
, "never")) {
780 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
781 &transparent_hugepage_flags
);
782 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
783 &transparent_hugepage_flags
);
788 pr_warn("transparent_hugepage= cannot parse, ignored\n");
791 __setup("transparent_hugepage=", setup_transparent_hugepage
);
793 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
795 if (likely(vma
->vm_flags
& VM_WRITE
))
796 pmd
= pmd_mkwrite(pmd
);
800 static inline struct list_head
*page_deferred_list(struct page
*page
)
803 * ->lru in the tail pages is occupied by compound_head.
804 * Let's use ->mapping + ->index in the second tail page as list_head.
806 return (struct list_head
*)&page
[2].mapping
;
809 void prep_transhuge_page(struct page
*page
)
812 * we use page->mapping and page->indexlru in second tail page
813 * as list_head: assuming THP order >= 2
816 INIT_LIST_HEAD(page_deferred_list(page
));
817 set_compound_page_dtor(page
, TRANSHUGE_PAGE_DTOR
);
820 static int __do_huge_pmd_anonymous_page(struct fault_env
*fe
, struct page
*page
,
823 struct vm_area_struct
*vma
= fe
->vma
;
824 struct mem_cgroup
*memcg
;
826 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
828 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
830 if (mem_cgroup_try_charge(page
, vma
->vm_mm
, gfp
, &memcg
, true)) {
832 count_vm_event(THP_FAULT_FALLBACK
);
833 return VM_FAULT_FALLBACK
;
836 pgtable
= pte_alloc_one(vma
->vm_mm
, haddr
);
837 if (unlikely(!pgtable
)) {
838 mem_cgroup_cancel_charge(page
, memcg
, true);
843 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
845 * The memory barrier inside __SetPageUptodate makes sure that
846 * clear_huge_page writes become visible before the set_pmd_at()
849 __SetPageUptodate(page
);
851 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
852 if (unlikely(!pmd_none(*fe
->pmd
))) {
853 spin_unlock(fe
->ptl
);
854 mem_cgroup_cancel_charge(page
, memcg
, true);
856 pte_free(vma
->vm_mm
, pgtable
);
860 /* Deliver the page fault to userland */
861 if (userfaultfd_missing(vma
)) {
864 spin_unlock(fe
->ptl
);
865 mem_cgroup_cancel_charge(page
, memcg
, true);
867 pte_free(vma
->vm_mm
, pgtable
);
868 ret
= handle_userfault(fe
, VM_UFFD_MISSING
);
869 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
873 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
874 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
875 page_add_new_anon_rmap(page
, vma
, haddr
, true);
876 mem_cgroup_commit_charge(page
, memcg
, false, true);
877 lru_cache_add_active_or_unevictable(page
, vma
);
878 pgtable_trans_huge_deposit(vma
->vm_mm
, fe
->pmd
, pgtable
);
879 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, entry
);
880 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
881 atomic_long_inc(&vma
->vm_mm
->nr_ptes
);
882 spin_unlock(fe
->ptl
);
883 count_vm_event(THP_FAULT_ALLOC
);
890 * If THP is set to always then directly reclaim/compact as necessary
891 * If set to defer then do no reclaim and defer to khugepaged
892 * If set to madvise and the VMA is flagged then directly reclaim/compact
894 static inline gfp_t
alloc_hugepage_direct_gfpmask(struct vm_area_struct
*vma
)
896 gfp_t reclaim_flags
= 0;
898 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
, &transparent_hugepage_flags
) &&
899 (vma
->vm_flags
& VM_HUGEPAGE
))
900 reclaim_flags
= __GFP_DIRECT_RECLAIM
;
901 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG
, &transparent_hugepage_flags
))
902 reclaim_flags
= __GFP_KSWAPD_RECLAIM
;
903 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG
, &transparent_hugepage_flags
))
904 reclaim_flags
= __GFP_DIRECT_RECLAIM
;
906 return GFP_TRANSHUGE
| reclaim_flags
;
909 /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
910 static inline gfp_t
alloc_hugepage_khugepaged_gfpmask(void)
912 return GFP_TRANSHUGE
| (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM
: 0);
915 /* Caller must hold page table lock. */
916 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
917 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
918 struct page
*zero_page
)
923 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
924 entry
= pmd_mkhuge(entry
);
926 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
927 set_pmd_at(mm
, haddr
, pmd
, entry
);
928 atomic_long_inc(&mm
->nr_ptes
);
932 int do_huge_pmd_anonymous_page(struct fault_env
*fe
)
934 struct vm_area_struct
*vma
= fe
->vma
;
937 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
939 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
940 return VM_FAULT_FALLBACK
;
941 if (unlikely(anon_vma_prepare(vma
)))
943 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
945 if (!(fe
->flags
& FAULT_FLAG_WRITE
) &&
946 !mm_forbids_zeropage(vma
->vm_mm
) &&
947 transparent_hugepage_use_zero_page()) {
949 struct page
*zero_page
;
952 pgtable
= pte_alloc_one(vma
->vm_mm
, haddr
);
953 if (unlikely(!pgtable
))
955 zero_page
= get_huge_zero_page();
956 if (unlikely(!zero_page
)) {
957 pte_free(vma
->vm_mm
, pgtable
);
958 count_vm_event(THP_FAULT_FALLBACK
);
959 return VM_FAULT_FALLBACK
;
961 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
964 if (pmd_none(*fe
->pmd
)) {
965 if (userfaultfd_missing(vma
)) {
966 spin_unlock(fe
->ptl
);
967 ret
= handle_userfault(fe
, VM_UFFD_MISSING
);
968 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
970 set_huge_zero_page(pgtable
, vma
->vm_mm
, vma
,
971 haddr
, fe
->pmd
, zero_page
);
972 spin_unlock(fe
->ptl
);
976 spin_unlock(fe
->ptl
);
978 pte_free(vma
->vm_mm
, pgtable
);
979 put_huge_zero_page();
983 gfp
= alloc_hugepage_direct_gfpmask(vma
);
984 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
985 if (unlikely(!page
)) {
986 count_vm_event(THP_FAULT_FALLBACK
);
987 return VM_FAULT_FALLBACK
;
989 prep_transhuge_page(page
);
990 return __do_huge_pmd_anonymous_page(fe
, page
, gfp
);
993 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
994 pmd_t
*pmd
, pfn_t pfn
, pgprot_t prot
, bool write
)
996 struct mm_struct
*mm
= vma
->vm_mm
;
1000 ptl
= pmd_lock(mm
, pmd
);
1001 entry
= pmd_mkhuge(pfn_t_pmd(pfn
, prot
));
1002 if (pfn_t_devmap(pfn
))
1003 entry
= pmd_mkdevmap(entry
);
1005 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
1006 entry
= maybe_pmd_mkwrite(entry
, vma
);
1008 set_pmd_at(mm
, addr
, pmd
, entry
);
1009 update_mmu_cache_pmd(vma
, addr
, pmd
);
1013 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
1014 pmd_t
*pmd
, pfn_t pfn
, bool write
)
1016 pgprot_t pgprot
= vma
->vm_page_prot
;
1018 * If we had pmd_special, we could avoid all these restrictions,
1019 * but we need to be consistent with PTEs and architectures that
1020 * can't support a 'special' bit.
1022 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
1023 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
1024 (VM_PFNMAP
|VM_MIXEDMAP
));
1025 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
1026 BUG_ON(!pfn_t_devmap(pfn
));
1028 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
1029 return VM_FAULT_SIGBUS
;
1030 if (track_pfn_insert(vma
, &pgprot
, pfn
))
1031 return VM_FAULT_SIGBUS
;
1032 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
1033 return VM_FAULT_NOPAGE
;
1035 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd
);
1037 static void touch_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
1043 * We should set the dirty bit only for FOLL_WRITE but for now
1044 * the dirty bit in the pmd is meaningless. And if the dirty
1045 * bit will become meaningful and we'll only set it with
1046 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1047 * set the young bit, instead of the current set_pmd_at.
1049 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1050 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
1052 update_mmu_cache_pmd(vma
, addr
, pmd
);
1055 struct page
*follow_devmap_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
1056 pmd_t
*pmd
, int flags
)
1058 unsigned long pfn
= pmd_pfn(*pmd
);
1059 struct mm_struct
*mm
= vma
->vm_mm
;
1060 struct dev_pagemap
*pgmap
;
1063 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1065 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1068 if (pmd_present(*pmd
) && pmd_devmap(*pmd
))
1073 if (flags
& FOLL_TOUCH
)
1074 touch_pmd(vma
, addr
, pmd
);
1077 * device mapped pages can only be returned if the
1078 * caller will manage the page reference count.
1080 if (!(flags
& FOLL_GET
))
1081 return ERR_PTR(-EEXIST
);
1083 pfn
+= (addr
& ~PMD_MASK
) >> PAGE_SHIFT
;
1084 pgmap
= get_dev_pagemap(pfn
, NULL
);
1086 return ERR_PTR(-EFAULT
);
1087 page
= pfn_to_page(pfn
);
1089 put_dev_pagemap(pgmap
);
1094 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
1095 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
1096 struct vm_area_struct
*vma
)
1098 spinlock_t
*dst_ptl
, *src_ptl
;
1099 struct page
*src_page
;
1101 pgtable_t pgtable
= NULL
;
1104 /* Skip if can be re-fill on fault */
1105 if (!vma_is_anonymous(vma
))
1108 pgtable
= pte_alloc_one(dst_mm
, addr
);
1109 if (unlikely(!pgtable
))
1112 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
1113 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
1114 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
1118 if (unlikely(!pmd_trans_huge(pmd
))) {
1119 pte_free(dst_mm
, pgtable
);
1123 * When page table lock is held, the huge zero pmd should not be
1124 * under splitting since we don't split the page itself, only pmd to
1127 if (is_huge_zero_pmd(pmd
)) {
1128 struct page
*zero_page
;
1130 * get_huge_zero_page() will never allocate a new page here,
1131 * since we already have a zero page to copy. It just takes a
1134 zero_page
= get_huge_zero_page();
1135 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
1141 src_page
= pmd_page(pmd
);
1142 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
1144 page_dup_rmap(src_page
, true);
1145 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1146 atomic_long_inc(&dst_mm
->nr_ptes
);
1147 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
1149 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
1150 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
1151 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
1155 spin_unlock(src_ptl
);
1156 spin_unlock(dst_ptl
);
1161 void huge_pmd_set_accessed(struct fault_env
*fe
, pmd_t orig_pmd
)
1164 unsigned long haddr
;
1166 fe
->ptl
= pmd_lock(fe
->vma
->vm_mm
, fe
->pmd
);
1167 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
1170 entry
= pmd_mkyoung(orig_pmd
);
1171 haddr
= fe
->address
& HPAGE_PMD_MASK
;
1172 if (pmdp_set_access_flags(fe
->vma
, haddr
, fe
->pmd
, entry
,
1173 fe
->flags
& FAULT_FLAG_WRITE
))
1174 update_mmu_cache_pmd(fe
->vma
, fe
->address
, fe
->pmd
);
1177 spin_unlock(fe
->ptl
);
1180 static int do_huge_pmd_wp_page_fallback(struct fault_env
*fe
, pmd_t orig_pmd
,
1183 struct vm_area_struct
*vma
= fe
->vma
;
1184 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
1185 struct mem_cgroup
*memcg
;
1189 struct page
**pages
;
1190 unsigned long mmun_start
; /* For mmu_notifiers */
1191 unsigned long mmun_end
; /* For mmu_notifiers */
1193 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1195 if (unlikely(!pages
)) {
1196 ret
|= VM_FAULT_OOM
;
1200 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1201 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1202 __GFP_OTHER_NODE
, vma
,
1203 fe
->address
, page_to_nid(page
));
1204 if (unlikely(!pages
[i
] ||
1205 mem_cgroup_try_charge(pages
[i
], vma
->vm_mm
,
1206 GFP_KERNEL
, &memcg
, false))) {
1210 memcg
= (void *)page_private(pages
[i
]);
1211 set_page_private(pages
[i
], 0);
1212 mem_cgroup_cancel_charge(pages
[i
], memcg
,
1217 ret
|= VM_FAULT_OOM
;
1220 set_page_private(pages
[i
], (unsigned long)memcg
);
1223 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1224 copy_user_highpage(pages
[i
], page
+ i
,
1225 haddr
+ PAGE_SIZE
* i
, vma
);
1226 __SetPageUptodate(pages
[i
]);
1231 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1232 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
1234 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
1235 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
1236 goto out_free_pages
;
1237 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1239 pmdp_huge_clear_flush_notify(vma
, haddr
, fe
->pmd
);
1240 /* leave pmd empty until pte is filled */
1242 pgtable
= pgtable_trans_huge_withdraw(vma
->vm_mm
, fe
->pmd
);
1243 pmd_populate(vma
->vm_mm
, &_pmd
, pgtable
);
1245 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1247 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1248 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1249 memcg
= (void *)page_private(pages
[i
]);
1250 set_page_private(pages
[i
], 0);
1251 page_add_new_anon_rmap(pages
[i
], fe
->vma
, haddr
, false);
1252 mem_cgroup_commit_charge(pages
[i
], memcg
, false, false);
1253 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
1254 fe
->pte
= pte_offset_map(&_pmd
, haddr
);
1255 VM_BUG_ON(!pte_none(*fe
->pte
));
1256 set_pte_at(vma
->vm_mm
, haddr
, fe
->pte
, entry
);
1261 smp_wmb(); /* make pte visible before pmd */
1262 pmd_populate(vma
->vm_mm
, fe
->pmd
, pgtable
);
1263 page_remove_rmap(page
, true);
1264 spin_unlock(fe
->ptl
);
1266 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
1268 ret
|= VM_FAULT_WRITE
;
1275 spin_unlock(fe
->ptl
);
1276 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
1277 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1278 memcg
= (void *)page_private(pages
[i
]);
1279 set_page_private(pages
[i
], 0);
1280 mem_cgroup_cancel_charge(pages
[i
], memcg
, false);
1287 int do_huge_pmd_wp_page(struct fault_env
*fe
, pmd_t orig_pmd
)
1289 struct vm_area_struct
*vma
= fe
->vma
;
1290 struct page
*page
= NULL
, *new_page
;
1291 struct mem_cgroup
*memcg
;
1292 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
1293 unsigned long mmun_start
; /* For mmu_notifiers */
1294 unsigned long mmun_end
; /* For mmu_notifiers */
1295 gfp_t huge_gfp
; /* for allocation and charge */
1298 fe
->ptl
= pmd_lockptr(vma
->vm_mm
, fe
->pmd
);
1299 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1300 if (is_huge_zero_pmd(orig_pmd
))
1303 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
)))
1306 page
= pmd_page(orig_pmd
);
1307 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1309 * We can only reuse the page if nobody else maps the huge page or it's
1312 if (page_trans_huge_mapcount(page
, NULL
) == 1) {
1314 entry
= pmd_mkyoung(orig_pmd
);
1315 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1316 if (pmdp_set_access_flags(vma
, haddr
, fe
->pmd
, entry
, 1))
1317 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1318 ret
|= VM_FAULT_WRITE
;
1322 spin_unlock(fe
->ptl
);
1324 if (transparent_hugepage_enabled(vma
) &&
1325 !transparent_hugepage_debug_cow()) {
1326 huge_gfp
= alloc_hugepage_direct_gfpmask(vma
);
1327 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1331 if (likely(new_page
)) {
1332 prep_transhuge_page(new_page
);
1335 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1336 ret
|= VM_FAULT_FALLBACK
;
1338 ret
= do_huge_pmd_wp_page_fallback(fe
, orig_pmd
, page
);
1339 if (ret
& VM_FAULT_OOM
) {
1340 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1341 ret
|= VM_FAULT_FALLBACK
;
1345 count_vm_event(THP_FAULT_FALLBACK
);
1349 if (unlikely(mem_cgroup_try_charge(new_page
, vma
->vm_mm
,
1350 huge_gfp
, &memcg
, true))) {
1352 split_huge_pmd(vma
, fe
->pmd
, fe
->address
);
1355 ret
|= VM_FAULT_FALLBACK
;
1356 count_vm_event(THP_FAULT_FALLBACK
);
1360 count_vm_event(THP_FAULT_ALLOC
);
1363 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1365 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1366 __SetPageUptodate(new_page
);
1369 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1370 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
1375 if (unlikely(!pmd_same(*fe
->pmd
, orig_pmd
))) {
1376 spin_unlock(fe
->ptl
);
1377 mem_cgroup_cancel_charge(new_page
, memcg
, true);
1382 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1383 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1384 pmdp_huge_clear_flush_notify(vma
, haddr
, fe
->pmd
);
1385 page_add_new_anon_rmap(new_page
, vma
, haddr
, true);
1386 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
1387 lru_cache_add_active_or_unevictable(new_page
, vma
);
1388 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, entry
);
1389 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1391 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1392 put_huge_zero_page();
1394 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1395 page_remove_rmap(page
, true);
1398 ret
|= VM_FAULT_WRITE
;
1400 spin_unlock(fe
->ptl
);
1402 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
1406 spin_unlock(fe
->ptl
);
1410 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1415 struct mm_struct
*mm
= vma
->vm_mm
;
1416 struct page
*page
= NULL
;
1418 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1420 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1423 /* Avoid dumping huge zero page */
1424 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1425 return ERR_PTR(-EFAULT
);
1427 /* Full NUMA hinting faults to serialise migration in fault paths */
1428 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1431 page
= pmd_page(*pmd
);
1432 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1433 if (flags
& FOLL_TOUCH
)
1434 touch_pmd(vma
, addr
, pmd
);
1435 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1437 * We don't mlock() pte-mapped THPs. This way we can avoid
1438 * leaking mlocked pages into non-VM_LOCKED VMAs.
1442 * In most cases the pmd is the only mapping of the page as we
1443 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1444 * writable private mappings in populate_vma_page_range().
1446 * The only scenario when we have the page shared here is if we
1447 * mlocking read-only mapping shared over fork(). We skip
1448 * mlocking such pages.
1452 * We can expect PageDoubleMap() to be stable under page lock:
1453 * for file pages we set it in page_add_file_rmap(), which
1454 * requires page to be locked.
1457 if (PageAnon(page
) && compound_mapcount(page
) != 1)
1459 if (PageDoubleMap(page
) || !page
->mapping
)
1461 if (!trylock_page(page
))
1464 if (page
->mapping
&& !PageDoubleMap(page
))
1465 mlock_vma_page(page
);
1469 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1470 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
1471 if (flags
& FOLL_GET
)
1478 /* NUMA hinting page fault entry point for trans huge pmds */
1479 int do_huge_pmd_numa_page(struct fault_env
*fe
, pmd_t pmd
)
1481 struct vm_area_struct
*vma
= fe
->vma
;
1482 struct anon_vma
*anon_vma
= NULL
;
1484 unsigned long haddr
= fe
->address
& HPAGE_PMD_MASK
;
1485 int page_nid
= -1, this_nid
= numa_node_id();
1486 int target_nid
, last_cpupid
= -1;
1488 bool migrated
= false;
1492 /* A PROT_NONE fault should not end up here */
1493 BUG_ON(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)));
1495 fe
->ptl
= pmd_lock(vma
->vm_mm
, fe
->pmd
);
1496 if (unlikely(!pmd_same(pmd
, *fe
->pmd
)))
1500 * If there are potential migrations, wait for completion and retry
1501 * without disrupting NUMA hinting information. Do not relock and
1502 * check_same as the page may no longer be mapped.
1504 if (unlikely(pmd_trans_migrating(*fe
->pmd
))) {
1505 page
= pmd_page(*fe
->pmd
);
1506 spin_unlock(fe
->ptl
);
1507 wait_on_page_locked(page
);
1511 page
= pmd_page(pmd
);
1512 BUG_ON(is_huge_zero_page(page
));
1513 page_nid
= page_to_nid(page
);
1514 last_cpupid
= page_cpupid_last(page
);
1515 count_vm_numa_event(NUMA_HINT_FAULTS
);
1516 if (page_nid
== this_nid
) {
1517 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1518 flags
|= TNF_FAULT_LOCAL
;
1521 /* See similar comment in do_numa_page for explanation */
1522 if (!(vma
->vm_flags
& VM_WRITE
))
1523 flags
|= TNF_NO_GROUP
;
1526 * Acquire the page lock to serialise THP migrations but avoid dropping
1527 * page_table_lock if at all possible
1529 page_locked
= trylock_page(page
);
1530 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1531 if (target_nid
== -1) {
1532 /* If the page was locked, there are no parallel migrations */
1537 /* Migration could have started since the pmd_trans_migrating check */
1539 spin_unlock(fe
->ptl
);
1540 wait_on_page_locked(page
);
1546 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1547 * to serialises splits
1550 spin_unlock(fe
->ptl
);
1551 anon_vma
= page_lock_anon_vma_read(page
);
1553 /* Confirm the PMD did not change while page_table_lock was released */
1555 if (unlikely(!pmd_same(pmd
, *fe
->pmd
))) {
1562 /* Bail if we fail to protect against THP splits for any reason */
1563 if (unlikely(!anon_vma
)) {
1570 * Migrate the THP to the requested node, returns with page unlocked
1571 * and access rights restored.
1573 spin_unlock(fe
->ptl
);
1574 migrated
= migrate_misplaced_transhuge_page(vma
->vm_mm
, vma
,
1575 fe
->pmd
, pmd
, fe
->address
, page
, target_nid
);
1577 flags
|= TNF_MIGRATED
;
1578 page_nid
= target_nid
;
1580 flags
|= TNF_MIGRATE_FAIL
;
1584 BUG_ON(!PageLocked(page
));
1585 was_writable
= pmd_write(pmd
);
1586 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1587 pmd
= pmd_mkyoung(pmd
);
1589 pmd
= pmd_mkwrite(pmd
);
1590 set_pmd_at(vma
->vm_mm
, haddr
, fe
->pmd
, pmd
);
1591 update_mmu_cache_pmd(vma
, fe
->address
, fe
->pmd
);
1594 spin_unlock(fe
->ptl
);
1598 page_unlock_anon_vma_read(anon_vma
);
1601 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, fe
->flags
);
1606 int madvise_free_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1607 pmd_t
*pmd
, unsigned long addr
, unsigned long next
)
1613 struct mm_struct
*mm
= tlb
->mm
;
1616 ptl
= pmd_trans_huge_lock(pmd
, vma
);
1621 if (is_huge_zero_pmd(orig_pmd
)) {
1626 page
= pmd_page(orig_pmd
);
1628 * If other processes are mapping this page, we couldn't discard
1629 * the page unless they all do MADV_FREE so let's skip the page.
1631 if (page_mapcount(page
) != 1)
1634 if (!trylock_page(page
))
1638 * If user want to discard part-pages of THP, split it so MADV_FREE
1639 * will deactivate only them.
1641 if (next
- addr
!= HPAGE_PMD_SIZE
) {
1644 split_huge_page(page
);
1650 if (PageDirty(page
))
1651 ClearPageDirty(page
);
1654 if (PageActive(page
))
1655 deactivate_page(page
);
1657 if (pmd_young(orig_pmd
) || pmd_dirty(orig_pmd
)) {
1658 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1660 orig_pmd
= pmd_mkold(orig_pmd
);
1661 orig_pmd
= pmd_mkclean(orig_pmd
);
1663 set_pmd_at(mm
, addr
, pmd
, orig_pmd
);
1664 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1673 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1674 pmd_t
*pmd
, unsigned long addr
)
1679 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1683 * For architectures like ppc64 we look at deposited pgtable
1684 * when calling pmdp_huge_get_and_clear. So do the
1685 * pgtable_trans_huge_withdraw after finishing pmdp related
1688 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1690 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1691 if (vma_is_dax(vma
)) {
1693 if (is_huge_zero_pmd(orig_pmd
))
1694 tlb_remove_page(tlb
, pmd_page(orig_pmd
));
1695 } else if (is_huge_zero_pmd(orig_pmd
)) {
1696 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1697 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1699 tlb_remove_page(tlb
, pmd_page(orig_pmd
));
1701 struct page
*page
= pmd_page(orig_pmd
);
1702 page_remove_rmap(page
, true);
1703 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1704 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1705 if (PageAnon(page
)) {
1707 pgtable
= pgtable_trans_huge_withdraw(tlb
->mm
, pmd
);
1708 pte_free(tlb
->mm
, pgtable
);
1709 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1710 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1712 add_mm_counter(tlb
->mm
, MM_FILEPAGES
, -HPAGE_PMD_NR
);
1715 tlb_remove_page_size(tlb
, page
, HPAGE_PMD_SIZE
);
1720 bool move_huge_pmd(struct vm_area_struct
*vma
, unsigned long old_addr
,
1721 unsigned long new_addr
, unsigned long old_end
,
1722 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1724 spinlock_t
*old_ptl
, *new_ptl
;
1726 struct mm_struct
*mm
= vma
->vm_mm
;
1728 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1729 (new_addr
& ~HPAGE_PMD_MASK
) ||
1730 old_end
- old_addr
< HPAGE_PMD_SIZE
)
1734 * The destination pmd shouldn't be established, free_pgtables()
1735 * should have release it.
1737 if (WARN_ON(!pmd_none(*new_pmd
))) {
1738 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1743 * We don't have to worry about the ordering of src and dst
1744 * ptlocks because exclusive mmap_sem prevents deadlock.
1746 old_ptl
= __pmd_trans_huge_lock(old_pmd
, vma
);
1748 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1749 if (new_ptl
!= old_ptl
)
1750 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1751 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1752 VM_BUG_ON(!pmd_none(*new_pmd
));
1754 if (pmd_move_must_withdraw(new_ptl
, old_ptl
) &&
1755 vma_is_anonymous(vma
)) {
1757 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1758 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1760 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1761 if (new_ptl
!= old_ptl
)
1762 spin_unlock(new_ptl
);
1763 spin_unlock(old_ptl
);
1771 * - 0 if PMD could not be locked
1772 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1773 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1775 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1776 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1778 struct mm_struct
*mm
= vma
->vm_mm
;
1782 ptl
= __pmd_trans_huge_lock(pmd
, vma
);
1785 bool preserve_write
= prot_numa
&& pmd_write(*pmd
);
1789 * Avoid trapping faults against the zero page. The read-only
1790 * data is likely to be read-cached on the local CPU and
1791 * local/remote hits to the zero page are not interesting.
1793 if (prot_numa
&& is_huge_zero_pmd(*pmd
)) {
1798 if (!prot_numa
|| !pmd_protnone(*pmd
)) {
1799 entry
= pmdp_huge_get_and_clear_notify(mm
, addr
, pmd
);
1800 entry
= pmd_modify(entry
, newprot
);
1802 entry
= pmd_mkwrite(entry
);
1804 set_pmd_at(mm
, addr
, pmd
, entry
);
1805 BUG_ON(vma_is_anonymous(vma
) && !preserve_write
&&
1815 * Returns true if a given pmd maps a thp, false otherwise.
1817 * Note that if it returns true, this routine returns without unlocking page
1818 * table lock. So callers must unlock it.
1820 spinlock_t
*__pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
)
1823 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1824 if (likely(pmd_trans_huge(*pmd
) || pmd_devmap(*pmd
)))
1830 #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1832 int hugepage_madvise(struct vm_area_struct
*vma
,
1833 unsigned long *vm_flags
, int advice
)
1839 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1840 * can't handle this properly after s390_enable_sie, so we simply
1841 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1843 if (mm_has_pgste(vma
->vm_mm
))
1847 * Be somewhat over-protective like KSM for now!
1849 if (*vm_flags
& VM_NO_THP
)
1851 *vm_flags
&= ~VM_NOHUGEPAGE
;
1852 *vm_flags
|= VM_HUGEPAGE
;
1854 * If the vma become good for khugepaged to scan,
1855 * register it here without waiting a page fault that
1856 * may not happen any time soon.
1858 if (unlikely(khugepaged_enter_vma_merge(vma
, *vm_flags
)))
1861 case MADV_NOHUGEPAGE
:
1863 * Be somewhat over-protective like KSM for now!
1865 if (*vm_flags
& VM_NO_THP
)
1867 *vm_flags
&= ~VM_HUGEPAGE
;
1868 *vm_flags
|= VM_NOHUGEPAGE
;
1870 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1871 * this vma even if we leave the mm registered in khugepaged if
1872 * it got registered before VM_NOHUGEPAGE was set.
1880 static int __init
khugepaged_slab_init(void)
1882 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
1883 sizeof(struct mm_slot
),
1884 __alignof__(struct mm_slot
), 0, NULL
);
1891 static void __init
khugepaged_slab_exit(void)
1893 kmem_cache_destroy(mm_slot_cache
);
1896 static inline struct mm_slot
*alloc_mm_slot(void)
1898 if (!mm_slot_cache
) /* initialization failed */
1900 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
1903 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
1905 kmem_cache_free(mm_slot_cache
, mm_slot
);
1908 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
1910 struct mm_slot
*mm_slot
;
1912 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
1913 if (mm
== mm_slot
->mm
)
1919 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
1920 struct mm_slot
*mm_slot
)
1923 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
1926 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
1928 return atomic_read(&mm
->mm_users
) == 0;
1931 int __khugepaged_enter(struct mm_struct
*mm
)
1933 struct mm_slot
*mm_slot
;
1936 mm_slot
= alloc_mm_slot();
1940 /* __khugepaged_exit() must not run from under us */
1941 VM_BUG_ON_MM(khugepaged_test_exit(mm
), mm
);
1942 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
1943 free_mm_slot(mm_slot
);
1947 spin_lock(&khugepaged_mm_lock
);
1948 insert_to_mm_slots_hash(mm
, mm_slot
);
1950 * Insert just behind the scanning cursor, to let the area settle
1953 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
1954 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
1955 spin_unlock(&khugepaged_mm_lock
);
1957 atomic_inc(&mm
->mm_count
);
1959 wake_up_interruptible(&khugepaged_wait
);
1964 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
1965 unsigned long vm_flags
)
1967 unsigned long hstart
, hend
;
1970 * Not yet faulted in so we will register later in the
1971 * page fault if needed.
1974 if (vma
->vm_ops
|| (vm_flags
& VM_NO_THP
))
1975 /* khugepaged not yet working on file or special mappings */
1977 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
1978 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
1980 return khugepaged_enter(vma
, vm_flags
);
1984 void __khugepaged_exit(struct mm_struct
*mm
)
1986 struct mm_slot
*mm_slot
;
1989 spin_lock(&khugepaged_mm_lock
);
1990 mm_slot
= get_mm_slot(mm
);
1991 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
1992 hash_del(&mm_slot
->hash
);
1993 list_del(&mm_slot
->mm_node
);
1996 spin_unlock(&khugepaged_mm_lock
);
1999 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
2000 free_mm_slot(mm_slot
);
2002 } else if (mm_slot
) {
2004 * This is required to serialize against
2005 * khugepaged_test_exit() (which is guaranteed to run
2006 * under mmap sem read mode). Stop here (after we
2007 * return all pagetables will be destroyed) until
2008 * khugepaged has finished working on the pagetables
2009 * under the mmap_sem.
2011 down_write(&mm
->mmap_sem
);
2012 up_write(&mm
->mmap_sem
);
2016 static void release_pte_page(struct page
*page
)
2018 /* 0 stands for page_is_file_cache(page) == false */
2019 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2021 putback_lru_page(page
);
2024 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
2026 while (--_pte
>= pte
) {
2027 pte_t pteval
= *_pte
;
2028 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
2029 release_pte_page(pte_page(pteval
));
2033 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
2034 unsigned long address
,
2037 struct page
*page
= NULL
;
2039 int none_or_zero
= 0, result
= 0;
2040 bool referenced
= false, writable
= false;
2042 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2043 _pte
++, address
+= PAGE_SIZE
) {
2044 pte_t pteval
= *_pte
;
2045 if (pte_none(pteval
) || (pte_present(pteval
) &&
2046 is_zero_pfn(pte_pfn(pteval
)))) {
2047 if (!userfaultfd_armed(vma
) &&
2048 ++none_or_zero
<= khugepaged_max_ptes_none
) {
2051 result
= SCAN_EXCEED_NONE_PTE
;
2055 if (!pte_present(pteval
)) {
2056 result
= SCAN_PTE_NON_PRESENT
;
2059 page
= vm_normal_page(vma
, address
, pteval
);
2060 if (unlikely(!page
)) {
2061 result
= SCAN_PAGE_NULL
;
2065 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2066 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
2067 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
2070 * We can do it before isolate_lru_page because the
2071 * page can't be freed from under us. NOTE: PG_lock
2072 * is needed to serialize against split_huge_page
2073 * when invoked from the VM.
2075 if (!trylock_page(page
)) {
2076 result
= SCAN_PAGE_LOCK
;
2081 * cannot use mapcount: can't collapse if there's a gup pin.
2082 * The page must only be referenced by the scanned process
2083 * and page swap cache.
2085 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2087 result
= SCAN_PAGE_COUNT
;
2090 if (pte_write(pteval
)) {
2093 if (PageSwapCache(page
) &&
2094 !reuse_swap_page(page
, NULL
)) {
2096 result
= SCAN_SWAP_CACHE_PAGE
;
2100 * Page is not in the swap cache. It can be collapsed
2106 * Isolate the page to avoid collapsing an hugepage
2107 * currently in use by the VM.
2109 if (isolate_lru_page(page
)) {
2111 result
= SCAN_DEL_PAGE_LRU
;
2114 /* 0 stands for page_is_file_cache(page) == false */
2115 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2116 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2117 VM_BUG_ON_PAGE(PageLRU(page
), page
);
2119 /* If there is no mapped pte young don't collapse the page */
2120 if (pte_young(pteval
) ||
2121 page_is_young(page
) || PageReferenced(page
) ||
2122 mmu_notifier_test_young(vma
->vm_mm
, address
))
2125 if (likely(writable
)) {
2126 if (likely(referenced
)) {
2127 result
= SCAN_SUCCEED
;
2128 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
2129 referenced
, writable
, result
);
2133 result
= SCAN_PAGE_RO
;
2137 release_pte_pages(pte
, _pte
);
2138 trace_mm_collapse_huge_page_isolate(page
, none_or_zero
,
2139 referenced
, writable
, result
);
2143 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2144 struct vm_area_struct
*vma
,
2145 unsigned long address
,
2149 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2150 pte_t pteval
= *_pte
;
2151 struct page
*src_page
;
2153 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2154 clear_user_highpage(page
, address
);
2155 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2156 if (is_zero_pfn(pte_pfn(pteval
))) {
2158 * ptl mostly unnecessary.
2162 * paravirt calls inside pte_clear here are
2165 pte_clear(vma
->vm_mm
, address
, _pte
);
2169 src_page
= pte_page(pteval
);
2170 copy_user_highpage(page
, src_page
, address
, vma
);
2171 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
2172 release_pte_page(src_page
);
2174 * ptl mostly unnecessary, but preempt has to
2175 * be disabled to update the per-cpu stats
2176 * inside page_remove_rmap().
2180 * paravirt calls inside pte_clear here are
2183 pte_clear(vma
->vm_mm
, address
, _pte
);
2184 page_remove_rmap(src_page
, false);
2186 free_page_and_swap_cache(src_page
);
2189 address
+= PAGE_SIZE
;
2194 static void khugepaged_alloc_sleep(void)
2198 add_wait_queue(&khugepaged_wait
, &wait
);
2199 freezable_schedule_timeout_interruptible(
2200 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2201 remove_wait_queue(&khugepaged_wait
, &wait
);
2204 static int khugepaged_node_load
[MAX_NUMNODES
];
2206 static bool khugepaged_scan_abort(int nid
)
2211 * If zone_reclaim_mode is disabled, then no extra effort is made to
2212 * allocate memory locally.
2214 if (!zone_reclaim_mode
)
2217 /* If there is a count for this node already, it must be acceptable */
2218 if (khugepaged_node_load
[nid
])
2221 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
2222 if (!khugepaged_node_load
[i
])
2224 if (node_distance(nid
, i
) > RECLAIM_DISTANCE
)
2231 static int khugepaged_find_target_node(void)
2233 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
2234 int nid
, target_node
= 0, max_value
= 0;
2236 /* find first node with max normal pages hit */
2237 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
2238 if (khugepaged_node_load
[nid
] > max_value
) {
2239 max_value
= khugepaged_node_load
[nid
];
2243 /* do some balance if several nodes have the same hit record */
2244 if (target_node
<= last_khugepaged_target_node
)
2245 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
2247 if (max_value
== khugepaged_node_load
[nid
]) {
2252 last_khugepaged_target_node
= target_node
;
2256 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2258 if (IS_ERR(*hpage
)) {
2264 khugepaged_alloc_sleep();
2265 } else if (*hpage
) {
2273 static struct page
*
2274 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2275 unsigned long address
, int node
)
2277 VM_BUG_ON_PAGE(*hpage
, *hpage
);
2280 * Before allocating the hugepage, release the mmap_sem read lock.
2281 * The allocation can take potentially a long time if it involves
2282 * sync compaction, and we do not need to hold the mmap_sem during
2283 * that. We will recheck the vma after taking it again in write mode.
2285 up_read(&mm
->mmap_sem
);
2287 *hpage
= __alloc_pages_node(node
, gfp
, HPAGE_PMD_ORDER
);
2288 if (unlikely(!*hpage
)) {
2289 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2290 *hpage
= ERR_PTR(-ENOMEM
);
2294 prep_transhuge_page(*hpage
);
2295 count_vm_event(THP_COLLAPSE_ALLOC
);
2299 static int khugepaged_find_target_node(void)
2304 static inline struct page
*alloc_khugepaged_hugepage(void)
2308 page
= alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2311 prep_transhuge_page(page
);
2315 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2320 hpage
= alloc_khugepaged_hugepage();
2322 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2327 khugepaged_alloc_sleep();
2329 count_vm_event(THP_COLLAPSE_ALLOC
);
2330 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2335 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2338 *hpage
= khugepaged_alloc_hugepage(wait
);
2340 if (unlikely(!*hpage
))
2346 static struct page
*
2347 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2348 unsigned long address
, int node
)
2350 up_read(&mm
->mmap_sem
);
2357 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2359 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2360 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2362 if (!vma
->anon_vma
|| vma
->vm_ops
)
2364 if (is_vma_temporary_stack(vma
))
2366 return !(vma
->vm_flags
& VM_NO_THP
);
2370 * If mmap_sem temporarily dropped, revalidate vma
2371 * before taking mmap_sem.
2372 * Return 0 if succeeds, otherwise return none-zero
2373 * value (scan code).
2376 static int hugepage_vma_revalidate(struct mm_struct
*mm
, unsigned long address
)
2378 struct vm_area_struct
*vma
;
2379 unsigned long hstart
, hend
;
2381 if (unlikely(khugepaged_test_exit(mm
)))
2382 return SCAN_ANY_PROCESS
;
2384 vma
= find_vma(mm
, address
);
2386 return SCAN_VMA_NULL
;
2388 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2389 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2390 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
2391 return SCAN_ADDRESS_RANGE
;
2392 if (!hugepage_vma_check(vma
))
2393 return SCAN_VMA_CHECK
;
2398 * Bring missing pages in from swap, to complete THP collapse.
2399 * Only done if khugepaged_scan_pmd believes it is worthwhile.
2401 * Called and returns without pte mapped or spinlocks held,
2402 * but with mmap_sem held to protect against vma changes.
2405 static bool __collapse_huge_page_swapin(struct mm_struct
*mm
,
2406 struct vm_area_struct
*vma
,
2407 unsigned long address
, pmd_t
*pmd
)
2410 int swapped_in
= 0, ret
= 0;
2411 struct fault_env fe
= {
2414 .flags
= FAULT_FLAG_ALLOW_RETRY
,
2418 fe
.pte
= pte_offset_map(pmd
, address
);
2419 for (; fe
.address
< address
+ HPAGE_PMD_NR
*PAGE_SIZE
;
2420 fe
.pte
++, fe
.address
+= PAGE_SIZE
) {
2422 if (!is_swap_pte(pteval
))
2425 ret
= do_swap_page(&fe
, pteval
);
2426 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
2427 if (ret
& VM_FAULT_RETRY
) {
2428 down_read(&mm
->mmap_sem
);
2429 /* vma is no longer available, don't continue to swapin */
2430 if (hugepage_vma_revalidate(mm
, address
))
2432 /* check if the pmd is still valid */
2433 if (mm_find_pmd(mm
, address
) != pmd
)
2436 if (ret
& VM_FAULT_ERROR
) {
2437 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, 0);
2440 /* pte is unmapped now, we need to map it */
2441 fe
.pte
= pte_offset_map(pmd
, fe
.address
);
2445 trace_mm_collapse_huge_page_swapin(mm
, swapped_in
, 1);
2449 static void collapse_huge_page(struct mm_struct
*mm
,
2450 unsigned long address
,
2451 struct page
**hpage
,
2452 struct vm_area_struct
*vma
,
2458 struct page
*new_page
;
2459 spinlock_t
*pmd_ptl
, *pte_ptl
;
2460 int isolated
= 0, result
= 0;
2461 struct mem_cgroup
*memcg
;
2462 unsigned long mmun_start
; /* For mmu_notifiers */
2463 unsigned long mmun_end
; /* For mmu_notifiers */
2466 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2468 /* Only allocate from the target node */
2469 gfp
= alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE
| __GFP_THISNODE
;
2471 /* release the mmap_sem read lock. */
2472 new_page
= khugepaged_alloc_page(hpage
, gfp
, mm
, address
, node
);
2474 result
= SCAN_ALLOC_HUGE_PAGE_FAIL
;
2478 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, gfp
, &memcg
, true))) {
2479 result
= SCAN_CGROUP_CHARGE_FAIL
;
2483 down_read(&mm
->mmap_sem
);
2484 result
= hugepage_vma_revalidate(mm
, address
);
2486 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2487 up_read(&mm
->mmap_sem
);
2491 pmd
= mm_find_pmd(mm
, address
);
2493 result
= SCAN_PMD_NULL
;
2494 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2495 up_read(&mm
->mmap_sem
);
2500 * __collapse_huge_page_swapin always returns with mmap_sem locked.
2501 * If it fails, release mmap_sem and jump directly out.
2502 * Continuing to collapse causes inconsistency.
2504 if (!__collapse_huge_page_swapin(mm
, vma
, address
, pmd
)) {
2505 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2506 up_read(&mm
->mmap_sem
);
2510 up_read(&mm
->mmap_sem
);
2512 * Prevent all access to pagetables with the exception of
2513 * gup_fast later handled by the ptep_clear_flush and the VM
2514 * handled by the anon_vma lock + PG_lock.
2516 down_write(&mm
->mmap_sem
);
2517 result
= hugepage_vma_revalidate(mm
, address
);
2520 /* check if the pmd is still valid */
2521 if (mm_find_pmd(mm
, address
) != pmd
)
2524 anon_vma_lock_write(vma
->anon_vma
);
2526 pte
= pte_offset_map(pmd
, address
);
2527 pte_ptl
= pte_lockptr(mm
, pmd
);
2529 mmun_start
= address
;
2530 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2531 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2532 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
2534 * After this gup_fast can't run anymore. This also removes
2535 * any huge TLB entry from the CPU so we won't allow
2536 * huge and small TLB entries for the same virtual address
2537 * to avoid the risk of CPU bugs in that area.
2539 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
2540 spin_unlock(pmd_ptl
);
2541 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2544 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2545 spin_unlock(pte_ptl
);
2547 if (unlikely(!isolated
)) {
2550 BUG_ON(!pmd_none(*pmd
));
2552 * We can only use set_pmd_at when establishing
2553 * hugepmds and never for establishing regular pmds that
2554 * points to regular pagetables. Use pmd_populate for that
2556 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
2557 spin_unlock(pmd_ptl
);
2558 anon_vma_unlock_write(vma
->anon_vma
);
2564 * All pages are isolated and locked so anon_vma rmap
2565 * can't run anymore.
2567 anon_vma_unlock_write(vma
->anon_vma
);
2569 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
2571 __SetPageUptodate(new_page
);
2572 pgtable
= pmd_pgtable(_pmd
);
2574 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2575 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
2578 * spin_lock() below is not the equivalent of smp_wmb(), so
2579 * this is needed to avoid the copy_huge_page writes to become
2580 * visible after the set_pmd_at() write.
2585 BUG_ON(!pmd_none(*pmd
));
2586 page_add_new_anon_rmap(new_page
, vma
, address
, true);
2587 mem_cgroup_commit_charge(new_page
, memcg
, false, true);
2588 lru_cache_add_active_or_unevictable(new_page
, vma
);
2589 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
2590 set_pmd_at(mm
, address
, pmd
, _pmd
);
2591 update_mmu_cache_pmd(vma
, address
, pmd
);
2592 spin_unlock(pmd_ptl
);
2596 khugepaged_pages_collapsed
++;
2597 result
= SCAN_SUCCEED
;
2599 up_write(&mm
->mmap_sem
);
2601 trace_mm_collapse_huge_page(mm
, isolated
, result
);
2604 mem_cgroup_cancel_charge(new_page
, memcg
, true);
2608 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2609 struct vm_area_struct
*vma
,
2610 unsigned long address
,
2611 struct page
**hpage
)
2615 int ret
= 0, none_or_zero
= 0, result
= 0;
2616 struct page
*page
= NULL
;
2617 unsigned long _address
;
2619 int node
= NUMA_NO_NODE
, unmapped
= 0;
2620 bool writable
= false, referenced
= false;
2622 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2624 pmd
= mm_find_pmd(mm
, address
);
2626 result
= SCAN_PMD_NULL
;
2630 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
2631 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2632 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2633 _pte
++, _address
+= PAGE_SIZE
) {
2634 pte_t pteval
= *_pte
;
2635 if (is_swap_pte(pteval
)) {
2636 if (++unmapped
<= khugepaged_max_ptes_swap
) {
2639 result
= SCAN_EXCEED_SWAP_PTE
;
2643 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2644 if (!userfaultfd_armed(vma
) &&
2645 ++none_or_zero
<= khugepaged_max_ptes_none
) {
2648 result
= SCAN_EXCEED_NONE_PTE
;
2652 if (!pte_present(pteval
)) {
2653 result
= SCAN_PTE_NON_PRESENT
;
2656 if (pte_write(pteval
))
2659 page
= vm_normal_page(vma
, _address
, pteval
);
2660 if (unlikely(!page
)) {
2661 result
= SCAN_PAGE_NULL
;
2665 /* TODO: teach khugepaged to collapse THP mapped with pte */
2666 if (PageCompound(page
)) {
2667 result
= SCAN_PAGE_COMPOUND
;
2672 * Record which node the original page is from and save this
2673 * information to khugepaged_node_load[].
2674 * Khupaged will allocate hugepage from the node has the max
2677 node
= page_to_nid(page
);
2678 if (khugepaged_scan_abort(node
)) {
2679 result
= SCAN_SCAN_ABORT
;
2682 khugepaged_node_load
[node
]++;
2683 if (!PageLRU(page
)) {
2684 result
= SCAN_PAGE_LRU
;
2687 if (PageLocked(page
)) {
2688 result
= SCAN_PAGE_LOCK
;
2691 if (!PageAnon(page
)) {
2692 result
= SCAN_PAGE_ANON
;
2697 * cannot use mapcount: can't collapse if there's a gup pin.
2698 * The page must only be referenced by the scanned process
2699 * and page swap cache.
2701 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2702 result
= SCAN_PAGE_COUNT
;
2705 if (pte_young(pteval
) ||
2706 page_is_young(page
) || PageReferenced(page
) ||
2707 mmu_notifier_test_young(vma
->vm_mm
, address
))
2712 result
= SCAN_SUCCEED
;
2715 result
= SCAN_NO_REFERENCED_PAGE
;
2718 result
= SCAN_PAGE_RO
;
2721 pte_unmap_unlock(pte
, ptl
);
2723 node
= khugepaged_find_target_node();
2724 /* collapse_huge_page will return with the mmap_sem released */
2725 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2728 trace_mm_khugepaged_scan_pmd(mm
, page
, writable
, referenced
,
2729 none_or_zero
, result
, unmapped
);
2733 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2735 struct mm_struct
*mm
= mm_slot
->mm
;
2737 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2739 if (khugepaged_test_exit(mm
)) {
2741 hash_del(&mm_slot
->hash
);
2742 list_del(&mm_slot
->mm_node
);
2745 * Not strictly needed because the mm exited already.
2747 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2750 /* khugepaged_mm_lock actually not necessary for the below */
2751 free_mm_slot(mm_slot
);
2756 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2757 struct page
**hpage
)
2758 __releases(&khugepaged_mm_lock
)
2759 __acquires(&khugepaged_mm_lock
)
2761 struct mm_slot
*mm_slot
;
2762 struct mm_struct
*mm
;
2763 struct vm_area_struct
*vma
;
2767 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2769 if (khugepaged_scan
.mm_slot
)
2770 mm_slot
= khugepaged_scan
.mm_slot
;
2772 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2773 struct mm_slot
, mm_node
);
2774 khugepaged_scan
.address
= 0;
2775 khugepaged_scan
.mm_slot
= mm_slot
;
2777 spin_unlock(&khugepaged_mm_lock
);
2780 down_read(&mm
->mmap_sem
);
2781 if (unlikely(khugepaged_test_exit(mm
)))
2784 vma
= find_vma(mm
, khugepaged_scan
.address
);
2787 for (; vma
; vma
= vma
->vm_next
) {
2788 unsigned long hstart
, hend
;
2791 if (unlikely(khugepaged_test_exit(mm
))) {
2795 if (!hugepage_vma_check(vma
)) {
2800 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2801 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2804 if (khugepaged_scan
.address
> hend
)
2806 if (khugepaged_scan
.address
< hstart
)
2807 khugepaged_scan
.address
= hstart
;
2808 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2810 while (khugepaged_scan
.address
< hend
) {
2813 if (unlikely(khugepaged_test_exit(mm
)))
2814 goto breakouterloop
;
2816 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2817 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2819 ret
= khugepaged_scan_pmd(mm
, vma
,
2820 khugepaged_scan
.address
,
2822 /* move to next address */
2823 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2824 progress
+= HPAGE_PMD_NR
;
2826 /* we released mmap_sem so break loop */
2827 goto breakouterloop_mmap_sem
;
2828 if (progress
>= pages
)
2829 goto breakouterloop
;
2833 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2834 breakouterloop_mmap_sem
:
2836 spin_lock(&khugepaged_mm_lock
);
2837 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2839 * Release the current mm_slot if this mm is about to die, or
2840 * if we scanned all vmas of this mm.
2842 if (khugepaged_test_exit(mm
) || !vma
) {
2844 * Make sure that if mm_users is reaching zero while
2845 * khugepaged runs here, khugepaged_exit will find
2846 * mm_slot not pointing to the exiting mm.
2848 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2849 khugepaged_scan
.mm_slot
= list_entry(
2850 mm_slot
->mm_node
.next
,
2851 struct mm_slot
, mm_node
);
2852 khugepaged_scan
.address
= 0;
2854 khugepaged_scan
.mm_slot
= NULL
;
2855 khugepaged_full_scans
++;
2858 collect_mm_slot(mm_slot
);
2864 static int khugepaged_has_work(void)
2866 return !list_empty(&khugepaged_scan
.mm_head
) &&
2867 khugepaged_enabled();
2870 static int khugepaged_wait_event(void)
2872 return !list_empty(&khugepaged_scan
.mm_head
) ||
2873 kthread_should_stop();
2876 static void khugepaged_do_scan(void)
2878 struct page
*hpage
= NULL
;
2879 unsigned int progress
= 0, pass_through_head
= 0;
2880 unsigned int pages
= khugepaged_pages_to_scan
;
2883 barrier(); /* write khugepaged_pages_to_scan to local stack */
2885 while (progress
< pages
) {
2886 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2891 if (unlikely(kthread_should_stop() || try_to_freeze()))
2894 spin_lock(&khugepaged_mm_lock
);
2895 if (!khugepaged_scan
.mm_slot
)
2896 pass_through_head
++;
2897 if (khugepaged_has_work() &&
2898 pass_through_head
< 2)
2899 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2903 spin_unlock(&khugepaged_mm_lock
);
2906 if (!IS_ERR_OR_NULL(hpage
))
2910 static bool khugepaged_should_wakeup(void)
2912 return kthread_should_stop() ||
2913 time_after_eq(jiffies
, khugepaged_sleep_expire
);
2916 static void khugepaged_wait_work(void)
2918 if (khugepaged_has_work()) {
2919 const unsigned long scan_sleep_jiffies
=
2920 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
);
2922 if (!scan_sleep_jiffies
)
2925 khugepaged_sleep_expire
= jiffies
+ scan_sleep_jiffies
;
2926 wait_event_freezable_timeout(khugepaged_wait
,
2927 khugepaged_should_wakeup(),
2928 scan_sleep_jiffies
);
2932 if (khugepaged_enabled())
2933 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2936 static int khugepaged(void *none
)
2938 struct mm_slot
*mm_slot
;
2941 set_user_nice(current
, MAX_NICE
);
2943 while (!kthread_should_stop()) {
2944 khugepaged_do_scan();
2945 khugepaged_wait_work();
2948 spin_lock(&khugepaged_mm_lock
);
2949 mm_slot
= khugepaged_scan
.mm_slot
;
2950 khugepaged_scan
.mm_slot
= NULL
;
2952 collect_mm_slot(mm_slot
);
2953 spin_unlock(&khugepaged_mm_lock
);
2957 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2958 unsigned long haddr
, pmd_t
*pmd
)
2960 struct mm_struct
*mm
= vma
->vm_mm
;
2965 /* leave pmd empty until pte is filled */
2966 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2968 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2969 pmd_populate(mm
, &_pmd
, pgtable
);
2971 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2973 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2974 entry
= pte_mkspecial(entry
);
2975 pte
= pte_offset_map(&_pmd
, haddr
);
2976 VM_BUG_ON(!pte_none(*pte
));
2977 set_pte_at(mm
, haddr
, pte
, entry
);
2980 smp_wmb(); /* make pte visible before pmd */
2981 pmd_populate(mm
, pmd
, pgtable
);
2982 put_huge_zero_page();
2985 static void __split_huge_pmd_locked(struct vm_area_struct
*vma
, pmd_t
*pmd
,
2986 unsigned long haddr
, bool freeze
)
2988 struct mm_struct
*mm
= vma
->vm_mm
;
2992 bool young
, write
, dirty
;
2996 VM_BUG_ON(haddr
& ~HPAGE_PMD_MASK
);
2997 VM_BUG_ON_VMA(vma
->vm_start
> haddr
, vma
);
2998 VM_BUG_ON_VMA(vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
, vma
);
2999 VM_BUG_ON(!pmd_trans_huge(*pmd
) && !pmd_devmap(*pmd
));
3001 count_vm_event(THP_SPLIT_PMD
);
3003 if (!vma_is_anonymous(vma
)) {
3004 _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
3005 if (is_huge_zero_pmd(_pmd
))
3006 put_huge_zero_page();
3007 if (vma_is_dax(vma
))
3009 page
= pmd_page(_pmd
);
3010 if (!PageReferenced(page
) && pmd_young(_pmd
))
3011 SetPageReferenced(page
);
3012 page_remove_rmap(page
, true);
3014 add_mm_counter(mm
, MM_FILEPAGES
, -HPAGE_PMD_NR
);
3016 } else if (is_huge_zero_pmd(*pmd
)) {
3017 return __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
3020 page
= pmd_page(*pmd
);
3021 VM_BUG_ON_PAGE(!page_count(page
), page
);
3022 page_ref_add(page
, HPAGE_PMD_NR
- 1);
3023 write
= pmd_write(*pmd
);
3024 young
= pmd_young(*pmd
);
3025 dirty
= pmd_dirty(*pmd
);
3027 pmdp_huge_split_prepare(vma
, haddr
, pmd
);
3028 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
3029 pmd_populate(mm
, &_pmd
, pgtable
);
3031 for (i
= 0, addr
= haddr
; i
< HPAGE_PMD_NR
; i
++, addr
+= PAGE_SIZE
) {
3034 * Note that NUMA hinting access restrictions are not
3035 * transferred to avoid any possibility of altering
3036 * permissions across VMAs.
3039 swp_entry_t swp_entry
;
3040 swp_entry
= make_migration_entry(page
+ i
, write
);
3041 entry
= swp_entry_to_pte(swp_entry
);
3043 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
3044 entry
= maybe_mkwrite(entry
, vma
);
3046 entry
= pte_wrprotect(entry
);
3048 entry
= pte_mkold(entry
);
3051 SetPageDirty(page
+ i
);
3052 pte
= pte_offset_map(&_pmd
, addr
);
3053 BUG_ON(!pte_none(*pte
));
3054 set_pte_at(mm
, addr
, pte
, entry
);
3055 atomic_inc(&page
[i
]._mapcount
);
3060 * Set PG_double_map before dropping compound_mapcount to avoid
3061 * false-negative page_mapped().
3063 if (compound_mapcount(page
) > 1 && !TestSetPageDoubleMap(page
)) {
3064 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3065 atomic_inc(&page
[i
]._mapcount
);
3068 if (atomic_add_negative(-1, compound_mapcount_ptr(page
))) {
3069 /* Last compound_mapcount is gone. */
3070 __dec_zone_page_state(page
, NR_ANON_THPS
);
3071 if (TestClearPageDoubleMap(page
)) {
3072 /* No need in mapcount reference anymore */
3073 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3074 atomic_dec(&page
[i
]._mapcount
);
3078 smp_wmb(); /* make pte visible before pmd */
3080 * Up to this point the pmd is present and huge and userland has the
3081 * whole access to the hugepage during the split (which happens in
3082 * place). If we overwrite the pmd with the not-huge version pointing
3083 * to the pte here (which of course we could if all CPUs were bug
3084 * free), userland could trigger a small page size TLB miss on the
3085 * small sized TLB while the hugepage TLB entry is still established in
3086 * the huge TLB. Some CPU doesn't like that.
3087 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
3088 * 383 on page 93. Intel should be safe but is also warns that it's
3089 * only safe if the permission and cache attributes of the two entries
3090 * loaded in the two TLB is identical (which should be the case here).
3091 * But it is generally safer to never allow small and huge TLB entries
3092 * for the same virtual address to be loaded simultaneously. So instead
3093 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
3094 * current pmd notpresent (atomically because here the pmd_trans_huge
3095 * and pmd_trans_splitting must remain set at all times on the pmd
3096 * until the split is complete for this pmd), then we flush the SMP TLB
3097 * and finally we write the non-huge version of the pmd entry with
3100 pmdp_invalidate(vma
, haddr
, pmd
);
3101 pmd_populate(mm
, pmd
, pgtable
);
3104 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
3105 page_remove_rmap(page
+ i
, false);
3111 void __split_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
3112 unsigned long address
, bool freeze
, struct page
*page
)
3115 struct mm_struct
*mm
= vma
->vm_mm
;
3116 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
3118 mmu_notifier_invalidate_range_start(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
3119 ptl
= pmd_lock(mm
, pmd
);
3122 * If caller asks to setup a migration entries, we need a page to check
3123 * pmd against. Otherwise we can end up replacing wrong page.
3125 VM_BUG_ON(freeze
&& !page
);
3126 if (page
&& page
!= pmd_page(*pmd
))
3129 if (pmd_trans_huge(*pmd
)) {
3130 page
= pmd_page(*pmd
);
3131 if (PageMlocked(page
))
3132 clear_page_mlock(page
);
3133 } else if (!pmd_devmap(*pmd
))
3135 __split_huge_pmd_locked(vma
, pmd
, haddr
, freeze
);
3138 mmu_notifier_invalidate_range_end(mm
, haddr
, haddr
+ HPAGE_PMD_SIZE
);
3141 void split_huge_pmd_address(struct vm_area_struct
*vma
, unsigned long address
,
3142 bool freeze
, struct page
*page
)
3148 pgd
= pgd_offset(vma
->vm_mm
, address
);
3149 if (!pgd_present(*pgd
))
3152 pud
= pud_offset(pgd
, address
);
3153 if (!pud_present(*pud
))
3156 pmd
= pmd_offset(pud
, address
);
3158 __split_huge_pmd(vma
, pmd
, address
, freeze
, page
);
3161 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
3162 unsigned long start
,
3167 * If the new start address isn't hpage aligned and it could
3168 * previously contain an hugepage: check if we need to split
3171 if (start
& ~HPAGE_PMD_MASK
&&
3172 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3173 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3174 split_huge_pmd_address(vma
, start
, false, NULL
);
3177 * If the new end address isn't hpage aligned and it could
3178 * previously contain an hugepage: check if we need to split
3181 if (end
& ~HPAGE_PMD_MASK
&&
3182 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3183 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3184 split_huge_pmd_address(vma
, end
, false, NULL
);
3187 * If we're also updating the vma->vm_next->vm_start, if the new
3188 * vm_next->vm_start isn't page aligned and it could previously
3189 * contain an hugepage: check if we need to split an huge pmd.
3191 if (adjust_next
> 0) {
3192 struct vm_area_struct
*next
= vma
->vm_next
;
3193 unsigned long nstart
= next
->vm_start
;
3194 nstart
+= adjust_next
<< PAGE_SHIFT
;
3195 if (nstart
& ~HPAGE_PMD_MASK
&&
3196 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
3197 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
3198 split_huge_pmd_address(next
, nstart
, false, NULL
);
3202 static void freeze_page(struct page
*page
)
3204 enum ttu_flags ttu_flags
= TTU_IGNORE_MLOCK
| TTU_IGNORE_ACCESS
|
3208 VM_BUG_ON_PAGE(!PageHead(page
), page
);
3211 ttu_flags
|= TTU_MIGRATION
;
3213 /* We only need TTU_SPLIT_HUGE_PMD once */
3214 ret
= try_to_unmap(page
, ttu_flags
| TTU_SPLIT_HUGE_PMD
);
3215 for (i
= 1; !ret
&& i
< HPAGE_PMD_NR
; i
++) {
3216 /* Cut short if the page is unmapped */
3217 if (page_count(page
) == 1)
3220 ret
= try_to_unmap(page
+ i
, ttu_flags
);
3222 VM_BUG_ON_PAGE(ret
, page
+ i
- 1);
3225 static void unfreeze_page(struct page
*page
)
3229 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3230 remove_migration_ptes(page
+ i
, page
+ i
, true);
3233 static void __split_huge_page_tail(struct page
*head
, int tail
,
3234 struct lruvec
*lruvec
, struct list_head
*list
)
3236 struct page
*page_tail
= head
+ tail
;
3238 VM_BUG_ON_PAGE(atomic_read(&page_tail
->_mapcount
) != -1, page_tail
);
3239 VM_BUG_ON_PAGE(page_ref_count(page_tail
) != 0, page_tail
);
3242 * tail_page->_refcount 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_inc() or
3245 * atomic_add(), we 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_inc()/atomic_add().
3252 if (PageAnon(head
)) {
3253 page_ref_inc(page_tail
);
3255 /* Additional pin to radix tree */
3256 page_ref_add(page_tail
, 2);
3259 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
3260 page_tail
->flags
|= (head
->flags
&
3261 ((1L << PG_referenced
) |
3262 (1L << PG_swapbacked
) |
3263 (1L << PG_mlocked
) |
3264 (1L << PG_uptodate
) |
3267 (1L << PG_unevictable
) |
3271 * After clearing PageTail the gup refcount can be released.
3272 * Page flags also must be visible before we make the page non-compound.
3276 clear_compound_head(page_tail
);
3278 if (page_is_young(head
))
3279 set_page_young(page_tail
);
3280 if (page_is_idle(head
))
3281 set_page_idle(page_tail
);
3283 /* ->mapping in first tail page is compound_mapcount */
3284 VM_BUG_ON_PAGE(tail
> 2 && page_tail
->mapping
!= TAIL_MAPPING
,
3286 page_tail
->mapping
= head
->mapping
;
3288 page_tail
->index
= head
->index
+ tail
;
3289 page_cpupid_xchg_last(page_tail
, page_cpupid_last(head
));
3290 lru_add_page_tail(head
, page_tail
, lruvec
, list
);
3293 static void __split_huge_page(struct page
*page
, struct list_head
*list
,
3294 unsigned long flags
)
3296 struct page
*head
= compound_head(page
);
3297 struct zone
*zone
= page_zone(head
);
3298 struct lruvec
*lruvec
;
3302 lruvec
= mem_cgroup_page_lruvec(head
, zone
);
3304 /* complete memcg works before add pages to LRU */
3305 mem_cgroup_split_huge_fixup(head
);
3307 if (!PageAnon(page
))
3308 end
= DIV_ROUND_UP(i_size_read(head
->mapping
->host
), PAGE_SIZE
);
3310 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
3311 __split_huge_page_tail(head
, i
, lruvec
, list
);
3312 /* Some pages can be beyond i_size: drop them from page cache */
3313 if (head
[i
].index
>= end
) {
3314 __ClearPageDirty(head
+ i
);
3315 __delete_from_page_cache(head
+ i
, NULL
);
3320 ClearPageCompound(head
);
3321 /* See comment in __split_huge_page_tail() */
3322 if (PageAnon(head
)) {
3325 /* Additional pin to radix tree */
3326 page_ref_add(head
, 2);
3327 spin_unlock(&head
->mapping
->tree_lock
);
3330 spin_unlock_irqrestore(&page_zone(head
)->lru_lock
, flags
);
3332 unfreeze_page(head
);
3334 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
3335 struct page
*subpage
= head
+ i
;
3336 if (subpage
== page
)
3338 unlock_page(subpage
);
3341 * Subpages may be freed if there wasn't any mapping
3342 * like if add_to_swap() is running on a lru page that
3343 * had its mapping zapped. And freeing these pages
3344 * requires taking the lru_lock so we do the put_page
3345 * of the tail pages after the split is complete.
3351 int total_mapcount(struct page
*page
)
3353 int i
, compound
, ret
;
3355 VM_BUG_ON_PAGE(PageTail(page
), page
);
3357 if (likely(!PageCompound(page
)))
3358 return atomic_read(&page
->_mapcount
) + 1;
3360 compound
= compound_mapcount(page
);
3364 for (i
= 0; i
< HPAGE_PMD_NR
; i
++)
3365 ret
+= atomic_read(&page
[i
]._mapcount
) + 1;
3366 /* File pages has compound_mapcount included in _mapcount */
3367 if (!PageAnon(page
))
3368 return ret
- compound
* HPAGE_PMD_NR
;
3369 if (PageDoubleMap(page
))
3370 ret
-= HPAGE_PMD_NR
;
3375 * This calculates accurately how many mappings a transparent hugepage
3376 * has (unlike page_mapcount() which isn't fully accurate). This full
3377 * accuracy is primarily needed to know if copy-on-write faults can
3378 * reuse the page and change the mapping to read-write instead of
3379 * copying them. At the same time this returns the total_mapcount too.
3381 * The function returns the highest mapcount any one of the subpages
3382 * has. If the return value is one, even if different processes are
3383 * mapping different subpages of the transparent hugepage, they can
3384 * all reuse it, because each process is reusing a different subpage.
3386 * The total_mapcount is instead counting all virtual mappings of the
3387 * subpages. If the total_mapcount is equal to "one", it tells the
3388 * caller all mappings belong to the same "mm" and in turn the
3389 * anon_vma of the transparent hugepage can become the vma->anon_vma
3390 * local one as no other process may be mapping any of the subpages.
3392 * It would be more accurate to replace page_mapcount() with
3393 * page_trans_huge_mapcount(), however we only use
3394 * page_trans_huge_mapcount() in the copy-on-write faults where we
3395 * need full accuracy to avoid breaking page pinning, because
3396 * page_trans_huge_mapcount() is slower than page_mapcount().
3398 int page_trans_huge_mapcount(struct page
*page
, int *total_mapcount
)
3400 int i
, ret
, _total_mapcount
, mapcount
;
3402 /* hugetlbfs shouldn't call it */
3403 VM_BUG_ON_PAGE(PageHuge(page
), page
);
3405 if (likely(!PageTransCompound(page
))) {
3406 mapcount
= atomic_read(&page
->_mapcount
) + 1;
3408 *total_mapcount
= mapcount
;
3412 page
= compound_head(page
);
3414 _total_mapcount
= ret
= 0;
3415 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
3416 mapcount
= atomic_read(&page
[i
]._mapcount
) + 1;
3417 ret
= max(ret
, mapcount
);
3418 _total_mapcount
+= mapcount
;
3420 if (PageDoubleMap(page
)) {
3422 _total_mapcount
-= HPAGE_PMD_NR
;
3424 mapcount
= compound_mapcount(page
);
3426 _total_mapcount
+= mapcount
;
3428 *total_mapcount
= _total_mapcount
;
3433 * This function splits huge page into normal pages. @page can point to any
3434 * subpage of huge page to split. Split doesn't change the position of @page.
3436 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3437 * The huge page must be locked.
3439 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3441 * Both head page and tail pages will inherit mapping, flags, and so on from
3444 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3445 * they are not mapped.
3447 * Returns 0 if the hugepage is split successfully.
3448 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3451 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
3453 struct page
*head
= compound_head(page
);
3454 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(head
));
3455 struct anon_vma
*anon_vma
= NULL
;
3456 struct address_space
*mapping
= NULL
;
3457 int count
, mapcount
, extra_pins
, ret
;
3459 unsigned long flags
;
3461 VM_BUG_ON_PAGE(is_huge_zero_page(page
), page
);
3462 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
3463 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
3464 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
3466 if (PageAnon(head
)) {
3468 * The caller does not necessarily hold an mmap_sem that would
3469 * prevent the anon_vma disappearing so we first we take a
3470 * reference to it and then lock the anon_vma for write. This
3471 * is similar to page_lock_anon_vma_read except the write lock
3472 * is taken to serialise against parallel split or collapse
3475 anon_vma
= page_get_anon_vma(head
);
3482 anon_vma_lock_write(anon_vma
);
3484 mapping
= head
->mapping
;
3492 /* Addidional pins from radix tree */
3493 extra_pins
= HPAGE_PMD_NR
;
3495 i_mmap_lock_read(mapping
);
3499 * Racy check if we can split the page, before freeze_page() will
3502 if (total_mapcount(head
) != page_count(head
) - extra_pins
- 1) {
3507 mlocked
= PageMlocked(page
);
3509 VM_BUG_ON_PAGE(compound_mapcount(head
), head
);
3511 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3515 /* prevent PageLRU to go away from under us, and freeze lru stats */
3516 spin_lock_irqsave(&page_zone(head
)->lru_lock
, flags
);
3521 spin_lock(&mapping
->tree_lock
);
3522 pslot
= radix_tree_lookup_slot(&mapping
->page_tree
,
3525 * Check if the head page is present in radix tree.
3526 * We assume all tail are present too, if head is there.
3528 if (radix_tree_deref_slot_protected(pslot
,
3529 &mapping
->tree_lock
) != head
)
3533 /* Prevent deferred_split_scan() touching ->_refcount */
3534 spin_lock(&pgdata
->split_queue_lock
);
3535 count
= page_count(head
);
3536 mapcount
= total_mapcount(head
);
3537 if (!mapcount
&& page_ref_freeze(head
, 1 + extra_pins
)) {
3538 if (!list_empty(page_deferred_list(head
))) {
3539 pgdata
->split_queue_len
--;
3540 list_del(page_deferred_list(head
));
3543 __dec_zone_page_state(page
, NR_SHMEM_THPS
);
3544 spin_unlock(&pgdata
->split_queue_lock
);
3545 __split_huge_page(page
, list
, flags
);
3548 if (IS_ENABLED(CONFIG_DEBUG_VM
) && mapcount
) {
3549 pr_alert("total_mapcount: %u, page_count(): %u\n",
3552 dump_page(head
, NULL
);
3553 dump_page(page
, "total_mapcount(head) > 0");
3556 spin_unlock(&pgdata
->split_queue_lock
);
3558 spin_unlock(&mapping
->tree_lock
);
3559 spin_unlock_irqrestore(&page_zone(head
)->lru_lock
, flags
);
3560 unfreeze_page(head
);
3566 anon_vma_unlock_write(anon_vma
);
3567 put_anon_vma(anon_vma
);
3570 i_mmap_unlock_read(mapping
);
3572 count_vm_event(!ret
? THP_SPLIT_PAGE
: THP_SPLIT_PAGE_FAILED
);
3576 void free_transhuge_page(struct page
*page
)
3578 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
3579 unsigned long flags
;
3581 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3582 if (!list_empty(page_deferred_list(page
))) {
3583 pgdata
->split_queue_len
--;
3584 list_del(page_deferred_list(page
));
3586 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3587 free_compound_page(page
);
3590 void deferred_split_huge_page(struct page
*page
)
3592 struct pglist_data
*pgdata
= NODE_DATA(page_to_nid(page
));
3593 unsigned long flags
;
3595 VM_BUG_ON_PAGE(!PageTransHuge(page
), page
);
3597 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3598 if (list_empty(page_deferred_list(page
))) {
3599 count_vm_event(THP_DEFERRED_SPLIT_PAGE
);
3600 list_add_tail(page_deferred_list(page
), &pgdata
->split_queue
);
3601 pgdata
->split_queue_len
++;
3603 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3606 static unsigned long deferred_split_count(struct shrinker
*shrink
,
3607 struct shrink_control
*sc
)
3609 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
3610 return ACCESS_ONCE(pgdata
->split_queue_len
);
3613 static unsigned long deferred_split_scan(struct shrinker
*shrink
,
3614 struct shrink_control
*sc
)
3616 struct pglist_data
*pgdata
= NODE_DATA(sc
->nid
);
3617 unsigned long flags
;
3618 LIST_HEAD(list
), *pos
, *next
;
3622 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3623 /* Take pin on all head pages to avoid freeing them under us */
3624 list_for_each_safe(pos
, next
, &pgdata
->split_queue
) {
3625 page
= list_entry((void *)pos
, struct page
, mapping
);
3626 page
= compound_head(page
);
3627 if (get_page_unless_zero(page
)) {
3628 list_move(page_deferred_list(page
), &list
);
3630 /* We lost race with put_compound_page() */
3631 list_del_init(page_deferred_list(page
));
3632 pgdata
->split_queue_len
--;
3634 if (!--sc
->nr_to_scan
)
3637 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3639 list_for_each_safe(pos
, next
, &list
) {
3640 page
= list_entry((void *)pos
, struct page
, mapping
);
3642 /* split_huge_page() removes page from list on success */
3643 if (!split_huge_page(page
))
3649 spin_lock_irqsave(&pgdata
->split_queue_lock
, flags
);
3650 list_splice_tail(&list
, &pgdata
->split_queue
);
3651 spin_unlock_irqrestore(&pgdata
->split_queue_lock
, flags
);
3654 * Stop shrinker if we didn't split any page, but the queue is empty.
3655 * This can happen if pages were freed under us.
3657 if (!split
&& list_empty(&pgdata
->split_queue
))
3662 static struct shrinker deferred_split_shrinker
= {
3663 .count_objects
= deferred_split_count
,
3664 .scan_objects
= deferred_split_scan
,
3665 .seeks
= DEFAULT_SEEKS
,
3666 .flags
= SHRINKER_NUMA_AWARE
,
3669 #ifdef CONFIG_DEBUG_FS
3670 static int split_huge_pages_set(void *data
, u64 val
)
3674 unsigned long pfn
, max_zone_pfn
;
3675 unsigned long total
= 0, split
= 0;
3680 for_each_populated_zone(zone
) {
3681 max_zone_pfn
= zone_end_pfn(zone
);
3682 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++) {
3683 if (!pfn_valid(pfn
))
3686 page
= pfn_to_page(pfn
);
3687 if (!get_page_unless_zero(page
))
3690 if (zone
!= page_zone(page
))
3693 if (!PageHead(page
) || PageHuge(page
) || !PageLRU(page
))
3698 if (!split_huge_page(page
))
3706 pr_info("%lu of %lu THP split\n", split
, total
);
3710 DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops
, NULL
, split_huge_pages_set
,
3713 static int __init
split_huge_pages_debugfs(void)
3717 ret
= debugfs_create_file("split_huge_pages", 0200, NULL
, NULL
,
3718 &split_huge_pages_fops
);
3720 pr_warn("Failed to create split_huge_pages in debugfs");
3723 late_initcall(split_huge_pages_debugfs
);