1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_MMZONE_H 3 #define _LINUX_MMZONE_H 4 5 #ifndef __ASSEMBLY__ 6 #ifndef __GENERATING_BOUNDS_H 7 8 #include <linux/spinlock.h> 9 #include <linux/list.h> 10 #include <linux/list_nulls.h> 11 #include <linux/wait.h> 12 #include <linux/bitops.h> 13 #include <linux/cache.h> 14 #include <linux/threads.h> 15 #include <linux/numa.h> 16 #include <linux/init.h> 17 #include <linux/seqlock.h> 18 #include <linux/nodemask.h> 19 #include <linux/pageblock-flags.h> 20 #include <linux/page-flags-layout.h> 21 #include <linux/atomic.h> 22 #include <linux/mm_types.h> 23 #include <linux/page-flags.h> 24 #include <linux/local_lock.h> 25 #include <linux/zswap.h> 26 #include <asm/page.h> 27 28 /* Free memory management - zoned buddy allocator. */ 29 #ifndef CONFIG_ARCH_FORCE_MAX_ORDER 30 #define MAX_PAGE_ORDER 10 31 #else 32 #define MAX_PAGE_ORDER CONFIG_ARCH_FORCE_MAX_ORDER 33 #endif 34 #define MAX_ORDER_NR_PAGES (1 << MAX_PAGE_ORDER) 35 36 #define IS_MAX_ORDER_ALIGNED(pfn) IS_ALIGNED(pfn, MAX_ORDER_NR_PAGES) 37 38 #define NR_PAGE_ORDERS (MAX_PAGE_ORDER + 1) 39 40 /* Defines the order for the number of pages that have a migrate type. */ 41 #ifndef CONFIG_PAGE_BLOCK_MAX_ORDER 42 #define PAGE_BLOCK_MAX_ORDER MAX_PAGE_ORDER 43 #else 44 #define PAGE_BLOCK_MAX_ORDER CONFIG_PAGE_BLOCK_MAX_ORDER 45 #endif /* CONFIG_PAGE_BLOCK_MAX_ORDER */ 46 47 /* 48 * The MAX_PAGE_ORDER, which defines the max order of pages to be allocated 49 * by the buddy allocator, has to be larger or equal to the PAGE_BLOCK_MAX_ORDER, 50 * which defines the order for the number of pages that can have a migrate type 51 */ 52 #if (PAGE_BLOCK_MAX_ORDER > MAX_PAGE_ORDER) 53 #error MAX_PAGE_ORDER must be >= PAGE_BLOCK_MAX_ORDER 54 #endif 55 56 /* 57 * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed 58 * costly to service. That is between allocation orders which should 59 * coalesce naturally under reasonable reclaim pressure and those which 60 * will not. 61 */ 62 #define PAGE_ALLOC_COSTLY_ORDER 3 63 64 enum migratetype { 65 MIGRATE_UNMOVABLE, 66 MIGRATE_MOVABLE, 67 MIGRATE_RECLAIMABLE, 68 MIGRATE_PCPTYPES, /* the number of types on the pcp lists */ 69 MIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES, 70 #ifdef CONFIG_CMA 71 /* 72 * MIGRATE_CMA migration type is designed to mimic the way 73 * ZONE_MOVABLE works. Only movable pages can be allocated 74 * from MIGRATE_CMA pageblocks and page allocator never 75 * implicitly change migration type of MIGRATE_CMA pageblock. 76 * 77 * The way to use it is to change migratetype of a range of 78 * pageblocks to MIGRATE_CMA which can be done by 79 * __free_pageblock_cma() function. 80 */ 81 MIGRATE_CMA, 82 __MIGRATE_TYPE_END = MIGRATE_CMA, 83 #else 84 __MIGRATE_TYPE_END = MIGRATE_HIGHATOMIC, 85 #endif 86 #ifdef CONFIG_MEMORY_ISOLATION 87 MIGRATE_ISOLATE, /* can't allocate from here */ 88 #endif 89 MIGRATE_TYPES 90 }; 91 92 /* In mm/page_alloc.c; keep in sync also with show_migration_types() there */ 93 extern const char * const migratetype_names[MIGRATE_TYPES]; 94 95 #ifdef CONFIG_CMA 96 # define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA) 97 # define is_migrate_cma_page(_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA) 98 /* 99 * __dump_folio() in mm/debug.c passes a folio pointer to on-stack struct folio, 100 * so folio_pfn() cannot be used and pfn is needed. 101 */ 102 # define is_migrate_cma_folio(folio, pfn) \ 103 (get_pfnblock_migratetype(&folio->page, pfn) == MIGRATE_CMA) 104 #else 105 # define is_migrate_cma(migratetype) false 106 # define is_migrate_cma_page(_page) false 107 # define is_migrate_cma_folio(folio, pfn) false 108 #endif 109 110 static inline bool is_migrate_movable(int mt) 111 { 112 return is_migrate_cma(mt) || mt == MIGRATE_MOVABLE; 113 } 114 115 /* 116 * Check whether a migratetype can be merged with another migratetype. 117 * 118 * It is only mergeable when it can fall back to other migratetypes for 119 * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c. 120 */ 121 static inline bool migratetype_is_mergeable(int mt) 122 { 123 return mt < MIGRATE_PCPTYPES; 124 } 125 126 #define for_each_migratetype_order(order, type) \ 127 for (order = 0; order < NR_PAGE_ORDERS; order++) \ 128 for (type = 0; type < MIGRATE_TYPES; type++) 129 130 extern int page_group_by_mobility_disabled; 131 132 #define get_pageblock_migratetype(page) \ 133 get_pfnblock_migratetype(page, page_to_pfn(page)) 134 135 #define folio_migratetype(folio) \ 136 get_pageblock_migratetype(&folio->page) 137 138 struct free_area { 139 struct list_head free_list[MIGRATE_TYPES]; 140 unsigned long nr_free; 141 }; 142 143 struct pglist_data; 144 145 #ifdef CONFIG_NUMA 146 enum numa_stat_item { 147 NUMA_HIT, /* allocated in intended node */ 148 NUMA_MISS, /* allocated in non intended node */ 149 NUMA_FOREIGN, /* was intended here, hit elsewhere */ 150 NUMA_INTERLEAVE_HIT, /* interleaver preferred this zone */ 151 NUMA_LOCAL, /* allocation from local node */ 152 NUMA_OTHER, /* allocation from other node */ 153 NR_VM_NUMA_EVENT_ITEMS 154 }; 155 #else 156 #define NR_VM_NUMA_EVENT_ITEMS 0 157 #endif 158 159 enum zone_stat_item { 160 /* First 128 byte cacheline (assuming 64 bit words) */ 161 NR_FREE_PAGES, 162 NR_FREE_PAGES_BLOCKS, 163 NR_ZONE_LRU_BASE, /* Used only for compaction and reclaim retry */ 164 NR_ZONE_INACTIVE_ANON = NR_ZONE_LRU_BASE, 165 NR_ZONE_ACTIVE_ANON, 166 NR_ZONE_INACTIVE_FILE, 167 NR_ZONE_ACTIVE_FILE, 168 NR_ZONE_UNEVICTABLE, 169 NR_ZONE_WRITE_PENDING, /* Count of dirty, writeback and unstable pages */ 170 NR_MLOCK, /* mlock()ed pages found and moved off LRU */ 171 /* Second 128 byte cacheline */ 172 #if IS_ENABLED(CONFIG_ZSMALLOC) 173 NR_ZSPAGES, /* allocated in zsmalloc */ 174 #endif 175 NR_FREE_CMA_PAGES, 176 #ifdef CONFIG_UNACCEPTED_MEMORY 177 NR_UNACCEPTED, 178 #endif 179 NR_VM_ZONE_STAT_ITEMS }; 180 181 enum node_stat_item { 182 NR_LRU_BASE, 183 NR_INACTIVE_ANON = NR_LRU_BASE, /* must match order of LRU_[IN]ACTIVE */ 184 NR_ACTIVE_ANON, /* " " " " " */ 185 NR_INACTIVE_FILE, /* " " " " " */ 186 NR_ACTIVE_FILE, /* " " " " " */ 187 NR_UNEVICTABLE, /* " " " " " */ 188 NR_SLAB_RECLAIMABLE_B, 189 NR_SLAB_UNRECLAIMABLE_B, 190 NR_ISOLATED_ANON, /* Temporary isolated pages from anon lru */ 191 NR_ISOLATED_FILE, /* Temporary isolated pages from file lru */ 192 WORKINGSET_NODES, 193 WORKINGSET_REFAULT_BASE, 194 WORKINGSET_REFAULT_ANON = WORKINGSET_REFAULT_BASE, 195 WORKINGSET_REFAULT_FILE, 196 WORKINGSET_ACTIVATE_BASE, 197 WORKINGSET_ACTIVATE_ANON = WORKINGSET_ACTIVATE_BASE, 198 WORKINGSET_ACTIVATE_FILE, 199 WORKINGSET_RESTORE_BASE, 200 WORKINGSET_RESTORE_ANON = WORKINGSET_RESTORE_BASE, 201 WORKINGSET_RESTORE_FILE, 202 WORKINGSET_NODERECLAIM, 203 NR_ANON_MAPPED, /* Mapped anonymous pages */ 204 NR_FILE_MAPPED, /* pagecache pages mapped into pagetables. 205 only modified from process context */ 206 NR_FILE_PAGES, 207 NR_FILE_DIRTY, 208 NR_WRITEBACK, 209 NR_SHMEM, /* shmem pages (included tmpfs/GEM pages) */ 210 NR_SHMEM_THPS, 211 NR_SHMEM_PMDMAPPED, 212 NR_FILE_THPS, 213 NR_FILE_PMDMAPPED, 214 NR_ANON_THPS, 215 NR_VMSCAN_WRITE, 216 NR_VMSCAN_IMMEDIATE, /* Prioritise for reclaim when writeback ends */ 217 NR_DIRTIED, /* page dirtyings since bootup */ 218 NR_WRITTEN, /* page writings since bootup */ 219 NR_THROTTLED_WRITTEN, /* NR_WRITTEN while reclaim throttled */ 220 NR_KERNEL_MISC_RECLAIMABLE, /* reclaimable non-slab kernel pages */ 221 NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */ 222 NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */ 223 NR_KERNEL_STACK_KB, /* measured in KiB */ 224 #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK) 225 NR_KERNEL_SCS_KB, /* measured in KiB */ 226 #endif 227 NR_PAGETABLE, /* used for pagetables */ 228 NR_SECONDARY_PAGETABLE, /* secondary pagetables, KVM & IOMMU */ 229 #ifdef CONFIG_IOMMU_SUPPORT 230 NR_IOMMU_PAGES, /* # of pages allocated by IOMMU */ 231 #endif 232 #ifdef CONFIG_SWAP 233 NR_SWAPCACHE, 234 #endif 235 #ifdef CONFIG_NUMA_BALANCING 236 PGPROMOTE_SUCCESS, /* promote successfully */ 237 /** 238 * Candidate pages for promotion based on hint fault latency. This 239 * counter is used to control the promotion rate and adjust the hot 240 * threshold. 241 */ 242 PGPROMOTE_CANDIDATE, 243 /** 244 * Not rate-limited (NRL) candidate pages for those can be promoted 245 * without considering hot threshold because of enough free pages in 246 * fast-tier node. These promotions bypass the regular hotness checks 247 * and do NOT influence the promotion rate-limiter or 248 * threshold-adjustment logic. 249 * This is for statistics/monitoring purposes. 250 */ 251 PGPROMOTE_CANDIDATE_NRL, 252 #endif 253 /* PGDEMOTE_*: pages demoted */ 254 PGDEMOTE_KSWAPD, 255 PGDEMOTE_DIRECT, 256 PGDEMOTE_KHUGEPAGED, 257 PGDEMOTE_PROACTIVE, 258 #ifdef CONFIG_HUGETLB_PAGE 259 NR_HUGETLB, 260 #endif 261 NR_BALLOON_PAGES, 262 NR_KERNEL_FILE_PAGES, 263 NR_GPU_ACTIVE, /* Pages assigned to GPU objects */ 264 NR_GPU_RECLAIM, /* Pages in shrinkable GPU pools */ 265 NR_VM_NODE_STAT_ITEMS 266 }; 267 268 /* 269 * Returns true if the item should be printed in THPs (/proc/vmstat 270 * currently prints number of anon, file and shmem THPs. But the item 271 * is charged in pages). 272 */ 273 static __always_inline bool vmstat_item_print_in_thp(enum node_stat_item item) 274 { 275 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 276 return false; 277 278 return item == NR_ANON_THPS || 279 item == NR_FILE_THPS || 280 item == NR_SHMEM_THPS || 281 item == NR_SHMEM_PMDMAPPED || 282 item == NR_FILE_PMDMAPPED; 283 } 284 285 /* 286 * Returns true if the value is measured in bytes (most vmstat values are 287 * measured in pages). This defines the API part, the internal representation 288 * might be different. 289 */ 290 static __always_inline bool vmstat_item_in_bytes(int idx) 291 { 292 /* 293 * Global and per-node slab counters track slab pages. 294 * It's expected that changes are multiples of PAGE_SIZE. 295 * Internally values are stored in pages. 296 * 297 * Per-memcg and per-lruvec counters track memory, consumed 298 * by individual slab objects. These counters are actually 299 * byte-precise. 300 */ 301 return (idx == NR_SLAB_RECLAIMABLE_B || 302 idx == NR_SLAB_UNRECLAIMABLE_B); 303 } 304 305 /* 306 * We do arithmetic on the LRU lists in various places in the code, 307 * so it is important to keep the active lists LRU_ACTIVE higher in 308 * the array than the corresponding inactive lists, and to keep 309 * the *_FILE lists LRU_FILE higher than the corresponding _ANON lists. 310 * 311 * This has to be kept in sync with the statistics in zone_stat_item 312 * above and the descriptions in vmstat_text in mm/vmstat.c 313 */ 314 #define LRU_BASE 0 315 #define LRU_ACTIVE 1 316 #define LRU_FILE 2 317 318 enum lru_list { 319 LRU_INACTIVE_ANON = LRU_BASE, 320 LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE, 321 LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE, 322 LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE, 323 LRU_UNEVICTABLE, 324 NR_LRU_LISTS 325 }; 326 327 enum vmscan_throttle_state { 328 VMSCAN_THROTTLE_WRITEBACK, 329 VMSCAN_THROTTLE_ISOLATED, 330 VMSCAN_THROTTLE_NOPROGRESS, 331 VMSCAN_THROTTLE_CONGESTED, 332 NR_VMSCAN_THROTTLE, 333 }; 334 335 #define for_each_lru(lru) for (lru = 0; lru < NR_LRU_LISTS; lru++) 336 337 #define for_each_evictable_lru(lru) for (lru = 0; lru <= LRU_ACTIVE_FILE; lru++) 338 339 static inline bool is_file_lru(enum lru_list lru) 340 { 341 return (lru == LRU_INACTIVE_FILE || lru == LRU_ACTIVE_FILE); 342 } 343 344 static inline bool is_active_lru(enum lru_list lru) 345 { 346 return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE); 347 } 348 349 #define WORKINGSET_ANON 0 350 #define WORKINGSET_FILE 1 351 #define ANON_AND_FILE 2 352 353 enum lruvec_flags { 354 /* 355 * An lruvec has many dirty pages backed by a congested BDI: 356 * 1. LRUVEC_CGROUP_CONGESTED is set by cgroup-level reclaim. 357 * It can be cleared by cgroup reclaim or kswapd. 358 * 2. LRUVEC_NODE_CONGESTED is set by kswapd node-level reclaim. 359 * It can only be cleared by kswapd. 360 * 361 * Essentially, kswapd can unthrottle an lruvec throttled by cgroup 362 * reclaim, but not vice versa. This only applies to the root cgroup. 363 * The goal is to prevent cgroup reclaim on the root cgroup (e.g. 364 * memory.reclaim) to unthrottle an unbalanced node (that was throttled 365 * by kswapd). 366 */ 367 LRUVEC_CGROUP_CONGESTED, 368 LRUVEC_NODE_CONGESTED, 369 }; 370 371 #endif /* !__GENERATING_BOUNDS_H */ 372 373 /* 374 * Evictable folios are divided into multiple generations. The youngest and the 375 * oldest generation numbers, max_seq and min_seq, are monotonically increasing. 376 * They form a sliding window of a variable size [MIN_NR_GENS, MAX_NR_GENS]. An 377 * offset within MAX_NR_GENS, i.e., gen, indexes the LRU list of the 378 * corresponding generation. The gen counter in folio->flags stores gen+1 while 379 * a folio is on one of lrugen->folios[]. Otherwise it stores 0. 380 * 381 * After a folio is faulted in, the aging needs to check the accessed bit at 382 * least twice before handing this folio over to the eviction. The first check 383 * clears the accessed bit from the initial fault; the second check makes sure 384 * this folio hasn't been used since then. This process, AKA second chance, 385 * requires a minimum of two generations, hence MIN_NR_GENS. And to maintain ABI 386 * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these two 387 * generations are considered active; the rest of generations, if they exist, 388 * are considered inactive. See lru_gen_is_active(). 389 * 390 * PG_active is always cleared while a folio is on one of lrugen->folios[] so 391 * that the sliding window needs not to worry about it. And it's set again when 392 * a folio considered active is isolated for non-reclaiming purposes, e.g., 393 * migration. See lru_gen_add_folio() and lru_gen_del_folio(). 394 * 395 * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the 396 * number of categories of the active/inactive LRU when keeping track of 397 * accesses through page tables. This requires order_base_2(MAX_NR_GENS+1) bits 398 * in folio->flags, masked by LRU_GEN_MASK. 399 */ 400 #define MIN_NR_GENS 2U 401 #define MAX_NR_GENS 4U 402 403 /* 404 * Each generation is divided into multiple tiers. A folio accessed N times 405 * through file descriptors is in tier order_base_2(N). A folio in the first 406 * tier (N=0,1) is marked by PG_referenced unless it was faulted in through page 407 * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is marked by 408 * PG_workingset. A folio in any other tier (1<N<5) between the first and last 409 * is marked by additional bits of LRU_REFS_WIDTH in folio->flags. 410 * 411 * In contrast to moving across generations which requires the LRU lock, moving 412 * across tiers only involves atomic operations on folio->flags and therefore 413 * has a negligible cost in the buffered access path. In the eviction path, 414 * comparisons of refaulted/(evicted+protected) from the first tier and the rest 415 * infer whether folios accessed multiple times through file descriptors are 416 * statistically hot and thus worth protecting. 417 * 418 * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the 419 * number of categories of the active/inactive LRU when keeping track of 420 * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in 421 * folio->flags, masked by LRU_REFS_MASK. 422 */ 423 #define MAX_NR_TIERS 4U 424 425 #ifndef __GENERATING_BOUNDS_H 426 427 #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF) 428 #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF) 429 430 /* 431 * For folios accessed multiple times through file descriptors, 432 * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->flags 433 * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its 434 * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily 435 * promoted into the second oldest generation in the eviction path. And when 436 * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that 437 * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MASK is 438 * only valid when PG_referenced is set. 439 * 440 * For folios accessed multiple times through page tables, folio_update_gen() 441 * from a page table walk or lru_gen_set_refs() from a rmap walk sets 442 * PG_referenced after the accessed bit is cleared for the first time. 443 * Thereafter, those two paths set PG_workingset and promote folios to the 444 * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears 445 * PG_referenced. Note that for this case, LRU_REFS_MASK is not used. 446 * 447 * For both cases above, after PG_workingset is set on a folio, it remains until 448 * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It 449 * can be set again if lru_gen_test_recent() returns true upon a refault. 450 */ 451 #define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced)) 452 453 struct lruvec; 454 struct page_vma_mapped_walk; 455 456 #ifdef CONFIG_LRU_GEN 457 458 enum { 459 LRU_GEN_ANON, 460 LRU_GEN_FILE, 461 }; 462 463 enum { 464 LRU_GEN_CORE, 465 LRU_GEN_MM_WALK, 466 LRU_GEN_NONLEAF_YOUNG, 467 NR_LRU_GEN_CAPS 468 }; 469 470 #define MIN_LRU_BATCH BITS_PER_LONG 471 #define MAX_LRU_BATCH (MIN_LRU_BATCH * 64) 472 473 /* whether to keep historical stats from evicted generations */ 474 #ifdef CONFIG_LRU_GEN_STATS 475 #define NR_HIST_GENS MAX_NR_GENS 476 #else 477 #define NR_HIST_GENS 1U 478 #endif 479 480 /* 481 * The youngest generation number is stored in max_seq for both anon and file 482 * types as they are aged on an equal footing. The oldest generation numbers are 483 * stored in min_seq[] separately for anon and file types so that they can be 484 * incremented independently. Ideally min_seq[] are kept in sync when both anon 485 * and file types are evictable. However, to adapt to situations like extreme 486 * swappiness, they are allowed to be out of sync by at most 487 * MAX_NR_GENS-MIN_NR_GENS-1. 488 * 489 * The number of pages in each generation is eventually consistent and therefore 490 * can be transiently negative when reset_batch_size() is pending. 491 */ 492 struct lru_gen_folio { 493 /* the aging increments the youngest generation number */ 494 unsigned long max_seq; 495 /* the eviction increments the oldest generation numbers */ 496 unsigned long min_seq[ANON_AND_FILE]; 497 /* the birth time of each generation in jiffies */ 498 unsigned long timestamps[MAX_NR_GENS]; 499 /* the multi-gen LRU lists, lazily sorted on eviction */ 500 struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; 501 /* the multi-gen LRU sizes, eventually consistent */ 502 long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; 503 /* the exponential moving average of refaulted */ 504 unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS]; 505 /* the exponential moving average of evicted+protected */ 506 unsigned long avg_total[ANON_AND_FILE][MAX_NR_TIERS]; 507 /* can only be modified under the LRU lock */ 508 unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; 509 /* can be modified without holding the LRU lock */ 510 atomic_long_t evicted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; 511 atomic_long_t refaulted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; 512 /* whether the multi-gen LRU is enabled */ 513 bool enabled; 514 /* the memcg generation this lru_gen_folio belongs to */ 515 u8 gen; 516 /* the list segment this lru_gen_folio belongs to */ 517 u8 seg; 518 /* per-node lru_gen_folio list for global reclaim */ 519 struct hlist_nulls_node list; 520 }; 521 522 enum { 523 MM_LEAF_TOTAL, /* total leaf entries */ 524 MM_LEAF_YOUNG, /* young leaf entries */ 525 MM_NONLEAF_FOUND, /* non-leaf entries found in Bloom filters */ 526 MM_NONLEAF_ADDED, /* non-leaf entries added to Bloom filters */ 527 NR_MM_STATS 528 }; 529 530 /* double-buffering Bloom filters */ 531 #define NR_BLOOM_FILTERS 2 532 533 struct lru_gen_mm_state { 534 /* synced with max_seq after each iteration */ 535 unsigned long seq; 536 /* where the current iteration continues after */ 537 struct list_head *head; 538 /* where the last iteration ended before */ 539 struct list_head *tail; 540 /* Bloom filters flip after each iteration */ 541 unsigned long *filters[NR_BLOOM_FILTERS]; 542 /* the mm stats for debugging */ 543 unsigned long stats[NR_HIST_GENS][NR_MM_STATS]; 544 }; 545 546 struct lru_gen_mm_walk { 547 /* the lruvec under reclaim */ 548 struct lruvec *lruvec; 549 /* max_seq from lru_gen_folio: can be out of date */ 550 unsigned long seq; 551 /* the next address within an mm to scan */ 552 unsigned long next_addr; 553 /* to batch promoted pages */ 554 int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; 555 /* to batch the mm stats */ 556 int mm_stats[NR_MM_STATS]; 557 /* total batched items */ 558 int batched; 559 int swappiness; 560 bool force_scan; 561 }; 562 563 /* 564 * For each node, memcgs are divided into two generations: the old and the 565 * young. For each generation, memcgs are randomly sharded into multiple bins 566 * to improve scalability. For each bin, the hlist_nulls is virtually divided 567 * into three segments: the head, the tail and the default. 568 * 569 * An onlining memcg is added to the tail of a random bin in the old generation. 570 * The eviction starts at the head of a random bin in the old generation. The 571 * per-node memcg generation counter, whose reminder (mod MEMCG_NR_GENS) indexes 572 * the old generation, is incremented when all its bins become empty. 573 * 574 * There are four operations: 575 * 1. MEMCG_LRU_HEAD, which moves a memcg to the head of a random bin in its 576 * current generation (old or young) and updates its "seg" to "head"; 577 * 2. MEMCG_LRU_TAIL, which moves a memcg to the tail of a random bin in its 578 * current generation (old or young) and updates its "seg" to "tail"; 579 * 3. MEMCG_LRU_OLD, which moves a memcg to the head of a random bin in the old 580 * generation, updates its "gen" to "old" and resets its "seg" to "default"; 581 * 4. MEMCG_LRU_YOUNG, which moves a memcg to the tail of a random bin in the 582 * young generation, updates its "gen" to "young" and resets its "seg" to 583 * "default". 584 * 585 * The events that trigger the above operations are: 586 * 1. Exceeding the soft limit, which triggers MEMCG_LRU_HEAD; 587 * 2. The first attempt to reclaim a memcg below low, which triggers 588 * MEMCG_LRU_TAIL; 589 * 3. The first attempt to reclaim a memcg offlined or below reclaimable size 590 * threshold, which triggers MEMCG_LRU_TAIL; 591 * 4. The second attempt to reclaim a memcg offlined or below reclaimable size 592 * threshold, which triggers MEMCG_LRU_YOUNG; 593 * 5. Attempting to reclaim a memcg below min, which triggers MEMCG_LRU_YOUNG; 594 * 6. Finishing the aging on the eviction path, which triggers MEMCG_LRU_YOUNG; 595 * 7. Offlining a memcg, which triggers MEMCG_LRU_OLD. 596 * 597 * Notes: 598 * 1. Memcg LRU only applies to global reclaim, and the round-robin incrementing 599 * of their max_seq counters ensures the eventual fairness to all eligible 600 * memcgs. For memcg reclaim, it still relies on mem_cgroup_iter(). 601 * 2. There are only two valid generations: old (seq) and young (seq+1). 602 * MEMCG_NR_GENS is set to three so that when reading the generation counter 603 * locklessly, a stale value (seq-1) does not wraparound to young. 604 */ 605 #define MEMCG_NR_GENS 3 606 #define MEMCG_NR_BINS 8 607 608 struct lru_gen_memcg { 609 /* the per-node memcg generation counter */ 610 unsigned long seq; 611 /* each memcg has one lru_gen_folio per node */ 612 unsigned long nr_memcgs[MEMCG_NR_GENS]; 613 /* per-node lru_gen_folio list for global reclaim */ 614 struct hlist_nulls_head fifo[MEMCG_NR_GENS][MEMCG_NR_BINS]; 615 /* protects the above */ 616 spinlock_t lock; 617 }; 618 619 void lru_gen_init_pgdat(struct pglist_data *pgdat); 620 void lru_gen_init_lruvec(struct lruvec *lruvec); 621 bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw); 622 623 void lru_gen_init_memcg(struct mem_cgroup *memcg); 624 void lru_gen_exit_memcg(struct mem_cgroup *memcg); 625 void lru_gen_online_memcg(struct mem_cgroup *memcg); 626 void lru_gen_offline_memcg(struct mem_cgroup *memcg); 627 void lru_gen_release_memcg(struct mem_cgroup *memcg); 628 void lru_gen_soft_reclaim(struct mem_cgroup *memcg, int nid); 629 630 #else /* !CONFIG_LRU_GEN */ 631 632 static inline void lru_gen_init_pgdat(struct pglist_data *pgdat) 633 { 634 } 635 636 static inline void lru_gen_init_lruvec(struct lruvec *lruvec) 637 { 638 } 639 640 static inline bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) 641 { 642 return false; 643 } 644 645 static inline void lru_gen_init_memcg(struct mem_cgroup *memcg) 646 { 647 } 648 649 static inline void lru_gen_exit_memcg(struct mem_cgroup *memcg) 650 { 651 } 652 653 static inline void lru_gen_online_memcg(struct mem_cgroup *memcg) 654 { 655 } 656 657 static inline void lru_gen_offline_memcg(struct mem_cgroup *memcg) 658 { 659 } 660 661 static inline void lru_gen_release_memcg(struct mem_cgroup *memcg) 662 { 663 } 664 665 static inline void lru_gen_soft_reclaim(struct mem_cgroup *memcg, int nid) 666 { 667 } 668 669 #endif /* CONFIG_LRU_GEN */ 670 671 struct lruvec { 672 struct list_head lists[NR_LRU_LISTS]; 673 /* per lruvec lru_lock for memcg */ 674 spinlock_t lru_lock; 675 /* 676 * These track the cost of reclaiming one LRU - file or anon - 677 * over the other. As the observed cost of reclaiming one LRU 678 * increases, the reclaim scan balance tips toward the other. 679 */ 680 unsigned long anon_cost; 681 unsigned long file_cost; 682 /* Non-resident age, driven by LRU movement */ 683 atomic_long_t nonresident_age; 684 /* Refaults at the time of last reclaim cycle */ 685 unsigned long refaults[ANON_AND_FILE]; 686 /* Various lruvec state flags (enum lruvec_flags) */ 687 unsigned long flags; 688 #ifdef CONFIG_LRU_GEN 689 /* evictable pages divided into generations */ 690 struct lru_gen_folio lrugen; 691 #ifdef CONFIG_LRU_GEN_WALKS_MMU 692 /* to concurrently iterate lru_gen_mm_list */ 693 struct lru_gen_mm_state mm_state; 694 #endif 695 #endif /* CONFIG_LRU_GEN */ 696 #ifdef CONFIG_MEMCG 697 struct pglist_data *pgdat; 698 #endif 699 struct zswap_lruvec_state zswap_lruvec_state; 700 }; 701 702 /* Isolate for asynchronous migration */ 703 #define ISOLATE_ASYNC_MIGRATE ((__force isolate_mode_t)0x4) 704 /* Isolate unevictable pages */ 705 #define ISOLATE_UNEVICTABLE ((__force isolate_mode_t)0x8) 706 707 /* LRU Isolation modes. */ 708 typedef unsigned __bitwise isolate_mode_t; 709 710 enum zone_watermarks { 711 WMARK_MIN, 712 WMARK_LOW, 713 WMARK_HIGH, 714 WMARK_PROMO, 715 NR_WMARK 716 }; 717 718 /* 719 * One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. Two additional lists 720 * are added for THP. One PCP list is used by GPF_MOVABLE, and the other PCP list 721 * is used by GFP_UNMOVABLE and GFP_RECLAIMABLE. 722 */ 723 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 724 #define NR_PCP_THP 2 725 #else 726 #define NR_PCP_THP 0 727 #endif 728 #define NR_LOWORDER_PCP_LISTS (MIGRATE_PCPTYPES * (PAGE_ALLOC_COSTLY_ORDER + 1)) 729 #define NR_PCP_LISTS (NR_LOWORDER_PCP_LISTS + NR_PCP_THP) 730 731 /* 732 * Flags used in pcp->flags field. 733 * 734 * PCPF_PREV_FREE_HIGH_ORDER: a high-order page is freed in the 735 * previous page freeing. To avoid to drain PCP for an accident 736 * high-order page freeing. 737 * 738 * PCPF_FREE_HIGH_BATCH: preserve "pcp->batch" pages in PCP before 739 * draining PCP for consecutive high-order pages freeing without 740 * allocation if data cache slice of CPU is large enough. To reduce 741 * zone lock contention and keep cache-hot pages reusing. 742 */ 743 #define PCPF_PREV_FREE_HIGH_ORDER BIT(0) 744 #define PCPF_FREE_HIGH_BATCH BIT(1) 745 746 struct per_cpu_pages { 747 spinlock_t lock; /* Protects lists field */ 748 int count; /* number of pages in the list */ 749 int high; /* high watermark, emptying needed */ 750 int high_min; /* min high watermark */ 751 int high_max; /* max high watermark */ 752 int batch; /* chunk size for buddy add/remove */ 753 u8 flags; /* protected by pcp->lock */ 754 u8 alloc_factor; /* batch scaling factor during allocate */ 755 #ifdef CONFIG_NUMA 756 u8 expire; /* When 0, remote pagesets are drained */ 757 #endif 758 short free_count; /* consecutive free count */ 759 760 /* Lists of pages, one per migrate type stored on the pcp-lists */ 761 struct list_head lists[NR_PCP_LISTS]; 762 } ____cacheline_aligned_in_smp; 763 764 struct per_cpu_zonestat { 765 #ifdef CONFIG_SMP 766 s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS]; 767 s8 stat_threshold; 768 #endif 769 #ifdef CONFIG_NUMA 770 /* 771 * Low priority inaccurate counters that are only folded 772 * on demand. Use a large type to avoid the overhead of 773 * folding during refresh_cpu_vm_stats. 774 */ 775 unsigned long vm_numa_event[NR_VM_NUMA_EVENT_ITEMS]; 776 #endif 777 }; 778 779 struct per_cpu_nodestat { 780 s8 stat_threshold; 781 s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS]; 782 }; 783 784 #endif /* !__GENERATING_BOUNDS.H */ 785 786 enum zone_type { 787 /* 788 * ZONE_DMA and ZONE_DMA32 are used when there are peripherals not able 789 * to DMA to all of the addressable memory (ZONE_NORMAL). 790 * On architectures where this area covers the whole 32 bit address 791 * space ZONE_DMA32 is used. ZONE_DMA is left for the ones with smaller 792 * DMA addressing constraints. This distinction is important as a 32bit 793 * DMA mask is assumed when ZONE_DMA32 is defined. Some 64-bit 794 * platforms may need both zones as they support peripherals with 795 * different DMA addressing limitations. 796 */ 797 #ifdef CONFIG_ZONE_DMA 798 ZONE_DMA, 799 #endif 800 #ifdef CONFIG_ZONE_DMA32 801 ZONE_DMA32, 802 #endif 803 /* 804 * Normal addressable memory is in ZONE_NORMAL. DMA operations can be 805 * performed on pages in ZONE_NORMAL if the DMA devices support 806 * transfers to all addressable memory. 807 */ 808 ZONE_NORMAL, 809 #ifdef CONFIG_HIGHMEM 810 /* 811 * A memory area that is only addressable by the kernel through 812 * mapping portions into its own address space. This is for example 813 * used by i386 to allow the kernel to address the memory beyond 814 * 900MB. The kernel will set up special mappings (page 815 * table entries on i386) for each page that the kernel needs to 816 * access. 817 */ 818 ZONE_HIGHMEM, 819 #endif 820 /* 821 * ZONE_MOVABLE is similar to ZONE_NORMAL, except that it contains 822 * movable pages with few exceptional cases described below. Main use 823 * cases for ZONE_MOVABLE are to make memory offlining/unplug more 824 * likely to succeed, and to locally limit unmovable allocations - e.g., 825 * to increase the number of THP/huge pages. Notable special cases are: 826 * 827 * 1. Pinned pages: (long-term) pinning of movable pages might 828 * essentially turn such pages unmovable. Therefore, we do not allow 829 * pinning long-term pages in ZONE_MOVABLE. When pages are pinned and 830 * faulted, they come from the right zone right away. However, it is 831 * still possible that address space already has pages in 832 * ZONE_MOVABLE at the time when pages are pinned (i.e. user has 833 * touches that memory before pinning). In such case we migrate them 834 * to a different zone. When migration fails - pinning fails. 835 * 2. memblock allocations: kernelcore/movablecore setups might create 836 * situations where ZONE_MOVABLE contains unmovable allocations 837 * after boot. Memory offlining and allocations fail early. 838 * 3. Memory holes: kernelcore/movablecore setups might create very rare 839 * situations where ZONE_MOVABLE contains memory holes after boot, 840 * for example, if we have sections that are only partially 841 * populated. Memory offlining and allocations fail early. 842 * 4. PG_hwpoison pages: while poisoned pages can be skipped during 843 * memory offlining, such pages cannot be allocated. 844 * 5. Unmovable PG_offline pages: in paravirtualized environments, 845 * hotplugged memory blocks might only partially be managed by the 846 * buddy (e.g., via XEN-balloon, Hyper-V balloon, virtio-mem). The 847 * parts not manged by the buddy are unmovable PG_offline pages. In 848 * some cases (virtio-mem), such pages can be skipped during 849 * memory offlining, however, cannot be moved/allocated. These 850 * techniques might use alloc_contig_range() to hide previously 851 * exposed pages from the buddy again (e.g., to implement some sort 852 * of memory unplug in virtio-mem). 853 * 6. ZERO_PAGE(0), kernelcore/movablecore setups might create 854 * situations where ZERO_PAGE(0) which is allocated differently 855 * on different platforms may end up in a movable zone. ZERO_PAGE(0) 856 * cannot be migrated. 857 * 7. Memory-hotplug: when using memmap_on_memory and onlining the 858 * memory to the MOVABLE zone, the vmemmap pages are also placed in 859 * such zone. Such pages cannot be really moved around as they are 860 * self-stored in the range, but they are treated as movable when 861 * the range they describe is about to be offlined. 862 * 863 * In general, no unmovable allocations that degrade memory offlining 864 * should end up in ZONE_MOVABLE. Allocators (like alloc_contig_range()) 865 * have to expect that migrating pages in ZONE_MOVABLE can fail (even 866 * if has_unmovable_pages() states that there are no unmovable pages, 867 * there can be false negatives). 868 */ 869 ZONE_MOVABLE, 870 #ifdef CONFIG_ZONE_DEVICE 871 ZONE_DEVICE, 872 #endif 873 __MAX_NR_ZONES 874 875 }; 876 877 #ifndef __GENERATING_BOUNDS_H 878 879 #define ASYNC_AND_SYNC 2 880 881 struct zone { 882 /* Read-mostly fields */ 883 884 /* zone watermarks, access with *_wmark_pages(zone) macros */ 885 unsigned long _watermark[NR_WMARK]; 886 unsigned long watermark_boost; 887 888 unsigned long nr_reserved_highatomic; 889 unsigned long nr_free_highatomic; 890 891 /* 892 * We don't know if the memory that we're going to allocate will be 893 * freeable or/and it will be released eventually, so to avoid totally 894 * wasting several GB of ram we must reserve some of the lower zone 895 * memory (otherwise we risk to run OOM on the lower zones despite 896 * there being tons of freeable ram on the higher zones). This array is 897 * recalculated at runtime if the sysctl_lowmem_reserve_ratio sysctl 898 * changes. 899 */ 900 long lowmem_reserve[MAX_NR_ZONES]; 901 902 #ifdef CONFIG_NUMA 903 int node; 904 #endif 905 struct pglist_data *zone_pgdat; 906 struct per_cpu_pages __percpu *per_cpu_pageset; 907 struct per_cpu_zonestat __percpu *per_cpu_zonestats; 908 /* 909 * the high and batch values are copied to individual pagesets for 910 * faster access 911 */ 912 int pageset_high_min; 913 int pageset_high_max; 914 int pageset_batch; 915 916 #ifndef CONFIG_SPARSEMEM 917 /* 918 * Flags for a pageblock_nr_pages block. See pageblock-flags.h. 919 * In SPARSEMEM, this map is stored in struct mem_section 920 */ 921 unsigned long *pageblock_flags; 922 #endif /* CONFIG_SPARSEMEM */ 923 924 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */ 925 unsigned long zone_start_pfn; 926 927 /* 928 * spanned_pages is the total pages spanned by the zone, including 929 * holes, which is calculated as: 930 * spanned_pages = zone_end_pfn - zone_start_pfn; 931 * 932 * present_pages is physical pages existing within the zone, which 933 * is calculated as: 934 * present_pages = spanned_pages - absent_pages(pages in holes); 935 * 936 * present_early_pages is present pages existing within the zone 937 * located on memory available since early boot, excluding hotplugged 938 * memory. 939 * 940 * managed_pages is present pages managed by the buddy system, which 941 * is calculated as (reserved_pages includes pages allocated by the 942 * bootmem allocator): 943 * managed_pages = present_pages - reserved_pages; 944 * 945 * cma pages is present pages that are assigned for CMA use 946 * (MIGRATE_CMA). 947 * 948 * So present_pages may be used by memory hotplug or memory power 949 * management logic to figure out unmanaged pages by checking 950 * (present_pages - managed_pages). And managed_pages should be used 951 * by page allocator and vm scanner to calculate all kinds of watermarks 952 * and thresholds. 953 * 954 * Locking rules: 955 * 956 * zone_start_pfn and spanned_pages are protected by span_seqlock. 957 * It is a seqlock because it has to be read outside of zone->lock, 958 * and it is done in the main allocator path. But, it is written 959 * quite infrequently. 960 * 961 * The span_seq lock is declared along with zone->lock because it is 962 * frequently read in proximity to zone->lock. It's good to 963 * give them a chance of being in the same cacheline. 964 * 965 * Write access to present_pages at runtime should be protected by 966 * mem_hotplug_begin/done(). Any reader who can't tolerant drift of 967 * present_pages should use get_online_mems() to get a stable value. 968 */ 969 atomic_long_t managed_pages; 970 unsigned long spanned_pages; 971 unsigned long present_pages; 972 #if defined(CONFIG_MEMORY_HOTPLUG) 973 unsigned long present_early_pages; 974 #endif 975 #ifdef CONFIG_CMA 976 unsigned long cma_pages; 977 #endif 978 979 const char *name; 980 981 #ifdef CONFIG_MEMORY_ISOLATION 982 /* 983 * Number of isolated pageblock. It is used to solve incorrect 984 * freepage counting problem due to racy retrieving migratetype 985 * of pageblock. Protected by zone->lock. 986 */ 987 unsigned long nr_isolate_pageblock; 988 #endif 989 990 #ifdef CONFIG_MEMORY_HOTPLUG 991 /* see spanned/present_pages for more description */ 992 seqlock_t span_seqlock; 993 #endif 994 995 int initialized; 996 997 /* Write-intensive fields used from the page allocator */ 998 CACHELINE_PADDING(_pad1_); 999 1000 /* free areas of different sizes */ 1001 struct free_area free_area[NR_PAGE_ORDERS]; 1002 1003 #ifdef CONFIG_UNACCEPTED_MEMORY 1004 /* Pages to be accepted. All pages on the list are MAX_PAGE_ORDER */ 1005 struct list_head unaccepted_pages; 1006 1007 /* To be called once the last page in the zone is accepted */ 1008 struct work_struct unaccepted_cleanup; 1009 #endif 1010 1011 /* zone flags, see below */ 1012 unsigned long flags; 1013 1014 /* Primarily protects free_area */ 1015 spinlock_t lock; 1016 1017 /* Pages to be freed when next trylock succeeds */ 1018 struct llist_head trylock_free_pages; 1019 1020 /* Write-intensive fields used by compaction and vmstats. */ 1021 CACHELINE_PADDING(_pad2_); 1022 1023 /* 1024 * When free pages are below this point, additional steps are taken 1025 * when reading the number of free pages to avoid per-cpu counter 1026 * drift allowing watermarks to be breached 1027 */ 1028 unsigned long percpu_drift_mark; 1029 1030 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 1031 /* pfn where compaction free scanner should start */ 1032 unsigned long compact_cached_free_pfn; 1033 /* pfn where compaction migration scanner should start */ 1034 unsigned long compact_cached_migrate_pfn[ASYNC_AND_SYNC]; 1035 unsigned long compact_init_migrate_pfn; 1036 unsigned long compact_init_free_pfn; 1037 #endif 1038 1039 #ifdef CONFIG_COMPACTION 1040 /* 1041 * On compaction failure, 1<<compact_defer_shift compactions 1042 * are skipped before trying again. The number attempted since 1043 * last failure is tracked with compact_considered. 1044 * compact_order_failed is the minimum compaction failed order. 1045 */ 1046 unsigned int compact_considered; 1047 unsigned int compact_defer_shift; 1048 int compact_order_failed; 1049 #endif 1050 1051 #if defined CONFIG_COMPACTION || defined CONFIG_CMA 1052 /* Set to true when the PG_migrate_skip bits should be cleared */ 1053 bool compact_blockskip_flush; 1054 #endif 1055 1056 bool contiguous; 1057 1058 CACHELINE_PADDING(_pad3_); 1059 /* Zone statistics */ 1060 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS]; 1061 atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS]; 1062 } ____cacheline_internodealigned_in_smp; 1063 1064 enum pgdat_flags { 1065 PGDAT_WRITEBACK, /* reclaim scanning has recently found 1066 * many pages under writeback 1067 */ 1068 PGDAT_RECLAIM_LOCKED, /* prevents concurrent reclaim */ 1069 }; 1070 1071 enum zone_flags { 1072 ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks. 1073 * Cleared when kswapd is woken. 1074 */ 1075 ZONE_RECLAIM_ACTIVE, /* kswapd may be scanning the zone. */ 1076 ZONE_BELOW_HIGH, /* zone is below high watermark. */ 1077 }; 1078 1079 static inline unsigned long wmark_pages(const struct zone *z, 1080 enum zone_watermarks w) 1081 { 1082 return z->_watermark[w] + z->watermark_boost; 1083 } 1084 1085 static inline unsigned long min_wmark_pages(const struct zone *z) 1086 { 1087 return wmark_pages(z, WMARK_MIN); 1088 } 1089 1090 static inline unsigned long low_wmark_pages(const struct zone *z) 1091 { 1092 return wmark_pages(z, WMARK_LOW); 1093 } 1094 1095 static inline unsigned long high_wmark_pages(const struct zone *z) 1096 { 1097 return wmark_pages(z, WMARK_HIGH); 1098 } 1099 1100 static inline unsigned long promo_wmark_pages(const struct zone *z) 1101 { 1102 return wmark_pages(z, WMARK_PROMO); 1103 } 1104 1105 static inline unsigned long zone_managed_pages(const struct zone *zone) 1106 { 1107 return (unsigned long)atomic_long_read(&zone->managed_pages); 1108 } 1109 1110 static inline unsigned long zone_cma_pages(struct zone *zone) 1111 { 1112 #ifdef CONFIG_CMA 1113 return zone->cma_pages; 1114 #else 1115 return 0; 1116 #endif 1117 } 1118 1119 static inline unsigned long zone_end_pfn(const struct zone *zone) 1120 { 1121 return zone->zone_start_pfn + zone->spanned_pages; 1122 } 1123 1124 static inline bool zone_spans_pfn(const struct zone *zone, unsigned long pfn) 1125 { 1126 return zone->zone_start_pfn <= pfn && pfn < zone_end_pfn(zone); 1127 } 1128 1129 static inline bool zone_is_initialized(const struct zone *zone) 1130 { 1131 return zone->initialized; 1132 } 1133 1134 static inline bool zone_is_empty(const struct zone *zone) 1135 { 1136 return zone->spanned_pages == 0; 1137 } 1138 1139 #ifndef BUILD_VDSO32_64 1140 /* 1141 * The zone field is never updated after free_area_init_core() 1142 * sets it, so none of the operations on it need to be atomic. 1143 */ 1144 1145 /* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_CPUPID] | ... | FLAGS | */ 1146 #define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH) 1147 #define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH) 1148 #define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH) 1149 #define LAST_CPUPID_PGOFF (ZONES_PGOFF - LAST_CPUPID_WIDTH) 1150 #define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH) 1151 #define LRU_GEN_PGOFF (KASAN_TAG_PGOFF - LRU_GEN_WIDTH) 1152 #define LRU_REFS_PGOFF (LRU_GEN_PGOFF - LRU_REFS_WIDTH) 1153 1154 /* 1155 * Define the bit shifts to access each section. For non-existent 1156 * sections we define the shift as 0; that plus a 0 mask ensures 1157 * the compiler will optimise away reference to them. 1158 */ 1159 #define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0)) 1160 #define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0)) 1161 #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0)) 1162 #define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0)) 1163 #define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH != 0)) 1164 1165 /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */ 1166 #ifdef NODE_NOT_IN_PAGE_FLAGS 1167 #define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT) 1168 #define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF) ? \ 1169 SECTIONS_PGOFF : ZONES_PGOFF) 1170 #else 1171 #define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT) 1172 #define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF) ? \ 1173 NODES_PGOFF : ZONES_PGOFF) 1174 #endif 1175 1176 #define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0)) 1177 1178 #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1) 1179 #define NODES_MASK ((1UL << NODES_WIDTH) - 1) 1180 #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1) 1181 #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1) 1182 #define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1) 1183 #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1) 1184 1185 static inline enum zone_type memdesc_zonenum(memdesc_flags_t flags) 1186 { 1187 ASSERT_EXCLUSIVE_BITS(flags.f, ZONES_MASK << ZONES_PGSHIFT); 1188 return (flags.f >> ZONES_PGSHIFT) & ZONES_MASK; 1189 } 1190 1191 static inline enum zone_type page_zonenum(const struct page *page) 1192 { 1193 return memdesc_zonenum(page->flags); 1194 } 1195 1196 static inline enum zone_type folio_zonenum(const struct folio *folio) 1197 { 1198 return memdesc_zonenum(folio->flags); 1199 } 1200 1201 #ifdef CONFIG_ZONE_DEVICE 1202 static inline bool memdesc_is_zone_device(memdesc_flags_t mdf) 1203 { 1204 return memdesc_zonenum(mdf) == ZONE_DEVICE; 1205 } 1206 1207 static inline struct dev_pagemap *page_pgmap(const struct page *page) 1208 { 1209 VM_WARN_ON_ONCE_PAGE(!memdesc_is_zone_device(page->flags), page); 1210 return page_folio(page)->pgmap; 1211 } 1212 1213 /* 1214 * Consecutive zone device pages should not be merged into the same sgl 1215 * or bvec segment with other types of pages or if they belong to different 1216 * pgmaps. Otherwise getting the pgmap of a given segment is not possible 1217 * without scanning the entire segment. This helper returns true either if 1218 * both pages are not zone device pages or both pages are zone device pages 1219 * with the same pgmap. 1220 */ 1221 static inline bool zone_device_pages_have_same_pgmap(const struct page *a, 1222 const struct page *b) 1223 { 1224 if (memdesc_is_zone_device(a->flags) != memdesc_is_zone_device(b->flags)) 1225 return false; 1226 if (!memdesc_is_zone_device(a->flags)) 1227 return true; 1228 return page_pgmap(a) == page_pgmap(b); 1229 } 1230 1231 extern void memmap_init_zone_device(struct zone *, unsigned long, 1232 unsigned long, struct dev_pagemap *); 1233 #else 1234 static inline bool memdesc_is_zone_device(memdesc_flags_t mdf) 1235 { 1236 return false; 1237 } 1238 static inline bool zone_device_pages_have_same_pgmap(const struct page *a, 1239 const struct page *b) 1240 { 1241 return true; 1242 } 1243 static inline struct dev_pagemap *page_pgmap(const struct page *page) 1244 { 1245 return NULL; 1246 } 1247 #endif 1248 1249 static inline bool is_zone_device_page(const struct page *page) 1250 { 1251 return memdesc_is_zone_device(page->flags); 1252 } 1253 1254 static inline bool folio_is_zone_device(const struct folio *folio) 1255 { 1256 return memdesc_is_zone_device(folio->flags); 1257 } 1258 1259 static inline bool is_zone_movable_page(const struct page *page) 1260 { 1261 return page_zonenum(page) == ZONE_MOVABLE; 1262 } 1263 1264 static inline bool folio_is_zone_movable(const struct folio *folio) 1265 { 1266 return folio_zonenum(folio) == ZONE_MOVABLE; 1267 } 1268 #endif 1269 1270 /* 1271 * Return true if [start_pfn, start_pfn + nr_pages) range has a non-empty 1272 * intersection with the given zone 1273 */ 1274 static inline bool zone_intersects(const struct zone *zone, 1275 unsigned long start_pfn, unsigned long nr_pages) 1276 { 1277 if (zone_is_empty(zone)) 1278 return false; 1279 if (start_pfn >= zone_end_pfn(zone) || 1280 start_pfn + nr_pages <= zone->zone_start_pfn) 1281 return false; 1282 1283 return true; 1284 } 1285 1286 /* 1287 * The "priority" of VM scanning is how much of the queues we will scan in one 1288 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the 1289 * queues ("queue_length >> 12") during an aging round. 1290 */ 1291 #define DEF_PRIORITY 12 1292 1293 /* Maximum number of zones on a zonelist */ 1294 #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES) 1295 1296 enum { 1297 ZONELIST_FALLBACK, /* zonelist with fallback */ 1298 #ifdef CONFIG_NUMA 1299 /* 1300 * The NUMA zonelists are doubled because we need zonelists that 1301 * restrict the allocations to a single node for __GFP_THISNODE. 1302 */ 1303 ZONELIST_NOFALLBACK, /* zonelist without fallback (__GFP_THISNODE) */ 1304 #endif 1305 MAX_ZONELISTS 1306 }; 1307 1308 /* 1309 * This struct contains information about a zone in a zonelist. It is stored 1310 * here to avoid dereferences into large structures and lookups of tables 1311 */ 1312 struct zoneref { 1313 struct zone *zone; /* Pointer to actual zone */ 1314 int zone_idx; /* zone_idx(zoneref->zone) */ 1315 }; 1316 1317 /* 1318 * One allocation request operates on a zonelist. A zonelist 1319 * is a list of zones, the first one is the 'goal' of the 1320 * allocation, the other zones are fallback zones, in decreasing 1321 * priority. 1322 * 1323 * To speed the reading of the zonelist, the zonerefs contain the zone index 1324 * of the entry being read. Helper functions to access information given 1325 * a struct zoneref are 1326 * 1327 * zonelist_zone() - Return the struct zone * for an entry in _zonerefs 1328 * zonelist_zone_idx() - Return the index of the zone for an entry 1329 * zonelist_node_idx() - Return the index of the node for an entry 1330 */ 1331 struct zonelist { 1332 struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1]; 1333 }; 1334 1335 /* 1336 * The array of struct pages for flatmem. 1337 * It must be declared for SPARSEMEM as well because there are configurations 1338 * that rely on that. 1339 */ 1340 extern struct page *mem_map; 1341 1342 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1343 struct deferred_split { 1344 spinlock_t split_queue_lock; 1345 struct list_head split_queue; 1346 unsigned long split_queue_len; 1347 }; 1348 #endif 1349 1350 #ifdef CONFIG_MEMORY_FAILURE 1351 /* 1352 * Per NUMA node memory failure handling statistics. 1353 */ 1354 struct memory_failure_stats { 1355 /* 1356 * Number of raw pages poisoned. 1357 * Cases not accounted: memory outside kernel control, offline page, 1358 * arch-specific memory_failure (SGX), hwpoison_filter() filtered 1359 * error events, and unpoison actions from hwpoison_unpoison. 1360 */ 1361 unsigned long total; 1362 /* 1363 * Recovery results of poisoned raw pages handled by memory_failure, 1364 * in sync with mf_result. 1365 * total = ignored + failed + delayed + recovered. 1366 * total * PAGE_SIZE * #nodes = /proc/meminfo/HardwareCorrupted. 1367 */ 1368 unsigned long ignored; 1369 unsigned long failed; 1370 unsigned long delayed; 1371 unsigned long recovered; 1372 }; 1373 #endif 1374 1375 /* 1376 * On NUMA machines, each NUMA node would have a pg_data_t to describe 1377 * it's memory layout. On UMA machines there is a single pglist_data which 1378 * describes the whole memory. 1379 * 1380 * Memory statistics and page replacement data structures are maintained on a 1381 * per-zone basis. 1382 */ 1383 typedef struct pglist_data { 1384 /* 1385 * node_zones contains just the zones for THIS node. Not all of the 1386 * zones may be populated, but it is the full list. It is referenced by 1387 * this node's node_zonelists as well as other node's node_zonelists. 1388 */ 1389 struct zone node_zones[MAX_NR_ZONES]; 1390 1391 /* 1392 * node_zonelists contains references to all zones in all nodes. 1393 * Generally the first zones will be references to this node's 1394 * node_zones. 1395 */ 1396 struct zonelist node_zonelists[MAX_ZONELISTS]; 1397 1398 int nr_zones; /* number of populated zones in this node */ 1399 #ifdef CONFIG_FLATMEM /* means !SPARSEMEM */ 1400 struct page *node_mem_map; 1401 #ifdef CONFIG_PAGE_EXTENSION 1402 struct page_ext *node_page_ext; 1403 #endif 1404 #endif 1405 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT) 1406 /* 1407 * Must be held any time you expect node_start_pfn, 1408 * node_present_pages, node_spanned_pages or nr_zones to stay constant. 1409 * Also synchronizes pgdat->first_deferred_pfn during deferred page 1410 * init. 1411 * 1412 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to 1413 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG 1414 * or CONFIG_DEFERRED_STRUCT_PAGE_INIT. 1415 * 1416 * Nests above zone->lock and zone->span_seqlock 1417 */ 1418 spinlock_t node_size_lock; 1419 #endif 1420 unsigned long node_start_pfn; 1421 unsigned long node_present_pages; /* total number of physical pages */ 1422 unsigned long node_spanned_pages; /* total size of physical page 1423 range, including holes */ 1424 int node_id; 1425 wait_queue_head_t kswapd_wait; 1426 wait_queue_head_t pfmemalloc_wait; 1427 1428 /* workqueues for throttling reclaim for different reasons. */ 1429 wait_queue_head_t reclaim_wait[NR_VMSCAN_THROTTLE]; 1430 1431 atomic_t nr_writeback_throttled;/* nr of writeback-throttled tasks */ 1432 unsigned long nr_reclaim_start; /* nr pages written while throttled 1433 * when throttling started. */ 1434 #ifdef CONFIG_MEMORY_HOTPLUG 1435 struct mutex kswapd_lock; 1436 #endif 1437 struct task_struct *kswapd; /* Protected by kswapd_lock */ 1438 int kswapd_order; 1439 enum zone_type kswapd_highest_zoneidx; 1440 1441 atomic_t kswapd_failures; /* Number of 'reclaimed == 0' runs */ 1442 1443 #ifdef CONFIG_COMPACTION 1444 int kcompactd_max_order; 1445 enum zone_type kcompactd_highest_zoneidx; 1446 wait_queue_head_t kcompactd_wait; 1447 struct task_struct *kcompactd; 1448 bool proactive_compact_trigger; 1449 #endif 1450 /* 1451 * This is a per-node reserve of pages that are not available 1452 * to userspace allocations. 1453 */ 1454 unsigned long totalreserve_pages; 1455 1456 #ifdef CONFIG_NUMA 1457 /* 1458 * node reclaim becomes active if more unmapped pages exist. 1459 */ 1460 unsigned long min_unmapped_pages; 1461 unsigned long min_slab_pages; 1462 #endif /* CONFIG_NUMA */ 1463 1464 /* Write-intensive fields used by page reclaim */ 1465 CACHELINE_PADDING(_pad1_); 1466 1467 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1468 /* 1469 * If memory initialisation on large machines is deferred then this 1470 * is the first PFN that needs to be initialised. 1471 */ 1472 unsigned long first_deferred_pfn; 1473 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 1474 1475 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1476 struct deferred_split deferred_split_queue; 1477 #endif 1478 1479 #ifdef CONFIG_NUMA_BALANCING 1480 /* start time in ms of current promote rate limit period */ 1481 unsigned int nbp_rl_start; 1482 /* number of promote candidate pages at start time of current rate limit period */ 1483 unsigned long nbp_rl_nr_cand; 1484 /* promote threshold in ms */ 1485 unsigned int nbp_threshold; 1486 /* start time in ms of current promote threshold adjustment period */ 1487 unsigned int nbp_th_start; 1488 /* 1489 * number of promote candidate pages at start time of current promote 1490 * threshold adjustment period 1491 */ 1492 unsigned long nbp_th_nr_cand; 1493 #endif 1494 /* Fields commonly accessed by the page reclaim scanner */ 1495 1496 /* 1497 * NOTE: THIS IS UNUSED IF MEMCG IS ENABLED. 1498 * 1499 * Use mem_cgroup_lruvec() to look up lruvecs. 1500 */ 1501 struct lruvec __lruvec; 1502 1503 unsigned long flags; 1504 1505 #ifdef CONFIG_LRU_GEN 1506 /* kswap mm walk data */ 1507 struct lru_gen_mm_walk mm_walk; 1508 /* lru_gen_folio list */ 1509 struct lru_gen_memcg memcg_lru; 1510 #endif 1511 1512 CACHELINE_PADDING(_pad2_); 1513 1514 /* Per-node vmstats */ 1515 struct per_cpu_nodestat __percpu *per_cpu_nodestats; 1516 atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS]; 1517 #ifdef CONFIG_NUMA 1518 struct memory_tier __rcu *memtier; 1519 #endif 1520 #ifdef CONFIG_MEMORY_FAILURE 1521 struct memory_failure_stats mf_stats; 1522 #endif 1523 } pg_data_t; 1524 1525 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages) 1526 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages) 1527 1528 #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn) 1529 #define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid)) 1530 1531 static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat) 1532 { 1533 return pgdat->node_start_pfn + pgdat->node_spanned_pages; 1534 } 1535 1536 #include <linux/memory_hotplug.h> 1537 1538 void build_all_zonelists(pg_data_t *pgdat); 1539 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, 1540 int highest_zoneidx, unsigned int alloc_flags, 1541 long free_pages); 1542 bool zone_watermark_ok(struct zone *z, unsigned int order, 1543 unsigned long mark, int highest_zoneidx, 1544 unsigned int alloc_flags); 1545 1546 enum kswapd_clear_hopeless_reason { 1547 KSWAPD_CLEAR_HOPELESS_OTHER = 0, 1548 KSWAPD_CLEAR_HOPELESS_KSWAPD, 1549 KSWAPD_CLEAR_HOPELESS_DIRECT, 1550 KSWAPD_CLEAR_HOPELESS_PCP, 1551 }; 1552 1553 void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order, 1554 enum zone_type highest_zoneidx); 1555 void kswapd_try_clear_hopeless(struct pglist_data *pgdat, 1556 unsigned int order, int highest_zoneidx); 1557 void kswapd_clear_hopeless(pg_data_t *pgdat, enum kswapd_clear_hopeless_reason reason); 1558 bool kswapd_test_hopeless(pg_data_t *pgdat); 1559 1560 /* 1561 * Memory initialization context, use to differentiate memory added by 1562 * the platform statically or via memory hotplug interface. 1563 */ 1564 enum meminit_context { 1565 MEMINIT_EARLY, 1566 MEMINIT_HOTPLUG, 1567 }; 1568 1569 extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn, 1570 unsigned long size); 1571 1572 extern void lruvec_init(struct lruvec *lruvec); 1573 1574 static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec) 1575 { 1576 #ifdef CONFIG_MEMCG 1577 return lruvec->pgdat; 1578 #else 1579 return container_of(lruvec, struct pglist_data, __lruvec); 1580 #endif 1581 } 1582 1583 #ifdef CONFIG_HAVE_MEMORYLESS_NODES 1584 int local_memory_node(int node_id); 1585 #else 1586 static inline int local_memory_node(int node_id) { return node_id; }; 1587 #endif 1588 1589 /* 1590 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc. 1591 */ 1592 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones) 1593 1594 #ifdef CONFIG_ZONE_DEVICE 1595 static inline bool zone_is_zone_device(const struct zone *zone) 1596 { 1597 return zone_idx(zone) == ZONE_DEVICE; 1598 } 1599 #else 1600 static inline bool zone_is_zone_device(const struct zone *zone) 1601 { 1602 return false; 1603 } 1604 #endif 1605 1606 /* 1607 * Returns true if a zone has pages managed by the buddy allocator. 1608 * All the reclaim decisions have to use this function rather than 1609 * populated_zone(). If the whole zone is reserved then we can easily 1610 * end up with populated_zone() && !managed_zone(). 1611 */ 1612 static inline bool managed_zone(const struct zone *zone) 1613 { 1614 return zone_managed_pages(zone); 1615 } 1616 1617 /* Returns true if a zone has memory */ 1618 static inline bool populated_zone(const struct zone *zone) 1619 { 1620 return zone->present_pages; 1621 } 1622 1623 #ifdef CONFIG_NUMA 1624 static inline int zone_to_nid(const struct zone *zone) 1625 { 1626 return zone->node; 1627 } 1628 1629 static inline void zone_set_nid(struct zone *zone, int nid) 1630 { 1631 zone->node = nid; 1632 } 1633 #else 1634 static inline int zone_to_nid(const struct zone *zone) 1635 { 1636 return 0; 1637 } 1638 1639 static inline void zone_set_nid(struct zone *zone, int nid) {} 1640 #endif 1641 1642 extern int movable_zone; 1643 1644 static inline int is_highmem_idx(enum zone_type idx) 1645 { 1646 #ifdef CONFIG_HIGHMEM 1647 return (idx == ZONE_HIGHMEM || 1648 (idx == ZONE_MOVABLE && movable_zone == ZONE_HIGHMEM)); 1649 #else 1650 return 0; 1651 #endif 1652 } 1653 1654 /** 1655 * is_highmem - helper function to quickly check if a struct zone is a 1656 * highmem zone or not. This is an attempt to keep references 1657 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum. 1658 * @zone: pointer to struct zone variable 1659 * Return: 1 for a highmem zone, 0 otherwise 1660 */ 1661 static inline int is_highmem(const struct zone *zone) 1662 { 1663 return is_highmem_idx(zone_idx(zone)); 1664 } 1665 1666 bool has_managed_zone(enum zone_type zone); 1667 static inline bool has_managed_dma(void) 1668 { 1669 #ifdef CONFIG_ZONE_DMA 1670 return has_managed_zone(ZONE_DMA); 1671 #else 1672 return false; 1673 #endif 1674 } 1675 1676 1677 #ifndef CONFIG_NUMA 1678 1679 extern struct pglist_data contig_page_data; 1680 static inline struct pglist_data *NODE_DATA(int nid) 1681 { 1682 return &contig_page_data; 1683 } 1684 1685 #else /* CONFIG_NUMA */ 1686 1687 #include <asm/mmzone.h> 1688 1689 #endif /* !CONFIG_NUMA */ 1690 1691 extern struct pglist_data *first_online_pgdat(void); 1692 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat); 1693 extern struct zone *next_zone(struct zone *zone); 1694 1695 /** 1696 * for_each_online_pgdat - helper macro to iterate over all online nodes 1697 * @pgdat: pointer to a pg_data_t variable 1698 */ 1699 #define for_each_online_pgdat(pgdat) \ 1700 for (pgdat = first_online_pgdat(); \ 1701 pgdat; \ 1702 pgdat = next_online_pgdat(pgdat)) 1703 /** 1704 * for_each_zone - helper macro to iterate over all memory zones 1705 * @zone: pointer to struct zone variable 1706 * 1707 * The user only needs to declare the zone variable, for_each_zone 1708 * fills it in. 1709 */ 1710 #define for_each_zone(zone) \ 1711 for (zone = (first_online_pgdat())->node_zones; \ 1712 zone; \ 1713 zone = next_zone(zone)) 1714 1715 #define for_each_populated_zone(zone) \ 1716 for (zone = (first_online_pgdat())->node_zones; \ 1717 zone; \ 1718 zone = next_zone(zone)) \ 1719 if (!populated_zone(zone)) \ 1720 ; /* do nothing */ \ 1721 else 1722 1723 static inline struct zone *zonelist_zone(struct zoneref *zoneref) 1724 { 1725 return zoneref->zone; 1726 } 1727 1728 static inline int zonelist_zone_idx(const struct zoneref *zoneref) 1729 { 1730 return zoneref->zone_idx; 1731 } 1732 1733 static inline int zonelist_node_idx(const struct zoneref *zoneref) 1734 { 1735 return zone_to_nid(zoneref->zone); 1736 } 1737 1738 struct zoneref *__next_zones_zonelist(struct zoneref *z, 1739 enum zone_type highest_zoneidx, 1740 nodemask_t *nodes); 1741 1742 /** 1743 * next_zones_zonelist - Returns the next zone at or below highest_zoneidx within the allowed nodemask using a cursor within a zonelist as a starting point 1744 * @z: The cursor used as a starting point for the search 1745 * @highest_zoneidx: The zone index of the highest zone to return 1746 * @nodes: An optional nodemask to filter the zonelist with 1747 * 1748 * This function returns the next zone at or below a given zone index that is 1749 * within the allowed nodemask using a cursor as the starting point for the 1750 * search. The zoneref returned is a cursor that represents the current zone 1751 * being examined. It should be advanced by one before calling 1752 * next_zones_zonelist again. 1753 * 1754 * Return: the next zone at or below highest_zoneidx within the allowed 1755 * nodemask using a cursor within a zonelist as a starting point 1756 */ 1757 static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z, 1758 enum zone_type highest_zoneidx, 1759 nodemask_t *nodes) 1760 { 1761 if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx)) 1762 return z; 1763 return __next_zones_zonelist(z, highest_zoneidx, nodes); 1764 } 1765 1766 /** 1767 * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist 1768 * @zonelist: The zonelist to search for a suitable zone 1769 * @highest_zoneidx: The zone index of the highest zone to return 1770 * @nodes: An optional nodemask to filter the zonelist with 1771 * 1772 * This function returns the first zone at or below a given zone index that is 1773 * within the allowed nodemask. The zoneref returned is a cursor that can be 1774 * used to iterate the zonelist with next_zones_zonelist by advancing it by 1775 * one before calling. 1776 * 1777 * When no eligible zone is found, zoneref->zone is NULL (zoneref itself is 1778 * never NULL). This may happen either genuinely, or due to concurrent nodemask 1779 * update due to cpuset modification. 1780 * 1781 * Return: Zoneref pointer for the first suitable zone found 1782 */ 1783 static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist, 1784 enum zone_type highest_zoneidx, 1785 nodemask_t *nodes) 1786 { 1787 return next_zones_zonelist(zonelist->_zonerefs, 1788 highest_zoneidx, nodes); 1789 } 1790 1791 /** 1792 * for_each_zone_zonelist_nodemask - helper macro to iterate over valid zones in a zonelist at or below a given zone index and within a nodemask 1793 * @zone: The current zone in the iterator 1794 * @z: The current pointer within zonelist->_zonerefs being iterated 1795 * @zlist: The zonelist being iterated 1796 * @highidx: The zone index of the highest zone to return 1797 * @nodemask: Nodemask allowed by the allocator 1798 * 1799 * This iterator iterates though all zones at or below a given zone index and 1800 * within a given nodemask 1801 */ 1802 #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \ 1803 for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \ 1804 zone; \ 1805 z = next_zones_zonelist(++z, highidx, nodemask), \ 1806 zone = zonelist_zone(z)) 1807 1808 #define for_next_zone_zonelist_nodemask(zone, z, highidx, nodemask) \ 1809 for (zone = zonelist_zone(z); \ 1810 zone; \ 1811 z = next_zones_zonelist(++z, highidx, nodemask), \ 1812 zone = zonelist_zone(z)) 1813 1814 1815 /** 1816 * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index 1817 * @zone: The current zone in the iterator 1818 * @z: The current pointer within zonelist->zones being iterated 1819 * @zlist: The zonelist being iterated 1820 * @highidx: The zone index of the highest zone to return 1821 * 1822 * This iterator iterates though all zones at or below a given zone index. 1823 */ 1824 #define for_each_zone_zonelist(zone, z, zlist, highidx) \ 1825 for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL) 1826 1827 /* Whether the 'nodes' are all movable nodes */ 1828 static inline bool movable_only_nodes(nodemask_t *nodes) 1829 { 1830 struct zonelist *zonelist; 1831 struct zoneref *z; 1832 int nid; 1833 1834 if (nodes_empty(*nodes)) 1835 return false; 1836 1837 /* 1838 * We can chose arbitrary node from the nodemask to get a 1839 * zonelist as they are interlinked. We just need to find 1840 * at least one zone that can satisfy kernel allocations. 1841 */ 1842 nid = first_node(*nodes); 1843 zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 1844 z = first_zones_zonelist(zonelist, ZONE_NORMAL, nodes); 1845 return (!zonelist_zone(z)) ? true : false; 1846 } 1847 1848 1849 #ifdef CONFIG_SPARSEMEM 1850 #include <asm/sparsemem.h> 1851 #endif 1852 1853 #ifdef CONFIG_FLATMEM 1854 #define pfn_to_nid(pfn) (0) 1855 #endif 1856 1857 #ifdef CONFIG_SPARSEMEM 1858 1859 /* 1860 * PA_SECTION_SHIFT physical address to/from section number 1861 * PFN_SECTION_SHIFT pfn to/from section number 1862 */ 1863 #define PA_SECTION_SHIFT (SECTION_SIZE_BITS) 1864 #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT) 1865 1866 #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT) 1867 1868 #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT) 1869 #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1)) 1870 1871 #define SECTION_BLOCKFLAGS_BITS \ 1872 ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS) 1873 1874 #if (MAX_PAGE_ORDER + PAGE_SHIFT) > SECTION_SIZE_BITS 1875 #error Allocator MAX_PAGE_ORDER exceeds SECTION_SIZE 1876 #endif 1877 1878 static inline unsigned long pfn_to_section_nr(unsigned long pfn) 1879 { 1880 return pfn >> PFN_SECTION_SHIFT; 1881 } 1882 static inline unsigned long section_nr_to_pfn(unsigned long sec) 1883 { 1884 return sec << PFN_SECTION_SHIFT; 1885 } 1886 1887 #define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK) 1888 #define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK) 1889 1890 #define SUBSECTION_SHIFT 21 1891 #define SUBSECTION_SIZE (1UL << SUBSECTION_SHIFT) 1892 1893 #define PFN_SUBSECTION_SHIFT (SUBSECTION_SHIFT - PAGE_SHIFT) 1894 #define PAGES_PER_SUBSECTION (1UL << PFN_SUBSECTION_SHIFT) 1895 #define PAGE_SUBSECTION_MASK (~(PAGES_PER_SUBSECTION-1)) 1896 1897 #if SUBSECTION_SHIFT > SECTION_SIZE_BITS 1898 #error Subsection size exceeds section size 1899 #else 1900 #define SUBSECTIONS_PER_SECTION (1UL << (SECTION_SIZE_BITS - SUBSECTION_SHIFT)) 1901 #endif 1902 1903 #define SUBSECTION_ALIGN_UP(pfn) ALIGN((pfn), PAGES_PER_SUBSECTION) 1904 #define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK) 1905 1906 struct mem_section_usage { 1907 struct rcu_head rcu; 1908 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1909 DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION); 1910 #endif 1911 /* See declaration of similar field in struct zone */ 1912 unsigned long pageblock_flags[0]; 1913 }; 1914 1915 void subsection_map_init(unsigned long pfn, unsigned long nr_pages); 1916 1917 struct page; 1918 struct page_ext; 1919 struct mem_section { 1920 /* 1921 * This is, logically, a pointer to an array of struct 1922 * pages. However, it is stored with some other magic. 1923 * (see sparse.c::sparse_init_one_section()) 1924 * 1925 * Additionally during early boot we encode node id of 1926 * the location of the section here to guide allocation. 1927 * (see sparse.c::memory_present()) 1928 * 1929 * Making it a UL at least makes someone do a cast 1930 * before using it wrong. 1931 */ 1932 unsigned long section_mem_map; 1933 1934 struct mem_section_usage *usage; 1935 #ifdef CONFIG_PAGE_EXTENSION 1936 /* 1937 * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use 1938 * section. (see page_ext.h about this.) 1939 */ 1940 struct page_ext *page_ext; 1941 unsigned long pad; 1942 #endif 1943 /* 1944 * WARNING: mem_section must be a power-of-2 in size for the 1945 * calculation and use of SECTION_ROOT_MASK to make sense. 1946 */ 1947 }; 1948 1949 #ifdef CONFIG_SPARSEMEM_EXTREME 1950 #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section)) 1951 #else 1952 #define SECTIONS_PER_ROOT 1 1953 #endif 1954 1955 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT) 1956 #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT) 1957 #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1) 1958 1959 #ifdef CONFIG_SPARSEMEM_EXTREME 1960 extern struct mem_section **mem_section; 1961 #else 1962 extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]; 1963 #endif 1964 1965 static inline unsigned long *section_to_usemap(struct mem_section *ms) 1966 { 1967 return ms->usage->pageblock_flags; 1968 } 1969 1970 static inline struct mem_section *__nr_to_section(unsigned long nr) 1971 { 1972 unsigned long root = SECTION_NR_TO_ROOT(nr); 1973 1974 if (unlikely(root >= NR_SECTION_ROOTS)) 1975 return NULL; 1976 1977 #ifdef CONFIG_SPARSEMEM_EXTREME 1978 if (!mem_section || !mem_section[root]) 1979 return NULL; 1980 #endif 1981 return &mem_section[root][nr & SECTION_ROOT_MASK]; 1982 } 1983 extern size_t mem_section_usage_size(void); 1984 1985 /* 1986 * We use the lower bits of the mem_map pointer to store 1987 * a little bit of information. The pointer is calculated 1988 * as mem_map - section_nr_to_pfn(pnum). The result is 1989 * aligned to the minimum alignment of the two values: 1990 * 1. All mem_map arrays are page-aligned. 1991 * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT 1992 * lowest bits. PFN_SECTION_SHIFT is arch-specific 1993 * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the 1994 * worst combination is powerpc with 256k pages, 1995 * which results in PFN_SECTION_SHIFT equal 6. 1996 * To sum it up, at least 6 bits are available on all architectures. 1997 * However, we can exceed 6 bits on some other architectures except 1998 * powerpc (e.g. 15 bits are available on x86_64, 13 bits are available 1999 * with the worst case of 64K pages on arm64) if we make sure the 2000 * exceeded bit is not applicable to powerpc. 2001 */ 2002 enum { 2003 SECTION_MARKED_PRESENT_BIT, 2004 SECTION_HAS_MEM_MAP_BIT, 2005 SECTION_IS_ONLINE_BIT, 2006 SECTION_IS_EARLY_BIT, 2007 #ifdef CONFIG_ZONE_DEVICE 2008 SECTION_TAINT_ZONE_DEVICE_BIT, 2009 #endif 2010 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT 2011 SECTION_IS_VMEMMAP_PREINIT_BIT, 2012 #endif 2013 SECTION_MAP_LAST_BIT, 2014 }; 2015 2016 #define SECTION_MARKED_PRESENT BIT(SECTION_MARKED_PRESENT_BIT) 2017 #define SECTION_HAS_MEM_MAP BIT(SECTION_HAS_MEM_MAP_BIT) 2018 #define SECTION_IS_ONLINE BIT(SECTION_IS_ONLINE_BIT) 2019 #define SECTION_IS_EARLY BIT(SECTION_IS_EARLY_BIT) 2020 #ifdef CONFIG_ZONE_DEVICE 2021 #define SECTION_TAINT_ZONE_DEVICE BIT(SECTION_TAINT_ZONE_DEVICE_BIT) 2022 #endif 2023 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT 2024 #define SECTION_IS_VMEMMAP_PREINIT BIT(SECTION_IS_VMEMMAP_PREINIT_BIT) 2025 #endif 2026 #define SECTION_MAP_MASK (~(BIT(SECTION_MAP_LAST_BIT) - 1)) 2027 #define SECTION_NID_SHIFT SECTION_MAP_LAST_BIT 2028 2029 static inline struct page *__section_mem_map_addr(struct mem_section *section) 2030 { 2031 unsigned long map = section->section_mem_map; 2032 map &= SECTION_MAP_MASK; 2033 return (struct page *)map; 2034 } 2035 2036 static inline int present_section(const struct mem_section *section) 2037 { 2038 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT)); 2039 } 2040 2041 static inline int present_section_nr(unsigned long nr) 2042 { 2043 return present_section(__nr_to_section(nr)); 2044 } 2045 2046 static inline int valid_section(const struct mem_section *section) 2047 { 2048 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP)); 2049 } 2050 2051 static inline int early_section(const struct mem_section *section) 2052 { 2053 return (section && (section->section_mem_map & SECTION_IS_EARLY)); 2054 } 2055 2056 static inline int valid_section_nr(unsigned long nr) 2057 { 2058 return valid_section(__nr_to_section(nr)); 2059 } 2060 2061 static inline int online_section(const struct mem_section *section) 2062 { 2063 return (section && (section->section_mem_map & SECTION_IS_ONLINE)); 2064 } 2065 2066 #ifdef CONFIG_ZONE_DEVICE 2067 static inline int online_device_section(const struct mem_section *section) 2068 { 2069 unsigned long flags = SECTION_IS_ONLINE | SECTION_TAINT_ZONE_DEVICE; 2070 2071 return section && ((section->section_mem_map & flags) == flags); 2072 } 2073 #else 2074 static inline int online_device_section(const struct mem_section *section) 2075 { 2076 return 0; 2077 } 2078 #endif 2079 2080 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT 2081 static inline int preinited_vmemmap_section(const struct mem_section *section) 2082 { 2083 return (section && 2084 (section->section_mem_map & SECTION_IS_VMEMMAP_PREINIT)); 2085 } 2086 2087 void sparse_vmemmap_init_nid_early(int nid); 2088 void sparse_vmemmap_init_nid_late(int nid); 2089 2090 #else 2091 static inline int preinited_vmemmap_section(const struct mem_section *section) 2092 { 2093 return 0; 2094 } 2095 static inline void sparse_vmemmap_init_nid_early(int nid) 2096 { 2097 } 2098 2099 static inline void sparse_vmemmap_init_nid_late(int nid) 2100 { 2101 } 2102 #endif 2103 2104 static inline int online_section_nr(unsigned long nr) 2105 { 2106 return online_section(__nr_to_section(nr)); 2107 } 2108 2109 #ifdef CONFIG_MEMORY_HOTPLUG 2110 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn); 2111 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn); 2112 #endif 2113 2114 static inline struct mem_section *__pfn_to_section(unsigned long pfn) 2115 { 2116 return __nr_to_section(pfn_to_section_nr(pfn)); 2117 } 2118 2119 extern unsigned long __highest_present_section_nr; 2120 2121 static inline int subsection_map_index(unsigned long pfn) 2122 { 2123 return (pfn & ~(PAGE_SECTION_MASK)) / PAGES_PER_SUBSECTION; 2124 } 2125 2126 #ifdef CONFIG_SPARSEMEM_VMEMMAP 2127 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn) 2128 { 2129 int idx = subsection_map_index(pfn); 2130 struct mem_section_usage *usage = READ_ONCE(ms->usage); 2131 2132 return usage ? test_bit(idx, usage->subsection_map) : 0; 2133 } 2134 2135 static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long *pfn) 2136 { 2137 struct mem_section_usage *usage = READ_ONCE(ms->usage); 2138 int idx = subsection_map_index(*pfn); 2139 unsigned long bit; 2140 2141 if (!usage) 2142 return false; 2143 2144 if (test_bit(idx, usage->subsection_map)) 2145 return true; 2146 2147 /* Find the next subsection that exists */ 2148 bit = find_next_bit(usage->subsection_map, SUBSECTIONS_PER_SECTION, idx); 2149 if (bit == SUBSECTIONS_PER_SECTION) 2150 return false; 2151 2152 *pfn = (*pfn & PAGE_SECTION_MASK) + (bit * PAGES_PER_SUBSECTION); 2153 return true; 2154 } 2155 #else 2156 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn) 2157 { 2158 return 1; 2159 } 2160 2161 static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long *pfn) 2162 { 2163 return true; 2164 } 2165 #endif 2166 2167 void sparse_init_early_section(int nid, struct page *map, unsigned long pnum, 2168 unsigned long flags); 2169 2170 #ifndef CONFIG_HAVE_ARCH_PFN_VALID 2171 /** 2172 * pfn_valid - check if there is a valid memory map entry for a PFN 2173 * @pfn: the page frame number to check 2174 * 2175 * Check if there is a valid memory map entry aka struct page for the @pfn. 2176 * Note, that availability of the memory map entry does not imply that 2177 * there is actual usable memory at that @pfn. The struct page may 2178 * represent a hole or an unusable page frame. 2179 * 2180 * Return: 1 for PFNs that have memory map entries and 0 otherwise 2181 */ 2182 static inline int pfn_valid(unsigned long pfn) 2183 { 2184 struct mem_section *ms; 2185 int ret; 2186 2187 /* 2188 * Ensure the upper PAGE_SHIFT bits are clear in the 2189 * pfn. Else it might lead to false positives when 2190 * some of the upper bits are set, but the lower bits 2191 * match a valid pfn. 2192 */ 2193 if (PHYS_PFN(PFN_PHYS(pfn)) != pfn) 2194 return 0; 2195 2196 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) 2197 return 0; 2198 ms = __pfn_to_section(pfn); 2199 rcu_read_lock_sched(); 2200 if (!valid_section(ms)) { 2201 rcu_read_unlock_sched(); 2202 return 0; 2203 } 2204 /* 2205 * Traditionally early sections always returned pfn_valid() for 2206 * the entire section-sized span. 2207 */ 2208 ret = early_section(ms) || pfn_section_valid(ms, pfn); 2209 rcu_read_unlock_sched(); 2210 2211 return ret; 2212 } 2213 2214 /* Returns end_pfn or higher if no valid PFN remaining in range */ 2215 static inline unsigned long first_valid_pfn(unsigned long pfn, unsigned long end_pfn) 2216 { 2217 unsigned long nr = pfn_to_section_nr(pfn); 2218 2219 rcu_read_lock_sched(); 2220 2221 while (nr <= __highest_present_section_nr && pfn < end_pfn) { 2222 struct mem_section *ms = __pfn_to_section(pfn); 2223 2224 if (valid_section(ms) && 2225 (early_section(ms) || pfn_section_first_valid(ms, &pfn))) { 2226 rcu_read_unlock_sched(); 2227 return pfn; 2228 } 2229 2230 /* Nothing left in this section? Skip to next section */ 2231 nr++; 2232 pfn = section_nr_to_pfn(nr); 2233 } 2234 2235 rcu_read_unlock_sched(); 2236 return end_pfn; 2237 } 2238 2239 static inline unsigned long next_valid_pfn(unsigned long pfn, unsigned long end_pfn) 2240 { 2241 pfn++; 2242 2243 if (pfn >= end_pfn) 2244 return end_pfn; 2245 2246 /* 2247 * Either every PFN within the section (or subsection for VMEMMAP) is 2248 * valid, or none of them are. So there's no point repeating the check 2249 * for every PFN; only call first_valid_pfn() again when crossing a 2250 * (sub)section boundary (i.e. !(pfn & ~PAGE_{SUB,}SECTION_MASK)). 2251 */ 2252 if (pfn & ~(IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP) ? 2253 PAGE_SUBSECTION_MASK : PAGE_SECTION_MASK)) 2254 return pfn; 2255 2256 return first_valid_pfn(pfn, end_pfn); 2257 } 2258 2259 2260 #define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ 2261 for ((_pfn) = first_valid_pfn((_start_pfn), (_end_pfn)); \ 2262 (_pfn) < (_end_pfn); \ 2263 (_pfn) = next_valid_pfn((_pfn), (_end_pfn))) 2264 2265 #endif 2266 2267 static inline int pfn_in_present_section(unsigned long pfn) 2268 { 2269 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) 2270 return 0; 2271 return present_section(__pfn_to_section(pfn)); 2272 } 2273 2274 static inline unsigned long next_present_section_nr(unsigned long section_nr) 2275 { 2276 while (++section_nr <= __highest_present_section_nr) { 2277 if (present_section_nr(section_nr)) 2278 return section_nr; 2279 } 2280 2281 return -1; 2282 } 2283 2284 #define for_each_present_section_nr(start, section_nr) \ 2285 for (section_nr = next_present_section_nr(start - 1); \ 2286 section_nr != -1; \ 2287 section_nr = next_present_section_nr(section_nr)) 2288 2289 /* 2290 * These are _only_ used during initialisation, therefore they 2291 * can use __initdata ... They could have names to indicate 2292 * this restriction. 2293 */ 2294 #ifdef CONFIG_NUMA 2295 #define pfn_to_nid(pfn) \ 2296 ({ \ 2297 unsigned long __pfn_to_nid_pfn = (pfn); \ 2298 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \ 2299 }) 2300 #else 2301 #define pfn_to_nid(pfn) (0) 2302 #endif 2303 2304 #else 2305 #define sparse_index_init(_sec, _nid) do {} while (0) 2306 #define sparse_vmemmap_init_nid_early(_nid) do {} while (0) 2307 #define sparse_vmemmap_init_nid_late(_nid) do {} while (0) 2308 #define pfn_in_present_section pfn_valid 2309 #define subsection_map_init(_pfn, _nr_pages) do {} while (0) 2310 #endif /* CONFIG_SPARSEMEM */ 2311 2312 /* 2313 * Fallback case for when the architecture provides its own pfn_valid() but 2314 * not a corresponding for_each_valid_pfn(). 2315 */ 2316 #ifndef for_each_valid_pfn 2317 #define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ 2318 for ((_pfn) = (_start_pfn); (_pfn) < (_end_pfn); (_pfn)++) \ 2319 if (pfn_valid(_pfn)) 2320 #endif 2321 2322 #endif /* !__GENERATING_BOUNDS.H */ 2323 #endif /* !__ASSEMBLY__ */ 2324 #endif /* _LINUX_MMZONE_H */ 2325