1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic hugetlb support. 4 * (C) Nadia Yvette Chambers, April 2004 5 */ 6 #include <linux/list.h> 7 #include <linux/init.h> 8 #include <linux/mm.h> 9 #include <linux/seq_file.h> 10 #include <linux/highmem.h> 11 #include <linux/mmu_notifier.h> 12 #include <linux/nodemask.h> 13 #include <linux/pagemap.h> 14 #include <linux/mempolicy.h> 15 #include <linux/compiler.h> 16 #include <linux/cpumask.h> 17 #include <linux/cpuset.h> 18 #include <linux/mutex.h> 19 #include <linux/memblock.h> 20 #include <linux/minmax.h> 21 #include <linux/slab.h> 22 #include <linux/sched/mm.h> 23 #include <linux/mmdebug.h> 24 #include <linux/sched/signal.h> 25 #include <linux/rmap.h> 26 #include <linux/string_choices.h> 27 #include <linux/string_helpers.h> 28 #include <linux/swap.h> 29 #include <linux/leafops.h> 30 #include <linux/jhash.h> 31 #include <linux/numa.h> 32 #include <linux/llist.h> 33 #include <linux/cma.h> 34 #include <linux/migrate.h> 35 #include <linux/nospec.h> 36 #include <linux/delayacct.h> 37 #include <linux/memory.h> 38 #include <linux/mm_inline.h> 39 #include <linux/padata.h> 40 #include <linux/pgalloc.h> 41 42 #include <asm/page.h> 43 #include <asm/tlb.h> 44 #include <asm/setup.h> 45 46 #include <linux/io.h> 47 #include <linux/node.h> 48 #include <linux/page_owner.h> 49 #include "internal.h" 50 #include "hugetlb_vmemmap.h" 51 #include "hugetlb_cma.h" 52 #include "hugetlb_internal.h" 53 #include <linux/page-isolation.h> 54 55 int hugetlb_max_hstate __read_mostly; 56 unsigned int default_hstate_idx; 57 struct hstate hstates[HUGE_MAX_HSTATE]; 58 59 __initdata nodemask_t hugetlb_bootmem_nodes; 60 __initdata struct list_head huge_boot_pages[MAX_NUMNODES]; 61 static unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE] __initdata; 62 63 /* 64 * Due to ordering constraints across the init code for various 65 * architectures, hugetlb hstate cmdline parameters can't simply 66 * be early_param. early_param might call the setup function 67 * before valid hugetlb page sizes are determined, leading to 68 * incorrect rejection of valid hugepagesz= options. 69 * 70 * So, record the parameters early and consume them whenever the 71 * init code is ready for them, by calling hugetlb_parse_params(). 72 */ 73 74 /* one (hugepagesz=,hugepages=) pair per hstate, one default_hugepagesz */ 75 #define HUGE_MAX_CMDLINE_ARGS (2 * HUGE_MAX_HSTATE + 1) 76 struct hugetlb_cmdline { 77 char *val; 78 int (*setup)(char *val); 79 }; 80 81 /* for command line parsing */ 82 static struct hstate * __initdata parsed_hstate; 83 static unsigned long __initdata default_hstate_max_huge_pages; 84 static bool __initdata parsed_valid_hugepagesz = true; 85 static bool __initdata parsed_default_hugepagesz; 86 static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata; 87 static unsigned long hugepage_allocation_threads __initdata; 88 89 static char hstate_cmdline_buf[COMMAND_LINE_SIZE] __initdata; 90 static int hstate_cmdline_index __initdata; 91 static struct hugetlb_cmdline hugetlb_params[HUGE_MAX_CMDLINE_ARGS] __initdata; 92 static int hugetlb_param_index __initdata; 93 static __init int hugetlb_add_param(char *s, int (*setup)(char *val)); 94 static __init void hugetlb_parse_params(void); 95 96 #define hugetlb_early_param(str, func) \ 97 static __init int func##args(char *s) \ 98 { \ 99 return hugetlb_add_param(s, func); \ 100 } \ 101 early_param(str, func##args) 102 103 /* 104 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages, 105 * free_huge_pages, and surplus_huge_pages. 106 */ 107 __cacheline_aligned_in_smp DEFINE_SPINLOCK(hugetlb_lock); 108 109 /* 110 * Serializes faults on the same logical page. This is used to 111 * prevent spurious OOMs when the hugepage pool is fully utilized. 112 */ 113 static int num_fault_mutexes __ro_after_init; 114 struct mutex *hugetlb_fault_mutex_table __ro_after_init; 115 116 /* Forward declaration */ 117 static int hugetlb_acct_memory(struct hstate *h, long delta); 118 static void hugetlb_vma_lock_free(struct vm_area_struct *vma); 119 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma); 120 static void hugetlb_unshare_pmds(struct vm_area_struct *vma, 121 unsigned long start, unsigned long end, bool take_locks); 122 static struct resv_map *vma_resv_map(struct vm_area_struct *vma); 123 124 static inline bool subpool_is_free(struct hugepage_subpool *spool) 125 { 126 if (spool->count) 127 return false; 128 if (spool->max_hpages != -1) 129 return spool->used_hpages == 0; 130 if (spool->min_hpages != -1) 131 return spool->rsv_hpages == spool->min_hpages; 132 133 return true; 134 } 135 136 static inline void unlock_or_release_subpool(struct hugepage_subpool *spool, 137 unsigned long irq_flags) 138 { 139 spin_unlock_irqrestore(&spool->lock, irq_flags); 140 141 /* If no pages are used, and no other handles to the subpool 142 * remain, give up any reservations based on minimum size and 143 * free the subpool */ 144 if (subpool_is_free(spool)) { 145 if (spool->min_hpages != -1) 146 hugetlb_acct_memory(spool->hstate, 147 -spool->min_hpages); 148 kfree(spool); 149 } 150 } 151 152 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 153 long min_hpages) 154 { 155 struct hugepage_subpool *spool; 156 157 spool = kzalloc_obj(*spool); 158 if (!spool) 159 return NULL; 160 161 spin_lock_init(&spool->lock); 162 spool->count = 1; 163 spool->max_hpages = max_hpages; 164 spool->hstate = h; 165 spool->min_hpages = min_hpages; 166 167 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { 168 kfree(spool); 169 return NULL; 170 } 171 spool->rsv_hpages = min_hpages; 172 173 return spool; 174 } 175 176 void hugepage_put_subpool(struct hugepage_subpool *spool) 177 { 178 unsigned long flags; 179 180 spin_lock_irqsave(&spool->lock, flags); 181 BUG_ON(!spool->count); 182 spool->count--; 183 unlock_or_release_subpool(spool, flags); 184 } 185 186 /* 187 * Subpool accounting for allocating and reserving pages. 188 * Return -ENOMEM if there are not enough resources to satisfy the 189 * request. Otherwise, return the number of pages by which the 190 * global pools must be adjusted (upward). The returned value may 191 * only be different than the passed value (delta) in the case where 192 * a subpool minimum size must be maintained. 193 */ 194 static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, 195 long delta) 196 { 197 long ret = delta; 198 199 if (!spool) 200 return ret; 201 202 spin_lock_irq(&spool->lock); 203 204 if (spool->max_hpages != -1) { /* maximum size accounting */ 205 if ((spool->used_hpages + delta) <= spool->max_hpages) 206 spool->used_hpages += delta; 207 else { 208 ret = -ENOMEM; 209 goto unlock_ret; 210 } 211 } 212 213 /* minimum size accounting */ 214 if (spool->min_hpages != -1 && spool->rsv_hpages) { 215 if (delta > spool->rsv_hpages) { 216 /* 217 * Asking for more reserves than those already taken on 218 * behalf of subpool. Return difference. 219 */ 220 ret = delta - spool->rsv_hpages; 221 spool->rsv_hpages = 0; 222 } else { 223 ret = 0; /* reserves already accounted for */ 224 spool->rsv_hpages -= delta; 225 } 226 } 227 228 unlock_ret: 229 spin_unlock_irq(&spool->lock); 230 return ret; 231 } 232 233 /* 234 * Subpool accounting for freeing and unreserving pages. 235 * Return the number of global page reservations that must be dropped. 236 * The return value may only be different than the passed value (delta) 237 * in the case where a subpool minimum size must be maintained. 238 */ 239 static long hugepage_subpool_put_pages(struct hugepage_subpool *spool, 240 long delta) 241 { 242 long ret = delta; 243 unsigned long flags; 244 245 if (!spool) 246 return delta; 247 248 spin_lock_irqsave(&spool->lock, flags); 249 250 if (spool->max_hpages != -1) /* maximum size accounting */ 251 spool->used_hpages -= delta; 252 253 /* minimum size accounting */ 254 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) { 255 if (spool->rsv_hpages + delta <= spool->min_hpages) 256 ret = 0; 257 else 258 ret = spool->rsv_hpages + delta - spool->min_hpages; 259 260 spool->rsv_hpages += delta; 261 if (spool->rsv_hpages > spool->min_hpages) 262 spool->rsv_hpages = spool->min_hpages; 263 } 264 265 /* 266 * If hugetlbfs_put_super couldn't free spool due to an outstanding 267 * quota reference, free it now. 268 */ 269 unlock_or_release_subpool(spool, flags); 270 271 return ret; 272 } 273 274 static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma) 275 { 276 return subpool_inode(file_inode(vma->vm_file)); 277 } 278 279 /* 280 * hugetlb vma_lock helper routines 281 */ 282 void hugetlb_vma_lock_read(struct vm_area_struct *vma) 283 { 284 if (__vma_shareable_lock(vma)) { 285 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 286 287 down_read(&vma_lock->rw_sema); 288 } else if (__vma_private_lock(vma)) { 289 struct resv_map *resv_map = vma_resv_map(vma); 290 291 down_read(&resv_map->rw_sema); 292 } 293 } 294 295 void hugetlb_vma_unlock_read(struct vm_area_struct *vma) 296 { 297 if (__vma_shareable_lock(vma)) { 298 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 299 300 up_read(&vma_lock->rw_sema); 301 } else if (__vma_private_lock(vma)) { 302 struct resv_map *resv_map = vma_resv_map(vma); 303 304 up_read(&resv_map->rw_sema); 305 } 306 } 307 308 void hugetlb_vma_lock_write(struct vm_area_struct *vma) 309 { 310 if (__vma_shareable_lock(vma)) { 311 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 312 313 down_write(&vma_lock->rw_sema); 314 } else if (__vma_private_lock(vma)) { 315 struct resv_map *resv_map = vma_resv_map(vma); 316 317 down_write(&resv_map->rw_sema); 318 } 319 } 320 321 void hugetlb_vma_unlock_write(struct vm_area_struct *vma) 322 { 323 if (__vma_shareable_lock(vma)) { 324 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 325 326 up_write(&vma_lock->rw_sema); 327 } else if (__vma_private_lock(vma)) { 328 struct resv_map *resv_map = vma_resv_map(vma); 329 330 up_write(&resv_map->rw_sema); 331 } 332 } 333 334 int hugetlb_vma_trylock_write(struct vm_area_struct *vma) 335 { 336 337 if (__vma_shareable_lock(vma)) { 338 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 339 340 return down_write_trylock(&vma_lock->rw_sema); 341 } else if (__vma_private_lock(vma)) { 342 struct resv_map *resv_map = vma_resv_map(vma); 343 344 return down_write_trylock(&resv_map->rw_sema); 345 } 346 347 return 1; 348 } 349 350 void hugetlb_vma_assert_locked(struct vm_area_struct *vma) 351 { 352 if (__vma_shareable_lock(vma)) { 353 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 354 355 lockdep_assert_held(&vma_lock->rw_sema); 356 } else if (__vma_private_lock(vma)) { 357 struct resv_map *resv_map = vma_resv_map(vma); 358 359 lockdep_assert_held(&resv_map->rw_sema); 360 } 361 } 362 363 void hugetlb_vma_lock_release(struct kref *kref) 364 { 365 struct hugetlb_vma_lock *vma_lock = container_of(kref, 366 struct hugetlb_vma_lock, refs); 367 368 kfree(vma_lock); 369 } 370 371 static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock) 372 { 373 struct vm_area_struct *vma = vma_lock->vma; 374 375 /* 376 * vma_lock structure may or not be released as a result of put, 377 * it certainly will no longer be attached to vma so clear pointer. 378 * Semaphore synchronizes access to vma_lock->vma field. 379 */ 380 vma_lock->vma = NULL; 381 vma->vm_private_data = NULL; 382 up_write(&vma_lock->rw_sema); 383 kref_put(&vma_lock->refs, hugetlb_vma_lock_release); 384 } 385 386 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma) 387 { 388 if (__vma_shareable_lock(vma)) { 389 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 390 391 __hugetlb_vma_unlock_write_put(vma_lock); 392 } else if (__vma_private_lock(vma)) { 393 struct resv_map *resv_map = vma_resv_map(vma); 394 395 /* no free for anon vmas, but still need to unlock */ 396 up_write(&resv_map->rw_sema); 397 } 398 } 399 400 static void hugetlb_vma_lock_free(struct vm_area_struct *vma) 401 { 402 /* 403 * Only present in sharable vmas. 404 */ 405 if (!vma || !__vma_shareable_lock(vma)) 406 return; 407 408 if (vma->vm_private_data) { 409 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 410 411 down_write(&vma_lock->rw_sema); 412 __hugetlb_vma_unlock_write_put(vma_lock); 413 } 414 } 415 416 /* 417 * vma specific semaphore used for pmd sharing and fault/truncation 418 * synchronization 419 */ 420 int hugetlb_vma_lock_alloc(struct vm_area_struct *vma) 421 { 422 struct hugetlb_vma_lock *vma_lock; 423 424 /* Only establish in (flags) sharable vmas */ 425 if (!vma || !(vma->vm_flags & VM_MAYSHARE)) 426 return 0; 427 428 /* Should never get here with non-NULL vm_private_data */ 429 if (vma->vm_private_data) 430 return -EINVAL; 431 432 vma_lock = kmalloc_obj(*vma_lock); 433 if (!vma_lock) { 434 /* 435 * If we can not allocate structure, then vma can not 436 * participate in pmd sharing. This is only a possible 437 * performance enhancement and memory saving issue. 438 * However, the lock is also used to synchronize page 439 * faults with truncation. If the lock is not present, 440 * unlikely races could leave pages in a file past i_size 441 * until the file is removed. Warn in the unlikely case of 442 * allocation failure. 443 */ 444 pr_warn_once("HugeTLB: unable to allocate vma specific lock\n"); 445 return -EINVAL; 446 } 447 448 kref_init(&vma_lock->refs); 449 init_rwsem(&vma_lock->rw_sema); 450 vma_lock->vma = vma; 451 vma->vm_private_data = vma_lock; 452 453 return 0; 454 } 455 456 /* Helper that removes a struct file_region from the resv_map cache and returns 457 * it for use. 458 */ 459 static struct file_region * 460 get_file_region_entry_from_cache(struct resv_map *resv, long from, long to) 461 { 462 struct file_region *nrg; 463 464 VM_BUG_ON(resv->region_cache_count <= 0); 465 466 resv->region_cache_count--; 467 nrg = list_first_entry(&resv->region_cache, struct file_region, link); 468 list_del(&nrg->link); 469 470 nrg->from = from; 471 nrg->to = to; 472 473 return nrg; 474 } 475 476 static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg, 477 struct file_region *rg) 478 { 479 #ifdef CONFIG_CGROUP_HUGETLB 480 nrg->reservation_counter = rg->reservation_counter; 481 nrg->css = rg->css; 482 if (rg->css) 483 css_get(rg->css); 484 #endif 485 } 486 487 /* Helper that records hugetlb_cgroup uncharge info. */ 488 static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg, 489 struct hstate *h, 490 struct resv_map *resv, 491 struct file_region *nrg) 492 { 493 #ifdef CONFIG_CGROUP_HUGETLB 494 if (h_cg) { 495 nrg->reservation_counter = 496 &h_cg->rsvd_hugepage[hstate_index(h)]; 497 nrg->css = &h_cg->css; 498 /* 499 * The caller will hold exactly one h_cg->css reference for the 500 * whole contiguous reservation region. But this area might be 501 * scattered when there are already some file_regions reside in 502 * it. As a result, many file_regions may share only one css 503 * reference. In order to ensure that one file_region must hold 504 * exactly one h_cg->css reference, we should do css_get for 505 * each file_region and leave the reference held by caller 506 * untouched. 507 */ 508 css_get(&h_cg->css); 509 if (!resv->pages_per_hpage) 510 resv->pages_per_hpage = pages_per_huge_page(h); 511 /* pages_per_hpage should be the same for all entries in 512 * a resv_map. 513 */ 514 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h)); 515 } else { 516 nrg->reservation_counter = NULL; 517 nrg->css = NULL; 518 } 519 #endif 520 } 521 522 static void put_uncharge_info(struct file_region *rg) 523 { 524 #ifdef CONFIG_CGROUP_HUGETLB 525 if (rg->css) 526 css_put(rg->css); 527 #endif 528 } 529 530 static bool has_same_uncharge_info(struct file_region *rg, 531 struct file_region *org) 532 { 533 #ifdef CONFIG_CGROUP_HUGETLB 534 return rg->reservation_counter == org->reservation_counter && 535 rg->css == org->css; 536 537 #else 538 return true; 539 #endif 540 } 541 542 static void coalesce_file_region(struct resv_map *resv, struct file_region *rg) 543 { 544 struct file_region *nrg, *prg; 545 546 prg = list_prev_entry(rg, link); 547 if (&prg->link != &resv->regions && prg->to == rg->from && 548 has_same_uncharge_info(prg, rg)) { 549 prg->to = rg->to; 550 551 list_del(&rg->link); 552 put_uncharge_info(rg); 553 kfree(rg); 554 555 rg = prg; 556 } 557 558 nrg = list_next_entry(rg, link); 559 if (&nrg->link != &resv->regions && nrg->from == rg->to && 560 has_same_uncharge_info(nrg, rg)) { 561 nrg->from = rg->from; 562 563 list_del(&rg->link); 564 put_uncharge_info(rg); 565 kfree(rg); 566 } 567 } 568 569 static inline long 570 hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from, 571 long to, struct hstate *h, struct hugetlb_cgroup *cg, 572 long *regions_needed) 573 { 574 struct file_region *nrg; 575 576 if (!regions_needed) { 577 nrg = get_file_region_entry_from_cache(map, from, to); 578 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg); 579 list_add(&nrg->link, rg); 580 coalesce_file_region(map, nrg); 581 } else { 582 *regions_needed += 1; 583 } 584 585 return to - from; 586 } 587 588 /* 589 * Must be called with resv->lock held. 590 * 591 * Calling this with regions_needed != NULL will count the number of pages 592 * to be added but will not modify the linked list. And regions_needed will 593 * indicate the number of file_regions needed in the cache to carry out to add 594 * the regions for this range. 595 */ 596 static long add_reservation_in_range(struct resv_map *resv, long f, long t, 597 struct hugetlb_cgroup *h_cg, 598 struct hstate *h, long *regions_needed) 599 { 600 long add = 0; 601 struct list_head *head = &resv->regions; 602 long last_accounted_offset = f; 603 struct file_region *iter, *trg = NULL; 604 struct list_head *rg = NULL; 605 606 if (regions_needed) 607 *regions_needed = 0; 608 609 /* In this loop, we essentially handle an entry for the range 610 * [last_accounted_offset, iter->from), at every iteration, with some 611 * bounds checking. 612 */ 613 list_for_each_entry_safe(iter, trg, head, link) { 614 /* Skip irrelevant regions that start before our range. */ 615 if (iter->from < f) { 616 /* If this region ends after the last accounted offset, 617 * then we need to update last_accounted_offset. 618 */ 619 if (iter->to > last_accounted_offset) 620 last_accounted_offset = iter->to; 621 continue; 622 } 623 624 /* When we find a region that starts beyond our range, we've 625 * finished. 626 */ 627 if (iter->from >= t) { 628 rg = iter->link.prev; 629 break; 630 } 631 632 /* Add an entry for last_accounted_offset -> iter->from, and 633 * update last_accounted_offset. 634 */ 635 if (iter->from > last_accounted_offset) 636 add += hugetlb_resv_map_add(resv, iter->link.prev, 637 last_accounted_offset, 638 iter->from, h, h_cg, 639 regions_needed); 640 641 last_accounted_offset = iter->to; 642 } 643 644 /* Handle the case where our range extends beyond 645 * last_accounted_offset. 646 */ 647 if (!rg) 648 rg = head->prev; 649 if (last_accounted_offset < t) 650 add += hugetlb_resv_map_add(resv, rg, last_accounted_offset, 651 t, h, h_cg, regions_needed); 652 653 return add; 654 } 655 656 /* Must be called with resv->lock acquired. Will drop lock to allocate entries. 657 */ 658 static int allocate_file_region_entries(struct resv_map *resv, 659 int regions_needed) 660 __must_hold(&resv->lock) 661 { 662 LIST_HEAD(allocated_regions); 663 int to_allocate = 0, i = 0; 664 struct file_region *trg = NULL, *rg = NULL; 665 666 VM_BUG_ON(regions_needed < 0); 667 668 /* 669 * Check for sufficient descriptors in the cache to accommodate 670 * the number of in progress add operations plus regions_needed. 671 * 672 * This is a while loop because when we drop the lock, some other call 673 * to region_add or region_del may have consumed some region_entries, 674 * so we keep looping here until we finally have enough entries for 675 * (adds_in_progress + regions_needed). 676 */ 677 while (resv->region_cache_count < 678 (resv->adds_in_progress + regions_needed)) { 679 to_allocate = resv->adds_in_progress + regions_needed - 680 resv->region_cache_count; 681 682 /* At this point, we should have enough entries in the cache 683 * for all the existing adds_in_progress. We should only be 684 * needing to allocate for regions_needed. 685 */ 686 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress); 687 688 spin_unlock(&resv->lock); 689 for (i = 0; i < to_allocate; i++) { 690 trg = kmalloc_obj(*trg); 691 if (!trg) 692 goto out_of_memory; 693 list_add(&trg->link, &allocated_regions); 694 } 695 696 spin_lock(&resv->lock); 697 698 list_splice(&allocated_regions, &resv->region_cache); 699 resv->region_cache_count += to_allocate; 700 } 701 702 return 0; 703 704 out_of_memory: 705 list_for_each_entry_safe(rg, trg, &allocated_regions, link) { 706 list_del(&rg->link); 707 kfree(rg); 708 } 709 return -ENOMEM; 710 } 711 712 /* 713 * Add the huge page range represented by [f, t) to the reserve 714 * map. Regions will be taken from the cache to fill in this range. 715 * Sufficient regions should exist in the cache due to the previous 716 * call to region_chg with the same range, but in some cases the cache will not 717 * have sufficient entries due to races with other code doing region_add or 718 * region_del. The extra needed entries will be allocated. 719 * 720 * regions_needed is the out value provided by a previous call to region_chg. 721 * 722 * Return the number of new huge pages added to the map. This number is greater 723 * than or equal to zero. If file_region entries needed to be allocated for 724 * this operation and we were not able to allocate, it returns -ENOMEM. 725 * region_add of regions of length 1 never allocate file_regions and cannot 726 * fail; region_chg will always allocate at least 1 entry and a region_add for 727 * 1 page will only require at most 1 entry. 728 */ 729 static long region_add(struct resv_map *resv, long f, long t, 730 long in_regions_needed, struct hstate *h, 731 struct hugetlb_cgroup *h_cg) 732 { 733 long add = 0, actual_regions_needed = 0; 734 735 spin_lock(&resv->lock); 736 retry: 737 738 /* Count how many regions are actually needed to execute this add. */ 739 add_reservation_in_range(resv, f, t, NULL, NULL, 740 &actual_regions_needed); 741 742 /* 743 * Check for sufficient descriptors in the cache to accommodate 744 * this add operation. Note that actual_regions_needed may be greater 745 * than in_regions_needed, as the resv_map may have been modified since 746 * the region_chg call. In this case, we need to make sure that we 747 * allocate extra entries, such that we have enough for all the 748 * existing adds_in_progress, plus the excess needed for this 749 * operation. 750 */ 751 if (actual_regions_needed > in_regions_needed && 752 resv->region_cache_count < 753 resv->adds_in_progress + 754 (actual_regions_needed - in_regions_needed)) { 755 /* region_add operation of range 1 should never need to 756 * allocate file_region entries. 757 */ 758 VM_BUG_ON(t - f <= 1); 759 760 if (allocate_file_region_entries( 761 resv, actual_regions_needed - in_regions_needed)) { 762 return -ENOMEM; 763 } 764 765 goto retry; 766 } 767 768 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL); 769 770 resv->adds_in_progress -= in_regions_needed; 771 772 spin_unlock(&resv->lock); 773 return add; 774 } 775 776 /* 777 * Examine the existing reserve map and determine how many 778 * huge pages in the specified range [f, t) are NOT currently 779 * represented. This routine is called before a subsequent 780 * call to region_add that will actually modify the reserve 781 * map to add the specified range [f, t). region_chg does 782 * not change the number of huge pages represented by the 783 * map. A number of new file_region structures is added to the cache as a 784 * placeholder, for the subsequent region_add call to use. At least 1 785 * file_region structure is added. 786 * 787 * out_regions_needed is the number of regions added to the 788 * resv->adds_in_progress. This value needs to be provided to a follow up call 789 * to region_add or region_abort for proper accounting. 790 * 791 * Returns the number of huge pages that need to be added to the existing 792 * reservation map for the range [f, t). This number is greater or equal to 793 * zero. -ENOMEM is returned if a new file_region structure or cache entry 794 * is needed and can not be allocated. 795 */ 796 static long region_chg(struct resv_map *resv, long f, long t, 797 long *out_regions_needed) 798 { 799 long chg = 0; 800 801 spin_lock(&resv->lock); 802 803 /* Count how many hugepages in this range are NOT represented. */ 804 chg = add_reservation_in_range(resv, f, t, NULL, NULL, 805 out_regions_needed); 806 807 if (*out_regions_needed == 0) 808 *out_regions_needed = 1; 809 810 if (allocate_file_region_entries(resv, *out_regions_needed)) 811 return -ENOMEM; 812 813 resv->adds_in_progress += *out_regions_needed; 814 815 spin_unlock(&resv->lock); 816 return chg; 817 } 818 819 /* 820 * Abort the in progress add operation. The adds_in_progress field 821 * of the resv_map keeps track of the operations in progress between 822 * calls to region_chg and region_add. Operations are sometimes 823 * aborted after the call to region_chg. In such cases, region_abort 824 * is called to decrement the adds_in_progress counter. regions_needed 825 * is the value returned by the region_chg call, it is used to decrement 826 * the adds_in_progress counter. 827 * 828 * NOTE: The range arguments [f, t) are not needed or used in this 829 * routine. They are kept to make reading the calling code easier as 830 * arguments will match the associated region_chg call. 831 */ 832 static void region_abort(struct resv_map *resv, long f, long t, 833 long regions_needed) 834 { 835 spin_lock(&resv->lock); 836 VM_BUG_ON(!resv->region_cache_count); 837 resv->adds_in_progress -= regions_needed; 838 spin_unlock(&resv->lock); 839 } 840 841 /* 842 * Delete the specified range [f, t) from the reserve map. If the 843 * t parameter is LONG_MAX, this indicates that ALL regions after f 844 * should be deleted. Locate the regions which intersect [f, t) 845 * and either trim, delete or split the existing regions. 846 * 847 * Returns the number of huge pages deleted from the reserve map. 848 * In the normal case, the return value is zero or more. In the 849 * case where a region must be split, a new region descriptor must 850 * be allocated. If the allocation fails, -ENOMEM will be returned. 851 * NOTE: If the parameter t == LONG_MAX, then we will never split 852 * a region and possibly return -ENOMEM. Callers specifying 853 * t == LONG_MAX do not need to check for -ENOMEM error. 854 */ 855 static long region_del(struct resv_map *resv, long f, long t) 856 { 857 struct list_head *head = &resv->regions; 858 struct file_region *rg, *trg; 859 struct file_region *nrg = NULL; 860 long del = 0; 861 862 retry: 863 spin_lock(&resv->lock); 864 list_for_each_entry_safe(rg, trg, head, link) { 865 /* 866 * Skip regions before the range to be deleted. file_region 867 * ranges are normally of the form [from, to). However, there 868 * may be a "placeholder" entry in the map which is of the form 869 * (from, to) with from == to. Check for placeholder entries 870 * at the beginning of the range to be deleted. 871 */ 872 if (rg->to <= f && (rg->to != rg->from || rg->to != f)) 873 continue; 874 875 if (rg->from >= t) 876 break; 877 878 if (f > rg->from && t < rg->to) { /* Must split region */ 879 /* 880 * Check for an entry in the cache before dropping 881 * lock and attempting allocation. 882 */ 883 if (!nrg && 884 resv->region_cache_count > resv->adds_in_progress) { 885 nrg = list_first_entry(&resv->region_cache, 886 struct file_region, 887 link); 888 list_del(&nrg->link); 889 resv->region_cache_count--; 890 } 891 892 if (!nrg) { 893 spin_unlock(&resv->lock); 894 nrg = kmalloc_obj(*nrg); 895 if (!nrg) 896 return -ENOMEM; 897 goto retry; 898 } 899 900 del += t - f; 901 hugetlb_cgroup_uncharge_file_region( 902 resv, rg, t - f, false); 903 904 /* New entry for end of split region */ 905 nrg->from = t; 906 nrg->to = rg->to; 907 908 copy_hugetlb_cgroup_uncharge_info(nrg, rg); 909 910 INIT_LIST_HEAD(&nrg->link); 911 912 /* Original entry is trimmed */ 913 rg->to = f; 914 915 list_add(&nrg->link, &rg->link); 916 nrg = NULL; 917 break; 918 } 919 920 if (f <= rg->from && t >= rg->to) { /* Remove entire region */ 921 del += rg->to - rg->from; 922 hugetlb_cgroup_uncharge_file_region(resv, rg, 923 rg->to - rg->from, true); 924 list_del(&rg->link); 925 kfree(rg); 926 continue; 927 } 928 929 if (f <= rg->from) { /* Trim beginning of region */ 930 hugetlb_cgroup_uncharge_file_region(resv, rg, 931 t - rg->from, false); 932 933 del += t - rg->from; 934 rg->from = t; 935 } else { /* Trim end of region */ 936 hugetlb_cgroup_uncharge_file_region(resv, rg, 937 rg->to - f, false); 938 939 del += rg->to - f; 940 rg->to = f; 941 } 942 } 943 944 spin_unlock(&resv->lock); 945 kfree(nrg); 946 return del; 947 } 948 949 /* 950 * A rare out of memory error was encountered which prevented removal of 951 * the reserve map region for a page. The huge page itself was free'ed 952 * and removed from the page cache. This routine will adjust the subpool 953 * usage count, and the global reserve count if needed. By incrementing 954 * these counts, the reserve map entry which could not be deleted will 955 * appear as a "reserved" entry instead of simply dangling with incorrect 956 * counts. 957 */ 958 void hugetlb_fix_reserve_counts(struct inode *inode) 959 { 960 struct hugepage_subpool *spool = subpool_inode(inode); 961 long rsv_adjust; 962 bool reserved = false; 963 964 rsv_adjust = hugepage_subpool_get_pages(spool, 1); 965 if (rsv_adjust > 0) { 966 struct hstate *h = hstate_inode(inode); 967 968 if (!hugetlb_acct_memory(h, 1)) 969 reserved = true; 970 } else if (!rsv_adjust) { 971 reserved = true; 972 } 973 974 if (!reserved) 975 pr_warn("hugetlb: Huge Page Reserved count may go negative.\n"); 976 } 977 978 /* 979 * Count and return the number of huge pages in the reserve map 980 * that intersect with the range [f, t). 981 */ 982 static long region_count(struct resv_map *resv, long f, long t) 983 { 984 struct list_head *head = &resv->regions; 985 struct file_region *rg; 986 long chg = 0; 987 988 spin_lock(&resv->lock); 989 /* Locate each segment we overlap with, and count that overlap. */ 990 list_for_each_entry(rg, head, link) { 991 long seg_from; 992 long seg_to; 993 994 if (rg->to <= f) 995 continue; 996 if (rg->from >= t) 997 break; 998 999 seg_from = max(rg->from, f); 1000 seg_to = min(rg->to, t); 1001 1002 chg += seg_to - seg_from; 1003 } 1004 spin_unlock(&resv->lock); 1005 1006 return chg; 1007 } 1008 1009 /* 1010 * Convert the address within this vma to the page offset within 1011 * the mapping, huge page units here. 1012 */ 1013 static pgoff_t vma_hugecache_offset(struct hstate *h, 1014 struct vm_area_struct *vma, unsigned long address) 1015 { 1016 return ((address - vma->vm_start) >> huge_page_shift(h)) + 1017 (vma->vm_pgoff >> huge_page_order(h)); 1018 } 1019 1020 /* 1021 * Flags for MAP_PRIVATE reservations. These are stored in the bottom 1022 * bits of the reservation map pointer, which are always clear due to 1023 * alignment. 1024 */ 1025 #define HPAGE_RESV_OWNER (1UL << 0) 1026 #define HPAGE_RESV_UNMAPPED (1UL << 1) 1027 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) 1028 1029 /* 1030 * These helpers are used to track how many pages are reserved for 1031 * faults in a MAP_PRIVATE mapping. Only the process that called mmap() 1032 * is guaranteed to have their future faults succeed. 1033 * 1034 * With the exception of hugetlb_dup_vma_private() which is called at fork(), 1035 * the reserve counters are updated with the hugetlb_lock held. It is safe 1036 * to reset the VMA at fork() time as it is not in use yet and there is no 1037 * chance of the global counters getting corrupted as a result of the values. 1038 * 1039 * The private mapping reservation is represented in a subtly different 1040 * manner to a shared mapping. A shared mapping has a region map associated 1041 * with the underlying file, this region map represents the backing file 1042 * pages which have ever had a reservation assigned which this persists even 1043 * after the page is instantiated. A private mapping has a region map 1044 * associated with the original mmap which is attached to all VMAs which 1045 * reference it, this region map represents those offsets which have consumed 1046 * reservation ie. where pages have been instantiated. 1047 */ 1048 static unsigned long get_vma_private_data(struct vm_area_struct *vma) 1049 { 1050 return (unsigned long)vma->vm_private_data; 1051 } 1052 1053 static void set_vma_private_data(struct vm_area_struct *vma, 1054 unsigned long value) 1055 { 1056 vma->vm_private_data = (void *)value; 1057 } 1058 1059 static void 1060 resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map, 1061 struct hugetlb_cgroup *h_cg, 1062 struct hstate *h) 1063 { 1064 #ifdef CONFIG_CGROUP_HUGETLB 1065 if (!h_cg || !h) { 1066 resv_map->reservation_counter = NULL; 1067 resv_map->pages_per_hpage = 0; 1068 resv_map->css = NULL; 1069 } else { 1070 resv_map->reservation_counter = 1071 &h_cg->rsvd_hugepage[hstate_index(h)]; 1072 resv_map->pages_per_hpage = pages_per_huge_page(h); 1073 resv_map->css = &h_cg->css; 1074 } 1075 #endif 1076 } 1077 1078 struct resv_map *resv_map_alloc(void) 1079 { 1080 struct resv_map *resv_map = kmalloc_obj(*resv_map); 1081 struct file_region *rg = kmalloc_obj(*rg); 1082 1083 if (!resv_map || !rg) { 1084 kfree(resv_map); 1085 kfree(rg); 1086 return NULL; 1087 } 1088 1089 kref_init(&resv_map->refs); 1090 spin_lock_init(&resv_map->lock); 1091 INIT_LIST_HEAD(&resv_map->regions); 1092 init_rwsem(&resv_map->rw_sema); 1093 1094 resv_map->adds_in_progress = 0; 1095 /* 1096 * Initialize these to 0. On shared mappings, 0's here indicate these 1097 * fields don't do cgroup accounting. On private mappings, these will be 1098 * re-initialized to the proper values, to indicate that hugetlb cgroup 1099 * reservations are to be un-charged from here. 1100 */ 1101 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL); 1102 1103 INIT_LIST_HEAD(&resv_map->region_cache); 1104 list_add(&rg->link, &resv_map->region_cache); 1105 resv_map->region_cache_count = 1; 1106 1107 return resv_map; 1108 } 1109 1110 void resv_map_release(struct kref *ref) 1111 { 1112 struct resv_map *resv_map = container_of(ref, struct resv_map, refs); 1113 struct list_head *head = &resv_map->region_cache; 1114 struct file_region *rg, *trg; 1115 1116 /* Clear out any active regions before we release the map. */ 1117 region_del(resv_map, 0, LONG_MAX); 1118 1119 /* ... and any entries left in the cache */ 1120 list_for_each_entry_safe(rg, trg, head, link) { 1121 list_del(&rg->link); 1122 kfree(rg); 1123 } 1124 1125 VM_BUG_ON(resv_map->adds_in_progress); 1126 1127 kfree(resv_map); 1128 } 1129 1130 static inline struct resv_map *inode_resv_map(struct inode *inode) 1131 { 1132 return HUGETLBFS_I(inode)->resv_map; 1133 } 1134 1135 static struct resv_map *vma_resv_map(struct vm_area_struct *vma) 1136 { 1137 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 1138 if (vma->vm_flags & VM_MAYSHARE) { 1139 struct address_space *mapping = vma->vm_file->f_mapping; 1140 struct inode *inode = mapping->host; 1141 1142 return inode_resv_map(inode); 1143 1144 } else { 1145 return (struct resv_map *)(get_vma_private_data(vma) & 1146 ~HPAGE_RESV_MASK); 1147 } 1148 } 1149 1150 static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) 1151 { 1152 VM_WARN_ON_ONCE_VMA(!is_vm_hugetlb_page(vma), vma); 1153 VM_WARN_ON_ONCE_VMA(vma->vm_flags & VM_MAYSHARE, vma); 1154 1155 set_vma_private_data(vma, get_vma_private_data(vma) | flags); 1156 } 1157 1158 static void set_vma_desc_resv_map(struct vm_area_desc *desc, struct resv_map *map) 1159 { 1160 VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags)); 1161 VM_WARN_ON_ONCE(vma_desc_test(desc, VMA_MAYSHARE_BIT)); 1162 1163 desc->private_data = map; 1164 } 1165 1166 static void set_vma_desc_resv_flags(struct vm_area_desc *desc, unsigned long flags) 1167 { 1168 VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags)); 1169 VM_WARN_ON_ONCE(vma_desc_test(desc, VMA_MAYSHARE_BIT)); 1170 1171 desc->private_data = (void *)((unsigned long)desc->private_data | flags); 1172 } 1173 1174 static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) 1175 { 1176 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 1177 1178 return (get_vma_private_data(vma) & flag) != 0; 1179 } 1180 1181 static bool is_vma_desc_resv_set(struct vm_area_desc *desc, unsigned long flag) 1182 { 1183 VM_WARN_ON_ONCE(!is_vma_hugetlb_flags(&desc->vma_flags)); 1184 1185 return ((unsigned long)desc->private_data) & flag; 1186 } 1187 1188 bool __vma_private_lock(struct vm_area_struct *vma) 1189 { 1190 return !(vma->vm_flags & VM_MAYSHARE) && 1191 get_vma_private_data(vma) & ~HPAGE_RESV_MASK && 1192 is_vma_resv_set(vma, HPAGE_RESV_OWNER); 1193 } 1194 1195 void hugetlb_dup_vma_private(struct vm_area_struct *vma) 1196 { 1197 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma); 1198 /* 1199 * Clear vm_private_data 1200 * - For shared mappings this is a per-vma semaphore that may be 1201 * allocated in a subsequent call to hugetlb_vm_op_open. 1202 * Before clearing, make sure pointer is not associated with vma 1203 * as this will leak the structure. This is the case when called 1204 * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already 1205 * been called to allocate a new structure. 1206 * - For MAP_PRIVATE mappings, this is the reserve map which does 1207 * not apply to children. Faults generated by the children are 1208 * not guaranteed to succeed, even if read-only. 1209 */ 1210 if (vma->vm_flags & VM_MAYSHARE) { 1211 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 1212 1213 if (vma_lock && vma_lock->vma != vma) 1214 vma->vm_private_data = NULL; 1215 } else { 1216 vma->vm_private_data = NULL; 1217 } 1218 } 1219 1220 /* 1221 * Reset and decrement one ref on hugepage private reservation. 1222 * Called with mm->mmap_lock writer semaphore held. 1223 * This function should be only used by mremap and operate on 1224 * same sized vma. It should never come here with last ref on the 1225 * reservation. 1226 */ 1227 void clear_vma_resv_huge_pages(struct vm_area_struct *vma) 1228 { 1229 /* 1230 * Clear the old hugetlb private page reservation. 1231 * It has already been transferred to new_vma. 1232 * 1233 * During a mremap() operation of a hugetlb vma we call move_vma() 1234 * which copies vma into new_vma and unmaps vma. After the copy 1235 * operation both new_vma and vma share a reference to the resv_map 1236 * struct, and at that point vma is about to be unmapped. We don't 1237 * want to return the reservation to the pool at unmap of vma because 1238 * the reservation still lives on in new_vma, so simply decrement the 1239 * ref here and remove the resv_map reference from this vma. 1240 */ 1241 struct resv_map *reservations = vma_resv_map(vma); 1242 1243 if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 1244 resv_map_put_hugetlb_cgroup_uncharge_info(reservations); 1245 kref_put(&reservations->refs, resv_map_release); 1246 } 1247 1248 hugetlb_dup_vma_private(vma); 1249 } 1250 1251 static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio) 1252 { 1253 int nid = folio_nid(folio); 1254 1255 lockdep_assert_held(&hugetlb_lock); 1256 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 1257 1258 list_move(&folio->lru, &h->hugepage_freelists[nid]); 1259 h->free_huge_pages++; 1260 h->free_huge_pages_node[nid]++; 1261 folio_set_hugetlb_freed(folio); 1262 } 1263 1264 static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h, 1265 int nid) 1266 { 1267 struct folio *folio; 1268 bool pin = !!(current->flags & PF_MEMALLOC_PIN); 1269 1270 lockdep_assert_held(&hugetlb_lock); 1271 list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) { 1272 if (pin && !folio_is_longterm_pinnable(folio)) 1273 continue; 1274 1275 if (folio_test_hwpoison(folio)) 1276 continue; 1277 1278 if (is_migrate_isolate_page(&folio->page)) 1279 continue; 1280 1281 list_move(&folio->lru, &h->hugepage_activelist); 1282 folio_ref_unfreeze(folio, 1); 1283 folio_clear_hugetlb_freed(folio); 1284 h->free_huge_pages--; 1285 h->free_huge_pages_node[nid]--; 1286 return folio; 1287 } 1288 1289 return NULL; 1290 } 1291 1292 static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask, 1293 int nid, nodemask_t *nmask) 1294 { 1295 unsigned int cpuset_mems_cookie; 1296 struct zonelist *zonelist; 1297 struct zone *zone; 1298 struct zoneref *z; 1299 int node = NUMA_NO_NODE; 1300 1301 /* 'nid' should not be NUMA_NO_NODE. Try to catch any misuse of it and rectifiy. */ 1302 if (nid == NUMA_NO_NODE) 1303 nid = numa_node_id(); 1304 1305 zonelist = node_zonelist(nid, gfp_mask); 1306 1307 retry_cpuset: 1308 cpuset_mems_cookie = read_mems_allowed_begin(); 1309 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) { 1310 struct folio *folio; 1311 1312 if (!cpuset_zone_allowed(zone, gfp_mask)) 1313 continue; 1314 /* 1315 * no need to ask again on the same node. Pool is node rather than 1316 * zone aware 1317 */ 1318 if (zone_to_nid(zone) == node) 1319 continue; 1320 node = zone_to_nid(zone); 1321 1322 folio = dequeue_hugetlb_folio_node_exact(h, node); 1323 if (folio) 1324 return folio; 1325 } 1326 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie))) 1327 goto retry_cpuset; 1328 1329 return NULL; 1330 } 1331 1332 static unsigned long available_huge_pages(struct hstate *h) 1333 { 1334 return h->free_huge_pages - h->resv_huge_pages; 1335 } 1336 1337 static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h, 1338 struct vm_area_struct *vma, 1339 unsigned long address, long gbl_chg) 1340 { 1341 struct folio *folio = NULL; 1342 struct mempolicy *mpol; 1343 gfp_t gfp_mask; 1344 nodemask_t *nodemask; 1345 int nid; 1346 1347 /* 1348 * gbl_chg==1 means the allocation requires a new page that was not 1349 * reserved before. Making sure there's at least one free page. 1350 */ 1351 if (gbl_chg && !available_huge_pages(h)) 1352 goto err; 1353 1354 gfp_mask = htlb_alloc_mask(h); 1355 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 1356 1357 if (mpol_is_preferred_many(mpol)) { 1358 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 1359 nid, nodemask); 1360 1361 /* Fallback to all nodes if page==NULL */ 1362 nodemask = NULL; 1363 } 1364 1365 if (!folio) 1366 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 1367 nid, nodemask); 1368 1369 mpol_cond_put(mpol); 1370 return folio; 1371 1372 err: 1373 return NULL; 1374 } 1375 1376 #if defined(CONFIG_ARCH_HAS_GIGANTIC_PAGE) && defined(CONFIG_CONTIG_ALLOC) 1377 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, 1378 int nid, nodemask_t *nodemask) 1379 { 1380 struct folio *folio; 1381 1382 folio = hugetlb_cma_alloc_frozen_folio(order, gfp_mask, nid, nodemask); 1383 if (folio) 1384 return folio; 1385 1386 if (hugetlb_cma_exclusive_alloc()) 1387 return NULL; 1388 1389 folio = (struct folio *)alloc_contig_frozen_pages(1 << order, gfp_mask, 1390 nid, nodemask); 1391 return folio; 1392 } 1393 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */ 1394 static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int nid, 1395 nodemask_t *nodemask) 1396 { 1397 return NULL; 1398 } 1399 #endif 1400 1401 /* 1402 * Remove hugetlb folio from lists. 1403 * If vmemmap exists for the folio, clear the hugetlb flag so that the 1404 * folio appears as just a compound page. Otherwise, wait until after 1405 * allocating vmemmap to clear the flag. 1406 * 1407 * Must be called with hugetlb lock held. 1408 */ 1409 void remove_hugetlb_folio(struct hstate *h, struct folio *folio, 1410 bool adjust_surplus) 1411 { 1412 int nid = folio_nid(folio); 1413 1414 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio); 1415 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio); 1416 1417 lockdep_assert_held(&hugetlb_lock); 1418 if (hstate_is_gigantic_no_runtime(h)) 1419 return; 1420 1421 list_del(&folio->lru); 1422 1423 if (folio_test_hugetlb_freed(folio)) { 1424 folio_clear_hugetlb_freed(folio); 1425 h->free_huge_pages--; 1426 h->free_huge_pages_node[nid]--; 1427 } 1428 if (adjust_surplus) { 1429 h->surplus_huge_pages--; 1430 h->surplus_huge_pages_node[nid]--; 1431 } 1432 1433 /* 1434 * We can only clear the hugetlb flag after allocating vmemmap 1435 * pages. Otherwise, someone (memory error handling) may try to write 1436 * to tail struct pages. 1437 */ 1438 if (!folio_test_hugetlb_vmemmap_optimized(folio)) 1439 __folio_clear_hugetlb(folio); 1440 1441 h->nr_huge_pages--; 1442 h->nr_huge_pages_node[nid]--; 1443 } 1444 1445 void add_hugetlb_folio(struct hstate *h, struct folio *folio, 1446 bool adjust_surplus) 1447 { 1448 int nid = folio_nid(folio); 1449 1450 VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio); 1451 1452 lockdep_assert_held(&hugetlb_lock); 1453 1454 INIT_LIST_HEAD(&folio->lru); 1455 h->nr_huge_pages++; 1456 h->nr_huge_pages_node[nid]++; 1457 1458 if (adjust_surplus) { 1459 h->surplus_huge_pages++; 1460 h->surplus_huge_pages_node[nid]++; 1461 } 1462 1463 __folio_set_hugetlb(folio); 1464 folio_change_private(folio, NULL); 1465 /* 1466 * We have to set hugetlb_vmemmap_optimized again as above 1467 * folio_change_private(folio, NULL) cleared it. 1468 */ 1469 folio_set_hugetlb_vmemmap_optimized(folio); 1470 1471 arch_clear_hugetlb_flags(folio); 1472 enqueue_hugetlb_folio(h, folio); 1473 } 1474 1475 static void __update_and_free_hugetlb_folio(struct hstate *h, 1476 struct folio *folio) 1477 { 1478 bool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio); 1479 1480 if (hstate_is_gigantic_no_runtime(h)) 1481 return; 1482 1483 /* 1484 * If we don't know which subpages are hwpoisoned, we can't free 1485 * the hugepage, so it's leaked intentionally. 1486 */ 1487 if (folio_test_hugetlb_raw_hwp_unreliable(folio)) 1488 return; 1489 1490 /* 1491 * If folio is not vmemmap optimized (!clear_flag), then the folio 1492 * is no longer identified as a hugetlb page. hugetlb_vmemmap_restore_folio 1493 * can only be passed hugetlb pages and will BUG otherwise. 1494 */ 1495 if (clear_flag && hugetlb_vmemmap_restore_folio(h, folio)) { 1496 spin_lock_irq(&hugetlb_lock); 1497 /* 1498 * If we cannot allocate vmemmap pages, just refuse to free the 1499 * page and put the page back on the hugetlb free list and treat 1500 * as a surplus page. 1501 */ 1502 add_hugetlb_folio(h, folio, true); 1503 spin_unlock_irq(&hugetlb_lock); 1504 return; 1505 } 1506 1507 /* 1508 * If vmemmap pages were allocated above, then we need to clear the 1509 * hugetlb flag under the hugetlb lock. 1510 */ 1511 if (folio_test_hugetlb(folio)) { 1512 spin_lock_irq(&hugetlb_lock); 1513 __folio_clear_hugetlb(folio); 1514 spin_unlock_irq(&hugetlb_lock); 1515 } 1516 1517 /* 1518 * Move PageHWPoison flag from head page to the raw error pages, 1519 * which makes any healthy subpages reusable. 1520 */ 1521 if (unlikely(folio_test_hwpoison(folio))) 1522 folio_clear_hugetlb_hwpoison(folio); 1523 1524 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 1525 if (folio_test_hugetlb_cma(folio)) 1526 hugetlb_cma_free_frozen_folio(folio); 1527 else 1528 free_frozen_pages(&folio->page, folio_order(folio)); 1529 } 1530 1531 /* 1532 * As update_and_free_hugetlb_folio() can be called under any context, so we cannot 1533 * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the 1534 * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate 1535 * the vmemmap pages. 1536 * 1537 * free_hpage_workfn() locklessly retrieves the linked list of pages to be 1538 * freed and frees them one-by-one. As the page->mapping pointer is going 1539 * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node 1540 * structure of a lockless linked list of huge pages to be freed. 1541 */ 1542 static LLIST_HEAD(hpage_freelist); 1543 1544 static void free_hpage_workfn(struct work_struct *work) 1545 { 1546 struct llist_node *node; 1547 1548 node = llist_del_all(&hpage_freelist); 1549 1550 while (node) { 1551 struct folio *folio; 1552 struct hstate *h; 1553 1554 folio = container_of((struct address_space **)node, 1555 struct folio, mapping); 1556 node = node->next; 1557 folio->mapping = NULL; 1558 /* 1559 * The VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio) in 1560 * folio_hstate() is going to trigger because a previous call to 1561 * remove_hugetlb_folio() will clear the hugetlb bit, so do 1562 * not use folio_hstate() directly. 1563 */ 1564 h = size_to_hstate(folio_size(folio)); 1565 1566 __update_and_free_hugetlb_folio(h, folio); 1567 1568 cond_resched(); 1569 } 1570 } 1571 static DECLARE_WORK(free_hpage_work, free_hpage_workfn); 1572 1573 static inline void flush_free_hpage_work(struct hstate *h) 1574 { 1575 if (hugetlb_vmemmap_optimizable(h)) 1576 flush_work(&free_hpage_work); 1577 } 1578 1579 static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio, 1580 bool atomic) 1581 { 1582 if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) { 1583 __update_and_free_hugetlb_folio(h, folio); 1584 return; 1585 } 1586 1587 /* 1588 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages. 1589 * 1590 * Only call schedule_work() if hpage_freelist is previously 1591 * empty. Otherwise, schedule_work() had been called but the workfn 1592 * hasn't retrieved the list yet. 1593 */ 1594 if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist)) 1595 schedule_work(&free_hpage_work); 1596 } 1597 1598 static void bulk_vmemmap_restore_error(struct hstate *h, 1599 struct list_head *folio_list, 1600 struct list_head *non_hvo_folios) 1601 { 1602 struct folio *folio, *t_folio; 1603 1604 if (!list_empty(non_hvo_folios)) { 1605 /* 1606 * Free any restored hugetlb pages so that restore of the 1607 * entire list can be retried. 1608 * The idea is that in the common case of ENOMEM errors freeing 1609 * hugetlb pages with vmemmap we will free up memory so that we 1610 * can allocate vmemmap for more hugetlb pages. 1611 */ 1612 list_for_each_entry_safe(folio, t_folio, non_hvo_folios, lru) { 1613 list_del(&folio->lru); 1614 spin_lock_irq(&hugetlb_lock); 1615 __folio_clear_hugetlb(folio); 1616 spin_unlock_irq(&hugetlb_lock); 1617 update_and_free_hugetlb_folio(h, folio, false); 1618 cond_resched(); 1619 } 1620 } else { 1621 /* 1622 * In the case where there are no folios which can be 1623 * immediately freed, we loop through the list trying to restore 1624 * vmemmap individually in the hope that someone elsewhere may 1625 * have done something to cause success (such as freeing some 1626 * memory). If unable to restore a hugetlb page, the hugetlb 1627 * page is made a surplus page and removed from the list. 1628 * If are able to restore vmemmap and free one hugetlb page, we 1629 * quit processing the list to retry the bulk operation. 1630 */ 1631 list_for_each_entry_safe(folio, t_folio, folio_list, lru) 1632 if (hugetlb_vmemmap_restore_folio(h, folio)) { 1633 list_del(&folio->lru); 1634 spin_lock_irq(&hugetlb_lock); 1635 add_hugetlb_folio(h, folio, true); 1636 spin_unlock_irq(&hugetlb_lock); 1637 } else { 1638 list_del(&folio->lru); 1639 spin_lock_irq(&hugetlb_lock); 1640 __folio_clear_hugetlb(folio); 1641 spin_unlock_irq(&hugetlb_lock); 1642 update_and_free_hugetlb_folio(h, folio, false); 1643 cond_resched(); 1644 break; 1645 } 1646 } 1647 } 1648 1649 static void update_and_free_pages_bulk(struct hstate *h, 1650 struct list_head *folio_list) 1651 { 1652 long ret; 1653 struct folio *folio, *t_folio; 1654 LIST_HEAD(non_hvo_folios); 1655 1656 /* 1657 * First allocate required vmemmmap (if necessary) for all folios. 1658 * Carefully handle errors and free up any available hugetlb pages 1659 * in an effort to make forward progress. 1660 */ 1661 retry: 1662 ret = hugetlb_vmemmap_restore_folios(h, folio_list, &non_hvo_folios); 1663 if (ret < 0) { 1664 bulk_vmemmap_restore_error(h, folio_list, &non_hvo_folios); 1665 goto retry; 1666 } 1667 1668 /* 1669 * At this point, list should be empty, ret should be >= 0 and there 1670 * should only be pages on the non_hvo_folios list. 1671 * Do note that the non_hvo_folios list could be empty. 1672 * Without HVO enabled, ret will be 0 and there is no need to call 1673 * __folio_clear_hugetlb as this was done previously. 1674 */ 1675 VM_WARN_ON(!list_empty(folio_list)); 1676 VM_WARN_ON(ret < 0); 1677 if (!list_empty(&non_hvo_folios) && ret) { 1678 spin_lock_irq(&hugetlb_lock); 1679 list_for_each_entry(folio, &non_hvo_folios, lru) 1680 __folio_clear_hugetlb(folio); 1681 spin_unlock_irq(&hugetlb_lock); 1682 } 1683 1684 list_for_each_entry_safe(folio, t_folio, &non_hvo_folios, lru) { 1685 update_and_free_hugetlb_folio(h, folio, false); 1686 cond_resched(); 1687 } 1688 } 1689 1690 struct hstate *size_to_hstate(unsigned long size) 1691 { 1692 struct hstate *h; 1693 1694 for_each_hstate(h) { 1695 if (huge_page_size(h) == size) 1696 return h; 1697 } 1698 return NULL; 1699 } 1700 1701 void free_huge_folio(struct folio *folio) 1702 { 1703 /* 1704 * Can't pass hstate in here because it is called from the 1705 * generic mm code. 1706 */ 1707 struct hstate *h = folio_hstate(folio); 1708 int nid = folio_nid(folio); 1709 struct hugepage_subpool *spool = hugetlb_folio_subpool(folio); 1710 bool restore_reserve; 1711 unsigned long flags; 1712 1713 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); 1714 VM_BUG_ON_FOLIO(folio_mapcount(folio), folio); 1715 1716 hugetlb_set_folio_subpool(folio, NULL); 1717 if (folio_test_anon(folio)) 1718 __ClearPageAnonExclusive(&folio->page); 1719 folio->mapping = NULL; 1720 restore_reserve = folio_test_hugetlb_restore_reserve(folio); 1721 folio_clear_hugetlb_restore_reserve(folio); 1722 1723 /* 1724 * If HPageRestoreReserve was set on page, page allocation consumed a 1725 * reservation. If the page was associated with a subpool, there 1726 * would have been a page reserved in the subpool before allocation 1727 * via hugepage_subpool_get_pages(). Since we are 'restoring' the 1728 * reservation, do not call hugepage_subpool_put_pages() as this will 1729 * remove the reserved page from the subpool. 1730 */ 1731 if (!restore_reserve) { 1732 /* 1733 * A return code of zero implies that the subpool will be 1734 * under its minimum size if the reservation is not restored 1735 * after page is free. Therefore, force restore_reserve 1736 * operation. 1737 */ 1738 if (hugepage_subpool_put_pages(spool, 1) == 0) 1739 restore_reserve = true; 1740 } 1741 1742 spin_lock_irqsave(&hugetlb_lock, flags); 1743 folio_clear_hugetlb_migratable(folio); 1744 hugetlb_cgroup_uncharge_folio(hstate_index(h), 1745 pages_per_huge_page(h), folio); 1746 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), 1747 pages_per_huge_page(h), folio); 1748 lruvec_stat_mod_folio(folio, NR_HUGETLB, -pages_per_huge_page(h)); 1749 mem_cgroup_uncharge(folio); 1750 if (restore_reserve) 1751 h->resv_huge_pages++; 1752 1753 if (folio_test_hugetlb_temporary(folio)) { 1754 remove_hugetlb_folio(h, folio, false); 1755 spin_unlock_irqrestore(&hugetlb_lock, flags); 1756 update_and_free_hugetlb_folio(h, folio, true); 1757 } else if (h->surplus_huge_pages_node[nid]) { 1758 /* remove the page from active list */ 1759 remove_hugetlb_folio(h, folio, true); 1760 spin_unlock_irqrestore(&hugetlb_lock, flags); 1761 update_and_free_hugetlb_folio(h, folio, true); 1762 } else { 1763 arch_clear_hugetlb_flags(folio); 1764 enqueue_hugetlb_folio(h, folio); 1765 spin_unlock_irqrestore(&hugetlb_lock, flags); 1766 } 1767 } 1768 1769 /* 1770 * Must be called with the hugetlb lock held 1771 */ 1772 static void account_new_hugetlb_folio(struct hstate *h, struct folio *folio) 1773 { 1774 lockdep_assert_held(&hugetlb_lock); 1775 h->nr_huge_pages++; 1776 h->nr_huge_pages_node[folio_nid(folio)]++; 1777 } 1778 1779 void init_new_hugetlb_folio(struct folio *folio) 1780 { 1781 __folio_set_hugetlb(folio); 1782 INIT_LIST_HEAD(&folio->lru); 1783 hugetlb_set_folio_subpool(folio, NULL); 1784 set_hugetlb_cgroup(folio, NULL); 1785 set_hugetlb_cgroup_rsvd(folio, NULL); 1786 } 1787 1788 /* 1789 * Find and lock address space (mapping) in write mode. 1790 * 1791 * Upon entry, the folio is locked which means that folio_mapping() is 1792 * stable. Due to locking order, we can only trylock_write. If we can 1793 * not get the lock, simply return NULL to caller. 1794 */ 1795 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio) 1796 { 1797 struct address_space *mapping = folio_mapping(folio); 1798 1799 if (!mapping) 1800 return mapping; 1801 1802 if (i_mmap_trylock_write(mapping)) 1803 return mapping; 1804 1805 return NULL; 1806 } 1807 1808 static struct folio *alloc_buddy_frozen_folio(int order, gfp_t gfp_mask, 1809 int nid, nodemask_t *nmask, nodemask_t *node_alloc_noretry) 1810 { 1811 struct folio *folio; 1812 bool alloc_try_hard = true; 1813 1814 /* 1815 * By default we always try hard to allocate the folio with 1816 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating folios in 1817 * a loop (to adjust global huge page counts) and previous allocation 1818 * failed, do not continue to try hard on the same node. Use the 1819 * node_alloc_noretry bitmap to manage this state information. 1820 */ 1821 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry)) 1822 alloc_try_hard = false; 1823 if (alloc_try_hard) 1824 gfp_mask |= __GFP_RETRY_MAYFAIL; 1825 1826 folio = (struct folio *)__alloc_frozen_pages(gfp_mask, order, nid, nmask); 1827 1828 /* 1829 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a 1830 * folio this indicates an overall state change. Clear bit so 1831 * that we resume normal 'try hard' allocations. 1832 */ 1833 if (node_alloc_noretry && folio && !alloc_try_hard) 1834 node_clear(nid, *node_alloc_noretry); 1835 1836 /* 1837 * If we tried hard to get a folio but failed, set bit so that 1838 * subsequent attempts will not try as hard until there is an 1839 * overall state change. 1840 */ 1841 if (node_alloc_noretry && !folio && alloc_try_hard) 1842 node_set(nid, *node_alloc_noretry); 1843 1844 if (!folio) { 1845 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); 1846 return NULL; 1847 } 1848 1849 __count_vm_event(HTLB_BUDDY_PGALLOC); 1850 return folio; 1851 } 1852 1853 static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, 1854 gfp_t gfp_mask, int nid, nodemask_t *nmask, 1855 nodemask_t *node_alloc_noretry) 1856 { 1857 struct folio *folio; 1858 int order = huge_page_order(h); 1859 1860 if (nid == NUMA_NO_NODE) 1861 nid = numa_mem_id(); 1862 1863 if (order_is_gigantic(order)) 1864 folio = alloc_gigantic_frozen_folio(order, gfp_mask, nid, nmask); 1865 else 1866 folio = alloc_buddy_frozen_folio(order, gfp_mask, nid, nmask, 1867 node_alloc_noretry); 1868 if (folio) 1869 init_new_hugetlb_folio(folio); 1870 return folio; 1871 } 1872 1873 /* 1874 * Common helper to allocate a fresh hugetlb folio. All specific allocators 1875 * should use this function to get new hugetlb folio 1876 * 1877 * Note that returned folio is 'frozen': ref count of head page and all tail 1878 * pages is zero, and the accounting must be done in the caller. 1879 */ 1880 static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h, 1881 gfp_t gfp_mask, int nid, nodemask_t *nmask) 1882 { 1883 struct folio *folio; 1884 1885 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); 1886 if (folio) 1887 hugetlb_vmemmap_optimize_folio(h, folio); 1888 return folio; 1889 } 1890 1891 void prep_and_add_allocated_folios(struct hstate *h, 1892 struct list_head *folio_list) 1893 { 1894 unsigned long flags; 1895 struct folio *folio, *tmp_f; 1896 1897 /* Send list for bulk vmemmap optimization processing */ 1898 hugetlb_vmemmap_optimize_folios(h, folio_list); 1899 1900 /* Add all new pool pages to free lists in one lock cycle */ 1901 spin_lock_irqsave(&hugetlb_lock, flags); 1902 list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { 1903 account_new_hugetlb_folio(h, folio); 1904 enqueue_hugetlb_folio(h, folio); 1905 } 1906 spin_unlock_irqrestore(&hugetlb_lock, flags); 1907 } 1908 1909 /* 1910 * Allocates a fresh hugetlb page in a node interleaved manner. The page 1911 * will later be added to the appropriate hugetlb pool. 1912 */ 1913 static struct folio *alloc_pool_huge_folio(struct hstate *h, 1914 nodemask_t *nodes_allowed, 1915 nodemask_t *node_alloc_noretry, 1916 int *next_node) 1917 { 1918 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 1919 int nr_nodes, node; 1920 1921 for_each_node_mask_to_alloc(next_node, nr_nodes, node, nodes_allowed) { 1922 struct folio *folio; 1923 1924 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, node, 1925 nodes_allowed, node_alloc_noretry); 1926 if (folio) 1927 return folio; 1928 } 1929 1930 return NULL; 1931 } 1932 1933 /* 1934 * Remove huge page from pool from next node to free. Attempt to keep 1935 * persistent huge pages more or less balanced over allowed nodes. 1936 * This routine only 'removes' the hugetlb page. The caller must make 1937 * an additional call to free the page to low level allocators. 1938 * Called with hugetlb_lock locked. 1939 */ 1940 static struct folio *remove_pool_hugetlb_folio(struct hstate *h, 1941 nodemask_t *nodes_allowed, bool acct_surplus) 1942 { 1943 int nr_nodes, node; 1944 struct folio *folio = NULL; 1945 1946 lockdep_assert_held(&hugetlb_lock); 1947 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 1948 /* 1949 * If we're returning unused surplus pages, only examine 1950 * nodes with surplus pages. 1951 */ 1952 if ((!acct_surplus || h->surplus_huge_pages_node[node]) && 1953 !list_empty(&h->hugepage_freelists[node])) { 1954 folio = list_entry(h->hugepage_freelists[node].next, 1955 struct folio, lru); 1956 remove_hugetlb_folio(h, folio, acct_surplus); 1957 break; 1958 } 1959 } 1960 1961 return folio; 1962 } 1963 1964 /* 1965 * Dissolve a given free hugetlb folio into free buddy pages. This function 1966 * does nothing for in-use hugetlb folios and non-hugetlb folios. 1967 * This function returns values like below: 1968 * 1969 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages 1970 * when the system is under memory pressure and the feature of 1971 * freeing unused vmemmap pages associated with each hugetlb page 1972 * is enabled. 1973 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use 1974 * (allocated or reserved.) 1975 * 0: successfully dissolved free hugepages or the page is not a 1976 * hugepage (considered as already dissolved) 1977 */ 1978 int dissolve_free_hugetlb_folio(struct folio *folio) 1979 { 1980 int rc = -EBUSY; 1981 1982 retry: 1983 /* Not to disrupt normal path by vainly holding hugetlb_lock */ 1984 if (!folio_test_hugetlb(folio)) 1985 return 0; 1986 1987 spin_lock_irq(&hugetlb_lock); 1988 if (!folio_test_hugetlb(folio)) { 1989 rc = 0; 1990 goto out; 1991 } 1992 1993 if (!folio_ref_count(folio)) { 1994 struct hstate *h = folio_hstate(folio); 1995 bool adjust_surplus = false; 1996 1997 if (!available_huge_pages(h)) 1998 goto out; 1999 2000 /* 2001 * We should make sure that the page is already on the free list 2002 * when it is dissolved. 2003 */ 2004 if (unlikely(!folio_test_hugetlb_freed(folio))) { 2005 spin_unlock_irq(&hugetlb_lock); 2006 cond_resched(); 2007 2008 /* 2009 * Theoretically, we should return -EBUSY when we 2010 * encounter this race. In fact, we have a chance 2011 * to successfully dissolve the page if we do a 2012 * retry. Because the race window is quite small. 2013 * If we seize this opportunity, it is an optimization 2014 * for increasing the success rate of dissolving page. 2015 */ 2016 goto retry; 2017 } 2018 2019 if (h->surplus_huge_pages_node[folio_nid(folio)]) 2020 adjust_surplus = true; 2021 remove_hugetlb_folio(h, folio, adjust_surplus); 2022 h->max_huge_pages--; 2023 spin_unlock_irq(&hugetlb_lock); 2024 2025 /* 2026 * Normally update_and_free_hugtlb_folio will allocate required vmemmmap 2027 * before freeing the page. update_and_free_hugtlb_folio will fail to 2028 * free the page if it can not allocate required vmemmap. We 2029 * need to adjust max_huge_pages if the page is not freed. 2030 * Attempt to allocate vmemmmap here so that we can take 2031 * appropriate action on failure. 2032 * 2033 * The folio_test_hugetlb check here is because 2034 * remove_hugetlb_folio will clear hugetlb folio flag for 2035 * non-vmemmap optimized hugetlb folios. 2036 */ 2037 if (folio_test_hugetlb(folio)) { 2038 rc = hugetlb_vmemmap_restore_folio(h, folio); 2039 if (rc) { 2040 spin_lock_irq(&hugetlb_lock); 2041 add_hugetlb_folio(h, folio, adjust_surplus); 2042 h->max_huge_pages++; 2043 goto out; 2044 } 2045 } else { 2046 rc = 0; 2047 } 2048 2049 update_and_free_hugetlb_folio(h, folio, false); 2050 return rc; 2051 } 2052 out: 2053 spin_unlock_irq(&hugetlb_lock); 2054 return rc; 2055 } 2056 2057 /* 2058 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to 2059 * make specified memory blocks removable from the system. 2060 * Note that this will dissolve a free gigantic hugepage completely, if any 2061 * part of it lies within the given range. 2062 * Also note that if dissolve_free_hugetlb_folio() returns with an error, all 2063 * free hugetlb folios that were dissolved before that error are lost. 2064 */ 2065 int dissolve_free_hugetlb_folios(unsigned long start_pfn, unsigned long end_pfn) 2066 { 2067 unsigned long pfn; 2068 struct folio *folio; 2069 int rc = 0; 2070 unsigned int order; 2071 struct hstate *h; 2072 2073 if (!hugepages_supported()) 2074 return rc; 2075 2076 order = huge_page_order(&default_hstate); 2077 for_each_hstate(h) 2078 order = min(order, huge_page_order(h)); 2079 2080 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) { 2081 folio = pfn_folio(pfn); 2082 rc = dissolve_free_hugetlb_folio(folio); 2083 if (rc) 2084 break; 2085 } 2086 2087 return rc; 2088 } 2089 2090 /* 2091 * Allocates a fresh surplus page from the page allocator. 2092 */ 2093 static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h, 2094 gfp_t gfp_mask, int nid, nodemask_t *nmask) 2095 { 2096 struct folio *folio = NULL; 2097 2098 if (hstate_is_gigantic_no_runtime(h)) 2099 return NULL; 2100 2101 spin_lock_irq(&hugetlb_lock); 2102 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) 2103 goto out_unlock; 2104 spin_unlock_irq(&hugetlb_lock); 2105 2106 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask); 2107 if (!folio) 2108 return NULL; 2109 2110 spin_lock_irq(&hugetlb_lock); 2111 /* 2112 * nr_huge_pages needs to be adjusted within the same lock cycle 2113 * as surplus_pages, otherwise it might confuse 2114 * persistent_huge_pages() momentarily. 2115 */ 2116 account_new_hugetlb_folio(h, folio); 2117 2118 /* 2119 * We could have raced with the pool size change. 2120 * Double check that and simply deallocate the new page 2121 * if we would end up overcommiting the surpluses. Abuse 2122 * temporary page to workaround the nasty free_huge_folio 2123 * codeflow 2124 */ 2125 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { 2126 folio_set_hugetlb_temporary(folio); 2127 spin_unlock_irq(&hugetlb_lock); 2128 free_huge_folio(folio); 2129 return NULL; 2130 } 2131 2132 h->surplus_huge_pages++; 2133 h->surplus_huge_pages_node[folio_nid(folio)]++; 2134 2135 out_unlock: 2136 spin_unlock_irq(&hugetlb_lock); 2137 2138 return folio; 2139 } 2140 2141 static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mask, 2142 int nid, nodemask_t *nmask) 2143 { 2144 struct folio *folio; 2145 2146 if (hstate_is_gigantic(h)) 2147 return NULL; 2148 2149 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask); 2150 if (!folio) 2151 return NULL; 2152 2153 spin_lock_irq(&hugetlb_lock); 2154 account_new_hugetlb_folio(h, folio); 2155 spin_unlock_irq(&hugetlb_lock); 2156 2157 /* fresh huge pages are frozen */ 2158 folio_ref_unfreeze(folio, 1); 2159 /* 2160 * We do not account these pages as surplus because they are only 2161 * temporary and will be released properly on the last reference 2162 */ 2163 folio_set_hugetlb_temporary(folio); 2164 2165 return folio; 2166 } 2167 2168 /* 2169 * Use the VMA's mpolicy to allocate a huge page from the buddy. 2170 */ 2171 static 2172 struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h, 2173 struct vm_area_struct *vma, unsigned long addr) 2174 { 2175 struct folio *folio = NULL; 2176 struct mempolicy *mpol; 2177 gfp_t gfp_mask = htlb_alloc_mask(h); 2178 int nid; 2179 nodemask_t *nodemask; 2180 2181 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask); 2182 if (mpol_is_preferred_many(mpol)) { 2183 gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); 2184 2185 folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask); 2186 2187 /* Fallback to all nodes if page==NULL */ 2188 nodemask = NULL; 2189 } 2190 2191 if (!folio) 2192 folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask); 2193 mpol_cond_put(mpol); 2194 return folio; 2195 } 2196 2197 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, 2198 nodemask_t *nmask, gfp_t gfp_mask) 2199 { 2200 struct folio *folio; 2201 2202 spin_lock_irq(&hugetlb_lock); 2203 if (!h->resv_huge_pages) { 2204 spin_unlock_irq(&hugetlb_lock); 2205 return NULL; 2206 } 2207 2208 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, preferred_nid, 2209 nmask); 2210 if (folio) 2211 h->resv_huge_pages--; 2212 2213 spin_unlock_irq(&hugetlb_lock); 2214 return folio; 2215 } 2216 2217 /* folio migration callback function */ 2218 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, 2219 nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback) 2220 { 2221 spin_lock_irq(&hugetlb_lock); 2222 if (available_huge_pages(h)) { 2223 struct folio *folio; 2224 2225 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, 2226 preferred_nid, nmask); 2227 if (folio) { 2228 spin_unlock_irq(&hugetlb_lock); 2229 return folio; 2230 } 2231 } 2232 spin_unlock_irq(&hugetlb_lock); 2233 2234 /* We cannot fallback to other nodes, as we could break the per-node pool. */ 2235 if (!allow_alloc_fallback) 2236 gfp_mask |= __GFP_THISNODE; 2237 2238 return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask); 2239 } 2240 2241 static nodemask_t *policy_mbind_nodemask(gfp_t gfp) 2242 { 2243 #ifdef CONFIG_NUMA 2244 struct mempolicy *mpol = get_task_policy(current); 2245 2246 /* 2247 * Only enforce MPOL_BIND policy which overlaps with cpuset policy 2248 * (from policy_nodemask) specifically for hugetlb case 2249 */ 2250 if (mpol->mode == MPOL_BIND && 2251 (apply_policy_zone(mpol, gfp_zone(gfp)) && 2252 cpuset_nodemask_valid_mems_allowed(&mpol->nodes))) 2253 return &mpol->nodes; 2254 #endif 2255 return NULL; 2256 } 2257 2258 /* 2259 * Increase the hugetlb pool such that it can accommodate a reservation 2260 * of size 'delta'. 2261 */ 2262 static int gather_surplus_pages(struct hstate *h, long delta) 2263 __must_hold(&hugetlb_lock) 2264 { 2265 LIST_HEAD(surplus_list); 2266 struct folio *folio, *tmp; 2267 int ret; 2268 long i; 2269 long needed, allocated; 2270 bool alloc_ok = true; 2271 nodemask_t *mbind_nodemask, alloc_nodemask; 2272 2273 mbind_nodemask = policy_mbind_nodemask(htlb_alloc_mask(h)); 2274 if (mbind_nodemask) 2275 nodes_and(alloc_nodemask, *mbind_nodemask, cpuset_current_mems_allowed); 2276 else 2277 alloc_nodemask = cpuset_current_mems_allowed; 2278 2279 lockdep_assert_held(&hugetlb_lock); 2280 needed = (h->resv_huge_pages + delta) - h->free_huge_pages; 2281 if (needed <= 0) { 2282 h->resv_huge_pages += delta; 2283 return 0; 2284 } 2285 2286 allocated = 0; 2287 2288 ret = -ENOMEM; 2289 retry: 2290 spin_unlock_irq(&hugetlb_lock); 2291 for (i = 0; i < needed; i++) { 2292 folio = NULL; 2293 2294 /* 2295 * It is okay to use NUMA_NO_NODE because we use numa_mem_id() 2296 * down the road to pick the current node if that is the case. 2297 */ 2298 folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h), 2299 NUMA_NO_NODE, &alloc_nodemask); 2300 if (!folio) { 2301 alloc_ok = false; 2302 break; 2303 } 2304 list_add(&folio->lru, &surplus_list); 2305 cond_resched(); 2306 } 2307 allocated += i; 2308 2309 /* 2310 * After retaking hugetlb_lock, we need to recalculate 'needed' 2311 * because either resv_huge_pages or free_huge_pages may have changed. 2312 */ 2313 spin_lock_irq(&hugetlb_lock); 2314 needed = (h->resv_huge_pages + delta) - 2315 (h->free_huge_pages + allocated); 2316 if (needed > 0) { 2317 if (alloc_ok) 2318 goto retry; 2319 /* 2320 * We were not able to allocate enough pages to 2321 * satisfy the entire reservation so we free what 2322 * we've allocated so far. 2323 */ 2324 goto free; 2325 } 2326 /* 2327 * The surplus_list now contains _at_least_ the number of extra pages 2328 * needed to accommodate the reservation. Add the appropriate number 2329 * of pages to the hugetlb pool and free the extras back to the buddy 2330 * allocator. Commit the entire reservation here to prevent another 2331 * process from stealing the pages as they are added to the pool but 2332 * before they are reserved. 2333 */ 2334 needed += allocated; 2335 h->resv_huge_pages += delta; 2336 ret = 0; 2337 2338 /* Free the needed pages to the hugetlb pool */ 2339 list_for_each_entry_safe(folio, tmp, &surplus_list, lru) { 2340 if ((--needed) < 0) 2341 break; 2342 /* Add the page to the hugetlb allocator */ 2343 enqueue_hugetlb_folio(h, folio); 2344 } 2345 free: 2346 spin_unlock_irq(&hugetlb_lock); 2347 2348 /* 2349 * Free unnecessary surplus pages to the buddy allocator. 2350 * Pages have no ref count, call free_huge_folio directly. 2351 */ 2352 list_for_each_entry_safe(folio, tmp, &surplus_list, lru) 2353 free_huge_folio(folio); 2354 spin_lock_irq(&hugetlb_lock); 2355 2356 return ret; 2357 } 2358 2359 /* 2360 * This routine has two main purposes: 2361 * 1) Decrement the reservation count (resv_huge_pages) by the value passed 2362 * in unused_resv_pages. This corresponds to the prior adjustments made 2363 * to the associated reservation map. 2364 * 2) Free any unused surplus pages that may have been allocated to satisfy 2365 * the reservation. As many as unused_resv_pages may be freed. 2366 */ 2367 static void return_unused_surplus_pages(struct hstate *h, 2368 unsigned long unused_resv_pages) 2369 { 2370 unsigned long nr_pages; 2371 LIST_HEAD(page_list); 2372 2373 lockdep_assert_held(&hugetlb_lock); 2374 /* Uncommit the reservation */ 2375 h->resv_huge_pages -= unused_resv_pages; 2376 2377 if (hstate_is_gigantic_no_runtime(h)) 2378 goto out; 2379 2380 /* 2381 * Part (or even all) of the reservation could have been backed 2382 * by pre-allocated pages. Only free surplus pages. 2383 */ 2384 nr_pages = min(unused_resv_pages, h->surplus_huge_pages); 2385 2386 /* 2387 * We want to release as many surplus pages as possible, spread 2388 * evenly across all nodes with memory. Iterate across these nodes 2389 * until we can no longer free unreserved surplus pages. This occurs 2390 * when the nodes with surplus pages have no free pages. 2391 * remove_pool_hugetlb_folio() will balance the freed pages across the 2392 * on-line nodes with memory and will handle the hstate accounting. 2393 */ 2394 while (nr_pages--) { 2395 struct folio *folio; 2396 2397 folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY], 1); 2398 if (!folio) 2399 goto out; 2400 2401 list_add(&folio->lru, &page_list); 2402 } 2403 2404 out: 2405 spin_unlock_irq(&hugetlb_lock); 2406 update_and_free_pages_bulk(h, &page_list); 2407 spin_lock_irq(&hugetlb_lock); 2408 } 2409 2410 2411 /* 2412 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation 2413 * are used by the huge page allocation routines to manage reservations. 2414 * 2415 * vma_needs_reservation is called to determine if the huge page at addr 2416 * within the vma has an associated reservation. If a reservation is 2417 * needed, the value 1 is returned. The caller is then responsible for 2418 * managing the global reservation and subpool usage counts. After 2419 * the huge page has been allocated, vma_commit_reservation is called 2420 * to add the page to the reservation map. If the page allocation fails, 2421 * the reservation must be ended instead of committed. vma_end_reservation 2422 * is called in such cases. 2423 * 2424 * In the normal case, vma_commit_reservation returns the same value 2425 * as the preceding vma_needs_reservation call. The only time this 2426 * is not the case is if a reserve map was changed between calls. It 2427 * is the responsibility of the caller to notice the difference and 2428 * take appropriate action. 2429 * 2430 * vma_add_reservation is used in error paths where a reservation must 2431 * be restored when a newly allocated huge page must be freed. It is 2432 * to be called after calling vma_needs_reservation to determine if a 2433 * reservation exists. 2434 * 2435 * vma_del_reservation is used in error paths where an entry in the reserve 2436 * map was created during huge page allocation and must be removed. It is to 2437 * be called after calling vma_needs_reservation to determine if a reservation 2438 * exists. 2439 */ 2440 enum vma_resv_mode { 2441 VMA_NEEDS_RESV, 2442 VMA_COMMIT_RESV, 2443 VMA_END_RESV, 2444 VMA_ADD_RESV, 2445 VMA_DEL_RESV, 2446 }; 2447 static long __vma_reservation_common(struct hstate *h, 2448 struct vm_area_struct *vma, unsigned long addr, 2449 enum vma_resv_mode mode) 2450 { 2451 struct resv_map *resv; 2452 pgoff_t idx; 2453 long ret; 2454 long dummy_out_regions_needed; 2455 2456 resv = vma_resv_map(vma); 2457 if (!resv) 2458 return 1; 2459 2460 idx = vma_hugecache_offset(h, vma, addr); 2461 switch (mode) { 2462 case VMA_NEEDS_RESV: 2463 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed); 2464 /* We assume that vma_reservation_* routines always operate on 2465 * 1 page, and that adding to resv map a 1 page entry can only 2466 * ever require 1 region. 2467 */ 2468 VM_BUG_ON(dummy_out_regions_needed != 1); 2469 break; 2470 case VMA_COMMIT_RESV: 2471 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 2472 /* region_add calls of range 1 should never fail. */ 2473 VM_BUG_ON(ret < 0); 2474 break; 2475 case VMA_END_RESV: 2476 region_abort(resv, idx, idx + 1, 1); 2477 ret = 0; 2478 break; 2479 case VMA_ADD_RESV: 2480 if (vma->vm_flags & VM_MAYSHARE) { 2481 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 2482 /* region_add calls of range 1 should never fail. */ 2483 VM_BUG_ON(ret < 0); 2484 } else { 2485 region_abort(resv, idx, idx + 1, 1); 2486 ret = region_del(resv, idx, idx + 1); 2487 } 2488 break; 2489 case VMA_DEL_RESV: 2490 if (vma->vm_flags & VM_MAYSHARE) { 2491 region_abort(resv, idx, idx + 1, 1); 2492 ret = region_del(resv, idx, idx + 1); 2493 } else { 2494 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL); 2495 /* region_add calls of range 1 should never fail. */ 2496 VM_BUG_ON(ret < 0); 2497 } 2498 break; 2499 default: 2500 BUG(); 2501 } 2502 2503 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV) 2504 return ret; 2505 /* 2506 * We know private mapping must have HPAGE_RESV_OWNER set. 2507 * 2508 * In most cases, reserves always exist for private mappings. 2509 * However, a file associated with mapping could have been 2510 * hole punched or truncated after reserves were consumed. 2511 * As subsequent fault on such a range will not use reserves. 2512 * Subtle - The reserve map for private mappings has the 2513 * opposite meaning than that of shared mappings. If NO 2514 * entry is in the reserve map, it means a reservation exists. 2515 * If an entry exists in the reserve map, it means the 2516 * reservation has already been consumed. As a result, the 2517 * return value of this routine is the opposite of the 2518 * value returned from reserve map manipulation routines above. 2519 */ 2520 if (ret > 0) 2521 return 0; 2522 if (ret == 0) 2523 return 1; 2524 return ret; 2525 } 2526 2527 static long vma_needs_reservation(struct hstate *h, 2528 struct vm_area_struct *vma, unsigned long addr) 2529 { 2530 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV); 2531 } 2532 2533 static long vma_commit_reservation(struct hstate *h, 2534 struct vm_area_struct *vma, unsigned long addr) 2535 { 2536 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV); 2537 } 2538 2539 static void vma_end_reservation(struct hstate *h, 2540 struct vm_area_struct *vma, unsigned long addr) 2541 { 2542 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV); 2543 } 2544 2545 static long vma_add_reservation(struct hstate *h, 2546 struct vm_area_struct *vma, unsigned long addr) 2547 { 2548 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV); 2549 } 2550 2551 static long vma_del_reservation(struct hstate *h, 2552 struct vm_area_struct *vma, unsigned long addr) 2553 { 2554 return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV); 2555 } 2556 2557 /* 2558 * This routine is called to restore reservation information on error paths. 2559 * It should ONLY be called for folios allocated via alloc_hugetlb_folio(), 2560 * and the hugetlb mutex should remain held when calling this routine. 2561 * 2562 * It handles two specific cases: 2563 * 1) A reservation was in place and the folio consumed the reservation. 2564 * hugetlb_restore_reserve is set in the folio. 2565 * 2) No reservation was in place for the page, so hugetlb_restore_reserve is 2566 * not set. However, alloc_hugetlb_folio always updates the reserve map. 2567 * 2568 * In case 1, free_huge_folio later in the error path will increment the 2569 * global reserve count. But, free_huge_folio does not have enough context 2570 * to adjust the reservation map. This case deals primarily with private 2571 * mappings. Adjust the reserve map here to be consistent with global 2572 * reserve count adjustments to be made by free_huge_folio. Make sure the 2573 * reserve map indicates there is a reservation present. 2574 * 2575 * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio. 2576 */ 2577 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, 2578 unsigned long address, struct folio *folio) 2579 { 2580 long rc = vma_needs_reservation(h, vma, address); 2581 2582 if (folio_test_hugetlb_restore_reserve(folio)) { 2583 if (unlikely(rc < 0)) 2584 /* 2585 * Rare out of memory condition in reserve map 2586 * manipulation. Clear hugetlb_restore_reserve so 2587 * that global reserve count will not be incremented 2588 * by free_huge_folio. This will make it appear 2589 * as though the reservation for this folio was 2590 * consumed. This may prevent the task from 2591 * faulting in the folio at a later time. This 2592 * is better than inconsistent global huge page 2593 * accounting of reserve counts. 2594 */ 2595 folio_clear_hugetlb_restore_reserve(folio); 2596 else if (rc) 2597 (void)vma_add_reservation(h, vma, address); 2598 else 2599 vma_end_reservation(h, vma, address); 2600 } else { 2601 if (!rc) { 2602 /* 2603 * This indicates there is an entry in the reserve map 2604 * not added by alloc_hugetlb_folio. We know it was added 2605 * before the alloc_hugetlb_folio call, otherwise 2606 * hugetlb_restore_reserve would be set on the folio. 2607 * Remove the entry so that a subsequent allocation 2608 * does not consume a reservation. 2609 */ 2610 rc = vma_del_reservation(h, vma, address); 2611 if (rc < 0) 2612 /* 2613 * VERY rare out of memory condition. Since 2614 * we can not delete the entry, set 2615 * hugetlb_restore_reserve so that the reserve 2616 * count will be incremented when the folio 2617 * is freed. This reserve will be consumed 2618 * on a subsequent allocation. 2619 */ 2620 folio_set_hugetlb_restore_reserve(folio); 2621 } else if (rc < 0) { 2622 /* 2623 * Rare out of memory condition from 2624 * vma_needs_reservation call. Memory allocation is 2625 * only attempted if a new entry is needed. Therefore, 2626 * this implies there is not an entry in the 2627 * reserve map. 2628 * 2629 * For shared mappings, no entry in the map indicates 2630 * no reservation. We are done. 2631 */ 2632 if (!(vma->vm_flags & VM_MAYSHARE)) 2633 /* 2634 * For private mappings, no entry indicates 2635 * a reservation is present. Since we can 2636 * not add an entry, set hugetlb_restore_reserve 2637 * on the folio so reserve count will be 2638 * incremented when freed. This reserve will 2639 * be consumed on a subsequent allocation. 2640 */ 2641 folio_set_hugetlb_restore_reserve(folio); 2642 } else { 2643 /* 2644 * No reservation present, do nothing 2645 */ 2646 vma_end_reservation(h, vma, address); 2647 } 2648 } 2649 } 2650 2651 /* 2652 * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve 2653 * the old one 2654 * @old_folio: Old folio to dissolve 2655 * @list: List to isolate the page in case we need to 2656 * Returns 0 on success, otherwise negated error. 2657 */ 2658 static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio, 2659 struct list_head *list) 2660 { 2661 gfp_t gfp_mask; 2662 struct hstate *h; 2663 int nid = folio_nid(old_folio); 2664 struct folio *new_folio = NULL; 2665 int ret = 0; 2666 2667 retry: 2668 /* 2669 * The old_folio might have been dissolved from under our feet, so make sure 2670 * to carefully check the state under the lock. 2671 */ 2672 spin_lock_irq(&hugetlb_lock); 2673 if (!folio_test_hugetlb(old_folio)) { 2674 /* 2675 * Freed from under us. Drop new_folio too. 2676 */ 2677 goto free_new; 2678 } else if (folio_ref_count(old_folio)) { 2679 bool isolated; 2680 2681 /* 2682 * Someone has grabbed the folio, try to isolate it here. 2683 * Fail with -EBUSY if not possible. 2684 */ 2685 spin_unlock_irq(&hugetlb_lock); 2686 isolated = folio_isolate_hugetlb(old_folio, list); 2687 ret = isolated ? 0 : -EBUSY; 2688 spin_lock_irq(&hugetlb_lock); 2689 goto free_new; 2690 } else if (!folio_test_hugetlb_freed(old_folio)) { 2691 /* 2692 * Folio's refcount is 0 but it has not been enqueued in the 2693 * freelist yet. Race window is small, so we can succeed here if 2694 * we retry. 2695 */ 2696 spin_unlock_irq(&hugetlb_lock); 2697 cond_resched(); 2698 goto retry; 2699 } else { 2700 h = folio_hstate(old_folio); 2701 if (!new_folio) { 2702 spin_unlock_irq(&hugetlb_lock); 2703 gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 2704 new_folio = alloc_fresh_hugetlb_folio(h, gfp_mask, 2705 nid, NULL); 2706 if (!new_folio) 2707 return -ENOMEM; 2708 goto retry; 2709 } 2710 2711 /* 2712 * Ok, old_folio is still a genuine free hugepage. Remove it from 2713 * the freelist and decrease the counters. These will be 2714 * incremented again when calling account_new_hugetlb_folio() 2715 * and enqueue_hugetlb_folio() for new_folio. The counters will 2716 * remain stable since this happens under the lock. 2717 */ 2718 remove_hugetlb_folio(h, old_folio, false); 2719 2720 /* 2721 * Ref count on new_folio is already zero as it was dropped 2722 * earlier. It can be directly added to the pool free list. 2723 */ 2724 account_new_hugetlb_folio(h, new_folio); 2725 enqueue_hugetlb_folio(h, new_folio); 2726 2727 /* 2728 * Folio has been replaced, we can safely free the old one. 2729 */ 2730 spin_unlock_irq(&hugetlb_lock); 2731 update_and_free_hugetlb_folio(h, old_folio, false); 2732 } 2733 2734 return ret; 2735 2736 free_new: 2737 spin_unlock_irq(&hugetlb_lock); 2738 if (new_folio) 2739 update_and_free_hugetlb_folio(h, new_folio, false); 2740 2741 return ret; 2742 } 2743 2744 int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) 2745 { 2746 int ret = -EBUSY; 2747 2748 /* Not to disrupt normal path by vainly holding hugetlb_lock */ 2749 if (!folio_test_hugetlb(folio)) 2750 return 0; 2751 2752 /* 2753 * Fence off gigantic pages as there is a cyclic dependency between 2754 * alloc_contig_range and them. Return -ENOMEM as this has the effect 2755 * of bailing out right away without further retrying. 2756 */ 2757 if (order_is_gigantic(folio_order(folio))) 2758 return -ENOMEM; 2759 2760 if (folio_ref_count(folio) && folio_isolate_hugetlb(folio, list)) 2761 ret = 0; 2762 else if (!folio_ref_count(folio)) 2763 ret = alloc_and_dissolve_hugetlb_folio(folio, list); 2764 2765 return ret; 2766 } 2767 2768 /* 2769 * replace_free_hugepage_folios - Replace free hugepage folios in a given pfn 2770 * range with new folios. 2771 * @start_pfn: start pfn of the given pfn range 2772 * @end_pfn: end pfn of the given pfn range 2773 * Returns 0 on success, otherwise negated error. 2774 */ 2775 int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) 2776 { 2777 unsigned long nr = 0; 2778 struct page *page; 2779 struct hstate *h; 2780 LIST_HEAD(list); 2781 int ret = 0; 2782 2783 /* Avoid pfn iterations if no free non-gigantic huge pages */ 2784 for_each_hstate(h) { 2785 if (hstate_is_gigantic(h)) 2786 continue; 2787 2788 nr += h->free_huge_pages; 2789 if (nr) 2790 break; 2791 } 2792 2793 if (!nr) 2794 return 0; 2795 2796 while (start_pfn < end_pfn) { 2797 page = pfn_to_page(start_pfn); 2798 nr = 1; 2799 2800 if (PageHuge(page) || PageCompound(page)) { 2801 struct folio *folio = page_folio(page); 2802 2803 nr = folio_nr_pages(folio) - folio_page_idx(folio, page); 2804 2805 /* 2806 * Don't disrupt normal path by vainly holding 2807 * hugetlb_lock 2808 */ 2809 if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) { 2810 if (order_is_gigantic(folio_order(folio))) { 2811 ret = -ENOMEM; 2812 break; 2813 } 2814 2815 ret = alloc_and_dissolve_hugetlb_folio(folio, &list); 2816 if (ret) 2817 break; 2818 2819 putback_movable_pages(&list); 2820 } 2821 } else if (PageBuddy(page)) { 2822 /* 2823 * Buddy order check without zone lock is unsafe and 2824 * the order is maybe invalid, but race should be 2825 * small, and the worst thing is skipping free hugetlb. 2826 */ 2827 const unsigned int order = buddy_order_unsafe(page); 2828 2829 if (order <= MAX_PAGE_ORDER) 2830 nr = 1UL << order; 2831 } 2832 start_pfn += nr; 2833 } 2834 2835 return ret; 2836 } 2837 2838 void wait_for_freed_hugetlb_folios(void) 2839 { 2840 if (llist_empty(&hpage_freelist)) 2841 return; 2842 2843 flush_work(&free_hpage_work); 2844 } 2845 2846 typedef enum { 2847 /* 2848 * For either 0/1: we checked the per-vma resv map, and one resv 2849 * count either can be reused (0), or an extra needed (1). 2850 */ 2851 MAP_CHG_REUSE = 0, 2852 MAP_CHG_NEEDED = 1, 2853 /* 2854 * Cannot use per-vma resv count can be used, hence a new resv 2855 * count is enforced. 2856 * 2857 * NOTE: This is mostly identical to MAP_CHG_NEEDED, except 2858 * that currently vma_needs_reservation() has an unwanted side 2859 * effect to either use end() or commit() to complete the 2860 * transaction. Hence it needs to differentiate from NEEDED. 2861 */ 2862 MAP_CHG_ENFORCED = 2, 2863 } map_chg_state; 2864 2865 /* 2866 * NOTE! "cow_from_owner" represents a very hacky usage only used in CoW 2867 * faults of hugetlb private mappings on top of a non-page-cache folio (in 2868 * which case even if there's a private vma resv map it won't cover such 2869 * allocation). New call sites should (probably) never set it to true!! 2870 * When it's set, the allocation will bypass all vma level reservations. 2871 */ 2872 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, 2873 unsigned long addr, bool cow_from_owner) 2874 { 2875 struct hugepage_subpool *spool = subpool_vma(vma); 2876 struct hstate *h = hstate_vma(vma); 2877 struct folio *folio; 2878 long retval, gbl_chg, gbl_reserve; 2879 map_chg_state map_chg; 2880 int ret, idx; 2881 struct hugetlb_cgroup *h_cg = NULL; 2882 gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL; 2883 2884 idx = hstate_index(h); 2885 2886 /* Whether we need a separate per-vma reservation? */ 2887 if (cow_from_owner) { 2888 /* 2889 * Special case! Since it's a CoW on top of a reserved 2890 * page, the private resv map doesn't count. So it cannot 2891 * consume the per-vma resv map even if it's reserved. 2892 */ 2893 map_chg = MAP_CHG_ENFORCED; 2894 } else { 2895 /* 2896 * Examine the region/reserve map to determine if the process 2897 * has a reservation for the page to be allocated. A return 2898 * code of zero indicates a reservation exists (no change). 2899 */ 2900 retval = vma_needs_reservation(h, vma, addr); 2901 if (retval < 0) 2902 return ERR_PTR(-ENOMEM); 2903 map_chg = retval ? MAP_CHG_NEEDED : MAP_CHG_REUSE; 2904 } 2905 2906 /* 2907 * Whether we need a separate global reservation? 2908 * 2909 * Processes that did not create the mapping will have no 2910 * reserves as indicated by the region/reserve map. Check 2911 * that the allocation will not exceed the subpool limit. 2912 * Or if it can get one from the pool reservation directly. 2913 */ 2914 if (map_chg) { 2915 gbl_chg = hugepage_subpool_get_pages(spool, 1); 2916 if (gbl_chg < 0) 2917 goto out_end_reservation; 2918 } else { 2919 /* 2920 * If we have the vma reservation ready, no need for extra 2921 * global reservation. 2922 */ 2923 gbl_chg = 0; 2924 } 2925 2926 /* 2927 * If this allocation is not consuming a per-vma reservation, 2928 * charge the hugetlb cgroup now. 2929 */ 2930 if (map_chg) { 2931 ret = hugetlb_cgroup_charge_cgroup_rsvd( 2932 idx, pages_per_huge_page(h), &h_cg); 2933 if (ret) 2934 goto out_subpool_put; 2935 } 2936 2937 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); 2938 if (ret) 2939 goto out_uncharge_cgroup_reservation; 2940 2941 spin_lock_irq(&hugetlb_lock); 2942 /* 2943 * glb_chg is passed to indicate whether or not a page must be taken 2944 * from the global free pool (global change). gbl_chg == 0 indicates 2945 * a reservation exists for the allocation. 2946 */ 2947 folio = dequeue_hugetlb_folio_vma(h, vma, addr, gbl_chg); 2948 if (!folio) { 2949 spin_unlock_irq(&hugetlb_lock); 2950 folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr); 2951 if (!folio) 2952 goto out_uncharge_cgroup; 2953 spin_lock_irq(&hugetlb_lock); 2954 list_add(&folio->lru, &h->hugepage_activelist); 2955 folio_ref_unfreeze(folio, 1); 2956 /* Fall through */ 2957 } 2958 2959 /* 2960 * Either dequeued or buddy-allocated folio needs to add special 2961 * mark to the folio when it consumes a global reservation. 2962 */ 2963 if (!gbl_chg) { 2964 folio_set_hugetlb_restore_reserve(folio); 2965 h->resv_huge_pages--; 2966 } 2967 2968 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, folio); 2969 /* If allocation is not consuming a reservation, also store the 2970 * hugetlb_cgroup pointer on the page. 2971 */ 2972 if (map_chg) { 2973 hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h), 2974 h_cg, folio); 2975 } 2976 2977 spin_unlock_irq(&hugetlb_lock); 2978 2979 hugetlb_set_folio_subpool(folio, spool); 2980 2981 if (map_chg != MAP_CHG_ENFORCED) { 2982 /* commit() is only needed if the map_chg is not enforced */ 2983 retval = vma_commit_reservation(h, vma, addr); 2984 /* 2985 * Check for possible race conditions. When it happens.. 2986 * The page was added to the reservation map between 2987 * vma_needs_reservation and vma_commit_reservation. 2988 * This indicates a race with hugetlb_reserve_pages. 2989 * Adjust for the subpool count incremented above AND 2990 * in hugetlb_reserve_pages for the same page. Also, 2991 * the reservation count added in hugetlb_reserve_pages 2992 * no longer applies. 2993 */ 2994 if (unlikely(map_chg == MAP_CHG_NEEDED && retval == 0)) { 2995 long rsv_adjust; 2996 2997 rsv_adjust = hugepage_subpool_put_pages(spool, 1); 2998 hugetlb_acct_memory(h, -rsv_adjust); 2999 spin_lock_irq(&hugetlb_lock); 3000 hugetlb_cgroup_uncharge_folio_rsvd( 3001 hstate_index(h), pages_per_huge_page(h), folio); 3002 spin_unlock_irq(&hugetlb_lock); 3003 } 3004 } 3005 3006 ret = mem_cgroup_charge_hugetlb(folio, gfp); 3007 /* 3008 * Unconditionally increment NR_HUGETLB here. If it turns out that 3009 * mem_cgroup_charge_hugetlb failed, then immediately free the page and 3010 * decrement NR_HUGETLB. 3011 */ 3012 lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); 3013 3014 if (ret == -ENOMEM) { 3015 free_huge_folio(folio); 3016 return ERR_PTR(-ENOMEM); 3017 } 3018 3019 return folio; 3020 3021 out_uncharge_cgroup: 3022 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); 3023 out_uncharge_cgroup_reservation: 3024 if (map_chg) 3025 hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), 3026 h_cg); 3027 out_subpool_put: 3028 /* 3029 * put page to subpool iff the quota of subpool's rsv_hpages is used 3030 * during hugepage_subpool_get_pages. 3031 */ 3032 if (map_chg && !gbl_chg) { 3033 gbl_reserve = hugepage_subpool_put_pages(spool, 1); 3034 hugetlb_acct_memory(h, -gbl_reserve); 3035 } 3036 3037 3038 out_end_reservation: 3039 if (map_chg != MAP_CHG_ENFORCED) 3040 vma_end_reservation(h, vma, addr); 3041 return ERR_PTR(-ENOSPC); 3042 } 3043 3044 static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact) 3045 { 3046 struct huge_bootmem_page *m; 3047 int listnode = nid; 3048 3049 if (hugetlb_early_cma(h)) 3050 m = hugetlb_cma_alloc_bootmem(h, &listnode, node_exact); 3051 else { 3052 if (node_exact) 3053 m = memblock_alloc_exact_nid_raw(huge_page_size(h), 3054 huge_page_size(h), 0, 3055 MEMBLOCK_ALLOC_ACCESSIBLE, nid); 3056 else { 3057 m = memblock_alloc_try_nid_raw(huge_page_size(h), 3058 huge_page_size(h), 0, 3059 MEMBLOCK_ALLOC_ACCESSIBLE, nid); 3060 /* 3061 * For pre-HVO to work correctly, pages need to be on 3062 * the list for the node they were actually allocated 3063 * from. That node may be different in the case of 3064 * fallback by memblock_alloc_try_nid_raw. So, 3065 * extract the actual node first. 3066 */ 3067 if (m) 3068 listnode = early_pfn_to_nid(PHYS_PFN(__pa(m))); 3069 } 3070 3071 if (m) { 3072 m->flags = 0; 3073 m->cma = NULL; 3074 } 3075 } 3076 3077 if (m) { 3078 /* 3079 * Use the beginning of the huge page to store the 3080 * huge_bootmem_page struct (until gather_bootmem 3081 * puts them into the mem_map). 3082 * 3083 * Put them into a private list first because mem_map 3084 * is not up yet. 3085 */ 3086 INIT_LIST_HEAD(&m->list); 3087 list_add(&m->list, &huge_boot_pages[listnode]); 3088 m->hstate = h; 3089 } 3090 3091 return m; 3092 } 3093 3094 int alloc_bootmem_huge_page(struct hstate *h, int nid) 3095 __attribute__ ((weak, alias("__alloc_bootmem_huge_page"))); 3096 int __alloc_bootmem_huge_page(struct hstate *h, int nid) 3097 { 3098 struct huge_bootmem_page *m = NULL; /* initialize for clang */ 3099 int nr_nodes, node = nid; 3100 3101 /* do node specific alloc */ 3102 if (nid != NUMA_NO_NODE) { 3103 m = alloc_bootmem(h, node, true); 3104 if (!m) 3105 return 0; 3106 goto found; 3107 } 3108 3109 /* allocate from next node when distributing huge pages */ 3110 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, 3111 &hugetlb_bootmem_nodes) { 3112 m = alloc_bootmem(h, node, false); 3113 if (!m) 3114 return 0; 3115 goto found; 3116 } 3117 3118 found: 3119 3120 /* 3121 * Only initialize the head struct page in memmap_init_reserved_pages, 3122 * rest of the struct pages will be initialized by the HugeTLB 3123 * subsystem itself. 3124 * The head struct page is used to get folio information by the HugeTLB 3125 * subsystem like zone id and node id. 3126 */ 3127 memblock_reserved_mark_noinit(__pa((void *)m + PAGE_SIZE), 3128 huge_page_size(h) - PAGE_SIZE); 3129 3130 return 1; 3131 } 3132 3133 /* Initialize [start_page:end_page_number] tail struct pages of a hugepage */ 3134 static void __init hugetlb_folio_init_tail_vmemmap(struct folio *folio, 3135 struct hstate *h, 3136 unsigned long start_page_number, 3137 unsigned long end_page_number) 3138 { 3139 enum zone_type zone = folio_zonenum(folio); 3140 int nid = folio_nid(folio); 3141 struct page *page = folio_page(folio, start_page_number); 3142 unsigned long head_pfn = folio_pfn(folio); 3143 unsigned long pfn, end_pfn = head_pfn + end_page_number; 3144 unsigned int order = huge_page_order(h); 3145 3146 /* 3147 * As we marked all tail pages with memblock_reserved_mark_noinit(), 3148 * we must initialize them ourselves here. 3149 */ 3150 for (pfn = head_pfn + start_page_number; pfn < end_pfn; page++, pfn++) { 3151 __init_single_page(page, pfn, zone, nid); 3152 prep_compound_tail(page, &folio->page, order); 3153 set_page_count(page, 0); 3154 } 3155 } 3156 3157 static void __init hugetlb_folio_init_vmemmap(struct folio *folio, 3158 struct hstate *h, 3159 unsigned long nr_pages) 3160 { 3161 int ret; 3162 3163 /* 3164 * This is an open-coded prep_compound_page() whereby we avoid 3165 * walking pages twice by initializing/preparing+freezing them in the 3166 * same go. 3167 */ 3168 __folio_clear_reserved(folio); 3169 __folio_set_head(folio); 3170 ret = folio_ref_freeze(folio, 1); 3171 VM_BUG_ON(!ret); 3172 hugetlb_folio_init_tail_vmemmap(folio, h, 1, nr_pages); 3173 prep_compound_head(&folio->page, huge_page_order(h)); 3174 } 3175 3176 static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m) 3177 { 3178 return m->flags & HUGE_BOOTMEM_HVO; 3179 } 3180 3181 static bool __init hugetlb_bootmem_page_earlycma(struct huge_bootmem_page *m) 3182 { 3183 return m->flags & HUGE_BOOTMEM_CMA; 3184 } 3185 3186 /* 3187 * memblock-allocated pageblocks might not have the migrate type set 3188 * if marked with the 'noinit' flag. Set it to the default (MIGRATE_MOVABLE) 3189 * here, or MIGRATE_CMA if this was a page allocated through an early CMA 3190 * reservation. 3191 * 3192 * In case of vmemmap optimized folios, the tail vmemmap pages are mapped 3193 * read-only, but that's ok - for sparse vmemmap this does not write to 3194 * the page structure. 3195 */ 3196 static void __init hugetlb_bootmem_init_migratetype(struct folio *folio, 3197 struct hstate *h) 3198 { 3199 unsigned long nr_pages = pages_per_huge_page(h), i; 3200 3201 WARN_ON_ONCE(!pageblock_aligned(folio_pfn(folio))); 3202 3203 for (i = 0; i < nr_pages; i += pageblock_nr_pages) { 3204 if (folio_test_hugetlb_cma(folio)) 3205 init_cma_pageblock(folio_page(folio, i)); 3206 else 3207 init_pageblock_migratetype(folio_page(folio, i), 3208 MIGRATE_MOVABLE, false); 3209 } 3210 } 3211 3212 static void __init prep_and_add_bootmem_folios(struct hstate *h, 3213 struct list_head *folio_list) 3214 { 3215 unsigned long flags; 3216 struct folio *folio, *tmp_f; 3217 3218 /* Send list for bulk vmemmap optimization processing */ 3219 hugetlb_vmemmap_optimize_bootmem_folios(h, folio_list); 3220 3221 list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { 3222 if (!folio_test_hugetlb_vmemmap_optimized(folio)) { 3223 /* 3224 * If HVO fails, initialize all tail struct pages 3225 * We do not worry about potential long lock hold 3226 * time as this is early in boot and there should 3227 * be no contention. 3228 */ 3229 hugetlb_folio_init_tail_vmemmap(folio, h, 3230 HUGETLB_VMEMMAP_RESERVE_PAGES, 3231 pages_per_huge_page(h)); 3232 } 3233 hugetlb_bootmem_init_migratetype(folio, h); 3234 /* Subdivide locks to achieve better parallel performance */ 3235 spin_lock_irqsave(&hugetlb_lock, flags); 3236 account_new_hugetlb_folio(h, folio); 3237 enqueue_hugetlb_folio(h, folio); 3238 spin_unlock_irqrestore(&hugetlb_lock, flags); 3239 } 3240 } 3241 3242 bool __init hugetlb_bootmem_page_zones_valid(int nid, 3243 struct huge_bootmem_page *m) 3244 { 3245 unsigned long start_pfn; 3246 bool valid; 3247 3248 if (m->flags & HUGE_BOOTMEM_ZONES_VALID) { 3249 /* 3250 * Already validated, skip check. 3251 */ 3252 return true; 3253 } 3254 3255 if (hugetlb_bootmem_page_earlycma(m)) { 3256 valid = cma_validate_zones(m->cma); 3257 goto out; 3258 } 3259 3260 start_pfn = virt_to_phys(m) >> PAGE_SHIFT; 3261 3262 valid = !pfn_range_intersects_zones(nid, start_pfn, 3263 pages_per_huge_page(m->hstate)); 3264 out: 3265 if (!valid) 3266 hstate_boot_nrinvalid[hstate_index(m->hstate)]++; 3267 3268 return valid; 3269 } 3270 3271 /* 3272 * Free a bootmem page that was found to be invalid (intersecting with 3273 * multiple zones). 3274 * 3275 * Since it intersects with multiple zones, we can't just do a free 3276 * operation on all pages at once, but instead have to walk all 3277 * pages, freeing them one by one. 3278 */ 3279 static void __init hugetlb_bootmem_free_invalid_page(int nid, struct page *page, 3280 struct hstate *h) 3281 { 3282 unsigned long npages = pages_per_huge_page(h); 3283 unsigned long pfn; 3284 3285 while (npages--) { 3286 pfn = page_to_pfn(page); 3287 __init_page_from_nid(pfn, nid); 3288 free_reserved_page(page); 3289 page++; 3290 } 3291 } 3292 3293 /* 3294 * Put bootmem huge pages into the standard lists after mem_map is up. 3295 * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. 3296 */ 3297 static void __init gather_bootmem_prealloc_node(unsigned long nid) 3298 { 3299 LIST_HEAD(folio_list); 3300 struct huge_bootmem_page *m, *tm; 3301 struct hstate *h = NULL, *prev_h = NULL; 3302 3303 list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) { 3304 struct page *page = virt_to_page(m); 3305 struct folio *folio = (void *)page; 3306 3307 h = m->hstate; 3308 if (!hugetlb_bootmem_page_zones_valid(nid, m)) { 3309 /* 3310 * Can't use this page. Initialize the 3311 * page structures if that hasn't already 3312 * been done, and give them to the page 3313 * allocator. 3314 */ 3315 hugetlb_bootmem_free_invalid_page(nid, page, h); 3316 continue; 3317 } 3318 3319 /* 3320 * It is possible to have multiple huge page sizes (hstates) 3321 * in this list. If so, process each size separately. 3322 */ 3323 if (h != prev_h && prev_h != NULL) 3324 prep_and_add_bootmem_folios(prev_h, &folio_list); 3325 prev_h = h; 3326 3327 VM_BUG_ON(!hstate_is_gigantic(h)); 3328 WARN_ON(folio_ref_count(folio) != 1); 3329 3330 hugetlb_folio_init_vmemmap(folio, h, 3331 HUGETLB_VMEMMAP_RESERVE_PAGES); 3332 init_new_hugetlb_folio(folio); 3333 3334 if (hugetlb_bootmem_page_prehvo(m)) 3335 /* 3336 * If pre-HVO was done, just set the 3337 * flag, the HVO code will then skip 3338 * this folio. 3339 */ 3340 folio_set_hugetlb_vmemmap_optimized(folio); 3341 3342 if (hugetlb_bootmem_page_earlycma(m)) 3343 folio_set_hugetlb_cma(folio); 3344 3345 list_add(&folio->lru, &folio_list); 3346 3347 /* 3348 * We need to restore the 'stolen' pages to totalram_pages 3349 * in order to fix confusing memory reports from free(1) and 3350 * other side-effects, like CommitLimit going negative. 3351 * 3352 * For CMA pages, this is done in init_cma_pageblock 3353 * (via hugetlb_bootmem_init_migratetype), so skip it here. 3354 */ 3355 if (!folio_test_hugetlb_cma(folio)) 3356 adjust_managed_page_count(page, pages_per_huge_page(h)); 3357 cond_resched(); 3358 } 3359 3360 prep_and_add_bootmem_folios(h, &folio_list); 3361 } 3362 3363 static void __init gather_bootmem_prealloc_parallel(unsigned long start, 3364 unsigned long end, void *arg) 3365 { 3366 int nid; 3367 3368 for (nid = start; nid < end; nid++) 3369 gather_bootmem_prealloc_node(nid); 3370 } 3371 3372 static void __init gather_bootmem_prealloc(void) 3373 { 3374 struct padata_mt_job job = { 3375 .thread_fn = gather_bootmem_prealloc_parallel, 3376 .fn_arg = NULL, 3377 .start = 0, 3378 .size = nr_node_ids, 3379 .align = 1, 3380 .min_chunk = 1, 3381 .max_threads = num_node_state(N_MEMORY), 3382 .numa_aware = true, 3383 }; 3384 3385 padata_do_multithreaded(&job); 3386 } 3387 3388 static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) 3389 { 3390 unsigned long i; 3391 char buf[32]; 3392 LIST_HEAD(folio_list); 3393 3394 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) { 3395 if (hstate_is_gigantic(h)) { 3396 if (!alloc_bootmem_huge_page(h, nid)) 3397 break; 3398 } else { 3399 struct folio *folio; 3400 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; 3401 3402 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, 3403 &node_states[N_MEMORY], NULL); 3404 if (!folio && !list_empty(&folio_list) && 3405 hugetlb_vmemmap_optimizable_size(h)) { 3406 prep_and_add_allocated_folios(h, &folio_list); 3407 INIT_LIST_HEAD(&folio_list); 3408 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, 3409 &node_states[N_MEMORY], NULL); 3410 } 3411 if (!folio) 3412 break; 3413 list_add(&folio->lru, &folio_list); 3414 } 3415 cond_resched(); 3416 } 3417 3418 if (!list_empty(&folio_list)) 3419 prep_and_add_allocated_folios(h, &folio_list); 3420 3421 if (i == h->max_huge_pages_node[nid]) 3422 return; 3423 3424 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3425 pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n", 3426 h->max_huge_pages_node[nid], buf, nid, i); 3427 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); 3428 h->max_huge_pages_node[nid] = i; 3429 } 3430 3431 static bool __init hugetlb_hstate_alloc_pages_specific_nodes(struct hstate *h) 3432 { 3433 int i; 3434 bool node_specific_alloc = false; 3435 3436 for_each_online_node(i) { 3437 if (h->max_huge_pages_node[i] > 0) { 3438 hugetlb_hstate_alloc_pages_onenode(h, i); 3439 node_specific_alloc = true; 3440 } 3441 } 3442 3443 return node_specific_alloc; 3444 } 3445 3446 static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated, struct hstate *h) 3447 { 3448 if (allocated < h->max_huge_pages) { 3449 char buf[32]; 3450 3451 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3452 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", 3453 h->max_huge_pages, buf, allocated); 3454 h->max_huge_pages = allocated; 3455 } 3456 } 3457 3458 static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned long end, void *arg) 3459 { 3460 struct hstate *h = (struct hstate *)arg; 3461 int i, num = end - start; 3462 nodemask_t node_alloc_noretry; 3463 LIST_HEAD(folio_list); 3464 int next_node = first_online_node; 3465 3466 /* Bit mask controlling how hard we retry per-node allocations.*/ 3467 nodes_clear(node_alloc_noretry); 3468 3469 for (i = 0; i < num; ++i) { 3470 struct folio *folio; 3471 3472 if (hugetlb_vmemmap_optimizable_size(h) && 3473 (si_mem_available() == 0) && !list_empty(&folio_list)) { 3474 prep_and_add_allocated_folios(h, &folio_list); 3475 INIT_LIST_HEAD(&folio_list); 3476 } 3477 folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY], 3478 &node_alloc_noretry, &next_node); 3479 if (!folio) 3480 break; 3481 3482 list_move(&folio->lru, &folio_list); 3483 cond_resched(); 3484 } 3485 3486 prep_and_add_allocated_folios(h, &folio_list); 3487 } 3488 3489 static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h) 3490 { 3491 unsigned long i; 3492 3493 for (i = 0; i < h->max_huge_pages; ++i) { 3494 if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE)) 3495 break; 3496 cond_resched(); 3497 } 3498 3499 return i; 3500 } 3501 3502 static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h) 3503 { 3504 struct padata_mt_job job = { 3505 .fn_arg = h, 3506 .align = 1, 3507 .numa_aware = true 3508 }; 3509 3510 unsigned long jiffies_start; 3511 unsigned long jiffies_end; 3512 unsigned long remaining; 3513 3514 job.thread_fn = hugetlb_pages_alloc_boot_node; 3515 3516 /* 3517 * job.max_threads is 25% of the available cpu threads by default. 3518 * 3519 * On large servers with terabytes of memory, huge page allocation 3520 * can consume a considerably amount of time. 3521 * 3522 * Tests below show how long it takes to allocate 1 TiB of memory with 2MiB huge pages. 3523 * 2MiB huge pages. Using more threads can significantly improve allocation time. 3524 * 3525 * +-----------------------+-------+-------+-------+-------+-------+ 3526 * | threads | 8 | 16 | 32 | 64 | 128 | 3527 * +-----------------------+-------+-------+-------+-------+-------+ 3528 * | skylake 144 cpus | 44s | 22s | 16s | 19s | 20s | 3529 * | cascade lake 192 cpus | 39s | 20s | 11s | 10s | 9s | 3530 * +-----------------------+-------+-------+-------+-------+-------+ 3531 */ 3532 if (hugepage_allocation_threads == 0) { 3533 hugepage_allocation_threads = num_online_cpus() / 4; 3534 hugepage_allocation_threads = max(hugepage_allocation_threads, 1); 3535 } 3536 3537 job.max_threads = hugepage_allocation_threads; 3538 3539 jiffies_start = jiffies; 3540 do { 3541 remaining = h->max_huge_pages - h->nr_huge_pages; 3542 3543 job.start = h->nr_huge_pages; 3544 job.size = remaining; 3545 job.min_chunk = remaining / hugepage_allocation_threads; 3546 padata_do_multithreaded(&job); 3547 3548 if (h->nr_huge_pages == h->max_huge_pages) 3549 break; 3550 3551 /* 3552 * Retry only if the vmemmap optimization might have been able to free 3553 * some memory back to the system. 3554 */ 3555 if (!hugetlb_vmemmap_optimizable(h)) 3556 break; 3557 3558 /* Continue if progress was made in last iteration */ 3559 } while (remaining != (h->max_huge_pages - h->nr_huge_pages)); 3560 3561 jiffies_end = jiffies; 3562 3563 pr_info("HugeTLB: allocation took %dms with hugepage_allocation_threads=%ld\n", 3564 jiffies_to_msecs(jiffies_end - jiffies_start), 3565 hugepage_allocation_threads); 3566 3567 return h->nr_huge_pages; 3568 } 3569 3570 /* 3571 * NOTE: this routine is called in different contexts for gigantic and 3572 * non-gigantic pages. 3573 * - For gigantic pages, this is called early in the boot process and 3574 * pages are allocated from memblock allocated or something similar. 3575 * Gigantic pages are actually added to pools later with the routine 3576 * gather_bootmem_prealloc. 3577 * - For non-gigantic pages, this is called later in the boot process after 3578 * all of mm is up and functional. Pages are allocated from buddy and 3579 * then added to hugetlb pools. 3580 */ 3581 static void __init hugetlb_hstate_alloc_pages(struct hstate *h) 3582 { 3583 unsigned long allocated; 3584 3585 /* 3586 * Skip gigantic hugepages allocation if early CMA 3587 * reservations are not available. 3588 */ 3589 if (hstate_is_gigantic(h) && hugetlb_cma_total_size() && 3590 !hugetlb_early_cma(h)) { 3591 pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n"); 3592 return; 3593 } 3594 3595 if (!h->max_huge_pages) 3596 return; 3597 3598 /* do node specific alloc */ 3599 if (hugetlb_hstate_alloc_pages_specific_nodes(h)) 3600 return; 3601 3602 /* below will do all node balanced alloc */ 3603 if (hstate_is_gigantic(h)) 3604 allocated = hugetlb_gigantic_pages_alloc_boot(h); 3605 else 3606 allocated = hugetlb_pages_alloc_boot(h); 3607 3608 hugetlb_hstate_alloc_pages_errcheck(allocated, h); 3609 } 3610 3611 static void __init hugetlb_init_hstates(void) 3612 { 3613 struct hstate *h, *h2; 3614 3615 for_each_hstate(h) { 3616 /* 3617 * Always reset to first_memory_node here, even if 3618 * next_nid_to_alloc was set before - we can't 3619 * reference hugetlb_bootmem_nodes after init, and 3620 * first_memory_node is right for all further allocations. 3621 */ 3622 h->next_nid_to_alloc = first_memory_node; 3623 h->next_nid_to_free = first_memory_node; 3624 3625 /* oversize hugepages were init'ed in early boot */ 3626 if (!hstate_is_gigantic(h)) 3627 hugetlb_hstate_alloc_pages(h); 3628 3629 /* 3630 * Set demote order for each hstate. Note that 3631 * h->demote_order is initially 0. 3632 * - We can not demote gigantic pages if runtime freeing 3633 * is not supported, so skip this. 3634 * - If CMA allocation is possible, we can not demote 3635 * HUGETLB_PAGE_ORDER or smaller size pages. 3636 */ 3637 if (hstate_is_gigantic_no_runtime(h)) 3638 continue; 3639 if (hugetlb_cma_total_size() && h->order <= HUGETLB_PAGE_ORDER) 3640 continue; 3641 for_each_hstate(h2) { 3642 if (h2 == h) 3643 continue; 3644 if (h2->order < h->order && 3645 h2->order > h->demote_order) 3646 h->demote_order = h2->order; 3647 } 3648 } 3649 } 3650 3651 static void __init report_hugepages(void) 3652 { 3653 struct hstate *h; 3654 unsigned long nrinvalid; 3655 3656 for_each_hstate(h) { 3657 char buf[32]; 3658 3659 nrinvalid = hstate_boot_nrinvalid[hstate_index(h)]; 3660 h->max_huge_pages -= nrinvalid; 3661 3662 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); 3663 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", 3664 buf, h->nr_huge_pages); 3665 if (nrinvalid) 3666 pr_info("HugeTLB: %s page size: %lu invalid page%s discarded\n", 3667 buf, nrinvalid, str_plural(nrinvalid)); 3668 pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n", 3669 hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf); 3670 } 3671 } 3672 3673 #ifdef CONFIG_HIGHMEM 3674 static void try_to_free_low(struct hstate *h, unsigned long count, 3675 nodemask_t *nodes_allowed) 3676 { 3677 int i; 3678 LIST_HEAD(page_list); 3679 3680 lockdep_assert_held(&hugetlb_lock); 3681 if (hstate_is_gigantic(h)) 3682 return; 3683 3684 /* 3685 * Collect pages to be freed on a list, and free after dropping lock 3686 */ 3687 for_each_node_mask(i, *nodes_allowed) { 3688 struct folio *folio, *next; 3689 struct list_head *freel = &h->hugepage_freelists[i]; 3690 list_for_each_entry_safe(folio, next, freel, lru) { 3691 if (count >= h->nr_huge_pages) 3692 goto out; 3693 if (folio_test_highmem(folio)) 3694 continue; 3695 remove_hugetlb_folio(h, folio, false); 3696 list_add(&folio->lru, &page_list); 3697 } 3698 } 3699 3700 out: 3701 spin_unlock_irq(&hugetlb_lock); 3702 update_and_free_pages_bulk(h, &page_list); 3703 spin_lock_irq(&hugetlb_lock); 3704 } 3705 #else 3706 static inline void try_to_free_low(struct hstate *h, unsigned long count, 3707 nodemask_t *nodes_allowed) 3708 { 3709 } 3710 #endif 3711 3712 /* 3713 * Increment or decrement surplus_huge_pages. Keep node-specific counters 3714 * balanced by operating on them in a round-robin fashion. 3715 * Returns 1 if an adjustment was made. 3716 */ 3717 static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed, 3718 int delta) 3719 { 3720 int nr_nodes, node; 3721 3722 lockdep_assert_held(&hugetlb_lock); 3723 VM_BUG_ON(delta != -1 && delta != 1); 3724 3725 if (delta < 0) { 3726 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, nodes_allowed) { 3727 if (h->surplus_huge_pages_node[node]) 3728 goto found; 3729 } 3730 } else { 3731 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) { 3732 if (h->surplus_huge_pages_node[node] < 3733 h->nr_huge_pages_node[node]) 3734 goto found; 3735 } 3736 } 3737 return 0; 3738 3739 found: 3740 h->surplus_huge_pages += delta; 3741 h->surplus_huge_pages_node[node] += delta; 3742 return 1; 3743 } 3744 3745 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) 3746 static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, 3747 nodemask_t *nodes_allowed) 3748 { 3749 unsigned long persistent_free_count; 3750 unsigned long min_count; 3751 unsigned long allocated; 3752 struct folio *folio; 3753 LIST_HEAD(page_list); 3754 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL); 3755 3756 /* 3757 * Bit mask controlling how hard we retry per-node allocations. 3758 * If we can not allocate the bit mask, do not attempt to allocate 3759 * the requested huge pages. 3760 */ 3761 if (node_alloc_noretry) 3762 nodes_clear(*node_alloc_noretry); 3763 else 3764 return -ENOMEM; 3765 3766 /* 3767 * resize_lock mutex prevents concurrent adjustments to number of 3768 * pages in hstate via the proc/sysfs interfaces. 3769 */ 3770 mutex_lock(&h->resize_lock); 3771 flush_free_hpage_work(h); 3772 spin_lock_irq(&hugetlb_lock); 3773 3774 /* 3775 * Check for a node specific request. 3776 * Changing node specific huge page count may require a corresponding 3777 * change to the global count. In any case, the passed node mask 3778 * (nodes_allowed) will restrict alloc/free to the specified node. 3779 */ 3780 if (nid != NUMA_NO_NODE) { 3781 unsigned long old_count = count; 3782 3783 count += persistent_huge_pages(h) - 3784 (h->nr_huge_pages_node[nid] - 3785 h->surplus_huge_pages_node[nid]); 3786 /* 3787 * User may have specified a large count value which caused the 3788 * above calculation to overflow. In this case, they wanted 3789 * to allocate as many huge pages as possible. Set count to 3790 * largest possible value to align with their intention. 3791 */ 3792 if (count < old_count) 3793 count = ULONG_MAX; 3794 } 3795 3796 /* 3797 * Gigantic pages runtime allocation depend on the capability for large 3798 * page range allocation. 3799 * If the system does not provide this feature, return an error when 3800 * the user tries to allocate gigantic pages but let the user free the 3801 * boottime allocated gigantic pages. 3802 */ 3803 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) { 3804 if (count > persistent_huge_pages(h)) { 3805 spin_unlock_irq(&hugetlb_lock); 3806 mutex_unlock(&h->resize_lock); 3807 NODEMASK_FREE(node_alloc_noretry); 3808 return -EINVAL; 3809 } 3810 /* Fall through to decrease pool */ 3811 } 3812 3813 /* 3814 * Increase the pool size 3815 * First take pages out of surplus state. Then make up the 3816 * remaining difference by allocating fresh huge pages. 3817 * 3818 * We might race with alloc_surplus_hugetlb_folio() here and be unable 3819 * to convert a surplus huge page to a normal huge page. That is 3820 * not critical, though, it just means the overall size of the 3821 * pool might be one hugepage larger than it needs to be, but 3822 * within all the constraints specified by the sysctls. 3823 */ 3824 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { 3825 if (!adjust_pool_surplus(h, nodes_allowed, -1)) 3826 break; 3827 } 3828 3829 allocated = 0; 3830 while (count > (persistent_huge_pages(h) + allocated)) { 3831 /* 3832 * If this allocation races such that we no longer need the 3833 * page, free_huge_folio will handle it by freeing the page 3834 * and reducing the surplus. 3835 */ 3836 spin_unlock_irq(&hugetlb_lock); 3837 3838 /* yield cpu to avoid soft lockup */ 3839 cond_resched(); 3840 3841 folio = alloc_pool_huge_folio(h, nodes_allowed, 3842 node_alloc_noretry, 3843 &h->next_nid_to_alloc); 3844 if (!folio) { 3845 prep_and_add_allocated_folios(h, &page_list); 3846 spin_lock_irq(&hugetlb_lock); 3847 goto out; 3848 } 3849 3850 list_add(&folio->lru, &page_list); 3851 allocated++; 3852 3853 /* Bail for signals. Probably ctrl-c from user */ 3854 if (signal_pending(current)) { 3855 prep_and_add_allocated_folios(h, &page_list); 3856 spin_lock_irq(&hugetlb_lock); 3857 goto out; 3858 } 3859 3860 spin_lock_irq(&hugetlb_lock); 3861 } 3862 3863 /* Add allocated pages to the pool */ 3864 if (!list_empty(&page_list)) { 3865 spin_unlock_irq(&hugetlb_lock); 3866 prep_and_add_allocated_folios(h, &page_list); 3867 spin_lock_irq(&hugetlb_lock); 3868 } 3869 3870 /* 3871 * Decrease the pool size 3872 * First return free pages to the buddy allocator (being careful 3873 * to keep enough around to satisfy reservations). Then place 3874 * pages into surplus state as needed so the pool will shrink 3875 * to the desired size as pages become free. 3876 * 3877 * By placing pages into the surplus state independent of the 3878 * overcommit value, we are allowing the surplus pool size to 3879 * exceed overcommit. There are few sane options here. Since 3880 * alloc_surplus_hugetlb_folio() is checking the global counter, 3881 * though, we'll note that we're not allowed to exceed surplus 3882 * and won't grow the pool anywhere else. Not until one of the 3883 * sysctls are changed, or the surplus pages go out of use. 3884 * 3885 * min_count is the expected number of persistent pages, we 3886 * shouldn't calculate min_count by using 3887 * resv_huge_pages + persistent_huge_pages() - free_huge_pages, 3888 * because there may exist free surplus huge pages, and this will 3889 * lead to subtracting twice. Free surplus huge pages come from HVO 3890 * failing to restore vmemmap, see comments in the callers of 3891 * hugetlb_vmemmap_restore_folio(). Thus, we should calculate 3892 * persistent free count first. 3893 */ 3894 persistent_free_count = h->free_huge_pages; 3895 if (h->free_huge_pages > persistent_huge_pages(h)) { 3896 if (h->free_huge_pages > h->surplus_huge_pages) 3897 persistent_free_count -= h->surplus_huge_pages; 3898 else 3899 persistent_free_count = 0; 3900 } 3901 min_count = h->resv_huge_pages + persistent_huge_pages(h) - persistent_free_count; 3902 min_count = max(count, min_count); 3903 try_to_free_low(h, min_count, nodes_allowed); 3904 3905 /* 3906 * Collect pages to be removed on list without dropping lock 3907 */ 3908 while (min_count < persistent_huge_pages(h)) { 3909 folio = remove_pool_hugetlb_folio(h, nodes_allowed, 0); 3910 if (!folio) 3911 break; 3912 3913 list_add(&folio->lru, &page_list); 3914 } 3915 /* free the pages after dropping lock */ 3916 spin_unlock_irq(&hugetlb_lock); 3917 update_and_free_pages_bulk(h, &page_list); 3918 flush_free_hpage_work(h); 3919 spin_lock_irq(&hugetlb_lock); 3920 3921 while (count < persistent_huge_pages(h)) { 3922 if (!adjust_pool_surplus(h, nodes_allowed, 1)) 3923 break; 3924 } 3925 out: 3926 h->max_huge_pages = persistent_huge_pages(h); 3927 spin_unlock_irq(&hugetlb_lock); 3928 mutex_unlock(&h->resize_lock); 3929 3930 NODEMASK_FREE(node_alloc_noretry); 3931 3932 return 0; 3933 } 3934 3935 static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, 3936 struct list_head *src_list) 3937 { 3938 long rc; 3939 struct folio *folio, *next; 3940 LIST_HEAD(dst_list); 3941 LIST_HEAD(ret_list); 3942 3943 rc = hugetlb_vmemmap_restore_folios(src, src_list, &ret_list); 3944 list_splice_init(&ret_list, src_list); 3945 3946 /* 3947 * Taking target hstate mutex synchronizes with set_max_huge_pages. 3948 * Without the mutex, pages added to target hstate could be marked 3949 * as surplus. 3950 * 3951 * Note that we already hold src->resize_lock. To prevent deadlock, 3952 * use the convention of always taking larger size hstate mutex first. 3953 */ 3954 mutex_lock(&dst->resize_lock); 3955 3956 list_for_each_entry_safe(folio, next, src_list, lru) { 3957 int i; 3958 bool cma; 3959 3960 if (folio_test_hugetlb_vmemmap_optimized(folio)) 3961 continue; 3962 3963 cma = folio_test_hugetlb_cma(folio); 3964 3965 list_del(&folio->lru); 3966 3967 split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst)); 3968 pgalloc_tag_split(folio, huge_page_order(src), huge_page_order(dst)); 3969 3970 for (i = 0; i < pages_per_huge_page(src); i += pages_per_huge_page(dst)) { 3971 struct page *page = folio_page(folio, i); 3972 /* Careful: see __split_huge_page_tail() */ 3973 struct folio *new_folio = (struct folio *)page; 3974 3975 clear_compound_head(page); 3976 prep_compound_page(page, dst->order); 3977 3978 new_folio->mapping = NULL; 3979 init_new_hugetlb_folio(new_folio); 3980 /* Copy the CMA flag so that it is freed correctly */ 3981 if (cma) 3982 folio_set_hugetlb_cma(new_folio); 3983 list_add(&new_folio->lru, &dst_list); 3984 } 3985 } 3986 3987 prep_and_add_allocated_folios(dst, &dst_list); 3988 3989 mutex_unlock(&dst->resize_lock); 3990 3991 return rc; 3992 } 3993 3994 long demote_pool_huge_page(struct hstate *src, nodemask_t *nodes_allowed, 3995 unsigned long nr_to_demote) 3996 __must_hold(&hugetlb_lock) 3997 { 3998 int nr_nodes, node; 3999 struct hstate *dst; 4000 long rc = 0; 4001 long nr_demoted = 0; 4002 4003 lockdep_assert_held(&hugetlb_lock); 4004 4005 /* We should never get here if no demote order */ 4006 if (!src->demote_order) { 4007 pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n"); 4008 return -EINVAL; /* internal error */ 4009 } 4010 dst = size_to_hstate(PAGE_SIZE << src->demote_order); 4011 4012 for_each_node_mask_to_free(src, nr_nodes, node, nodes_allowed) { 4013 LIST_HEAD(list); 4014 struct folio *folio, *next; 4015 4016 list_for_each_entry_safe(folio, next, &src->hugepage_freelists[node], lru) { 4017 if (folio_test_hwpoison(folio)) 4018 continue; 4019 4020 remove_hugetlb_folio(src, folio, false); 4021 list_add(&folio->lru, &list); 4022 4023 if (++nr_demoted == nr_to_demote) 4024 break; 4025 } 4026 4027 spin_unlock_irq(&hugetlb_lock); 4028 4029 rc = demote_free_hugetlb_folios(src, dst, &list); 4030 4031 spin_lock_irq(&hugetlb_lock); 4032 4033 list_for_each_entry_safe(folio, next, &list, lru) { 4034 list_del(&folio->lru); 4035 add_hugetlb_folio(src, folio, false); 4036 4037 nr_demoted--; 4038 } 4039 4040 if (rc < 0 || nr_demoted == nr_to_demote) 4041 break; 4042 } 4043 4044 /* 4045 * Not absolutely necessary, but for consistency update max_huge_pages 4046 * based on pool changes for the demoted page. 4047 */ 4048 src->max_huge_pages -= nr_demoted; 4049 dst->max_huge_pages += nr_demoted << (huge_page_order(src) - huge_page_order(dst)); 4050 4051 if (rc < 0) 4052 return rc; 4053 4054 if (nr_demoted) 4055 return nr_demoted; 4056 /* 4057 * Only way to get here is if all pages on free lists are poisoned. 4058 * Return -EBUSY so that caller will not retry. 4059 */ 4060 return -EBUSY; 4061 } 4062 4063 ssize_t __nr_hugepages_store_common(bool obey_mempolicy, 4064 struct hstate *h, int nid, 4065 unsigned long count, size_t len) 4066 { 4067 int err; 4068 nodemask_t nodes_allowed, *n_mask; 4069 4070 if (hstate_is_gigantic_no_runtime(h)) 4071 return -EINVAL; 4072 4073 if (nid == NUMA_NO_NODE) { 4074 /* 4075 * global hstate attribute 4076 */ 4077 if (!(obey_mempolicy && 4078 init_nodemask_of_mempolicy(&nodes_allowed))) 4079 n_mask = &node_states[N_MEMORY]; 4080 else 4081 n_mask = &nodes_allowed; 4082 } else { 4083 /* 4084 * Node specific request. count adjustment happens in 4085 * set_max_huge_pages() after acquiring hugetlb_lock. 4086 */ 4087 init_nodemask_of_node(&nodes_allowed, nid); 4088 n_mask = &nodes_allowed; 4089 } 4090 4091 err = set_max_huge_pages(h, count, nid, n_mask); 4092 4093 return err ? err : len; 4094 } 4095 4096 static int __init hugetlb_init(void) 4097 { 4098 int i; 4099 4100 BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE < 4101 __NR_HPAGEFLAGS); 4102 BUILD_BUG_ON_INVALID(HUGETLB_PAGE_ORDER > MAX_FOLIO_ORDER); 4103 4104 if (!hugepages_supported()) { 4105 if (hugetlb_max_hstate || default_hstate_max_huge_pages) 4106 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n"); 4107 return 0; 4108 } 4109 4110 /* 4111 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some 4112 * architectures depend on setup being done here. 4113 */ 4114 hugetlb_add_hstate(HUGETLB_PAGE_ORDER); 4115 if (!parsed_default_hugepagesz) { 4116 /* 4117 * If we did not parse a default huge page size, set 4118 * default_hstate_idx to HPAGE_SIZE hstate. And, if the 4119 * number of huge pages for this default size was implicitly 4120 * specified, set that here as well. 4121 * Note that the implicit setting will overwrite an explicit 4122 * setting. A warning will be printed in this case. 4123 */ 4124 default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE)); 4125 if (default_hstate_max_huge_pages) { 4126 if (default_hstate.max_huge_pages) { 4127 char buf[32]; 4128 4129 string_get_size(huge_page_size(&default_hstate), 4130 1, STRING_UNITS_2, buf, 32); 4131 pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n", 4132 default_hstate.max_huge_pages, buf); 4133 pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n", 4134 default_hstate_max_huge_pages); 4135 } 4136 default_hstate.max_huge_pages = 4137 default_hstate_max_huge_pages; 4138 4139 for_each_online_node(i) 4140 default_hstate.max_huge_pages_node[i] = 4141 default_hugepages_in_node[i]; 4142 } 4143 } 4144 4145 hugetlb_init_hstates(); 4146 gather_bootmem_prealloc(); 4147 report_hugepages(); 4148 4149 hugetlb_sysfs_init(); 4150 hugetlb_cgroup_file_init(); 4151 hugetlb_sysctl_init(); 4152 4153 #ifdef CONFIG_SMP 4154 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); 4155 #else 4156 num_fault_mutexes = 1; 4157 #endif 4158 hugetlb_fault_mutex_table = 4159 kmalloc_objs(struct mutex, num_fault_mutexes); 4160 BUG_ON(!hugetlb_fault_mutex_table); 4161 4162 for (i = 0; i < num_fault_mutexes; i++) 4163 mutex_init(&hugetlb_fault_mutex_table[i]); 4164 return 0; 4165 } 4166 subsys_initcall(hugetlb_init); 4167 4168 /* Overwritten by architectures with more huge page sizes */ 4169 bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size) 4170 { 4171 return size == HPAGE_SIZE; 4172 } 4173 4174 void __init hugetlb_add_hstate(unsigned int order) 4175 { 4176 struct hstate *h; 4177 unsigned long i; 4178 4179 if (size_to_hstate(PAGE_SIZE << order)) { 4180 return; 4181 } 4182 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE); 4183 BUG_ON(order < order_base_2(__NR_USED_SUBPAGE)); 4184 WARN_ON(order > MAX_FOLIO_ORDER); 4185 h = &hstates[hugetlb_max_hstate++]; 4186 __mutex_init(&h->resize_lock, "resize mutex", &h->resize_key); 4187 h->order = order; 4188 h->mask = ~(huge_page_size(h) - 1); 4189 for (i = 0; i < MAX_NUMNODES; ++i) 4190 INIT_LIST_HEAD(&h->hugepage_freelists[i]); 4191 INIT_LIST_HEAD(&h->hugepage_activelist); 4192 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", 4193 huge_page_size(h)/SZ_1K); 4194 4195 parsed_hstate = h; 4196 } 4197 4198 bool __init __weak hugetlb_node_alloc_supported(void) 4199 { 4200 return true; 4201 } 4202 4203 static void __init hugepages_clear_pages_in_node(void) 4204 { 4205 if (!hugetlb_max_hstate) { 4206 default_hstate_max_huge_pages = 0; 4207 memset(default_hugepages_in_node, 0, 4208 sizeof(default_hugepages_in_node)); 4209 } else { 4210 parsed_hstate->max_huge_pages = 0; 4211 memset(parsed_hstate->max_huge_pages_node, 0, 4212 sizeof(parsed_hstate->max_huge_pages_node)); 4213 } 4214 } 4215 4216 static __init int hugetlb_add_param(char *s, int (*setup)(char *)) 4217 { 4218 size_t len; 4219 char *p; 4220 4221 if (!s) 4222 return -EINVAL; 4223 4224 if (hugetlb_param_index >= HUGE_MAX_CMDLINE_ARGS) 4225 return -EINVAL; 4226 4227 len = strlen(s) + 1; 4228 if (len + hstate_cmdline_index > sizeof(hstate_cmdline_buf)) 4229 return -EINVAL; 4230 4231 p = &hstate_cmdline_buf[hstate_cmdline_index]; 4232 memcpy(p, s, len); 4233 hstate_cmdline_index += len; 4234 4235 hugetlb_params[hugetlb_param_index].val = p; 4236 hugetlb_params[hugetlb_param_index].setup = setup; 4237 4238 hugetlb_param_index++; 4239 4240 return 0; 4241 } 4242 4243 static __init void hugetlb_parse_params(void) 4244 { 4245 int i; 4246 struct hugetlb_cmdline *hcp; 4247 4248 for (i = 0; i < hugetlb_param_index; i++) { 4249 hcp = &hugetlb_params[i]; 4250 4251 hcp->setup(hcp->val); 4252 } 4253 4254 hugetlb_cma_validate_params(); 4255 } 4256 4257 /* 4258 * hugepages command line processing 4259 * hugepages normally follows a valid hugepagsz or default_hugepagsz 4260 * specification. If not, ignore the hugepages value. hugepages can also 4261 * be the first huge page command line option in which case it implicitly 4262 * specifies the number of huge pages for the default size. 4263 */ 4264 static int __init hugepages_setup(char *s) 4265 { 4266 unsigned long *mhp; 4267 static unsigned long *last_mhp; 4268 int node = NUMA_NO_NODE; 4269 int count; 4270 unsigned long tmp; 4271 char *p = s; 4272 4273 if (!hugepages_supported()) { 4274 pr_warn("HugeTLB: hugepages unsupported, ignoring hugepages=%s cmdline\n", s); 4275 return 0; 4276 } 4277 4278 if (!parsed_valid_hugepagesz) { 4279 pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s); 4280 parsed_valid_hugepagesz = true; 4281 return -EINVAL; 4282 } 4283 4284 /* 4285 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter 4286 * yet, so this hugepages= parameter goes to the "default hstate". 4287 * Otherwise, it goes with the previously parsed hugepagesz or 4288 * default_hugepagesz. 4289 */ 4290 else if (!hugetlb_max_hstate) 4291 mhp = &default_hstate_max_huge_pages; 4292 else 4293 mhp = &parsed_hstate->max_huge_pages; 4294 4295 if (mhp == last_mhp) { 4296 pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s); 4297 return 1; 4298 } 4299 4300 while (*p) { 4301 count = 0; 4302 if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4303 goto invalid; 4304 /* Parameter is node format */ 4305 if (p[count] == ':') { 4306 if (!hugetlb_node_alloc_supported()) { 4307 pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n"); 4308 return 1; 4309 } 4310 if (tmp >= MAX_NUMNODES || !node_online(tmp)) 4311 goto invalid; 4312 node = array_index_nospec(tmp, MAX_NUMNODES); 4313 p += count + 1; 4314 /* Parse hugepages */ 4315 if (sscanf(p, "%lu%n", &tmp, &count) != 1) 4316 goto invalid; 4317 if (!hugetlb_max_hstate) 4318 default_hugepages_in_node[node] = tmp; 4319 else 4320 parsed_hstate->max_huge_pages_node[node] = tmp; 4321 *mhp += tmp; 4322 /* Go to parse next node*/ 4323 if (p[count] == ',') 4324 p += count + 1; 4325 else 4326 break; 4327 } else { 4328 if (p != s) 4329 goto invalid; 4330 *mhp = tmp; 4331 break; 4332 } 4333 } 4334 4335 last_mhp = mhp; 4336 4337 return 0; 4338 4339 invalid: 4340 pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p); 4341 hugepages_clear_pages_in_node(); 4342 return -EINVAL; 4343 } 4344 hugetlb_early_param("hugepages", hugepages_setup); 4345 4346 /* 4347 * hugepagesz command line processing 4348 * A specific huge page size can only be specified once with hugepagesz. 4349 * hugepagesz is followed by hugepages on the command line. The global 4350 * variable 'parsed_valid_hugepagesz' is used to determine if prior 4351 * hugepagesz argument was valid. 4352 */ 4353 static int __init hugepagesz_setup(char *s) 4354 { 4355 unsigned long size; 4356 struct hstate *h; 4357 4358 if (!hugepages_supported()) { 4359 pr_warn("HugeTLB: hugepages unsupported, ignoring hugepagesz=%s cmdline\n", s); 4360 return 0; 4361 } 4362 4363 parsed_valid_hugepagesz = false; 4364 size = (unsigned long)memparse(s, NULL); 4365 4366 if (!arch_hugetlb_valid_size(size)) { 4367 pr_err("HugeTLB: unsupported hugepagesz=%s\n", s); 4368 return -EINVAL; 4369 } 4370 4371 h = size_to_hstate(size); 4372 if (h) { 4373 /* 4374 * hstate for this size already exists. This is normally 4375 * an error, but is allowed if the existing hstate is the 4376 * default hstate. More specifically, it is only allowed if 4377 * the number of huge pages for the default hstate was not 4378 * previously specified. 4379 */ 4380 if (!parsed_default_hugepagesz || h != &default_hstate || 4381 default_hstate.max_huge_pages) { 4382 pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s); 4383 return -EINVAL; 4384 } 4385 4386 /* 4387 * No need to call hugetlb_add_hstate() as hstate already 4388 * exists. But, do set parsed_hstate so that a following 4389 * hugepages= parameter will be applied to this hstate. 4390 */ 4391 parsed_hstate = h; 4392 parsed_valid_hugepagesz = true; 4393 return 0; 4394 } 4395 4396 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4397 parsed_valid_hugepagesz = true; 4398 return 0; 4399 } 4400 hugetlb_early_param("hugepagesz", hugepagesz_setup); 4401 4402 /* 4403 * default_hugepagesz command line input 4404 * Only one instance of default_hugepagesz allowed on command line. 4405 */ 4406 static int __init default_hugepagesz_setup(char *s) 4407 { 4408 unsigned long size; 4409 int i; 4410 4411 if (!hugepages_supported()) { 4412 pr_warn("HugeTLB: hugepages unsupported, ignoring default_hugepagesz=%s cmdline\n", 4413 s); 4414 return 0; 4415 } 4416 4417 parsed_valid_hugepagesz = false; 4418 if (parsed_default_hugepagesz) { 4419 pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s); 4420 return -EINVAL; 4421 } 4422 4423 size = (unsigned long)memparse(s, NULL); 4424 4425 if (!arch_hugetlb_valid_size(size)) { 4426 pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s); 4427 return -EINVAL; 4428 } 4429 4430 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); 4431 parsed_valid_hugepagesz = true; 4432 parsed_default_hugepagesz = true; 4433 default_hstate_idx = hstate_index(size_to_hstate(size)); 4434 4435 /* 4436 * The number of default huge pages (for this size) could have been 4437 * specified as the first hugetlb parameter: hugepages=X. If so, 4438 * then default_hstate_max_huge_pages is set. If the default huge 4439 * page size is gigantic (> MAX_PAGE_ORDER), then the pages must be 4440 * allocated here from bootmem allocator. 4441 */ 4442 if (default_hstate_max_huge_pages) { 4443 default_hstate.max_huge_pages = default_hstate_max_huge_pages; 4444 /* 4445 * Since this is an early parameter, we can't check 4446 * NUMA node state yet, so loop through MAX_NUMNODES. 4447 */ 4448 for (i = 0; i < MAX_NUMNODES; i++) { 4449 if (default_hugepages_in_node[i] != 0) 4450 default_hstate.max_huge_pages_node[i] = 4451 default_hugepages_in_node[i]; 4452 } 4453 default_hstate_max_huge_pages = 0; 4454 } 4455 4456 return 0; 4457 } 4458 hugetlb_early_param("default_hugepagesz", default_hugepagesz_setup); 4459 4460 void __init hugetlb_bootmem_set_nodes(void) 4461 { 4462 int i, nid; 4463 unsigned long start_pfn, end_pfn; 4464 4465 if (!nodes_empty(hugetlb_bootmem_nodes)) 4466 return; 4467 4468 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 4469 if (end_pfn > start_pfn) 4470 node_set(nid, hugetlb_bootmem_nodes); 4471 } 4472 } 4473 4474 void __init hugetlb_bootmem_alloc(void) 4475 { 4476 struct hstate *h; 4477 int i; 4478 4479 hugetlb_bootmem_set_nodes(); 4480 4481 for (i = 0; i < MAX_NUMNODES; i++) 4482 INIT_LIST_HEAD(&huge_boot_pages[i]); 4483 4484 hugetlb_parse_params(); 4485 4486 for_each_hstate(h) { 4487 h->next_nid_to_alloc = first_online_node; 4488 4489 if (hstate_is_gigantic(h)) 4490 hugetlb_hstate_alloc_pages(h); 4491 } 4492 } 4493 4494 /* 4495 * hugepage_alloc_threads command line parsing. 4496 * 4497 * When set, use this specific number of threads for the boot 4498 * allocation of hugepages. 4499 */ 4500 static int __init hugepage_alloc_threads_setup(char *s) 4501 { 4502 unsigned long allocation_threads; 4503 4504 if (kstrtoul(s, 0, &allocation_threads) != 0) 4505 return 1; 4506 4507 if (allocation_threads == 0) 4508 return 1; 4509 4510 hugepage_allocation_threads = allocation_threads; 4511 4512 return 1; 4513 } 4514 __setup("hugepage_alloc_threads=", hugepage_alloc_threads_setup); 4515 4516 static unsigned int allowed_mems_nr(struct hstate *h) 4517 { 4518 int node; 4519 unsigned int nr = 0; 4520 nodemask_t *mbind_nodemask; 4521 unsigned int *array = h->free_huge_pages_node; 4522 gfp_t gfp_mask = htlb_alloc_mask(h); 4523 4524 mbind_nodemask = policy_mbind_nodemask(gfp_mask); 4525 for_each_node_mask(node, cpuset_current_mems_allowed) { 4526 if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) 4527 nr += array[node]; 4528 } 4529 4530 return nr; 4531 } 4532 4533 void hugetlb_report_meminfo(struct seq_file *m) 4534 { 4535 struct hstate *h; 4536 unsigned long total = 0; 4537 4538 if (!hugepages_supported()) 4539 return; 4540 4541 for_each_hstate(h) { 4542 unsigned long count = h->nr_huge_pages; 4543 4544 total += huge_page_size(h) * count; 4545 4546 if (h == &default_hstate) 4547 seq_printf(m, 4548 "HugePages_Total: %5lu\n" 4549 "HugePages_Free: %5lu\n" 4550 "HugePages_Rsvd: %5lu\n" 4551 "HugePages_Surp: %5lu\n" 4552 "Hugepagesize: %8lu kB\n", 4553 count, 4554 h->free_huge_pages, 4555 h->resv_huge_pages, 4556 h->surplus_huge_pages, 4557 huge_page_size(h) / SZ_1K); 4558 } 4559 4560 seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K); 4561 } 4562 4563 int hugetlb_report_node_meminfo(char *buf, int len, int nid) 4564 { 4565 struct hstate *h = &default_hstate; 4566 4567 if (!hugepages_supported()) 4568 return 0; 4569 4570 return sysfs_emit_at(buf, len, 4571 "Node %d HugePages_Total: %5u\n" 4572 "Node %d HugePages_Free: %5u\n" 4573 "Node %d HugePages_Surp: %5u\n", 4574 nid, h->nr_huge_pages_node[nid], 4575 nid, h->free_huge_pages_node[nid], 4576 nid, h->surplus_huge_pages_node[nid]); 4577 } 4578 4579 void hugetlb_show_meminfo_node(int nid) 4580 { 4581 struct hstate *h; 4582 4583 if (!hugepages_supported()) 4584 return; 4585 4586 for_each_hstate(h) 4587 printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", 4588 nid, 4589 h->nr_huge_pages_node[nid], 4590 h->free_huge_pages_node[nid], 4591 h->surplus_huge_pages_node[nid], 4592 huge_page_size(h) / SZ_1K); 4593 } 4594 4595 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) 4596 { 4597 seq_printf(m, "HugetlbPages:\t%8lu kB\n", 4598 K(atomic_long_read(&mm->hugetlb_usage))); 4599 } 4600 4601 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ 4602 unsigned long hugetlb_total_pages(void) 4603 { 4604 struct hstate *h; 4605 unsigned long nr_total_pages = 0; 4606 4607 for_each_hstate(h) 4608 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); 4609 return nr_total_pages; 4610 } 4611 4612 static int hugetlb_acct_memory(struct hstate *h, long delta) 4613 { 4614 int ret = -ENOMEM; 4615 4616 if (!delta) 4617 return 0; 4618 4619 spin_lock_irq(&hugetlb_lock); 4620 /* 4621 * When cpuset is configured, it breaks the strict hugetlb page 4622 * reservation as the accounting is done on a global variable. Such 4623 * reservation is completely rubbish in the presence of cpuset because 4624 * the reservation is not checked against page availability for the 4625 * current cpuset. Application can still potentially OOM'ed by kernel 4626 * with lack of free htlb page in cpuset that the task is in. 4627 * Attempt to enforce strict accounting with cpuset is almost 4628 * impossible (or too ugly) because cpuset is too fluid that 4629 * task or memory node can be dynamically moved between cpusets. 4630 * 4631 * The change of semantics for shared hugetlb mapping with cpuset is 4632 * undesirable. However, in order to preserve some of the semantics, 4633 * we fall back to check against current free page availability as 4634 * a best attempt and hopefully to minimize the impact of changing 4635 * semantics that cpuset has. 4636 * 4637 * Apart from cpuset, we also have memory policy mechanism that 4638 * also determines from which node the kernel will allocate memory 4639 * in a NUMA system. So similar to cpuset, we also should consider 4640 * the memory policy of the current task. Similar to the description 4641 * above. 4642 */ 4643 if (delta > 0) { 4644 if (gather_surplus_pages(h, delta) < 0) 4645 goto out; 4646 4647 if (delta > allowed_mems_nr(h)) { 4648 return_unused_surplus_pages(h, delta); 4649 goto out; 4650 } 4651 } 4652 4653 ret = 0; 4654 if (delta < 0) 4655 return_unused_surplus_pages(h, (unsigned long) -delta); 4656 4657 out: 4658 spin_unlock_irq(&hugetlb_lock); 4659 return ret; 4660 } 4661 4662 static void hugetlb_vm_op_open(struct vm_area_struct *vma) 4663 { 4664 struct resv_map *resv = vma_resv_map(vma); 4665 4666 /* 4667 * HPAGE_RESV_OWNER indicates a private mapping. 4668 * This new VMA should share its siblings reservation map if present. 4669 * The VMA will only ever have a valid reservation map pointer where 4670 * it is being copied for another still existing VMA. As that VMA 4671 * has a reference to the reservation map it cannot disappear until 4672 * after this open call completes. It is therefore safe to take a 4673 * new reference here without additional locking. 4674 */ 4675 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { 4676 resv_map_dup_hugetlb_cgroup_uncharge_info(resv); 4677 kref_get(&resv->refs); 4678 } 4679 4680 /* 4681 * vma_lock structure for sharable mappings is vma specific. 4682 * Clear old pointer (if copied via vm_area_dup) and allocate 4683 * new structure. Before clearing, make sure vma_lock is not 4684 * for this vma. 4685 */ 4686 if (vma->vm_flags & VM_MAYSHARE) { 4687 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 4688 4689 if (vma_lock) { 4690 if (vma_lock->vma != vma) { 4691 vma->vm_private_data = NULL; 4692 hugetlb_vma_lock_alloc(vma); 4693 } else { 4694 pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__); 4695 } 4696 } else { 4697 hugetlb_vma_lock_alloc(vma); 4698 } 4699 } 4700 } 4701 4702 static void hugetlb_vm_op_close(struct vm_area_struct *vma) 4703 { 4704 struct hstate *h = hstate_vma(vma); 4705 struct resv_map *resv; 4706 struct hugepage_subpool *spool = subpool_vma(vma); 4707 unsigned long reserve, start, end; 4708 long gbl_reserve; 4709 4710 hugetlb_vma_lock_free(vma); 4711 4712 resv = vma_resv_map(vma); 4713 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) 4714 return; 4715 4716 start = vma_hugecache_offset(h, vma, vma->vm_start); 4717 end = vma_hugecache_offset(h, vma, vma->vm_end); 4718 4719 reserve = (end - start) - region_count(resv, start, end); 4720 hugetlb_cgroup_uncharge_counter(resv, start, end); 4721 if (reserve) { 4722 /* 4723 * Decrement reserve counts. The global reserve count may be 4724 * adjusted if the subpool has a minimum size. 4725 */ 4726 gbl_reserve = hugepage_subpool_put_pages(spool, reserve); 4727 hugetlb_acct_memory(h, -gbl_reserve); 4728 } 4729 4730 kref_put(&resv->refs, resv_map_release); 4731 } 4732 4733 static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) 4734 { 4735 if (addr & ~(huge_page_mask(hstate_vma(vma)))) 4736 return -EINVAL; 4737 return 0; 4738 } 4739 4740 void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) 4741 { 4742 /* 4743 * PMD sharing is only possible for PUD_SIZE-aligned address ranges 4744 * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this 4745 * split, unshare PMDs in the PUD_SIZE interval surrounding addr now. 4746 * This function is called in the middle of a VMA split operation, with 4747 * MM, VMA and rmap all write-locked to prevent concurrent page table 4748 * walks (except hardware and gup_fast()). 4749 */ 4750 vma_assert_write_locked(vma); 4751 i_mmap_assert_write_locked(vma->vm_file->f_mapping); 4752 4753 if (addr & ~PUD_MASK) { 4754 unsigned long floor = addr & PUD_MASK; 4755 unsigned long ceil = floor + PUD_SIZE; 4756 4757 if (floor >= vma->vm_start && ceil <= vma->vm_end) { 4758 /* 4759 * Locking: 4760 * Use take_locks=false here. 4761 * The file rmap lock is already held. 4762 * The hugetlb VMA lock can't be taken when we already 4763 * hold the file rmap lock, and we don't need it because 4764 * its purpose is to synchronize against concurrent page 4765 * table walks, which are not possible thanks to the 4766 * locks held by our caller. 4767 */ 4768 hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false); 4769 } 4770 } 4771 } 4772 4773 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) 4774 { 4775 return huge_page_size(hstate_vma(vma)); 4776 } 4777 4778 /* 4779 * We cannot handle pagefaults against hugetlb pages at all. They cause 4780 * handle_mm_fault() to try to instantiate regular-sized pages in the 4781 * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get 4782 * this far. 4783 */ 4784 static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf) 4785 { 4786 BUG(); 4787 return 0; 4788 } 4789 4790 #ifdef CONFIG_USERFAULTFD 4791 static bool hugetlb_can_userfault(struct vm_area_struct *vma, 4792 vm_flags_t vm_flags) 4793 { 4794 return true; 4795 } 4796 4797 static const struct vm_uffd_ops hugetlb_uffd_ops = { 4798 .can_userfault = hugetlb_can_userfault, 4799 }; 4800 #endif 4801 4802 /* 4803 * When a new function is introduced to vm_operations_struct and added 4804 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops. 4805 * This is because under System V memory model, mappings created via 4806 * shmget/shmat with "huge page" specified are backed by hugetlbfs files, 4807 * their original vm_ops are overwritten with shm_vm_ops. 4808 */ 4809 const struct vm_operations_struct hugetlb_vm_ops = { 4810 .fault = hugetlb_vm_op_fault, 4811 .open = hugetlb_vm_op_open, 4812 .close = hugetlb_vm_op_close, 4813 .may_split = hugetlb_vm_op_split, 4814 .pagesize = hugetlb_vm_op_pagesize, 4815 #ifdef CONFIG_USERFAULTFD 4816 .uffd_ops = &hugetlb_uffd_ops, 4817 #endif 4818 }; 4819 4820 static pte_t make_huge_pte(struct vm_area_struct *vma, struct folio *folio, 4821 bool try_mkwrite) 4822 { 4823 pte_t entry = folio_mk_pte(folio, vma->vm_page_prot); 4824 unsigned int shift = huge_page_shift(hstate_vma(vma)); 4825 4826 if (try_mkwrite && (vma->vm_flags & VM_WRITE)) { 4827 entry = pte_mkwrite_novma(pte_mkdirty(entry)); 4828 } else { 4829 entry = pte_wrprotect(entry); 4830 } 4831 entry = pte_mkyoung(entry); 4832 entry = arch_make_huge_pte(entry, shift, vma->vm_flags); 4833 4834 return entry; 4835 } 4836 4837 static void set_huge_ptep_writable(struct vm_area_struct *vma, 4838 unsigned long address, pte_t *ptep) 4839 { 4840 pte_t entry; 4841 4842 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(vma->vm_mm, address, ptep))); 4843 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) 4844 update_mmu_cache(vma, address, ptep); 4845 } 4846 4847 static void set_huge_ptep_maybe_writable(struct vm_area_struct *vma, 4848 unsigned long address, pte_t *ptep) 4849 { 4850 if (vma->vm_flags & VM_WRITE) 4851 set_huge_ptep_writable(vma, address, ptep); 4852 } 4853 4854 static void 4855 hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr, 4856 struct folio *new_folio, pte_t old, unsigned long sz) 4857 { 4858 pte_t newpte = make_huge_pte(vma, new_folio, true); 4859 4860 __folio_mark_uptodate(new_folio); 4861 hugetlb_add_new_anon_rmap(new_folio, vma, addr); 4862 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old)) 4863 newpte = huge_pte_mkuffd_wp(newpte); 4864 set_huge_pte_at(vma->vm_mm, addr, ptep, newpte, sz); 4865 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); 4866 folio_set_hugetlb_migratable(new_folio); 4867 } 4868 4869 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, 4870 struct vm_area_struct *dst_vma, 4871 struct vm_area_struct *src_vma) 4872 { 4873 pte_t *src_pte, *dst_pte, entry; 4874 struct folio *pte_folio; 4875 unsigned long addr; 4876 bool cow = is_cow_mapping(src_vma->vm_flags); 4877 struct hstate *h = hstate_vma(src_vma); 4878 unsigned long sz = huge_page_size(h); 4879 unsigned long npages = pages_per_huge_page(h); 4880 struct mmu_notifier_range range; 4881 unsigned long last_addr_mask; 4882 softleaf_t softleaf; 4883 int ret = 0; 4884 4885 if (cow) { 4886 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src, 4887 src_vma->vm_start, 4888 src_vma->vm_end); 4889 mmu_notifier_invalidate_range_start(&range); 4890 vma_assert_write_locked(src_vma); 4891 raw_write_seqcount_begin(&src->write_protect_seq); 4892 } else { 4893 /* 4894 * For shared mappings the vma lock must be held before 4895 * calling hugetlb_walk() in the src vma. Otherwise, the 4896 * returned ptep could go away if part of a shared pmd and 4897 * another thread calls huge_pmd_unshare. 4898 */ 4899 hugetlb_vma_lock_read(src_vma); 4900 } 4901 4902 last_addr_mask = hugetlb_mask_last_page(h); 4903 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { 4904 spinlock_t *src_ptl, *dst_ptl; 4905 src_pte = hugetlb_walk(src_vma, addr, sz); 4906 if (!src_pte) { 4907 addr |= last_addr_mask; 4908 continue; 4909 } 4910 dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz); 4911 if (!dst_pte) { 4912 ret = -ENOMEM; 4913 break; 4914 } 4915 4916 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 4917 /* If the pagetables are shared, there is nothing to do */ 4918 if (ptdesc_pmd_is_shared(virt_to_ptdesc(dst_pte))) { 4919 addr |= last_addr_mask; 4920 continue; 4921 } 4922 #endif 4923 4924 dst_ptl = huge_pte_lock(h, dst, dst_pte); 4925 src_ptl = huge_pte_lockptr(h, src, src_pte); 4926 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 4927 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); 4928 again: 4929 if (huge_pte_none(entry)) { 4930 /* Skip if src entry none. */ 4931 goto next; 4932 } 4933 4934 softleaf = softleaf_from_pte(entry); 4935 if (unlikely(softleaf_is_hwpoison(softleaf))) { 4936 if (!userfaultfd_wp(dst_vma)) 4937 entry = huge_pte_clear_uffd_wp(entry); 4938 set_huge_pte_at(dst, addr, dst_pte, entry, sz); 4939 } else if (unlikely(softleaf_is_migration(softleaf))) { 4940 bool uffd_wp = pte_swp_uffd_wp(entry); 4941 4942 if (!softleaf_is_migration_read(softleaf) && cow) { 4943 /* 4944 * COW mappings require pages in both 4945 * parent and child to be set to read. 4946 */ 4947 softleaf = make_readable_migration_entry( 4948 swp_offset(softleaf)); 4949 entry = swp_entry_to_pte(softleaf); 4950 if (userfaultfd_wp(src_vma) && uffd_wp) 4951 entry = pte_swp_mkuffd_wp(entry); 4952 set_huge_pte_at(src, addr, src_pte, entry, sz); 4953 } 4954 if (!userfaultfd_wp(dst_vma)) 4955 entry = huge_pte_clear_uffd_wp(entry); 4956 set_huge_pte_at(dst, addr, dst_pte, entry, sz); 4957 } else if (unlikely(pte_is_marker(entry))) { 4958 const pte_marker marker = copy_pte_marker(softleaf, dst_vma); 4959 4960 if (marker) 4961 set_huge_pte_at(dst, addr, dst_pte, 4962 make_pte_marker(marker), sz); 4963 } else { 4964 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); 4965 pte_folio = page_folio(pte_page(entry)); 4966 folio_get(pte_folio); 4967 4968 /* 4969 * Failing to duplicate the anon rmap is a rare case 4970 * where we see pinned hugetlb pages while they're 4971 * prone to COW. We need to do the COW earlier during 4972 * fork. 4973 * 4974 * When pre-allocating the page or copying data, we 4975 * need to be without the pgtable locks since we could 4976 * sleep during the process. 4977 */ 4978 if (!folio_test_anon(pte_folio)) { 4979 hugetlb_add_file_rmap(pte_folio); 4980 } else if (hugetlb_try_dup_anon_rmap(pte_folio, src_vma)) { 4981 pte_t src_pte_old = entry; 4982 struct folio *new_folio; 4983 4984 spin_unlock(src_ptl); 4985 spin_unlock(dst_ptl); 4986 /* Do not use reserve as it's private owned */ 4987 new_folio = alloc_hugetlb_folio(dst_vma, addr, false); 4988 if (IS_ERR(new_folio)) { 4989 folio_put(pte_folio); 4990 ret = PTR_ERR(new_folio); 4991 break; 4992 } 4993 ret = copy_user_large_folio(new_folio, pte_folio, 4994 addr, dst_vma); 4995 folio_put(pte_folio); 4996 if (ret) { 4997 folio_put(new_folio); 4998 break; 4999 } 5000 5001 /* Install the new hugetlb folio if src pte stable */ 5002 dst_ptl = huge_pte_lock(h, dst, dst_pte); 5003 src_ptl = huge_pte_lockptr(h, src, src_pte); 5004 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5005 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); 5006 if (!pte_same(src_pte_old, entry)) { 5007 restore_reserve_on_error(h, dst_vma, addr, 5008 new_folio); 5009 folio_put(new_folio); 5010 /* huge_ptep of dst_pte won't change as in child */ 5011 goto again; 5012 } 5013 hugetlb_install_folio(dst_vma, dst_pte, addr, 5014 new_folio, src_pte_old, sz); 5015 goto next; 5016 } 5017 5018 if (cow) { 5019 /* 5020 * No need to notify as we are downgrading page 5021 * table protection not changing it to point 5022 * to a new page. 5023 * 5024 * See Documentation/mm/mmu_notifier.rst 5025 */ 5026 huge_ptep_set_wrprotect(src, addr, src_pte); 5027 entry = huge_pte_wrprotect(entry); 5028 } 5029 5030 if (!userfaultfd_wp(dst_vma)) 5031 entry = huge_pte_clear_uffd_wp(entry); 5032 5033 set_huge_pte_at(dst, addr, dst_pte, entry, sz); 5034 hugetlb_count_add(npages, dst); 5035 } 5036 5037 next: 5038 spin_unlock(src_ptl); 5039 spin_unlock(dst_ptl); 5040 } 5041 5042 if (cow) { 5043 raw_write_seqcount_end(&src->write_protect_seq); 5044 mmu_notifier_invalidate_range_end(&range); 5045 } else { 5046 hugetlb_vma_unlock_read(src_vma); 5047 } 5048 5049 return ret; 5050 } 5051 5052 static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr, 5053 unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte, 5054 unsigned long sz) 5055 { 5056 bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma); 5057 struct hstate *h = hstate_vma(vma); 5058 struct mm_struct *mm = vma->vm_mm; 5059 spinlock_t *src_ptl, *dst_ptl; 5060 pte_t pte; 5061 5062 dst_ptl = huge_pte_lock(h, mm, dst_pte); 5063 src_ptl = huge_pte_lockptr(h, mm, src_pte); 5064 5065 /* 5066 * We don't have to worry about the ordering of src and dst ptlocks 5067 * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock. 5068 */ 5069 if (src_ptl != dst_ptl) 5070 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); 5071 5072 pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz); 5073 5074 if (need_clear_uffd_wp && pte_is_uffd_wp_marker(pte)) { 5075 huge_pte_clear(mm, new_addr, dst_pte, sz); 5076 } else { 5077 if (need_clear_uffd_wp) { 5078 if (pte_present(pte)) 5079 pte = huge_pte_clear_uffd_wp(pte); 5080 else 5081 pte = pte_swp_clear_uffd_wp(pte); 5082 } 5083 set_huge_pte_at(mm, new_addr, dst_pte, pte, sz); 5084 } 5085 5086 if (src_ptl != dst_ptl) 5087 spin_unlock(src_ptl); 5088 spin_unlock(dst_ptl); 5089 } 5090 5091 int move_hugetlb_page_tables(struct vm_area_struct *vma, 5092 struct vm_area_struct *new_vma, 5093 unsigned long old_addr, unsigned long new_addr, 5094 unsigned long len) 5095 { 5096 struct hstate *h = hstate_vma(vma); 5097 struct address_space *mapping = vma->vm_file->f_mapping; 5098 unsigned long sz = huge_page_size(h); 5099 struct mm_struct *mm = vma->vm_mm; 5100 unsigned long old_end = old_addr + len; 5101 unsigned long last_addr_mask; 5102 pte_t *src_pte, *dst_pte; 5103 struct mmu_notifier_range range; 5104 struct mmu_gather tlb; 5105 5106 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr, 5107 old_end); 5108 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 5109 /* 5110 * In case of shared PMDs, we should cover the maximum possible 5111 * range. 5112 */ 5113 flush_cache_range(vma, range.start, range.end); 5114 tlb_gather_mmu_vma(&tlb, vma); 5115 5116 mmu_notifier_invalidate_range_start(&range); 5117 last_addr_mask = hugetlb_mask_last_page(h); 5118 /* Prevent race with file truncation */ 5119 hugetlb_vma_lock_write(vma); 5120 i_mmap_lock_write(mapping); 5121 for (; old_addr < old_end; old_addr += sz, new_addr += sz) { 5122 src_pte = hugetlb_walk(vma, old_addr, sz); 5123 if (!src_pte) { 5124 old_addr |= last_addr_mask; 5125 new_addr |= last_addr_mask; 5126 continue; 5127 } 5128 if (huge_pte_none(huge_ptep_get(mm, old_addr, src_pte))) 5129 continue; 5130 5131 if (huge_pmd_unshare(&tlb, vma, old_addr, src_pte)) { 5132 old_addr |= last_addr_mask; 5133 new_addr |= last_addr_mask; 5134 continue; 5135 } 5136 5137 dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz); 5138 if (!dst_pte) 5139 break; 5140 5141 move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte, sz); 5142 tlb_remove_huge_tlb_entry(h, &tlb, src_pte, old_addr); 5143 } 5144 5145 tlb_flush_mmu_tlbonly(&tlb); 5146 huge_pmd_unshare_flush(&tlb, vma); 5147 5148 mmu_notifier_invalidate_range_end(&range); 5149 i_mmap_unlock_write(mapping); 5150 hugetlb_vma_unlock_write(vma); 5151 tlb_finish_mmu(&tlb); 5152 5153 return len + old_addr - old_end; 5154 } 5155 5156 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, 5157 unsigned long start, unsigned long end, 5158 struct folio *folio, zap_flags_t zap_flags) 5159 { 5160 struct mm_struct *mm = vma->vm_mm; 5161 const bool folio_provided = !!folio; 5162 unsigned long address; 5163 pte_t *ptep; 5164 pte_t pte; 5165 spinlock_t *ptl; 5166 struct hstate *h = hstate_vma(vma); 5167 unsigned long sz = huge_page_size(h); 5168 bool adjust_reservation; 5169 unsigned long last_addr_mask; 5170 5171 WARN_ON(!is_vm_hugetlb_page(vma)); 5172 BUG_ON(start & ~huge_page_mask(h)); 5173 BUG_ON(end & ~huge_page_mask(h)); 5174 5175 /* 5176 * This is a hugetlb vma, all the pte entries should point 5177 * to huge page. 5178 */ 5179 tlb_change_page_size(tlb, sz); 5180 tlb_start_vma(tlb, vma); 5181 5182 last_addr_mask = hugetlb_mask_last_page(h); 5183 address = start; 5184 for (; address < end; address += sz) { 5185 ptep = hugetlb_walk(vma, address, sz); 5186 if (!ptep) { 5187 address |= last_addr_mask; 5188 continue; 5189 } 5190 5191 ptl = huge_pte_lock(h, mm, ptep); 5192 if (huge_pmd_unshare(tlb, vma, address, ptep)) { 5193 spin_unlock(ptl); 5194 address |= last_addr_mask; 5195 continue; 5196 } 5197 5198 pte = huge_ptep_get(mm, address, ptep); 5199 if (huge_pte_none(pte)) { 5200 spin_unlock(ptl); 5201 continue; 5202 } 5203 5204 /* 5205 * Migrating hugepage or HWPoisoned hugepage is already 5206 * unmapped and its refcount is dropped, so just clear pte here. 5207 */ 5208 if (unlikely(!pte_present(pte))) { 5209 /* 5210 * If the pte was wr-protected by uffd-wp in any of the 5211 * swap forms, meanwhile the caller does not want to 5212 * drop the uffd-wp bit in this zap, then replace the 5213 * pte with a marker. 5214 */ 5215 if (pte_swp_uffd_wp_any(pte) && 5216 !(zap_flags & ZAP_FLAG_DROP_MARKER)) 5217 set_huge_pte_at(mm, address, ptep, 5218 make_pte_marker(PTE_MARKER_UFFD_WP), 5219 sz); 5220 else 5221 huge_pte_clear(mm, address, ptep, sz); 5222 spin_unlock(ptl); 5223 continue; 5224 } 5225 5226 /* 5227 * If a folio is supplied, it is because a specific 5228 * folio is being unmapped, not a range. Ensure the folio we 5229 * are about to unmap is the actual folio of interest. 5230 */ 5231 if (folio_provided) { 5232 if (folio != page_folio(pte_page(pte))) { 5233 spin_unlock(ptl); 5234 continue; 5235 } 5236 /* 5237 * Mark the VMA as having unmapped its page so that 5238 * future faults in this VMA will fail rather than 5239 * looking like data was lost 5240 */ 5241 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); 5242 } else { 5243 folio = page_folio(pte_page(pte)); 5244 } 5245 5246 pte = huge_ptep_get_and_clear(mm, address, ptep, sz); 5247 tlb_remove_huge_tlb_entry(h, tlb, ptep, address); 5248 if (huge_pte_dirty(pte)) 5249 folio_mark_dirty(folio); 5250 /* Leave a uffd-wp pte marker if needed */ 5251 if (huge_pte_uffd_wp(pte) && 5252 !(zap_flags & ZAP_FLAG_DROP_MARKER)) 5253 set_huge_pte_at(mm, address, ptep, 5254 make_pte_marker(PTE_MARKER_UFFD_WP), 5255 sz); 5256 hugetlb_count_sub(pages_per_huge_page(h), mm); 5257 hugetlb_remove_rmap(folio); 5258 spin_unlock(ptl); 5259 5260 /* 5261 * Restore the reservation for anonymous page, otherwise the 5262 * backing page could be stolen by someone. 5263 * If there we are freeing a surplus, do not set the restore 5264 * reservation bit. 5265 */ 5266 adjust_reservation = false; 5267 5268 spin_lock_irq(&hugetlb_lock); 5269 if (!h->surplus_huge_pages && __vma_private_lock(vma) && 5270 folio_test_anon(folio)) { 5271 folio_set_hugetlb_restore_reserve(folio); 5272 /* Reservation to be adjusted after the spin lock */ 5273 adjust_reservation = true; 5274 } 5275 spin_unlock_irq(&hugetlb_lock); 5276 5277 /* 5278 * Adjust the reservation for the region that will have the 5279 * reserve restored. Keep in mind that vma_needs_reservation() changes 5280 * resv->adds_in_progress if it succeeds. If this is not done, 5281 * do_exit() will not see it, and will keep the reservation 5282 * forever. 5283 */ 5284 if (adjust_reservation) { 5285 int rc = vma_needs_reservation(h, vma, address); 5286 5287 if (rc < 0) 5288 /* Pressumably allocate_file_region_entries failed 5289 * to allocate a file_region struct. Clear 5290 * hugetlb_restore_reserve so that global reserve 5291 * count will not be incremented by free_huge_folio. 5292 * Act as if we consumed the reservation. 5293 */ 5294 folio_clear_hugetlb_restore_reserve(folio); 5295 else if (rc) 5296 vma_add_reservation(h, vma, address); 5297 } 5298 5299 tlb_remove_page_size(tlb, folio_page(folio, 0), 5300 folio_size(folio)); 5301 /* 5302 * If we were instructed to unmap a specific folio, we're done. 5303 */ 5304 if (folio_provided) 5305 break; 5306 } 5307 tlb_end_vma(tlb, vma); 5308 5309 huge_pmd_unshare_flush(tlb, vma); 5310 } 5311 5312 void __hugetlb_zap_begin(struct vm_area_struct *vma, 5313 unsigned long *start, unsigned long *end) 5314 { 5315 if (!vma->vm_file) /* hugetlbfs_file_mmap error */ 5316 return; 5317 5318 adjust_range_if_pmd_sharing_possible(vma, start, end); 5319 hugetlb_vma_lock_write(vma); 5320 if (vma->vm_file) 5321 i_mmap_lock_write(vma->vm_file->f_mapping); 5322 } 5323 5324 void __hugetlb_zap_end(struct vm_area_struct *vma, 5325 struct zap_details *details) 5326 { 5327 zap_flags_t zap_flags = details ? details->zap_flags : 0; 5328 5329 if (!vma->vm_file) /* hugetlbfs_file_mmap error */ 5330 return; 5331 5332 if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */ 5333 /* 5334 * Unlock and free the vma lock before releasing i_mmap_rwsem. 5335 * When the vma_lock is freed, this makes the vma ineligible 5336 * for pmd sharing. And, i_mmap_rwsem is required to set up 5337 * pmd sharing. This is important as page tables for this 5338 * unmapped range will be asynchrously deleted. If the page 5339 * tables are shared, there will be issues when accessed by 5340 * someone else. 5341 */ 5342 __hugetlb_vma_unlock_write_free(vma); 5343 } else { 5344 hugetlb_vma_unlock_write(vma); 5345 } 5346 5347 if (vma->vm_file) 5348 i_mmap_unlock_write(vma->vm_file->f_mapping); 5349 } 5350 5351 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, 5352 unsigned long end, struct folio *folio, 5353 zap_flags_t zap_flags) 5354 { 5355 struct mmu_notifier_range range; 5356 struct mmu_gather tlb; 5357 5358 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, 5359 start, end); 5360 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 5361 mmu_notifier_invalidate_range_start(&range); 5362 tlb_gather_mmu(&tlb, vma->vm_mm); 5363 5364 __unmap_hugepage_range(&tlb, vma, start, end, 5365 folio, zap_flags); 5366 5367 mmu_notifier_invalidate_range_end(&range); 5368 tlb_finish_mmu(&tlb); 5369 } 5370 5371 /* 5372 * This is called when the original mapper is failing to COW a MAP_PRIVATE 5373 * mapping it owns the reserve page for. The intention is to unmap the page 5374 * from other VMAs and let the children be SIGKILLed if they are faulting the 5375 * same region. 5376 */ 5377 static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, 5378 struct folio *folio, unsigned long address) 5379 { 5380 struct hstate *h = hstate_vma(vma); 5381 struct vm_area_struct *iter_vma; 5382 struct address_space *mapping; 5383 pgoff_t pgoff; 5384 5385 /* 5386 * vm_pgoff is in PAGE_SIZE units, hence the different calculation 5387 * from page cache lookup which is in HPAGE_SIZE units. 5388 */ 5389 address = address & huge_page_mask(h); 5390 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + 5391 vma->vm_pgoff; 5392 mapping = vma->vm_file->f_mapping; 5393 5394 /* 5395 * Take the mapping lock for the duration of the table walk. As 5396 * this mapping should be shared between all the VMAs, 5397 * __unmap_hugepage_range() is called as the lock is already held 5398 */ 5399 i_mmap_lock_write(mapping); 5400 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { 5401 /* Do not unmap the current VMA */ 5402 if (iter_vma == vma) 5403 continue; 5404 5405 /* 5406 * Shared VMAs have their own reserves and do not affect 5407 * MAP_PRIVATE accounting but it is possible that a shared 5408 * VMA is using the same page so check and skip such VMAs. 5409 */ 5410 if (iter_vma->vm_flags & VM_MAYSHARE) 5411 continue; 5412 5413 /* 5414 * Unmap the page from other VMAs without their own reserves. 5415 * They get marked to be SIGKILLed if they fault in these 5416 * areas. This is because a future no-page fault on this VMA 5417 * could insert a zeroed page instead of the data existing 5418 * from the time of fork. This would look like data corruption 5419 */ 5420 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) 5421 unmap_hugepage_range(iter_vma, address, 5422 address + huge_page_size(h), 5423 folio, 0); 5424 } 5425 i_mmap_unlock_write(mapping); 5426 } 5427 5428 /* 5429 * hugetlb_wp() should be called with page lock of the original hugepage held. 5430 * Called with hugetlb_fault_mutex_table held and pte_page locked so we 5431 * cannot race with other handlers or page migration. 5432 * Keep the pte_same checks anyway to make transition from the mutex easier. 5433 */ 5434 static vm_fault_t hugetlb_wp(struct vm_fault *vmf) 5435 { 5436 struct vm_area_struct *vma = vmf->vma; 5437 struct mm_struct *mm = vma->vm_mm; 5438 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; 5439 pte_t pte = huge_ptep_get(mm, vmf->address, vmf->pte); 5440 struct hstate *h = hstate_vma(vma); 5441 struct folio *old_folio; 5442 struct folio *new_folio; 5443 bool cow_from_owner = 0; 5444 vm_fault_t ret = 0; 5445 struct mmu_notifier_range range; 5446 5447 /* 5448 * Never handle CoW for uffd-wp protected pages. It should be only 5449 * handled when the uffd-wp protection is removed. 5450 * 5451 * Note that only the CoW optimization path (in hugetlb_no_page()) 5452 * can trigger this, because hugetlb_fault() will always resolve 5453 * uffd-wp bit first. 5454 */ 5455 if (!unshare && huge_pte_uffd_wp(pte)) 5456 return 0; 5457 5458 /* Let's take out MAP_SHARED mappings first. */ 5459 if (vma->vm_flags & VM_MAYSHARE) { 5460 set_huge_ptep_writable(vma, vmf->address, vmf->pte); 5461 return 0; 5462 } 5463 5464 old_folio = page_folio(pte_page(pte)); 5465 5466 delayacct_wpcopy_start(); 5467 5468 retry_avoidcopy: 5469 /* 5470 * If no-one else is actually using this page, we're the exclusive 5471 * owner and can reuse this page. 5472 * 5473 * Note that we don't rely on the (safer) folio refcount here, because 5474 * copying the hugetlb folio when there are unexpected (temporary) 5475 * folio references could harm simple fork()+exit() users when 5476 * we run out of free hugetlb folios: we would have to kill processes 5477 * in scenarios that used to work. As a side effect, there can still 5478 * be leaks between processes, for example, with FOLL_GET users. 5479 */ 5480 if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) { 5481 if (!PageAnonExclusive(&old_folio->page)) { 5482 folio_move_anon_rmap(old_folio, vma); 5483 SetPageAnonExclusive(&old_folio->page); 5484 } 5485 if (likely(!unshare)) 5486 set_huge_ptep_maybe_writable(vma, vmf->address, 5487 vmf->pte); 5488 5489 delayacct_wpcopy_end(); 5490 return 0; 5491 } 5492 VM_BUG_ON_PAGE(folio_test_anon(old_folio) && 5493 PageAnonExclusive(&old_folio->page), &old_folio->page); 5494 5495 /* 5496 * If the process that created a MAP_PRIVATE mapping is about to perform 5497 * a COW due to a shared page count, attempt to satisfy the allocation 5498 * without using the existing reserves. 5499 * In order to determine where this is a COW on a MAP_PRIVATE mapping it 5500 * is enough to check whether the old_folio is anonymous. This means that 5501 * the reserve for this address was consumed. If reserves were used, a 5502 * partial faulted mapping at the fime of fork() could consume its reserves 5503 * on COW instead of the full address range. 5504 */ 5505 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && 5506 folio_test_anon(old_folio)) 5507 cow_from_owner = true; 5508 5509 folio_get(old_folio); 5510 5511 /* 5512 * Drop page table lock as buddy allocator may be called. It will 5513 * be acquired again before returning to the caller, as expected. 5514 */ 5515 spin_unlock(vmf->ptl); 5516 new_folio = alloc_hugetlb_folio(vma, vmf->address, cow_from_owner); 5517 5518 if (IS_ERR(new_folio)) { 5519 /* 5520 * If a process owning a MAP_PRIVATE mapping fails to COW, 5521 * it is due to references held by a child and an insufficient 5522 * huge page pool. To guarantee the original mappers 5523 * reliability, unmap the page from child processes. The child 5524 * may get SIGKILLed if it later faults. 5525 */ 5526 if (cow_from_owner) { 5527 struct address_space *mapping = vma->vm_file->f_mapping; 5528 pgoff_t idx; 5529 u32 hash; 5530 5531 folio_put(old_folio); 5532 /* 5533 * Drop hugetlb_fault_mutex and vma_lock before 5534 * unmapping. unmapping needs to hold vma_lock 5535 * in write mode. Dropping vma_lock in read mode 5536 * here is OK as COW mappings do not interact with 5537 * PMD sharing. 5538 * 5539 * Reacquire both after unmap operation. 5540 */ 5541 idx = vma_hugecache_offset(h, vma, vmf->address); 5542 hash = hugetlb_fault_mutex_hash(mapping, idx); 5543 hugetlb_vma_unlock_read(vma); 5544 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5545 5546 unmap_ref_private(mm, vma, old_folio, vmf->address); 5547 5548 mutex_lock(&hugetlb_fault_mutex_table[hash]); 5549 hugetlb_vma_lock_read(vma); 5550 spin_lock(vmf->ptl); 5551 vmf->pte = hugetlb_walk(vma, vmf->address, 5552 huge_page_size(h)); 5553 if (likely(vmf->pte && 5554 pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) 5555 goto retry_avoidcopy; 5556 /* 5557 * race occurs while re-acquiring page table 5558 * lock, and our job is done. 5559 */ 5560 delayacct_wpcopy_end(); 5561 return 0; 5562 } 5563 5564 ret = vmf_error(PTR_ERR(new_folio)); 5565 goto out_release_old; 5566 } 5567 5568 /* 5569 * When the original hugepage is shared one, it does not have 5570 * anon_vma prepared. 5571 */ 5572 ret = __vmf_anon_prepare(vmf); 5573 if (unlikely(ret)) 5574 goto out_release_all; 5575 5576 if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) { 5577 ret = VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); 5578 goto out_release_all; 5579 } 5580 __folio_mark_uptodate(new_folio); 5581 5582 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address, 5583 vmf->address + huge_page_size(h)); 5584 mmu_notifier_invalidate_range_start(&range); 5585 5586 /* 5587 * Retake the page table lock to check for racing updates 5588 * before the page tables are altered 5589 */ 5590 spin_lock(vmf->ptl); 5591 vmf->pte = hugetlb_walk(vma, vmf->address, huge_page_size(h)); 5592 if (likely(vmf->pte && pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) { 5593 pte_t newpte = make_huge_pte(vma, new_folio, !unshare); 5594 5595 /* Break COW or unshare */ 5596 huge_ptep_clear_flush(vma, vmf->address, vmf->pte); 5597 hugetlb_remove_rmap(old_folio); 5598 hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address); 5599 if (huge_pte_uffd_wp(pte)) 5600 newpte = huge_pte_mkuffd_wp(newpte); 5601 set_huge_pte_at(mm, vmf->address, vmf->pte, newpte, 5602 huge_page_size(h)); 5603 folio_set_hugetlb_migratable(new_folio); 5604 /* Make the old page be freed below */ 5605 new_folio = old_folio; 5606 } 5607 spin_unlock(vmf->ptl); 5608 mmu_notifier_invalidate_range_end(&range); 5609 out_release_all: 5610 /* 5611 * No restore in case of successful pagetable update (Break COW or 5612 * unshare) 5613 */ 5614 if (new_folio != old_folio) 5615 restore_reserve_on_error(h, vma, vmf->address, new_folio); 5616 folio_put(new_folio); 5617 out_release_old: 5618 folio_put(old_folio); 5619 5620 spin_lock(vmf->ptl); /* Caller expects lock to be held */ 5621 5622 delayacct_wpcopy_end(); 5623 return ret; 5624 } 5625 5626 /* 5627 * Return whether there is a pagecache page to back given address within VMA. 5628 */ 5629 bool hugetlbfs_pagecache_present(struct hstate *h, 5630 struct vm_area_struct *vma, unsigned long address) 5631 { 5632 struct address_space *mapping = vma->vm_file->f_mapping; 5633 pgoff_t idx = linear_page_index(vma, address); 5634 struct folio *folio; 5635 5636 folio = filemap_get_folio(mapping, idx); 5637 if (IS_ERR(folio)) 5638 return false; 5639 folio_put(folio); 5640 return true; 5641 } 5642 5643 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping, 5644 pgoff_t idx) 5645 { 5646 struct inode *inode = mapping->host; 5647 struct hstate *h = hstate_inode(inode); 5648 int err; 5649 5650 idx <<= huge_page_order(h); 5651 __folio_set_locked(folio); 5652 err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL); 5653 5654 if (unlikely(err)) { 5655 __folio_clear_locked(folio); 5656 return err; 5657 } 5658 folio_clear_hugetlb_restore_reserve(folio); 5659 5660 /* 5661 * mark folio dirty so that it will not be removed from cache/file 5662 * by non-hugetlbfs specific code paths. 5663 */ 5664 folio_mark_dirty(folio); 5665 5666 spin_lock(&inode->i_lock); 5667 inode->i_blocks += blocks_per_huge_page(h); 5668 spin_unlock(&inode->i_lock); 5669 return 0; 5670 } 5671 5672 static inline vm_fault_t hugetlb_handle_userfault(struct vm_fault *vmf, 5673 struct address_space *mapping, 5674 unsigned long reason) 5675 { 5676 u32 hash; 5677 5678 /* 5679 * vma_lock and hugetlb_fault_mutex must be dropped before handling 5680 * userfault. Also mmap_lock could be dropped due to handling 5681 * userfault, any vma operation should be careful from here. 5682 */ 5683 hugetlb_vma_unlock_read(vmf->vma); 5684 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); 5685 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5686 return handle_userfault(vmf, reason); 5687 } 5688 5689 /* 5690 * Recheck pte with pgtable lock. Returns true if pte didn't change, or 5691 * false if pte changed or is changing. 5692 */ 5693 static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm, unsigned long addr, 5694 pte_t *ptep, pte_t old_pte) 5695 { 5696 spinlock_t *ptl; 5697 bool same; 5698 5699 ptl = huge_pte_lock(h, mm, ptep); 5700 same = pte_same(huge_ptep_get(mm, addr, ptep), old_pte); 5701 spin_unlock(ptl); 5702 5703 return same; 5704 } 5705 5706 static vm_fault_t hugetlb_no_page(struct address_space *mapping, 5707 struct vm_fault *vmf) 5708 { 5709 u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); 5710 bool new_folio, new_anon_folio = false; 5711 struct vm_area_struct *vma = vmf->vma; 5712 struct mm_struct *mm = vma->vm_mm; 5713 struct hstate *h = hstate_vma(vma); 5714 vm_fault_t ret = VM_FAULT_SIGBUS; 5715 bool folio_locked = true; 5716 struct folio *folio; 5717 unsigned long size; 5718 pte_t new_pte; 5719 5720 /* 5721 * Currently, we are forced to kill the process in the event the 5722 * original mapper has unmapped pages from the child due to a failed 5723 * COW/unsharing. Warn that such a situation has occurred as it may not 5724 * be obvious. 5725 */ 5726 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { 5727 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n", 5728 current->pid); 5729 goto out; 5730 } 5731 5732 /* 5733 * Use page lock to guard against racing truncation 5734 * before we get page_table_lock. 5735 */ 5736 new_folio = false; 5737 folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff); 5738 if (IS_ERR(folio)) { 5739 size = i_size_read(mapping->host) >> huge_page_shift(h); 5740 if (vmf->pgoff >= size) 5741 goto out; 5742 /* Check for page in userfault range */ 5743 if (userfaultfd_missing(vma)) { 5744 /* 5745 * Since hugetlb_no_page() was examining pte 5746 * without pgtable lock, we need to re-test under 5747 * lock because the pte may not be stable and could 5748 * have changed from under us. Try to detect 5749 * either changed or during-changing ptes and retry 5750 * properly when needed. 5751 * 5752 * Note that userfaultfd is actually fine with 5753 * false positives (e.g. caused by pte changed), 5754 * but not wrong logical events (e.g. caused by 5755 * reading a pte during changing). The latter can 5756 * confuse the userspace, so the strictness is very 5757 * much preferred. E.g., MISSING event should 5758 * never happen on the page after UFFDIO_COPY has 5759 * correctly installed the page and returned. 5760 */ 5761 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) { 5762 ret = 0; 5763 goto out; 5764 } 5765 5766 return hugetlb_handle_userfault(vmf, mapping, 5767 VM_UFFD_MISSING); 5768 } 5769 5770 if (!(vma->vm_flags & VM_MAYSHARE)) { 5771 ret = __vmf_anon_prepare(vmf); 5772 if (unlikely(ret)) 5773 goto out; 5774 } 5775 5776 folio = alloc_hugetlb_folio(vma, vmf->address, false); 5777 if (IS_ERR(folio)) { 5778 /* 5779 * Returning error will result in faulting task being 5780 * sent SIGBUS. The hugetlb fault mutex prevents two 5781 * tasks from racing to fault in the same page which 5782 * could result in false unable to allocate errors. 5783 * Page migration does not take the fault mutex, but 5784 * does a clear then write of pte's under page table 5785 * lock. Page fault code could race with migration, 5786 * notice the clear pte and try to allocate a page 5787 * here. Before returning error, get ptl and make 5788 * sure there really is no pte entry. 5789 */ 5790 if (hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) 5791 ret = vmf_error(PTR_ERR(folio)); 5792 else 5793 ret = 0; 5794 goto out; 5795 } 5796 folio_zero_user(folio, vmf->real_address); 5797 __folio_mark_uptodate(folio); 5798 new_folio = true; 5799 5800 if (vma->vm_flags & VM_MAYSHARE) { 5801 int err = hugetlb_add_to_page_cache(folio, mapping, 5802 vmf->pgoff); 5803 if (err) { 5804 /* 5805 * err can't be -EEXIST which implies someone 5806 * else consumed the reservation since hugetlb 5807 * fault mutex is held when add a hugetlb page 5808 * to the page cache. So it's safe to call 5809 * restore_reserve_on_error() here. 5810 */ 5811 restore_reserve_on_error(h, vma, vmf->address, 5812 folio); 5813 folio_put(folio); 5814 ret = VM_FAULT_SIGBUS; 5815 goto out; 5816 } 5817 } else { 5818 new_anon_folio = true; 5819 folio_lock(folio); 5820 } 5821 } else { 5822 /* 5823 * If memory error occurs between mmap() and fault, some process 5824 * don't have hwpoisoned swap entry for errored virtual address. 5825 * So we need to block hugepage fault by PG_hwpoison bit check. 5826 */ 5827 if (unlikely(folio_test_hwpoison(folio))) { 5828 ret = VM_FAULT_HWPOISON_LARGE | 5829 VM_FAULT_SET_HINDEX(hstate_index(h)); 5830 goto backout_unlocked; 5831 } 5832 5833 /* Check for page in userfault range. */ 5834 if (userfaultfd_minor(vma)) { 5835 folio_unlock(folio); 5836 folio_put(folio); 5837 /* See comment in userfaultfd_missing() block above */ 5838 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) { 5839 ret = 0; 5840 goto out; 5841 } 5842 return hugetlb_handle_userfault(vmf, mapping, 5843 VM_UFFD_MINOR); 5844 } 5845 } 5846 5847 /* 5848 * If we are going to COW a private mapping later, we examine the 5849 * pending reservations for this page now. This will ensure that 5850 * any allocations necessary to record that reservation occur outside 5851 * the spinlock. 5852 */ 5853 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 5854 if (vma_needs_reservation(h, vma, vmf->address) < 0) { 5855 ret = VM_FAULT_OOM; 5856 goto backout_unlocked; 5857 } 5858 /* Just decrements count, does not deallocate */ 5859 vma_end_reservation(h, vma, vmf->address); 5860 } 5861 5862 vmf->ptl = huge_pte_lock(h, mm, vmf->pte); 5863 ret = 0; 5864 /* If pte changed from under us, retry */ 5865 if (!pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), vmf->orig_pte)) 5866 goto backout; 5867 5868 if (new_anon_folio) 5869 hugetlb_add_new_anon_rmap(folio, vma, vmf->address); 5870 else 5871 hugetlb_add_file_rmap(folio); 5872 new_pte = make_huge_pte(vma, folio, vma->vm_flags & VM_SHARED); 5873 /* 5874 * If this pte was previously wr-protected, keep it wr-protected even 5875 * if populated. 5876 */ 5877 if (unlikely(pte_is_uffd_wp_marker(vmf->orig_pte))) 5878 new_pte = huge_pte_mkuffd_wp(new_pte); 5879 set_huge_pte_at(mm, vmf->address, vmf->pte, new_pte, huge_page_size(h)); 5880 5881 hugetlb_count_add(pages_per_huge_page(h), mm); 5882 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { 5883 /* 5884 * No need to keep file folios locked. See comment in 5885 * hugetlb_fault(). 5886 */ 5887 if (!new_anon_folio) { 5888 folio_locked = false; 5889 folio_unlock(folio); 5890 } 5891 /* Optimization, do the COW without a second fault */ 5892 ret = hugetlb_wp(vmf); 5893 } 5894 5895 spin_unlock(vmf->ptl); 5896 5897 /* 5898 * Only set hugetlb_migratable in newly allocated pages. Existing pages 5899 * found in the pagecache may not have hugetlb_migratable if they have 5900 * been isolated for migration. 5901 */ 5902 if (new_folio) 5903 folio_set_hugetlb_migratable(folio); 5904 5905 if (folio_locked) 5906 folio_unlock(folio); 5907 out: 5908 hugetlb_vma_unlock_read(vma); 5909 5910 /* 5911 * We must check to release the per-VMA lock. __vmf_anon_prepare() is 5912 * the only way ret can be set to VM_FAULT_RETRY. 5913 */ 5914 if (unlikely(ret & VM_FAULT_RETRY)) 5915 vma_end_read(vma); 5916 5917 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5918 return ret; 5919 5920 backout: 5921 spin_unlock(vmf->ptl); 5922 backout_unlocked: 5923 /* We only need to restore reservations for private mappings */ 5924 if (new_anon_folio) 5925 restore_reserve_on_error(h, vma, vmf->address, folio); 5926 5927 folio_unlock(folio); 5928 folio_put(folio); 5929 goto out; 5930 } 5931 5932 #ifdef CONFIG_SMP 5933 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 5934 { 5935 unsigned long key[2]; 5936 u32 hash; 5937 5938 key[0] = (unsigned long) mapping; 5939 key[1] = idx; 5940 5941 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0); 5942 5943 return hash & (num_fault_mutexes - 1); 5944 } 5945 #else 5946 /* 5947 * For uniprocessor systems we always use a single mutex, so just 5948 * return 0 and avoid the hashing overhead. 5949 */ 5950 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx) 5951 { 5952 return 0; 5953 } 5954 #endif 5955 5956 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 5957 unsigned long address, unsigned int flags) 5958 { 5959 vm_fault_t ret; 5960 u32 hash; 5961 struct folio *folio = NULL; 5962 struct hstate *h = hstate_vma(vma); 5963 struct address_space *mapping; 5964 bool need_wait_lock = false; 5965 struct vm_fault vmf = { 5966 .vma = vma, 5967 .address = address & huge_page_mask(h), 5968 .real_address = address, 5969 .flags = flags, 5970 .pgoff = vma_hugecache_offset(h, vma, 5971 address & huge_page_mask(h)), 5972 /* TODO: Track hugetlb faults using vm_fault */ 5973 5974 /* 5975 * Some fields may not be initialized, be careful as it may 5976 * be hard to debug if called functions make assumptions 5977 */ 5978 }; 5979 5980 /* 5981 * Serialize hugepage allocation and instantiation, so that we don't 5982 * get spurious allocation failures if two CPUs race to instantiate 5983 * the same page in the page cache. 5984 */ 5985 mapping = vma->vm_file->f_mapping; 5986 hash = hugetlb_fault_mutex_hash(mapping, vmf.pgoff); 5987 mutex_lock(&hugetlb_fault_mutex_table[hash]); 5988 5989 /* 5990 * Acquire vma lock before calling huge_pte_alloc and hold 5991 * until finished with vmf.pte. This prevents huge_pmd_unshare from 5992 * being called elsewhere and making the vmf.pte no longer valid. 5993 */ 5994 hugetlb_vma_lock_read(vma); 5995 vmf.pte = huge_pte_alloc(mm, vma, vmf.address, huge_page_size(h)); 5996 if (!vmf.pte) { 5997 hugetlb_vma_unlock_read(vma); 5998 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 5999 return VM_FAULT_OOM; 6000 } 6001 6002 vmf.orig_pte = huge_ptep_get(mm, vmf.address, vmf.pte); 6003 if (huge_pte_none(vmf.orig_pte)) 6004 /* 6005 * hugetlb_no_page will drop vma lock and hugetlb fault 6006 * mutex internally, which make us return immediately. 6007 */ 6008 return hugetlb_no_page(mapping, &vmf); 6009 6010 if (pte_is_marker(vmf.orig_pte)) { 6011 const pte_marker marker = 6012 softleaf_to_marker(softleaf_from_pte(vmf.orig_pte)); 6013 6014 if (marker & PTE_MARKER_POISONED) { 6015 ret = VM_FAULT_HWPOISON_LARGE | 6016 VM_FAULT_SET_HINDEX(hstate_index(h)); 6017 goto out_mutex; 6018 } else if (WARN_ON_ONCE(marker & PTE_MARKER_GUARD)) { 6019 /* This isn't supported in hugetlb. */ 6020 ret = VM_FAULT_SIGSEGV; 6021 goto out_mutex; 6022 } 6023 6024 return hugetlb_no_page(mapping, &vmf); 6025 } 6026 6027 ret = 0; 6028 6029 /* Not present, either a migration or a hwpoisoned entry */ 6030 if (!pte_present(vmf.orig_pte) && !huge_pte_none(vmf.orig_pte)) { 6031 const softleaf_t softleaf = softleaf_from_pte(vmf.orig_pte); 6032 6033 if (softleaf_is_migration(softleaf)) { 6034 /* 6035 * Release the hugetlb fault lock now, but retain 6036 * the vma lock, because it is needed to guard the 6037 * huge_pte_lockptr() later in 6038 * migration_entry_wait_huge(). The vma lock will 6039 * be released there. 6040 */ 6041 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6042 migration_entry_wait_huge(vma, vmf.address, vmf.pte); 6043 return 0; 6044 } 6045 if (softleaf_is_hwpoison(softleaf)) { 6046 ret = VM_FAULT_HWPOISON_LARGE | 6047 VM_FAULT_SET_HINDEX(hstate_index(h)); 6048 } 6049 6050 goto out_mutex; 6051 } 6052 6053 /* 6054 * If we are going to COW/unshare the mapping later, we examine the 6055 * pending reservations for this page now. This will ensure that any 6056 * allocations necessary to record that reservation occur outside the 6057 * spinlock. 6058 */ 6059 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) && 6060 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) { 6061 if (vma_needs_reservation(h, vma, vmf.address) < 0) { 6062 ret = VM_FAULT_OOM; 6063 goto out_mutex; 6064 } 6065 /* Just decrements count, does not deallocate */ 6066 vma_end_reservation(h, vma, vmf.address); 6067 } 6068 6069 vmf.ptl = huge_pte_lock(h, mm, vmf.pte); 6070 6071 /* Check for a racing update before calling hugetlb_wp() */ 6072 if (unlikely(!pte_same(vmf.orig_pte, huge_ptep_get(mm, vmf.address, vmf.pte)))) 6073 goto out_ptl; 6074 6075 /* Handle userfault-wp first, before trying to lock more pages */ 6076 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(mm, vmf.address, vmf.pte)) && 6077 (flags & FAULT_FLAG_WRITE) && !huge_pte_write(vmf.orig_pte)) { 6078 if (!userfaultfd_wp_async(vma)) { 6079 spin_unlock(vmf.ptl); 6080 hugetlb_vma_unlock_read(vma); 6081 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6082 return handle_userfault(&vmf, VM_UFFD_WP); 6083 } 6084 6085 vmf.orig_pte = huge_pte_clear_uffd_wp(vmf.orig_pte); 6086 set_huge_pte_at(mm, vmf.address, vmf.pte, vmf.orig_pte, 6087 huge_page_size(hstate_vma(vma))); 6088 /* Fallthrough to CoW */ 6089 } 6090 6091 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) { 6092 if (!huge_pte_write(vmf.orig_pte)) { 6093 /* 6094 * Anonymous folios need to be lock since hugetlb_wp() 6095 * checks whether we can re-use the folio exclusively 6096 * for us in case we are the only user of it. 6097 */ 6098 folio = page_folio(pte_page(vmf.orig_pte)); 6099 if (folio_test_anon(folio) && !folio_trylock(folio)) { 6100 need_wait_lock = true; 6101 goto out_ptl; 6102 } 6103 folio_get(folio); 6104 ret = hugetlb_wp(&vmf); 6105 if (folio_test_anon(folio)) 6106 folio_unlock(folio); 6107 folio_put(folio); 6108 goto out_ptl; 6109 } else if (likely(flags & FAULT_FLAG_WRITE)) { 6110 vmf.orig_pte = huge_pte_mkdirty(vmf.orig_pte); 6111 } 6112 } 6113 vmf.orig_pte = pte_mkyoung(vmf.orig_pte); 6114 if (huge_ptep_set_access_flags(vma, vmf.address, vmf.pte, vmf.orig_pte, 6115 flags & FAULT_FLAG_WRITE)) 6116 update_mmu_cache(vma, vmf.address, vmf.pte); 6117 out_ptl: 6118 spin_unlock(vmf.ptl); 6119 out_mutex: 6120 hugetlb_vma_unlock_read(vma); 6121 6122 /* 6123 * We must check to release the per-VMA lock. __vmf_anon_prepare() in 6124 * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY. 6125 */ 6126 if (unlikely(ret & VM_FAULT_RETRY)) 6127 vma_end_read(vma); 6128 6129 mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6130 /* 6131 * hugetlb_wp drops all the locks, but the folio lock, before trying to 6132 * unmap the folio from other processes. During that window, if another 6133 * process mapping that folio faults in, it will take the mutex and then 6134 * it will wait on folio_lock, causing an ABBA deadlock. 6135 * Use trylock instead and bail out if we fail. 6136 * 6137 * Ideally, we should hold a refcount on the folio we wait for, but we do 6138 * not want to use the folio after it becomes unlocked, but rather just 6139 * wait for it to become unlocked, so hopefully next fault successes on 6140 * the trylock. 6141 */ 6142 if (need_wait_lock) 6143 folio_wait_locked(folio); 6144 return ret; 6145 } 6146 6147 #ifdef CONFIG_USERFAULTFD 6148 /* 6149 * Can probably be eliminated, but still used by hugetlb_mfill_atomic_pte(). 6150 */ 6151 static struct folio *alloc_hugetlb_folio_vma(struct hstate *h, 6152 struct vm_area_struct *vma, unsigned long address) 6153 { 6154 struct mempolicy *mpol; 6155 nodemask_t *nodemask; 6156 struct folio *folio; 6157 gfp_t gfp_mask; 6158 int node; 6159 6160 gfp_mask = htlb_alloc_mask(h); 6161 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask); 6162 /* 6163 * This is used to allocate a temporary hugetlb to hold the copied 6164 * content, which will then be copied again to the final hugetlb 6165 * consuming a reservation. Set the alloc_fallback to false to indicate 6166 * that breaking the per-node hugetlb pool is not allowed in this case. 6167 */ 6168 folio = alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask, false); 6169 mpol_cond_put(mpol); 6170 6171 return folio; 6172 } 6173 6174 /* 6175 * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte 6176 * with modifications for hugetlb pages. 6177 */ 6178 int hugetlb_mfill_atomic_pte(pte_t *dst_pte, 6179 struct vm_area_struct *dst_vma, 6180 unsigned long dst_addr, 6181 unsigned long src_addr, 6182 uffd_flags_t flags, 6183 struct folio **foliop) 6184 { 6185 struct mm_struct *dst_mm = dst_vma->vm_mm; 6186 bool is_continue = uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE); 6187 bool wp_enabled = (flags & MFILL_ATOMIC_WP); 6188 struct hstate *h = hstate_vma(dst_vma); 6189 struct address_space *mapping = dst_vma->vm_file->f_mapping; 6190 pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr); 6191 unsigned long size = huge_page_size(h); 6192 int vm_shared = dst_vma->vm_flags & VM_SHARED; 6193 pte_t _dst_pte; 6194 spinlock_t *ptl; 6195 int ret = -ENOMEM; 6196 struct folio *folio; 6197 bool folio_in_pagecache = false; 6198 pte_t dst_ptep; 6199 6200 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) { 6201 ptl = huge_pte_lock(h, dst_mm, dst_pte); 6202 6203 /* Don't overwrite any existing PTEs (even markers) */ 6204 if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) { 6205 spin_unlock(ptl); 6206 return -EEXIST; 6207 } 6208 6209 _dst_pte = make_pte_marker(PTE_MARKER_POISONED); 6210 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size); 6211 6212 /* No need to invalidate - it was non-present before */ 6213 update_mmu_cache(dst_vma, dst_addr, dst_pte); 6214 6215 spin_unlock(ptl); 6216 return 0; 6217 } 6218 6219 if (is_continue) { 6220 ret = -EFAULT; 6221 folio = filemap_lock_hugetlb_folio(h, mapping, idx); 6222 if (IS_ERR(folio)) 6223 goto out; 6224 folio_in_pagecache = true; 6225 } else if (!*foliop) { 6226 /* If a folio already exists, then it's UFFDIO_COPY for 6227 * a non-missing case. Return -EEXIST. 6228 */ 6229 if (vm_shared && 6230 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 6231 ret = -EEXIST; 6232 goto out; 6233 } 6234 6235 folio = alloc_hugetlb_folio(dst_vma, dst_addr, false); 6236 if (IS_ERR(folio)) { 6237 pte_t *actual_pte = hugetlb_walk(dst_vma, dst_addr, PMD_SIZE); 6238 if (actual_pte) { 6239 ret = -EEXIST; 6240 goto out; 6241 } 6242 ret = -ENOMEM; 6243 goto out; 6244 } 6245 6246 ret = copy_folio_from_user(folio, (const void __user *) src_addr, 6247 false); 6248 6249 /* fallback to copy_from_user outside mmap_lock */ 6250 if (unlikely(ret)) { 6251 ret = -ENOENT; 6252 /* Free the allocated folio which may have 6253 * consumed a reservation. 6254 */ 6255 restore_reserve_on_error(h, dst_vma, dst_addr, folio); 6256 folio_put(folio); 6257 6258 /* Allocate a temporary folio to hold the copied 6259 * contents. 6260 */ 6261 folio = alloc_hugetlb_folio_vma(h, dst_vma, dst_addr); 6262 if (!folio) { 6263 ret = -ENOMEM; 6264 goto out; 6265 } 6266 *foliop = folio; 6267 /* Set the outparam foliop and return to the caller to 6268 * copy the contents outside the lock. Don't free the 6269 * folio. 6270 */ 6271 goto out; 6272 } 6273 } else { 6274 if (vm_shared && 6275 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) { 6276 folio_put(*foliop); 6277 ret = -EEXIST; 6278 *foliop = NULL; 6279 goto out; 6280 } 6281 6282 folio = alloc_hugetlb_folio(dst_vma, dst_addr, false); 6283 if (IS_ERR(folio)) { 6284 folio_put(*foliop); 6285 ret = -ENOMEM; 6286 *foliop = NULL; 6287 goto out; 6288 } 6289 ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma); 6290 folio_put(*foliop); 6291 *foliop = NULL; 6292 if (ret) { 6293 folio_put(folio); 6294 goto out; 6295 } 6296 } 6297 6298 /* 6299 * If we just allocated a new page, we need a memory barrier to ensure 6300 * that preceding stores to the page become visible before the 6301 * set_pte_at() write. The memory barrier inside __folio_mark_uptodate 6302 * is what we need. 6303 * 6304 * In the case where we have not allocated a new page (is_continue), 6305 * the page must already be uptodate. UFFDIO_CONTINUE already includes 6306 * an earlier smp_wmb() to ensure that prior stores will be visible 6307 * before the set_pte_at() write. 6308 */ 6309 if (!is_continue) 6310 __folio_mark_uptodate(folio); 6311 else 6312 WARN_ON_ONCE(!folio_test_uptodate(folio)); 6313 6314 /* Add shared, newly allocated pages to the page cache. */ 6315 if (vm_shared && !is_continue) { 6316 ret = -EFAULT; 6317 if (idx >= (i_size_read(mapping->host) >> huge_page_shift(h))) 6318 goto out_release_nounlock; 6319 6320 /* 6321 * Serialization between remove_inode_hugepages() and 6322 * hugetlb_add_to_page_cache() below happens through the 6323 * hugetlb_fault_mutex_table that here must be hold by 6324 * the caller. 6325 */ 6326 ret = hugetlb_add_to_page_cache(folio, mapping, idx); 6327 if (ret) 6328 goto out_release_nounlock; 6329 folio_in_pagecache = true; 6330 } 6331 6332 ptl = huge_pte_lock(h, dst_mm, dst_pte); 6333 6334 ret = -EIO; 6335 if (folio_test_hwpoison(folio)) 6336 goto out_release_unlock; 6337 6338 ret = -EEXIST; 6339 6340 dst_ptep = huge_ptep_get(dst_mm, dst_addr, dst_pte); 6341 /* 6342 * See comment about UFFD marker overwriting in 6343 * mfill_atomic_install_pte(). 6344 */ 6345 if (!huge_pte_none(dst_ptep) && !pte_is_uffd_marker(dst_ptep)) 6346 goto out_release_unlock; 6347 6348 if (folio_in_pagecache) 6349 hugetlb_add_file_rmap(folio); 6350 else 6351 hugetlb_add_new_anon_rmap(folio, dst_vma, dst_addr); 6352 6353 /* 6354 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY 6355 * with wp flag set, don't set pte write bit. 6356 */ 6357 _dst_pte = make_huge_pte(dst_vma, folio, 6358 !wp_enabled && !(is_continue && !vm_shared)); 6359 /* 6360 * Always mark UFFDIO_COPY page dirty; note that this may not be 6361 * extremely important for hugetlbfs for now since swapping is not 6362 * supported, but we should still be clear in that this page cannot be 6363 * thrown away at will, even if write bit not set. 6364 */ 6365 _dst_pte = huge_pte_mkdirty(_dst_pte); 6366 _dst_pte = pte_mkyoung(_dst_pte); 6367 6368 if (wp_enabled) 6369 _dst_pte = huge_pte_mkuffd_wp(_dst_pte); 6370 6371 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size); 6372 6373 hugetlb_count_add(pages_per_huge_page(h), dst_mm); 6374 6375 /* No need to invalidate - it was non-present before */ 6376 update_mmu_cache(dst_vma, dst_addr, dst_pte); 6377 6378 spin_unlock(ptl); 6379 if (!is_continue) 6380 folio_set_hugetlb_migratable(folio); 6381 if (vm_shared || is_continue) 6382 folio_unlock(folio); 6383 ret = 0; 6384 out: 6385 return ret; 6386 out_release_unlock: 6387 spin_unlock(ptl); 6388 if (vm_shared || is_continue) 6389 folio_unlock(folio); 6390 out_release_nounlock: 6391 if (!folio_in_pagecache) 6392 restore_reserve_on_error(h, dst_vma, dst_addr, folio); 6393 folio_put(folio); 6394 goto out; 6395 } 6396 #endif /* CONFIG_USERFAULTFD */ 6397 6398 long hugetlb_change_protection(struct vm_area_struct *vma, 6399 unsigned long address, unsigned long end, 6400 pgprot_t newprot, unsigned long cp_flags) 6401 { 6402 struct mm_struct *mm = vma->vm_mm; 6403 unsigned long start = address; 6404 pte_t *ptep; 6405 pte_t pte; 6406 struct hstate *h = hstate_vma(vma); 6407 long pages = 0, psize = huge_page_size(h); 6408 struct mmu_notifier_range range; 6409 unsigned long last_addr_mask; 6410 bool uffd_wp = cp_flags & MM_CP_UFFD_WP; 6411 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; 6412 struct mmu_gather tlb; 6413 6414 /* 6415 * In the case of shared PMDs, the area to flush could be beyond 6416 * start/end. Set range.start/range.end to cover the maximum possible 6417 * range if PMD sharing is possible. 6418 */ 6419 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 6420 0, mm, start, end); 6421 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); 6422 6423 BUG_ON(address >= end); 6424 flush_cache_range(vma, range.start, range.end); 6425 tlb_gather_mmu_vma(&tlb, vma); 6426 6427 mmu_notifier_invalidate_range_start(&range); 6428 hugetlb_vma_lock_write(vma); 6429 i_mmap_lock_write(vma->vm_file->f_mapping); 6430 last_addr_mask = hugetlb_mask_last_page(h); 6431 for (; address < end; address += psize) { 6432 softleaf_t entry; 6433 spinlock_t *ptl; 6434 6435 ptep = hugetlb_walk(vma, address, psize); 6436 if (!ptep) { 6437 if (!uffd_wp) { 6438 address |= last_addr_mask; 6439 continue; 6440 } 6441 /* 6442 * Userfaultfd wr-protect requires pgtable 6443 * pre-allocations to install pte markers. 6444 */ 6445 ptep = huge_pte_alloc(mm, vma, address, psize); 6446 if (!ptep) { 6447 pages = -ENOMEM; 6448 break; 6449 } 6450 } 6451 ptl = huge_pte_lock(h, mm, ptep); 6452 if (huge_pmd_unshare(&tlb, vma, address, ptep)) { 6453 /* 6454 * When uffd-wp is enabled on the vma, unshare 6455 * shouldn't happen at all. Warn about it if it 6456 * happened due to some reason. 6457 */ 6458 WARN_ON_ONCE(uffd_wp || uffd_wp_resolve); 6459 pages++; 6460 spin_unlock(ptl); 6461 address |= last_addr_mask; 6462 continue; 6463 } 6464 pte = huge_ptep_get(mm, address, ptep); 6465 if (huge_pte_none(pte)) { 6466 if (unlikely(uffd_wp)) 6467 /* Safe to modify directly (none->non-present). */ 6468 set_huge_pte_at(mm, address, ptep, 6469 make_pte_marker(PTE_MARKER_UFFD_WP), 6470 psize); 6471 goto next; 6472 } 6473 6474 entry = softleaf_from_pte(pte); 6475 if (unlikely(softleaf_is_hwpoison(entry))) { 6476 /* Nothing to do. */ 6477 } else if (unlikely(softleaf_is_migration(entry))) { 6478 struct folio *folio = softleaf_to_folio(entry); 6479 pte_t newpte = pte; 6480 6481 if (softleaf_is_migration_write(entry)) { 6482 if (folio_test_anon(folio)) 6483 entry = make_readable_exclusive_migration_entry( 6484 swp_offset(entry)); 6485 else 6486 entry = make_readable_migration_entry( 6487 swp_offset(entry)); 6488 newpte = swp_entry_to_pte(entry); 6489 pages++; 6490 } 6491 6492 if (uffd_wp) 6493 newpte = pte_swp_mkuffd_wp(newpte); 6494 else if (uffd_wp_resolve) 6495 newpte = pte_swp_clear_uffd_wp(newpte); 6496 if (!pte_same(pte, newpte)) 6497 set_huge_pte_at(mm, address, ptep, newpte, psize); 6498 } else if (unlikely(pte_is_marker(pte))) { 6499 /* 6500 * Do nothing on a poison marker; page is 6501 * corrupted, permissions do not apply. Here 6502 * pte_marker_uffd_wp()==true implies !poison 6503 * because they're mutual exclusive. 6504 */ 6505 if (pte_is_uffd_wp_marker(pte) && uffd_wp_resolve) 6506 /* Safe to modify directly (non-present->none). */ 6507 huge_pte_clear(mm, address, ptep, psize); 6508 } else { 6509 pte_t old_pte; 6510 unsigned int shift = huge_page_shift(hstate_vma(vma)); 6511 6512 old_pte = huge_ptep_modify_prot_start(vma, address, ptep); 6513 pte = huge_pte_modify(old_pte, newprot); 6514 pte = arch_make_huge_pte(pte, shift, vma->vm_flags); 6515 if (uffd_wp) 6516 pte = huge_pte_mkuffd_wp(pte); 6517 else if (uffd_wp_resolve) 6518 pte = huge_pte_clear_uffd_wp(pte); 6519 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte); 6520 pages++; 6521 tlb_remove_huge_tlb_entry(h, &tlb, ptep, address); 6522 } 6523 6524 next: 6525 spin_unlock(ptl); 6526 cond_resched(); 6527 } 6528 6529 tlb_flush_mmu_tlbonly(&tlb); 6530 huge_pmd_unshare_flush(&tlb, vma); 6531 /* 6532 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs() we are 6533 * downgrading page table protection not changing it to point to a new 6534 * page. 6535 * 6536 * See Documentation/mm/mmu_notifier.rst 6537 */ 6538 i_mmap_unlock_write(vma->vm_file->f_mapping); 6539 hugetlb_vma_unlock_write(vma); 6540 mmu_notifier_invalidate_range_end(&range); 6541 tlb_finish_mmu(&tlb); 6542 6543 return pages > 0 ? (pages << h->order) : pages; 6544 } 6545 6546 /* 6547 * Update the reservation map for the range [from, to]. 6548 * 6549 * Returns the number of entries that would be added to the reservation map 6550 * associated with the range [from, to]. This number is greater or equal to 6551 * zero. -EINVAL or -ENOMEM is returned in case of any errors. 6552 */ 6553 6554 long hugetlb_reserve_pages(struct inode *inode, 6555 long from, long to, 6556 struct vm_area_desc *desc, 6557 vma_flags_t vma_flags) 6558 { 6559 long chg = -1, add = -1, spool_resv, gbl_resv; 6560 struct hstate *h = hstate_inode(inode); 6561 struct hugepage_subpool *spool = subpool_inode(inode); 6562 struct resv_map *resv_map; 6563 struct hugetlb_cgroup *h_cg = NULL; 6564 long gbl_reserve, regions_needed = 0; 6565 int err; 6566 6567 /* This should never happen */ 6568 if (from > to) { 6569 VM_WARN(1, "%s called with a negative range\n", __func__); 6570 return -EINVAL; 6571 } 6572 6573 /* 6574 * Only apply hugepage reservation if asked. At fault time, an 6575 * attempt will be made for VM_NORESERVE to allocate a page 6576 * without using reserves 6577 */ 6578 if (vma_flags_test(&vma_flags, VMA_NORESERVE_BIT)) 6579 return 0; 6580 6581 /* 6582 * Shared mappings base their reservation on the number of pages that 6583 * are already allocated on behalf of the file. Private mappings need 6584 * to reserve the full area even if read-only as mprotect() may be 6585 * called to make the mapping read-write. Assume !desc is a shm mapping 6586 */ 6587 if (!desc || vma_desc_test(desc, VMA_MAYSHARE_BIT)) { 6588 /* 6589 * resv_map can not be NULL as hugetlb_reserve_pages is only 6590 * called for inodes for which resv_maps were created (see 6591 * hugetlbfs_get_inode). 6592 */ 6593 resv_map = inode_resv_map(inode); 6594 6595 chg = region_chg(resv_map, from, to, ®ions_needed); 6596 } else { 6597 /* Private mapping. */ 6598 resv_map = resv_map_alloc(); 6599 if (!resv_map) { 6600 err = -ENOMEM; 6601 goto out_err; 6602 } 6603 6604 chg = to - from; 6605 6606 set_vma_desc_resv_map(desc, resv_map); 6607 set_vma_desc_resv_flags(desc, HPAGE_RESV_OWNER); 6608 } 6609 6610 if (chg < 0) { 6611 /* region_chg() above can return -ENOMEM */ 6612 err = (chg == -ENOMEM) ? -ENOMEM : -EINVAL; 6613 goto out_err; 6614 } 6615 6616 err = hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h), 6617 chg * pages_per_huge_page(h), &h_cg); 6618 if (err < 0) 6619 goto out_err; 6620 6621 if (desc && !vma_desc_test(desc, VMA_MAYSHARE_BIT) && h_cg) { 6622 /* For private mappings, the hugetlb_cgroup uncharge info hangs 6623 * of the resv_map. 6624 */ 6625 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h); 6626 } 6627 6628 /* 6629 * There must be enough pages in the subpool for the mapping. If 6630 * the subpool has a minimum size, there may be some global 6631 * reservations already in place (gbl_reserve). 6632 */ 6633 gbl_reserve = hugepage_subpool_get_pages(spool, chg); 6634 if (gbl_reserve < 0) { 6635 err = gbl_reserve; 6636 goto out_uncharge_cgroup; 6637 } 6638 6639 /* 6640 * Check enough hugepages are available for the reservation. 6641 * Hand the pages back to the subpool if there are not 6642 */ 6643 err = hugetlb_acct_memory(h, gbl_reserve); 6644 if (err < 0) 6645 goto out_put_pages; 6646 6647 /* 6648 * Account for the reservations made. Shared mappings record regions 6649 * that have reservations as they are shared by multiple VMAs. 6650 * When the last VMA disappears, the region map says how much 6651 * the reservation was and the page cache tells how much of 6652 * the reservation was consumed. Private mappings are per-VMA and 6653 * only the consumed reservations are tracked. When the VMA 6654 * disappears, the original reservation is the VMA size and the 6655 * consumed reservations are stored in the map. Hence, nothing 6656 * else has to be done for private mappings here 6657 */ 6658 if (!desc || vma_desc_test(desc, VMA_MAYSHARE_BIT)) { 6659 add = region_add(resv_map, from, to, regions_needed, h, h_cg); 6660 6661 if (unlikely(add < 0)) { 6662 hugetlb_acct_memory(h, -gbl_reserve); 6663 err = add; 6664 goto out_put_pages; 6665 } else if (unlikely(chg > add)) { 6666 /* 6667 * pages in this range were added to the reserve 6668 * map between region_chg and region_add. This 6669 * indicates a race with alloc_hugetlb_folio. Adjust 6670 * the subpool and reserve counts modified above 6671 * based on the difference. 6672 */ 6673 long rsv_adjust; 6674 6675 /* 6676 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the 6677 * reference to h_cg->css. See comment below for detail. 6678 */ 6679 hugetlb_cgroup_uncharge_cgroup_rsvd( 6680 hstate_index(h), 6681 (chg - add) * pages_per_huge_page(h), h_cg); 6682 6683 rsv_adjust = hugepage_subpool_put_pages(spool, 6684 chg - add); 6685 hugetlb_acct_memory(h, -rsv_adjust); 6686 } else if (h_cg) { 6687 /* 6688 * The file_regions will hold their own reference to 6689 * h_cg->css. So we should release the reference held 6690 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are 6691 * done. 6692 */ 6693 hugetlb_cgroup_put_rsvd_cgroup(h_cg); 6694 } 6695 } 6696 return chg; 6697 6698 out_put_pages: 6699 spool_resv = chg - gbl_reserve; 6700 if (spool_resv) { 6701 /* put sub pool's reservation back, chg - gbl_reserve */ 6702 gbl_resv = hugepage_subpool_put_pages(spool, spool_resv); 6703 /* 6704 * subpool's reserved pages can not be put back due to race, 6705 * return to hstate. 6706 */ 6707 hugetlb_acct_memory(h, -gbl_resv); 6708 } 6709 /* Restore used_hpages for pages that failed global reservation */ 6710 if (gbl_reserve && spool) { 6711 unsigned long flags; 6712 6713 spin_lock_irqsave(&spool->lock, flags); 6714 if (spool->max_hpages != -1) 6715 spool->used_hpages -= gbl_reserve; 6716 unlock_or_release_subpool(spool, flags); 6717 } 6718 out_uncharge_cgroup: 6719 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h), 6720 chg * pages_per_huge_page(h), h_cg); 6721 out_err: 6722 if (!desc || vma_desc_test(desc, VMA_MAYSHARE_BIT)) 6723 /* Only call region_abort if the region_chg succeeded but the 6724 * region_add failed or didn't run. 6725 */ 6726 if (chg >= 0 && add < 0) 6727 region_abort(resv_map, from, to, regions_needed); 6728 if (desc && is_vma_desc_resv_set(desc, HPAGE_RESV_OWNER)) { 6729 kref_put(&resv_map->refs, resv_map_release); 6730 set_vma_desc_resv_map(desc, NULL); 6731 } 6732 return err; 6733 } 6734 6735 long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 6736 long freed) 6737 { 6738 struct hstate *h = hstate_inode(inode); 6739 struct resv_map *resv_map = inode_resv_map(inode); 6740 long chg = 0; 6741 struct hugepage_subpool *spool = subpool_inode(inode); 6742 long gbl_reserve; 6743 6744 /* 6745 * Since this routine can be called in the evict inode path for all 6746 * hugetlbfs inodes, resv_map could be NULL. 6747 */ 6748 if (resv_map) { 6749 chg = region_del(resv_map, start, end); 6750 /* 6751 * region_del() can fail in the rare case where a region 6752 * must be split and another region descriptor can not be 6753 * allocated. If end == LONG_MAX, it will not fail. 6754 */ 6755 if (chg < 0) 6756 return chg; 6757 } 6758 6759 spin_lock(&inode->i_lock); 6760 inode->i_blocks -= (blocks_per_huge_page(h) * freed); 6761 spin_unlock(&inode->i_lock); 6762 6763 /* 6764 * If the subpool has a minimum size, the number of global 6765 * reservations to be released may be adjusted. 6766 * 6767 * Note that !resv_map implies freed == 0. So (chg - freed) 6768 * won't go negative. 6769 */ 6770 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); 6771 hugetlb_acct_memory(h, -gbl_reserve); 6772 6773 return 0; 6774 } 6775 6776 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 6777 static unsigned long page_table_shareable(struct vm_area_struct *svma, 6778 struct vm_area_struct *vma, 6779 unsigned long addr, pgoff_t idx) 6780 { 6781 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + 6782 svma->vm_start; 6783 unsigned long sbase = saddr & PUD_MASK; 6784 unsigned long s_end = sbase + PUD_SIZE; 6785 6786 /* Allow segments to share if only one is marked locked */ 6787 vm_flags_t vm_flags = vma->vm_flags & ~VM_LOCKED_MASK; 6788 vm_flags_t svm_flags = svma->vm_flags & ~VM_LOCKED_MASK; 6789 6790 /* 6791 * match the virtual addresses, permission and the alignment of the 6792 * page table page. 6793 * 6794 * Also, vma_lock (vm_private_data) is required for sharing. 6795 */ 6796 if (pmd_index(addr) != pmd_index(saddr) || 6797 vm_flags != svm_flags || 6798 !range_in_vma(svma, sbase, s_end) || 6799 !svma->vm_private_data) 6800 return 0; 6801 6802 return saddr; 6803 } 6804 6805 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 6806 { 6807 unsigned long start = addr & PUD_MASK; 6808 unsigned long end = start + PUD_SIZE; 6809 6810 #ifdef CONFIG_USERFAULTFD 6811 if (uffd_disable_huge_pmd_share(vma)) 6812 return false; 6813 #endif 6814 /* 6815 * check on proper vm_flags and page table alignment 6816 */ 6817 if (!(vma->vm_flags & VM_MAYSHARE)) 6818 return false; 6819 if (!vma->vm_private_data) /* vma lock required for sharing */ 6820 return false; 6821 if (!range_in_vma(vma, start, end)) 6822 return false; 6823 return true; 6824 } 6825 6826 /* 6827 * Determine if start,end range within vma could be mapped by shared pmd. 6828 * If yes, adjust start and end to cover range associated with possible 6829 * shared pmd mappings. 6830 */ 6831 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 6832 unsigned long *start, unsigned long *end) 6833 { 6834 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE), 6835 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); 6836 6837 /* 6838 * vma needs to span at least one aligned PUD size, and the range 6839 * must be at least partially within in. 6840 */ 6841 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) || 6842 (*end <= v_start) || (*start >= v_end)) 6843 return; 6844 6845 /* Extend the range to be PUD aligned for a worst case scenario */ 6846 if (*start > v_start) 6847 *start = ALIGN_DOWN(*start, PUD_SIZE); 6848 6849 if (*end < v_end) 6850 *end = ALIGN(*end, PUD_SIZE); 6851 } 6852 6853 /* 6854 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() 6855 * and returns the corresponding pte. While this is not necessary for the 6856 * !shared pmd case because we can allocate the pmd later as well, it makes the 6857 * code much cleaner. pmd allocation is essential for the shared case because 6858 * pud has to be populated inside the same i_mmap_rwsem section - otherwise 6859 * racing tasks could either miss the sharing (see huge_pte_offset) or select a 6860 * bad pmd for sharing. 6861 */ 6862 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 6863 unsigned long addr, pud_t *pud) 6864 { 6865 struct address_space *mapping = vma->vm_file->f_mapping; 6866 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + 6867 vma->vm_pgoff; 6868 struct vm_area_struct *svma; 6869 unsigned long saddr; 6870 pte_t *spte = NULL; 6871 pte_t *pte; 6872 6873 i_mmap_lock_read(mapping); 6874 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { 6875 if (svma == vma) 6876 continue; 6877 6878 saddr = page_table_shareable(svma, vma, addr, idx); 6879 if (saddr) { 6880 spte = hugetlb_walk(svma, saddr, 6881 vma_mmu_pagesize(svma)); 6882 if (spte) { 6883 ptdesc_pmd_pts_inc(virt_to_ptdesc(spte)); 6884 break; 6885 } 6886 } 6887 } 6888 6889 if (!spte) 6890 goto out; 6891 6892 spin_lock(&mm->page_table_lock); 6893 if (pud_none(*pud)) { 6894 pud_populate(mm, pud, 6895 (pmd_t *)((unsigned long)spte & PAGE_MASK)); 6896 mm_inc_nr_pmds(mm); 6897 } else { 6898 ptdesc_pmd_pts_dec(virt_to_ptdesc(spte)); 6899 } 6900 spin_unlock(&mm->page_table_lock); 6901 out: 6902 pte = (pte_t *)pmd_alloc(mm, pud, addr); 6903 i_mmap_unlock_read(mapping); 6904 return pte; 6905 } 6906 6907 /** 6908 * huge_pmd_unshare - Unmap a pmd table if it is shared by multiple users 6909 * @tlb: the current mmu_gather. 6910 * @vma: the vma covering the pmd table. 6911 * @addr: the address we are trying to unshare. 6912 * @ptep: pointer into the (pmd) page table. 6913 * 6914 * Called with the page table lock held, the i_mmap_rwsem held in write mode 6915 * and the hugetlb vma lock held in write mode. 6916 * 6917 * Note: The caller must call huge_pmd_unshare_flush() before dropping the 6918 * i_mmap_rwsem. 6919 * 6920 * Returns: 1 if it was a shared PMD table and it got unmapped, or 0 if it 6921 * was not a shared PMD table. 6922 */ 6923 int huge_pmd_unshare(struct mmu_gather *tlb, struct vm_area_struct *vma, 6924 unsigned long addr, pte_t *ptep) 6925 { 6926 unsigned long sz = huge_page_size(hstate_vma(vma)); 6927 struct mm_struct *mm = vma->vm_mm; 6928 pgd_t *pgd = pgd_offset(mm, addr); 6929 p4d_t *p4d = p4d_offset(pgd, addr); 6930 pud_t *pud = pud_offset(p4d, addr); 6931 6932 if (sz != PMD_SIZE) 6933 return 0; 6934 if (!ptdesc_pmd_is_shared(virt_to_ptdesc(ptep))) 6935 return 0; 6936 i_mmap_assert_write_locked(vma->vm_file->f_mapping); 6937 hugetlb_vma_assert_locked(vma); 6938 pud_clear(pud); 6939 6940 tlb_unshare_pmd_ptdesc(tlb, virt_to_ptdesc(ptep), addr); 6941 6942 mm_dec_nr_pmds(mm); 6943 return 1; 6944 } 6945 6946 /* 6947 * huge_pmd_unshare_flush - Complete a sequence of huge_pmd_unshare() calls 6948 * @tlb: the current mmu_gather. 6949 * @vma: the vma covering the pmd table. 6950 * 6951 * Perform necessary TLB flushes or IPI broadcasts to synchronize PMD table 6952 * unsharing with concurrent page table walkers. 6953 * 6954 * This function must be called after a sequence of huge_pmd_unshare() 6955 * calls while still holding the i_mmap_rwsem. 6956 */ 6957 void huge_pmd_unshare_flush(struct mmu_gather *tlb, struct vm_area_struct *vma) 6958 { 6959 /* 6960 * We must synchronize page table unsharing such that nobody will 6961 * try reusing a previously-shared page table while it might still 6962 * be in use by previous sharers (TLB, GUP_fast). 6963 */ 6964 i_mmap_assert_write_locked(vma->vm_file->f_mapping); 6965 6966 tlb_flush_unshared_tables(tlb); 6967 } 6968 6969 #else /* !CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */ 6970 6971 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 6972 unsigned long addr, pud_t *pud) 6973 { 6974 return NULL; 6975 } 6976 6977 int huge_pmd_unshare(struct mmu_gather *tlb, struct vm_area_struct *vma, 6978 unsigned long addr, pte_t *ptep) 6979 { 6980 return 0; 6981 } 6982 6983 void huge_pmd_unshare_flush(struct mmu_gather *tlb, struct vm_area_struct *vma) 6984 { 6985 } 6986 6987 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 6988 unsigned long *start, unsigned long *end) 6989 { 6990 } 6991 6992 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr) 6993 { 6994 return false; 6995 } 6996 #endif /* CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */ 6997 6998 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB 6999 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 7000 unsigned long addr, unsigned long sz) 7001 { 7002 pgd_t *pgd; 7003 p4d_t *p4d; 7004 pud_t *pud; 7005 pte_t *pte = NULL; 7006 7007 pgd = pgd_offset(mm, addr); 7008 p4d = p4d_alloc(mm, pgd, addr); 7009 if (!p4d) 7010 return NULL; 7011 pud = pud_alloc(mm, p4d, addr); 7012 if (pud) { 7013 if (sz == PUD_SIZE) { 7014 pte = (pte_t *)pud; 7015 } else { 7016 BUG_ON(sz != PMD_SIZE); 7017 if (want_pmd_share(vma, addr) && pud_none(*pud)) 7018 pte = huge_pmd_share(mm, vma, addr, pud); 7019 else 7020 pte = (pte_t *)pmd_alloc(mm, pud, addr); 7021 } 7022 } 7023 7024 if (pte) { 7025 pte_t pteval = ptep_get_lockless(pte); 7026 7027 BUG_ON(pte_present(pteval) && !pte_huge(pteval)); 7028 } 7029 7030 return pte; 7031 } 7032 7033 /* 7034 * huge_pte_offset() - Walk the page table to resolve the hugepage 7035 * entry at address @addr 7036 * 7037 * Return: Pointer to page table entry (PUD or PMD) for 7038 * address @addr, or NULL if a !p*d_present() entry is encountered and the 7039 * size @sz doesn't match the hugepage size at this level of the page 7040 * table. 7041 */ 7042 pte_t *huge_pte_offset(struct mm_struct *mm, 7043 unsigned long addr, unsigned long sz) 7044 { 7045 pgd_t *pgd; 7046 p4d_t *p4d; 7047 pud_t *pud; 7048 pmd_t *pmd; 7049 7050 pgd = pgd_offset(mm, addr); 7051 if (!pgd_present(*pgd)) 7052 return NULL; 7053 p4d = p4d_offset(pgd, addr); 7054 if (!p4d_present(*p4d)) 7055 return NULL; 7056 7057 pud = pud_offset(p4d, addr); 7058 if (sz == PUD_SIZE) 7059 /* must be pud huge, non-present or none */ 7060 return (pte_t *)pud; 7061 if (!pud_present(*pud)) 7062 return NULL; 7063 /* must have a valid entry and size to go further */ 7064 7065 pmd = pmd_offset(pud, addr); 7066 /* must be pmd huge, non-present or none */ 7067 return (pte_t *)pmd; 7068 } 7069 7070 /* 7071 * Return a mask that can be used to update an address to the last huge 7072 * page in a page table page mapping size. Used to skip non-present 7073 * page table entries when linearly scanning address ranges. Architectures 7074 * with unique huge page to page table relationships can define their own 7075 * version of this routine. 7076 */ 7077 unsigned long hugetlb_mask_last_page(struct hstate *h) 7078 { 7079 unsigned long hp_size = huge_page_size(h); 7080 7081 if (hp_size == PUD_SIZE) 7082 return P4D_SIZE - PUD_SIZE; 7083 else if (hp_size == PMD_SIZE) 7084 return PUD_SIZE - PMD_SIZE; 7085 else 7086 return 0UL; 7087 } 7088 7089 #else 7090 7091 /* See description above. Architectures can provide their own version. */ 7092 __weak unsigned long hugetlb_mask_last_page(struct hstate *h) 7093 { 7094 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 7095 if (huge_page_size(h) == PMD_SIZE) 7096 return PUD_SIZE - PMD_SIZE; 7097 #endif 7098 return 0UL; 7099 } 7100 7101 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ 7102 7103 /** 7104 * folio_isolate_hugetlb - try to isolate an allocated hugetlb folio 7105 * @folio: the folio to isolate 7106 * @list: the list to add the folio to on success 7107 * 7108 * Isolate an allocated (refcount > 0) hugetlb folio, marking it as 7109 * isolated/non-migratable, and moving it from the active list to the 7110 * given list. 7111 * 7112 * Isolation will fail if @folio is not an allocated hugetlb folio, or if 7113 * it is already isolated/non-migratable. 7114 * 7115 * On success, an additional folio reference is taken that must be dropped 7116 * using folio_putback_hugetlb() to undo the isolation. 7117 * 7118 * Return: True if isolation worked, otherwise False. 7119 */ 7120 bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list) 7121 { 7122 bool ret = true; 7123 7124 spin_lock_irq(&hugetlb_lock); 7125 if (!folio_test_hugetlb(folio) || 7126 !folio_test_hugetlb_migratable(folio) || 7127 !folio_try_get(folio)) { 7128 ret = false; 7129 goto unlock; 7130 } 7131 folio_clear_hugetlb_migratable(folio); 7132 list_move_tail(&folio->lru, list); 7133 unlock: 7134 spin_unlock_irq(&hugetlb_lock); 7135 return ret; 7136 } 7137 7138 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison) 7139 { 7140 int ret = 0; 7141 7142 *hugetlb = false; 7143 spin_lock_irq(&hugetlb_lock); 7144 if (folio_test_hugetlb(folio)) { 7145 *hugetlb = true; 7146 if (folio_test_hugetlb_freed(folio)) 7147 ret = 0; 7148 else if (folio_test_hugetlb_migratable(folio) || unpoison) 7149 ret = folio_try_get(folio); 7150 else 7151 ret = -EBUSY; 7152 } 7153 spin_unlock_irq(&hugetlb_lock); 7154 return ret; 7155 } 7156 7157 int get_huge_page_for_hwpoison(unsigned long pfn, int flags, 7158 bool *migratable_cleared) 7159 { 7160 int ret; 7161 7162 spin_lock_irq(&hugetlb_lock); 7163 ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared); 7164 spin_unlock_irq(&hugetlb_lock); 7165 return ret; 7166 } 7167 7168 /** 7169 * folio_putback_hugetlb - unisolate a hugetlb folio 7170 * @folio: the isolated hugetlb folio 7171 * 7172 * Putback/un-isolate the hugetlb folio that was previous isolated using 7173 * folio_isolate_hugetlb(): marking it non-isolated/migratable and putting it 7174 * back onto the active list. 7175 * 7176 * Will drop the additional folio reference obtained through 7177 * folio_isolate_hugetlb(). 7178 */ 7179 void folio_putback_hugetlb(struct folio *folio) 7180 { 7181 spin_lock_irq(&hugetlb_lock); 7182 folio_set_hugetlb_migratable(folio); 7183 list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist); 7184 spin_unlock_irq(&hugetlb_lock); 7185 folio_put(folio); 7186 } 7187 7188 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason) 7189 { 7190 struct hstate *h = folio_hstate(old_folio); 7191 7192 hugetlb_cgroup_migrate(old_folio, new_folio); 7193 folio_set_owner_migrate_reason(new_folio, reason); 7194 7195 /* 7196 * transfer temporary state of the new hugetlb folio. This is 7197 * reverse to other transitions because the newpage is going to 7198 * be final while the old one will be freed so it takes over 7199 * the temporary status. 7200 * 7201 * Also note that we have to transfer the per-node surplus state 7202 * here as well otherwise the global surplus count will not match 7203 * the per-node's. 7204 */ 7205 if (folio_test_hugetlb_temporary(new_folio)) { 7206 int old_nid = folio_nid(old_folio); 7207 int new_nid = folio_nid(new_folio); 7208 7209 folio_set_hugetlb_temporary(old_folio); 7210 folio_clear_hugetlb_temporary(new_folio); 7211 7212 7213 /* 7214 * There is no need to transfer the per-node surplus state 7215 * when we do not cross the node. 7216 */ 7217 if (new_nid == old_nid) 7218 return; 7219 spin_lock_irq(&hugetlb_lock); 7220 if (h->surplus_huge_pages_node[old_nid]) { 7221 h->surplus_huge_pages_node[old_nid]--; 7222 h->surplus_huge_pages_node[new_nid]++; 7223 } 7224 spin_unlock_irq(&hugetlb_lock); 7225 } 7226 7227 /* 7228 * Our old folio is isolated and has "migratable" cleared until it 7229 * is putback. As migration succeeded, set the new folio "migratable" 7230 * and add it to the active list. 7231 */ 7232 spin_lock_irq(&hugetlb_lock); 7233 folio_set_hugetlb_migratable(new_folio); 7234 list_move_tail(&new_folio->lru, &(folio_hstate(new_folio))->hugepage_activelist); 7235 spin_unlock_irq(&hugetlb_lock); 7236 } 7237 7238 /* 7239 * If @take_locks is false, the caller must ensure that no concurrent page table 7240 * access can happen (except for gup_fast() and hardware page walks). 7241 * If @take_locks is true, we take the hugetlb VMA lock (to lock out things like 7242 * concurrent page fault handling) and the file rmap lock. 7243 */ 7244 static void hugetlb_unshare_pmds(struct vm_area_struct *vma, 7245 unsigned long start, 7246 unsigned long end, 7247 bool take_locks) 7248 { 7249 struct hstate *h = hstate_vma(vma); 7250 unsigned long sz = huge_page_size(h); 7251 struct mm_struct *mm = vma->vm_mm; 7252 struct mmu_notifier_range range; 7253 struct mmu_gather tlb; 7254 unsigned long address; 7255 spinlock_t *ptl; 7256 pte_t *ptep; 7257 7258 if (!(vma->vm_flags & VM_MAYSHARE)) 7259 return; 7260 7261 if (start >= end) 7262 return; 7263 7264 flush_cache_range(vma, start, end); 7265 tlb_gather_mmu_vma(&tlb, vma); 7266 7267 /* 7268 * No need to call adjust_range_if_pmd_sharing_possible(), because 7269 * we have already done the PUD_SIZE alignment. 7270 */ 7271 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, 7272 start, end); 7273 mmu_notifier_invalidate_range_start(&range); 7274 if (take_locks) { 7275 hugetlb_vma_lock_write(vma); 7276 i_mmap_lock_write(vma->vm_file->f_mapping); 7277 } else { 7278 i_mmap_assert_write_locked(vma->vm_file->f_mapping); 7279 } 7280 for (address = start; address < end; address += PUD_SIZE) { 7281 ptep = hugetlb_walk(vma, address, sz); 7282 if (!ptep) 7283 continue; 7284 ptl = huge_pte_lock(h, mm, ptep); 7285 huge_pmd_unshare(&tlb, vma, address, ptep); 7286 spin_unlock(ptl); 7287 } 7288 huge_pmd_unshare_flush(&tlb, vma); 7289 if (take_locks) { 7290 i_mmap_unlock_write(vma->vm_file->f_mapping); 7291 hugetlb_vma_unlock_write(vma); 7292 } 7293 /* 7294 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs(), see 7295 * Documentation/mm/mmu_notifier.rst. 7296 */ 7297 mmu_notifier_invalidate_range_end(&range); 7298 tlb_finish_mmu(&tlb); 7299 } 7300 7301 /* 7302 * This function will unconditionally remove all the shared pmd pgtable entries 7303 * within the specific vma for a hugetlbfs memory range. 7304 */ 7305 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) 7306 { 7307 hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), 7308 ALIGN_DOWN(vma->vm_end, PUD_SIZE), 7309 /* take_locks = */ true); 7310 } 7311 7312 /* 7313 * For hugetlb, mremap() is an odd edge case - while the VMA copying is 7314 * performed, we permit both the old and new VMAs to reference the same 7315 * reservation. 7316 * 7317 * We fix this up after the operation succeeds, or if a newly allocated VMA 7318 * is closed as a result of a failure to allocate memory. 7319 */ 7320 void fixup_hugetlb_reservations(struct vm_area_struct *vma) 7321 { 7322 if (is_vm_hugetlb_page(vma)) 7323 clear_vma_resv_huge_pages(vma); 7324 } 7325