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