This source file includes following definitions.
- transparent_hugepage_enabled
- get_huge_zero_page
- put_huge_zero_page
- mm_get_huge_zero_page
- mm_put_huge_zero_page
- shrink_huge_zero_page_count
- shrink_huge_zero_page_scan
- enabled_show
- enabled_store
- single_hugepage_flag_show
- single_hugepage_flag_store
- defrag_show
- defrag_store
- use_zero_page_show
- use_zero_page_store
- hpage_pmd_size_show
- debug_cow_show
- debug_cow_store
- hugepage_init_sysfs
- hugepage_exit_sysfs
- hugepage_init_sysfs
- hugepage_exit_sysfs
- hugepage_init
- setup_transparent_hugepage
- maybe_pmd_mkwrite
- get_deferred_split_queue
- get_deferred_split_queue
- prep_transhuge_page
- __thp_get_unmapped_area
- thp_get_unmapped_area
- __do_huge_pmd_anonymous_page
- alloc_hugepage_direct_gfpmask
- set_huge_zero_page
- do_huge_pmd_anonymous_page
- insert_pfn_pmd
- vmf_insert_pfn_pmd
- maybe_pud_mkwrite
- insert_pfn_pud
- vmf_insert_pfn_pud
- touch_pmd
- follow_devmap_pmd
- copy_huge_pmd
- touch_pud
- follow_devmap_pud
- copy_huge_pud
- huge_pud_set_accessed
- huge_pmd_set_accessed
- do_huge_pmd_wp_page_fallback
- do_huge_pmd_wp_page
- can_follow_write_pmd
- follow_trans_huge_pmd
- do_huge_pmd_numa_page
- madvise_free_huge_pmd
- zap_deposited_table
- zap_huge_pmd
- pmd_move_must_withdraw
- move_soft_dirty_pmd
- move_huge_pmd
- change_huge_pmd
- __pmd_trans_huge_lock
- __pud_trans_huge_lock
- zap_huge_pud
- __split_huge_pud_locked
- __split_huge_pud
- __split_huge_zero_page_pmd
- __split_huge_pmd_locked
- __split_huge_pmd
- split_huge_pmd_address
- vma_adjust_trans_huge
- unmap_page
- remap_page
- __split_huge_page_tail
- __split_huge_page
- total_mapcount
- page_trans_huge_mapcount
- can_split_huge_page
- split_huge_page_to_list
- free_transhuge_page
- deferred_split_huge_page
- deferred_split_count
- deferred_split_scan
- split_huge_pages_set
- split_huge_pages_debugfs
- set_pmd_migration_entry
- remove_migration_pmd
1
2
3
4
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/mm.h>
9 #include <linux/sched.h>
10 #include <linux/sched/coredump.h>
11 #include <linux/sched/numa_balancing.h>
12 #include <linux/highmem.h>
13 #include <linux/hugetlb.h>
14 #include <linux/mmu_notifier.h>
15 #include <linux/rmap.h>
16 #include <linux/swap.h>
17 #include <linux/shrinker.h>
18 #include <linux/mm_inline.h>
19 #include <linux/swapops.h>
20 #include <linux/dax.h>
21 #include <linux/khugepaged.h>
22 #include <linux/freezer.h>
23 #include <linux/pfn_t.h>
24 #include <linux/mman.h>
25 #include <linux/memremap.h>
26 #include <linux/pagemap.h>
27 #include <linux/debugfs.h>
28 #include <linux/migrate.h>
29 #include <linux/hashtable.h>
30 #include <linux/userfaultfd_k.h>
31 #include <linux/page_idle.h>
32 #include <linux/shmem_fs.h>
33 #include <linux/oom.h>
34 #include <linux/numa.h>
35 #include <linux/page_owner.h>
36
37 #include <asm/tlb.h>
38 #include <asm/pgalloc.h>
39 #include "internal.h"
40
41
42
43
44
45
46
47
48
49 unsigned long transparent_hugepage_flags __read_mostly =
50 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
51 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
52 #endif
53 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
54 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
55 #endif
56 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
57 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
58 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
59
60 static struct shrinker deferred_split_shrinker;
61
62 static atomic_t huge_zero_refcount;
63 struct page *huge_zero_page __read_mostly;
64
65 bool transparent_hugepage_enabled(struct vm_area_struct *vma)
66 {
67
68 unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
69
70 if (!transhuge_vma_suitable(vma, addr))
71 return false;
72 if (vma_is_anonymous(vma))
73 return __transparent_hugepage_enabled(vma);
74 if (vma_is_shmem(vma))
75 return shmem_huge_enabled(vma);
76
77 return false;
78 }
79
80 static struct page *get_huge_zero_page(void)
81 {
82 struct page *zero_page;
83 retry:
84 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
85 return READ_ONCE(huge_zero_page);
86
87 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
88 HPAGE_PMD_ORDER);
89 if (!zero_page) {
90 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
91 return NULL;
92 }
93 count_vm_event(THP_ZERO_PAGE_ALLOC);
94 preempt_disable();
95 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
96 preempt_enable();
97 __free_pages(zero_page, compound_order(zero_page));
98 goto retry;
99 }
100
101
102 atomic_set(&huge_zero_refcount, 2);
103 preempt_enable();
104 return READ_ONCE(huge_zero_page);
105 }
106
107 static void put_huge_zero_page(void)
108 {
109
110
111
112
113 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
114 }
115
116 struct page *mm_get_huge_zero_page(struct mm_struct *mm)
117 {
118 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
119 return READ_ONCE(huge_zero_page);
120
121 if (!get_huge_zero_page())
122 return NULL;
123
124 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
125 put_huge_zero_page();
126
127 return READ_ONCE(huge_zero_page);
128 }
129
130 void mm_put_huge_zero_page(struct mm_struct *mm)
131 {
132 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
133 put_huge_zero_page();
134 }
135
136 static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
137 struct shrink_control *sc)
138 {
139
140 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
141 }
142
143 static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
144 struct shrink_control *sc)
145 {
146 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
147 struct page *zero_page = xchg(&huge_zero_page, NULL);
148 BUG_ON(zero_page == NULL);
149 __free_pages(zero_page, compound_order(zero_page));
150 return HPAGE_PMD_NR;
151 }
152
153 return 0;
154 }
155
156 static struct shrinker huge_zero_page_shrinker = {
157 .count_objects = shrink_huge_zero_page_count,
158 .scan_objects = shrink_huge_zero_page_scan,
159 .seeks = DEFAULT_SEEKS,
160 };
161
162 #ifdef CONFIG_SYSFS
163 static ssize_t enabled_show(struct kobject *kobj,
164 struct kobj_attribute *attr, char *buf)
165 {
166 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
167 return sprintf(buf, "[always] madvise never\n");
168 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
169 return sprintf(buf, "always [madvise] never\n");
170 else
171 return sprintf(buf, "always madvise [never]\n");
172 }
173
174 static ssize_t enabled_store(struct kobject *kobj,
175 struct kobj_attribute *attr,
176 const char *buf, size_t count)
177 {
178 ssize_t ret = count;
179
180 if (sysfs_streq(buf, "always")) {
181 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
182 set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
183 } else if (sysfs_streq(buf, "madvise")) {
184 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
185 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
186 } else if (sysfs_streq(buf, "never")) {
187 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
188 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
189 } else
190 ret = -EINVAL;
191
192 if (ret > 0) {
193 int err = start_stop_khugepaged();
194 if (err)
195 ret = err;
196 }
197 return ret;
198 }
199 static struct kobj_attribute enabled_attr =
200 __ATTR(enabled, 0644, enabled_show, enabled_store);
201
202 ssize_t single_hugepage_flag_show(struct kobject *kobj,
203 struct kobj_attribute *attr, char *buf,
204 enum transparent_hugepage_flag flag)
205 {
206 return sprintf(buf, "%d\n",
207 !!test_bit(flag, &transparent_hugepage_flags));
208 }
209
210 ssize_t single_hugepage_flag_store(struct kobject *kobj,
211 struct kobj_attribute *attr,
212 const char *buf, size_t count,
213 enum transparent_hugepage_flag flag)
214 {
215 unsigned long value;
216 int ret;
217
218 ret = kstrtoul(buf, 10, &value);
219 if (ret < 0)
220 return ret;
221 if (value > 1)
222 return -EINVAL;
223
224 if (value)
225 set_bit(flag, &transparent_hugepage_flags);
226 else
227 clear_bit(flag, &transparent_hugepage_flags);
228
229 return count;
230 }
231
232 static ssize_t defrag_show(struct kobject *kobj,
233 struct kobj_attribute *attr, char *buf)
234 {
235 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
236 return sprintf(buf, "[always] defer defer+madvise madvise never\n");
237 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
238 return sprintf(buf, "always [defer] defer+madvise madvise never\n");
239 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
240 return sprintf(buf, "always defer [defer+madvise] madvise never\n");
241 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
242 return sprintf(buf, "always defer defer+madvise [madvise] never\n");
243 return sprintf(buf, "always defer defer+madvise madvise [never]\n");
244 }
245
246 static ssize_t defrag_store(struct kobject *kobj,
247 struct kobj_attribute *attr,
248 const char *buf, size_t count)
249 {
250 if (sysfs_streq(buf, "always")) {
251 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
252 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
253 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
254 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
255 } else if (sysfs_streq(buf, "defer+madvise")) {
256 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
257 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
258 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
259 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
260 } else if (sysfs_streq(buf, "defer")) {
261 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
262 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
263 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
264 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
265 } else if (sysfs_streq(buf, "madvise")) {
266 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
267 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
268 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
269 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
270 } else if (sysfs_streq(buf, "never")) {
271 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
272 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
273 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
274 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
275 } else
276 return -EINVAL;
277
278 return count;
279 }
280 static struct kobj_attribute defrag_attr =
281 __ATTR(defrag, 0644, defrag_show, defrag_store);
282
283 static ssize_t use_zero_page_show(struct kobject *kobj,
284 struct kobj_attribute *attr, char *buf)
285 {
286 return single_hugepage_flag_show(kobj, attr, buf,
287 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
288 }
289 static ssize_t use_zero_page_store(struct kobject *kobj,
290 struct kobj_attribute *attr, const char *buf, size_t count)
291 {
292 return single_hugepage_flag_store(kobj, attr, buf, count,
293 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
294 }
295 static struct kobj_attribute use_zero_page_attr =
296 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
297
298 static ssize_t hpage_pmd_size_show(struct kobject *kobj,
299 struct kobj_attribute *attr, char *buf)
300 {
301 return sprintf(buf, "%lu\n", HPAGE_PMD_SIZE);
302 }
303 static struct kobj_attribute hpage_pmd_size_attr =
304 __ATTR_RO(hpage_pmd_size);
305
306 #ifdef CONFIG_DEBUG_VM
307 static ssize_t debug_cow_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *buf)
309 {
310 return single_hugepage_flag_show(kobj, attr, buf,
311 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
312 }
313 static ssize_t debug_cow_store(struct kobject *kobj,
314 struct kobj_attribute *attr,
315 const char *buf, size_t count)
316 {
317 return single_hugepage_flag_store(kobj, attr, buf, count,
318 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
319 }
320 static struct kobj_attribute debug_cow_attr =
321 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
322 #endif
323
324 static struct attribute *hugepage_attr[] = {
325 &enabled_attr.attr,
326 &defrag_attr.attr,
327 &use_zero_page_attr.attr,
328 &hpage_pmd_size_attr.attr,
329 #if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
330 &shmem_enabled_attr.attr,
331 #endif
332 #ifdef CONFIG_DEBUG_VM
333 &debug_cow_attr.attr,
334 #endif
335 NULL,
336 };
337
338 static const struct attribute_group hugepage_attr_group = {
339 .attrs = hugepage_attr,
340 };
341
342 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
343 {
344 int err;
345
346 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
347 if (unlikely(!*hugepage_kobj)) {
348 pr_err("failed to create transparent hugepage kobject\n");
349 return -ENOMEM;
350 }
351
352 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
353 if (err) {
354 pr_err("failed to register transparent hugepage group\n");
355 goto delete_obj;
356 }
357
358 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
359 if (err) {
360 pr_err("failed to register transparent hugepage group\n");
361 goto remove_hp_group;
362 }
363
364 return 0;
365
366 remove_hp_group:
367 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
368 delete_obj:
369 kobject_put(*hugepage_kobj);
370 return err;
371 }
372
373 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
374 {
375 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
376 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
377 kobject_put(hugepage_kobj);
378 }
379 #else
380 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
381 {
382 return 0;
383 }
384
385 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
386 {
387 }
388 #endif
389
390 static int __init hugepage_init(void)
391 {
392 int err;
393 struct kobject *hugepage_kobj;
394
395 if (!has_transparent_hugepage()) {
396 transparent_hugepage_flags = 0;
397 return -EINVAL;
398 }
399
400
401
402
403 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
404
405
406
407
408 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
409
410 err = hugepage_init_sysfs(&hugepage_kobj);
411 if (err)
412 goto err_sysfs;
413
414 err = khugepaged_init();
415 if (err)
416 goto err_slab;
417
418 err = register_shrinker(&huge_zero_page_shrinker);
419 if (err)
420 goto err_hzp_shrinker;
421 err = register_shrinker(&deferred_split_shrinker);
422 if (err)
423 goto err_split_shrinker;
424
425
426
427
428
429
430 if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
431 transparent_hugepage_flags = 0;
432 return 0;
433 }
434
435 err = start_stop_khugepaged();
436 if (err)
437 goto err_khugepaged;
438
439 return 0;
440 err_khugepaged:
441 unregister_shrinker(&deferred_split_shrinker);
442 err_split_shrinker:
443 unregister_shrinker(&huge_zero_page_shrinker);
444 err_hzp_shrinker:
445 khugepaged_destroy();
446 err_slab:
447 hugepage_exit_sysfs(hugepage_kobj);
448 err_sysfs:
449 return err;
450 }
451 subsys_initcall(hugepage_init);
452
453 static int __init setup_transparent_hugepage(char *str)
454 {
455 int ret = 0;
456 if (!str)
457 goto out;
458 if (!strcmp(str, "always")) {
459 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
460 &transparent_hugepage_flags);
461 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
462 &transparent_hugepage_flags);
463 ret = 1;
464 } else if (!strcmp(str, "madvise")) {
465 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
466 &transparent_hugepage_flags);
467 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
468 &transparent_hugepage_flags);
469 ret = 1;
470 } else if (!strcmp(str, "never")) {
471 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
472 &transparent_hugepage_flags);
473 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
474 &transparent_hugepage_flags);
475 ret = 1;
476 }
477 out:
478 if (!ret)
479 pr_warn("transparent_hugepage= cannot parse, ignored\n");
480 return ret;
481 }
482 __setup("transparent_hugepage=", setup_transparent_hugepage);
483
484 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
485 {
486 if (likely(vma->vm_flags & VM_WRITE))
487 pmd = pmd_mkwrite(pmd);
488 return pmd;
489 }
490
491 #ifdef CONFIG_MEMCG
492 static inline struct deferred_split *get_deferred_split_queue(struct page *page)
493 {
494 struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
495 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
496
497 if (memcg)
498 return &memcg->deferred_split_queue;
499 else
500 return &pgdat->deferred_split_queue;
501 }
502 #else
503 static inline struct deferred_split *get_deferred_split_queue(struct page *page)
504 {
505 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
506
507 return &pgdat->deferred_split_queue;
508 }
509 #endif
510
511 void prep_transhuge_page(struct page *page)
512 {
513
514
515
516
517
518 INIT_LIST_HEAD(page_deferred_list(page));
519 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
520 }
521
522 static unsigned long __thp_get_unmapped_area(struct file *filp,
523 unsigned long addr, unsigned long len,
524 loff_t off, unsigned long flags, unsigned long size)
525 {
526 loff_t off_end = off + len;
527 loff_t off_align = round_up(off, size);
528 unsigned long len_pad, ret;
529
530 if (off_end <= off_align || (off_end - off_align) < size)
531 return 0;
532
533 len_pad = len + size;
534 if (len_pad < len || (off + len_pad) < off)
535 return 0;
536
537 ret = current->mm->get_unmapped_area(filp, addr, len_pad,
538 off >> PAGE_SHIFT, flags);
539
540
541
542
543
544 if (IS_ERR_VALUE(ret))
545 return 0;
546
547
548
549
550
551 if (ret == addr)
552 return addr;
553
554 ret += (off - ret) & (size - 1);
555 return ret;
556 }
557
558 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
559 unsigned long len, unsigned long pgoff, unsigned long flags)
560 {
561 unsigned long ret;
562 loff_t off = (loff_t)pgoff << PAGE_SHIFT;
563
564 if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
565 goto out;
566
567 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
568 if (ret)
569 return ret;
570 out:
571 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
572 }
573 EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
574
575 static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
576 struct page *page, gfp_t gfp)
577 {
578 struct vm_area_struct *vma = vmf->vma;
579 struct mem_cgroup *memcg;
580 pgtable_t pgtable;
581 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
582 vm_fault_t ret = 0;
583
584 VM_BUG_ON_PAGE(!PageCompound(page), page);
585
586 if (mem_cgroup_try_charge_delay(page, vma->vm_mm, gfp, &memcg, true)) {
587 put_page(page);
588 count_vm_event(THP_FAULT_FALLBACK);
589 return VM_FAULT_FALLBACK;
590 }
591
592 pgtable = pte_alloc_one(vma->vm_mm);
593 if (unlikely(!pgtable)) {
594 ret = VM_FAULT_OOM;
595 goto release;
596 }
597
598 clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
599
600
601
602
603
604 __SetPageUptodate(page);
605
606 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
607 if (unlikely(!pmd_none(*vmf->pmd))) {
608 goto unlock_release;
609 } else {
610 pmd_t entry;
611
612 ret = check_stable_address_space(vma->vm_mm);
613 if (ret)
614 goto unlock_release;
615
616
617 if (userfaultfd_missing(vma)) {
618 vm_fault_t ret2;
619
620 spin_unlock(vmf->ptl);
621 mem_cgroup_cancel_charge(page, memcg, true);
622 put_page(page);
623 pte_free(vma->vm_mm, pgtable);
624 ret2 = handle_userfault(vmf, VM_UFFD_MISSING);
625 VM_BUG_ON(ret2 & VM_FAULT_FALLBACK);
626 return ret2;
627 }
628
629 entry = mk_huge_pmd(page, vma->vm_page_prot);
630 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
631 page_add_new_anon_rmap(page, vma, haddr, true);
632 mem_cgroup_commit_charge(page, memcg, false, true);
633 lru_cache_add_active_or_unevictable(page, vma);
634 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
635 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
636 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
637 mm_inc_nr_ptes(vma->vm_mm);
638 spin_unlock(vmf->ptl);
639 count_vm_event(THP_FAULT_ALLOC);
640 count_memcg_events(memcg, THP_FAULT_ALLOC, 1);
641 }
642
643 return 0;
644 unlock_release:
645 spin_unlock(vmf->ptl);
646 release:
647 if (pgtable)
648 pte_free(vma->vm_mm, pgtable);
649 mem_cgroup_cancel_charge(page, memcg, true);
650 put_page(page);
651 return ret;
652
653 }
654
655
656
657
658
659
660
661
662
663
664 static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
665 {
666 const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
667
668
669 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
670 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
671
672
673 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
674 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
675
676
677 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
678 return GFP_TRANSHUGE_LIGHT |
679 (vma_madvised ? __GFP_DIRECT_RECLAIM :
680 __GFP_KSWAPD_RECLAIM);
681
682
683 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
684 return GFP_TRANSHUGE_LIGHT |
685 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
686
687 return GFP_TRANSHUGE_LIGHT;
688 }
689
690
691 static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
692 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
693 struct page *zero_page)
694 {
695 pmd_t entry;
696 if (!pmd_none(*pmd))
697 return false;
698 entry = mk_pmd(zero_page, vma->vm_page_prot);
699 entry = pmd_mkhuge(entry);
700 if (pgtable)
701 pgtable_trans_huge_deposit(mm, pmd, pgtable);
702 set_pmd_at(mm, haddr, pmd, entry);
703 mm_inc_nr_ptes(mm);
704 return true;
705 }
706
707 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
708 {
709 struct vm_area_struct *vma = vmf->vma;
710 gfp_t gfp;
711 struct page *page;
712 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
713
714 if (!transhuge_vma_suitable(vma, haddr))
715 return VM_FAULT_FALLBACK;
716 if (unlikely(anon_vma_prepare(vma)))
717 return VM_FAULT_OOM;
718 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
719 return VM_FAULT_OOM;
720 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
721 !mm_forbids_zeropage(vma->vm_mm) &&
722 transparent_hugepage_use_zero_page()) {
723 pgtable_t pgtable;
724 struct page *zero_page;
725 bool set;
726 vm_fault_t ret;
727 pgtable = pte_alloc_one(vma->vm_mm);
728 if (unlikely(!pgtable))
729 return VM_FAULT_OOM;
730 zero_page = mm_get_huge_zero_page(vma->vm_mm);
731 if (unlikely(!zero_page)) {
732 pte_free(vma->vm_mm, pgtable);
733 count_vm_event(THP_FAULT_FALLBACK);
734 return VM_FAULT_FALLBACK;
735 }
736 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
737 ret = 0;
738 set = false;
739 if (pmd_none(*vmf->pmd)) {
740 ret = check_stable_address_space(vma->vm_mm);
741 if (ret) {
742 spin_unlock(vmf->ptl);
743 } else if (userfaultfd_missing(vma)) {
744 spin_unlock(vmf->ptl);
745 ret = handle_userfault(vmf, VM_UFFD_MISSING);
746 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
747 } else {
748 set_huge_zero_page(pgtable, vma->vm_mm, vma,
749 haddr, vmf->pmd, zero_page);
750 spin_unlock(vmf->ptl);
751 set = true;
752 }
753 } else
754 spin_unlock(vmf->ptl);
755 if (!set)
756 pte_free(vma->vm_mm, pgtable);
757 return ret;
758 }
759 gfp = alloc_hugepage_direct_gfpmask(vma);
760 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
761 if (unlikely(!page)) {
762 count_vm_event(THP_FAULT_FALLBACK);
763 return VM_FAULT_FALLBACK;
764 }
765 prep_transhuge_page(page);
766 return __do_huge_pmd_anonymous_page(vmf, page, gfp);
767 }
768
769 static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
770 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
771 pgtable_t pgtable)
772 {
773 struct mm_struct *mm = vma->vm_mm;
774 pmd_t entry;
775 spinlock_t *ptl;
776
777 ptl = pmd_lock(mm, pmd);
778 if (!pmd_none(*pmd)) {
779 if (write) {
780 if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
781 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
782 goto out_unlock;
783 }
784 entry = pmd_mkyoung(*pmd);
785 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
786 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
787 update_mmu_cache_pmd(vma, addr, pmd);
788 }
789
790 goto out_unlock;
791 }
792
793 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
794 if (pfn_t_devmap(pfn))
795 entry = pmd_mkdevmap(entry);
796 if (write) {
797 entry = pmd_mkyoung(pmd_mkdirty(entry));
798 entry = maybe_pmd_mkwrite(entry, vma);
799 }
800
801 if (pgtable) {
802 pgtable_trans_huge_deposit(mm, pmd, pgtable);
803 mm_inc_nr_ptes(mm);
804 pgtable = NULL;
805 }
806
807 set_pmd_at(mm, addr, pmd, entry);
808 update_mmu_cache_pmd(vma, addr, pmd);
809
810 out_unlock:
811 spin_unlock(ptl);
812 if (pgtable)
813 pte_free(mm, pgtable);
814 }
815
816 vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)
817 {
818 unsigned long addr = vmf->address & PMD_MASK;
819 struct vm_area_struct *vma = vmf->vma;
820 pgprot_t pgprot = vma->vm_page_prot;
821 pgtable_t pgtable = NULL;
822
823
824
825
826
827
828 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
829 !pfn_t_devmap(pfn));
830 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
831 (VM_PFNMAP|VM_MIXEDMAP));
832 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
833
834 if (addr < vma->vm_start || addr >= vma->vm_end)
835 return VM_FAULT_SIGBUS;
836
837 if (arch_needs_pgtable_deposit()) {
838 pgtable = pte_alloc_one(vma->vm_mm);
839 if (!pgtable)
840 return VM_FAULT_OOM;
841 }
842
843 track_pfn_insert(vma, &pgprot, pfn);
844
845 insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
846 return VM_FAULT_NOPAGE;
847 }
848 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
849
850 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
851 static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
852 {
853 if (likely(vma->vm_flags & VM_WRITE))
854 pud = pud_mkwrite(pud);
855 return pud;
856 }
857
858 static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
859 pud_t *pud, pfn_t pfn, pgprot_t prot, bool write)
860 {
861 struct mm_struct *mm = vma->vm_mm;
862 pud_t entry;
863 spinlock_t *ptl;
864
865 ptl = pud_lock(mm, pud);
866 if (!pud_none(*pud)) {
867 if (write) {
868 if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
869 WARN_ON_ONCE(!is_huge_zero_pud(*pud));
870 goto out_unlock;
871 }
872 entry = pud_mkyoung(*pud);
873 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
874 if (pudp_set_access_flags(vma, addr, pud, entry, 1))
875 update_mmu_cache_pud(vma, addr, pud);
876 }
877 goto out_unlock;
878 }
879
880 entry = pud_mkhuge(pfn_t_pud(pfn, prot));
881 if (pfn_t_devmap(pfn))
882 entry = pud_mkdevmap(entry);
883 if (write) {
884 entry = pud_mkyoung(pud_mkdirty(entry));
885 entry = maybe_pud_mkwrite(entry, vma);
886 }
887 set_pud_at(mm, addr, pud, entry);
888 update_mmu_cache_pud(vma, addr, pud);
889
890 out_unlock:
891 spin_unlock(ptl);
892 }
893
894 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write)
895 {
896 unsigned long addr = vmf->address & PUD_MASK;
897 struct vm_area_struct *vma = vmf->vma;
898 pgprot_t pgprot = vma->vm_page_prot;
899
900
901
902
903
904
905 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
906 !pfn_t_devmap(pfn));
907 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
908 (VM_PFNMAP|VM_MIXEDMAP));
909 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
910
911 if (addr < vma->vm_start || addr >= vma->vm_end)
912 return VM_FAULT_SIGBUS;
913
914 track_pfn_insert(vma, &pgprot, pfn);
915
916 insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write);
917 return VM_FAULT_NOPAGE;
918 }
919 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud);
920 #endif
921
922 static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
923 pmd_t *pmd, int flags)
924 {
925 pmd_t _pmd;
926
927 _pmd = pmd_mkyoung(*pmd);
928 if (flags & FOLL_WRITE)
929 _pmd = pmd_mkdirty(_pmd);
930 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
931 pmd, _pmd, flags & FOLL_WRITE))
932 update_mmu_cache_pmd(vma, addr, pmd);
933 }
934
935 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
936 pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
937 {
938 unsigned long pfn = pmd_pfn(*pmd);
939 struct mm_struct *mm = vma->vm_mm;
940 struct page *page;
941
942 assert_spin_locked(pmd_lockptr(mm, pmd));
943
944
945
946
947
948 WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
949
950 if (flags & FOLL_WRITE && !pmd_write(*pmd))
951 return NULL;
952
953 if (pmd_present(*pmd) && pmd_devmap(*pmd))
954 ;
955 else
956 return NULL;
957
958 if (flags & FOLL_TOUCH)
959 touch_pmd(vma, addr, pmd, flags);
960
961
962
963
964
965 if (!(flags & FOLL_GET))
966 return ERR_PTR(-EEXIST);
967
968 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
969 *pgmap = get_dev_pagemap(pfn, *pgmap);
970 if (!*pgmap)
971 return ERR_PTR(-EFAULT);
972 page = pfn_to_page(pfn);
973 get_page(page);
974
975 return page;
976 }
977
978 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
979 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
980 struct vm_area_struct *vma)
981 {
982 spinlock_t *dst_ptl, *src_ptl;
983 struct page *src_page;
984 pmd_t pmd;
985 pgtable_t pgtable = NULL;
986 int ret = -ENOMEM;
987
988
989 if (!vma_is_anonymous(vma))
990 return 0;
991
992 pgtable = pte_alloc_one(dst_mm);
993 if (unlikely(!pgtable))
994 goto out;
995
996 dst_ptl = pmd_lock(dst_mm, dst_pmd);
997 src_ptl = pmd_lockptr(src_mm, src_pmd);
998 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
999
1000 ret = -EAGAIN;
1001 pmd = *src_pmd;
1002
1003 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1004 if (unlikely(is_swap_pmd(pmd))) {
1005 swp_entry_t entry = pmd_to_swp_entry(pmd);
1006
1007 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1008 if (is_write_migration_entry(entry)) {
1009 make_migration_entry_read(&entry);
1010 pmd = swp_entry_to_pmd(entry);
1011 if (pmd_swp_soft_dirty(*src_pmd))
1012 pmd = pmd_swp_mksoft_dirty(pmd);
1013 set_pmd_at(src_mm, addr, src_pmd, pmd);
1014 }
1015 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1016 mm_inc_nr_ptes(dst_mm);
1017 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1018 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1019 ret = 0;
1020 goto out_unlock;
1021 }
1022 #endif
1023
1024 if (unlikely(!pmd_trans_huge(pmd))) {
1025 pte_free(dst_mm, pgtable);
1026 goto out_unlock;
1027 }
1028
1029
1030
1031
1032
1033 if (is_huge_zero_pmd(pmd)) {
1034 struct page *zero_page;
1035
1036
1037
1038
1039
1040 zero_page = mm_get_huge_zero_page(dst_mm);
1041 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
1042 zero_page);
1043 ret = 0;
1044 goto out_unlock;
1045 }
1046
1047 src_page = pmd_page(pmd);
1048 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1049 get_page(src_page);
1050 page_dup_rmap(src_page, true);
1051 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1052 mm_inc_nr_ptes(dst_mm);
1053 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1054
1055 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1056 pmd = pmd_mkold(pmd_wrprotect(pmd));
1057 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1058
1059 ret = 0;
1060 out_unlock:
1061 spin_unlock(src_ptl);
1062 spin_unlock(dst_ptl);
1063 out:
1064 return ret;
1065 }
1066
1067 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1068 static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
1069 pud_t *pud, int flags)
1070 {
1071 pud_t _pud;
1072
1073 _pud = pud_mkyoung(*pud);
1074 if (flags & FOLL_WRITE)
1075 _pud = pud_mkdirty(_pud);
1076 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
1077 pud, _pud, flags & FOLL_WRITE))
1078 update_mmu_cache_pud(vma, addr, pud);
1079 }
1080
1081 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
1082 pud_t *pud, int flags, struct dev_pagemap **pgmap)
1083 {
1084 unsigned long pfn = pud_pfn(*pud);
1085 struct mm_struct *mm = vma->vm_mm;
1086 struct page *page;
1087
1088 assert_spin_locked(pud_lockptr(mm, pud));
1089
1090 if (flags & FOLL_WRITE && !pud_write(*pud))
1091 return NULL;
1092
1093 if (pud_present(*pud) && pud_devmap(*pud))
1094 ;
1095 else
1096 return NULL;
1097
1098 if (flags & FOLL_TOUCH)
1099 touch_pud(vma, addr, pud, flags);
1100
1101
1102
1103
1104
1105 if (!(flags & FOLL_GET))
1106 return ERR_PTR(-EEXIST);
1107
1108 pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
1109 *pgmap = get_dev_pagemap(pfn, *pgmap);
1110 if (!*pgmap)
1111 return ERR_PTR(-EFAULT);
1112 page = pfn_to_page(pfn);
1113 get_page(page);
1114
1115 return page;
1116 }
1117
1118 int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1119 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1120 struct vm_area_struct *vma)
1121 {
1122 spinlock_t *dst_ptl, *src_ptl;
1123 pud_t pud;
1124 int ret;
1125
1126 dst_ptl = pud_lock(dst_mm, dst_pud);
1127 src_ptl = pud_lockptr(src_mm, src_pud);
1128 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1129
1130 ret = -EAGAIN;
1131 pud = *src_pud;
1132 if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
1133 goto out_unlock;
1134
1135
1136
1137
1138
1139
1140 if (is_huge_zero_pud(pud)) {
1141
1142 }
1143
1144 pudp_set_wrprotect(src_mm, addr, src_pud);
1145 pud = pud_mkold(pud_wrprotect(pud));
1146 set_pud_at(dst_mm, addr, dst_pud, pud);
1147
1148 ret = 0;
1149 out_unlock:
1150 spin_unlock(src_ptl);
1151 spin_unlock(dst_ptl);
1152 return ret;
1153 }
1154
1155 void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
1156 {
1157 pud_t entry;
1158 unsigned long haddr;
1159 bool write = vmf->flags & FAULT_FLAG_WRITE;
1160
1161 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1162 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1163 goto unlock;
1164
1165 entry = pud_mkyoung(orig_pud);
1166 if (write)
1167 entry = pud_mkdirty(entry);
1168 haddr = vmf->address & HPAGE_PUD_MASK;
1169 if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write))
1170 update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud);
1171
1172 unlock:
1173 spin_unlock(vmf->ptl);
1174 }
1175 #endif
1176
1177 void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd)
1178 {
1179 pmd_t entry;
1180 unsigned long haddr;
1181 bool write = vmf->flags & FAULT_FLAG_WRITE;
1182
1183 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1184 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
1185 goto unlock;
1186
1187 entry = pmd_mkyoung(orig_pmd);
1188 if (write)
1189 entry = pmd_mkdirty(entry);
1190 haddr = vmf->address & HPAGE_PMD_MASK;
1191 if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write))
1192 update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd);
1193
1194 unlock:
1195 spin_unlock(vmf->ptl);
1196 }
1197
1198 static vm_fault_t do_huge_pmd_wp_page_fallback(struct vm_fault *vmf,
1199 pmd_t orig_pmd, struct page *page)
1200 {
1201 struct vm_area_struct *vma = vmf->vma;
1202 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
1203 struct mem_cgroup *memcg;
1204 pgtable_t pgtable;
1205 pmd_t _pmd;
1206 int i;
1207 vm_fault_t ret = 0;
1208 struct page **pages;
1209 struct mmu_notifier_range range;
1210
1211 pages = kmalloc_array(HPAGE_PMD_NR, sizeof(struct page *),
1212 GFP_KERNEL);
1213 if (unlikely(!pages)) {
1214 ret |= VM_FAULT_OOM;
1215 goto out;
1216 }
1217
1218 for (i = 0; i < HPAGE_PMD_NR; i++) {
1219 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE, vma,
1220 vmf->address, page_to_nid(page));
1221 if (unlikely(!pages[i] ||
1222 mem_cgroup_try_charge_delay(pages[i], vma->vm_mm,
1223 GFP_KERNEL, &memcg, false))) {
1224 if (pages[i])
1225 put_page(pages[i]);
1226 while (--i >= 0) {
1227 memcg = (void *)page_private(pages[i]);
1228 set_page_private(pages[i], 0);
1229 mem_cgroup_cancel_charge(pages[i], memcg,
1230 false);
1231 put_page(pages[i]);
1232 }
1233 kfree(pages);
1234 ret |= VM_FAULT_OOM;
1235 goto out;
1236 }
1237 set_page_private(pages[i], (unsigned long)memcg);
1238 }
1239
1240 for (i = 0; i < HPAGE_PMD_NR; i++) {
1241 copy_user_highpage(pages[i], page + i,
1242 haddr + PAGE_SIZE * i, vma);
1243 __SetPageUptodate(pages[i]);
1244 cond_resched();
1245 }
1246
1247 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1248 haddr, haddr + HPAGE_PMD_SIZE);
1249 mmu_notifier_invalidate_range_start(&range);
1250
1251 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1252 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
1253 goto out_free_pages;
1254 VM_BUG_ON_PAGE(!PageHead(page), page);
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264 pmdp_huge_clear_flush_notify(vma, haddr, vmf->pmd);
1265
1266 pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, vmf->pmd);
1267 pmd_populate(vma->vm_mm, &_pmd, pgtable);
1268
1269 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1270 pte_t entry;
1271 entry = mk_pte(pages[i], vma->vm_page_prot);
1272 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1273 memcg = (void *)page_private(pages[i]);
1274 set_page_private(pages[i], 0);
1275 page_add_new_anon_rmap(pages[i], vmf->vma, haddr, false);
1276 mem_cgroup_commit_charge(pages[i], memcg, false, false);
1277 lru_cache_add_active_or_unevictable(pages[i], vma);
1278 vmf->pte = pte_offset_map(&_pmd, haddr);
1279 VM_BUG_ON(!pte_none(*vmf->pte));
1280 set_pte_at(vma->vm_mm, haddr, vmf->pte, entry);
1281 pte_unmap(vmf->pte);
1282 }
1283 kfree(pages);
1284
1285 smp_wmb();
1286 pmd_populate(vma->vm_mm, vmf->pmd, pgtable);
1287 page_remove_rmap(page, true);
1288 spin_unlock(vmf->ptl);
1289
1290
1291
1292
1293
1294 mmu_notifier_invalidate_range_only_end(&range);
1295
1296 ret |= VM_FAULT_WRITE;
1297 put_page(page);
1298
1299 out:
1300 return ret;
1301
1302 out_free_pages:
1303 spin_unlock(vmf->ptl);
1304 mmu_notifier_invalidate_range_end(&range);
1305 for (i = 0; i < HPAGE_PMD_NR; i++) {
1306 memcg = (void *)page_private(pages[i]);
1307 set_page_private(pages[i], 0);
1308 mem_cgroup_cancel_charge(pages[i], memcg, false);
1309 put_page(pages[i]);
1310 }
1311 kfree(pages);
1312 goto out;
1313 }
1314
1315 vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)
1316 {
1317 struct vm_area_struct *vma = vmf->vma;
1318 struct page *page = NULL, *new_page;
1319 struct mem_cgroup *memcg;
1320 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
1321 struct mmu_notifier_range range;
1322 gfp_t huge_gfp;
1323 vm_fault_t ret = 0;
1324
1325 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
1326 VM_BUG_ON_VMA(!vma->anon_vma, vma);
1327 if (is_huge_zero_pmd(orig_pmd))
1328 goto alloc;
1329 spin_lock(vmf->ptl);
1330 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
1331 goto out_unlock;
1332
1333 page = pmd_page(orig_pmd);
1334 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
1335
1336
1337
1338
1339 if (!trylock_page(page)) {
1340 get_page(page);
1341 spin_unlock(vmf->ptl);
1342 lock_page(page);
1343 spin_lock(vmf->ptl);
1344 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1345 unlock_page(page);
1346 put_page(page);
1347 goto out_unlock;
1348 }
1349 put_page(page);
1350 }
1351 if (reuse_swap_page(page, NULL)) {
1352 pmd_t entry;
1353 entry = pmd_mkyoung(orig_pmd);
1354 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1355 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
1356 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1357 ret |= VM_FAULT_WRITE;
1358 unlock_page(page);
1359 goto out_unlock;
1360 }
1361 unlock_page(page);
1362 get_page(page);
1363 spin_unlock(vmf->ptl);
1364 alloc:
1365 if (__transparent_hugepage_enabled(vma) &&
1366 !transparent_hugepage_debug_cow()) {
1367 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
1368 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
1369 } else
1370 new_page = NULL;
1371
1372 if (likely(new_page)) {
1373 prep_transhuge_page(new_page);
1374 } else {
1375 if (!page) {
1376 split_huge_pmd(vma, vmf->pmd, vmf->address);
1377 ret |= VM_FAULT_FALLBACK;
1378 } else {
1379 ret = do_huge_pmd_wp_page_fallback(vmf, orig_pmd, page);
1380 if (ret & VM_FAULT_OOM) {
1381 split_huge_pmd(vma, vmf->pmd, vmf->address);
1382 ret |= VM_FAULT_FALLBACK;
1383 }
1384 put_page(page);
1385 }
1386 count_vm_event(THP_FAULT_FALLBACK);
1387 goto out;
1388 }
1389
1390 if (unlikely(mem_cgroup_try_charge_delay(new_page, vma->vm_mm,
1391 huge_gfp, &memcg, true))) {
1392 put_page(new_page);
1393 split_huge_pmd(vma, vmf->pmd, vmf->address);
1394 if (page)
1395 put_page(page);
1396 ret |= VM_FAULT_FALLBACK;
1397 count_vm_event(THP_FAULT_FALLBACK);
1398 goto out;
1399 }
1400
1401 count_vm_event(THP_FAULT_ALLOC);
1402 count_memcg_events(memcg, THP_FAULT_ALLOC, 1);
1403
1404 if (!page)
1405 clear_huge_page(new_page, vmf->address, HPAGE_PMD_NR);
1406 else
1407 copy_user_huge_page(new_page, page, vmf->address,
1408 vma, HPAGE_PMD_NR);
1409 __SetPageUptodate(new_page);
1410
1411 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1412 haddr, haddr + HPAGE_PMD_SIZE);
1413 mmu_notifier_invalidate_range_start(&range);
1414
1415 spin_lock(vmf->ptl);
1416 if (page)
1417 put_page(page);
1418 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1419 spin_unlock(vmf->ptl);
1420 mem_cgroup_cancel_charge(new_page, memcg, true);
1421 put_page(new_page);
1422 goto out_mn;
1423 } else {
1424 pmd_t entry;
1425 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1426 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1427 pmdp_huge_clear_flush_notify(vma, haddr, vmf->pmd);
1428 page_add_new_anon_rmap(new_page, vma, haddr, true);
1429 mem_cgroup_commit_charge(new_page, memcg, false, true);
1430 lru_cache_add_active_or_unevictable(new_page, vma);
1431 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
1432 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1433 if (!page) {
1434 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1435 } else {
1436 VM_BUG_ON_PAGE(!PageHead(page), page);
1437 page_remove_rmap(page, true);
1438 put_page(page);
1439 }
1440 ret |= VM_FAULT_WRITE;
1441 }
1442 spin_unlock(vmf->ptl);
1443 out_mn:
1444
1445
1446
1447
1448 mmu_notifier_invalidate_range_only_end(&range);
1449 out:
1450 return ret;
1451 out_unlock:
1452 spin_unlock(vmf->ptl);
1453 return ret;
1454 }
1455
1456
1457
1458
1459
1460 static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
1461 {
1462 return pmd_write(pmd) || ((flags & FOLL_COW) && pmd_dirty(pmd));
1463 }
1464
1465 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1466 unsigned long addr,
1467 pmd_t *pmd,
1468 unsigned int flags)
1469 {
1470 struct mm_struct *mm = vma->vm_mm;
1471 struct page *page = NULL;
1472
1473 assert_spin_locked(pmd_lockptr(mm, pmd));
1474
1475 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
1476 goto out;
1477
1478
1479 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1480 return ERR_PTR(-EFAULT);
1481
1482
1483 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
1484 goto out;
1485
1486 page = pmd_page(*pmd);
1487 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
1488 if (flags & FOLL_TOUCH)
1489 touch_pmd(vma, addr, pmd, flags);
1490 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 if (PageAnon(page) && compound_mapcount(page) != 1)
1513 goto skip_mlock;
1514 if (PageDoubleMap(page) || !page->mapping)
1515 goto skip_mlock;
1516 if (!trylock_page(page))
1517 goto skip_mlock;
1518 lru_add_drain();
1519 if (page->mapping && !PageDoubleMap(page))
1520 mlock_vma_page(page);
1521 unlock_page(page);
1522 }
1523 skip_mlock:
1524 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1525 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
1526 if (flags & FOLL_GET)
1527 get_page(page);
1528
1529 out:
1530 return page;
1531 }
1532
1533
1534 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)
1535 {
1536 struct vm_area_struct *vma = vmf->vma;
1537 struct anon_vma *anon_vma = NULL;
1538 struct page *page;
1539 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
1540 int page_nid = NUMA_NO_NODE, this_nid = numa_node_id();
1541 int target_nid, last_cpupid = -1;
1542 bool page_locked;
1543 bool migrated = false;
1544 bool was_writable;
1545 int flags = 0;
1546
1547 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1548 if (unlikely(!pmd_same(pmd, *vmf->pmd)))
1549 goto out_unlock;
1550
1551
1552
1553
1554
1555
1556 if (unlikely(pmd_trans_migrating(*vmf->pmd))) {
1557 page = pmd_page(*vmf->pmd);
1558 if (!get_page_unless_zero(page))
1559 goto out_unlock;
1560 spin_unlock(vmf->ptl);
1561 put_and_wait_on_page_locked(page);
1562 goto out;
1563 }
1564
1565 page = pmd_page(pmd);
1566 BUG_ON(is_huge_zero_page(page));
1567 page_nid = page_to_nid(page);
1568 last_cpupid = page_cpupid_last(page);
1569 count_vm_numa_event(NUMA_HINT_FAULTS);
1570 if (page_nid == this_nid) {
1571 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
1572 flags |= TNF_FAULT_LOCAL;
1573 }
1574
1575
1576 if (!pmd_savedwrite(pmd))
1577 flags |= TNF_NO_GROUP;
1578
1579
1580
1581
1582
1583 page_locked = trylock_page(page);
1584 target_nid = mpol_misplaced(page, vma, haddr);
1585 if (target_nid == NUMA_NO_NODE) {
1586
1587 if (page_locked)
1588 goto clear_pmdnuma;
1589 }
1590
1591
1592 if (!page_locked) {
1593 page_nid = NUMA_NO_NODE;
1594 if (!get_page_unless_zero(page))
1595 goto out_unlock;
1596 spin_unlock(vmf->ptl);
1597 put_and_wait_on_page_locked(page);
1598 goto out;
1599 }
1600
1601
1602
1603
1604
1605 get_page(page);
1606 spin_unlock(vmf->ptl);
1607 anon_vma = page_lock_anon_vma_read(page);
1608
1609
1610 spin_lock(vmf->ptl);
1611 if (unlikely(!pmd_same(pmd, *vmf->pmd))) {
1612 unlock_page(page);
1613 put_page(page);
1614 page_nid = NUMA_NO_NODE;
1615 goto out_unlock;
1616 }
1617
1618
1619 if (unlikely(!anon_vma)) {
1620 put_page(page);
1621 page_nid = NUMA_NO_NODE;
1622 goto clear_pmdnuma;
1623 }
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636 if (mm_tlb_flush_pending(vma->vm_mm)) {
1637 flush_tlb_range(vma, haddr, haddr + HPAGE_PMD_SIZE);
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647 mmu_notifier_invalidate_range(vma->vm_mm, haddr,
1648 haddr + HPAGE_PMD_SIZE);
1649 }
1650
1651
1652
1653
1654
1655 spin_unlock(vmf->ptl);
1656
1657 migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
1658 vmf->pmd, pmd, vmf->address, page, target_nid);
1659 if (migrated) {
1660 flags |= TNF_MIGRATED;
1661 page_nid = target_nid;
1662 } else
1663 flags |= TNF_MIGRATE_FAIL;
1664
1665 goto out;
1666 clear_pmdnuma:
1667 BUG_ON(!PageLocked(page));
1668 was_writable = pmd_savedwrite(pmd);
1669 pmd = pmd_modify(pmd, vma->vm_page_prot);
1670 pmd = pmd_mkyoung(pmd);
1671 if (was_writable)
1672 pmd = pmd_mkwrite(pmd);
1673 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1674 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1675 unlock_page(page);
1676 out_unlock:
1677 spin_unlock(vmf->ptl);
1678
1679 out:
1680 if (anon_vma)
1681 page_unlock_anon_vma_read(anon_vma);
1682
1683 if (page_nid != NUMA_NO_NODE)
1684 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
1685 flags);
1686
1687 return 0;
1688 }
1689
1690
1691
1692
1693
1694 bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1695 pmd_t *pmd, unsigned long addr, unsigned long next)
1696 {
1697 spinlock_t *ptl;
1698 pmd_t orig_pmd;
1699 struct page *page;
1700 struct mm_struct *mm = tlb->mm;
1701 bool ret = false;
1702
1703 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1704
1705 ptl = pmd_trans_huge_lock(pmd, vma);
1706 if (!ptl)
1707 goto out_unlocked;
1708
1709 orig_pmd = *pmd;
1710 if (is_huge_zero_pmd(orig_pmd))
1711 goto out;
1712
1713 if (unlikely(!pmd_present(orig_pmd))) {
1714 VM_BUG_ON(thp_migration_supported() &&
1715 !is_pmd_migration_entry(orig_pmd));
1716 goto out;
1717 }
1718
1719 page = pmd_page(orig_pmd);
1720
1721
1722
1723
1724 if (page_mapcount(page) != 1)
1725 goto out;
1726
1727 if (!trylock_page(page))
1728 goto out;
1729
1730
1731
1732
1733
1734 if (next - addr != HPAGE_PMD_SIZE) {
1735 get_page(page);
1736 spin_unlock(ptl);
1737 split_huge_page(page);
1738 unlock_page(page);
1739 put_page(page);
1740 goto out_unlocked;
1741 }
1742
1743 if (PageDirty(page))
1744 ClearPageDirty(page);
1745 unlock_page(page);
1746
1747 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1748 pmdp_invalidate(vma, addr, pmd);
1749 orig_pmd = pmd_mkold(orig_pmd);
1750 orig_pmd = pmd_mkclean(orig_pmd);
1751
1752 set_pmd_at(mm, addr, pmd, orig_pmd);
1753 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1754 }
1755
1756 mark_page_lazyfree(page);
1757 ret = true;
1758 out:
1759 spin_unlock(ptl);
1760 out_unlocked:
1761 return ret;
1762 }
1763
1764 static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1765 {
1766 pgtable_t pgtable;
1767
1768 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1769 pte_free(mm, pgtable);
1770 mm_dec_nr_ptes(mm);
1771 }
1772
1773 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1774 pmd_t *pmd, unsigned long addr)
1775 {
1776 pmd_t orig_pmd;
1777 spinlock_t *ptl;
1778
1779 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1780
1781 ptl = __pmd_trans_huge_lock(pmd, vma);
1782 if (!ptl)
1783 return 0;
1784
1785
1786
1787
1788
1789
1790 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1791 tlb->fullmm);
1792 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1793 if (vma_is_dax(vma)) {
1794 if (arch_needs_pgtable_deposit())
1795 zap_deposited_table(tlb->mm, pmd);
1796 spin_unlock(ptl);
1797 if (is_huge_zero_pmd(orig_pmd))
1798 tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
1799 } else if (is_huge_zero_pmd(orig_pmd)) {
1800 zap_deposited_table(tlb->mm, pmd);
1801 spin_unlock(ptl);
1802 tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
1803 } else {
1804 struct page *page = NULL;
1805 int flush_needed = 1;
1806
1807 if (pmd_present(orig_pmd)) {
1808 page = pmd_page(orig_pmd);
1809 page_remove_rmap(page, true);
1810 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1811 VM_BUG_ON_PAGE(!PageHead(page), page);
1812 } else if (thp_migration_supported()) {
1813 swp_entry_t entry;
1814
1815 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1816 entry = pmd_to_swp_entry(orig_pmd);
1817 page = pfn_to_page(swp_offset(entry));
1818 flush_needed = 0;
1819 } else
1820 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1821
1822 if (PageAnon(page)) {
1823 zap_deposited_table(tlb->mm, pmd);
1824 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1825 } else {
1826 if (arch_needs_pgtable_deposit())
1827 zap_deposited_table(tlb->mm, pmd);
1828 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
1829 }
1830
1831 spin_unlock(ptl);
1832 if (flush_needed)
1833 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
1834 }
1835 return 1;
1836 }
1837
1838 #ifndef pmd_move_must_withdraw
1839 static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1840 spinlock_t *old_pmd_ptl,
1841 struct vm_area_struct *vma)
1842 {
1843
1844
1845
1846
1847
1848
1849 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1850 }
1851 #endif
1852
1853 static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1854 {
1855 #ifdef CONFIG_MEM_SOFT_DIRTY
1856 if (unlikely(is_pmd_migration_entry(pmd)))
1857 pmd = pmd_swp_mksoft_dirty(pmd);
1858 else if (pmd_present(pmd))
1859 pmd = pmd_mksoft_dirty(pmd);
1860 #endif
1861 return pmd;
1862 }
1863
1864 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
1865 unsigned long new_addr, unsigned long old_end,
1866 pmd_t *old_pmd, pmd_t *new_pmd)
1867 {
1868 spinlock_t *old_ptl, *new_ptl;
1869 pmd_t pmd;
1870 struct mm_struct *mm = vma->vm_mm;
1871 bool force_flush = false;
1872
1873 if ((old_addr & ~HPAGE_PMD_MASK) ||
1874 (new_addr & ~HPAGE_PMD_MASK) ||
1875 old_end - old_addr < HPAGE_PMD_SIZE)
1876 return false;
1877
1878
1879
1880
1881
1882 if (WARN_ON(!pmd_none(*new_pmd))) {
1883 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1884 return false;
1885 }
1886
1887
1888
1889
1890
1891 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1892 if (old_ptl) {
1893 new_ptl = pmd_lockptr(mm, new_pmd);
1894 if (new_ptl != old_ptl)
1895 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
1896 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
1897 if (pmd_present(pmd))
1898 force_flush = true;
1899 VM_BUG_ON(!pmd_none(*new_pmd));
1900
1901 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
1902 pgtable_t pgtable;
1903 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1904 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
1905 }
1906 pmd = move_soft_dirty_pmd(pmd);
1907 set_pmd_at(mm, new_addr, new_pmd, pmd);
1908 if (force_flush)
1909 flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
1910 if (new_ptl != old_ptl)
1911 spin_unlock(new_ptl);
1912 spin_unlock(old_ptl);
1913 return true;
1914 }
1915 return false;
1916 }
1917
1918
1919
1920
1921
1922
1923
1924 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1925 unsigned long addr, pgprot_t newprot, int prot_numa)
1926 {
1927 struct mm_struct *mm = vma->vm_mm;
1928 spinlock_t *ptl;
1929 pmd_t entry;
1930 bool preserve_write;
1931 int ret;
1932
1933 ptl = __pmd_trans_huge_lock(pmd, vma);
1934 if (!ptl)
1935 return 0;
1936
1937 preserve_write = prot_numa && pmd_write(*pmd);
1938 ret = 1;
1939
1940 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1941 if (is_swap_pmd(*pmd)) {
1942 swp_entry_t entry = pmd_to_swp_entry(*pmd);
1943
1944 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
1945 if (is_write_migration_entry(entry)) {
1946 pmd_t newpmd;
1947
1948
1949
1950
1951 make_migration_entry_read(&entry);
1952 newpmd = swp_entry_to_pmd(entry);
1953 if (pmd_swp_soft_dirty(*pmd))
1954 newpmd = pmd_swp_mksoft_dirty(newpmd);
1955 set_pmd_at(mm, addr, pmd, newpmd);
1956 }
1957 goto unlock;
1958 }
1959 #endif
1960
1961
1962
1963
1964
1965
1966 if (prot_numa && is_huge_zero_pmd(*pmd))
1967 goto unlock;
1968
1969 if (prot_numa && pmd_protnone(*pmd))
1970 goto unlock;
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993 entry = pmdp_invalidate(vma, addr, pmd);
1994
1995 entry = pmd_modify(entry, newprot);
1996 if (preserve_write)
1997 entry = pmd_mk_savedwrite(entry);
1998 ret = HPAGE_PMD_NR;
1999 set_pmd_at(mm, addr, pmd, entry);
2000 BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
2001 unlock:
2002 spin_unlock(ptl);
2003 return ret;
2004 }
2005
2006
2007
2008
2009
2010
2011
2012 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
2013 {
2014 spinlock_t *ptl;
2015 ptl = pmd_lock(vma->vm_mm, pmd);
2016 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
2017 pmd_devmap(*pmd)))
2018 return ptl;
2019 spin_unlock(ptl);
2020 return NULL;
2021 }
2022
2023
2024
2025
2026
2027
2028
2029 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
2030 {
2031 spinlock_t *ptl;
2032
2033 ptl = pud_lock(vma->vm_mm, pud);
2034 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
2035 return ptl;
2036 spin_unlock(ptl);
2037 return NULL;
2038 }
2039
2040 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
2041 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
2042 pud_t *pud, unsigned long addr)
2043 {
2044 spinlock_t *ptl;
2045
2046 ptl = __pud_trans_huge_lock(pud, vma);
2047 if (!ptl)
2048 return 0;
2049
2050
2051
2052
2053
2054
2055 pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
2056 tlb_remove_pud_tlb_entry(tlb, pud, addr);
2057 if (vma_is_dax(vma)) {
2058 spin_unlock(ptl);
2059
2060 } else {
2061
2062 BUG();
2063 }
2064 return 1;
2065 }
2066
2067 static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
2068 unsigned long haddr)
2069 {
2070 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
2071 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2072 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
2073 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
2074
2075 count_vm_event(THP_SPLIT_PUD);
2076
2077 pudp_huge_clear_flush_notify(vma, haddr, pud);
2078 }
2079
2080 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
2081 unsigned long address)
2082 {
2083 spinlock_t *ptl;
2084 struct mmu_notifier_range range;
2085
2086 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
2087 address & HPAGE_PUD_MASK,
2088 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
2089 mmu_notifier_invalidate_range_start(&range);
2090 ptl = pud_lock(vma->vm_mm, pud);
2091 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
2092 goto out;
2093 __split_huge_pud_locked(vma, pud, range.start);
2094
2095 out:
2096 spin_unlock(ptl);
2097
2098
2099
2100
2101 mmu_notifier_invalidate_range_only_end(&range);
2102 }
2103 #endif
2104
2105 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2106 unsigned long haddr, pmd_t *pmd)
2107 {
2108 struct mm_struct *mm = vma->vm_mm;
2109 pgtable_t pgtable;
2110 pmd_t _pmd;
2111 int i;
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121 pmdp_huge_clear_flush(vma, haddr, pmd);
2122
2123 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2124 pmd_populate(mm, &_pmd, pgtable);
2125
2126 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2127 pte_t *pte, entry;
2128 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2129 entry = pte_mkspecial(entry);
2130 pte = pte_offset_map(&_pmd, haddr);
2131 VM_BUG_ON(!pte_none(*pte));
2132 set_pte_at(mm, haddr, pte, entry);
2133 pte_unmap(pte);
2134 }
2135 smp_wmb();
2136 pmd_populate(mm, pmd, pgtable);
2137 }
2138
2139 static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
2140 unsigned long haddr, bool freeze)
2141 {
2142 struct mm_struct *mm = vma->vm_mm;
2143 struct page *page;
2144 pgtable_t pgtable;
2145 pmd_t old_pmd, _pmd;
2146 bool young, write, soft_dirty, pmd_migration = false;
2147 unsigned long addr;
2148 int i;
2149
2150 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2151 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2152 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
2153 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
2154 && !pmd_devmap(*pmd));
2155
2156 count_vm_event(THP_SPLIT_PMD);
2157
2158 if (!vma_is_anonymous(vma)) {
2159 _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2160
2161
2162
2163
2164 if (arch_needs_pgtable_deposit())
2165 zap_deposited_table(mm, pmd);
2166 if (vma_is_dax(vma))
2167 return;
2168 page = pmd_page(_pmd);
2169 if (!PageDirty(page) && pmd_dirty(_pmd))
2170 set_page_dirty(page);
2171 if (!PageReferenced(page) && pmd_young(_pmd))
2172 SetPageReferenced(page);
2173 page_remove_rmap(page, true);
2174 put_page(page);
2175 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
2176 return;
2177 } else if (is_huge_zero_pmd(*pmd)) {
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2188 }
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2211
2212 pmd_migration = is_pmd_migration_entry(old_pmd);
2213 if (unlikely(pmd_migration)) {
2214 swp_entry_t entry;
2215
2216 entry = pmd_to_swp_entry(old_pmd);
2217 page = pfn_to_page(swp_offset(entry));
2218 write = is_write_migration_entry(entry);
2219 young = false;
2220 soft_dirty = pmd_swp_soft_dirty(old_pmd);
2221 } else {
2222 page = pmd_page(old_pmd);
2223 if (pmd_dirty(old_pmd))
2224 SetPageDirty(page);
2225 write = pmd_write(old_pmd);
2226 young = pmd_young(old_pmd);
2227 soft_dirty = pmd_soft_dirty(old_pmd);
2228 }
2229 VM_BUG_ON_PAGE(!page_count(page), page);
2230 page_ref_add(page, HPAGE_PMD_NR - 1);
2231
2232
2233
2234
2235
2236 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2237 pmd_populate(mm, &_pmd, pgtable);
2238
2239 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
2240 pte_t entry, *pte;
2241
2242
2243
2244
2245
2246 if (freeze || pmd_migration) {
2247 swp_entry_t swp_entry;
2248 swp_entry = make_migration_entry(page + i, write);
2249 entry = swp_entry_to_pte(swp_entry);
2250 if (soft_dirty)
2251 entry = pte_swp_mksoft_dirty(entry);
2252 } else {
2253 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
2254 entry = maybe_mkwrite(entry, vma);
2255 if (!write)
2256 entry = pte_wrprotect(entry);
2257 if (!young)
2258 entry = pte_mkold(entry);
2259 if (soft_dirty)
2260 entry = pte_mksoft_dirty(entry);
2261 }
2262 pte = pte_offset_map(&_pmd, addr);
2263 BUG_ON(!pte_none(*pte));
2264 set_pte_at(mm, addr, pte, entry);
2265 atomic_inc(&page[i]._mapcount);
2266 pte_unmap(pte);
2267 }
2268
2269
2270
2271
2272
2273 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2274 for (i = 0; i < HPAGE_PMD_NR; i++)
2275 atomic_inc(&page[i]._mapcount);
2276 }
2277
2278 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2279
2280 __dec_node_page_state(page, NR_ANON_THPS);
2281 if (TestClearPageDoubleMap(page)) {
2282
2283 for (i = 0; i < HPAGE_PMD_NR; i++)
2284 atomic_dec(&page[i]._mapcount);
2285 }
2286 }
2287
2288 smp_wmb();
2289 pmd_populate(mm, pmd, pgtable);
2290
2291 if (freeze) {
2292 for (i = 0; i < HPAGE_PMD_NR; i++) {
2293 page_remove_rmap(page + i, false);
2294 put_page(page + i);
2295 }
2296 }
2297 }
2298
2299 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
2300 unsigned long address, bool freeze, struct page *page)
2301 {
2302 spinlock_t *ptl;
2303 struct mmu_notifier_range range;
2304
2305 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
2306 address & HPAGE_PMD_MASK,
2307 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2308 mmu_notifier_invalidate_range_start(&range);
2309 ptl = pmd_lock(vma->vm_mm, pmd);
2310
2311
2312
2313
2314
2315 VM_BUG_ON(freeze && !page);
2316 if (page && page != pmd_page(*pmd))
2317 goto out;
2318
2319 if (pmd_trans_huge(*pmd)) {
2320 page = pmd_page(*pmd);
2321 if (PageMlocked(page))
2322 clear_page_mlock(page);
2323 } else if (!(pmd_devmap(*pmd) || is_pmd_migration_entry(*pmd)))
2324 goto out;
2325 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
2326 out:
2327 spin_unlock(ptl);
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341 mmu_notifier_invalidate_range_only_end(&range);
2342 }
2343
2344 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
2345 bool freeze, struct page *page)
2346 {
2347 pgd_t *pgd;
2348 p4d_t *p4d;
2349 pud_t *pud;
2350 pmd_t *pmd;
2351
2352 pgd = pgd_offset(vma->vm_mm, address);
2353 if (!pgd_present(*pgd))
2354 return;
2355
2356 p4d = p4d_offset(pgd, address);
2357 if (!p4d_present(*p4d))
2358 return;
2359
2360 pud = pud_offset(p4d, address);
2361 if (!pud_present(*pud))
2362 return;
2363
2364 pmd = pmd_offset(pud, address);
2365
2366 __split_huge_pmd(vma, pmd, address, freeze, page);
2367 }
2368
2369 void vma_adjust_trans_huge(struct vm_area_struct *vma,
2370 unsigned long start,
2371 unsigned long end,
2372 long adjust_next)
2373 {
2374
2375
2376
2377
2378
2379 if (start & ~HPAGE_PMD_MASK &&
2380 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2381 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2382 split_huge_pmd_address(vma, start, false, NULL);
2383
2384
2385
2386
2387
2388
2389 if (end & ~HPAGE_PMD_MASK &&
2390 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2391 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2392 split_huge_pmd_address(vma, end, false, NULL);
2393
2394
2395
2396
2397
2398
2399 if (adjust_next > 0) {
2400 struct vm_area_struct *next = vma->vm_next;
2401 unsigned long nstart = next->vm_start;
2402 nstart += adjust_next << PAGE_SHIFT;
2403 if (nstart & ~HPAGE_PMD_MASK &&
2404 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2405 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2406 split_huge_pmd_address(next, nstart, false, NULL);
2407 }
2408 }
2409
2410 static void unmap_page(struct page *page)
2411 {
2412 enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS |
2413 TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD;
2414 bool unmap_success;
2415
2416 VM_BUG_ON_PAGE(!PageHead(page), page);
2417
2418 if (PageAnon(page))
2419 ttu_flags |= TTU_SPLIT_FREEZE;
2420
2421 unmap_success = try_to_unmap(page, ttu_flags);
2422 VM_BUG_ON_PAGE(!unmap_success, page);
2423 }
2424
2425 static void remap_page(struct page *page)
2426 {
2427 int i;
2428 if (PageTransHuge(page)) {
2429 remove_migration_ptes(page, page, true);
2430 } else {
2431 for (i = 0; i < HPAGE_PMD_NR; i++)
2432 remove_migration_ptes(page + i, page + i, true);
2433 }
2434 }
2435
2436 static void __split_huge_page_tail(struct page *head, int tail,
2437 struct lruvec *lruvec, struct list_head *list)
2438 {
2439 struct page *page_tail = head + tail;
2440
2441 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
2442
2443
2444
2445
2446
2447
2448
2449 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2450 page_tail->flags |= (head->flags &
2451 ((1L << PG_referenced) |
2452 (1L << PG_swapbacked) |
2453 (1L << PG_swapcache) |
2454 (1L << PG_mlocked) |
2455 (1L << PG_uptodate) |
2456 (1L << PG_active) |
2457 (1L << PG_workingset) |
2458 (1L << PG_locked) |
2459 (1L << PG_unevictable) |
2460 (1L << PG_dirty)));
2461
2462
2463 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2464 page_tail);
2465 page_tail->mapping = head->mapping;
2466 page_tail->index = head->index + tail;
2467
2468
2469 smp_wmb();
2470
2471
2472
2473
2474
2475
2476
2477 clear_compound_head(page_tail);
2478
2479
2480 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2481 PageSwapCache(head)));
2482
2483 if (page_is_young(head))
2484 set_page_young(page_tail);
2485 if (page_is_idle(head))
2486 set_page_idle(page_tail);
2487
2488 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
2489
2490
2491
2492
2493
2494
2495 lru_add_page_tail(head, page_tail, lruvec, list);
2496 }
2497
2498 static void __split_huge_page(struct page *page, struct list_head *list,
2499 pgoff_t end, unsigned long flags)
2500 {
2501 struct page *head = compound_head(page);
2502 pg_data_t *pgdat = page_pgdat(head);
2503 struct lruvec *lruvec;
2504 struct address_space *swap_cache = NULL;
2505 unsigned long offset = 0;
2506 int i;
2507
2508 lruvec = mem_cgroup_page_lruvec(head, pgdat);
2509
2510
2511 mem_cgroup_split_huge_fixup(head);
2512
2513 if (PageAnon(head) && PageSwapCache(head)) {
2514 swp_entry_t entry = { .val = page_private(head) };
2515
2516 offset = swp_offset(entry);
2517 swap_cache = swap_address_space(entry);
2518 xa_lock(&swap_cache->i_pages);
2519 }
2520
2521 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
2522 __split_huge_page_tail(head, i, lruvec, list);
2523
2524 if (head[i].index >= end) {
2525 ClearPageDirty(head + i);
2526 __delete_from_page_cache(head + i, NULL);
2527 if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
2528 shmem_uncharge(head->mapping->host, 1);
2529 put_page(head + i);
2530 } else if (!PageAnon(page)) {
2531 __xa_store(&head->mapping->i_pages, head[i].index,
2532 head + i, 0);
2533 } else if (swap_cache) {
2534 __xa_store(&swap_cache->i_pages, offset + i,
2535 head + i, 0);
2536 }
2537 }
2538
2539 ClearPageCompound(head);
2540
2541 split_page_owner(head, HPAGE_PMD_ORDER);
2542
2543
2544 if (PageAnon(head)) {
2545
2546 if (PageSwapCache(head)) {
2547 page_ref_add(head, 2);
2548 xa_unlock(&swap_cache->i_pages);
2549 } else {
2550 page_ref_inc(head);
2551 }
2552 } else {
2553
2554 page_ref_add(head, 2);
2555 xa_unlock(&head->mapping->i_pages);
2556 }
2557
2558 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
2559
2560 remap_page(head);
2561
2562 for (i = 0; i < HPAGE_PMD_NR; i++) {
2563 struct page *subpage = head + i;
2564 if (subpage == page)
2565 continue;
2566 unlock_page(subpage);
2567
2568
2569
2570
2571
2572
2573
2574
2575 put_page(subpage);
2576 }
2577 }
2578
2579 int total_mapcount(struct page *page)
2580 {
2581 int i, compound, ret;
2582
2583 VM_BUG_ON_PAGE(PageTail(page), page);
2584
2585 if (likely(!PageCompound(page)))
2586 return atomic_read(&page->_mapcount) + 1;
2587
2588 compound = compound_mapcount(page);
2589 if (PageHuge(page))
2590 return compound;
2591 ret = compound;
2592 for (i = 0; i < HPAGE_PMD_NR; i++)
2593 ret += atomic_read(&page[i]._mapcount) + 1;
2594
2595 if (!PageAnon(page))
2596 return ret - compound * HPAGE_PMD_NR;
2597 if (PageDoubleMap(page))
2598 ret -= HPAGE_PMD_NR;
2599 return ret;
2600 }
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626 int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
2627 {
2628 int i, ret, _total_mapcount, mapcount;
2629
2630
2631 VM_BUG_ON_PAGE(PageHuge(page), page);
2632
2633 if (likely(!PageTransCompound(page))) {
2634 mapcount = atomic_read(&page->_mapcount) + 1;
2635 if (total_mapcount)
2636 *total_mapcount = mapcount;
2637 return mapcount;
2638 }
2639
2640 page = compound_head(page);
2641
2642 _total_mapcount = ret = 0;
2643 for (i = 0; i < HPAGE_PMD_NR; i++) {
2644 mapcount = atomic_read(&page[i]._mapcount) + 1;
2645 ret = max(ret, mapcount);
2646 _total_mapcount += mapcount;
2647 }
2648 if (PageDoubleMap(page)) {
2649 ret -= 1;
2650 _total_mapcount -= HPAGE_PMD_NR;
2651 }
2652 mapcount = compound_mapcount(page);
2653 ret += mapcount;
2654 _total_mapcount += mapcount;
2655 if (total_mapcount)
2656 *total_mapcount = _total_mapcount;
2657 return ret;
2658 }
2659
2660
2661 bool can_split_huge_page(struct page *page, int *pextra_pins)
2662 {
2663 int extra_pins;
2664
2665
2666 if (PageAnon(page))
2667 extra_pins = PageSwapCache(page) ? HPAGE_PMD_NR : 0;
2668 else
2669 extra_pins = HPAGE_PMD_NR;
2670 if (pextra_pins)
2671 *pextra_pins = extra_pins;
2672 return total_mapcount(page) == page_count(page) - extra_pins - 1;
2673 }
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694 int split_huge_page_to_list(struct page *page, struct list_head *list)
2695 {
2696 struct page *head = compound_head(page);
2697 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
2698 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2699 struct anon_vma *anon_vma = NULL;
2700 struct address_space *mapping = NULL;
2701 int count, mapcount, extra_pins, ret;
2702 bool mlocked;
2703 unsigned long flags;
2704 pgoff_t end;
2705
2706 VM_BUG_ON_PAGE(is_huge_zero_page(head), head);
2707 VM_BUG_ON_PAGE(!PageLocked(page), page);
2708 VM_BUG_ON_PAGE(!PageCompound(page), page);
2709
2710 if (PageWriteback(page))
2711 return -EBUSY;
2712
2713 if (PageAnon(head)) {
2714
2715
2716
2717
2718
2719
2720
2721
2722 anon_vma = page_get_anon_vma(head);
2723 if (!anon_vma) {
2724 ret = -EBUSY;
2725 goto out;
2726 }
2727 end = -1;
2728 mapping = NULL;
2729 anon_vma_lock_write(anon_vma);
2730 } else {
2731 mapping = head->mapping;
2732
2733
2734 if (!mapping) {
2735 ret = -EBUSY;
2736 goto out;
2737 }
2738
2739 anon_vma = NULL;
2740 i_mmap_lock_read(mapping);
2741
2742
2743
2744
2745
2746
2747
2748
2749 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
2750 }
2751
2752
2753
2754
2755
2756 if (!can_split_huge_page(head, &extra_pins)) {
2757 ret = -EBUSY;
2758 goto out_unlock;
2759 }
2760
2761 mlocked = PageMlocked(page);
2762 unmap_page(head);
2763 VM_BUG_ON_PAGE(compound_mapcount(head), head);
2764
2765
2766 if (mlocked)
2767 lru_add_drain();
2768
2769
2770 spin_lock_irqsave(&pgdata->lru_lock, flags);
2771
2772 if (mapping) {
2773 XA_STATE(xas, &mapping->i_pages, page_index(head));
2774
2775
2776
2777
2778
2779 xa_lock(&mapping->i_pages);
2780 if (xas_load(&xas) != head)
2781 goto fail;
2782 }
2783
2784
2785 spin_lock(&ds_queue->split_queue_lock);
2786 count = page_count(head);
2787 mapcount = total_mapcount(head);
2788 if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
2789 if (!list_empty(page_deferred_list(head))) {
2790 ds_queue->split_queue_len--;
2791 list_del(page_deferred_list(head));
2792 }
2793 if (mapping) {
2794 if (PageSwapBacked(page))
2795 __dec_node_page_state(page, NR_SHMEM_THPS);
2796 else
2797 __dec_node_page_state(page, NR_FILE_THPS);
2798 }
2799
2800 spin_unlock(&ds_queue->split_queue_lock);
2801 __split_huge_page(page, list, end, flags);
2802 if (PageSwapCache(head)) {
2803 swp_entry_t entry = { .val = page_private(head) };
2804
2805 ret = split_swap_cluster(entry);
2806 } else
2807 ret = 0;
2808 } else {
2809 if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
2810 pr_alert("total_mapcount: %u, page_count(): %u\n",
2811 mapcount, count);
2812 if (PageTail(page))
2813 dump_page(head, NULL);
2814 dump_page(page, "total_mapcount(head) > 0");
2815 BUG();
2816 }
2817 spin_unlock(&ds_queue->split_queue_lock);
2818 fail: if (mapping)
2819 xa_unlock(&mapping->i_pages);
2820 spin_unlock_irqrestore(&pgdata->lru_lock, flags);
2821 remap_page(head);
2822 ret = -EBUSY;
2823 }
2824
2825 out_unlock:
2826 if (anon_vma) {
2827 anon_vma_unlock_write(anon_vma);
2828 put_anon_vma(anon_vma);
2829 }
2830 if (mapping)
2831 i_mmap_unlock_read(mapping);
2832 out:
2833 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2834 return ret;
2835 }
2836
2837 void free_transhuge_page(struct page *page)
2838 {
2839 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2840 unsigned long flags;
2841
2842 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2843 if (!list_empty(page_deferred_list(page))) {
2844 ds_queue->split_queue_len--;
2845 list_del(page_deferred_list(page));
2846 }
2847 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2848 free_compound_page(page);
2849 }
2850
2851 void deferred_split_huge_page(struct page *page)
2852 {
2853 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2854 #ifdef CONFIG_MEMCG
2855 struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
2856 #endif
2857 unsigned long flags;
2858
2859 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871 if (PageSwapCache(page))
2872 return;
2873
2874 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2875 if (list_empty(page_deferred_list(page))) {
2876 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
2877 list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2878 ds_queue->split_queue_len++;
2879 #ifdef CONFIG_MEMCG
2880 if (memcg)
2881 memcg_set_shrinker_bit(memcg, page_to_nid(page),
2882 deferred_split_shrinker.id);
2883 #endif
2884 }
2885 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2886 }
2887
2888 static unsigned long deferred_split_count(struct shrinker *shrink,
2889 struct shrink_control *sc)
2890 {
2891 struct pglist_data *pgdata = NODE_DATA(sc->nid);
2892 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
2893
2894 #ifdef CONFIG_MEMCG
2895 if (sc->memcg)
2896 ds_queue = &sc->memcg->deferred_split_queue;
2897 #endif
2898 return READ_ONCE(ds_queue->split_queue_len);
2899 }
2900
2901 static unsigned long deferred_split_scan(struct shrinker *shrink,
2902 struct shrink_control *sc)
2903 {
2904 struct pglist_data *pgdata = NODE_DATA(sc->nid);
2905 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
2906 unsigned long flags;
2907 LIST_HEAD(list), *pos, *next;
2908 struct page *page;
2909 int split = 0;
2910
2911 #ifdef CONFIG_MEMCG
2912 if (sc->memcg)
2913 ds_queue = &sc->memcg->deferred_split_queue;
2914 #endif
2915
2916 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2917
2918 list_for_each_safe(pos, next, &ds_queue->split_queue) {
2919 page = list_entry((void *)pos, struct page, mapping);
2920 page = compound_head(page);
2921 if (get_page_unless_zero(page)) {
2922 list_move(page_deferred_list(page), &list);
2923 } else {
2924
2925 list_del_init(page_deferred_list(page));
2926 ds_queue->split_queue_len--;
2927 }
2928 if (!--sc->nr_to_scan)
2929 break;
2930 }
2931 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2932
2933 list_for_each_safe(pos, next, &list) {
2934 page = list_entry((void *)pos, struct page, mapping);
2935 if (!trylock_page(page))
2936 goto next;
2937
2938 if (!split_huge_page(page))
2939 split++;
2940 unlock_page(page);
2941 next:
2942 put_page(page);
2943 }
2944
2945 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2946 list_splice_tail(&list, &ds_queue->split_queue);
2947 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
2948
2949
2950
2951
2952
2953 if (!split && list_empty(&ds_queue->split_queue))
2954 return SHRINK_STOP;
2955 return split;
2956 }
2957
2958 static struct shrinker deferred_split_shrinker = {
2959 .count_objects = deferred_split_count,
2960 .scan_objects = deferred_split_scan,
2961 .seeks = DEFAULT_SEEKS,
2962 .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
2963 SHRINKER_NONSLAB,
2964 };
2965
2966 #ifdef CONFIG_DEBUG_FS
2967 static int split_huge_pages_set(void *data, u64 val)
2968 {
2969 struct zone *zone;
2970 struct page *page;
2971 unsigned long pfn, max_zone_pfn;
2972 unsigned long total = 0, split = 0;
2973
2974 if (val != 1)
2975 return -EINVAL;
2976
2977 for_each_populated_zone(zone) {
2978 max_zone_pfn = zone_end_pfn(zone);
2979 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
2980 if (!pfn_valid(pfn))
2981 continue;
2982
2983 page = pfn_to_page(pfn);
2984 if (!get_page_unless_zero(page))
2985 continue;
2986
2987 if (zone != page_zone(page))
2988 goto next;
2989
2990 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
2991 goto next;
2992
2993 total++;
2994 lock_page(page);
2995 if (!split_huge_page(page))
2996 split++;
2997 unlock_page(page);
2998 next:
2999 put_page(page);
3000 }
3001 }
3002
3003 pr_info("%lu of %lu THP split\n", split, total);
3004
3005 return 0;
3006 }
3007 DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3008 "%llu\n");
3009
3010 static int __init split_huge_pages_debugfs(void)
3011 {
3012 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3013 &split_huge_pages_fops);
3014 return 0;
3015 }
3016 late_initcall(split_huge_pages_debugfs);
3017 #endif
3018
3019 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
3020 void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
3021 struct page *page)
3022 {
3023 struct vm_area_struct *vma = pvmw->vma;
3024 struct mm_struct *mm = vma->vm_mm;
3025 unsigned long address = pvmw->address;
3026 pmd_t pmdval;
3027 swp_entry_t entry;
3028 pmd_t pmdswp;
3029
3030 if (!(pvmw->pmd && !pvmw->pte))
3031 return;
3032
3033 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
3034 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
3035 if (pmd_dirty(pmdval))
3036 set_page_dirty(page);
3037 entry = make_migration_entry(page, pmd_write(pmdval));
3038 pmdswp = swp_entry_to_pmd(entry);
3039 if (pmd_soft_dirty(pmdval))
3040 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3041 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
3042 page_remove_rmap(page, true);
3043 put_page(page);
3044 }
3045
3046 void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3047 {
3048 struct vm_area_struct *vma = pvmw->vma;
3049 struct mm_struct *mm = vma->vm_mm;
3050 unsigned long address = pvmw->address;
3051 unsigned long mmun_start = address & HPAGE_PMD_MASK;
3052 pmd_t pmde;
3053 swp_entry_t entry;
3054
3055 if (!(pvmw->pmd && !pvmw->pte))
3056 return;
3057
3058 entry = pmd_to_swp_entry(*pvmw->pmd);
3059 get_page(new);
3060 pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot));
3061 if (pmd_swp_soft_dirty(*pvmw->pmd))
3062 pmde = pmd_mksoft_dirty(pmde);
3063 if (is_write_migration_entry(entry))
3064 pmde = maybe_pmd_mkwrite(pmde, vma);
3065
3066 flush_cache_range(vma, mmun_start, mmun_start + HPAGE_PMD_SIZE);
3067 if (PageAnon(new))
3068 page_add_anon_rmap(new, vma, mmun_start, true);
3069 else
3070 page_add_file_rmap(new, true);
3071 set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
3072 if ((vma->vm_flags & VM_LOCKED) && !PageDoubleMap(new))
3073 mlock_vma_page(new);
3074 update_mmu_cache_pmd(vma, address, pvmw->pmd);
3075 }
3076 #endif