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/wait.h>
11 #include <linux/bitops.h>
12 #include <linux/cache.h>
13 #include <linux/threads.h>
14 #include <linux/numa.h>
15 #include <linux/init.h>
16 #include <linux/seqlock.h>
17 #include <linux/nodemask.h>
18 #include <linux/pageblock-flags.h>
19 #include <linux/page-flags-layout.h>
20 #include <linux/atomic.h>
21 #include <linux/mm_types.h>
22 #include <linux/page-flags.h>
23 #include <linux/android_kabi.h>
24 #include <asm/page.h>
25
26 /* Free memory management - zoned buddy allocator. */
27 #ifndef CONFIG_FORCE_MAX_ZONEORDER
28 #define MAX_ORDER 11
29 #else
30 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
31 #endif
32 #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
33
34 /*
35 * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
36 * costly to service. That is between allocation orders which should
37 * coalesce naturally under reasonable reclaim pressure and those which
38 * will not.
39 */
40 #define PAGE_ALLOC_COSTLY_ORDER 3
41
42 enum migratetype {
43 MIGRATE_UNMOVABLE,
44 MIGRATE_MOVABLE,
45 MIGRATE_RECLAIMABLE,
46 #ifdef CONFIG_CMA
47 /*
48 * MIGRATE_CMA migration type is designed to mimic the way
49 * ZONE_MOVABLE works. Only movable pages can be allocated
50 * from MIGRATE_CMA pageblocks and page allocator never
51 * implicitly change migration type of MIGRATE_CMA pageblock.
52 *
53 * The way to use it is to change migratetype of a range of
54 * pageblocks to MIGRATE_CMA which can be done by
55 * __free_pageblock_cma() function. What is important though
56 * is that a range of pageblocks must be aligned to
57 * MAX_ORDER_NR_PAGES should biggest page be bigger then
58 * a single pageblock.
59 */
60 MIGRATE_CMA,
61 #endif
62 MIGRATE_PCPTYPES, /* the number of types on the pcp lists */
63 MIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES,
64 #ifdef CONFIG_MEMORY_ISOLATION
65 MIGRATE_ISOLATE, /* can't allocate from here */
66 #endif
67 MIGRATE_TYPES
68 };
69
70 /* In mm/page_alloc.c; keep in sync also with show_migration_types() there */
71 extern const char * const migratetype_names[MIGRATE_TYPES];
72
73 #ifdef CONFIG_CMA
74 # define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)
75 # define is_migrate_cma_page(_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA)
76 # define get_cma_migrate_type() MIGRATE_CMA
77 #else
78 # define is_migrate_cma(migratetype) false
79 # define is_migrate_cma_page(_page) false
80 # define get_cma_migrate_type() MIGRATE_MOVABLE
81 #endif
82
is_migrate_movable(int mt)83 static inline bool is_migrate_movable(int mt)
84 {
85 return is_migrate_cma(mt) || mt == MIGRATE_MOVABLE;
86 }
87
88 #define for_each_migratetype_order(order, type) \
89 for (order = 0; order < MAX_ORDER; order++) \
90 for (type = 0; type < MIGRATE_TYPES; type++)
91
92 extern int page_group_by_mobility_disabled;
93
94 #define NR_MIGRATETYPE_BITS (PB_migrate_end - PB_migrate + 1)
95 #define MIGRATETYPE_MASK ((1UL << NR_MIGRATETYPE_BITS) - 1)
96
97 #define get_pageblock_migratetype(page) \
98 get_pfnblock_flags_mask(page, page_to_pfn(page), \
99 PB_migrate_end, MIGRATETYPE_MASK)
100
101 struct free_area {
102 struct list_head free_list[MIGRATE_TYPES];
103 unsigned long nr_free;
104 };
105
106 /* Used for pages not on another list */
add_to_free_area(struct page * page,struct free_area * area,int migratetype)107 static inline void add_to_free_area(struct page *page, struct free_area *area,
108 int migratetype)
109 {
110 list_add(&page->lru, &area->free_list[migratetype]);
111 area->nr_free++;
112 }
113
114 /* Used for pages not on another list */
add_to_free_area_tail(struct page * page,struct free_area * area,int migratetype)115 static inline void add_to_free_area_tail(struct page *page, struct free_area *area,
116 int migratetype)
117 {
118 list_add_tail(&page->lru, &area->free_list[migratetype]);
119 area->nr_free++;
120 }
121
122 #ifdef CONFIG_SHUFFLE_PAGE_ALLOCATOR
123 /* Used to preserve page allocation order entropy */
124 void add_to_free_area_random(struct page *page, struct free_area *area,
125 int migratetype);
126 #else
add_to_free_area_random(struct page * page,struct free_area * area,int migratetype)127 static inline void add_to_free_area_random(struct page *page,
128 struct free_area *area, int migratetype)
129 {
130 add_to_free_area(page, area, migratetype);
131 }
132 #endif
133
134 /* Used for pages which are on another list */
move_to_free_area(struct page * page,struct free_area * area,int migratetype)135 static inline void move_to_free_area(struct page *page, struct free_area *area,
136 int migratetype)
137 {
138 list_move(&page->lru, &area->free_list[migratetype]);
139 }
140
get_page_from_free_area(struct free_area * area,int migratetype)141 static inline struct page *get_page_from_free_area(struct free_area *area,
142 int migratetype)
143 {
144 return list_first_entry_or_null(&area->free_list[migratetype],
145 struct page, lru);
146 }
147
del_page_from_free_area(struct page * page,struct free_area * area)148 static inline void del_page_from_free_area(struct page *page,
149 struct free_area *area)
150 {
151 list_del(&page->lru);
152 __ClearPageBuddy(page);
153 set_page_private(page, 0);
154 area->nr_free--;
155 }
156
free_area_empty(struct free_area * area,int migratetype)157 static inline bool free_area_empty(struct free_area *area, int migratetype)
158 {
159 return list_empty(&area->free_list[migratetype]);
160 }
161
162 struct pglist_data;
163
164 /*
165 * zone->lock and the zone lru_lock are two of the hottest locks in the kernel.
166 * So add a wild amount of padding here to ensure that they fall into separate
167 * cachelines. There are very few zone structures in the machine, so space
168 * consumption is not a concern here.
169 */
170 #if defined(CONFIG_SMP)
171 struct zone_padding {
172 char x[0];
173 } ____cacheline_internodealigned_in_smp;
174 #define ZONE_PADDING(name) struct zone_padding name;
175 #else
176 #define ZONE_PADDING(name)
177 #endif
178
179 #ifdef CONFIG_NUMA
180 enum numa_stat_item {
181 NUMA_HIT, /* allocated in intended node */
182 NUMA_MISS, /* allocated in non intended node */
183 NUMA_FOREIGN, /* was intended here, hit elsewhere */
184 NUMA_INTERLEAVE_HIT, /* interleaver preferred this zone */
185 NUMA_LOCAL, /* allocation from local node */
186 NUMA_OTHER, /* allocation from other node */
187 NR_VM_NUMA_STAT_ITEMS
188 };
189 #else
190 #define NR_VM_NUMA_STAT_ITEMS 0
191 #endif
192
193 enum zone_stat_item {
194 /* First 128 byte cacheline (assuming 64 bit words) */
195 NR_FREE_PAGES,
196 NR_ZONE_LRU_BASE, /* Used only for compaction and reclaim retry */
197 NR_ZONE_INACTIVE_ANON = NR_ZONE_LRU_BASE,
198 NR_ZONE_ACTIVE_ANON,
199 NR_ZONE_INACTIVE_FILE,
200 NR_ZONE_ACTIVE_FILE,
201 NR_ZONE_UNEVICTABLE,
202 NR_ZONE_WRITE_PENDING, /* Count of dirty, writeback and unstable pages */
203 NR_MLOCK, /* mlock()ed pages found and moved off LRU */
204 NR_PAGETABLE, /* used for pagetables */
205 NR_KERNEL_STACK_KB, /* measured in KiB */
206 #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
207 NR_KERNEL_SCS_BYTES, /* measured in bytes */
208 #endif
209 /* Second 128 byte cacheline */
210 NR_BOUNCE,
211 #if IS_ENABLED(CONFIG_ZSMALLOC)
212 NR_ZSPAGES, /* allocated in zsmalloc */
213 #endif
214 NR_FREE_CMA_PAGES,
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,
225 NR_SLAB_UNRECLAIMABLE,
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,
230 WORKINGSET_ACTIVATE,
231 WORKINGSET_RESTORE,
232 WORKINGSET_NODERECLAIM,
233 NR_ANON_MAPPED, /* Mapped anonymous pages */
234 NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
235 only modified from process context */
236 NR_FILE_PAGES,
237 NR_FILE_DIRTY,
238 NR_WRITEBACK,
239 NR_WRITEBACK_TEMP, /* Writeback using temporary buffers */
240 NR_SHMEM, /* shmem pages (included tmpfs/GEM pages) */
241 NR_SHMEM_THPS,
242 NR_SHMEM_PMDMAPPED,
243 NR_FILE_THPS,
244 NR_FILE_PMDMAPPED,
245 NR_ANON_THPS,
246 NR_UNSTABLE_NFS, /* NFS unstable pages */
247 NR_VMSCAN_WRITE,
248 NR_VMSCAN_IMMEDIATE, /* Prioritise for reclaim when writeback ends */
249 NR_DIRTIED, /* page dirtyings since bootup */
250 NR_WRITTEN, /* page writings since bootup */
251 NR_KERNEL_MISC_RECLAIMABLE, /* reclaimable non-slab kernel pages */
252 NR_VM_NODE_STAT_ITEMS
253 };
254
255 /*
256 * We do arithmetic on the LRU lists in various places in the code,
257 * so it is important to keep the active lists LRU_ACTIVE higher in
258 * the array than the corresponding inactive lists, and to keep
259 * the *_FILE lists LRU_FILE higher than the corresponding _ANON lists.
260 *
261 * This has to be kept in sync with the statistics in zone_stat_item
262 * above and the descriptions in vmstat_text in mm/vmstat.c
263 */
264 #define LRU_BASE 0
265 #define LRU_ACTIVE 1
266 #define LRU_FILE 2
267
268 enum lru_list {
269 LRU_INACTIVE_ANON = LRU_BASE,
270 LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE,
271 LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
272 LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
273 LRU_UNEVICTABLE,
274 NR_LRU_LISTS
275 };
276
277 #define for_each_lru(lru) for (lru = 0; lru < NR_LRU_LISTS; lru++)
278
279 #define for_each_evictable_lru(lru) for (lru = 0; lru <= LRU_ACTIVE_FILE; lru++)
280
is_file_lru(enum lru_list lru)281 static inline int is_file_lru(enum lru_list lru)
282 {
283 return (lru == LRU_INACTIVE_FILE || lru == LRU_ACTIVE_FILE);
284 }
285
is_active_lru(enum lru_list lru)286 static inline int is_active_lru(enum lru_list lru)
287 {
288 return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE);
289 }
290
291 struct zone_reclaim_stat {
292 /*
293 * The pageout code in vmscan.c keeps track of how many of the
294 * mem/swap backed and file backed pages are referenced.
295 * The higher the rotated/scanned ratio, the more valuable
296 * that cache is.
297 *
298 * The anon LRU stats live in [0], file LRU stats in [1]
299 */
300 unsigned long recent_rotated[2];
301 unsigned long recent_scanned[2];
302 };
303
304 struct lruvec {
305 struct list_head lists[NR_LRU_LISTS];
306 struct zone_reclaim_stat reclaim_stat;
307 /* Evictions & activations on the inactive file list */
308 atomic_long_t inactive_age;
309 /* Refaults at the time of last reclaim cycle */
310 unsigned long refaults;
311 #ifdef CONFIG_MEMCG
312 struct pglist_data *pgdat;
313 #endif
314 };
315
316 /* Isolate unmapped file */
317 #define ISOLATE_UNMAPPED ((__force isolate_mode_t)0x2)
318 /* Isolate for asynchronous migration */
319 #define ISOLATE_ASYNC_MIGRATE ((__force isolate_mode_t)0x4)
320 /* Isolate unevictable pages */
321 #define ISOLATE_UNEVICTABLE ((__force isolate_mode_t)0x8)
322
323 /* LRU Isolation modes. */
324 typedef unsigned __bitwise isolate_mode_t;
325
326 enum zone_watermarks {
327 WMARK_MIN,
328 WMARK_LOW,
329 WMARK_HIGH,
330 NR_WMARK
331 };
332
333 #define min_wmark_pages(z) (z->_watermark[WMARK_MIN] + z->watermark_boost)
334 #define low_wmark_pages(z) (z->_watermark[WMARK_LOW] + z->watermark_boost)
335 #define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost)
336 #define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost)
337
338 struct per_cpu_pages {
339 int count; /* number of pages in the list */
340 int high; /* high watermark, emptying needed */
341 int batch; /* chunk size for buddy add/remove */
342
343 /* Lists of pages, one per migrate type stored on the pcp-lists */
344 struct list_head lists[MIGRATE_PCPTYPES];
345 };
346
347 struct per_cpu_pageset {
348 struct per_cpu_pages pcp;
349 #ifdef CONFIG_NUMA
350 s8 expire;
351 u16 vm_numa_stat_diff[NR_VM_NUMA_STAT_ITEMS];
352 #endif
353 #ifdef CONFIG_SMP
354 s8 stat_threshold;
355 s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
356 #endif
357 };
358
359 struct per_cpu_nodestat {
360 s8 stat_threshold;
361 s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
362 };
363
364 #endif /* !__GENERATING_BOUNDS.H */
365
366 enum zone_type {
367 #ifdef CONFIG_ZONE_DMA
368 /*
369 * ZONE_DMA is used when there are devices that are not able
370 * to do DMA to all of addressable memory (ZONE_NORMAL). Then we
371 * carve out the portion of memory that is needed for these devices.
372 * The range is arch specific.
373 *
374 * Some examples
375 *
376 * Architecture Limit
377 * ---------------------------
378 * parisc, ia64, sparc <4G
379 * s390, powerpc <2G
380 * arm Various
381 * alpha Unlimited or 0-16MB.
382 *
383 * i386, x86_64 and multiple other arches
384 * <16M.
385 */
386 ZONE_DMA,
387 #endif
388 #ifdef CONFIG_ZONE_DMA32
389 /*
390 * x86_64 needs two ZONE_DMAs because it supports devices that are
391 * only able to do DMA to the lower 16M but also 32 bit devices that
392 * can only do DMA areas below 4G.
393 */
394 ZONE_DMA32,
395 #endif
396 /*
397 * Normal addressable memory is in ZONE_NORMAL. DMA operations can be
398 * performed on pages in ZONE_NORMAL if the DMA devices support
399 * transfers to all addressable memory.
400 */
401 ZONE_NORMAL,
402 #ifdef CONFIG_HIGHMEM
403 /*
404 * A memory area that is only addressable by the kernel through
405 * mapping portions into its own address space. This is for example
406 * used by i386 to allow the kernel to address the memory beyond
407 * 900MB. The kernel will set up special mappings (page
408 * table entries on i386) for each page that the kernel needs to
409 * access.
410 */
411 ZONE_HIGHMEM,
412 #endif
413 ZONE_MOVABLE,
414 #ifdef CONFIG_ZONE_DEVICE
415 ZONE_DEVICE,
416 #endif
417 __MAX_NR_ZONES
418
419 };
420
421 #ifndef __GENERATING_BOUNDS_H
422
423 struct zone {
424 /* Read-mostly fields */
425
426 /* zone watermarks, access with *_wmark_pages(zone) macros */
427 unsigned long _watermark[NR_WMARK];
428 unsigned long watermark_boost;
429
430 unsigned long nr_reserved_highatomic;
431
432 /*
433 * We don't know if the memory that we're going to allocate will be
434 * freeable or/and it will be released eventually, so to avoid totally
435 * wasting several GB of ram we must reserve some of the lower zone
436 * memory (otherwise we risk to run OOM on the lower zones despite
437 * there being tons of freeable ram on the higher zones). This array is
438 * recalculated at runtime if the sysctl_lowmem_reserve_ratio sysctl
439 * changes.
440 */
441 long lowmem_reserve[MAX_NR_ZONES];
442
443 #ifdef CONFIG_NUMA
444 int node;
445 #endif
446 struct pglist_data *zone_pgdat;
447 struct per_cpu_pageset __percpu *pageset;
448
449 #ifdef CONFIG_CMA
450 bool cma_alloc;
451 #endif
452
453 #ifndef CONFIG_SPARSEMEM
454 /*
455 * Flags for a pageblock_nr_pages block. See pageblock-flags.h.
456 * In SPARSEMEM, this map is stored in struct mem_section
457 */
458 unsigned long *pageblock_flags;
459 #endif /* CONFIG_SPARSEMEM */
460
461 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
462 unsigned long zone_start_pfn;
463
464 /*
465 * spanned_pages is the total pages spanned by the zone, including
466 * holes, which is calculated as:
467 * spanned_pages = zone_end_pfn - zone_start_pfn;
468 *
469 * present_pages is physical pages existing within the zone, which
470 * is calculated as:
471 * present_pages = spanned_pages - absent_pages(pages in holes);
472 *
473 * managed_pages is present pages managed by the buddy system, which
474 * is calculated as (reserved_pages includes pages allocated by the
475 * bootmem allocator):
476 * managed_pages = present_pages - reserved_pages;
477 *
478 * So present_pages may be used by memory hotplug or memory power
479 * management logic to figure out unmanaged pages by checking
480 * (present_pages - managed_pages). And managed_pages should be used
481 * by page allocator and vm scanner to calculate all kinds of watermarks
482 * and thresholds.
483 *
484 * Locking rules:
485 *
486 * zone_start_pfn and spanned_pages are protected by span_seqlock.
487 * It is a seqlock because it has to be read outside of zone->lock,
488 * and it is done in the main allocator path. But, it is written
489 * quite infrequently.
490 *
491 * The span_seq lock is declared along with zone->lock because it is
492 * frequently read in proximity to zone->lock. It's good to
493 * give them a chance of being in the same cacheline.
494 *
495 * Write access to present_pages at runtime should be protected by
496 * mem_hotplug_begin/end(). Any reader who can't tolerant drift of
497 * present_pages should get_online_mems() to get a stable value.
498 */
499 atomic_long_t managed_pages;
500 unsigned long spanned_pages;
501 unsigned long present_pages;
502
503 const char *name;
504
505 #ifdef CONFIG_MEMORY_ISOLATION
506 /*
507 * Number of isolated pageblock. It is used to solve incorrect
508 * freepage counting problem due to racy retrieving migratetype
509 * of pageblock. Protected by zone->lock.
510 */
511 unsigned long nr_isolate_pageblock;
512 #endif
513
514 #ifdef CONFIG_MEMORY_HOTPLUG
515 /* see spanned/present_pages for more description */
516 seqlock_t span_seqlock;
517 #endif
518
519 int initialized;
520
521 /* Write-intensive fields used from the page allocator */
522 ZONE_PADDING(_pad1_)
523
524 /* free areas of different sizes */
525 struct free_area free_area[MAX_ORDER];
526
527 /* zone flags, see below */
528 unsigned long flags;
529
530 /* Primarily protects free_area */
531 spinlock_t lock;
532
533 /* Write-intensive fields used by compaction and vmstats. */
534 ZONE_PADDING(_pad2_)
535
536 /*
537 * When free pages are below this point, additional steps are taken
538 * when reading the number of free pages to avoid per-cpu counter
539 * drift allowing watermarks to be breached
540 */
541 unsigned long percpu_drift_mark;
542
543 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
544 /* pfn where compaction free scanner should start */
545 unsigned long compact_cached_free_pfn;
546 /* pfn where async and sync compaction migration scanner should start */
547 unsigned long compact_cached_migrate_pfn[2];
548 unsigned long compact_init_migrate_pfn;
549 unsigned long compact_init_free_pfn;
550 #endif
551
552 #ifdef CONFIG_COMPACTION
553 /*
554 * On compaction failure, 1<<compact_defer_shift compactions
555 * are skipped before trying again. The number attempted since
556 * last failure is tracked with compact_considered.
557 */
558 unsigned int compact_considered;
559 unsigned int compact_defer_shift;
560 int compact_order_failed;
561 #endif
562
563 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
564 /* Set to true when the PG_migrate_skip bits should be cleared */
565 bool compact_blockskip_flush;
566 #endif
567
568 bool contiguous;
569
570 ZONE_PADDING(_pad3_)
571 /* Zone statistics */
572 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
573 atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS];
574
575 ANDROID_KABI_RESERVE(1);
576 ANDROID_KABI_RESERVE(2);
577 ANDROID_KABI_RESERVE(3);
578 ANDROID_KABI_RESERVE(4);
579 } ____cacheline_internodealigned_in_smp;
580
581 enum pgdat_flags {
582 PGDAT_CONGESTED, /* pgdat has many dirty pages backed by
583 * a congested BDI
584 */
585 PGDAT_DIRTY, /* reclaim scanning has recently found
586 * many dirty file pages at the tail
587 * of the LRU.
588 */
589 PGDAT_WRITEBACK, /* reclaim scanning has recently found
590 * many pages under writeback
591 */
592 PGDAT_RECLAIM_LOCKED, /* prevents concurrent reclaim */
593 };
594
595 enum zone_flags {
596 ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks.
597 * Cleared when kswapd is woken.
598 */
599 };
600
zone_managed_pages(struct zone * zone)601 static inline unsigned long zone_managed_pages(struct zone *zone)
602 {
603 return (unsigned long)atomic_long_read(&zone->managed_pages);
604 }
605
zone_end_pfn(const struct zone * zone)606 static inline unsigned long zone_end_pfn(const struct zone *zone)
607 {
608 return zone->zone_start_pfn + zone->spanned_pages;
609 }
610
zone_spans_pfn(const struct zone * zone,unsigned long pfn)611 static inline bool zone_spans_pfn(const struct zone *zone, unsigned long pfn)
612 {
613 return zone->zone_start_pfn <= pfn && pfn < zone_end_pfn(zone);
614 }
615
zone_is_initialized(struct zone * zone)616 static inline bool zone_is_initialized(struct zone *zone)
617 {
618 return zone->initialized;
619 }
620
zone_is_empty(struct zone * zone)621 static inline bool zone_is_empty(struct zone *zone)
622 {
623 return zone->spanned_pages == 0;
624 }
625
626 /*
627 * Return true if [start_pfn, start_pfn + nr_pages) range has a non-empty
628 * intersection with the given zone
629 */
zone_intersects(struct zone * zone,unsigned long start_pfn,unsigned long nr_pages)630 static inline bool zone_intersects(struct zone *zone,
631 unsigned long start_pfn, unsigned long nr_pages)
632 {
633 if (zone_is_empty(zone))
634 return false;
635 if (start_pfn >= zone_end_pfn(zone) ||
636 start_pfn + nr_pages <= zone->zone_start_pfn)
637 return false;
638
639 return true;
640 }
641
642 /*
643 * The "priority" of VM scanning is how much of the queues we will scan in one
644 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
645 * queues ("queue_length >> 12") during an aging round.
646 */
647 #define DEF_PRIORITY 12
648
649 /* Maximum number of zones on a zonelist */
650 #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
651
652 enum {
653 ZONELIST_FALLBACK, /* zonelist with fallback */
654 #ifdef CONFIG_NUMA
655 /*
656 * The NUMA zonelists are doubled because we need zonelists that
657 * restrict the allocations to a single node for __GFP_THISNODE.
658 */
659 ZONELIST_NOFALLBACK, /* zonelist without fallback (__GFP_THISNODE) */
660 #endif
661 MAX_ZONELISTS
662 };
663
664 /*
665 * This struct contains information about a zone in a zonelist. It is stored
666 * here to avoid dereferences into large structures and lookups of tables
667 */
668 struct zoneref {
669 struct zone *zone; /* Pointer to actual zone */
670 int zone_idx; /* zone_idx(zoneref->zone) */
671 };
672
673 /*
674 * One allocation request operates on a zonelist. A zonelist
675 * is a list of zones, the first one is the 'goal' of the
676 * allocation, the other zones are fallback zones, in decreasing
677 * priority.
678 *
679 * To speed the reading of the zonelist, the zonerefs contain the zone index
680 * of the entry being read. Helper functions to access information given
681 * a struct zoneref are
682 *
683 * zonelist_zone() - Return the struct zone * for an entry in _zonerefs
684 * zonelist_zone_idx() - Return the index of the zone for an entry
685 * zonelist_node_idx() - Return the index of the node for an entry
686 */
687 struct zonelist {
688 struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
689 };
690
691 #ifndef CONFIG_DISCONTIGMEM
692 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
693 extern struct page *mem_map;
694 #endif
695
696 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_GKI_OPT_FEATURES)
697 struct deferred_split {
698 spinlock_t split_queue_lock;
699 struct list_head split_queue;
700 unsigned long split_queue_len;
701 };
702 #endif
703
704 /*
705 * On NUMA machines, each NUMA node would have a pg_data_t to describe
706 * it's memory layout. On UMA machines there is a single pglist_data which
707 * describes the whole memory.
708 *
709 * Memory statistics and page replacement data structures are maintained on a
710 * per-zone basis.
711 */
712 struct bootmem_data;
713 typedef struct pglist_data {
714 struct zone node_zones[MAX_NR_ZONES];
715 struct zonelist node_zonelists[MAX_ZONELISTS];
716 int nr_zones;
717 #ifdef CONFIG_FLAT_NODE_MEM_MAP /* means !SPARSEMEM */
718 struct page *node_mem_map;
719 #ifdef CONFIG_PAGE_EXTENSION
720 struct page_ext *node_page_ext;
721 #endif
722 #endif
723 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
724 /*
725 * Must be held any time you expect node_start_pfn,
726 * node_present_pages, node_spanned_pages or nr_zones to stay constant.
727 * Also synchronizes pgdat->first_deferred_pfn during deferred page
728 * init.
729 *
730 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
731 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
732 * or CONFIG_DEFERRED_STRUCT_PAGE_INIT.
733 *
734 * Nests above zone->lock and zone->span_seqlock
735 */
736 spinlock_t node_size_lock;
737 #endif
738 unsigned long node_start_pfn;
739 unsigned long node_present_pages; /* total number of physical pages */
740 unsigned long node_spanned_pages; /* total size of physical page
741 range, including holes */
742 int node_id;
743 wait_queue_head_t kswapd_wait;
744 wait_queue_head_t pfmemalloc_wait;
745 struct task_struct *kswapd; /* Protected by
746 mem_hotplug_begin/end() */
747 int kswapd_order;
748 enum zone_type kswapd_classzone_idx;
749
750 int kswapd_failures; /* Number of 'reclaimed == 0' runs */
751
752 #ifdef CONFIG_COMPACTION
753 int kcompactd_max_order;
754 enum zone_type kcompactd_classzone_idx;
755 wait_queue_head_t kcompactd_wait;
756 struct task_struct *kcompactd;
757 #endif
758 /*
759 * This is a per-node reserve of pages that are not available
760 * to userspace allocations.
761 */
762 unsigned long totalreserve_pages;
763
764 #ifdef CONFIG_NUMA
765 /*
766 * zone reclaim becomes active if more unmapped pages exist.
767 */
768 unsigned long min_unmapped_pages;
769 unsigned long min_slab_pages;
770 #endif /* CONFIG_NUMA */
771
772 /* Write-intensive fields used by page reclaim */
773 ZONE_PADDING(_pad1_)
774 spinlock_t lru_lock;
775
776 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
777 /*
778 * If memory initialisation on large machines is deferred then this
779 * is the first PFN that needs to be initialised.
780 */
781 unsigned long first_deferred_pfn;
782 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
783
784 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_GKI_OPT_FEATURES)
785 struct deferred_split deferred_split_queue;
786 #endif
787
788 /* Fields commonly accessed by the page reclaim scanner */
789 struct lruvec lruvec;
790
791 unsigned long flags;
792
793 ZONE_PADDING(_pad2_)
794
795 /* Per-node vmstats */
796 struct per_cpu_nodestat __percpu *per_cpu_nodestats;
797 atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS];
798 } pg_data_t;
799
800 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
801 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
802 #ifdef CONFIG_FLAT_NODE_MEM_MAP
803 #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
804 #else
805 #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
806 #endif
807 #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
808
809 #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn)
810 #define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid))
811
node_lruvec(struct pglist_data * pgdat)812 static inline struct lruvec *node_lruvec(struct pglist_data *pgdat)
813 {
814 return &pgdat->lruvec;
815 }
816
pgdat_end_pfn(pg_data_t * pgdat)817 static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
818 {
819 return pgdat->node_start_pfn + pgdat->node_spanned_pages;
820 }
821
pgdat_is_empty(pg_data_t * pgdat)822 static inline bool pgdat_is_empty(pg_data_t *pgdat)
823 {
824 return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
825 }
826
827 #include <linux/memory_hotplug.h>
828
829 void build_all_zonelists(pg_data_t *pgdat);
830 void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
831 enum zone_type classzone_idx);
832 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
833 int classzone_idx, unsigned int alloc_flags,
834 long free_pages);
835 bool zone_watermark_ok(struct zone *z, unsigned int order,
836 unsigned long mark, int classzone_idx,
837 unsigned int alloc_flags);
838 bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
839 unsigned long mark, int classzone_idx);
840 /*
841 * Memory initialization context, use to differentiate memory added by
842 * the platform statically or via memory hotplug interface.
843 */
844 enum meminit_context {
845 MEMINIT_EARLY,
846 MEMINIT_HOTPLUG,
847 };
848
849 extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
850 unsigned long size);
851
852 extern void lruvec_init(struct lruvec *lruvec);
853
lruvec_pgdat(struct lruvec * lruvec)854 static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec)
855 {
856 #ifdef CONFIG_MEMCG
857 return lruvec->pgdat;
858 #else
859 return container_of(lruvec, struct pglist_data, lruvec);
860 #endif
861 }
862
863 extern unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx);
864
865 #ifdef CONFIG_HAVE_MEMORY_PRESENT
866 void memory_present(int nid, unsigned long start, unsigned long end);
867 #else
memory_present(int nid,unsigned long start,unsigned long end)868 static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
869 #endif
870
871 #if defined(CONFIG_SPARSEMEM)
872 void memblocks_present(void);
873 #else
memblocks_present(void)874 static inline void memblocks_present(void) {}
875 #endif
876
877 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
878 int local_memory_node(int node_id);
879 #else
local_memory_node(int node_id)880 static inline int local_memory_node(int node_id) { return node_id; };
881 #endif
882
883 /*
884 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
885 */
886 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
887
888 /*
889 * Returns true if a zone has pages managed by the buddy allocator.
890 * All the reclaim decisions have to use this function rather than
891 * populated_zone(). If the whole zone is reserved then we can easily
892 * end up with populated_zone() && !managed_zone().
893 */
managed_zone(struct zone * zone)894 static inline bool managed_zone(struct zone *zone)
895 {
896 return zone_managed_pages(zone);
897 }
898
899 /* Returns true if a zone has memory */
populated_zone(struct zone * zone)900 static inline bool populated_zone(struct zone *zone)
901 {
902 return zone->present_pages;
903 }
904
905 #ifdef CONFIG_NUMA
zone_to_nid(struct zone * zone)906 static inline int zone_to_nid(struct zone *zone)
907 {
908 return zone->node;
909 }
910
zone_set_nid(struct zone * zone,int nid)911 static inline void zone_set_nid(struct zone *zone, int nid)
912 {
913 zone->node = nid;
914 }
915 #else
zone_to_nid(struct zone * zone)916 static inline int zone_to_nid(struct zone *zone)
917 {
918 return 0;
919 }
920
zone_set_nid(struct zone * zone,int nid)921 static inline void zone_set_nid(struct zone *zone, int nid) {}
922 #endif
923
924 extern int movable_zone;
925
926 #ifdef CONFIG_HIGHMEM
zone_movable_is_highmem(void)927 static inline int zone_movable_is_highmem(void)
928 {
929 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
930 return movable_zone == ZONE_HIGHMEM;
931 #else
932 return (ZONE_MOVABLE - 1) == ZONE_HIGHMEM;
933 #endif
934 }
935 #endif
936
is_highmem_idx(enum zone_type idx)937 static inline int is_highmem_idx(enum zone_type idx)
938 {
939 #ifdef CONFIG_HIGHMEM
940 return (idx == ZONE_HIGHMEM ||
941 (idx == ZONE_MOVABLE && zone_movable_is_highmem()));
942 #else
943 return 0;
944 #endif
945 }
946
947 #ifdef CONFIG_ZONE_DMA
948 bool has_managed_dma(void);
949 #else
has_managed_dma(void)950 static inline bool has_managed_dma(void)
951 {
952 return false;
953 }
954 #endif
955
956 /**
957 * is_highmem - helper function to quickly check if a struct zone is a
958 * highmem zone or not. This is an attempt to keep references
959 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
960 * @zone - pointer to struct zone variable
961 */
is_highmem(struct zone * zone)962 static inline int is_highmem(struct zone *zone)
963 {
964 #ifdef CONFIG_HIGHMEM
965 return is_highmem_idx(zone_idx(zone));
966 #else
967 return 0;
968 #endif
969 }
970
971 /* These two functions are used to setup the per zone pages min values */
972 struct ctl_table;
973 int min_free_kbytes_sysctl_handler(struct ctl_table *, int,
974 void __user *, size_t *, loff_t *);
975 int watermark_boost_factor_sysctl_handler(struct ctl_table *, int,
976 void __user *, size_t *, loff_t *);
977 int watermark_scale_factor_sysctl_handler(struct ctl_table *, int,
978 void __user *, size_t *, loff_t *);
979 extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES];
980 int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int,
981 void __user *, size_t *, loff_t *);
982 int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int,
983 void __user *, size_t *, loff_t *);
984 int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
985 void __user *, size_t *, loff_t *);
986 int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
987 void __user *, size_t *, loff_t *);
988
989 extern int numa_zonelist_order_handler(struct ctl_table *, int,
990 void __user *, size_t *, loff_t *);
991 extern char numa_zonelist_order[];
992 #define NUMA_ZONELIST_ORDER_LEN 16
993
994 #ifndef CONFIG_NEED_MULTIPLE_NODES
995
996 extern struct pglist_data contig_page_data;
997 #define NODE_DATA(nid) (&contig_page_data)
998 #define NODE_MEM_MAP(nid) mem_map
999
1000 #else /* CONFIG_NEED_MULTIPLE_NODES */
1001
1002 #include <asm/mmzone.h>
1003
1004 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
1005
1006 extern struct pglist_data *first_online_pgdat(void);
1007 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
1008 extern struct zone *next_zone(struct zone *zone);
1009
1010 /**
1011 * for_each_online_pgdat - helper macro to iterate over all online nodes
1012 * @pgdat - pointer to a pg_data_t variable
1013 */
1014 #define for_each_online_pgdat(pgdat) \
1015 for (pgdat = first_online_pgdat(); \
1016 pgdat; \
1017 pgdat = next_online_pgdat(pgdat))
1018 /**
1019 * for_each_zone - helper macro to iterate over all memory zones
1020 * @zone - pointer to struct zone variable
1021 *
1022 * The user only needs to declare the zone variable, for_each_zone
1023 * fills it in.
1024 */
1025 #define for_each_zone(zone) \
1026 for (zone = (first_online_pgdat())->node_zones; \
1027 zone; \
1028 zone = next_zone(zone))
1029
1030 #define for_each_populated_zone(zone) \
1031 for (zone = (first_online_pgdat())->node_zones; \
1032 zone; \
1033 zone = next_zone(zone)) \
1034 if (!populated_zone(zone)) \
1035 ; /* do nothing */ \
1036 else
1037
zonelist_zone(struct zoneref * zoneref)1038 static inline struct zone *zonelist_zone(struct zoneref *zoneref)
1039 {
1040 return zoneref->zone;
1041 }
1042
zonelist_zone_idx(struct zoneref * zoneref)1043 static inline int zonelist_zone_idx(struct zoneref *zoneref)
1044 {
1045 return zoneref->zone_idx;
1046 }
1047
zonelist_node_idx(struct zoneref * zoneref)1048 static inline int zonelist_node_idx(struct zoneref *zoneref)
1049 {
1050 return zone_to_nid(zoneref->zone);
1051 }
1052
1053 struct zoneref *__next_zones_zonelist(struct zoneref *z,
1054 enum zone_type highest_zoneidx,
1055 nodemask_t *nodes);
1056
1057 /**
1058 * 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
1059 * @z - The cursor used as a starting point for the search
1060 * @highest_zoneidx - The zone index of the highest zone to return
1061 * @nodes - An optional nodemask to filter the zonelist with
1062 *
1063 * This function returns the next zone at or below a given zone index that is
1064 * within the allowed nodemask using a cursor as the starting point for the
1065 * search. The zoneref returned is a cursor that represents the current zone
1066 * being examined. It should be advanced by one before calling
1067 * next_zones_zonelist again.
1068 */
next_zones_zonelist(struct zoneref * z,enum zone_type highest_zoneidx,nodemask_t * nodes)1069 static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z,
1070 enum zone_type highest_zoneidx,
1071 nodemask_t *nodes)
1072 {
1073 if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx))
1074 return z;
1075 return __next_zones_zonelist(z, highest_zoneidx, nodes);
1076 }
1077
1078 /**
1079 * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
1080 * @zonelist - The zonelist to search for a suitable zone
1081 * @highest_zoneidx - The zone index of the highest zone to return
1082 * @nodes - An optional nodemask to filter the zonelist with
1083 * @return - Zoneref pointer for the first suitable zone found (see below)
1084 *
1085 * This function returns the first zone at or below a given zone index that is
1086 * within the allowed nodemask. The zoneref returned is a cursor that can be
1087 * used to iterate the zonelist with next_zones_zonelist by advancing it by
1088 * one before calling.
1089 *
1090 * When no eligible zone is found, zoneref->zone is NULL (zoneref itself is
1091 * never NULL). This may happen either genuinely, or due to concurrent nodemask
1092 * update due to cpuset modification.
1093 */
first_zones_zonelist(struct zonelist * zonelist,enum zone_type highest_zoneidx,nodemask_t * nodes)1094 static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
1095 enum zone_type highest_zoneidx,
1096 nodemask_t *nodes)
1097 {
1098 return next_zones_zonelist(zonelist->_zonerefs,
1099 highest_zoneidx, nodes);
1100 }
1101
1102 /**
1103 * 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
1104 * @zone - The current zone in the iterator
1105 * @z - The current pointer within zonelist->zones being iterated
1106 * @zlist - The zonelist being iterated
1107 * @highidx - The zone index of the highest zone to return
1108 * @nodemask - Nodemask allowed by the allocator
1109 *
1110 * This iterator iterates though all zones at or below a given zone index and
1111 * within a given nodemask
1112 */
1113 #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
1114 for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \
1115 zone; \
1116 z = next_zones_zonelist(++z, highidx, nodemask), \
1117 zone = zonelist_zone(z))
1118
1119 #define for_next_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
1120 for (zone = z->zone; \
1121 zone; \
1122 z = next_zones_zonelist(++z, highidx, nodemask), \
1123 zone = zonelist_zone(z))
1124
1125
1126 /**
1127 * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
1128 * @zone - The current zone in the iterator
1129 * @z - The current pointer within zonelist->zones being iterated
1130 * @zlist - The zonelist being iterated
1131 * @highidx - The zone index of the highest zone to return
1132 *
1133 * This iterator iterates though all zones at or below a given zone index.
1134 */
1135 #define for_each_zone_zonelist(zone, z, zlist, highidx) \
1136 for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
1137
1138 #ifdef CONFIG_SPARSEMEM
1139 #include <asm/sparsemem.h>
1140 #endif
1141
1142 #if !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) && \
1143 !defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
early_pfn_to_nid(unsigned long pfn)1144 static inline unsigned long early_pfn_to_nid(unsigned long pfn)
1145 {
1146 BUILD_BUG_ON(IS_ENABLED(CONFIG_NUMA));
1147 return 0;
1148 }
1149 #endif
1150
1151 #ifdef CONFIG_FLATMEM
1152 #define pfn_to_nid(pfn) (0)
1153 #endif
1154
1155 #ifdef CONFIG_SPARSEMEM
1156
1157 /*
1158 * SECTION_SHIFT #bits space required to store a section #
1159 *
1160 * PA_SECTION_SHIFT physical address to/from section number
1161 * PFN_SECTION_SHIFT pfn to/from section number
1162 */
1163 #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
1164 #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
1165
1166 #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
1167
1168 #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
1169 #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
1170
1171 #define SECTION_BLOCKFLAGS_BITS \
1172 ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
1173
1174 #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
1175 #error Allocator MAX_ORDER exceeds SECTION_SIZE
1176 #endif
1177
pfn_to_section_nr(unsigned long pfn)1178 static inline unsigned long pfn_to_section_nr(unsigned long pfn)
1179 {
1180 return pfn >> PFN_SECTION_SHIFT;
1181 }
section_nr_to_pfn(unsigned long sec)1182 static inline unsigned long section_nr_to_pfn(unsigned long sec)
1183 {
1184 return sec << PFN_SECTION_SHIFT;
1185 }
1186
1187 #define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK)
1188 #define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK)
1189
1190 #define SUBSECTION_SHIFT 21
1191
1192 #define PFN_SUBSECTION_SHIFT (SUBSECTION_SHIFT - PAGE_SHIFT)
1193 #define PAGES_PER_SUBSECTION (1UL << PFN_SUBSECTION_SHIFT)
1194 #define PAGE_SUBSECTION_MASK (~(PAGES_PER_SUBSECTION-1))
1195
1196 #if SUBSECTION_SHIFT > SECTION_SIZE_BITS
1197 #error Subsection size exceeds section size
1198 #else
1199 #define SUBSECTIONS_PER_SECTION (1UL << (SECTION_SIZE_BITS - SUBSECTION_SHIFT))
1200 #endif
1201
1202 #define SUBSECTION_ALIGN_UP(pfn) ALIGN((pfn), PAGES_PER_SUBSECTION)
1203 #define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK)
1204
1205 struct mem_section_usage {
1206 DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION);
1207 /* See declaration of similar field in struct zone */
1208 unsigned long pageblock_flags[0];
1209 };
1210
1211 void subsection_map_init(unsigned long pfn, unsigned long nr_pages);
1212
1213 struct page;
1214 struct page_ext;
1215 struct mem_section {
1216 /*
1217 * This is, logically, a pointer to an array of struct
1218 * pages. However, it is stored with some other magic.
1219 * (see sparse.c::sparse_init_one_section())
1220 *
1221 * Additionally during early boot we encode node id of
1222 * the location of the section here to guide allocation.
1223 * (see sparse.c::memory_present())
1224 *
1225 * Making it a UL at least makes someone do a cast
1226 * before using it wrong.
1227 */
1228 unsigned long section_mem_map;
1229
1230 struct mem_section_usage *usage;
1231 #ifdef CONFIG_PAGE_EXTENSION
1232 /*
1233 * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use
1234 * section. (see page_ext.h about this.)
1235 */
1236 struct page_ext *page_ext;
1237 unsigned long pad;
1238 #endif
1239 /*
1240 * WARNING: mem_section must be a power-of-2 in size for the
1241 * calculation and use of SECTION_ROOT_MASK to make sense.
1242 */
1243 };
1244
1245 #ifdef CONFIG_SPARSEMEM_EXTREME
1246 #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
1247 #else
1248 #define SECTIONS_PER_ROOT 1
1249 #endif
1250
1251 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
1252 #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT)
1253 #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
1254
1255 #ifdef CONFIG_SPARSEMEM_EXTREME
1256 extern struct mem_section **mem_section;
1257 #else
1258 extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
1259 #endif
1260
section_to_usemap(struct mem_section * ms)1261 static inline unsigned long *section_to_usemap(struct mem_section *ms)
1262 {
1263 return ms->usage->pageblock_flags;
1264 }
1265
__nr_to_section(unsigned long nr)1266 static inline struct mem_section *__nr_to_section(unsigned long nr)
1267 {
1268 unsigned long root = SECTION_NR_TO_ROOT(nr);
1269
1270 if (unlikely(root >= NR_SECTION_ROOTS))
1271 return NULL;
1272
1273 #ifdef CONFIG_SPARSEMEM_EXTREME
1274 if (!mem_section || !mem_section[root])
1275 return NULL;
1276 #endif
1277 return &mem_section[root][nr & SECTION_ROOT_MASK];
1278 }
1279 extern unsigned long __section_nr(struct mem_section *ms);
1280 extern size_t mem_section_usage_size(void);
1281
1282 /*
1283 * We use the lower bits of the mem_map pointer to store
1284 * a little bit of information. The pointer is calculated
1285 * as mem_map - section_nr_to_pfn(pnum). The result is
1286 * aligned to the minimum alignment of the two values:
1287 * 1. All mem_map arrays are page-aligned.
1288 * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT
1289 * lowest bits. PFN_SECTION_SHIFT is arch-specific
1290 * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the
1291 * worst combination is powerpc with 256k pages,
1292 * which results in PFN_SECTION_SHIFT equal 6.
1293 * To sum it up, at least 6 bits are available.
1294 */
1295 #define SECTION_MARKED_PRESENT (1UL<<0)
1296 #define SECTION_HAS_MEM_MAP (1UL<<1)
1297 #define SECTION_IS_ONLINE (1UL<<2)
1298 #define SECTION_IS_EARLY (1UL<<3)
1299 #define SECTION_MAP_LAST_BIT (1UL<<4)
1300 #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
1301 #define SECTION_NID_SHIFT 3
1302
__section_mem_map_addr(struct mem_section * section)1303 static inline struct page *__section_mem_map_addr(struct mem_section *section)
1304 {
1305 unsigned long map = section->section_mem_map;
1306 map &= SECTION_MAP_MASK;
1307 return (struct page *)map;
1308 }
1309
present_section(struct mem_section * section)1310 static inline int present_section(struct mem_section *section)
1311 {
1312 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
1313 }
1314
present_section_nr(unsigned long nr)1315 static inline int present_section_nr(unsigned long nr)
1316 {
1317 return present_section(__nr_to_section(nr));
1318 }
1319
valid_section(struct mem_section * section)1320 static inline int valid_section(struct mem_section *section)
1321 {
1322 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
1323 }
1324
early_section(struct mem_section * section)1325 static inline int early_section(struct mem_section *section)
1326 {
1327 return (section && (section->section_mem_map & SECTION_IS_EARLY));
1328 }
1329
valid_section_nr(unsigned long nr)1330 static inline int valid_section_nr(unsigned long nr)
1331 {
1332 return valid_section(__nr_to_section(nr));
1333 }
1334
online_section(struct mem_section * section)1335 static inline int online_section(struct mem_section *section)
1336 {
1337 return (section && (section->section_mem_map & SECTION_IS_ONLINE));
1338 }
1339
online_section_nr(unsigned long nr)1340 static inline int online_section_nr(unsigned long nr)
1341 {
1342 return online_section(__nr_to_section(nr));
1343 }
1344
1345 #ifdef CONFIG_MEMORY_HOTPLUG
1346 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1347 #ifdef CONFIG_MEMORY_HOTREMOVE
1348 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1349 #endif
1350 #endif
1351
__pfn_to_section(unsigned long pfn)1352 static inline struct mem_section *__pfn_to_section(unsigned long pfn)
1353 {
1354 return __nr_to_section(pfn_to_section_nr(pfn));
1355 }
1356
1357 extern unsigned long __highest_present_section_nr;
1358
subsection_map_index(unsigned long pfn)1359 static inline int subsection_map_index(unsigned long pfn)
1360 {
1361 return (pfn & ~(PAGE_SECTION_MASK)) / PAGES_PER_SUBSECTION;
1362 }
1363
1364 #ifdef CONFIG_SPARSEMEM_VMEMMAP
pfn_section_valid(struct mem_section * ms,unsigned long pfn)1365 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1366 {
1367 int idx = subsection_map_index(pfn);
1368
1369 return test_bit(idx, ms->usage->subsection_map);
1370 }
1371 #else
pfn_section_valid(struct mem_section * ms,unsigned long pfn)1372 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1373 {
1374 return 1;
1375 }
1376 #endif
1377
1378 #ifndef CONFIG_HAVE_ARCH_PFN_VALID
pfn_valid(unsigned long pfn)1379 static inline int pfn_valid(unsigned long pfn)
1380 {
1381 struct mem_section *ms;
1382
1383 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1384 return 0;
1385 ms = __nr_to_section(pfn_to_section_nr(pfn));
1386 if (!valid_section(ms))
1387 return 0;
1388 /*
1389 * Traditionally early sections always returned pfn_valid() for
1390 * the entire section-sized span.
1391 */
1392 return early_section(ms) || pfn_section_valid(ms, pfn);
1393 }
1394 #endif
1395
pfn_present(unsigned long pfn)1396 static inline int pfn_present(unsigned long pfn)
1397 {
1398 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1399 return 0;
1400 return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
1401 }
1402
1403 /*
1404 * These are _only_ used during initialisation, therefore they
1405 * can use __initdata ... They could have names to indicate
1406 * this restriction.
1407 */
1408 #ifdef CONFIG_NUMA
1409 #define pfn_to_nid(pfn) \
1410 ({ \
1411 unsigned long __pfn_to_nid_pfn = (pfn); \
1412 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
1413 })
1414 #else
1415 #define pfn_to_nid(pfn) (0)
1416 #endif
1417
1418 #define early_pfn_valid(pfn) pfn_valid(pfn)
1419 void sparse_init(void);
1420 #else
1421 #define sparse_init() do {} while (0)
1422 #define sparse_index_init(_sec, _nid) do {} while (0)
1423 #define pfn_present pfn_valid
1424 #define subsection_map_init(_pfn, _nr_pages) do {} while (0)
1425 #endif /* CONFIG_SPARSEMEM */
1426
1427 /*
1428 * During memory init memblocks map pfns to nids. The search is expensive and
1429 * this caches recent lookups. The implementation of __early_pfn_to_nid
1430 * may treat start/end as pfns or sections.
1431 */
1432 struct mminit_pfnnid_cache {
1433 unsigned long last_start;
1434 unsigned long last_end;
1435 int last_nid;
1436 };
1437
1438 #ifndef early_pfn_valid
1439 #define early_pfn_valid(pfn) (1)
1440 #endif
1441
1442 void memory_present(int nid, unsigned long start, unsigned long end);
1443
1444 /*
1445 * If it is possible to have holes within a MAX_ORDER_NR_PAGES, then we
1446 * need to check pfn validity within that MAX_ORDER_NR_PAGES block.
1447 * pfn_valid_within() should be used in this case; we optimise this away
1448 * when we have no holes within a MAX_ORDER_NR_PAGES block.
1449 */
1450 #ifdef CONFIG_HOLES_IN_ZONE
1451 #define pfn_valid_within(pfn) pfn_valid(pfn)
1452 #else
1453 #define pfn_valid_within(pfn) (1)
1454 #endif
1455
1456 #endif /* !__GENERATING_BOUNDS.H */
1457 #endif /* !__ASSEMBLY__ */
1458 #endif /* _LINUX_MMZONE_H */
1459