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