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