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