]> git.ipfire.org Git - people/ms/linux.git/blame - mm/huge_memory.c
ARM: head.S: rename PMD_ORDER to PMD_ENTRY_ORDER
[people/ms/linux.git] / mm / huge_memory.c
CommitLineData
20c8ccb1 1// SPDX-License-Identifier: GPL-2.0-only
71e3aac0
AA
2/*
3 * Copyright (C) 2009 Red Hat, Inc.
71e3aac0
AA
4 */
5
ae3a8c1c
AM
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
71e3aac0
AA
8#include <linux/mm.h>
9#include <linux/sched.h>
fa6c0231 10#include <linux/sched/mm.h>
f7ccbae4 11#include <linux/sched/coredump.h>
6a3827d7 12#include <linux/sched/numa_balancing.h>
71e3aac0
AA
13#include <linux/highmem.h>
14#include <linux/hugetlb.h>
15#include <linux/mmu_notifier.h>
16#include <linux/rmap.h>
17#include <linux/swap.h>
97ae1749 18#include <linux/shrinker.h>
ba76149f 19#include <linux/mm_inline.h>
e9b61f19 20#include <linux/swapops.h>
4897c765 21#include <linux/dax.h>
ba76149f 22#include <linux/khugepaged.h>
878aee7d 23#include <linux/freezer.h>
f25748e3 24#include <linux/pfn_t.h>
a664b2d8 25#include <linux/mman.h>
3565fce3 26#include <linux/memremap.h>
325adeb5 27#include <linux/pagemap.h>
49071d43 28#include <linux/debugfs.h>
4daae3b4 29#include <linux/migrate.h>
43b5fbbd 30#include <linux/hashtable.h>
6b251fc9 31#include <linux/userfaultfd_k.h>
33c3fc71 32#include <linux/page_idle.h>
baa355fd 33#include <linux/shmem_fs.h>
6b31d595 34#include <linux/oom.h>
98fa15f3 35#include <linux/numa.h>
f7da677b 36#include <linux/page_owner.h>
a1a3a2fc 37#include <linux/sched/sysctl.h>
97ae1749 38
71e3aac0
AA
39#include <asm/tlb.h>
40#include <asm/pgalloc.h>
41#include "internal.h"
014bb1de 42#include "swap.h"
71e3aac0 43
283fd6fe
AK
44#define CREATE_TRACE_POINTS
45#include <trace/events/thp.h>
46
ba76149f 47/*
b14d595a
MD
48 * By default, transparent hugepage support is disabled in order to avoid
49 * risking an increased memory footprint for applications that are not
50 * guaranteed to benefit from it. When transparent hugepage support is
51 * enabled, it is for all mappings, and khugepaged scans all mappings.
8bfa3f9a
JW
52 * Defrag is invoked by khugepaged hugepage allocations and by page faults
53 * for all hugepage allocations.
ba76149f 54 */
71e3aac0 55unsigned long transparent_hugepage_flags __read_mostly =
13ece886 56#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
ba76149f 57 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
13ece886
AA
58#endif
59#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
60 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
61#endif
444eb2a4 62 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
79da5407
KS
63 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
64 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
ba76149f 65
9a982250 66static struct shrinker deferred_split_shrinker;
f000565a 67
97ae1749 68static atomic_t huge_zero_refcount;
56873f43 69struct page *huge_zero_page __read_mostly;
3b77e8c8 70unsigned long huge_zero_pfn __read_mostly = ~0UL;
4a6c1297 71
9fec5168
YS
72bool hugepage_vma_check(struct vm_area_struct *vma,
73 unsigned long vm_flags,
7da4e2cb 74 bool smaps, bool in_pf)
7635d9cb 75{
9fec5168
YS
76 if (!vma->vm_mm) /* vdso */
77 return false;
78
7da4e2cb
YS
79 /*
80 * Explicitly disabled through madvise or prctl, or some
81 * architectures may disable THP for some mappings, for
82 * example, s390 kvm.
83 * */
84 if ((vm_flags & VM_NOHUGEPAGE) ||
85 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
9fec5168 86 return false;
7da4e2cb
YS
87 /*
88 * If the hardware/firmware marked hugepage support disabled.
89 */
90 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
c0630669 91 return false;
9fec5168 92
7da4e2cb 93 /* khugepaged doesn't collapse DAX vma, but page fault is fine. */
9fec5168 94 if (vma_is_dax(vma))
7da4e2cb
YS
95 return in_pf;
96
97 /*
98 * Special VMA and hugetlb VMA.
99 * Must be checked after dax since some dax mappings may have
100 * VM_MIXEDMAP set.
101 */
102 if (vm_flags & VM_NO_KHUGEPAGED)
9fec5168
YS
103 return false;
104
7da4e2cb
YS
105 /*
106 * Check alignment for file vma and size for both file and anon vma.
107 *
108 * Skip the check for page fault. Huge fault does the check in fault
109 * handlers. And this check is not suitable for huge PUD fault.
110 */
111 if (!in_pf &&
112 !transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE)))
9fec5168
YS
113 return false;
114
7da4e2cb
YS
115 /*
116 * Enabled via shmem mount options or sysfs settings.
117 * Must be done before hugepage flags check since shmem has its
118 * own flags.
119 */
120 if (!in_pf && shmem_file(vma->vm_file))
c0630669 121 return shmem_huge_enabled(vma);
9fec5168 122
1064026b 123 if (!hugepage_flags_enabled())
9fec5168
YS
124 return false;
125
126 /* THP settings require madvise. */
1064026b 127 if (!(vm_flags & VM_HUGEPAGE) && !hugepage_flags_always())
9fec5168
YS
128 return false;
129
130 /* Only regular file is valid */
7da4e2cb 131 if (!in_pf && file_thp_enabled(vma))
78d12c19 132 return true;
7635d9cb 133
9fec5168
YS
134 if (!vma_is_anonymous(vma))
135 return false;
136
137 if (vma_is_temporary_stack(vma))
138 return false;
139
140 /*
141 * THPeligible bit of smaps should show 1 for proper VMAs even
142 * though anon_vma is not initialized yet.
7da4e2cb
YS
143 *
144 * Allow page fault since anon_vma may be not initialized until
145 * the first page fault.
9fec5168
YS
146 */
147 if (!vma->anon_vma)
7da4e2cb 148 return (smaps || in_pf);
9fec5168
YS
149
150 return true;
7635d9cb
MH
151}
152
aaa9705b 153static bool get_huge_zero_page(void)
97ae1749
KS
154{
155 struct page *zero_page;
156retry:
157 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
aaa9705b 158 return true;
97ae1749
KS
159
160 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
4a6c1297 161 HPAGE_PMD_ORDER);
d8a8e1f0
KS
162 if (!zero_page) {
163 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
aaa9705b 164 return false;
d8a8e1f0
KS
165 }
166 count_vm_event(THP_ZERO_PAGE_ALLOC);
97ae1749 167 preempt_disable();
5918d10a 168 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
97ae1749 169 preempt_enable();
5ddacbe9 170 __free_pages(zero_page, compound_order(zero_page));
97ae1749
KS
171 goto retry;
172 }
3b77e8c8 173 WRITE_ONCE(huge_zero_pfn, page_to_pfn(zero_page));
97ae1749
KS
174
175 /* We take additional reference here. It will be put back by shrinker */
176 atomic_set(&huge_zero_refcount, 2);
177 preempt_enable();
aaa9705b 178 return true;
4a6c1297
KS
179}
180
6fcb52a5 181static void put_huge_zero_page(void)
4a6c1297 182{
97ae1749
KS
183 /*
184 * Counter should never go to zero here. Only shrinker can put
185 * last reference.
186 */
187 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
4a6c1297
KS
188}
189
6fcb52a5
AL
190struct page *mm_get_huge_zero_page(struct mm_struct *mm)
191{
192 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
193 return READ_ONCE(huge_zero_page);
194
195 if (!get_huge_zero_page())
196 return NULL;
197
198 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
199 put_huge_zero_page();
200
201 return READ_ONCE(huge_zero_page);
202}
203
204void mm_put_huge_zero_page(struct mm_struct *mm)
205{
206 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
207 put_huge_zero_page();
208}
209
48896466
GC
210static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
211 struct shrink_control *sc)
4a6c1297 212{
48896466
GC
213 /* we can free zero page only if last reference remains */
214 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
215}
97ae1749 216
48896466
GC
217static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
218 struct shrink_control *sc)
219{
97ae1749 220 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
5918d10a
KS
221 struct page *zero_page = xchg(&huge_zero_page, NULL);
222 BUG_ON(zero_page == NULL);
3b77e8c8 223 WRITE_ONCE(huge_zero_pfn, ~0UL);
5ddacbe9 224 __free_pages(zero_page, compound_order(zero_page));
48896466 225 return HPAGE_PMD_NR;
97ae1749
KS
226 }
227
228 return 0;
4a6c1297
KS
229}
230
97ae1749 231static struct shrinker huge_zero_page_shrinker = {
48896466
GC
232 .count_objects = shrink_huge_zero_page_count,
233 .scan_objects = shrink_huge_zero_page_scan,
97ae1749
KS
234 .seeks = DEFAULT_SEEKS,
235};
236
71e3aac0 237#ifdef CONFIG_SYSFS
71e3aac0
AA
238static ssize_t enabled_show(struct kobject *kobj,
239 struct kobj_attribute *attr, char *buf)
240{
bfb0ffeb
JP
241 const char *output;
242
444eb2a4 243 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
bfb0ffeb
JP
244 output = "[always] madvise never";
245 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
246 &transparent_hugepage_flags))
247 output = "always [madvise] never";
444eb2a4 248 else
bfb0ffeb
JP
249 output = "always madvise [never]";
250
251 return sysfs_emit(buf, "%s\n", output);
71e3aac0 252}
444eb2a4 253
71e3aac0
AA
254static ssize_t enabled_store(struct kobject *kobj,
255 struct kobj_attribute *attr,
256 const char *buf, size_t count)
257{
21440d7e 258 ssize_t ret = count;
ba76149f 259
f42f2552 260 if (sysfs_streq(buf, "always")) {
21440d7e
DR
261 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
262 set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
f42f2552 263 } else if (sysfs_streq(buf, "madvise")) {
21440d7e
DR
264 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
265 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
f42f2552 266 } else if (sysfs_streq(buf, "never")) {
21440d7e
DR
267 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
268 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
269 } else
270 ret = -EINVAL;
ba76149f
AA
271
272 if (ret > 0) {
b46e756f 273 int err = start_stop_khugepaged();
ba76149f
AA
274 if (err)
275 ret = err;
276 }
ba76149f 277 return ret;
71e3aac0
AA
278}
279static struct kobj_attribute enabled_attr =
280 __ATTR(enabled, 0644, enabled_show, enabled_store);
281
b46e756f 282ssize_t single_hugepage_flag_show(struct kobject *kobj,
bfb0ffeb
JP
283 struct kobj_attribute *attr, char *buf,
284 enum transparent_hugepage_flag flag)
71e3aac0 285{
bfb0ffeb
JP
286 return sysfs_emit(buf, "%d\n",
287 !!test_bit(flag, &transparent_hugepage_flags));
71e3aac0 288}
e27e6151 289
b46e756f 290ssize_t single_hugepage_flag_store(struct kobject *kobj,
71e3aac0
AA
291 struct kobj_attribute *attr,
292 const char *buf, size_t count,
293 enum transparent_hugepage_flag flag)
294{
e27e6151
BH
295 unsigned long value;
296 int ret;
297
298 ret = kstrtoul(buf, 10, &value);
299 if (ret < 0)
300 return ret;
301 if (value > 1)
302 return -EINVAL;
303
304 if (value)
71e3aac0 305 set_bit(flag, &transparent_hugepage_flags);
e27e6151 306 else
71e3aac0 307 clear_bit(flag, &transparent_hugepage_flags);
71e3aac0
AA
308
309 return count;
310}
311
71e3aac0
AA
312static ssize_t defrag_show(struct kobject *kobj,
313 struct kobj_attribute *attr, char *buf)
314{
bfb0ffeb
JP
315 const char *output;
316
317 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
318 &transparent_hugepage_flags))
319 output = "[always] defer defer+madvise madvise never";
320 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
321 &transparent_hugepage_flags))
322 output = "always [defer] defer+madvise madvise never";
323 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
324 &transparent_hugepage_flags))
325 output = "always defer [defer+madvise] madvise never";
326 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
327 &transparent_hugepage_flags))
328 output = "always defer defer+madvise [madvise] never";
329 else
330 output = "always defer defer+madvise madvise [never]";
331
332 return sysfs_emit(buf, "%s\n", output);
71e3aac0 333}
21440d7e 334
71e3aac0
AA
335static ssize_t defrag_store(struct kobject *kobj,
336 struct kobj_attribute *attr,
337 const char *buf, size_t count)
338{
f42f2552 339 if (sysfs_streq(buf, "always")) {
21440d7e
DR
340 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
341 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
342 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
343 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
f42f2552 344 } else if (sysfs_streq(buf, "defer+madvise")) {
21440d7e
DR
345 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
346 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
347 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
348 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
f42f2552 349 } else if (sysfs_streq(buf, "defer")) {
4fad7fb6
DR
350 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
351 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
352 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
353 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
f42f2552 354 } else if (sysfs_streq(buf, "madvise")) {
21440d7e
DR
355 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
356 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
357 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
358 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
f42f2552 359 } else if (sysfs_streq(buf, "never")) {
21440d7e
DR
360 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
361 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
362 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
363 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
364 } else
365 return -EINVAL;
366
367 return count;
71e3aac0
AA
368}
369static struct kobj_attribute defrag_attr =
370 __ATTR(defrag, 0644, defrag_show, defrag_store);
371
79da5407 372static ssize_t use_zero_page_show(struct kobject *kobj,
ae7a927d 373 struct kobj_attribute *attr, char *buf)
79da5407 374{
b46e756f 375 return single_hugepage_flag_show(kobj, attr, buf,
ae7a927d 376 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
79da5407
KS
377}
378static ssize_t use_zero_page_store(struct kobject *kobj,
379 struct kobj_attribute *attr, const char *buf, size_t count)
380{
b46e756f 381 return single_hugepage_flag_store(kobj, attr, buf, count,
79da5407
KS
382 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
383}
384static struct kobj_attribute use_zero_page_attr =
385 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
49920d28
HD
386
387static ssize_t hpage_pmd_size_show(struct kobject *kobj,
ae7a927d 388 struct kobj_attribute *attr, char *buf)
49920d28 389{
ae7a927d 390 return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE);
49920d28
HD
391}
392static struct kobj_attribute hpage_pmd_size_attr =
393 __ATTR_RO(hpage_pmd_size);
394
71e3aac0
AA
395static struct attribute *hugepage_attr[] = {
396 &enabled_attr.attr,
397 &defrag_attr.attr,
79da5407 398 &use_zero_page_attr.attr,
49920d28 399 &hpage_pmd_size_attr.attr,
396bcc52 400#ifdef CONFIG_SHMEM
5a6e75f8 401 &shmem_enabled_attr.attr,
71e3aac0
AA
402#endif
403 NULL,
404};
405
8aa95a21 406static const struct attribute_group hugepage_attr_group = {
71e3aac0 407 .attrs = hugepage_attr,
ba76149f
AA
408};
409
569e5590 410static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
71e3aac0 411{
71e3aac0
AA
412 int err;
413
569e5590
SL
414 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
415 if (unlikely(!*hugepage_kobj)) {
ae3a8c1c 416 pr_err("failed to create transparent hugepage kobject\n");
569e5590 417 return -ENOMEM;
ba76149f
AA
418 }
419
569e5590 420 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
ba76149f 421 if (err) {
ae3a8c1c 422 pr_err("failed to register transparent hugepage group\n");
569e5590 423 goto delete_obj;
ba76149f
AA
424 }
425
569e5590 426 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
ba76149f 427 if (err) {
ae3a8c1c 428 pr_err("failed to register transparent hugepage group\n");
569e5590 429 goto remove_hp_group;
ba76149f 430 }
569e5590
SL
431
432 return 0;
433
434remove_hp_group:
435 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
436delete_obj:
437 kobject_put(*hugepage_kobj);
438 return err;
439}
440
441static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
442{
443 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
444 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
445 kobject_put(hugepage_kobj);
446}
447#else
448static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
449{
450 return 0;
451}
452
453static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
454{
455}
456#endif /* CONFIG_SYSFS */
457
458static int __init hugepage_init(void)
459{
460 int err;
461 struct kobject *hugepage_kobj;
462
463 if (!has_transparent_hugepage()) {
bae84953
AK
464 /*
465 * Hardware doesn't support hugepages, hence disable
466 * DAX PMD support.
467 */
468 transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_NEVER_DAX;
569e5590
SL
469 return -EINVAL;
470 }
471
ff20c2e0
KS
472 /*
473 * hugepages can't be allocated by the buddy allocator
474 */
475 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
476 /*
477 * we use page->mapping and page->index in second tail page
478 * as list_head: assuming THP order >= 2
479 */
480 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
481
569e5590
SL
482 err = hugepage_init_sysfs(&hugepage_kobj);
483 if (err)
65ebb64f 484 goto err_sysfs;
ba76149f 485
b46e756f 486 err = khugepaged_init();
ba76149f 487 if (err)
65ebb64f 488 goto err_slab;
ba76149f 489
e33c267a 490 err = register_shrinker(&huge_zero_page_shrinker, "thp-zero");
65ebb64f
KS
491 if (err)
492 goto err_hzp_shrinker;
e33c267a 493 err = register_shrinker(&deferred_split_shrinker, "thp-deferred_split");
9a982250
KS
494 if (err)
495 goto err_split_shrinker;
97ae1749 496
97562cd2
RR
497 /*
498 * By default disable transparent hugepages on smaller systems,
499 * where the extra memory used could hurt more than TLB overhead
500 * is likely to save. The admin can still enable it through /sys.
501 */
ca79b0c2 502 if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
97562cd2 503 transparent_hugepage_flags = 0;
79553da2
KS
504 return 0;
505 }
97562cd2 506
79553da2 507 err = start_stop_khugepaged();
65ebb64f
KS
508 if (err)
509 goto err_khugepaged;
ba76149f 510
569e5590 511 return 0;
65ebb64f 512err_khugepaged:
9a982250
KS
513 unregister_shrinker(&deferred_split_shrinker);
514err_split_shrinker:
65ebb64f
KS
515 unregister_shrinker(&huge_zero_page_shrinker);
516err_hzp_shrinker:
b46e756f 517 khugepaged_destroy();
65ebb64f 518err_slab:
569e5590 519 hugepage_exit_sysfs(hugepage_kobj);
65ebb64f 520err_sysfs:
ba76149f 521 return err;
71e3aac0 522}
a64fb3cd 523subsys_initcall(hugepage_init);
71e3aac0
AA
524
525static int __init setup_transparent_hugepage(char *str)
526{
527 int ret = 0;
528 if (!str)
529 goto out;
530 if (!strcmp(str, "always")) {
531 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
532 &transparent_hugepage_flags);
533 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
534 &transparent_hugepage_flags);
535 ret = 1;
536 } else if (!strcmp(str, "madvise")) {
537 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
538 &transparent_hugepage_flags);
539 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
540 &transparent_hugepage_flags);
541 ret = 1;
542 } else if (!strcmp(str, "never")) {
543 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
544 &transparent_hugepage_flags);
545 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
546 &transparent_hugepage_flags);
547 ret = 1;
548 }
549out:
550 if (!ret)
ae3a8c1c 551 pr_warn("transparent_hugepage= cannot parse, ignored\n");
71e3aac0
AA
552 return ret;
553}
554__setup("transparent_hugepage=", setup_transparent_hugepage);
555
f55e1014 556pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
71e3aac0 557{
f55e1014 558 if (likely(vma->vm_flags & VM_WRITE))
71e3aac0
AA
559 pmd = pmd_mkwrite(pmd);
560 return pmd;
561}
562
87eaceb3
YS
563#ifdef CONFIG_MEMCG
564static inline struct deferred_split *get_deferred_split_queue(struct page *page)
9a982250 565{
bcfe06bf 566 struct mem_cgroup *memcg = page_memcg(compound_head(page));
87eaceb3
YS
567 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
568
569 if (memcg)
570 return &memcg->deferred_split_queue;
571 else
572 return &pgdat->deferred_split_queue;
9a982250 573}
87eaceb3
YS
574#else
575static inline struct deferred_split *get_deferred_split_queue(struct page *page)
576{
577 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
578
579 return &pgdat->deferred_split_queue;
580}
581#endif
9a982250
KS
582
583void prep_transhuge_page(struct page *page)
584{
585 /*
586 * we use page->mapping and page->indexlru in second tail page
587 * as list_head: assuming THP order >= 2
588 */
9a982250
KS
589
590 INIT_LIST_HEAD(page_deferred_list(page));
591 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
592}
593
562beb72 594static inline bool is_transparent_hugepage(struct page *page)
005ba37c
SC
595{
596 if (!PageCompound(page))
fa1f68cc 597 return false;
005ba37c
SC
598
599 page = compound_head(page);
600 return is_huge_zero_page(page) ||
601 page[1].compound_dtor == TRANSHUGE_PAGE_DTOR;
602}
005ba37c 603
97d3d0f9
KS
604static unsigned long __thp_get_unmapped_area(struct file *filp,
605 unsigned long addr, unsigned long len,
74d2fad1
TK
606 loff_t off, unsigned long flags, unsigned long size)
607{
74d2fad1
TK
608 loff_t off_end = off + len;
609 loff_t off_align = round_up(off, size);
97d3d0f9 610 unsigned long len_pad, ret;
74d2fad1
TK
611
612 if (off_end <= off_align || (off_end - off_align) < size)
613 return 0;
614
615 len_pad = len + size;
616 if (len_pad < len || (off + len_pad) < off)
617 return 0;
618
97d3d0f9 619 ret = current->mm->get_unmapped_area(filp, addr, len_pad,
74d2fad1 620 off >> PAGE_SHIFT, flags);
97d3d0f9
KS
621
622 /*
623 * The failure might be due to length padding. The caller will retry
624 * without the padding.
625 */
626 if (IS_ERR_VALUE(ret))
74d2fad1
TK
627 return 0;
628
97d3d0f9
KS
629 /*
630 * Do not try to align to THP boundary if allocation at the address
631 * hint succeeds.
632 */
633 if (ret == addr)
634 return addr;
635
636 ret += (off - ret) & (size - 1);
637 return ret;
74d2fad1
TK
638}
639
640unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
641 unsigned long len, unsigned long pgoff, unsigned long flags)
642{
97d3d0f9 643 unsigned long ret;
74d2fad1
TK
644 loff_t off = (loff_t)pgoff << PAGE_SHIFT;
645
97d3d0f9
KS
646 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
647 if (ret)
648 return ret;
1854bc6e 649
74d2fad1
TK
650 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
651}
652EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
653
2b740303
SJ
654static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
655 struct page *page, gfp_t gfp)
71e3aac0 656{
82b0f8c3 657 struct vm_area_struct *vma = vmf->vma;
71e3aac0 658 pgtable_t pgtable;
82b0f8c3 659 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
2b740303 660 vm_fault_t ret = 0;
71e3aac0 661
309381fe 662 VM_BUG_ON_PAGE(!PageCompound(page), page);
00501b53 663
8f425e4e 664 if (mem_cgroup_charge(page_folio(page), vma->vm_mm, gfp)) {
6b251fc9
AA
665 put_page(page);
666 count_vm_event(THP_FAULT_FALLBACK);
85b9f46e 667 count_vm_event(THP_FAULT_FALLBACK_CHARGE);
6b251fc9
AA
668 return VM_FAULT_FALLBACK;
669 }
9d82c694 670 cgroup_throttle_swaprate(page, gfp);
00501b53 671
4cf58924 672 pgtable = pte_alloc_one(vma->vm_mm);
00501b53 673 if (unlikely(!pgtable)) {
6b31d595
MH
674 ret = VM_FAULT_OOM;
675 goto release;
00501b53 676 }
71e3aac0 677
c79b57e4 678 clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
52f37629
MK
679 /*
680 * The memory barrier inside __SetPageUptodate makes sure that
681 * clear_huge_page writes become visible before the set_pmd_at()
682 * write.
683 */
71e3aac0
AA
684 __SetPageUptodate(page);
685
82b0f8c3
JK
686 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
687 if (unlikely(!pmd_none(*vmf->pmd))) {
6b31d595 688 goto unlock_release;
71e3aac0
AA
689 } else {
690 pmd_t entry;
6b251fc9 691
6b31d595
MH
692 ret = check_stable_address_space(vma->vm_mm);
693 if (ret)
694 goto unlock_release;
695
6b251fc9
AA
696 /* Deliver the page fault to userland */
697 if (userfaultfd_missing(vma)) {
82b0f8c3 698 spin_unlock(vmf->ptl);
6b251fc9 699 put_page(page);
bae473a4 700 pte_free(vma->vm_mm, pgtable);
8fd5eda4
ML
701 ret = handle_userfault(vmf, VM_UFFD_MISSING);
702 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
703 return ret;
6b251fc9
AA
704 }
705
3122359a 706 entry = mk_huge_pmd(page, vma->vm_page_prot);
f55e1014 707 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
40f2bbf7 708 page_add_new_anon_rmap(page, vma, haddr);
b518154e 709 lru_cache_add_inactive_or_unevictable(page, vma);
82b0f8c3
JK
710 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
711 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
fca40573 712 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
bae473a4 713 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
c4812909 714 mm_inc_nr_ptes(vma->vm_mm);
82b0f8c3 715 spin_unlock(vmf->ptl);
6b251fc9 716 count_vm_event(THP_FAULT_ALLOC);
9d82c694 717 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
71e3aac0
AA
718 }
719
aa2e878e 720 return 0;
6b31d595
MH
721unlock_release:
722 spin_unlock(vmf->ptl);
723release:
724 if (pgtable)
725 pte_free(vma->vm_mm, pgtable);
6b31d595
MH
726 put_page(page);
727 return ret;
728
71e3aac0
AA
729}
730
444eb2a4 731/*
21440d7e
DR
732 * always: directly stall for all thp allocations
733 * defer: wake kswapd and fail if not immediately available
734 * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
735 * fail if not immediately available
736 * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
737 * available
738 * never: never stall for any thp allocation
444eb2a4 739 */
164cc4fe 740gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
444eb2a4 741{
164cc4fe 742 const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
2f0799a0 743
ac79f78d 744 /* Always do synchronous compaction */
a8282608
AA
745 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
746 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
ac79f78d
DR
747
748 /* Kick kcompactd and fail quickly */
21440d7e 749 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
19deb769 750 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
ac79f78d
DR
751
752 /* Synchronous compaction if madvised, otherwise kick kcompactd */
21440d7e 753 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
19deb769
DR
754 return GFP_TRANSHUGE_LIGHT |
755 (vma_madvised ? __GFP_DIRECT_RECLAIM :
756 __GFP_KSWAPD_RECLAIM);
ac79f78d
DR
757
758 /* Only do synchronous compaction if madvised */
21440d7e 759 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
19deb769
DR
760 return GFP_TRANSHUGE_LIGHT |
761 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
ac79f78d 762
19deb769 763 return GFP_TRANSHUGE_LIGHT;
444eb2a4
MG
764}
765
c4088ebd 766/* Caller must hold page table lock. */
2efeb8da 767static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
97ae1749 768 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
5918d10a 769 struct page *zero_page)
fc9fe822
KS
770{
771 pmd_t entry;
7c414164 772 if (!pmd_none(*pmd))
2efeb8da 773 return;
5918d10a 774 entry = mk_pmd(zero_page, vma->vm_page_prot);
fc9fe822 775 entry = pmd_mkhuge(entry);
12c9d70b
MW
776 if (pgtable)
777 pgtable_trans_huge_deposit(mm, pmd, pgtable);
fc9fe822 778 set_pmd_at(mm, haddr, pmd, entry);
c4812909 779 mm_inc_nr_ptes(mm);
fc9fe822
KS
780}
781
2b740303 782vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
71e3aac0 783{
82b0f8c3 784 struct vm_area_struct *vma = vmf->vma;
077fcf11 785 gfp_t gfp;
cb196ee1 786 struct folio *folio;
82b0f8c3 787 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
71e3aac0 788
43675e6f 789 if (!transhuge_vma_suitable(vma, haddr))
c0292554 790 return VM_FAULT_FALLBACK;
128ec037
KS
791 if (unlikely(anon_vma_prepare(vma)))
792 return VM_FAULT_OOM;
4fa6893f 793 khugepaged_enter_vma(vma, vma->vm_flags);
d2081b2b 794
82b0f8c3 795 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
bae473a4 796 !mm_forbids_zeropage(vma->vm_mm) &&
128ec037
KS
797 transparent_hugepage_use_zero_page()) {
798 pgtable_t pgtable;
799 struct page *zero_page;
2b740303 800 vm_fault_t ret;
4cf58924 801 pgtable = pte_alloc_one(vma->vm_mm);
128ec037 802 if (unlikely(!pgtable))
ba76149f 803 return VM_FAULT_OOM;
6fcb52a5 804 zero_page = mm_get_huge_zero_page(vma->vm_mm);
128ec037 805 if (unlikely(!zero_page)) {
bae473a4 806 pte_free(vma->vm_mm, pgtable);
81ab4201 807 count_vm_event(THP_FAULT_FALLBACK);
c0292554 808 return VM_FAULT_FALLBACK;
b9bbfbe3 809 }
82b0f8c3 810 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
6b251fc9 811 ret = 0;
82b0f8c3 812 if (pmd_none(*vmf->pmd)) {
6b31d595
MH
813 ret = check_stable_address_space(vma->vm_mm);
814 if (ret) {
815 spin_unlock(vmf->ptl);
bfe8cc1d 816 pte_free(vma->vm_mm, pgtable);
6b31d595 817 } else if (userfaultfd_missing(vma)) {
82b0f8c3 818 spin_unlock(vmf->ptl);
bfe8cc1d 819 pte_free(vma->vm_mm, pgtable);
82b0f8c3 820 ret = handle_userfault(vmf, VM_UFFD_MISSING);
6b251fc9
AA
821 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
822 } else {
bae473a4 823 set_huge_zero_page(pgtable, vma->vm_mm, vma,
82b0f8c3 824 haddr, vmf->pmd, zero_page);
fca40573 825 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
82b0f8c3 826 spin_unlock(vmf->ptl);
6b251fc9 827 }
bfe8cc1d 828 } else {
82b0f8c3 829 spin_unlock(vmf->ptl);
bae473a4 830 pte_free(vma->vm_mm, pgtable);
bfe8cc1d 831 }
6b251fc9 832 return ret;
71e3aac0 833 }
164cc4fe 834 gfp = vma_thp_gfp_mask(vma);
cb196ee1
MWO
835 folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, vma, haddr, true);
836 if (unlikely(!folio)) {
128ec037 837 count_vm_event(THP_FAULT_FALLBACK);
c0292554 838 return VM_FAULT_FALLBACK;
128ec037 839 }
cb196ee1 840 return __do_huge_pmd_anonymous_page(vmf, &folio->page, gfp);
71e3aac0
AA
841}
842
ae18d6dc 843static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
3b6521f5
OH
844 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
845 pgtable_t pgtable)
5cad465d
MW
846{
847 struct mm_struct *mm = vma->vm_mm;
848 pmd_t entry;
849 spinlock_t *ptl;
850
851 ptl = pmd_lock(mm, pmd);
c6f3c5ee
AK
852 if (!pmd_none(*pmd)) {
853 if (write) {
854 if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
855 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
856 goto out_unlock;
857 }
858 entry = pmd_mkyoung(*pmd);
859 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
860 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
861 update_mmu_cache_pmd(vma, addr, pmd);
862 }
863
864 goto out_unlock;
865 }
866
f25748e3
DW
867 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
868 if (pfn_t_devmap(pfn))
869 entry = pmd_mkdevmap(entry);
01871e59 870 if (write) {
f55e1014
LT
871 entry = pmd_mkyoung(pmd_mkdirty(entry));
872 entry = maybe_pmd_mkwrite(entry, vma);
5cad465d 873 }
3b6521f5
OH
874
875 if (pgtable) {
876 pgtable_trans_huge_deposit(mm, pmd, pgtable);
c4812909 877 mm_inc_nr_ptes(mm);
c6f3c5ee 878 pgtable = NULL;
3b6521f5
OH
879 }
880
01871e59
RZ
881 set_pmd_at(mm, addr, pmd, entry);
882 update_mmu_cache_pmd(vma, addr, pmd);
c6f3c5ee
AK
883
884out_unlock:
5cad465d 885 spin_unlock(ptl);
c6f3c5ee
AK
886 if (pgtable)
887 pte_free(mm, pgtable);
5cad465d
MW
888}
889
9a9731b1
THV
890/**
891 * vmf_insert_pfn_pmd_prot - insert a pmd size pfn
892 * @vmf: Structure describing the fault
893 * @pfn: pfn to insert
894 * @pgprot: page protection to use
895 * @write: whether it's a write fault
896 *
897 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info and
898 * also consult the vmf_insert_mixed_prot() documentation when
899 * @pgprot != @vmf->vma->vm_page_prot.
900 *
901 * Return: vm_fault_t value.
902 */
903vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
904 pgprot_t pgprot, bool write)
5cad465d 905{
fce86ff5
DW
906 unsigned long addr = vmf->address & PMD_MASK;
907 struct vm_area_struct *vma = vmf->vma;
3b6521f5 908 pgtable_t pgtable = NULL;
fce86ff5 909
5cad465d
MW
910 /*
911 * If we had pmd_special, we could avoid all these restrictions,
912 * but we need to be consistent with PTEs and architectures that
913 * can't support a 'special' bit.
914 */
e1fb4a08
DJ
915 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
916 !pfn_t_devmap(pfn));
5cad465d
MW
917 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
918 (VM_PFNMAP|VM_MIXEDMAP));
919 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
5cad465d
MW
920
921 if (addr < vma->vm_start || addr >= vma->vm_end)
922 return VM_FAULT_SIGBUS;
308a047c 923
3b6521f5 924 if (arch_needs_pgtable_deposit()) {
4cf58924 925 pgtable = pte_alloc_one(vma->vm_mm);
3b6521f5
OH
926 if (!pgtable)
927 return VM_FAULT_OOM;
928 }
929
308a047c
BP
930 track_pfn_insert(vma, &pgprot, pfn);
931
fce86ff5 932 insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
ae18d6dc 933 return VM_FAULT_NOPAGE;
5cad465d 934}
9a9731b1 935EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd_prot);
5cad465d 936
a00cc7d9 937#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
f55e1014 938static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
a00cc7d9 939{
f55e1014 940 if (likely(vma->vm_flags & VM_WRITE))
a00cc7d9
MW
941 pud = pud_mkwrite(pud);
942 return pud;
943}
944
945static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
946 pud_t *pud, pfn_t pfn, pgprot_t prot, bool write)
947{
948 struct mm_struct *mm = vma->vm_mm;
949 pud_t entry;
950 spinlock_t *ptl;
951
952 ptl = pud_lock(mm, pud);
c6f3c5ee
AK
953 if (!pud_none(*pud)) {
954 if (write) {
955 if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
956 WARN_ON_ONCE(!is_huge_zero_pud(*pud));
957 goto out_unlock;
958 }
959 entry = pud_mkyoung(*pud);
960 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
961 if (pudp_set_access_flags(vma, addr, pud, entry, 1))
962 update_mmu_cache_pud(vma, addr, pud);
963 }
964 goto out_unlock;
965 }
966
a00cc7d9
MW
967 entry = pud_mkhuge(pfn_t_pud(pfn, prot));
968 if (pfn_t_devmap(pfn))
969 entry = pud_mkdevmap(entry);
970 if (write) {
f55e1014
LT
971 entry = pud_mkyoung(pud_mkdirty(entry));
972 entry = maybe_pud_mkwrite(entry, vma);
a00cc7d9
MW
973 }
974 set_pud_at(mm, addr, pud, entry);
975 update_mmu_cache_pud(vma, addr, pud);
c6f3c5ee
AK
976
977out_unlock:
a00cc7d9
MW
978 spin_unlock(ptl);
979}
980
9a9731b1
THV
981/**
982 * vmf_insert_pfn_pud_prot - insert a pud size pfn
983 * @vmf: Structure describing the fault
984 * @pfn: pfn to insert
985 * @pgprot: page protection to use
986 * @write: whether it's a write fault
987 *
988 * Insert a pud size pfn. See vmf_insert_pfn() for additional info and
989 * also consult the vmf_insert_mixed_prot() documentation when
990 * @pgprot != @vmf->vma->vm_page_prot.
991 *
992 * Return: vm_fault_t value.
993 */
994vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
995 pgprot_t pgprot, bool write)
a00cc7d9 996{
fce86ff5
DW
997 unsigned long addr = vmf->address & PUD_MASK;
998 struct vm_area_struct *vma = vmf->vma;
fce86ff5 999
a00cc7d9
MW
1000 /*
1001 * If we had pud_special, we could avoid all these restrictions,
1002 * but we need to be consistent with PTEs and architectures that
1003 * can't support a 'special' bit.
1004 */
62ec0d8c
DJ
1005 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
1006 !pfn_t_devmap(pfn));
a00cc7d9
MW
1007 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1008 (VM_PFNMAP|VM_MIXEDMAP));
1009 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
a00cc7d9
MW
1010
1011 if (addr < vma->vm_start || addr >= vma->vm_end)
1012 return VM_FAULT_SIGBUS;
1013
1014 track_pfn_insert(vma, &pgprot, pfn);
1015
fce86ff5 1016 insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write);
a00cc7d9
MW
1017 return VM_FAULT_NOPAGE;
1018}
9a9731b1 1019EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud_prot);
a00cc7d9
MW
1020#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1021
3565fce3 1022static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
a8f97366 1023 pmd_t *pmd, int flags)
3565fce3
DW
1024{
1025 pmd_t _pmd;
1026
a8f97366
KS
1027 _pmd = pmd_mkyoung(*pmd);
1028 if (flags & FOLL_WRITE)
1029 _pmd = pmd_mkdirty(_pmd);
3565fce3 1030 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
a8f97366 1031 pmd, _pmd, flags & FOLL_WRITE))
3565fce3
DW
1032 update_mmu_cache_pmd(vma, addr, pmd);
1033}
1034
1035struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
df06b37f 1036 pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
3565fce3
DW
1037{
1038 unsigned long pfn = pmd_pfn(*pmd);
1039 struct mm_struct *mm = vma->vm_mm;
3565fce3
DW
1040 struct page *page;
1041
1042 assert_spin_locked(pmd_lockptr(mm, pmd));
1043
8310d48b
KF
1044 /*
1045 * When we COW a devmap PMD entry, we split it into PTEs, so we should
1046 * not be in this function with `flags & FOLL_COW` set.
1047 */
1048 WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
1049
3faa52c0
JH
1050 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
1051 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
1052 (FOLL_PIN | FOLL_GET)))
1053 return NULL;
1054
f6f37321 1055 if (flags & FOLL_WRITE && !pmd_write(*pmd))
3565fce3
DW
1056 return NULL;
1057
1058 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1059 /* pass */;
1060 else
1061 return NULL;
1062
1063 if (flags & FOLL_TOUCH)
a8f97366 1064 touch_pmd(vma, addr, pmd, flags);
3565fce3
DW
1065
1066 /*
1067 * device mapped pages can only be returned if the
1068 * caller will manage the page reference count.
1069 */
3faa52c0 1070 if (!(flags & (FOLL_GET | FOLL_PIN)))
3565fce3
DW
1071 return ERR_PTR(-EEXIST);
1072
1073 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
df06b37f
KB
1074 *pgmap = get_dev_pagemap(pfn, *pgmap);
1075 if (!*pgmap)
3565fce3
DW
1076 return ERR_PTR(-EFAULT);
1077 page = pfn_to_page(pfn);
3faa52c0
JH
1078 if (!try_grab_page(page, flags))
1079 page = ERR_PTR(-ENOMEM);
3565fce3
DW
1080
1081 return page;
1082}
1083
71e3aac0
AA
1084int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1085 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
8f34f1ea 1086 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
71e3aac0 1087{
c4088ebd 1088 spinlock_t *dst_ptl, *src_ptl;
71e3aac0
AA
1089 struct page *src_page;
1090 pmd_t pmd;
12c9d70b 1091 pgtable_t pgtable = NULL;
628d47ce 1092 int ret = -ENOMEM;
71e3aac0 1093
628d47ce 1094 /* Skip if can be re-fill on fault */
8f34f1ea 1095 if (!vma_is_anonymous(dst_vma))
628d47ce
KS
1096 return 0;
1097
4cf58924 1098 pgtable = pte_alloc_one(dst_mm);
628d47ce
KS
1099 if (unlikely(!pgtable))
1100 goto out;
71e3aac0 1101
c4088ebd
KS
1102 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1103 src_ptl = pmd_lockptr(src_mm, src_pmd);
1104 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
71e3aac0
AA
1105
1106 ret = -EAGAIN;
1107 pmd = *src_pmd;
84c3fc4e
ZY
1108
1109#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1110 if (unlikely(is_swap_pmd(pmd))) {
1111 swp_entry_t entry = pmd_to_swp_entry(pmd);
1112
1113 VM_BUG_ON(!is_pmd_migration_entry(pmd));
6c287605 1114 if (!is_readable_migration_entry(entry)) {
4dd845b5
AP
1115 entry = make_readable_migration_entry(
1116 swp_offset(entry));
84c3fc4e 1117 pmd = swp_entry_to_pmd(entry);
ab6e3d09
NH
1118 if (pmd_swp_soft_dirty(*src_pmd))
1119 pmd = pmd_swp_mksoft_dirty(pmd);
8f34f1ea
PX
1120 if (pmd_swp_uffd_wp(*src_pmd))
1121 pmd = pmd_swp_mkuffd_wp(pmd);
84c3fc4e
ZY
1122 set_pmd_at(src_mm, addr, src_pmd, pmd);
1123 }
dd8a67f9 1124 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
af5b0f6a 1125 mm_inc_nr_ptes(dst_mm);
dd8a67f9 1126 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
8f34f1ea
PX
1127 if (!userfaultfd_wp(dst_vma))
1128 pmd = pmd_swp_clear_uffd_wp(pmd);
84c3fc4e
ZY
1129 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1130 ret = 0;
1131 goto out_unlock;
1132 }
1133#endif
1134
628d47ce 1135 if (unlikely(!pmd_trans_huge(pmd))) {
71e3aac0
AA
1136 pte_free(dst_mm, pgtable);
1137 goto out_unlock;
1138 }
fc9fe822 1139 /*
c4088ebd 1140 * When page table lock is held, the huge zero pmd should not be
fc9fe822
KS
1141 * under splitting since we don't split the page itself, only pmd to
1142 * a page table.
1143 */
1144 if (is_huge_zero_pmd(pmd)) {
97ae1749
KS
1145 /*
1146 * get_huge_zero_page() will never allocate a new page here,
1147 * since we already have a zero page to copy. It just takes a
1148 * reference.
1149 */
5fc7a5f6
PX
1150 mm_get_huge_zero_page(dst_mm);
1151 goto out_zero_page;
fc9fe822 1152 }
de466bd6 1153
628d47ce
KS
1154 src_page = pmd_page(pmd);
1155 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
d042035e 1156
fb3d824d
DH
1157 get_page(src_page);
1158 if (unlikely(page_try_dup_anon_rmap(src_page, true, src_vma))) {
1159 /* Page maybe pinned: split and retry the fault on PTEs. */
1160 put_page(src_page);
d042035e
PX
1161 pte_free(dst_mm, pgtable);
1162 spin_unlock(src_ptl);
1163 spin_unlock(dst_ptl);
8f34f1ea 1164 __split_huge_pmd(src_vma, src_pmd, addr, false, NULL);
d042035e
PX
1165 return -EAGAIN;
1166 }
628d47ce 1167 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
5fc7a5f6 1168out_zero_page:
c4812909 1169 mm_inc_nr_ptes(dst_mm);
628d47ce 1170 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
71e3aac0 1171 pmdp_set_wrprotect(src_mm, addr, src_pmd);
8f34f1ea
PX
1172 if (!userfaultfd_wp(dst_vma))
1173 pmd = pmd_clear_uffd_wp(pmd);
71e3aac0
AA
1174 pmd = pmd_mkold(pmd_wrprotect(pmd));
1175 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
71e3aac0
AA
1176
1177 ret = 0;
1178out_unlock:
c4088ebd
KS
1179 spin_unlock(src_ptl);
1180 spin_unlock(dst_ptl);
71e3aac0
AA
1181out:
1182 return ret;
1183}
1184
a00cc7d9
MW
1185#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1186static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
a8f97366 1187 pud_t *pud, int flags)
a00cc7d9
MW
1188{
1189 pud_t _pud;
1190
a8f97366
KS
1191 _pud = pud_mkyoung(*pud);
1192 if (flags & FOLL_WRITE)
1193 _pud = pud_mkdirty(_pud);
a00cc7d9 1194 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
a8f97366 1195 pud, _pud, flags & FOLL_WRITE))
a00cc7d9
MW
1196 update_mmu_cache_pud(vma, addr, pud);
1197}
1198
1199struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
df06b37f 1200 pud_t *pud, int flags, struct dev_pagemap **pgmap)
a00cc7d9
MW
1201{
1202 unsigned long pfn = pud_pfn(*pud);
1203 struct mm_struct *mm = vma->vm_mm;
a00cc7d9
MW
1204 struct page *page;
1205
1206 assert_spin_locked(pud_lockptr(mm, pud));
1207
f6f37321 1208 if (flags & FOLL_WRITE && !pud_write(*pud))
a00cc7d9
MW
1209 return NULL;
1210
3faa52c0
JH
1211 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
1212 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
1213 (FOLL_PIN | FOLL_GET)))
1214 return NULL;
1215
a00cc7d9
MW
1216 if (pud_present(*pud) && pud_devmap(*pud))
1217 /* pass */;
1218 else
1219 return NULL;
1220
1221 if (flags & FOLL_TOUCH)
a8f97366 1222 touch_pud(vma, addr, pud, flags);
a00cc7d9
MW
1223
1224 /*
1225 * device mapped pages can only be returned if the
1226 * caller will manage the page reference count.
3faa52c0
JH
1227 *
1228 * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here:
a00cc7d9 1229 */
3faa52c0 1230 if (!(flags & (FOLL_GET | FOLL_PIN)))
a00cc7d9
MW
1231 return ERR_PTR(-EEXIST);
1232
1233 pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
df06b37f
KB
1234 *pgmap = get_dev_pagemap(pfn, *pgmap);
1235 if (!*pgmap)
a00cc7d9
MW
1236 return ERR_PTR(-EFAULT);
1237 page = pfn_to_page(pfn);
3faa52c0
JH
1238 if (!try_grab_page(page, flags))
1239 page = ERR_PTR(-ENOMEM);
a00cc7d9
MW
1240
1241 return page;
1242}
1243
1244int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1245 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1246 struct vm_area_struct *vma)
1247{
1248 spinlock_t *dst_ptl, *src_ptl;
1249 pud_t pud;
1250 int ret;
1251
1252 dst_ptl = pud_lock(dst_mm, dst_pud);
1253 src_ptl = pud_lockptr(src_mm, src_pud);
1254 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1255
1256 ret = -EAGAIN;
1257 pud = *src_pud;
1258 if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
1259 goto out_unlock;
1260
1261 /*
1262 * When page table lock is held, the huge zero pud should not be
1263 * under splitting since we don't split the page itself, only pud to
1264 * a page table.
1265 */
1266 if (is_huge_zero_pud(pud)) {
1267 /* No huge zero pud yet */
1268 }
1269
fb3d824d
DH
1270 /*
1271 * TODO: once we support anonymous pages, use page_try_dup_anon_rmap()
1272 * and split if duplicating fails.
1273 */
a00cc7d9
MW
1274 pudp_set_wrprotect(src_mm, addr, src_pud);
1275 pud = pud_mkold(pud_wrprotect(pud));
1276 set_pud_at(dst_mm, addr, dst_pud, pud);
1277
1278 ret = 0;
1279out_unlock:
1280 spin_unlock(src_ptl);
1281 spin_unlock(dst_ptl);
1282 return ret;
1283}
1284
1285void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
1286{
1287 pud_t entry;
1288 unsigned long haddr;
1289 bool write = vmf->flags & FAULT_FLAG_WRITE;
1290
1291 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1292 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1293 goto unlock;
1294
1295 entry = pud_mkyoung(orig_pud);
1296 if (write)
1297 entry = pud_mkdirty(entry);
1298 haddr = vmf->address & HPAGE_PUD_MASK;
1299 if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write))
1300 update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud);
1301
1302unlock:
1303 spin_unlock(vmf->ptl);
1304}
1305#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1306
5db4f15c 1307void huge_pmd_set_accessed(struct vm_fault *vmf)
a1dd450b
WD
1308{
1309 pmd_t entry;
1310 unsigned long haddr;
20f664aa 1311 bool write = vmf->flags & FAULT_FLAG_WRITE;
5db4f15c 1312 pmd_t orig_pmd = vmf->orig_pmd;
a1dd450b 1313
82b0f8c3
JK
1314 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1315 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
a1dd450b
WD
1316 goto unlock;
1317
1318 entry = pmd_mkyoung(orig_pmd);
20f664aa
MK
1319 if (write)
1320 entry = pmd_mkdirty(entry);
82b0f8c3 1321 haddr = vmf->address & HPAGE_PMD_MASK;
20f664aa 1322 if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write))
82b0f8c3 1323 update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd);
a1dd450b
WD
1324
1325unlock:
82b0f8c3 1326 spin_unlock(vmf->ptl);
a1dd450b
WD
1327}
1328
5db4f15c 1329vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
71e3aac0 1330{
c89357e2 1331 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
82b0f8c3 1332 struct vm_area_struct *vma = vmf->vma;
3917c802 1333 struct page *page;
82b0f8c3 1334 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
5db4f15c 1335 pmd_t orig_pmd = vmf->orig_pmd;
71e3aac0 1336
82b0f8c3 1337 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
81d1b09c 1338 VM_BUG_ON_VMA(!vma->anon_vma, vma);
3917c802 1339
c89357e2
DH
1340 VM_BUG_ON(unshare && (vmf->flags & FAULT_FLAG_WRITE));
1341 VM_BUG_ON(!unshare && !(vmf->flags & FAULT_FLAG_WRITE));
1342
93b4796d 1343 if (is_huge_zero_pmd(orig_pmd))
3917c802
KS
1344 goto fallback;
1345
82b0f8c3 1346 spin_lock(vmf->ptl);
3917c802
KS
1347
1348 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1349 spin_unlock(vmf->ptl);
1350 return 0;
1351 }
71e3aac0
AA
1352
1353 page = pmd_page(orig_pmd);
f6004e73 1354 VM_BUG_ON_PAGE(!PageHead(page), page);
3917c802 1355
6c287605
DH
1356 /* Early check when only holding the PT lock. */
1357 if (PageAnonExclusive(page))
1358 goto reuse;
1359
ba3c4ce6
HY
1360 if (!trylock_page(page)) {
1361 get_page(page);
1362 spin_unlock(vmf->ptl);
1363 lock_page(page);
1364 spin_lock(vmf->ptl);
1365 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
3917c802 1366 spin_unlock(vmf->ptl);
ba3c4ce6
HY
1367 unlock_page(page);
1368 put_page(page);
3917c802 1369 return 0;
ba3c4ce6
HY
1370 }
1371 put_page(page);
1372 }
3917c802 1373
6c287605
DH
1374 /* Recheck after temporarily dropping the PT lock. */
1375 if (PageAnonExclusive(page)) {
1376 unlock_page(page);
1377 goto reuse;
1378 }
1379
3917c802 1380 /*
c89357e2 1381 * See do_wp_page(): we can only reuse the page exclusively if there are
3bff7e3f
DH
1382 * no additional references. Note that we always drain the LRU
1383 * pagevecs immediately after adding a THP.
3917c802 1384 */
3bff7e3f
DH
1385 if (page_count(page) > 1 + PageSwapCache(page) * thp_nr_pages(page))
1386 goto unlock_fallback;
1387 if (PageSwapCache(page))
1388 try_to_free_swap(page);
1389 if (page_count(page) == 1) {
71e3aac0 1390 pmd_t entry;
6c54dc6c
DH
1391
1392 page_move_anon_rmap(page, vma);
6c287605
DH
1393 unlock_page(page);
1394reuse:
c89357e2
DH
1395 if (unlikely(unshare)) {
1396 spin_unlock(vmf->ptl);
1397 return 0;
1398 }
71e3aac0 1399 entry = pmd_mkyoung(orig_pmd);
f55e1014 1400 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
3917c802 1401 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
82b0f8c3 1402 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
82b0f8c3 1403 spin_unlock(vmf->ptl);
3917c802 1404 return VM_FAULT_WRITE;
71e3aac0 1405 }
3917c802 1406
3bff7e3f 1407unlock_fallback:
3917c802 1408 unlock_page(page);
82b0f8c3 1409 spin_unlock(vmf->ptl);
3917c802
KS
1410fallback:
1411 __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
1412 return VM_FAULT_FALLBACK;
71e3aac0
AA
1413}
1414
8310d48b 1415/*
a308c71b
PX
1416 * FOLL_FORCE can write to even unwritable pmd's, but only
1417 * after we've gone through a COW cycle and they are dirty.
8310d48b
KF
1418 */
1419static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
1420{
a308c71b
PX
1421 return pmd_write(pmd) ||
1422 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
8310d48b
KF
1423}
1424
b676b293 1425struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
71e3aac0
AA
1426 unsigned long addr,
1427 pmd_t *pmd,
1428 unsigned int flags)
1429{
b676b293 1430 struct mm_struct *mm = vma->vm_mm;
71e3aac0
AA
1431 struct page *page = NULL;
1432
c4088ebd 1433 assert_spin_locked(pmd_lockptr(mm, pmd));
71e3aac0 1434
8310d48b 1435 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
71e3aac0
AA
1436 goto out;
1437
85facf25
KS
1438 /* Avoid dumping huge zero page */
1439 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1440 return ERR_PTR(-EFAULT);
1441
2b4847e7 1442 /* Full NUMA hinting faults to serialise migration in fault paths */
8a0516ed 1443 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
2b4847e7
MG
1444 goto out;
1445
71e3aac0 1446 page = pmd_page(*pmd);
ca120cf6 1447 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
3faa52c0 1448
a7f22660
DH
1449 if (!pmd_write(*pmd) && gup_must_unshare(flags, page))
1450 return ERR_PTR(-EMLINK);
1451
b6a2619c
DH
1452 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
1453 !PageAnonExclusive(page), page);
1454
3faa52c0
JH
1455 if (!try_grab_page(page, flags))
1456 return ERR_PTR(-ENOMEM);
1457
3565fce3 1458 if (flags & FOLL_TOUCH)
a8f97366 1459 touch_pmd(vma, addr, pmd, flags);
3faa52c0 1460
71e3aac0 1461 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
ca120cf6 1462 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
71e3aac0
AA
1463
1464out:
1465 return page;
1466}
1467
d10e63f2 1468/* NUMA hinting page fault entry point for trans huge pmds */
5db4f15c 1469vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
d10e63f2 1470{
82b0f8c3 1471 struct vm_area_struct *vma = vmf->vma;
c5b5a3dd
YS
1472 pmd_t oldpmd = vmf->orig_pmd;
1473 pmd_t pmd;
b32967ff 1474 struct page *page;
82b0f8c3 1475 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
c5b5a3dd 1476 int page_nid = NUMA_NO_NODE;
90572890 1477 int target_nid, last_cpupid = -1;
8191acbd 1478 bool migrated = false;
c5b5a3dd 1479 bool was_writable = pmd_savedwrite(oldpmd);
6688cc05 1480 int flags = 0;
d10e63f2 1481
82b0f8c3 1482 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
c5b5a3dd 1483 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
82b0f8c3 1484 spin_unlock(vmf->ptl);
de466bd6
MG
1485 goto out;
1486 }
1487
c5b5a3dd
YS
1488 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1489 page = vm_normal_page_pmd(vma, haddr, pmd);
1490 if (!page)
1491 goto out_map;
1492
1493 /* See similar comment in do_numa_page for explanation */
1494 if (!was_writable)
1495 flags |= TNF_NO_GROUP;
1496
1497 page_nid = page_to_nid(page);
1498 last_cpupid = page_cpupid_last(page);
1499 target_nid = numa_migrate_prep(page, vma, haddr, page_nid,
1500 &flags);
1501
1502 if (target_nid == NUMA_NO_NODE) {
1503 put_page(page);
1504 goto out_map;
1505 }
1506
82b0f8c3 1507 spin_unlock(vmf->ptl);
8b1b436d 1508
c5b5a3dd 1509 migrated = migrate_misplaced_page(page, vma, target_nid);
6688cc05
PZ
1510 if (migrated) {
1511 flags |= TNF_MIGRATED;
8191acbd 1512 page_nid = target_nid;
c5b5a3dd 1513 } else {
074c2381 1514 flags |= TNF_MIGRATE_FAIL;
c5b5a3dd
YS
1515 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1516 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
1517 spin_unlock(vmf->ptl);
1518 goto out;
1519 }
1520 goto out_map;
1521 }
b8916634
MG
1522
1523out:
98fa15f3 1524 if (page_nid != NUMA_NO_NODE)
82b0f8c3 1525 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
9a8b300f 1526 flags);
8191acbd 1527
d10e63f2 1528 return 0;
c5b5a3dd
YS
1529
1530out_map:
1531 /* Restore the PMD */
1532 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1533 pmd = pmd_mkyoung(pmd);
1534 if (was_writable)
1535 pmd = pmd_mkwrite(pmd);
1536 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1537 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1538 spin_unlock(vmf->ptl);
1539 goto out;
d10e63f2
MG
1540}
1541
319904ad
HY
1542/*
1543 * Return true if we do MADV_FREE successfully on entire pmd page.
1544 * Otherwise, return false.
1545 */
1546bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
b8d3c4c3 1547 pmd_t *pmd, unsigned long addr, unsigned long next)
b8d3c4c3
MK
1548{
1549 spinlock_t *ptl;
1550 pmd_t orig_pmd;
1551 struct page *page;
1552 struct mm_struct *mm = tlb->mm;
319904ad 1553 bool ret = false;
b8d3c4c3 1554
ed6a7935 1555 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
07e32661 1556
b6ec57f4
KS
1557 ptl = pmd_trans_huge_lock(pmd, vma);
1558 if (!ptl)
25eedabe 1559 goto out_unlocked;
b8d3c4c3
MK
1560
1561 orig_pmd = *pmd;
319904ad 1562 if (is_huge_zero_pmd(orig_pmd))
b8d3c4c3 1563 goto out;
b8d3c4c3 1564
84c3fc4e
ZY
1565 if (unlikely(!pmd_present(orig_pmd))) {
1566 VM_BUG_ON(thp_migration_supported() &&
1567 !is_pmd_migration_entry(orig_pmd));
1568 goto out;
1569 }
1570
b8d3c4c3
MK
1571 page = pmd_page(orig_pmd);
1572 /*
1573 * If other processes are mapping this page, we couldn't discard
1574 * the page unless they all do MADV_FREE so let's skip the page.
1575 */
babbbdd0 1576 if (total_mapcount(page) != 1)
b8d3c4c3
MK
1577 goto out;
1578
1579 if (!trylock_page(page))
1580 goto out;
1581
1582 /*
1583 * If user want to discard part-pages of THP, split it so MADV_FREE
1584 * will deactivate only them.
1585 */
1586 if (next - addr != HPAGE_PMD_SIZE) {
1587 get_page(page);
1588 spin_unlock(ptl);
9818b8cd 1589 split_huge_page(page);
b8d3c4c3 1590 unlock_page(page);
bbf29ffc 1591 put_page(page);
b8d3c4c3
MK
1592 goto out_unlocked;
1593 }
1594
1595 if (PageDirty(page))
1596 ClearPageDirty(page);
1597 unlock_page(page);
1598
b8d3c4c3 1599 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
58ceeb6b 1600 pmdp_invalidate(vma, addr, pmd);
b8d3c4c3
MK
1601 orig_pmd = pmd_mkold(orig_pmd);
1602 orig_pmd = pmd_mkclean(orig_pmd);
1603
1604 set_pmd_at(mm, addr, pmd, orig_pmd);
1605 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1606 }
802a3a92
SL
1607
1608 mark_page_lazyfree(page);
319904ad 1609 ret = true;
b8d3c4c3
MK
1610out:
1611 spin_unlock(ptl);
1612out_unlocked:
1613 return ret;
1614}
1615
953c66c2
AK
1616static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1617{
1618 pgtable_t pgtable;
1619
1620 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1621 pte_free(mm, pgtable);
c4812909 1622 mm_dec_nr_ptes(mm);
953c66c2
AK
1623}
1624
71e3aac0 1625int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
f21760b1 1626 pmd_t *pmd, unsigned long addr)
71e3aac0 1627{
da146769 1628 pmd_t orig_pmd;
bf929152 1629 spinlock_t *ptl;
71e3aac0 1630
ed6a7935 1631 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
07e32661 1632
b6ec57f4
KS
1633 ptl = __pmd_trans_huge_lock(pmd, vma);
1634 if (!ptl)
da146769
KS
1635 return 0;
1636 /*
1637 * For architectures like ppc64 we look at deposited pgtable
1638 * when calling pmdp_huge_get_and_clear. So do the
1639 * pgtable_trans_huge_withdraw after finishing pmdp related
1640 * operations.
1641 */
93a98695
AK
1642 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
1643 tlb->fullmm);
da146769 1644 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
2484ca9b 1645 if (vma_is_special_huge(vma)) {
3b6521f5
OH
1646 if (arch_needs_pgtable_deposit())
1647 zap_deposited_table(tlb->mm, pmd);
da146769 1648 spin_unlock(ptl);
da146769 1649 } else if (is_huge_zero_pmd(orig_pmd)) {
c14a6eb4 1650 zap_deposited_table(tlb->mm, pmd);
da146769 1651 spin_unlock(ptl);
da146769 1652 } else {
616b8371
ZY
1653 struct page *page = NULL;
1654 int flush_needed = 1;
1655
1656 if (pmd_present(orig_pmd)) {
1657 page = pmd_page(orig_pmd);
cea86fe2 1658 page_remove_rmap(page, vma, true);
616b8371
ZY
1659 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1660 VM_BUG_ON_PAGE(!PageHead(page), page);
1661 } else if (thp_migration_supported()) {
1662 swp_entry_t entry;
1663
1664 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1665 entry = pmd_to_swp_entry(orig_pmd);
af5cdaf8 1666 page = pfn_swap_entry_to_page(entry);
616b8371
ZY
1667 flush_needed = 0;
1668 } else
1669 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1670
b5072380 1671 if (PageAnon(page)) {
c14a6eb4 1672 zap_deposited_table(tlb->mm, pmd);
b5072380
KS
1673 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1674 } else {
953c66c2
AK
1675 if (arch_needs_pgtable_deposit())
1676 zap_deposited_table(tlb->mm, pmd);
fadae295 1677 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
b5072380 1678 }
616b8371 1679
da146769 1680 spin_unlock(ptl);
616b8371
ZY
1681 if (flush_needed)
1682 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
025c5b24 1683 }
da146769 1684 return 1;
71e3aac0
AA
1685}
1686
1dd38b6c
AK
1687#ifndef pmd_move_must_withdraw
1688static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1689 spinlock_t *old_pmd_ptl,
1690 struct vm_area_struct *vma)
1691{
1692 /*
1693 * With split pmd lock we also need to move preallocated
1694 * PTE page table if new_pmd is on different PMD page table.
1695 *
1696 * We also don't deposit and withdraw tables for file pages.
1697 */
1698 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1699}
1700#endif
1701
ab6e3d09
NH
1702static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1703{
1704#ifdef CONFIG_MEM_SOFT_DIRTY
1705 if (unlikely(is_pmd_migration_entry(pmd)))
1706 pmd = pmd_swp_mksoft_dirty(pmd);
1707 else if (pmd_present(pmd))
1708 pmd = pmd_mksoft_dirty(pmd);
1709#endif
1710 return pmd;
1711}
1712
bf8616d5 1713bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
b8aa9d9d 1714 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
37a1c49a 1715{
bf929152 1716 spinlock_t *old_ptl, *new_ptl;
37a1c49a 1717 pmd_t pmd;
37a1c49a 1718 struct mm_struct *mm = vma->vm_mm;
5d190420 1719 bool force_flush = false;
37a1c49a 1720
37a1c49a
AA
1721 /*
1722 * The destination pmd shouldn't be established, free_pgtables()
1723 * should have release it.
1724 */
1725 if (WARN_ON(!pmd_none(*new_pmd))) {
1726 VM_BUG_ON(pmd_trans_huge(*new_pmd));
4b471e88 1727 return false;
37a1c49a
AA
1728 }
1729
bf929152
KS
1730 /*
1731 * We don't have to worry about the ordering of src and dst
c1e8d7c6 1732 * ptlocks because exclusive mmap_lock prevents deadlock.
bf929152 1733 */
b6ec57f4
KS
1734 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1735 if (old_ptl) {
bf929152
KS
1736 new_ptl = pmd_lockptr(mm, new_pmd);
1737 if (new_ptl != old_ptl)
1738 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
8809aa2d 1739 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
eb66ae03 1740 if (pmd_present(pmd))
a2ce2666 1741 force_flush = true;
025c5b24 1742 VM_BUG_ON(!pmd_none(*new_pmd));
3592806c 1743
1dd38b6c 1744 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
b3084f4d 1745 pgtable_t pgtable;
3592806c
KS
1746 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1747 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
3592806c 1748 }
ab6e3d09
NH
1749 pmd = move_soft_dirty_pmd(pmd);
1750 set_pmd_at(mm, new_addr, new_pmd, pmd);
5d190420
AL
1751 if (force_flush)
1752 flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
eb66ae03
LT
1753 if (new_ptl != old_ptl)
1754 spin_unlock(new_ptl);
bf929152 1755 spin_unlock(old_ptl);
4b471e88 1756 return true;
37a1c49a 1757 }
4b471e88 1758 return false;
37a1c49a
AA
1759}
1760
f123d74a
MG
1761/*
1762 * Returns
1763 * - 0 if PMD could not be locked
f0953a1b 1764 * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary
e346e668 1765 * or if prot_numa but THP migration is not supported
f0953a1b 1766 * - HPAGE_PMD_NR if protections changed and TLB flush necessary
f123d74a 1767 */
4a18419f
NA
1768int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1769 pmd_t *pmd, unsigned long addr, pgprot_t newprot,
1770 unsigned long cp_flags)
cd7548ab
JW
1771{
1772 struct mm_struct *mm = vma->vm_mm;
bf929152 1773 spinlock_t *ptl;
c9fe6656 1774 pmd_t oldpmd, entry;
0a85e51d
KS
1775 bool preserve_write;
1776 int ret;
58705444 1777 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
292924b2
PX
1778 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
1779 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
cd7548ab 1780
4a18419f
NA
1781 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1782
e346e668
YS
1783 if (prot_numa && !thp_migration_supported())
1784 return 1;
1785
b6ec57f4 1786 ptl = __pmd_trans_huge_lock(pmd, vma);
0a85e51d
KS
1787 if (!ptl)
1788 return 0;
e944fd67 1789
0a85e51d
KS
1790 preserve_write = prot_numa && pmd_write(*pmd);
1791 ret = 1;
e944fd67 1792
84c3fc4e
ZY
1793#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1794 if (is_swap_pmd(*pmd)) {
1795 swp_entry_t entry = pmd_to_swp_entry(*pmd);
6c287605 1796 struct page *page = pfn_swap_entry_to_page(entry);
84c3fc4e
ZY
1797
1798 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
4dd845b5 1799 if (is_writable_migration_entry(entry)) {
84c3fc4e
ZY
1800 pmd_t newpmd;
1801 /*
1802 * A protection check is difficult so
1803 * just be safe and disable write
1804 */
6c287605
DH
1805 if (PageAnon(page))
1806 entry = make_readable_exclusive_migration_entry(swp_offset(entry));
1807 else
1808 entry = make_readable_migration_entry(swp_offset(entry));
84c3fc4e 1809 newpmd = swp_entry_to_pmd(entry);
ab6e3d09
NH
1810 if (pmd_swp_soft_dirty(*pmd))
1811 newpmd = pmd_swp_mksoft_dirty(newpmd);
8f34f1ea
PX
1812 if (pmd_swp_uffd_wp(*pmd))
1813 newpmd = pmd_swp_mkuffd_wp(newpmd);
84c3fc4e
ZY
1814 set_pmd_at(mm, addr, pmd, newpmd);
1815 }
1816 goto unlock;
1817 }
1818#endif
1819
a1a3a2fc
HY
1820 if (prot_numa) {
1821 struct page *page;
1822 /*
1823 * Avoid trapping faults against the zero page. The read-only
1824 * data is likely to be read-cached on the local CPU and
1825 * local/remote hits to the zero page are not interesting.
1826 */
1827 if (is_huge_zero_pmd(*pmd))
1828 goto unlock;
025c5b24 1829
a1a3a2fc
HY
1830 if (pmd_protnone(*pmd))
1831 goto unlock;
0a85e51d 1832
a1a3a2fc
HY
1833 page = pmd_page(*pmd);
1834 /*
1835 * Skip scanning top tier node if normal numa
1836 * balancing is disabled
1837 */
1838 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
1839 node_is_toptier(page_to_nid(page)))
1840 goto unlock;
1841 }
ced10803 1842 /*
3e4e28c5 1843 * In case prot_numa, we are under mmap_read_lock(mm). It's critical
ced10803 1844 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
3e4e28c5 1845 * which is also under mmap_read_lock(mm):
ced10803
KS
1846 *
1847 * CPU0: CPU1:
1848 * change_huge_pmd(prot_numa=1)
1849 * pmdp_huge_get_and_clear_notify()
1850 * madvise_dontneed()
1851 * zap_pmd_range()
1852 * pmd_trans_huge(*pmd) == 0 (without ptl)
1853 * // skip the pmd
1854 * set_pmd_at();
1855 * // pmd is re-established
1856 *
1857 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1858 * which may break userspace.
1859 *
4f831457 1860 * pmdp_invalidate_ad() is required to make sure we don't miss
ced10803
KS
1861 * dirty/young flags set by hardware.
1862 */
4f831457 1863 oldpmd = pmdp_invalidate_ad(vma, addr, pmd);
ced10803 1864
c9fe6656 1865 entry = pmd_modify(oldpmd, newprot);
0a85e51d
KS
1866 if (preserve_write)
1867 entry = pmd_mk_savedwrite(entry);
292924b2
PX
1868 if (uffd_wp) {
1869 entry = pmd_wrprotect(entry);
1870 entry = pmd_mkuffd_wp(entry);
1871 } else if (uffd_wp_resolve) {
1872 /*
1873 * Leave the write bit to be handled by PF interrupt
1874 * handler, then things like COW could be properly
1875 * handled.
1876 */
1877 entry = pmd_clear_uffd_wp(entry);
1878 }
0a85e51d
KS
1879 ret = HPAGE_PMD_NR;
1880 set_pmd_at(mm, addr, pmd, entry);
4a18419f 1881
c9fe6656
NA
1882 if (huge_pmd_needs_flush(oldpmd, entry))
1883 tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE);
4a18419f 1884
0a85e51d
KS
1885 BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
1886unlock:
1887 spin_unlock(ptl);
025c5b24
NH
1888 return ret;
1889}
1890
1891/*
8f19b0c0 1892 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
025c5b24 1893 *
8f19b0c0
HY
1894 * Note that if it returns page table lock pointer, this routine returns without
1895 * unlocking page table lock. So callers must unlock it.
025c5b24 1896 */
b6ec57f4 1897spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
025c5b24 1898{
b6ec57f4
KS
1899 spinlock_t *ptl;
1900 ptl = pmd_lock(vma->vm_mm, pmd);
84c3fc4e
ZY
1901 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
1902 pmd_devmap(*pmd)))
b6ec57f4
KS
1903 return ptl;
1904 spin_unlock(ptl);
1905 return NULL;
cd7548ab
JW
1906}
1907
a00cc7d9
MW
1908/*
1909 * Returns true if a given pud maps a thp, false otherwise.
1910 *
1911 * Note that if it returns true, this routine returns without unlocking page
1912 * table lock. So callers must unlock it.
1913 */
1914spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
1915{
1916 spinlock_t *ptl;
1917
1918 ptl = pud_lock(vma->vm_mm, pud);
1919 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
1920 return ptl;
1921 spin_unlock(ptl);
1922 return NULL;
1923}
1924
1925#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1926int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
1927 pud_t *pud, unsigned long addr)
1928{
a00cc7d9
MW
1929 spinlock_t *ptl;
1930
1931 ptl = __pud_trans_huge_lock(pud, vma);
1932 if (!ptl)
1933 return 0;
1934 /*
1935 * For architectures like ppc64 we look at deposited pgtable
1936 * when calling pudp_huge_get_and_clear. So do the
1937 * pgtable_trans_huge_withdraw after finishing pudp related
1938 * operations.
1939 */
70516b93 1940 pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
a00cc7d9 1941 tlb_remove_pud_tlb_entry(tlb, pud, addr);
2484ca9b 1942 if (vma_is_special_huge(vma)) {
a00cc7d9
MW
1943 spin_unlock(ptl);
1944 /* No zero page support yet */
1945 } else {
1946 /* No support for anonymous PUD pages yet */
1947 BUG();
1948 }
1949 return 1;
1950}
1951
1952static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
1953 unsigned long haddr)
1954{
1955 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
1956 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1957 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
1958 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
1959
ce9311cf 1960 count_vm_event(THP_SPLIT_PUD);
a00cc7d9
MW
1961
1962 pudp_huge_clear_flush_notify(vma, haddr, pud);
1963}
1964
1965void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
1966 unsigned long address)
1967{
1968 spinlock_t *ptl;
ac46d4f3 1969 struct mmu_notifier_range range;
a00cc7d9 1970
7269f999 1971 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 1972 address & HPAGE_PUD_MASK,
ac46d4f3
JG
1973 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
1974 mmu_notifier_invalidate_range_start(&range);
1975 ptl = pud_lock(vma->vm_mm, pud);
a00cc7d9
MW
1976 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
1977 goto out;
ac46d4f3 1978 __split_huge_pud_locked(vma, pud, range.start);
a00cc7d9
MW
1979
1980out:
1981 spin_unlock(ptl);
4645b9fe
JG
1982 /*
1983 * No need to double call mmu_notifier->invalidate_range() callback as
1984 * the above pudp_huge_clear_flush_notify() did already call it.
1985 */
ac46d4f3 1986 mmu_notifier_invalidate_range_only_end(&range);
a00cc7d9
MW
1987}
1988#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1989
eef1b3ba
KS
1990static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
1991 unsigned long haddr, pmd_t *pmd)
1992{
1993 struct mm_struct *mm = vma->vm_mm;
1994 pgtable_t pgtable;
1995 pmd_t _pmd;
1996 int i;
1997
0f10851e
JG
1998 /*
1999 * Leave pmd empty until pte is filled note that it is fine to delay
2000 * notification until mmu_notifier_invalidate_range_end() as we are
2001 * replacing a zero pmd write protected page with a zero pte write
2002 * protected page.
2003 *
ee65728e 2004 * See Documentation/mm/mmu_notifier.rst
0f10851e
JG
2005 */
2006 pmdp_huge_clear_flush(vma, haddr, pmd);
eef1b3ba
KS
2007
2008 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2009 pmd_populate(mm, &_pmd, pgtable);
2010
2011 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2012 pte_t *pte, entry;
2013 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2014 entry = pte_mkspecial(entry);
2015 pte = pte_offset_map(&_pmd, haddr);
2016 VM_BUG_ON(!pte_none(*pte));
2017 set_pte_at(mm, haddr, pte, entry);
2018 pte_unmap(pte);
2019 }
2020 smp_wmb(); /* make pte visible before pmd */
2021 pmd_populate(mm, pmd, pgtable);
eef1b3ba
KS
2022}
2023
2024static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
ba988280 2025 unsigned long haddr, bool freeze)
eef1b3ba
KS
2026{
2027 struct mm_struct *mm = vma->vm_mm;
2028 struct page *page;
2029 pgtable_t pgtable;
423ac9af 2030 pmd_t old_pmd, _pmd;
292924b2 2031 bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
6c287605 2032 bool anon_exclusive = false;
2ac015e2 2033 unsigned long addr;
eef1b3ba
KS
2034 int i;
2035
2036 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2037 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2038 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
84c3fc4e
ZY
2039 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
2040 && !pmd_devmap(*pmd));
eef1b3ba
KS
2041
2042 count_vm_event(THP_SPLIT_PMD);
2043
d21b9e57 2044 if (!vma_is_anonymous(vma)) {
99fa8a48 2045 old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
953c66c2
AK
2046 /*
2047 * We are going to unmap this huge page. So
2048 * just go ahead and zap it
2049 */
2050 if (arch_needs_pgtable_deposit())
2051 zap_deposited_table(mm, pmd);
2484ca9b 2052 if (vma_is_special_huge(vma))
d21b9e57 2053 return;
99fa8a48
HD
2054 if (unlikely(is_pmd_migration_entry(old_pmd))) {
2055 swp_entry_t entry;
2056
2057 entry = pmd_to_swp_entry(old_pmd);
af5cdaf8 2058 page = pfn_swap_entry_to_page(entry);
99fa8a48
HD
2059 } else {
2060 page = pmd_page(old_pmd);
2061 if (!PageDirty(page) && pmd_dirty(old_pmd))
2062 set_page_dirty(page);
2063 if (!PageReferenced(page) && pmd_young(old_pmd))
2064 SetPageReferenced(page);
cea86fe2 2065 page_remove_rmap(page, vma, true);
99fa8a48
HD
2066 put_page(page);
2067 }
fadae295 2068 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
eef1b3ba 2069 return;
99fa8a48
HD
2070 }
2071
3b77e8c8 2072 if (is_huge_zero_pmd(*pmd)) {
4645b9fe
JG
2073 /*
2074 * FIXME: Do we want to invalidate secondary mmu by calling
2075 * mmu_notifier_invalidate_range() see comments below inside
2076 * __split_huge_pmd() ?
2077 *
2078 * We are going from a zero huge page write protected to zero
2079 * small page also write protected so it does not seems useful
2080 * to invalidate secondary mmu at this time.
2081 */
eef1b3ba
KS
2082 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2083 }
2084
423ac9af
AK
2085 /*
2086 * Up to this point the pmd is present and huge and userland has the
2087 * whole access to the hugepage during the split (which happens in
2088 * place). If we overwrite the pmd with the not-huge version pointing
2089 * to the pte here (which of course we could if all CPUs were bug
2090 * free), userland could trigger a small page size TLB miss on the
2091 * small sized TLB while the hugepage TLB entry is still established in
2092 * the huge TLB. Some CPU doesn't like that.
42742d9b
AK
2093 * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
2094 * 383 on page 105. Intel should be safe but is also warns that it's
423ac9af
AK
2095 * only safe if the permission and cache attributes of the two entries
2096 * loaded in the two TLB is identical (which should be the case here).
2097 * But it is generally safer to never allow small and huge TLB entries
2098 * for the same virtual address to be loaded simultaneously. So instead
2099 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2100 * current pmd notpresent (atomically because here the pmd_trans_huge
2101 * must remain set at all times on the pmd until the split is complete
2102 * for this pmd), then we flush the SMP TLB and finally we write the
2103 * non-huge version of the pmd entry with pmd_populate.
2104 */
2105 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2106
423ac9af 2107 pmd_migration = is_pmd_migration_entry(old_pmd);
2e83ee1d 2108 if (unlikely(pmd_migration)) {
84c3fc4e
ZY
2109 swp_entry_t entry;
2110
423ac9af 2111 entry = pmd_to_swp_entry(old_pmd);
af5cdaf8 2112 page = pfn_swap_entry_to_page(entry);
4dd845b5 2113 write = is_writable_migration_entry(entry);
6c287605
DH
2114 if (PageAnon(page))
2115 anon_exclusive = is_readable_exclusive_migration_entry(entry);
2e83ee1d
PX
2116 young = false;
2117 soft_dirty = pmd_swp_soft_dirty(old_pmd);
f45ec5ff 2118 uffd_wp = pmd_swp_uffd_wp(old_pmd);
2e83ee1d 2119 } else {
423ac9af 2120 page = pmd_page(old_pmd);
2e83ee1d
PX
2121 if (pmd_dirty(old_pmd))
2122 SetPageDirty(page);
2123 write = pmd_write(old_pmd);
2124 young = pmd_young(old_pmd);
2125 soft_dirty = pmd_soft_dirty(old_pmd);
292924b2 2126 uffd_wp = pmd_uffd_wp(old_pmd);
6c287605 2127
9d84604b
HD
2128 VM_BUG_ON_PAGE(!page_count(page), page);
2129 page_ref_add(page, HPAGE_PMD_NR - 1);
6c287605
DH
2130
2131 /*
2132 * Without "freeze", we'll simply split the PMD, propagating the
2133 * PageAnonExclusive() flag for each PTE by setting it for
2134 * each subpage -- no need to (temporarily) clear.
2135 *
2136 * With "freeze" we want to replace mapped pages by
2137 * migration entries right away. This is only possible if we
2138 * managed to clear PageAnonExclusive() -- see
2139 * set_pmd_migration_entry().
2140 *
2141 * In case we cannot clear PageAnonExclusive(), split the PMD
2142 * only and let try_to_migrate_one() fail later.
2143 */
2144 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
2145 if (freeze && anon_exclusive && page_try_share_anon_rmap(page))
2146 freeze = false;
2e83ee1d 2147 }
eef1b3ba 2148
423ac9af
AK
2149 /*
2150 * Withdraw the table only after we mark the pmd entry invalid.
2151 * This's critical for some architectures (Power).
2152 */
eef1b3ba
KS
2153 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2154 pmd_populate(mm, &_pmd, pgtable);
2155
2ac015e2 2156 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
eef1b3ba
KS
2157 pte_t entry, *pte;
2158 /*
2159 * Note that NUMA hinting access restrictions are not
2160 * transferred to avoid any possibility of altering
2161 * permissions across VMAs.
2162 */
84c3fc4e 2163 if (freeze || pmd_migration) {
ba988280 2164 swp_entry_t swp_entry;
4dd845b5
AP
2165 if (write)
2166 swp_entry = make_writable_migration_entry(
2167 page_to_pfn(page + i));
6c287605
DH
2168 else if (anon_exclusive)
2169 swp_entry = make_readable_exclusive_migration_entry(
2170 page_to_pfn(page + i));
4dd845b5
AP
2171 else
2172 swp_entry = make_readable_migration_entry(
2173 page_to_pfn(page + i));
ba988280 2174 entry = swp_entry_to_pte(swp_entry);
804dd150
AA
2175 if (soft_dirty)
2176 entry = pte_swp_mksoft_dirty(entry);
f45ec5ff
PX
2177 if (uffd_wp)
2178 entry = pte_swp_mkuffd_wp(entry);
ba988280 2179 } else {
6d2329f8 2180 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
b8d3c4c3 2181 entry = maybe_mkwrite(entry, vma);
6c287605
DH
2182 if (anon_exclusive)
2183 SetPageAnonExclusive(page + i);
ba988280
KS
2184 if (!write)
2185 entry = pte_wrprotect(entry);
2186 if (!young)
2187 entry = pte_mkold(entry);
804dd150
AA
2188 if (soft_dirty)
2189 entry = pte_mksoft_dirty(entry);
292924b2
PX
2190 if (uffd_wp)
2191 entry = pte_mkuffd_wp(entry);
ba988280 2192 }
2ac015e2 2193 pte = pte_offset_map(&_pmd, addr);
eef1b3ba 2194 BUG_ON(!pte_none(*pte));
2ac015e2 2195 set_pte_at(mm, addr, pte, entry);
ec0abae6 2196 if (!pmd_migration)
eef1b3ba 2197 atomic_inc(&page[i]._mapcount);
ec0abae6 2198 pte_unmap(pte);
eef1b3ba
KS
2199 }
2200
ec0abae6
RC
2201 if (!pmd_migration) {
2202 /*
2203 * Set PG_double_map before dropping compound_mapcount to avoid
2204 * false-negative page_mapped().
2205 */
2206 if (compound_mapcount(page) > 1 &&
2207 !TestSetPageDoubleMap(page)) {
eef1b3ba 2208 for (i = 0; i < HPAGE_PMD_NR; i++)
ec0abae6
RC
2209 atomic_inc(&page[i]._mapcount);
2210 }
2211
2212 lock_page_memcg(page);
2213 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2214 /* Last compound_mapcount is gone. */
69473e5d
MS
2215 __mod_lruvec_page_state(page, NR_ANON_THPS,
2216 -HPAGE_PMD_NR);
ec0abae6
RC
2217 if (TestClearPageDoubleMap(page)) {
2218 /* No need in mapcount reference anymore */
2219 for (i = 0; i < HPAGE_PMD_NR; i++)
2220 atomic_dec(&page[i]._mapcount);
2221 }
eef1b3ba 2222 }
ec0abae6 2223 unlock_page_memcg(page);
cea86fe2
HD
2224
2225 /* Above is effectively page_remove_rmap(page, vma, true) */
2226 munlock_vma_page(page, vma, true);
eef1b3ba
KS
2227 }
2228
2229 smp_wmb(); /* make pte visible before pmd */
2230 pmd_populate(mm, pmd, pgtable);
e9b61f19
KS
2231
2232 if (freeze) {
2ac015e2 2233 for (i = 0; i < HPAGE_PMD_NR; i++) {
cea86fe2 2234 page_remove_rmap(page + i, vma, false);
e9b61f19
KS
2235 put_page(page + i);
2236 }
2237 }
eef1b3ba
KS
2238}
2239
2240void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
af28a988 2241 unsigned long address, bool freeze, struct folio *folio)
eef1b3ba
KS
2242{
2243 spinlock_t *ptl;
ac46d4f3 2244 struct mmu_notifier_range range;
eef1b3ba 2245
7269f999 2246 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 2247 address & HPAGE_PMD_MASK,
ac46d4f3
JG
2248 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2249 mmu_notifier_invalidate_range_start(&range);
2250 ptl = pmd_lock(vma->vm_mm, pmd);
33f4751e
NH
2251
2252 /*
af28a988
MWO
2253 * If caller asks to setup a migration entry, we need a folio to check
2254 * pmd against. Otherwise we can end up replacing wrong folio.
33f4751e 2255 */
af28a988 2256 VM_BUG_ON(freeze && !folio);
83a8441f 2257 VM_WARN_ON_ONCE(folio && !folio_test_locked(folio));
33f4751e 2258
7f760917 2259 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) ||
83a8441f
MWO
2260 is_pmd_migration_entry(*pmd)) {
2261 if (folio && folio != page_folio(pmd_page(*pmd)))
2262 goto out;
7f760917 2263 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
83a8441f 2264 }
7f760917 2265
e90309c9 2266out:
eef1b3ba 2267 spin_unlock(ptl);
4645b9fe
JG
2268 /*
2269 * No need to double call mmu_notifier->invalidate_range() callback.
2270 * They are 3 cases to consider inside __split_huge_pmd_locked():
2271 * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious
2272 * 2) __split_huge_zero_page_pmd() read only zero page and any write
2273 * fault will trigger a flush_notify before pointing to a new page
2274 * (it is fine if the secondary mmu keeps pointing to the old zero
2275 * page in the meantime)
2276 * 3) Split a huge pmd into pte pointing to the same page. No need
2277 * to invalidate secondary tlb entry they are all still valid.
2278 * any further changes to individual pte will notify. So no need
2279 * to call mmu_notifier->invalidate_range()
2280 */
ac46d4f3 2281 mmu_notifier_invalidate_range_only_end(&range);
eef1b3ba
KS
2282}
2283
fec89c10 2284void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
af28a988 2285 bool freeze, struct folio *folio)
94fcc585 2286{
f72e7dcd 2287 pgd_t *pgd;
c2febafc 2288 p4d_t *p4d;
f72e7dcd 2289 pud_t *pud;
94fcc585
AA
2290 pmd_t *pmd;
2291
78ddc534 2292 pgd = pgd_offset(vma->vm_mm, address);
f72e7dcd
HD
2293 if (!pgd_present(*pgd))
2294 return;
2295
c2febafc
KS
2296 p4d = p4d_offset(pgd, address);
2297 if (!p4d_present(*p4d))
2298 return;
2299
2300 pud = pud_offset(p4d, address);
f72e7dcd
HD
2301 if (!pud_present(*pud))
2302 return;
2303
2304 pmd = pmd_offset(pud, address);
fec89c10 2305
af28a988 2306 __split_huge_pmd(vma, pmd, address, freeze, folio);
94fcc585
AA
2307}
2308
71f9e58e
ML
2309static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
2310{
2311 /*
2312 * If the new address isn't hpage aligned and it could previously
2313 * contain an hugepage: check if we need to split an huge pmd.
2314 */
2315 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
2316 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
2317 ALIGN(address, HPAGE_PMD_SIZE)))
2318 split_huge_pmd_address(vma, address, false, NULL);
2319}
2320
e1b9996b 2321void vma_adjust_trans_huge(struct vm_area_struct *vma,
94fcc585
AA
2322 unsigned long start,
2323 unsigned long end,
2324 long adjust_next)
2325{
71f9e58e
ML
2326 /* Check if we need to split start first. */
2327 split_huge_pmd_if_needed(vma, start);
94fcc585 2328
71f9e58e
ML
2329 /* Check if we need to split end next. */
2330 split_huge_pmd_if_needed(vma, end);
94fcc585
AA
2331
2332 /*
71f9e58e
ML
2333 * If we're also updating the vma->vm_next->vm_start,
2334 * check if we need to split it.
94fcc585
AA
2335 */
2336 if (adjust_next > 0) {
2337 struct vm_area_struct *next = vma->vm_next;
2338 unsigned long nstart = next->vm_start;
f9d86a60 2339 nstart += adjust_next;
71f9e58e 2340 split_huge_pmd_if_needed(next, nstart);
94fcc585
AA
2341 }
2342}
e9b61f19 2343
906f9cdf 2344static void unmap_page(struct page *page)
e9b61f19 2345{
869f7ee6 2346 struct folio *folio = page_folio(page);
a98a2f0c
AP
2347 enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD |
2348 TTU_SYNC;
e9b61f19
KS
2349
2350 VM_BUG_ON_PAGE(!PageHead(page), page);
2351
a98a2f0c
AP
2352 /*
2353 * Anon pages need migration entries to preserve them, but file
2354 * pages can simply be left unmapped, then faulted back on demand.
2355 * If that is ever changed (perhaps for mlock), update remap_page().
2356 */
4b8554c5
MWO
2357 if (folio_test_anon(folio))
2358 try_to_migrate(folio, ttu_flags);
a98a2f0c 2359 else
869f7ee6 2360 try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK);
e9b61f19
KS
2361}
2362
4eecb8b9 2363static void remap_page(struct folio *folio, unsigned long nr)
e9b61f19 2364{
4eecb8b9 2365 int i = 0;
ab02c252 2366
64b586d1 2367 /* If unmap_page() uses try_to_migrate() on file, remove this check */
4eecb8b9 2368 if (!folio_test_anon(folio))
ab02c252 2369 return;
4eecb8b9
MWO
2370 for (;;) {
2371 remove_migration_ptes(folio, folio, true);
2372 i += folio_nr_pages(folio);
2373 if (i >= nr)
2374 break;
2375 folio = folio_next(folio);
ace71a19 2376 }
e9b61f19
KS
2377}
2378
94866635 2379static void lru_add_page_tail(struct page *head, struct page *tail,
88dcb9a3
AS
2380 struct lruvec *lruvec, struct list_head *list)
2381{
94866635
AS
2382 VM_BUG_ON_PAGE(!PageHead(head), head);
2383 VM_BUG_ON_PAGE(PageCompound(tail), head);
2384 VM_BUG_ON_PAGE(PageLRU(tail), head);
6168d0da 2385 lockdep_assert_held(&lruvec->lru_lock);
88dcb9a3 2386
6dbb5741 2387 if (list) {
88dcb9a3 2388 /* page reclaim is reclaiming a huge page */
6dbb5741 2389 VM_WARN_ON(PageLRU(head));
94866635
AS
2390 get_page(tail);
2391 list_add_tail(&tail->lru, list);
88dcb9a3 2392 } else {
6dbb5741
AS
2393 /* head is still on lru (and we have it frozen) */
2394 VM_WARN_ON(!PageLRU(head));
07ca7606
HD
2395 if (PageUnevictable(tail))
2396 tail->mlock_count = 0;
2397 else
2398 list_add_tail(&tail->lru, &head->lru);
6dbb5741 2399 SetPageLRU(tail);
88dcb9a3
AS
2400 }
2401}
2402
8df651c7 2403static void __split_huge_page_tail(struct page *head, int tail,
e9b61f19
KS
2404 struct lruvec *lruvec, struct list_head *list)
2405{
e9b61f19
KS
2406 struct page *page_tail = head + tail;
2407
8df651c7 2408 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
e9b61f19
KS
2409
2410 /*
605ca5ed
KK
2411 * Clone page flags before unfreezing refcount.
2412 *
2413 * After successful get_page_unless_zero() might follow flags change,
8958b249 2414 * for example lock_page() which set PG_waiters.
6c287605
DH
2415 *
2416 * Note that for mapped sub-pages of an anonymous THP,
2417 * PG_anon_exclusive has been cleared in unmap_page() and is stored in
2418 * the migration entry instead from where remap_page() will restore it.
2419 * We can still have PG_anon_exclusive set on effectively unmapped and
2420 * unreferenced sub-pages of an anonymous THP: we can simply drop
2421 * PG_anon_exclusive (-> PG_mappedtodisk) for these here.
e9b61f19 2422 */
e9b61f19
KS
2423 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2424 page_tail->flags |= (head->flags &
2425 ((1L << PG_referenced) |
2426 (1L << PG_swapbacked) |
38d8b4e6 2427 (1L << PG_swapcache) |
e9b61f19
KS
2428 (1L << PG_mlocked) |
2429 (1L << PG_uptodate) |
2430 (1L << PG_active) |
1899ad18 2431 (1L << PG_workingset) |
e9b61f19 2432 (1L << PG_locked) |
b8d3c4c3 2433 (1L << PG_unevictable) |
72e6afa0
CM
2434#ifdef CONFIG_64BIT
2435 (1L << PG_arch_2) |
2436#endif
b8d3c4c3 2437 (1L << PG_dirty)));
e9b61f19 2438
173d9d9f
HD
2439 /* ->mapping in first tail page is compound_mapcount */
2440 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2441 page_tail);
2442 page_tail->mapping = head->mapping;
2443 page_tail->index = head->index + tail;
b653db77 2444 page_tail->private = 0;
173d9d9f 2445
605ca5ed 2446 /* Page flags must be visible before we make the page non-compound. */
e9b61f19
KS
2447 smp_wmb();
2448
605ca5ed
KK
2449 /*
2450 * Clear PageTail before unfreezing page refcount.
2451 *
2452 * After successful get_page_unless_zero() might follow put_page()
2453 * which needs correct compound_head().
2454 */
e9b61f19
KS
2455 clear_compound_head(page_tail);
2456
605ca5ed
KK
2457 /* Finally unfreeze refcount. Additional reference from page cache. */
2458 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2459 PageSwapCache(head)));
2460
e9b61f19
KS
2461 if (page_is_young(head))
2462 set_page_young(page_tail);
2463 if (page_is_idle(head))
2464 set_page_idle(page_tail);
2465
e9b61f19 2466 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
94723aaf
MH
2467
2468 /*
2469 * always add to the tail because some iterators expect new
2470 * pages to show after the currently processed elements - e.g.
2471 * migrate_pages
2472 */
e9b61f19 2473 lru_add_page_tail(head, page_tail, lruvec, list);
e9b61f19
KS
2474}
2475
baa355fd 2476static void __split_huge_page(struct page *page, struct list_head *list,
b6769834 2477 pgoff_t end)
e9b61f19 2478{
e809c3fe
MWO
2479 struct folio *folio = page_folio(page);
2480 struct page *head = &folio->page;
e9b61f19 2481 struct lruvec *lruvec;
4101196b
MWO
2482 struct address_space *swap_cache = NULL;
2483 unsigned long offset = 0;
8cce5475 2484 unsigned int nr = thp_nr_pages(head);
8df651c7 2485 int i;
e9b61f19 2486
e9b61f19 2487 /* complete memcg works before add pages to LRU */
be6c8982 2488 split_page_memcg(head, nr);
e9b61f19 2489
4101196b
MWO
2490 if (PageAnon(head) && PageSwapCache(head)) {
2491 swp_entry_t entry = { .val = page_private(head) };
2492
2493 offset = swp_offset(entry);
2494 swap_cache = swap_address_space(entry);
2495 xa_lock(&swap_cache->i_pages);
2496 }
2497
f0953a1b 2498 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */
e809c3fe 2499 lruvec = folio_lruvec_lock(folio);
b6769834 2500
eac96c3e
YS
2501 ClearPageHasHWPoisoned(head);
2502
8cce5475 2503 for (i = nr - 1; i >= 1; i--) {
8df651c7 2504 __split_huge_page_tail(head, i, lruvec, list);
d144bf62 2505 /* Some pages can be beyond EOF: drop them from page cache */
baa355fd 2506 if (head[i].index >= end) {
2d077d4b 2507 ClearPageDirty(head + i);
baa355fd 2508 __delete_from_page_cache(head + i, NULL);
d144bf62 2509 if (shmem_mapping(head->mapping))
800d8c63 2510 shmem_uncharge(head->mapping->host, 1);
baa355fd 2511 put_page(head + i);
4101196b
MWO
2512 } else if (!PageAnon(page)) {
2513 __xa_store(&head->mapping->i_pages, head[i].index,
2514 head + i, 0);
2515 } else if (swap_cache) {
2516 __xa_store(&swap_cache->i_pages, offset + i,
2517 head + i, 0);
baa355fd
KS
2518 }
2519 }
e9b61f19
KS
2520
2521 ClearPageCompound(head);
6168d0da 2522 unlock_page_lruvec(lruvec);
b6769834 2523 /* Caller disabled irqs, so they are still disabled here */
f7da677b 2524
8cce5475 2525 split_page_owner(head, nr);
f7da677b 2526
baa355fd
KS
2527 /* See comment in __split_huge_page_tail() */
2528 if (PageAnon(head)) {
aa5dc07f 2529 /* Additional pin to swap cache */
4101196b 2530 if (PageSwapCache(head)) {
38d8b4e6 2531 page_ref_add(head, 2);
4101196b
MWO
2532 xa_unlock(&swap_cache->i_pages);
2533 } else {
38d8b4e6 2534 page_ref_inc(head);
4101196b 2535 }
baa355fd 2536 } else {
aa5dc07f 2537 /* Additional pin to page cache */
baa355fd 2538 page_ref_add(head, 2);
b93b0163 2539 xa_unlock(&head->mapping->i_pages);
baa355fd 2540 }
b6769834 2541 local_irq_enable();
e9b61f19 2542
4eecb8b9 2543 remap_page(folio, nr);
e9b61f19 2544
c4f9c701
HY
2545 if (PageSwapCache(head)) {
2546 swp_entry_t entry = { .val = page_private(head) };
2547
2548 split_swap_cluster(entry);
2549 }
2550
8cce5475 2551 for (i = 0; i < nr; i++) {
e9b61f19
KS
2552 struct page *subpage = head + i;
2553 if (subpage == page)
2554 continue;
2555 unlock_page(subpage);
2556
2557 /*
2558 * Subpages may be freed if there wasn't any mapping
2559 * like if add_to_swap() is running on a lru page that
2560 * had its mapping zapped. And freeing these pages
2561 * requires taking the lru_lock so we do the put_page
2562 * of the tail pages after the split is complete.
2563 */
2564 put_page(subpage);
2565 }
2566}
2567
b8f593cd 2568/* Racy check whether the huge page can be split */
d4b4084a 2569bool can_split_folio(struct folio *folio, int *pextra_pins)
b8f593cd
HY
2570{
2571 int extra_pins;
2572
aa5dc07f 2573 /* Additional pins from page cache */
d4b4084a
MWO
2574 if (folio_test_anon(folio))
2575 extra_pins = folio_test_swapcache(folio) ?
2576 folio_nr_pages(folio) : 0;
b8f593cd 2577 else
d4b4084a 2578 extra_pins = folio_nr_pages(folio);
b8f593cd
HY
2579 if (pextra_pins)
2580 *pextra_pins = extra_pins;
d4b4084a 2581 return folio_mapcount(folio) == folio_ref_count(folio) - extra_pins - 1;
b8f593cd
HY
2582}
2583
e9b61f19
KS
2584/*
2585 * This function splits huge page into normal pages. @page can point to any
2586 * subpage of huge page to split. Split doesn't change the position of @page.
2587 *
2588 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
2589 * The huge page must be locked.
2590 *
2591 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
2592 *
2593 * Both head page and tail pages will inherit mapping, flags, and so on from
2594 * the hugepage.
2595 *
2596 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
2597 * they are not mapped.
2598 *
2599 * Returns 0 if the hugepage is split successfully.
2600 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
2601 * us.
2602 */
2603int split_huge_page_to_list(struct page *page, struct list_head *list)
2604{
4eecb8b9
MWO
2605 struct folio *folio = page_folio(page);
2606 struct page *head = &folio->page;
a8803e6c 2607 struct deferred_split *ds_queue = get_deferred_split_queue(head);
6b24ca4a 2608 XA_STATE(xas, &head->mapping->i_pages, head->index);
baa355fd
KS
2609 struct anon_vma *anon_vma = NULL;
2610 struct address_space *mapping = NULL;
504e070d 2611 int extra_pins, ret;
006d3ff2 2612 pgoff_t end;
478d134e 2613 bool is_hzp;
e9b61f19 2614
a8803e6c
WY
2615 VM_BUG_ON_PAGE(!PageLocked(head), head);
2616 VM_BUG_ON_PAGE(!PageCompound(head), head);
e9b61f19 2617
478d134e
XY
2618 is_hzp = is_huge_zero_page(head);
2619 VM_WARN_ON_ONCE_PAGE(is_hzp, head);
2620 if (is_hzp)
2621 return -EBUSY;
2622
a8803e6c 2623 if (PageWriteback(head))
59807685
HY
2624 return -EBUSY;
2625
baa355fd
KS
2626 if (PageAnon(head)) {
2627 /*
c1e8d7c6 2628 * The caller does not necessarily hold an mmap_lock that would
baa355fd
KS
2629 * prevent the anon_vma disappearing so we first we take a
2630 * reference to it and then lock the anon_vma for write. This
2f031c6f 2631 * is similar to folio_lock_anon_vma_read except the write lock
baa355fd
KS
2632 * is taken to serialise against parallel split or collapse
2633 * operations.
2634 */
2635 anon_vma = page_get_anon_vma(head);
2636 if (!anon_vma) {
2637 ret = -EBUSY;
2638 goto out;
2639 }
006d3ff2 2640 end = -1;
baa355fd
KS
2641 mapping = NULL;
2642 anon_vma_lock_write(anon_vma);
2643 } else {
2644 mapping = head->mapping;
2645
2646 /* Truncated ? */
2647 if (!mapping) {
2648 ret = -EBUSY;
2649 goto out;
2650 }
2651
6b24ca4a
MWO
2652 xas_split_alloc(&xas, head, compound_order(head),
2653 mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK);
2654 if (xas_error(&xas)) {
2655 ret = xas_error(&xas);
2656 goto out;
2657 }
2658
baa355fd
KS
2659 anon_vma = NULL;
2660 i_mmap_lock_read(mapping);
006d3ff2
HD
2661
2662 /*
2663 *__split_huge_page() may need to trim off pages beyond EOF:
2664 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2665 * which cannot be nested inside the page tree lock. So note
2666 * end now: i_size itself may be changed at any moment, but
2667 * head page lock is good enough to serialize the trimming.
2668 */
2669 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
d144bf62
HD
2670 if (shmem_mapping(mapping))
2671 end = shmem_fallocend(mapping->host, end);
e9b61f19 2672 }
e9b61f19
KS
2673
2674 /*
906f9cdf 2675 * Racy check if we can split the page, before unmap_page() will
e9b61f19
KS
2676 * split PMDs
2677 */
d4b4084a 2678 if (!can_split_folio(folio, &extra_pins)) {
e9b61f19
KS
2679 ret = -EBUSY;
2680 goto out_unlock;
2681 }
2682
906f9cdf 2683 unmap_page(head);
e9b61f19 2684
b6769834
AS
2685 /* block interrupt reentry in xa_lock and spinlock */
2686 local_irq_disable();
baa355fd 2687 if (mapping) {
baa355fd 2688 /*
aa5dc07f 2689 * Check if the head page is present in page cache.
baa355fd
KS
2690 * We assume all tail are present too, if head is there.
2691 */
6b24ca4a
MWO
2692 xas_lock(&xas);
2693 xas_reset(&xas);
aa5dc07f 2694 if (xas_load(&xas) != head)
baa355fd
KS
2695 goto fail;
2696 }
2697
0139aa7b 2698 /* Prevent deferred_split_scan() touching ->_refcount */
364c1eeb 2699 spin_lock(&ds_queue->split_queue_lock);
504e070d 2700 if (page_ref_freeze(head, 1 + extra_pins)) {
9a982250 2701 if (!list_empty(page_deferred_list(head))) {
364c1eeb 2702 ds_queue->split_queue_len--;
9a982250
KS
2703 list_del(page_deferred_list(head));
2704 }
afb97172 2705 spin_unlock(&ds_queue->split_queue_lock);
06d3eff6 2706 if (mapping) {
bf9ecead
MS
2707 int nr = thp_nr_pages(head);
2708
6b24ca4a 2709 xas_split(&xas, head, thp_order(head));
1ca7554d 2710 if (PageSwapBacked(head)) {
57b2847d
MS
2711 __mod_lruvec_page_state(head, NR_SHMEM_THPS,
2712 -nr);
1ca7554d 2713 } else {
bf9ecead
MS
2714 __mod_lruvec_page_state(head, NR_FILE_THPS,
2715 -nr);
1ca7554d
MS
2716 filemap_nr_thps_dec(mapping);
2717 }
06d3eff6
KS
2718 }
2719
b6769834 2720 __split_huge_page(page, list, end);
c4f9c701 2721 ret = 0;
e9b61f19 2722 } else {
364c1eeb 2723 spin_unlock(&ds_queue->split_queue_lock);
504e070d
YS
2724fail:
2725 if (mapping)
6b24ca4a 2726 xas_unlock(&xas);
b6769834 2727 local_irq_enable();
4eecb8b9 2728 remap_page(folio, folio_nr_pages(folio));
e9b61f19
KS
2729 ret = -EBUSY;
2730 }
2731
2732out_unlock:
baa355fd
KS
2733 if (anon_vma) {
2734 anon_vma_unlock_write(anon_vma);
2735 put_anon_vma(anon_vma);
2736 }
2737 if (mapping)
2738 i_mmap_unlock_read(mapping);
e9b61f19 2739out:
69a37a8b 2740 xas_destroy(&xas);
e9b61f19
KS
2741 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2742 return ret;
2743}
9a982250
KS
2744
2745void free_transhuge_page(struct page *page)
2746{
87eaceb3 2747 struct deferred_split *ds_queue = get_deferred_split_queue(page);
9a982250
KS
2748 unsigned long flags;
2749
364c1eeb 2750 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2751 if (!list_empty(page_deferred_list(page))) {
364c1eeb 2752 ds_queue->split_queue_len--;
9a982250
KS
2753 list_del(page_deferred_list(page));
2754 }
364c1eeb 2755 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2756 free_compound_page(page);
2757}
2758
2759void deferred_split_huge_page(struct page *page)
2760{
87eaceb3
YS
2761 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2762#ifdef CONFIG_MEMCG
bcfe06bf 2763 struct mem_cgroup *memcg = page_memcg(compound_head(page));
87eaceb3 2764#endif
9a982250
KS
2765 unsigned long flags;
2766
2767 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
2768
87eaceb3
YS
2769 /*
2770 * The try_to_unmap() in page reclaim path might reach here too,
2771 * this may cause a race condition to corrupt deferred split queue.
2772 * And, if page reclaim is already handling the same page, it is
2773 * unnecessary to handle it again in shrinker.
2774 *
2775 * Check PageSwapCache to determine if the page is being
2776 * handled by page reclaim since THP swap would add the page into
2777 * swap cache before calling try_to_unmap().
2778 */
2779 if (PageSwapCache(page))
2780 return;
2781
364c1eeb 2782 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2783 if (list_empty(page_deferred_list(page))) {
f9719a03 2784 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
364c1eeb
YS
2785 list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2786 ds_queue->split_queue_len++;
87eaceb3
YS
2787#ifdef CONFIG_MEMCG
2788 if (memcg)
2bfd3637
YS
2789 set_shrinker_bit(memcg, page_to_nid(page),
2790 deferred_split_shrinker.id);
87eaceb3 2791#endif
9a982250 2792 }
364c1eeb 2793 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2794}
2795
2796static unsigned long deferred_split_count(struct shrinker *shrink,
2797 struct shrink_control *sc)
2798{
a3d0a918 2799 struct pglist_data *pgdata = NODE_DATA(sc->nid);
364c1eeb 2800 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
87eaceb3
YS
2801
2802#ifdef CONFIG_MEMCG
2803 if (sc->memcg)
2804 ds_queue = &sc->memcg->deferred_split_queue;
2805#endif
364c1eeb 2806 return READ_ONCE(ds_queue->split_queue_len);
9a982250
KS
2807}
2808
2809static unsigned long deferred_split_scan(struct shrinker *shrink,
2810 struct shrink_control *sc)
2811{
a3d0a918 2812 struct pglist_data *pgdata = NODE_DATA(sc->nid);
364c1eeb 2813 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
9a982250
KS
2814 unsigned long flags;
2815 LIST_HEAD(list), *pos, *next;
2816 struct page *page;
2817 int split = 0;
2818
87eaceb3
YS
2819#ifdef CONFIG_MEMCG
2820 if (sc->memcg)
2821 ds_queue = &sc->memcg->deferred_split_queue;
2822#endif
2823
364c1eeb 2824 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2825 /* Take pin on all head pages to avoid freeing them under us */
364c1eeb 2826 list_for_each_safe(pos, next, &ds_queue->split_queue) {
dfe5c51c 2827 page = list_entry((void *)pos, struct page, deferred_list);
9a982250 2828 page = compound_head(page);
e3ae1953
KS
2829 if (get_page_unless_zero(page)) {
2830 list_move(page_deferred_list(page), &list);
2831 } else {
2832 /* We lost race with put_compound_page() */
9a982250 2833 list_del_init(page_deferred_list(page));
364c1eeb 2834 ds_queue->split_queue_len--;
9a982250 2835 }
e3ae1953
KS
2836 if (!--sc->nr_to_scan)
2837 break;
9a982250 2838 }
364c1eeb 2839 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2840
2841 list_for_each_safe(pos, next, &list) {
dfe5c51c 2842 page = list_entry((void *)pos, struct page, deferred_list);
fa41b900
KS
2843 if (!trylock_page(page))
2844 goto next;
9a982250
KS
2845 /* split_huge_page() removes page from list on success */
2846 if (!split_huge_page(page))
2847 split++;
2848 unlock_page(page);
fa41b900 2849next:
9a982250
KS
2850 put_page(page);
2851 }
2852
364c1eeb
YS
2853 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2854 list_splice_tail(&list, &ds_queue->split_queue);
2855 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250 2856
cb8d68ec
KS
2857 /*
2858 * Stop shrinker if we didn't split any page, but the queue is empty.
2859 * This can happen if pages were freed under us.
2860 */
364c1eeb 2861 if (!split && list_empty(&ds_queue->split_queue))
cb8d68ec
KS
2862 return SHRINK_STOP;
2863 return split;
9a982250
KS
2864}
2865
2866static struct shrinker deferred_split_shrinker = {
2867 .count_objects = deferred_split_count,
2868 .scan_objects = deferred_split_scan,
2869 .seeks = DEFAULT_SEEKS,
87eaceb3
YS
2870 .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
2871 SHRINKER_NONSLAB,
9a982250 2872};
49071d43
KS
2873
2874#ifdef CONFIG_DEBUG_FS
fa6c0231 2875static void split_huge_pages_all(void)
49071d43
KS
2876{
2877 struct zone *zone;
2878 struct page *page;
2879 unsigned long pfn, max_zone_pfn;
2880 unsigned long total = 0, split = 0;
2881
fa6c0231 2882 pr_debug("Split all THPs\n");
49071d43
KS
2883 for_each_populated_zone(zone) {
2884 max_zone_pfn = zone_end_pfn(zone);
2885 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
2886 if (!pfn_valid(pfn))
2887 continue;
2888
2889 page = pfn_to_page(pfn);
2890 if (!get_page_unless_zero(page))
2891 continue;
2892
2893 if (zone != page_zone(page))
2894 goto next;
2895
baa355fd 2896 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
49071d43
KS
2897 goto next;
2898
2899 total++;
2900 lock_page(page);
2901 if (!split_huge_page(page))
2902 split++;
2903 unlock_page(page);
2904next:
2905 put_page(page);
fa6c0231 2906 cond_resched();
49071d43
KS
2907 }
2908 }
2909
fa6c0231
ZY
2910 pr_debug("%lu of %lu THP split\n", split, total);
2911}
49071d43 2912
fa6c0231
ZY
2913static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
2914{
2915 return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) ||
2916 is_vm_hugetlb_page(vma);
2917}
2918
2919static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
2920 unsigned long vaddr_end)
2921{
2922 int ret = 0;
2923 struct task_struct *task;
2924 struct mm_struct *mm;
2925 unsigned long total = 0, split = 0;
2926 unsigned long addr;
2927
2928 vaddr_start &= PAGE_MASK;
2929 vaddr_end &= PAGE_MASK;
2930
2931 /* Find the task_struct from pid */
2932 rcu_read_lock();
2933 task = find_task_by_vpid(pid);
2934 if (!task) {
2935 rcu_read_unlock();
2936 ret = -ESRCH;
2937 goto out;
2938 }
2939 get_task_struct(task);
2940 rcu_read_unlock();
2941
2942 /* Find the mm_struct */
2943 mm = get_task_mm(task);
2944 put_task_struct(task);
2945
2946 if (!mm) {
2947 ret = -EINVAL;
2948 goto out;
2949 }
2950
2951 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n",
2952 pid, vaddr_start, vaddr_end);
2953
2954 mmap_read_lock(mm);
2955 /*
2956 * always increase addr by PAGE_SIZE, since we could have a PTE page
2957 * table filled with PTE-mapped THPs, each of which is distinct.
2958 */
2959 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
2960 struct vm_area_struct *vma = find_vma(mm, addr);
fa6c0231
ZY
2961 struct page *page;
2962
2963 if (!vma || addr < vma->vm_start)
2964 break;
2965
2966 /* skip special VMA and hugetlb VMA */
2967 if (vma_not_suitable_for_thp_split(vma)) {
2968 addr = vma->vm_end;
2969 continue;
2970 }
2971
2972 /* FOLL_DUMP to ignore special (like zero) pages */
87d2762e 2973 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
fa6c0231
ZY
2974
2975 if (IS_ERR(page))
2976 continue;
3218f871 2977 if (!page || is_zone_device_page(page))
fa6c0231
ZY
2978 continue;
2979
2980 if (!is_transparent_hugepage(page))
2981 goto next;
2982
2983 total++;
d4b4084a 2984 if (!can_split_folio(page_folio(page), NULL))
fa6c0231
ZY
2985 goto next;
2986
2987 if (!trylock_page(page))
2988 goto next;
2989
2990 if (!split_huge_page(page))
2991 split++;
2992
2993 unlock_page(page);
2994next:
2995 put_page(page);
2996 cond_resched();
2997 }
2998 mmap_read_unlock(mm);
2999 mmput(mm);
3000
3001 pr_debug("%lu of %lu THP split\n", split, total);
3002
3003out:
3004 return ret;
49071d43 3005}
fa6c0231 3006
fbe37501
ZY
3007static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
3008 pgoff_t off_end)
3009{
3010 struct filename *file;
3011 struct file *candidate;
3012 struct address_space *mapping;
3013 int ret = -EINVAL;
3014 pgoff_t index;
3015 int nr_pages = 1;
3016 unsigned long total = 0, split = 0;
3017
3018 file = getname_kernel(file_path);
3019 if (IS_ERR(file))
3020 return ret;
3021
3022 candidate = file_open_name(file, O_RDONLY, 0);
3023 if (IS_ERR(candidate))
3024 goto out;
3025
3026 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n",
3027 file_path, off_start, off_end);
3028
3029 mapping = candidate->f_mapping;
3030
3031 for (index = off_start; index < off_end; index += nr_pages) {
3032 struct page *fpage = pagecache_get_page(mapping, index,
3033 FGP_ENTRY | FGP_HEAD, 0);
3034
3035 nr_pages = 1;
3036 if (xa_is_value(fpage) || !fpage)
3037 continue;
3038
3039 if (!is_transparent_hugepage(fpage))
3040 goto next;
3041
3042 total++;
3043 nr_pages = thp_nr_pages(fpage);
3044
3045 if (!trylock_page(fpage))
3046 goto next;
3047
3048 if (!split_huge_page(fpage))
3049 split++;
3050
3051 unlock_page(fpage);
3052next:
3053 put_page(fpage);
3054 cond_resched();
3055 }
3056
3057 filp_close(candidate, NULL);
3058 ret = 0;
3059
3060 pr_debug("%lu of %lu file-backed THP split\n", split, total);
3061out:
3062 putname(file);
3063 return ret;
3064}
3065
fa6c0231
ZY
3066#define MAX_INPUT_BUF_SZ 255
3067
3068static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
3069 size_t count, loff_t *ppops)
3070{
3071 static DEFINE_MUTEX(split_debug_mutex);
3072 ssize_t ret;
fbe37501
ZY
3073 /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3074 char input_buf[MAX_INPUT_BUF_SZ];
fa6c0231
ZY
3075 int pid;
3076 unsigned long vaddr_start, vaddr_end;
3077
3078 ret = mutex_lock_interruptible(&split_debug_mutex);
3079 if (ret)
3080 return ret;
3081
3082 ret = -EFAULT;
3083
3084 memset(input_buf, 0, MAX_INPUT_BUF_SZ);
3085 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
3086 goto out;
3087
3088 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
fbe37501
ZY
3089
3090 if (input_buf[0] == '/') {
3091 char *tok;
3092 char *buf = input_buf;
3093 char file_path[MAX_INPUT_BUF_SZ];
3094 pgoff_t off_start = 0, off_end = 0;
3095 size_t input_len = strlen(input_buf);
3096
3097 tok = strsep(&buf, ",");
3098 if (tok) {
1212e00c 3099 strcpy(file_path, tok);
fbe37501
ZY
3100 } else {
3101 ret = -EINVAL;
3102 goto out;
3103 }
3104
3105 ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3106 if (ret != 2) {
3107 ret = -EINVAL;
3108 goto out;
3109 }
3110 ret = split_huge_pages_in_file(file_path, off_start, off_end);
3111 if (!ret)
3112 ret = input_len;
3113
3114 goto out;
3115 }
3116
fa6c0231
ZY
3117 ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3118 if (ret == 1 && pid == 1) {
3119 split_huge_pages_all();
3120 ret = strlen(input_buf);
3121 goto out;
3122 } else if (ret != 3) {
3123 ret = -EINVAL;
3124 goto out;
3125 }
3126
3127 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3128 if (!ret)
3129 ret = strlen(input_buf);
3130out:
3131 mutex_unlock(&split_debug_mutex);
3132 return ret;
3133
3134}
3135
3136static const struct file_operations split_huge_pages_fops = {
3137 .owner = THIS_MODULE,
3138 .write = split_huge_pages_write,
3139 .llseek = no_llseek,
3140};
49071d43
KS
3141
3142static int __init split_huge_pages_debugfs(void)
3143{
d9f7979c
GKH
3144 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3145 &split_huge_pages_fops);
49071d43
KS
3146 return 0;
3147}
3148late_initcall(split_huge_pages_debugfs);
3149#endif
616b8371
ZY
3150
3151#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
7f5abe60 3152int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
616b8371
ZY
3153 struct page *page)
3154{
3155 struct vm_area_struct *vma = pvmw->vma;
3156 struct mm_struct *mm = vma->vm_mm;
3157 unsigned long address = pvmw->address;
6c287605 3158 bool anon_exclusive;
616b8371
ZY
3159 pmd_t pmdval;
3160 swp_entry_t entry;
ab6e3d09 3161 pmd_t pmdswp;
616b8371
ZY
3162
3163 if (!(pvmw->pmd && !pvmw->pte))
7f5abe60 3164 return 0;
616b8371 3165
616b8371 3166 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
8a8683ad 3167 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
6c287605
DH
3168
3169 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
3170 if (anon_exclusive && page_try_share_anon_rmap(page)) {
3171 set_pmd_at(mm, address, pvmw->pmd, pmdval);
7f5abe60 3172 return -EBUSY;
6c287605
DH
3173 }
3174
616b8371
ZY
3175 if (pmd_dirty(pmdval))
3176 set_page_dirty(page);
4dd845b5
AP
3177 if (pmd_write(pmdval))
3178 entry = make_writable_migration_entry(page_to_pfn(page));
6c287605
DH
3179 else if (anon_exclusive)
3180 entry = make_readable_exclusive_migration_entry(page_to_pfn(page));
4dd845b5
AP
3181 else
3182 entry = make_readable_migration_entry(page_to_pfn(page));
ab6e3d09
NH
3183 pmdswp = swp_entry_to_pmd(entry);
3184 if (pmd_soft_dirty(pmdval))
3185 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3186 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
cea86fe2 3187 page_remove_rmap(page, vma, true);
616b8371 3188 put_page(page);
283fd6fe 3189 trace_set_migration_pmd(address, pmd_val(pmdswp));
7f5abe60
DH
3190
3191 return 0;
616b8371
ZY
3192}
3193
3194void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3195{
3196 struct vm_area_struct *vma = pvmw->vma;
3197 struct mm_struct *mm = vma->vm_mm;
3198 unsigned long address = pvmw->address;
3199 unsigned long mmun_start = address & HPAGE_PMD_MASK;
3200 pmd_t pmde;
3201 swp_entry_t entry;
3202
3203 if (!(pvmw->pmd && !pvmw->pte))
3204 return;
3205
3206 entry = pmd_to_swp_entry(*pvmw->pmd);
3207 get_page(new);
3208 pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot));
ab6e3d09
NH
3209 if (pmd_swp_soft_dirty(*pvmw->pmd))
3210 pmde = pmd_mksoft_dirty(pmde);
4dd845b5 3211 if (is_writable_migration_entry(entry))
f55e1014 3212 pmde = maybe_pmd_mkwrite(pmde, vma);
8f34f1ea
PX
3213 if (pmd_swp_uffd_wp(*pvmw->pmd))
3214 pmde = pmd_wrprotect(pmd_mkuffd_wp(pmde));
616b8371 3215
6c287605
DH
3216 if (PageAnon(new)) {
3217 rmap_t rmap_flags = RMAP_COMPOUND;
3218
3219 if (!is_readable_migration_entry(entry))
3220 rmap_flags |= RMAP_EXCLUSIVE;
3221
3222 page_add_anon_rmap(new, vma, mmun_start, rmap_flags);
3223 } else {
cea86fe2 3224 page_add_file_rmap(new, vma, true);
6c287605
DH
3225 }
3226 VM_BUG_ON(pmd_write(pmde) && PageAnon(new) && !PageAnonExclusive(new));
616b8371 3227 set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
5cbcf225
MS
3228
3229 /* No need to invalidate - it was non-present before */
616b8371 3230 update_mmu_cache_pmd(vma, address, pvmw->pmd);
283fd6fe 3231 trace_remove_migration_pmd(address, pmd_val(pmde));
616b8371
ZY
3232}
3233#endif