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