mm/huge_memory: use helper touch_pmd in huge_pmd_set_accessed
[linux-block.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,
a69e4717 1023 pmd_t *pmd, bool write)
3565fce3
DW
1024{
1025 pmd_t _pmd;
1026
a8f97366 1027 _pmd = pmd_mkyoung(*pmd);
a69e4717 1028 if (write)
a8f97366 1029 _pmd = pmd_mkdirty(_pmd);
3565fce3 1030 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
a69e4717 1031 pmd, _pmd, 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)
a69e4717 1064 touch_pmd(vma, addr, pmd, flags & FOLL_WRITE);
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,
5fe653e9 1187 pud_t *pud, bool write)
a00cc7d9
MW
1188{
1189 pud_t _pud;
1190
a8f97366 1191 _pud = pud_mkyoung(*pud);
5fe653e9 1192 if (write)
a8f97366 1193 _pud = pud_mkdirty(_pud);
a00cc7d9 1194 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
5fe653e9 1195 pud, _pud, 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)
5fe653e9 1222 touch_pud(vma, addr, pud, flags & FOLL_WRITE);
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{
a00cc7d9
MW
1287 bool write = vmf->flags & FAULT_FLAG_WRITE;
1288
1289 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1290 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1291 goto unlock;
1292
5fe653e9 1293 touch_pud(vmf->vma, vmf->address, vmf->pud, write);
a00cc7d9
MW
1294unlock:
1295 spin_unlock(vmf->ptl);
1296}
1297#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1298
5db4f15c 1299void huge_pmd_set_accessed(struct vm_fault *vmf)
a1dd450b 1300{
20f664aa 1301 bool write = vmf->flags & FAULT_FLAG_WRITE;
a1dd450b 1302
82b0f8c3 1303 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
a69e4717 1304 if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd)))
a1dd450b
WD
1305 goto unlock;
1306
a69e4717 1307 touch_pmd(vmf->vma, vmf->address, vmf->pmd, write);
a1dd450b
WD
1308
1309unlock:
82b0f8c3 1310 spin_unlock(vmf->ptl);
a1dd450b
WD
1311}
1312
5db4f15c 1313vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
71e3aac0 1314{
c89357e2 1315 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
82b0f8c3 1316 struct vm_area_struct *vma = vmf->vma;
3917c802 1317 struct page *page;
82b0f8c3 1318 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
5db4f15c 1319 pmd_t orig_pmd = vmf->orig_pmd;
71e3aac0 1320
82b0f8c3 1321 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
81d1b09c 1322 VM_BUG_ON_VMA(!vma->anon_vma, vma);
3917c802 1323
c89357e2
DH
1324 VM_BUG_ON(unshare && (vmf->flags & FAULT_FLAG_WRITE));
1325 VM_BUG_ON(!unshare && !(vmf->flags & FAULT_FLAG_WRITE));
1326
93b4796d 1327 if (is_huge_zero_pmd(orig_pmd))
3917c802
KS
1328 goto fallback;
1329
82b0f8c3 1330 spin_lock(vmf->ptl);
3917c802
KS
1331
1332 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1333 spin_unlock(vmf->ptl);
1334 return 0;
1335 }
71e3aac0
AA
1336
1337 page = pmd_page(orig_pmd);
f6004e73 1338 VM_BUG_ON_PAGE(!PageHead(page), page);
3917c802 1339
6c287605
DH
1340 /* Early check when only holding the PT lock. */
1341 if (PageAnonExclusive(page))
1342 goto reuse;
1343
ba3c4ce6
HY
1344 if (!trylock_page(page)) {
1345 get_page(page);
1346 spin_unlock(vmf->ptl);
1347 lock_page(page);
1348 spin_lock(vmf->ptl);
1349 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
3917c802 1350 spin_unlock(vmf->ptl);
ba3c4ce6
HY
1351 unlock_page(page);
1352 put_page(page);
3917c802 1353 return 0;
ba3c4ce6
HY
1354 }
1355 put_page(page);
1356 }
3917c802 1357
6c287605
DH
1358 /* Recheck after temporarily dropping the PT lock. */
1359 if (PageAnonExclusive(page)) {
1360 unlock_page(page);
1361 goto reuse;
1362 }
1363
3917c802 1364 /*
c89357e2 1365 * See do_wp_page(): we can only reuse the page exclusively if there are
3bff7e3f
DH
1366 * no additional references. Note that we always drain the LRU
1367 * pagevecs immediately after adding a THP.
3917c802 1368 */
3bff7e3f
DH
1369 if (page_count(page) > 1 + PageSwapCache(page) * thp_nr_pages(page))
1370 goto unlock_fallback;
1371 if (PageSwapCache(page))
1372 try_to_free_swap(page);
1373 if (page_count(page) == 1) {
71e3aac0 1374 pmd_t entry;
6c54dc6c
DH
1375
1376 page_move_anon_rmap(page, vma);
6c287605
DH
1377 unlock_page(page);
1378reuse:
c89357e2
DH
1379 if (unlikely(unshare)) {
1380 spin_unlock(vmf->ptl);
1381 return 0;
1382 }
71e3aac0 1383 entry = pmd_mkyoung(orig_pmd);
f55e1014 1384 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
3917c802 1385 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
82b0f8c3 1386 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
82b0f8c3 1387 spin_unlock(vmf->ptl);
3917c802 1388 return VM_FAULT_WRITE;
71e3aac0 1389 }
3917c802 1390
3bff7e3f 1391unlock_fallback:
3917c802 1392 unlock_page(page);
82b0f8c3 1393 spin_unlock(vmf->ptl);
3917c802
KS
1394fallback:
1395 __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
1396 return VM_FAULT_FALLBACK;
71e3aac0
AA
1397}
1398
8310d48b 1399/*
a308c71b
PX
1400 * FOLL_FORCE can write to even unwritable pmd's, but only
1401 * after we've gone through a COW cycle and they are dirty.
8310d48b
KF
1402 */
1403static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
1404{
a308c71b
PX
1405 return pmd_write(pmd) ||
1406 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
8310d48b
KF
1407}
1408
b676b293 1409struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
71e3aac0
AA
1410 unsigned long addr,
1411 pmd_t *pmd,
1412 unsigned int flags)
1413{
b676b293 1414 struct mm_struct *mm = vma->vm_mm;
71e3aac0
AA
1415 struct page *page = NULL;
1416
c4088ebd 1417 assert_spin_locked(pmd_lockptr(mm, pmd));
71e3aac0 1418
8310d48b 1419 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
71e3aac0
AA
1420 goto out;
1421
85facf25
KS
1422 /* Avoid dumping huge zero page */
1423 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1424 return ERR_PTR(-EFAULT);
1425
2b4847e7 1426 /* Full NUMA hinting faults to serialise migration in fault paths */
8a0516ed 1427 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
2b4847e7
MG
1428 goto out;
1429
71e3aac0 1430 page = pmd_page(*pmd);
ca120cf6 1431 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
3faa52c0 1432
a7f22660
DH
1433 if (!pmd_write(*pmd) && gup_must_unshare(flags, page))
1434 return ERR_PTR(-EMLINK);
1435
b6a2619c
DH
1436 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
1437 !PageAnonExclusive(page), page);
1438
3faa52c0
JH
1439 if (!try_grab_page(page, flags))
1440 return ERR_PTR(-ENOMEM);
1441
3565fce3 1442 if (flags & FOLL_TOUCH)
a69e4717 1443 touch_pmd(vma, addr, pmd, flags & FOLL_WRITE);
3faa52c0 1444
71e3aac0 1445 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
ca120cf6 1446 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
71e3aac0
AA
1447
1448out:
1449 return page;
1450}
1451
d10e63f2 1452/* NUMA hinting page fault entry point for trans huge pmds */
5db4f15c 1453vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
d10e63f2 1454{
82b0f8c3 1455 struct vm_area_struct *vma = vmf->vma;
c5b5a3dd
YS
1456 pmd_t oldpmd = vmf->orig_pmd;
1457 pmd_t pmd;
b32967ff 1458 struct page *page;
82b0f8c3 1459 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
c5b5a3dd 1460 int page_nid = NUMA_NO_NODE;
90572890 1461 int target_nid, last_cpupid = -1;
8191acbd 1462 bool migrated = false;
c5b5a3dd 1463 bool was_writable = pmd_savedwrite(oldpmd);
6688cc05 1464 int flags = 0;
d10e63f2 1465
82b0f8c3 1466 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
c5b5a3dd 1467 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
82b0f8c3 1468 spin_unlock(vmf->ptl);
de466bd6
MG
1469 goto out;
1470 }
1471
c5b5a3dd
YS
1472 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1473 page = vm_normal_page_pmd(vma, haddr, pmd);
1474 if (!page)
1475 goto out_map;
1476
1477 /* See similar comment in do_numa_page for explanation */
1478 if (!was_writable)
1479 flags |= TNF_NO_GROUP;
1480
1481 page_nid = page_to_nid(page);
1482 last_cpupid = page_cpupid_last(page);
1483 target_nid = numa_migrate_prep(page, vma, haddr, page_nid,
1484 &flags);
1485
1486 if (target_nid == NUMA_NO_NODE) {
1487 put_page(page);
1488 goto out_map;
1489 }
1490
82b0f8c3 1491 spin_unlock(vmf->ptl);
8b1b436d 1492
c5b5a3dd 1493 migrated = migrate_misplaced_page(page, vma, target_nid);
6688cc05
PZ
1494 if (migrated) {
1495 flags |= TNF_MIGRATED;
8191acbd 1496 page_nid = target_nid;
c5b5a3dd 1497 } else {
074c2381 1498 flags |= TNF_MIGRATE_FAIL;
c5b5a3dd
YS
1499 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1500 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
1501 spin_unlock(vmf->ptl);
1502 goto out;
1503 }
1504 goto out_map;
1505 }
b8916634
MG
1506
1507out:
98fa15f3 1508 if (page_nid != NUMA_NO_NODE)
82b0f8c3 1509 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
9a8b300f 1510 flags);
8191acbd 1511
d10e63f2 1512 return 0;
c5b5a3dd
YS
1513
1514out_map:
1515 /* Restore the PMD */
1516 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1517 pmd = pmd_mkyoung(pmd);
1518 if (was_writable)
1519 pmd = pmd_mkwrite(pmd);
1520 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1521 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1522 spin_unlock(vmf->ptl);
1523 goto out;
d10e63f2
MG
1524}
1525
319904ad
HY
1526/*
1527 * Return true if we do MADV_FREE successfully on entire pmd page.
1528 * Otherwise, return false.
1529 */
1530bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
b8d3c4c3 1531 pmd_t *pmd, unsigned long addr, unsigned long next)
b8d3c4c3
MK
1532{
1533 spinlock_t *ptl;
1534 pmd_t orig_pmd;
1535 struct page *page;
1536 struct mm_struct *mm = tlb->mm;
319904ad 1537 bool ret = false;
b8d3c4c3 1538
ed6a7935 1539 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
07e32661 1540
b6ec57f4
KS
1541 ptl = pmd_trans_huge_lock(pmd, vma);
1542 if (!ptl)
25eedabe 1543 goto out_unlocked;
b8d3c4c3
MK
1544
1545 orig_pmd = *pmd;
319904ad 1546 if (is_huge_zero_pmd(orig_pmd))
b8d3c4c3 1547 goto out;
b8d3c4c3 1548
84c3fc4e
ZY
1549 if (unlikely(!pmd_present(orig_pmd))) {
1550 VM_BUG_ON(thp_migration_supported() &&
1551 !is_pmd_migration_entry(orig_pmd));
1552 goto out;
1553 }
1554
b8d3c4c3
MK
1555 page = pmd_page(orig_pmd);
1556 /*
1557 * If other processes are mapping this page, we couldn't discard
1558 * the page unless they all do MADV_FREE so let's skip the page.
1559 */
babbbdd0 1560 if (total_mapcount(page) != 1)
b8d3c4c3
MK
1561 goto out;
1562
1563 if (!trylock_page(page))
1564 goto out;
1565
1566 /*
1567 * If user want to discard part-pages of THP, split it so MADV_FREE
1568 * will deactivate only them.
1569 */
1570 if (next - addr != HPAGE_PMD_SIZE) {
1571 get_page(page);
1572 spin_unlock(ptl);
9818b8cd 1573 split_huge_page(page);
b8d3c4c3 1574 unlock_page(page);
bbf29ffc 1575 put_page(page);
b8d3c4c3
MK
1576 goto out_unlocked;
1577 }
1578
1579 if (PageDirty(page))
1580 ClearPageDirty(page);
1581 unlock_page(page);
1582
b8d3c4c3 1583 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
58ceeb6b 1584 pmdp_invalidate(vma, addr, pmd);
b8d3c4c3
MK
1585 orig_pmd = pmd_mkold(orig_pmd);
1586 orig_pmd = pmd_mkclean(orig_pmd);
1587
1588 set_pmd_at(mm, addr, pmd, orig_pmd);
1589 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1590 }
802a3a92
SL
1591
1592 mark_page_lazyfree(page);
319904ad 1593 ret = true;
b8d3c4c3
MK
1594out:
1595 spin_unlock(ptl);
1596out_unlocked:
1597 return ret;
1598}
1599
953c66c2
AK
1600static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1601{
1602 pgtable_t pgtable;
1603
1604 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1605 pte_free(mm, pgtable);
c4812909 1606 mm_dec_nr_ptes(mm);
953c66c2
AK
1607}
1608
71e3aac0 1609int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
f21760b1 1610 pmd_t *pmd, unsigned long addr)
71e3aac0 1611{
da146769 1612 pmd_t orig_pmd;
bf929152 1613 spinlock_t *ptl;
71e3aac0 1614
ed6a7935 1615 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
07e32661 1616
b6ec57f4
KS
1617 ptl = __pmd_trans_huge_lock(pmd, vma);
1618 if (!ptl)
da146769
KS
1619 return 0;
1620 /*
1621 * For architectures like ppc64 we look at deposited pgtable
1622 * when calling pmdp_huge_get_and_clear. So do the
1623 * pgtable_trans_huge_withdraw after finishing pmdp related
1624 * operations.
1625 */
93a98695
AK
1626 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
1627 tlb->fullmm);
da146769 1628 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
2484ca9b 1629 if (vma_is_special_huge(vma)) {
3b6521f5
OH
1630 if (arch_needs_pgtable_deposit())
1631 zap_deposited_table(tlb->mm, pmd);
da146769 1632 spin_unlock(ptl);
da146769 1633 } else if (is_huge_zero_pmd(orig_pmd)) {
c14a6eb4 1634 zap_deposited_table(tlb->mm, pmd);
da146769 1635 spin_unlock(ptl);
da146769 1636 } else {
616b8371
ZY
1637 struct page *page = NULL;
1638 int flush_needed = 1;
1639
1640 if (pmd_present(orig_pmd)) {
1641 page = pmd_page(orig_pmd);
cea86fe2 1642 page_remove_rmap(page, vma, true);
616b8371
ZY
1643 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1644 VM_BUG_ON_PAGE(!PageHead(page), page);
1645 } else if (thp_migration_supported()) {
1646 swp_entry_t entry;
1647
1648 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1649 entry = pmd_to_swp_entry(orig_pmd);
af5cdaf8 1650 page = pfn_swap_entry_to_page(entry);
616b8371
ZY
1651 flush_needed = 0;
1652 } else
1653 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1654
b5072380 1655 if (PageAnon(page)) {
c14a6eb4 1656 zap_deposited_table(tlb->mm, pmd);
b5072380
KS
1657 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1658 } else {
953c66c2
AK
1659 if (arch_needs_pgtable_deposit())
1660 zap_deposited_table(tlb->mm, pmd);
fadae295 1661 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
b5072380 1662 }
616b8371 1663
da146769 1664 spin_unlock(ptl);
616b8371
ZY
1665 if (flush_needed)
1666 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
025c5b24 1667 }
da146769 1668 return 1;
71e3aac0
AA
1669}
1670
1dd38b6c
AK
1671#ifndef pmd_move_must_withdraw
1672static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1673 spinlock_t *old_pmd_ptl,
1674 struct vm_area_struct *vma)
1675{
1676 /*
1677 * With split pmd lock we also need to move preallocated
1678 * PTE page table if new_pmd is on different PMD page table.
1679 *
1680 * We also don't deposit and withdraw tables for file pages.
1681 */
1682 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1683}
1684#endif
1685
ab6e3d09
NH
1686static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1687{
1688#ifdef CONFIG_MEM_SOFT_DIRTY
1689 if (unlikely(is_pmd_migration_entry(pmd)))
1690 pmd = pmd_swp_mksoft_dirty(pmd);
1691 else if (pmd_present(pmd))
1692 pmd = pmd_mksoft_dirty(pmd);
1693#endif
1694 return pmd;
1695}
1696
bf8616d5 1697bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
b8aa9d9d 1698 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
37a1c49a 1699{
bf929152 1700 spinlock_t *old_ptl, *new_ptl;
37a1c49a 1701 pmd_t pmd;
37a1c49a 1702 struct mm_struct *mm = vma->vm_mm;
5d190420 1703 bool force_flush = false;
37a1c49a 1704
37a1c49a
AA
1705 /*
1706 * The destination pmd shouldn't be established, free_pgtables()
1707 * should have release it.
1708 */
1709 if (WARN_ON(!pmd_none(*new_pmd))) {
1710 VM_BUG_ON(pmd_trans_huge(*new_pmd));
4b471e88 1711 return false;
37a1c49a
AA
1712 }
1713
bf929152
KS
1714 /*
1715 * We don't have to worry about the ordering of src and dst
c1e8d7c6 1716 * ptlocks because exclusive mmap_lock prevents deadlock.
bf929152 1717 */
b6ec57f4
KS
1718 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1719 if (old_ptl) {
bf929152
KS
1720 new_ptl = pmd_lockptr(mm, new_pmd);
1721 if (new_ptl != old_ptl)
1722 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
8809aa2d 1723 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
eb66ae03 1724 if (pmd_present(pmd))
a2ce2666 1725 force_flush = true;
025c5b24 1726 VM_BUG_ON(!pmd_none(*new_pmd));
3592806c 1727
1dd38b6c 1728 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
b3084f4d 1729 pgtable_t pgtable;
3592806c
KS
1730 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1731 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
3592806c 1732 }
ab6e3d09
NH
1733 pmd = move_soft_dirty_pmd(pmd);
1734 set_pmd_at(mm, new_addr, new_pmd, pmd);
5d190420 1735 if (force_flush)
7c38f181 1736 flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
eb66ae03
LT
1737 if (new_ptl != old_ptl)
1738 spin_unlock(new_ptl);
bf929152 1739 spin_unlock(old_ptl);
4b471e88 1740 return true;
37a1c49a 1741 }
4b471e88 1742 return false;
37a1c49a
AA
1743}
1744
f123d74a
MG
1745/*
1746 * Returns
1747 * - 0 if PMD could not be locked
f0953a1b 1748 * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary
e346e668 1749 * or if prot_numa but THP migration is not supported
f0953a1b 1750 * - HPAGE_PMD_NR if protections changed and TLB flush necessary
f123d74a 1751 */
4a18419f
NA
1752int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1753 pmd_t *pmd, unsigned long addr, pgprot_t newprot,
1754 unsigned long cp_flags)
cd7548ab
JW
1755{
1756 struct mm_struct *mm = vma->vm_mm;
bf929152 1757 spinlock_t *ptl;
c9fe6656 1758 pmd_t oldpmd, entry;
0a85e51d
KS
1759 bool preserve_write;
1760 int ret;
58705444 1761 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
292924b2
PX
1762 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
1763 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
cd7548ab 1764
4a18419f
NA
1765 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1766
e346e668
YS
1767 if (prot_numa && !thp_migration_supported())
1768 return 1;
1769
b6ec57f4 1770 ptl = __pmd_trans_huge_lock(pmd, vma);
0a85e51d
KS
1771 if (!ptl)
1772 return 0;
e944fd67 1773
0a85e51d
KS
1774 preserve_write = prot_numa && pmd_write(*pmd);
1775 ret = 1;
e944fd67 1776
84c3fc4e
ZY
1777#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1778 if (is_swap_pmd(*pmd)) {
1779 swp_entry_t entry = pmd_to_swp_entry(*pmd);
6c287605 1780 struct page *page = pfn_swap_entry_to_page(entry);
84c3fc4e
ZY
1781
1782 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
4dd845b5 1783 if (is_writable_migration_entry(entry)) {
84c3fc4e
ZY
1784 pmd_t newpmd;
1785 /*
1786 * A protection check is difficult so
1787 * just be safe and disable write
1788 */
6c287605
DH
1789 if (PageAnon(page))
1790 entry = make_readable_exclusive_migration_entry(swp_offset(entry));
1791 else
1792 entry = make_readable_migration_entry(swp_offset(entry));
84c3fc4e 1793 newpmd = swp_entry_to_pmd(entry);
ab6e3d09
NH
1794 if (pmd_swp_soft_dirty(*pmd))
1795 newpmd = pmd_swp_mksoft_dirty(newpmd);
8f34f1ea
PX
1796 if (pmd_swp_uffd_wp(*pmd))
1797 newpmd = pmd_swp_mkuffd_wp(newpmd);
84c3fc4e
ZY
1798 set_pmd_at(mm, addr, pmd, newpmd);
1799 }
1800 goto unlock;
1801 }
1802#endif
1803
a1a3a2fc
HY
1804 if (prot_numa) {
1805 struct page *page;
1806 /*
1807 * Avoid trapping faults against the zero page. The read-only
1808 * data is likely to be read-cached on the local CPU and
1809 * local/remote hits to the zero page are not interesting.
1810 */
1811 if (is_huge_zero_pmd(*pmd))
1812 goto unlock;
025c5b24 1813
a1a3a2fc
HY
1814 if (pmd_protnone(*pmd))
1815 goto unlock;
0a85e51d 1816
a1a3a2fc
HY
1817 page = pmd_page(*pmd);
1818 /*
1819 * Skip scanning top tier node if normal numa
1820 * balancing is disabled
1821 */
1822 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
1823 node_is_toptier(page_to_nid(page)))
1824 goto unlock;
1825 }
ced10803 1826 /*
3e4e28c5 1827 * In case prot_numa, we are under mmap_read_lock(mm). It's critical
ced10803 1828 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
3e4e28c5 1829 * which is also under mmap_read_lock(mm):
ced10803
KS
1830 *
1831 * CPU0: CPU1:
1832 * change_huge_pmd(prot_numa=1)
1833 * pmdp_huge_get_and_clear_notify()
1834 * madvise_dontneed()
1835 * zap_pmd_range()
1836 * pmd_trans_huge(*pmd) == 0 (without ptl)
1837 * // skip the pmd
1838 * set_pmd_at();
1839 * // pmd is re-established
1840 *
1841 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1842 * which may break userspace.
1843 *
4f831457 1844 * pmdp_invalidate_ad() is required to make sure we don't miss
ced10803
KS
1845 * dirty/young flags set by hardware.
1846 */
4f831457 1847 oldpmd = pmdp_invalidate_ad(vma, addr, pmd);
ced10803 1848
c9fe6656 1849 entry = pmd_modify(oldpmd, newprot);
0a85e51d
KS
1850 if (preserve_write)
1851 entry = pmd_mk_savedwrite(entry);
292924b2
PX
1852 if (uffd_wp) {
1853 entry = pmd_wrprotect(entry);
1854 entry = pmd_mkuffd_wp(entry);
1855 } else if (uffd_wp_resolve) {
1856 /*
1857 * Leave the write bit to be handled by PF interrupt
1858 * handler, then things like COW could be properly
1859 * handled.
1860 */
1861 entry = pmd_clear_uffd_wp(entry);
1862 }
0a85e51d
KS
1863 ret = HPAGE_PMD_NR;
1864 set_pmd_at(mm, addr, pmd, entry);
4a18419f 1865
c9fe6656
NA
1866 if (huge_pmd_needs_flush(oldpmd, entry))
1867 tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE);
4a18419f 1868
0a85e51d
KS
1869 BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
1870unlock:
1871 spin_unlock(ptl);
025c5b24
NH
1872 return ret;
1873}
1874
1875/*
8f19b0c0 1876 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
025c5b24 1877 *
8f19b0c0
HY
1878 * Note that if it returns page table lock pointer, this routine returns without
1879 * unlocking page table lock. So callers must unlock it.
025c5b24 1880 */
b6ec57f4 1881spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
025c5b24 1882{
b6ec57f4
KS
1883 spinlock_t *ptl;
1884 ptl = pmd_lock(vma->vm_mm, pmd);
84c3fc4e
ZY
1885 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
1886 pmd_devmap(*pmd)))
b6ec57f4
KS
1887 return ptl;
1888 spin_unlock(ptl);
1889 return NULL;
cd7548ab
JW
1890}
1891
a00cc7d9 1892/*
d965e390 1893 * Returns page table lock pointer if a given pud maps a thp, NULL otherwise.
a00cc7d9 1894 *
d965e390
ML
1895 * Note that if it returns page table lock pointer, this routine returns without
1896 * unlocking page table lock. So callers must unlock it.
a00cc7d9
MW
1897 */
1898spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
1899{
1900 spinlock_t *ptl;
1901
1902 ptl = pud_lock(vma->vm_mm, pud);
1903 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
1904 return ptl;
1905 spin_unlock(ptl);
1906 return NULL;
1907}
1908
1909#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1910int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
1911 pud_t *pud, unsigned long addr)
1912{
a00cc7d9
MW
1913 spinlock_t *ptl;
1914
1915 ptl = __pud_trans_huge_lock(pud, vma);
1916 if (!ptl)
1917 return 0;
1918 /*
1919 * For architectures like ppc64 we look at deposited pgtable
1920 * when calling pudp_huge_get_and_clear. So do the
1921 * pgtable_trans_huge_withdraw after finishing pudp related
1922 * operations.
1923 */
70516b93 1924 pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
a00cc7d9 1925 tlb_remove_pud_tlb_entry(tlb, pud, addr);
2484ca9b 1926 if (vma_is_special_huge(vma)) {
a00cc7d9
MW
1927 spin_unlock(ptl);
1928 /* No zero page support yet */
1929 } else {
1930 /* No support for anonymous PUD pages yet */
1931 BUG();
1932 }
1933 return 1;
1934}
1935
1936static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
1937 unsigned long haddr)
1938{
1939 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
1940 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1941 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
1942 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
1943
ce9311cf 1944 count_vm_event(THP_SPLIT_PUD);
a00cc7d9
MW
1945
1946 pudp_huge_clear_flush_notify(vma, haddr, pud);
1947}
1948
1949void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
1950 unsigned long address)
1951{
1952 spinlock_t *ptl;
ac46d4f3 1953 struct mmu_notifier_range range;
a00cc7d9 1954
7269f999 1955 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 1956 address & HPAGE_PUD_MASK,
ac46d4f3
JG
1957 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
1958 mmu_notifier_invalidate_range_start(&range);
1959 ptl = pud_lock(vma->vm_mm, pud);
a00cc7d9
MW
1960 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
1961 goto out;
ac46d4f3 1962 __split_huge_pud_locked(vma, pud, range.start);
a00cc7d9
MW
1963
1964out:
1965 spin_unlock(ptl);
4645b9fe
JG
1966 /*
1967 * No need to double call mmu_notifier->invalidate_range() callback as
1968 * the above pudp_huge_clear_flush_notify() did already call it.
1969 */
ac46d4f3 1970 mmu_notifier_invalidate_range_only_end(&range);
a00cc7d9
MW
1971}
1972#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1973
eef1b3ba
KS
1974static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
1975 unsigned long haddr, pmd_t *pmd)
1976{
1977 struct mm_struct *mm = vma->vm_mm;
1978 pgtable_t pgtable;
1979 pmd_t _pmd;
1980 int i;
1981
0f10851e
JG
1982 /*
1983 * Leave pmd empty until pte is filled note that it is fine to delay
1984 * notification until mmu_notifier_invalidate_range_end() as we are
1985 * replacing a zero pmd write protected page with a zero pte write
1986 * protected page.
1987 *
ee65728e 1988 * See Documentation/mm/mmu_notifier.rst
0f10851e
JG
1989 */
1990 pmdp_huge_clear_flush(vma, haddr, pmd);
eef1b3ba
KS
1991
1992 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1993 pmd_populate(mm, &_pmd, pgtable);
1994
1995 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1996 pte_t *pte, entry;
1997 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1998 entry = pte_mkspecial(entry);
1999 pte = pte_offset_map(&_pmd, haddr);
2000 VM_BUG_ON(!pte_none(*pte));
2001 set_pte_at(mm, haddr, pte, entry);
2002 pte_unmap(pte);
2003 }
2004 smp_wmb(); /* make pte visible before pmd */
2005 pmd_populate(mm, pmd, pgtable);
eef1b3ba
KS
2006}
2007
2008static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
ba988280 2009 unsigned long haddr, bool freeze)
eef1b3ba
KS
2010{
2011 struct mm_struct *mm = vma->vm_mm;
2012 struct page *page;
2013 pgtable_t pgtable;
423ac9af 2014 pmd_t old_pmd, _pmd;
292924b2 2015 bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
6c287605 2016 bool anon_exclusive = false;
2ac015e2 2017 unsigned long addr;
eef1b3ba
KS
2018 int i;
2019
2020 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2021 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2022 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
84c3fc4e
ZY
2023 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
2024 && !pmd_devmap(*pmd));
eef1b3ba
KS
2025
2026 count_vm_event(THP_SPLIT_PMD);
2027
d21b9e57 2028 if (!vma_is_anonymous(vma)) {
99fa8a48 2029 old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
953c66c2
AK
2030 /*
2031 * We are going to unmap this huge page. So
2032 * just go ahead and zap it
2033 */
2034 if (arch_needs_pgtable_deposit())
2035 zap_deposited_table(mm, pmd);
2484ca9b 2036 if (vma_is_special_huge(vma))
d21b9e57 2037 return;
99fa8a48
HD
2038 if (unlikely(is_pmd_migration_entry(old_pmd))) {
2039 swp_entry_t entry;
2040
2041 entry = pmd_to_swp_entry(old_pmd);
af5cdaf8 2042 page = pfn_swap_entry_to_page(entry);
99fa8a48
HD
2043 } else {
2044 page = pmd_page(old_pmd);
2045 if (!PageDirty(page) && pmd_dirty(old_pmd))
2046 set_page_dirty(page);
2047 if (!PageReferenced(page) && pmd_young(old_pmd))
2048 SetPageReferenced(page);
cea86fe2 2049 page_remove_rmap(page, vma, true);
99fa8a48
HD
2050 put_page(page);
2051 }
fadae295 2052 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
eef1b3ba 2053 return;
99fa8a48
HD
2054 }
2055
3b77e8c8 2056 if (is_huge_zero_pmd(*pmd)) {
4645b9fe
JG
2057 /*
2058 * FIXME: Do we want to invalidate secondary mmu by calling
2059 * mmu_notifier_invalidate_range() see comments below inside
2060 * __split_huge_pmd() ?
2061 *
2062 * We are going from a zero huge page write protected to zero
2063 * small page also write protected so it does not seems useful
2064 * to invalidate secondary mmu at this time.
2065 */
eef1b3ba
KS
2066 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2067 }
2068
423ac9af
AK
2069 /*
2070 * Up to this point the pmd is present and huge and userland has the
2071 * whole access to the hugepage during the split (which happens in
2072 * place). If we overwrite the pmd with the not-huge version pointing
2073 * to the pte here (which of course we could if all CPUs were bug
2074 * free), userland could trigger a small page size TLB miss on the
2075 * small sized TLB while the hugepage TLB entry is still established in
2076 * the huge TLB. Some CPU doesn't like that.
42742d9b
AK
2077 * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
2078 * 383 on page 105. Intel should be safe but is also warns that it's
423ac9af
AK
2079 * only safe if the permission and cache attributes of the two entries
2080 * loaded in the two TLB is identical (which should be the case here).
2081 * But it is generally safer to never allow small and huge TLB entries
2082 * for the same virtual address to be loaded simultaneously. So instead
2083 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2084 * current pmd notpresent (atomically because here the pmd_trans_huge
2085 * must remain set at all times on the pmd until the split is complete
2086 * for this pmd), then we flush the SMP TLB and finally we write the
2087 * non-huge version of the pmd entry with pmd_populate.
2088 */
2089 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2090
423ac9af 2091 pmd_migration = is_pmd_migration_entry(old_pmd);
2e83ee1d 2092 if (unlikely(pmd_migration)) {
84c3fc4e
ZY
2093 swp_entry_t entry;
2094
423ac9af 2095 entry = pmd_to_swp_entry(old_pmd);
af5cdaf8 2096 page = pfn_swap_entry_to_page(entry);
4dd845b5 2097 write = is_writable_migration_entry(entry);
6c287605
DH
2098 if (PageAnon(page))
2099 anon_exclusive = is_readable_exclusive_migration_entry(entry);
2e83ee1d
PX
2100 young = false;
2101 soft_dirty = pmd_swp_soft_dirty(old_pmd);
f45ec5ff 2102 uffd_wp = pmd_swp_uffd_wp(old_pmd);
2e83ee1d 2103 } else {
423ac9af 2104 page = pmd_page(old_pmd);
2e83ee1d
PX
2105 if (pmd_dirty(old_pmd))
2106 SetPageDirty(page);
2107 write = pmd_write(old_pmd);
2108 young = pmd_young(old_pmd);
2109 soft_dirty = pmd_soft_dirty(old_pmd);
292924b2 2110 uffd_wp = pmd_uffd_wp(old_pmd);
6c287605 2111
9d84604b
HD
2112 VM_BUG_ON_PAGE(!page_count(page), page);
2113 page_ref_add(page, HPAGE_PMD_NR - 1);
6c287605
DH
2114
2115 /*
2116 * Without "freeze", we'll simply split the PMD, propagating the
2117 * PageAnonExclusive() flag for each PTE by setting it for
2118 * each subpage -- no need to (temporarily) clear.
2119 *
2120 * With "freeze" we want to replace mapped pages by
2121 * migration entries right away. This is only possible if we
2122 * managed to clear PageAnonExclusive() -- see
2123 * set_pmd_migration_entry().
2124 *
2125 * In case we cannot clear PageAnonExclusive(), split the PMD
2126 * only and let try_to_migrate_one() fail later.
2127 */
2128 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
2129 if (freeze && anon_exclusive && page_try_share_anon_rmap(page))
2130 freeze = false;
2e83ee1d 2131 }
eef1b3ba 2132
423ac9af
AK
2133 /*
2134 * Withdraw the table only after we mark the pmd entry invalid.
2135 * This's critical for some architectures (Power).
2136 */
eef1b3ba
KS
2137 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2138 pmd_populate(mm, &_pmd, pgtable);
2139
2ac015e2 2140 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
eef1b3ba
KS
2141 pte_t entry, *pte;
2142 /*
2143 * Note that NUMA hinting access restrictions are not
2144 * transferred to avoid any possibility of altering
2145 * permissions across VMAs.
2146 */
84c3fc4e 2147 if (freeze || pmd_migration) {
ba988280 2148 swp_entry_t swp_entry;
4dd845b5
AP
2149 if (write)
2150 swp_entry = make_writable_migration_entry(
2151 page_to_pfn(page + i));
6c287605
DH
2152 else if (anon_exclusive)
2153 swp_entry = make_readable_exclusive_migration_entry(
2154 page_to_pfn(page + i));
4dd845b5
AP
2155 else
2156 swp_entry = make_readable_migration_entry(
2157 page_to_pfn(page + i));
ba988280 2158 entry = swp_entry_to_pte(swp_entry);
804dd150
AA
2159 if (soft_dirty)
2160 entry = pte_swp_mksoft_dirty(entry);
f45ec5ff
PX
2161 if (uffd_wp)
2162 entry = pte_swp_mkuffd_wp(entry);
ba988280 2163 } else {
6d2329f8 2164 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
b8d3c4c3 2165 entry = maybe_mkwrite(entry, vma);
6c287605
DH
2166 if (anon_exclusive)
2167 SetPageAnonExclusive(page + i);
ba988280
KS
2168 if (!write)
2169 entry = pte_wrprotect(entry);
2170 if (!young)
2171 entry = pte_mkold(entry);
804dd150
AA
2172 if (soft_dirty)
2173 entry = pte_mksoft_dirty(entry);
292924b2
PX
2174 if (uffd_wp)
2175 entry = pte_mkuffd_wp(entry);
ba988280 2176 }
2ac015e2 2177 pte = pte_offset_map(&_pmd, addr);
eef1b3ba 2178 BUG_ON(!pte_none(*pte));
2ac015e2 2179 set_pte_at(mm, addr, pte, entry);
ec0abae6 2180 if (!pmd_migration)
eef1b3ba 2181 atomic_inc(&page[i]._mapcount);
ec0abae6 2182 pte_unmap(pte);
eef1b3ba
KS
2183 }
2184
ec0abae6
RC
2185 if (!pmd_migration) {
2186 /*
2187 * Set PG_double_map before dropping compound_mapcount to avoid
2188 * false-negative page_mapped().
2189 */
2190 if (compound_mapcount(page) > 1 &&
2191 !TestSetPageDoubleMap(page)) {
eef1b3ba 2192 for (i = 0; i < HPAGE_PMD_NR; i++)
ec0abae6
RC
2193 atomic_inc(&page[i]._mapcount);
2194 }
2195
2196 lock_page_memcg(page);
2197 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2198 /* Last compound_mapcount is gone. */
69473e5d
MS
2199 __mod_lruvec_page_state(page, NR_ANON_THPS,
2200 -HPAGE_PMD_NR);
ec0abae6
RC
2201 if (TestClearPageDoubleMap(page)) {
2202 /* No need in mapcount reference anymore */
2203 for (i = 0; i < HPAGE_PMD_NR; i++)
2204 atomic_dec(&page[i]._mapcount);
2205 }
eef1b3ba 2206 }
ec0abae6 2207 unlock_page_memcg(page);
cea86fe2
HD
2208
2209 /* Above is effectively page_remove_rmap(page, vma, true) */
2210 munlock_vma_page(page, vma, true);
eef1b3ba
KS
2211 }
2212
2213 smp_wmb(); /* make pte visible before pmd */
2214 pmd_populate(mm, pmd, pgtable);
e9b61f19
KS
2215
2216 if (freeze) {
2ac015e2 2217 for (i = 0; i < HPAGE_PMD_NR; i++) {
cea86fe2 2218 page_remove_rmap(page + i, vma, false);
e9b61f19
KS
2219 put_page(page + i);
2220 }
2221 }
eef1b3ba
KS
2222}
2223
2224void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
af28a988 2225 unsigned long address, bool freeze, struct folio *folio)
eef1b3ba
KS
2226{
2227 spinlock_t *ptl;
ac46d4f3 2228 struct mmu_notifier_range range;
eef1b3ba 2229
7269f999 2230 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
6f4f13e8 2231 address & HPAGE_PMD_MASK,
ac46d4f3
JG
2232 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2233 mmu_notifier_invalidate_range_start(&range);
2234 ptl = pmd_lock(vma->vm_mm, pmd);
33f4751e
NH
2235
2236 /*
af28a988
MWO
2237 * If caller asks to setup a migration entry, we need a folio to check
2238 * pmd against. Otherwise we can end up replacing wrong folio.
33f4751e 2239 */
af28a988 2240 VM_BUG_ON(freeze && !folio);
83a8441f 2241 VM_WARN_ON_ONCE(folio && !folio_test_locked(folio));
33f4751e 2242
7f760917 2243 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) ||
83a8441f
MWO
2244 is_pmd_migration_entry(*pmd)) {
2245 if (folio && folio != page_folio(pmd_page(*pmd)))
2246 goto out;
7f760917 2247 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
83a8441f 2248 }
7f760917 2249
e90309c9 2250out:
eef1b3ba 2251 spin_unlock(ptl);
4645b9fe
JG
2252 /*
2253 * No need to double call mmu_notifier->invalidate_range() callback.
2254 * They are 3 cases to consider inside __split_huge_pmd_locked():
2255 * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious
2256 * 2) __split_huge_zero_page_pmd() read only zero page and any write
2257 * fault will trigger a flush_notify before pointing to a new page
2258 * (it is fine if the secondary mmu keeps pointing to the old zero
2259 * page in the meantime)
2260 * 3) Split a huge pmd into pte pointing to the same page. No need
2261 * to invalidate secondary tlb entry they are all still valid.
2262 * any further changes to individual pte will notify. So no need
2263 * to call mmu_notifier->invalidate_range()
2264 */
ac46d4f3 2265 mmu_notifier_invalidate_range_only_end(&range);
eef1b3ba
KS
2266}
2267
fec89c10 2268void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
af28a988 2269 bool freeze, struct folio *folio)
94fcc585 2270{
f72e7dcd 2271 pgd_t *pgd;
c2febafc 2272 p4d_t *p4d;
f72e7dcd 2273 pud_t *pud;
94fcc585
AA
2274 pmd_t *pmd;
2275
78ddc534 2276 pgd = pgd_offset(vma->vm_mm, address);
f72e7dcd
HD
2277 if (!pgd_present(*pgd))
2278 return;
2279
c2febafc
KS
2280 p4d = p4d_offset(pgd, address);
2281 if (!p4d_present(*p4d))
2282 return;
2283
2284 pud = pud_offset(p4d, address);
f72e7dcd
HD
2285 if (!pud_present(*pud))
2286 return;
2287
2288 pmd = pmd_offset(pud, address);
fec89c10 2289
af28a988 2290 __split_huge_pmd(vma, pmd, address, freeze, folio);
94fcc585
AA
2291}
2292
71f9e58e
ML
2293static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
2294{
2295 /*
2296 * If the new address isn't hpage aligned and it could previously
2297 * contain an hugepage: check if we need to split an huge pmd.
2298 */
2299 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
2300 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
2301 ALIGN(address, HPAGE_PMD_SIZE)))
2302 split_huge_pmd_address(vma, address, false, NULL);
2303}
2304
e1b9996b 2305void vma_adjust_trans_huge(struct vm_area_struct *vma,
94fcc585
AA
2306 unsigned long start,
2307 unsigned long end,
2308 long adjust_next)
2309{
71f9e58e
ML
2310 /* Check if we need to split start first. */
2311 split_huge_pmd_if_needed(vma, start);
94fcc585 2312
71f9e58e
ML
2313 /* Check if we need to split end next. */
2314 split_huge_pmd_if_needed(vma, end);
94fcc585
AA
2315
2316 /*
71f9e58e
ML
2317 * If we're also updating the vma->vm_next->vm_start,
2318 * check if we need to split it.
94fcc585
AA
2319 */
2320 if (adjust_next > 0) {
2321 struct vm_area_struct *next = vma->vm_next;
2322 unsigned long nstart = next->vm_start;
f9d86a60 2323 nstart += adjust_next;
71f9e58e 2324 split_huge_pmd_if_needed(next, nstart);
94fcc585
AA
2325 }
2326}
e9b61f19 2327
906f9cdf 2328static void unmap_page(struct page *page)
e9b61f19 2329{
869f7ee6 2330 struct folio *folio = page_folio(page);
a98a2f0c
AP
2331 enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD |
2332 TTU_SYNC;
e9b61f19
KS
2333
2334 VM_BUG_ON_PAGE(!PageHead(page), page);
2335
a98a2f0c
AP
2336 /*
2337 * Anon pages need migration entries to preserve them, but file
2338 * pages can simply be left unmapped, then faulted back on demand.
2339 * If that is ever changed (perhaps for mlock), update remap_page().
2340 */
4b8554c5
MWO
2341 if (folio_test_anon(folio))
2342 try_to_migrate(folio, ttu_flags);
a98a2f0c 2343 else
869f7ee6 2344 try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK);
e9b61f19
KS
2345}
2346
4eecb8b9 2347static void remap_page(struct folio *folio, unsigned long nr)
e9b61f19 2348{
4eecb8b9 2349 int i = 0;
ab02c252 2350
64b586d1 2351 /* If unmap_page() uses try_to_migrate() on file, remove this check */
4eecb8b9 2352 if (!folio_test_anon(folio))
ab02c252 2353 return;
4eecb8b9
MWO
2354 for (;;) {
2355 remove_migration_ptes(folio, folio, true);
2356 i += folio_nr_pages(folio);
2357 if (i >= nr)
2358 break;
2359 folio = folio_next(folio);
ace71a19 2360 }
e9b61f19
KS
2361}
2362
94866635 2363static void lru_add_page_tail(struct page *head, struct page *tail,
88dcb9a3
AS
2364 struct lruvec *lruvec, struct list_head *list)
2365{
94866635
AS
2366 VM_BUG_ON_PAGE(!PageHead(head), head);
2367 VM_BUG_ON_PAGE(PageCompound(tail), head);
2368 VM_BUG_ON_PAGE(PageLRU(tail), head);
6168d0da 2369 lockdep_assert_held(&lruvec->lru_lock);
88dcb9a3 2370
6dbb5741 2371 if (list) {
88dcb9a3 2372 /* page reclaim is reclaiming a huge page */
6dbb5741 2373 VM_WARN_ON(PageLRU(head));
94866635
AS
2374 get_page(tail);
2375 list_add_tail(&tail->lru, list);
88dcb9a3 2376 } else {
6dbb5741
AS
2377 /* head is still on lru (and we have it frozen) */
2378 VM_WARN_ON(!PageLRU(head));
07ca7606
HD
2379 if (PageUnevictable(tail))
2380 tail->mlock_count = 0;
2381 else
2382 list_add_tail(&tail->lru, &head->lru);
6dbb5741 2383 SetPageLRU(tail);
88dcb9a3
AS
2384 }
2385}
2386
8df651c7 2387static void __split_huge_page_tail(struct page *head, int tail,
e9b61f19
KS
2388 struct lruvec *lruvec, struct list_head *list)
2389{
e9b61f19
KS
2390 struct page *page_tail = head + tail;
2391
8df651c7 2392 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
e9b61f19
KS
2393
2394 /*
605ca5ed
KK
2395 * Clone page flags before unfreezing refcount.
2396 *
2397 * After successful get_page_unless_zero() might follow flags change,
8958b249 2398 * for example lock_page() which set PG_waiters.
6c287605
DH
2399 *
2400 * Note that for mapped sub-pages of an anonymous THP,
2401 * PG_anon_exclusive has been cleared in unmap_page() and is stored in
2402 * the migration entry instead from where remap_page() will restore it.
2403 * We can still have PG_anon_exclusive set on effectively unmapped and
2404 * unreferenced sub-pages of an anonymous THP: we can simply drop
2405 * PG_anon_exclusive (-> PG_mappedtodisk) for these here.
e9b61f19 2406 */
e9b61f19
KS
2407 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2408 page_tail->flags |= (head->flags &
2409 ((1L << PG_referenced) |
2410 (1L << PG_swapbacked) |
38d8b4e6 2411 (1L << PG_swapcache) |
e9b61f19
KS
2412 (1L << PG_mlocked) |
2413 (1L << PG_uptodate) |
2414 (1L << PG_active) |
1899ad18 2415 (1L << PG_workingset) |
e9b61f19 2416 (1L << PG_locked) |
b8d3c4c3 2417 (1L << PG_unevictable) |
72e6afa0
CM
2418#ifdef CONFIG_64BIT
2419 (1L << PG_arch_2) |
2420#endif
b8d3c4c3 2421 (1L << PG_dirty)));
e9b61f19 2422
173d9d9f
HD
2423 /* ->mapping in first tail page is compound_mapcount */
2424 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2425 page_tail);
2426 page_tail->mapping = head->mapping;
2427 page_tail->index = head->index + tail;
b653db77 2428 page_tail->private = 0;
173d9d9f 2429
605ca5ed 2430 /* Page flags must be visible before we make the page non-compound. */
e9b61f19
KS
2431 smp_wmb();
2432
605ca5ed
KK
2433 /*
2434 * Clear PageTail before unfreezing page refcount.
2435 *
2436 * After successful get_page_unless_zero() might follow put_page()
2437 * which needs correct compound_head().
2438 */
e9b61f19
KS
2439 clear_compound_head(page_tail);
2440
605ca5ed
KK
2441 /* Finally unfreeze refcount. Additional reference from page cache. */
2442 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2443 PageSwapCache(head)));
2444
e9b61f19
KS
2445 if (page_is_young(head))
2446 set_page_young(page_tail);
2447 if (page_is_idle(head))
2448 set_page_idle(page_tail);
2449
e9b61f19 2450 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
94723aaf
MH
2451
2452 /*
2453 * always add to the tail because some iterators expect new
2454 * pages to show after the currently processed elements - e.g.
2455 * migrate_pages
2456 */
e9b61f19 2457 lru_add_page_tail(head, page_tail, lruvec, list);
e9b61f19
KS
2458}
2459
baa355fd 2460static void __split_huge_page(struct page *page, struct list_head *list,
b6769834 2461 pgoff_t end)
e9b61f19 2462{
e809c3fe
MWO
2463 struct folio *folio = page_folio(page);
2464 struct page *head = &folio->page;
e9b61f19 2465 struct lruvec *lruvec;
4101196b
MWO
2466 struct address_space *swap_cache = NULL;
2467 unsigned long offset = 0;
8cce5475 2468 unsigned int nr = thp_nr_pages(head);
8df651c7 2469 int i;
e9b61f19 2470
e9b61f19 2471 /* complete memcg works before add pages to LRU */
be6c8982 2472 split_page_memcg(head, nr);
e9b61f19 2473
4101196b
MWO
2474 if (PageAnon(head) && PageSwapCache(head)) {
2475 swp_entry_t entry = { .val = page_private(head) };
2476
2477 offset = swp_offset(entry);
2478 swap_cache = swap_address_space(entry);
2479 xa_lock(&swap_cache->i_pages);
2480 }
2481
f0953a1b 2482 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */
e809c3fe 2483 lruvec = folio_lruvec_lock(folio);
b6769834 2484
eac96c3e
YS
2485 ClearPageHasHWPoisoned(head);
2486
8cce5475 2487 for (i = nr - 1; i >= 1; i--) {
8df651c7 2488 __split_huge_page_tail(head, i, lruvec, list);
d144bf62 2489 /* Some pages can be beyond EOF: drop them from page cache */
baa355fd 2490 if (head[i].index >= end) {
2d077d4b 2491 ClearPageDirty(head + i);
baa355fd 2492 __delete_from_page_cache(head + i, NULL);
d144bf62 2493 if (shmem_mapping(head->mapping))
800d8c63 2494 shmem_uncharge(head->mapping->host, 1);
baa355fd 2495 put_page(head + i);
4101196b
MWO
2496 } else if (!PageAnon(page)) {
2497 __xa_store(&head->mapping->i_pages, head[i].index,
2498 head + i, 0);
2499 } else if (swap_cache) {
2500 __xa_store(&swap_cache->i_pages, offset + i,
2501 head + i, 0);
baa355fd
KS
2502 }
2503 }
e9b61f19
KS
2504
2505 ClearPageCompound(head);
6168d0da 2506 unlock_page_lruvec(lruvec);
b6769834 2507 /* Caller disabled irqs, so they are still disabled here */
f7da677b 2508
8cce5475 2509 split_page_owner(head, nr);
f7da677b 2510
baa355fd
KS
2511 /* See comment in __split_huge_page_tail() */
2512 if (PageAnon(head)) {
aa5dc07f 2513 /* Additional pin to swap cache */
4101196b 2514 if (PageSwapCache(head)) {
38d8b4e6 2515 page_ref_add(head, 2);
4101196b
MWO
2516 xa_unlock(&swap_cache->i_pages);
2517 } else {
38d8b4e6 2518 page_ref_inc(head);
4101196b 2519 }
baa355fd 2520 } else {
aa5dc07f 2521 /* Additional pin to page cache */
baa355fd 2522 page_ref_add(head, 2);
b93b0163 2523 xa_unlock(&head->mapping->i_pages);
baa355fd 2524 }
b6769834 2525 local_irq_enable();
e9b61f19 2526
4eecb8b9 2527 remap_page(folio, nr);
e9b61f19 2528
c4f9c701
HY
2529 if (PageSwapCache(head)) {
2530 swp_entry_t entry = { .val = page_private(head) };
2531
2532 split_swap_cluster(entry);
2533 }
2534
8cce5475 2535 for (i = 0; i < nr; i++) {
e9b61f19
KS
2536 struct page *subpage = head + i;
2537 if (subpage == page)
2538 continue;
2539 unlock_page(subpage);
2540
2541 /*
2542 * Subpages may be freed if there wasn't any mapping
2543 * like if add_to_swap() is running on a lru page that
2544 * had its mapping zapped. And freeing these pages
2545 * requires taking the lru_lock so we do the put_page
2546 * of the tail pages after the split is complete.
2547 */
2548 put_page(subpage);
2549 }
2550}
2551
b8f593cd 2552/* Racy check whether the huge page can be split */
d4b4084a 2553bool can_split_folio(struct folio *folio, int *pextra_pins)
b8f593cd
HY
2554{
2555 int extra_pins;
2556
aa5dc07f 2557 /* Additional pins from page cache */
d4b4084a
MWO
2558 if (folio_test_anon(folio))
2559 extra_pins = folio_test_swapcache(folio) ?
2560 folio_nr_pages(folio) : 0;
b8f593cd 2561 else
d4b4084a 2562 extra_pins = folio_nr_pages(folio);
b8f593cd
HY
2563 if (pextra_pins)
2564 *pextra_pins = extra_pins;
d4b4084a 2565 return folio_mapcount(folio) == folio_ref_count(folio) - extra_pins - 1;
b8f593cd
HY
2566}
2567
e9b61f19
KS
2568/*
2569 * This function splits huge page into normal pages. @page can point to any
2570 * subpage of huge page to split. Split doesn't change the position of @page.
2571 *
2572 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
2573 * The huge page must be locked.
2574 *
2575 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
2576 *
2577 * Both head page and tail pages will inherit mapping, flags, and so on from
2578 * the hugepage.
2579 *
2580 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
2581 * they are not mapped.
2582 *
2583 * Returns 0 if the hugepage is split successfully.
2584 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
2585 * us.
2586 */
2587int split_huge_page_to_list(struct page *page, struct list_head *list)
2588{
4eecb8b9
MWO
2589 struct folio *folio = page_folio(page);
2590 struct page *head = &folio->page;
a8803e6c 2591 struct deferred_split *ds_queue = get_deferred_split_queue(head);
6b24ca4a 2592 XA_STATE(xas, &head->mapping->i_pages, head->index);
baa355fd
KS
2593 struct anon_vma *anon_vma = NULL;
2594 struct address_space *mapping = NULL;
504e070d 2595 int extra_pins, ret;
006d3ff2 2596 pgoff_t end;
478d134e 2597 bool is_hzp;
e9b61f19 2598
a8803e6c
WY
2599 VM_BUG_ON_PAGE(!PageLocked(head), head);
2600 VM_BUG_ON_PAGE(!PageCompound(head), head);
e9b61f19 2601
478d134e
XY
2602 is_hzp = is_huge_zero_page(head);
2603 VM_WARN_ON_ONCE_PAGE(is_hzp, head);
2604 if (is_hzp)
2605 return -EBUSY;
2606
a8803e6c 2607 if (PageWriteback(head))
59807685
HY
2608 return -EBUSY;
2609
baa355fd
KS
2610 if (PageAnon(head)) {
2611 /*
c1e8d7c6 2612 * The caller does not necessarily hold an mmap_lock that would
baa355fd
KS
2613 * prevent the anon_vma disappearing so we first we take a
2614 * reference to it and then lock the anon_vma for write. This
2f031c6f 2615 * is similar to folio_lock_anon_vma_read except the write lock
baa355fd
KS
2616 * is taken to serialise against parallel split or collapse
2617 * operations.
2618 */
2619 anon_vma = page_get_anon_vma(head);
2620 if (!anon_vma) {
2621 ret = -EBUSY;
2622 goto out;
2623 }
006d3ff2 2624 end = -1;
baa355fd
KS
2625 mapping = NULL;
2626 anon_vma_lock_write(anon_vma);
2627 } else {
2628 mapping = head->mapping;
2629
2630 /* Truncated ? */
2631 if (!mapping) {
2632 ret = -EBUSY;
2633 goto out;
2634 }
2635
6b24ca4a
MWO
2636 xas_split_alloc(&xas, head, compound_order(head),
2637 mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK);
2638 if (xas_error(&xas)) {
2639 ret = xas_error(&xas);
2640 goto out;
2641 }
2642
baa355fd
KS
2643 anon_vma = NULL;
2644 i_mmap_lock_read(mapping);
006d3ff2
HD
2645
2646 /*
2647 *__split_huge_page() may need to trim off pages beyond EOF:
2648 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2649 * which cannot be nested inside the page tree lock. So note
2650 * end now: i_size itself may be changed at any moment, but
2651 * head page lock is good enough to serialize the trimming.
2652 */
2653 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
d144bf62
HD
2654 if (shmem_mapping(mapping))
2655 end = shmem_fallocend(mapping->host, end);
e9b61f19 2656 }
e9b61f19
KS
2657
2658 /*
906f9cdf 2659 * Racy check if we can split the page, before unmap_page() will
e9b61f19
KS
2660 * split PMDs
2661 */
d4b4084a 2662 if (!can_split_folio(folio, &extra_pins)) {
e9b61f19
KS
2663 ret = -EBUSY;
2664 goto out_unlock;
2665 }
2666
906f9cdf 2667 unmap_page(head);
e9b61f19 2668
b6769834
AS
2669 /* block interrupt reentry in xa_lock and spinlock */
2670 local_irq_disable();
baa355fd 2671 if (mapping) {
baa355fd 2672 /*
aa5dc07f 2673 * Check if the head page is present in page cache.
baa355fd
KS
2674 * We assume all tail are present too, if head is there.
2675 */
6b24ca4a
MWO
2676 xas_lock(&xas);
2677 xas_reset(&xas);
aa5dc07f 2678 if (xas_load(&xas) != head)
baa355fd
KS
2679 goto fail;
2680 }
2681
0139aa7b 2682 /* Prevent deferred_split_scan() touching ->_refcount */
364c1eeb 2683 spin_lock(&ds_queue->split_queue_lock);
504e070d 2684 if (page_ref_freeze(head, 1 + extra_pins)) {
9a982250 2685 if (!list_empty(page_deferred_list(head))) {
364c1eeb 2686 ds_queue->split_queue_len--;
9a982250
KS
2687 list_del(page_deferred_list(head));
2688 }
afb97172 2689 spin_unlock(&ds_queue->split_queue_lock);
06d3eff6 2690 if (mapping) {
bf9ecead
MS
2691 int nr = thp_nr_pages(head);
2692
6b24ca4a 2693 xas_split(&xas, head, thp_order(head));
1ca7554d 2694 if (PageSwapBacked(head)) {
57b2847d
MS
2695 __mod_lruvec_page_state(head, NR_SHMEM_THPS,
2696 -nr);
1ca7554d 2697 } else {
bf9ecead
MS
2698 __mod_lruvec_page_state(head, NR_FILE_THPS,
2699 -nr);
1ca7554d
MS
2700 filemap_nr_thps_dec(mapping);
2701 }
06d3eff6
KS
2702 }
2703
b6769834 2704 __split_huge_page(page, list, end);
c4f9c701 2705 ret = 0;
e9b61f19 2706 } else {
364c1eeb 2707 spin_unlock(&ds_queue->split_queue_lock);
504e070d
YS
2708fail:
2709 if (mapping)
6b24ca4a 2710 xas_unlock(&xas);
b6769834 2711 local_irq_enable();
4eecb8b9 2712 remap_page(folio, folio_nr_pages(folio));
e9b61f19
KS
2713 ret = -EBUSY;
2714 }
2715
2716out_unlock:
baa355fd
KS
2717 if (anon_vma) {
2718 anon_vma_unlock_write(anon_vma);
2719 put_anon_vma(anon_vma);
2720 }
2721 if (mapping)
2722 i_mmap_unlock_read(mapping);
e9b61f19 2723out:
69a37a8b 2724 xas_destroy(&xas);
e9b61f19
KS
2725 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2726 return ret;
2727}
9a982250
KS
2728
2729void free_transhuge_page(struct page *page)
2730{
87eaceb3 2731 struct deferred_split *ds_queue = get_deferred_split_queue(page);
9a982250
KS
2732 unsigned long flags;
2733
364c1eeb 2734 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2735 if (!list_empty(page_deferred_list(page))) {
364c1eeb 2736 ds_queue->split_queue_len--;
9a982250
KS
2737 list_del(page_deferred_list(page));
2738 }
364c1eeb 2739 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2740 free_compound_page(page);
2741}
2742
2743void deferred_split_huge_page(struct page *page)
2744{
87eaceb3
YS
2745 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2746#ifdef CONFIG_MEMCG
bcfe06bf 2747 struct mem_cgroup *memcg = page_memcg(compound_head(page));
87eaceb3 2748#endif
9a982250
KS
2749 unsigned long flags;
2750
2751 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
2752
87eaceb3
YS
2753 /*
2754 * The try_to_unmap() in page reclaim path might reach here too,
2755 * this may cause a race condition to corrupt deferred split queue.
2756 * And, if page reclaim is already handling the same page, it is
2757 * unnecessary to handle it again in shrinker.
2758 *
2759 * Check PageSwapCache to determine if the page is being
2760 * handled by page reclaim since THP swap would add the page into
2761 * swap cache before calling try_to_unmap().
2762 */
2763 if (PageSwapCache(page))
2764 return;
2765
364c1eeb 2766 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2767 if (list_empty(page_deferred_list(page))) {
f9719a03 2768 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
364c1eeb
YS
2769 list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2770 ds_queue->split_queue_len++;
87eaceb3
YS
2771#ifdef CONFIG_MEMCG
2772 if (memcg)
2bfd3637
YS
2773 set_shrinker_bit(memcg, page_to_nid(page),
2774 deferred_split_shrinker.id);
87eaceb3 2775#endif
9a982250 2776 }
364c1eeb 2777 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2778}
2779
2780static unsigned long deferred_split_count(struct shrinker *shrink,
2781 struct shrink_control *sc)
2782{
a3d0a918 2783 struct pglist_data *pgdata = NODE_DATA(sc->nid);
364c1eeb 2784 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
87eaceb3
YS
2785
2786#ifdef CONFIG_MEMCG
2787 if (sc->memcg)
2788 ds_queue = &sc->memcg->deferred_split_queue;
2789#endif
364c1eeb 2790 return READ_ONCE(ds_queue->split_queue_len);
9a982250
KS
2791}
2792
2793static unsigned long deferred_split_scan(struct shrinker *shrink,
2794 struct shrink_control *sc)
2795{
a3d0a918 2796 struct pglist_data *pgdata = NODE_DATA(sc->nid);
364c1eeb 2797 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
9a982250
KS
2798 unsigned long flags;
2799 LIST_HEAD(list), *pos, *next;
2800 struct page *page;
2801 int split = 0;
2802
87eaceb3
YS
2803#ifdef CONFIG_MEMCG
2804 if (sc->memcg)
2805 ds_queue = &sc->memcg->deferred_split_queue;
2806#endif
2807
364c1eeb 2808 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
9a982250 2809 /* Take pin on all head pages to avoid freeing them under us */
364c1eeb 2810 list_for_each_safe(pos, next, &ds_queue->split_queue) {
dfe5c51c 2811 page = list_entry((void *)pos, struct page, deferred_list);
9a982250 2812 page = compound_head(page);
e3ae1953
KS
2813 if (get_page_unless_zero(page)) {
2814 list_move(page_deferred_list(page), &list);
2815 } else {
2816 /* We lost race with put_compound_page() */
9a982250 2817 list_del_init(page_deferred_list(page));
364c1eeb 2818 ds_queue->split_queue_len--;
9a982250 2819 }
e3ae1953
KS
2820 if (!--sc->nr_to_scan)
2821 break;
9a982250 2822 }
364c1eeb 2823 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250
KS
2824
2825 list_for_each_safe(pos, next, &list) {
dfe5c51c 2826 page = list_entry((void *)pos, struct page, deferred_list);
fa41b900
KS
2827 if (!trylock_page(page))
2828 goto next;
9a982250
KS
2829 /* split_huge_page() removes page from list on success */
2830 if (!split_huge_page(page))
2831 split++;
2832 unlock_page(page);
fa41b900 2833next:
9a982250
KS
2834 put_page(page);
2835 }
2836
364c1eeb
YS
2837 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2838 list_splice_tail(&list, &ds_queue->split_queue);
2839 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
9a982250 2840
cb8d68ec
KS
2841 /*
2842 * Stop shrinker if we didn't split any page, but the queue is empty.
2843 * This can happen if pages were freed under us.
2844 */
364c1eeb 2845 if (!split && list_empty(&ds_queue->split_queue))
cb8d68ec
KS
2846 return SHRINK_STOP;
2847 return split;
9a982250
KS
2848}
2849
2850static struct shrinker deferred_split_shrinker = {
2851 .count_objects = deferred_split_count,
2852 .scan_objects = deferred_split_scan,
2853 .seeks = DEFAULT_SEEKS,
87eaceb3
YS
2854 .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
2855 SHRINKER_NONSLAB,
9a982250 2856};
49071d43
KS
2857
2858#ifdef CONFIG_DEBUG_FS
fa6c0231 2859static void split_huge_pages_all(void)
49071d43
KS
2860{
2861 struct zone *zone;
2862 struct page *page;
2863 unsigned long pfn, max_zone_pfn;
2864 unsigned long total = 0, split = 0;
2865
fa6c0231 2866 pr_debug("Split all THPs\n");
49071d43
KS
2867 for_each_populated_zone(zone) {
2868 max_zone_pfn = zone_end_pfn(zone);
2869 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
2870 if (!pfn_valid(pfn))
2871 continue;
2872
2873 page = pfn_to_page(pfn);
2874 if (!get_page_unless_zero(page))
2875 continue;
2876
2877 if (zone != page_zone(page))
2878 goto next;
2879
baa355fd 2880 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
49071d43
KS
2881 goto next;
2882
2883 total++;
2884 lock_page(page);
2885 if (!split_huge_page(page))
2886 split++;
2887 unlock_page(page);
2888next:
2889 put_page(page);
fa6c0231 2890 cond_resched();
49071d43
KS
2891 }
2892 }
2893
fa6c0231
ZY
2894 pr_debug("%lu of %lu THP split\n", split, total);
2895}
49071d43 2896
fa6c0231
ZY
2897static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
2898{
2899 return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) ||
2900 is_vm_hugetlb_page(vma);
2901}
2902
2903static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
2904 unsigned long vaddr_end)
2905{
2906 int ret = 0;
2907 struct task_struct *task;
2908 struct mm_struct *mm;
2909 unsigned long total = 0, split = 0;
2910 unsigned long addr;
2911
2912 vaddr_start &= PAGE_MASK;
2913 vaddr_end &= PAGE_MASK;
2914
2915 /* Find the task_struct from pid */
2916 rcu_read_lock();
2917 task = find_task_by_vpid(pid);
2918 if (!task) {
2919 rcu_read_unlock();
2920 ret = -ESRCH;
2921 goto out;
2922 }
2923 get_task_struct(task);
2924 rcu_read_unlock();
2925
2926 /* Find the mm_struct */
2927 mm = get_task_mm(task);
2928 put_task_struct(task);
2929
2930 if (!mm) {
2931 ret = -EINVAL;
2932 goto out;
2933 }
2934
2935 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n",
2936 pid, vaddr_start, vaddr_end);
2937
2938 mmap_read_lock(mm);
2939 /*
2940 * always increase addr by PAGE_SIZE, since we could have a PTE page
2941 * table filled with PTE-mapped THPs, each of which is distinct.
2942 */
2943 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
2944 struct vm_area_struct *vma = find_vma(mm, addr);
fa6c0231
ZY
2945 struct page *page;
2946
2947 if (!vma || addr < vma->vm_start)
2948 break;
2949
2950 /* skip special VMA and hugetlb VMA */
2951 if (vma_not_suitable_for_thp_split(vma)) {
2952 addr = vma->vm_end;
2953 continue;
2954 }
2955
2956 /* FOLL_DUMP to ignore special (like zero) pages */
87d2762e 2957 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
fa6c0231
ZY
2958
2959 if (IS_ERR(page))
2960 continue;
3218f871 2961 if (!page || is_zone_device_page(page))
fa6c0231
ZY
2962 continue;
2963
2964 if (!is_transparent_hugepage(page))
2965 goto next;
2966
2967 total++;
d4b4084a 2968 if (!can_split_folio(page_folio(page), NULL))
fa6c0231
ZY
2969 goto next;
2970
2971 if (!trylock_page(page))
2972 goto next;
2973
2974 if (!split_huge_page(page))
2975 split++;
2976
2977 unlock_page(page);
2978next:
2979 put_page(page);
2980 cond_resched();
2981 }
2982 mmap_read_unlock(mm);
2983 mmput(mm);
2984
2985 pr_debug("%lu of %lu THP split\n", split, total);
2986
2987out:
2988 return ret;
49071d43 2989}
fa6c0231 2990
fbe37501
ZY
2991static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
2992 pgoff_t off_end)
2993{
2994 struct filename *file;
2995 struct file *candidate;
2996 struct address_space *mapping;
2997 int ret = -EINVAL;
2998 pgoff_t index;
2999 int nr_pages = 1;
3000 unsigned long total = 0, split = 0;
3001
3002 file = getname_kernel(file_path);
3003 if (IS_ERR(file))
3004 return ret;
3005
3006 candidate = file_open_name(file, O_RDONLY, 0);
3007 if (IS_ERR(candidate))
3008 goto out;
3009
3010 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n",
3011 file_path, off_start, off_end);
3012
3013 mapping = candidate->f_mapping;
3014
3015 for (index = off_start; index < off_end; index += nr_pages) {
3016 struct page *fpage = pagecache_get_page(mapping, index,
3017 FGP_ENTRY | FGP_HEAD, 0);
3018
3019 nr_pages = 1;
3020 if (xa_is_value(fpage) || !fpage)
3021 continue;
3022
3023 if (!is_transparent_hugepage(fpage))
3024 goto next;
3025
3026 total++;
3027 nr_pages = thp_nr_pages(fpage);
3028
3029 if (!trylock_page(fpage))
3030 goto next;
3031
3032 if (!split_huge_page(fpage))
3033 split++;
3034
3035 unlock_page(fpage);
3036next:
3037 put_page(fpage);
3038 cond_resched();
3039 }
3040
3041 filp_close(candidate, NULL);
3042 ret = 0;
3043
3044 pr_debug("%lu of %lu file-backed THP split\n", split, total);
3045out:
3046 putname(file);
3047 return ret;
3048}
3049
fa6c0231
ZY
3050#define MAX_INPUT_BUF_SZ 255
3051
3052static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
3053 size_t count, loff_t *ppops)
3054{
3055 static DEFINE_MUTEX(split_debug_mutex);
3056 ssize_t ret;
fbe37501
ZY
3057 /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3058 char input_buf[MAX_INPUT_BUF_SZ];
fa6c0231
ZY
3059 int pid;
3060 unsigned long vaddr_start, vaddr_end;
3061
3062 ret = mutex_lock_interruptible(&split_debug_mutex);
3063 if (ret)
3064 return ret;
3065
3066 ret = -EFAULT;
3067
3068 memset(input_buf, 0, MAX_INPUT_BUF_SZ);
3069 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
3070 goto out;
3071
3072 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
fbe37501
ZY
3073
3074 if (input_buf[0] == '/') {
3075 char *tok;
3076 char *buf = input_buf;
3077 char file_path[MAX_INPUT_BUF_SZ];
3078 pgoff_t off_start = 0, off_end = 0;
3079 size_t input_len = strlen(input_buf);
3080
3081 tok = strsep(&buf, ",");
3082 if (tok) {
1212e00c 3083 strcpy(file_path, tok);
fbe37501
ZY
3084 } else {
3085 ret = -EINVAL;
3086 goto out;
3087 }
3088
3089 ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3090 if (ret != 2) {
3091 ret = -EINVAL;
3092 goto out;
3093 }
3094 ret = split_huge_pages_in_file(file_path, off_start, off_end);
3095 if (!ret)
3096 ret = input_len;
3097
3098 goto out;
3099 }
3100
fa6c0231
ZY
3101 ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3102 if (ret == 1 && pid == 1) {
3103 split_huge_pages_all();
3104 ret = strlen(input_buf);
3105 goto out;
3106 } else if (ret != 3) {
3107 ret = -EINVAL;
3108 goto out;
3109 }
3110
3111 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3112 if (!ret)
3113 ret = strlen(input_buf);
3114out:
3115 mutex_unlock(&split_debug_mutex);
3116 return ret;
3117
3118}
3119
3120static const struct file_operations split_huge_pages_fops = {
3121 .owner = THIS_MODULE,
3122 .write = split_huge_pages_write,
3123 .llseek = no_llseek,
3124};
49071d43
KS
3125
3126static int __init split_huge_pages_debugfs(void)
3127{
d9f7979c
GKH
3128 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3129 &split_huge_pages_fops);
49071d43
KS
3130 return 0;
3131}
3132late_initcall(split_huge_pages_debugfs);
3133#endif
616b8371
ZY
3134
3135#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
7f5abe60 3136int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
616b8371
ZY
3137 struct page *page)
3138{
3139 struct vm_area_struct *vma = pvmw->vma;
3140 struct mm_struct *mm = vma->vm_mm;
3141 unsigned long address = pvmw->address;
6c287605 3142 bool anon_exclusive;
616b8371
ZY
3143 pmd_t pmdval;
3144 swp_entry_t entry;
ab6e3d09 3145 pmd_t pmdswp;
616b8371
ZY
3146
3147 if (!(pvmw->pmd && !pvmw->pte))
7f5abe60 3148 return 0;
616b8371 3149
616b8371 3150 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
8a8683ad 3151 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
6c287605
DH
3152
3153 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
3154 if (anon_exclusive && page_try_share_anon_rmap(page)) {
3155 set_pmd_at(mm, address, pvmw->pmd, pmdval);
7f5abe60 3156 return -EBUSY;
6c287605
DH
3157 }
3158
616b8371
ZY
3159 if (pmd_dirty(pmdval))
3160 set_page_dirty(page);
4dd845b5
AP
3161 if (pmd_write(pmdval))
3162 entry = make_writable_migration_entry(page_to_pfn(page));
6c287605
DH
3163 else if (anon_exclusive)
3164 entry = make_readable_exclusive_migration_entry(page_to_pfn(page));
4dd845b5
AP
3165 else
3166 entry = make_readable_migration_entry(page_to_pfn(page));
ab6e3d09
NH
3167 pmdswp = swp_entry_to_pmd(entry);
3168 if (pmd_soft_dirty(pmdval))
3169 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3170 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
cea86fe2 3171 page_remove_rmap(page, vma, true);
616b8371 3172 put_page(page);
283fd6fe 3173 trace_set_migration_pmd(address, pmd_val(pmdswp));
7f5abe60
DH
3174
3175 return 0;
616b8371
ZY
3176}
3177
3178void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3179{
3180 struct vm_area_struct *vma = pvmw->vma;
3181 struct mm_struct *mm = vma->vm_mm;
3182 unsigned long address = pvmw->address;
3183 unsigned long mmun_start = address & HPAGE_PMD_MASK;
3184 pmd_t pmde;
3185 swp_entry_t entry;
3186
3187 if (!(pvmw->pmd && !pvmw->pte))
3188 return;
3189
3190 entry = pmd_to_swp_entry(*pvmw->pmd);
3191 get_page(new);
4286f147 3192 pmde = pmd_mkold(mk_huge_pmd(new, READ_ONCE(vma->vm_page_prot)));
ab6e3d09
NH
3193 if (pmd_swp_soft_dirty(*pvmw->pmd))
3194 pmde = pmd_mksoft_dirty(pmde);
4dd845b5 3195 if (is_writable_migration_entry(entry))
f55e1014 3196 pmde = maybe_pmd_mkwrite(pmde, vma);
8f34f1ea
PX
3197 if (pmd_swp_uffd_wp(*pvmw->pmd))
3198 pmde = pmd_wrprotect(pmd_mkuffd_wp(pmde));
616b8371 3199
6c287605
DH
3200 if (PageAnon(new)) {
3201 rmap_t rmap_flags = RMAP_COMPOUND;
3202
3203 if (!is_readable_migration_entry(entry))
3204 rmap_flags |= RMAP_EXCLUSIVE;
3205
3206 page_add_anon_rmap(new, vma, mmun_start, rmap_flags);
3207 } else {
cea86fe2 3208 page_add_file_rmap(new, vma, true);
6c287605
DH
3209 }
3210 VM_BUG_ON(pmd_write(pmde) && PageAnon(new) && !PageAnonExclusive(new));
616b8371 3211 set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
5cbcf225
MS
3212
3213 /* No need to invalidate - it was non-present before */
616b8371 3214 update_mmu_cache_pmd(vma, address, pvmw->pmd);
283fd6fe 3215 trace_remove_migration_pmd(address, pmd_val(pmde));
616b8371
ZY
3216}
3217#endif