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