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/dax.h>
20 #include <linux/kthread.h>
21 #include <linux/khugepaged.h>
22 #include <linux/freezer.h>
23 #include <linux/mman.h>
24 #include <linux/pagemap.h>
25 #include <linux/migrate.h>
26 #include <linux/hashtable.h>
27 #include <linux/userfaultfd_k.h>
30 #include <asm/pgalloc.h>
34 * By default transparent hugepage support is disabled in order that avoid
35 * to risk increase the memory footprint of applications without a guaranteed
36 * benefit. When transparent hugepage support is enabled, is for all mappings,
37 * and khugepaged scans all mappings.
38 * Defrag is invoked by khugepaged hugepage allocations and by page faults
39 * for all hugepage allocations.
41 unsigned long transparent_hugepage_flags __read_mostly
=
42 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
43 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
45 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
46 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
48 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
49 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
50 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
52 /* default scan 8*512 pte (or vmas) every 30 second */
53 static unsigned int khugepaged_pages_to_scan __read_mostly
= HPAGE_PMD_NR
*8;
54 static unsigned int khugepaged_pages_collapsed
;
55 static unsigned int khugepaged_full_scans
;
56 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
57 /* during fragmentation poll the hugepage allocator once every minute */
58 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
59 static struct task_struct
*khugepaged_thread __read_mostly
;
60 static DEFINE_MUTEX(khugepaged_mutex
);
61 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
62 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
64 * default collapse hugepages if there is at least one pte mapped like
65 * it would have happened if the vma was large enough during page
68 static unsigned int khugepaged_max_ptes_none __read_mostly
= HPAGE_PMD_NR
-1;
70 static int khugepaged(void *none
);
71 static int khugepaged_slab_init(void);
72 static void khugepaged_slab_exit(void);
74 #define MM_SLOTS_HASH_BITS 10
75 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
77 static struct kmem_cache
*mm_slot_cache __read_mostly
;
80 * struct mm_slot - hash lookup from mm to mm_slot
81 * @hash: hash collision list
82 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
83 * @mm: the mm that this information is valid for
86 struct hlist_node hash
;
87 struct list_head mm_node
;
92 * struct khugepaged_scan - cursor for scanning
93 * @mm_head: the head of the mm list to scan
94 * @mm_slot: the current mm_slot we are scanning
95 * @address: the next address inside that to be scanned
97 * There is only the one khugepaged_scan instance of this cursor structure.
99 struct khugepaged_scan
{
100 struct list_head mm_head
;
101 struct mm_slot
*mm_slot
;
102 unsigned long address
;
104 static struct khugepaged_scan khugepaged_scan
= {
105 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
109 static void set_recommended_min_free_kbytes(void)
113 unsigned long recommended_min
;
115 for_each_populated_zone(zone
)
118 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
119 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
122 * Make sure that on average at least two pageblocks are almost free
123 * of another type, one for a migratetype to fall back to and a
124 * second to avoid subsequent fallbacks of other types There are 3
125 * MIGRATE_TYPES we care about.
127 recommended_min
+= pageblock_nr_pages
* nr_zones
*
128 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
130 /* don't ever allow to reserve more than 5% of the lowmem */
131 recommended_min
= min(recommended_min
,
132 (unsigned long) nr_free_buffer_pages() / 20);
133 recommended_min
<<= (PAGE_SHIFT
-10);
135 if (recommended_min
> min_free_kbytes
) {
136 if (user_min_free_kbytes
>= 0)
137 pr_info("raising min_free_kbytes from %d to %lu "
138 "to help transparent hugepage allocations\n",
139 min_free_kbytes
, recommended_min
);
141 min_free_kbytes
= recommended_min
;
143 setup_per_zone_wmarks();
146 static int start_stop_khugepaged(void)
149 if (khugepaged_enabled()) {
150 if (!khugepaged_thread
)
151 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
153 if (unlikely(IS_ERR(khugepaged_thread
))) {
154 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
155 err
= PTR_ERR(khugepaged_thread
);
156 khugepaged_thread
= NULL
;
160 if (!list_empty(&khugepaged_scan
.mm_head
))
161 wake_up_interruptible(&khugepaged_wait
);
163 set_recommended_min_free_kbytes();
164 } else if (khugepaged_thread
) {
165 kthread_stop(khugepaged_thread
);
166 khugepaged_thread
= NULL
;
172 static atomic_t huge_zero_refcount
;
173 struct page
*huge_zero_page __read_mostly
;
175 struct page
*get_huge_zero_page(void)
177 struct page
*zero_page
;
179 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
180 return READ_ONCE(huge_zero_page
);
182 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
185 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
188 count_vm_event(THP_ZERO_PAGE_ALLOC
);
190 if (cmpxchg(&huge_zero_page
, NULL
, zero_page
)) {
192 __free_pages(zero_page
, compound_order(zero_page
));
196 /* We take additional reference here. It will be put back by shrinker */
197 atomic_set(&huge_zero_refcount
, 2);
199 return READ_ONCE(huge_zero_page
);
202 static void put_huge_zero_page(void)
205 * Counter should never go to zero here. Only shrinker can put
208 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
211 static unsigned long shrink_huge_zero_page_count(struct shrinker
*shrink
,
212 struct shrink_control
*sc
)
214 /* we can free zero page only if last reference remains */
215 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
218 static unsigned long shrink_huge_zero_page_scan(struct shrinker
*shrink
,
219 struct shrink_control
*sc
)
221 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
222 struct page
*zero_page
= xchg(&huge_zero_page
, NULL
);
223 BUG_ON(zero_page
== NULL
);
224 __free_pages(zero_page
, compound_order(zero_page
));
231 static struct shrinker huge_zero_page_shrinker
= {
232 .count_objects
= shrink_huge_zero_page_count
,
233 .scan_objects
= shrink_huge_zero_page_scan
,
234 .seeks
= DEFAULT_SEEKS
,
239 static ssize_t
double_flag_show(struct kobject
*kobj
,
240 struct kobj_attribute
*attr
, char *buf
,
241 enum transparent_hugepage_flag enabled
,
242 enum transparent_hugepage_flag req_madv
)
244 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
245 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
246 return sprintf(buf
, "[always] madvise never\n");
247 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
248 return sprintf(buf
, "always [madvise] never\n");
250 return sprintf(buf
, "always madvise [never]\n");
252 static ssize_t
double_flag_store(struct kobject
*kobj
,
253 struct kobj_attribute
*attr
,
254 const char *buf
, size_t count
,
255 enum transparent_hugepage_flag enabled
,
256 enum transparent_hugepage_flag req_madv
)
258 if (!memcmp("always", buf
,
259 min(sizeof("always")-1, count
))) {
260 set_bit(enabled
, &transparent_hugepage_flags
);
261 clear_bit(req_madv
, &transparent_hugepage_flags
);
262 } else if (!memcmp("madvise", buf
,
263 min(sizeof("madvise")-1, count
))) {
264 clear_bit(enabled
, &transparent_hugepage_flags
);
265 set_bit(req_madv
, &transparent_hugepage_flags
);
266 } else if (!memcmp("never", buf
,
267 min(sizeof("never")-1, count
))) {
268 clear_bit(enabled
, &transparent_hugepage_flags
);
269 clear_bit(req_madv
, &transparent_hugepage_flags
);
276 static ssize_t
enabled_show(struct kobject
*kobj
,
277 struct kobj_attribute
*attr
, char *buf
)
279 return double_flag_show(kobj
, attr
, buf
,
280 TRANSPARENT_HUGEPAGE_FLAG
,
281 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
283 static ssize_t
enabled_store(struct kobject
*kobj
,
284 struct kobj_attribute
*attr
,
285 const char *buf
, size_t count
)
289 ret
= double_flag_store(kobj
, attr
, buf
, count
,
290 TRANSPARENT_HUGEPAGE_FLAG
,
291 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
296 mutex_lock(&khugepaged_mutex
);
297 err
= start_stop_khugepaged();
298 mutex_unlock(&khugepaged_mutex
);
306 static struct kobj_attribute enabled_attr
=
307 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
309 static ssize_t
single_flag_show(struct kobject
*kobj
,
310 struct kobj_attribute
*attr
, char *buf
,
311 enum transparent_hugepage_flag flag
)
313 return sprintf(buf
, "%d\n",
314 !!test_bit(flag
, &transparent_hugepage_flags
));
317 static ssize_t
single_flag_store(struct kobject
*kobj
,
318 struct kobj_attribute
*attr
,
319 const char *buf
, size_t count
,
320 enum transparent_hugepage_flag flag
)
325 ret
= kstrtoul(buf
, 10, &value
);
332 set_bit(flag
, &transparent_hugepage_flags
);
334 clear_bit(flag
, &transparent_hugepage_flags
);
340 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
341 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
342 * memory just to allocate one more hugepage.
344 static ssize_t
defrag_show(struct kobject
*kobj
,
345 struct kobj_attribute
*attr
, char *buf
)
347 return double_flag_show(kobj
, attr
, buf
,
348 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
349 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
351 static ssize_t
defrag_store(struct kobject
*kobj
,
352 struct kobj_attribute
*attr
,
353 const char *buf
, size_t count
)
355 return double_flag_store(kobj
, attr
, buf
, count
,
356 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
357 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
359 static struct kobj_attribute defrag_attr
=
360 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
362 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
363 struct kobj_attribute
*attr
, char *buf
)
365 return single_flag_show(kobj
, attr
, buf
,
366 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
368 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
369 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
371 return single_flag_store(kobj
, attr
, buf
, count
,
372 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
374 static struct kobj_attribute use_zero_page_attr
=
375 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
376 #ifdef CONFIG_DEBUG_VM
377 static ssize_t
debug_cow_show(struct kobject
*kobj
,
378 struct kobj_attribute
*attr
, char *buf
)
380 return single_flag_show(kobj
, attr
, buf
,
381 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
383 static ssize_t
debug_cow_store(struct kobject
*kobj
,
384 struct kobj_attribute
*attr
,
385 const char *buf
, size_t count
)
387 return single_flag_store(kobj
, attr
, buf
, count
,
388 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
390 static struct kobj_attribute debug_cow_attr
=
391 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
392 #endif /* CONFIG_DEBUG_VM */
394 static struct attribute
*hugepage_attr
[] = {
397 &use_zero_page_attr
.attr
,
398 #ifdef CONFIG_DEBUG_VM
399 &debug_cow_attr
.attr
,
404 static struct attribute_group hugepage_attr_group
= {
405 .attrs
= hugepage_attr
,
408 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
409 struct kobj_attribute
*attr
,
412 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
415 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
416 struct kobj_attribute
*attr
,
417 const char *buf
, size_t count
)
422 err
= kstrtoul(buf
, 10, &msecs
);
423 if (err
|| msecs
> UINT_MAX
)
426 khugepaged_scan_sleep_millisecs
= msecs
;
427 wake_up_interruptible(&khugepaged_wait
);
431 static struct kobj_attribute scan_sleep_millisecs_attr
=
432 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
433 scan_sleep_millisecs_store
);
435 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
436 struct kobj_attribute
*attr
,
439 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
442 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
443 struct kobj_attribute
*attr
,
444 const char *buf
, size_t count
)
449 err
= kstrtoul(buf
, 10, &msecs
);
450 if (err
|| msecs
> UINT_MAX
)
453 khugepaged_alloc_sleep_millisecs
= msecs
;
454 wake_up_interruptible(&khugepaged_wait
);
458 static struct kobj_attribute alloc_sleep_millisecs_attr
=
459 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
460 alloc_sleep_millisecs_store
);
462 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
463 struct kobj_attribute
*attr
,
466 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
468 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
469 struct kobj_attribute
*attr
,
470 const char *buf
, size_t count
)
475 err
= kstrtoul(buf
, 10, &pages
);
476 if (err
|| !pages
|| pages
> UINT_MAX
)
479 khugepaged_pages_to_scan
= pages
;
483 static struct kobj_attribute pages_to_scan_attr
=
484 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
485 pages_to_scan_store
);
487 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
488 struct kobj_attribute
*attr
,
491 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
493 static struct kobj_attribute pages_collapsed_attr
=
494 __ATTR_RO(pages_collapsed
);
496 static ssize_t
full_scans_show(struct kobject
*kobj
,
497 struct kobj_attribute
*attr
,
500 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
502 static struct kobj_attribute full_scans_attr
=
503 __ATTR_RO(full_scans
);
505 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
506 struct kobj_attribute
*attr
, char *buf
)
508 return single_flag_show(kobj
, attr
, buf
,
509 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
511 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
512 struct kobj_attribute
*attr
,
513 const char *buf
, size_t count
)
515 return single_flag_store(kobj
, attr
, buf
, count
,
516 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
518 static struct kobj_attribute khugepaged_defrag_attr
=
519 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
520 khugepaged_defrag_store
);
523 * max_ptes_none controls if khugepaged should collapse hugepages over
524 * any unmapped ptes in turn potentially increasing the memory
525 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
526 * reduce the available free memory in the system as it
527 * runs. Increasing max_ptes_none will instead potentially reduce the
528 * free memory in the system during the khugepaged scan.
530 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
531 struct kobj_attribute
*attr
,
534 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
536 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
537 struct kobj_attribute
*attr
,
538 const char *buf
, size_t count
)
541 unsigned long max_ptes_none
;
543 err
= kstrtoul(buf
, 10, &max_ptes_none
);
544 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
547 khugepaged_max_ptes_none
= max_ptes_none
;
551 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
552 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
553 khugepaged_max_ptes_none_store
);
555 static struct attribute
*khugepaged_attr
[] = {
556 &khugepaged_defrag_attr
.attr
,
557 &khugepaged_max_ptes_none_attr
.attr
,
558 &pages_to_scan_attr
.attr
,
559 &pages_collapsed_attr
.attr
,
560 &full_scans_attr
.attr
,
561 &scan_sleep_millisecs_attr
.attr
,
562 &alloc_sleep_millisecs_attr
.attr
,
566 static struct attribute_group khugepaged_attr_group
= {
567 .attrs
= khugepaged_attr
,
568 .name
= "khugepaged",
571 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
575 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
576 if (unlikely(!*hugepage_kobj
)) {
577 pr_err("failed to create transparent hugepage kobject\n");
581 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
583 pr_err("failed to register transparent hugepage group\n");
587 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
589 pr_err("failed to register transparent hugepage group\n");
590 goto remove_hp_group
;
596 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
598 kobject_put(*hugepage_kobj
);
602 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
604 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
605 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
606 kobject_put(hugepage_kobj
);
609 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
614 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
617 #endif /* CONFIG_SYSFS */
619 static int __init
hugepage_init(void)
622 struct kobject
*hugepage_kobj
;
624 if (!has_transparent_hugepage()) {
625 transparent_hugepage_flags
= 0;
629 err
= hugepage_init_sysfs(&hugepage_kobj
);
633 err
= khugepaged_slab_init();
637 err
= register_shrinker(&huge_zero_page_shrinker
);
639 goto err_hzp_shrinker
;
642 * By default disable transparent hugepages on smaller systems,
643 * where the extra memory used could hurt more than TLB overhead
644 * is likely to save. The admin can still enable it through /sys.
646 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
647 transparent_hugepage_flags
= 0;
651 err
= start_stop_khugepaged();
657 unregister_shrinker(&huge_zero_page_shrinker
);
659 khugepaged_slab_exit();
661 hugepage_exit_sysfs(hugepage_kobj
);
665 subsys_initcall(hugepage_init
);
667 static int __init
setup_transparent_hugepage(char *str
)
672 if (!strcmp(str
, "always")) {
673 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
674 &transparent_hugepage_flags
);
675 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
676 &transparent_hugepage_flags
);
678 } else if (!strcmp(str
, "madvise")) {
679 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
680 &transparent_hugepage_flags
);
681 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
682 &transparent_hugepage_flags
);
684 } else if (!strcmp(str
, "never")) {
685 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
686 &transparent_hugepage_flags
);
687 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
688 &transparent_hugepage_flags
);
693 pr_warn("transparent_hugepage= cannot parse, ignored\n");
696 __setup("transparent_hugepage=", setup_transparent_hugepage
);
698 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
700 if (likely(vma
->vm_flags
& VM_WRITE
))
701 pmd
= pmd_mkwrite(pmd
);
705 static inline pmd_t
mk_huge_pmd(struct page
*page
, pgprot_t prot
)
708 entry
= mk_pmd(page
, prot
);
709 entry
= pmd_mkhuge(entry
);
713 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
714 struct vm_area_struct
*vma
,
715 unsigned long address
, pmd_t
*pmd
,
716 struct page
*page
, gfp_t gfp
,
719 struct mem_cgroup
*memcg
;
722 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
724 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
726 if (mem_cgroup_try_charge(page
, mm
, gfp
, &memcg
)) {
728 count_vm_event(THP_FAULT_FALLBACK
);
729 return VM_FAULT_FALLBACK
;
732 pgtable
= pte_alloc_one(mm
, haddr
);
733 if (unlikely(!pgtable
)) {
734 mem_cgroup_cancel_charge(page
, memcg
);
739 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
741 * The memory barrier inside __SetPageUptodate makes sure that
742 * clear_huge_page writes become visible before the set_pmd_at()
745 __SetPageUptodate(page
);
747 ptl
= pmd_lock(mm
, pmd
);
748 if (unlikely(!pmd_none(*pmd
))) {
750 mem_cgroup_cancel_charge(page
, memcg
);
752 pte_free(mm
, pgtable
);
756 /* Deliver the page fault to userland */
757 if (userfaultfd_missing(vma
)) {
761 mem_cgroup_cancel_charge(page
, memcg
);
763 pte_free(mm
, pgtable
);
764 ret
= handle_userfault(vma
, address
, flags
,
766 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
770 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
771 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
772 page_add_new_anon_rmap(page
, vma
, haddr
);
773 mem_cgroup_commit_charge(page
, memcg
, false);
774 lru_cache_add_active_or_unevictable(page
, vma
);
775 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
776 set_pmd_at(mm
, haddr
, pmd
, entry
);
777 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
778 atomic_long_inc(&mm
->nr_ptes
);
780 count_vm_event(THP_FAULT_ALLOC
);
786 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
788 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_WAIT
)) | extra_gfp
;
791 /* Caller must hold page table lock. */
792 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
793 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
794 struct page
*zero_page
)
799 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
800 entry
= pmd_mkhuge(entry
);
801 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
802 set_pmd_at(mm
, haddr
, pmd
, entry
);
803 atomic_long_inc(&mm
->nr_ptes
);
807 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
808 unsigned long address
, pmd_t
*pmd
,
813 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
815 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
816 return VM_FAULT_FALLBACK
;
817 if (unlikely(anon_vma_prepare(vma
)))
819 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
821 if (!(flags
& FAULT_FLAG_WRITE
) && !mm_forbids_zeropage(mm
) &&
822 transparent_hugepage_use_zero_page()) {
825 struct page
*zero_page
;
828 pgtable
= pte_alloc_one(mm
, haddr
);
829 if (unlikely(!pgtable
))
831 zero_page
= get_huge_zero_page();
832 if (unlikely(!zero_page
)) {
833 pte_free(mm
, pgtable
);
834 count_vm_event(THP_FAULT_FALLBACK
);
835 return VM_FAULT_FALLBACK
;
837 ptl
= pmd_lock(mm
, pmd
);
840 if (pmd_none(*pmd
)) {
841 if (userfaultfd_missing(vma
)) {
843 ret
= handle_userfault(vma
, address
, flags
,
845 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
847 set_huge_zero_page(pgtable
, mm
, vma
,
856 pte_free(mm
, pgtable
);
857 put_huge_zero_page();
861 gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
862 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
863 if (unlikely(!page
)) {
864 count_vm_event(THP_FAULT_FALLBACK
);
865 return VM_FAULT_FALLBACK
;
867 return __do_huge_pmd_anonymous_page(mm
, vma
, address
, pmd
, page
, gfp
,
871 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
872 pmd_t
*pmd
, unsigned long pfn
, pgprot_t prot
, bool write
)
874 struct mm_struct
*mm
= vma
->vm_mm
;
878 ptl
= pmd_lock(mm
, pmd
);
879 if (pmd_none(*pmd
)) {
880 entry
= pmd_mkhuge(pfn_pmd(pfn
, prot
));
882 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
883 entry
= maybe_pmd_mkwrite(entry
, vma
);
885 set_pmd_at(mm
, addr
, pmd
, entry
);
886 update_mmu_cache_pmd(vma
, addr
, pmd
);
891 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
892 pmd_t
*pmd
, unsigned long pfn
, bool write
)
894 pgprot_t pgprot
= vma
->vm_page_prot
;
896 * If we had pmd_special, we could avoid all these restrictions,
897 * but we need to be consistent with PTEs and architectures that
898 * can't support a 'special' bit.
900 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
901 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
902 (VM_PFNMAP
|VM_MIXEDMAP
));
903 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
904 BUG_ON((vma
->vm_flags
& VM_MIXEDMAP
) && pfn_valid(pfn
));
906 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
907 return VM_FAULT_SIGBUS
;
908 if (track_pfn_insert(vma
, &pgprot
, pfn
))
909 return VM_FAULT_SIGBUS
;
910 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
911 return VM_FAULT_NOPAGE
;
914 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
915 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
916 struct vm_area_struct
*vma
)
918 spinlock_t
*dst_ptl
, *src_ptl
;
919 struct page
*src_page
;
925 pgtable
= pte_alloc_one(dst_mm
, addr
);
926 if (unlikely(!pgtable
))
929 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
930 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
931 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
935 if (unlikely(!pmd_trans_huge(pmd
))) {
936 pte_free(dst_mm
, pgtable
);
940 * When page table lock is held, the huge zero pmd should not be
941 * under splitting since we don't split the page itself, only pmd to
944 if (is_huge_zero_pmd(pmd
)) {
945 struct page
*zero_page
;
947 * get_huge_zero_page() will never allocate a new page here,
948 * since we already have a zero page to copy. It just takes a
951 zero_page
= get_huge_zero_page();
952 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
958 if (unlikely(pmd_trans_splitting(pmd
))) {
959 /* split huge page running from under us */
960 spin_unlock(src_ptl
);
961 spin_unlock(dst_ptl
);
962 pte_free(dst_mm
, pgtable
);
964 wait_split_huge_page(vma
->anon_vma
, src_pmd
); /* src_vma */
967 src_page
= pmd_page(pmd
);
968 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
970 page_dup_rmap(src_page
);
971 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
973 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
974 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
975 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
976 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
977 atomic_long_inc(&dst_mm
->nr_ptes
);
981 spin_unlock(src_ptl
);
982 spin_unlock(dst_ptl
);
987 void huge_pmd_set_accessed(struct mm_struct
*mm
,
988 struct vm_area_struct
*vma
,
989 unsigned long address
,
990 pmd_t
*pmd
, pmd_t orig_pmd
,
997 ptl
= pmd_lock(mm
, pmd
);
998 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1001 entry
= pmd_mkyoung(orig_pmd
);
1002 haddr
= address
& HPAGE_PMD_MASK
;
1003 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
1004 update_mmu_cache_pmd(vma
, address
, pmd
);
1011 * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
1012 * during copy_user_huge_page()'s copy_page_rep(): in the case when
1013 * the source page gets split and a tail freed before copy completes.
1014 * Called under pmd_lock of checked pmd, so safe from splitting itself.
1016 static void get_user_huge_page(struct page
*page
)
1018 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC
)) {
1019 struct page
*endpage
= page
+ HPAGE_PMD_NR
;
1021 atomic_add(HPAGE_PMD_NR
, &page
->_count
);
1022 while (++page
< endpage
)
1023 get_huge_page_tail(page
);
1029 static void put_user_huge_page(struct page
*page
)
1031 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC
)) {
1032 struct page
*endpage
= page
+ HPAGE_PMD_NR
;
1034 while (page
< endpage
)
1041 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
1042 struct vm_area_struct
*vma
,
1043 unsigned long address
,
1044 pmd_t
*pmd
, pmd_t orig_pmd
,
1046 unsigned long haddr
)
1048 struct mem_cgroup
*memcg
;
1053 struct page
**pages
;
1054 unsigned long mmun_start
; /* For mmu_notifiers */
1055 unsigned long mmun_end
; /* For mmu_notifiers */
1057 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1059 if (unlikely(!pages
)) {
1060 ret
|= VM_FAULT_OOM
;
1064 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1065 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1067 vma
, address
, page_to_nid(page
));
1068 if (unlikely(!pages
[i
] ||
1069 mem_cgroup_try_charge(pages
[i
], mm
, GFP_KERNEL
,
1074 memcg
= (void *)page_private(pages
[i
]);
1075 set_page_private(pages
[i
], 0);
1076 mem_cgroup_cancel_charge(pages
[i
], memcg
);
1080 ret
|= VM_FAULT_OOM
;
1083 set_page_private(pages
[i
], (unsigned long)memcg
);
1086 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1087 copy_user_highpage(pages
[i
], page
+ i
,
1088 haddr
+ PAGE_SIZE
* i
, vma
);
1089 __SetPageUptodate(pages
[i
]);
1094 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1095 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1097 ptl
= pmd_lock(mm
, pmd
);
1098 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1099 goto out_free_pages
;
1100 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1102 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1103 /* leave pmd empty until pte is filled */
1105 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1106 pmd_populate(mm
, &_pmd
, pgtable
);
1108 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1110 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1111 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1112 memcg
= (void *)page_private(pages
[i
]);
1113 set_page_private(pages
[i
], 0);
1114 page_add_new_anon_rmap(pages
[i
], vma
, haddr
);
1115 mem_cgroup_commit_charge(pages
[i
], memcg
, false);
1116 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
1117 pte
= pte_offset_map(&_pmd
, haddr
);
1118 VM_BUG_ON(!pte_none(*pte
));
1119 set_pte_at(mm
, haddr
, pte
, entry
);
1124 smp_wmb(); /* make pte visible before pmd */
1125 pmd_populate(mm
, pmd
, pgtable
);
1126 page_remove_rmap(page
);
1129 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1131 ret
|= VM_FAULT_WRITE
;
1139 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1140 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1141 memcg
= (void *)page_private(pages
[i
]);
1142 set_page_private(pages
[i
], 0);
1143 mem_cgroup_cancel_charge(pages
[i
], memcg
);
1150 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1151 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
1155 struct page
*page
= NULL
, *new_page
;
1156 struct mem_cgroup
*memcg
;
1157 unsigned long haddr
;
1158 unsigned long mmun_start
; /* For mmu_notifiers */
1159 unsigned long mmun_end
; /* For mmu_notifiers */
1160 gfp_t huge_gfp
; /* for allocation and charge */
1162 ptl
= pmd_lockptr(mm
, pmd
);
1163 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1164 haddr
= address
& HPAGE_PMD_MASK
;
1165 if (is_huge_zero_pmd(orig_pmd
))
1168 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1171 page
= pmd_page(orig_pmd
);
1172 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1173 if (page_mapcount(page
) == 1) {
1175 entry
= pmd_mkyoung(orig_pmd
);
1176 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1177 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
1178 update_mmu_cache_pmd(vma
, address
, pmd
);
1179 ret
|= VM_FAULT_WRITE
;
1182 get_user_huge_page(page
);
1185 if (transparent_hugepage_enabled(vma
) &&
1186 !transparent_hugepage_debug_cow()) {
1187 huge_gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
1188 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1192 if (unlikely(!new_page
)) {
1194 split_huge_page_pmd(vma
, address
, pmd
);
1195 ret
|= VM_FAULT_FALLBACK
;
1197 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
1198 pmd
, orig_pmd
, page
, haddr
);
1199 if (ret
& VM_FAULT_OOM
) {
1200 split_huge_page(page
);
1201 ret
|= VM_FAULT_FALLBACK
;
1203 put_user_huge_page(page
);
1205 count_vm_event(THP_FAULT_FALLBACK
);
1209 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, huge_gfp
, &memcg
))) {
1212 split_huge_page(page
);
1213 put_user_huge_page(page
);
1215 split_huge_page_pmd(vma
, address
, pmd
);
1216 ret
|= VM_FAULT_FALLBACK
;
1217 count_vm_event(THP_FAULT_FALLBACK
);
1221 count_vm_event(THP_FAULT_ALLOC
);
1224 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1226 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1227 __SetPageUptodate(new_page
);
1230 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1231 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1235 put_user_huge_page(page
);
1236 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
1238 mem_cgroup_cancel_charge(new_page
, memcg
);
1243 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1244 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1245 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1246 page_add_new_anon_rmap(new_page
, vma
, haddr
);
1247 mem_cgroup_commit_charge(new_page
, memcg
, false);
1248 lru_cache_add_active_or_unevictable(new_page
, vma
);
1249 set_pmd_at(mm
, haddr
, pmd
, entry
);
1250 update_mmu_cache_pmd(vma
, address
, pmd
);
1252 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1253 put_huge_zero_page();
1255 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1256 page_remove_rmap(page
);
1259 ret
|= VM_FAULT_WRITE
;
1263 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1271 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1276 struct mm_struct
*mm
= vma
->vm_mm
;
1277 struct page
*page
= NULL
;
1279 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1281 if (flags
& FOLL_WRITE
&& !pmd_write(*pmd
))
1284 /* Avoid dumping huge zero page */
1285 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1286 return ERR_PTR(-EFAULT
);
1288 /* Full NUMA hinting faults to serialise migration in fault paths */
1289 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1292 page
= pmd_page(*pmd
);
1293 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1294 if (flags
& FOLL_TOUCH
) {
1297 * We should set the dirty bit only for FOLL_WRITE but
1298 * for now the dirty bit in the pmd is meaningless.
1299 * And if the dirty bit will become meaningful and
1300 * we'll only set it with FOLL_WRITE, an atomic
1301 * set_bit will be required on the pmd to set the
1302 * young bit, instead of the current set_pmd_at.
1304 _pmd
= pmd_mkyoung(pmd_mkdirty(*pmd
));
1305 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
1307 update_mmu_cache_pmd(vma
, addr
, pmd
);
1309 if ((flags
& FOLL_POPULATE
) && (vma
->vm_flags
& VM_LOCKED
)) {
1310 if (page
->mapping
&& trylock_page(page
)) {
1313 mlock_vma_page(page
);
1317 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1318 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
1319 if (flags
& FOLL_GET
)
1320 get_page_foll(page
);
1326 /* NUMA hinting page fault entry point for trans huge pmds */
1327 int do_huge_pmd_numa_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1328 unsigned long addr
, pmd_t pmd
, pmd_t
*pmdp
)
1331 struct anon_vma
*anon_vma
= NULL
;
1333 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1334 int page_nid
= -1, this_nid
= numa_node_id();
1335 int target_nid
, last_cpupid
= -1;
1337 bool migrated
= false;
1341 /* A PROT_NONE fault should not end up here */
1342 BUG_ON(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)));
1344 ptl
= pmd_lock(mm
, pmdp
);
1345 if (unlikely(!pmd_same(pmd
, *pmdp
)))
1349 * If there are potential migrations, wait for completion and retry
1350 * without disrupting NUMA hinting information. Do not relock and
1351 * check_same as the page may no longer be mapped.
1353 if (unlikely(pmd_trans_migrating(*pmdp
))) {
1354 page
= pmd_page(*pmdp
);
1356 wait_on_page_locked(page
);
1360 page
= pmd_page(pmd
);
1361 BUG_ON(is_huge_zero_page(page
));
1362 page_nid
= page_to_nid(page
);
1363 last_cpupid
= page_cpupid_last(page
);
1364 count_vm_numa_event(NUMA_HINT_FAULTS
);
1365 if (page_nid
== this_nid
) {
1366 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1367 flags
|= TNF_FAULT_LOCAL
;
1370 /* See similar comment in do_numa_page for explanation */
1371 if (!(vma
->vm_flags
& VM_WRITE
))
1372 flags
|= TNF_NO_GROUP
;
1375 * Acquire the page lock to serialise THP migrations but avoid dropping
1376 * page_table_lock if at all possible
1378 page_locked
= trylock_page(page
);
1379 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1380 if (target_nid
== -1) {
1381 /* If the page was locked, there are no parallel migrations */
1386 /* Migration could have started since the pmd_trans_migrating check */
1389 wait_on_page_locked(page
);
1395 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1396 * to serialises splits
1400 anon_vma
= page_lock_anon_vma_read(page
);
1402 /* Confirm the PMD did not change while page_table_lock was released */
1404 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1411 /* Bail if we fail to protect against THP splits for any reason */
1412 if (unlikely(!anon_vma
)) {
1419 * Migrate the THP to the requested node, returns with page unlocked
1420 * and access rights restored.
1423 migrated
= migrate_misplaced_transhuge_page(mm
, vma
,
1424 pmdp
, pmd
, addr
, page
, target_nid
);
1426 flags
|= TNF_MIGRATED
;
1427 page_nid
= target_nid
;
1429 flags
|= TNF_MIGRATE_FAIL
;
1433 BUG_ON(!PageLocked(page
));
1434 was_writable
= pmd_write(pmd
);
1435 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1436 pmd
= pmd_mkyoung(pmd
);
1438 pmd
= pmd_mkwrite(pmd
);
1439 set_pmd_at(mm
, haddr
, pmdp
, pmd
);
1440 update_mmu_cache_pmd(vma
, addr
, pmdp
);
1447 page_unlock_anon_vma_read(anon_vma
);
1450 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, flags
);
1455 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1456 pmd_t
*pmd
, unsigned long addr
)
1461 if (__pmd_trans_huge_lock(pmd
, vma
, &ptl
) != 1)
1464 * For architectures like ppc64 we look at deposited pgtable
1465 * when calling pmdp_huge_get_and_clear. So do the
1466 * pgtable_trans_huge_withdraw after finishing pmdp related
1469 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1471 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1472 if (vma_is_dax(vma
)) {
1474 if (is_huge_zero_pmd(orig_pmd
))
1475 put_huge_zero_page();
1476 } else if (is_huge_zero_pmd(orig_pmd
)) {
1477 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1478 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1480 put_huge_zero_page();
1482 struct page
*page
= pmd_page(orig_pmd
);
1483 page_remove_rmap(page
);
1484 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1485 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1486 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1487 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1488 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1490 tlb_remove_page(tlb
, page
);
1495 int move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1496 unsigned long old_addr
,
1497 unsigned long new_addr
, unsigned long old_end
,
1498 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1500 spinlock_t
*old_ptl
, *new_ptl
;
1504 struct mm_struct
*mm
= vma
->vm_mm
;
1506 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1507 (new_addr
& ~HPAGE_PMD_MASK
) ||
1508 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1509 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1513 * The destination pmd shouldn't be established, free_pgtables()
1514 * should have release it.
1516 if (WARN_ON(!pmd_none(*new_pmd
))) {
1517 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1522 * We don't have to worry about the ordering of src and dst
1523 * ptlocks because exclusive mmap_sem prevents deadlock.
1525 ret
= __pmd_trans_huge_lock(old_pmd
, vma
, &old_ptl
);
1527 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1528 if (new_ptl
!= old_ptl
)
1529 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1530 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1531 VM_BUG_ON(!pmd_none(*new_pmd
));
1533 if (pmd_move_must_withdraw(new_ptl
, old_ptl
)) {
1535 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1536 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1538 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1539 if (new_ptl
!= old_ptl
)
1540 spin_unlock(new_ptl
);
1541 spin_unlock(old_ptl
);
1549 * - 0 if PMD could not be locked
1550 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1551 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1553 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1554 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1556 struct mm_struct
*mm
= vma
->vm_mm
;
1560 if (__pmd_trans_huge_lock(pmd
, vma
, &ptl
) == 1) {
1562 bool preserve_write
= prot_numa
&& pmd_write(*pmd
);
1566 * Avoid trapping faults against the zero page. The read-only
1567 * data is likely to be read-cached on the local CPU and
1568 * local/remote hits to the zero page are not interesting.
1570 if (prot_numa
&& is_huge_zero_pmd(*pmd
)) {
1575 if (!prot_numa
|| !pmd_protnone(*pmd
)) {
1576 entry
= pmdp_huge_get_and_clear_notify(mm
, addr
, pmd
);
1577 entry
= pmd_modify(entry
, newprot
);
1579 entry
= pmd_mkwrite(entry
);
1581 set_pmd_at(mm
, addr
, pmd
, entry
);
1582 BUG_ON(!preserve_write
&& pmd_write(entry
));
1591 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1592 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1594 * Note that if it returns 1, this routine returns without unlocking page
1595 * table locks. So callers must unlock them.
1597 int __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
,
1600 *ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1601 if (likely(pmd_trans_huge(*pmd
))) {
1602 if (unlikely(pmd_trans_splitting(*pmd
))) {
1604 wait_split_huge_page(vma
->anon_vma
, pmd
);
1607 /* Thp mapped by 'pmd' is stable, so we can
1608 * handle it as it is. */
1617 * This function returns whether a given @page is mapped onto the @address
1618 * in the virtual space of @mm.
1620 * When it's true, this function returns *pmd with holding the page table lock
1621 * and passing it back to the caller via @ptl.
1622 * If it's false, returns NULL without holding the page table lock.
1624 pmd_t
*page_check_address_pmd(struct page
*page
,
1625 struct mm_struct
*mm
,
1626 unsigned long address
,
1627 enum page_check_address_pmd_flag flag
,
1634 if (address
& ~HPAGE_PMD_MASK
)
1637 pgd
= pgd_offset(mm
, address
);
1638 if (!pgd_present(*pgd
))
1640 pud
= pud_offset(pgd
, address
);
1641 if (!pud_present(*pud
))
1643 pmd
= pmd_offset(pud
, address
);
1645 *ptl
= pmd_lock(mm
, pmd
);
1646 if (!pmd_present(*pmd
))
1648 if (pmd_page(*pmd
) != page
)
1651 * split_vma() may create temporary aliased mappings. There is
1652 * no risk as long as all huge pmd are found and have their
1653 * splitting bit set before __split_huge_page_refcount
1654 * runs. Finding the same huge pmd more than once during the
1655 * same rmap walk is not a problem.
1657 if (flag
== PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
&&
1658 pmd_trans_splitting(*pmd
))
1660 if (pmd_trans_huge(*pmd
)) {
1661 VM_BUG_ON(flag
== PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
&&
1662 !pmd_trans_splitting(*pmd
));
1670 static int __split_huge_page_splitting(struct page
*page
,
1671 struct vm_area_struct
*vma
,
1672 unsigned long address
)
1674 struct mm_struct
*mm
= vma
->vm_mm
;
1678 /* For mmu_notifiers */
1679 const unsigned long mmun_start
= address
;
1680 const unsigned long mmun_end
= address
+ HPAGE_PMD_SIZE
;
1682 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1683 pmd
= page_check_address_pmd(page
, mm
, address
,
1684 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
, &ptl
);
1687 * We can't temporarily set the pmd to null in order
1688 * to split it, the pmd must remain marked huge at all
1689 * times or the VM won't take the pmd_trans_huge paths
1690 * and it won't wait on the anon_vma->root->rwsem to
1691 * serialize against split_huge_page*.
1693 pmdp_splitting_flush(vma
, address
, pmd
);
1698 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1703 static void __split_huge_page_refcount(struct page
*page
,
1704 struct list_head
*list
)
1707 struct zone
*zone
= page_zone(page
);
1708 struct lruvec
*lruvec
;
1711 /* prevent PageLRU to go away from under us, and freeze lru stats */
1712 spin_lock_irq(&zone
->lru_lock
);
1713 lruvec
= mem_cgroup_page_lruvec(page
, zone
);
1715 compound_lock(page
);
1716 /* complete memcg works before add pages to LRU */
1717 mem_cgroup_split_huge_fixup(page
);
1719 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1720 struct page
*page_tail
= page
+ i
;
1722 /* tail_page->_mapcount cannot change */
1723 BUG_ON(page_mapcount(page_tail
) < 0);
1724 tail_count
+= page_mapcount(page_tail
);
1725 /* check for overflow */
1726 BUG_ON(tail_count
< 0);
1727 BUG_ON(atomic_read(&page_tail
->_count
) != 0);
1729 * tail_page->_count is zero and not changing from
1730 * under us. But get_page_unless_zero() may be running
1731 * from under us on the tail_page. If we used
1732 * atomic_set() below instead of atomic_add(), we
1733 * would then run atomic_set() concurrently with
1734 * get_page_unless_zero(), and atomic_set() is
1735 * implemented in C not using locked ops. spin_unlock
1736 * on x86 sometime uses locked ops because of PPro
1737 * errata 66, 92, so unless somebody can guarantee
1738 * atomic_set() here would be safe on all archs (and
1739 * not only on x86), it's safer to use atomic_add().
1741 atomic_add(page_mapcount(page
) + page_mapcount(page_tail
) + 1,
1742 &page_tail
->_count
);
1744 /* after clearing PageTail the gup refcount can be released */
1745 smp_mb__after_atomic();
1747 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
1748 page_tail
->flags
|= (page
->flags
&
1749 ((1L << PG_referenced
) |
1750 (1L << PG_swapbacked
) |
1751 (1L << PG_mlocked
) |
1752 (1L << PG_uptodate
) |
1754 (1L << PG_unevictable
)));
1755 page_tail
->flags
|= (1L << PG_dirty
);
1757 /* clear PageTail before overwriting first_page */
1761 * __split_huge_page_splitting() already set the
1762 * splitting bit in all pmd that could map this
1763 * hugepage, that will ensure no CPU can alter the
1764 * mapcount on the head page. The mapcount is only
1765 * accounted in the head page and it has to be
1766 * transferred to all tail pages in the below code. So
1767 * for this code to be safe, the split the mapcount
1768 * can't change. But that doesn't mean userland can't
1769 * keep changing and reading the page contents while
1770 * we transfer the mapcount, so the pmd splitting
1771 * status is achieved setting a reserved bit in the
1772 * pmd, not by clearing the present bit.
1774 page_tail
->_mapcount
= page
->_mapcount
;
1776 BUG_ON(page_tail
->mapping
);
1777 page_tail
->mapping
= page
->mapping
;
1779 page_tail
->index
= page
->index
+ i
;
1780 page_cpupid_xchg_last(page_tail
, page_cpupid_last(page
));
1782 BUG_ON(!PageAnon(page_tail
));
1783 BUG_ON(!PageUptodate(page_tail
));
1784 BUG_ON(!PageDirty(page_tail
));
1785 BUG_ON(!PageSwapBacked(page_tail
));
1787 lru_add_page_tail(page
, page_tail
, lruvec
, list
);
1789 atomic_sub(tail_count
, &page
->_count
);
1790 BUG_ON(atomic_read(&page
->_count
) <= 0);
1792 __mod_zone_page_state(zone
, NR_ANON_TRANSPARENT_HUGEPAGES
, -1);
1794 ClearPageCompound(page
);
1795 compound_unlock(page
);
1796 spin_unlock_irq(&zone
->lru_lock
);
1798 for (i
= 1; i
< HPAGE_PMD_NR
; i
++) {
1799 struct page
*page_tail
= page
+ i
;
1800 BUG_ON(page_count(page_tail
) <= 0);
1802 * Tail pages may be freed if there wasn't any mapping
1803 * like if add_to_swap() is running on a lru page that
1804 * had its mapping zapped. And freeing these pages
1805 * requires taking the lru_lock so we do the put_page
1806 * of the tail pages after the split is complete.
1808 put_page(page_tail
);
1812 * Only the head page (now become a regular page) is required
1813 * to be pinned by the caller.
1815 BUG_ON(page_count(page
) <= 0);
1818 static int __split_huge_page_map(struct page
*page
,
1819 struct vm_area_struct
*vma
,
1820 unsigned long address
)
1822 struct mm_struct
*mm
= vma
->vm_mm
;
1827 unsigned long haddr
;
1829 pmd
= page_check_address_pmd(page
, mm
, address
,
1830 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
, &ptl
);
1832 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1833 pmd_populate(mm
, &_pmd
, pgtable
);
1834 if (pmd_write(*pmd
))
1835 BUG_ON(page_mapcount(page
) != 1);
1838 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1840 BUG_ON(PageCompound(page
+i
));
1842 * Note that NUMA hinting access restrictions are not
1843 * transferred to avoid any possibility of altering
1844 * permissions across VMAs.
1846 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
1847 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1848 if (!pmd_write(*pmd
))
1849 entry
= pte_wrprotect(entry
);
1850 if (!pmd_young(*pmd
))
1851 entry
= pte_mkold(entry
);
1852 pte
= pte_offset_map(&_pmd
, haddr
);
1853 BUG_ON(!pte_none(*pte
));
1854 set_pte_at(mm
, haddr
, pte
, entry
);
1858 smp_wmb(); /* make pte visible before pmd */
1860 * Up to this point the pmd is present and huge and
1861 * userland has the whole access to the hugepage
1862 * during the split (which happens in place). If we
1863 * overwrite the pmd with the not-huge version
1864 * pointing to the pte here (which of course we could
1865 * if all CPUs were bug free), userland could trigger
1866 * a small page size TLB miss on the small sized TLB
1867 * while the hugepage TLB entry is still established
1868 * in the huge TLB. Some CPU doesn't like that. See
1869 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1870 * Erratum 383 on page 93. Intel should be safe but is
1871 * also warns that it's only safe if the permission
1872 * and cache attributes of the two entries loaded in
1873 * the two TLB is identical (which should be the case
1874 * here). But it is generally safer to never allow
1875 * small and huge TLB entries for the same virtual
1876 * address to be loaded simultaneously. So instead of
1877 * doing "pmd_populate(); flush_tlb_range();" we first
1878 * mark the current pmd notpresent (atomically because
1879 * here the pmd_trans_huge and pmd_trans_splitting
1880 * must remain set at all times on the pmd until the
1881 * split is complete for this pmd), then we flush the
1882 * SMP TLB and finally we write the non-huge version
1883 * of the pmd entry with pmd_populate.
1885 pmdp_invalidate(vma
, address
, pmd
);
1886 pmd_populate(mm
, pmd
, pgtable
);
1894 /* must be called with anon_vma->root->rwsem held */
1895 static void __split_huge_page(struct page
*page
,
1896 struct anon_vma
*anon_vma
,
1897 struct list_head
*list
)
1899 int mapcount
, mapcount2
;
1900 pgoff_t pgoff
= page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
1901 struct anon_vma_chain
*avc
;
1903 BUG_ON(!PageHead(page
));
1904 BUG_ON(PageTail(page
));
1907 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1908 struct vm_area_struct
*vma
= avc
->vma
;
1909 unsigned long addr
= vma_address(page
, vma
);
1910 BUG_ON(is_vma_temporary_stack(vma
));
1911 mapcount
+= __split_huge_page_splitting(page
, vma
, addr
);
1914 * It is critical that new vmas are added to the tail of the
1915 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1916 * and establishes a child pmd before
1917 * __split_huge_page_splitting() freezes the parent pmd (so if
1918 * we fail to prevent copy_huge_pmd() from running until the
1919 * whole __split_huge_page() is complete), we will still see
1920 * the newly established pmd of the child later during the
1921 * walk, to be able to set it as pmd_trans_splitting too.
1923 if (mapcount
!= page_mapcount(page
)) {
1924 pr_err("mapcount %d page_mapcount %d\n",
1925 mapcount
, page_mapcount(page
));
1929 __split_huge_page_refcount(page
, list
);
1932 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1933 struct vm_area_struct
*vma
= avc
->vma
;
1934 unsigned long addr
= vma_address(page
, vma
);
1935 BUG_ON(is_vma_temporary_stack(vma
));
1936 mapcount2
+= __split_huge_page_map(page
, vma
, addr
);
1938 if (mapcount
!= mapcount2
) {
1939 pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
1940 mapcount
, mapcount2
, page_mapcount(page
));
1946 * Split a hugepage into normal pages. This doesn't change the position of head
1947 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1948 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1949 * from the hugepage.
1950 * Return 0 if the hugepage is split successfully otherwise return 1.
1952 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
1954 struct anon_vma
*anon_vma
;
1957 BUG_ON(is_huge_zero_page(page
));
1958 BUG_ON(!PageAnon(page
));
1961 * The caller does not necessarily hold an mmap_sem that would prevent
1962 * the anon_vma disappearing so we first we take a reference to it
1963 * and then lock the anon_vma for write. This is similar to
1964 * page_lock_anon_vma_read except the write lock is taken to serialise
1965 * against parallel split or collapse operations.
1967 anon_vma
= page_get_anon_vma(page
);
1970 anon_vma_lock_write(anon_vma
);
1973 if (!PageCompound(page
))
1976 BUG_ON(!PageSwapBacked(page
));
1977 __split_huge_page(page
, anon_vma
, list
);
1978 count_vm_event(THP_SPLIT
);
1980 BUG_ON(PageCompound(page
));
1982 anon_vma_unlock_write(anon_vma
);
1983 put_anon_vma(anon_vma
);
1988 #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
1990 int hugepage_madvise(struct vm_area_struct
*vma
,
1991 unsigned long *vm_flags
, int advice
)
1997 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1998 * can't handle this properly after s390_enable_sie, so we simply
1999 * ignore the madvise to prevent qemu from causing a SIGSEGV.
2001 if (mm_has_pgste(vma
->vm_mm
))
2005 * Be somewhat over-protective like KSM for now!
2007 if (*vm_flags
& (VM_HUGEPAGE
| VM_NO_THP
))
2009 *vm_flags
&= ~VM_NOHUGEPAGE
;
2010 *vm_flags
|= VM_HUGEPAGE
;
2012 * If the vma become good for khugepaged to scan,
2013 * register it here without waiting a page fault that
2014 * may not happen any time soon.
2016 if (unlikely(khugepaged_enter_vma_merge(vma
, *vm_flags
)))
2019 case MADV_NOHUGEPAGE
:
2021 * Be somewhat over-protective like KSM for now!
2023 if (*vm_flags
& (VM_NOHUGEPAGE
| VM_NO_THP
))
2025 *vm_flags
&= ~VM_HUGEPAGE
;
2026 *vm_flags
|= VM_NOHUGEPAGE
;
2028 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
2029 * this vma even if we leave the mm registered in khugepaged if
2030 * it got registered before VM_NOHUGEPAGE was set.
2038 static int __init
khugepaged_slab_init(void)
2040 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
2041 sizeof(struct mm_slot
),
2042 __alignof__(struct mm_slot
), 0, NULL
);
2049 static void __init
khugepaged_slab_exit(void)
2051 kmem_cache_destroy(mm_slot_cache
);
2054 static inline struct mm_slot
*alloc_mm_slot(void)
2056 if (!mm_slot_cache
) /* initialization failed */
2058 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
2061 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
2063 kmem_cache_free(mm_slot_cache
, mm_slot
);
2066 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
2068 struct mm_slot
*mm_slot
;
2070 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
2071 if (mm
== mm_slot
->mm
)
2077 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
2078 struct mm_slot
*mm_slot
)
2081 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
2084 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
2086 return atomic_read(&mm
->mm_users
) == 0;
2089 int __khugepaged_enter(struct mm_struct
*mm
)
2091 struct mm_slot
*mm_slot
;
2094 mm_slot
= alloc_mm_slot();
2098 /* __khugepaged_exit() must not run from under us */
2099 VM_BUG_ON_MM(khugepaged_test_exit(mm
), mm
);
2100 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
2101 free_mm_slot(mm_slot
);
2105 spin_lock(&khugepaged_mm_lock
);
2106 insert_to_mm_slots_hash(mm
, mm_slot
);
2108 * Insert just behind the scanning cursor, to let the area settle
2111 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
2112 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
2113 spin_unlock(&khugepaged_mm_lock
);
2115 atomic_inc(&mm
->mm_count
);
2117 wake_up_interruptible(&khugepaged_wait
);
2122 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
2123 unsigned long vm_flags
)
2125 unsigned long hstart
, hend
;
2128 * Not yet faulted in so we will register later in the
2129 * page fault if needed.
2133 /* khugepaged not yet working on file or special mappings */
2135 VM_BUG_ON_VMA(vm_flags
& VM_NO_THP
, vma
);
2136 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2137 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2139 return khugepaged_enter(vma
, vm_flags
);
2143 void __khugepaged_exit(struct mm_struct
*mm
)
2145 struct mm_slot
*mm_slot
;
2148 spin_lock(&khugepaged_mm_lock
);
2149 mm_slot
= get_mm_slot(mm
);
2150 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
2151 hash_del(&mm_slot
->hash
);
2152 list_del(&mm_slot
->mm_node
);
2155 spin_unlock(&khugepaged_mm_lock
);
2158 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
2159 free_mm_slot(mm_slot
);
2161 } else if (mm_slot
) {
2163 * This is required to serialize against
2164 * khugepaged_test_exit() (which is guaranteed to run
2165 * under mmap sem read mode). Stop here (after we
2166 * return all pagetables will be destroyed) until
2167 * khugepaged has finished working on the pagetables
2168 * under the mmap_sem.
2170 down_write(&mm
->mmap_sem
);
2171 up_write(&mm
->mmap_sem
);
2175 static void release_pte_page(struct page
*page
)
2177 /* 0 stands for page_is_file_cache(page) == false */
2178 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2180 putback_lru_page(page
);
2183 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
2185 while (--_pte
>= pte
) {
2186 pte_t pteval
= *_pte
;
2187 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
2188 release_pte_page(pte_page(pteval
));
2192 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
2193 unsigned long address
,
2198 int none_or_zero
= 0;
2199 bool referenced
= false, writable
= false;
2200 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2201 _pte
++, address
+= PAGE_SIZE
) {
2202 pte_t pteval
= *_pte
;
2203 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2204 if (!userfaultfd_armed(vma
) &&
2205 ++none_or_zero
<= khugepaged_max_ptes_none
)
2210 if (!pte_present(pteval
))
2212 page
= vm_normal_page(vma
, address
, pteval
);
2213 if (unlikely(!page
))
2216 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2217 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
2218 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
2221 * We can do it before isolate_lru_page because the
2222 * page can't be freed from under us. NOTE: PG_lock
2223 * is needed to serialize against split_huge_page
2224 * when invoked from the VM.
2226 if (!trylock_page(page
))
2230 * cannot use mapcount: can't collapse if there's a gup pin.
2231 * The page must only be referenced by the scanned process
2232 * and page swap cache.
2234 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2238 if (pte_write(pteval
)) {
2241 if (PageSwapCache(page
) && !reuse_swap_page(page
)) {
2246 * Page is not in the swap cache. It can be collapsed
2252 * Isolate the page to avoid collapsing an hugepage
2253 * currently in use by the VM.
2255 if (isolate_lru_page(page
)) {
2259 /* 0 stands for page_is_file_cache(page) == false */
2260 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2261 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2262 VM_BUG_ON_PAGE(PageLRU(page
), page
);
2264 /* If there is no mapped pte young don't collapse the page */
2265 if (pte_young(pteval
) || PageReferenced(page
) ||
2266 mmu_notifier_test_young(vma
->vm_mm
, address
))
2269 if (likely(referenced
&& writable
))
2272 release_pte_pages(pte
, _pte
);
2276 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2277 struct vm_area_struct
*vma
,
2278 unsigned long address
,
2282 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2283 pte_t pteval
= *_pte
;
2284 struct page
*src_page
;
2286 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2287 clear_user_highpage(page
, address
);
2288 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2289 if (is_zero_pfn(pte_pfn(pteval
))) {
2291 * ptl mostly unnecessary.
2295 * paravirt calls inside pte_clear here are
2298 pte_clear(vma
->vm_mm
, address
, _pte
);
2302 src_page
= pte_page(pteval
);
2303 copy_user_highpage(page
, src_page
, address
, vma
);
2304 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
2305 release_pte_page(src_page
);
2307 * ptl mostly unnecessary, but preempt has to
2308 * be disabled to update the per-cpu stats
2309 * inside page_remove_rmap().
2313 * paravirt calls inside pte_clear here are
2316 pte_clear(vma
->vm_mm
, address
, _pte
);
2317 page_remove_rmap(src_page
);
2319 free_page_and_swap_cache(src_page
);
2322 address
+= PAGE_SIZE
;
2327 static void khugepaged_alloc_sleep(void)
2329 wait_event_freezable_timeout(khugepaged_wait
, false,
2330 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2333 static int khugepaged_node_load
[MAX_NUMNODES
];
2335 static bool khugepaged_scan_abort(int nid
)
2340 * If zone_reclaim_mode is disabled, then no extra effort is made to
2341 * allocate memory locally.
2343 if (!zone_reclaim_mode
)
2346 /* If there is a count for this node already, it must be acceptable */
2347 if (khugepaged_node_load
[nid
])
2350 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
2351 if (!khugepaged_node_load
[i
])
2353 if (node_distance(nid
, i
) > RECLAIM_DISTANCE
)
2360 static int khugepaged_find_target_node(void)
2362 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
2363 int nid
, target_node
= 0, max_value
= 0;
2365 /* find first node with max normal pages hit */
2366 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
2367 if (khugepaged_node_load
[nid
] > max_value
) {
2368 max_value
= khugepaged_node_load
[nid
];
2372 /* do some balance if several nodes have the same hit record */
2373 if (target_node
<= last_khugepaged_target_node
)
2374 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
2376 if (max_value
== khugepaged_node_load
[nid
]) {
2381 last_khugepaged_target_node
= target_node
;
2385 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2387 if (IS_ERR(*hpage
)) {
2393 khugepaged_alloc_sleep();
2394 } else if (*hpage
) {
2402 static struct page
*
2403 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2404 struct vm_area_struct
*vma
, unsigned long address
,
2407 VM_BUG_ON_PAGE(*hpage
, *hpage
);
2410 * Before allocating the hugepage, release the mmap_sem read lock.
2411 * The allocation can take potentially a long time if it involves
2412 * sync compaction, and we do not need to hold the mmap_sem during
2413 * that. We will recheck the vma after taking it again in write mode.
2415 up_read(&mm
->mmap_sem
);
2417 *hpage
= alloc_pages_exact_node(node
, gfp
, HPAGE_PMD_ORDER
);
2418 if (unlikely(!*hpage
)) {
2419 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2420 *hpage
= ERR_PTR(-ENOMEM
);
2424 count_vm_event(THP_COLLAPSE_ALLOC
);
2428 static int khugepaged_find_target_node(void)
2433 static inline struct page
*alloc_hugepage(int defrag
)
2435 return alloc_pages(alloc_hugepage_gfpmask(defrag
, 0),
2439 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2444 hpage
= alloc_hugepage(khugepaged_defrag());
2446 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2451 khugepaged_alloc_sleep();
2453 count_vm_event(THP_COLLAPSE_ALLOC
);
2454 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2459 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2462 *hpage
= khugepaged_alloc_hugepage(wait
);
2464 if (unlikely(!*hpage
))
2470 static struct page
*
2471 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2472 struct vm_area_struct
*vma
, unsigned long address
,
2475 up_read(&mm
->mmap_sem
);
2482 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2484 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2485 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2488 if (!vma
->anon_vma
|| vma
->vm_ops
)
2490 if (is_vma_temporary_stack(vma
))
2492 VM_BUG_ON_VMA(vma
->vm_flags
& VM_NO_THP
, vma
);
2496 static void collapse_huge_page(struct mm_struct
*mm
,
2497 unsigned long address
,
2498 struct page
**hpage
,
2499 struct vm_area_struct
*vma
,
2505 struct page
*new_page
;
2506 spinlock_t
*pmd_ptl
, *pte_ptl
;
2508 unsigned long hstart
, hend
;
2509 struct mem_cgroup
*memcg
;
2510 unsigned long mmun_start
; /* For mmu_notifiers */
2511 unsigned long mmun_end
; /* For mmu_notifiers */
2514 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2516 /* Only allocate from the target node */
2517 gfp
= alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE
) |
2520 /* release the mmap_sem read lock. */
2521 new_page
= khugepaged_alloc_page(hpage
, gfp
, mm
, vma
, address
, node
);
2525 if (unlikely(mem_cgroup_try_charge(new_page
, mm
,
2530 * Prevent all access to pagetables with the exception of
2531 * gup_fast later hanlded by the ptep_clear_flush and the VM
2532 * handled by the anon_vma lock + PG_lock.
2534 down_write(&mm
->mmap_sem
);
2535 if (unlikely(khugepaged_test_exit(mm
)))
2538 vma
= find_vma(mm
, address
);
2541 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2542 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2543 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
2545 if (!hugepage_vma_check(vma
))
2547 pmd
= mm_find_pmd(mm
, address
);
2551 anon_vma_lock_write(vma
->anon_vma
);
2553 pte
= pte_offset_map(pmd
, address
);
2554 pte_ptl
= pte_lockptr(mm
, pmd
);
2556 mmun_start
= address
;
2557 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2558 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2559 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
2561 * After this gup_fast can't run anymore. This also removes
2562 * any huge TLB entry from the CPU so we won't allow
2563 * huge and small TLB entries for the same virtual address
2564 * to avoid the risk of CPU bugs in that area.
2566 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
2567 spin_unlock(pmd_ptl
);
2568 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2571 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2572 spin_unlock(pte_ptl
);
2574 if (unlikely(!isolated
)) {
2577 BUG_ON(!pmd_none(*pmd
));
2579 * We can only use set_pmd_at when establishing
2580 * hugepmds and never for establishing regular pmds that
2581 * points to regular pagetables. Use pmd_populate for that
2583 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
2584 spin_unlock(pmd_ptl
);
2585 anon_vma_unlock_write(vma
->anon_vma
);
2590 * All pages are isolated and locked so anon_vma rmap
2591 * can't run anymore.
2593 anon_vma_unlock_write(vma
->anon_vma
);
2595 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
2597 __SetPageUptodate(new_page
);
2598 pgtable
= pmd_pgtable(_pmd
);
2600 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2601 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
2604 * spin_lock() below is not the equivalent of smp_wmb(), so
2605 * this is needed to avoid the copy_huge_page writes to become
2606 * visible after the set_pmd_at() write.
2611 BUG_ON(!pmd_none(*pmd
));
2612 page_add_new_anon_rmap(new_page
, vma
, address
);
2613 mem_cgroup_commit_charge(new_page
, memcg
, false);
2614 lru_cache_add_active_or_unevictable(new_page
, vma
);
2615 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
2616 set_pmd_at(mm
, address
, pmd
, _pmd
);
2617 update_mmu_cache_pmd(vma
, address
, pmd
);
2618 spin_unlock(pmd_ptl
);
2622 khugepaged_pages_collapsed
++;
2624 up_write(&mm
->mmap_sem
);
2628 mem_cgroup_cancel_charge(new_page
, memcg
);
2632 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2633 struct vm_area_struct
*vma
,
2634 unsigned long address
,
2635 struct page
**hpage
)
2639 int ret
= 0, none_or_zero
= 0;
2641 unsigned long _address
;
2643 int node
= NUMA_NO_NODE
;
2644 bool writable
= false, referenced
= false;
2646 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2648 pmd
= mm_find_pmd(mm
, address
);
2652 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
2653 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2654 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2655 _pte
++, _address
+= PAGE_SIZE
) {
2656 pte_t pteval
= *_pte
;
2657 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2658 if (!userfaultfd_armed(vma
) &&
2659 ++none_or_zero
<= khugepaged_max_ptes_none
)
2664 if (!pte_present(pteval
))
2666 if (pte_write(pteval
))
2669 page
= vm_normal_page(vma
, _address
, pteval
);
2670 if (unlikely(!page
))
2673 * Record which node the original page is from and save this
2674 * information to khugepaged_node_load[].
2675 * Khupaged will allocate hugepage from the node has the max
2678 node
= page_to_nid(page
);
2679 if (khugepaged_scan_abort(node
))
2681 khugepaged_node_load
[node
]++;
2682 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2683 if (!PageLRU(page
) || PageLocked(page
) || !PageAnon(page
))
2686 * cannot use mapcount: can't collapse if there's a gup pin.
2687 * The page must only be referenced by the scanned process
2688 * and page swap cache.
2690 if (page_count(page
) != 1 + !!PageSwapCache(page
))
2692 if (pte_young(pteval
) || PageReferenced(page
) ||
2693 mmu_notifier_test_young(vma
->vm_mm
, address
))
2696 if (referenced
&& writable
)
2699 pte_unmap_unlock(pte
, ptl
);
2701 node
= khugepaged_find_target_node();
2702 /* collapse_huge_page will return with the mmap_sem released */
2703 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2709 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2711 struct mm_struct
*mm
= mm_slot
->mm
;
2713 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2715 if (khugepaged_test_exit(mm
)) {
2717 hash_del(&mm_slot
->hash
);
2718 list_del(&mm_slot
->mm_node
);
2721 * Not strictly needed because the mm exited already.
2723 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2726 /* khugepaged_mm_lock actually not necessary for the below */
2727 free_mm_slot(mm_slot
);
2732 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2733 struct page
**hpage
)
2734 __releases(&khugepaged_mm_lock
)
2735 __acquires(&khugepaged_mm_lock
)
2737 struct mm_slot
*mm_slot
;
2738 struct mm_struct
*mm
;
2739 struct vm_area_struct
*vma
;
2743 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2745 if (khugepaged_scan
.mm_slot
)
2746 mm_slot
= khugepaged_scan
.mm_slot
;
2748 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2749 struct mm_slot
, mm_node
);
2750 khugepaged_scan
.address
= 0;
2751 khugepaged_scan
.mm_slot
= mm_slot
;
2753 spin_unlock(&khugepaged_mm_lock
);
2756 down_read(&mm
->mmap_sem
);
2757 if (unlikely(khugepaged_test_exit(mm
)))
2760 vma
= find_vma(mm
, khugepaged_scan
.address
);
2763 for (; vma
; vma
= vma
->vm_next
) {
2764 unsigned long hstart
, hend
;
2767 if (unlikely(khugepaged_test_exit(mm
))) {
2771 if (!hugepage_vma_check(vma
)) {
2776 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2777 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2780 if (khugepaged_scan
.address
> hend
)
2782 if (khugepaged_scan
.address
< hstart
)
2783 khugepaged_scan
.address
= hstart
;
2784 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2786 while (khugepaged_scan
.address
< hend
) {
2789 if (unlikely(khugepaged_test_exit(mm
)))
2790 goto breakouterloop
;
2792 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2793 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2795 ret
= khugepaged_scan_pmd(mm
, vma
,
2796 khugepaged_scan
.address
,
2798 /* move to next address */
2799 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2800 progress
+= HPAGE_PMD_NR
;
2802 /* we released mmap_sem so break loop */
2803 goto breakouterloop_mmap_sem
;
2804 if (progress
>= pages
)
2805 goto breakouterloop
;
2809 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2810 breakouterloop_mmap_sem
:
2812 spin_lock(&khugepaged_mm_lock
);
2813 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2815 * Release the current mm_slot if this mm is about to die, or
2816 * if we scanned all vmas of this mm.
2818 if (khugepaged_test_exit(mm
) || !vma
) {
2820 * Make sure that if mm_users is reaching zero while
2821 * khugepaged runs here, khugepaged_exit will find
2822 * mm_slot not pointing to the exiting mm.
2824 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2825 khugepaged_scan
.mm_slot
= list_entry(
2826 mm_slot
->mm_node
.next
,
2827 struct mm_slot
, mm_node
);
2828 khugepaged_scan
.address
= 0;
2830 khugepaged_scan
.mm_slot
= NULL
;
2831 khugepaged_full_scans
++;
2834 collect_mm_slot(mm_slot
);
2840 static int khugepaged_has_work(void)
2842 return !list_empty(&khugepaged_scan
.mm_head
) &&
2843 khugepaged_enabled();
2846 static int khugepaged_wait_event(void)
2848 return !list_empty(&khugepaged_scan
.mm_head
) ||
2849 kthread_should_stop();
2852 static void khugepaged_do_scan(void)
2854 struct page
*hpage
= NULL
;
2855 unsigned int progress
= 0, pass_through_head
= 0;
2856 unsigned int pages
= khugepaged_pages_to_scan
;
2859 barrier(); /* write khugepaged_pages_to_scan to local stack */
2861 while (progress
< pages
) {
2862 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2867 if (unlikely(kthread_should_stop() || try_to_freeze()))
2870 spin_lock(&khugepaged_mm_lock
);
2871 if (!khugepaged_scan
.mm_slot
)
2872 pass_through_head
++;
2873 if (khugepaged_has_work() &&
2874 pass_through_head
< 2)
2875 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2879 spin_unlock(&khugepaged_mm_lock
);
2882 if (!IS_ERR_OR_NULL(hpage
))
2886 static void khugepaged_wait_work(void)
2888 if (khugepaged_has_work()) {
2889 if (!khugepaged_scan_sleep_millisecs
)
2892 wait_event_freezable_timeout(khugepaged_wait
,
2893 kthread_should_stop(),
2894 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2898 if (khugepaged_enabled())
2899 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2902 static int khugepaged(void *none
)
2904 struct mm_slot
*mm_slot
;
2907 set_user_nice(current
, MAX_NICE
);
2909 while (!kthread_should_stop()) {
2910 khugepaged_do_scan();
2911 khugepaged_wait_work();
2914 spin_lock(&khugepaged_mm_lock
);
2915 mm_slot
= khugepaged_scan
.mm_slot
;
2916 khugepaged_scan
.mm_slot
= NULL
;
2918 collect_mm_slot(mm_slot
);
2919 spin_unlock(&khugepaged_mm_lock
);
2923 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2924 unsigned long haddr
, pmd_t
*pmd
)
2926 struct mm_struct
*mm
= vma
->vm_mm
;
2931 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2932 /* leave pmd empty until pte is filled */
2934 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2935 pmd_populate(mm
, &_pmd
, pgtable
);
2937 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2939 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2940 entry
= pte_mkspecial(entry
);
2941 pte
= pte_offset_map(&_pmd
, haddr
);
2942 VM_BUG_ON(!pte_none(*pte
));
2943 set_pte_at(mm
, haddr
, pte
, entry
);
2946 smp_wmb(); /* make pte visible before pmd */
2947 pmd_populate(mm
, pmd
, pgtable
);
2948 put_huge_zero_page();
2951 void __split_huge_page_pmd(struct vm_area_struct
*vma
, unsigned long address
,
2955 struct page
*page
= NULL
;
2956 struct mm_struct
*mm
= vma
->vm_mm
;
2957 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
2958 unsigned long mmun_start
; /* For mmu_notifiers */
2959 unsigned long mmun_end
; /* For mmu_notifiers */
2961 BUG_ON(vma
->vm_start
> haddr
|| vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
);
2964 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
2966 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2967 ptl
= pmd_lock(mm
, pmd
);
2968 if (unlikely(!pmd_trans_huge(*pmd
)))
2970 if (vma_is_dax(vma
)) {
2971 pmd_t _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2972 if (is_huge_zero_pmd(_pmd
))
2973 put_huge_zero_page();
2974 } else if (is_huge_zero_pmd(*pmd
)) {
2975 __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
2977 page
= pmd_page(*pmd
);
2978 VM_BUG_ON_PAGE(!page_count(page
), page
);
2983 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2988 split_huge_page(page
);
2992 * We don't always have down_write of mmap_sem here: a racing
2993 * do_huge_pmd_wp_page() might have copied-on-write to another
2994 * huge page before our split_huge_page() got the anon_vma lock.
2996 if (unlikely(pmd_trans_huge(*pmd
)))
3000 void split_huge_page_pmd_mm(struct mm_struct
*mm
, unsigned long address
,
3003 struct vm_area_struct
*vma
;
3005 vma
= find_vma(mm
, address
);
3006 BUG_ON(vma
== NULL
);
3007 split_huge_page_pmd(vma
, address
, pmd
);
3010 static void split_huge_page_address(struct mm_struct
*mm
,
3011 unsigned long address
)
3017 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
3019 pgd
= pgd_offset(mm
, address
);
3020 if (!pgd_present(*pgd
))
3023 pud
= pud_offset(pgd
, address
);
3024 if (!pud_present(*pud
))
3027 pmd
= pmd_offset(pud
, address
);
3028 if (!pmd_present(*pmd
))
3031 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3032 * materialize from under us.
3034 split_huge_page_pmd_mm(mm
, address
, pmd
);
3037 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
3038 unsigned long start
,
3043 * If the new start address isn't hpage aligned and it could
3044 * previously contain an hugepage: check if we need to split
3047 if (start
& ~HPAGE_PMD_MASK
&&
3048 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3049 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3050 split_huge_page_address(vma
->vm_mm
, start
);
3053 * If the new end address isn't hpage aligned and it could
3054 * previously contain an hugepage: check if we need to split
3057 if (end
& ~HPAGE_PMD_MASK
&&
3058 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3059 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3060 split_huge_page_address(vma
->vm_mm
, end
);
3063 * If we're also updating the vma->vm_next->vm_start, if the new
3064 * vm_next->vm_start isn't page aligned and it could previously
3065 * contain an hugepage: check if we need to split an huge pmd.
3067 if (adjust_next
> 0) {
3068 struct vm_area_struct
*next
= vma
->vm_next
;
3069 unsigned long nstart
= next
->vm_start
;
3070 nstart
+= adjust_next
<< PAGE_SHIFT
;
3071 if (nstart
& ~HPAGE_PMD_MASK
&&
3072 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
3073 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
3074 split_huge_page_address(next
->vm_mm
, nstart
);