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