1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/mm/page_alloc.c 4 * 5 * Manages the free list, the system allocates free pages here. 6 * Note that kmalloc() lives in slab.c 7 * 8 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 9 * Swap reorganised 29.12.95, Stephen Tweedie 10 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 11 * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999 12 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999 13 * Zone balancing, Kanoj Sarcar, SGI, Jan 2000 14 * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002 15 * (lots of bits borrowed from Ingo Molnar & Andrew Morton) 16 */ 17 18 #include <linux/stddef.h> 19 #include <linux/mm.h> 20 #include <linux/highmem.h> 21 #include <linux/interrupt.h> 22 #include <linux/jiffies.h> 23 #include <linux/compiler.h> 24 #include <linux/kernel.h> 25 #include <linux/kasan.h> 26 #include <linux/kmsan.h> 27 #include <linux/module.h> 28 #include <linux/suspend.h> 29 #include <linux/ratelimit.h> 30 #include <linux/oom.h> 31 #include <linux/topology.h> 32 #include <linux/sysctl.h> 33 #include <linux/cpu.h> 34 #include <linux/cpuset.h> 35 #include <linux/pagevec.h> 36 #include <linux/memory_hotplug.h> 37 #include <linux/nodemask.h> 38 #include <linux/vmstat.h> 39 #include <linux/fault-inject.h> 40 #include <linux/compaction.h> 41 #include <trace/events/kmem.h> 42 #include <trace/events/oom.h> 43 #include <linux/prefetch.h> 44 #include <linux/mm_inline.h> 45 #include <linux/mmu_notifier.h> 46 #include <linux/migrate.h> 47 #include <linux/sched/mm.h> 48 #include <linux/page_owner.h> 49 #include <linux/page_table_check.h> 50 #include <linux/memcontrol.h> 51 #include <linux/ftrace.h> 52 #include <linux/lockdep.h> 53 #include <linux/psi.h> 54 #include <linux/khugepaged.h> 55 #include <linux/delayacct.h> 56 #include <linux/cacheinfo.h> 57 #include <linux/pgalloc_tag.h> 58 #include <asm/div64.h> 59 #include "internal.h" 60 #include "shuffle.h" 61 #include "page_reporting.h" 62 63 /* Free Page Internal flags: for internal, non-pcp variants of free_pages(). */ 64 typedef int __bitwise fpi_t; 65 66 /* No special request */ 67 #define FPI_NONE ((__force fpi_t)0) 68 69 /* 70 * Skip free page reporting notification for the (possibly merged) page. 71 * This does not hinder free page reporting from grabbing the page, 72 * reporting it and marking it "reported" - it only skips notifying 73 * the free page reporting infrastructure about a newly freed page. For 74 * example, used when temporarily pulling a page from a freelist and 75 * putting it back unmodified. 76 */ 77 #define FPI_SKIP_REPORT_NOTIFY ((__force fpi_t)BIT(0)) 78 79 /* 80 * Place the (possibly merged) page to the tail of the freelist. Will ignore 81 * page shuffling (relevant code - e.g., memory onlining - is expected to 82 * shuffle the whole zone). 83 * 84 * Note: No code should rely on this flag for correctness - it's purely 85 * to allow for optimizations when handing back either fresh pages 86 * (memory onlining) or untouched pages (page isolation, free page 87 * reporting). 88 */ 89 #define FPI_TO_TAIL ((__force fpi_t)BIT(1)) 90 91 /* Free the page without taking locks. Rely on trylock only. */ 92 #define FPI_TRYLOCK ((__force fpi_t)BIT(2)) 93 94 /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ 95 static DEFINE_MUTEX(pcp_batch_high_lock); 96 #define MIN_PERCPU_PAGELIST_HIGH_FRACTION (8) 97 98 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT) 99 /* 100 * On SMP, spin_trylock is sufficient protection. 101 * On PREEMPT_RT, spin_trylock is equivalent on both SMP and UP. 102 */ 103 #define pcp_trylock_prepare(flags) do { } while (0) 104 #define pcp_trylock_finish(flag) do { } while (0) 105 #else 106 107 /* UP spin_trylock always succeeds so disable IRQs to prevent re-entrancy. */ 108 #define pcp_trylock_prepare(flags) local_irq_save(flags) 109 #define pcp_trylock_finish(flags) local_irq_restore(flags) 110 #endif 111 112 /* 113 * Locking a pcp requires a PCP lookup followed by a spinlock. To avoid 114 * a migration causing the wrong PCP to be locked and remote memory being 115 * potentially allocated, pin the task to the CPU for the lookup+lock. 116 * preempt_disable is used on !RT because it is faster than migrate_disable. 117 * migrate_disable is used on RT because otherwise RT spinlock usage is 118 * interfered with and a high priority task cannot preempt the allocator. 119 */ 120 #ifndef CONFIG_PREEMPT_RT 121 #define pcpu_task_pin() preempt_disable() 122 #define pcpu_task_unpin() preempt_enable() 123 #else 124 #define pcpu_task_pin() migrate_disable() 125 #define pcpu_task_unpin() migrate_enable() 126 #endif 127 128 /* 129 * Generic helper to lookup and a per-cpu variable with an embedded spinlock. 130 * Return value should be used with equivalent unlock helper. 131 */ 132 #define pcpu_spin_lock(type, member, ptr) \ 133 ({ \ 134 type *_ret; \ 135 pcpu_task_pin(); \ 136 _ret = this_cpu_ptr(ptr); \ 137 spin_lock(&_ret->member); \ 138 _ret; \ 139 }) 140 141 #define pcpu_spin_trylock(type, member, ptr) \ 142 ({ \ 143 type *_ret; \ 144 pcpu_task_pin(); \ 145 _ret = this_cpu_ptr(ptr); \ 146 if (!spin_trylock(&_ret->member)) { \ 147 pcpu_task_unpin(); \ 148 _ret = NULL; \ 149 } \ 150 _ret; \ 151 }) 152 153 #define pcpu_spin_unlock(member, ptr) \ 154 ({ \ 155 spin_unlock(&ptr->member); \ 156 pcpu_task_unpin(); \ 157 }) 158 159 /* struct per_cpu_pages specific helpers. */ 160 #define pcp_spin_lock(ptr) \ 161 pcpu_spin_lock(struct per_cpu_pages, lock, ptr) 162 163 #define pcp_spin_trylock(ptr) \ 164 pcpu_spin_trylock(struct per_cpu_pages, lock, ptr) 165 166 #define pcp_spin_unlock(ptr) \ 167 pcpu_spin_unlock(lock, ptr) 168 169 #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID 170 DEFINE_PER_CPU(int, numa_node); 171 EXPORT_PER_CPU_SYMBOL(numa_node); 172 #endif 173 174 DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key); 175 176 #ifdef CONFIG_HAVE_MEMORYLESS_NODES 177 /* 178 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly. 179 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined. 180 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem() 181 * defined in <linux/topology.h>. 182 */ 183 DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */ 184 EXPORT_PER_CPU_SYMBOL(_numa_mem_); 185 #endif 186 187 static DEFINE_MUTEX(pcpu_drain_mutex); 188 189 #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY 190 volatile unsigned long latent_entropy __latent_entropy; 191 EXPORT_SYMBOL(latent_entropy); 192 #endif 193 194 /* 195 * Array of node states. 196 */ 197 nodemask_t node_states[NR_NODE_STATES] __read_mostly = { 198 [N_POSSIBLE] = NODE_MASK_ALL, 199 [N_ONLINE] = { { [0] = 1UL } }, 200 #ifndef CONFIG_NUMA 201 [N_NORMAL_MEMORY] = { { [0] = 1UL } }, 202 #ifdef CONFIG_HIGHMEM 203 [N_HIGH_MEMORY] = { { [0] = 1UL } }, 204 #endif 205 [N_MEMORY] = { { [0] = 1UL } }, 206 [N_CPU] = { { [0] = 1UL } }, 207 #endif /* NUMA */ 208 }; 209 EXPORT_SYMBOL(node_states); 210 211 gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; 212 213 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE 214 unsigned int pageblock_order __read_mostly; 215 #endif 216 217 static void __free_pages_ok(struct page *page, unsigned int order, 218 fpi_t fpi_flags); 219 220 /* 221 * results with 256, 32 in the lowmem_reserve sysctl: 222 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high) 223 * 1G machine -> (16M dma, 784M normal, 224M high) 224 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA 225 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL 226 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA 227 * 228 * TBD: should special case ZONE_DMA32 machines here - in those we normally 229 * don't need any ZONE_NORMAL reservation 230 */ 231 static int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES] = { 232 #ifdef CONFIG_ZONE_DMA 233 [ZONE_DMA] = 256, 234 #endif 235 #ifdef CONFIG_ZONE_DMA32 236 [ZONE_DMA32] = 256, 237 #endif 238 [ZONE_NORMAL] = 32, 239 #ifdef CONFIG_HIGHMEM 240 [ZONE_HIGHMEM] = 0, 241 #endif 242 [ZONE_MOVABLE] = 0, 243 }; 244 245 char * const zone_names[MAX_NR_ZONES] = { 246 #ifdef CONFIG_ZONE_DMA 247 "DMA", 248 #endif 249 #ifdef CONFIG_ZONE_DMA32 250 "DMA32", 251 #endif 252 "Normal", 253 #ifdef CONFIG_HIGHMEM 254 "HighMem", 255 #endif 256 "Movable", 257 #ifdef CONFIG_ZONE_DEVICE 258 "Device", 259 #endif 260 }; 261 262 const char * const migratetype_names[MIGRATE_TYPES] = { 263 "Unmovable", 264 "Movable", 265 "Reclaimable", 266 "HighAtomic", 267 #ifdef CONFIG_CMA 268 "CMA", 269 #endif 270 #ifdef CONFIG_MEMORY_ISOLATION 271 "Isolate", 272 #endif 273 }; 274 275 int min_free_kbytes = 1024; 276 int user_min_free_kbytes = -1; 277 static int watermark_boost_factor __read_mostly = 15000; 278 static int watermark_scale_factor = 10; 279 int defrag_mode; 280 281 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ 282 int movable_zone; 283 EXPORT_SYMBOL(movable_zone); 284 285 #if MAX_NUMNODES > 1 286 unsigned int nr_node_ids __read_mostly = MAX_NUMNODES; 287 unsigned int nr_online_nodes __read_mostly = 1; 288 EXPORT_SYMBOL(nr_node_ids); 289 EXPORT_SYMBOL(nr_online_nodes); 290 #endif 291 292 static bool page_contains_unaccepted(struct page *page, unsigned int order); 293 static bool cond_accept_memory(struct zone *zone, unsigned int order, 294 int alloc_flags); 295 static bool __free_unaccepted(struct page *page); 296 297 int page_group_by_mobility_disabled __read_mostly; 298 299 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 300 /* 301 * During boot we initialize deferred pages on-demand, as needed, but once 302 * page_alloc_init_late() has finished, the deferred pages are all initialized, 303 * and we can permanently disable that path. 304 */ 305 DEFINE_STATIC_KEY_TRUE(deferred_pages); 306 307 static inline bool deferred_pages_enabled(void) 308 { 309 return static_branch_unlikely(&deferred_pages); 310 } 311 312 /* 313 * deferred_grow_zone() is __init, but it is called from 314 * get_page_from_freelist() during early boot until deferred_pages permanently 315 * disables this call. This is why we have refdata wrapper to avoid warning, 316 * and to ensure that the function body gets unloaded. 317 */ 318 static bool __ref 319 _deferred_grow_zone(struct zone *zone, unsigned int order) 320 { 321 return deferred_grow_zone(zone, order); 322 } 323 #else 324 static inline bool deferred_pages_enabled(void) 325 { 326 return false; 327 } 328 329 static inline bool _deferred_grow_zone(struct zone *zone, unsigned int order) 330 { 331 return false; 332 } 333 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 334 335 /* Return a pointer to the bitmap storing bits affecting a block of pages */ 336 static inline unsigned long *get_pageblock_bitmap(const struct page *page, 337 unsigned long pfn) 338 { 339 #ifdef CONFIG_SPARSEMEM 340 return section_to_usemap(__pfn_to_section(pfn)); 341 #else 342 return page_zone(page)->pageblock_flags; 343 #endif /* CONFIG_SPARSEMEM */ 344 } 345 346 static inline int pfn_to_bitidx(const struct page *page, unsigned long pfn) 347 { 348 #ifdef CONFIG_SPARSEMEM 349 pfn &= (PAGES_PER_SECTION-1); 350 #else 351 pfn = pfn - pageblock_start_pfn(page_zone(page)->zone_start_pfn); 352 #endif /* CONFIG_SPARSEMEM */ 353 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; 354 } 355 356 static __always_inline bool is_standalone_pb_bit(enum pageblock_bits pb_bit) 357 { 358 return pb_bit > PB_migrate_end && pb_bit < __NR_PAGEBLOCK_BITS; 359 } 360 361 static __always_inline void 362 get_pfnblock_bitmap_bitidx(const struct page *page, unsigned long pfn, 363 unsigned long **bitmap_word, unsigned long *bitidx) 364 { 365 unsigned long *bitmap; 366 unsigned long word_bitidx; 367 368 #ifdef CONFIG_MEMORY_ISOLATION 369 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 8); 370 #else 371 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4); 372 #endif 373 BUILD_BUG_ON(__MIGRATE_TYPE_END >= (1 << PB_migratetype_bits)); 374 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page); 375 376 bitmap = get_pageblock_bitmap(page, pfn); 377 *bitidx = pfn_to_bitidx(page, pfn); 378 word_bitidx = *bitidx / BITS_PER_LONG; 379 *bitidx &= (BITS_PER_LONG - 1); 380 *bitmap_word = &bitmap[word_bitidx]; 381 } 382 383 384 /** 385 * __get_pfnblock_flags_mask - Return the requested group of flags for 386 * a pageblock_nr_pages block of pages 387 * @page: The page within the block of interest 388 * @pfn: The target page frame number 389 * @mask: mask of bits that the caller is interested in 390 * 391 * Return: pageblock_bits flags 392 */ 393 static unsigned long __get_pfnblock_flags_mask(const struct page *page, 394 unsigned long pfn, 395 unsigned long mask) 396 { 397 unsigned long *bitmap_word; 398 unsigned long bitidx; 399 unsigned long word; 400 401 get_pfnblock_bitmap_bitidx(page, pfn, &bitmap_word, &bitidx); 402 /* 403 * This races, without locks, with set_pfnblock_migratetype(). Ensure 404 * a consistent read of the memory array, so that results, even though 405 * racy, are not corrupted. 406 */ 407 word = READ_ONCE(*bitmap_word); 408 return (word >> bitidx) & mask; 409 } 410 411 /** 412 * get_pfnblock_bit - Check if a standalone bit of a pageblock is set 413 * @page: The page within the block of interest 414 * @pfn: The target page frame number 415 * @pb_bit: pageblock bit to check 416 * 417 * Return: true if the bit is set, otherwise false 418 */ 419 bool get_pfnblock_bit(const struct page *page, unsigned long pfn, 420 enum pageblock_bits pb_bit) 421 { 422 unsigned long *bitmap_word; 423 unsigned long bitidx; 424 425 if (WARN_ON_ONCE(!is_standalone_pb_bit(pb_bit))) 426 return false; 427 428 get_pfnblock_bitmap_bitidx(page, pfn, &bitmap_word, &bitidx); 429 430 return test_bit(bitidx + pb_bit, bitmap_word); 431 } 432 433 /** 434 * get_pfnblock_migratetype - Return the migratetype of a pageblock 435 * @page: The page within the block of interest 436 * @pfn: The target page frame number 437 * 438 * Return: The migratetype of the pageblock 439 * 440 * Use get_pfnblock_migratetype() if caller already has both @page and @pfn 441 * to save a call to page_to_pfn(). 442 */ 443 __always_inline enum migratetype 444 get_pfnblock_migratetype(const struct page *page, unsigned long pfn) 445 { 446 unsigned long mask = MIGRATETYPE_AND_ISO_MASK; 447 unsigned long flags; 448 449 flags = __get_pfnblock_flags_mask(page, pfn, mask); 450 451 #ifdef CONFIG_MEMORY_ISOLATION 452 if (flags & BIT(PB_migrate_isolate)) 453 return MIGRATE_ISOLATE; 454 #endif 455 return flags & MIGRATETYPE_MASK; 456 } 457 458 /** 459 * __set_pfnblock_flags_mask - Set the requested group of flags for 460 * a pageblock_nr_pages block of pages 461 * @page: The page within the block of interest 462 * @pfn: The target page frame number 463 * @flags: The flags to set 464 * @mask: mask of bits that the caller is interested in 465 */ 466 static void __set_pfnblock_flags_mask(struct page *page, unsigned long pfn, 467 unsigned long flags, unsigned long mask) 468 { 469 unsigned long *bitmap_word; 470 unsigned long bitidx; 471 unsigned long word; 472 473 get_pfnblock_bitmap_bitidx(page, pfn, &bitmap_word, &bitidx); 474 475 mask <<= bitidx; 476 flags <<= bitidx; 477 478 word = READ_ONCE(*bitmap_word); 479 do { 480 } while (!try_cmpxchg(bitmap_word, &word, (word & ~mask) | flags)); 481 } 482 483 /** 484 * set_pfnblock_bit - Set a standalone bit of a pageblock 485 * @page: The page within the block of interest 486 * @pfn: The target page frame number 487 * @pb_bit: pageblock bit to set 488 */ 489 void set_pfnblock_bit(const struct page *page, unsigned long pfn, 490 enum pageblock_bits pb_bit) 491 { 492 unsigned long *bitmap_word; 493 unsigned long bitidx; 494 495 if (WARN_ON_ONCE(!is_standalone_pb_bit(pb_bit))) 496 return; 497 498 get_pfnblock_bitmap_bitidx(page, pfn, &bitmap_word, &bitidx); 499 500 set_bit(bitidx + pb_bit, bitmap_word); 501 } 502 503 /** 504 * clear_pfnblock_bit - Clear a standalone bit of a pageblock 505 * @page: The page within the block of interest 506 * @pfn: The target page frame number 507 * @pb_bit: pageblock bit to clear 508 */ 509 void clear_pfnblock_bit(const struct page *page, unsigned long pfn, 510 enum pageblock_bits pb_bit) 511 { 512 unsigned long *bitmap_word; 513 unsigned long bitidx; 514 515 if (WARN_ON_ONCE(!is_standalone_pb_bit(pb_bit))) 516 return; 517 518 get_pfnblock_bitmap_bitidx(page, pfn, &bitmap_word, &bitidx); 519 520 clear_bit(bitidx + pb_bit, bitmap_word); 521 } 522 523 /** 524 * set_pageblock_migratetype - Set the migratetype of a pageblock 525 * @page: The page within the block of interest 526 * @migratetype: migratetype to set 527 */ 528 static void set_pageblock_migratetype(struct page *page, 529 enum migratetype migratetype) 530 { 531 if (unlikely(page_group_by_mobility_disabled && 532 migratetype < MIGRATE_PCPTYPES)) 533 migratetype = MIGRATE_UNMOVABLE; 534 535 #ifdef CONFIG_MEMORY_ISOLATION 536 if (migratetype == MIGRATE_ISOLATE) { 537 VM_WARN_ONCE(1, 538 "Use set_pageblock_isolate() for pageblock isolation"); 539 return; 540 } 541 VM_WARN_ONCE(get_pfnblock_bit(page, page_to_pfn(page), 542 PB_migrate_isolate), 543 "Use clear_pageblock_isolate() to unisolate pageblock"); 544 /* MIGRATETYPE_AND_ISO_MASK clears PB_migrate_isolate if it is set */ 545 #endif 546 __set_pfnblock_flags_mask(page, page_to_pfn(page), 547 (unsigned long)migratetype, 548 MIGRATETYPE_AND_ISO_MASK); 549 } 550 551 void __meminit init_pageblock_migratetype(struct page *page, 552 enum migratetype migratetype, 553 bool isolate) 554 { 555 unsigned long flags; 556 557 if (unlikely(page_group_by_mobility_disabled && 558 migratetype < MIGRATE_PCPTYPES)) 559 migratetype = MIGRATE_UNMOVABLE; 560 561 flags = migratetype; 562 563 #ifdef CONFIG_MEMORY_ISOLATION 564 if (migratetype == MIGRATE_ISOLATE) { 565 VM_WARN_ONCE( 566 1, 567 "Set isolate=true to isolate pageblock with a migratetype"); 568 return; 569 } 570 if (isolate) 571 flags |= BIT(PB_migrate_isolate); 572 #endif 573 __set_pfnblock_flags_mask(page, page_to_pfn(page), flags, 574 MIGRATETYPE_AND_ISO_MASK); 575 } 576 577 #ifdef CONFIG_DEBUG_VM 578 static int page_outside_zone_boundaries(struct zone *zone, struct page *page) 579 { 580 int ret; 581 unsigned seq; 582 unsigned long pfn = page_to_pfn(page); 583 unsigned long sp, start_pfn; 584 585 do { 586 seq = zone_span_seqbegin(zone); 587 start_pfn = zone->zone_start_pfn; 588 sp = zone->spanned_pages; 589 ret = !zone_spans_pfn(zone, pfn); 590 } while (zone_span_seqretry(zone, seq)); 591 592 if (ret) 593 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n", 594 pfn, zone_to_nid(zone), zone->name, 595 start_pfn, start_pfn + sp); 596 597 return ret; 598 } 599 600 /* 601 * Temporary debugging check for pages not lying within a given zone. 602 */ 603 static bool __maybe_unused bad_range(struct zone *zone, struct page *page) 604 { 605 if (page_outside_zone_boundaries(zone, page)) 606 return true; 607 if (zone != page_zone(page)) 608 return true; 609 610 return false; 611 } 612 #else 613 static inline bool __maybe_unused bad_range(struct zone *zone, struct page *page) 614 { 615 return false; 616 } 617 #endif 618 619 static void bad_page(struct page *page, const char *reason) 620 { 621 static unsigned long resume; 622 static unsigned long nr_shown; 623 static unsigned long nr_unshown; 624 625 /* 626 * Allow a burst of 60 reports, then keep quiet for that minute; 627 * or allow a steady drip of one report per second. 628 */ 629 if (nr_shown == 60) { 630 if (time_before(jiffies, resume)) { 631 nr_unshown++; 632 goto out; 633 } 634 if (nr_unshown) { 635 pr_alert( 636 "BUG: Bad page state: %lu messages suppressed\n", 637 nr_unshown); 638 nr_unshown = 0; 639 } 640 nr_shown = 0; 641 } 642 if (nr_shown++ == 0) 643 resume = jiffies + 60 * HZ; 644 645 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n", 646 current->comm, page_to_pfn(page)); 647 dump_page(page, reason); 648 649 print_modules(); 650 dump_stack(); 651 out: 652 /* Leave bad fields for debug, except PageBuddy could make trouble */ 653 if (PageBuddy(page)) 654 __ClearPageBuddy(page); 655 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 656 } 657 658 static inline unsigned int order_to_pindex(int migratetype, int order) 659 { 660 661 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 662 bool movable; 663 if (order > PAGE_ALLOC_COSTLY_ORDER) { 664 VM_BUG_ON(order != HPAGE_PMD_ORDER); 665 666 movable = migratetype == MIGRATE_MOVABLE; 667 668 return NR_LOWORDER_PCP_LISTS + movable; 669 } 670 #else 671 VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); 672 #endif 673 674 return (MIGRATE_PCPTYPES * order) + migratetype; 675 } 676 677 static inline int pindex_to_order(unsigned int pindex) 678 { 679 int order = pindex / MIGRATE_PCPTYPES; 680 681 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 682 if (pindex >= NR_LOWORDER_PCP_LISTS) 683 order = HPAGE_PMD_ORDER; 684 #else 685 VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); 686 #endif 687 688 return order; 689 } 690 691 static inline bool pcp_allowed_order(unsigned int order) 692 { 693 if (order <= PAGE_ALLOC_COSTLY_ORDER) 694 return true; 695 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 696 if (order == HPAGE_PMD_ORDER) 697 return true; 698 #endif 699 return false; 700 } 701 702 /* 703 * Higher-order pages are called "compound pages". They are structured thusly: 704 * 705 * The first PAGE_SIZE page is called the "head page" and have PG_head set. 706 * 707 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded 708 * in bit 0 of page->compound_head. The rest of bits is pointer to head page. 709 * 710 * The first tail page's ->compound_order holds the order of allocation. 711 * This usage means that zero-order pages may not be compound. 712 */ 713 714 void prep_compound_page(struct page *page, unsigned int order) 715 { 716 int i; 717 int nr_pages = 1 << order; 718 719 __SetPageHead(page); 720 for (i = 1; i < nr_pages; i++) 721 prep_compound_tail(page, i); 722 723 prep_compound_head(page, order); 724 } 725 726 static inline void set_buddy_order(struct page *page, unsigned int order) 727 { 728 set_page_private(page, order); 729 __SetPageBuddy(page); 730 } 731 732 #ifdef CONFIG_COMPACTION 733 static inline struct capture_control *task_capc(struct zone *zone) 734 { 735 struct capture_control *capc = current->capture_control; 736 737 return unlikely(capc) && 738 !(current->flags & PF_KTHREAD) && 739 !capc->page && 740 capc->cc->zone == zone ? capc : NULL; 741 } 742 743 static inline bool 744 compaction_capture(struct capture_control *capc, struct page *page, 745 int order, int migratetype) 746 { 747 if (!capc || order != capc->cc->order) 748 return false; 749 750 /* Do not accidentally pollute CMA or isolated regions*/ 751 if (is_migrate_cma(migratetype) || 752 is_migrate_isolate(migratetype)) 753 return false; 754 755 /* 756 * Do not let lower order allocations pollute a movable pageblock 757 * unless compaction is also requesting movable pages. 758 * This might let an unmovable request use a reclaimable pageblock 759 * and vice-versa but no more than normal fallback logic which can 760 * have trouble finding a high-order free page. 761 */ 762 if (order < pageblock_order && migratetype == MIGRATE_MOVABLE && 763 capc->cc->migratetype != MIGRATE_MOVABLE) 764 return false; 765 766 if (migratetype != capc->cc->migratetype) 767 trace_mm_page_alloc_extfrag(page, capc->cc->order, order, 768 capc->cc->migratetype, migratetype); 769 770 capc->page = page; 771 return true; 772 } 773 774 #else 775 static inline struct capture_control *task_capc(struct zone *zone) 776 { 777 return NULL; 778 } 779 780 static inline bool 781 compaction_capture(struct capture_control *capc, struct page *page, 782 int order, int migratetype) 783 { 784 return false; 785 } 786 #endif /* CONFIG_COMPACTION */ 787 788 static inline void account_freepages(struct zone *zone, int nr_pages, 789 int migratetype) 790 { 791 lockdep_assert_held(&zone->lock); 792 793 if (is_migrate_isolate(migratetype)) 794 return; 795 796 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); 797 798 if (is_migrate_cma(migratetype)) 799 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); 800 else if (is_migrate_highatomic(migratetype)) 801 WRITE_ONCE(zone->nr_free_highatomic, 802 zone->nr_free_highatomic + nr_pages); 803 } 804 805 /* Used for pages not on another list */ 806 static inline void __add_to_free_list(struct page *page, struct zone *zone, 807 unsigned int order, int migratetype, 808 bool tail) 809 { 810 struct free_area *area = &zone->free_area[order]; 811 int nr_pages = 1 << order; 812 813 VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype, 814 "page type is %d, passed migratetype is %d (nr=%d)\n", 815 get_pageblock_migratetype(page), migratetype, nr_pages); 816 817 if (tail) 818 list_add_tail(&page->buddy_list, &area->free_list[migratetype]); 819 else 820 list_add(&page->buddy_list, &area->free_list[migratetype]); 821 area->nr_free++; 822 823 if (order >= pageblock_order && !is_migrate_isolate(migratetype)) 824 __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, nr_pages); 825 } 826 827 /* 828 * Used for pages which are on another list. Move the pages to the tail 829 * of the list - so the moved pages won't immediately be considered for 830 * allocation again (e.g., optimization for memory onlining). 831 */ 832 static inline void move_to_free_list(struct page *page, struct zone *zone, 833 unsigned int order, int old_mt, int new_mt) 834 { 835 struct free_area *area = &zone->free_area[order]; 836 int nr_pages = 1 << order; 837 838 /* Free page moving can fail, so it happens before the type update */ 839 VM_WARN_ONCE(get_pageblock_migratetype(page) != old_mt, 840 "page type is %d, passed migratetype is %d (nr=%d)\n", 841 get_pageblock_migratetype(page), old_mt, nr_pages); 842 843 list_move_tail(&page->buddy_list, &area->free_list[new_mt]); 844 845 account_freepages(zone, -nr_pages, old_mt); 846 account_freepages(zone, nr_pages, new_mt); 847 848 if (order >= pageblock_order && 849 is_migrate_isolate(old_mt) != is_migrate_isolate(new_mt)) { 850 if (!is_migrate_isolate(old_mt)) 851 nr_pages = -nr_pages; 852 __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, nr_pages); 853 } 854 } 855 856 static inline void __del_page_from_free_list(struct page *page, struct zone *zone, 857 unsigned int order, int migratetype) 858 { 859 int nr_pages = 1 << order; 860 861 VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype, 862 "page type is %d, passed migratetype is %d (nr=%d)\n", 863 get_pageblock_migratetype(page), migratetype, nr_pages); 864 865 /* clear reported state and update reported page count */ 866 if (page_reported(page)) 867 __ClearPageReported(page); 868 869 list_del(&page->buddy_list); 870 __ClearPageBuddy(page); 871 set_page_private(page, 0); 872 zone->free_area[order].nr_free--; 873 874 if (order >= pageblock_order && !is_migrate_isolate(migratetype)) 875 __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, -nr_pages); 876 } 877 878 static inline void del_page_from_free_list(struct page *page, struct zone *zone, 879 unsigned int order, int migratetype) 880 { 881 __del_page_from_free_list(page, zone, order, migratetype); 882 account_freepages(zone, -(1 << order), migratetype); 883 } 884 885 static inline struct page *get_page_from_free_area(struct free_area *area, 886 int migratetype) 887 { 888 return list_first_entry_or_null(&area->free_list[migratetype], 889 struct page, buddy_list); 890 } 891 892 /* 893 * If this is less than the 2nd largest possible page, check if the buddy 894 * of the next-higher order is free. If it is, it's possible 895 * that pages are being freed that will coalesce soon. In case, 896 * that is happening, add the free page to the tail of the list 897 * so it's less likely to be used soon and more likely to be merged 898 * as a 2-level higher order page 899 */ 900 static inline bool 901 buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, 902 struct page *page, unsigned int order) 903 { 904 unsigned long higher_page_pfn; 905 struct page *higher_page; 906 907 if (order >= MAX_PAGE_ORDER - 1) 908 return false; 909 910 higher_page_pfn = buddy_pfn & pfn; 911 higher_page = page + (higher_page_pfn - pfn); 912 913 return find_buddy_page_pfn(higher_page, higher_page_pfn, order + 1, 914 NULL) != NULL; 915 } 916 917 /* 918 * Freeing function for a buddy system allocator. 919 * 920 * The concept of a buddy system is to maintain direct-mapped table 921 * (containing bit values) for memory blocks of various "orders". 922 * The bottom level table contains the map for the smallest allocatable 923 * units of memory (here, pages), and each level above it describes 924 * pairs of units from the levels below, hence, "buddies". 925 * At a high level, all that happens here is marking the table entry 926 * at the bottom level available, and propagating the changes upward 927 * as necessary, plus some accounting needed to play nicely with other 928 * parts of the VM system. 929 * At each level, we keep a list of pages, which are heads of continuous 930 * free pages of length of (1 << order) and marked with PageBuddy. 931 * Page's order is recorded in page_private(page) field. 932 * So when we are allocating or freeing one, we can derive the state of the 933 * other. That is, if we allocate a small block, and both were 934 * free, the remainder of the region must be split into blocks. 935 * If a block is freed, and its buddy is also free, then this 936 * triggers coalescing into a block of larger size. 937 * 938 * -- nyc 939 */ 940 941 static inline void __free_one_page(struct page *page, 942 unsigned long pfn, 943 struct zone *zone, unsigned int order, 944 int migratetype, fpi_t fpi_flags) 945 { 946 struct capture_control *capc = task_capc(zone); 947 unsigned long buddy_pfn = 0; 948 unsigned long combined_pfn; 949 struct page *buddy; 950 bool to_tail; 951 952 VM_BUG_ON(!zone_is_initialized(zone)); 953 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page); 954 955 VM_BUG_ON(migratetype == -1); 956 VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); 957 VM_BUG_ON_PAGE(bad_range(zone, page), page); 958 959 account_freepages(zone, 1 << order, migratetype); 960 961 while (order < MAX_PAGE_ORDER) { 962 int buddy_mt = migratetype; 963 964 if (compaction_capture(capc, page, order, migratetype)) { 965 account_freepages(zone, -(1 << order), migratetype); 966 return; 967 } 968 969 buddy = find_buddy_page_pfn(page, pfn, order, &buddy_pfn); 970 if (!buddy) 971 goto done_merging; 972 973 if (unlikely(order >= pageblock_order)) { 974 /* 975 * We want to prevent merge between freepages on pageblock 976 * without fallbacks and normal pageblock. Without this, 977 * pageblock isolation could cause incorrect freepage or CMA 978 * accounting or HIGHATOMIC accounting. 979 */ 980 buddy_mt = get_pfnblock_migratetype(buddy, buddy_pfn); 981 982 if (migratetype != buddy_mt && 983 (!migratetype_is_mergeable(migratetype) || 984 !migratetype_is_mergeable(buddy_mt))) 985 goto done_merging; 986 } 987 988 /* 989 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page, 990 * merge with it and move up one order. 991 */ 992 if (page_is_guard(buddy)) 993 clear_page_guard(zone, buddy, order); 994 else 995 __del_page_from_free_list(buddy, zone, order, buddy_mt); 996 997 if (unlikely(buddy_mt != migratetype)) { 998 /* 999 * Match buddy type. This ensures that an 1000 * expand() down the line puts the sub-blocks 1001 * on the right freelists. 1002 */ 1003 set_pageblock_migratetype(buddy, migratetype); 1004 } 1005 1006 combined_pfn = buddy_pfn & pfn; 1007 page = page + (combined_pfn - pfn); 1008 pfn = combined_pfn; 1009 order++; 1010 } 1011 1012 done_merging: 1013 set_buddy_order(page, order); 1014 1015 if (fpi_flags & FPI_TO_TAIL) 1016 to_tail = true; 1017 else if (is_shuffle_order(order)) 1018 to_tail = shuffle_pick_tail(); 1019 else 1020 to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); 1021 1022 __add_to_free_list(page, zone, order, migratetype, to_tail); 1023 1024 /* Notify page reporting subsystem of freed page */ 1025 if (!(fpi_flags & FPI_SKIP_REPORT_NOTIFY)) 1026 page_reporting_notify_free(order); 1027 } 1028 1029 /* 1030 * A bad page could be due to a number of fields. Instead of multiple branches, 1031 * try and check multiple fields with one check. The caller must do a detailed 1032 * check if necessary. 1033 */ 1034 static inline bool page_expected_state(struct page *page, 1035 unsigned long check_flags) 1036 { 1037 if (unlikely(atomic_read(&page->_mapcount) != -1)) 1038 return false; 1039 1040 if (unlikely((unsigned long)page->mapping | 1041 page_ref_count(page) | 1042 #ifdef CONFIG_MEMCG 1043 page->memcg_data | 1044 #endif 1045 page_pool_page_is_pp(page) | 1046 (page->flags & check_flags))) 1047 return false; 1048 1049 return true; 1050 } 1051 1052 static const char *page_bad_reason(struct page *page, unsigned long flags) 1053 { 1054 const char *bad_reason = NULL; 1055 1056 if (unlikely(atomic_read(&page->_mapcount) != -1)) 1057 bad_reason = "nonzero mapcount"; 1058 if (unlikely(page->mapping != NULL)) 1059 bad_reason = "non-NULL mapping"; 1060 if (unlikely(page_ref_count(page) != 0)) 1061 bad_reason = "nonzero _refcount"; 1062 if (unlikely(page->flags & flags)) { 1063 if (flags == PAGE_FLAGS_CHECK_AT_PREP) 1064 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag(s) set"; 1065 else 1066 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set"; 1067 } 1068 #ifdef CONFIG_MEMCG 1069 if (unlikely(page->memcg_data)) 1070 bad_reason = "page still charged to cgroup"; 1071 #endif 1072 if (unlikely(page_pool_page_is_pp(page))) 1073 bad_reason = "page_pool leak"; 1074 return bad_reason; 1075 } 1076 1077 static inline bool free_page_is_bad(struct page *page) 1078 { 1079 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE))) 1080 return false; 1081 1082 /* Something has gone sideways, find it */ 1083 bad_page(page, page_bad_reason(page, PAGE_FLAGS_CHECK_AT_FREE)); 1084 return true; 1085 } 1086 1087 static inline bool is_check_pages_enabled(void) 1088 { 1089 return static_branch_unlikely(&check_pages_enabled); 1090 } 1091 1092 static int free_tail_page_prepare(struct page *head_page, struct page *page) 1093 { 1094 struct folio *folio = (struct folio *)head_page; 1095 int ret = 1; 1096 1097 /* 1098 * We rely page->lru.next never has bit 0 set, unless the page 1099 * is PageTail(). Let's make sure that's true even for poisoned ->lru. 1100 */ 1101 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1); 1102 1103 if (!is_check_pages_enabled()) { 1104 ret = 0; 1105 goto out; 1106 } 1107 switch (page - head_page) { 1108 case 1: 1109 /* the first tail page: these may be in place of ->mapping */ 1110 if (unlikely(folio_large_mapcount(folio))) { 1111 bad_page(page, "nonzero large_mapcount"); 1112 goto out; 1113 } 1114 if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT) && 1115 unlikely(atomic_read(&folio->_nr_pages_mapped))) { 1116 bad_page(page, "nonzero nr_pages_mapped"); 1117 goto out; 1118 } 1119 if (IS_ENABLED(CONFIG_MM_ID)) { 1120 if (unlikely(folio->_mm_id_mapcount[0] != -1)) { 1121 bad_page(page, "nonzero mm mapcount 0"); 1122 goto out; 1123 } 1124 if (unlikely(folio->_mm_id_mapcount[1] != -1)) { 1125 bad_page(page, "nonzero mm mapcount 1"); 1126 goto out; 1127 } 1128 } 1129 if (IS_ENABLED(CONFIG_64BIT)) { 1130 if (unlikely(atomic_read(&folio->_entire_mapcount) + 1)) { 1131 bad_page(page, "nonzero entire_mapcount"); 1132 goto out; 1133 } 1134 if (unlikely(atomic_read(&folio->_pincount))) { 1135 bad_page(page, "nonzero pincount"); 1136 goto out; 1137 } 1138 } 1139 break; 1140 case 2: 1141 /* the second tail page: deferred_list overlaps ->mapping */ 1142 if (unlikely(!list_empty(&folio->_deferred_list))) { 1143 bad_page(page, "on deferred list"); 1144 goto out; 1145 } 1146 if (!IS_ENABLED(CONFIG_64BIT)) { 1147 if (unlikely(atomic_read(&folio->_entire_mapcount) + 1)) { 1148 bad_page(page, "nonzero entire_mapcount"); 1149 goto out; 1150 } 1151 if (unlikely(atomic_read(&folio->_pincount))) { 1152 bad_page(page, "nonzero pincount"); 1153 goto out; 1154 } 1155 } 1156 break; 1157 case 3: 1158 /* the third tail page: hugetlb specifics overlap ->mappings */ 1159 if (IS_ENABLED(CONFIG_HUGETLB_PAGE)) 1160 break; 1161 fallthrough; 1162 default: 1163 if (page->mapping != TAIL_MAPPING) { 1164 bad_page(page, "corrupted mapping in tail page"); 1165 goto out; 1166 } 1167 break; 1168 } 1169 if (unlikely(!PageTail(page))) { 1170 bad_page(page, "PageTail not set"); 1171 goto out; 1172 } 1173 if (unlikely(compound_head(page) != head_page)) { 1174 bad_page(page, "compound_head not consistent"); 1175 goto out; 1176 } 1177 ret = 0; 1178 out: 1179 page->mapping = NULL; 1180 clear_compound_head(page); 1181 return ret; 1182 } 1183 1184 /* 1185 * Skip KASAN memory poisoning when either: 1186 * 1187 * 1. For generic KASAN: deferred memory initialization has not yet completed. 1188 * Tag-based KASAN modes skip pages freed via deferred memory initialization 1189 * using page tags instead (see below). 1190 * 2. For tag-based KASAN modes: the page has a match-all KASAN tag, indicating 1191 * that error detection is disabled for accesses via the page address. 1192 * 1193 * Pages will have match-all tags in the following circumstances: 1194 * 1195 * 1. Pages are being initialized for the first time, including during deferred 1196 * memory init; see the call to page_kasan_tag_reset in __init_single_page. 1197 * 2. The allocation was not unpoisoned due to __GFP_SKIP_KASAN, with the 1198 * exception of pages unpoisoned by kasan_unpoison_vmalloc. 1199 * 3. The allocation was excluded from being checked due to sampling, 1200 * see the call to kasan_unpoison_pages. 1201 * 1202 * Poisoning pages during deferred memory init will greatly lengthen the 1203 * process and cause problem in large memory systems as the deferred pages 1204 * initialization is done with interrupt disabled. 1205 * 1206 * Assuming that there will be no reference to those newly initialized 1207 * pages before they are ever allocated, this should have no effect on 1208 * KASAN memory tracking as the poison will be properly inserted at page 1209 * allocation time. The only corner case is when pages are allocated by 1210 * on-demand allocation and then freed again before the deferred pages 1211 * initialization is done, but this is not likely to happen. 1212 */ 1213 static inline bool should_skip_kasan_poison(struct page *page) 1214 { 1215 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 1216 return deferred_pages_enabled(); 1217 1218 return page_kasan_tag(page) == KASAN_TAG_KERNEL; 1219 } 1220 1221 static void kernel_init_pages(struct page *page, int numpages) 1222 { 1223 int i; 1224 1225 /* s390's use of memset() could override KASAN redzones. */ 1226 kasan_disable_current(); 1227 for (i = 0; i < numpages; i++) 1228 clear_highpage_kasan_tagged(page + i); 1229 kasan_enable_current(); 1230 } 1231 1232 #ifdef CONFIG_MEM_ALLOC_PROFILING 1233 1234 /* Should be called only if mem_alloc_profiling_enabled() */ 1235 void __clear_page_tag_ref(struct page *page) 1236 { 1237 union pgtag_ref_handle handle; 1238 union codetag_ref ref; 1239 1240 if (get_page_tag_ref(page, &ref, &handle)) { 1241 set_codetag_empty(&ref); 1242 update_page_tag_ref(handle, &ref); 1243 put_page_tag_ref(handle); 1244 } 1245 } 1246 1247 /* Should be called only if mem_alloc_profiling_enabled() */ 1248 static noinline 1249 void __pgalloc_tag_add(struct page *page, struct task_struct *task, 1250 unsigned int nr) 1251 { 1252 union pgtag_ref_handle handle; 1253 union codetag_ref ref; 1254 1255 if (get_page_tag_ref(page, &ref, &handle)) { 1256 alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); 1257 update_page_tag_ref(handle, &ref); 1258 put_page_tag_ref(handle); 1259 } 1260 } 1261 1262 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, 1263 unsigned int nr) 1264 { 1265 if (mem_alloc_profiling_enabled()) 1266 __pgalloc_tag_add(page, task, nr); 1267 } 1268 1269 /* Should be called only if mem_alloc_profiling_enabled() */ 1270 static noinline 1271 void __pgalloc_tag_sub(struct page *page, unsigned int nr) 1272 { 1273 union pgtag_ref_handle handle; 1274 union codetag_ref ref; 1275 1276 if (get_page_tag_ref(page, &ref, &handle)) { 1277 alloc_tag_sub(&ref, PAGE_SIZE * nr); 1278 update_page_tag_ref(handle, &ref); 1279 put_page_tag_ref(handle); 1280 } 1281 } 1282 1283 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) 1284 { 1285 if (mem_alloc_profiling_enabled()) 1286 __pgalloc_tag_sub(page, nr); 1287 } 1288 1289 /* When tag is not NULL, assuming mem_alloc_profiling_enabled */ 1290 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) 1291 { 1292 if (tag) 1293 this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr); 1294 } 1295 1296 #else /* CONFIG_MEM_ALLOC_PROFILING */ 1297 1298 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, 1299 unsigned int nr) {} 1300 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} 1301 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {} 1302 1303 #endif /* CONFIG_MEM_ALLOC_PROFILING */ 1304 1305 __always_inline bool free_pages_prepare(struct page *page, 1306 unsigned int order) 1307 { 1308 int bad = 0; 1309 bool skip_kasan_poison = should_skip_kasan_poison(page); 1310 bool init = want_init_on_free(); 1311 bool compound = PageCompound(page); 1312 struct folio *folio = page_folio(page); 1313 1314 VM_BUG_ON_PAGE(PageTail(page), page); 1315 1316 trace_mm_page_free(page, order); 1317 kmsan_free_page(page, order); 1318 1319 if (memcg_kmem_online() && PageMemcgKmem(page)) 1320 __memcg_kmem_uncharge_page(page, order); 1321 1322 /* 1323 * In rare cases, when truncation or holepunching raced with 1324 * munlock after VM_LOCKED was cleared, Mlocked may still be 1325 * found set here. This does not indicate a problem, unless 1326 * "unevictable_pgs_cleared" appears worryingly large. 1327 */ 1328 if (unlikely(folio_test_mlocked(folio))) { 1329 long nr_pages = folio_nr_pages(folio); 1330 1331 __folio_clear_mlocked(folio); 1332 zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages); 1333 count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages); 1334 } 1335 1336 if (unlikely(PageHWPoison(page)) && !order) { 1337 /* Do not let hwpoison pages hit pcplists/buddy */ 1338 reset_page_owner(page, order); 1339 page_table_check_free(page, order); 1340 pgalloc_tag_sub(page, 1 << order); 1341 1342 /* 1343 * The page is isolated and accounted for. 1344 * Mark the codetag as empty to avoid accounting error 1345 * when the page is freed by unpoison_memory(). 1346 */ 1347 clear_page_tag_ref(page); 1348 return false; 1349 } 1350 1351 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); 1352 1353 /* 1354 * Check tail pages before head page information is cleared to 1355 * avoid checking PageCompound for order-0 pages. 1356 */ 1357 if (unlikely(order)) { 1358 int i; 1359 1360 if (compound) { 1361 page[1].flags &= ~PAGE_FLAGS_SECOND; 1362 #ifdef NR_PAGES_IN_LARGE_FOLIO 1363 folio->_nr_pages = 0; 1364 #endif 1365 } 1366 for (i = 1; i < (1 << order); i++) { 1367 if (compound) 1368 bad += free_tail_page_prepare(page, page + i); 1369 if (is_check_pages_enabled()) { 1370 if (free_page_is_bad(page + i)) { 1371 bad++; 1372 continue; 1373 } 1374 } 1375 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; 1376 } 1377 } 1378 if (folio_test_anon(folio)) { 1379 mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1); 1380 folio->mapping = NULL; 1381 } 1382 if (unlikely(page_has_type(page))) 1383 /* Reset the page_type (which overlays _mapcount) */ 1384 page->page_type = UINT_MAX; 1385 1386 if (is_check_pages_enabled()) { 1387 if (free_page_is_bad(page)) 1388 bad++; 1389 if (bad) 1390 return false; 1391 } 1392 1393 page_cpupid_reset_last(page); 1394 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; 1395 reset_page_owner(page, order); 1396 page_table_check_free(page, order); 1397 pgalloc_tag_sub(page, 1 << order); 1398 1399 if (!PageHighMem(page)) { 1400 debug_check_no_locks_freed(page_address(page), 1401 PAGE_SIZE << order); 1402 debug_check_no_obj_freed(page_address(page), 1403 PAGE_SIZE << order); 1404 } 1405 1406 kernel_poison_pages(page, 1 << order); 1407 1408 /* 1409 * As memory initialization might be integrated into KASAN, 1410 * KASAN poisoning and memory initialization code must be 1411 * kept together to avoid discrepancies in behavior. 1412 * 1413 * With hardware tag-based KASAN, memory tags must be set before the 1414 * page becomes unavailable via debug_pagealloc or arch_free_page. 1415 */ 1416 if (!skip_kasan_poison) { 1417 kasan_poison_pages(page, order, init); 1418 1419 /* Memory is already initialized if KASAN did it internally. */ 1420 if (kasan_has_integrated_init()) 1421 init = false; 1422 } 1423 if (init) 1424 kernel_init_pages(page, 1 << order); 1425 1426 /* 1427 * arch_free_page() can make the page's contents inaccessible. s390 1428 * does this. So nothing which can access the page's contents should 1429 * happen after this. 1430 */ 1431 arch_free_page(page, order); 1432 1433 debug_pagealloc_unmap_pages(page, 1 << order); 1434 1435 return true; 1436 } 1437 1438 /* 1439 * Frees a number of pages from the PCP lists 1440 * Assumes all pages on list are in same zone. 1441 * count is the number of pages to free. 1442 */ 1443 static void free_pcppages_bulk(struct zone *zone, int count, 1444 struct per_cpu_pages *pcp, 1445 int pindex) 1446 { 1447 unsigned long flags; 1448 unsigned int order; 1449 struct page *page; 1450 1451 /* 1452 * Ensure proper count is passed which otherwise would stuck in the 1453 * below while (list_empty(list)) loop. 1454 */ 1455 count = min(pcp->count, count); 1456 1457 /* Ensure requested pindex is drained first. */ 1458 pindex = pindex - 1; 1459 1460 spin_lock_irqsave(&zone->lock, flags); 1461 1462 while (count > 0) { 1463 struct list_head *list; 1464 int nr_pages; 1465 1466 /* Remove pages from lists in a round-robin fashion. */ 1467 do { 1468 if (++pindex > NR_PCP_LISTS - 1) 1469 pindex = 0; 1470 list = &pcp->lists[pindex]; 1471 } while (list_empty(list)); 1472 1473 order = pindex_to_order(pindex); 1474 nr_pages = 1 << order; 1475 do { 1476 unsigned long pfn; 1477 int mt; 1478 1479 page = list_last_entry(list, struct page, pcp_list); 1480 pfn = page_to_pfn(page); 1481 mt = get_pfnblock_migratetype(page, pfn); 1482 1483 /* must delete to avoid corrupting pcp list */ 1484 list_del(&page->pcp_list); 1485 count -= nr_pages; 1486 pcp->count -= nr_pages; 1487 1488 __free_one_page(page, pfn, zone, order, mt, FPI_NONE); 1489 trace_mm_page_pcpu_drain(page, order, mt); 1490 } while (count > 0 && !list_empty(list)); 1491 } 1492 1493 spin_unlock_irqrestore(&zone->lock, flags); 1494 } 1495 1496 /* Split a multi-block free page into its individual pageblocks. */ 1497 static void split_large_buddy(struct zone *zone, struct page *page, 1498 unsigned long pfn, int order, fpi_t fpi) 1499 { 1500 unsigned long end = pfn + (1 << order); 1501 1502 VM_WARN_ON_ONCE(!IS_ALIGNED(pfn, 1 << order)); 1503 /* Caller removed page from freelist, buddy info cleared! */ 1504 VM_WARN_ON_ONCE(PageBuddy(page)); 1505 1506 if (order > pageblock_order) 1507 order = pageblock_order; 1508 1509 do { 1510 int mt = get_pfnblock_migratetype(page, pfn); 1511 1512 __free_one_page(page, pfn, zone, order, mt, fpi); 1513 pfn += 1 << order; 1514 if (pfn == end) 1515 break; 1516 page = pfn_to_page(pfn); 1517 } while (1); 1518 } 1519 1520 static void add_page_to_zone_llist(struct zone *zone, struct page *page, 1521 unsigned int order) 1522 { 1523 /* Remember the order */ 1524 page->order = order; 1525 /* Add the page to the free list */ 1526 llist_add(&page->pcp_llist, &zone->trylock_free_pages); 1527 } 1528 1529 static void free_one_page(struct zone *zone, struct page *page, 1530 unsigned long pfn, unsigned int order, 1531 fpi_t fpi_flags) 1532 { 1533 struct llist_head *llhead; 1534 unsigned long flags; 1535 1536 if (unlikely(fpi_flags & FPI_TRYLOCK)) { 1537 if (!spin_trylock_irqsave(&zone->lock, flags)) { 1538 add_page_to_zone_llist(zone, page, order); 1539 return; 1540 } 1541 } else { 1542 spin_lock_irqsave(&zone->lock, flags); 1543 } 1544 1545 /* The lock succeeded. Process deferred pages. */ 1546 llhead = &zone->trylock_free_pages; 1547 if (unlikely(!llist_empty(llhead) && !(fpi_flags & FPI_TRYLOCK))) { 1548 struct llist_node *llnode; 1549 struct page *p, *tmp; 1550 1551 llnode = llist_del_all(llhead); 1552 llist_for_each_entry_safe(p, tmp, llnode, pcp_llist) { 1553 unsigned int p_order = p->order; 1554 1555 split_large_buddy(zone, p, page_to_pfn(p), p_order, fpi_flags); 1556 __count_vm_events(PGFREE, 1 << p_order); 1557 } 1558 } 1559 split_large_buddy(zone, page, pfn, order, fpi_flags); 1560 spin_unlock_irqrestore(&zone->lock, flags); 1561 1562 __count_vm_events(PGFREE, 1 << order); 1563 } 1564 1565 static void __free_pages_ok(struct page *page, unsigned int order, 1566 fpi_t fpi_flags) 1567 { 1568 unsigned long pfn = page_to_pfn(page); 1569 struct zone *zone = page_zone(page); 1570 1571 if (free_pages_prepare(page, order)) 1572 free_one_page(zone, page, pfn, order, fpi_flags); 1573 } 1574 1575 void __meminit __free_pages_core(struct page *page, unsigned int order, 1576 enum meminit_context context) 1577 { 1578 unsigned int nr_pages = 1 << order; 1579 struct page *p = page; 1580 unsigned int loop; 1581 1582 /* 1583 * When initializing the memmap, __init_single_page() sets the refcount 1584 * of all pages to 1 ("allocated"/"not free"). We have to set the 1585 * refcount of all involved pages to 0. 1586 * 1587 * Note that hotplugged memory pages are initialized to PageOffline(). 1588 * Pages freed from memblock might be marked as reserved. 1589 */ 1590 if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG) && 1591 unlikely(context == MEMINIT_HOTPLUG)) { 1592 for (loop = 0; loop < nr_pages; loop++, p++) { 1593 VM_WARN_ON_ONCE(PageReserved(p)); 1594 __ClearPageOffline(p); 1595 set_page_count(p, 0); 1596 } 1597 1598 adjust_managed_page_count(page, nr_pages); 1599 } else { 1600 for (loop = 0; loop < nr_pages; loop++, p++) { 1601 __ClearPageReserved(p); 1602 set_page_count(p, 0); 1603 } 1604 1605 /* memblock adjusts totalram_pages() manually. */ 1606 atomic_long_add(nr_pages, &page_zone(page)->managed_pages); 1607 } 1608 1609 if (page_contains_unaccepted(page, order)) { 1610 if (order == MAX_PAGE_ORDER && __free_unaccepted(page)) 1611 return; 1612 1613 accept_memory(page_to_phys(page), PAGE_SIZE << order); 1614 } 1615 1616 /* 1617 * Bypass PCP and place fresh pages right to the tail, primarily 1618 * relevant for memory onlining. 1619 */ 1620 __free_pages_ok(page, order, FPI_TO_TAIL); 1621 } 1622 1623 /* 1624 * Check that the whole (or subset of) a pageblock given by the interval of 1625 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it 1626 * with the migration of free compaction scanner. 1627 * 1628 * Return struct page pointer of start_pfn, or NULL if checks were not passed. 1629 * 1630 * It's possible on some configurations to have a setup like node0 node1 node0 1631 * i.e. it's possible that all pages within a zones range of pages do not 1632 * belong to a single zone. We assume that a border between node0 and node1 1633 * can occur within a single pageblock, but not a node0 node1 node0 1634 * interleaving within a single pageblock. It is therefore sufficient to check 1635 * the first and last page of a pageblock and avoid checking each individual 1636 * page in a pageblock. 1637 * 1638 * Note: the function may return non-NULL struct page even for a page block 1639 * which contains a memory hole (i.e. there is no physical memory for a subset 1640 * of the pfn range). For example, if the pageblock order is MAX_PAGE_ORDER, which 1641 * will fall into 2 sub-sections, and the end pfn of the pageblock may be hole 1642 * even though the start pfn is online and valid. This should be safe most of 1643 * the time because struct pages are still initialized via init_unavailable_range() 1644 * and pfn walkers shouldn't touch any physical memory range for which they do 1645 * not recognize any specific metadata in struct pages. 1646 */ 1647 struct page *__pageblock_pfn_to_page(unsigned long start_pfn, 1648 unsigned long end_pfn, struct zone *zone) 1649 { 1650 struct page *start_page; 1651 struct page *end_page; 1652 1653 /* end_pfn is one past the range we are checking */ 1654 end_pfn--; 1655 1656 if (!pfn_valid(end_pfn)) 1657 return NULL; 1658 1659 start_page = pfn_to_online_page(start_pfn); 1660 if (!start_page) 1661 return NULL; 1662 1663 if (page_zone(start_page) != zone) 1664 return NULL; 1665 1666 end_page = pfn_to_page(end_pfn); 1667 1668 /* This gives a shorter code than deriving page_zone(end_page) */ 1669 if (page_zone_id(start_page) != page_zone_id(end_page)) 1670 return NULL; 1671 1672 return start_page; 1673 } 1674 1675 /* 1676 * The order of subdivision here is critical for the IO subsystem. 1677 * Please do not alter this order without good reasons and regression 1678 * testing. Specifically, as large blocks of memory are subdivided, 1679 * the order in which smaller blocks are delivered depends on the order 1680 * they're subdivided in this function. This is the primary factor 1681 * influencing the order in which pages are delivered to the IO 1682 * subsystem according to empirical testing, and this is also justified 1683 * by considering the behavior of a buddy system containing a single 1684 * large block of memory acted on by a series of small allocations. 1685 * This behavior is a critical factor in sglist merging's success. 1686 * 1687 * -- nyc 1688 */ 1689 static inline unsigned int expand(struct zone *zone, struct page *page, int low, 1690 int high, int migratetype) 1691 { 1692 unsigned int size = 1 << high; 1693 unsigned int nr_added = 0; 1694 1695 while (high > low) { 1696 high--; 1697 size >>= 1; 1698 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); 1699 1700 /* 1701 * Mark as guard pages (or page), that will allow to 1702 * merge back to allocator when buddy will be freed. 1703 * Corresponding page table entries will not be touched, 1704 * pages will stay not present in virtual address space 1705 */ 1706 if (set_page_guard(zone, &page[size], high)) 1707 continue; 1708 1709 __add_to_free_list(&page[size], zone, high, migratetype, false); 1710 set_buddy_order(&page[size], high); 1711 nr_added += size; 1712 } 1713 1714 return nr_added; 1715 } 1716 1717 static __always_inline void page_del_and_expand(struct zone *zone, 1718 struct page *page, int low, 1719 int high, int migratetype) 1720 { 1721 int nr_pages = 1 << high; 1722 1723 __del_page_from_free_list(page, zone, high, migratetype); 1724 nr_pages -= expand(zone, page, low, high, migratetype); 1725 account_freepages(zone, -nr_pages, migratetype); 1726 } 1727 1728 static void check_new_page_bad(struct page *page) 1729 { 1730 if (unlikely(PageHWPoison(page))) { 1731 /* Don't complain about hwpoisoned pages */ 1732 if (PageBuddy(page)) 1733 __ClearPageBuddy(page); 1734 return; 1735 } 1736 1737 bad_page(page, 1738 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP)); 1739 } 1740 1741 /* 1742 * This page is about to be returned from the page allocator 1743 */ 1744 static bool check_new_page(struct page *page) 1745 { 1746 if (likely(page_expected_state(page, 1747 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON))) 1748 return false; 1749 1750 check_new_page_bad(page); 1751 return true; 1752 } 1753 1754 static inline bool check_new_pages(struct page *page, unsigned int order) 1755 { 1756 if (is_check_pages_enabled()) { 1757 for (int i = 0; i < (1 << order); i++) { 1758 struct page *p = page + i; 1759 1760 if (check_new_page(p)) 1761 return true; 1762 } 1763 } 1764 1765 return false; 1766 } 1767 1768 static inline bool should_skip_kasan_unpoison(gfp_t flags) 1769 { 1770 /* Don't skip if a software KASAN mode is enabled. */ 1771 if (IS_ENABLED(CONFIG_KASAN_GENERIC) || 1772 IS_ENABLED(CONFIG_KASAN_SW_TAGS)) 1773 return false; 1774 1775 /* Skip, if hardware tag-based KASAN is not enabled. */ 1776 if (!kasan_hw_tags_enabled()) 1777 return true; 1778 1779 /* 1780 * With hardware tag-based KASAN enabled, skip if this has been 1781 * requested via __GFP_SKIP_KASAN. 1782 */ 1783 return flags & __GFP_SKIP_KASAN; 1784 } 1785 1786 static inline bool should_skip_init(gfp_t flags) 1787 { 1788 /* Don't skip, if hardware tag-based KASAN is not enabled. */ 1789 if (!kasan_hw_tags_enabled()) 1790 return false; 1791 1792 /* For hardware tag-based KASAN, skip if requested. */ 1793 return (flags & __GFP_SKIP_ZERO); 1794 } 1795 1796 inline void post_alloc_hook(struct page *page, unsigned int order, 1797 gfp_t gfp_flags) 1798 { 1799 bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) && 1800 !should_skip_init(gfp_flags); 1801 bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS); 1802 int i; 1803 1804 set_page_private(page, 0); 1805 1806 arch_alloc_page(page, order); 1807 debug_pagealloc_map_pages(page, 1 << order); 1808 1809 /* 1810 * Page unpoisoning must happen before memory initialization. 1811 * Otherwise, the poison pattern will be overwritten for __GFP_ZERO 1812 * allocations and the page unpoisoning code will complain. 1813 */ 1814 kernel_unpoison_pages(page, 1 << order); 1815 1816 /* 1817 * As memory initialization might be integrated into KASAN, 1818 * KASAN unpoisoning and memory initializion code must be 1819 * kept together to avoid discrepancies in behavior. 1820 */ 1821 1822 /* 1823 * If memory tags should be zeroed 1824 * (which happens only when memory should be initialized as well). 1825 */ 1826 if (zero_tags) { 1827 /* Initialize both memory and memory tags. */ 1828 for (i = 0; i != 1 << order; ++i) 1829 tag_clear_highpage(page + i); 1830 1831 /* Take note that memory was initialized by the loop above. */ 1832 init = false; 1833 } 1834 if (!should_skip_kasan_unpoison(gfp_flags) && 1835 kasan_unpoison_pages(page, order, init)) { 1836 /* Take note that memory was initialized by KASAN. */ 1837 if (kasan_has_integrated_init()) 1838 init = false; 1839 } else { 1840 /* 1841 * If memory tags have not been set by KASAN, reset the page 1842 * tags to ensure page_address() dereferencing does not fault. 1843 */ 1844 for (i = 0; i != 1 << order; ++i) 1845 page_kasan_tag_reset(page + i); 1846 } 1847 /* If memory is still not initialized, initialize it now. */ 1848 if (init) 1849 kernel_init_pages(page, 1 << order); 1850 1851 set_page_owner(page, order, gfp_flags); 1852 page_table_check_alloc(page, order); 1853 pgalloc_tag_add(page, current, 1 << order); 1854 } 1855 1856 static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, 1857 unsigned int alloc_flags) 1858 { 1859 post_alloc_hook(page, order, gfp_flags); 1860 1861 if (order && (gfp_flags & __GFP_COMP)) 1862 prep_compound_page(page, order); 1863 1864 /* 1865 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to 1866 * allocate the page. The expectation is that the caller is taking 1867 * steps that will free more memory. The caller should avoid the page 1868 * being used for !PFMEMALLOC purposes. 1869 */ 1870 if (alloc_flags & ALLOC_NO_WATERMARKS) 1871 set_page_pfmemalloc(page); 1872 else 1873 clear_page_pfmemalloc(page); 1874 } 1875 1876 /* 1877 * Go through the free lists for the given migratetype and remove 1878 * the smallest available page from the freelists 1879 */ 1880 static __always_inline 1881 struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, 1882 int migratetype) 1883 { 1884 unsigned int current_order; 1885 struct free_area *area; 1886 struct page *page; 1887 1888 /* Find a page of the appropriate size in the preferred list */ 1889 for (current_order = order; current_order < NR_PAGE_ORDERS; ++current_order) { 1890 area = &(zone->free_area[current_order]); 1891 page = get_page_from_free_area(area, migratetype); 1892 if (!page) 1893 continue; 1894 1895 page_del_and_expand(zone, page, order, current_order, 1896 migratetype); 1897 trace_mm_page_alloc_zone_locked(page, order, migratetype, 1898 pcp_allowed_order(order) && 1899 migratetype < MIGRATE_PCPTYPES); 1900 return page; 1901 } 1902 1903 return NULL; 1904 } 1905 1906 1907 /* 1908 * This array describes the order lists are fallen back to when 1909 * the free lists for the desirable migrate type are depleted 1910 * 1911 * The other migratetypes do not have fallbacks. 1912 */ 1913 static int fallbacks[MIGRATE_PCPTYPES][MIGRATE_PCPTYPES - 1] = { 1914 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE }, 1915 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE }, 1916 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE }, 1917 }; 1918 1919 #ifdef CONFIG_CMA 1920 static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone, 1921 unsigned int order) 1922 { 1923 return __rmqueue_smallest(zone, order, MIGRATE_CMA); 1924 } 1925 #else 1926 static inline struct page *__rmqueue_cma_fallback(struct zone *zone, 1927 unsigned int order) { return NULL; } 1928 #endif 1929 1930 /* 1931 * Move all free pages of a block to new type's freelist. Caller needs to 1932 * change the block type. 1933 */ 1934 static int __move_freepages_block(struct zone *zone, unsigned long start_pfn, 1935 int old_mt, int new_mt) 1936 { 1937 struct page *page; 1938 unsigned long pfn, end_pfn; 1939 unsigned int order; 1940 int pages_moved = 0; 1941 1942 VM_WARN_ON(start_pfn & (pageblock_nr_pages - 1)); 1943 end_pfn = pageblock_end_pfn(start_pfn); 1944 1945 for (pfn = start_pfn; pfn < end_pfn;) { 1946 page = pfn_to_page(pfn); 1947 if (!PageBuddy(page)) { 1948 pfn++; 1949 continue; 1950 } 1951 1952 /* Make sure we are not inadvertently changing nodes */ 1953 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page); 1954 VM_BUG_ON_PAGE(page_zone(page) != zone, page); 1955 1956 order = buddy_order(page); 1957 1958 move_to_free_list(page, zone, order, old_mt, new_mt); 1959 1960 pfn += 1 << order; 1961 pages_moved += 1 << order; 1962 } 1963 1964 return pages_moved; 1965 } 1966 1967 static bool prep_move_freepages_block(struct zone *zone, struct page *page, 1968 unsigned long *start_pfn, 1969 int *num_free, int *num_movable) 1970 { 1971 unsigned long pfn, start, end; 1972 1973 pfn = page_to_pfn(page); 1974 start = pageblock_start_pfn(pfn); 1975 end = pageblock_end_pfn(pfn); 1976 1977 /* 1978 * The caller only has the lock for @zone, don't touch ranges 1979 * that straddle into other zones. While we could move part of 1980 * the range that's inside the zone, this call is usually 1981 * accompanied by other operations such as migratetype updates 1982 * which also should be locked. 1983 */ 1984 if (!zone_spans_pfn(zone, start)) 1985 return false; 1986 if (!zone_spans_pfn(zone, end - 1)) 1987 return false; 1988 1989 *start_pfn = start; 1990 1991 if (num_free) { 1992 *num_free = 0; 1993 *num_movable = 0; 1994 for (pfn = start; pfn < end;) { 1995 page = pfn_to_page(pfn); 1996 if (PageBuddy(page)) { 1997 int nr = 1 << buddy_order(page); 1998 1999 *num_free += nr; 2000 pfn += nr; 2001 continue; 2002 } 2003 /* 2004 * We assume that pages that could be isolated for 2005 * migration are movable. But we don't actually try 2006 * isolating, as that would be expensive. 2007 */ 2008 if (PageLRU(page) || page_has_movable_ops(page)) 2009 (*num_movable)++; 2010 pfn++; 2011 } 2012 } 2013 2014 return true; 2015 } 2016 2017 static int move_freepages_block(struct zone *zone, struct page *page, 2018 int old_mt, int new_mt) 2019 { 2020 unsigned long start_pfn; 2021 int res; 2022 2023 if (!prep_move_freepages_block(zone, page, &start_pfn, NULL, NULL)) 2024 return -1; 2025 2026 res = __move_freepages_block(zone, start_pfn, old_mt, new_mt); 2027 set_pageblock_migratetype(pfn_to_page(start_pfn), new_mt); 2028 2029 return res; 2030 2031 } 2032 2033 #ifdef CONFIG_MEMORY_ISOLATION 2034 /* Look for a buddy that straddles start_pfn */ 2035 static unsigned long find_large_buddy(unsigned long start_pfn) 2036 { 2037 int order = 0; 2038 struct page *page; 2039 unsigned long pfn = start_pfn; 2040 2041 while (!PageBuddy(page = pfn_to_page(pfn))) { 2042 /* Nothing found */ 2043 if (++order > MAX_PAGE_ORDER) 2044 return start_pfn; 2045 pfn &= ~0UL << order; 2046 } 2047 2048 /* 2049 * Found a preceding buddy, but does it straddle? 2050 */ 2051 if (pfn + (1 << buddy_order(page)) > start_pfn) 2052 return pfn; 2053 2054 /* Nothing found */ 2055 return start_pfn; 2056 } 2057 2058 static inline void toggle_pageblock_isolate(struct page *page, bool isolate) 2059 { 2060 if (isolate) 2061 set_pfnblock_bit(page, page_to_pfn(page), PB_migrate_isolate); 2062 else 2063 clear_pfnblock_bit(page, page_to_pfn(page), PB_migrate_isolate); 2064 } 2065 2066 /** 2067 * __move_freepages_block_isolate - move free pages in block for page isolation 2068 * @zone: the zone 2069 * @page: the pageblock page 2070 * @isolate: to isolate the given pageblock or unisolate it 2071 * 2072 * This is similar to move_freepages_block(), but handles the special 2073 * case encountered in page isolation, where the block of interest 2074 * might be part of a larger buddy spanning multiple pageblocks. 2075 * 2076 * Unlike the regular page allocator path, which moves pages while 2077 * stealing buddies off the freelist, page isolation is interested in 2078 * arbitrary pfn ranges that may have overlapping buddies on both ends. 2079 * 2080 * This function handles that. Straddling buddies are split into 2081 * individual pageblocks. Only the block of interest is moved. 2082 * 2083 * Returns %true if pages could be moved, %false otherwise. 2084 */ 2085 static bool __move_freepages_block_isolate(struct zone *zone, 2086 struct page *page, bool isolate) 2087 { 2088 unsigned long start_pfn, pfn; 2089 int from_mt; 2090 int to_mt; 2091 2092 if (isolate == get_pageblock_isolate(page)) { 2093 VM_WARN_ONCE(1, "%s a pageblock that is already in that state", 2094 isolate ? "Isolate" : "Unisolate"); 2095 return false; 2096 } 2097 2098 if (!prep_move_freepages_block(zone, page, &start_pfn, NULL, NULL)) 2099 return false; 2100 2101 /* No splits needed if buddies can't span multiple blocks */ 2102 if (pageblock_order == MAX_PAGE_ORDER) 2103 goto move; 2104 2105 /* We're a tail block in a larger buddy */ 2106 pfn = find_large_buddy(start_pfn); 2107 if (pfn != start_pfn) { 2108 struct page *buddy = pfn_to_page(pfn); 2109 int order = buddy_order(buddy); 2110 2111 del_page_from_free_list(buddy, zone, order, 2112 get_pfnblock_migratetype(buddy, pfn)); 2113 toggle_pageblock_isolate(page, isolate); 2114 split_large_buddy(zone, buddy, pfn, order, FPI_NONE); 2115 return true; 2116 } 2117 2118 /* We're the starting block of a larger buddy */ 2119 if (PageBuddy(page) && buddy_order(page) > pageblock_order) { 2120 int order = buddy_order(page); 2121 2122 del_page_from_free_list(page, zone, order, 2123 get_pfnblock_migratetype(page, pfn)); 2124 toggle_pageblock_isolate(page, isolate); 2125 split_large_buddy(zone, page, pfn, order, FPI_NONE); 2126 return true; 2127 } 2128 move: 2129 /* Use MIGRATETYPE_MASK to get non-isolate migratetype */ 2130 if (isolate) { 2131 from_mt = __get_pfnblock_flags_mask(page, page_to_pfn(page), 2132 MIGRATETYPE_MASK); 2133 to_mt = MIGRATE_ISOLATE; 2134 } else { 2135 from_mt = MIGRATE_ISOLATE; 2136 to_mt = __get_pfnblock_flags_mask(page, page_to_pfn(page), 2137 MIGRATETYPE_MASK); 2138 } 2139 2140 __move_freepages_block(zone, start_pfn, from_mt, to_mt); 2141 toggle_pageblock_isolate(pfn_to_page(start_pfn), isolate); 2142 2143 return true; 2144 } 2145 2146 bool pageblock_isolate_and_move_free_pages(struct zone *zone, struct page *page) 2147 { 2148 return __move_freepages_block_isolate(zone, page, true); 2149 } 2150 2151 bool pageblock_unisolate_and_move_free_pages(struct zone *zone, struct page *page) 2152 { 2153 return __move_freepages_block_isolate(zone, page, false); 2154 } 2155 2156 #endif /* CONFIG_MEMORY_ISOLATION */ 2157 2158 static void change_pageblock_range(struct page *pageblock_page, 2159 int start_order, int migratetype) 2160 { 2161 int nr_pageblocks = 1 << (start_order - pageblock_order); 2162 2163 while (nr_pageblocks--) { 2164 set_pageblock_migratetype(pageblock_page, migratetype); 2165 pageblock_page += pageblock_nr_pages; 2166 } 2167 } 2168 2169 static inline bool boost_watermark(struct zone *zone) 2170 { 2171 unsigned long max_boost; 2172 2173 if (!watermark_boost_factor) 2174 return false; 2175 /* 2176 * Don't bother in zones that are unlikely to produce results. 2177 * On small machines, including kdump capture kernels running 2178 * in a small area, boosting the watermark can cause an out of 2179 * memory situation immediately. 2180 */ 2181 if ((pageblock_nr_pages * 4) > zone_managed_pages(zone)) 2182 return false; 2183 2184 max_boost = mult_frac(zone->_watermark[WMARK_HIGH], 2185 watermark_boost_factor, 10000); 2186 2187 /* 2188 * high watermark may be uninitialised if fragmentation occurs 2189 * very early in boot so do not boost. We do not fall 2190 * through and boost by pageblock_nr_pages as failing 2191 * allocations that early means that reclaim is not going 2192 * to help and it may even be impossible to reclaim the 2193 * boosted watermark resulting in a hang. 2194 */ 2195 if (!max_boost) 2196 return false; 2197 2198 max_boost = max(pageblock_nr_pages, max_boost); 2199 2200 zone->watermark_boost = min(zone->watermark_boost + pageblock_nr_pages, 2201 max_boost); 2202 2203 return true; 2204 } 2205 2206 /* 2207 * When we are falling back to another migratetype during allocation, should we 2208 * try to claim an entire block to satisfy further allocations, instead of 2209 * polluting multiple pageblocks? 2210 */ 2211 static bool should_try_claim_block(unsigned int order, int start_mt) 2212 { 2213 /* 2214 * Leaving this order check is intended, although there is 2215 * relaxed order check in next check. The reason is that 2216 * we can actually claim the whole pageblock if this condition met, 2217 * but, below check doesn't guarantee it and that is just heuristic 2218 * so could be changed anytime. 2219 */ 2220 if (order >= pageblock_order) 2221 return true; 2222 2223 /* 2224 * Above a certain threshold, always try to claim, as it's likely there 2225 * will be more free pages in the pageblock. 2226 */ 2227 if (order >= pageblock_order / 2) 2228 return true; 2229 2230 /* 2231 * Unmovable/reclaimable allocations would cause permanent 2232 * fragmentations if they fell back to allocating from a movable block 2233 * (polluting it), so we try to claim the whole block regardless of the 2234 * allocation size. Later movable allocations can always steal from this 2235 * block, which is less problematic. 2236 */ 2237 if (start_mt == MIGRATE_RECLAIMABLE || start_mt == MIGRATE_UNMOVABLE) 2238 return true; 2239 2240 if (page_group_by_mobility_disabled) 2241 return true; 2242 2243 /* 2244 * Movable pages won't cause permanent fragmentation, so when you alloc 2245 * small pages, we just need to temporarily steal unmovable or 2246 * reclaimable pages that are closest to the request size. After a 2247 * while, memory compaction may occur to form large contiguous pages, 2248 * and the next movable allocation may not need to steal. 2249 */ 2250 return false; 2251 } 2252 2253 /* 2254 * Check whether there is a suitable fallback freepage with requested order. 2255 * If claimable is true, this function returns fallback_mt only if 2256 * we would do this whole-block claiming. This would help to reduce 2257 * fragmentation due to mixed migratetype pages in one pageblock. 2258 */ 2259 int find_suitable_fallback(struct free_area *area, unsigned int order, 2260 int migratetype, bool claimable) 2261 { 2262 int i; 2263 2264 if (claimable && !should_try_claim_block(order, migratetype)) 2265 return -2; 2266 2267 if (area->nr_free == 0) 2268 return -1; 2269 2270 for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) { 2271 int fallback_mt = fallbacks[migratetype][i]; 2272 2273 if (!free_area_empty(area, fallback_mt)) 2274 return fallback_mt; 2275 } 2276 2277 return -1; 2278 } 2279 2280 /* 2281 * This function implements actual block claiming behaviour. If order is large 2282 * enough, we can claim the whole pageblock for the requested migratetype. If 2283 * not, we check the pageblock for constituent pages; if at least half of the 2284 * pages are free or compatible, we can still claim the whole block, so pages 2285 * freed in the future will be put on the correct free list. 2286 */ 2287 static struct page * 2288 try_to_claim_block(struct zone *zone, struct page *page, 2289 int current_order, int order, int start_type, 2290 int block_type, unsigned int alloc_flags) 2291 { 2292 int free_pages, movable_pages, alike_pages; 2293 unsigned long start_pfn; 2294 2295 /* Take ownership for orders >= pageblock_order */ 2296 if (current_order >= pageblock_order) { 2297 unsigned int nr_added; 2298 2299 del_page_from_free_list(page, zone, current_order, block_type); 2300 change_pageblock_range(page, current_order, start_type); 2301 nr_added = expand(zone, page, order, current_order, start_type); 2302 account_freepages(zone, nr_added, start_type); 2303 return page; 2304 } 2305 2306 /* 2307 * Boost watermarks to increase reclaim pressure to reduce the 2308 * likelihood of future fallbacks. Wake kswapd now as the node 2309 * may be balanced overall and kswapd will not wake naturally. 2310 */ 2311 if (boost_watermark(zone) && (alloc_flags & ALLOC_KSWAPD)) 2312 set_bit(ZONE_BOOSTED_WATERMARK, &zone->flags); 2313 2314 /* moving whole block can fail due to zone boundary conditions */ 2315 if (!prep_move_freepages_block(zone, page, &start_pfn, &free_pages, 2316 &movable_pages)) 2317 return NULL; 2318 2319 /* 2320 * Determine how many pages are compatible with our allocation. 2321 * For movable allocation, it's the number of movable pages which 2322 * we just obtained. For other types it's a bit more tricky. 2323 */ 2324 if (start_type == MIGRATE_MOVABLE) { 2325 alike_pages = movable_pages; 2326 } else { 2327 /* 2328 * If we are falling back a RECLAIMABLE or UNMOVABLE allocation 2329 * to MOVABLE pageblock, consider all non-movable pages as 2330 * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or 2331 * vice versa, be conservative since we can't distinguish the 2332 * exact migratetype of non-movable pages. 2333 */ 2334 if (block_type == MIGRATE_MOVABLE) 2335 alike_pages = pageblock_nr_pages 2336 - (free_pages + movable_pages); 2337 else 2338 alike_pages = 0; 2339 } 2340 /* 2341 * If a sufficient number of pages in the block are either free or of 2342 * compatible migratability as our allocation, claim the whole block. 2343 */ 2344 if (free_pages + alike_pages >= (1 << (pageblock_order-1)) || 2345 page_group_by_mobility_disabled) { 2346 __move_freepages_block(zone, start_pfn, block_type, start_type); 2347 set_pageblock_migratetype(pfn_to_page(start_pfn), start_type); 2348 return __rmqueue_smallest(zone, order, start_type); 2349 } 2350 2351 return NULL; 2352 } 2353 2354 /* 2355 * Try to allocate from some fallback migratetype by claiming the entire block, 2356 * i.e. converting it to the allocation's start migratetype. 2357 * 2358 * The use of signed ints for order and current_order is a deliberate 2359 * deviation from the rest of this file, to make the for loop 2360 * condition simpler. 2361 */ 2362 static __always_inline struct page * 2363 __rmqueue_claim(struct zone *zone, int order, int start_migratetype, 2364 unsigned int alloc_flags) 2365 { 2366 struct free_area *area; 2367 int current_order; 2368 int min_order = order; 2369 struct page *page; 2370 int fallback_mt; 2371 2372 /* 2373 * Do not steal pages from freelists belonging to other pageblocks 2374 * i.e. orders < pageblock_order. If there are no local zones free, 2375 * the zonelists will be reiterated without ALLOC_NOFRAGMENT. 2376 */ 2377 if (order < pageblock_order && alloc_flags & ALLOC_NOFRAGMENT) 2378 min_order = pageblock_order; 2379 2380 /* 2381 * Find the largest available free page in the other list. This roughly 2382 * approximates finding the pageblock with the most free pages, which 2383 * would be too costly to do exactly. 2384 */ 2385 for (current_order = MAX_PAGE_ORDER; current_order >= min_order; 2386 --current_order) { 2387 area = &(zone->free_area[current_order]); 2388 fallback_mt = find_suitable_fallback(area, current_order, 2389 start_migratetype, true); 2390 2391 /* No block in that order */ 2392 if (fallback_mt == -1) 2393 continue; 2394 2395 /* Advanced into orders too low to claim, abort */ 2396 if (fallback_mt == -2) 2397 break; 2398 2399 page = get_page_from_free_area(area, fallback_mt); 2400 page = try_to_claim_block(zone, page, current_order, order, 2401 start_migratetype, fallback_mt, 2402 alloc_flags); 2403 if (page) { 2404 trace_mm_page_alloc_extfrag(page, order, current_order, 2405 start_migratetype, fallback_mt); 2406 return page; 2407 } 2408 } 2409 2410 return NULL; 2411 } 2412 2413 /* 2414 * Try to steal a single page from some fallback migratetype. Leave the rest of 2415 * the block as its current migratetype, potentially causing fragmentation. 2416 */ 2417 static __always_inline struct page * 2418 __rmqueue_steal(struct zone *zone, int order, int start_migratetype) 2419 { 2420 struct free_area *area; 2421 int current_order; 2422 struct page *page; 2423 int fallback_mt; 2424 2425 for (current_order = order; current_order < NR_PAGE_ORDERS; current_order++) { 2426 area = &(zone->free_area[current_order]); 2427 fallback_mt = find_suitable_fallback(area, current_order, 2428 start_migratetype, false); 2429 if (fallback_mt == -1) 2430 continue; 2431 2432 page = get_page_from_free_area(area, fallback_mt); 2433 page_del_and_expand(zone, page, order, current_order, fallback_mt); 2434 trace_mm_page_alloc_extfrag(page, order, current_order, 2435 start_migratetype, fallback_mt); 2436 return page; 2437 } 2438 2439 return NULL; 2440 } 2441 2442 enum rmqueue_mode { 2443 RMQUEUE_NORMAL, 2444 RMQUEUE_CMA, 2445 RMQUEUE_CLAIM, 2446 RMQUEUE_STEAL, 2447 }; 2448 2449 /* 2450 * Do the hard work of removing an element from the buddy allocator. 2451 * Call me with the zone->lock already held. 2452 */ 2453 static __always_inline struct page * 2454 __rmqueue(struct zone *zone, unsigned int order, int migratetype, 2455 unsigned int alloc_flags, enum rmqueue_mode *mode) 2456 { 2457 struct page *page; 2458 2459 if (IS_ENABLED(CONFIG_CMA)) { 2460 /* 2461 * Balance movable allocations between regular and CMA areas by 2462 * allocating from CMA when over half of the zone's free memory 2463 * is in the CMA area. 2464 */ 2465 if (alloc_flags & ALLOC_CMA && 2466 zone_page_state(zone, NR_FREE_CMA_PAGES) > 2467 zone_page_state(zone, NR_FREE_PAGES) / 2) { 2468 page = __rmqueue_cma_fallback(zone, order); 2469 if (page) 2470 return page; 2471 } 2472 } 2473 2474 /* 2475 * First try the freelists of the requested migratetype, then try 2476 * fallbacks modes with increasing levels of fragmentation risk. 2477 * 2478 * The fallback logic is expensive and rmqueue_bulk() calls in 2479 * a loop with the zone->lock held, meaning the freelists are 2480 * not subject to any outside changes. Remember in *mode where 2481 * we found pay dirt, to save us the search on the next call. 2482 */ 2483 switch (*mode) { 2484 case RMQUEUE_NORMAL: 2485 page = __rmqueue_smallest(zone, order, migratetype); 2486 if (page) 2487 return page; 2488 fallthrough; 2489 case RMQUEUE_CMA: 2490 if (alloc_flags & ALLOC_CMA) { 2491 page = __rmqueue_cma_fallback(zone, order); 2492 if (page) { 2493 *mode = RMQUEUE_CMA; 2494 return page; 2495 } 2496 } 2497 fallthrough; 2498 case RMQUEUE_CLAIM: 2499 page = __rmqueue_claim(zone, order, migratetype, alloc_flags); 2500 if (page) { 2501 /* Replenished preferred freelist, back to normal mode. */ 2502 *mode = RMQUEUE_NORMAL; 2503 return page; 2504 } 2505 fallthrough; 2506 case RMQUEUE_STEAL: 2507 if (!(alloc_flags & ALLOC_NOFRAGMENT)) { 2508 page = __rmqueue_steal(zone, order, migratetype); 2509 if (page) { 2510 *mode = RMQUEUE_STEAL; 2511 return page; 2512 } 2513 } 2514 } 2515 return NULL; 2516 } 2517 2518 /* 2519 * Obtain a specified number of elements from the buddy allocator, all under 2520 * a single hold of the lock, for efficiency. Add them to the supplied list. 2521 * Returns the number of new pages which were placed at *list. 2522 */ 2523 static int rmqueue_bulk(struct zone *zone, unsigned int order, 2524 unsigned long count, struct list_head *list, 2525 int migratetype, unsigned int alloc_flags) 2526 { 2527 enum rmqueue_mode rmqm = RMQUEUE_NORMAL; 2528 unsigned long flags; 2529 int i; 2530 2531 if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { 2532 if (!spin_trylock_irqsave(&zone->lock, flags)) 2533 return 0; 2534 } else { 2535 spin_lock_irqsave(&zone->lock, flags); 2536 } 2537 for (i = 0; i < count; ++i) { 2538 struct page *page = __rmqueue(zone, order, migratetype, 2539 alloc_flags, &rmqm); 2540 if (unlikely(page == NULL)) 2541 break; 2542 2543 /* 2544 * Split buddy pages returned by expand() are received here in 2545 * physical page order. The page is added to the tail of 2546 * caller's list. From the callers perspective, the linked list 2547 * is ordered by page number under some conditions. This is 2548 * useful for IO devices that can forward direction from the 2549 * head, thus also in the physical page order. This is useful 2550 * for IO devices that can merge IO requests if the physical 2551 * pages are ordered properly. 2552 */ 2553 list_add_tail(&page->pcp_list, list); 2554 } 2555 spin_unlock_irqrestore(&zone->lock, flags); 2556 2557 return i; 2558 } 2559 2560 /* 2561 * Called from the vmstat counter updater to decay the PCP high. 2562 * Return whether there are addition works to do. 2563 */ 2564 int decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp) 2565 { 2566 int high_min, to_drain, batch; 2567 int todo = 0; 2568 2569 high_min = READ_ONCE(pcp->high_min); 2570 batch = READ_ONCE(pcp->batch); 2571 /* 2572 * Decrease pcp->high periodically to try to free possible 2573 * idle PCP pages. And, avoid to free too many pages to 2574 * control latency. This caps pcp->high decrement too. 2575 */ 2576 if (pcp->high > high_min) { 2577 pcp->high = max3(pcp->count - (batch << CONFIG_PCP_BATCH_SCALE_MAX), 2578 pcp->high - (pcp->high >> 3), high_min); 2579 if (pcp->high > high_min) 2580 todo++; 2581 } 2582 2583 to_drain = pcp->count - pcp->high; 2584 if (to_drain > 0) { 2585 spin_lock(&pcp->lock); 2586 free_pcppages_bulk(zone, to_drain, pcp, 0); 2587 spin_unlock(&pcp->lock); 2588 todo++; 2589 } 2590 2591 return todo; 2592 } 2593 2594 #ifdef CONFIG_NUMA 2595 /* 2596 * Called from the vmstat counter updater to drain pagesets of this 2597 * currently executing processor on remote nodes after they have 2598 * expired. 2599 */ 2600 void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) 2601 { 2602 int to_drain, batch; 2603 2604 batch = READ_ONCE(pcp->batch); 2605 to_drain = min(pcp->count, batch); 2606 if (to_drain > 0) { 2607 spin_lock(&pcp->lock); 2608 free_pcppages_bulk(zone, to_drain, pcp, 0); 2609 spin_unlock(&pcp->lock); 2610 } 2611 } 2612 #endif 2613 2614 /* 2615 * Drain pcplists of the indicated processor and zone. 2616 */ 2617 static void drain_pages_zone(unsigned int cpu, struct zone *zone) 2618 { 2619 struct per_cpu_pages *pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); 2620 int count; 2621 2622 do { 2623 spin_lock(&pcp->lock); 2624 count = pcp->count; 2625 if (count) { 2626 int to_drain = min(count, 2627 pcp->batch << CONFIG_PCP_BATCH_SCALE_MAX); 2628 2629 free_pcppages_bulk(zone, to_drain, pcp, 0); 2630 count -= to_drain; 2631 } 2632 spin_unlock(&pcp->lock); 2633 } while (count); 2634 } 2635 2636 /* 2637 * Drain pcplists of all zones on the indicated processor. 2638 */ 2639 static void drain_pages(unsigned int cpu) 2640 { 2641 struct zone *zone; 2642 2643 for_each_populated_zone(zone) { 2644 drain_pages_zone(cpu, zone); 2645 } 2646 } 2647 2648 /* 2649 * Spill all of this CPU's per-cpu pages back into the buddy allocator. 2650 */ 2651 void drain_local_pages(struct zone *zone) 2652 { 2653 int cpu = smp_processor_id(); 2654 2655 if (zone) 2656 drain_pages_zone(cpu, zone); 2657 else 2658 drain_pages(cpu); 2659 } 2660 2661 /* 2662 * The implementation of drain_all_pages(), exposing an extra parameter to 2663 * drain on all cpus. 2664 * 2665 * drain_all_pages() is optimized to only execute on cpus where pcplists are 2666 * not empty. The check for non-emptiness can however race with a free to 2667 * pcplist that has not yet increased the pcp->count from 0 to 1. Callers 2668 * that need the guarantee that every CPU has drained can disable the 2669 * optimizing racy check. 2670 */ 2671 static void __drain_all_pages(struct zone *zone, bool force_all_cpus) 2672 { 2673 int cpu; 2674 2675 /* 2676 * Allocate in the BSS so we won't require allocation in 2677 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y 2678 */ 2679 static cpumask_t cpus_with_pcps; 2680 2681 /* 2682 * Do not drain if one is already in progress unless it's specific to 2683 * a zone. Such callers are primarily CMA and memory hotplug and need 2684 * the drain to be complete when the call returns. 2685 */ 2686 if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) { 2687 if (!zone) 2688 return; 2689 mutex_lock(&pcpu_drain_mutex); 2690 } 2691 2692 /* 2693 * We don't care about racing with CPU hotplug event 2694 * as offline notification will cause the notified 2695 * cpu to drain that CPU pcps and on_each_cpu_mask 2696 * disables preemption as part of its processing 2697 */ 2698 for_each_online_cpu(cpu) { 2699 struct per_cpu_pages *pcp; 2700 struct zone *z; 2701 bool has_pcps = false; 2702 2703 if (force_all_cpus) { 2704 /* 2705 * The pcp.count check is racy, some callers need a 2706 * guarantee that no cpu is missed. 2707 */ 2708 has_pcps = true; 2709 } else if (zone) { 2710 pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); 2711 if (pcp->count) 2712 has_pcps = true; 2713 } else { 2714 for_each_populated_zone(z) { 2715 pcp = per_cpu_ptr(z->per_cpu_pageset, cpu); 2716 if (pcp->count) { 2717 has_pcps = true; 2718 break; 2719 } 2720 } 2721 } 2722 2723 if (has_pcps) 2724 cpumask_set_cpu(cpu, &cpus_with_pcps); 2725 else 2726 cpumask_clear_cpu(cpu, &cpus_with_pcps); 2727 } 2728 2729 for_each_cpu(cpu, &cpus_with_pcps) { 2730 if (zone) 2731 drain_pages_zone(cpu, zone); 2732 else 2733 drain_pages(cpu); 2734 } 2735 2736 mutex_unlock(&pcpu_drain_mutex); 2737 } 2738 2739 /* 2740 * Spill all the per-cpu pages from all CPUs back into the buddy allocator. 2741 * 2742 * When zone parameter is non-NULL, spill just the single zone's pages. 2743 */ 2744 void drain_all_pages(struct zone *zone) 2745 { 2746 __drain_all_pages(zone, false); 2747 } 2748 2749 static int nr_pcp_free(struct per_cpu_pages *pcp, int batch, int high, bool free_high) 2750 { 2751 int min_nr_free, max_nr_free; 2752 2753 /* Free as much as possible if batch freeing high-order pages. */ 2754 if (unlikely(free_high)) 2755 return min(pcp->count, batch << CONFIG_PCP_BATCH_SCALE_MAX); 2756 2757 /* Check for PCP disabled or boot pageset */ 2758 if (unlikely(high < batch)) 2759 return 1; 2760 2761 /* Leave at least pcp->batch pages on the list */ 2762 min_nr_free = batch; 2763 max_nr_free = high - batch; 2764 2765 /* 2766 * Increase the batch number to the number of the consecutive 2767 * freed pages to reduce zone lock contention. 2768 */ 2769 batch = clamp_t(int, pcp->free_count, min_nr_free, max_nr_free); 2770 2771 return batch; 2772 } 2773 2774 static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, 2775 int batch, bool free_high) 2776 { 2777 int high, high_min, high_max; 2778 2779 high_min = READ_ONCE(pcp->high_min); 2780 high_max = READ_ONCE(pcp->high_max); 2781 high = pcp->high = clamp(pcp->high, high_min, high_max); 2782 2783 if (unlikely(!high)) 2784 return 0; 2785 2786 if (unlikely(free_high)) { 2787 pcp->high = max(high - (batch << CONFIG_PCP_BATCH_SCALE_MAX), 2788 high_min); 2789 return 0; 2790 } 2791 2792 /* 2793 * If reclaim is active, limit the number of pages that can be 2794 * stored on pcp lists 2795 */ 2796 if (test_bit(ZONE_RECLAIM_ACTIVE, &zone->flags)) { 2797 int free_count = max_t(int, pcp->free_count, batch); 2798 2799 pcp->high = max(high - free_count, high_min); 2800 return min(batch << 2, pcp->high); 2801 } 2802 2803 if (high_min == high_max) 2804 return high; 2805 2806 if (test_bit(ZONE_BELOW_HIGH, &zone->flags)) { 2807 int free_count = max_t(int, pcp->free_count, batch); 2808 2809 pcp->high = max(high - free_count, high_min); 2810 high = max(pcp->count, high_min); 2811 } else if (pcp->count >= high) { 2812 int need_high = pcp->free_count + batch; 2813 2814 /* pcp->high should be large enough to hold batch freed pages */ 2815 if (pcp->high < need_high) 2816 pcp->high = clamp(need_high, high_min, high_max); 2817 } 2818 2819 return high; 2820 } 2821 2822 static void free_frozen_page_commit(struct zone *zone, 2823 struct per_cpu_pages *pcp, struct page *page, int migratetype, 2824 unsigned int order, fpi_t fpi_flags) 2825 { 2826 int high, batch; 2827 int pindex; 2828 bool free_high = false; 2829 2830 /* 2831 * On freeing, reduce the number of pages that are batch allocated. 2832 * See nr_pcp_alloc() where alloc_factor is increased for subsequent 2833 * allocations. 2834 */ 2835 pcp->alloc_factor >>= 1; 2836 __count_vm_events(PGFREE, 1 << order); 2837 pindex = order_to_pindex(migratetype, order); 2838 list_add(&page->pcp_list, &pcp->lists[pindex]); 2839 pcp->count += 1 << order; 2840 2841 batch = READ_ONCE(pcp->batch); 2842 /* 2843 * As high-order pages other than THP's stored on PCP can contribute 2844 * to fragmentation, limit the number stored when PCP is heavily 2845 * freeing without allocation. The remainder after bulk freeing 2846 * stops will be drained from vmstat refresh context. 2847 */ 2848 if (order && order <= PAGE_ALLOC_COSTLY_ORDER) { 2849 free_high = (pcp->free_count >= (batch + pcp->high_min / 2) && 2850 (pcp->flags & PCPF_PREV_FREE_HIGH_ORDER) && 2851 (!(pcp->flags & PCPF_FREE_HIGH_BATCH) || 2852 pcp->count >= batch)); 2853 pcp->flags |= PCPF_PREV_FREE_HIGH_ORDER; 2854 } else if (pcp->flags & PCPF_PREV_FREE_HIGH_ORDER) { 2855 pcp->flags &= ~PCPF_PREV_FREE_HIGH_ORDER; 2856 } 2857 if (pcp->free_count < (batch << CONFIG_PCP_BATCH_SCALE_MAX)) 2858 pcp->free_count += (1 << order); 2859 2860 if (unlikely(fpi_flags & FPI_TRYLOCK)) { 2861 /* 2862 * Do not attempt to take a zone lock. Let pcp->count get 2863 * over high mark temporarily. 2864 */ 2865 return; 2866 } 2867 high = nr_pcp_high(pcp, zone, batch, free_high); 2868 if (pcp->count >= high) { 2869 free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high), 2870 pcp, pindex); 2871 if (test_bit(ZONE_BELOW_HIGH, &zone->flags) && 2872 zone_watermark_ok(zone, 0, high_wmark_pages(zone), 2873 ZONE_MOVABLE, 0)) 2874 clear_bit(ZONE_BELOW_HIGH, &zone->flags); 2875 } 2876 } 2877 2878 /* 2879 * Free a pcp page 2880 */ 2881 static void __free_frozen_pages(struct page *page, unsigned int order, 2882 fpi_t fpi_flags) 2883 { 2884 unsigned long __maybe_unused UP_flags; 2885 struct per_cpu_pages *pcp; 2886 struct zone *zone; 2887 unsigned long pfn = page_to_pfn(page); 2888 int migratetype; 2889 2890 if (!pcp_allowed_order(order)) { 2891 __free_pages_ok(page, order, fpi_flags); 2892 return; 2893 } 2894 2895 if (!free_pages_prepare(page, order)) 2896 return; 2897 2898 /* 2899 * We only track unmovable, reclaimable and movable on pcp lists. 2900 * Place ISOLATE pages on the isolated list because they are being 2901 * offlined but treat HIGHATOMIC and CMA as movable pages so we can 2902 * get those areas back if necessary. Otherwise, we may have to free 2903 * excessively into the page allocator 2904 */ 2905 zone = page_zone(page); 2906 migratetype = get_pfnblock_migratetype(page, pfn); 2907 if (unlikely(migratetype >= MIGRATE_PCPTYPES)) { 2908 if (unlikely(is_migrate_isolate(migratetype))) { 2909 free_one_page(zone, page, pfn, order, fpi_flags); 2910 return; 2911 } 2912 migratetype = MIGRATE_MOVABLE; 2913 } 2914 2915 if (unlikely((fpi_flags & FPI_TRYLOCK) && IS_ENABLED(CONFIG_PREEMPT_RT) 2916 && (in_nmi() || in_hardirq()))) { 2917 add_page_to_zone_llist(zone, page, order); 2918 return; 2919 } 2920 pcp_trylock_prepare(UP_flags); 2921 pcp = pcp_spin_trylock(zone->per_cpu_pageset); 2922 if (pcp) { 2923 free_frozen_page_commit(zone, pcp, page, migratetype, order, fpi_flags); 2924 pcp_spin_unlock(pcp); 2925 } else { 2926 free_one_page(zone, page, pfn, order, fpi_flags); 2927 } 2928 pcp_trylock_finish(UP_flags); 2929 } 2930 2931 void free_frozen_pages(struct page *page, unsigned int order) 2932 { 2933 __free_frozen_pages(page, order, FPI_NONE); 2934 } 2935 2936 /* 2937 * Free a batch of folios 2938 */ 2939 void free_unref_folios(struct folio_batch *folios) 2940 { 2941 unsigned long __maybe_unused UP_flags; 2942 struct per_cpu_pages *pcp = NULL; 2943 struct zone *locked_zone = NULL; 2944 int i, j; 2945 2946 /* Prepare folios for freeing */ 2947 for (i = 0, j = 0; i < folios->nr; i++) { 2948 struct folio *folio = folios->folios[i]; 2949 unsigned long pfn = folio_pfn(folio); 2950 unsigned int order = folio_order(folio); 2951 2952 if (!free_pages_prepare(&folio->page, order)) 2953 continue; 2954 /* 2955 * Free orders not handled on the PCP directly to the 2956 * allocator. 2957 */ 2958 if (!pcp_allowed_order(order)) { 2959 free_one_page(folio_zone(folio), &folio->page, 2960 pfn, order, FPI_NONE); 2961 continue; 2962 } 2963 folio->private = (void *)(unsigned long)order; 2964 if (j != i) 2965 folios->folios[j] = folio; 2966 j++; 2967 } 2968 folios->nr = j; 2969 2970 for (i = 0; i < folios->nr; i++) { 2971 struct folio *folio = folios->folios[i]; 2972 struct zone *zone = folio_zone(folio); 2973 unsigned long pfn = folio_pfn(folio); 2974 unsigned int order = (unsigned long)folio->private; 2975 int migratetype; 2976 2977 folio->private = NULL; 2978 migratetype = get_pfnblock_migratetype(&folio->page, pfn); 2979 2980 /* Different zone requires a different pcp lock */ 2981 if (zone != locked_zone || 2982 is_migrate_isolate(migratetype)) { 2983 if (pcp) { 2984 pcp_spin_unlock(pcp); 2985 pcp_trylock_finish(UP_flags); 2986 locked_zone = NULL; 2987 pcp = NULL; 2988 } 2989 2990 /* 2991 * Free isolated pages directly to the 2992 * allocator, see comment in free_frozen_pages. 2993 */ 2994 if (is_migrate_isolate(migratetype)) { 2995 free_one_page(zone, &folio->page, pfn, 2996 order, FPI_NONE); 2997 continue; 2998 } 2999 3000 /* 3001 * trylock is necessary as folios may be getting freed 3002 * from IRQ or SoftIRQ context after an IO completion. 3003 */ 3004 pcp_trylock_prepare(UP_flags); 3005 pcp = pcp_spin_trylock(zone->per_cpu_pageset); 3006 if (unlikely(!pcp)) { 3007 pcp_trylock_finish(UP_flags); 3008 free_one_page(zone, &folio->page, pfn, 3009 order, FPI_NONE); 3010 continue; 3011 } 3012 locked_zone = zone; 3013 } 3014 3015 /* 3016 * Non-isolated types over MIGRATE_PCPTYPES get added 3017 * to the MIGRATE_MOVABLE pcp list. 3018 */ 3019 if (unlikely(migratetype >= MIGRATE_PCPTYPES)) 3020 migratetype = MIGRATE_MOVABLE; 3021 3022 trace_mm_page_free_batched(&folio->page); 3023 free_frozen_page_commit(zone, pcp, &folio->page, migratetype, 3024 order, FPI_NONE); 3025 } 3026 3027 if (pcp) { 3028 pcp_spin_unlock(pcp); 3029 pcp_trylock_finish(UP_flags); 3030 } 3031 folio_batch_reinit(folios); 3032 } 3033 3034 /* 3035 * split_page takes a non-compound higher-order page, and splits it into 3036 * n (1<<order) sub-pages: page[0..n] 3037 * Each sub-page must be freed individually. 3038 * 3039 * Note: this is probably too low level an operation for use in drivers. 3040 * Please consult with lkml before using this in your driver. 3041 */ 3042 void split_page(struct page *page, unsigned int order) 3043 { 3044 int i; 3045 3046 VM_BUG_ON_PAGE(PageCompound(page), page); 3047 VM_BUG_ON_PAGE(!page_count(page), page); 3048 3049 for (i = 1; i < (1 << order); i++) 3050 set_page_refcounted(page + i); 3051 split_page_owner(page, order, 0); 3052 pgalloc_tag_split(page_folio(page), order, 0); 3053 split_page_memcg(page, order); 3054 } 3055 EXPORT_SYMBOL_GPL(split_page); 3056 3057 int __isolate_free_page(struct page *page, unsigned int order) 3058 { 3059 struct zone *zone = page_zone(page); 3060 int mt = get_pageblock_migratetype(page); 3061 3062 if (!is_migrate_isolate(mt)) { 3063 unsigned long watermark; 3064 /* 3065 * Obey watermarks as if the page was being allocated. We can 3066 * emulate a high-order watermark check with a raised order-0 3067 * watermark, because we already know our high-order page 3068 * exists. 3069 */ 3070 watermark = zone->_watermark[WMARK_MIN] + (1UL << order); 3071 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) 3072 return 0; 3073 } 3074 3075 del_page_from_free_list(page, zone, order, mt); 3076 3077 /* 3078 * Set the pageblock if the isolated page is at least half of a 3079 * pageblock 3080 */ 3081 if (order >= pageblock_order - 1) { 3082 struct page *endpage = page + (1 << order) - 1; 3083 for (; page < endpage; page += pageblock_nr_pages) { 3084 int mt = get_pageblock_migratetype(page); 3085 /* 3086 * Only change normal pageblocks (i.e., they can merge 3087 * with others) 3088 */ 3089 if (migratetype_is_mergeable(mt)) 3090 move_freepages_block(zone, page, mt, 3091 MIGRATE_MOVABLE); 3092 } 3093 } 3094 3095 return 1UL << order; 3096 } 3097 3098 /** 3099 * __putback_isolated_page - Return a now-isolated page back where we got it 3100 * @page: Page that was isolated 3101 * @order: Order of the isolated page 3102 * @mt: The page's pageblock's migratetype 3103 * 3104 * This function is meant to return a page pulled from the free lists via 3105 * __isolate_free_page back to the free lists they were pulled from. 3106 */ 3107 void __putback_isolated_page(struct page *page, unsigned int order, int mt) 3108 { 3109 struct zone *zone = page_zone(page); 3110 3111 /* zone lock should be held when this function is called */ 3112 lockdep_assert_held(&zone->lock); 3113 3114 /* Return isolated page to tail of freelist. */ 3115 __free_one_page(page, page_to_pfn(page), zone, order, mt, 3116 FPI_SKIP_REPORT_NOTIFY | FPI_TO_TAIL); 3117 } 3118 3119 /* 3120 * Update NUMA hit/miss statistics 3121 */ 3122 static inline void zone_statistics(struct zone *preferred_zone, struct zone *z, 3123 long nr_account) 3124 { 3125 #ifdef CONFIG_NUMA 3126 enum numa_stat_item local_stat = NUMA_LOCAL; 3127 3128 /* skip numa counters update if numa stats is disabled */ 3129 if (!static_branch_likely(&vm_numa_stat_key)) 3130 return; 3131 3132 if (zone_to_nid(z) != numa_node_id()) 3133 local_stat = NUMA_OTHER; 3134 3135 if (zone_to_nid(z) == zone_to_nid(preferred_zone)) 3136 __count_numa_events(z, NUMA_HIT, nr_account); 3137 else { 3138 __count_numa_events(z, NUMA_MISS, nr_account); 3139 __count_numa_events(preferred_zone, NUMA_FOREIGN, nr_account); 3140 } 3141 __count_numa_events(z, local_stat, nr_account); 3142 #endif 3143 } 3144 3145 static __always_inline 3146 struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone, 3147 unsigned int order, unsigned int alloc_flags, 3148 int migratetype) 3149 { 3150 struct page *page; 3151 unsigned long flags; 3152 3153 do { 3154 page = NULL; 3155 if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { 3156 if (!spin_trylock_irqsave(&zone->lock, flags)) 3157 return NULL; 3158 } else { 3159 spin_lock_irqsave(&zone->lock, flags); 3160 } 3161 if (alloc_flags & ALLOC_HIGHATOMIC) 3162 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); 3163 if (!page) { 3164 enum rmqueue_mode rmqm = RMQUEUE_NORMAL; 3165 3166 page = __rmqueue(zone, order, migratetype, alloc_flags, &rmqm); 3167 3168 /* 3169 * If the allocation fails, allow OOM handling and 3170 * order-0 (atomic) allocs access to HIGHATOMIC 3171 * reserves as failing now is worse than failing a 3172 * high-order atomic allocation in the future. 3173 */ 3174 if (!page && (alloc_flags & (ALLOC_OOM|ALLOC_NON_BLOCK))) 3175 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); 3176 3177 if (!page) { 3178 spin_unlock_irqrestore(&zone->lock, flags); 3179 return NULL; 3180 } 3181 } 3182 spin_unlock_irqrestore(&zone->lock, flags); 3183 } while (check_new_pages(page, order)); 3184 3185 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); 3186 zone_statistics(preferred_zone, zone, 1); 3187 3188 return page; 3189 } 3190 3191 static int nr_pcp_alloc(struct per_cpu_pages *pcp, struct zone *zone, int order) 3192 { 3193 int high, base_batch, batch, max_nr_alloc; 3194 int high_max, high_min; 3195 3196 base_batch = READ_ONCE(pcp->batch); 3197 high_min = READ_ONCE(pcp->high_min); 3198 high_max = READ_ONCE(pcp->high_max); 3199 high = pcp->high = clamp(pcp->high, high_min, high_max); 3200 3201 /* Check for PCP disabled or boot pageset */ 3202 if (unlikely(high < base_batch)) 3203 return 1; 3204 3205 if (order) 3206 batch = base_batch; 3207 else 3208 batch = (base_batch << pcp->alloc_factor); 3209 3210 /* 3211 * If we had larger pcp->high, we could avoid to allocate from 3212 * zone. 3213 */ 3214 if (high_min != high_max && !test_bit(ZONE_BELOW_HIGH, &zone->flags)) 3215 high = pcp->high = min(high + batch, high_max); 3216 3217 if (!order) { 3218 max_nr_alloc = max(high - pcp->count - base_batch, base_batch); 3219 /* 3220 * Double the number of pages allocated each time there is 3221 * subsequent allocation of order-0 pages without any freeing. 3222 */ 3223 if (batch <= max_nr_alloc && 3224 pcp->alloc_factor < CONFIG_PCP_BATCH_SCALE_MAX) 3225 pcp->alloc_factor++; 3226 batch = min(batch, max_nr_alloc); 3227 } 3228 3229 /* 3230 * Scale batch relative to order if batch implies free pages 3231 * can be stored on the PCP. Batch can be 1 for small zones or 3232 * for boot pagesets which should never store free pages as 3233 * the pages may belong to arbitrary zones. 3234 */ 3235 if (batch > 1) 3236 batch = max(batch >> order, 2); 3237 3238 return batch; 3239 } 3240 3241 /* Remove page from the per-cpu list, caller must protect the list */ 3242 static inline 3243 struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, 3244 int migratetype, 3245 unsigned int alloc_flags, 3246 struct per_cpu_pages *pcp, 3247 struct list_head *list) 3248 { 3249 struct page *page; 3250 3251 do { 3252 if (list_empty(list)) { 3253 int batch = nr_pcp_alloc(pcp, zone, order); 3254 int alloced; 3255 3256 alloced = rmqueue_bulk(zone, order, 3257 batch, list, 3258 migratetype, alloc_flags); 3259 3260 pcp->count += alloced << order; 3261 if (unlikely(list_empty(list))) 3262 return NULL; 3263 } 3264 3265 page = list_first_entry(list, struct page, pcp_list); 3266 list_del(&page->pcp_list); 3267 pcp->count -= 1 << order; 3268 } while (check_new_pages(page, order)); 3269 3270 return page; 3271 } 3272 3273 /* Lock and remove page from the per-cpu list */ 3274 static struct page *rmqueue_pcplist(struct zone *preferred_zone, 3275 struct zone *zone, unsigned int order, 3276 int migratetype, unsigned int alloc_flags) 3277 { 3278 struct per_cpu_pages *pcp; 3279 struct list_head *list; 3280 struct page *page; 3281 unsigned long __maybe_unused UP_flags; 3282 3283 /* spin_trylock may fail due to a parallel drain or IRQ reentrancy. */ 3284 pcp_trylock_prepare(UP_flags); 3285 pcp = pcp_spin_trylock(zone->per_cpu_pageset); 3286 if (!pcp) { 3287 pcp_trylock_finish(UP_flags); 3288 return NULL; 3289 } 3290 3291 /* 3292 * On allocation, reduce the number of pages that are batch freed. 3293 * See nr_pcp_free() where free_factor is increased for subsequent 3294 * frees. 3295 */ 3296 pcp->free_count >>= 1; 3297 list = &pcp->lists[order_to_pindex(migratetype, order)]; 3298 page = __rmqueue_pcplist(zone, order, migratetype, alloc_flags, pcp, list); 3299 pcp_spin_unlock(pcp); 3300 pcp_trylock_finish(UP_flags); 3301 if (page) { 3302 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); 3303 zone_statistics(preferred_zone, zone, 1); 3304 } 3305 return page; 3306 } 3307 3308 /* 3309 * Allocate a page from the given zone. 3310 * Use pcplists for THP or "cheap" high-order allocations. 3311 */ 3312 3313 /* 3314 * Do not instrument rmqueue() with KMSAN. This function may call 3315 * __msan_poison_alloca() through a call to set_pfnblock_migratetype(). 3316 * If __msan_poison_alloca() attempts to allocate pages for the stack depot, it 3317 * may call rmqueue() again, which will result in a deadlock. 3318 */ 3319 __no_sanitize_memory 3320 static inline 3321 struct page *rmqueue(struct zone *preferred_zone, 3322 struct zone *zone, unsigned int order, 3323 gfp_t gfp_flags, unsigned int alloc_flags, 3324 int migratetype) 3325 { 3326 struct page *page; 3327 3328 if (likely(pcp_allowed_order(order))) { 3329 page = rmqueue_pcplist(preferred_zone, zone, order, 3330 migratetype, alloc_flags); 3331 if (likely(page)) 3332 goto out; 3333 } 3334 3335 page = rmqueue_buddy(preferred_zone, zone, order, alloc_flags, 3336 migratetype); 3337 3338 out: 3339 /* Separate test+clear to avoid unnecessary atomics */ 3340 if ((alloc_flags & ALLOC_KSWAPD) && 3341 unlikely(test_bit(ZONE_BOOSTED_WATERMARK, &zone->flags))) { 3342 clear_bit(ZONE_BOOSTED_WATERMARK, &zone->flags); 3343 wakeup_kswapd(zone, 0, 0, zone_idx(zone)); 3344 } 3345 3346 VM_BUG_ON_PAGE(page && bad_range(zone, page), page); 3347 return page; 3348 } 3349 3350 /* 3351 * Reserve the pageblock(s) surrounding an allocation request for 3352 * exclusive use of high-order atomic allocations if there are no 3353 * empty page blocks that contain a page with a suitable order 3354 */ 3355 static void reserve_highatomic_pageblock(struct page *page, int order, 3356 struct zone *zone) 3357 { 3358 int mt; 3359 unsigned long max_managed, flags; 3360 3361 /* 3362 * The number reserved as: minimum is 1 pageblock, maximum is 3363 * roughly 1% of a zone. But if 1% of a zone falls below a 3364 * pageblock size, then don't reserve any pageblocks. 3365 * Check is race-prone but harmless. 3366 */ 3367 if ((zone_managed_pages(zone) / 100) < pageblock_nr_pages) 3368 return; 3369 max_managed = ALIGN((zone_managed_pages(zone) / 100), pageblock_nr_pages); 3370 if (zone->nr_reserved_highatomic >= max_managed) 3371 return; 3372 3373 spin_lock_irqsave(&zone->lock, flags); 3374 3375 /* Recheck the nr_reserved_highatomic limit under the lock */ 3376 if (zone->nr_reserved_highatomic >= max_managed) 3377 goto out_unlock; 3378 3379 /* Yoink! */ 3380 mt = get_pageblock_migratetype(page); 3381 /* Only reserve normal pageblocks (i.e., they can merge with others) */ 3382 if (!migratetype_is_mergeable(mt)) 3383 goto out_unlock; 3384 3385 if (order < pageblock_order) { 3386 if (move_freepages_block(zone, page, mt, MIGRATE_HIGHATOMIC) == -1) 3387 goto out_unlock; 3388 zone->nr_reserved_highatomic += pageblock_nr_pages; 3389 } else { 3390 change_pageblock_range(page, order, MIGRATE_HIGHATOMIC); 3391 zone->nr_reserved_highatomic += 1 << order; 3392 } 3393 3394 out_unlock: 3395 spin_unlock_irqrestore(&zone->lock, flags); 3396 } 3397 3398 /* 3399 * Used when an allocation is about to fail under memory pressure. This 3400 * potentially hurts the reliability of high-order allocations when under 3401 * intense memory pressure but failed atomic allocations should be easier 3402 * to recover from than an OOM. 3403 * 3404 * If @force is true, try to unreserve pageblocks even though highatomic 3405 * pageblock is exhausted. 3406 */ 3407 static bool unreserve_highatomic_pageblock(const struct alloc_context *ac, 3408 bool force) 3409 { 3410 struct zonelist *zonelist = ac->zonelist; 3411 unsigned long flags; 3412 struct zoneref *z; 3413 struct zone *zone; 3414 struct page *page; 3415 int order; 3416 int ret; 3417 3418 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->highest_zoneidx, 3419 ac->nodemask) { 3420 /* 3421 * Preserve at least one pageblock unless memory pressure 3422 * is really high. 3423 */ 3424 if (!force && zone->nr_reserved_highatomic <= 3425 pageblock_nr_pages) 3426 continue; 3427 3428 spin_lock_irqsave(&zone->lock, flags); 3429 for (order = 0; order < NR_PAGE_ORDERS; order++) { 3430 struct free_area *area = &(zone->free_area[order]); 3431 unsigned long size; 3432 3433 page = get_page_from_free_area(area, MIGRATE_HIGHATOMIC); 3434 if (!page) 3435 continue; 3436 3437 size = max(pageblock_nr_pages, 1UL << order); 3438 /* 3439 * It should never happen but changes to 3440 * locking could inadvertently allow a per-cpu 3441 * drain to add pages to MIGRATE_HIGHATOMIC 3442 * while unreserving so be safe and watch for 3443 * underflows. 3444 */ 3445 if (WARN_ON_ONCE(size > zone->nr_reserved_highatomic)) 3446 size = zone->nr_reserved_highatomic; 3447 zone->nr_reserved_highatomic -= size; 3448 3449 /* 3450 * Convert to ac->migratetype and avoid the normal 3451 * pageblock stealing heuristics. Minimally, the caller 3452 * is doing the work and needs the pages. More 3453 * importantly, if the block was always converted to 3454 * MIGRATE_UNMOVABLE or another type then the number 3455 * of pageblocks that cannot be completely freed 3456 * may increase. 3457 */ 3458 if (order < pageblock_order) 3459 ret = move_freepages_block(zone, page, 3460 MIGRATE_HIGHATOMIC, 3461 ac->migratetype); 3462 else { 3463 move_to_free_list(page, zone, order, 3464 MIGRATE_HIGHATOMIC, 3465 ac->migratetype); 3466 change_pageblock_range(page, order, 3467 ac->migratetype); 3468 ret = 1; 3469 } 3470 /* 3471 * Reserving the block(s) already succeeded, 3472 * so this should not fail on zone boundaries. 3473 */ 3474 WARN_ON_ONCE(ret == -1); 3475 if (ret > 0) { 3476 spin_unlock_irqrestore(&zone->lock, flags); 3477 return ret; 3478 } 3479 } 3480 spin_unlock_irqrestore(&zone->lock, flags); 3481 } 3482 3483 return false; 3484 } 3485 3486 static inline long __zone_watermark_unusable_free(struct zone *z, 3487 unsigned int order, unsigned int alloc_flags) 3488 { 3489 long unusable_free = (1 << order) - 1; 3490 3491 /* 3492 * If the caller does not have rights to reserves below the min 3493 * watermark then subtract the free pages reserved for highatomic. 3494 */ 3495 if (likely(!(alloc_flags & ALLOC_RESERVES))) 3496 unusable_free += READ_ONCE(z->nr_free_highatomic); 3497 3498 #ifdef CONFIG_CMA 3499 /* If allocation can't use CMA areas don't use free CMA pages */ 3500 if (!(alloc_flags & ALLOC_CMA)) 3501 unusable_free += zone_page_state(z, NR_FREE_CMA_PAGES); 3502 #endif 3503 3504 return unusable_free; 3505 } 3506 3507 /* 3508 * Return true if free base pages are above 'mark'. For high-order checks it 3509 * will return true of the order-0 watermark is reached and there is at least 3510 * one free page of a suitable size. Checking now avoids taking the zone lock 3511 * to check in the allocation paths if no pages are free. 3512 */ 3513 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, 3514 int highest_zoneidx, unsigned int alloc_flags, 3515 long free_pages) 3516 { 3517 long min = mark; 3518 int o; 3519 3520 /* free_pages may go negative - that's OK */ 3521 free_pages -= __zone_watermark_unusable_free(z, order, alloc_flags); 3522 3523 if (unlikely(alloc_flags & ALLOC_RESERVES)) { 3524 /* 3525 * __GFP_HIGH allows access to 50% of the min reserve as well 3526 * as OOM. 3527 */ 3528 if (alloc_flags & ALLOC_MIN_RESERVE) { 3529 min -= min / 2; 3530 3531 /* 3532 * Non-blocking allocations (e.g. GFP_ATOMIC) can 3533 * access more reserves than just __GFP_HIGH. Other 3534 * non-blocking allocations requests such as GFP_NOWAIT 3535 * or (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) do not get 3536 * access to the min reserve. 3537 */ 3538 if (alloc_flags & ALLOC_NON_BLOCK) 3539 min -= min / 4; 3540 } 3541 3542 /* 3543 * OOM victims can try even harder than the normal reserve 3544 * users on the grounds that it's definitely going to be in 3545 * the exit path shortly and free memory. Any allocation it 3546 * makes during the free path will be small and short-lived. 3547 */ 3548 if (alloc_flags & ALLOC_OOM) 3549 min -= min / 2; 3550 } 3551 3552 /* 3553 * Check watermarks for an order-0 allocation request. If these 3554 * are not met, then a high-order request also cannot go ahead 3555 * even if a suitable page happened to be free. 3556 */ 3557 if (free_pages <= min + z->lowmem_reserve[highest_zoneidx]) 3558 return false; 3559 3560 /* If this is an order-0 request then the watermark is fine */ 3561 if (!order) 3562 return true; 3563 3564 /* For a high-order request, check at least one suitable page is free */ 3565 for (o = order; o < NR_PAGE_ORDERS; o++) { 3566 struct free_area *area = &z->free_area[o]; 3567 int mt; 3568 3569 if (!area->nr_free) 3570 continue; 3571 3572 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) { 3573 if (!free_area_empty(area, mt)) 3574 return true; 3575 } 3576 3577 #ifdef CONFIG_CMA 3578 if ((alloc_flags & ALLOC_CMA) && 3579 !free_area_empty(area, MIGRATE_CMA)) { 3580 return true; 3581 } 3582 #endif 3583 if ((alloc_flags & (ALLOC_HIGHATOMIC|ALLOC_OOM)) && 3584 !free_area_empty(area, MIGRATE_HIGHATOMIC)) { 3585 return true; 3586 } 3587 } 3588 return false; 3589 } 3590 3591 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, 3592 int highest_zoneidx, unsigned int alloc_flags) 3593 { 3594 return __zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags, 3595 zone_page_state(z, NR_FREE_PAGES)); 3596 } 3597 3598 static inline bool zone_watermark_fast(struct zone *z, unsigned int order, 3599 unsigned long mark, int highest_zoneidx, 3600 unsigned int alloc_flags, gfp_t gfp_mask) 3601 { 3602 long free_pages; 3603 3604 free_pages = zone_page_state(z, NR_FREE_PAGES); 3605 3606 /* 3607 * Fast check for order-0 only. If this fails then the reserves 3608 * need to be calculated. 3609 */ 3610 if (!order) { 3611 long usable_free; 3612 long reserved; 3613 3614 usable_free = free_pages; 3615 reserved = __zone_watermark_unusable_free(z, 0, alloc_flags); 3616 3617 /* reserved may over estimate high-atomic reserves. */ 3618 usable_free -= min(usable_free, reserved); 3619 if (usable_free > mark + z->lowmem_reserve[highest_zoneidx]) 3620 return true; 3621 } 3622 3623 if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags, 3624 free_pages)) 3625 return true; 3626 3627 /* 3628 * Ignore watermark boosting for __GFP_HIGH order-0 allocations 3629 * when checking the min watermark. The min watermark is the 3630 * point where boosting is ignored so that kswapd is woken up 3631 * when below the low watermark. 3632 */ 3633 if (unlikely(!order && (alloc_flags & ALLOC_MIN_RESERVE) && z->watermark_boost 3634 && ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) { 3635 mark = z->_watermark[WMARK_MIN]; 3636 return __zone_watermark_ok(z, order, mark, highest_zoneidx, 3637 alloc_flags, free_pages); 3638 } 3639 3640 return false; 3641 } 3642 3643 #ifdef CONFIG_NUMA 3644 int __read_mostly node_reclaim_distance = RECLAIM_DISTANCE; 3645 3646 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) 3647 { 3648 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <= 3649 node_reclaim_distance; 3650 } 3651 #else /* CONFIG_NUMA */ 3652 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) 3653 { 3654 return true; 3655 } 3656 #endif /* CONFIG_NUMA */ 3657 3658 /* 3659 * The restriction on ZONE_DMA32 as being a suitable zone to use to avoid 3660 * fragmentation is subtle. If the preferred zone was HIGHMEM then 3661 * premature use of a lower zone may cause lowmem pressure problems that 3662 * are worse than fragmentation. If the next zone is ZONE_DMA then it is 3663 * probably too small. It only makes sense to spread allocations to avoid 3664 * fragmentation between the Normal and DMA32 zones. 3665 */ 3666 static inline unsigned int 3667 alloc_flags_nofragment(struct zone *zone, gfp_t gfp_mask) 3668 { 3669 unsigned int alloc_flags; 3670 3671 /* 3672 * __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD 3673 * to save a branch. 3674 */ 3675 alloc_flags = (__force int) (gfp_mask & __GFP_KSWAPD_RECLAIM); 3676 3677 if (defrag_mode) { 3678 alloc_flags |= ALLOC_NOFRAGMENT; 3679 return alloc_flags; 3680 } 3681 3682 #ifdef CONFIG_ZONE_DMA32 3683 if (!zone) 3684 return alloc_flags; 3685 3686 if (zone_idx(zone) != ZONE_NORMAL) 3687 return alloc_flags; 3688 3689 /* 3690 * If ZONE_DMA32 exists, assume it is the one after ZONE_NORMAL and 3691 * the pointer is within zone->zone_pgdat->node_zones[]. Also assume 3692 * on UMA that if Normal is populated then so is DMA32. 3693 */ 3694 BUILD_BUG_ON(ZONE_NORMAL - ZONE_DMA32 != 1); 3695 if (nr_online_nodes > 1 && !populated_zone(--zone)) 3696 return alloc_flags; 3697 3698 alloc_flags |= ALLOC_NOFRAGMENT; 3699 #endif /* CONFIG_ZONE_DMA32 */ 3700 return alloc_flags; 3701 } 3702 3703 /* Must be called after current_gfp_context() which can change gfp_mask */ 3704 static inline unsigned int gfp_to_alloc_flags_cma(gfp_t gfp_mask, 3705 unsigned int alloc_flags) 3706 { 3707 #ifdef CONFIG_CMA 3708 if (gfp_migratetype(gfp_mask) == MIGRATE_MOVABLE) 3709 alloc_flags |= ALLOC_CMA; 3710 #endif 3711 return alloc_flags; 3712 } 3713 3714 /* 3715 * get_page_from_freelist goes through the zonelist trying to allocate 3716 * a page. 3717 */ 3718 static struct page * 3719 get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, 3720 const struct alloc_context *ac) 3721 { 3722 struct zoneref *z; 3723 struct zone *zone; 3724 struct pglist_data *last_pgdat = NULL; 3725 bool last_pgdat_dirty_ok = false; 3726 bool no_fallback; 3727 3728 retry: 3729 /* 3730 * Scan zonelist, looking for a zone with enough free. 3731 * See also cpuset_current_node_allowed() comment in kernel/cgroup/cpuset.c. 3732 */ 3733 no_fallback = alloc_flags & ALLOC_NOFRAGMENT; 3734 z = ac->preferred_zoneref; 3735 for_next_zone_zonelist_nodemask(zone, z, ac->highest_zoneidx, 3736 ac->nodemask) { 3737 struct page *page; 3738 unsigned long mark; 3739 3740 if (cpusets_enabled() && 3741 (alloc_flags & ALLOC_CPUSET) && 3742 !__cpuset_zone_allowed(zone, gfp_mask)) 3743 continue; 3744 /* 3745 * When allocating a page cache page for writing, we 3746 * want to get it from a node that is within its dirty 3747 * limit, such that no single node holds more than its 3748 * proportional share of globally allowed dirty pages. 3749 * The dirty limits take into account the node's 3750 * lowmem reserves and high watermark so that kswapd 3751 * should be able to balance it without having to 3752 * write pages from its LRU list. 3753 * 3754 * XXX: For now, allow allocations to potentially 3755 * exceed the per-node dirty limit in the slowpath 3756 * (spread_dirty_pages unset) before going into reclaim, 3757 * which is important when on a NUMA setup the allowed 3758 * nodes are together not big enough to reach the 3759 * global limit. The proper fix for these situations 3760 * will require awareness of nodes in the 3761 * dirty-throttling and the flusher threads. 3762 */ 3763 if (ac->spread_dirty_pages) { 3764 if (last_pgdat != zone->zone_pgdat) { 3765 last_pgdat = zone->zone_pgdat; 3766 last_pgdat_dirty_ok = node_dirty_ok(zone->zone_pgdat); 3767 } 3768 3769 if (!last_pgdat_dirty_ok) 3770 continue; 3771 } 3772 3773 if (no_fallback && !defrag_mode && nr_online_nodes > 1 && 3774 zone != zonelist_zone(ac->preferred_zoneref)) { 3775 int local_nid; 3776 3777 /* 3778 * If moving to a remote node, retry but allow 3779 * fragmenting fallbacks. Locality is more important 3780 * than fragmentation avoidance. 3781 */ 3782 local_nid = zonelist_node_idx(ac->preferred_zoneref); 3783 if (zone_to_nid(zone) != local_nid) { 3784 alloc_flags &= ~ALLOC_NOFRAGMENT; 3785 goto retry; 3786 } 3787 } 3788 3789 cond_accept_memory(zone, order, alloc_flags); 3790 3791 /* 3792 * Detect whether the number of free pages is below high 3793 * watermark. If so, we will decrease pcp->high and free 3794 * PCP pages in free path to reduce the possibility of 3795 * premature page reclaiming. Detection is done here to 3796 * avoid to do that in hotter free path. 3797 */ 3798 if (test_bit(ZONE_BELOW_HIGH, &zone->flags)) 3799 goto check_alloc_wmark; 3800 3801 mark = high_wmark_pages(zone); 3802 if (zone_watermark_fast(zone, order, mark, 3803 ac->highest_zoneidx, alloc_flags, 3804 gfp_mask)) 3805 goto try_this_zone; 3806 else 3807 set_bit(ZONE_BELOW_HIGH, &zone->flags); 3808 3809 check_alloc_wmark: 3810 mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); 3811 if (!zone_watermark_fast(zone, order, mark, 3812 ac->highest_zoneidx, alloc_flags, 3813 gfp_mask)) { 3814 int ret; 3815 3816 if (cond_accept_memory(zone, order, alloc_flags)) 3817 goto try_this_zone; 3818 3819 /* 3820 * Watermark failed for this zone, but see if we can 3821 * grow this zone if it contains deferred pages. 3822 */ 3823 if (deferred_pages_enabled()) { 3824 if (_deferred_grow_zone(zone, order)) 3825 goto try_this_zone; 3826 } 3827 /* Checked here to keep the fast path fast */ 3828 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK); 3829 if (alloc_flags & ALLOC_NO_WATERMARKS) 3830 goto try_this_zone; 3831 3832 if (!node_reclaim_enabled() || 3833 !zone_allows_reclaim(zonelist_zone(ac->preferred_zoneref), zone)) 3834 continue; 3835 3836 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order); 3837 switch (ret) { 3838 case NODE_RECLAIM_NOSCAN: 3839 /* did not scan */ 3840 continue; 3841 case NODE_RECLAIM_FULL: 3842 /* scanned but unreclaimable */ 3843 continue; 3844 default: 3845 /* did we reclaim enough */ 3846 if (zone_watermark_ok(zone, order, mark, 3847 ac->highest_zoneidx, alloc_flags)) 3848 goto try_this_zone; 3849 3850 continue; 3851 } 3852 } 3853 3854 try_this_zone: 3855 page = rmqueue(zonelist_zone(ac->preferred_zoneref), zone, order, 3856 gfp_mask, alloc_flags, ac->migratetype); 3857 if (page) { 3858 prep_new_page(page, order, gfp_mask, alloc_flags); 3859 3860 /* 3861 * If this is a high-order atomic allocation then check 3862 * if the pageblock should be reserved for the future 3863 */ 3864 if (unlikely(alloc_flags & ALLOC_HIGHATOMIC)) 3865 reserve_highatomic_pageblock(page, order, zone); 3866 3867 return page; 3868 } else { 3869 if (cond_accept_memory(zone, order, alloc_flags)) 3870 goto try_this_zone; 3871 3872 /* Try again if zone has deferred pages */ 3873 if (deferred_pages_enabled()) { 3874 if (_deferred_grow_zone(zone, order)) 3875 goto try_this_zone; 3876 } 3877 } 3878 } 3879 3880 /* 3881 * It's possible on a UMA machine to get through all zones that are 3882 * fragmented. If avoiding fragmentation, reset and try again. 3883 */ 3884 if (no_fallback && !defrag_mode) { 3885 alloc_flags &= ~ALLOC_NOFRAGMENT; 3886 goto retry; 3887 } 3888 3889 return NULL; 3890 } 3891 3892 static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask) 3893 { 3894 unsigned int filter = SHOW_MEM_FILTER_NODES; 3895 3896 /* 3897 * This documents exceptions given to allocations in certain 3898 * contexts that are allowed to allocate outside current's set 3899 * of allowed nodes. 3900 */ 3901 if (!(gfp_mask & __GFP_NOMEMALLOC)) 3902 if (tsk_is_oom_victim(current) || 3903 (current->flags & (PF_MEMALLOC | PF_EXITING))) 3904 filter &= ~SHOW_MEM_FILTER_NODES; 3905 if (!in_task() || !(gfp_mask & __GFP_DIRECT_RECLAIM)) 3906 filter &= ~SHOW_MEM_FILTER_NODES; 3907 3908 __show_mem(filter, nodemask, gfp_zone(gfp_mask)); 3909 } 3910 3911 void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...) 3912 { 3913 struct va_format vaf; 3914 va_list args; 3915 static DEFINE_RATELIMIT_STATE(nopage_rs, 10*HZ, 1); 3916 3917 if ((gfp_mask & __GFP_NOWARN) || 3918 !__ratelimit(&nopage_rs) || 3919 ((gfp_mask & __GFP_DMA) && !has_managed_dma())) 3920 return; 3921 3922 va_start(args, fmt); 3923 vaf.fmt = fmt; 3924 vaf.va = &args; 3925 pr_warn("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl", 3926 current->comm, &vaf, gfp_mask, &gfp_mask, 3927 nodemask_pr_args(nodemask)); 3928 va_end(args); 3929 3930 cpuset_print_current_mems_allowed(); 3931 pr_cont("\n"); 3932 dump_stack(); 3933 warn_alloc_show_mem(gfp_mask, nodemask); 3934 } 3935 3936 static inline struct page * 3937 __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order, 3938 unsigned int alloc_flags, 3939 const struct alloc_context *ac) 3940 { 3941 struct page *page; 3942 3943 page = get_page_from_freelist(gfp_mask, order, 3944 alloc_flags|ALLOC_CPUSET, ac); 3945 /* 3946 * fallback to ignore cpuset restriction if our nodes 3947 * are depleted 3948 */ 3949 if (!page) 3950 page = get_page_from_freelist(gfp_mask, order, 3951 alloc_flags, ac); 3952 return page; 3953 } 3954 3955 static inline struct page * 3956 __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, 3957 const struct alloc_context *ac, unsigned long *did_some_progress) 3958 { 3959 struct oom_control oc = { 3960 .zonelist = ac->zonelist, 3961 .nodemask = ac->nodemask, 3962 .memcg = NULL, 3963 .gfp_mask = gfp_mask, 3964 .order = order, 3965 }; 3966 struct page *page; 3967 3968 *did_some_progress = 0; 3969 3970 /* 3971 * Acquire the oom lock. If that fails, somebody else is 3972 * making progress for us. 3973 */ 3974 if (!mutex_trylock(&oom_lock)) { 3975 *did_some_progress = 1; 3976 schedule_timeout_uninterruptible(1); 3977 return NULL; 3978 } 3979 3980 /* 3981 * Go through the zonelist yet one more time, keep very high watermark 3982 * here, this is only to catch a parallel oom killing, we must fail if 3983 * we're still under heavy pressure. But make sure that this reclaim 3984 * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY 3985 * allocation which will never fail due to oom_lock already held. 3986 */ 3987 page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) & 3988 ~__GFP_DIRECT_RECLAIM, order, 3989 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac); 3990 if (page) 3991 goto out; 3992 3993 /* Coredumps can quickly deplete all memory reserves */ 3994 if (current->flags & PF_DUMPCORE) 3995 goto out; 3996 /* The OOM killer will not help higher order allocs */ 3997 if (order > PAGE_ALLOC_COSTLY_ORDER) 3998 goto out; 3999 /* 4000 * We have already exhausted all our reclaim opportunities without any 4001 * success so it is time to admit defeat. We will skip the OOM killer 4002 * because it is very likely that the caller has a more reasonable 4003 * fallback than shooting a random task. 4004 * 4005 * The OOM killer may not free memory on a specific node. 4006 */ 4007 if (gfp_mask & (__GFP_RETRY_MAYFAIL | __GFP_THISNODE)) 4008 goto out; 4009 /* The OOM killer does not needlessly kill tasks for lowmem */ 4010 if (ac->highest_zoneidx < ZONE_NORMAL) 4011 goto out; 4012 if (pm_suspended_storage()) 4013 goto out; 4014 /* 4015 * XXX: GFP_NOFS allocations should rather fail than rely on 4016 * other request to make a forward progress. 4017 * We are in an unfortunate situation where out_of_memory cannot 4018 * do much for this context but let's try it to at least get 4019 * access to memory reserved if the current task is killed (see 4020 * out_of_memory). Once filesystems are ready to handle allocation 4021 * failures more gracefully we should just bail out here. 4022 */ 4023 4024 /* Exhausted what can be done so it's blame time */ 4025 if (out_of_memory(&oc) || 4026 WARN_ON_ONCE_GFP(gfp_mask & __GFP_NOFAIL, gfp_mask)) { 4027 *did_some_progress = 1; 4028 4029 /* 4030 * Help non-failing allocations by giving them access to memory 4031 * reserves 4032 */ 4033 if (gfp_mask & __GFP_NOFAIL) 4034 page = __alloc_pages_cpuset_fallback(gfp_mask, order, 4035 ALLOC_NO_WATERMARKS, ac); 4036 } 4037 out: 4038 mutex_unlock(&oom_lock); 4039 return page; 4040 } 4041 4042 /* 4043 * Maximum number of compaction retries with a progress before OOM 4044 * killer is consider as the only way to move forward. 4045 */ 4046 #define MAX_COMPACT_RETRIES 16 4047 4048 #ifdef CONFIG_COMPACTION 4049 /* Try memory compaction for high-order allocations before reclaim */ 4050 static struct page * 4051 __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, 4052 unsigned int alloc_flags, const struct alloc_context *ac, 4053 enum compact_priority prio, enum compact_result *compact_result) 4054 { 4055 struct page *page = NULL; 4056 unsigned long pflags; 4057 unsigned int noreclaim_flag; 4058 4059 if (!order) 4060 return NULL; 4061 4062 psi_memstall_enter(&pflags); 4063 delayacct_compact_start(); 4064 noreclaim_flag = memalloc_noreclaim_save(); 4065 4066 *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac, 4067 prio, &page); 4068 4069 memalloc_noreclaim_restore(noreclaim_flag); 4070 psi_memstall_leave(&pflags); 4071 delayacct_compact_end(); 4072 4073 if (*compact_result == COMPACT_SKIPPED) 4074 return NULL; 4075 /* 4076 * At least in one zone compaction wasn't deferred or skipped, so let's 4077 * count a compaction stall 4078 */ 4079 count_vm_event(COMPACTSTALL); 4080 4081 /* Prep a captured page if available */ 4082 if (page) 4083 prep_new_page(page, order, gfp_mask, alloc_flags); 4084 4085 /* Try get a page from the freelist if available */ 4086 if (!page) 4087 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); 4088 4089 if (page) { 4090 struct zone *zone = page_zone(page); 4091 4092 zone->compact_blockskip_flush = false; 4093 compaction_defer_reset(zone, order, true); 4094 count_vm_event(COMPACTSUCCESS); 4095 return page; 4096 } 4097 4098 /* 4099 * It's bad if compaction run occurs and fails. The most likely reason 4100 * is that pages exist, but not enough to satisfy watermarks. 4101 */ 4102 count_vm_event(COMPACTFAIL); 4103 4104 cond_resched(); 4105 4106 return NULL; 4107 } 4108 4109 static inline bool 4110 should_compact_retry(struct alloc_context *ac, int order, int alloc_flags, 4111 enum compact_result compact_result, 4112 enum compact_priority *compact_priority, 4113 int *compaction_retries) 4114 { 4115 int max_retries = MAX_COMPACT_RETRIES; 4116 int min_priority; 4117 bool ret = false; 4118 int retries = *compaction_retries; 4119 enum compact_priority priority = *compact_priority; 4120 4121 if (!order) 4122 return false; 4123 4124 if (fatal_signal_pending(current)) 4125 return false; 4126 4127 /* 4128 * Compaction was skipped due to a lack of free order-0 4129 * migration targets. Continue if reclaim can help. 4130 */ 4131 if (compact_result == COMPACT_SKIPPED) { 4132 ret = compaction_zonelist_suitable(ac, order, alloc_flags); 4133 goto out; 4134 } 4135 4136 /* 4137 * Compaction managed to coalesce some page blocks, but the 4138 * allocation failed presumably due to a race. Retry some. 4139 */ 4140 if (compact_result == COMPACT_SUCCESS) { 4141 /* 4142 * !costly requests are much more important than 4143 * __GFP_RETRY_MAYFAIL costly ones because they are de 4144 * facto nofail and invoke OOM killer to move on while 4145 * costly can fail and users are ready to cope with 4146 * that. 1/4 retries is rather arbitrary but we would 4147 * need much more detailed feedback from compaction to 4148 * make a better decision. 4149 */ 4150 if (order > PAGE_ALLOC_COSTLY_ORDER) 4151 max_retries /= 4; 4152 4153 if (++(*compaction_retries) <= max_retries) { 4154 ret = true; 4155 goto out; 4156 } 4157 } 4158 4159 /* 4160 * Compaction failed. Retry with increasing priority. 4161 */ 4162 min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ? 4163 MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY; 4164 4165 if (*compact_priority > min_priority) { 4166 (*compact_priority)--; 4167 *compaction_retries = 0; 4168 ret = true; 4169 } 4170 out: 4171 trace_compact_retry(order, priority, compact_result, retries, max_retries, ret); 4172 return ret; 4173 } 4174 #else 4175 static inline struct page * 4176 __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, 4177 unsigned int alloc_flags, const struct alloc_context *ac, 4178 enum compact_priority prio, enum compact_result *compact_result) 4179 { 4180 *compact_result = COMPACT_SKIPPED; 4181 return NULL; 4182 } 4183 4184 static inline bool 4185 should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags, 4186 enum compact_result compact_result, 4187 enum compact_priority *compact_priority, 4188 int *compaction_retries) 4189 { 4190 struct zone *zone; 4191 struct zoneref *z; 4192 4193 if (!order || order > PAGE_ALLOC_COSTLY_ORDER) 4194 return false; 4195 4196 /* 4197 * There are setups with compaction disabled which would prefer to loop 4198 * inside the allocator rather than hit the oom killer prematurely. 4199 * Let's give them a good hope and keep retrying while the order-0 4200 * watermarks are OK. 4201 */ 4202 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 4203 ac->highest_zoneidx, ac->nodemask) { 4204 if (zone_watermark_ok(zone, 0, min_wmark_pages(zone), 4205 ac->highest_zoneidx, alloc_flags)) 4206 return true; 4207 } 4208 return false; 4209 } 4210 #endif /* CONFIG_COMPACTION */ 4211 4212 #ifdef CONFIG_LOCKDEP 4213 static struct lockdep_map __fs_reclaim_map = 4214 STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map); 4215 4216 static bool __need_reclaim(gfp_t gfp_mask) 4217 { 4218 /* no reclaim without waiting on it */ 4219 if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) 4220 return false; 4221 4222 /* this guy won't enter reclaim */ 4223 if (current->flags & PF_MEMALLOC) 4224 return false; 4225 4226 if (gfp_mask & __GFP_NOLOCKDEP) 4227 return false; 4228 4229 return true; 4230 } 4231 4232 void __fs_reclaim_acquire(unsigned long ip) 4233 { 4234 lock_acquire_exclusive(&__fs_reclaim_map, 0, 0, NULL, ip); 4235 } 4236 4237 void __fs_reclaim_release(unsigned long ip) 4238 { 4239 lock_release(&__fs_reclaim_map, ip); 4240 } 4241 4242 void fs_reclaim_acquire(gfp_t gfp_mask) 4243 { 4244 gfp_mask = current_gfp_context(gfp_mask); 4245 4246 if (__need_reclaim(gfp_mask)) { 4247 if (gfp_mask & __GFP_FS) 4248 __fs_reclaim_acquire(_RET_IP_); 4249 4250 #ifdef CONFIG_MMU_NOTIFIER 4251 lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); 4252 lock_map_release(&__mmu_notifier_invalidate_range_start_map); 4253 #endif 4254 4255 } 4256 } 4257 EXPORT_SYMBOL_GPL(fs_reclaim_acquire); 4258 4259 void fs_reclaim_release(gfp_t gfp_mask) 4260 { 4261 gfp_mask = current_gfp_context(gfp_mask); 4262 4263 if (__need_reclaim(gfp_mask)) { 4264 if (gfp_mask & __GFP_FS) 4265 __fs_reclaim_release(_RET_IP_); 4266 } 4267 } 4268 EXPORT_SYMBOL_GPL(fs_reclaim_release); 4269 #endif 4270 4271 /* 4272 * Zonelists may change due to hotplug during allocation. Detect when zonelists 4273 * have been rebuilt so allocation retries. Reader side does not lock and 4274 * retries the allocation if zonelist changes. Writer side is protected by the 4275 * embedded spin_lock. 4276 */ 4277 static DEFINE_SEQLOCK(zonelist_update_seq); 4278 4279 static unsigned int zonelist_iter_begin(void) 4280 { 4281 if (IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 4282 return read_seqbegin(&zonelist_update_seq); 4283 4284 return 0; 4285 } 4286 4287 static unsigned int check_retry_zonelist(unsigned int seq) 4288 { 4289 if (IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 4290 return read_seqretry(&zonelist_update_seq, seq); 4291 4292 return seq; 4293 } 4294 4295 /* Perform direct synchronous page reclaim */ 4296 static unsigned long 4297 __perform_reclaim(gfp_t gfp_mask, unsigned int order, 4298 const struct alloc_context *ac) 4299 { 4300 unsigned int noreclaim_flag; 4301 unsigned long progress; 4302 4303 cond_resched(); 4304 4305 /* We now go into synchronous reclaim */ 4306 cpuset_memory_pressure_bump(); 4307 fs_reclaim_acquire(gfp_mask); 4308 noreclaim_flag = memalloc_noreclaim_save(); 4309 4310 progress = try_to_free_pages(ac->zonelist, order, gfp_mask, 4311 ac->nodemask); 4312 4313 memalloc_noreclaim_restore(noreclaim_flag); 4314 fs_reclaim_release(gfp_mask); 4315 4316 cond_resched(); 4317 4318 return progress; 4319 } 4320 4321 /* The really slow allocator path where we enter direct reclaim */ 4322 static inline struct page * 4323 __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order, 4324 unsigned int alloc_flags, const struct alloc_context *ac, 4325 unsigned long *did_some_progress) 4326 { 4327 struct page *page = NULL; 4328 unsigned long pflags; 4329 bool drained = false; 4330 4331 psi_memstall_enter(&pflags); 4332 *did_some_progress = __perform_reclaim(gfp_mask, order, ac); 4333 if (unlikely(!(*did_some_progress))) 4334 goto out; 4335 4336 retry: 4337 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); 4338 4339 /* 4340 * If an allocation failed after direct reclaim, it could be because 4341 * pages are pinned on the per-cpu lists or in high alloc reserves. 4342 * Shrink them and try again 4343 */ 4344 if (!page && !drained) { 4345 unreserve_highatomic_pageblock(ac, false); 4346 drain_all_pages(NULL); 4347 drained = true; 4348 goto retry; 4349 } 4350 out: 4351 psi_memstall_leave(&pflags); 4352 4353 return page; 4354 } 4355 4356 static void wake_all_kswapds(unsigned int order, gfp_t gfp_mask, 4357 const struct alloc_context *ac) 4358 { 4359 struct zoneref *z; 4360 struct zone *zone; 4361 pg_data_t *last_pgdat = NULL; 4362 enum zone_type highest_zoneidx = ac->highest_zoneidx; 4363 unsigned int reclaim_order; 4364 4365 if (defrag_mode) 4366 reclaim_order = max(order, pageblock_order); 4367 else 4368 reclaim_order = order; 4369 4370 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, highest_zoneidx, 4371 ac->nodemask) { 4372 if (!managed_zone(zone)) 4373 continue; 4374 if (last_pgdat == zone->zone_pgdat) 4375 continue; 4376 wakeup_kswapd(zone, gfp_mask, reclaim_order, highest_zoneidx); 4377 last_pgdat = zone->zone_pgdat; 4378 } 4379 } 4380 4381 static inline unsigned int 4382 gfp_to_alloc_flags(gfp_t gfp_mask, unsigned int order) 4383 { 4384 unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET; 4385 4386 /* 4387 * __GFP_HIGH is assumed to be the same as ALLOC_MIN_RESERVE 4388 * and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD 4389 * to save two branches. 4390 */ 4391 BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_MIN_RESERVE); 4392 BUILD_BUG_ON(__GFP_KSWAPD_RECLAIM != (__force gfp_t) ALLOC_KSWAPD); 4393 4394 /* 4395 * The caller may dip into page reserves a bit more if the caller 4396 * cannot run direct reclaim, or if the caller has realtime scheduling 4397 * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will 4398 * set both ALLOC_NON_BLOCK and ALLOC_MIN_RESERVE(__GFP_HIGH). 4399 */ 4400 alloc_flags |= (__force int) 4401 (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM)); 4402 4403 if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) { 4404 /* 4405 * Not worth trying to allocate harder for __GFP_NOMEMALLOC even 4406 * if it can't schedule. 4407 */ 4408 if (!(gfp_mask & __GFP_NOMEMALLOC)) { 4409 alloc_flags |= ALLOC_NON_BLOCK; 4410 4411 if (order > 0) 4412 alloc_flags |= ALLOC_HIGHATOMIC; 4413 } 4414 4415 /* 4416 * Ignore cpuset mems for non-blocking __GFP_HIGH (probably 4417 * GFP_ATOMIC) rather than fail, see the comment for 4418 * cpuset_current_node_allowed(). 4419 */ 4420 if (alloc_flags & ALLOC_MIN_RESERVE) 4421 alloc_flags &= ~ALLOC_CPUSET; 4422 } else if (unlikely(rt_or_dl_task(current)) && in_task()) 4423 alloc_flags |= ALLOC_MIN_RESERVE; 4424 4425 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, alloc_flags); 4426 4427 if (defrag_mode) 4428 alloc_flags |= ALLOC_NOFRAGMENT; 4429 4430 return alloc_flags; 4431 } 4432 4433 static bool oom_reserves_allowed(struct task_struct *tsk) 4434 { 4435 if (!tsk_is_oom_victim(tsk)) 4436 return false; 4437 4438 /* 4439 * !MMU doesn't have oom reaper so give access to memory reserves 4440 * only to the thread with TIF_MEMDIE set 4441 */ 4442 if (!IS_ENABLED(CONFIG_MMU) && !test_thread_flag(TIF_MEMDIE)) 4443 return false; 4444 4445 return true; 4446 } 4447 4448 /* 4449 * Distinguish requests which really need access to full memory 4450 * reserves from oom victims which can live with a portion of it 4451 */ 4452 static inline int __gfp_pfmemalloc_flags(gfp_t gfp_mask) 4453 { 4454 if (unlikely(gfp_mask & __GFP_NOMEMALLOC)) 4455 return 0; 4456 if (gfp_mask & __GFP_MEMALLOC) 4457 return ALLOC_NO_WATERMARKS; 4458 if (in_serving_softirq() && (current->flags & PF_MEMALLOC)) 4459 return ALLOC_NO_WATERMARKS; 4460 if (!in_interrupt()) { 4461 if (current->flags & PF_MEMALLOC) 4462 return ALLOC_NO_WATERMARKS; 4463 else if (oom_reserves_allowed(current)) 4464 return ALLOC_OOM; 4465 } 4466 4467 return 0; 4468 } 4469 4470 bool gfp_pfmemalloc_allowed(gfp_t gfp_mask) 4471 { 4472 return !!__gfp_pfmemalloc_flags(gfp_mask); 4473 } 4474 4475 /* 4476 * Checks whether it makes sense to retry the reclaim to make a forward progress 4477 * for the given allocation request. 4478 * 4479 * We give up when we either have tried MAX_RECLAIM_RETRIES in a row 4480 * without success, or when we couldn't even meet the watermark if we 4481 * reclaimed all remaining pages on the LRU lists. 4482 * 4483 * Returns true if a retry is viable or false to enter the oom path. 4484 */ 4485 static inline bool 4486 should_reclaim_retry(gfp_t gfp_mask, unsigned order, 4487 struct alloc_context *ac, int alloc_flags, 4488 bool did_some_progress, int *no_progress_loops) 4489 { 4490 struct zone *zone; 4491 struct zoneref *z; 4492 bool ret = false; 4493 4494 /* 4495 * Costly allocations might have made a progress but this doesn't mean 4496 * their order will become available due to high fragmentation so 4497 * always increment the no progress counter for them 4498 */ 4499 if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER) 4500 *no_progress_loops = 0; 4501 else 4502 (*no_progress_loops)++; 4503 4504 if (*no_progress_loops > MAX_RECLAIM_RETRIES) 4505 goto out; 4506 4507 4508 /* 4509 * Keep reclaiming pages while there is a chance this will lead 4510 * somewhere. If none of the target zones can satisfy our allocation 4511 * request even if all reclaimable pages are considered then we are 4512 * screwed and have to go OOM. 4513 */ 4514 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, 4515 ac->highest_zoneidx, ac->nodemask) { 4516 unsigned long available; 4517 unsigned long reclaimable; 4518 unsigned long min_wmark = min_wmark_pages(zone); 4519 bool wmark; 4520 4521 if (cpusets_enabled() && 4522 (alloc_flags & ALLOC_CPUSET) && 4523 !__cpuset_zone_allowed(zone, gfp_mask)) 4524 continue; 4525 4526 available = reclaimable = zone_reclaimable_pages(zone); 4527 available += zone_page_state_snapshot(zone, NR_FREE_PAGES); 4528 4529 /* 4530 * Would the allocation succeed if we reclaimed all 4531 * reclaimable pages? 4532 */ 4533 wmark = __zone_watermark_ok(zone, order, min_wmark, 4534 ac->highest_zoneidx, alloc_flags, available); 4535 trace_reclaim_retry_zone(z, order, reclaimable, 4536 available, min_wmark, *no_progress_loops, wmark); 4537 if (wmark) { 4538 ret = true; 4539 break; 4540 } 4541 } 4542 4543 /* 4544 * Memory allocation/reclaim might be called from a WQ context and the 4545 * current implementation of the WQ concurrency control doesn't 4546 * recognize that a particular WQ is congested if the worker thread is 4547 * looping without ever sleeping. Therefore we have to do a short sleep 4548 * here rather than calling cond_resched(). 4549 */ 4550 if (current->flags & PF_WQ_WORKER) 4551 schedule_timeout_uninterruptible(1); 4552 else 4553 cond_resched(); 4554 out: 4555 /* Before OOM, exhaust highatomic_reserve */ 4556 if (!ret) 4557 return unreserve_highatomic_pageblock(ac, true); 4558 4559 return ret; 4560 } 4561 4562 static inline bool 4563 check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac) 4564 { 4565 /* 4566 * It's possible that cpuset's mems_allowed and the nodemask from 4567 * mempolicy don't intersect. This should be normally dealt with by 4568 * policy_nodemask(), but it's possible to race with cpuset update in 4569 * such a way the check therein was true, and then it became false 4570 * before we got our cpuset_mems_cookie here. 4571 * This assumes that for all allocations, ac->nodemask can come only 4572 * from MPOL_BIND mempolicy (whose documented semantics is to be ignored 4573 * when it does not intersect with the cpuset restrictions) or the 4574 * caller can deal with a violated nodemask. 4575 */ 4576 if (cpusets_enabled() && ac->nodemask && 4577 !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) { 4578 ac->nodemask = NULL; 4579 return true; 4580 } 4581 4582 /* 4583 * When updating a task's mems_allowed or mempolicy nodemask, it is 4584 * possible to race with parallel threads in such a way that our 4585 * allocation can fail while the mask is being updated. If we are about 4586 * to fail, check if the cpuset changed during allocation and if so, 4587 * retry. 4588 */ 4589 if (read_mems_allowed_retry(cpuset_mems_cookie)) 4590 return true; 4591 4592 return false; 4593 } 4594 4595 static inline struct page * 4596 __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, 4597 struct alloc_context *ac) 4598 { 4599 bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM; 4600 bool can_compact = gfp_compaction_allowed(gfp_mask); 4601 bool nofail = gfp_mask & __GFP_NOFAIL; 4602 const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER; 4603 struct page *page = NULL; 4604 unsigned int alloc_flags; 4605 unsigned long did_some_progress; 4606 enum compact_priority compact_priority; 4607 enum compact_result compact_result; 4608 int compaction_retries; 4609 int no_progress_loops; 4610 unsigned int cpuset_mems_cookie; 4611 unsigned int zonelist_iter_cookie; 4612 int reserve_flags; 4613 4614 if (unlikely(nofail)) { 4615 /* 4616 * We most definitely don't want callers attempting to 4617 * allocate greater than order-1 page units with __GFP_NOFAIL. 4618 */ 4619 WARN_ON_ONCE(order > 1); 4620 /* 4621 * Also we don't support __GFP_NOFAIL without __GFP_DIRECT_RECLAIM, 4622 * otherwise, we may result in lockup. 4623 */ 4624 WARN_ON_ONCE(!can_direct_reclaim); 4625 /* 4626 * PF_MEMALLOC request from this context is rather bizarre 4627 * because we cannot reclaim anything and only can loop waiting 4628 * for somebody to do a work for us. 4629 */ 4630 WARN_ON_ONCE(current->flags & PF_MEMALLOC); 4631 } 4632 4633 restart: 4634 compaction_retries = 0; 4635 no_progress_loops = 0; 4636 compact_result = COMPACT_SKIPPED; 4637 compact_priority = DEF_COMPACT_PRIORITY; 4638 cpuset_mems_cookie = read_mems_allowed_begin(); 4639 zonelist_iter_cookie = zonelist_iter_begin(); 4640 4641 /* 4642 * The fast path uses conservative alloc_flags to succeed only until 4643 * kswapd needs to be woken up, and to avoid the cost of setting up 4644 * alloc_flags precisely. So we do that now. 4645 */ 4646 alloc_flags = gfp_to_alloc_flags(gfp_mask, order); 4647 4648 /* 4649 * We need to recalculate the starting point for the zonelist iterator 4650 * because we might have used different nodemask in the fast path, or 4651 * there was a cpuset modification and we are retrying - otherwise we 4652 * could end up iterating over non-eligible zones endlessly. 4653 */ 4654 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, 4655 ac->highest_zoneidx, ac->nodemask); 4656 if (!zonelist_zone(ac->preferred_zoneref)) 4657 goto nopage; 4658 4659 /* 4660 * Check for insane configurations where the cpuset doesn't contain 4661 * any suitable zone to satisfy the request - e.g. non-movable 4662 * GFP_HIGHUSER allocations from MOVABLE nodes only. 4663 */ 4664 if (cpusets_insane_config() && (gfp_mask & __GFP_HARDWALL)) { 4665 struct zoneref *z = first_zones_zonelist(ac->zonelist, 4666 ac->highest_zoneidx, 4667 &cpuset_current_mems_allowed); 4668 if (!zonelist_zone(z)) 4669 goto nopage; 4670 } 4671 4672 if (alloc_flags & ALLOC_KSWAPD) 4673 wake_all_kswapds(order, gfp_mask, ac); 4674 4675 /* 4676 * The adjusted alloc_flags might result in immediate success, so try 4677 * that first 4678 */ 4679 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); 4680 if (page) 4681 goto got_pg; 4682 4683 /* 4684 * For costly allocations, try direct compaction first, as it's likely 4685 * that we have enough base pages and don't need to reclaim. For non- 4686 * movable high-order allocations, do that as well, as compaction will 4687 * try prevent permanent fragmentation by migrating from blocks of the 4688 * same migratetype. 4689 * Don't try this for allocations that are allowed to ignore 4690 * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen. 4691 */ 4692 if (can_direct_reclaim && can_compact && 4693 (costly_order || 4694 (order > 0 && ac->migratetype != MIGRATE_MOVABLE)) 4695 && !gfp_pfmemalloc_allowed(gfp_mask)) { 4696 page = __alloc_pages_direct_compact(gfp_mask, order, 4697 alloc_flags, ac, 4698 INIT_COMPACT_PRIORITY, 4699 &compact_result); 4700 if (page) 4701 goto got_pg; 4702 4703 /* 4704 * Checks for costly allocations with __GFP_NORETRY, which 4705 * includes some THP page fault allocations 4706 */ 4707 if (costly_order && (gfp_mask & __GFP_NORETRY)) { 4708 /* 4709 * If allocating entire pageblock(s) and compaction 4710 * failed because all zones are below low watermarks 4711 * or is prohibited because it recently failed at this 4712 * order, fail immediately unless the allocator has 4713 * requested compaction and reclaim retry. 4714 * 4715 * Reclaim is 4716 * - potentially very expensive because zones are far 4717 * below their low watermarks or this is part of very 4718 * bursty high order allocations, 4719 * - not guaranteed to help because isolate_freepages() 4720 * may not iterate over freed pages as part of its 4721 * linear scan, and 4722 * - unlikely to make entire pageblocks free on its 4723 * own. 4724 */ 4725 if (compact_result == COMPACT_SKIPPED || 4726 compact_result == COMPACT_DEFERRED) 4727 goto nopage; 4728 4729 /* 4730 * Looks like reclaim/compaction is worth trying, but 4731 * sync compaction could be very expensive, so keep 4732 * using async compaction. 4733 */ 4734 compact_priority = INIT_COMPACT_PRIORITY; 4735 } 4736 } 4737 4738 retry: 4739 /* 4740 * Deal with possible cpuset update races or zonelist updates to avoid 4741 * infinite retries. 4742 */ 4743 if (check_retry_cpuset(cpuset_mems_cookie, ac) || 4744 check_retry_zonelist(zonelist_iter_cookie)) 4745 goto restart; 4746 4747 /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */ 4748 if (alloc_flags & ALLOC_KSWAPD) 4749 wake_all_kswapds(order, gfp_mask, ac); 4750 4751 reserve_flags = __gfp_pfmemalloc_flags(gfp_mask); 4752 if (reserve_flags) 4753 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, reserve_flags) | 4754 (alloc_flags & ALLOC_KSWAPD); 4755 4756 /* 4757 * Reset the nodemask and zonelist iterators if memory policies can be 4758 * ignored. These allocations are high priority and system rather than 4759 * user oriented. 4760 */ 4761 if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) { 4762 ac->nodemask = NULL; 4763 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, 4764 ac->highest_zoneidx, ac->nodemask); 4765 } 4766 4767 /* Attempt with potentially adjusted zonelist and alloc_flags */ 4768 page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); 4769 if (page) 4770 goto got_pg; 4771 4772 /* Caller is not willing to reclaim, we can't balance anything */ 4773 if (!can_direct_reclaim) 4774 goto nopage; 4775 4776 /* Avoid recursion of direct reclaim */ 4777 if (current->flags & PF_MEMALLOC) 4778 goto nopage; 4779 4780 /* Try direct reclaim and then allocating */ 4781 page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac, 4782 &did_some_progress); 4783 if (page) 4784 goto got_pg; 4785 4786 /* Try direct compaction and then allocating */ 4787 page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, 4788 compact_priority, &compact_result); 4789 if (page) 4790 goto got_pg; 4791 4792 /* Do not loop if specifically requested */ 4793 if (gfp_mask & __GFP_NORETRY) 4794 goto nopage; 4795 4796 /* 4797 * Do not retry costly high order allocations unless they are 4798 * __GFP_RETRY_MAYFAIL and we can compact 4799 */ 4800 if (costly_order && (!can_compact || 4801 !(gfp_mask & __GFP_RETRY_MAYFAIL))) 4802 goto nopage; 4803 4804 if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags, 4805 did_some_progress > 0, &no_progress_loops)) 4806 goto retry; 4807 4808 /* 4809 * It doesn't make any sense to retry for the compaction if the order-0 4810 * reclaim is not able to make any progress because the current 4811 * implementation of the compaction depends on the sufficient amount 4812 * of free memory (see __compaction_suitable) 4813 */ 4814 if (did_some_progress > 0 && can_compact && 4815 should_compact_retry(ac, order, alloc_flags, 4816 compact_result, &compact_priority, 4817 &compaction_retries)) 4818 goto retry; 4819 4820 /* Reclaim/compaction failed to prevent the fallback */ 4821 if (defrag_mode && (alloc_flags & ALLOC_NOFRAGMENT)) { 4822 alloc_flags &= ~ALLOC_NOFRAGMENT; 4823 goto retry; 4824 } 4825 4826 /* 4827 * Deal with possible cpuset update races or zonelist updates to avoid 4828 * a unnecessary OOM kill. 4829 */ 4830 if (check_retry_cpuset(cpuset_mems_cookie, ac) || 4831 check_retry_zonelist(zonelist_iter_cookie)) 4832 goto restart; 4833 4834 /* Reclaim has failed us, start killing things */ 4835 page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress); 4836 if (page) 4837 goto got_pg; 4838 4839 /* Avoid allocations with no watermarks from looping endlessly */ 4840 if (tsk_is_oom_victim(current) && 4841 (alloc_flags & ALLOC_OOM || 4842 (gfp_mask & __GFP_NOMEMALLOC))) 4843 goto nopage; 4844 4845 /* Retry as long as the OOM killer is making progress */ 4846 if (did_some_progress) { 4847 no_progress_loops = 0; 4848 goto retry; 4849 } 4850 4851 nopage: 4852 /* 4853 * Deal with possible cpuset update races or zonelist updates to avoid 4854 * a unnecessary OOM kill. 4855 */ 4856 if (check_retry_cpuset(cpuset_mems_cookie, ac) || 4857 check_retry_zonelist(zonelist_iter_cookie)) 4858 goto restart; 4859 4860 /* 4861 * Make sure that __GFP_NOFAIL request doesn't leak out and make sure 4862 * we always retry 4863 */ 4864 if (unlikely(nofail)) { 4865 /* 4866 * Lacking direct_reclaim we can't do anything to reclaim memory, 4867 * we disregard these unreasonable nofail requests and still 4868 * return NULL 4869 */ 4870 if (!can_direct_reclaim) 4871 goto fail; 4872 4873 /* 4874 * Help non-failing allocations by giving some access to memory 4875 * reserves normally used for high priority non-blocking 4876 * allocations but do not use ALLOC_NO_WATERMARKS because this 4877 * could deplete whole memory reserves which would just make 4878 * the situation worse. 4879 */ 4880 page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_MIN_RESERVE, ac); 4881 if (page) 4882 goto got_pg; 4883 4884 cond_resched(); 4885 goto retry; 4886 } 4887 fail: 4888 warn_alloc(gfp_mask, ac->nodemask, 4889 "page allocation failure: order:%u", order); 4890 got_pg: 4891 return page; 4892 } 4893 4894 static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, 4895 int preferred_nid, nodemask_t *nodemask, 4896 struct alloc_context *ac, gfp_t *alloc_gfp, 4897 unsigned int *alloc_flags) 4898 { 4899 ac->highest_zoneidx = gfp_zone(gfp_mask); 4900 ac->zonelist = node_zonelist(preferred_nid, gfp_mask); 4901 ac->nodemask = nodemask; 4902 ac->migratetype = gfp_migratetype(gfp_mask); 4903 4904 if (cpusets_enabled()) { 4905 *alloc_gfp |= __GFP_HARDWALL; 4906 /* 4907 * When we are in the interrupt context, it is irrelevant 4908 * to the current task context. It means that any node ok. 4909 */ 4910 if (in_task() && !ac->nodemask) 4911 ac->nodemask = &cpuset_current_mems_allowed; 4912 else 4913 *alloc_flags |= ALLOC_CPUSET; 4914 } 4915 4916 might_alloc(gfp_mask); 4917 4918 /* 4919 * Don't invoke should_fail logic, since it may call 4920 * get_random_u32() and printk() which need to spin_lock. 4921 */ 4922 if (!(*alloc_flags & ALLOC_TRYLOCK) && 4923 should_fail_alloc_page(gfp_mask, order)) 4924 return false; 4925 4926 *alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, *alloc_flags); 4927 4928 /* Dirty zone balancing only done in the fast path */ 4929 ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE); 4930 4931 /* 4932 * The preferred zone is used for statistics but crucially it is 4933 * also used as the starting point for the zonelist iterator. It 4934 * may get reset for allocations that ignore memory policies. 4935 */ 4936 ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, 4937 ac->highest_zoneidx, ac->nodemask); 4938 4939 return true; 4940 } 4941 4942 /* 4943 * __alloc_pages_bulk - Allocate a number of order-0 pages to an array 4944 * @gfp: GFP flags for the allocation 4945 * @preferred_nid: The preferred NUMA node ID to allocate from 4946 * @nodemask: Set of nodes to allocate from, may be NULL 4947 * @nr_pages: The number of pages desired in the array 4948 * @page_array: Array to store the pages 4949 * 4950 * This is a batched version of the page allocator that attempts to 4951 * allocate nr_pages quickly. Pages are added to the page_array. 4952 * 4953 * Note that only NULL elements are populated with pages and nr_pages 4954 * is the maximum number of pages that will be stored in the array. 4955 * 4956 * Returns the number of pages in the array. 4957 */ 4958 unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, 4959 nodemask_t *nodemask, int nr_pages, 4960 struct page **page_array) 4961 { 4962 struct page *page; 4963 unsigned long __maybe_unused UP_flags; 4964 struct zone *zone; 4965 struct zoneref *z; 4966 struct per_cpu_pages *pcp; 4967 struct list_head *pcp_list; 4968 struct alloc_context ac; 4969 gfp_t alloc_gfp; 4970 unsigned int alloc_flags = ALLOC_WMARK_LOW; 4971 int nr_populated = 0, nr_account = 0; 4972 4973 /* 4974 * Skip populated array elements to determine if any pages need 4975 * to be allocated before disabling IRQs. 4976 */ 4977 while (nr_populated < nr_pages && page_array[nr_populated]) 4978 nr_populated++; 4979 4980 /* No pages requested? */ 4981 if (unlikely(nr_pages <= 0)) 4982 goto out; 4983 4984 /* Already populated array? */ 4985 if (unlikely(nr_pages - nr_populated == 0)) 4986 goto out; 4987 4988 /* Bulk allocator does not support memcg accounting. */ 4989 if (memcg_kmem_online() && (gfp & __GFP_ACCOUNT)) 4990 goto failed; 4991 4992 /* Use the single page allocator for one page. */ 4993 if (nr_pages - nr_populated == 1) 4994 goto failed; 4995 4996 #ifdef CONFIG_PAGE_OWNER 4997 /* 4998 * PAGE_OWNER may recurse into the allocator to allocate space to 4999 * save the stack with pagesets.lock held. Releasing/reacquiring 5000 * removes much of the performance benefit of bulk allocation so 5001 * force the caller to allocate one page at a time as it'll have 5002 * similar performance to added complexity to the bulk allocator. 5003 */ 5004 if (static_branch_unlikely(&page_owner_inited)) 5005 goto failed; 5006 #endif 5007 5008 /* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */ 5009 gfp &= gfp_allowed_mask; 5010 alloc_gfp = gfp; 5011 if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &alloc_gfp, &alloc_flags)) 5012 goto out; 5013 gfp = alloc_gfp; 5014 5015 /* Find an allowed local zone that meets the low watermark. */ 5016 z = ac.preferred_zoneref; 5017 for_next_zone_zonelist_nodemask(zone, z, ac.highest_zoneidx, ac.nodemask) { 5018 unsigned long mark; 5019 5020 if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) && 5021 !__cpuset_zone_allowed(zone, gfp)) { 5022 continue; 5023 } 5024 5025 if (nr_online_nodes > 1 && zone != zonelist_zone(ac.preferred_zoneref) && 5026 zone_to_nid(zone) != zonelist_node_idx(ac.preferred_zoneref)) { 5027 goto failed; 5028 } 5029 5030 cond_accept_memory(zone, 0, alloc_flags); 5031 retry_this_zone: 5032 mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages; 5033 if (zone_watermark_fast(zone, 0, mark, 5034 zonelist_zone_idx(ac.preferred_zoneref), 5035 alloc_flags, gfp)) { 5036 break; 5037 } 5038 5039 if (cond_accept_memory(zone, 0, alloc_flags)) 5040 goto retry_this_zone; 5041 5042 /* Try again if zone has deferred pages */ 5043 if (deferred_pages_enabled()) { 5044 if (_deferred_grow_zone(zone, 0)) 5045 goto retry_this_zone; 5046 } 5047 } 5048 5049 /* 5050 * If there are no allowed local zones that meets the watermarks then 5051 * try to allocate a single page and reclaim if necessary. 5052 */ 5053 if (unlikely(!zone)) 5054 goto failed; 5055 5056 /* spin_trylock may fail due to a parallel drain or IRQ reentrancy. */ 5057 pcp_trylock_prepare(UP_flags); 5058 pcp = pcp_spin_trylock(zone->per_cpu_pageset); 5059 if (!pcp) 5060 goto failed_irq; 5061 5062 /* Attempt the batch allocation */ 5063 pcp_list = &pcp->lists[order_to_pindex(ac.migratetype, 0)]; 5064 while (nr_populated < nr_pages) { 5065 5066 /* Skip existing pages */ 5067 if (page_array[nr_populated]) { 5068 nr_populated++; 5069 continue; 5070 } 5071 5072 page = __rmqueue_pcplist(zone, 0, ac.migratetype, alloc_flags, 5073 pcp, pcp_list); 5074 if (unlikely(!page)) { 5075 /* Try and allocate at least one page */ 5076 if (!nr_account) { 5077 pcp_spin_unlock(pcp); 5078 goto failed_irq; 5079 } 5080 break; 5081 } 5082 nr_account++; 5083 5084 prep_new_page(page, 0, gfp, 0); 5085 set_page_refcounted(page); 5086 page_array[nr_populated++] = page; 5087 } 5088 5089 pcp_spin_unlock(pcp); 5090 pcp_trylock_finish(UP_flags); 5091 5092 __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account); 5093 zone_statistics(zonelist_zone(ac.preferred_zoneref), zone, nr_account); 5094 5095 out: 5096 return nr_populated; 5097 5098 failed_irq: 5099 pcp_trylock_finish(UP_flags); 5100 5101 failed: 5102 page = __alloc_pages_noprof(gfp, 0, preferred_nid, nodemask); 5103 if (page) 5104 page_array[nr_populated++] = page; 5105 goto out; 5106 } 5107 EXPORT_SYMBOL_GPL(alloc_pages_bulk_noprof); 5108 5109 /* 5110 * This is the 'heart' of the zoned buddy allocator. 5111 */ 5112 struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order, 5113 int preferred_nid, nodemask_t *nodemask) 5114 { 5115 struct page *page; 5116 unsigned int alloc_flags = ALLOC_WMARK_LOW; 5117 gfp_t alloc_gfp; /* The gfp_t that was actually used for allocation */ 5118 struct alloc_context ac = { }; 5119 5120 /* 5121 * There are several places where we assume that the order value is sane 5122 * so bail out early if the request is out of bound. 5123 */ 5124 if (WARN_ON_ONCE_GFP(order > MAX_PAGE_ORDER, gfp)) 5125 return NULL; 5126 5127 gfp &= gfp_allowed_mask; 5128 /* 5129 * Apply scoped allocation constraints. This is mainly about GFP_NOFS 5130 * resp. GFP_NOIO which has to be inherited for all allocation requests 5131 * from a particular context which has been marked by 5132 * memalloc_no{fs,io}_{save,restore}. And PF_MEMALLOC_PIN which ensures 5133 * movable zones are not used during allocation. 5134 */ 5135 gfp = current_gfp_context(gfp); 5136 alloc_gfp = gfp; 5137 if (!prepare_alloc_pages(gfp, order, preferred_nid, nodemask, &ac, 5138 &alloc_gfp, &alloc_flags)) 5139 return NULL; 5140 5141 /* 5142 * Forbid the first pass from falling back to types that fragment 5143 * memory until all local zones are considered. 5144 */ 5145 alloc_flags |= alloc_flags_nofragment(zonelist_zone(ac.preferred_zoneref), gfp); 5146 5147 /* First allocation attempt */ 5148 page = get_page_from_freelist(alloc_gfp, order, alloc_flags, &ac); 5149 if (likely(page)) 5150 goto out; 5151 5152 alloc_gfp = gfp; 5153 ac.spread_dirty_pages = false; 5154 5155 /* 5156 * Restore the original nodemask if it was potentially replaced with 5157 * &cpuset_current_mems_allowed to optimize the fast-path attempt. 5158 */ 5159 ac.nodemask = nodemask; 5160 5161 page = __alloc_pages_slowpath(alloc_gfp, order, &ac); 5162 5163 out: 5164 if (memcg_kmem_online() && (gfp & __GFP_ACCOUNT) && page && 5165 unlikely(__memcg_kmem_charge_page(page, gfp, order) != 0)) { 5166 free_frozen_pages(page, order); 5167 page = NULL; 5168 } 5169 5170 trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); 5171 kmsan_alloc_page(page, order, alloc_gfp); 5172 5173 return page; 5174 } 5175 EXPORT_SYMBOL(__alloc_frozen_pages_noprof); 5176 5177 struct page *__alloc_pages_noprof(gfp_t gfp, unsigned int order, 5178 int preferred_nid, nodemask_t *nodemask) 5179 { 5180 struct page *page; 5181 5182 page = __alloc_frozen_pages_noprof(gfp, order, preferred_nid, nodemask); 5183 if (page) 5184 set_page_refcounted(page); 5185 return page; 5186 } 5187 EXPORT_SYMBOL(__alloc_pages_noprof); 5188 5189 struct folio *__folio_alloc_noprof(gfp_t gfp, unsigned int order, int preferred_nid, 5190 nodemask_t *nodemask) 5191 { 5192 struct page *page = __alloc_pages_noprof(gfp | __GFP_COMP, order, 5193 preferred_nid, nodemask); 5194 return page_rmappable_folio(page); 5195 } 5196 EXPORT_SYMBOL(__folio_alloc_noprof); 5197 5198 /* 5199 * Common helper functions. Never use with __GFP_HIGHMEM because the returned 5200 * address cannot represent highmem pages. Use alloc_pages and then kmap if 5201 * you need to access high mem. 5202 */ 5203 unsigned long get_free_pages_noprof(gfp_t gfp_mask, unsigned int order) 5204 { 5205 struct page *page; 5206 5207 page = alloc_pages_noprof(gfp_mask & ~__GFP_HIGHMEM, order); 5208 if (!page) 5209 return 0; 5210 return (unsigned long) page_address(page); 5211 } 5212 EXPORT_SYMBOL(get_free_pages_noprof); 5213 5214 unsigned long get_zeroed_page_noprof(gfp_t gfp_mask) 5215 { 5216 return get_free_pages_noprof(gfp_mask | __GFP_ZERO, 0); 5217 } 5218 EXPORT_SYMBOL(get_zeroed_page_noprof); 5219 5220 static void ___free_pages(struct page *page, unsigned int order, 5221 fpi_t fpi_flags) 5222 { 5223 /* get PageHead before we drop reference */ 5224 int head = PageHead(page); 5225 /* get alloc tag in case the page is released by others */ 5226 struct alloc_tag *tag = pgalloc_tag_get(page); 5227 5228 if (put_page_testzero(page)) 5229 __free_frozen_pages(page, order, fpi_flags); 5230 else if (!head) { 5231 pgalloc_tag_sub_pages(tag, (1 << order) - 1); 5232 while (order-- > 0) 5233 __free_frozen_pages(page + (1 << order), order, 5234 fpi_flags); 5235 } 5236 } 5237 5238 /** 5239 * __free_pages - Free pages allocated with alloc_pages(). 5240 * @page: The page pointer returned from alloc_pages(). 5241 * @order: The order of the allocation. 5242 * 5243 * This function can free multi-page allocations that are not compound 5244 * pages. It does not check that the @order passed in matches that of 5245 * the allocation, so it is easy to leak memory. Freeing more memory 5246 * than was allocated will probably emit a warning. 5247 * 5248 * If the last reference to this page is speculative, it will be released 5249 * by put_page() which only frees the first page of a non-compound 5250 * allocation. To prevent the remaining pages from being leaked, we free 5251 * the subsequent pages here. If you want to use the page's reference 5252 * count to decide when to free the allocation, you should allocate a 5253 * compound page, and use put_page() instead of __free_pages(). 5254 * 5255 * Context: May be called in interrupt context or while holding a normal 5256 * spinlock, but not in NMI context or while holding a raw spinlock. 5257 */ 5258 void __free_pages(struct page *page, unsigned int order) 5259 { 5260 ___free_pages(page, order, FPI_NONE); 5261 } 5262 EXPORT_SYMBOL(__free_pages); 5263 5264 /* 5265 * Can be called while holding raw_spin_lock or from IRQ and NMI for any 5266 * page type (not only those that came from alloc_pages_nolock) 5267 */ 5268 void free_pages_nolock(struct page *page, unsigned int order) 5269 { 5270 ___free_pages(page, order, FPI_TRYLOCK); 5271 } 5272 5273 void free_pages(unsigned long addr, unsigned int order) 5274 { 5275 if (addr != 0) { 5276 VM_BUG_ON(!virt_addr_valid((void *)addr)); 5277 __free_pages(virt_to_page((void *)addr), order); 5278 } 5279 } 5280 5281 EXPORT_SYMBOL(free_pages); 5282 5283 static void *make_alloc_exact(unsigned long addr, unsigned int order, 5284 size_t size) 5285 { 5286 if (addr) { 5287 unsigned long nr = DIV_ROUND_UP(size, PAGE_SIZE); 5288 struct page *page = virt_to_page((void *)addr); 5289 struct page *last = page + nr; 5290 5291 split_page_owner(page, order, 0); 5292 pgalloc_tag_split(page_folio(page), order, 0); 5293 split_page_memcg(page, order); 5294 while (page < --last) 5295 set_page_refcounted(last); 5296 5297 last = page + (1UL << order); 5298 for (page += nr; page < last; page++) 5299 __free_pages_ok(page, 0, FPI_TO_TAIL); 5300 } 5301 return (void *)addr; 5302 } 5303 5304 /** 5305 * alloc_pages_exact - allocate an exact number physically-contiguous pages. 5306 * @size: the number of bytes to allocate 5307 * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP 5308 * 5309 * This function is similar to alloc_pages(), except that it allocates the 5310 * minimum number of pages to satisfy the request. alloc_pages() can only 5311 * allocate memory in power-of-two pages. 5312 * 5313 * This function is also limited by MAX_PAGE_ORDER. 5314 * 5315 * Memory allocated by this function must be released by free_pages_exact(). 5316 * 5317 * Return: pointer to the allocated area or %NULL in case of error. 5318 */ 5319 void *alloc_pages_exact_noprof(size_t size, gfp_t gfp_mask) 5320 { 5321 unsigned int order = get_order(size); 5322 unsigned long addr; 5323 5324 if (WARN_ON_ONCE(gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) 5325 gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); 5326 5327 addr = get_free_pages_noprof(gfp_mask, order); 5328 return make_alloc_exact(addr, order, size); 5329 } 5330 EXPORT_SYMBOL(alloc_pages_exact_noprof); 5331 5332 /** 5333 * alloc_pages_exact_nid - allocate an exact number of physically-contiguous 5334 * pages on a node. 5335 * @nid: the preferred node ID where memory should be allocated 5336 * @size: the number of bytes to allocate 5337 * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP 5338 * 5339 * Like alloc_pages_exact(), but try to allocate on node nid first before falling 5340 * back. 5341 * 5342 * Return: pointer to the allocated area or %NULL in case of error. 5343 */ 5344 void * __meminit alloc_pages_exact_nid_noprof(int nid, size_t size, gfp_t gfp_mask) 5345 { 5346 unsigned int order = get_order(size); 5347 struct page *p; 5348 5349 if (WARN_ON_ONCE(gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) 5350 gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); 5351 5352 p = alloc_pages_node_noprof(nid, gfp_mask, order); 5353 if (!p) 5354 return NULL; 5355 return make_alloc_exact((unsigned long)page_address(p), order, size); 5356 } 5357 5358 /** 5359 * free_pages_exact - release memory allocated via alloc_pages_exact() 5360 * @virt: the value returned by alloc_pages_exact. 5361 * @size: size of allocation, same value as passed to alloc_pages_exact(). 5362 * 5363 * Release the memory allocated by a previous call to alloc_pages_exact. 5364 */ 5365 void free_pages_exact(void *virt, size_t size) 5366 { 5367 unsigned long addr = (unsigned long)virt; 5368 unsigned long end = addr + PAGE_ALIGN(size); 5369 5370 while (addr < end) { 5371 free_page(addr); 5372 addr += PAGE_SIZE; 5373 } 5374 } 5375 EXPORT_SYMBOL(free_pages_exact); 5376 5377 /** 5378 * nr_free_zone_pages - count number of pages beyond high watermark 5379 * @offset: The zone index of the highest zone 5380 * 5381 * nr_free_zone_pages() counts the number of pages which are beyond the 5382 * high watermark within all zones at or below a given zone index. For each 5383 * zone, the number of pages is calculated as: 5384 * 5385 * nr_free_zone_pages = managed_pages - high_pages 5386 * 5387 * Return: number of pages beyond high watermark. 5388 */ 5389 static unsigned long nr_free_zone_pages(int offset) 5390 { 5391 struct zoneref *z; 5392 struct zone *zone; 5393 5394 /* Just pick one node, since fallback list is circular */ 5395 unsigned long sum = 0; 5396 5397 struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL); 5398 5399 for_each_zone_zonelist(zone, z, zonelist, offset) { 5400 unsigned long size = zone_managed_pages(zone); 5401 unsigned long high = high_wmark_pages(zone); 5402 if (size > high) 5403 sum += size - high; 5404 } 5405 5406 return sum; 5407 } 5408 5409 /** 5410 * nr_free_buffer_pages - count number of pages beyond high watermark 5411 * 5412 * nr_free_buffer_pages() counts the number of pages which are beyond the high 5413 * watermark within ZONE_DMA and ZONE_NORMAL. 5414 * 5415 * Return: number of pages beyond high watermark within ZONE_DMA and 5416 * ZONE_NORMAL. 5417 */ 5418 unsigned long nr_free_buffer_pages(void) 5419 { 5420 return nr_free_zone_pages(gfp_zone(GFP_USER)); 5421 } 5422 EXPORT_SYMBOL_GPL(nr_free_buffer_pages); 5423 5424 static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref) 5425 { 5426 zoneref->zone = zone; 5427 zoneref->zone_idx = zone_idx(zone); 5428 } 5429 5430 /* 5431 * Builds allocation fallback zone lists. 5432 * 5433 * Add all populated zones of a node to the zonelist. 5434 */ 5435 static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs) 5436 { 5437 struct zone *zone; 5438 enum zone_type zone_type = MAX_NR_ZONES; 5439 int nr_zones = 0; 5440 5441 do { 5442 zone_type--; 5443 zone = pgdat->node_zones + zone_type; 5444 if (populated_zone(zone)) { 5445 zoneref_set_zone(zone, &zonerefs[nr_zones++]); 5446 check_highest_zone(zone_type); 5447 } 5448 } while (zone_type); 5449 5450 return nr_zones; 5451 } 5452 5453 #ifdef CONFIG_NUMA 5454 5455 static int __parse_numa_zonelist_order(char *s) 5456 { 5457 /* 5458 * We used to support different zonelists modes but they turned 5459 * out to be just not useful. Let's keep the warning in place 5460 * if somebody still use the cmd line parameter so that we do 5461 * not fail it silently 5462 */ 5463 if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) { 5464 pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s); 5465 return -EINVAL; 5466 } 5467 return 0; 5468 } 5469 5470 static char numa_zonelist_order[] = "Node"; 5471 #define NUMA_ZONELIST_ORDER_LEN 16 5472 /* 5473 * sysctl handler for numa_zonelist_order 5474 */ 5475 static int numa_zonelist_order_handler(const struct ctl_table *table, int write, 5476 void *buffer, size_t *length, loff_t *ppos) 5477 { 5478 if (write) 5479 return __parse_numa_zonelist_order(buffer); 5480 return proc_dostring(table, write, buffer, length, ppos); 5481 } 5482 5483 static int node_load[MAX_NUMNODES]; 5484 5485 /** 5486 * find_next_best_node - find the next node that should appear in a given node's fallback list 5487 * @node: node whose fallback list we're appending 5488 * @used_node_mask: nodemask_t of already used nodes 5489 * 5490 * We use a number of factors to determine which is the next node that should 5491 * appear on a given node's fallback list. The node should not have appeared 5492 * already in @node's fallback list, and it should be the next closest node 5493 * according to the distance array (which contains arbitrary distance values 5494 * from each node to each node in the system), and should also prefer nodes 5495 * with no CPUs, since presumably they'll have very little allocation pressure 5496 * on them otherwise. 5497 * 5498 * Return: node id of the found node or %NUMA_NO_NODE if no node is found. 5499 */ 5500 int find_next_best_node(int node, nodemask_t *used_node_mask) 5501 { 5502 int n, val; 5503 int min_val = INT_MAX; 5504 int best_node = NUMA_NO_NODE; 5505 5506 /* 5507 * Use the local node if we haven't already, but for memoryless local 5508 * node, we should skip it and fall back to other nodes. 5509 */ 5510 if (!node_isset(node, *used_node_mask) && node_state(node, N_MEMORY)) { 5511 node_set(node, *used_node_mask); 5512 return node; 5513 } 5514 5515 for_each_node_state(n, N_MEMORY) { 5516 5517 /* Don't want a node to appear more than once */ 5518 if (node_isset(n, *used_node_mask)) 5519 continue; 5520 5521 /* Use the distance array to find the distance */ 5522 val = node_distance(node, n); 5523 5524 /* Penalize nodes under us ("prefer the next node") */ 5525 val += (n < node); 5526 5527 /* Give preference to headless and unused nodes */ 5528 if (!cpumask_empty(cpumask_of_node(n))) 5529 val += PENALTY_FOR_NODE_WITH_CPUS; 5530 5531 /* Slight preference for less loaded node */ 5532 val *= MAX_NUMNODES; 5533 val += node_load[n]; 5534 5535 if (val < min_val) { 5536 min_val = val; 5537 best_node = n; 5538 } 5539 } 5540 5541 if (best_node >= 0) 5542 node_set(best_node, *used_node_mask); 5543 5544 return best_node; 5545 } 5546 5547 5548 /* 5549 * Build zonelists ordered by node and zones within node. 5550 * This results in maximum locality--normal zone overflows into local 5551 * DMA zone, if any--but risks exhausting DMA zone. 5552 */ 5553 static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order, 5554 unsigned nr_nodes) 5555 { 5556 struct zoneref *zonerefs; 5557 int i; 5558 5559 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; 5560 5561 for (i = 0; i < nr_nodes; i++) { 5562 int nr_zones; 5563 5564 pg_data_t *node = NODE_DATA(node_order[i]); 5565 5566 nr_zones = build_zonerefs_node(node, zonerefs); 5567 zonerefs += nr_zones; 5568 } 5569 zonerefs->zone = NULL; 5570 zonerefs->zone_idx = 0; 5571 } 5572 5573 /* 5574 * Build __GFP_THISNODE zonelists 5575 */ 5576 static void build_thisnode_zonelists(pg_data_t *pgdat) 5577 { 5578 struct zoneref *zonerefs; 5579 int nr_zones; 5580 5581 zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs; 5582 nr_zones = build_zonerefs_node(pgdat, zonerefs); 5583 zonerefs += nr_zones; 5584 zonerefs->zone = NULL; 5585 zonerefs->zone_idx = 0; 5586 } 5587 5588 static void build_zonelists(pg_data_t *pgdat) 5589 { 5590 static int node_order[MAX_NUMNODES]; 5591 int node, nr_nodes = 0; 5592 nodemask_t used_mask = NODE_MASK_NONE; 5593 int local_node, prev_node; 5594 5595 /* NUMA-aware ordering of nodes */ 5596 local_node = pgdat->node_id; 5597 prev_node = local_node; 5598 5599 memset(node_order, 0, sizeof(node_order)); 5600 while ((node = find_next_best_node(local_node, &used_mask)) >= 0) { 5601 /* 5602 * We don't want to pressure a particular node. 5603 * So adding penalty to the first node in same 5604 * distance group to make it round-robin. 5605 */ 5606 if (node_distance(local_node, node) != 5607 node_distance(local_node, prev_node)) 5608 node_load[node] += 1; 5609 5610 node_order[nr_nodes++] = node; 5611 prev_node = node; 5612 } 5613 5614 build_zonelists_in_node_order(pgdat, node_order, nr_nodes); 5615 build_thisnode_zonelists(pgdat); 5616 pr_info("Fallback order for Node %d: ", local_node); 5617 for (node = 0; node < nr_nodes; node++) 5618 pr_cont("%d ", node_order[node]); 5619 pr_cont("\n"); 5620 } 5621 5622 #ifdef CONFIG_HAVE_MEMORYLESS_NODES 5623 /* 5624 * Return node id of node used for "local" allocations. 5625 * I.e., first node id of first zone in arg node's generic zonelist. 5626 * Used for initializing percpu 'numa_mem', which is used primarily 5627 * for kernel allocations, so use GFP_KERNEL flags to locate zonelist. 5628 */ 5629 int local_memory_node(int node) 5630 { 5631 struct zoneref *z; 5632 5633 z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL), 5634 gfp_zone(GFP_KERNEL), 5635 NULL); 5636 return zonelist_node_idx(z); 5637 } 5638 #endif 5639 5640 static void setup_min_unmapped_ratio(void); 5641 static void setup_min_slab_ratio(void); 5642 #else /* CONFIG_NUMA */ 5643 5644 static void build_zonelists(pg_data_t *pgdat) 5645 { 5646 struct zoneref *zonerefs; 5647 int nr_zones; 5648 5649 zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; 5650 nr_zones = build_zonerefs_node(pgdat, zonerefs); 5651 zonerefs += nr_zones; 5652 5653 zonerefs->zone = NULL; 5654 zonerefs->zone_idx = 0; 5655 } 5656 5657 #endif /* CONFIG_NUMA */ 5658 5659 /* 5660 * Boot pageset table. One per cpu which is going to be used for all 5661 * zones and all nodes. The parameters will be set in such a way 5662 * that an item put on a list will immediately be handed over to 5663 * the buddy list. This is safe since pageset manipulation is done 5664 * with interrupts disabled. 5665 * 5666 * The boot_pagesets must be kept even after bootup is complete for 5667 * unused processors and/or zones. They do play a role for bootstrapping 5668 * hotplugged processors. 5669 * 5670 * zoneinfo_show() and maybe other functions do 5671 * not check if the processor is online before following the pageset pointer. 5672 * Other parts of the kernel may not check if the zone is available. 5673 */ 5674 static void per_cpu_pages_init(struct per_cpu_pages *pcp, struct per_cpu_zonestat *pzstats); 5675 /* These effectively disable the pcplists in the boot pageset completely */ 5676 #define BOOT_PAGESET_HIGH 0 5677 #define BOOT_PAGESET_BATCH 1 5678 static DEFINE_PER_CPU(struct per_cpu_pages, boot_pageset); 5679 static DEFINE_PER_CPU(struct per_cpu_zonestat, boot_zonestats); 5680 5681 static void __build_all_zonelists(void *data) 5682 { 5683 int nid; 5684 int __maybe_unused cpu; 5685 pg_data_t *self = data; 5686 unsigned long flags; 5687 5688 /* 5689 * The zonelist_update_seq must be acquired with irqsave because the 5690 * reader can be invoked from IRQ with GFP_ATOMIC. 5691 */ 5692 write_seqlock_irqsave(&zonelist_update_seq, flags); 5693 /* 5694 * Also disable synchronous printk() to prevent any printk() from 5695 * trying to hold port->lock, for 5696 * tty_insert_flip_string_and_push_buffer() on other CPU might be 5697 * calling kmalloc(GFP_ATOMIC | __GFP_NOWARN) with port->lock held. 5698 */ 5699 printk_deferred_enter(); 5700 5701 #ifdef CONFIG_NUMA 5702 memset(node_load, 0, sizeof(node_load)); 5703 #endif 5704 5705 /* 5706 * This node is hotadded and no memory is yet present. So just 5707 * building zonelists is fine - no need to touch other nodes. 5708 */ 5709 if (self && !node_online(self->node_id)) { 5710 build_zonelists(self); 5711 } else { 5712 /* 5713 * All possible nodes have pgdat preallocated 5714 * in free_area_init 5715 */ 5716 for_each_node(nid) { 5717 pg_data_t *pgdat = NODE_DATA(nid); 5718 5719 build_zonelists(pgdat); 5720 } 5721 5722 #ifdef CONFIG_HAVE_MEMORYLESS_NODES 5723 /* 5724 * We now know the "local memory node" for each node-- 5725 * i.e., the node of the first zone in the generic zonelist. 5726 * Set up numa_mem percpu variable for on-line cpus. During 5727 * boot, only the boot cpu should be on-line; we'll init the 5728 * secondary cpus' numa_mem as they come on-line. During 5729 * node/memory hotplug, we'll fixup all on-line cpus. 5730 */ 5731 for_each_online_cpu(cpu) 5732 set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu))); 5733 #endif 5734 } 5735 5736 printk_deferred_exit(); 5737 write_sequnlock_irqrestore(&zonelist_update_seq, flags); 5738 } 5739 5740 static noinline void __init 5741 build_all_zonelists_init(void) 5742 { 5743 int cpu; 5744 5745 __build_all_zonelists(NULL); 5746 5747 /* 5748 * Initialize the boot_pagesets that are going to be used 5749 * for bootstrapping processors. The real pagesets for 5750 * each zone will be allocated later when the per cpu 5751 * allocator is available. 5752 * 5753 * boot_pagesets are used also for bootstrapping offline 5754 * cpus if the system is already booted because the pagesets 5755 * are needed to initialize allocators on a specific cpu too. 5756 * F.e. the percpu allocator needs the page allocator which 5757 * needs the percpu allocator in order to allocate its pagesets 5758 * (a chicken-egg dilemma). 5759 */ 5760 for_each_possible_cpu(cpu) 5761 per_cpu_pages_init(&per_cpu(boot_pageset, cpu), &per_cpu(boot_zonestats, cpu)); 5762 5763 mminit_verify_zonelist(); 5764 cpuset_init_current_mems_allowed(); 5765 } 5766 5767 /* 5768 * unless system_state == SYSTEM_BOOTING. 5769 * 5770 * __ref due to call of __init annotated helper build_all_zonelists_init 5771 * [protected by SYSTEM_BOOTING]. 5772 */ 5773 void __ref build_all_zonelists(pg_data_t *pgdat) 5774 { 5775 unsigned long vm_total_pages; 5776 5777 if (system_state == SYSTEM_BOOTING) { 5778 build_all_zonelists_init(); 5779 } else { 5780 __build_all_zonelists(pgdat); 5781 /* cpuset refresh routine should be here */ 5782 } 5783 /* Get the number of free pages beyond high watermark in all zones. */ 5784 vm_total_pages = nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE)); 5785 /* 5786 * Disable grouping by mobility if the number of pages in the 5787 * system is too low to allow the mechanism to work. It would be 5788 * more accurate, but expensive to check per-zone. This check is 5789 * made on memory-hotadd so a system can start with mobility 5790 * disabled and enable it later 5791 */ 5792 if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES)) 5793 page_group_by_mobility_disabled = 1; 5794 else 5795 page_group_by_mobility_disabled = 0; 5796 5797 pr_info("Built %u zonelists, mobility grouping %s. Total pages: %ld\n", 5798 nr_online_nodes, 5799 str_off_on(page_group_by_mobility_disabled), 5800 vm_total_pages); 5801 #ifdef CONFIG_NUMA 5802 pr_info("Policy zone: %s\n", zone_names[policy_zone]); 5803 #endif 5804 } 5805 5806 static int zone_batchsize(struct zone *zone) 5807 { 5808 #ifdef CONFIG_MMU 5809 int batch; 5810 5811 /* 5812 * The number of pages to batch allocate is either ~0.1% 5813 * of the zone or 1MB, whichever is smaller. The batch 5814 * size is striking a balance between allocation latency 5815 * and zone lock contention. 5816 */ 5817 batch = min(zone_managed_pages(zone) >> 10, SZ_1M / PAGE_SIZE); 5818 batch /= 4; /* We effectively *= 4 below */ 5819 if (batch < 1) 5820 batch = 1; 5821 5822 /* 5823 * Clamp the batch to a 2^n - 1 value. Having a power 5824 * of 2 value was found to be more likely to have 5825 * suboptimal cache aliasing properties in some cases. 5826 * 5827 * For example if 2 tasks are alternately allocating 5828 * batches of pages, one task can end up with a lot 5829 * of pages of one half of the possible page colors 5830 * and the other with pages of the other colors. 5831 */ 5832 batch = rounddown_pow_of_two(batch + batch/2) - 1; 5833 5834 return batch; 5835 5836 #else 5837 /* The deferral and batching of frees should be suppressed under NOMMU 5838 * conditions. 5839 * 5840 * The problem is that NOMMU needs to be able to allocate large chunks 5841 * of contiguous memory as there's no hardware page translation to 5842 * assemble apparent contiguous memory from discontiguous pages. 5843 * 5844 * Queueing large contiguous runs of pages for batching, however, 5845 * causes the pages to actually be freed in smaller chunks. As there 5846 * can be a significant delay between the individual batches being 5847 * recycled, this leads to the once large chunks of space being 5848 * fragmented and becoming unavailable for high-order allocations. 5849 */ 5850 return 0; 5851 #endif 5852 } 5853 5854 static int percpu_pagelist_high_fraction; 5855 static int zone_highsize(struct zone *zone, int batch, int cpu_online, 5856 int high_fraction) 5857 { 5858 #ifdef CONFIG_MMU 5859 int high; 5860 int nr_split_cpus; 5861 unsigned long total_pages; 5862 5863 if (!high_fraction) { 5864 /* 5865 * By default, the high value of the pcp is based on the zone 5866 * low watermark so that if they are full then background 5867 * reclaim will not be started prematurely. 5868 */ 5869 total_pages = low_wmark_pages(zone); 5870 } else { 5871 /* 5872 * If percpu_pagelist_high_fraction is configured, the high 5873 * value is based on a fraction of the managed pages in the 5874 * zone. 5875 */ 5876 total_pages = zone_managed_pages(zone) / high_fraction; 5877 } 5878 5879 /* 5880 * Split the high value across all online CPUs local to the zone. Note 5881 * that early in boot that CPUs may not be online yet and that during 5882 * CPU hotplug that the cpumask is not yet updated when a CPU is being 5883 * onlined. For memory nodes that have no CPUs, split the high value 5884 * across all online CPUs to mitigate the risk that reclaim is triggered 5885 * prematurely due to pages stored on pcp lists. 5886 */ 5887 nr_split_cpus = cpumask_weight(cpumask_of_node(zone_to_nid(zone))) + cpu_online; 5888 if (!nr_split_cpus) 5889 nr_split_cpus = num_online_cpus(); 5890 high = total_pages / nr_split_cpus; 5891 5892 /* 5893 * Ensure high is at least batch*4. The multiple is based on the 5894 * historical relationship between high and batch. 5895 */ 5896 high = max(high, batch << 2); 5897 5898 return high; 5899 #else 5900 return 0; 5901 #endif 5902 } 5903 5904 /* 5905 * pcp->high and pcp->batch values are related and generally batch is lower 5906 * than high. They are also related to pcp->count such that count is lower 5907 * than high, and as soon as it reaches high, the pcplist is flushed. 5908 * 5909 * However, guaranteeing these relations at all times would require e.g. write 5910 * barriers here but also careful usage of read barriers at the read side, and 5911 * thus be prone to error and bad for performance. Thus the update only prevents 5912 * store tearing. Any new users of pcp->batch, pcp->high_min and pcp->high_max 5913 * should ensure they can cope with those fields changing asynchronously, and 5914 * fully trust only the pcp->count field on the local CPU with interrupts 5915 * disabled. 5916 * 5917 * mutex_is_locked(&pcp_batch_high_lock) required when calling this function 5918 * outside of boot time (or some other assurance that no concurrent updaters 5919 * exist). 5920 */ 5921 static void pageset_update(struct per_cpu_pages *pcp, unsigned long high_min, 5922 unsigned long high_max, unsigned long batch) 5923 { 5924 WRITE_ONCE(pcp->batch, batch); 5925 WRITE_ONCE(pcp->high_min, high_min); 5926 WRITE_ONCE(pcp->high_max, high_max); 5927 } 5928 5929 static void per_cpu_pages_init(struct per_cpu_pages *pcp, struct per_cpu_zonestat *pzstats) 5930 { 5931 int pindex; 5932 5933 memset(pcp, 0, sizeof(*pcp)); 5934 memset(pzstats, 0, sizeof(*pzstats)); 5935 5936 spin_lock_init(&pcp->lock); 5937 for (pindex = 0; pindex < NR_PCP_LISTS; pindex++) 5938 INIT_LIST_HEAD(&pcp->lists[pindex]); 5939 5940 /* 5941 * Set batch and high values safe for a boot pageset. A true percpu 5942 * pageset's initialization will update them subsequently. Here we don't 5943 * need to be as careful as pageset_update() as nobody can access the 5944 * pageset yet. 5945 */ 5946 pcp->high_min = BOOT_PAGESET_HIGH; 5947 pcp->high_max = BOOT_PAGESET_HIGH; 5948 pcp->batch = BOOT_PAGESET_BATCH; 5949 pcp->free_count = 0; 5950 } 5951 5952 static void __zone_set_pageset_high_and_batch(struct zone *zone, unsigned long high_min, 5953 unsigned long high_max, unsigned long batch) 5954 { 5955 struct per_cpu_pages *pcp; 5956 int cpu; 5957 5958 for_each_possible_cpu(cpu) { 5959 pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); 5960 pageset_update(pcp, high_min, high_max, batch); 5961 } 5962 } 5963 5964 /* 5965 * Calculate and set new high and batch values for all per-cpu pagesets of a 5966 * zone based on the zone's size. 5967 */ 5968 static void zone_set_pageset_high_and_batch(struct zone *zone, int cpu_online) 5969 { 5970 int new_high_min, new_high_max, new_batch; 5971 5972 new_batch = max(1, zone_batchsize(zone)); 5973 if (percpu_pagelist_high_fraction) { 5974 new_high_min = zone_highsize(zone, new_batch, cpu_online, 5975 percpu_pagelist_high_fraction); 5976 /* 5977 * PCP high is tuned manually, disable auto-tuning via 5978 * setting high_min and high_max to the manual value. 5979 */ 5980 new_high_max = new_high_min; 5981 } else { 5982 new_high_min = zone_highsize(zone, new_batch, cpu_online, 0); 5983 new_high_max = zone_highsize(zone, new_batch, cpu_online, 5984 MIN_PERCPU_PAGELIST_HIGH_FRACTION); 5985 } 5986 5987 if (zone->pageset_high_min == new_high_min && 5988 zone->pageset_high_max == new_high_max && 5989 zone->pageset_batch == new_batch) 5990 return; 5991 5992 zone->pageset_high_min = new_high_min; 5993 zone->pageset_high_max = new_high_max; 5994 zone->pageset_batch = new_batch; 5995 5996 __zone_set_pageset_high_and_batch(zone, new_high_min, new_high_max, 5997 new_batch); 5998 } 5999 6000 void __meminit setup_zone_pageset(struct zone *zone) 6001 { 6002 int cpu; 6003 6004 /* Size may be 0 on !SMP && !NUMA */ 6005 if (sizeof(struct per_cpu_zonestat) > 0) 6006 zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat); 6007 6008 zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages); 6009 for_each_possible_cpu(cpu) { 6010 struct per_cpu_pages *pcp; 6011 struct per_cpu_zonestat *pzstats; 6012 6013 pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); 6014 pzstats = per_cpu_ptr(zone->per_cpu_zonestats, cpu); 6015 per_cpu_pages_init(pcp, pzstats); 6016 } 6017 6018 zone_set_pageset_high_and_batch(zone, 0); 6019 } 6020 6021 /* 6022 * The zone indicated has a new number of managed_pages; batch sizes and percpu 6023 * page high values need to be recalculated. 6024 */ 6025 static void zone_pcp_update(struct zone *zone, int cpu_online) 6026 { 6027 mutex_lock(&pcp_batch_high_lock); 6028 zone_set_pageset_high_and_batch(zone, cpu_online); 6029 mutex_unlock(&pcp_batch_high_lock); 6030 } 6031 6032 static void zone_pcp_update_cacheinfo(struct zone *zone, unsigned int cpu) 6033 { 6034 struct per_cpu_pages *pcp; 6035 struct cpu_cacheinfo *cci; 6036 6037 pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); 6038 cci = get_cpu_cacheinfo(cpu); 6039 /* 6040 * If data cache slice of CPU is large enough, "pcp->batch" 6041 * pages can be preserved in PCP before draining PCP for 6042 * consecutive high-order pages freeing without allocation. 6043 * This can reduce zone lock contention without hurting 6044 * cache-hot pages sharing. 6045 */ 6046 spin_lock(&pcp->lock); 6047 if ((cci->per_cpu_data_slice_size >> PAGE_SHIFT) > 3 * pcp->batch) 6048 pcp->flags |= PCPF_FREE_HIGH_BATCH; 6049 else 6050 pcp->flags &= ~PCPF_FREE_HIGH_BATCH; 6051 spin_unlock(&pcp->lock); 6052 } 6053 6054 void setup_pcp_cacheinfo(unsigned int cpu) 6055 { 6056 struct zone *zone; 6057 6058 for_each_populated_zone(zone) 6059 zone_pcp_update_cacheinfo(zone, cpu); 6060 } 6061 6062 /* 6063 * Allocate per cpu pagesets and initialize them. 6064 * Before this call only boot pagesets were available. 6065 */ 6066 void __init setup_per_cpu_pageset(void) 6067 { 6068 struct pglist_data *pgdat; 6069 struct zone *zone; 6070 int __maybe_unused cpu; 6071 6072 for_each_populated_zone(zone) 6073 setup_zone_pageset(zone); 6074 6075 #ifdef CONFIG_NUMA 6076 /* 6077 * Unpopulated zones continue using the boot pagesets. 6078 * The numa stats for these pagesets need to be reset. 6079 * Otherwise, they will end up skewing the stats of 6080 * the nodes these zones are associated with. 6081 */ 6082 for_each_possible_cpu(cpu) { 6083 struct per_cpu_zonestat *pzstats = &per_cpu(boot_zonestats, cpu); 6084 memset(pzstats->vm_numa_event, 0, 6085 sizeof(pzstats->vm_numa_event)); 6086 } 6087 #endif 6088 6089 for_each_online_pgdat(pgdat) 6090 pgdat->per_cpu_nodestats = 6091 alloc_percpu(struct per_cpu_nodestat); 6092 } 6093 6094 __meminit void zone_pcp_init(struct zone *zone) 6095 { 6096 /* 6097 * per cpu subsystem is not up at this point. The following code 6098 * relies on the ability of the linker to provide the 6099 * offset of a (static) per cpu variable into the per cpu area. 6100 */ 6101 zone->per_cpu_pageset = &boot_pageset; 6102 zone->per_cpu_zonestats = &boot_zonestats; 6103 zone->pageset_high_min = BOOT_PAGESET_HIGH; 6104 zone->pageset_high_max = BOOT_PAGESET_HIGH; 6105 zone->pageset_batch = BOOT_PAGESET_BATCH; 6106 6107 if (populated_zone(zone)) 6108 pr_debug(" %s zone: %lu pages, LIFO batch:%u\n", zone->name, 6109 zone->present_pages, zone_batchsize(zone)); 6110 } 6111 6112 static void setup_per_zone_lowmem_reserve(void); 6113 6114 void adjust_managed_page_count(struct page *page, long count) 6115 { 6116 atomic_long_add(count, &page_zone(page)->managed_pages); 6117 totalram_pages_add(count); 6118 setup_per_zone_lowmem_reserve(); 6119 } 6120 EXPORT_SYMBOL(adjust_managed_page_count); 6121 6122 unsigned long free_reserved_area(void *start, void *end, int poison, const char *s) 6123 { 6124 void *pos; 6125 unsigned long pages = 0; 6126 6127 start = (void *)PAGE_ALIGN((unsigned long)start); 6128 end = (void *)((unsigned long)end & PAGE_MASK); 6129 for (pos = start; pos < end; pos += PAGE_SIZE, pages++) { 6130 struct page *page = virt_to_page(pos); 6131 void *direct_map_addr; 6132 6133 /* 6134 * 'direct_map_addr' might be different from 'pos' 6135 * because some architectures' virt_to_page() 6136 * work with aliases. Getting the direct map 6137 * address ensures that we get a _writeable_ 6138 * alias for the memset(). 6139 */ 6140 direct_map_addr = page_address(page); 6141 /* 6142 * Perform a kasan-unchecked memset() since this memory 6143 * has not been initialized. 6144 */ 6145 direct_map_addr = kasan_reset_tag(direct_map_addr); 6146 if ((unsigned int)poison <= 0xFF) 6147 memset(direct_map_addr, poison, PAGE_SIZE); 6148 6149 free_reserved_page(page); 6150 } 6151 6152 if (pages && s) 6153 pr_info("Freeing %s memory: %ldK\n", s, K(pages)); 6154 6155 return pages; 6156 } 6157 6158 void free_reserved_page(struct page *page) 6159 { 6160 clear_page_tag_ref(page); 6161 ClearPageReserved(page); 6162 init_page_count(page); 6163 __free_page(page); 6164 adjust_managed_page_count(page, 1); 6165 } 6166 EXPORT_SYMBOL(free_reserved_page); 6167 6168 static int page_alloc_cpu_dead(unsigned int cpu) 6169 { 6170 struct zone *zone; 6171 6172 lru_add_drain_cpu(cpu); 6173 mlock_drain_remote(cpu); 6174 drain_pages(cpu); 6175 6176 /* 6177 * Spill the event counters of the dead processor 6178 * into the current processors event counters. 6179 * This artificially elevates the count of the current 6180 * processor. 6181 */ 6182 vm_events_fold_cpu(cpu); 6183 6184 /* 6185 * Zero the differential counters of the dead processor 6186 * so that the vm statistics are consistent. 6187 * 6188 * This is only okay since the processor is dead and cannot 6189 * race with what we are doing. 6190 */ 6191 cpu_vm_stats_fold(cpu); 6192 6193 for_each_populated_zone(zone) 6194 zone_pcp_update(zone, 0); 6195 6196 return 0; 6197 } 6198 6199 static int page_alloc_cpu_online(unsigned int cpu) 6200 { 6201 struct zone *zone; 6202 6203 for_each_populated_zone(zone) 6204 zone_pcp_update(zone, 1); 6205 return 0; 6206 } 6207 6208 void __init page_alloc_init_cpuhp(void) 6209 { 6210 int ret; 6211 6212 ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC, 6213 "mm/page_alloc:pcp", 6214 page_alloc_cpu_online, 6215 page_alloc_cpu_dead); 6216 WARN_ON(ret < 0); 6217 } 6218 6219 /* 6220 * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio 6221 * or min_free_kbytes changes. 6222 */ 6223 static void calculate_totalreserve_pages(void) 6224 { 6225 struct pglist_data *pgdat; 6226 unsigned long reserve_pages = 0; 6227 enum zone_type i, j; 6228 6229 for_each_online_pgdat(pgdat) { 6230 6231 pgdat->totalreserve_pages = 0; 6232 6233 for (i = 0; i < MAX_NR_ZONES; i++) { 6234 struct zone *zone = pgdat->node_zones + i; 6235 long max = 0; 6236 unsigned long managed_pages = zone_managed_pages(zone); 6237 6238 /* Find valid and maximum lowmem_reserve in the zone */ 6239 for (j = i; j < MAX_NR_ZONES; j++) { 6240 if (zone->lowmem_reserve[j] > max) 6241 max = zone->lowmem_reserve[j]; 6242 } 6243 6244 /* we treat the high watermark as reserved pages. */ 6245 max += high_wmark_pages(zone); 6246 6247 if (max > managed_pages) 6248 max = managed_pages; 6249 6250 pgdat->totalreserve_pages += max; 6251 6252 reserve_pages += max; 6253 } 6254 } 6255 totalreserve_pages = reserve_pages; 6256 trace_mm_calculate_totalreserve_pages(totalreserve_pages); 6257 } 6258 6259 /* 6260 * setup_per_zone_lowmem_reserve - called whenever 6261 * sysctl_lowmem_reserve_ratio changes. Ensures that each zone 6262 * has a correct pages reserved value, so an adequate number of 6263 * pages are left in the zone after a successful __alloc_pages(). 6264 */ 6265 static void setup_per_zone_lowmem_reserve(void) 6266 { 6267 struct pglist_data *pgdat; 6268 enum zone_type i, j; 6269 6270 for_each_online_pgdat(pgdat) { 6271 for (i = 0; i < MAX_NR_ZONES - 1; i++) { 6272 struct zone *zone = &pgdat->node_zones[i]; 6273 int ratio = sysctl_lowmem_reserve_ratio[i]; 6274 bool clear = !ratio || !zone_managed_pages(zone); 6275 unsigned long managed_pages = 0; 6276 6277 for (j = i + 1; j < MAX_NR_ZONES; j++) { 6278 struct zone *upper_zone = &pgdat->node_zones[j]; 6279 6280 managed_pages += zone_managed_pages(upper_zone); 6281 6282 if (clear) 6283 zone->lowmem_reserve[j] = 0; 6284 else 6285 zone->lowmem_reserve[j] = managed_pages / ratio; 6286 trace_mm_setup_per_zone_lowmem_reserve(zone, upper_zone, 6287 zone->lowmem_reserve[j]); 6288 } 6289 } 6290 } 6291 6292 /* update totalreserve_pages */ 6293 calculate_totalreserve_pages(); 6294 } 6295 6296 static void __setup_per_zone_wmarks(void) 6297 { 6298 unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10); 6299 unsigned long lowmem_pages = 0; 6300 struct zone *zone; 6301 unsigned long flags; 6302 6303 /* Calculate total number of !ZONE_HIGHMEM and !ZONE_MOVABLE pages */ 6304 for_each_zone(zone) { 6305 if (!is_highmem(zone) && zone_idx(zone) != ZONE_MOVABLE) 6306 lowmem_pages += zone_managed_pages(zone); 6307 } 6308 6309 for_each_zone(zone) { 6310 u64 tmp; 6311 6312 spin_lock_irqsave(&zone->lock, flags); 6313 tmp = (u64)pages_min * zone_managed_pages(zone); 6314 tmp = div64_ul(tmp, lowmem_pages); 6315 if (is_highmem(zone) || zone_idx(zone) == ZONE_MOVABLE) { 6316 /* 6317 * __GFP_HIGH and PF_MEMALLOC allocations usually don't 6318 * need highmem and movable zones pages, so cap pages_min 6319 * to a small value here. 6320 * 6321 * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN) 6322 * deltas control async page reclaim, and so should 6323 * not be capped for highmem and movable zones. 6324 */ 6325 unsigned long min_pages; 6326 6327 min_pages = zone_managed_pages(zone) / 1024; 6328 min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL); 6329 zone->_watermark[WMARK_MIN] = min_pages; 6330 } else { 6331 /* 6332 * If it's a lowmem zone, reserve a number of pages 6333 * proportionate to the zone's size. 6334 */ 6335 zone->_watermark[WMARK_MIN] = tmp; 6336 } 6337 6338 /* 6339 * Set the kswapd watermarks distance according to the 6340 * scale factor in proportion to available memory, but 6341 * ensure a minimum size on small systems. 6342 */ 6343 tmp = max_t(u64, tmp >> 2, 6344 mult_frac(zone_managed_pages(zone), 6345 watermark_scale_factor, 10000)); 6346 6347 zone->watermark_boost = 0; 6348 zone->_watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp; 6349 zone->_watermark[WMARK_HIGH] = low_wmark_pages(zone) + tmp; 6350 zone->_watermark[WMARK_PROMO] = high_wmark_pages(zone) + tmp; 6351 trace_mm_setup_per_zone_wmarks(zone); 6352 6353 spin_unlock_irqrestore(&zone->lock, flags); 6354 } 6355 6356 /* update totalreserve_pages */ 6357 calculate_totalreserve_pages(); 6358 } 6359 6360 /** 6361 * setup_per_zone_wmarks - called when min_free_kbytes changes 6362 * or when memory is hot-{added|removed} 6363 * 6364 * Ensures that the watermark[min,low,high] values for each zone are set 6365 * correctly with respect to min_free_kbytes. 6366 */ 6367 void setup_per_zone_wmarks(void) 6368 { 6369 struct zone *zone; 6370 static DEFINE_SPINLOCK(lock); 6371 6372 spin_lock(&lock); 6373 __setup_per_zone_wmarks(); 6374 spin_unlock(&lock); 6375 6376 /* 6377 * The watermark size have changed so update the pcpu batch 6378 * and high limits or the limits may be inappropriate. 6379 */ 6380 for_each_zone(zone) 6381 zone_pcp_update(zone, 0); 6382 } 6383 6384 /* 6385 * Initialise min_free_kbytes. 6386 * 6387 * For small machines we want it small (128k min). For large machines 6388 * we want it large (256MB max). But it is not linear, because network 6389 * bandwidth does not increase linearly with machine size. We use 6390 * 6391 * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy: 6392 * min_free_kbytes = sqrt(lowmem_kbytes * 16) 6393 * 6394 * which yields 6395 * 6396 * 16MB: 512k 6397 * 32MB: 724k 6398 * 64MB: 1024k 6399 * 128MB: 1448k 6400 * 256MB: 2048k 6401 * 512MB: 2896k 6402 * 1024MB: 4096k 6403 * 2048MB: 5792k 6404 * 4096MB: 8192k 6405 * 8192MB: 11584k 6406 * 16384MB: 16384k 6407 */ 6408 void calculate_min_free_kbytes(void) 6409 { 6410 unsigned long lowmem_kbytes; 6411 int new_min_free_kbytes; 6412 6413 lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10); 6414 new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16); 6415 6416 if (new_min_free_kbytes > user_min_free_kbytes) 6417 min_free_kbytes = clamp(new_min_free_kbytes, 128, 262144); 6418 else 6419 pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n", 6420 new_min_free_kbytes, user_min_free_kbytes); 6421 6422 } 6423 6424 int __meminit init_per_zone_wmark_min(void) 6425 { 6426 calculate_min_free_kbytes(); 6427 setup_per_zone_wmarks(); 6428 refresh_zone_stat_thresholds(); 6429 setup_per_zone_lowmem_reserve(); 6430 6431 #ifdef CONFIG_NUMA 6432 setup_min_unmapped_ratio(); 6433 setup_min_slab_ratio(); 6434 #endif 6435 6436 khugepaged_min_free_kbytes_update(); 6437 6438 return 0; 6439 } 6440 postcore_initcall(init_per_zone_wmark_min) 6441 6442 /* 6443 * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so 6444 * that we can call two helper functions whenever min_free_kbytes 6445 * changes. 6446 */ 6447 static int min_free_kbytes_sysctl_handler(const struct ctl_table *table, int write, 6448 void *buffer, size_t *length, loff_t *ppos) 6449 { 6450 int rc; 6451 6452 rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 6453 if (rc) 6454 return rc; 6455 6456 if (write) { 6457 user_min_free_kbytes = min_free_kbytes; 6458 setup_per_zone_wmarks(); 6459 } 6460 return 0; 6461 } 6462 6463 static int watermark_scale_factor_sysctl_handler(const struct ctl_table *table, int write, 6464 void *buffer, size_t *length, loff_t *ppos) 6465 { 6466 int rc; 6467 6468 rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 6469 if (rc) 6470 return rc; 6471 6472 if (write) 6473 setup_per_zone_wmarks(); 6474 6475 return 0; 6476 } 6477 6478 #ifdef CONFIG_NUMA 6479 static void setup_min_unmapped_ratio(void) 6480 { 6481 pg_data_t *pgdat; 6482 struct zone *zone; 6483 6484 for_each_online_pgdat(pgdat) 6485 pgdat->min_unmapped_pages = 0; 6486 6487 for_each_zone(zone) 6488 zone->zone_pgdat->min_unmapped_pages += (zone_managed_pages(zone) * 6489 sysctl_min_unmapped_ratio) / 100; 6490 } 6491 6492 6493 static int sysctl_min_unmapped_ratio_sysctl_handler(const struct ctl_table *table, int write, 6494 void *buffer, size_t *length, loff_t *ppos) 6495 { 6496 int rc; 6497 6498 rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 6499 if (rc) 6500 return rc; 6501 6502 setup_min_unmapped_ratio(); 6503 6504 return 0; 6505 } 6506 6507 static void setup_min_slab_ratio(void) 6508 { 6509 pg_data_t *pgdat; 6510 struct zone *zone; 6511 6512 for_each_online_pgdat(pgdat) 6513 pgdat->min_slab_pages = 0; 6514 6515 for_each_zone(zone) 6516 zone->zone_pgdat->min_slab_pages += (zone_managed_pages(zone) * 6517 sysctl_min_slab_ratio) / 100; 6518 } 6519 6520 static int sysctl_min_slab_ratio_sysctl_handler(const struct ctl_table *table, int write, 6521 void *buffer, size_t *length, loff_t *ppos) 6522 { 6523 int rc; 6524 6525 rc = proc_dointvec_minmax(table, write, buffer, length, ppos); 6526 if (rc) 6527 return rc; 6528 6529 setup_min_slab_ratio(); 6530 6531 return 0; 6532 } 6533 #endif 6534 6535 /* 6536 * lowmem_reserve_ratio_sysctl_handler - just a wrapper around 6537 * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve() 6538 * whenever sysctl_lowmem_reserve_ratio changes. 6539 * 6540 * The reserve ratio obviously has absolutely no relation with the 6541 * minimum watermarks. The lowmem reserve ratio can only make sense 6542 * if in function of the boot time zone sizes. 6543 */ 6544 static int lowmem_reserve_ratio_sysctl_handler(const struct ctl_table *table, 6545 int write, void *buffer, size_t *length, loff_t *ppos) 6546 { 6547 int i; 6548 6549 proc_dointvec_minmax(table, write, buffer, length, ppos); 6550 6551 for (i = 0; i < MAX_NR_ZONES; i++) { 6552 if (sysctl_lowmem_reserve_ratio[i] < 1) 6553 sysctl_lowmem_reserve_ratio[i] = 0; 6554 } 6555 6556 setup_per_zone_lowmem_reserve(); 6557 return 0; 6558 } 6559 6560 /* 6561 * percpu_pagelist_high_fraction - changes the pcp->high for each zone on each 6562 * cpu. It is the fraction of total pages in each zone that a hot per cpu 6563 * pagelist can have before it gets flushed back to buddy allocator. 6564 */ 6565 static int percpu_pagelist_high_fraction_sysctl_handler(const struct ctl_table *table, 6566 int write, void *buffer, size_t *length, loff_t *ppos) 6567 { 6568 struct zone *zone; 6569 int old_percpu_pagelist_high_fraction; 6570 int ret; 6571 6572 mutex_lock(&pcp_batch_high_lock); 6573 old_percpu_pagelist_high_fraction = percpu_pagelist_high_fraction; 6574 6575 ret = proc_dointvec_minmax(table, write, buffer, length, ppos); 6576 if (!write || ret < 0) 6577 goto out; 6578 6579 /* Sanity checking to avoid pcp imbalance */ 6580 if (percpu_pagelist_high_fraction && 6581 percpu_pagelist_high_fraction < MIN_PERCPU_PAGELIST_HIGH_FRACTION) { 6582 percpu_pagelist_high_fraction = old_percpu_pagelist_high_fraction; 6583 ret = -EINVAL; 6584 goto out; 6585 } 6586 6587 /* No change? */ 6588 if (percpu_pagelist_high_fraction == old_percpu_pagelist_high_fraction) 6589 goto out; 6590 6591 for_each_populated_zone(zone) 6592 zone_set_pageset_high_and_batch(zone, 0); 6593 out: 6594 mutex_unlock(&pcp_batch_high_lock); 6595 return ret; 6596 } 6597 6598 static const struct ctl_table page_alloc_sysctl_table[] = { 6599 { 6600 .procname = "min_free_kbytes", 6601 .data = &min_free_kbytes, 6602 .maxlen = sizeof(min_free_kbytes), 6603 .mode = 0644, 6604 .proc_handler = min_free_kbytes_sysctl_handler, 6605 .extra1 = SYSCTL_ZERO, 6606 }, 6607 { 6608 .procname = "watermark_boost_factor", 6609 .data = &watermark_boost_factor, 6610 .maxlen = sizeof(watermark_boost_factor), 6611 .mode = 0644, 6612 .proc_handler = proc_dointvec_minmax, 6613 .extra1 = SYSCTL_ZERO, 6614 }, 6615 { 6616 .procname = "watermark_scale_factor", 6617 .data = &watermark_scale_factor, 6618 .maxlen = sizeof(watermark_scale_factor), 6619 .mode = 0644, 6620 .proc_handler = watermark_scale_factor_sysctl_handler, 6621 .extra1 = SYSCTL_ONE, 6622 .extra2 = SYSCTL_THREE_THOUSAND, 6623 }, 6624 { 6625 .procname = "defrag_mode", 6626 .data = &defrag_mode, 6627 .maxlen = sizeof(defrag_mode), 6628 .mode = 0644, 6629 .proc_handler = proc_dointvec_minmax, 6630 .extra1 = SYSCTL_ZERO, 6631 .extra2 = SYSCTL_ONE, 6632 }, 6633 { 6634 .procname = "percpu_pagelist_high_fraction", 6635 .data = &percpu_pagelist_high_fraction, 6636 .maxlen = sizeof(percpu_pagelist_high_fraction), 6637 .mode = 0644, 6638 .proc_handler = percpu_pagelist_high_fraction_sysctl_handler, 6639 .extra1 = SYSCTL_ZERO, 6640 }, 6641 { 6642 .procname = "lowmem_reserve_ratio", 6643 .data = &sysctl_lowmem_reserve_ratio, 6644 .maxlen = sizeof(sysctl_lowmem_reserve_ratio), 6645 .mode = 0644, 6646 .proc_handler = lowmem_reserve_ratio_sysctl_handler, 6647 }, 6648 #ifdef CONFIG_NUMA 6649 { 6650 .procname = "numa_zonelist_order", 6651 .data = &numa_zonelist_order, 6652 .maxlen = NUMA_ZONELIST_ORDER_LEN, 6653 .mode = 0644, 6654 .proc_handler = numa_zonelist_order_handler, 6655 }, 6656 { 6657 .procname = "min_unmapped_ratio", 6658 .data = &sysctl_min_unmapped_ratio, 6659 .maxlen = sizeof(sysctl_min_unmapped_ratio), 6660 .mode = 0644, 6661 .proc_handler = sysctl_min_unmapped_ratio_sysctl_handler, 6662 .extra1 = SYSCTL_ZERO, 6663 .extra2 = SYSCTL_ONE_HUNDRED, 6664 }, 6665 { 6666 .procname = "min_slab_ratio", 6667 .data = &sysctl_min_slab_ratio, 6668 .maxlen = sizeof(sysctl_min_slab_ratio), 6669 .mode = 0644, 6670 .proc_handler = sysctl_min_slab_ratio_sysctl_handler, 6671 .extra1 = SYSCTL_ZERO, 6672 .extra2 = SYSCTL_ONE_HUNDRED, 6673 }, 6674 #endif 6675 }; 6676 6677 void __init page_alloc_sysctl_init(void) 6678 { 6679 register_sysctl_init("vm", page_alloc_sysctl_table); 6680 } 6681 6682 #ifdef CONFIG_CONTIG_ALLOC 6683 /* Usage: See admin-guide/dynamic-debug-howto.rst */ 6684 static void alloc_contig_dump_pages(struct list_head *page_list) 6685 { 6686 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, "migrate failure"); 6687 6688 if (DYNAMIC_DEBUG_BRANCH(descriptor)) { 6689 struct page *page; 6690 6691 dump_stack(); 6692 list_for_each_entry(page, page_list, lru) 6693 dump_page(page, "migration failure"); 6694 } 6695 } 6696 6697 /* [start, end) must belong to a single zone. */ 6698 static int __alloc_contig_migrate_range(struct compact_control *cc, 6699 unsigned long start, unsigned long end) 6700 { 6701 /* This function is based on compact_zone() from compaction.c. */ 6702 unsigned int nr_reclaimed; 6703 unsigned long pfn = start; 6704 unsigned int tries = 0; 6705 int ret = 0; 6706 struct migration_target_control mtc = { 6707 .nid = zone_to_nid(cc->zone), 6708 .gfp_mask = cc->gfp_mask, 6709 .reason = MR_CONTIG_RANGE, 6710 }; 6711 6712 lru_cache_disable(); 6713 6714 while (pfn < end || !list_empty(&cc->migratepages)) { 6715 if (fatal_signal_pending(current)) { 6716 ret = -EINTR; 6717 break; 6718 } 6719 6720 if (list_empty(&cc->migratepages)) { 6721 cc->nr_migratepages = 0; 6722 ret = isolate_migratepages_range(cc, pfn, end); 6723 if (ret && ret != -EAGAIN) 6724 break; 6725 pfn = cc->migrate_pfn; 6726 tries = 0; 6727 } else if (++tries == 5) { 6728 ret = -EBUSY; 6729 break; 6730 } 6731 6732 nr_reclaimed = reclaim_clean_pages_from_list(cc->zone, 6733 &cc->migratepages); 6734 cc->nr_migratepages -= nr_reclaimed; 6735 6736 ret = migrate_pages(&cc->migratepages, alloc_migration_target, 6737 NULL, (unsigned long)&mtc, cc->mode, MR_CONTIG_RANGE, NULL); 6738 6739 /* 6740 * On -ENOMEM, migrate_pages() bails out right away. It is pointless 6741 * to retry again over this error, so do the same here. 6742 */ 6743 if (ret == -ENOMEM) 6744 break; 6745 } 6746 6747 lru_cache_enable(); 6748 if (ret < 0) { 6749 if (!(cc->gfp_mask & __GFP_NOWARN) && ret == -EBUSY) 6750 alloc_contig_dump_pages(&cc->migratepages); 6751 putback_movable_pages(&cc->migratepages); 6752 } 6753 6754 return (ret < 0) ? ret : 0; 6755 } 6756 6757 static void split_free_pages(struct list_head *list, gfp_t gfp_mask) 6758 { 6759 int order; 6760 6761 for (order = 0; order < NR_PAGE_ORDERS; order++) { 6762 struct page *page, *next; 6763 int nr_pages = 1 << order; 6764 6765 list_for_each_entry_safe(page, next, &list[order], lru) { 6766 int i; 6767 6768 post_alloc_hook(page, order, gfp_mask); 6769 set_page_refcounted(page); 6770 if (!order) 6771 continue; 6772 6773 split_page(page, order); 6774 6775 /* Add all subpages to the order-0 head, in sequence. */ 6776 list_del(&page->lru); 6777 for (i = 0; i < nr_pages; i++) 6778 list_add_tail(&page[i].lru, &list[0]); 6779 } 6780 } 6781 } 6782 6783 static int __alloc_contig_verify_gfp_mask(gfp_t gfp_mask, gfp_t *gfp_cc_mask) 6784 { 6785 const gfp_t reclaim_mask = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 6786 const gfp_t action_mask = __GFP_COMP | __GFP_RETRY_MAYFAIL | __GFP_NOWARN | 6787 __GFP_ZERO | __GFP_ZEROTAGS | __GFP_SKIP_ZERO; 6788 const gfp_t cc_action_mask = __GFP_RETRY_MAYFAIL | __GFP_NOWARN; 6789 6790 /* 6791 * We are given the range to allocate; node, mobility and placement 6792 * hints are irrelevant at this point. We'll simply ignore them. 6793 */ 6794 gfp_mask &= ~(GFP_ZONEMASK | __GFP_RECLAIMABLE | __GFP_WRITE | 6795 __GFP_HARDWALL | __GFP_THISNODE | __GFP_MOVABLE); 6796 6797 /* 6798 * We only support most reclaim flags (but not NOFAIL/NORETRY), and 6799 * selected action flags. 6800 */ 6801 if (gfp_mask & ~(reclaim_mask | action_mask)) 6802 return -EINVAL; 6803 6804 /* 6805 * Flags to control page compaction/migration/reclaim, to free up our 6806 * page range. Migratable pages are movable, __GFP_MOVABLE is implied 6807 * for them. 6808 * 6809 * Traditionally we always had __GFP_RETRY_MAYFAIL set, keep doing that 6810 * to not degrade callers. 6811 */ 6812 *gfp_cc_mask = (gfp_mask & (reclaim_mask | cc_action_mask)) | 6813 __GFP_MOVABLE | __GFP_RETRY_MAYFAIL; 6814 return 0; 6815 } 6816 6817 /** 6818 * alloc_contig_range() -- tries to allocate given range of pages 6819 * @start: start PFN to allocate 6820 * @end: one-past-the-last PFN to allocate 6821 * @alloc_flags: allocation information 6822 * @gfp_mask: GFP mask. Node/zone/placement hints are ignored; only some 6823 * action and reclaim modifiers are supported. Reclaim modifiers 6824 * control allocation behavior during compaction/migration/reclaim. 6825 * 6826 * The PFN range does not have to be pageblock aligned. The PFN range must 6827 * belong to a single zone. 6828 * 6829 * The first thing this routine does is attempt to MIGRATE_ISOLATE all 6830 * pageblocks in the range. Once isolated, the pageblocks should not 6831 * be modified by others. 6832 * 6833 * Return: zero on success or negative error code. On success all 6834 * pages which PFN is in [start, end) are allocated for the caller and 6835 * need to be freed with free_contig_range(). 6836 */ 6837 int alloc_contig_range_noprof(unsigned long start, unsigned long end, 6838 acr_flags_t alloc_flags, gfp_t gfp_mask) 6839 { 6840 unsigned long outer_start, outer_end; 6841 int ret = 0; 6842 6843 struct compact_control cc = { 6844 .nr_migratepages = 0, 6845 .order = -1, 6846 .zone = page_zone(pfn_to_page(start)), 6847 .mode = MIGRATE_SYNC, 6848 .ignore_skip_hint = true, 6849 .no_set_skip_hint = true, 6850 .alloc_contig = true, 6851 }; 6852 INIT_LIST_HEAD(&cc.migratepages); 6853 enum pb_isolate_mode mode = (alloc_flags & ACR_FLAGS_CMA) ? 6854 PB_ISOLATE_MODE_CMA_ALLOC : 6855 PB_ISOLATE_MODE_OTHER; 6856 6857 gfp_mask = current_gfp_context(gfp_mask); 6858 if (__alloc_contig_verify_gfp_mask(gfp_mask, (gfp_t *)&cc.gfp_mask)) 6859 return -EINVAL; 6860 6861 /* 6862 * What we do here is we mark all pageblocks in range as 6863 * MIGRATE_ISOLATE. Because pageblock and max order pages may 6864 * have different sizes, and due to the way page allocator 6865 * work, start_isolate_page_range() has special handlings for this. 6866 * 6867 * Once the pageblocks are marked as MIGRATE_ISOLATE, we 6868 * migrate the pages from an unaligned range (ie. pages that 6869 * we are interested in). This will put all the pages in 6870 * range back to page allocator as MIGRATE_ISOLATE. 6871 * 6872 * When this is done, we take the pages in range from page 6873 * allocator removing them from the buddy system. This way 6874 * page allocator will never consider using them. 6875 * 6876 * This lets us mark the pageblocks back as 6877 * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the 6878 * aligned range but not in the unaligned, original range are 6879 * put back to page allocator so that buddy can use them. 6880 */ 6881 6882 ret = start_isolate_page_range(start, end, mode); 6883 if (ret) 6884 goto done; 6885 6886 drain_all_pages(cc.zone); 6887 6888 /* 6889 * In case of -EBUSY, we'd like to know which page causes problem. 6890 * So, just fall through. test_pages_isolated() has a tracepoint 6891 * which will report the busy page. 6892 * 6893 * It is possible that busy pages could become available before 6894 * the call to test_pages_isolated, and the range will actually be 6895 * allocated. So, if we fall through be sure to clear ret so that 6896 * -EBUSY is not accidentally used or returned to caller. 6897 */ 6898 ret = __alloc_contig_migrate_range(&cc, start, end); 6899 if (ret && ret != -EBUSY) 6900 goto done; 6901 6902 /* 6903 * When in-use hugetlb pages are migrated, they may simply be released 6904 * back into the free hugepage pool instead of being returned to the 6905 * buddy system. After the migration of in-use huge pages is completed, 6906 * we will invoke replace_free_hugepage_folios() to ensure that these 6907 * hugepages are properly released to the buddy system. 6908 */ 6909 ret = replace_free_hugepage_folios(start, end); 6910 if (ret) 6911 goto done; 6912 6913 /* 6914 * Pages from [start, end) are within a pageblock_nr_pages 6915 * aligned blocks that are marked as MIGRATE_ISOLATE. What's 6916 * more, all pages in [start, end) are free in page allocator. 6917 * What we are going to do is to allocate all pages from 6918 * [start, end) (that is remove them from page allocator). 6919 * 6920 * The only problem is that pages at the beginning and at the 6921 * end of interesting range may be not aligned with pages that 6922 * page allocator holds, ie. they can be part of higher order 6923 * pages. Because of this, we reserve the bigger range and 6924 * once this is done free the pages we are not interested in. 6925 * 6926 * We don't have to hold zone->lock here because the pages are 6927 * isolated thus they won't get removed from buddy. 6928 */ 6929 outer_start = find_large_buddy(start); 6930 6931 /* Make sure the range is really isolated. */ 6932 if (test_pages_isolated(outer_start, end, mode)) { 6933 ret = -EBUSY; 6934 goto done; 6935 } 6936 6937 /* Grab isolated pages from freelists. */ 6938 outer_end = isolate_freepages_range(&cc, outer_start, end); 6939 if (!outer_end) { 6940 ret = -EBUSY; 6941 goto done; 6942 } 6943 6944 if (!(gfp_mask & __GFP_COMP)) { 6945 split_free_pages(cc.freepages, gfp_mask); 6946 6947 /* Free head and tail (if any) */ 6948 if (start != outer_start) 6949 free_contig_range(outer_start, start - outer_start); 6950 if (end != outer_end) 6951 free_contig_range(end, outer_end - end); 6952 } else if (start == outer_start && end == outer_end && is_power_of_2(end - start)) { 6953 struct page *head = pfn_to_page(start); 6954 int order = ilog2(end - start); 6955 6956 check_new_pages(head, order); 6957 prep_new_page(head, order, gfp_mask, 0); 6958 set_page_refcounted(head); 6959 } else { 6960 ret = -EINVAL; 6961 WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n", 6962 start, end, outer_start, outer_end); 6963 } 6964 done: 6965 undo_isolate_page_range(start, end); 6966 return ret; 6967 } 6968 EXPORT_SYMBOL(alloc_contig_range_noprof); 6969 6970 static int __alloc_contig_pages(unsigned long start_pfn, 6971 unsigned long nr_pages, gfp_t gfp_mask) 6972 { 6973 unsigned long end_pfn = start_pfn + nr_pages; 6974 6975 return alloc_contig_range_noprof(start_pfn, end_pfn, ACR_FLAGS_NONE, 6976 gfp_mask); 6977 } 6978 6979 static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, 6980 unsigned long nr_pages) 6981 { 6982 unsigned long i, end_pfn = start_pfn + nr_pages; 6983 struct page *page; 6984 6985 for (i = start_pfn; i < end_pfn; i++) { 6986 page = pfn_to_online_page(i); 6987 if (!page) 6988 return false; 6989 6990 if (page_zone(page) != z) 6991 return false; 6992 6993 if (PageReserved(page)) 6994 return false; 6995 6996 if (PageHuge(page)) 6997 return false; 6998 } 6999 return true; 7000 } 7001 7002 static bool zone_spans_last_pfn(const struct zone *zone, 7003 unsigned long start_pfn, unsigned long nr_pages) 7004 { 7005 unsigned long last_pfn = start_pfn + nr_pages - 1; 7006 7007 return zone_spans_pfn(zone, last_pfn); 7008 } 7009 7010 /** 7011 * alloc_contig_pages() -- tries to find and allocate contiguous range of pages 7012 * @nr_pages: Number of contiguous pages to allocate 7013 * @gfp_mask: GFP mask. Node/zone/placement hints limit the search; only some 7014 * action and reclaim modifiers are supported. Reclaim modifiers 7015 * control allocation behavior during compaction/migration/reclaim. 7016 * @nid: Target node 7017 * @nodemask: Mask for other possible nodes 7018 * 7019 * This routine is a wrapper around alloc_contig_range(). It scans over zones 7020 * on an applicable zonelist to find a contiguous pfn range which can then be 7021 * tried for allocation with alloc_contig_range(). This routine is intended 7022 * for allocation requests which can not be fulfilled with the buddy allocator. 7023 * 7024 * The allocated memory is always aligned to a page boundary. If nr_pages is a 7025 * power of two, then allocated range is also guaranteed to be aligned to same 7026 * nr_pages (e.g. 1GB request would be aligned to 1GB). 7027 * 7028 * Allocated pages can be freed with free_contig_range() or by manually calling 7029 * __free_page() on each allocated page. 7030 * 7031 * Return: pointer to contiguous pages on success, or NULL if not successful. 7032 */ 7033 struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, 7034 int nid, nodemask_t *nodemask) 7035 { 7036 unsigned long ret, pfn, flags; 7037 struct zonelist *zonelist; 7038 struct zone *zone; 7039 struct zoneref *z; 7040 7041 zonelist = node_zonelist(nid, gfp_mask); 7042 for_each_zone_zonelist_nodemask(zone, z, zonelist, 7043 gfp_zone(gfp_mask), nodemask) { 7044 spin_lock_irqsave(&zone->lock, flags); 7045 7046 pfn = ALIGN(zone->zone_start_pfn, nr_pages); 7047 while (zone_spans_last_pfn(zone, pfn, nr_pages)) { 7048 if (pfn_range_valid_contig(zone, pfn, nr_pages)) { 7049 /* 7050 * We release the zone lock here because 7051 * alloc_contig_range() will also lock the zone 7052 * at some point. If there's an allocation 7053 * spinning on this lock, it may win the race 7054 * and cause alloc_contig_range() to fail... 7055 */ 7056 spin_unlock_irqrestore(&zone->lock, flags); 7057 ret = __alloc_contig_pages(pfn, nr_pages, 7058 gfp_mask); 7059 if (!ret) 7060 return pfn_to_page(pfn); 7061 spin_lock_irqsave(&zone->lock, flags); 7062 } 7063 pfn += nr_pages; 7064 } 7065 spin_unlock_irqrestore(&zone->lock, flags); 7066 } 7067 return NULL; 7068 } 7069 #endif /* CONFIG_CONTIG_ALLOC */ 7070 7071 void free_contig_range(unsigned long pfn, unsigned long nr_pages) 7072 { 7073 unsigned long count = 0; 7074 struct folio *folio = pfn_folio(pfn); 7075 7076 if (folio_test_large(folio)) { 7077 int expected = folio_nr_pages(folio); 7078 7079 if (nr_pages == expected) 7080 folio_put(folio); 7081 else 7082 WARN(true, "PFN %lu: nr_pages %lu != expected %d\n", 7083 pfn, nr_pages, expected); 7084 return; 7085 } 7086 7087 for (; nr_pages--; pfn++) { 7088 struct page *page = pfn_to_page(pfn); 7089 7090 count += page_count(page) != 1; 7091 __free_page(page); 7092 } 7093 WARN(count != 0, "%lu pages are still in use!\n", count); 7094 } 7095 EXPORT_SYMBOL(free_contig_range); 7096 7097 /* 7098 * Effectively disable pcplists for the zone by setting the high limit to 0 7099 * and draining all cpus. A concurrent page freeing on another CPU that's about 7100 * to put the page on pcplist will either finish before the drain and the page 7101 * will be drained, or observe the new high limit and skip the pcplist. 7102 * 7103 * Must be paired with a call to zone_pcp_enable(). 7104 */ 7105 void zone_pcp_disable(struct zone *zone) 7106 { 7107 mutex_lock(&pcp_batch_high_lock); 7108 __zone_set_pageset_high_and_batch(zone, 0, 0, 1); 7109 __drain_all_pages(zone, true); 7110 } 7111 7112 void zone_pcp_enable(struct zone *zone) 7113 { 7114 __zone_set_pageset_high_and_batch(zone, zone->pageset_high_min, 7115 zone->pageset_high_max, zone->pageset_batch); 7116 mutex_unlock(&pcp_batch_high_lock); 7117 } 7118 7119 void zone_pcp_reset(struct zone *zone) 7120 { 7121 int cpu; 7122 struct per_cpu_zonestat *pzstats; 7123 7124 if (zone->per_cpu_pageset != &boot_pageset) { 7125 for_each_online_cpu(cpu) { 7126 pzstats = per_cpu_ptr(zone->per_cpu_zonestats, cpu); 7127 drain_zonestat(zone, pzstats); 7128 } 7129 free_percpu(zone->per_cpu_pageset); 7130 zone->per_cpu_pageset = &boot_pageset; 7131 if (zone->per_cpu_zonestats != &boot_zonestats) { 7132 free_percpu(zone->per_cpu_zonestats); 7133 zone->per_cpu_zonestats = &boot_zonestats; 7134 } 7135 } 7136 } 7137 7138 #ifdef CONFIG_MEMORY_HOTREMOVE 7139 /* 7140 * All pages in the range must be in a single zone, must not contain holes, 7141 * must span full sections, and must be isolated before calling this function. 7142 * 7143 * Returns the number of managed (non-PageOffline()) pages in the range: the 7144 * number of pages for which memory offlining code must adjust managed page 7145 * counters using adjust_managed_page_count(). 7146 */ 7147 unsigned long __offline_isolated_pages(unsigned long start_pfn, 7148 unsigned long end_pfn) 7149 { 7150 unsigned long already_offline = 0, flags; 7151 unsigned long pfn = start_pfn; 7152 struct page *page; 7153 struct zone *zone; 7154 unsigned int order; 7155 7156 offline_mem_sections(pfn, end_pfn); 7157 zone = page_zone(pfn_to_page(pfn)); 7158 spin_lock_irqsave(&zone->lock, flags); 7159 while (pfn < end_pfn) { 7160 page = pfn_to_page(pfn); 7161 /* 7162 * The HWPoisoned page may be not in buddy system, and 7163 * page_count() is not 0. 7164 */ 7165 if (unlikely(!PageBuddy(page) && PageHWPoison(page))) { 7166 pfn++; 7167 continue; 7168 } 7169 /* 7170 * At this point all remaining PageOffline() pages have a 7171 * reference count of 0 and can simply be skipped. 7172 */ 7173 if (PageOffline(page)) { 7174 BUG_ON(page_count(page)); 7175 BUG_ON(PageBuddy(page)); 7176 already_offline++; 7177 pfn++; 7178 continue; 7179 } 7180 7181 BUG_ON(page_count(page)); 7182 BUG_ON(!PageBuddy(page)); 7183 VM_WARN_ON(get_pageblock_migratetype(page) != MIGRATE_ISOLATE); 7184 order = buddy_order(page); 7185 del_page_from_free_list(page, zone, order, MIGRATE_ISOLATE); 7186 pfn += (1 << order); 7187 } 7188 spin_unlock_irqrestore(&zone->lock, flags); 7189 7190 return end_pfn - start_pfn - already_offline; 7191 } 7192 #endif 7193 7194 /* 7195 * This function returns a stable result only if called under zone lock. 7196 */ 7197 bool is_free_buddy_page(const struct page *page) 7198 { 7199 unsigned long pfn = page_to_pfn(page); 7200 unsigned int order; 7201 7202 for (order = 0; order < NR_PAGE_ORDERS; order++) { 7203 const struct page *head = page - (pfn & ((1 << order) - 1)); 7204 7205 if (PageBuddy(head) && 7206 buddy_order_unsafe(head) >= order) 7207 break; 7208 } 7209 7210 return order <= MAX_PAGE_ORDER; 7211 } 7212 EXPORT_SYMBOL(is_free_buddy_page); 7213 7214 #ifdef CONFIG_MEMORY_FAILURE 7215 static inline void add_to_free_list(struct page *page, struct zone *zone, 7216 unsigned int order, int migratetype, 7217 bool tail) 7218 { 7219 __add_to_free_list(page, zone, order, migratetype, tail); 7220 account_freepages(zone, 1 << order, migratetype); 7221 } 7222 7223 /* 7224 * Break down a higher-order page in sub-pages, and keep our target out of 7225 * buddy allocator. 7226 */ 7227 static void break_down_buddy_pages(struct zone *zone, struct page *page, 7228 struct page *target, int low, int high, 7229 int migratetype) 7230 { 7231 unsigned long size = 1 << high; 7232 struct page *current_buddy; 7233 7234 while (high > low) { 7235 high--; 7236 size >>= 1; 7237 7238 if (target >= &page[size]) { 7239 current_buddy = page; 7240 page = page + size; 7241 } else { 7242 current_buddy = page + size; 7243 } 7244 7245 if (set_page_guard(zone, current_buddy, high)) 7246 continue; 7247 7248 add_to_free_list(current_buddy, zone, high, migratetype, false); 7249 set_buddy_order(current_buddy, high); 7250 } 7251 } 7252 7253 /* 7254 * Take a page that will be marked as poisoned off the buddy allocator. 7255 */ 7256 bool take_page_off_buddy(struct page *page) 7257 { 7258 struct zone *zone = page_zone(page); 7259 unsigned long pfn = page_to_pfn(page); 7260 unsigned long flags; 7261 unsigned int order; 7262 bool ret = false; 7263 7264 spin_lock_irqsave(&zone->lock, flags); 7265 for (order = 0; order < NR_PAGE_ORDERS; order++) { 7266 struct page *page_head = page - (pfn & ((1 << order) - 1)); 7267 int page_order = buddy_order(page_head); 7268 7269 if (PageBuddy(page_head) && page_order >= order) { 7270 unsigned long pfn_head = page_to_pfn(page_head); 7271 int migratetype = get_pfnblock_migratetype(page_head, 7272 pfn_head); 7273 7274 del_page_from_free_list(page_head, zone, page_order, 7275 migratetype); 7276 break_down_buddy_pages(zone, page_head, page, 0, 7277 page_order, migratetype); 7278 SetPageHWPoisonTakenOff(page); 7279 ret = true; 7280 break; 7281 } 7282 if (page_count(page_head) > 0) 7283 break; 7284 } 7285 spin_unlock_irqrestore(&zone->lock, flags); 7286 return ret; 7287 } 7288 7289 /* 7290 * Cancel takeoff done by take_page_off_buddy(). 7291 */ 7292 bool put_page_back_buddy(struct page *page) 7293 { 7294 struct zone *zone = page_zone(page); 7295 unsigned long flags; 7296 bool ret = false; 7297 7298 spin_lock_irqsave(&zone->lock, flags); 7299 if (put_page_testzero(page)) { 7300 unsigned long pfn = page_to_pfn(page); 7301 int migratetype = get_pfnblock_migratetype(page, pfn); 7302 7303 ClearPageHWPoisonTakenOff(page); 7304 __free_one_page(page, pfn, zone, 0, migratetype, FPI_NONE); 7305 if (TestClearPageHWPoison(page)) { 7306 ret = true; 7307 } 7308 } 7309 spin_unlock_irqrestore(&zone->lock, flags); 7310 7311 return ret; 7312 } 7313 #endif 7314 7315 #ifdef CONFIG_ZONE_DMA 7316 bool has_managed_dma(void) 7317 { 7318 struct pglist_data *pgdat; 7319 7320 for_each_online_pgdat(pgdat) { 7321 struct zone *zone = &pgdat->node_zones[ZONE_DMA]; 7322 7323 if (managed_zone(zone)) 7324 return true; 7325 } 7326 return false; 7327 } 7328 #endif /* CONFIG_ZONE_DMA */ 7329 7330 #ifdef CONFIG_UNACCEPTED_MEMORY 7331 7332 static bool lazy_accept = true; 7333 7334 static int __init accept_memory_parse(char *p) 7335 { 7336 if (!strcmp(p, "lazy")) { 7337 lazy_accept = true; 7338 return 0; 7339 } else if (!strcmp(p, "eager")) { 7340 lazy_accept = false; 7341 return 0; 7342 } else { 7343 return -EINVAL; 7344 } 7345 } 7346 early_param("accept_memory", accept_memory_parse); 7347 7348 static bool page_contains_unaccepted(struct page *page, unsigned int order) 7349 { 7350 phys_addr_t start = page_to_phys(page); 7351 7352 return range_contains_unaccepted_memory(start, PAGE_SIZE << order); 7353 } 7354 7355 static void __accept_page(struct zone *zone, unsigned long *flags, 7356 struct page *page) 7357 { 7358 list_del(&page->lru); 7359 account_freepages(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); 7360 __mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES); 7361 __ClearPageUnaccepted(page); 7362 spin_unlock_irqrestore(&zone->lock, *flags); 7363 7364 accept_memory(page_to_phys(page), PAGE_SIZE << MAX_PAGE_ORDER); 7365 7366 __free_pages_ok(page, MAX_PAGE_ORDER, FPI_TO_TAIL); 7367 } 7368 7369 void accept_page(struct page *page) 7370 { 7371 struct zone *zone = page_zone(page); 7372 unsigned long flags; 7373 7374 spin_lock_irqsave(&zone->lock, flags); 7375 if (!PageUnaccepted(page)) { 7376 spin_unlock_irqrestore(&zone->lock, flags); 7377 return; 7378 } 7379 7380 /* Unlocks zone->lock */ 7381 __accept_page(zone, &flags, page); 7382 } 7383 7384 static bool try_to_accept_memory_one(struct zone *zone) 7385 { 7386 unsigned long flags; 7387 struct page *page; 7388 7389 spin_lock_irqsave(&zone->lock, flags); 7390 page = list_first_entry_or_null(&zone->unaccepted_pages, 7391 struct page, lru); 7392 if (!page) { 7393 spin_unlock_irqrestore(&zone->lock, flags); 7394 return false; 7395 } 7396 7397 /* Unlocks zone->lock */ 7398 __accept_page(zone, &flags, page); 7399 7400 return true; 7401 } 7402 7403 static bool cond_accept_memory(struct zone *zone, unsigned int order, 7404 int alloc_flags) 7405 { 7406 long to_accept, wmark; 7407 bool ret = false; 7408 7409 if (list_empty(&zone->unaccepted_pages)) 7410 return false; 7411 7412 /* Bailout, since try_to_accept_memory_one() needs to take a lock */ 7413 if (alloc_flags & ALLOC_TRYLOCK) 7414 return false; 7415 7416 wmark = promo_wmark_pages(zone); 7417 7418 /* 7419 * Watermarks have not been initialized yet. 7420 * 7421 * Accepting one MAX_ORDER page to ensure progress. 7422 */ 7423 if (!wmark) 7424 return try_to_accept_memory_one(zone); 7425 7426 /* How much to accept to get to promo watermark? */ 7427 to_accept = wmark - 7428 (zone_page_state(zone, NR_FREE_PAGES) - 7429 __zone_watermark_unusable_free(zone, order, 0) - 7430 zone_page_state(zone, NR_UNACCEPTED)); 7431 7432 while (to_accept > 0) { 7433 if (!try_to_accept_memory_one(zone)) 7434 break; 7435 ret = true; 7436 to_accept -= MAX_ORDER_NR_PAGES; 7437 } 7438 7439 return ret; 7440 } 7441 7442 static bool __free_unaccepted(struct page *page) 7443 { 7444 struct zone *zone = page_zone(page); 7445 unsigned long flags; 7446 7447 if (!lazy_accept) 7448 return false; 7449 7450 spin_lock_irqsave(&zone->lock, flags); 7451 list_add_tail(&page->lru, &zone->unaccepted_pages); 7452 account_freepages(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); 7453 __mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES); 7454 __SetPageUnaccepted(page); 7455 spin_unlock_irqrestore(&zone->lock, flags); 7456 7457 return true; 7458 } 7459 7460 #else 7461 7462 static bool page_contains_unaccepted(struct page *page, unsigned int order) 7463 { 7464 return false; 7465 } 7466 7467 static bool cond_accept_memory(struct zone *zone, unsigned int order, 7468 int alloc_flags) 7469 { 7470 return false; 7471 } 7472 7473 static bool __free_unaccepted(struct page *page) 7474 { 7475 BUILD_BUG(); 7476 return false; 7477 } 7478 7479 #endif /* CONFIG_UNACCEPTED_MEMORY */ 7480 7481 /** 7482 * alloc_pages_nolock - opportunistic reentrant allocation from any context 7483 * @nid: node to allocate from 7484 * @order: allocation order size 7485 * 7486 * Allocates pages of a given order from the given node. This is safe to 7487 * call from any context (from atomic, NMI, and also reentrant 7488 * allocator -> tracepoint -> alloc_pages_nolock_noprof). 7489 * Allocation is best effort and to be expected to fail easily so nobody should 7490 * rely on the success. Failures are not reported via warn_alloc(). 7491 * See always fail conditions below. 7492 * 7493 * Return: allocated page or NULL on failure. NULL does not mean EBUSY or EAGAIN. 7494 * It means ENOMEM. There is no reason to call it again and expect !NULL. 7495 */ 7496 struct page *alloc_pages_nolock_noprof(int nid, unsigned int order) 7497 { 7498 /* 7499 * Do not specify __GFP_DIRECT_RECLAIM, since direct claim is not allowed. 7500 * Do not specify __GFP_KSWAPD_RECLAIM either, since wake up of kswapd 7501 * is not safe in arbitrary context. 7502 * 7503 * These two are the conditions for gfpflags_allow_spinning() being true. 7504 * 7505 * Specify __GFP_NOWARN since failing alloc_pages_nolock() is not a reason 7506 * to warn. Also warn would trigger printk() which is unsafe from 7507 * various contexts. We cannot use printk_deferred_enter() to mitigate, 7508 * since the running context is unknown. 7509 * 7510 * Specify __GFP_ZERO to make sure that call to kmsan_alloc_page() below 7511 * is safe in any context. Also zeroing the page is mandatory for 7512 * BPF use cases. 7513 * 7514 * Though __GFP_NOMEMALLOC is not checked in the code path below, 7515 * specify it here to highlight that alloc_pages_nolock() 7516 * doesn't want to deplete reserves. 7517 */ 7518 gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC 7519 | __GFP_ACCOUNT; 7520 unsigned int alloc_flags = ALLOC_TRYLOCK; 7521 struct alloc_context ac = { }; 7522 struct page *page; 7523 7524 /* 7525 * In PREEMPT_RT spin_trylock() will call raw_spin_lock() which is 7526 * unsafe in NMI. If spin_trylock() is called from hard IRQ the current 7527 * task may be waiting for one rt_spin_lock, but rt_spin_trylock() will 7528 * mark the task as the owner of another rt_spin_lock which will 7529 * confuse PI logic, so return immediately if called form hard IRQ or 7530 * NMI. 7531 * 7532 * Note, irqs_disabled() case is ok. This function can be called 7533 * from raw_spin_lock_irqsave region. 7534 */ 7535 if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())) 7536 return NULL; 7537 if (!pcp_allowed_order(order)) 7538 return NULL; 7539 7540 /* Bailout, since _deferred_grow_zone() needs to take a lock */ 7541 if (deferred_pages_enabled()) 7542 return NULL; 7543 7544 if (nid == NUMA_NO_NODE) 7545 nid = numa_node_id(); 7546 7547 prepare_alloc_pages(alloc_gfp, order, nid, NULL, &ac, 7548 &alloc_gfp, &alloc_flags); 7549 7550 /* 7551 * Best effort allocation from percpu free list. 7552 * If it's empty attempt to spin_trylock zone->lock. 7553 */ 7554 page = get_page_from_freelist(alloc_gfp, order, alloc_flags, &ac); 7555 7556 /* Unlike regular alloc_pages() there is no __alloc_pages_slowpath(). */ 7557 7558 if (page) 7559 set_page_refcounted(page); 7560 7561 if (memcg_kmem_online() && page && 7562 unlikely(__memcg_kmem_charge_page(page, alloc_gfp, order) != 0)) { 7563 free_pages_nolock(page, order); 7564 page = NULL; 7565 } 7566 trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); 7567 kmsan_alloc_page(page, order, alloc_gfp); 7568 return page; 7569 } 7570