1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2009 Red Hat, Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/mm.h> 9 #include <linux/sched.h> 10 #include <linux/sched/mm.h> 11 #include <linux/sched/numa_balancing.h> 12 #include <linux/highmem.h> 13 #include <linux/hugetlb.h> 14 #include <linux/mmu_notifier.h> 15 #include <linux/rmap.h> 16 #include <linux/swap.h> 17 #include <linux/list_lru.h> 18 #include <linux/shrinker.h> 19 #include <linux/mm_inline.h> 20 #include <linux/swapops.h> 21 #include <linux/backing-dev.h> 22 #include <linux/dax.h> 23 #include <linux/mm_types.h> 24 #include <linux/khugepaged.h> 25 #include <linux/freezer.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 #include <linux/page_owner.h> 38 #include <linux/sched/sysctl.h> 39 #include <linux/memory-tiers.h> 40 #include <linux/compat.h> 41 #include <linux/pgalloc.h> 42 #include <linux/pgalloc_tag.h> 43 #include <linux/pagewalk.h> 44 #include <linux/cleanup.h> 45 46 #include <asm/tlb.h> 47 #include "internal.h" 48 #include "swap.h" 49 50 #define CREATE_TRACE_POINTS 51 #include <trace/events/thp.h> 52 53 /* 54 * By default, transparent hugepage support is disabled in order to avoid 55 * risking an increased memory footprint for applications that are not 56 * guaranteed to benefit from it. When transparent hugepage support is 57 * enabled, it is for all mappings, and khugepaged scans all mappings. 58 * Defrag is invoked by khugepaged hugepage allocations and by page faults 59 * for all hugepage allocations. 60 */ 61 unsigned long transparent_hugepage_flags __read_mostly = 62 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS 63 (1<<TRANSPARENT_HUGEPAGE_FLAG)| 64 #endif 65 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE 66 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)| 67 #endif 68 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)| 69 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)| 70 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); 71 72 static struct lock_class_key deferred_split_key; 73 static struct list_lru deferred_split_lru; 74 static struct shrinker *deferred_split_shrinker; 75 static unsigned long deferred_split_count(struct shrinker *shrink, 76 struct shrink_control *sc); 77 static unsigned long deferred_split_scan(struct shrinker *shrink, 78 struct shrink_control *sc); 79 static bool split_underused_thp = true; 80 81 #define HUGE_ZERO_UNSET_PFN (~0UL) 82 struct folio *huge_zero_folio __read_mostly; 83 unsigned long huge_zero_pfn __read_mostly = HUGE_ZERO_UNSET_PFN; 84 #ifndef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO 85 static atomic_t huge_zero_refcount; 86 static DEFINE_SPINLOCK(huge_zero_lock); 87 static struct shrinker *huge_zero_folio_shrinker; 88 #endif 89 90 unsigned long huge_anon_orders_always __read_mostly; 91 unsigned long huge_anon_orders_madvise __read_mostly; 92 unsigned long huge_anon_orders_inherit __read_mostly; 93 static bool anon_orders_configured __initdata; 94 95 static inline bool file_thp_enabled(struct vm_area_struct *vma) 96 { 97 struct inode *inode; 98 99 if (!vma->vm_file) 100 return false; 101 102 inode = file_inode(vma->vm_file); 103 104 if (IS_ANON_FILE(inode)) 105 return false; 106 107 if (!mapping_pmd_folio_support(vma->vm_file->f_mapping)) 108 return false; 109 110 return S_ISREG(inode->i_mode); 111 } 112 113 /* If returns true, we are unable to access the VMA's folios. */ 114 static bool vma_is_special_huge(const struct vm_area_struct *vma) 115 { 116 if (vma_is_dax(vma)) 117 return false; 118 return vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT); 119 } 120 121 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, 122 vm_flags_t vm_flags, 123 enum tva_type type, 124 unsigned long orders) 125 { 126 const bool smaps = type == TVA_SMAPS; 127 const bool in_pf = type == TVA_PAGEFAULT; 128 const bool forced_collapse = type == TVA_FORCED_COLLAPSE; 129 unsigned long supported_orders; 130 131 /* Check the intersection of requested and supported orders. */ 132 if (vma_is_anonymous(vma)) 133 supported_orders = THP_ORDERS_ALL_ANON; 134 else if (vma_is_dax(vma) || vma_is_special_huge(vma)) 135 supported_orders = THP_ORDERS_ALL_SPECIAL_DAX; 136 else 137 supported_orders = THP_ORDERS_ALL_FILE_DEFAULT; 138 139 orders &= supported_orders; 140 if (!orders) 141 return 0; 142 143 if (!vma->vm_mm) /* vdso */ 144 return 0; 145 146 if (thp_disabled_by_hw() || vma_thp_disabled(vma, vm_flags, forced_collapse)) 147 return 0; 148 149 /* khugepaged doesn't collapse DAX vma, but page fault is fine. */ 150 if (vma_is_dax(vma)) 151 return in_pf ? orders : 0; 152 153 /* 154 * khugepaged special VMA and hugetlb VMA. 155 * Must be checked after dax since some dax mappings may have 156 * VM_MIXEDMAP set. 157 */ 158 if (!in_pf && !smaps && (vm_flags & VM_NO_KHUGEPAGED)) 159 return 0; 160 161 /* 162 * Check alignment for file vma and size for both file and anon vma by 163 * filtering out the unsuitable orders. 164 * 165 * Skip the check for page fault. Huge fault does the check in fault 166 * handlers. 167 */ 168 if (!in_pf) { 169 int order = highest_order(orders); 170 unsigned long addr; 171 172 while (orders) { 173 addr = vma->vm_end - (PAGE_SIZE << order); 174 if (thp_vma_suitable_order(vma, addr, order)) 175 break; 176 order = next_order(&orders, order); 177 } 178 179 if (!orders) 180 return 0; 181 } 182 183 /* 184 * Enabled via shmem mount options or sysfs settings. 185 * Must be done before hugepage flags check since shmem has its 186 * own flags. 187 */ 188 if (!in_pf && shmem_file(vma->vm_file)) 189 return orders & shmem_allowable_huge_orders(file_inode(vma->vm_file), 190 vma, vma->vm_pgoff, 0, 191 forced_collapse); 192 193 if (!vma_is_anonymous(vma)) { 194 /* 195 * Enforce THP collapse requirements as necessary. Anonymous vmas 196 * were already handled in thp_vma_allowable_orders(). 197 */ 198 if (!forced_collapse && 199 (!hugepage_global_enabled() || (!(vm_flags & VM_HUGEPAGE) && 200 !hugepage_global_always()))) 201 return 0; 202 203 /* 204 * Trust that ->huge_fault() handlers know what they are doing 205 * in fault path. 206 */ 207 if (((in_pf || smaps)) && vma->vm_ops->huge_fault) 208 return orders; 209 /* Only regular file is valid in collapse path */ 210 if (((!in_pf || smaps)) && file_thp_enabled(vma)) 211 return orders; 212 return 0; 213 } 214 215 if (vma_is_temporary_stack(vma)) 216 return 0; 217 218 /* 219 * THPeligible bit of smaps should show 1 for proper VMAs even 220 * though anon_vma is not initialized yet. 221 * 222 * Allow page fault since anon_vma may be not initialized until 223 * the first page fault. 224 */ 225 if (!vma->anon_vma) 226 return (smaps || in_pf) ? orders : 0; 227 228 return orders; 229 } 230 231 static struct folio *alloc_huge_zero_folio(void) 232 { 233 struct folio *zero_folio; 234 235 zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) & 236 ~__GFP_MOVABLE, 237 HPAGE_PMD_ORDER); 238 if (!zero_folio) { 239 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED); 240 return NULL; 241 } 242 folio_clear_large_rmappable(zero_folio); /* Explicitly not rmappable. */ 243 return zero_folio; 244 } 245 246 #ifdef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO 247 static int __init huge_zero_init(void) 248 { 249 huge_zero_folio = alloc_huge_zero_folio(); 250 if (!huge_zero_folio) { 251 pr_warn("Allocating persistent huge zero folio failed\n"); 252 } else { 253 huge_zero_pfn = folio_pfn(huge_zero_folio); 254 count_vm_event(THP_ZERO_PAGE_ALLOC); 255 } 256 return 0; 257 } 258 259 static void __init huge_zero_shrinker_exit(void) 260 { 261 } 262 263 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm) 264 { 265 return huge_zero_folio; 266 } 267 268 void mm_put_huge_zero_folio(struct mm_struct *mm) 269 { 270 } 271 #else 272 static bool get_huge_zero_folio(void) 273 { 274 struct folio *zero_folio; 275 276 /* Paired with atomic_set_release(). */ 277 if (likely(atomic_inc_not_zero(&huge_zero_refcount))) 278 return true; 279 280 zero_folio = alloc_huge_zero_folio(); 281 if (unlikely(!zero_folio)) 282 return false; 283 284 /* Paired with critical section in shrink_huge_zero_folio_scan(). */ 285 spin_lock(&huge_zero_lock); 286 if (huge_zero_folio) { 287 /* Somebody else already installed it. */ 288 atomic_inc(&huge_zero_refcount); 289 spin_unlock(&huge_zero_lock); 290 folio_put(zero_folio); 291 return true; 292 } 293 WRITE_ONCE(huge_zero_folio, zero_folio); 294 WRITE_ONCE(huge_zero_pfn, folio_pfn(zero_folio)); 295 /* Paired with atomic_inc_not_zero(). +1 for shrinker pin. */ 296 atomic_set_release(&huge_zero_refcount, 2); 297 spin_unlock(&huge_zero_lock); 298 299 count_vm_event(THP_ZERO_PAGE_ALLOC); 300 return true; 301 } 302 303 static void put_huge_zero_folio(void) 304 { 305 /* 306 * Counter should never go to zero here. Only shrinker can put 307 * last reference. 308 */ 309 WARN_ON_ONCE(atomic_dec_and_test(&huge_zero_refcount)); 310 } 311 312 static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink, 313 struct shrink_control *sc) 314 { 315 /* we can free zero page only if last reference remains */ 316 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0; 317 } 318 319 static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, 320 struct shrink_control *sc) 321 { 322 struct folio *zero_folio; 323 324 /* Paired with critical section in get_huge_zero_folio(). */ 325 scoped_guard(spinlock, &huge_zero_lock) { 326 /* Paired with atomic_inc_not_zero() in get_huge_zero_folio(). */ 327 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) != 1) 328 return 0; 329 330 zero_folio = huge_zero_folio; 331 VM_WARN_ON_ONCE(!zero_folio); 332 WRITE_ONCE(huge_zero_folio, NULL); 333 WRITE_ONCE(huge_zero_pfn, HUGE_ZERO_UNSET_PFN); 334 } 335 336 folio_put(zero_folio); 337 return HPAGE_PMD_NR; 338 } 339 340 static int __init huge_zero_init(void) 341 { 342 huge_zero_folio_shrinker = shrinker_alloc(0, "thp-zero"); 343 if (!huge_zero_folio_shrinker) { 344 shrinker_free(deferred_split_shrinker); 345 list_lru_destroy(&deferred_split_lru); 346 return -ENOMEM; 347 } 348 349 huge_zero_folio_shrinker->count_objects = shrink_huge_zero_folio_count; 350 huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan; 351 shrinker_register(huge_zero_folio_shrinker); 352 return 0; 353 } 354 355 static void __init huge_zero_shrinker_exit(void) 356 { 357 shrinker_free(huge_zero_folio_shrinker); 358 } 359 360 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm) 361 { 362 if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) 363 return READ_ONCE(huge_zero_folio); 364 365 if (!get_huge_zero_folio()) 366 return NULL; 367 368 if (mm_flags_test_and_set(MMF_HUGE_ZERO_FOLIO, mm)) 369 put_huge_zero_folio(); 370 371 return READ_ONCE(huge_zero_folio); 372 } 373 374 void mm_put_huge_zero_folio(struct mm_struct *mm) 375 { 376 if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) 377 put_huge_zero_folio(); 378 } 379 #endif /* CONFIG_PERSISTENT_HUGE_ZERO_FOLIO */ 380 381 #ifdef CONFIG_SYSFS 382 static ssize_t enabled_show(struct kobject *kobj, 383 struct kobj_attribute *attr, char *buf) 384 { 385 const char *output; 386 387 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags)) 388 output = "[always] madvise never"; 389 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 390 &transparent_hugepage_flags)) 391 output = "always [madvise] never"; 392 else 393 output = "always madvise [never]"; 394 395 return sysfs_emit(buf, "%s\n", output); 396 } 397 398 enum anon_enabled_mode { 399 ANON_ENABLED_ALWAYS = 0, 400 ANON_ENABLED_INHERIT = 1, 401 ANON_ENABLED_MADVISE = 2, 402 ANON_ENABLED_NEVER = 3, 403 }; 404 405 static const char * const anon_enabled_mode_strings[] = { 406 [ANON_ENABLED_ALWAYS] = "always", 407 [ANON_ENABLED_INHERIT] = "inherit", 408 [ANON_ENABLED_MADVISE] = "madvise", 409 [ANON_ENABLED_NEVER] = "never", 410 }; 411 412 enum global_enabled_mode { 413 GLOBAL_ENABLED_ALWAYS = 0, 414 GLOBAL_ENABLED_MADVISE = 1, 415 GLOBAL_ENABLED_NEVER = 2, 416 }; 417 418 static const char * const global_enabled_mode_strings[] = { 419 [GLOBAL_ENABLED_ALWAYS] = "always", 420 [GLOBAL_ENABLED_MADVISE] = "madvise", 421 [GLOBAL_ENABLED_NEVER] = "never", 422 }; 423 424 static bool set_global_enabled_mode(enum global_enabled_mode mode) 425 { 426 static const unsigned long thp_flags[] = { 427 TRANSPARENT_HUGEPAGE_FLAG, 428 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 429 }; 430 enum global_enabled_mode m; 431 bool changed = false; 432 433 for (m = 0; m < ARRAY_SIZE(thp_flags); m++) { 434 if (m == mode) 435 changed |= !test_and_set_bit(thp_flags[m], 436 &transparent_hugepage_flags); 437 else 438 changed |= test_and_clear_bit(thp_flags[m], 439 &transparent_hugepage_flags); 440 } 441 442 return changed; 443 } 444 445 static ssize_t enabled_store(struct kobject *kobj, 446 struct kobj_attribute *attr, 447 const char *buf, size_t count) 448 { 449 int mode; 450 451 mode = sysfs_match_string(global_enabled_mode_strings, buf); 452 if (mode < 0) 453 return -EINVAL; 454 455 if (set_global_enabled_mode(mode)) { 456 int err = start_stop_khugepaged(); 457 458 if (err) 459 return err; 460 } else { 461 /* 462 * Recalculate watermarks even when the mode didn't 463 * change, as the previous code always called 464 * start_stop_khugepaged() which does this internally. 465 */ 466 set_recommended_min_free_kbytes(); 467 } 468 return count; 469 } 470 471 static struct kobj_attribute enabled_attr = __ATTR_RW(enabled); 472 473 ssize_t single_hugepage_flag_show(struct kobject *kobj, 474 struct kobj_attribute *attr, char *buf, 475 enum transparent_hugepage_flag flag) 476 { 477 return sysfs_emit(buf, "%d\n", 478 !!test_bit(flag, &transparent_hugepage_flags)); 479 } 480 481 ssize_t single_hugepage_flag_store(struct kobject *kobj, 482 struct kobj_attribute *attr, 483 const char *buf, size_t count, 484 enum transparent_hugepage_flag flag) 485 { 486 unsigned long value; 487 int ret; 488 489 ret = kstrtoul(buf, 10, &value); 490 if (ret < 0) 491 return ret; 492 if (value > 1) 493 return -EINVAL; 494 495 if (value) 496 set_bit(flag, &transparent_hugepage_flags); 497 else 498 clear_bit(flag, &transparent_hugepage_flags); 499 500 return count; 501 } 502 503 enum defrag_mode { 504 DEFRAG_ALWAYS = 0, 505 DEFRAG_DEFER, 506 DEFRAG_DEFER_MADVISE, 507 DEFRAG_MADVISE, 508 DEFRAG_NEVER, 509 }; 510 511 static const char * const defrag_mode_strings[] = { 512 [DEFRAG_ALWAYS] = "always", 513 [DEFRAG_DEFER] = "defer", 514 [DEFRAG_DEFER_MADVISE] = "defer+madvise", 515 [DEFRAG_MADVISE] = "madvise", 516 [DEFRAG_NEVER] = "never", 517 }; 518 519 static const enum transparent_hugepage_flag defrag_flags[] = { 520 [DEFRAG_ALWAYS] = TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, 521 [DEFRAG_DEFER] = TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, 522 [DEFRAG_DEFER_MADVISE] = TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, 523 [DEFRAG_MADVISE] = TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, 524 }; 525 526 static ssize_t defrag_show(struct kobject *kobj, 527 struct kobj_attribute *attr, char *buf) 528 { 529 int active = DEFRAG_NEVER; 530 int len = 0; 531 int i; 532 533 for (i = 0; i < ARRAY_SIZE(defrag_flags); i++) { 534 if (test_bit(defrag_flags[i], &transparent_hugepage_flags)) { 535 active = i; 536 break; 537 } 538 } 539 540 for (i = 0; i < ARRAY_SIZE(defrag_mode_strings); i++) { 541 if (i == active) 542 len += sysfs_emit_at(buf, len, "[%s] ", 543 defrag_mode_strings[i]); 544 else 545 len += sysfs_emit_at(buf, len, "%s ", 546 defrag_mode_strings[i]); 547 } 548 549 /* Replace trailing space with newline */ 550 buf[len - 1] = '\n'; 551 552 return len; 553 } 554 555 static ssize_t defrag_store(struct kobject *kobj, 556 struct kobj_attribute *attr, 557 const char *buf, size_t count) 558 { 559 int mode, m; 560 561 mode = sysfs_match_string(defrag_mode_strings, buf); 562 if (mode < 0) 563 return -EINVAL; 564 565 for (m = 0; m < ARRAY_SIZE(defrag_flags); m++) { 566 if (m == mode) 567 set_bit(defrag_flags[m], &transparent_hugepage_flags); 568 else 569 clear_bit(defrag_flags[m], &transparent_hugepage_flags); 570 } 571 572 return count; 573 } 574 static struct kobj_attribute defrag_attr = __ATTR_RW(defrag); 575 576 static ssize_t use_zero_page_show(struct kobject *kobj, 577 struct kobj_attribute *attr, char *buf) 578 { 579 return single_hugepage_flag_show(kobj, attr, buf, 580 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); 581 } 582 static ssize_t use_zero_page_store(struct kobject *kobj, 583 struct kobj_attribute *attr, const char *buf, size_t count) 584 { 585 return single_hugepage_flag_store(kobj, attr, buf, count, 586 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); 587 } 588 static struct kobj_attribute use_zero_page_attr = __ATTR_RW(use_zero_page); 589 590 static ssize_t hpage_pmd_size_show(struct kobject *kobj, 591 struct kobj_attribute *attr, char *buf) 592 { 593 return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE); 594 } 595 static struct kobj_attribute hpage_pmd_size_attr = 596 __ATTR_RO(hpage_pmd_size); 597 598 static ssize_t split_underused_thp_show(struct kobject *kobj, 599 struct kobj_attribute *attr, char *buf) 600 { 601 return sysfs_emit(buf, "%d\n", split_underused_thp); 602 } 603 604 static ssize_t split_underused_thp_store(struct kobject *kobj, 605 struct kobj_attribute *attr, 606 const char *buf, size_t count) 607 { 608 int err = kstrtobool(buf, &split_underused_thp); 609 610 if (err < 0) 611 return err; 612 613 return count; 614 } 615 616 static struct kobj_attribute split_underused_thp_attr = __ATTR( 617 shrink_underused, 0644, split_underused_thp_show, split_underused_thp_store); 618 619 static struct attribute *hugepage_attr[] = { 620 &enabled_attr.attr, 621 &defrag_attr.attr, 622 &use_zero_page_attr.attr, 623 &hpage_pmd_size_attr.attr, 624 #ifdef CONFIG_SHMEM 625 &shmem_enabled_attr.attr, 626 #endif 627 &split_underused_thp_attr.attr, 628 NULL, 629 }; 630 631 static const struct attribute_group hugepage_attr_group = { 632 .attrs = hugepage_attr, 633 }; 634 635 static void hugepage_exit_sysfs(struct kobject *hugepage_kobj); 636 static void thpsize_release(struct kobject *kobj); 637 static DEFINE_SPINLOCK(huge_anon_orders_lock); 638 static LIST_HEAD(thpsize_list); 639 640 static ssize_t anon_enabled_show(struct kobject *kobj, 641 struct kobj_attribute *attr, char *buf) 642 { 643 int order = to_thpsize(kobj)->order; 644 const char *output; 645 646 if (test_bit(order, &huge_anon_orders_always)) 647 output = "[always] inherit madvise never"; 648 else if (test_bit(order, &huge_anon_orders_inherit)) 649 output = "always [inherit] madvise never"; 650 else if (test_bit(order, &huge_anon_orders_madvise)) 651 output = "always inherit [madvise] never"; 652 else 653 output = "always inherit madvise [never]"; 654 655 return sysfs_emit(buf, "%s\n", output); 656 } 657 658 static bool set_anon_enabled_mode(int order, enum anon_enabled_mode mode) 659 { 660 static unsigned long *enabled_orders[] = { 661 &huge_anon_orders_always, 662 &huge_anon_orders_inherit, 663 &huge_anon_orders_madvise, 664 }; 665 enum anon_enabled_mode m; 666 bool changed = false; 667 668 spin_lock(&huge_anon_orders_lock); 669 for (m = 0; m < ARRAY_SIZE(enabled_orders); m++) { 670 if (m == mode) 671 changed |= !__test_and_set_bit(order, enabled_orders[m]); 672 else 673 changed |= __test_and_clear_bit(order, enabled_orders[m]); 674 } 675 spin_unlock(&huge_anon_orders_lock); 676 677 return changed; 678 } 679 680 static ssize_t anon_enabled_store(struct kobject *kobj, 681 struct kobj_attribute *attr, 682 const char *buf, size_t count) 683 { 684 int order = to_thpsize(kobj)->order; 685 int mode; 686 687 mode = sysfs_match_string(anon_enabled_mode_strings, buf); 688 if (mode < 0) 689 return -EINVAL; 690 691 if (set_anon_enabled_mode(order, mode)) { 692 int err = start_stop_khugepaged(); 693 694 if (err) 695 return err; 696 } else { 697 /* 698 * Recalculate watermarks even when the mode didn't 699 * change, as the previous code always called 700 * start_stop_khugepaged() which does this internally. 701 */ 702 set_recommended_min_free_kbytes(); 703 } 704 705 return count; 706 } 707 708 static struct kobj_attribute anon_enabled_attr = 709 __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store); 710 711 static struct attribute *anon_ctrl_attrs[] = { 712 &anon_enabled_attr.attr, 713 NULL, 714 }; 715 716 static const struct attribute_group anon_ctrl_attr_grp = { 717 .attrs = anon_ctrl_attrs, 718 }; 719 720 static struct attribute *file_ctrl_attrs[] = { 721 #ifdef CONFIG_SHMEM 722 &thpsize_shmem_enabled_attr.attr, 723 #endif 724 NULL, 725 }; 726 727 static const struct attribute_group file_ctrl_attr_grp = { 728 .attrs = file_ctrl_attrs, 729 }; 730 731 static struct attribute *any_ctrl_attrs[] = { 732 NULL, 733 }; 734 735 static const struct attribute_group any_ctrl_attr_grp = { 736 .attrs = any_ctrl_attrs, 737 }; 738 739 static const struct kobj_type thpsize_ktype = { 740 .release = &thpsize_release, 741 .sysfs_ops = &kobj_sysfs_ops, 742 }; 743 744 DEFINE_PER_CPU(struct mthp_stat, mthp_stats) = {{{0}}}; 745 746 static unsigned long sum_mthp_stat(int order, enum mthp_stat_item item) 747 { 748 unsigned long sum = 0; 749 int cpu; 750 751 for_each_possible_cpu(cpu) { 752 struct mthp_stat *this = &per_cpu(mthp_stats, cpu); 753 754 sum += this->stats[order][item]; 755 } 756 757 return sum; 758 } 759 760 #define DEFINE_MTHP_STAT_ATTR(_name, _index) \ 761 static ssize_t _name##_show(struct kobject *kobj, \ 762 struct kobj_attribute *attr, char *buf) \ 763 { \ 764 int order = to_thpsize(kobj)->order; \ 765 \ 766 return sysfs_emit(buf, "%lu\n", sum_mthp_stat(order, _index)); \ 767 } \ 768 static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 769 770 DEFINE_MTHP_STAT_ATTR(anon_fault_alloc, MTHP_STAT_ANON_FAULT_ALLOC); 771 DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK); 772 DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE); 773 DEFINE_MTHP_STAT_ATTR(collapse_alloc, MTHP_STAT_COLLAPSE_ALLOC); 774 DEFINE_MTHP_STAT_ATTR(collapse_alloc_failed, MTHP_STAT_COLLAPSE_ALLOC_FAILED); 775 DEFINE_MTHP_STAT_ATTR(zswpout, MTHP_STAT_ZSWPOUT); 776 DEFINE_MTHP_STAT_ATTR(swpin, MTHP_STAT_SWPIN); 777 DEFINE_MTHP_STAT_ATTR(swpin_fallback, MTHP_STAT_SWPIN_FALLBACK); 778 DEFINE_MTHP_STAT_ATTR(swpin_fallback_charge, MTHP_STAT_SWPIN_FALLBACK_CHARGE); 779 DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT); 780 DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK); 781 #ifdef CONFIG_SHMEM 782 DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC); 783 DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK); 784 DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE); 785 #endif 786 DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT); 787 DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED); 788 DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED); 789 DEFINE_MTHP_STAT_ATTR(nr_anon, MTHP_STAT_NR_ANON); 790 DEFINE_MTHP_STAT_ATTR(nr_anon_partially_mapped, MTHP_STAT_NR_ANON_PARTIALLY_MAPPED); 791 DEFINE_MTHP_STAT_ATTR(collapse_exceed_swap_pte, MTHP_STAT_COLLAPSE_EXCEED_SWAP); 792 DEFINE_MTHP_STAT_ATTR(collapse_exceed_none_pte, MTHP_STAT_COLLAPSE_EXCEED_NONE); 793 DEFINE_MTHP_STAT_ATTR(collapse_exceed_shared_pte, MTHP_STAT_COLLAPSE_EXCEED_SHARED); 794 795 796 static struct attribute *anon_stats_attrs[] = { 797 &anon_fault_alloc_attr.attr, 798 &anon_fault_fallback_attr.attr, 799 &anon_fault_fallback_charge_attr.attr, 800 #ifndef CONFIG_SHMEM 801 &zswpout_attr.attr, 802 &swpin_attr.attr, 803 &swpin_fallback_attr.attr, 804 &swpin_fallback_charge_attr.attr, 805 &swpout_attr.attr, 806 &swpout_fallback_attr.attr, 807 #endif 808 &split_deferred_attr.attr, 809 &nr_anon_attr.attr, 810 &nr_anon_partially_mapped_attr.attr, 811 &collapse_exceed_swap_pte_attr.attr, 812 &collapse_exceed_none_pte_attr.attr, 813 &collapse_exceed_shared_pte_attr.attr, 814 NULL, 815 }; 816 817 static struct attribute_group anon_stats_attr_grp = { 818 .name = "stats", 819 .attrs = anon_stats_attrs, 820 }; 821 822 static struct attribute *file_stats_attrs[] = { 823 #ifdef CONFIG_SHMEM 824 &shmem_alloc_attr.attr, 825 &shmem_fallback_attr.attr, 826 &shmem_fallback_charge_attr.attr, 827 #endif 828 NULL, 829 }; 830 831 static struct attribute_group file_stats_attr_grp = { 832 .name = "stats", 833 .attrs = file_stats_attrs, 834 }; 835 836 static struct attribute *any_stats_attrs[] = { 837 #ifdef CONFIG_SHMEM 838 &zswpout_attr.attr, 839 &swpin_attr.attr, 840 &swpin_fallback_attr.attr, 841 &swpin_fallback_charge_attr.attr, 842 &swpout_attr.attr, 843 &swpout_fallback_attr.attr, 844 #endif 845 &split_attr.attr, 846 &split_failed_attr.attr, 847 &collapse_alloc_attr.attr, 848 &collapse_alloc_failed_attr.attr, 849 NULL, 850 }; 851 852 static struct attribute_group any_stats_attr_grp = { 853 .name = "stats", 854 .attrs = any_stats_attrs, 855 }; 856 857 static int sysfs_add_group(struct kobject *kobj, 858 const struct attribute_group *grp) 859 { 860 int ret = -ENOENT; 861 862 /* 863 * If the group is named, try to merge first, assuming the subdirectory 864 * was already created. This avoids the warning emitted by 865 * sysfs_create_group() if the directory already exists. 866 */ 867 if (grp->name) 868 ret = sysfs_merge_group(kobj, grp); 869 if (ret) 870 ret = sysfs_create_group(kobj, grp); 871 872 return ret; 873 } 874 875 static struct thpsize *thpsize_create(int order, struct kobject *parent) 876 { 877 unsigned long size = (PAGE_SIZE << order) / SZ_1K; 878 struct thpsize *thpsize; 879 int ret = -ENOMEM; 880 881 thpsize = kzalloc_obj(*thpsize); 882 if (!thpsize) 883 goto err; 884 885 thpsize->order = order; 886 887 ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent, 888 "hugepages-%lukB", size); 889 if (ret) { 890 kfree(thpsize); 891 goto err; 892 } 893 894 895 ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp); 896 if (ret) 897 goto err_put; 898 899 ret = sysfs_add_group(&thpsize->kobj, &any_stats_attr_grp); 900 if (ret) 901 goto err_put; 902 903 if (BIT(order) & THP_ORDERS_ALL_ANON) { 904 ret = sysfs_add_group(&thpsize->kobj, &anon_ctrl_attr_grp); 905 if (ret) 906 goto err_put; 907 908 ret = sysfs_add_group(&thpsize->kobj, &anon_stats_attr_grp); 909 if (ret) 910 goto err_put; 911 } 912 913 if (BIT(order) & THP_ORDERS_ALL_FILE_DEFAULT) { 914 ret = sysfs_add_group(&thpsize->kobj, &file_ctrl_attr_grp); 915 if (ret) 916 goto err_put; 917 918 ret = sysfs_add_group(&thpsize->kobj, &file_stats_attr_grp); 919 if (ret) 920 goto err_put; 921 } 922 923 return thpsize; 924 err_put: 925 kobject_put(&thpsize->kobj); 926 err: 927 return ERR_PTR(ret); 928 } 929 930 static void thpsize_release(struct kobject *kobj) 931 { 932 kfree(to_thpsize(kobj)); 933 } 934 935 static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) 936 { 937 int err; 938 struct thpsize *thpsize; 939 unsigned long orders; 940 int order; 941 942 /* 943 * Default to setting PMD-sized THP to inherit the global setting and 944 * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time 945 * constant so we have to do this here. 946 */ 947 if (!anon_orders_configured) 948 huge_anon_orders_inherit = BIT(PMD_ORDER); 949 950 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); 951 if (unlikely(!*hugepage_kobj)) { 952 pr_err("failed to create transparent hugepage kobject\n"); 953 return -ENOMEM; 954 } 955 956 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group); 957 if (err) { 958 pr_err("failed to register transparent hugepage group\n"); 959 goto delete_obj; 960 } 961 962 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group); 963 if (err) { 964 pr_err("failed to register transparent hugepage group\n"); 965 goto remove_hp_group; 966 } 967 968 orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT; 969 order = highest_order(orders); 970 while (orders) { 971 thpsize = thpsize_create(order, *hugepage_kobj); 972 if (IS_ERR(thpsize)) { 973 pr_err("failed to create thpsize for order %d\n", order); 974 err = PTR_ERR(thpsize); 975 goto remove_all; 976 } 977 list_add(&thpsize->node, &thpsize_list); 978 order = next_order(&orders, order); 979 } 980 981 return 0; 982 983 remove_all: 984 hugepage_exit_sysfs(*hugepage_kobj); 985 return err; 986 remove_hp_group: 987 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group); 988 delete_obj: 989 kobject_put(*hugepage_kobj); 990 return err; 991 } 992 993 static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj) 994 { 995 struct thpsize *thpsize, *tmp; 996 997 list_for_each_entry_safe(thpsize, tmp, &thpsize_list, node) { 998 list_del(&thpsize->node); 999 kobject_put(&thpsize->kobj); 1000 } 1001 1002 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group); 1003 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group); 1004 kobject_put(hugepage_kobj); 1005 } 1006 #else 1007 static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj) 1008 { 1009 return 0; 1010 } 1011 1012 static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) 1013 { 1014 } 1015 #endif /* CONFIG_SYSFS */ 1016 1017 int folio_memcg_alloc_deferred(struct folio *folio) 1018 { 1019 if (mem_cgroup_disabled()) 1020 return 0; 1021 return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL); 1022 } 1023 1024 static int __init thp_shrinker_init(void) 1025 { 1026 deferred_split_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE | 1027 SHRINKER_MEMCG_AWARE, 1028 "thp-deferred_split"); 1029 if (!deferred_split_shrinker) 1030 return -ENOMEM; 1031 1032 if (list_lru_init_memcg_key(&deferred_split_lru, 1033 deferred_split_shrinker, 1034 &deferred_split_key)) { 1035 shrinker_free(deferred_split_shrinker); 1036 return -ENOMEM; 1037 } 1038 1039 deferred_split_shrinker->count_objects = deferred_split_count; 1040 deferred_split_shrinker->scan_objects = deferred_split_scan; 1041 shrinker_register(deferred_split_shrinker); 1042 1043 return huge_zero_init(); 1044 } 1045 1046 static void __init thp_shrinker_exit(void) 1047 { 1048 shrinker_free(deferred_split_shrinker); 1049 list_lru_destroy(&deferred_split_lru); 1050 huge_zero_shrinker_exit(); 1051 } 1052 1053 static int __init hugepage_init(void) 1054 { 1055 int err; 1056 struct kobject *hugepage_kobj; 1057 1058 if (!has_transparent_hugepage()) { 1059 transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED; 1060 return -EINVAL; 1061 } 1062 1063 /* 1064 * hugepages can't be allocated by the buddy allocator 1065 */ 1066 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER > MAX_PAGE_ORDER); 1067 1068 err = hugepage_init_sysfs(&hugepage_kobj); 1069 if (err) 1070 goto err_sysfs; 1071 1072 err = khugepaged_init(); 1073 if (err) 1074 goto err_slab; 1075 1076 err = thp_shrinker_init(); 1077 if (err) 1078 goto err_shrinker; 1079 1080 /* 1081 * By default disable transparent hugepages on smaller systems, 1082 * where the extra memory used could hurt more than TLB overhead 1083 * is likely to save. The admin can still enable it through /sys. 1084 */ 1085 if (totalram_pages() < MB_TO_PAGES(512)) { 1086 transparent_hugepage_flags = 0; 1087 return 0; 1088 } 1089 1090 err = start_stop_khugepaged(); 1091 if (err) 1092 goto err_khugepaged; 1093 1094 return 0; 1095 err_khugepaged: 1096 thp_shrinker_exit(); 1097 err_shrinker: 1098 khugepaged_destroy(); 1099 err_slab: 1100 hugepage_exit_sysfs(hugepage_kobj); 1101 err_sysfs: 1102 return err; 1103 } 1104 subsys_initcall(hugepage_init); 1105 1106 static int __init setup_transparent_hugepage(char *str) 1107 { 1108 int ret = 0; 1109 if (!str) 1110 goto out; 1111 if (!strcmp(str, "always")) { 1112 set_bit(TRANSPARENT_HUGEPAGE_FLAG, 1113 &transparent_hugepage_flags); 1114 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 1115 &transparent_hugepage_flags); 1116 ret = 1; 1117 } else if (!strcmp(str, "madvise")) { 1118 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, 1119 &transparent_hugepage_flags); 1120 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 1121 &transparent_hugepage_flags); 1122 ret = 1; 1123 } else if (!strcmp(str, "never")) { 1124 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, 1125 &transparent_hugepage_flags); 1126 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 1127 &transparent_hugepage_flags); 1128 ret = 1; 1129 } 1130 out: 1131 if (!ret) 1132 pr_warn("transparent_hugepage= cannot parse, ignored\n"); 1133 return ret; 1134 } 1135 __setup("transparent_hugepage=", setup_transparent_hugepage); 1136 1137 static char str_dup[PAGE_SIZE] __initdata; 1138 static int __init setup_thp_anon(char *str) 1139 { 1140 char *token, *range, *policy, *subtoken; 1141 unsigned long always, inherit, madvise; 1142 char *start_size, *end_size; 1143 int start, end, nr; 1144 char *p; 1145 1146 if (!str || strlen(str) + 1 > PAGE_SIZE) 1147 goto err; 1148 strscpy(str_dup, str); 1149 1150 always = huge_anon_orders_always; 1151 madvise = huge_anon_orders_madvise; 1152 inherit = huge_anon_orders_inherit; 1153 p = str_dup; 1154 while ((token = strsep(&p, ";")) != NULL) { 1155 range = strsep(&token, ":"); 1156 policy = token; 1157 1158 if (!policy) 1159 goto err; 1160 1161 while ((subtoken = strsep(&range, ",")) != NULL) { 1162 if (strchr(subtoken, '-')) { 1163 start_size = strsep(&subtoken, "-"); 1164 end_size = subtoken; 1165 1166 start = get_order_from_str(start_size, THP_ORDERS_ALL_ANON); 1167 end = get_order_from_str(end_size, THP_ORDERS_ALL_ANON); 1168 } else { 1169 start_size = end_size = subtoken; 1170 start = end = get_order_from_str(subtoken, 1171 THP_ORDERS_ALL_ANON); 1172 } 1173 1174 if (start == -EINVAL) { 1175 pr_err("invalid size %s in thp_anon boot parameter\n", start_size); 1176 goto err; 1177 } 1178 1179 if (end == -EINVAL) { 1180 pr_err("invalid size %s in thp_anon boot parameter\n", end_size); 1181 goto err; 1182 } 1183 1184 if (start < 0 || end < 0 || start > end) 1185 goto err; 1186 1187 nr = end - start + 1; 1188 if (!strcmp(policy, "always")) { 1189 bitmap_set(&always, start, nr); 1190 bitmap_clear(&inherit, start, nr); 1191 bitmap_clear(&madvise, start, nr); 1192 } else if (!strcmp(policy, "madvise")) { 1193 bitmap_set(&madvise, start, nr); 1194 bitmap_clear(&inherit, start, nr); 1195 bitmap_clear(&always, start, nr); 1196 } else if (!strcmp(policy, "inherit")) { 1197 bitmap_set(&inherit, start, nr); 1198 bitmap_clear(&madvise, start, nr); 1199 bitmap_clear(&always, start, nr); 1200 } else if (!strcmp(policy, "never")) { 1201 bitmap_clear(&inherit, start, nr); 1202 bitmap_clear(&madvise, start, nr); 1203 bitmap_clear(&always, start, nr); 1204 } else { 1205 pr_err("invalid policy %s in thp_anon boot parameter\n", policy); 1206 goto err; 1207 } 1208 } 1209 } 1210 1211 huge_anon_orders_always = always; 1212 huge_anon_orders_madvise = madvise; 1213 huge_anon_orders_inherit = inherit; 1214 anon_orders_configured = true; 1215 return 1; 1216 1217 err: 1218 pr_warn("thp_anon=%s: error parsing string, ignoring setting\n", str); 1219 return 0; 1220 } 1221 __setup("thp_anon=", setup_thp_anon); 1222 1223 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) 1224 { 1225 if (likely(vma->vm_flags & VM_WRITE)) 1226 pmd = pmd_mkwrite(pmd, vma); 1227 return pmd; 1228 } 1229 1230 static inline bool is_transparent_hugepage(const struct folio *folio) 1231 { 1232 if (!folio_test_large(folio)) 1233 return false; 1234 1235 return is_huge_zero_folio(folio) || 1236 folio_test_large_rmappable(folio); 1237 } 1238 1239 static unsigned long __thp_get_unmapped_area(struct file *filp, 1240 unsigned long addr, unsigned long len, 1241 loff_t off, unsigned long flags, unsigned long size, 1242 vm_flags_t vm_flags) 1243 { 1244 loff_t off_end = off + len; 1245 loff_t off_align = round_up(off, size); 1246 unsigned long len_pad, ret, off_sub; 1247 1248 if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) 1249 return 0; 1250 1251 if (off_end <= off_align || (off_end - off_align) < size) 1252 return 0; 1253 1254 len_pad = len + size; 1255 if (len_pad < len || (off + len_pad) < off) 1256 return 0; 1257 1258 ret = mm_get_unmapped_area_vmflags(filp, addr, len_pad, 1259 off >> PAGE_SHIFT, flags, vm_flags); 1260 1261 /* 1262 * The failure might be due to length padding. The caller will retry 1263 * without the padding. 1264 */ 1265 if (IS_ERR_VALUE(ret)) 1266 return 0; 1267 1268 /* 1269 * Do not try to align to THP boundary if allocation at the address 1270 * hint succeeds. 1271 */ 1272 if (ret == addr) 1273 return addr; 1274 1275 off_sub = (off - ret) & (size - 1); 1276 1277 if (mm_flags_test(MMF_TOPDOWN, current->mm) && !off_sub) 1278 return ret + size; 1279 1280 ret += off_sub; 1281 return ret; 1282 } 1283 1284 unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, 1285 unsigned long len, unsigned long pgoff, unsigned long flags, 1286 vm_flags_t vm_flags) 1287 { 1288 unsigned long ret; 1289 loff_t off = (loff_t)pgoff << PAGE_SHIFT; 1290 1291 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE, vm_flags); 1292 if (ret) 1293 return ret; 1294 1295 return mm_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, 1296 vm_flags); 1297 } 1298 1299 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, 1300 unsigned long len, unsigned long pgoff, unsigned long flags) 1301 { 1302 return thp_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, 0); 1303 } 1304 EXPORT_SYMBOL_GPL(thp_get_unmapped_area); 1305 1306 static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma, 1307 unsigned long addr) 1308 { 1309 gfp_t gfp = vma_thp_gfp_mask(vma); 1310 const int order = HPAGE_PMD_ORDER; 1311 struct folio *folio; 1312 1313 folio = vma_alloc_folio(gfp, order, vma, addr & HPAGE_PMD_MASK); 1314 1315 if (unlikely(!folio)) { 1316 count_vm_event(THP_FAULT_FALLBACK); 1317 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK); 1318 return NULL; 1319 } 1320 1321 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); 1322 if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { 1323 folio_put(folio); 1324 count_vm_event(THP_FAULT_FALLBACK); 1325 count_vm_event(THP_FAULT_FALLBACK_CHARGE); 1326 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK); 1327 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE); 1328 return NULL; 1329 } 1330 1331 if (folio_memcg_alloc_deferred(folio)) { 1332 folio_put(folio); 1333 count_vm_event(THP_FAULT_FALLBACK); 1334 count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK); 1335 return NULL; 1336 } 1337 1338 folio_throttle_swaprate(folio, gfp); 1339 1340 /* 1341 * When a folio is not zeroed during allocation (__GFP_ZERO not used) 1342 * or user folios require special handling, folio_zero_user() is used to 1343 * make sure that the page corresponding to the faulting address will be 1344 * hot in the cache after zeroing. 1345 */ 1346 if (user_alloc_needs_zeroing()) 1347 folio_zero_user(folio, addr); 1348 /* 1349 * The memory barrier inside __folio_mark_uptodate makes sure that 1350 * folio_zero_user writes become visible before the set_pmd_at() 1351 * write. 1352 */ 1353 __folio_mark_uptodate(folio); 1354 return folio; 1355 } 1356 1357 void map_anon_folio_pmd_nopf(struct folio *folio, pmd_t *pmd, 1358 struct vm_area_struct *vma, unsigned long haddr) 1359 { 1360 pmd_t entry; 1361 1362 entry = folio_mk_pmd(folio, vma->vm_page_prot); 1363 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 1364 folio_add_new_anon_rmap(folio, vma, haddr, RMAP_EXCLUSIVE); 1365 folio_add_lru_vma(folio, vma); 1366 set_pmd_at(vma->vm_mm, haddr, pmd, entry); 1367 update_mmu_cache_pmd(vma, haddr, pmd); 1368 deferred_split_folio(folio, false); 1369 } 1370 1371 static void map_anon_folio_pmd_pf(struct folio *folio, pmd_t *pmd, 1372 struct vm_area_struct *vma, unsigned long haddr) 1373 { 1374 map_anon_folio_pmd_nopf(folio, pmd, vma, haddr); 1375 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR); 1376 count_vm_event(THP_FAULT_ALLOC); 1377 count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_ALLOC); 1378 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC); 1379 } 1380 1381 static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) 1382 { 1383 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 1384 struct vm_area_struct *vma = vmf->vma; 1385 struct folio *folio; 1386 pgtable_t pgtable; 1387 vm_fault_t ret = 0; 1388 1389 folio = vma_alloc_anon_folio_pmd(vma, vmf->address); 1390 if (unlikely(!folio)) 1391 return VM_FAULT_FALLBACK; 1392 1393 pgtable = pte_alloc_one(vma->vm_mm); 1394 if (unlikely(!pgtable)) { 1395 ret = VM_FAULT_OOM; 1396 goto release; 1397 } 1398 1399 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 1400 if (unlikely(!pmd_none(*vmf->pmd))) { 1401 goto unlock_release; 1402 } else { 1403 ret = check_stable_address_space(vma->vm_mm); 1404 if (ret) 1405 goto unlock_release; 1406 1407 /* Deliver the page fault to userland */ 1408 if (userfaultfd_missing(vma)) { 1409 spin_unlock(vmf->ptl); 1410 folio_put(folio); 1411 pte_free(vma->vm_mm, pgtable); 1412 ret = handle_userfault(vmf, VM_UFFD_MISSING); 1413 VM_BUG_ON(ret & VM_FAULT_FALLBACK); 1414 return ret; 1415 } 1416 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 1417 map_anon_folio_pmd_pf(folio, vmf->pmd, vma, haddr); 1418 mm_inc_nr_ptes(vma->vm_mm); 1419 spin_unlock(vmf->ptl); 1420 } 1421 1422 return 0; 1423 unlock_release: 1424 spin_unlock(vmf->ptl); 1425 release: 1426 if (pgtable) 1427 pte_free(vma->vm_mm, pgtable); 1428 folio_put(folio); 1429 return ret; 1430 1431 } 1432 1433 vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf) 1434 { 1435 struct vm_area_struct *vma = vmf->vma; 1436 vm_fault_t ret = 0; 1437 spinlock_t *ptl; 1438 softleaf_t entry; 1439 struct page *page; 1440 struct folio *folio; 1441 1442 if (vmf->flags & FAULT_FLAG_VMA_LOCK) { 1443 vma_end_read(vma); 1444 return VM_FAULT_RETRY; 1445 } 1446 1447 ptl = pmd_lock(vma->vm_mm, vmf->pmd); 1448 if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd))) { 1449 spin_unlock(ptl); 1450 return 0; 1451 } 1452 1453 entry = softleaf_from_pmd(vmf->orig_pmd); 1454 page = softleaf_to_page(entry); 1455 folio = page_folio(page); 1456 vmf->page = page; 1457 vmf->pte = NULL; 1458 if (folio_trylock(folio)) { 1459 folio_get(folio); 1460 spin_unlock(ptl); 1461 ret = page_pgmap(page)->ops->migrate_to_ram(vmf); 1462 folio_unlock(folio); 1463 folio_put(folio); 1464 } else { 1465 spin_unlock(ptl); 1466 } 1467 1468 return ret; 1469 } 1470 1471 /* 1472 * always: directly stall for all thp allocations 1473 * defer: wake kswapd and fail if not immediately available 1474 * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise 1475 * fail if not immediately available 1476 * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately 1477 * available 1478 * never: never stall for any thp allocation 1479 */ 1480 gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma) 1481 { 1482 const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE); 1483 1484 /* Always do synchronous compaction */ 1485 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags)) 1486 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY); 1487 1488 /* Kick kcompactd and fail quickly */ 1489 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags)) 1490 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM; 1491 1492 /* Synchronous compaction if madvised, otherwise kick kcompactd */ 1493 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags)) 1494 return GFP_TRANSHUGE_LIGHT | 1495 (vma_madvised ? __GFP_DIRECT_RECLAIM : 1496 __GFP_KSWAPD_RECLAIM); 1497 1498 /* Only do synchronous compaction if madvised */ 1499 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags)) 1500 return GFP_TRANSHUGE_LIGHT | 1501 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0); 1502 1503 return GFP_TRANSHUGE_LIGHT; 1504 } 1505 1506 /* Caller must hold page table lock. */ 1507 static void set_huge_zero_folio(pgtable_t pgtable, struct mm_struct *mm, 1508 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, 1509 struct folio *zero_folio) 1510 { 1511 pmd_t entry; 1512 entry = folio_mk_pmd(zero_folio, vma->vm_page_prot); 1513 entry = pmd_mkspecial(entry); 1514 pgtable_trans_huge_deposit(mm, pmd, pgtable); 1515 set_pmd_at(mm, haddr, pmd, entry); 1516 mm_inc_nr_ptes(mm); 1517 } 1518 1519 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) 1520 { 1521 struct vm_area_struct *vma = vmf->vma; 1522 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 1523 vm_fault_t ret; 1524 1525 if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) 1526 return VM_FAULT_FALLBACK; 1527 ret = vmf_anon_prepare(vmf); 1528 if (ret) 1529 return ret; 1530 khugepaged_enter_vma(vma, vma->vm_flags); 1531 1532 if (!(vmf->flags & FAULT_FLAG_WRITE) && 1533 !mm_forbids_zeropage(vma->vm_mm) && 1534 transparent_hugepage_use_zero_page()) { 1535 pgtable_t pgtable; 1536 struct folio *zero_folio; 1537 vm_fault_t ret; 1538 1539 pgtable = pte_alloc_one(vma->vm_mm); 1540 if (unlikely(!pgtable)) 1541 return VM_FAULT_OOM; 1542 zero_folio = mm_get_huge_zero_folio(vma->vm_mm); 1543 if (unlikely(!zero_folio)) { 1544 pte_free(vma->vm_mm, pgtable); 1545 count_vm_event(THP_FAULT_FALLBACK); 1546 return VM_FAULT_FALLBACK; 1547 } 1548 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 1549 ret = 0; 1550 if (pmd_none(*vmf->pmd)) { 1551 ret = check_stable_address_space(vma->vm_mm); 1552 if (ret) { 1553 spin_unlock(vmf->ptl); 1554 pte_free(vma->vm_mm, pgtable); 1555 } else if (userfaultfd_missing(vma)) { 1556 spin_unlock(vmf->ptl); 1557 pte_free(vma->vm_mm, pgtable); 1558 ret = handle_userfault(vmf, VM_UFFD_MISSING); 1559 VM_BUG_ON(ret & VM_FAULT_FALLBACK); 1560 } else { 1561 set_huge_zero_folio(pgtable, vma->vm_mm, vma, 1562 haddr, vmf->pmd, zero_folio); 1563 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); 1564 spin_unlock(vmf->ptl); 1565 } 1566 } else { 1567 spin_unlock(vmf->ptl); 1568 pte_free(vma->vm_mm, pgtable); 1569 } 1570 return ret; 1571 } 1572 1573 return __do_huge_pmd_anonymous_page(vmf); 1574 } 1575 1576 struct folio_or_pfn { 1577 union { 1578 struct folio *folio; 1579 unsigned long pfn; 1580 }; 1581 bool is_folio; 1582 }; 1583 1584 static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr, 1585 pmd_t *pmd, struct folio_or_pfn fop, pgprot_t prot, 1586 bool write) 1587 { 1588 struct mm_struct *mm = vma->vm_mm; 1589 pgtable_t pgtable = NULL; 1590 spinlock_t *ptl; 1591 pmd_t entry; 1592 1593 if (addr < vma->vm_start || addr >= vma->vm_end) 1594 return VM_FAULT_SIGBUS; 1595 1596 if (arch_needs_pgtable_deposit()) { 1597 pgtable = pte_alloc_one(vma->vm_mm); 1598 if (!pgtable) 1599 return VM_FAULT_OOM; 1600 } 1601 1602 ptl = pmd_lock(mm, pmd); 1603 if (!pmd_none(*pmd)) { 1604 const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) : 1605 fop.pfn; 1606 1607 if (write) { 1608 if (pmd_pfn(*pmd) != pfn) { 1609 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)); 1610 goto out_unlock; 1611 } 1612 entry = pmd_mkyoung(*pmd); 1613 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 1614 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1)) 1615 update_mmu_cache_pmd(vma, addr, pmd); 1616 } 1617 goto out_unlock; 1618 } 1619 1620 if (fop.is_folio) { 1621 entry = folio_mk_pmd(fop.folio, vma->vm_page_prot); 1622 1623 if (is_huge_zero_folio(fop.folio)) { 1624 entry = pmd_mkspecial(entry); 1625 } else { 1626 folio_get(fop.folio); 1627 folio_add_file_rmap_pmd(fop.folio, &fop.folio->page, vma); 1628 add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PMD_NR); 1629 } 1630 } else { 1631 entry = pmd_mkhuge(pfn_pmd(fop.pfn, prot)); 1632 entry = pmd_mkspecial(entry); 1633 } 1634 if (write) { 1635 entry = pmd_mkyoung(pmd_mkdirty(entry)); 1636 entry = maybe_pmd_mkwrite(entry, vma); 1637 } 1638 1639 if (pgtable) { 1640 pgtable_trans_huge_deposit(mm, pmd, pgtable); 1641 mm_inc_nr_ptes(mm); 1642 pgtable = NULL; 1643 } 1644 1645 set_pmd_at(mm, addr, pmd, entry); 1646 update_mmu_cache_pmd(vma, addr, pmd); 1647 1648 out_unlock: 1649 spin_unlock(ptl); 1650 if (pgtable) 1651 pte_free(mm, pgtable); 1652 return VM_FAULT_NOPAGE; 1653 } 1654 1655 /** 1656 * vmf_insert_pfn_pmd - insert a pmd size pfn 1657 * @vmf: Structure describing the fault 1658 * @pfn: pfn to insert 1659 * @write: whether it's a write fault 1660 * 1661 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info. 1662 * 1663 * Return: vm_fault_t value. 1664 */ 1665 vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn, 1666 bool write) 1667 { 1668 unsigned long addr = vmf->address & PMD_MASK; 1669 struct vm_area_struct *vma = vmf->vma; 1670 pgprot_t pgprot = vma->vm_page_prot; 1671 struct folio_or_pfn fop = { 1672 .pfn = pfn, 1673 }; 1674 1675 /* 1676 * If we had pmd_special, we could avoid all these restrictions, 1677 * but we need to be consistent with PTEs and architectures that 1678 * can't support a 'special' bit. 1679 */ 1680 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1681 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1682 (VM_PFNMAP|VM_MIXEDMAP)); 1683 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1684 1685 pfnmap_setup_cachemode_pfn(pfn, &pgprot); 1686 1687 return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write); 1688 } 1689 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd); 1690 1691 vm_fault_t vmf_insert_folio_pmd(struct vm_fault *vmf, struct folio *folio, 1692 bool write) 1693 { 1694 struct vm_area_struct *vma = vmf->vma; 1695 unsigned long addr = vmf->address & PMD_MASK; 1696 struct folio_or_pfn fop = { 1697 .folio = folio, 1698 .is_folio = true, 1699 }; 1700 1701 if (WARN_ON_ONCE(folio_order(folio) != PMD_ORDER)) 1702 return VM_FAULT_SIGBUS; 1703 1704 return insert_pmd(vma, addr, vmf->pmd, fop, vma->vm_page_prot, write); 1705 } 1706 EXPORT_SYMBOL_GPL(vmf_insert_folio_pmd); 1707 1708 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 1709 static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma) 1710 { 1711 if (likely(vma->vm_flags & VM_WRITE)) 1712 pud = pud_mkwrite(pud); 1713 return pud; 1714 } 1715 1716 static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr, 1717 pud_t *pud, struct folio_or_pfn fop, pgprot_t prot, bool write) 1718 { 1719 struct mm_struct *mm = vma->vm_mm; 1720 spinlock_t *ptl; 1721 pud_t entry; 1722 1723 if (addr < vma->vm_start || addr >= vma->vm_end) 1724 return VM_FAULT_SIGBUS; 1725 1726 ptl = pud_lock(mm, pud); 1727 if (!pud_none(*pud)) { 1728 const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) : 1729 fop.pfn; 1730 1731 if (write) { 1732 if (WARN_ON_ONCE(pud_pfn(*pud) != pfn)) 1733 goto out_unlock; 1734 entry = pud_mkyoung(*pud); 1735 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma); 1736 if (pudp_set_access_flags(vma, addr, pud, entry, 1)) 1737 update_mmu_cache_pud(vma, addr, pud); 1738 } 1739 goto out_unlock; 1740 } 1741 1742 if (fop.is_folio) { 1743 entry = folio_mk_pud(fop.folio, vma->vm_page_prot); 1744 1745 folio_get(fop.folio); 1746 folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma); 1747 add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR); 1748 } else { 1749 entry = pud_mkhuge(pfn_pud(fop.pfn, prot)); 1750 entry = pud_mkspecial(entry); 1751 } 1752 if (write) { 1753 entry = pud_mkyoung(pud_mkdirty(entry)); 1754 entry = maybe_pud_mkwrite(entry, vma); 1755 } 1756 set_pud_at(mm, addr, pud, entry); 1757 update_mmu_cache_pud(vma, addr, pud); 1758 out_unlock: 1759 spin_unlock(ptl); 1760 return VM_FAULT_NOPAGE; 1761 } 1762 1763 /** 1764 * vmf_insert_pfn_pud - insert a pud size pfn 1765 * @vmf: Structure describing the fault 1766 * @pfn: pfn to insert 1767 * @write: whether it's a write fault 1768 * 1769 * Insert a pud size pfn. See vmf_insert_pfn() for additional info. 1770 * 1771 * Return: vm_fault_t value. 1772 */ 1773 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn, 1774 bool write) 1775 { 1776 unsigned long addr = vmf->address & PUD_MASK; 1777 struct vm_area_struct *vma = vmf->vma; 1778 pgprot_t pgprot = vma->vm_page_prot; 1779 struct folio_or_pfn fop = { 1780 .pfn = pfn, 1781 }; 1782 1783 /* 1784 * If we had pud_special, we could avoid all these restrictions, 1785 * but we need to be consistent with PTEs and architectures that 1786 * can't support a 'special' bit. 1787 */ 1788 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); 1789 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == 1790 (VM_PFNMAP|VM_MIXEDMAP)); 1791 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); 1792 1793 pfnmap_setup_cachemode_pfn(pfn, &pgprot); 1794 1795 return insert_pud(vma, addr, vmf->pud, fop, pgprot, write); 1796 } 1797 EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud); 1798 1799 /** 1800 * vmf_insert_folio_pud - insert a pud size folio mapped by a pud entry 1801 * @vmf: Structure describing the fault 1802 * @folio: folio to insert 1803 * @write: whether it's a write fault 1804 * 1805 * Return: vm_fault_t value. 1806 */ 1807 vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf, struct folio *folio, 1808 bool write) 1809 { 1810 struct vm_area_struct *vma = vmf->vma; 1811 unsigned long addr = vmf->address & PUD_MASK; 1812 struct folio_or_pfn fop = { 1813 .folio = folio, 1814 .is_folio = true, 1815 }; 1816 1817 if (WARN_ON_ONCE(folio_order(folio) != PUD_ORDER)) 1818 return VM_FAULT_SIGBUS; 1819 1820 return insert_pud(vma, addr, vmf->pud, fop, vma->vm_page_prot, write); 1821 } 1822 EXPORT_SYMBOL_GPL(vmf_insert_folio_pud); 1823 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 1824 1825 /** 1826 * touch_pmd - Mark page table pmd entry as accessed and dirty (for write) 1827 * @vma: The VMA covering @addr 1828 * @addr: The virtual address 1829 * @pmd: pmd pointer into the page table mapping @addr 1830 * @write: Whether it's a write access 1831 * 1832 * Return: whether the pmd entry is changed 1833 */ 1834 bool touch_pmd(struct vm_area_struct *vma, unsigned long addr, 1835 pmd_t *pmd, bool write) 1836 { 1837 pmd_t entry; 1838 1839 entry = pmd_mkyoung(*pmd); 1840 if (write) 1841 entry = pmd_mkdirty(entry); 1842 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK, 1843 pmd, entry, write)) { 1844 update_mmu_cache_pmd(vma, addr, pmd); 1845 return true; 1846 } 1847 1848 return false; 1849 } 1850 1851 static void copy_huge_non_present_pmd( 1852 struct mm_struct *dst_mm, struct mm_struct *src_mm, 1853 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1854 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 1855 pmd_t pmd, pgtable_t pgtable) 1856 { 1857 softleaf_t entry = softleaf_from_pmd(pmd); 1858 struct folio *src_folio; 1859 1860 VM_WARN_ON_ONCE(!pmd_is_valid_softleaf(pmd)); 1861 1862 if (softleaf_is_migration_write(entry) || 1863 softleaf_is_migration_read_exclusive(entry)) { 1864 entry = make_readable_migration_entry(swp_offset(entry)); 1865 pmd = swp_entry_to_pmd(entry); 1866 if (pmd_swp_soft_dirty(*src_pmd)) 1867 pmd = pmd_swp_mksoft_dirty(pmd); 1868 if (pmd_swp_uffd_wp(*src_pmd)) 1869 pmd = pmd_swp_mkuffd_wp(pmd); 1870 set_pmd_at(src_mm, addr, src_pmd, pmd); 1871 } else if (softleaf_is_device_private(entry)) { 1872 /* 1873 * For device private entries, since there are no 1874 * read exclusive entries, writable = !readable 1875 */ 1876 if (softleaf_is_device_private_write(entry)) { 1877 entry = make_readable_device_private_entry(swp_offset(entry)); 1878 pmd = swp_entry_to_pmd(entry); 1879 1880 if (pmd_swp_soft_dirty(*src_pmd)) 1881 pmd = pmd_swp_mksoft_dirty(pmd); 1882 if (pmd_swp_uffd_wp(*src_pmd)) 1883 pmd = pmd_swp_mkuffd_wp(pmd); 1884 set_pmd_at(src_mm, addr, src_pmd, pmd); 1885 } 1886 1887 src_folio = softleaf_to_folio(entry); 1888 VM_WARN_ON(!folio_test_large(src_folio)); 1889 1890 folio_get(src_folio); 1891 /* 1892 * folio_try_dup_anon_rmap_pmd does not fail for 1893 * device private entries. 1894 */ 1895 folio_try_dup_anon_rmap_pmd(src_folio, &src_folio->page, 1896 dst_vma, src_vma); 1897 } 1898 1899 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); 1900 mm_inc_nr_ptes(dst_mm); 1901 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); 1902 if (!userfaultfd_wp(dst_vma)) 1903 pmd = pmd_swp_clear_uffd_wp(pmd); 1904 set_pmd_at(dst_mm, addr, dst_pmd, pmd); 1905 } 1906 1907 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, 1908 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 1909 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) 1910 { 1911 spinlock_t *dst_ptl, *src_ptl; 1912 struct page *src_page; 1913 struct folio *src_folio; 1914 pmd_t pmd; 1915 pgtable_t pgtable = NULL; 1916 int ret = -ENOMEM; 1917 1918 pmd = pmdp_get_lockless(src_pmd); 1919 if (unlikely(pmd_present(pmd) && pmd_special(pmd) && 1920 !is_huge_zero_pmd(pmd))) { 1921 dst_ptl = pmd_lock(dst_mm, dst_pmd); 1922 src_ptl = pmd_lockptr(src_mm, src_pmd); 1923 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1924 /* 1925 * No need to recheck the pmd, it can't change with write 1926 * mmap lock held here. 1927 * 1928 * Meanwhile, making sure it's not a CoW VMA with writable 1929 * mapping, otherwise it means either the anon page wrongly 1930 * applied special bit, or we made the PRIVATE mapping be 1931 * able to wrongly write to the backend MMIO. 1932 */ 1933 VM_WARN_ON_ONCE(is_cow_mapping(src_vma->vm_flags) && pmd_write(pmd)); 1934 goto set_pmd; 1935 } 1936 1937 /* Skip if can be re-fill on fault */ 1938 if (!vma_is_anonymous(dst_vma)) 1939 return 0; 1940 1941 pgtable = pte_alloc_one(dst_mm); 1942 if (unlikely(!pgtable)) 1943 goto out; 1944 1945 dst_ptl = pmd_lock(dst_mm, dst_pmd); 1946 src_ptl = pmd_lockptr(src_mm, src_pmd); 1947 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 1948 1949 ret = -EAGAIN; 1950 pmd = *src_pmd; 1951 1952 if (unlikely(thp_migration_supported() && 1953 pmd_is_valid_softleaf(pmd))) { 1954 copy_huge_non_present_pmd(dst_mm, src_mm, dst_pmd, src_pmd, addr, 1955 dst_vma, src_vma, pmd, pgtable); 1956 ret = 0; 1957 goto out_unlock; 1958 } 1959 1960 if (unlikely(!pmd_trans_huge(pmd))) { 1961 pte_free(dst_mm, pgtable); 1962 goto out_unlock; 1963 } 1964 /* 1965 * When page table lock is held, the huge zero pmd should not be 1966 * under splitting since we don't split the page itself, only pmd to 1967 * a page table. 1968 */ 1969 if (is_huge_zero_pmd(pmd)) { 1970 /* 1971 * mm_get_huge_zero_folio() will never allocate a new 1972 * folio here, since we already have a zero page to 1973 * copy. It just takes a reference. 1974 */ 1975 mm_get_huge_zero_folio(dst_mm); 1976 goto out_zero_page; 1977 } 1978 1979 src_page = pmd_page(pmd); 1980 VM_BUG_ON_PAGE(!PageHead(src_page), src_page); 1981 src_folio = page_folio(src_page); 1982 1983 folio_get(src_folio); 1984 if (unlikely(folio_try_dup_anon_rmap_pmd(src_folio, src_page, dst_vma, src_vma))) { 1985 /* Page maybe pinned: split and retry the fault on PTEs. */ 1986 folio_put(src_folio); 1987 pte_free(dst_mm, pgtable); 1988 spin_unlock(src_ptl); 1989 spin_unlock(dst_ptl); 1990 __split_huge_pmd(src_vma, src_pmd, addr, false); 1991 return -EAGAIN; 1992 } 1993 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); 1994 out_zero_page: 1995 mm_inc_nr_ptes(dst_mm); 1996 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); 1997 pmdp_set_wrprotect(src_mm, addr, src_pmd); 1998 if (!userfaultfd_wp(dst_vma)) 1999 pmd = pmd_clear_uffd_wp(pmd); 2000 pmd = pmd_wrprotect(pmd); 2001 set_pmd: 2002 pmd = pmd_mkold(pmd); 2003 set_pmd_at(dst_mm, addr, dst_pmd, pmd); 2004 2005 ret = 0; 2006 out_unlock: 2007 spin_unlock(src_ptl); 2008 spin_unlock(dst_ptl); 2009 out: 2010 return ret; 2011 } 2012 2013 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 2014 void touch_pud(struct vm_area_struct *vma, unsigned long addr, 2015 pud_t *pud, bool write) 2016 { 2017 pud_t _pud; 2018 2019 _pud = pud_mkyoung(*pud); 2020 if (write) 2021 _pud = pud_mkdirty(_pud); 2022 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK, 2023 pud, _pud, write)) 2024 update_mmu_cache_pud(vma, addr, pud); 2025 } 2026 2027 int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, 2028 pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 2029 struct vm_area_struct *vma) 2030 { 2031 spinlock_t *dst_ptl, *src_ptl; 2032 pud_t pud; 2033 int ret; 2034 2035 dst_ptl = pud_lock(dst_mm, dst_pud); 2036 src_ptl = pud_lockptr(src_mm, src_pud); 2037 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 2038 2039 ret = -EAGAIN; 2040 pud = *src_pud; 2041 if (unlikely(!pud_trans_huge(pud))) 2042 goto out_unlock; 2043 2044 /* 2045 * TODO: once we support anonymous pages, use 2046 * folio_try_dup_anon_rmap_*() and split if duplicating fails. 2047 */ 2048 if (is_cow_mapping(vma->vm_flags) && pud_write(pud)) { 2049 pudp_set_wrprotect(src_mm, addr, src_pud); 2050 pud = pud_wrprotect(pud); 2051 } 2052 pud = pud_mkold(pud); 2053 set_pud_at(dst_mm, addr, dst_pud, pud); 2054 2055 ret = 0; 2056 out_unlock: 2057 spin_unlock(src_ptl); 2058 spin_unlock(dst_ptl); 2059 return ret; 2060 } 2061 2062 void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) 2063 { 2064 bool write = vmf->flags & FAULT_FLAG_WRITE; 2065 2066 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud); 2067 if (unlikely(!pud_same(*vmf->pud, orig_pud))) 2068 goto unlock; 2069 2070 touch_pud(vmf->vma, vmf->address, vmf->pud, write); 2071 unlock: 2072 spin_unlock(vmf->ptl); 2073 } 2074 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 2075 2076 bool huge_pmd_set_accessed(struct vm_fault *vmf) 2077 { 2078 bool write = vmf->flags & FAULT_FLAG_WRITE; 2079 2080 if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd))) 2081 return false; 2082 2083 return touch_pmd(vmf->vma, vmf->address, vmf->pmd, write); 2084 } 2085 2086 static vm_fault_t do_huge_zero_wp_pmd(struct vm_fault *vmf) 2087 { 2088 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 2089 struct vm_area_struct *vma = vmf->vma; 2090 struct mmu_notifier_range range; 2091 struct folio *folio; 2092 vm_fault_t ret = 0; 2093 2094 folio = vma_alloc_anon_folio_pmd(vma, vmf->address); 2095 if (unlikely(!folio)) 2096 return VM_FAULT_FALLBACK; 2097 2098 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, haddr, 2099 haddr + HPAGE_PMD_SIZE); 2100 mmu_notifier_invalidate_range_start(&range); 2101 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 2102 if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd))) 2103 goto release; 2104 ret = check_stable_address_space(vma->vm_mm); 2105 if (ret) 2106 goto release; 2107 (void)pmdp_huge_clear_flush(vma, haddr, vmf->pmd); 2108 map_anon_folio_pmd_pf(folio, vmf->pmd, vma, haddr); 2109 goto unlock; 2110 release: 2111 folio_put(folio); 2112 unlock: 2113 spin_unlock(vmf->ptl); 2114 mmu_notifier_invalidate_range_end(&range); 2115 return ret; 2116 } 2117 2118 vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) 2119 { 2120 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 2121 struct vm_area_struct *vma = vmf->vma; 2122 struct folio *folio; 2123 struct page *page; 2124 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 2125 pmd_t orig_pmd = vmf->orig_pmd; 2126 2127 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd); 2128 VM_BUG_ON_VMA(!vma->anon_vma, vma); 2129 2130 if (is_huge_zero_pmd(orig_pmd)) { 2131 vm_fault_t ret = do_huge_zero_wp_pmd(vmf); 2132 2133 if (!(ret & VM_FAULT_FALLBACK)) 2134 return ret; 2135 2136 /* Fallback to splitting PMD if THP cannot be allocated */ 2137 goto fallback; 2138 } 2139 2140 spin_lock(vmf->ptl); 2141 2142 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) { 2143 spin_unlock(vmf->ptl); 2144 return 0; 2145 } 2146 2147 page = pmd_page(orig_pmd); 2148 folio = page_folio(page); 2149 VM_BUG_ON_PAGE(!PageHead(page), page); 2150 2151 /* Early check when only holding the PT lock. */ 2152 if (PageAnonExclusive(page)) 2153 goto reuse; 2154 2155 if (!folio_trylock(folio)) { 2156 folio_get(folio); 2157 spin_unlock(vmf->ptl); 2158 folio_lock(folio); 2159 spin_lock(vmf->ptl); 2160 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) { 2161 spin_unlock(vmf->ptl); 2162 folio_unlock(folio); 2163 folio_put(folio); 2164 return 0; 2165 } 2166 folio_put(folio); 2167 } 2168 2169 /* Recheck after temporarily dropping the PT lock. */ 2170 if (PageAnonExclusive(page)) { 2171 folio_unlock(folio); 2172 goto reuse; 2173 } 2174 2175 /* 2176 * See do_wp_page(): we can only reuse the folio exclusively if 2177 * there are no additional references. Note that we always drain 2178 * the LRU cache immediately after adding a THP. 2179 */ 2180 if (folio_ref_count(folio) > 2181 1 + folio_test_swapcache(folio) * folio_nr_pages(folio)) 2182 goto unlock_fallback; 2183 if (folio_test_swapcache(folio)) 2184 folio_free_swap(folio); 2185 if (folio_ref_count(folio) == 1) { 2186 pmd_t entry; 2187 2188 folio_move_anon_rmap(folio, vma); 2189 SetPageAnonExclusive(page); 2190 folio_unlock(folio); 2191 reuse: 2192 if (unlikely(unshare)) { 2193 spin_unlock(vmf->ptl); 2194 return 0; 2195 } 2196 entry = pmd_mkyoung(orig_pmd); 2197 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); 2198 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1)) 2199 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); 2200 spin_unlock(vmf->ptl); 2201 return 0; 2202 } 2203 2204 unlock_fallback: 2205 folio_unlock(folio); 2206 spin_unlock(vmf->ptl); 2207 fallback: 2208 __split_huge_pmd(vma, vmf->pmd, vmf->address, false); 2209 return VM_FAULT_FALLBACK; 2210 } 2211 2212 static inline bool can_change_pmd_writable(struct vm_area_struct *vma, 2213 unsigned long addr, pmd_t pmd) 2214 { 2215 struct page *page; 2216 2217 if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) 2218 return false; 2219 2220 /* Don't touch entries that are not even readable (NUMA hinting). */ 2221 if (pmd_protnone(pmd)) 2222 return false; 2223 2224 /* Do we need write faults for softdirty tracking? */ 2225 if (pmd_needs_soft_dirty_wp(vma, pmd)) 2226 return false; 2227 2228 /* Do we need write faults for uffd-wp tracking? */ 2229 if (userfaultfd_huge_pmd_wp(vma, pmd)) 2230 return false; 2231 2232 if (!(vma->vm_flags & VM_SHARED)) { 2233 /* See can_change_pte_writable(). */ 2234 page = vm_normal_page_pmd(vma, addr, pmd); 2235 return page && PageAnon(page) && PageAnonExclusive(page); 2236 } 2237 2238 /* See can_change_pte_writable(). */ 2239 return pmd_dirty(pmd); 2240 } 2241 2242 /* NUMA hinting page fault entry point for trans huge pmds */ 2243 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) 2244 { 2245 struct vm_area_struct *vma = vmf->vma; 2246 struct folio *folio; 2247 unsigned long haddr = vmf->address & HPAGE_PMD_MASK; 2248 int nid = NUMA_NO_NODE; 2249 int target_nid, last_cpupid; 2250 pmd_t pmd, old_pmd; 2251 bool writable = false; 2252 int flags = 0; 2253 2254 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 2255 old_pmd = pmdp_get(vmf->pmd); 2256 2257 if (unlikely(!pmd_same(old_pmd, vmf->orig_pmd))) { 2258 spin_unlock(vmf->ptl); 2259 return 0; 2260 } 2261 2262 pmd = pmd_modify(old_pmd, vma->vm_page_prot); 2263 2264 /* 2265 * Detect now whether the PMD could be writable; this information 2266 * is only valid while holding the PT lock. 2267 */ 2268 writable = pmd_write(pmd); 2269 if (!writable && vma_wants_manual_pte_write_upgrade(vma) && 2270 can_change_pmd_writable(vma, vmf->address, pmd)) 2271 writable = true; 2272 2273 folio = vm_normal_folio_pmd(vma, haddr, pmd); 2274 if (!folio) 2275 goto out_map; 2276 2277 nid = folio_nid(folio); 2278 2279 target_nid = numa_migrate_check(folio, vmf, haddr, &flags, writable, 2280 &last_cpupid); 2281 if (target_nid == NUMA_NO_NODE) 2282 goto out_map; 2283 if (migrate_misplaced_folio_prepare(folio, vma, target_nid)) { 2284 flags |= TNF_MIGRATE_FAIL; 2285 goto out_map; 2286 } 2287 /* The folio is isolated and isolation code holds a folio reference. */ 2288 spin_unlock(vmf->ptl); 2289 writable = false; 2290 2291 if (!migrate_misplaced_folio(folio, target_nid)) { 2292 flags |= TNF_MIGRATED; 2293 nid = target_nid; 2294 task_numa_fault(last_cpupid, nid, HPAGE_PMD_NR, flags); 2295 return 0; 2296 } 2297 2298 flags |= TNF_MIGRATE_FAIL; 2299 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); 2300 if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd))) { 2301 spin_unlock(vmf->ptl); 2302 return 0; 2303 } 2304 out_map: 2305 /* Restore the PMD */ 2306 pmd = pmd_modify(pmdp_get(vmf->pmd), vma->vm_page_prot); 2307 pmd = pmd_mkyoung(pmd); 2308 if (writable) 2309 pmd = pmd_mkwrite(pmd, vma); 2310 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd); 2311 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); 2312 spin_unlock(vmf->ptl); 2313 2314 if (nid != NUMA_NO_NODE) 2315 task_numa_fault(last_cpupid, nid, HPAGE_PMD_NR, flags); 2316 return 0; 2317 } 2318 2319 /* 2320 * Return true if we do MADV_FREE successfully on entire pmd page. 2321 * Otherwise, return false. 2322 */ 2323 bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, 2324 pmd_t *pmd, unsigned long addr, unsigned long next) 2325 { 2326 spinlock_t *ptl; 2327 pmd_t orig_pmd; 2328 struct folio *folio; 2329 struct mm_struct *mm = tlb->mm; 2330 bool ret = false; 2331 2332 tlb_change_page_size(tlb, HPAGE_PMD_SIZE); 2333 2334 ptl = pmd_trans_huge_lock(pmd, vma); 2335 if (!ptl) 2336 goto out_unlocked; 2337 2338 orig_pmd = *pmd; 2339 if (is_huge_zero_pmd(orig_pmd)) 2340 goto out; 2341 2342 if (unlikely(!pmd_present(orig_pmd))) { 2343 VM_BUG_ON(thp_migration_supported() && 2344 !pmd_is_migration_entry(orig_pmd)); 2345 goto out; 2346 } 2347 2348 folio = pmd_folio(orig_pmd); 2349 /* 2350 * If other processes are mapping this folio, we couldn't discard 2351 * the folio unless they all do MADV_FREE so let's skip the folio. 2352 */ 2353 if (folio_maybe_mapped_shared(folio)) 2354 goto out; 2355 2356 if (!folio_trylock(folio)) 2357 goto out; 2358 2359 /* 2360 * If user want to discard part-pages of THP, split it so MADV_FREE 2361 * will deactivate only them. 2362 */ 2363 if (next - addr != HPAGE_PMD_SIZE) { 2364 folio_get(folio); 2365 spin_unlock(ptl); 2366 split_folio(folio); 2367 folio_unlock(folio); 2368 folio_put(folio); 2369 goto out_unlocked; 2370 } 2371 2372 if (folio_test_dirty(folio)) 2373 folio_clear_dirty(folio); 2374 folio_unlock(folio); 2375 2376 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) { 2377 pmdp_invalidate(vma, addr, pmd); 2378 orig_pmd = pmd_mkold(orig_pmd); 2379 orig_pmd = pmd_mkclean(orig_pmd); 2380 2381 set_pmd_at(mm, addr, pmd, orig_pmd); 2382 tlb_remove_pmd_tlb_entry(tlb, pmd, addr); 2383 } 2384 2385 folio_mark_lazyfree(folio); 2386 ret = true; 2387 out: 2388 spin_unlock(ptl); 2389 out_unlocked: 2390 return ret; 2391 } 2392 2393 static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd) 2394 { 2395 pgtable_t pgtable; 2396 2397 pgtable = pgtable_trans_huge_withdraw(mm, pmd); 2398 pte_free(mm, pgtable); 2399 mm_dec_nr_ptes(mm); 2400 } 2401 2402 static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma, 2403 pmd_t pmdval, struct folio *folio, bool is_present) 2404 { 2405 const bool is_device_private = folio_is_device_private(folio); 2406 2407 /* Present and device private folios are rmappable. */ 2408 if (is_present || is_device_private) 2409 folio_remove_rmap_pmd(folio, &folio->page, vma); 2410 2411 if (folio_test_anon(folio)) { 2412 add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); 2413 } else { 2414 add_mm_counter(mm, mm_counter_file(folio), 2415 -HPAGE_PMD_NR); 2416 2417 if (is_present && pmd_young(pmdval) && 2418 likely(vma_has_recency(vma))) 2419 folio_mark_accessed(folio); 2420 } 2421 2422 /* Device private folios are pinned. */ 2423 if (is_device_private) 2424 folio_put(folio); 2425 } 2426 2427 static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *vma, 2428 unsigned long addr, pmd_t pmdval, bool is_present) 2429 { 2430 if (is_present) 2431 return vm_normal_folio_pmd(vma, addr, pmdval); 2432 2433 if (!thp_migration_supported()) 2434 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); 2435 return pmd_to_softleaf_folio(pmdval); 2436 } 2437 2438 static bool has_deposited_pgtable(struct vm_area_struct *vma, pmd_t pmdval, 2439 struct folio *folio) 2440 { 2441 /* Some architectures require unconditional depositing. */ 2442 if (arch_needs_pgtable_deposit()) 2443 return true; 2444 2445 /* 2446 * Huge zero always deposited except for DAX which handles itself, see 2447 * set_huge_zero_folio(). 2448 */ 2449 if (is_huge_zero_pmd(pmdval)) 2450 return !vma_is_dax(vma); 2451 2452 /* 2453 * Otherwise, only anonymous folios are deposited, see 2454 * __do_huge_pmd_anonymous_page(). 2455 */ 2456 return folio && folio_test_anon(folio); 2457 } 2458 2459 /** 2460 * zap_huge_pmd - Zap a huge THP which is of PMD size. 2461 * @tlb: The MMU gather TLB state associated with the operation. 2462 * @vma: The VMA containing the range to zap. 2463 * @pmd: A pointer to the leaf PMD entry. 2464 * @addr: The virtual address for the range to zap. 2465 * 2466 * Returns: %true on success, %false otherwise. 2467 */ 2468 bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, 2469 pmd_t *pmd, unsigned long addr) 2470 { 2471 struct mm_struct *mm = tlb->mm; 2472 struct folio *folio = NULL; 2473 bool is_present = false; 2474 bool has_deposit; 2475 spinlock_t *ptl; 2476 pmd_t orig_pmd; 2477 2478 tlb_change_page_size(tlb, HPAGE_PMD_SIZE); 2479 2480 ptl = __pmd_trans_huge_lock(pmd, vma); 2481 if (!ptl) 2482 return false; 2483 /* 2484 * For architectures like ppc64 we look at deposited pgtable 2485 * when calling pmdp_huge_get_and_clear. So do the 2486 * pgtable_trans_huge_withdraw after finishing pmdp related 2487 * operations. 2488 */ 2489 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd, 2490 tlb->fullmm); 2491 arch_check_zapped_pmd(vma, orig_pmd); 2492 tlb_remove_pmd_tlb_entry(tlb, pmd, addr); 2493 2494 is_present = pmd_present(orig_pmd); 2495 folio = normal_or_softleaf_folio_pmd(vma, addr, orig_pmd, is_present); 2496 has_deposit = has_deposited_pgtable(vma, orig_pmd, folio); 2497 if (folio) 2498 zap_huge_pmd_folio(mm, vma, orig_pmd, folio, is_present); 2499 if (has_deposit) 2500 zap_deposited_table(mm, pmd); 2501 2502 spin_unlock(ptl); 2503 if (is_present && folio) 2504 tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); 2505 return true; 2506 } 2507 2508 #ifndef pmd_move_must_withdraw 2509 static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl, 2510 spinlock_t *old_pmd_ptl, 2511 struct vm_area_struct *vma) 2512 { 2513 /* 2514 * With split pmd lock we also need to move preallocated 2515 * PTE page table if new_pmd is on different PMD page table. 2516 * 2517 * We also don't deposit and withdraw tables for file pages. 2518 */ 2519 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma); 2520 } 2521 #endif 2522 2523 static pmd_t move_soft_dirty_pmd(pmd_t pmd) 2524 { 2525 if (pgtable_supports_soft_dirty()) { 2526 if (unlikely(pmd_is_migration_entry(pmd))) 2527 pmd = pmd_swp_mksoft_dirty(pmd); 2528 else if (pmd_present(pmd)) 2529 pmd = pmd_mksoft_dirty(pmd); 2530 } 2531 2532 return pmd; 2533 } 2534 2535 static pmd_t clear_uffd_wp_pmd(pmd_t pmd) 2536 { 2537 if (pmd_none(pmd)) 2538 return pmd; 2539 if (pmd_present(pmd)) 2540 pmd = pmd_clear_uffd_wp(pmd); 2541 else 2542 pmd = pmd_swp_clear_uffd_wp(pmd); 2543 2544 return pmd; 2545 } 2546 2547 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, 2548 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd) 2549 { 2550 spinlock_t *old_ptl, *new_ptl; 2551 pmd_t pmd; 2552 struct mm_struct *mm = vma->vm_mm; 2553 bool force_flush = false; 2554 2555 /* 2556 * The destination pmd shouldn't be established, free_pgtables() 2557 * should have released it; but move_page_tables() might have already 2558 * inserted a page table, if racing against shmem/file collapse. 2559 */ 2560 if (!pmd_none(*new_pmd)) { 2561 VM_BUG_ON(pmd_trans_huge(*new_pmd)); 2562 return false; 2563 } 2564 2565 /* 2566 * We don't have to worry about the ordering of src and dst 2567 * ptlocks because exclusive mmap_lock prevents deadlock. 2568 */ 2569 old_ptl = __pmd_trans_huge_lock(old_pmd, vma); 2570 if (old_ptl) { 2571 new_ptl = pmd_lockptr(mm, new_pmd); 2572 if (new_ptl != old_ptl) 2573 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 2574 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); 2575 if (pmd_present(pmd)) 2576 force_flush = true; 2577 VM_BUG_ON(!pmd_none(*new_pmd)); 2578 2579 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) { 2580 pgtable_t pgtable; 2581 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd); 2582 pgtable_trans_huge_deposit(mm, new_pmd, pgtable); 2583 } 2584 pmd = move_soft_dirty_pmd(pmd); 2585 if (vma_has_uffd_without_event_remap(vma)) 2586 pmd = clear_uffd_wp_pmd(pmd); 2587 set_pmd_at(mm, new_addr, new_pmd, pmd); 2588 if (force_flush) 2589 flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE); 2590 if (new_ptl != old_ptl) 2591 spin_unlock(new_ptl); 2592 spin_unlock(old_ptl); 2593 return true; 2594 } 2595 return false; 2596 } 2597 2598 static void change_non_present_huge_pmd(struct mm_struct *mm, 2599 unsigned long addr, pmd_t *pmd, bool uffd_wp, 2600 bool uffd_wp_resolve) 2601 { 2602 softleaf_t entry = softleaf_from_pmd(*pmd); 2603 const struct folio *folio = softleaf_to_folio(entry); 2604 pmd_t newpmd; 2605 2606 VM_WARN_ON(!pmd_is_valid_softleaf(*pmd)); 2607 if (softleaf_is_migration_write(entry)) { 2608 /* 2609 * A protection check is difficult so 2610 * just be safe and disable write 2611 */ 2612 if (folio_test_anon(folio)) 2613 entry = make_readable_exclusive_migration_entry(swp_offset(entry)); 2614 else 2615 entry = make_readable_migration_entry(swp_offset(entry)); 2616 newpmd = swp_entry_to_pmd(entry); 2617 if (pmd_swp_soft_dirty(*pmd)) 2618 newpmd = pmd_swp_mksoft_dirty(newpmd); 2619 } else if (softleaf_is_device_private_write(entry)) { 2620 entry = make_readable_device_private_entry(swp_offset(entry)); 2621 newpmd = swp_entry_to_pmd(entry); 2622 if (pmd_swp_uffd_wp(*pmd)) 2623 newpmd = pmd_swp_mkuffd_wp(newpmd); 2624 } else { 2625 newpmd = *pmd; 2626 } 2627 2628 if (uffd_wp) 2629 newpmd = pmd_swp_mkuffd_wp(newpmd); 2630 else if (uffd_wp_resolve) 2631 newpmd = pmd_swp_clear_uffd_wp(newpmd); 2632 if (!pmd_same(*pmd, newpmd)) 2633 set_pmd_at(mm, addr, pmd, newpmd); 2634 } 2635 2636 /* 2637 * Returns 2638 * - 0 if PMD could not be locked 2639 * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary 2640 * or if prot_numa but THP migration is not supported 2641 * - HPAGE_PMD_NR if protections changed and TLB flush necessary 2642 */ 2643 int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, 2644 pmd_t *pmd, unsigned long addr, pgprot_t newprot, 2645 unsigned long cp_flags) 2646 { 2647 struct mm_struct *mm = vma->vm_mm; 2648 spinlock_t *ptl; 2649 pmd_t oldpmd, entry; 2650 bool prot_numa = cp_flags & MM_CP_PROT_NUMA; 2651 bool uffd_wp = cp_flags & MM_CP_UFFD_WP; 2652 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; 2653 int ret = 1; 2654 2655 tlb_change_page_size(tlb, HPAGE_PMD_SIZE); 2656 2657 if (prot_numa && !thp_migration_supported()) 2658 return 1; 2659 2660 ptl = __pmd_trans_huge_lock(pmd, vma); 2661 if (!ptl) 2662 return 0; 2663 2664 if (thp_migration_supported() && pmd_is_valid_softleaf(*pmd)) { 2665 change_non_present_huge_pmd(mm, addr, pmd, uffd_wp, 2666 uffd_wp_resolve); 2667 goto unlock; 2668 } 2669 2670 if (prot_numa) { 2671 2672 /* 2673 * Avoid trapping faults against the zero page. The read-only 2674 * data is likely to be read-cached on the local CPU and 2675 * local/remote hits to the zero page are not interesting. 2676 */ 2677 if (is_huge_zero_pmd(*pmd)) 2678 goto unlock; 2679 2680 if (pmd_protnone(*pmd)) 2681 goto unlock; 2682 2683 if (!folio_can_map_prot_numa(pmd_folio(*pmd), vma, 2684 vma_is_single_threaded_private(vma))) 2685 goto unlock; 2686 } 2687 /* 2688 * In case prot_numa, we are under mmap_read_lock(mm). It's critical 2689 * to not clear pmd intermittently to avoid race with MADV_DONTNEED 2690 * which is also under mmap_read_lock(mm): 2691 * 2692 * CPU0: CPU1: 2693 * change_huge_pmd(prot_numa=1) 2694 * pmdp_huge_get_and_clear_notify() 2695 * madvise_dontneed() 2696 * zap_pmd_range() 2697 * pmd_trans_huge(*pmd) == 0 (without ptl) 2698 * // skip the pmd 2699 * set_pmd_at(); 2700 * // pmd is re-established 2701 * 2702 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it 2703 * which may break userspace. 2704 * 2705 * pmdp_invalidate_ad() is required to make sure we don't miss 2706 * dirty/young flags set by hardware. 2707 */ 2708 oldpmd = pmdp_invalidate_ad(vma, addr, pmd); 2709 2710 entry = pmd_modify(oldpmd, newprot); 2711 if (uffd_wp) 2712 entry = pmd_mkuffd_wp(entry); 2713 else if (uffd_wp_resolve) 2714 /* 2715 * Leave the write bit to be handled by PF interrupt 2716 * handler, then things like COW could be properly 2717 * handled. 2718 */ 2719 entry = pmd_clear_uffd_wp(entry); 2720 2721 /* See change_pte_range(). */ 2722 if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) && 2723 can_change_pmd_writable(vma, addr, entry)) 2724 entry = pmd_mkwrite(entry, vma); 2725 2726 ret = HPAGE_PMD_NR; 2727 set_pmd_at(mm, addr, pmd, entry); 2728 2729 if (huge_pmd_needs_flush(oldpmd, entry)) 2730 tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE); 2731 unlock: 2732 spin_unlock(ptl); 2733 return ret; 2734 } 2735 2736 /* 2737 * Returns: 2738 * 2739 * - 0: if pud leaf changed from under us 2740 * - 1: if pud can be skipped 2741 * - HPAGE_PUD_NR: if pud was successfully processed 2742 */ 2743 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 2744 int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, 2745 pud_t *pudp, unsigned long addr, pgprot_t newprot, 2746 unsigned long cp_flags) 2747 { 2748 struct mm_struct *mm = vma->vm_mm; 2749 pud_t oldpud, entry; 2750 spinlock_t *ptl; 2751 2752 tlb_change_page_size(tlb, HPAGE_PUD_SIZE); 2753 2754 /* NUMA balancing doesn't apply to dax */ 2755 if (cp_flags & MM_CP_PROT_NUMA) 2756 return 1; 2757 2758 /* 2759 * Huge entries on userfault-wp only works with anonymous, while we 2760 * don't have anonymous PUDs yet. 2761 */ 2762 if (WARN_ON_ONCE(cp_flags & MM_CP_UFFD_WP_ALL)) 2763 return 1; 2764 2765 ptl = __pud_trans_huge_lock(pudp, vma); 2766 if (!ptl) 2767 return 0; 2768 2769 /* 2770 * Can't clear PUD or it can race with concurrent zapping. See 2771 * change_huge_pmd(). 2772 */ 2773 oldpud = pudp_invalidate(vma, addr, pudp); 2774 entry = pud_modify(oldpud, newprot); 2775 set_pud_at(mm, addr, pudp, entry); 2776 tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE); 2777 2778 spin_unlock(ptl); 2779 return HPAGE_PUD_NR; 2780 } 2781 #endif 2782 2783 #ifdef CONFIG_USERFAULTFD 2784 /* 2785 * The PT lock for src_pmd and dst_vma/src_vma (for reading) are locked by 2786 * the caller, but it must return after releasing the page_table_lock. 2787 * Just move the page from src_pmd to dst_pmd if possible. 2788 * Return zero if succeeded in moving the page, -EAGAIN if it needs to be 2789 * repeated by the caller, or other errors in case of failure. 2790 */ 2791 int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval, 2792 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, 2793 unsigned long dst_addr, unsigned long src_addr) 2794 { 2795 pmd_t _dst_pmd, src_pmdval; 2796 struct page *src_page; 2797 struct folio *src_folio; 2798 spinlock_t *src_ptl, *dst_ptl; 2799 pgtable_t src_pgtable; 2800 struct mmu_notifier_range range; 2801 int err = 0; 2802 2803 src_pmdval = *src_pmd; 2804 src_ptl = pmd_lockptr(mm, src_pmd); 2805 2806 lockdep_assert_held(src_ptl); 2807 vma_assert_locked(src_vma); 2808 vma_assert_locked(dst_vma); 2809 2810 /* Sanity checks before the operation */ 2811 if (WARN_ON_ONCE(!pmd_none(dst_pmdval)) || WARN_ON_ONCE(src_addr & ~HPAGE_PMD_MASK) || 2812 WARN_ON_ONCE(dst_addr & ~HPAGE_PMD_MASK)) { 2813 spin_unlock(src_ptl); 2814 return -EINVAL; 2815 } 2816 2817 if (!pmd_trans_huge(src_pmdval)) { 2818 spin_unlock(src_ptl); 2819 if (pmd_is_migration_entry(src_pmdval)) { 2820 pmd_migration_entry_wait(mm, src_pmd); 2821 return -EAGAIN; 2822 } 2823 return -ENOENT; 2824 } 2825 2826 src_page = pmd_page(src_pmdval); 2827 2828 if (!is_huge_zero_pmd(src_pmdval)) { 2829 if (unlikely(!PageAnonExclusive(src_page))) { 2830 spin_unlock(src_ptl); 2831 return -EBUSY; 2832 } 2833 2834 src_folio = page_folio(src_page); 2835 folio_get(src_folio); 2836 } else 2837 src_folio = NULL; 2838 2839 spin_unlock(src_ptl); 2840 2841 flush_cache_range(src_vma, src_addr, src_addr + HPAGE_PMD_SIZE); 2842 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, src_addr, 2843 src_addr + HPAGE_PMD_SIZE); 2844 mmu_notifier_invalidate_range_start(&range); 2845 2846 if (src_folio) 2847 folio_lock(src_folio); 2848 2849 dst_ptl = pmd_lockptr(mm, dst_pmd); 2850 double_pt_lock(src_ptl, dst_ptl); 2851 if (unlikely(!pmd_same(*src_pmd, src_pmdval) || 2852 !pmd_same(*dst_pmd, dst_pmdval))) { 2853 err = -EAGAIN; 2854 goto unlock_ptls; 2855 } 2856 if (src_folio) { 2857 if (folio_maybe_dma_pinned(src_folio) || 2858 !PageAnonExclusive(&src_folio->page)) { 2859 err = -EBUSY; 2860 goto unlock_ptls; 2861 } 2862 2863 if (WARN_ON_ONCE(!folio_test_head(src_folio)) || 2864 WARN_ON_ONCE(!folio_test_anon(src_folio))) { 2865 err = -EBUSY; 2866 goto unlock_ptls; 2867 } 2868 2869 src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); 2870 /* Folio got pinned from under us. Put it back and fail the move. */ 2871 if (folio_maybe_dma_pinned(src_folio)) { 2872 set_pmd_at(mm, src_addr, src_pmd, src_pmdval); 2873 err = -EBUSY; 2874 goto unlock_ptls; 2875 } 2876 2877 folio_move_anon_rmap(src_folio, dst_vma); 2878 src_folio->index = linear_page_index(dst_vma, dst_addr); 2879 2880 _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot); 2881 /* Follow mremap() behavior and treat the entry dirty after the move */ 2882 _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma); 2883 } else { 2884 src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd); 2885 _dst_pmd = move_soft_dirty_pmd(src_pmdval); 2886 _dst_pmd = clear_uffd_wp_pmd(_dst_pmd); 2887 } 2888 set_pmd_at(mm, dst_addr, dst_pmd, _dst_pmd); 2889 2890 src_pgtable = pgtable_trans_huge_withdraw(mm, src_pmd); 2891 pgtable_trans_huge_deposit(mm, dst_pmd, src_pgtable); 2892 unlock_ptls: 2893 double_pt_unlock(src_ptl, dst_ptl); 2894 /* unblock rmap walks */ 2895 if (src_folio) 2896 folio_unlock(src_folio); 2897 mmu_notifier_invalidate_range_end(&range); 2898 if (src_folio) 2899 folio_put(src_folio); 2900 return err; 2901 } 2902 #endif /* CONFIG_USERFAULTFD */ 2903 2904 /* 2905 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise. 2906 * 2907 * Note that if it returns page table lock pointer, this routine returns without 2908 * unlocking page table lock. So callers must unlock it. 2909 */ 2910 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) 2911 { 2912 spinlock_t *ptl; 2913 2914 ptl = pmd_lock(vma->vm_mm, pmd); 2915 if (likely(pmd_is_huge(*pmd))) 2916 return ptl; 2917 spin_unlock(ptl); 2918 return NULL; 2919 } 2920 2921 /* 2922 * Returns page table lock pointer if a given pud maps a thp, NULL otherwise. 2923 * 2924 * Note that if it returns page table lock pointer, this routine returns without 2925 * unlocking page table lock. So callers must unlock it. 2926 */ 2927 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma) 2928 { 2929 spinlock_t *ptl; 2930 2931 ptl = pud_lock(vma->vm_mm, pud); 2932 if (likely(pud_trans_huge(*pud))) 2933 return ptl; 2934 spin_unlock(ptl); 2935 return NULL; 2936 } 2937 2938 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 2939 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, 2940 pud_t *pud, unsigned long addr) 2941 { 2942 spinlock_t *ptl; 2943 pud_t orig_pud; 2944 2945 ptl = __pud_trans_huge_lock(pud, vma); 2946 if (!ptl) 2947 return 0; 2948 2949 orig_pud = pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); 2950 arch_check_zapped_pud(vma, orig_pud); 2951 tlb_remove_pud_tlb_entry(tlb, pud, addr); 2952 if (vma_is_special_huge(vma)) { 2953 spin_unlock(ptl); 2954 /* No zero page support yet */ 2955 } else { 2956 struct page *page = NULL; 2957 struct folio *folio; 2958 2959 /* No support for anonymous PUD pages or migration yet */ 2960 VM_WARN_ON_ONCE(vma_is_anonymous(vma) || 2961 !pud_present(orig_pud)); 2962 2963 page = pud_page(orig_pud); 2964 folio = page_folio(page); 2965 folio_remove_rmap_pud(folio, page, vma); 2966 add_mm_counter(tlb->mm, mm_counter_file(folio), -HPAGE_PUD_NR); 2967 2968 spin_unlock(ptl); 2969 tlb_remove_page_size(tlb, page, HPAGE_PUD_SIZE); 2970 } 2971 return 1; 2972 } 2973 2974 static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud, 2975 unsigned long haddr) 2976 { 2977 struct folio *folio; 2978 struct page *page; 2979 pud_t old_pud; 2980 2981 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK); 2982 VM_BUG_ON_VMA(vma->vm_start > haddr, vma); 2983 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma); 2984 VM_BUG_ON(!pud_trans_huge(*pud)); 2985 2986 count_vm_event(THP_SPLIT_PUD); 2987 2988 old_pud = pudp_huge_clear_flush(vma, haddr, pud); 2989 2990 if (!vma_is_dax(vma)) 2991 return; 2992 2993 page = pud_page(old_pud); 2994 folio = page_folio(page); 2995 2996 if (!folio_test_dirty(folio) && pud_dirty(old_pud)) 2997 folio_mark_dirty(folio); 2998 if (!folio_test_referenced(folio) && pud_young(old_pud)) 2999 folio_set_referenced(folio); 3000 folio_remove_rmap_pud(folio, page, vma); 3001 add_mm_counter(vma->vm_mm, mm_counter_file(folio), 3002 -HPAGE_PUD_NR); 3003 folio_put(folio); 3004 } 3005 3006 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, 3007 unsigned long address) 3008 { 3009 spinlock_t *ptl; 3010 struct mmu_notifier_range range; 3011 3012 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 3013 address & HPAGE_PUD_MASK, 3014 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE); 3015 mmu_notifier_invalidate_range_start(&range); 3016 ptl = pud_lock(vma->vm_mm, pud); 3017 if (unlikely(!pud_trans_huge(*pud))) 3018 goto out; 3019 __split_huge_pud_locked(vma, pud, range.start); 3020 3021 out: 3022 spin_unlock(ptl); 3023 mmu_notifier_invalidate_range_end(&range); 3024 } 3025 #else 3026 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, 3027 unsigned long address) 3028 { 3029 } 3030 #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 3031 3032 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, 3033 unsigned long haddr, pmd_t *pmd) 3034 { 3035 struct mm_struct *mm = vma->vm_mm; 3036 pgtable_t pgtable; 3037 pmd_t _pmd, old_pmd; 3038 unsigned long addr; 3039 pte_t *pte; 3040 int i; 3041 3042 /* 3043 * Leave pmd empty until pte is filled note that it is fine to delay 3044 * notification until mmu_notifier_invalidate_range_end() as we are 3045 * replacing a zero pmd write protected page with a zero pte write 3046 * protected page. 3047 * 3048 * See Documentation/mm/mmu_notifier.rst 3049 */ 3050 old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd); 3051 3052 pgtable = pgtable_trans_huge_withdraw(mm, pmd); 3053 pmd_populate(mm, &_pmd, pgtable); 3054 3055 pte = pte_offset_map(&_pmd, haddr); 3056 VM_BUG_ON(!pte); 3057 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { 3058 pte_t entry; 3059 3060 entry = pfn_pte(zero_pfn(addr), vma->vm_page_prot); 3061 entry = pte_mkspecial(entry); 3062 if (pmd_uffd_wp(old_pmd)) 3063 entry = pte_mkuffd_wp(entry); 3064 VM_BUG_ON(!pte_none(ptep_get(pte))); 3065 set_pte_at(mm, addr, pte, entry); 3066 pte++; 3067 } 3068 pte_unmap(pte - 1); 3069 smp_wmb(); /* make pte visible before pmd */ 3070 pmd_populate(mm, pmd, pgtable); 3071 } 3072 3073 static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, 3074 unsigned long haddr, bool freeze) 3075 { 3076 struct mm_struct *mm = vma->vm_mm; 3077 struct folio *folio; 3078 struct page *page; 3079 pgtable_t pgtable; 3080 pmd_t old_pmd, _pmd; 3081 bool soft_dirty, uffd_wp = false, young = false, write = false; 3082 bool anon_exclusive = false, dirty = false; 3083 unsigned long addr; 3084 pte_t *pte; 3085 int i; 3086 3087 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK); 3088 VM_BUG_ON_VMA(vma->vm_start > haddr, vma); 3089 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma); 3090 3091 VM_WARN_ON_ONCE(!pmd_is_valid_softleaf(*pmd) && !pmd_trans_huge(*pmd)); 3092 3093 count_vm_event(THP_SPLIT_PMD); 3094 3095 if (!vma_is_anonymous(vma)) { 3096 old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd); 3097 /* 3098 * We are going to unmap this huge page. So 3099 * just go ahead and zap it 3100 */ 3101 if (arch_needs_pgtable_deposit()) 3102 zap_deposited_table(mm, pmd); 3103 if (vma_is_special_huge(vma)) 3104 return; 3105 if (unlikely(pmd_is_migration_entry(old_pmd))) { 3106 const softleaf_t old_entry = softleaf_from_pmd(old_pmd); 3107 3108 folio = softleaf_to_folio(old_entry); 3109 } else if (is_huge_zero_pmd(old_pmd)) { 3110 return; 3111 } else { 3112 page = pmd_page(old_pmd); 3113 folio = page_folio(page); 3114 if (!folio_test_dirty(folio) && pmd_dirty(old_pmd)) 3115 folio_mark_dirty(folio); 3116 if (!folio_test_referenced(folio) && pmd_young(old_pmd)) 3117 folio_set_referenced(folio); 3118 folio_remove_rmap_pmd(folio, page, vma); 3119 add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR); 3120 folio_put(folio); 3121 return; 3122 } 3123 add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR); 3124 return; 3125 } 3126 3127 if (is_huge_zero_pmd(*pmd)) { 3128 /* 3129 * FIXME: Do we want to invalidate secondary mmu by calling 3130 * mmu_notifier_arch_invalidate_secondary_tlbs() see comments below 3131 * inside __split_huge_pmd() ? 3132 * 3133 * We are going from a zero huge page write protected to zero 3134 * small page also write protected so it does not seems useful 3135 * to invalidate secondary mmu at this time. 3136 */ 3137 return __split_huge_zero_page_pmd(vma, haddr, pmd); 3138 } 3139 3140 if (pmd_is_migration_entry(*pmd)) { 3141 softleaf_t entry; 3142 3143 old_pmd = *pmd; 3144 entry = softleaf_from_pmd(old_pmd); 3145 page = softleaf_to_page(entry); 3146 folio = page_folio(page); 3147 3148 soft_dirty = pmd_swp_soft_dirty(old_pmd); 3149 uffd_wp = pmd_swp_uffd_wp(old_pmd); 3150 3151 write = softleaf_is_migration_write(entry); 3152 if (PageAnon(page)) 3153 anon_exclusive = softleaf_is_migration_read_exclusive(entry); 3154 young = softleaf_is_migration_young(entry); 3155 dirty = softleaf_is_migration_dirty(entry); 3156 } else if (pmd_is_device_private_entry(*pmd)) { 3157 softleaf_t entry; 3158 3159 old_pmd = *pmd; 3160 entry = softleaf_from_pmd(old_pmd); 3161 page = softleaf_to_page(entry); 3162 folio = page_folio(page); 3163 3164 soft_dirty = pmd_swp_soft_dirty(old_pmd); 3165 uffd_wp = pmd_swp_uffd_wp(old_pmd); 3166 3167 write = softleaf_is_device_private_write(entry); 3168 anon_exclusive = PageAnonExclusive(page); 3169 3170 /* 3171 * Device private THP should be treated the same as regular 3172 * folios w.r.t anon exclusive handling. See the comments for 3173 * folio handling and anon_exclusive below. 3174 */ 3175 if (freeze && anon_exclusive && 3176 folio_try_share_anon_rmap_pmd(folio, page)) 3177 freeze = false; 3178 if (!freeze) { 3179 rmap_t rmap_flags = RMAP_NONE; 3180 3181 folio_ref_add(folio, HPAGE_PMD_NR - 1); 3182 if (anon_exclusive) 3183 rmap_flags |= RMAP_EXCLUSIVE; 3184 3185 folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR, 3186 vma, haddr, rmap_flags); 3187 } 3188 } else { 3189 /* 3190 * Up to this point the pmd is present and huge and userland has 3191 * the whole access to the hugepage during the split (which 3192 * happens in place). If we overwrite the pmd with the not-huge 3193 * version pointing to the pte here (which of course we could if 3194 * all CPUs were bug free), userland could trigger a small page 3195 * size TLB miss on the small sized TLB while the hugepage TLB 3196 * entry is still established in the huge TLB. Some CPU doesn't 3197 * like that. See 3198 * http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum 3199 * 383 on page 105. Intel should be safe but is also warns that 3200 * it's only safe if the permission and cache attributes of the 3201 * two entries loaded in the two TLB is identical (which should 3202 * be the case here). But it is generally safer to never allow 3203 * small and huge TLB entries for the same virtual address to be 3204 * loaded simultaneously. So instead of doing "pmd_populate(); 3205 * flush_pmd_tlb_range();" we first mark the current pmd 3206 * notpresent (atomically because here the pmd_trans_huge must 3207 * remain set at all times on the pmd until the split is 3208 * complete for this pmd), then we flush the SMP TLB and finally 3209 * we write the non-huge version of the pmd entry with 3210 * pmd_populate. 3211 */ 3212 old_pmd = pmdp_invalidate(vma, haddr, pmd); 3213 page = pmd_page(old_pmd); 3214 folio = page_folio(page); 3215 if (pmd_dirty(old_pmd)) { 3216 dirty = true; 3217 folio_set_dirty(folio); 3218 } 3219 write = pmd_write(old_pmd); 3220 young = pmd_young(old_pmd); 3221 soft_dirty = pmd_soft_dirty(old_pmd); 3222 uffd_wp = pmd_uffd_wp(old_pmd); 3223 3224 VM_WARN_ON_FOLIO(!folio_ref_count(folio), folio); 3225 VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); 3226 3227 /* 3228 * Without "freeze", we'll simply split the PMD, propagating the 3229 * PageAnonExclusive() flag for each PTE by setting it for 3230 * each subpage -- no need to (temporarily) clear. 3231 * 3232 * With "freeze" we want to replace mapped pages by 3233 * migration entries right away. This is only possible if we 3234 * managed to clear PageAnonExclusive() -- see 3235 * set_pmd_migration_entry(). 3236 * 3237 * In case we cannot clear PageAnonExclusive(), split the PMD 3238 * only and let try_to_migrate_one() fail later. 3239 * 3240 * See folio_try_share_anon_rmap_pmd(): invalidate PMD first. 3241 */ 3242 anon_exclusive = PageAnonExclusive(page); 3243 if (freeze && anon_exclusive && 3244 folio_try_share_anon_rmap_pmd(folio, page)) 3245 freeze = false; 3246 if (!freeze) { 3247 rmap_t rmap_flags = RMAP_NONE; 3248 3249 folio_ref_add(folio, HPAGE_PMD_NR - 1); 3250 if (anon_exclusive) 3251 rmap_flags |= RMAP_EXCLUSIVE; 3252 folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR, 3253 vma, haddr, rmap_flags); 3254 } 3255 } 3256 3257 /* 3258 * Withdraw the table only after we mark the pmd entry invalid. 3259 * This's critical for some architectures (Power). 3260 */ 3261 pgtable = pgtable_trans_huge_withdraw(mm, pmd); 3262 pmd_populate(mm, &_pmd, pgtable); 3263 3264 pte = pte_offset_map(&_pmd, haddr); 3265 VM_BUG_ON(!pte); 3266 3267 /* 3268 * Note that NUMA hinting access restrictions are not transferred to 3269 * avoid any possibility of altering permissions across VMAs. 3270 */ 3271 if (freeze || pmd_is_migration_entry(old_pmd)) { 3272 pte_t entry; 3273 swp_entry_t swp_entry; 3274 3275 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { 3276 if (write) 3277 swp_entry = make_writable_migration_entry( 3278 page_to_pfn(page + i)); 3279 else if (anon_exclusive) 3280 swp_entry = make_readable_exclusive_migration_entry( 3281 page_to_pfn(page + i)); 3282 else 3283 swp_entry = make_readable_migration_entry( 3284 page_to_pfn(page + i)); 3285 if (young) 3286 swp_entry = make_migration_entry_young(swp_entry); 3287 if (dirty) 3288 swp_entry = make_migration_entry_dirty(swp_entry); 3289 entry = swp_entry_to_pte(swp_entry); 3290 if (soft_dirty) 3291 entry = pte_swp_mksoft_dirty(entry); 3292 if (uffd_wp) 3293 entry = pte_swp_mkuffd_wp(entry); 3294 VM_WARN_ON(!pte_none(ptep_get(pte + i))); 3295 set_pte_at(mm, addr, pte + i, entry); 3296 } 3297 } else if (pmd_is_device_private_entry(old_pmd)) { 3298 pte_t entry; 3299 swp_entry_t swp_entry; 3300 3301 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { 3302 /* 3303 * anon_exclusive was already propagated to the relevant 3304 * pages corresponding to the pte entries when freeze 3305 * is false. 3306 */ 3307 if (write) 3308 swp_entry = make_writable_device_private_entry( 3309 page_to_pfn(page + i)); 3310 else 3311 swp_entry = make_readable_device_private_entry( 3312 page_to_pfn(page + i)); 3313 /* 3314 * Young and dirty bits are not progated via swp_entry 3315 */ 3316 entry = swp_entry_to_pte(swp_entry); 3317 if (soft_dirty) 3318 entry = pte_swp_mksoft_dirty(entry); 3319 if (uffd_wp) 3320 entry = pte_swp_mkuffd_wp(entry); 3321 VM_WARN_ON(!pte_none(ptep_get(pte + i))); 3322 set_pte_at(mm, addr, pte + i, entry); 3323 } 3324 } else { 3325 pte_t entry; 3326 3327 entry = mk_pte(page, READ_ONCE(vma->vm_page_prot)); 3328 if (write) 3329 entry = pte_mkwrite(entry, vma); 3330 if (!young) 3331 entry = pte_mkold(entry); 3332 /* NOTE: this may set soft-dirty too on some archs */ 3333 if (dirty) 3334 entry = pte_mkdirty(entry); 3335 if (soft_dirty) 3336 entry = pte_mksoft_dirty(entry); 3337 if (uffd_wp) 3338 entry = pte_mkuffd_wp(entry); 3339 3340 for (i = 0; i < HPAGE_PMD_NR; i++) 3341 VM_WARN_ON(!pte_none(ptep_get(pte + i))); 3342 3343 set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR); 3344 } 3345 pte_unmap(pte); 3346 3347 if (!pmd_is_migration_entry(*pmd)) 3348 folio_remove_rmap_pmd(folio, page, vma); 3349 if (freeze) 3350 put_page(page); 3351 3352 smp_wmb(); /* make pte visible before pmd */ 3353 pmd_populate(mm, pmd, pgtable); 3354 } 3355 3356 void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, 3357 pmd_t *pmd, bool freeze) 3358 { 3359 VM_WARN_ON_ONCE(!IS_ALIGNED(address, HPAGE_PMD_SIZE)); 3360 if (pmd_trans_huge(*pmd) || pmd_is_valid_softleaf(*pmd)) 3361 __split_huge_pmd_locked(vma, pmd, address, freeze); 3362 } 3363 3364 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, 3365 unsigned long address, bool freeze) 3366 { 3367 spinlock_t *ptl; 3368 struct mmu_notifier_range range; 3369 3370 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 3371 address & HPAGE_PMD_MASK, 3372 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE); 3373 mmu_notifier_invalidate_range_start(&range); 3374 ptl = pmd_lock(vma->vm_mm, pmd); 3375 split_huge_pmd_locked(vma, range.start, pmd, freeze); 3376 spin_unlock(ptl); 3377 mmu_notifier_invalidate_range_end(&range); 3378 } 3379 3380 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, 3381 bool freeze) 3382 { 3383 pmd_t *pmd = mm_find_pmd(vma->vm_mm, address); 3384 3385 if (!pmd) 3386 return; 3387 3388 __split_huge_pmd(vma, pmd, address, freeze); 3389 } 3390 3391 static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address) 3392 { 3393 /* 3394 * If the new address isn't hpage aligned and it could previously 3395 * contain an hugepage: check if we need to split an huge pmd. 3396 */ 3397 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) && 3398 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE), 3399 ALIGN(address, HPAGE_PMD_SIZE))) 3400 split_huge_pmd_address(vma, address, false); 3401 } 3402 3403 void vma_adjust_trans_huge(struct vm_area_struct *vma, 3404 unsigned long start, 3405 unsigned long end, 3406 struct vm_area_struct *next) 3407 { 3408 /* Check if we need to split start first. */ 3409 split_huge_pmd_if_needed(vma, start); 3410 3411 /* Check if we need to split end next. */ 3412 split_huge_pmd_if_needed(vma, end); 3413 3414 /* If we're incrementing next->vm_start, we might need to split it. */ 3415 if (next) 3416 split_huge_pmd_if_needed(next, end); 3417 } 3418 3419 static void unmap_folio(struct folio *folio) 3420 { 3421 enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SYNC | 3422 TTU_BATCH_FLUSH; 3423 3424 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); 3425 3426 if (folio_test_pmd_mappable(folio)) 3427 ttu_flags |= TTU_SPLIT_HUGE_PMD; 3428 3429 /* 3430 * Anon pages need migration entries to preserve them, but file 3431 * pages can simply be left unmapped, then faulted back on demand. 3432 * If that is ever changed (perhaps for mlock), update remap_page(). 3433 */ 3434 if (folio_test_anon(folio)) 3435 try_to_migrate(folio, ttu_flags); 3436 else 3437 try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK); 3438 3439 try_to_unmap_flush(); 3440 } 3441 3442 static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, 3443 unsigned long addr, pmd_t *pmdp, 3444 struct folio *folio) 3445 { 3446 struct mm_struct *mm = vma->vm_mm; 3447 int ref_count, map_count; 3448 pmd_t orig_pmd = *pmdp; 3449 3450 if (pmd_dirty(orig_pmd)) 3451 folio_set_dirty(folio); 3452 if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { 3453 folio_set_swapbacked(folio); 3454 return false; 3455 } 3456 3457 orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp); 3458 3459 /* 3460 * Syncing against concurrent GUP-fast: 3461 * - clear PMD; barrier; read refcount 3462 * - inc refcount; barrier; read PMD 3463 */ 3464 smp_mb(); 3465 3466 ref_count = folio_ref_count(folio); 3467 map_count = folio_mapcount(folio); 3468 3469 /* 3470 * Order reads for folio refcount and dirty flag 3471 * (see comments in __remove_mapping()). 3472 */ 3473 smp_rmb(); 3474 3475 /* 3476 * If the folio or its PMD is redirtied at this point, or if there 3477 * are unexpected references, we will give up to discard this folio 3478 * and remap it. 3479 * 3480 * The only folio refs must be one from isolation plus the rmap(s). 3481 */ 3482 if (pmd_dirty(orig_pmd)) 3483 folio_set_dirty(folio); 3484 if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) { 3485 folio_set_swapbacked(folio); 3486 set_pmd_at(mm, addr, pmdp, orig_pmd); 3487 return false; 3488 } 3489 3490 if (ref_count != map_count + 1) { 3491 set_pmd_at(mm, addr, pmdp, orig_pmd); 3492 return false; 3493 } 3494 3495 folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma); 3496 zap_deposited_table(mm, pmdp); 3497 add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); 3498 if (vma->vm_flags & VM_LOCKED) 3499 mlock_drain_local(); 3500 folio_put(folio); 3501 3502 return true; 3503 } 3504 3505 bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, 3506 pmd_t *pmdp, struct folio *folio) 3507 { 3508 VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); 3509 VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); 3510 VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); 3511 VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); 3512 VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); 3513 3514 return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); 3515 } 3516 3517 static void remap_page(struct folio *folio, unsigned long nr, int flags) 3518 { 3519 int i = 0; 3520 3521 /* If unmap_folio() uses try_to_migrate() on file, remove this check */ 3522 if (!folio_test_anon(folio)) 3523 return; 3524 for (;;) { 3525 remove_migration_ptes(folio, folio, TTU_RMAP_LOCKED | flags); 3526 i += folio_nr_pages(folio); 3527 if (i >= nr) 3528 break; 3529 folio = folio_next(folio); 3530 } 3531 } 3532 3533 static void lru_add_split_folio(struct folio *folio, struct folio *new_folio, 3534 struct lruvec *lruvec, struct list_head *list) 3535 { 3536 VM_BUG_ON_FOLIO(folio_test_lru(new_folio), folio); 3537 lockdep_assert_held(&lruvec->lru_lock); 3538 3539 if (folio_is_device_private(folio)) 3540 return; 3541 3542 if (list) { 3543 /* page reclaim is reclaiming a huge page */ 3544 VM_WARN_ON(folio_test_lru(folio)); 3545 folio_get(new_folio); 3546 list_add_tail(&new_folio->lru, list); 3547 } else { 3548 /* head is still on lru (and we have it frozen) */ 3549 VM_WARN_ON(!folio_test_lru(folio)); 3550 if (folio_test_unevictable(folio)) 3551 new_folio->mlock_count = 0; 3552 else 3553 list_add_tail(&new_folio->lru, &folio->lru); 3554 folio_set_lru(new_folio); 3555 } 3556 } 3557 3558 static bool page_range_has_hwpoisoned(struct page *page, long nr_pages) 3559 { 3560 for (; nr_pages; page++, nr_pages--) 3561 if (PageHWPoison(page)) 3562 return true; 3563 return false; 3564 } 3565 3566 /* 3567 * It splits @folio into @new_order folios and copies the @folio metadata to 3568 * all the resulting folios. 3569 */ 3570 static void __split_folio_to_order(struct folio *folio, int old_order, 3571 int new_order) 3572 { 3573 /* Scan poisoned pages when split a poisoned folio to large folios */ 3574 const bool handle_hwpoison = folio_test_has_hwpoisoned(folio) && new_order; 3575 long new_nr_pages = 1 << new_order; 3576 long nr_pages = 1 << old_order; 3577 long i; 3578 3579 folio_clear_has_hwpoisoned(folio); 3580 3581 /* Check first new_nr_pages since the loop below skips them */ 3582 if (handle_hwpoison && 3583 page_range_has_hwpoisoned(folio_page(folio, 0), new_nr_pages)) 3584 folio_set_has_hwpoisoned(folio); 3585 /* 3586 * Skip the first new_nr_pages, since the new folio from them have all 3587 * the flags from the original folio. 3588 */ 3589 for (i = new_nr_pages; i < nr_pages; i += new_nr_pages) { 3590 struct page *new_head = &folio->page + i; 3591 /* 3592 * Careful: new_folio is not a "real" folio before we cleared PageTail. 3593 * Don't pass it around before clear_compound_head(). 3594 */ 3595 struct folio *new_folio = (struct folio *)new_head; 3596 3597 VM_BUG_ON_PAGE(atomic_read(&new_folio->_mapcount) != -1, new_head); 3598 3599 /* 3600 * Clone page flags before unfreezing refcount. 3601 * 3602 * After successful get_page_unless_zero() might follow flags change, 3603 * for example lock_page() which set PG_waiters. 3604 * 3605 * Note that for mapped sub-pages of an anonymous THP, 3606 * PG_anon_exclusive has been cleared in unmap_folio() and is stored in 3607 * the migration entry instead from where remap_page() will restore it. 3608 * We can still have PG_anon_exclusive set on effectively unmapped and 3609 * unreferenced sub-pages of an anonymous THP: we can simply drop 3610 * PG_anon_exclusive (-> PG_mappedtodisk) for these here. 3611 */ 3612 new_folio->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP; 3613 new_folio->flags.f |= (folio->flags.f & 3614 ((1L << PG_referenced) | 3615 (1L << PG_swapbacked) | 3616 (1L << PG_swapcache) | 3617 (1L << PG_mlocked) | 3618 (1L << PG_uptodate) | 3619 (1L << PG_active) | 3620 (1L << PG_workingset) | 3621 (1L << PG_locked) | 3622 (1L << PG_unevictable) | 3623 #ifdef CONFIG_ARCH_USES_PG_ARCH_2 3624 (1L << PG_arch_2) | 3625 #endif 3626 #ifdef CONFIG_ARCH_USES_PG_ARCH_3 3627 (1L << PG_arch_3) | 3628 #endif 3629 (1L << PG_dirty) | 3630 (1L << PG_dropbehind) | 3631 LRU_GEN_MASK | LRU_REFS_MASK)); 3632 3633 new_folio->mapping = folio->mapping; 3634 new_folio->index = folio->index + i; 3635 3636 if (folio_test_swapcache(folio)) 3637 new_folio->swap.val = folio->swap.val + i; 3638 3639 /* Page flags must be visible before we make the page non-compound. */ 3640 smp_wmb(); 3641 3642 /* 3643 * Clear PageTail before unfreezing page refcount. 3644 * 3645 * After successful get_page_unless_zero() might follow put_page() 3646 * which needs correct compound_head(). 3647 */ 3648 clear_compound_head(new_head); 3649 if (new_order) { 3650 prep_compound_page(new_head, new_order); 3651 folio_set_large_rmappable(new_folio); 3652 } 3653 3654 /* 3655 * PG_has_hwpoisoned is on the 2nd page, so set it after 3656 * the compound head is prepped. 3657 */ 3658 if (handle_hwpoison && 3659 page_range_has_hwpoisoned(new_head, new_nr_pages)) 3660 folio_set_has_hwpoisoned(new_folio); 3661 3662 if (folio_test_young(folio)) 3663 folio_set_young(new_folio); 3664 if (folio_test_idle(folio)) 3665 folio_set_idle(new_folio); 3666 #ifdef CONFIG_MEMCG 3667 new_folio->memcg_data = folio->memcg_data; 3668 #endif 3669 3670 folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio)); 3671 } 3672 3673 if (new_order) 3674 folio_set_order(folio, new_order); 3675 else 3676 ClearPageCompound(&folio->page); 3677 } 3678 3679 /** 3680 * __split_unmapped_folio() - splits an unmapped @folio to lower order folios in 3681 * two ways: uniform split or non-uniform split. 3682 * @folio: the to-be-split folio 3683 * @new_order: the smallest order of the after split folios (since buddy 3684 * allocator like split generates folios with orders from @folio's 3685 * order - 1 to new_order). 3686 * @split_at: in buddy allocator like split, the folio containing @split_at 3687 * will be split until its order becomes @new_order. 3688 * @xas: xa_state pointing to folio->mapping->i_pages and locked by caller 3689 * @mapping: @folio->mapping 3690 * @split_type: if the split is uniform or not (buddy allocator like split) 3691 * 3692 * 3693 * 1. uniform split: the given @folio into multiple @new_order small folios, 3694 * where all small folios have the same order. This is done when 3695 * split_type is SPLIT_TYPE_UNIFORM. 3696 * 2. buddy allocator like (non-uniform) split: the given @folio is split into 3697 * half and one of the half (containing the given page) is split into half 3698 * until the given @folio's order becomes @new_order. This is done when 3699 * split_type is SPLIT_TYPE_NON_UNIFORM. 3700 * 3701 * The high level flow for these two methods are: 3702 * 3703 * 1. uniform split: @xas is split with no expectation of failure and a single 3704 * __split_folio_to_order() is called to split the @folio into @new_order 3705 * along with stats update. 3706 * 2. non-uniform split: folio_order - @new_order calls to 3707 * __split_folio_to_order() are expected to be made in a for loop to split 3708 * the @folio to one lower order at a time. The folio containing @split_at 3709 * is split in each iteration. @xas is split into half in each iteration and 3710 * can fail. A failed @xas split leaves split folios as is without merging 3711 * them back. 3712 * 3713 * After splitting, the caller's folio reference will be transferred to the 3714 * folio containing @split_at. The caller needs to unlock and/or free 3715 * after-split folios if necessary. 3716 * 3717 * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be 3718 * split but not to @new_order, the caller needs to check) 3719 */ 3720 static int __split_unmapped_folio(struct folio *folio, int new_order, 3721 struct page *split_at, struct xa_state *xas, 3722 struct address_space *mapping, enum split_type split_type) 3723 { 3724 const bool is_anon = folio_test_anon(folio); 3725 int old_order = folio_order(folio); 3726 int start_order = split_type == SPLIT_TYPE_UNIFORM ? new_order : old_order - 1; 3727 struct folio *old_folio = folio; 3728 int split_order; 3729 3730 /* 3731 * split to new_order one order at a time. For uniform split, 3732 * folio is split to new_order directly. 3733 */ 3734 for (split_order = start_order; 3735 split_order >= new_order; 3736 split_order--) { 3737 int nr_new_folios = 1UL << (old_order - split_order); 3738 3739 /* order-1 anonymous folio is not supported */ 3740 if (is_anon && split_order == 1) 3741 continue; 3742 3743 if (mapping) { 3744 /* 3745 * uniform split has xas_split_alloc() called before 3746 * irq is disabled to allocate enough memory, whereas 3747 * non-uniform split can handle ENOMEM. 3748 * Use the to-be-split folio, so that a parallel 3749 * folio_try_get() waits on it until xarray is updated 3750 * with after-split folios and the original one is 3751 * unfrozen. 3752 */ 3753 if (split_type == SPLIT_TYPE_UNIFORM) { 3754 xas_split(xas, old_folio, old_order); 3755 } else { 3756 xas_set_order(xas, folio->index, split_order); 3757 xas_try_split(xas, old_folio, old_order); 3758 if (xas_error(xas)) 3759 return xas_error(xas); 3760 } 3761 } 3762 3763 folio_split_memcg_refs(folio, old_order, split_order); 3764 split_page_owner(&folio->page, old_order, split_order); 3765 pgalloc_tag_split(folio, old_order, split_order); 3766 __split_folio_to_order(folio, old_order, split_order); 3767 3768 if (is_anon) { 3769 mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1); 3770 mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios); 3771 } 3772 /* 3773 * If uniform split, the process is complete. 3774 * If non-uniform, continue splitting the folio at @split_at 3775 * as long as the next @split_order is >= @new_order. 3776 */ 3777 folio = page_folio(split_at); 3778 old_order = split_order; 3779 } 3780 3781 return 0; 3782 } 3783 3784 /** 3785 * folio_check_splittable() - check if a folio can be split to a given order 3786 * @folio: folio to be split 3787 * @new_order: the smallest order of the after split folios (since buddy 3788 * allocator like split generates folios with orders from @folio's 3789 * order - 1 to new_order). 3790 * @split_type: uniform or non-uniform split 3791 * 3792 * folio_check_splittable() checks if @folio can be split to @new_order using 3793 * @split_type method. The truncated folio check must come first. 3794 * 3795 * Context: folio must be locked. 3796 * 3797 * Return: 0 - @folio can be split to @new_order, otherwise an error number is 3798 * returned. 3799 */ 3800 int folio_check_splittable(struct folio *folio, unsigned int new_order, 3801 enum split_type split_type) 3802 { 3803 VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); 3804 /* 3805 * Folios that just got truncated cannot get split. Signal to the 3806 * caller that there was a race. 3807 * 3808 * TODO: this will also currently refuse folios without a mapping in the 3809 * swapcache (shmem or to-be-anon folios). 3810 */ 3811 if (!folio->mapping && !folio_test_anon(folio)) 3812 return -EBUSY; 3813 3814 /* order-1 is not supported for anonymous THP. */ 3815 if (folio_test_anon(folio) && new_order == 1) 3816 return -EINVAL; 3817 3818 /* 3819 * swapcache folio could only be split to order 0 3820 * 3821 * non-uniform split creates after-split folios with orders from 3822 * folio_order(folio) - 1 to new_order, making it not suitable for any 3823 * swapcache folio split. Only uniform split to order-0 can be used 3824 * here. 3825 */ 3826 if ((split_type == SPLIT_TYPE_NON_UNIFORM || new_order) && folio_test_swapcache(folio)) { 3827 return -EINVAL; 3828 } 3829 3830 if (is_huge_zero_folio(folio)) 3831 return -EINVAL; 3832 3833 if (folio_test_writeback(folio)) 3834 return -EBUSY; 3835 3836 return 0; 3837 } 3838 3839 /* Number of folio references from the pagecache or the swapcache. */ 3840 static unsigned int folio_cache_ref_count(const struct folio *folio) 3841 { 3842 if (folio_test_anon(folio) && !folio_test_swapcache(folio)) 3843 return 0; 3844 return folio_nr_pages(folio); 3845 } 3846 3847 static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int new_order, 3848 struct page *split_at, struct xa_state *xas, 3849 struct address_space *mapping, bool do_lru, 3850 struct list_head *list, enum split_type split_type, 3851 pgoff_t end, int *nr_shmem_dropped) 3852 { 3853 struct folio *end_folio = folio_next(folio); 3854 struct folio *new_folio, *next; 3855 int old_order = folio_order(folio); 3856 struct list_lru_one *lru; 3857 bool dequeue_deferred; 3858 int ret = 0; 3859 3860 VM_WARN_ON_ONCE(!mapping && end); 3861 /* 3862 * If this folio can be on the deferred split queue, lock out 3863 * the shrinker before freezing the ref. If the shrinker sees 3864 * a 0-ref folio, it assumes it beat folio_put() to the list 3865 * lock and must clean up the LRU state - the same dequeue we 3866 * will do below as part of the split. 3867 */ 3868 dequeue_deferred = folio_test_anon(folio) && old_order > 1; 3869 if (dequeue_deferred) { 3870 struct mem_cgroup *memcg; 3871 3872 rcu_read_lock(); 3873 memcg = folio_memcg(folio); 3874 lru = list_lru_lock(&deferred_split_lru, 3875 folio_nid(folio), &memcg); 3876 } 3877 if (folio_ref_freeze(folio, folio_cache_ref_count(folio) + 1)) { 3878 struct swap_cluster_info *ci = NULL; 3879 struct lruvec *lruvec; 3880 3881 if (dequeue_deferred) { 3882 __list_lru_del(&deferred_split_lru, lru, 3883 &folio->_deferred_list, folio_nid(folio)); 3884 if (folio_test_partially_mapped(folio)) { 3885 folio_clear_partially_mapped(folio); 3886 mod_mthp_stat(old_order, 3887 MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); 3888 } 3889 list_lru_unlock(lru); 3890 rcu_read_unlock(); 3891 } 3892 3893 if (mapping) { 3894 int nr = folio_nr_pages(folio); 3895 3896 if (folio_test_pmd_mappable(folio) && 3897 new_order < HPAGE_PMD_ORDER) { 3898 if (folio_test_swapbacked(folio)) { 3899 lruvec_stat_mod_folio(folio, 3900 NR_SHMEM_THPS, -nr); 3901 } else { 3902 lruvec_stat_mod_folio(folio, 3903 NR_FILE_THPS, -nr); 3904 } 3905 } 3906 } 3907 3908 if (folio_test_swapcache(folio)) { 3909 if (mapping) { 3910 VM_WARN_ON_ONCE_FOLIO(mapping, folio); 3911 return -EINVAL; 3912 } 3913 3914 ci = swap_cluster_get_and_lock(folio); 3915 } 3916 3917 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */ 3918 if (do_lru) 3919 lruvec = folio_lruvec_lock(folio); 3920 3921 ret = __split_unmapped_folio(folio, new_order, split_at, xas, 3922 mapping, split_type); 3923 3924 /* 3925 * Unfreeze after-split folios and put them back to the right 3926 * list. @folio should be kept frozon until page cache 3927 * entries are updated with all the other after-split folios 3928 * to prevent others seeing stale page cache entries. 3929 * As a result, new_folio starts from the next folio of 3930 * @folio. 3931 */ 3932 for (new_folio = folio_next(folio); new_folio != end_folio; 3933 new_folio = next) { 3934 unsigned long nr_pages = folio_nr_pages(new_folio); 3935 3936 next = folio_next(new_folio); 3937 3938 zone_device_private_split_cb(folio, new_folio); 3939 3940 folio_ref_unfreeze(new_folio, 3941 folio_cache_ref_count(new_folio) + 1); 3942 3943 if (do_lru) 3944 lru_add_split_folio(folio, new_folio, lruvec, list); 3945 3946 /* 3947 * Anonymous folio with swap cache. 3948 * NOTE: shmem in swap cache is not supported yet. 3949 */ 3950 if (ci) { 3951 __swap_cache_replace_folio(ci, folio, new_folio); 3952 continue; 3953 } 3954 3955 /* Anonymous folio without swap cache */ 3956 if (!mapping) 3957 continue; 3958 3959 /* Add the new folio to the page cache. */ 3960 if (new_folio->index < end) { 3961 __xa_store(&mapping->i_pages, new_folio->index, 3962 new_folio, 0); 3963 continue; 3964 } 3965 3966 VM_WARN_ON_ONCE(!nr_shmem_dropped); 3967 /* Drop folio beyond EOF: ->index >= end */ 3968 if (shmem_mapping(mapping) && nr_shmem_dropped) 3969 *nr_shmem_dropped += nr_pages; 3970 else if (folio_test_clear_dirty(new_folio)) 3971 folio_account_cleaned( 3972 new_folio, inode_to_wb(mapping->host)); 3973 __filemap_remove_folio(new_folio, NULL); 3974 folio_put_refs(new_folio, nr_pages); 3975 } 3976 3977 zone_device_private_split_cb(folio, NULL); 3978 /* 3979 * Unfreeze @folio only after all page cache entries, which 3980 * used to point to it, have been updated with new folios. 3981 * Otherwise, a parallel folio_try_get() can grab @folio 3982 * and its caller can see stale page cache entries. 3983 */ 3984 folio_ref_unfreeze(folio, folio_cache_ref_count(folio) + 1); 3985 3986 if (do_lru) 3987 lruvec_unlock(lruvec); 3988 3989 if (ci) 3990 swap_cluster_unlock(ci); 3991 } else { 3992 if (dequeue_deferred) { 3993 list_lru_unlock(lru); 3994 rcu_read_unlock(); 3995 } 3996 return -EAGAIN; 3997 } 3998 3999 return ret; 4000 } 4001 4002 /** 4003 * __folio_split() - split a folio at @split_at to a @new_order folio 4004 * @folio: folio to split 4005 * @new_order: the order of the new folio 4006 * @split_at: a page within the new folio 4007 * @lock_at: a page within @folio to be left locked to caller 4008 * @list: after-split folios will be put on it if non NULL 4009 * @split_type: perform uniform split or not (non-uniform split) 4010 * 4011 * It calls __split_unmapped_folio() to perform uniform and non-uniform split. 4012 * It is in charge of checking whether the split is supported or not and 4013 * preparing @folio for __split_unmapped_folio(). 4014 * 4015 * After splitting, the after-split folio containing @lock_at remains locked 4016 * and others are unlocked: 4017 * 1. for uniform split, @lock_at points to one of @folio's subpages; 4018 * 2. for buddy allocator like (non-uniform) split, @lock_at points to @folio. 4019 * 4020 * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be 4021 * split but not to @new_order, the caller needs to check) 4022 */ 4023 static int __folio_split(struct folio *folio, unsigned int new_order, 4024 struct page *split_at, struct page *lock_at, 4025 struct list_head *list, enum split_type split_type) 4026 { 4027 XA_STATE(xas, &folio->mapping->i_pages, folio->index); 4028 struct folio *end_folio = folio_next(folio); 4029 bool is_anon = folio_test_anon(folio); 4030 struct address_space *mapping = NULL; 4031 struct anon_vma *anon_vma = NULL; 4032 int old_order = folio_order(folio); 4033 struct folio *new_folio, *next; 4034 int nr_shmem_dropped = 0; 4035 enum ttu_flags ttu_flags = 0; 4036 int ret; 4037 pgoff_t end = 0; 4038 4039 VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); 4040 VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio); 4041 4042 if (folio != page_folio(split_at) || folio != page_folio(lock_at)) { 4043 ret = -EINVAL; 4044 goto out; 4045 } 4046 4047 if (new_order >= old_order) { 4048 ret = -EINVAL; 4049 goto out; 4050 } 4051 4052 ret = folio_check_splittable(folio, new_order, split_type); 4053 if (ret) { 4054 VM_WARN_ONCE(ret == -EINVAL, "Tried to split an unsplittable folio"); 4055 goto out; 4056 } 4057 4058 if (is_anon) { 4059 /* 4060 * The caller does not necessarily hold an mmap_lock that would 4061 * prevent the anon_vma disappearing so we first we take a 4062 * reference to it and then lock the anon_vma for write. This 4063 * is similar to folio_lock_anon_vma_read except the write lock 4064 * is taken to serialise against parallel split or collapse 4065 * operations. 4066 */ 4067 anon_vma = folio_get_anon_vma(folio); 4068 if (!anon_vma) { 4069 ret = -EBUSY; 4070 goto out; 4071 } 4072 anon_vma_lock_write(anon_vma); 4073 mapping = NULL; 4074 } else { 4075 unsigned int min_order; 4076 gfp_t gfp; 4077 4078 mapping = folio->mapping; 4079 min_order = mapping_min_folio_order(mapping); 4080 if (new_order < min_order) { 4081 ret = -EINVAL; 4082 goto out; 4083 } 4084 4085 gfp = current_gfp_context(mapping_gfp_mask(mapping) & 4086 GFP_RECLAIM_MASK); 4087 4088 if (!filemap_release_folio(folio, gfp)) { 4089 ret = -EBUSY; 4090 goto out; 4091 } 4092 4093 mapping_set_update(&xas, mapping); 4094 4095 if (split_type == SPLIT_TYPE_UNIFORM) { 4096 xas_set_order(&xas, folio->index, new_order); 4097 xas_split_alloc(&xas, folio, old_order, gfp); 4098 if (xas_error(&xas)) { 4099 ret = xas_error(&xas); 4100 goto out; 4101 } 4102 } 4103 4104 anon_vma = NULL; 4105 i_mmap_lock_read(mapping); 4106 4107 /* 4108 *__split_unmapped_folio() may need to trim off pages beyond 4109 * EOF: but on 32-bit, i_size_read() takes an irq-unsafe 4110 * seqlock, which cannot be nested inside the page tree lock. 4111 * So note end now: i_size itself may be changed at any moment, 4112 * but folio lock is good enough to serialize the trimming. 4113 */ 4114 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); 4115 if (shmem_mapping(mapping)) 4116 end = shmem_fallocend(mapping->host, end); 4117 } 4118 4119 /* 4120 * Racy check if we can split the page, before unmap_folio() will 4121 * split PMDs 4122 */ 4123 if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1) { 4124 ret = -EAGAIN; 4125 goto out_unlock; 4126 } 4127 4128 unmap_folio(folio); 4129 4130 /* block interrupt reentry in xa_lock and spinlock */ 4131 local_irq_disable(); 4132 if (mapping) { 4133 /* 4134 * Check if the folio is present in page cache. 4135 * We assume all tail are present too, if folio is there. 4136 */ 4137 xas_lock(&xas); 4138 xas_reset(&xas); 4139 if (xas_load(&xas) != folio) { 4140 ret = -EAGAIN; 4141 goto fail; 4142 } 4143 } 4144 4145 ret = __folio_freeze_and_split_unmapped(folio, new_order, split_at, &xas, mapping, 4146 true, list, split_type, end, &nr_shmem_dropped); 4147 fail: 4148 if (mapping) 4149 xas_unlock(&xas); 4150 4151 local_irq_enable(); 4152 4153 if (nr_shmem_dropped) 4154 shmem_uncharge(mapping->host, nr_shmem_dropped); 4155 4156 if (!ret && is_anon && !folio_is_device_private(folio)) 4157 ttu_flags = TTU_USE_SHARED_ZEROPAGE; 4158 4159 remap_page(folio, 1 << old_order, ttu_flags); 4160 4161 /* 4162 * Drop the mapping while the inode is still pinned. @folio stays 4163 * locked and present in the page cache until the loop below, so 4164 * eviction cannot free the inode yet; @lock_at is not enough, it may 4165 * be a tail beyond EOF that the split already dropped from the page 4166 * cache. Nothing past this point may touch the inode or the mapping. 4167 */ 4168 if (mapping) { 4169 i_mmap_unlock_read(mapping); 4170 mapping = NULL; 4171 } 4172 4173 /* 4174 * Unlock all after-split folios except the one containing 4175 * @lock_at page. If @folio is not split, it will be kept locked. 4176 */ 4177 for (new_folio = folio; new_folio != end_folio; new_folio = next) { 4178 next = folio_next(new_folio); 4179 if (new_folio == page_folio(lock_at)) 4180 continue; 4181 4182 folio_unlock(new_folio); 4183 /* 4184 * Subpages whose mapping has been zapped may be freed 4185 * earlier, but freeing them requires taking the 4186 * lru_lock, so we defer put_page() on tail pages until 4187 * after the split completes. 4188 */ 4189 free_folio_and_swap_cache(new_folio); 4190 } 4191 4192 out_unlock: 4193 if (anon_vma) { 4194 anon_vma_unlock_write(anon_vma); 4195 put_anon_vma(anon_vma); 4196 } 4197 if (mapping) 4198 i_mmap_unlock_read(mapping); 4199 out: 4200 xas_destroy(&xas); 4201 if (is_pmd_order(old_order)) 4202 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED); 4203 count_mthp_stat(old_order, !ret ? MTHP_STAT_SPLIT : MTHP_STAT_SPLIT_FAILED); 4204 return ret; 4205 } 4206 4207 /** 4208 * folio_split_unmapped() - split a large anon folio that is already unmapped 4209 * @folio: folio to split 4210 * @new_order: the order of folios after split 4211 * 4212 * This function is a helper for splitting folios that have already been 4213 * unmapped. The use case is that the device or the CPU can refuse to migrate 4214 * THP pages in the middle of migration, due to allocation issues on either 4215 * side. 4216 * 4217 * anon_vma_lock is not required to be held, mmap_read_lock() or 4218 * mmap_write_lock() should be held. @folio is expected to be locked by the 4219 * caller. device-private and non device-private folios are supported along 4220 * with folios that are in the swapcache. @folio should also be unmapped and 4221 * isolated from LRU (if applicable) 4222 * 4223 * Upon return, the folio is not remapped, split folios are not added to LRU, 4224 * free_folio_and_swap_cache() is not called, and new folios remain locked. 4225 * 4226 * Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to 4227 * insufficient reference count or extra pins). 4228 */ 4229 int folio_split_unmapped(struct folio *folio, unsigned int new_order) 4230 { 4231 int ret = 0; 4232 4233 VM_WARN_ON_ONCE_FOLIO(folio_mapped(folio), folio); 4234 VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); 4235 VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio); 4236 VM_WARN_ON_ONCE_FOLIO(!folio_test_anon(folio), folio); 4237 4238 if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1) 4239 return -EAGAIN; 4240 4241 local_irq_disable(); 4242 ret = __folio_freeze_and_split_unmapped(folio, new_order, &folio->page, NULL, 4243 NULL, false, NULL, SPLIT_TYPE_UNIFORM, 4244 0, NULL); 4245 local_irq_enable(); 4246 return ret; 4247 } 4248 4249 /* 4250 * This function splits a large folio into smaller folios of order @new_order. 4251 * @page can point to any page of the large folio to split. The split operation 4252 * does not change the position of @page. 4253 * 4254 * Prerequisites: 4255 * 4256 * 1) The caller must hold a reference on the @page's owning folio, also known 4257 * as the large folio. 4258 * 4259 * 2) The large folio must be locked. 4260 * 4261 * 3) The folio must not be pinned. Any unexpected folio references, including 4262 * GUP pins, will result in the folio not getting split; instead, the caller 4263 * will receive an -EAGAIN. 4264 * 4265 * 4) @new_order > 1, usually. Splitting to order-1 anonymous folios is not 4266 * supported for non-file-backed folios, because folio->_deferred_list, which 4267 * is used by partially mapped folios, is stored in subpage 2, but an order-1 4268 * folio only has subpages 0 and 1. File-backed order-1 folios are supported, 4269 * since they do not use _deferred_list. 4270 * 4271 * After splitting, the caller's folio reference will be transferred to @page, 4272 * resulting in a raised refcount of @page after this call. The other pages may 4273 * be freed if they are not mapped. 4274 * 4275 * If @list is null, tail pages will be added to LRU list, otherwise, to @list. 4276 * 4277 * Pages in @new_order will inherit the mapping, flags, and so on from the 4278 * huge page. 4279 * 4280 * Returns 0 if the huge page was split successfully. 4281 * 4282 * Returns -EAGAIN if the folio has unexpected reference (e.g., GUP) or if 4283 * the folio was concurrently removed from the page cache. 4284 * 4285 * Returns -EBUSY when trying to split the huge zeropage, if the folio is 4286 * under writeback, if fs-specific folio metadata cannot currently be 4287 * released, or if some unexpected race happened (e.g., anon VMA disappeared, 4288 * truncation). 4289 * 4290 * Callers should ensure that the order respects the address space mapping 4291 * min-order if one is set for non-anonymous folios. 4292 * 4293 * Returns -EINVAL when trying to split to an order that is incompatible 4294 * with the folio. Splitting to order 0 is compatible with all folios. 4295 */ 4296 int __split_huge_page_to_list_to_order(struct page *page, struct list_head *list, 4297 unsigned int new_order) 4298 { 4299 struct folio *folio = page_folio(page); 4300 4301 return __folio_split(folio, new_order, &folio->page, page, list, 4302 SPLIT_TYPE_UNIFORM); 4303 } 4304 4305 /** 4306 * folio_split() - split a folio at @split_at to a @new_order folio 4307 * @folio: folio to split 4308 * @new_order: the order of the new folio 4309 * @split_at: a page within the new folio 4310 * @list: after-split folios are added to @list if not null, otherwise to LRU 4311 * list 4312 * 4313 * It has the same prerequisites and returns as 4314 * split_huge_page_to_list_to_order(). 4315 * 4316 * Split a folio at @split_at to a new_order folio, leave the 4317 * remaining subpages of the original folio as large as possible. For example, 4318 * in the case of splitting an order-9 folio at its third order-3 subpages to 4319 * an order-3 folio, there are 2^(9-3)=64 order-3 subpages in the order-9 folio. 4320 * After the split, there will be a group of folios with different orders and 4321 * the new folio containing @split_at is marked in bracket: 4322 * [order-4, {order-3}, order-3, order-5, order-6, order-7, order-8]. 4323 * 4324 * After split, folio is left locked for caller. 4325 * 4326 * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be 4327 * split but not to @new_order, the caller needs to check) 4328 */ 4329 int folio_split(struct folio *folio, unsigned int new_order, 4330 struct page *split_at, struct list_head *list) 4331 { 4332 return __folio_split(folio, new_order, split_at, &folio->page, list, 4333 SPLIT_TYPE_NON_UNIFORM); 4334 } 4335 4336 /** 4337 * min_order_for_split() - get the minimum order @folio can be split to 4338 * @folio: folio to split 4339 * 4340 * min_order_for_split() tells the minimum order @folio can be split to. 4341 * If a file-backed folio is truncated, 0 will be returned. Any subsequent 4342 * split attempt should get -EBUSY from split checking code. 4343 * 4344 * Return: @folio's minimum order for split 4345 */ 4346 unsigned int min_order_for_split(struct folio *folio) 4347 { 4348 if (folio_test_anon(folio)) 4349 return 0; 4350 4351 /* 4352 * If the folio got truncated, we don't know the previous mapping and 4353 * consequently the old min order. But it doesn't matter, as any split 4354 * attempt will immediately fail with -EBUSY as the folio cannot get 4355 * split until freed. 4356 */ 4357 if (!folio->mapping) 4358 return 0; 4359 4360 return mapping_min_folio_order(folio->mapping); 4361 } 4362 4363 int split_folio_to_list(struct folio *folio, struct list_head *list) 4364 { 4365 return split_huge_page_to_list_to_order(&folio->page, list, 0); 4366 } 4367 4368 /* 4369 * __folio_unqueue_deferred_split() is not to be called directly: 4370 * the folio_unqueue_deferred_split() inline wrapper in mm/internal.h 4371 * limits its calls to those folios which may have a _deferred_list for 4372 * queueing THP splits, and that list is (racily observed to be) non-empty. 4373 * 4374 * It is unsafe to call folio_unqueue_deferred_split() until folio refcount is 4375 * zero: because even when the list_lru lock is held, a non-empty 4376 * _deferred_list might be in use on deferred_split_scan()'s unlocked 4377 * on-stack list. 4378 * 4379 * The list_lru sublist is determined by folio's memcg: it is therefore 4380 * important to unqueue deferred split before changing folio memcg. 4381 */ 4382 bool __folio_unqueue_deferred_split(struct folio *folio) 4383 { 4384 struct mem_cgroup *memcg; 4385 struct list_lru_one *lru; 4386 int nid = folio_nid(folio); 4387 unsigned long flags; 4388 bool unqueued = false; 4389 4390 WARN_ON_ONCE(folio_ref_count(folio)); 4391 WARN_ON_ONCE(!mem_cgroup_disabled() && !folio_memcg_charged(folio)); 4392 4393 rcu_read_lock(); 4394 memcg = folio_memcg(folio); 4395 lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags); 4396 if (__list_lru_del(&deferred_split_lru, lru, &folio->_deferred_list, nid)) { 4397 if (folio_test_partially_mapped(folio)) { 4398 folio_clear_partially_mapped(folio); 4399 mod_mthp_stat(folio_order(folio), 4400 MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); 4401 } 4402 unqueued = true; 4403 } 4404 list_lru_unlock_irqrestore(lru, &flags); 4405 rcu_read_unlock(); 4406 4407 return unqueued; /* useful for debug warnings */ 4408 } 4409 4410 /* partially_mapped=false won't clear PG_partially_mapped folio flag */ 4411 void deferred_split_folio(struct folio *folio, bool partially_mapped) 4412 { 4413 struct list_lru_one *lru; 4414 int nid; 4415 struct mem_cgroup *memcg; 4416 unsigned long flags; 4417 4418 /* 4419 * Order 1 folios have no space for a deferred list, but we also 4420 * won't waste much memory by not adding them to the deferred list. 4421 */ 4422 if (folio_order(folio) <= 1) 4423 return; 4424 4425 if (!partially_mapped && !split_underused_thp) 4426 return; 4427 4428 /* 4429 * Exclude swapcache: originally to avoid a corrupt deferred split 4430 * queue. Nowadays that is fully prevented by __memcg1_swapout(); 4431 * but if page reclaim is already handling the same folio, it is 4432 * unnecessary to handle it again in the shrinker, so excluding 4433 * swapcache here may still be a useful optimization. 4434 */ 4435 if (folio_test_swapcache(folio)) 4436 return; 4437 4438 nid = folio_nid(folio); 4439 4440 rcu_read_lock(); 4441 memcg = folio_memcg(folio); 4442 lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags); 4443 if (partially_mapped) { 4444 if (!folio_test_partially_mapped(folio)) { 4445 folio_set_partially_mapped(folio); 4446 if (folio_test_pmd_mappable(folio)) 4447 count_vm_event(THP_DEFERRED_SPLIT_PAGE); 4448 count_mthp_stat(folio_order(folio), MTHP_STAT_SPLIT_DEFERRED); 4449 mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, 1); 4450 } 4451 } else { 4452 /* partially mapped folios cannot become non-partially mapped */ 4453 VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio); 4454 } 4455 __list_lru_add(&deferred_split_lru, lru, &folio->_deferred_list, nid, memcg); 4456 list_lru_unlock_irqrestore(lru, &flags); 4457 rcu_read_unlock(); 4458 } 4459 4460 static unsigned long deferred_split_count(struct shrinker *shrink, 4461 struct shrink_control *sc) 4462 { 4463 unsigned long count; 4464 4465 count = list_lru_shrink_count(&deferred_split_lru, sc); 4466 return count ?: SHRINK_EMPTY; 4467 } 4468 4469 static bool thp_underused(struct folio *folio) 4470 { 4471 int num_zero_pages = 0, num_filled_pages = 0; 4472 int i; 4473 4474 if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1) 4475 return false; 4476 4477 if (folio_contain_hwpoisoned_page(folio)) 4478 return false; 4479 4480 for (i = 0; i < folio_nr_pages(folio); i++) { 4481 if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) { 4482 if (++num_zero_pages > khugepaged_max_ptes_none) 4483 return true; 4484 } else { 4485 /* 4486 * Another path for early exit once the number 4487 * of non-zero filled pages exceeds threshold. 4488 */ 4489 if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none) 4490 return false; 4491 } 4492 } 4493 return false; 4494 } 4495 4496 static enum lru_status deferred_split_isolate(struct list_head *item, 4497 struct list_lru_one *lru, 4498 void *cb_arg) 4499 { 4500 struct folio *folio = container_of(item, struct folio, _deferred_list); 4501 struct list_head *freeable = cb_arg; 4502 4503 if (folio_try_get(folio)) { 4504 list_lru_isolate_move(lru, item, freeable); 4505 return LRU_REMOVED; 4506 } 4507 4508 /* 4509 * We lost race with folio_put(). Read folio state before the 4510 * isolate: folio_unqueue_deferred_split() checks list_empty() 4511 * locklessly, so once removed the folio can be freed any time. 4512 */ 4513 if (folio_test_partially_mapped(folio)) { 4514 folio_clear_partially_mapped(folio); 4515 mod_mthp_stat(folio_order(folio), 4516 MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); 4517 } 4518 list_lru_isolate(lru, item); 4519 return LRU_REMOVED; 4520 } 4521 4522 static unsigned long deferred_split_scan(struct shrinker *shrink, 4523 struct shrink_control *sc) 4524 { 4525 LIST_HEAD(dispose); 4526 struct folio *folio, *next; 4527 int split = 0; 4528 unsigned long isolated; 4529 4530 isolated = list_lru_shrink_walk_irq(&deferred_split_lru, sc, 4531 deferred_split_isolate, &dispose); 4532 4533 list_for_each_entry_safe(folio, next, &dispose, _deferred_list) { 4534 bool did_split = false; 4535 bool underused = false; 4536 4537 list_del_init(&folio->_deferred_list); 4538 4539 if (!folio_test_partially_mapped(folio)) { 4540 /* 4541 * See try_to_map_unused_to_zeropage(): we cannot 4542 * optimize zero-filled pages after splitting an 4543 * mlocked folio. 4544 */ 4545 if (folio_test_mlocked(folio)) 4546 goto next; 4547 underused = thp_underused(folio); 4548 if (!underused) 4549 goto next; 4550 } 4551 if (!folio_trylock(folio)) 4552 goto requeue; 4553 if (!split_folio(folio)) { 4554 did_split = true; 4555 if (underused) 4556 count_vm_event(THP_UNDERUSED_SPLIT_PAGE); 4557 split++; 4558 } 4559 folio_unlock(folio); 4560 next: 4561 /* 4562 * If thp_underused() returns false, or if split_folio() 4563 * succeeds, or if split_folio() fails in the case it was 4564 * underused, then consider it used and don't add it back to 4565 * split_queue. 4566 */ 4567 if (!did_split && folio_test_partially_mapped(folio)) { 4568 requeue: 4569 rcu_read_lock(); 4570 list_lru_add_irq(&deferred_split_lru, 4571 &folio->_deferred_list, 4572 folio_nid(folio), 4573 folio_memcg(folio)); 4574 rcu_read_unlock(); 4575 } 4576 folio_put(folio); 4577 } 4578 4579 if (!split && !isolated) 4580 return SHRINK_STOP; 4581 return split; 4582 } 4583 4584 #ifdef CONFIG_DEBUG_FS 4585 static void split_huge_pages_all(void) 4586 { 4587 struct zone *zone; 4588 struct page *page; 4589 struct folio *folio; 4590 unsigned long pfn, max_zone_pfn; 4591 unsigned long total = 0, split = 0; 4592 4593 pr_debug("Split all THPs\n"); 4594 for_each_zone(zone) { 4595 if (!managed_zone(zone)) 4596 continue; 4597 max_zone_pfn = zone_end_pfn(zone); 4598 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) { 4599 int nr_pages; 4600 4601 page = pfn_to_online_page(pfn); 4602 if (!page || PageTail(page)) 4603 continue; 4604 folio = page_folio(page); 4605 if (!folio_try_get(folio)) 4606 continue; 4607 4608 if (unlikely(page_folio(page) != folio)) 4609 goto next; 4610 4611 if (zone != folio_zone(folio)) 4612 goto next; 4613 4614 if (!folio_test_large(folio) 4615 || folio_test_hugetlb(folio) 4616 || !folio_test_lru(folio)) 4617 goto next; 4618 4619 total++; 4620 folio_lock(folio); 4621 nr_pages = folio_nr_pages(folio); 4622 if (!split_folio(folio)) 4623 split++; 4624 pfn += nr_pages - 1; 4625 folio_unlock(folio); 4626 next: 4627 folio_put(folio); 4628 cond_resched(); 4629 } 4630 } 4631 4632 pr_debug("%lu of %lu THP split\n", split, total); 4633 } 4634 4635 static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma) 4636 { 4637 if (vma_is_dax(vma)) 4638 return true; 4639 if (vma_is_special_huge(vma)) 4640 return true; 4641 if (vma_test(vma, VMA_IO_BIT)) 4642 return true; 4643 if (is_vm_hugetlb_page(vma)) 4644 return true; 4645 4646 return false; 4647 } 4648 4649 static int split_huge_pages_pid(int pid, unsigned long vaddr_start, 4650 unsigned long vaddr_end, unsigned int new_order, 4651 long in_folio_offset) 4652 { 4653 int ret = 0; 4654 struct task_struct *task; 4655 struct mm_struct *mm; 4656 unsigned long total = 0, split = 0; 4657 unsigned long addr; 4658 4659 vaddr_start &= PAGE_MASK; 4660 vaddr_end &= PAGE_MASK; 4661 4662 task = find_get_task_by_vpid(pid); 4663 if (!task) { 4664 ret = -ESRCH; 4665 goto out; 4666 } 4667 4668 /* Find the mm_struct */ 4669 mm = get_task_mm(task); 4670 put_task_struct(task); 4671 4672 if (!mm) { 4673 ret = -EINVAL; 4674 goto out; 4675 } 4676 4677 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx], new_order: %u, in_folio_offset: %ld\n", 4678 pid, vaddr_start, vaddr_end, new_order, in_folio_offset); 4679 4680 mmap_read_lock(mm); 4681 /* 4682 * always increase addr by PAGE_SIZE, since we could have a PTE page 4683 * table filled with PTE-mapped THPs, each of which is distinct. 4684 */ 4685 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) { 4686 struct vm_area_struct *vma = vma_lookup(mm, addr); 4687 struct folio_walk fw; 4688 struct folio *folio; 4689 struct address_space *mapping; 4690 unsigned int target_order = new_order; 4691 4692 if (!vma) 4693 break; 4694 4695 /* skip special VMA and hugetlb VMA */ 4696 if (vma_not_suitable_for_thp_split(vma)) { 4697 addr = vma->vm_end; 4698 continue; 4699 } 4700 4701 folio = folio_walk_start(&fw, vma, addr, 0); 4702 if (!folio) 4703 continue; 4704 4705 if (!is_transparent_hugepage(folio)) 4706 goto next; 4707 4708 if (!folio_test_anon(folio)) { 4709 mapping = folio->mapping; 4710 target_order = max(new_order, 4711 mapping_min_folio_order(mapping)); 4712 } 4713 4714 if (target_order >= folio_order(folio)) 4715 goto next; 4716 4717 total++; 4718 /* 4719 * For folios with private, split_huge_page_to_list_to_order() 4720 * will try to drop it before split and then check if the folio 4721 * can be split or not. So skip the check here. 4722 */ 4723 if (!folio_test_private(folio) && 4724 folio_expected_ref_count(folio) != folio_ref_count(folio)) 4725 goto next; 4726 4727 if (!folio_trylock(folio)) 4728 goto next; 4729 folio_get(folio); 4730 folio_walk_end(&fw, vma); 4731 4732 if (!folio_test_anon(folio) && folio->mapping != mapping) 4733 goto unlock; 4734 4735 if (in_folio_offset < 0 || 4736 in_folio_offset >= folio_nr_pages(folio)) { 4737 if (!split_folio_to_order(folio, target_order)) 4738 split++; 4739 } else { 4740 struct page *split_at = folio_page(folio, 4741 in_folio_offset); 4742 if (!folio_split(folio, target_order, split_at, NULL)) 4743 split++; 4744 } 4745 4746 unlock: 4747 4748 folio_unlock(folio); 4749 folio_put(folio); 4750 4751 cond_resched(); 4752 continue; 4753 next: 4754 folio_walk_end(&fw, vma); 4755 cond_resched(); 4756 } 4757 mmap_read_unlock(mm); 4758 mmput(mm); 4759 4760 pr_debug("%lu of %lu THP split\n", split, total); 4761 4762 out: 4763 return ret; 4764 } 4765 4766 static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, 4767 pgoff_t off_end, unsigned int new_order, 4768 long in_folio_offset) 4769 { 4770 struct file *candidate; 4771 struct address_space *mapping; 4772 pgoff_t index; 4773 int nr_pages = 1; 4774 unsigned long total = 0, split = 0; 4775 unsigned int min_order; 4776 unsigned int target_order; 4777 4778 CLASS(filename_kernel, file)(file_path); 4779 candidate = file_open_name(file, O_RDONLY, 0); 4780 if (IS_ERR(candidate)) 4781 return -EINVAL; 4782 4783 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx], new_order: %u, in_folio_offset: %ld\n", 4784 file_path, off_start, off_end, new_order, in_folio_offset); 4785 4786 mapping = candidate->f_mapping; 4787 min_order = mapping_min_folio_order(mapping); 4788 target_order = max(new_order, min_order); 4789 4790 for (index = off_start; index < off_end; index += nr_pages) { 4791 struct folio *folio = filemap_get_folio(mapping, index); 4792 4793 nr_pages = 1; 4794 if (IS_ERR(folio)) 4795 continue; 4796 4797 if (!folio_test_large(folio)) 4798 goto next; 4799 4800 total++; 4801 nr_pages = folio_nr_pages(folio); 4802 4803 if (target_order >= folio_order(folio)) 4804 goto next; 4805 4806 if (!folio_trylock(folio)) 4807 goto next; 4808 4809 if (folio->mapping != mapping) 4810 goto unlock; 4811 4812 if (in_folio_offset < 0 || in_folio_offset >= nr_pages) { 4813 if (!split_folio_to_order(folio, target_order)) 4814 split++; 4815 } else { 4816 struct page *split_at = folio_page(folio, 4817 in_folio_offset); 4818 if (!folio_split(folio, target_order, split_at, NULL)) 4819 split++; 4820 } 4821 4822 unlock: 4823 folio_unlock(folio); 4824 next: 4825 folio_put(folio); 4826 cond_resched(); 4827 } 4828 4829 filp_close(candidate, NULL); 4830 pr_debug("%lu of %lu file-backed THP split\n", split, total); 4831 return 0; 4832 } 4833 4834 #define MAX_INPUT_BUF_SZ 255 4835 4836 static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, 4837 size_t count, loff_t *ppops) 4838 { 4839 static DEFINE_MUTEX(split_debug_mutex); 4840 ssize_t ret; 4841 /* 4842 * hold pid, start_vaddr, end_vaddr, new_order or 4843 * file_path, off_start, off_end, new_order 4844 */ 4845 char input_buf[MAX_INPUT_BUF_SZ]; 4846 int pid; 4847 unsigned long vaddr_start, vaddr_end; 4848 unsigned int new_order = 0; 4849 long in_folio_offset = -1; 4850 4851 ret = mutex_lock_interruptible(&split_debug_mutex); 4852 if (ret) 4853 return ret; 4854 4855 ret = -EFAULT; 4856 4857 memset(input_buf, 0, MAX_INPUT_BUF_SZ); 4858 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ))) 4859 goto out; 4860 4861 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0'; 4862 4863 if (input_buf[0] == '/') { 4864 char *tok; 4865 char *tok_buf = input_buf; 4866 char file_path[MAX_INPUT_BUF_SZ]; 4867 pgoff_t off_start = 0, off_end = 0; 4868 size_t input_len = strlen(input_buf); 4869 4870 tok = strsep(&tok_buf, ","); 4871 if (tok && tok_buf) { 4872 strscpy(file_path, tok); 4873 } else { 4874 ret = -EINVAL; 4875 goto out; 4876 } 4877 4878 ret = sscanf(tok_buf, "0x%lx,0x%lx,%d,%ld", &off_start, &off_end, 4879 &new_order, &in_folio_offset); 4880 if (ret != 2 && ret != 3 && ret != 4) { 4881 ret = -EINVAL; 4882 goto out; 4883 } 4884 ret = split_huge_pages_in_file(file_path, off_start, off_end, 4885 new_order, in_folio_offset); 4886 if (!ret) 4887 ret = input_len; 4888 4889 goto out; 4890 } 4891 4892 ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d,%ld", &pid, &vaddr_start, 4893 &vaddr_end, &new_order, &in_folio_offset); 4894 if (ret == 1 && pid == 1) { 4895 split_huge_pages_all(); 4896 ret = strlen(input_buf); 4897 goto out; 4898 } else if (ret != 3 && ret != 4 && ret != 5) { 4899 ret = -EINVAL; 4900 goto out; 4901 } 4902 4903 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order, 4904 in_folio_offset); 4905 if (!ret) 4906 ret = strlen(input_buf); 4907 out: 4908 mutex_unlock(&split_debug_mutex); 4909 return ret; 4910 4911 } 4912 4913 static const struct file_operations split_huge_pages_fops = { 4914 .owner = THIS_MODULE, 4915 .write = split_huge_pages_write, 4916 }; 4917 4918 static int __init split_huge_pages_debugfs(void) 4919 { 4920 debugfs_create_file("split_huge_pages", 0200, NULL, NULL, 4921 &split_huge_pages_fops); 4922 return 0; 4923 } 4924 late_initcall(split_huge_pages_debugfs); 4925 #endif 4926 4927 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION 4928 int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, 4929 struct page *page) 4930 { 4931 struct folio *folio = page_folio(page); 4932 struct vm_area_struct *vma = pvmw->vma; 4933 struct mm_struct *mm = vma->vm_mm; 4934 unsigned long address = pvmw->address; 4935 bool anon_exclusive, present, writable, softdirty, uffd_wp; 4936 pmd_t pmdval; 4937 swp_entry_t entry; 4938 pmd_t pmdswp; 4939 4940 if (!(pvmw->pmd && !pvmw->pte)) 4941 return 0; 4942 4943 present = pmd_present(*pvmw->pmd); 4944 if (likely(present)) { 4945 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE); 4946 4947 pmdval = pmdp_invalidate(vma, address, pvmw->pmd); 4948 4949 writable = pmd_write(pmdval); 4950 softdirty = pmd_soft_dirty(pmdval); 4951 uffd_wp = pmd_uffd_wp(pmdval); 4952 } else { 4953 softleaf_t old_entry; 4954 4955 pmdval = pmdp_huge_get_and_clear(vma->vm_mm, address, pvmw->pmd); 4956 old_entry = softleaf_from_pmd(pmdval); 4957 4958 writable = softleaf_is_device_private_write(old_entry); 4959 softdirty = pmd_swp_soft_dirty(pmdval); 4960 uffd_wp = pmd_swp_uffd_wp(pmdval); 4961 } 4962 4963 /* See folio_try_share_anon_rmap_pmd(): invalidate PMD first. */ 4964 anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(page); 4965 if (anon_exclusive && folio_try_share_anon_rmap_pmd(folio, page)) { 4966 set_pmd_at(mm, address, pvmw->pmd, pmdval); 4967 return -EBUSY; 4968 } 4969 4970 /* Determine type of migration entry. */ 4971 if (writable) 4972 entry = make_writable_migration_entry(page_to_pfn(page)); 4973 else if (anon_exclusive) 4974 entry = make_readable_exclusive_migration_entry(page_to_pfn(page)); 4975 else 4976 entry = make_readable_migration_entry(page_to_pfn(page)); 4977 4978 /* Set A/D bits as necessary. */ 4979 if (present && pmd_young(pmdval)) 4980 entry = make_migration_entry_young(entry); 4981 if (present && pmd_dirty(pmdval)) { 4982 folio_mark_dirty(folio); 4983 entry = make_migration_entry_dirty(entry); 4984 } 4985 4986 /* Set PMD. */ 4987 pmdswp = swp_entry_to_pmd(entry); 4988 if (softdirty) 4989 pmdswp = pmd_swp_mksoft_dirty(pmdswp); 4990 if (uffd_wp) 4991 pmdswp = pmd_swp_mkuffd_wp(pmdswp); 4992 set_pmd_at(mm, address, pvmw->pmd, pmdswp); 4993 4994 /* Migration entry installed: cleanup rmap, folio. */ 4995 folio_remove_rmap_pmd(folio, page, vma); 4996 folio_put(folio); 4997 trace_set_migration_pmd(address, pmd_val(pmdswp)); 4998 4999 return 0; 5000 } 5001 5002 void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) 5003 { 5004 struct folio *folio = page_folio(new); 5005 struct vm_area_struct *vma = pvmw->vma; 5006 struct mm_struct *mm = vma->vm_mm; 5007 unsigned long address = pvmw->address; 5008 unsigned long haddr = address & HPAGE_PMD_MASK; 5009 pmd_t pmde; 5010 softleaf_t entry; 5011 5012 if (!(pvmw->pmd && !pvmw->pte)) 5013 return; 5014 5015 entry = softleaf_from_pmd(*pvmw->pmd); 5016 folio_get(folio); 5017 pmde = folio_mk_pmd(folio, READ_ONCE(vma->vm_page_prot)); 5018 5019 if (pmd_swp_soft_dirty(*pvmw->pmd)) 5020 pmde = pmd_mksoft_dirty(pmde); 5021 if (softleaf_is_migration_write(entry)) 5022 pmde = pmd_mkwrite(pmde, vma); 5023 if (pmd_swp_uffd_wp(*pvmw->pmd)) 5024 pmde = pmd_mkuffd_wp(pmde); 5025 if (!softleaf_is_migration_young(entry)) 5026 pmde = pmd_mkold(pmde); 5027 /* NOTE: this may contain setting soft-dirty on some archs */ 5028 if (folio_test_dirty(folio) && softleaf_is_migration_dirty(entry)) 5029 pmde = pmd_mkdirty(pmde); 5030 5031 if (folio_is_device_private(folio)) { 5032 swp_entry_t entry; 5033 5034 if (pmd_write(pmde)) 5035 entry = make_writable_device_private_entry( 5036 page_to_pfn(new)); 5037 else 5038 entry = make_readable_device_private_entry( 5039 page_to_pfn(new)); 5040 pmde = swp_entry_to_pmd(entry); 5041 5042 if (pmd_swp_soft_dirty(*pvmw->pmd)) 5043 pmde = pmd_swp_mksoft_dirty(pmde); 5044 if (pmd_swp_uffd_wp(*pvmw->pmd)) 5045 pmde = pmd_swp_mkuffd_wp(pmde); 5046 } 5047 5048 if (folio_test_anon(folio)) { 5049 rmap_t rmap_flags = RMAP_NONE; 5050 5051 if (!softleaf_is_migration_read(entry)) 5052 rmap_flags |= RMAP_EXCLUSIVE; 5053 5054 folio_add_anon_rmap_pmd(folio, new, vma, haddr, rmap_flags); 5055 } else { 5056 folio_add_file_rmap_pmd(folio, new, vma); 5057 } 5058 VM_BUG_ON(pmd_write(pmde) && folio_test_anon(folio) && !PageAnonExclusive(new)); 5059 set_pmd_at(mm, haddr, pvmw->pmd, pmde); 5060 5061 /* No need to invalidate - it was non-present before */ 5062 update_mmu_cache_pmd(vma, address, pvmw->pmd); 5063 trace_remove_migration_pmd(address, pmd_val(pmde)); 5064 } 5065 #endif 5066