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>
28 #include <linux/page_idle.h>
31 #include <asm/pgalloc.h>
35 * By default transparent hugepage support is disabled in order that avoid
36 * to risk increase the memory footprint of applications without a guaranteed
37 * benefit. When transparent hugepage support is enabled, is for all mappings,
38 * and khugepaged scans all mappings.
39 * Defrag is invoked by khugepaged hugepage allocations and by page faults
40 * for all hugepage allocations.
42 unsigned long transparent_hugepage_flags __read_mostly
=
43 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
44 (1<<TRANSPARENT_HUGEPAGE_FLAG
)|
46 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
47 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
)|
49 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
)|
50 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
)|
51 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
53 /* default scan 8*512 pte (or vmas) every 30 second */
54 static unsigned int khugepaged_pages_to_scan __read_mostly
= HPAGE_PMD_NR
*8;
55 static unsigned int khugepaged_pages_collapsed
;
56 static unsigned int khugepaged_full_scans
;
57 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly
= 10000;
58 /* during fragmentation poll the hugepage allocator once every minute */
59 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly
= 60000;
60 static struct task_struct
*khugepaged_thread __read_mostly
;
61 static DEFINE_MUTEX(khugepaged_mutex
);
62 static DEFINE_SPINLOCK(khugepaged_mm_lock
);
63 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait
);
65 * default collapse hugepages if there is at least one pte mapped like
66 * it would have happened if the vma was large enough during page
69 static unsigned int khugepaged_max_ptes_none __read_mostly
= HPAGE_PMD_NR
-1;
71 static int khugepaged(void *none
);
72 static int khugepaged_slab_init(void);
73 static void khugepaged_slab_exit(void);
75 #define MM_SLOTS_HASH_BITS 10
76 static __read_mostly
DEFINE_HASHTABLE(mm_slots_hash
, MM_SLOTS_HASH_BITS
);
78 static struct kmem_cache
*mm_slot_cache __read_mostly
;
81 * struct mm_slot - hash lookup from mm to mm_slot
82 * @hash: hash collision list
83 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
84 * @mm: the mm that this information is valid for
87 struct hlist_node hash
;
88 struct list_head mm_node
;
93 * struct khugepaged_scan - cursor for scanning
94 * @mm_head: the head of the mm list to scan
95 * @mm_slot: the current mm_slot we are scanning
96 * @address: the next address inside that to be scanned
98 * There is only the one khugepaged_scan instance of this cursor structure.
100 struct khugepaged_scan
{
101 struct list_head mm_head
;
102 struct mm_slot
*mm_slot
;
103 unsigned long address
;
105 static struct khugepaged_scan khugepaged_scan
= {
106 .mm_head
= LIST_HEAD_INIT(khugepaged_scan
.mm_head
),
110 static void set_recommended_min_free_kbytes(void)
114 unsigned long recommended_min
;
116 for_each_populated_zone(zone
)
119 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
120 recommended_min
= pageblock_nr_pages
* nr_zones
* 2;
123 * Make sure that on average at least two pageblocks are almost free
124 * of another type, one for a migratetype to fall back to and a
125 * second to avoid subsequent fallbacks of other types There are 3
126 * MIGRATE_TYPES we care about.
128 recommended_min
+= pageblock_nr_pages
* nr_zones
*
129 MIGRATE_PCPTYPES
* MIGRATE_PCPTYPES
;
131 /* don't ever allow to reserve more than 5% of the lowmem */
132 recommended_min
= min(recommended_min
,
133 (unsigned long) nr_free_buffer_pages() / 20);
134 recommended_min
<<= (PAGE_SHIFT
-10);
136 if (recommended_min
> min_free_kbytes
) {
137 if (user_min_free_kbytes
>= 0)
138 pr_info("raising min_free_kbytes from %d to %lu "
139 "to help transparent hugepage allocations\n",
140 min_free_kbytes
, recommended_min
);
142 min_free_kbytes
= recommended_min
;
144 setup_per_zone_wmarks();
147 static int start_stop_khugepaged(void)
150 if (khugepaged_enabled()) {
151 if (!khugepaged_thread
)
152 khugepaged_thread
= kthread_run(khugepaged
, NULL
,
154 if (IS_ERR(khugepaged_thread
)) {
155 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
156 err
= PTR_ERR(khugepaged_thread
);
157 khugepaged_thread
= NULL
;
161 if (!list_empty(&khugepaged_scan
.mm_head
))
162 wake_up_interruptible(&khugepaged_wait
);
164 set_recommended_min_free_kbytes();
165 } else if (khugepaged_thread
) {
166 kthread_stop(khugepaged_thread
);
167 khugepaged_thread
= NULL
;
173 static atomic_t huge_zero_refcount
;
174 struct page
*huge_zero_page __read_mostly
;
176 struct page
*get_huge_zero_page(void)
178 struct page
*zero_page
;
180 if (likely(atomic_inc_not_zero(&huge_zero_refcount
)))
181 return READ_ONCE(huge_zero_page
);
183 zero_page
= alloc_pages((GFP_TRANSHUGE
| __GFP_ZERO
) & ~__GFP_MOVABLE
,
186 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED
);
189 count_vm_event(THP_ZERO_PAGE_ALLOC
);
191 if (cmpxchg(&huge_zero_page
, NULL
, zero_page
)) {
193 __free_pages(zero_page
, compound_order(zero_page
));
197 /* We take additional reference here. It will be put back by shrinker */
198 atomic_set(&huge_zero_refcount
, 2);
200 return READ_ONCE(huge_zero_page
);
203 static void put_huge_zero_page(void)
206 * Counter should never go to zero here. Only shrinker can put
209 BUG_ON(atomic_dec_and_test(&huge_zero_refcount
));
212 static unsigned long shrink_huge_zero_page_count(struct shrinker
*shrink
,
213 struct shrink_control
*sc
)
215 /* we can free zero page only if last reference remains */
216 return atomic_read(&huge_zero_refcount
) == 1 ? HPAGE_PMD_NR
: 0;
219 static unsigned long shrink_huge_zero_page_scan(struct shrinker
*shrink
,
220 struct shrink_control
*sc
)
222 if (atomic_cmpxchg(&huge_zero_refcount
, 1, 0) == 1) {
223 struct page
*zero_page
= xchg(&huge_zero_page
, NULL
);
224 BUG_ON(zero_page
== NULL
);
225 __free_pages(zero_page
, compound_order(zero_page
));
232 static struct shrinker huge_zero_page_shrinker
= {
233 .count_objects
= shrink_huge_zero_page_count
,
234 .scan_objects
= shrink_huge_zero_page_scan
,
235 .seeks
= DEFAULT_SEEKS
,
240 static ssize_t
double_flag_show(struct kobject
*kobj
,
241 struct kobj_attribute
*attr
, char *buf
,
242 enum transparent_hugepage_flag enabled
,
243 enum transparent_hugepage_flag req_madv
)
245 if (test_bit(enabled
, &transparent_hugepage_flags
)) {
246 VM_BUG_ON(test_bit(req_madv
, &transparent_hugepage_flags
));
247 return sprintf(buf
, "[always] madvise never\n");
248 } else if (test_bit(req_madv
, &transparent_hugepage_flags
))
249 return sprintf(buf
, "always [madvise] never\n");
251 return sprintf(buf
, "always madvise [never]\n");
253 static ssize_t
double_flag_store(struct kobject
*kobj
,
254 struct kobj_attribute
*attr
,
255 const char *buf
, size_t count
,
256 enum transparent_hugepage_flag enabled
,
257 enum transparent_hugepage_flag req_madv
)
259 if (!memcmp("always", buf
,
260 min(sizeof("always")-1, count
))) {
261 set_bit(enabled
, &transparent_hugepage_flags
);
262 clear_bit(req_madv
, &transparent_hugepage_flags
);
263 } else if (!memcmp("madvise", buf
,
264 min(sizeof("madvise")-1, count
))) {
265 clear_bit(enabled
, &transparent_hugepage_flags
);
266 set_bit(req_madv
, &transparent_hugepage_flags
);
267 } else if (!memcmp("never", buf
,
268 min(sizeof("never")-1, count
))) {
269 clear_bit(enabled
, &transparent_hugepage_flags
);
270 clear_bit(req_madv
, &transparent_hugepage_flags
);
277 static ssize_t
enabled_show(struct kobject
*kobj
,
278 struct kobj_attribute
*attr
, char *buf
)
280 return double_flag_show(kobj
, attr
, buf
,
281 TRANSPARENT_HUGEPAGE_FLAG
,
282 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
284 static ssize_t
enabled_store(struct kobject
*kobj
,
285 struct kobj_attribute
*attr
,
286 const char *buf
, size_t count
)
290 ret
= double_flag_store(kobj
, attr
, buf
, count
,
291 TRANSPARENT_HUGEPAGE_FLAG
,
292 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
);
297 mutex_lock(&khugepaged_mutex
);
298 err
= start_stop_khugepaged();
299 mutex_unlock(&khugepaged_mutex
);
307 static struct kobj_attribute enabled_attr
=
308 __ATTR(enabled
, 0644, enabled_show
, enabled_store
);
310 static ssize_t
single_flag_show(struct kobject
*kobj
,
311 struct kobj_attribute
*attr
, char *buf
,
312 enum transparent_hugepage_flag flag
)
314 return sprintf(buf
, "%d\n",
315 !!test_bit(flag
, &transparent_hugepage_flags
));
318 static ssize_t
single_flag_store(struct kobject
*kobj
,
319 struct kobj_attribute
*attr
,
320 const char *buf
, size_t count
,
321 enum transparent_hugepage_flag flag
)
326 ret
= kstrtoul(buf
, 10, &value
);
333 set_bit(flag
, &transparent_hugepage_flags
);
335 clear_bit(flag
, &transparent_hugepage_flags
);
341 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
342 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
343 * memory just to allocate one more hugepage.
345 static ssize_t
defrag_show(struct kobject
*kobj
,
346 struct kobj_attribute
*attr
, char *buf
)
348 return double_flag_show(kobj
, attr
, buf
,
349 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
350 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
352 static ssize_t
defrag_store(struct kobject
*kobj
,
353 struct kobj_attribute
*attr
,
354 const char *buf
, size_t count
)
356 return double_flag_store(kobj
, attr
, buf
, count
,
357 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG
,
358 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG
);
360 static struct kobj_attribute defrag_attr
=
361 __ATTR(defrag
, 0644, defrag_show
, defrag_store
);
363 static ssize_t
use_zero_page_show(struct kobject
*kobj
,
364 struct kobj_attribute
*attr
, char *buf
)
366 return single_flag_show(kobj
, attr
, buf
,
367 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
369 static ssize_t
use_zero_page_store(struct kobject
*kobj
,
370 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
372 return single_flag_store(kobj
, attr
, buf
, count
,
373 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG
);
375 static struct kobj_attribute use_zero_page_attr
=
376 __ATTR(use_zero_page
, 0644, use_zero_page_show
, use_zero_page_store
);
377 #ifdef CONFIG_DEBUG_VM
378 static ssize_t
debug_cow_show(struct kobject
*kobj
,
379 struct kobj_attribute
*attr
, char *buf
)
381 return single_flag_show(kobj
, attr
, buf
,
382 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
384 static ssize_t
debug_cow_store(struct kobject
*kobj
,
385 struct kobj_attribute
*attr
,
386 const char *buf
, size_t count
)
388 return single_flag_store(kobj
, attr
, buf
, count
,
389 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
);
391 static struct kobj_attribute debug_cow_attr
=
392 __ATTR(debug_cow
, 0644, debug_cow_show
, debug_cow_store
);
393 #endif /* CONFIG_DEBUG_VM */
395 static struct attribute
*hugepage_attr
[] = {
398 &use_zero_page_attr
.attr
,
399 #ifdef CONFIG_DEBUG_VM
400 &debug_cow_attr
.attr
,
405 static struct attribute_group hugepage_attr_group
= {
406 .attrs
= hugepage_attr
,
409 static ssize_t
scan_sleep_millisecs_show(struct kobject
*kobj
,
410 struct kobj_attribute
*attr
,
413 return sprintf(buf
, "%u\n", khugepaged_scan_sleep_millisecs
);
416 static ssize_t
scan_sleep_millisecs_store(struct kobject
*kobj
,
417 struct kobj_attribute
*attr
,
418 const char *buf
, size_t count
)
423 err
= kstrtoul(buf
, 10, &msecs
);
424 if (err
|| msecs
> UINT_MAX
)
427 khugepaged_scan_sleep_millisecs
= msecs
;
428 wake_up_interruptible(&khugepaged_wait
);
432 static struct kobj_attribute scan_sleep_millisecs_attr
=
433 __ATTR(scan_sleep_millisecs
, 0644, scan_sleep_millisecs_show
,
434 scan_sleep_millisecs_store
);
436 static ssize_t
alloc_sleep_millisecs_show(struct kobject
*kobj
,
437 struct kobj_attribute
*attr
,
440 return sprintf(buf
, "%u\n", khugepaged_alloc_sleep_millisecs
);
443 static ssize_t
alloc_sleep_millisecs_store(struct kobject
*kobj
,
444 struct kobj_attribute
*attr
,
445 const char *buf
, size_t count
)
450 err
= kstrtoul(buf
, 10, &msecs
);
451 if (err
|| msecs
> UINT_MAX
)
454 khugepaged_alloc_sleep_millisecs
= msecs
;
455 wake_up_interruptible(&khugepaged_wait
);
459 static struct kobj_attribute alloc_sleep_millisecs_attr
=
460 __ATTR(alloc_sleep_millisecs
, 0644, alloc_sleep_millisecs_show
,
461 alloc_sleep_millisecs_store
);
463 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
464 struct kobj_attribute
*attr
,
467 return sprintf(buf
, "%u\n", khugepaged_pages_to_scan
);
469 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
470 struct kobj_attribute
*attr
,
471 const char *buf
, size_t count
)
476 err
= kstrtoul(buf
, 10, &pages
);
477 if (err
|| !pages
|| pages
> UINT_MAX
)
480 khugepaged_pages_to_scan
= pages
;
484 static struct kobj_attribute pages_to_scan_attr
=
485 __ATTR(pages_to_scan
, 0644, pages_to_scan_show
,
486 pages_to_scan_store
);
488 static ssize_t
pages_collapsed_show(struct kobject
*kobj
,
489 struct kobj_attribute
*attr
,
492 return sprintf(buf
, "%u\n", khugepaged_pages_collapsed
);
494 static struct kobj_attribute pages_collapsed_attr
=
495 __ATTR_RO(pages_collapsed
);
497 static ssize_t
full_scans_show(struct kobject
*kobj
,
498 struct kobj_attribute
*attr
,
501 return sprintf(buf
, "%u\n", khugepaged_full_scans
);
503 static struct kobj_attribute full_scans_attr
=
504 __ATTR_RO(full_scans
);
506 static ssize_t
khugepaged_defrag_show(struct kobject
*kobj
,
507 struct kobj_attribute
*attr
, char *buf
)
509 return single_flag_show(kobj
, attr
, buf
,
510 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
512 static ssize_t
khugepaged_defrag_store(struct kobject
*kobj
,
513 struct kobj_attribute
*attr
,
514 const char *buf
, size_t count
)
516 return single_flag_store(kobj
, attr
, buf
, count
,
517 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG
);
519 static struct kobj_attribute khugepaged_defrag_attr
=
520 __ATTR(defrag
, 0644, khugepaged_defrag_show
,
521 khugepaged_defrag_store
);
524 * max_ptes_none controls if khugepaged should collapse hugepages over
525 * any unmapped ptes in turn potentially increasing the memory
526 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
527 * reduce the available free memory in the system as it
528 * runs. Increasing max_ptes_none will instead potentially reduce the
529 * free memory in the system during the khugepaged scan.
531 static ssize_t
khugepaged_max_ptes_none_show(struct kobject
*kobj
,
532 struct kobj_attribute
*attr
,
535 return sprintf(buf
, "%u\n", khugepaged_max_ptes_none
);
537 static ssize_t
khugepaged_max_ptes_none_store(struct kobject
*kobj
,
538 struct kobj_attribute
*attr
,
539 const char *buf
, size_t count
)
542 unsigned long max_ptes_none
;
544 err
= kstrtoul(buf
, 10, &max_ptes_none
);
545 if (err
|| max_ptes_none
> HPAGE_PMD_NR
-1)
548 khugepaged_max_ptes_none
= max_ptes_none
;
552 static struct kobj_attribute khugepaged_max_ptes_none_attr
=
553 __ATTR(max_ptes_none
, 0644, khugepaged_max_ptes_none_show
,
554 khugepaged_max_ptes_none_store
);
556 static struct attribute
*khugepaged_attr
[] = {
557 &khugepaged_defrag_attr
.attr
,
558 &khugepaged_max_ptes_none_attr
.attr
,
559 &pages_to_scan_attr
.attr
,
560 &pages_collapsed_attr
.attr
,
561 &full_scans_attr
.attr
,
562 &scan_sleep_millisecs_attr
.attr
,
563 &alloc_sleep_millisecs_attr
.attr
,
567 static struct attribute_group khugepaged_attr_group
= {
568 .attrs
= khugepaged_attr
,
569 .name
= "khugepaged",
572 static int __init
hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
576 *hugepage_kobj
= kobject_create_and_add("transparent_hugepage", mm_kobj
);
577 if (unlikely(!*hugepage_kobj
)) {
578 pr_err("failed to create transparent hugepage kobject\n");
582 err
= sysfs_create_group(*hugepage_kobj
, &hugepage_attr_group
);
584 pr_err("failed to register transparent hugepage group\n");
588 err
= sysfs_create_group(*hugepage_kobj
, &khugepaged_attr_group
);
590 pr_err("failed to register transparent hugepage group\n");
591 goto remove_hp_group
;
597 sysfs_remove_group(*hugepage_kobj
, &hugepage_attr_group
);
599 kobject_put(*hugepage_kobj
);
603 static void __init
hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
605 sysfs_remove_group(hugepage_kobj
, &khugepaged_attr_group
);
606 sysfs_remove_group(hugepage_kobj
, &hugepage_attr_group
);
607 kobject_put(hugepage_kobj
);
610 static inline int hugepage_init_sysfs(struct kobject
**hugepage_kobj
)
615 static inline void hugepage_exit_sysfs(struct kobject
*hugepage_kobj
)
618 #endif /* CONFIG_SYSFS */
620 static int __init
hugepage_init(void)
623 struct kobject
*hugepage_kobj
;
625 if (!has_transparent_hugepage()) {
626 transparent_hugepage_flags
= 0;
630 err
= hugepage_init_sysfs(&hugepage_kobj
);
634 err
= khugepaged_slab_init();
638 err
= register_shrinker(&huge_zero_page_shrinker
);
640 goto err_hzp_shrinker
;
643 * By default disable transparent hugepages on smaller systems,
644 * where the extra memory used could hurt more than TLB overhead
645 * is likely to save. The admin can still enable it through /sys.
647 if (totalram_pages
< (512 << (20 - PAGE_SHIFT
))) {
648 transparent_hugepage_flags
= 0;
652 err
= start_stop_khugepaged();
658 unregister_shrinker(&huge_zero_page_shrinker
);
660 khugepaged_slab_exit();
662 hugepage_exit_sysfs(hugepage_kobj
);
666 subsys_initcall(hugepage_init
);
668 static int __init
setup_transparent_hugepage(char *str
)
673 if (!strcmp(str
, "always")) {
674 set_bit(TRANSPARENT_HUGEPAGE_FLAG
,
675 &transparent_hugepage_flags
);
676 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
677 &transparent_hugepage_flags
);
679 } else if (!strcmp(str
, "madvise")) {
680 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
681 &transparent_hugepage_flags
);
682 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
683 &transparent_hugepage_flags
);
685 } else if (!strcmp(str
, "never")) {
686 clear_bit(TRANSPARENT_HUGEPAGE_FLAG
,
687 &transparent_hugepage_flags
);
688 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG
,
689 &transparent_hugepage_flags
);
694 pr_warn("transparent_hugepage= cannot parse, ignored\n");
697 __setup("transparent_hugepage=", setup_transparent_hugepage
);
699 pmd_t
maybe_pmd_mkwrite(pmd_t pmd
, struct vm_area_struct
*vma
)
701 if (likely(vma
->vm_flags
& VM_WRITE
))
702 pmd
= pmd_mkwrite(pmd
);
706 static inline pmd_t
mk_huge_pmd(struct page
*page
, pgprot_t prot
)
709 entry
= mk_pmd(page
, prot
);
710 entry
= pmd_mkhuge(entry
);
714 static int __do_huge_pmd_anonymous_page(struct mm_struct
*mm
,
715 struct vm_area_struct
*vma
,
716 unsigned long address
, pmd_t
*pmd
,
717 struct page
*page
, gfp_t gfp
,
720 struct mem_cgroup
*memcg
;
723 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
725 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
727 if (mem_cgroup_try_charge(page
, mm
, gfp
, &memcg
)) {
729 count_vm_event(THP_FAULT_FALLBACK
);
730 return VM_FAULT_FALLBACK
;
733 pgtable
= pte_alloc_one(mm
, haddr
);
734 if (unlikely(!pgtable
)) {
735 mem_cgroup_cancel_charge(page
, memcg
);
740 clear_huge_page(page
, haddr
, HPAGE_PMD_NR
);
742 * The memory barrier inside __SetPageUptodate makes sure that
743 * clear_huge_page writes become visible before the set_pmd_at()
746 __SetPageUptodate(page
);
748 ptl
= pmd_lock(mm
, pmd
);
749 if (unlikely(!pmd_none(*pmd
))) {
751 mem_cgroup_cancel_charge(page
, memcg
);
753 pte_free(mm
, pgtable
);
757 /* Deliver the page fault to userland */
758 if (userfaultfd_missing(vma
)) {
762 mem_cgroup_cancel_charge(page
, memcg
);
764 pte_free(mm
, pgtable
);
765 ret
= handle_userfault(vma
, address
, flags
,
767 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
771 entry
= mk_huge_pmd(page
, vma
->vm_page_prot
);
772 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
773 page_add_new_anon_rmap(page
, vma
, haddr
);
774 mem_cgroup_commit_charge(page
, memcg
, false);
775 lru_cache_add_active_or_unevictable(page
, vma
);
776 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
777 set_pmd_at(mm
, haddr
, pmd
, entry
);
778 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
779 atomic_long_inc(&mm
->nr_ptes
);
781 count_vm_event(THP_FAULT_ALLOC
);
787 static inline gfp_t
alloc_hugepage_gfpmask(int defrag
, gfp_t extra_gfp
)
789 return (GFP_TRANSHUGE
& ~(defrag
? 0 : __GFP_RECLAIM
)) | extra_gfp
;
792 /* Caller must hold page table lock. */
793 static bool set_huge_zero_page(pgtable_t pgtable
, struct mm_struct
*mm
,
794 struct vm_area_struct
*vma
, unsigned long haddr
, pmd_t
*pmd
,
795 struct page
*zero_page
)
800 entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
801 entry
= pmd_mkhuge(entry
);
802 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
803 set_pmd_at(mm
, haddr
, pmd
, entry
);
804 atomic_long_inc(&mm
->nr_ptes
);
808 int do_huge_pmd_anonymous_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
809 unsigned long address
, pmd_t
*pmd
,
814 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
816 if (haddr
< vma
->vm_start
|| haddr
+ HPAGE_PMD_SIZE
> vma
->vm_end
)
817 return VM_FAULT_FALLBACK
;
818 if (unlikely(anon_vma_prepare(vma
)))
820 if (unlikely(khugepaged_enter(vma
, vma
->vm_flags
)))
822 if (!(flags
& FAULT_FLAG_WRITE
) && !mm_forbids_zeropage(mm
) &&
823 transparent_hugepage_use_zero_page()) {
826 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
);
839 if (pmd_none(*pmd
)) {
840 if (userfaultfd_missing(vma
)) {
842 pte_free(mm
, pgtable
);
843 put_huge_zero_page();
844 ret
= handle_userfault(vma
, address
, flags
,
846 VM_BUG_ON(ret
& VM_FAULT_FALLBACK
);
848 set_huge_zero_page(pgtable
, mm
, vma
,
855 pte_free(mm
, pgtable
);
856 put_huge_zero_page();
860 gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
861 page
= alloc_hugepage_vma(gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
862 if (unlikely(!page
)) {
863 count_vm_event(THP_FAULT_FALLBACK
);
864 return VM_FAULT_FALLBACK
;
866 return __do_huge_pmd_anonymous_page(mm
, vma
, address
, pmd
, page
, gfp
,
870 static void insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
871 pmd_t
*pmd
, unsigned long pfn
, pgprot_t prot
, bool write
)
873 struct mm_struct
*mm
= vma
->vm_mm
;
877 ptl
= pmd_lock(mm
, pmd
);
878 if (pmd_none(*pmd
)) {
879 entry
= pmd_mkhuge(pfn_pmd(pfn
, prot
));
881 entry
= pmd_mkyoung(pmd_mkdirty(entry
));
882 entry
= maybe_pmd_mkwrite(entry
, vma
);
884 set_pmd_at(mm
, addr
, pmd
, entry
);
885 update_mmu_cache_pmd(vma
, addr
, pmd
);
890 int vmf_insert_pfn_pmd(struct vm_area_struct
*vma
, unsigned long addr
,
891 pmd_t
*pmd
, unsigned long pfn
, bool write
)
893 pgprot_t pgprot
= vma
->vm_page_prot
;
895 * If we had pmd_special, we could avoid all these restrictions,
896 * but we need to be consistent with PTEs and architectures that
897 * can't support a 'special' bit.
899 BUG_ON(!(vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)));
900 BUG_ON((vma
->vm_flags
& (VM_PFNMAP
|VM_MIXEDMAP
)) ==
901 (VM_PFNMAP
|VM_MIXEDMAP
));
902 BUG_ON((vma
->vm_flags
& VM_PFNMAP
) && is_cow_mapping(vma
->vm_flags
));
903 BUG_ON((vma
->vm_flags
& VM_MIXEDMAP
) && pfn_valid(pfn
));
905 if (addr
< vma
->vm_start
|| addr
>= vma
->vm_end
)
906 return VM_FAULT_SIGBUS
;
907 if (track_pfn_insert(vma
, &pgprot
, pfn
))
908 return VM_FAULT_SIGBUS
;
909 insert_pfn_pmd(vma
, addr
, pmd
, pfn
, pgprot
, write
);
910 return VM_FAULT_NOPAGE
;
913 int copy_huge_pmd(struct mm_struct
*dst_mm
, struct mm_struct
*src_mm
,
914 pmd_t
*dst_pmd
, pmd_t
*src_pmd
, unsigned long addr
,
915 struct vm_area_struct
*vma
)
917 spinlock_t
*dst_ptl
, *src_ptl
;
918 struct page
*src_page
;
924 pgtable
= pte_alloc_one(dst_mm
, addr
);
925 if (unlikely(!pgtable
))
928 dst_ptl
= pmd_lock(dst_mm
, dst_pmd
);
929 src_ptl
= pmd_lockptr(src_mm
, src_pmd
);
930 spin_lock_nested(src_ptl
, SINGLE_DEPTH_NESTING
);
934 if (unlikely(!pmd_trans_huge(pmd
))) {
935 pte_free(dst_mm
, pgtable
);
939 * When page table lock is held, the huge zero pmd should not be
940 * under splitting since we don't split the page itself, only pmd to
943 if (is_huge_zero_pmd(pmd
)) {
944 struct page
*zero_page
;
946 * get_huge_zero_page() will never allocate a new page here,
947 * since we already have a zero page to copy. It just takes a
950 zero_page
= get_huge_zero_page();
951 set_huge_zero_page(pgtable
, dst_mm
, vma
, addr
, dst_pmd
,
957 if (unlikely(pmd_trans_splitting(pmd
))) {
958 /* split huge page running from under us */
959 spin_unlock(src_ptl
);
960 spin_unlock(dst_ptl
);
961 pte_free(dst_mm
, pgtable
);
963 wait_split_huge_page(vma
->anon_vma
, src_pmd
); /* src_vma */
966 src_page
= pmd_page(pmd
);
967 VM_BUG_ON_PAGE(!PageHead(src_page
), src_page
);
969 page_dup_rmap(src_page
);
970 add_mm_counter(dst_mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
972 pmdp_set_wrprotect(src_mm
, addr
, src_pmd
);
973 pmd
= pmd_mkold(pmd_wrprotect(pmd
));
974 pgtable_trans_huge_deposit(dst_mm
, dst_pmd
, pgtable
);
975 set_pmd_at(dst_mm
, addr
, dst_pmd
, pmd
);
976 atomic_long_inc(&dst_mm
->nr_ptes
);
980 spin_unlock(src_ptl
);
981 spin_unlock(dst_ptl
);
986 void huge_pmd_set_accessed(struct mm_struct
*mm
,
987 struct vm_area_struct
*vma
,
988 unsigned long address
,
989 pmd_t
*pmd
, pmd_t orig_pmd
,
996 ptl
= pmd_lock(mm
, pmd
);
997 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1000 entry
= pmd_mkyoung(orig_pmd
);
1001 haddr
= address
& HPAGE_PMD_MASK
;
1002 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, dirty
))
1003 update_mmu_cache_pmd(vma
, address
, pmd
);
1010 * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
1011 * during copy_user_huge_page()'s copy_page_rep(): in the case when
1012 * the source page gets split and a tail freed before copy completes.
1013 * Called under pmd_lock of checked pmd, so safe from splitting itself.
1015 static void get_user_huge_page(struct page
*page
)
1017 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC
)) {
1018 struct page
*endpage
= page
+ HPAGE_PMD_NR
;
1020 atomic_add(HPAGE_PMD_NR
, &page
->_count
);
1021 while (++page
< endpage
)
1022 get_huge_page_tail(page
);
1028 static void put_user_huge_page(struct page
*page
)
1030 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC
)) {
1031 struct page
*endpage
= page
+ HPAGE_PMD_NR
;
1033 while (page
< endpage
)
1040 static int do_huge_pmd_wp_page_fallback(struct mm_struct
*mm
,
1041 struct vm_area_struct
*vma
,
1042 unsigned long address
,
1043 pmd_t
*pmd
, pmd_t orig_pmd
,
1045 unsigned long haddr
)
1047 struct mem_cgroup
*memcg
;
1052 struct page
**pages
;
1053 unsigned long mmun_start
; /* For mmu_notifiers */
1054 unsigned long mmun_end
; /* For mmu_notifiers */
1056 pages
= kmalloc(sizeof(struct page
*) * HPAGE_PMD_NR
,
1058 if (unlikely(!pages
)) {
1059 ret
|= VM_FAULT_OOM
;
1063 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1064 pages
[i
] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE
|
1066 vma
, address
, page_to_nid(page
));
1067 if (unlikely(!pages
[i
] ||
1068 mem_cgroup_try_charge(pages
[i
], mm
, GFP_KERNEL
,
1073 memcg
= (void *)page_private(pages
[i
]);
1074 set_page_private(pages
[i
], 0);
1075 mem_cgroup_cancel_charge(pages
[i
], memcg
);
1079 ret
|= VM_FAULT_OOM
;
1082 set_page_private(pages
[i
], (unsigned long)memcg
);
1085 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1086 copy_user_highpage(pages
[i
], page
+ i
,
1087 haddr
+ PAGE_SIZE
* i
, vma
);
1088 __SetPageUptodate(pages
[i
]);
1093 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1094 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1096 ptl
= pmd_lock(mm
, pmd
);
1097 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1098 goto out_free_pages
;
1099 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1101 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1102 /* leave pmd empty until pte is filled */
1104 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1105 pmd_populate(mm
, &_pmd
, pgtable
);
1107 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1109 entry
= mk_pte(pages
[i
], vma
->vm_page_prot
);
1110 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1111 memcg
= (void *)page_private(pages
[i
]);
1112 set_page_private(pages
[i
], 0);
1113 page_add_new_anon_rmap(pages
[i
], vma
, haddr
);
1114 mem_cgroup_commit_charge(pages
[i
], memcg
, false);
1115 lru_cache_add_active_or_unevictable(pages
[i
], vma
);
1116 pte
= pte_offset_map(&_pmd
, haddr
);
1117 VM_BUG_ON(!pte_none(*pte
));
1118 set_pte_at(mm
, haddr
, pte
, entry
);
1123 smp_wmb(); /* make pte visible before pmd */
1124 pmd_populate(mm
, pmd
, pgtable
);
1125 page_remove_rmap(page
);
1128 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1130 ret
|= VM_FAULT_WRITE
;
1138 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1139 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
1140 memcg
= (void *)page_private(pages
[i
]);
1141 set_page_private(pages
[i
], 0);
1142 mem_cgroup_cancel_charge(pages
[i
], memcg
);
1149 int do_huge_pmd_wp_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1150 unsigned long address
, pmd_t
*pmd
, pmd_t orig_pmd
)
1154 struct page
*page
= NULL
, *new_page
;
1155 struct mem_cgroup
*memcg
;
1156 unsigned long haddr
;
1157 unsigned long mmun_start
; /* For mmu_notifiers */
1158 unsigned long mmun_end
; /* For mmu_notifiers */
1159 gfp_t huge_gfp
; /* for allocation and charge */
1161 ptl
= pmd_lockptr(mm
, pmd
);
1162 VM_BUG_ON_VMA(!vma
->anon_vma
, vma
);
1163 haddr
= address
& HPAGE_PMD_MASK
;
1164 if (is_huge_zero_pmd(orig_pmd
))
1167 if (unlikely(!pmd_same(*pmd
, orig_pmd
)))
1170 page
= pmd_page(orig_pmd
);
1171 VM_BUG_ON_PAGE(!PageCompound(page
) || !PageHead(page
), page
);
1172 if (page_mapcount(page
) == 1) {
1174 entry
= pmd_mkyoung(orig_pmd
);
1175 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1176 if (pmdp_set_access_flags(vma
, haddr
, pmd
, entry
, 1))
1177 update_mmu_cache_pmd(vma
, address
, pmd
);
1178 ret
|= VM_FAULT_WRITE
;
1181 get_user_huge_page(page
);
1184 if (transparent_hugepage_enabled(vma
) &&
1185 !transparent_hugepage_debug_cow()) {
1186 huge_gfp
= alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma
), 0);
1187 new_page
= alloc_hugepage_vma(huge_gfp
, vma
, haddr
, HPAGE_PMD_ORDER
);
1191 if (unlikely(!new_page
)) {
1193 split_huge_page_pmd(vma
, address
, pmd
);
1194 ret
|= VM_FAULT_FALLBACK
;
1196 ret
= do_huge_pmd_wp_page_fallback(mm
, vma
, address
,
1197 pmd
, orig_pmd
, page
, haddr
);
1198 if (ret
& VM_FAULT_OOM
) {
1199 split_huge_page(page
);
1200 ret
|= VM_FAULT_FALLBACK
;
1202 put_user_huge_page(page
);
1204 count_vm_event(THP_FAULT_FALLBACK
);
1208 if (unlikely(mem_cgroup_try_charge(new_page
, mm
, huge_gfp
, &memcg
))) {
1211 split_huge_page(page
);
1212 put_user_huge_page(page
);
1214 split_huge_page_pmd(vma
, address
, pmd
);
1215 ret
|= VM_FAULT_FALLBACK
;
1216 count_vm_event(THP_FAULT_FALLBACK
);
1220 count_vm_event(THP_FAULT_ALLOC
);
1223 clear_huge_page(new_page
, haddr
, HPAGE_PMD_NR
);
1225 copy_user_huge_page(new_page
, page
, haddr
, vma
, HPAGE_PMD_NR
);
1226 __SetPageUptodate(new_page
);
1229 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
1230 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1234 put_user_huge_page(page
);
1235 if (unlikely(!pmd_same(*pmd
, orig_pmd
))) {
1237 mem_cgroup_cancel_charge(new_page
, memcg
);
1242 entry
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
1243 entry
= maybe_pmd_mkwrite(pmd_mkdirty(entry
), vma
);
1244 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
1245 page_add_new_anon_rmap(new_page
, vma
, haddr
);
1246 mem_cgroup_commit_charge(new_page
, memcg
, false);
1247 lru_cache_add_active_or_unevictable(new_page
, vma
);
1248 set_pmd_at(mm
, haddr
, pmd
, entry
);
1249 update_mmu_cache_pmd(vma
, address
, pmd
);
1251 add_mm_counter(mm
, MM_ANONPAGES
, HPAGE_PMD_NR
);
1252 put_huge_zero_page();
1254 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1255 page_remove_rmap(page
);
1258 ret
|= VM_FAULT_WRITE
;
1262 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1271 * FOLL_FORCE can write to even unwritable pmd's, but only
1272 * after we've gone through a COW cycle and they are dirty.
1274 static inline bool can_follow_write_pmd(pmd_t pmd
, unsigned int flags
)
1276 return pmd_write(pmd
) ||
1277 ((flags
& FOLL_FORCE
) && (flags
& FOLL_COW
) && pmd_dirty(pmd
));
1280 struct page
*follow_trans_huge_pmd(struct vm_area_struct
*vma
,
1285 struct mm_struct
*mm
= vma
->vm_mm
;
1286 struct page
*page
= NULL
;
1288 assert_spin_locked(pmd_lockptr(mm
, pmd
));
1290 if (flags
& FOLL_WRITE
&& !can_follow_write_pmd(*pmd
, flags
))
1293 /* Avoid dumping huge zero page */
1294 if ((flags
& FOLL_DUMP
) && is_huge_zero_pmd(*pmd
))
1295 return ERR_PTR(-EFAULT
);
1297 /* Full NUMA hinting faults to serialise migration in fault paths */
1298 if ((flags
& FOLL_NUMA
) && pmd_protnone(*pmd
))
1301 page
= pmd_page(*pmd
);
1302 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1303 if (flags
& FOLL_TOUCH
) {
1305 _pmd
= pmd_mkyoung(*pmd
);
1306 if (flags
& FOLL_WRITE
)
1307 _pmd
= pmd_mkdirty(_pmd
);
1308 if (pmdp_set_access_flags(vma
, addr
& HPAGE_PMD_MASK
,
1309 pmd
, _pmd
, flags
& FOLL_WRITE
))
1310 update_mmu_cache_pmd(vma
, addr
, pmd
);
1312 if ((flags
& FOLL_MLOCK
) && (vma
->vm_flags
& VM_LOCKED
)) {
1313 if (page
->mapping
&& trylock_page(page
)) {
1316 mlock_vma_page(page
);
1320 page
+= (addr
& ~HPAGE_PMD_MASK
) >> PAGE_SHIFT
;
1321 VM_BUG_ON_PAGE(!PageCompound(page
), page
);
1322 if (flags
& FOLL_GET
)
1323 get_page_foll(page
);
1329 /* NUMA hinting page fault entry point for trans huge pmds */
1330 int do_huge_pmd_numa_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1331 unsigned long addr
, pmd_t pmd
, pmd_t
*pmdp
)
1334 struct anon_vma
*anon_vma
= NULL
;
1336 unsigned long haddr
= addr
& HPAGE_PMD_MASK
;
1337 int page_nid
= -1, this_nid
= numa_node_id();
1338 int target_nid
, last_cpupid
= -1;
1340 bool migrated
= false;
1344 /* A PROT_NONE fault should not end up here */
1345 BUG_ON(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)));
1347 ptl
= pmd_lock(mm
, pmdp
);
1348 if (unlikely(!pmd_same(pmd
, *pmdp
)))
1352 * If there are potential migrations, wait for completion and retry
1353 * without disrupting NUMA hinting information. Do not relock and
1354 * check_same as the page may no longer be mapped.
1356 if (unlikely(pmd_trans_migrating(*pmdp
))) {
1357 page
= pmd_page(*pmdp
);
1358 if (!get_page_unless_zero(page
))
1361 wait_on_page_locked(page
);
1366 page
= pmd_page(pmd
);
1367 BUG_ON(is_huge_zero_page(page
));
1368 page_nid
= page_to_nid(page
);
1369 last_cpupid
= page_cpupid_last(page
);
1370 count_vm_numa_event(NUMA_HINT_FAULTS
);
1371 if (page_nid
== this_nid
) {
1372 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL
);
1373 flags
|= TNF_FAULT_LOCAL
;
1376 /* See similar comment in do_numa_page for explanation */
1377 if (!(vma
->vm_flags
& VM_WRITE
))
1378 flags
|= TNF_NO_GROUP
;
1381 * Acquire the page lock to serialise THP migrations but avoid dropping
1382 * page_table_lock if at all possible
1384 page_locked
= trylock_page(page
);
1385 target_nid
= mpol_misplaced(page
, vma
, haddr
);
1386 if (target_nid
== -1) {
1387 /* If the page was locked, there are no parallel migrations */
1392 /* Migration could have started since the pmd_trans_migrating check */
1395 if (!get_page_unless_zero(page
))
1398 wait_on_page_locked(page
);
1404 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1405 * to serialises splits
1409 anon_vma
= page_lock_anon_vma_read(page
);
1411 /* Confirm the PMD did not change while page_table_lock was released */
1413 if (unlikely(!pmd_same(pmd
, *pmdp
))) {
1420 /* Bail if we fail to protect against THP splits for any reason */
1421 if (unlikely(!anon_vma
)) {
1428 * Migrate the THP to the requested node, returns with page unlocked
1429 * and access rights restored.
1432 migrated
= migrate_misplaced_transhuge_page(mm
, vma
,
1433 pmdp
, pmd
, addr
, page
, target_nid
);
1435 flags
|= TNF_MIGRATED
;
1436 page_nid
= target_nid
;
1438 flags
|= TNF_MIGRATE_FAIL
;
1442 BUG_ON(!PageLocked(page
));
1443 was_writable
= pmd_write(pmd
);
1444 pmd
= pmd_modify(pmd
, vma
->vm_page_prot
);
1445 pmd
= pmd_mkyoung(pmd
);
1447 pmd
= pmd_mkwrite(pmd
);
1448 set_pmd_at(mm
, haddr
, pmdp
, pmd
);
1449 update_mmu_cache_pmd(vma
, addr
, pmdp
);
1456 page_unlock_anon_vma_read(anon_vma
);
1459 task_numa_fault(last_cpupid
, page_nid
, HPAGE_PMD_NR
, flags
);
1464 int zap_huge_pmd(struct mmu_gather
*tlb
, struct vm_area_struct
*vma
,
1465 pmd_t
*pmd
, unsigned long addr
)
1470 if (__pmd_trans_huge_lock(pmd
, vma
, &ptl
) != 1)
1473 * For architectures like ppc64 we look at deposited pgtable
1474 * when calling pmdp_huge_get_and_clear. So do the
1475 * pgtable_trans_huge_withdraw after finishing pmdp related
1478 orig_pmd
= pmdp_huge_get_and_clear_full(tlb
->mm
, addr
, pmd
,
1480 tlb_remove_pmd_tlb_entry(tlb
, pmd
, addr
);
1481 if (vma_is_dax(vma
)) {
1483 if (is_huge_zero_pmd(orig_pmd
))
1484 put_huge_zero_page();
1485 } else if (is_huge_zero_pmd(orig_pmd
)) {
1486 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1487 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1489 put_huge_zero_page();
1491 struct page
*page
= pmd_page(orig_pmd
);
1492 page_remove_rmap(page
);
1493 VM_BUG_ON_PAGE(page_mapcount(page
) < 0, page
);
1494 add_mm_counter(tlb
->mm
, MM_ANONPAGES
, -HPAGE_PMD_NR
);
1495 VM_BUG_ON_PAGE(!PageHead(page
), page
);
1496 pte_free(tlb
->mm
, pgtable_trans_huge_withdraw(tlb
->mm
, pmd
));
1497 atomic_long_dec(&tlb
->mm
->nr_ptes
);
1499 tlb_remove_page(tlb
, page
);
1504 int move_huge_pmd(struct vm_area_struct
*vma
, struct vm_area_struct
*new_vma
,
1505 unsigned long old_addr
,
1506 unsigned long new_addr
, unsigned long old_end
,
1507 pmd_t
*old_pmd
, pmd_t
*new_pmd
)
1509 spinlock_t
*old_ptl
, *new_ptl
;
1512 bool force_flush
= false;
1513 struct mm_struct
*mm
= vma
->vm_mm
;
1515 if ((old_addr
& ~HPAGE_PMD_MASK
) ||
1516 (new_addr
& ~HPAGE_PMD_MASK
) ||
1517 old_end
- old_addr
< HPAGE_PMD_SIZE
||
1518 (new_vma
->vm_flags
& VM_NOHUGEPAGE
))
1522 * The destination pmd shouldn't be established, free_pgtables()
1523 * should have release it.
1525 if (WARN_ON(!pmd_none(*new_pmd
))) {
1526 VM_BUG_ON(pmd_trans_huge(*new_pmd
));
1531 * We don't have to worry about the ordering of src and dst
1532 * ptlocks because exclusive mmap_sem prevents deadlock.
1534 ret
= __pmd_trans_huge_lock(old_pmd
, vma
, &old_ptl
);
1536 new_ptl
= pmd_lockptr(mm
, new_pmd
);
1537 if (new_ptl
!= old_ptl
)
1538 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
1539 pmd
= pmdp_huge_get_and_clear(mm
, old_addr
, old_pmd
);
1540 if (pmd_present(pmd
))
1542 VM_BUG_ON(!pmd_none(*new_pmd
));
1544 if (pmd_move_must_withdraw(new_ptl
, old_ptl
)) {
1546 pgtable
= pgtable_trans_huge_withdraw(mm
, old_pmd
);
1547 pgtable_trans_huge_deposit(mm
, new_pmd
, pgtable
);
1549 set_pmd_at(mm
, new_addr
, new_pmd
, pmd_mksoft_dirty(pmd
));
1551 flush_tlb_range(vma
, old_addr
, old_addr
+ PMD_SIZE
);
1552 if (new_ptl
!= old_ptl
)
1553 spin_unlock(new_ptl
);
1554 spin_unlock(old_ptl
);
1562 * - 0 if PMD could not be locked
1563 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1564 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1566 int change_huge_pmd(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1567 unsigned long addr
, pgprot_t newprot
, int prot_numa
)
1569 struct mm_struct
*mm
= vma
->vm_mm
;
1572 bool preserve_write
;
1576 if (__pmd_trans_huge_lock(pmd
, vma
, &ptl
) != 1)
1579 preserve_write
= prot_numa
&& pmd_write(*pmd
);
1583 * Avoid trapping faults against the zero page. The read-only
1584 * data is likely to be read-cached on the local CPU and
1585 * local/remote hits to the zero page are not interesting.
1587 if (prot_numa
&& is_huge_zero_pmd(*pmd
))
1590 if (prot_numa
&& pmd_protnone(*pmd
))
1594 * In case prot_numa, we are under down_read(mmap_sem). It's critical
1595 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
1596 * which is also under down_read(mmap_sem):
1599 * change_huge_pmd(prot_numa=1)
1600 * pmdp_huge_get_and_clear_notify()
1601 * madvise_dontneed()
1603 * pmd_trans_huge(*pmd) == 0 (without ptl)
1606 * // pmd is re-established
1608 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1609 * which may break userspace.
1611 * pmdp_invalidate() is required to make sure we don't miss
1612 * dirty/young flags set by hardware.
1615 pmdp_invalidate(vma
, addr
, pmd
);
1618 * Recover dirty/young flags. It relies on pmdp_invalidate to not
1621 if (pmd_dirty(*pmd
))
1622 entry
= pmd_mkdirty(entry
);
1623 if (pmd_young(*pmd
))
1624 entry
= pmd_mkyoung(entry
);
1626 entry
= pmd_modify(entry
, newprot
);
1628 entry
= pmd_mkwrite(entry
);
1630 set_pmd_at(mm
, addr
, pmd
, entry
);
1631 BUG_ON(!preserve_write
&& pmd_write(entry
));
1638 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1639 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1641 * Note that if it returns 1, this routine returns without unlocking page
1642 * table locks. So callers must unlock them.
1644 int __pmd_trans_huge_lock(pmd_t
*pmd
, struct vm_area_struct
*vma
,
1647 *ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1648 if (likely(pmd_trans_huge(*pmd
))) {
1649 if (unlikely(pmd_trans_splitting(*pmd
))) {
1651 wait_split_huge_page(vma
->anon_vma
, pmd
);
1654 /* Thp mapped by 'pmd' is stable, so we can
1655 * handle it as it is. */
1664 * This function returns whether a given @page is mapped onto the @address
1665 * in the virtual space of @mm.
1667 * When it's true, this function returns *pmd with holding the page table lock
1668 * and passing it back to the caller via @ptl.
1669 * If it's false, returns NULL without holding the page table lock.
1671 pmd_t
*page_check_address_pmd(struct page
*page
,
1672 struct mm_struct
*mm
,
1673 unsigned long address
,
1674 enum page_check_address_pmd_flag flag
,
1681 if (address
& ~HPAGE_PMD_MASK
)
1684 pgd
= pgd_offset(mm
, address
);
1685 if (!pgd_present(*pgd
))
1687 pud
= pud_offset(pgd
, address
);
1688 if (!pud_present(*pud
))
1690 pmd
= pmd_offset(pud
, address
);
1692 *ptl
= pmd_lock(mm
, pmd
);
1693 if (!pmd_present(*pmd
))
1695 if (pmd_page(*pmd
) != page
)
1698 * split_vma() may create temporary aliased mappings. There is
1699 * no risk as long as all huge pmd are found and have their
1700 * splitting bit set before __split_huge_page_refcount
1701 * runs. Finding the same huge pmd more than once during the
1702 * same rmap walk is not a problem.
1704 if (flag
== PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
&&
1705 pmd_trans_splitting(*pmd
))
1707 if (pmd_trans_huge(*pmd
)) {
1708 VM_BUG_ON(flag
== PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
&&
1709 !pmd_trans_splitting(*pmd
));
1717 static int __split_huge_page_splitting(struct page
*page
,
1718 struct vm_area_struct
*vma
,
1719 unsigned long address
)
1721 struct mm_struct
*mm
= vma
->vm_mm
;
1725 /* For mmu_notifiers */
1726 const unsigned long mmun_start
= address
;
1727 const unsigned long mmun_end
= address
+ HPAGE_PMD_SIZE
;
1729 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
1730 pmd
= page_check_address_pmd(page
, mm
, address
,
1731 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG
, &ptl
);
1734 * We can't temporarily set the pmd to null in order
1735 * to split it, the pmd must remain marked huge at all
1736 * times or the VM won't take the pmd_trans_huge paths
1737 * and it won't wait on the anon_vma->root->rwsem to
1738 * serialize against split_huge_page*.
1740 pmdp_splitting_flush(vma
, address
, pmd
);
1745 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
1750 static void __split_huge_page_refcount(struct page
*page
,
1751 struct list_head
*list
)
1754 struct zone
*zone
= page_zone(page
);
1755 struct lruvec
*lruvec
;
1758 /* prevent PageLRU to go away from under us, and freeze lru stats */
1759 spin_lock_irq(&zone
->lru_lock
);
1760 lruvec
= mem_cgroup_page_lruvec(page
, zone
);
1762 compound_lock(page
);
1763 /* complete memcg works before add pages to LRU */
1764 mem_cgroup_split_huge_fixup(page
);
1766 for (i
= HPAGE_PMD_NR
- 1; i
>= 1; i
--) {
1767 struct page
*page_tail
= page
+ i
;
1769 /* tail_page->_mapcount cannot change */
1770 BUG_ON(page_mapcount(page_tail
) < 0);
1771 tail_count
+= page_mapcount(page_tail
);
1772 /* check for overflow */
1773 BUG_ON(tail_count
< 0);
1774 BUG_ON(atomic_read(&page_tail
->_count
) != 0);
1776 * tail_page->_count is zero and not changing from
1777 * under us. But get_page_unless_zero() may be running
1778 * from under us on the tail_page. If we used
1779 * atomic_set() below instead of atomic_add(), we
1780 * would then run atomic_set() concurrently with
1781 * get_page_unless_zero(), and atomic_set() is
1782 * implemented in C not using locked ops. spin_unlock
1783 * on x86 sometime uses locked ops because of PPro
1784 * errata 66, 92, so unless somebody can guarantee
1785 * atomic_set() here would be safe on all archs (and
1786 * not only on x86), it's safer to use atomic_add().
1788 atomic_add(page_mapcount(page
) + page_mapcount(page_tail
) + 1,
1789 &page_tail
->_count
);
1791 /* after clearing PageTail the gup refcount can be released */
1792 smp_mb__after_atomic();
1794 page_tail
->flags
&= ~PAGE_FLAGS_CHECK_AT_PREP
;
1795 page_tail
->flags
|= (page
->flags
&
1796 ((1L << PG_referenced
) |
1797 (1L << PG_swapbacked
) |
1798 (1L << PG_mlocked
) |
1799 (1L << PG_uptodate
) |
1801 (1L << PG_unevictable
)));
1802 page_tail
->flags
|= (1L << PG_dirty
);
1804 clear_compound_head(page_tail
);
1806 if (page_is_young(page
))
1807 set_page_young(page_tail
);
1808 if (page_is_idle(page
))
1809 set_page_idle(page_tail
);
1812 * __split_huge_page_splitting() already set the
1813 * splitting bit in all pmd that could map this
1814 * hugepage, that will ensure no CPU can alter the
1815 * mapcount on the head page. The mapcount is only
1816 * accounted in the head page and it has to be
1817 * transferred to all tail pages in the below code. So
1818 * for this code to be safe, the split the mapcount
1819 * can't change. But that doesn't mean userland can't
1820 * keep changing and reading the page contents while
1821 * we transfer the mapcount, so the pmd splitting
1822 * status is achieved setting a reserved bit in the
1823 * pmd, not by clearing the present bit.
1825 page_tail
->_mapcount
= page
->_mapcount
;
1827 BUG_ON(page_tail
->mapping
);
1828 page_tail
->mapping
= page
->mapping
;
1830 page_tail
->index
= page
->index
+ i
;
1831 page_cpupid_xchg_last(page_tail
, page_cpupid_last(page
));
1833 BUG_ON(!PageAnon(page_tail
));
1834 BUG_ON(!PageUptodate(page_tail
));
1835 BUG_ON(!PageDirty(page_tail
));
1836 BUG_ON(!PageSwapBacked(page_tail
));
1838 lru_add_page_tail(page
, page_tail
, lruvec
, list
);
1840 atomic_sub(tail_count
, &page
->_count
);
1841 BUG_ON(atomic_read(&page
->_count
) <= 0);
1843 __mod_zone_page_state(zone
, NR_ANON_TRANSPARENT_HUGEPAGES
, -1);
1845 ClearPageCompound(page
);
1846 compound_unlock(page
);
1847 spin_unlock_irq(&zone
->lru_lock
);
1849 for (i
= 1; i
< HPAGE_PMD_NR
; i
++) {
1850 struct page
*page_tail
= page
+ i
;
1851 BUG_ON(page_count(page_tail
) <= 0);
1853 * Tail pages may be freed if there wasn't any mapping
1854 * like if add_to_swap() is running on a lru page that
1855 * had its mapping zapped. And freeing these pages
1856 * requires taking the lru_lock so we do the put_page
1857 * of the tail pages after the split is complete.
1859 put_page(page_tail
);
1863 * Only the head page (now become a regular page) is required
1864 * to be pinned by the caller.
1866 BUG_ON(page_count(page
) <= 0);
1869 static int __split_huge_page_map(struct page
*page
,
1870 struct vm_area_struct
*vma
,
1871 unsigned long address
)
1873 struct mm_struct
*mm
= vma
->vm_mm
;
1878 unsigned long haddr
;
1880 pmd
= page_check_address_pmd(page
, mm
, address
,
1881 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG
, &ptl
);
1883 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
1884 pmd_populate(mm
, &_pmd
, pgtable
);
1885 if (pmd_write(*pmd
))
1886 BUG_ON(page_mapcount(page
) != 1);
1889 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
1891 BUG_ON(PageCompound(page
+i
));
1893 * Note that NUMA hinting access restrictions are not
1894 * transferred to avoid any possibility of altering
1895 * permissions across VMAs.
1897 entry
= mk_pte(page
+ i
, vma
->vm_page_prot
);
1898 entry
= maybe_mkwrite(pte_mkdirty(entry
), vma
);
1899 if (!pmd_write(*pmd
))
1900 entry
= pte_wrprotect(entry
);
1901 if (!pmd_young(*pmd
))
1902 entry
= pte_mkold(entry
);
1903 pte
= pte_offset_map(&_pmd
, haddr
);
1904 BUG_ON(!pte_none(*pte
));
1905 set_pte_at(mm
, haddr
, pte
, entry
);
1909 smp_wmb(); /* make pte visible before pmd */
1911 * Up to this point the pmd is present and huge and
1912 * userland has the whole access to the hugepage
1913 * during the split (which happens in place). If we
1914 * overwrite the pmd with the not-huge version
1915 * pointing to the pte here (which of course we could
1916 * if all CPUs were bug free), userland could trigger
1917 * a small page size TLB miss on the small sized TLB
1918 * while the hugepage TLB entry is still established
1919 * in the huge TLB. Some CPU doesn't like that. See
1920 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1921 * Erratum 383 on page 93. Intel should be safe but is
1922 * also warns that it's only safe if the permission
1923 * and cache attributes of the two entries loaded in
1924 * the two TLB is identical (which should be the case
1925 * here). But it is generally safer to never allow
1926 * small and huge TLB entries for the same virtual
1927 * address to be loaded simultaneously. So instead of
1928 * doing "pmd_populate(); flush_pmd_tlb_range();" we first
1929 * mark the current pmd notpresent (atomically because
1930 * here the pmd_trans_huge and pmd_trans_splitting
1931 * must remain set at all times on the pmd until the
1932 * split is complete for this pmd), then we flush the
1933 * SMP TLB and finally we write the non-huge version
1934 * of the pmd entry with pmd_populate.
1936 pmdp_invalidate(vma
, address
, pmd
);
1937 pmd_populate(mm
, pmd
, pgtable
);
1945 /* must be called with anon_vma->root->rwsem held */
1946 static void __split_huge_page(struct page
*page
,
1947 struct anon_vma
*anon_vma
,
1948 struct list_head
*list
)
1950 int mapcount
, mapcount2
;
1951 pgoff_t pgoff
= page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
1952 struct anon_vma_chain
*avc
;
1954 BUG_ON(!PageHead(page
));
1955 BUG_ON(PageTail(page
));
1958 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1959 struct vm_area_struct
*vma
= avc
->vma
;
1960 unsigned long addr
= vma_address(page
, vma
);
1961 BUG_ON(is_vma_temporary_stack(vma
));
1962 mapcount
+= __split_huge_page_splitting(page
, vma
, addr
);
1965 * It is critical that new vmas are added to the tail of the
1966 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1967 * and establishes a child pmd before
1968 * __split_huge_page_splitting() freezes the parent pmd (so if
1969 * we fail to prevent copy_huge_pmd() from running until the
1970 * whole __split_huge_page() is complete), we will still see
1971 * the newly established pmd of the child later during the
1972 * walk, to be able to set it as pmd_trans_splitting too.
1974 if (mapcount
!= page_mapcount(page
)) {
1975 pr_err("mapcount %d page_mapcount %d\n",
1976 mapcount
, page_mapcount(page
));
1980 __split_huge_page_refcount(page
, list
);
1983 anon_vma_interval_tree_foreach(avc
, &anon_vma
->rb_root
, pgoff
, pgoff
) {
1984 struct vm_area_struct
*vma
= avc
->vma
;
1985 unsigned long addr
= vma_address(page
, vma
);
1986 BUG_ON(is_vma_temporary_stack(vma
));
1987 mapcount2
+= __split_huge_page_map(page
, vma
, addr
);
1989 if (mapcount
!= mapcount2
) {
1990 pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
1991 mapcount
, mapcount2
, page_mapcount(page
));
1997 * Split a hugepage into normal pages. This doesn't change the position of head
1998 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1999 * @list. Both head page and tail pages will inherit mapping, flags, and so on
2000 * from the hugepage.
2001 * Return 0 if the hugepage is split successfully otherwise return 1.
2003 int split_huge_page_to_list(struct page
*page
, struct list_head
*list
)
2005 struct anon_vma
*anon_vma
;
2008 BUG_ON(is_huge_zero_page(page
));
2009 BUG_ON(!PageAnon(page
));
2012 * The caller does not necessarily hold an mmap_sem that would prevent
2013 * the anon_vma disappearing so we first we take a reference to it
2014 * and then lock the anon_vma for write. This is similar to
2015 * page_lock_anon_vma_read except the write lock is taken to serialise
2016 * against parallel split or collapse operations.
2018 anon_vma
= page_get_anon_vma(page
);
2021 anon_vma_lock_write(anon_vma
);
2024 if (!PageCompound(page
))
2027 BUG_ON(!PageSwapBacked(page
));
2028 __split_huge_page(page
, anon_vma
, list
);
2029 count_vm_event(THP_SPLIT
);
2031 BUG_ON(PageCompound(page
));
2033 anon_vma_unlock_write(anon_vma
);
2034 put_anon_vma(anon_vma
);
2039 #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
2041 int hugepage_madvise(struct vm_area_struct
*vma
,
2042 unsigned long *vm_flags
, int advice
)
2048 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
2049 * can't handle this properly after s390_enable_sie, so we simply
2050 * ignore the madvise to prevent qemu from causing a SIGSEGV.
2052 if (mm_has_pgste(vma
->vm_mm
))
2056 * Be somewhat over-protective like KSM for now!
2058 if (*vm_flags
& VM_NO_THP
)
2060 *vm_flags
&= ~VM_NOHUGEPAGE
;
2061 *vm_flags
|= VM_HUGEPAGE
;
2063 * If the vma become good for khugepaged to scan,
2064 * register it here without waiting a page fault that
2065 * may not happen any time soon.
2067 if (unlikely(khugepaged_enter_vma_merge(vma
, *vm_flags
)))
2070 case MADV_NOHUGEPAGE
:
2072 * Be somewhat over-protective like KSM for now!
2074 if (*vm_flags
& VM_NO_THP
)
2076 *vm_flags
&= ~VM_HUGEPAGE
;
2077 *vm_flags
|= VM_NOHUGEPAGE
;
2079 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
2080 * this vma even if we leave the mm registered in khugepaged if
2081 * it got registered before VM_NOHUGEPAGE was set.
2089 static int __init
khugepaged_slab_init(void)
2091 mm_slot_cache
= kmem_cache_create("khugepaged_mm_slot",
2092 sizeof(struct mm_slot
),
2093 __alignof__(struct mm_slot
), 0, NULL
);
2100 static void __init
khugepaged_slab_exit(void)
2102 kmem_cache_destroy(mm_slot_cache
);
2105 static inline struct mm_slot
*alloc_mm_slot(void)
2107 if (!mm_slot_cache
) /* initialization failed */
2109 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
2112 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
2114 kmem_cache_free(mm_slot_cache
, mm_slot
);
2117 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
2119 struct mm_slot
*mm_slot
;
2121 hash_for_each_possible(mm_slots_hash
, mm_slot
, hash
, (unsigned long)mm
)
2122 if (mm
== mm_slot
->mm
)
2128 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
2129 struct mm_slot
*mm_slot
)
2132 hash_add(mm_slots_hash
, &mm_slot
->hash
, (long)mm
);
2135 static inline int khugepaged_test_exit(struct mm_struct
*mm
)
2137 return atomic_read(&mm
->mm_users
) == 0 || !mmget_still_valid(mm
);
2140 int __khugepaged_enter(struct mm_struct
*mm
)
2142 struct mm_slot
*mm_slot
;
2145 mm_slot
= alloc_mm_slot();
2149 /* __khugepaged_exit() must not run from under us */
2150 VM_BUG_ON_MM(atomic_read(&mm
->mm_users
) == 0, mm
);
2151 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE
, &mm
->flags
))) {
2152 free_mm_slot(mm_slot
);
2156 spin_lock(&khugepaged_mm_lock
);
2157 insert_to_mm_slots_hash(mm
, mm_slot
);
2159 * Insert just behind the scanning cursor, to let the area settle
2162 wakeup
= list_empty(&khugepaged_scan
.mm_head
);
2163 list_add_tail(&mm_slot
->mm_node
, &khugepaged_scan
.mm_head
);
2164 spin_unlock(&khugepaged_mm_lock
);
2166 atomic_inc(&mm
->mm_count
);
2168 wake_up_interruptible(&khugepaged_wait
);
2173 int khugepaged_enter_vma_merge(struct vm_area_struct
*vma
,
2174 unsigned long vm_flags
)
2176 unsigned long hstart
, hend
;
2179 * Not yet faulted in so we will register later in the
2180 * page fault if needed.
2183 if (vma
->vm_ops
|| (vm_flags
& VM_NO_THP
))
2184 /* khugepaged not yet working on file or special mappings */
2186 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2187 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2189 return khugepaged_enter(vma
, vm_flags
);
2193 void __khugepaged_exit(struct mm_struct
*mm
)
2195 struct mm_slot
*mm_slot
;
2198 spin_lock(&khugepaged_mm_lock
);
2199 mm_slot
= get_mm_slot(mm
);
2200 if (mm_slot
&& khugepaged_scan
.mm_slot
!= mm_slot
) {
2201 hash_del(&mm_slot
->hash
);
2202 list_del(&mm_slot
->mm_node
);
2205 spin_unlock(&khugepaged_mm_lock
);
2208 clear_bit(MMF_VM_HUGEPAGE
, &mm
->flags
);
2209 free_mm_slot(mm_slot
);
2211 } else if (mm_slot
) {
2213 * This is required to serialize against
2214 * khugepaged_test_exit() (which is guaranteed to run
2215 * under mmap sem read mode). Stop here (after we
2216 * return all pagetables will be destroyed) until
2217 * khugepaged has finished working on the pagetables
2218 * under the mmap_sem.
2220 down_write(&mm
->mmap_sem
);
2221 up_write(&mm
->mmap_sem
);
2225 static void release_pte_page(struct page
*page
)
2227 /* 0 stands for page_is_file_cache(page) == false */
2228 dec_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2230 putback_lru_page(page
);
2233 static void release_pte_pages(pte_t
*pte
, pte_t
*_pte
)
2235 while (--_pte
>= pte
) {
2236 pte_t pteval
= *_pte
;
2237 if (!pte_none(pteval
) && !is_zero_pfn(pte_pfn(pteval
)))
2238 release_pte_page(pte_page(pteval
));
2242 static int __collapse_huge_page_isolate(struct vm_area_struct
*vma
,
2243 unsigned long address
,
2248 int none_or_zero
= 0;
2249 bool referenced
= false, writable
= false;
2250 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2251 _pte
++, address
+= PAGE_SIZE
) {
2252 pte_t pteval
= *_pte
;
2253 if (pte_none(pteval
) || (pte_present(pteval
) &&
2254 is_zero_pfn(pte_pfn(pteval
)))) {
2255 if (!userfaultfd_armed(vma
) &&
2256 ++none_or_zero
<= khugepaged_max_ptes_none
)
2261 if (!pte_present(pteval
))
2263 page
= vm_normal_page(vma
, address
, pteval
);
2264 if (unlikely(!page
))
2267 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2268 VM_BUG_ON_PAGE(!PageAnon(page
), page
);
2269 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
2272 * We can do it before isolate_lru_page because the
2273 * page can't be freed from under us. NOTE: PG_lock
2274 * is needed to serialize against split_huge_page
2275 * when invoked from the VM.
2277 if (!trylock_page(page
))
2281 * cannot use mapcount: can't collapse if there's a gup pin.
2282 * The page must only be referenced by the scanned process
2283 * and page swap cache.
2285 if (page_count(page
) != 1 + !!PageSwapCache(page
)) {
2289 if (pte_write(pteval
)) {
2292 if (PageSwapCache(page
) && !reuse_swap_page(page
)) {
2297 * Page is not in the swap cache. It can be collapsed
2303 * Isolate the page to avoid collapsing an hugepage
2304 * currently in use by the VM.
2306 if (isolate_lru_page(page
)) {
2310 /* 0 stands for page_is_file_cache(page) == false */
2311 inc_zone_page_state(page
, NR_ISOLATED_ANON
+ 0);
2312 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
2313 VM_BUG_ON_PAGE(PageLRU(page
), page
);
2315 /* If there is no mapped pte young don't collapse the page */
2316 if (pte_young(pteval
) ||
2317 page_is_young(page
) || PageReferenced(page
) ||
2318 mmu_notifier_test_young(vma
->vm_mm
, address
))
2321 if (likely(referenced
&& writable
))
2324 release_pte_pages(pte
, _pte
);
2328 static void __collapse_huge_page_copy(pte_t
*pte
, struct page
*page
,
2329 struct vm_area_struct
*vma
,
2330 unsigned long address
,
2334 for (_pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
; _pte
++) {
2335 pte_t pteval
= *_pte
;
2336 struct page
*src_page
;
2338 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2339 clear_user_highpage(page
, address
);
2340 add_mm_counter(vma
->vm_mm
, MM_ANONPAGES
, 1);
2341 if (is_zero_pfn(pte_pfn(pteval
))) {
2343 * ptl mostly unnecessary.
2347 * paravirt calls inside pte_clear here are
2350 pte_clear(vma
->vm_mm
, address
, _pte
);
2354 src_page
= pte_page(pteval
);
2355 copy_user_highpage(page
, src_page
, address
, vma
);
2356 VM_BUG_ON_PAGE(page_mapcount(src_page
) != 1, src_page
);
2357 release_pte_page(src_page
);
2359 * ptl mostly unnecessary, but preempt has to
2360 * be disabled to update the per-cpu stats
2361 * inside page_remove_rmap().
2365 * paravirt calls inside pte_clear here are
2368 pte_clear(vma
->vm_mm
, address
, _pte
);
2369 page_remove_rmap(src_page
);
2371 free_page_and_swap_cache(src_page
);
2374 address
+= PAGE_SIZE
;
2379 static void khugepaged_alloc_sleep(void)
2383 add_wait_queue(&khugepaged_wait
, &wait
);
2384 freezable_schedule_timeout_interruptible(
2385 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs
));
2386 remove_wait_queue(&khugepaged_wait
, &wait
);
2389 static int khugepaged_node_load
[MAX_NUMNODES
];
2391 static bool khugepaged_scan_abort(int nid
)
2396 * If zone_reclaim_mode is disabled, then no extra effort is made to
2397 * allocate memory locally.
2399 if (!zone_reclaim_mode
)
2402 /* If there is a count for this node already, it must be acceptable */
2403 if (khugepaged_node_load
[nid
])
2406 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
2407 if (!khugepaged_node_load
[i
])
2409 if (node_distance(nid
, i
) > RECLAIM_DISTANCE
)
2416 static int khugepaged_find_target_node(void)
2418 static int last_khugepaged_target_node
= NUMA_NO_NODE
;
2419 int nid
, target_node
= 0, max_value
= 0;
2421 /* find first node with max normal pages hit */
2422 for (nid
= 0; nid
< MAX_NUMNODES
; nid
++)
2423 if (khugepaged_node_load
[nid
] > max_value
) {
2424 max_value
= khugepaged_node_load
[nid
];
2428 /* do some balance if several nodes have the same hit record */
2429 if (target_node
<= last_khugepaged_target_node
)
2430 for (nid
= last_khugepaged_target_node
+ 1; nid
< MAX_NUMNODES
;
2432 if (max_value
== khugepaged_node_load
[nid
]) {
2437 last_khugepaged_target_node
= target_node
;
2441 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2443 if (IS_ERR(*hpage
)) {
2449 khugepaged_alloc_sleep();
2450 } else if (*hpage
) {
2458 static struct page
*
2459 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2460 unsigned long address
, int node
)
2462 VM_BUG_ON_PAGE(*hpage
, *hpage
);
2465 * Before allocating the hugepage, release the mmap_sem read lock.
2466 * The allocation can take potentially a long time if it involves
2467 * sync compaction, and we do not need to hold the mmap_sem during
2468 * that. We will recheck the vma after taking it again in write mode.
2470 up_read(&mm
->mmap_sem
);
2472 *hpage
= __alloc_pages_node(node
, gfp
, HPAGE_PMD_ORDER
);
2473 if (unlikely(!*hpage
)) {
2474 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2475 *hpage
= ERR_PTR(-ENOMEM
);
2479 count_vm_event(THP_COLLAPSE_ALLOC
);
2483 static int khugepaged_find_target_node(void)
2488 static inline struct page
*alloc_hugepage(int defrag
)
2490 return alloc_pages(alloc_hugepage_gfpmask(defrag
, 0),
2494 static struct page
*khugepaged_alloc_hugepage(bool *wait
)
2499 hpage
= alloc_hugepage(khugepaged_defrag());
2501 count_vm_event(THP_COLLAPSE_ALLOC_FAILED
);
2506 khugepaged_alloc_sleep();
2508 count_vm_event(THP_COLLAPSE_ALLOC
);
2509 } while (unlikely(!hpage
) && likely(khugepaged_enabled()));
2514 static bool khugepaged_prealloc_page(struct page
**hpage
, bool *wait
)
2517 *hpage
= khugepaged_alloc_hugepage(wait
);
2519 if (unlikely(!*hpage
))
2525 static struct page
*
2526 khugepaged_alloc_page(struct page
**hpage
, gfp_t gfp
, struct mm_struct
*mm
,
2527 unsigned long address
, int node
)
2529 up_read(&mm
->mmap_sem
);
2536 static bool hugepage_vma_check(struct vm_area_struct
*vma
)
2538 if ((!(vma
->vm_flags
& VM_HUGEPAGE
) && !khugepaged_always()) ||
2539 (vma
->vm_flags
& VM_NOHUGEPAGE
))
2542 if (!vma
->anon_vma
|| vma
->vm_ops
)
2544 if (is_vma_temporary_stack(vma
))
2546 return !(vma
->vm_flags
& VM_NO_THP
);
2549 static void collapse_huge_page(struct mm_struct
*mm
,
2550 unsigned long address
,
2551 struct page
**hpage
,
2552 struct vm_area_struct
*vma
,
2558 struct page
*new_page
;
2559 spinlock_t
*pmd_ptl
, *pte_ptl
;
2561 unsigned long hstart
, hend
;
2562 struct mem_cgroup
*memcg
;
2563 unsigned long mmun_start
; /* For mmu_notifiers */
2564 unsigned long mmun_end
; /* For mmu_notifiers */
2567 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2569 /* Only allocate from the target node */
2570 gfp
= alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE
) |
2573 /* release the mmap_sem read lock. */
2574 new_page
= khugepaged_alloc_page(hpage
, gfp
, mm
, address
, node
);
2578 if (unlikely(mem_cgroup_try_charge(new_page
, mm
,
2583 * Prevent all access to pagetables with the exception of
2584 * gup_fast later hanlded by the ptep_clear_flush and the VM
2585 * handled by the anon_vma lock + PG_lock.
2587 down_write(&mm
->mmap_sem
);
2588 if (unlikely(khugepaged_test_exit(mm
)))
2591 vma
= find_vma(mm
, address
);
2594 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2595 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2596 if (address
< hstart
|| address
+ HPAGE_PMD_SIZE
> hend
)
2598 if (!hugepage_vma_check(vma
))
2600 pmd
= mm_find_pmd(mm
, address
);
2604 anon_vma_lock_write(vma
->anon_vma
);
2606 pte
= pte_offset_map(pmd
, address
);
2607 pte_ptl
= pte_lockptr(mm
, pmd
);
2609 mmun_start
= address
;
2610 mmun_end
= address
+ HPAGE_PMD_SIZE
;
2611 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
2612 pmd_ptl
= pmd_lock(mm
, pmd
); /* probably unnecessary */
2614 * After this gup_fast can't run anymore. This also removes
2615 * any huge TLB entry from the CPU so we won't allow
2616 * huge and small TLB entries for the same virtual address
2617 * to avoid the risk of CPU bugs in that area.
2619 _pmd
= pmdp_collapse_flush(vma
, address
, pmd
);
2620 spin_unlock(pmd_ptl
);
2621 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
2624 isolated
= __collapse_huge_page_isolate(vma
, address
, pte
);
2625 spin_unlock(pte_ptl
);
2627 if (unlikely(!isolated
)) {
2630 BUG_ON(!pmd_none(*pmd
));
2632 * We can only use set_pmd_at when establishing
2633 * hugepmds and never for establishing regular pmds that
2634 * points to regular pagetables. Use pmd_populate for that
2636 pmd_populate(mm
, pmd
, pmd_pgtable(_pmd
));
2637 spin_unlock(pmd_ptl
);
2638 anon_vma_unlock_write(vma
->anon_vma
);
2643 * All pages are isolated and locked so anon_vma rmap
2644 * can't run anymore.
2646 anon_vma_unlock_write(vma
->anon_vma
);
2648 __collapse_huge_page_copy(pte
, new_page
, vma
, address
, pte_ptl
);
2650 __SetPageUptodate(new_page
);
2651 pgtable
= pmd_pgtable(_pmd
);
2653 _pmd
= mk_huge_pmd(new_page
, vma
->vm_page_prot
);
2654 _pmd
= maybe_pmd_mkwrite(pmd_mkdirty(_pmd
), vma
);
2657 * spin_lock() below is not the equivalent of smp_wmb(), so
2658 * this is needed to avoid the copy_huge_page writes to become
2659 * visible after the set_pmd_at() write.
2664 BUG_ON(!pmd_none(*pmd
));
2665 page_add_new_anon_rmap(new_page
, vma
, address
);
2666 mem_cgroup_commit_charge(new_page
, memcg
, false);
2667 lru_cache_add_active_or_unevictable(new_page
, vma
);
2668 pgtable_trans_huge_deposit(mm
, pmd
, pgtable
);
2669 set_pmd_at(mm
, address
, pmd
, _pmd
);
2670 update_mmu_cache_pmd(vma
, address
, pmd
);
2671 spin_unlock(pmd_ptl
);
2675 khugepaged_pages_collapsed
++;
2677 up_write(&mm
->mmap_sem
);
2681 mem_cgroup_cancel_charge(new_page
, memcg
);
2685 static int khugepaged_scan_pmd(struct mm_struct
*mm
,
2686 struct vm_area_struct
*vma
,
2687 unsigned long address
,
2688 struct page
**hpage
)
2692 int ret
= 0, none_or_zero
= 0;
2694 unsigned long _address
;
2696 int node
= NUMA_NO_NODE
;
2697 bool writable
= false, referenced
= false;
2699 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
2701 pmd
= mm_find_pmd(mm
, address
);
2705 memset(khugepaged_node_load
, 0, sizeof(khugepaged_node_load
));
2706 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
2707 for (_address
= address
, _pte
= pte
; _pte
< pte
+HPAGE_PMD_NR
;
2708 _pte
++, _address
+= PAGE_SIZE
) {
2709 pte_t pteval
= *_pte
;
2710 if (pte_none(pteval
) || is_zero_pfn(pte_pfn(pteval
))) {
2711 if (!userfaultfd_armed(vma
) &&
2712 ++none_or_zero
<= khugepaged_max_ptes_none
)
2717 if (!pte_present(pteval
))
2719 if (pte_write(pteval
))
2722 page
= vm_normal_page(vma
, _address
, pteval
);
2723 if (unlikely(!page
))
2726 * Record which node the original page is from and save this
2727 * information to khugepaged_node_load[].
2728 * Khupaged will allocate hugepage from the node has the max
2731 node
= page_to_nid(page
);
2732 if (khugepaged_scan_abort(node
))
2734 khugepaged_node_load
[node
]++;
2735 VM_BUG_ON_PAGE(PageCompound(page
), page
);
2736 if (!PageLRU(page
) || PageLocked(page
) || !PageAnon(page
))
2739 * cannot use mapcount: can't collapse if there's a gup pin.
2740 * The page must only be referenced by the scanned process
2741 * and page swap cache.
2743 if (page_count(page
) != 1 + !!PageSwapCache(page
))
2745 if (pte_young(pteval
) ||
2746 page_is_young(page
) || PageReferenced(page
) ||
2747 mmu_notifier_test_young(vma
->vm_mm
, address
))
2750 if (referenced
&& writable
)
2753 pte_unmap_unlock(pte
, ptl
);
2755 node
= khugepaged_find_target_node();
2756 /* collapse_huge_page will return with the mmap_sem released */
2757 collapse_huge_page(mm
, address
, hpage
, vma
, node
);
2763 static void collect_mm_slot(struct mm_slot
*mm_slot
)
2765 struct mm_struct
*mm
= mm_slot
->mm
;
2767 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2769 if (khugepaged_test_exit(mm
)) {
2771 hash_del(&mm_slot
->hash
);
2772 list_del(&mm_slot
->mm_node
);
2775 * Not strictly needed because the mm exited already.
2777 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2780 /* khugepaged_mm_lock actually not necessary for the below */
2781 free_mm_slot(mm_slot
);
2786 static unsigned int khugepaged_scan_mm_slot(unsigned int pages
,
2787 struct page
**hpage
)
2788 __releases(&khugepaged_mm_lock
)
2789 __acquires(&khugepaged_mm_lock
)
2791 struct mm_slot
*mm_slot
;
2792 struct mm_struct
*mm
;
2793 struct vm_area_struct
*vma
;
2797 VM_BUG_ON(NR_CPUS
!= 1 && !spin_is_locked(&khugepaged_mm_lock
));
2799 if (khugepaged_scan
.mm_slot
)
2800 mm_slot
= khugepaged_scan
.mm_slot
;
2802 mm_slot
= list_entry(khugepaged_scan
.mm_head
.next
,
2803 struct mm_slot
, mm_node
);
2804 khugepaged_scan
.address
= 0;
2805 khugepaged_scan
.mm_slot
= mm_slot
;
2807 spin_unlock(&khugepaged_mm_lock
);
2810 down_read(&mm
->mmap_sem
);
2811 if (unlikely(khugepaged_test_exit(mm
)))
2814 vma
= find_vma(mm
, khugepaged_scan
.address
);
2817 for (; vma
; vma
= vma
->vm_next
) {
2818 unsigned long hstart
, hend
;
2821 if (unlikely(khugepaged_test_exit(mm
))) {
2825 if (!hugepage_vma_check(vma
)) {
2830 hstart
= (vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
;
2831 hend
= vma
->vm_end
& HPAGE_PMD_MASK
;
2834 if (khugepaged_scan
.address
> hend
)
2836 if (khugepaged_scan
.address
< hstart
)
2837 khugepaged_scan
.address
= hstart
;
2838 VM_BUG_ON(khugepaged_scan
.address
& ~HPAGE_PMD_MASK
);
2840 while (khugepaged_scan
.address
< hend
) {
2843 if (unlikely(khugepaged_test_exit(mm
)))
2844 goto breakouterloop
;
2846 VM_BUG_ON(khugepaged_scan
.address
< hstart
||
2847 khugepaged_scan
.address
+ HPAGE_PMD_SIZE
>
2849 ret
= khugepaged_scan_pmd(mm
, vma
,
2850 khugepaged_scan
.address
,
2852 /* move to next address */
2853 khugepaged_scan
.address
+= HPAGE_PMD_SIZE
;
2854 progress
+= HPAGE_PMD_NR
;
2856 /* we released mmap_sem so break loop */
2857 goto breakouterloop_mmap_sem
;
2858 if (progress
>= pages
)
2859 goto breakouterloop
;
2863 up_read(&mm
->mmap_sem
); /* exit_mmap will destroy ptes after this */
2864 breakouterloop_mmap_sem
:
2866 spin_lock(&khugepaged_mm_lock
);
2867 VM_BUG_ON(khugepaged_scan
.mm_slot
!= mm_slot
);
2869 * Release the current mm_slot if this mm is about to die, or
2870 * if we scanned all vmas of this mm.
2872 if (khugepaged_test_exit(mm
) || !vma
) {
2874 * Make sure that if mm_users is reaching zero while
2875 * khugepaged runs here, khugepaged_exit will find
2876 * mm_slot not pointing to the exiting mm.
2878 if (mm_slot
->mm_node
.next
!= &khugepaged_scan
.mm_head
) {
2879 khugepaged_scan
.mm_slot
= list_entry(
2880 mm_slot
->mm_node
.next
,
2881 struct mm_slot
, mm_node
);
2882 khugepaged_scan
.address
= 0;
2884 khugepaged_scan
.mm_slot
= NULL
;
2885 khugepaged_full_scans
++;
2888 collect_mm_slot(mm_slot
);
2894 static int khugepaged_has_work(void)
2896 return !list_empty(&khugepaged_scan
.mm_head
) &&
2897 khugepaged_enabled();
2900 static int khugepaged_wait_event(void)
2902 return !list_empty(&khugepaged_scan
.mm_head
) ||
2903 kthread_should_stop();
2906 static void khugepaged_do_scan(void)
2908 struct page
*hpage
= NULL
;
2909 unsigned int progress
= 0, pass_through_head
= 0;
2910 unsigned int pages
= khugepaged_pages_to_scan
;
2913 barrier(); /* write khugepaged_pages_to_scan to local stack */
2915 while (progress
< pages
) {
2916 if (!khugepaged_prealloc_page(&hpage
, &wait
))
2921 if (unlikely(kthread_should_stop() || try_to_freeze()))
2924 spin_lock(&khugepaged_mm_lock
);
2925 if (!khugepaged_scan
.mm_slot
)
2926 pass_through_head
++;
2927 if (khugepaged_has_work() &&
2928 pass_through_head
< 2)
2929 progress
+= khugepaged_scan_mm_slot(pages
- progress
,
2933 spin_unlock(&khugepaged_mm_lock
);
2936 if (!IS_ERR_OR_NULL(hpage
))
2940 static void khugepaged_wait_work(void)
2942 if (khugepaged_has_work()) {
2943 if (!khugepaged_scan_sleep_millisecs
)
2946 wait_event_freezable_timeout(khugepaged_wait
,
2947 kthread_should_stop(),
2948 msecs_to_jiffies(khugepaged_scan_sleep_millisecs
));
2952 if (khugepaged_enabled())
2953 wait_event_freezable(khugepaged_wait
, khugepaged_wait_event());
2956 static int khugepaged(void *none
)
2958 struct mm_slot
*mm_slot
;
2961 set_user_nice(current
, MAX_NICE
);
2963 while (!kthread_should_stop()) {
2964 khugepaged_do_scan();
2965 khugepaged_wait_work();
2968 spin_lock(&khugepaged_mm_lock
);
2969 mm_slot
= khugepaged_scan
.mm_slot
;
2970 khugepaged_scan
.mm_slot
= NULL
;
2972 collect_mm_slot(mm_slot
);
2973 spin_unlock(&khugepaged_mm_lock
);
2977 static void __split_huge_zero_page_pmd(struct vm_area_struct
*vma
,
2978 unsigned long haddr
, pmd_t
*pmd
)
2980 struct mm_struct
*mm
= vma
->vm_mm
;
2985 pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
2986 /* leave pmd empty until pte is filled */
2988 pgtable
= pgtable_trans_huge_withdraw(mm
, pmd
);
2989 pmd_populate(mm
, &_pmd
, pgtable
);
2991 for (i
= 0; i
< HPAGE_PMD_NR
; i
++, haddr
+= PAGE_SIZE
) {
2993 entry
= pfn_pte(my_zero_pfn(haddr
), vma
->vm_page_prot
);
2994 entry
= pte_mkspecial(entry
);
2995 pte
= pte_offset_map(&_pmd
, haddr
);
2996 VM_BUG_ON(!pte_none(*pte
));
2997 set_pte_at(mm
, haddr
, pte
, entry
);
3000 smp_wmb(); /* make pte visible before pmd */
3001 pmd_populate(mm
, pmd
, pgtable
);
3002 put_huge_zero_page();
3005 void __split_huge_page_pmd(struct vm_area_struct
*vma
, unsigned long address
,
3009 struct page
*page
= NULL
;
3010 struct mm_struct
*mm
= vma
->vm_mm
;
3011 unsigned long haddr
= address
& HPAGE_PMD_MASK
;
3012 unsigned long mmun_start
; /* For mmu_notifiers */
3013 unsigned long mmun_end
; /* For mmu_notifiers */
3015 BUG_ON(vma
->vm_start
> haddr
|| vma
->vm_end
< haddr
+ HPAGE_PMD_SIZE
);
3018 mmun_end
= haddr
+ HPAGE_PMD_SIZE
;
3020 mmu_notifier_invalidate_range_start(mm
, mmun_start
, mmun_end
);
3021 ptl
= pmd_lock(mm
, pmd
);
3022 if (unlikely(!pmd_trans_huge(*pmd
)))
3024 if (vma_is_dax(vma
)) {
3025 pmd_t _pmd
= pmdp_huge_clear_flush_notify(vma
, haddr
, pmd
);
3026 if (is_huge_zero_pmd(_pmd
))
3027 put_huge_zero_page();
3028 } else if (is_huge_zero_pmd(*pmd
)) {
3029 __split_huge_zero_page_pmd(vma
, haddr
, pmd
);
3031 page
= pmd_page(*pmd
);
3032 VM_BUG_ON_PAGE(!page_count(page
), page
);
3037 mmu_notifier_invalidate_range_end(mm
, mmun_start
, mmun_end
);
3042 split_huge_page(page
);
3046 * We don't always have down_write of mmap_sem here: a racing
3047 * do_huge_pmd_wp_page() might have copied-on-write to another
3048 * huge page before our split_huge_page() got the anon_vma lock.
3050 if (unlikely(pmd_trans_huge(*pmd
)))
3054 void split_huge_page_pmd_mm(struct mm_struct
*mm
, unsigned long address
,
3057 struct vm_area_struct
*vma
;
3059 vma
= find_vma(mm
, address
);
3060 BUG_ON(vma
== NULL
);
3061 split_huge_page_pmd(vma
, address
, pmd
);
3064 static void split_huge_page_address(struct mm_struct
*mm
,
3065 unsigned long address
)
3071 VM_BUG_ON(!(address
& ~HPAGE_PMD_MASK
));
3073 pgd
= pgd_offset(mm
, address
);
3074 if (!pgd_present(*pgd
))
3077 pud
= pud_offset(pgd
, address
);
3078 if (!pud_present(*pud
))
3081 pmd
= pmd_offset(pud
, address
);
3082 if (!pmd_present(*pmd
))
3085 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3086 * materialize from under us.
3088 split_huge_page_pmd_mm(mm
, address
, pmd
);
3091 void vma_adjust_trans_huge(struct vm_area_struct
*vma
,
3092 unsigned long start
,
3097 * If the new start address isn't hpage aligned and it could
3098 * previously contain an hugepage: check if we need to split
3101 if (start
& ~HPAGE_PMD_MASK
&&
3102 (start
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3103 (start
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3104 split_huge_page_address(vma
->vm_mm
, start
);
3107 * If the new end address isn't hpage aligned and it could
3108 * previously contain an hugepage: check if we need to split
3111 if (end
& ~HPAGE_PMD_MASK
&&
3112 (end
& HPAGE_PMD_MASK
) >= vma
->vm_start
&&
3113 (end
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= vma
->vm_end
)
3114 split_huge_page_address(vma
->vm_mm
, end
);
3117 * If we're also updating the vma->vm_next->vm_start, if the new
3118 * vm_next->vm_start isn't page aligned and it could previously
3119 * contain an hugepage: check if we need to split an huge pmd.
3121 if (adjust_next
> 0) {
3122 struct vm_area_struct
*next
= vma
->vm_next
;
3123 unsigned long nstart
= next
->vm_start
;
3124 nstart
+= adjust_next
<< PAGE_SHIFT
;
3125 if (nstart
& ~HPAGE_PMD_MASK
&&
3126 (nstart
& HPAGE_PMD_MASK
) >= next
->vm_start
&&
3127 (nstart
& HPAGE_PMD_MASK
) + HPAGE_PMD_SIZE
<= next
->vm_end
)
3128 split_huge_page_address(next
->vm_mm
, nstart
);