1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MMZONE_H
3 #define _LINUX_MMZONE_H
4
5 #ifndef __ASSEMBLY__
6 #ifndef __GENERATING_BOUNDS_H
7
8 #include <linux/spinlock.h>
9 #include <linux/list.h>
10 #include <linux/list_nulls.h>
11 #include <linux/wait.h>
12 #include <linux/bitops.h>
13 #include <linux/cache.h>
14 #include <linux/threads.h>
15 #include <linux/numa.h>
16 #include <linux/init.h>
17 #include <linux/seqlock.h>
18 #include <linux/nodemask.h>
19 #include <linux/pageblock-flags.h>
20 #include <linux/page-flags-layout.h>
21 #include <linux/atomic.h>
22 #include <linux/mm_types.h>
23 #include <linux/page-flags.h>
24 #include <linux/local_lock.h>
25 #include <linux/android_kabi.h>
26 #include <asm/page.h>
27
28 /* Free memory management - zoned buddy allocator. */
29 #ifndef CONFIG_FORCE_MAX_ZONEORDER
30 #define MAX_ORDER 11
31 #else
32 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
33 #endif
34 #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
35
36 /*
37 * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
38 * costly to service. That is between allocation orders which should
39 * coalesce naturally under reasonable reclaim pressure and those which
40 * will not.
41 */
42 #define PAGE_ALLOC_COSTLY_ORDER 3
43
44 #define MAX_KSWAPD_THREADS 16
45
46 enum migratetype {
47 MIGRATE_UNMOVABLE,
48 MIGRATE_MOVABLE,
49 MIGRATE_RECLAIMABLE,
50 #ifdef CONFIG_CMA
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 than
62 * a single pageblock.
63 */
64 MIGRATE_CMA,
65 #endif
66 MIGRATE_PCPTYPES, /* the number of types on the pcp lists */
67 MIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES,
68 #ifdef CONFIG_MEMORY_ISOLATION
69 MIGRATE_ISOLATE, /* can't allocate from here */
70 #endif
71 MIGRATE_TYPES
72 };
73
74 /* In mm/page_alloc.c; keep in sync also with show_migration_types() there */
75 extern const char * const migratetype_names[MIGRATE_TYPES];
76
77 #ifdef CONFIG_CMA
78 # define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)
79 # define is_migrate_cma_page(_page) ({ \
80 int mt = get_pageblock_migratetype(_page); \
81 bool ret = (mt == MIGRATE_ISOLATE || mt == MIGRATE_CMA) ? true : false; \
82 ret; \
83 })
84 # define get_cma_migrate_type() MIGRATE_CMA
85 #else
86 # define is_migrate_cma(migratetype) false
87 # define is_migrate_cma_page(_page) false
88 # define get_cma_migrate_type() MIGRATE_MOVABLE
89 #endif
90
is_migrate_movable(int mt)91 static inline bool is_migrate_movable(int mt)
92 {
93 return is_migrate_cma(mt) || mt == MIGRATE_MOVABLE;
94 }
95
96 #define for_each_migratetype_order(order, type) \
97 for (order = 0; order < MAX_ORDER; order++) \
98 for (type = 0; type < MIGRATE_TYPES; type++)
99
100 extern int page_group_by_mobility_disabled;
101
102 #define MIGRATETYPE_MASK ((1UL << PB_migratetype_bits) - 1)
103
104 #define get_pageblock_migratetype(page) \
105 get_pfnblock_flags_mask(page, page_to_pfn(page), MIGRATETYPE_MASK)
106
107 struct free_area {
108 struct list_head free_list[MIGRATE_TYPES];
109 unsigned long nr_free;
110 };
111
get_page_from_free_area(struct free_area * area,int migratetype)112 static inline struct page *get_page_from_free_area(struct free_area *area,
113 int migratetype)
114 {
115 return list_first_entry_or_null(&area->free_list[migratetype],
116 struct page, lru);
117 }
118
free_area_empty(struct free_area * area,int migratetype)119 static inline bool free_area_empty(struct free_area *area, int migratetype)
120 {
121 return list_empty(&area->free_list[migratetype]);
122 }
123
124 struct pglist_data;
125
126 /*
127 * Add a wild amount of padding here to ensure data fall into separate
128 * cachelines. There are very few zone structures in the machine, so space
129 * consumption is not a concern here.
130 */
131 #if defined(CONFIG_SMP)
132 struct zone_padding {
133 char x[0];
134 } ____cacheline_internodealigned_in_smp;
135 #define ZONE_PADDING(name) struct zone_padding name;
136 #else
137 #define ZONE_PADDING(name)
138 #endif
139
140 #ifdef CONFIG_NUMA
141 enum numa_stat_item {
142 NUMA_HIT, /* allocated in intended node */
143 NUMA_MISS, /* allocated in non intended node */
144 NUMA_FOREIGN, /* was intended here, hit elsewhere */
145 NUMA_INTERLEAVE_HIT, /* interleaver preferred this zone */
146 NUMA_LOCAL, /* allocation from local node */
147 NUMA_OTHER, /* allocation from other node */
148 NR_VM_NUMA_EVENT_ITEMS
149 };
150 #else
151 #define NR_VM_NUMA_EVENT_ITEMS 0
152 #endif
153
154 enum zone_stat_item {
155 /* First 128 byte cacheline (assuming 64 bit words) */
156 NR_FREE_PAGES,
157 NR_ZONE_LRU_BASE, /* Used only for compaction and reclaim retry */
158 NR_ZONE_INACTIVE_ANON = NR_ZONE_LRU_BASE,
159 NR_ZONE_ACTIVE_ANON,
160 NR_ZONE_INACTIVE_FILE,
161 NR_ZONE_ACTIVE_FILE,
162 NR_ZONE_UNEVICTABLE,
163 NR_ZONE_WRITE_PENDING, /* Count of dirty, writeback and unstable pages */
164 NR_MLOCK, /* mlock()ed pages found and moved off LRU */
165 /* Second 128 byte cacheline */
166 NR_BOUNCE,
167 NR_ZSPAGES, /* allocated in zsmalloc */
168 NR_FREE_CMA_PAGES,
169 NR_VM_ZONE_STAT_ITEMS };
170
171 enum node_stat_item {
172 NR_LRU_BASE,
173 NR_INACTIVE_ANON = NR_LRU_BASE, /* must match order of LRU_[IN]ACTIVE */
174 NR_ACTIVE_ANON, /* " " " " " */
175 NR_INACTIVE_FILE, /* " " " " " */
176 NR_ACTIVE_FILE, /* " " " " " */
177 NR_UNEVICTABLE, /* " " " " " */
178 NR_SLAB_RECLAIMABLE_B,
179 NR_SLAB_UNRECLAIMABLE_B,
180 NR_ISOLATED_ANON, /* Temporary isolated pages from anon lru */
181 NR_ISOLATED_FILE, /* Temporary isolated pages from file lru */
182 WORKINGSET_NODES,
183 WORKINGSET_REFAULT_BASE,
184 WORKINGSET_REFAULT_ANON = WORKINGSET_REFAULT_BASE,
185 WORKINGSET_REFAULT_FILE,
186 WORKINGSET_ACTIVATE_BASE,
187 WORKINGSET_ACTIVATE_ANON = WORKINGSET_ACTIVATE_BASE,
188 WORKINGSET_ACTIVATE_FILE,
189 WORKINGSET_RESTORE_BASE,
190 WORKINGSET_RESTORE_ANON = WORKINGSET_RESTORE_BASE,
191 WORKINGSET_RESTORE_FILE,
192 WORKINGSET_NODERECLAIM,
193 NR_ANON_MAPPED, /* Mapped anonymous pages */
194 NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
195 only modified from process context */
196 NR_FILE_PAGES,
197 NR_FILE_DIRTY,
198 NR_WRITEBACK,
199 NR_WRITEBACK_TEMP, /* Writeback using temporary buffers */
200 NR_SHMEM, /* shmem pages (included tmpfs/GEM pages) */
201 NR_SHMEM_THPS,
202 NR_SHMEM_PMDMAPPED,
203 NR_FILE_THPS,
204 NR_FILE_PMDMAPPED,
205 NR_ANON_THPS,
206 NR_VMSCAN_WRITE,
207 NR_VMSCAN_IMMEDIATE, /* Prioritise for reclaim when writeback ends */
208 NR_DIRTIED, /* page dirtyings since bootup */
209 NR_WRITTEN, /* page writings since bootup */
210 NR_KERNEL_MISC_RECLAIMABLE, /* reclaimable non-slab kernel pages */
211 NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */
212 NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */
213 NR_KERNEL_STACK_KB, /* measured in KiB */
214 #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
215 NR_KERNEL_SCS_KB, /* measured in KiB */
216 #endif
217 NR_PAGETABLE, /* used for pagetables */
218 NR_SECONDARY_PAGETABLE, /* secondary pagetables, e.g. KVM pagetables */
219 #ifdef CONFIG_SWAP
220 NR_SWAPCACHE,
221 #endif
222 NR_VM_NODE_STAT_ITEMS
223 };
224
225 /*
226 * Returns true if the item should be printed in THPs (/proc/vmstat
227 * currently prints number of anon, file and shmem THPs. But the item
228 * is charged in pages).
229 */
vmstat_item_print_in_thp(enum node_stat_item item)230 static __always_inline bool vmstat_item_print_in_thp(enum node_stat_item item)
231 {
232 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
233 return false;
234
235 return item == NR_ANON_THPS ||
236 item == NR_FILE_THPS ||
237 item == NR_SHMEM_THPS ||
238 item == NR_SHMEM_PMDMAPPED ||
239 item == NR_FILE_PMDMAPPED;
240 }
241
242 /*
243 * Returns true if the value is measured in bytes (most vmstat values are
244 * measured in pages). This defines the API part, the internal representation
245 * might be different.
246 */
vmstat_item_in_bytes(int idx)247 static __always_inline bool vmstat_item_in_bytes(int idx)
248 {
249 /*
250 * Global and per-node slab counters track slab pages.
251 * It's expected that changes are multiples of PAGE_SIZE.
252 * Internally values are stored in pages.
253 *
254 * Per-memcg and per-lruvec counters track memory, consumed
255 * by individual slab objects. These counters are actually
256 * byte-precise.
257 */
258 return (idx == NR_SLAB_RECLAIMABLE_B ||
259 idx == NR_SLAB_UNRECLAIMABLE_B);
260 }
261
262 /*
263 * We do arithmetic on the LRU lists in various places in the code,
264 * so it is important to keep the active lists LRU_ACTIVE higher in
265 * the array than the corresponding inactive lists, and to keep
266 * the *_FILE lists LRU_FILE higher than the corresponding _ANON lists.
267 *
268 * This has to be kept in sync with the statistics in zone_stat_item
269 * above and the descriptions in vmstat_text in mm/vmstat.c
270 */
271 #define LRU_BASE 0
272 #define LRU_ACTIVE 1
273 #define LRU_FILE 2
274
275 enum lru_list {
276 LRU_INACTIVE_ANON = LRU_BASE,
277 LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE,
278 LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
279 LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
280 LRU_UNEVICTABLE,
281 NR_LRU_LISTS
282 };
283
284 #define for_each_lru(lru) for (lru = 0; lru < NR_LRU_LISTS; lru++)
285
286 #define for_each_evictable_lru(lru) for (lru = 0; lru <= LRU_ACTIVE_FILE; lru++)
287
is_file_lru(enum lru_list lru)288 static inline bool is_file_lru(enum lru_list lru)
289 {
290 return (lru == LRU_INACTIVE_FILE || lru == LRU_ACTIVE_FILE);
291 }
292
is_active_lru(enum lru_list lru)293 static inline bool is_active_lru(enum lru_list lru)
294 {
295 return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE);
296 }
297
298 #define ANON_AND_FILE 2
299
300 enum lruvec_flags {
301 LRUVEC_CONGESTED, /* lruvec has many dirty pages
302 * backed by a congested BDI
303 */
304 };
305
306 #endif /* !__GENERATING_BOUNDS_H */
307
308 /*
309 * Evictable pages are divided into multiple generations. The youngest and the
310 * oldest generation numbers, max_seq and min_seq, are monotonically increasing.
311 * They form a sliding window of a variable size [MIN_NR_GENS, MAX_NR_GENS]. An
312 * offset within MAX_NR_GENS, i.e., gen, indexes the LRU list of the
313 * corresponding generation. The gen counter in page->flags stores gen+1 while
314 * a page is on one of lrugen->pages[]. Otherwise it stores 0.
315 *
316 * A page is added to the youngest generation on faulting. The aging needs to
317 * check the accessed bit at least twice before handing this page over to the
318 * eviction. The first check takes care of the accessed bit set on the initial
319 * fault; the second check makes sure this page hasn't been used since then.
320 * This process, AKA second chance, requires a minimum of two generations,
321 * hence MIN_NR_GENS. And to maintain ABI compatibility with the active/inactive
322 * LRU, e.g., /proc/vmstat, these two generations are considered active; the
323 * rest of generations, if they exist, are considered inactive. See
324 * lru_gen_is_active().
325 *
326 * PG_active is always cleared while a page is on one of lrugen->pages[] so
327 * that the aging needs not to worry about it. And it's set again when a page
328 * considered active is isolated for non-reclaiming purposes, e.g., migration.
329 * See lru_gen_add_page() and lru_gen_del_page().
330 *
331 * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the
332 * number of categories of the active/inactive LRU when keeping track of
333 * accesses through page tables. This requires order_base_2(MAX_NR_GENS+1) bits
334 * in page->flags.
335 */
336 #define MIN_NR_GENS 2U
337 #define MAX_NR_GENS 4U
338
339 /*
340 * Each generation is divided into multiple tiers. A page accessed N times
341 * through file descriptors is in tier order_base_2(N). A page in the first tier
342 * (N=0,1) is marked by PG_referenced unless it was faulted in through page
343 * tables or read ahead. A page in any other tier (N>1) is marked by
344 * PG_referenced and PG_workingset. This implies a minimum of two tiers is
345 * supported without using additional bits in page->flags.
346 *
347 * In contrast to moving across generations which requires the LRU lock, moving
348 * across tiers only involves atomic operations on page->flags and therefore
349 * has a negligible cost in the buffered access path. In the eviction path,
350 * comparisons of refaulted/(evicted+protected) from the first tier and the
351 * rest infer whether pages accessed multiple times through file descriptors
352 * are statistically hot and thus worth protecting.
353 *
354 * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the
355 * number of categories of the active/inactive LRU when keeping track of
356 * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in
357 * page->flags.
358 */
359 #define MAX_NR_TIERS 4U
360
361 #ifndef __GENERATING_BOUNDS_H
362
363 struct lruvec;
364 struct page_vma_mapped_walk;
365
366 #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF)
367 #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF)
368
369 /* see the comment on MEMCG_NR_GENS */
370 enum {
371 MEMCG_LRU_NOP,
372 MEMCG_LRU_HEAD,
373 MEMCG_LRU_TAIL,
374 MEMCG_LRU_OLD,
375 MEMCG_LRU_YOUNG,
376 };
377
378 #ifdef CONFIG_LRU_GEN
379
380 enum {
381 LRU_GEN_ANON,
382 LRU_GEN_FILE,
383 };
384
385 enum {
386 LRU_GEN_CORE,
387 LRU_GEN_MM_WALK,
388 LRU_GEN_NONLEAF_YOUNG,
389 NR_LRU_GEN_CAPS
390 };
391
392 #define MIN_LRU_BATCH BITS_PER_LONG
393 #define MAX_LRU_BATCH (MIN_LRU_BATCH * 64)
394
395 /* whether to keep historical stats from evicted generations */
396 #ifdef CONFIG_LRU_GEN_STATS
397 #define NR_HIST_GENS MAX_NR_GENS
398 #else
399 #define NR_HIST_GENS 1U
400 #endif
401
402 /*
403 * The youngest generation number is stored in max_seq for both anon and file
404 * types as they are aged on an equal footing. The oldest generation numbers are
405 * stored in min_seq[] separately for anon and file types as clean file pages
406 * can be evicted regardless of swap constraints.
407 *
408 * Normally anon and file min_seq are in sync. But if swapping is constrained,
409 * e.g., out of swap space, file min_seq is allowed to advance and leave anon
410 * min_seq behind.
411 *
412 * The number of pages in each generation is eventually consistent and therefore
413 * can be transiently negative when reset_batch_size() is pending.
414 */
415 struct lru_gen_page {
416 /* the aging increments the youngest generation number */
417 unsigned long max_seq;
418 /* the eviction increments the oldest generation numbers */
419 unsigned long min_seq[ANON_AND_FILE];
420 /* the birth time of each generation in jiffies */
421 unsigned long timestamps[MAX_NR_GENS];
422 /* the multi-gen LRU lists, lazily sorted on eviction */
423 struct list_head pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
424 /* the multi-gen LRU sizes, eventually consistent */
425 long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
426 /* the exponential moving average of refaulted */
427 unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS];
428 /* the exponential moving average of evicted+protected */
429 unsigned long avg_total[ANON_AND_FILE][MAX_NR_TIERS];
430 /* the first tier doesn't need protection, hence the minus one */
431 unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS - 1];
432 /* can be modified without holding the LRU lock */
433 atomic_long_t evicted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS];
434 atomic_long_t refaulted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS];
435 /* whether the multi-gen LRU is enabled */
436 bool enabled;
437 #ifdef CONFIG_MEMCG
438 /* the memcg generation this lru_gen_page belongs to */
439 u8 gen;
440 /* the list segment this lru_gen_page belongs to */
441 u8 seg;
442 /* per-node lru_gen_page list for global reclaim */
443 struct hlist_nulls_node list;
444 #endif
445
446 ANDROID_KABI_RESERVE(1);
447 ANDROID_KABI_RESERVE(2);
448 };
449
450 enum {
451 MM_LEAF_TOTAL, /* total leaf entries */
452 MM_LEAF_OLD, /* old leaf entries */
453 MM_LEAF_YOUNG, /* young leaf entries */
454 MM_NONLEAF_TOTAL, /* total non-leaf entries */
455 MM_NONLEAF_FOUND, /* non-leaf entries found in Bloom filters */
456 MM_NONLEAF_ADDED, /* non-leaf entries added to Bloom filters */
457 NR_MM_STATS
458 };
459
460 /* double-buffering Bloom filters */
461 #define NR_BLOOM_FILTERS 2
462
463 struct lru_gen_mm_state {
464 /* set to max_seq after each iteration */
465 unsigned long seq;
466 /* where the current iteration continues after */
467 struct list_head *head;
468 /* where the last iteration ended before */
469 struct list_head *tail;
470 /* Bloom filters flip after each iteration */
471 unsigned long *filters[NR_BLOOM_FILTERS];
472 /* the mm stats for debugging */
473 unsigned long stats[NR_HIST_GENS][NR_MM_STATS];
474
475 ANDROID_KABI_RESERVE(1);
476 };
477
478 struct lru_gen_mm_walk {
479 /* the lruvec under reclaim */
480 struct lruvec *lruvec;
481 /* unstable max_seq from lru_gen_page */
482 unsigned long max_seq;
483 /* the next address within an mm to scan */
484 unsigned long next_addr;
485 /* to batch promoted pages */
486 int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
487 /* to batch the mm stats */
488 int mm_stats[NR_MM_STATS];
489 /* total batched items */
490 int batched;
491 bool can_swap;
492 bool force_scan;
493
494 ANDROID_KABI_RESERVE(1);
495 ANDROID_KABI_RESERVE(2);
496 };
497
498 void lru_gen_init_lruvec(struct lruvec *lruvec);
499 void lru_gen_look_around(struct page_vma_mapped_walk *pvmw);
500
501 #ifdef CONFIG_MEMCG
502
503 /*
504 * For each node, memcgs are divided into two generations: the old and the
505 * young. For each generation, memcgs are randomly sharded into multiple bins
506 * to improve scalability. For each bin, the hlist_nulls is virtually divided
507 * into three segments: the head, the tail and the default.
508 *
509 * An onlining memcg is added to the tail of a random bin in the old generation.
510 * The eviction starts at the head of a random bin in the old generation. The
511 * per-node memcg generation counter, whose reminder (mod MEMCG_NR_GENS) indexes
512 * the old generation, is incremented when all its bins become empty.
513 *
514 * There are four operations:
515 * 1. MEMCG_LRU_HEAD, which moves an memcg to the head of a random bin in its
516 * current generation (old or young) and updates its "seg" to "head";
517 * 2. MEMCG_LRU_TAIL, which moves an memcg to the tail of a random bin in its
518 * current generation (old or young) and updates its "seg" to "tail";
519 * 3. MEMCG_LRU_OLD, which moves an memcg to the head of a random bin in the old
520 * generation, updates its "gen" to "old" and resets its "seg" to "default";
521 * 4. MEMCG_LRU_YOUNG, which moves an memcg to the tail of a random bin in the
522 * young generation, updates its "gen" to "young" and resets its "seg" to
523 * "default".
524 *
525 * The events that trigger the above operations are:
526 * 1. Exceeding the soft limit, which triggers MEMCG_LRU_HEAD;
527 * 2. The first attempt to reclaim an memcg below low, which triggers
528 * MEMCG_LRU_TAIL;
529 * 3. The first attempt to reclaim an memcg below reclaimable size threshold,
530 * which triggers MEMCG_LRU_TAIL;
531 * 4. The second attempt to reclaim an memcg below reclaimable size threshold,
532 * which triggers MEMCG_LRU_YOUNG;
533 * 5. Attempting to reclaim an memcg below min, which triggers MEMCG_LRU_YOUNG;
534 * 6. Finishing the aging on the eviction path, which triggers MEMCG_LRU_YOUNG;
535 * 7. Offlining an memcg, which triggers MEMCG_LRU_OLD.
536 *
537 * Note that memcg LRU only applies to global reclaim, and the round-robin
538 * incrementing of their max_seq counters ensures the eventual fairness to all
539 * eligible memcgs. For memcg reclaim, it still relies on mem_cgroup_iter().
540 */
541 #define MEMCG_NR_GENS 2
542 #define MEMCG_NR_BINS 8
543
544 struct lru_gen_memcg {
545 /* the per-node memcg generation counter */
546 unsigned long seq;
547 /* each memcg has one lru_gen_page per node */
548 unsigned long nr_memcgs[MEMCG_NR_GENS];
549 /* per-node lru_gen_page list for global reclaim */
550 struct hlist_nulls_head fifo[MEMCG_NR_GENS][MEMCG_NR_BINS];
551 /* protects the above */
552 spinlock_t lock;
553
554 ANDROID_KABI_RESERVE(1);
555 ANDROID_KABI_RESERVE(2);
556 };
557
558 void lru_gen_init_pgdat(struct pglist_data *pgdat);
559
560 void lru_gen_init_memcg(struct mem_cgroup *memcg);
561 void lru_gen_exit_memcg(struct mem_cgroup *memcg);
562 void lru_gen_online_memcg(struct mem_cgroup *memcg);
563 void lru_gen_offline_memcg(struct mem_cgroup *memcg);
564 void lru_gen_release_memcg(struct mem_cgroup *memcg);
565 void lru_gen_rotate_memcg(struct lruvec *lruvec, int op);
566
567 #else /* !CONFIG_MEMCG */
568
569 #define MEMCG_NR_GENS 1
570
571 struct lru_gen_memcg {
572 };
573
lru_gen_init_pgdat(struct pglist_data * pgdat)574 static inline void lru_gen_init_pgdat(struct pglist_data *pgdat)
575 {
576 }
577
578 #endif /* CONFIG_MEMCG */
579
580 #else /* !CONFIG_LRU_GEN */
581
lru_gen_init_pgdat(struct pglist_data * pgdat)582 static inline void lru_gen_init_pgdat(struct pglist_data *pgdat)
583 {
584 }
585
lru_gen_init_lruvec(struct lruvec * lruvec)586 static inline void lru_gen_init_lruvec(struct lruvec *lruvec)
587 {
588 }
589
lru_gen_look_around(struct page_vma_mapped_walk * pvmw)590 static inline void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
591 {
592 }
593
594 #ifdef CONFIG_MEMCG
595
lru_gen_init_memcg(struct mem_cgroup * memcg)596 static inline void lru_gen_init_memcg(struct mem_cgroup *memcg)
597 {
598 }
599
lru_gen_exit_memcg(struct mem_cgroup * memcg)600 static inline void lru_gen_exit_memcg(struct mem_cgroup *memcg)
601 {
602 }
603
lru_gen_online_memcg(struct mem_cgroup * memcg)604 static inline void lru_gen_online_memcg(struct mem_cgroup *memcg)
605 {
606 }
607
lru_gen_offline_memcg(struct mem_cgroup * memcg)608 static inline void lru_gen_offline_memcg(struct mem_cgroup *memcg)
609 {
610 }
611
lru_gen_release_memcg(struct mem_cgroup * memcg)612 static inline void lru_gen_release_memcg(struct mem_cgroup *memcg)
613 {
614 }
615
lru_gen_rotate_memcg(struct lruvec * lruvec,int op)616 static inline void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
617 {
618 }
619
620 #endif /* CONFIG_MEMCG */
621
622 #endif /* CONFIG_LRU_GEN */
623
624 struct lruvec {
625 struct list_head lists[NR_LRU_LISTS];
626 /* per lruvec lru_lock for memcg */
627 spinlock_t lru_lock;
628 /*
629 * These track the cost of reclaiming one LRU - file or anon -
630 * over the other. As the observed cost of reclaiming one LRU
631 * increases, the reclaim scan balance tips toward the other.
632 */
633 unsigned long anon_cost;
634 unsigned long file_cost;
635 /* Non-resident age, driven by LRU movement */
636 atomic_long_t nonresident_age;
637 /* Refaults at the time of last reclaim cycle */
638 unsigned long refaults[ANON_AND_FILE];
639 /* Various lruvec state flags (enum lruvec_flags) */
640 unsigned long flags;
641 #ifdef CONFIG_LRU_GEN
642 /* evictable pages divided into generations */
643 struct lru_gen_page lrugen;
644 /* to concurrently iterate lru_gen_mm_list */
645 struct lru_gen_mm_state mm_state;
646 #endif
647 #ifdef CONFIG_MEMCG
648 struct pglist_data *pgdat;
649 #endif
650 ANDROID_VENDOR_DATA(1);
651 ANDROID_KABI_RESERVE(1);
652 ANDROID_KABI_RESERVE(2);
653 };
654
655 /* Isolate unmapped pages */
656 #define ISOLATE_UNMAPPED ((__force isolate_mode_t)0x2)
657 /* Isolate for asynchronous migration */
658 #define ISOLATE_ASYNC_MIGRATE ((__force isolate_mode_t)0x4)
659 /* Isolate unevictable pages */
660 #define ISOLATE_UNEVICTABLE ((__force isolate_mode_t)0x8)
661
662 /* LRU Isolation modes. */
663 typedef unsigned __bitwise isolate_mode_t;
664
665 enum zone_watermarks {
666 WMARK_MIN,
667 WMARK_LOW,
668 WMARK_HIGH,
669 NR_WMARK
670 };
671
672 /*
673 * One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. One additional list
674 * for THP which will usually be GFP_MOVABLE. Even if it is another type,
675 * it should not contribute to serious fragmentation causing THP allocation
676 * failures.
677 */
678 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
679 #define NR_PCP_THP 1
680 #else
681 #define NR_PCP_THP 0
682 #endif
683 #define NR_LOWORDER_PCP_LISTS (MIGRATE_PCPTYPES * (PAGE_ALLOC_COSTLY_ORDER + 1))
684 #define NR_PCP_LISTS (NR_LOWORDER_PCP_LISTS + NR_PCP_THP)
685
686 /*
687 * Shift to encode migratetype and order in the same integer, with order
688 * in the least significant bits.
689 */
690 #define NR_PCP_ORDER_WIDTH 8
691 #define NR_PCP_ORDER_MASK ((1<<NR_PCP_ORDER_WIDTH) - 1)
692
693 #define min_wmark_pages(z) (z->_watermark[WMARK_MIN] + z->watermark_boost)
694 #define low_wmark_pages(z) (z->_watermark[WMARK_LOW] + z->watermark_boost)
695 #define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost)
696 #define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost)
697
698 /* Fields and list protected by pagesets local_lock in page_alloc.c */
699 struct per_cpu_pages {
700 spinlock_t lock; /* Protects lists field */
701 int count; /* number of pages in the list */
702 int high; /* high watermark, emptying needed */
703 int batch; /* chunk size for buddy add/remove */
704 short free_factor; /* batch scaling factor during free */
705 #ifdef CONFIG_NUMA
706 short expire; /* When 0, remote pagesets are drained */
707 #endif
708
709 /* Lists of pages, one per migrate type stored on the pcp-lists */
710 struct list_head lists[NR_PCP_LISTS];
711 } ____cacheline_aligned_in_smp;
712
713 struct per_cpu_zonestat {
714 #ifdef CONFIG_SMP
715 s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
716 s8 stat_threshold;
717 #endif
718 #ifdef CONFIG_NUMA
719 /*
720 * Low priority inaccurate counters that are only folded
721 * on demand. Use a large type to avoid the overhead of
722 * folding during refresh_cpu_vm_stats.
723 */
724 unsigned long vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
725 #endif
726 };
727
728 struct per_cpu_nodestat {
729 s8 stat_threshold;
730 s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
731 };
732
733 #endif /* !__GENERATING_BOUNDS.H */
734
735 enum zone_type {
736 /*
737 * ZONE_DMA and ZONE_DMA32 are used when there are peripherals not able
738 * to DMA to all of the addressable memory (ZONE_NORMAL).
739 * On architectures where this area covers the whole 32 bit address
740 * space ZONE_DMA32 is used. ZONE_DMA is left for the ones with smaller
741 * DMA addressing constraints. This distinction is important as a 32bit
742 * DMA mask is assumed when ZONE_DMA32 is defined. Some 64-bit
743 * platforms may need both zones as they support peripherals with
744 * different DMA addressing limitations.
745 */
746 #ifdef CONFIG_ZONE_DMA
747 ZONE_DMA,
748 #endif
749 #ifdef CONFIG_ZONE_DMA32
750 ZONE_DMA32,
751 #endif
752 /*
753 * Normal addressable memory is in ZONE_NORMAL. DMA operations can be
754 * performed on pages in ZONE_NORMAL if the DMA devices support
755 * transfers to all addressable memory.
756 */
757 ZONE_NORMAL,
758 #ifdef CONFIG_HIGHMEM
759 /*
760 * A memory area that is only addressable by the kernel through
761 * mapping portions into its own address space. This is for example
762 * used by i386 to allow the kernel to address the memory beyond
763 * 900MB. The kernel will set up special mappings (page
764 * table entries on i386) for each page that the kernel needs to
765 * access.
766 */
767 ZONE_HIGHMEM,
768 #endif
769 /*
770 * ZONE_MOVABLE is similar to ZONE_NORMAL, except that it contains
771 * movable pages with few exceptional cases described below. Main use
772 * cases for ZONE_MOVABLE are to make memory offlining/unplug more
773 * likely to succeed, and to locally limit unmovable allocations - e.g.,
774 * to increase the number of THP/huge pages. Notable special cases are:
775 *
776 * 1. Pinned pages: (long-term) pinning of movable pages might
777 * essentially turn such pages unmovable. Therefore, we do not allow
778 * pinning long-term pages in ZONE_MOVABLE. When pages are pinned and
779 * faulted, they come from the right zone right away. However, it is
780 * still possible that address space already has pages in
781 * ZONE_MOVABLE at the time when pages are pinned (i.e. user has
782 * touches that memory before pinning). In such case we migrate them
783 * to a different zone. When migration fails - pinning fails.
784 * 2. memblock allocations: kernelcore/movablecore setups might create
785 * situations where ZONE_MOVABLE contains unmovable allocations
786 * after boot. Memory offlining and allocations fail early.
787 * 3. Memory holes: kernelcore/movablecore setups might create very rare
788 * situations where ZONE_MOVABLE contains memory holes after boot,
789 * for example, if we have sections that are only partially
790 * populated. Memory offlining and allocations fail early.
791 * 4. PG_hwpoison pages: while poisoned pages can be skipped during
792 * memory offlining, such pages cannot be allocated.
793 * 5. Unmovable PG_offline pages: in paravirtualized environments,
794 * hotplugged memory blocks might only partially be managed by the
795 * buddy (e.g., via XEN-balloon, Hyper-V balloon, virtio-mem). The
796 * parts not manged by the buddy are unmovable PG_offline pages. In
797 * some cases (virtio-mem), such pages can be skipped during
798 * memory offlining, however, cannot be moved/allocated. These
799 * techniques might use alloc_contig_range() to hide previously
800 * exposed pages from the buddy again (e.g., to implement some sort
801 * of memory unplug in virtio-mem).
802 * 6. ZERO_PAGE(0), kernelcore/movablecore setups might create
803 * situations where ZERO_PAGE(0) which is allocated differently
804 * on different platforms may end up in a movable zone. ZERO_PAGE(0)
805 * cannot be migrated.
806 * 7. Memory-hotplug: when using memmap_on_memory and onlining the
807 * memory to the MOVABLE zone, the vmemmap pages are also placed in
808 * such zone. Such pages cannot be really moved around as they are
809 * self-stored in the range, but they are treated as movable when
810 * the range they describe is about to be offlined.
811 *
812 * In general, no unmovable allocations that degrade memory offlining
813 * should end up in ZONE_MOVABLE. Allocators (like alloc_contig_range())
814 * have to expect that migrating pages in ZONE_MOVABLE can fail (even
815 * if has_unmovable_pages() states that there are no unmovable pages,
816 * there can be false negatives).
817 */
818 ZONE_MOVABLE,
819 #ifdef CONFIG_ZONE_DEVICE
820 ZONE_DEVICE,
821 #endif
822 __MAX_NR_ZONES
823
824 };
825
826 #ifndef __GENERATING_BOUNDS_H
827
828 #define ASYNC_AND_SYNC 2
829
830 struct zone {
831 /* Read-mostly fields */
832
833 /* zone watermarks, access with *_wmark_pages(zone) macros */
834 unsigned long _watermark[NR_WMARK];
835 unsigned long watermark_boost;
836
837 unsigned long nr_reserved_highatomic;
838
839 /*
840 * We don't know if the memory that we're going to allocate will be
841 * freeable or/and it will be released eventually, so to avoid totally
842 * wasting several GB of ram we must reserve some of the lower zone
843 * memory (otherwise we risk to run OOM on the lower zones despite
844 * there being tons of freeable ram on the higher zones). This array is
845 * recalculated at runtime if the sysctl_lowmem_reserve_ratio sysctl
846 * changes.
847 */
848 long lowmem_reserve[MAX_NR_ZONES];
849
850 #ifdef CONFIG_NUMA
851 int node;
852 #endif
853 struct pglist_data *zone_pgdat;
854 struct per_cpu_pages __percpu *per_cpu_pageset;
855 struct per_cpu_zonestat __percpu *per_cpu_zonestats;
856 /*
857 * the high and batch values are copied to individual pagesets for
858 * faster access
859 */
860 int pageset_high;
861 int pageset_batch;
862
863 #ifndef CONFIG_SPARSEMEM
864 /*
865 * Flags for a pageblock_nr_pages block. See pageblock-flags.h.
866 * In SPARSEMEM, this map is stored in struct mem_section
867 */
868 unsigned long *pageblock_flags;
869 #endif /* CONFIG_SPARSEMEM */
870
871 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
872 unsigned long zone_start_pfn;
873
874 /*
875 * spanned_pages is the total pages spanned by the zone, including
876 * holes, which is calculated as:
877 * spanned_pages = zone_end_pfn - zone_start_pfn;
878 *
879 * present_pages is physical pages existing within the zone, which
880 * is calculated as:
881 * present_pages = spanned_pages - absent_pages(pages in holes);
882 *
883 * present_early_pages is present pages existing within the zone
884 * located on memory available since early boot, excluding hotplugged
885 * memory.
886 *
887 * managed_pages is present pages managed by the buddy system, which
888 * is calculated as (reserved_pages includes pages allocated by the
889 * bootmem allocator):
890 * managed_pages = present_pages - reserved_pages;
891 *
892 * cma pages is present pages that are assigned for CMA use
893 * (MIGRATE_CMA).
894 *
895 * So present_pages may be used by memory hotplug or memory power
896 * management logic to figure out unmanaged pages by checking
897 * (present_pages - managed_pages). And managed_pages should be used
898 * by page allocator and vm scanner to calculate all kinds of watermarks
899 * and thresholds.
900 *
901 * Locking rules:
902 *
903 * zone_start_pfn and spanned_pages are protected by span_seqlock.
904 * It is a seqlock because it has to be read outside of zone->lock,
905 * and it is done in the main allocator path. But, it is written
906 * quite infrequently.
907 *
908 * The span_seq lock is declared along with zone->lock because it is
909 * frequently read in proximity to zone->lock. It's good to
910 * give them a chance of being in the same cacheline.
911 *
912 * Write access to present_pages at runtime should be protected by
913 * mem_hotplug_begin/end(). Any reader who can't tolerant drift of
914 * present_pages should get_online_mems() to get a stable value.
915 */
916 atomic_long_t managed_pages;
917 unsigned long spanned_pages;
918 unsigned long present_pages;
919 #if defined(CONFIG_MEMORY_HOTPLUG)
920 unsigned long present_early_pages;
921 #endif
922 #ifdef CONFIG_CMA
923 unsigned long cma_pages;
924 #endif
925
926 const char *name;
927
928 #ifdef CONFIG_MEMORY_ISOLATION
929 /*
930 * Number of isolated pageblock. It is used to solve incorrect
931 * freepage counting problem due to racy retrieving migratetype
932 * of pageblock. Protected by zone->lock.
933 */
934 unsigned long nr_isolate_pageblock;
935 #endif
936
937 #ifdef CONFIG_MEMORY_HOTPLUG
938 /* see spanned/present_pages for more description */
939 seqlock_t span_seqlock;
940 #endif
941
942 int initialized;
943
944 /* Write-intensive fields used from the page allocator */
945 ZONE_PADDING(_pad1_)
946
947 /* free areas of different sizes */
948 struct free_area free_area[MAX_ORDER];
949
950 /* zone flags, see below */
951 unsigned long flags;
952
953 /* Primarily protects free_area */
954 spinlock_t lock;
955
956 /* Write-intensive fields used by compaction and vmstats. */
957 ZONE_PADDING(_pad2_)
958
959 /*
960 * When free pages are below this point, additional steps are taken
961 * when reading the number of free pages to avoid per-cpu counter
962 * drift allowing watermarks to be breached
963 */
964 unsigned long percpu_drift_mark;
965
966 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
967 /* pfn where compaction free scanner should start */
968 unsigned long compact_cached_free_pfn;
969 /* pfn where compaction migration scanner should start */
970 unsigned long compact_cached_migrate_pfn[ASYNC_AND_SYNC];
971 unsigned long compact_init_migrate_pfn;
972 unsigned long compact_init_free_pfn;
973 #endif
974
975 #ifdef CONFIG_COMPACTION
976 /*
977 * On compaction failure, 1<<compact_defer_shift compactions
978 * are skipped before trying again. The number attempted since
979 * last failure is tracked with compact_considered.
980 * compact_order_failed is the minimum compaction failed order.
981 */
982 unsigned int compact_considered;
983 unsigned int compact_defer_shift;
984 int compact_order_failed;
985 #endif
986
987 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
988 /* Set to true when the PG_migrate_skip bits should be cleared */
989 bool compact_blockskip_flush;
990 #endif
991
992 bool contiguous;
993
994 ZONE_PADDING(_pad3_)
995 /* Zone statistics */
996 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
997 atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
998
999 ANDROID_KABI_RESERVE(1);
1000 ANDROID_KABI_RESERVE(2);
1001 ANDROID_KABI_RESERVE(3);
1002 ANDROID_KABI_RESERVE(4);
1003 } ____cacheline_internodealigned_in_smp;
1004
1005 enum pgdat_flags {
1006 PGDAT_DIRTY, /* reclaim scanning has recently found
1007 * many dirty file pages at the tail
1008 * of the LRU.
1009 */
1010 PGDAT_WRITEBACK, /* reclaim scanning has recently found
1011 * many pages under writeback
1012 */
1013 PGDAT_RECLAIM_LOCKED, /* prevents concurrent reclaim */
1014 };
1015
1016 enum zone_flags {
1017 ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks.
1018 * Cleared when kswapd is woken.
1019 */
1020 ZONE_RECLAIM_ACTIVE, /* kswapd may be scanning the zone. */
1021 };
1022
zone_managed_pages(struct zone * zone)1023 static inline unsigned long zone_managed_pages(struct zone *zone)
1024 {
1025 return (unsigned long)atomic_long_read(&zone->managed_pages);
1026 }
1027
zone_cma_pages(struct zone * zone)1028 static inline unsigned long zone_cma_pages(struct zone *zone)
1029 {
1030 #ifdef CONFIG_CMA
1031 return zone->cma_pages;
1032 #else
1033 return 0;
1034 #endif
1035 }
1036
zone_end_pfn(const struct zone * zone)1037 static inline unsigned long zone_end_pfn(const struct zone *zone)
1038 {
1039 return zone->zone_start_pfn + zone->spanned_pages;
1040 }
1041
zone_spans_pfn(const struct zone * zone,unsigned long pfn)1042 static inline bool zone_spans_pfn(const struct zone *zone, unsigned long pfn)
1043 {
1044 return zone->zone_start_pfn <= pfn && pfn < zone_end_pfn(zone);
1045 }
1046
zone_is_initialized(struct zone * zone)1047 static inline bool zone_is_initialized(struct zone *zone)
1048 {
1049 return zone->initialized;
1050 }
1051
zone_is_empty(struct zone * zone)1052 static inline bool zone_is_empty(struct zone *zone)
1053 {
1054 return zone->spanned_pages == 0;
1055 }
1056
1057 /*
1058 * Return true if [start_pfn, start_pfn + nr_pages) range has a non-empty
1059 * intersection with the given zone
1060 */
zone_intersects(struct zone * zone,unsigned long start_pfn,unsigned long nr_pages)1061 static inline bool zone_intersects(struct zone *zone,
1062 unsigned long start_pfn, unsigned long nr_pages)
1063 {
1064 if (zone_is_empty(zone))
1065 return false;
1066 if (start_pfn >= zone_end_pfn(zone) ||
1067 start_pfn + nr_pages <= zone->zone_start_pfn)
1068 return false;
1069
1070 return true;
1071 }
1072
1073 /*
1074 * The "priority" of VM scanning is how much of the queues we will scan in one
1075 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
1076 * queues ("queue_length >> 12") during an aging round.
1077 */
1078 #define DEF_PRIORITY 12
1079
1080 /* Maximum number of zones on a zonelist */
1081 #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
1082
1083 enum {
1084 ZONELIST_FALLBACK, /* zonelist with fallback */
1085 #ifdef CONFIG_NUMA
1086 /*
1087 * The NUMA zonelists are doubled because we need zonelists that
1088 * restrict the allocations to a single node for __GFP_THISNODE.
1089 */
1090 ZONELIST_NOFALLBACK, /* zonelist without fallback (__GFP_THISNODE) */
1091 #endif
1092 MAX_ZONELISTS
1093 };
1094
1095 /*
1096 * This struct contains information about a zone in a zonelist. It is stored
1097 * here to avoid dereferences into large structures and lookups of tables
1098 */
1099 struct zoneref {
1100 struct zone *zone; /* Pointer to actual zone */
1101 int zone_idx; /* zone_idx(zoneref->zone) */
1102 };
1103
1104 /*
1105 * One allocation request operates on a zonelist. A zonelist
1106 * is a list of zones, the first one is the 'goal' of the
1107 * allocation, the other zones are fallback zones, in decreasing
1108 * priority.
1109 *
1110 * To speed the reading of the zonelist, the zonerefs contain the zone index
1111 * of the entry being read. Helper functions to access information given
1112 * a struct zoneref are
1113 *
1114 * zonelist_zone() - Return the struct zone * for an entry in _zonerefs
1115 * zonelist_zone_idx() - Return the index of the zone for an entry
1116 * zonelist_node_idx() - Return the index of the node for an entry
1117 */
1118 struct zonelist {
1119 struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
1120 };
1121
1122 /*
1123 * The array of struct pages for flatmem.
1124 * It must be declared for SPARSEMEM as well because there are configurations
1125 * that rely on that.
1126 */
1127 extern struct page *mem_map;
1128
1129 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1130 struct deferred_split {
1131 spinlock_t split_queue_lock;
1132 struct list_head split_queue;
1133 unsigned long split_queue_len;
1134 };
1135 #endif
1136
1137 /*
1138 * On NUMA machines, each NUMA node would have a pg_data_t to describe
1139 * it's memory layout. On UMA machines there is a single pglist_data which
1140 * describes the whole memory.
1141 *
1142 * Memory statistics and page replacement data structures are maintained on a
1143 * per-zone basis.
1144 */
1145 typedef struct pglist_data {
1146 /*
1147 * node_zones contains just the zones for THIS node. Not all of the
1148 * zones may be populated, but it is the full list. It is referenced by
1149 * this node's node_zonelists as well as other node's node_zonelists.
1150 */
1151 struct zone node_zones[MAX_NR_ZONES];
1152
1153 /*
1154 * node_zonelists contains references to all zones in all nodes.
1155 * Generally the first zones will be references to this node's
1156 * node_zones.
1157 */
1158 struct zonelist node_zonelists[MAX_ZONELISTS];
1159
1160 int nr_zones; /* number of populated zones in this node */
1161 #ifdef CONFIG_FLATMEM /* means !SPARSEMEM */
1162 struct page *node_mem_map;
1163 #ifdef CONFIG_PAGE_EXTENSION
1164 struct page_ext *node_page_ext;
1165 #endif
1166 #endif
1167 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
1168 /*
1169 * Must be held any time you expect node_start_pfn,
1170 * node_present_pages, node_spanned_pages or nr_zones to stay constant.
1171 * Also synchronizes pgdat->first_deferred_pfn during deferred page
1172 * init.
1173 *
1174 * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
1175 * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
1176 * or CONFIG_DEFERRED_STRUCT_PAGE_INIT.
1177 *
1178 * Nests above zone->lock and zone->span_seqlock
1179 */
1180 spinlock_t node_size_lock;
1181 #endif
1182 unsigned long node_start_pfn;
1183 unsigned long node_present_pages; /* total number of physical pages */
1184 unsigned long node_spanned_pages; /* total size of physical page
1185 range, including holes */
1186 int node_id;
1187 wait_queue_head_t kswapd_wait;
1188 wait_queue_head_t pfmemalloc_wait;
1189 struct task_struct *kswapd; /* Protected by
1190 mem_hotplug_begin/end() */
1191 struct task_struct *mkswapd[MAX_KSWAPD_THREADS];
1192 int kswapd_order;
1193 enum zone_type kswapd_highest_zoneidx;
1194
1195 int kswapd_failures; /* Number of 'reclaimed == 0' runs */
1196
1197 ANDROID_OEM_DATA(1);
1198 #ifdef CONFIG_COMPACTION
1199 int kcompactd_max_order;
1200 enum zone_type kcompactd_highest_zoneidx;
1201 wait_queue_head_t kcompactd_wait;
1202 struct task_struct *kcompactd;
1203 bool proactive_compact_trigger;
1204 #endif
1205 /*
1206 * This is a per-node reserve of pages that are not available
1207 * to userspace allocations.
1208 */
1209 unsigned long totalreserve_pages;
1210
1211 #ifdef CONFIG_NUMA
1212 /*
1213 * node reclaim becomes active if more unmapped pages exist.
1214 */
1215 unsigned long min_unmapped_pages;
1216 unsigned long min_slab_pages;
1217 #endif /* CONFIG_NUMA */
1218
1219 /* Write-intensive fields used by page reclaim */
1220 ZONE_PADDING(_pad1_)
1221
1222 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1223 /*
1224 * If memory initialisation on large machines is deferred then this
1225 * is the first PFN that needs to be initialised.
1226 */
1227 unsigned long first_deferred_pfn;
1228 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1229
1230 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1231 struct deferred_split deferred_split_queue;
1232 #endif
1233
1234 /* Fields commonly accessed by the page reclaim scanner */
1235
1236 /*
1237 * NOTE: THIS IS UNUSED IF MEMCG IS ENABLED.
1238 *
1239 * Use mem_cgroup_lruvec() to look up lruvecs.
1240 */
1241 struct lruvec __lruvec;
1242
1243 unsigned long flags;
1244
1245 #ifdef CONFIG_LRU_GEN
1246 /* kswap mm walk data */
1247 struct lru_gen_mm_walk mm_walk;
1248 /* lru_gen_page list */
1249 struct lru_gen_memcg memcg_lru;
1250 #endif
1251
1252 ZONE_PADDING(_pad2_)
1253
1254 /* Per-node vmstats */
1255 struct per_cpu_nodestat __percpu *per_cpu_nodestats;
1256 atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS];
1257 } pg_data_t;
1258
1259 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
1260 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
1261 #ifdef CONFIG_FLATMEM
1262 #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
1263 #else
1264 #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
1265 #endif
1266 #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
1267
1268 #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn)
1269 #define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid))
1270
pgdat_end_pfn(pg_data_t * pgdat)1271 static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
1272 {
1273 return pgdat->node_start_pfn + pgdat->node_spanned_pages;
1274 }
1275
pgdat_is_empty(pg_data_t * pgdat)1276 static inline bool pgdat_is_empty(pg_data_t *pgdat)
1277 {
1278 return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
1279 }
1280
1281 #include <linux/memory_hotplug.h>
1282
1283 void build_all_zonelists(pg_data_t *pgdat);
1284 void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
1285 enum zone_type highest_zoneidx);
1286 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
1287 int highest_zoneidx, unsigned int alloc_flags,
1288 long free_pages);
1289 bool zone_watermark_ok(struct zone *z, unsigned int order,
1290 unsigned long mark, int highest_zoneidx,
1291 unsigned int alloc_flags);
1292 bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
1293 unsigned long mark, int highest_zoneidx);
1294 /*
1295 * Memory initialization context, use to differentiate memory added by
1296 * the platform statically or via memory hotplug interface.
1297 */
1298 enum meminit_context {
1299 MEMINIT_EARLY,
1300 MEMINIT_HOTPLUG,
1301 };
1302
1303 extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
1304 unsigned long size);
1305
1306 extern void lruvec_init(struct lruvec *lruvec);
1307
lruvec_pgdat(struct lruvec * lruvec)1308 static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec)
1309 {
1310 #ifdef CONFIG_MEMCG
1311 return lruvec->pgdat;
1312 #else
1313 return container_of(lruvec, struct pglist_data, __lruvec);
1314 #endif
1315 }
1316
1317 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
1318 int local_memory_node(int node_id);
1319 #else
local_memory_node(int node_id)1320 static inline int local_memory_node(int node_id) { return node_id; };
1321 #endif
1322
1323 /*
1324 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
1325 */
1326 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
1327
1328 #ifdef CONFIG_ZONE_DEVICE
zone_is_zone_device(struct zone * zone)1329 static inline bool zone_is_zone_device(struct zone *zone)
1330 {
1331 return zone_idx(zone) == ZONE_DEVICE;
1332 }
1333 #else
zone_is_zone_device(struct zone * zone)1334 static inline bool zone_is_zone_device(struct zone *zone)
1335 {
1336 return false;
1337 }
1338 #endif
1339
1340 /*
1341 * Returns true if a zone has pages managed by the buddy allocator.
1342 * All the reclaim decisions have to use this function rather than
1343 * populated_zone(). If the whole zone is reserved then we can easily
1344 * end up with populated_zone() && !managed_zone().
1345 */
managed_zone(struct zone * zone)1346 static inline bool managed_zone(struct zone *zone)
1347 {
1348 return zone_managed_pages(zone);
1349 }
1350
1351 /* Returns true if a zone has memory */
populated_zone(struct zone * zone)1352 static inline bool populated_zone(struct zone *zone)
1353 {
1354 return zone->present_pages;
1355 }
1356
1357 #ifdef CONFIG_NUMA
zone_to_nid(struct zone * zone)1358 static inline int zone_to_nid(struct zone *zone)
1359 {
1360 return zone->node;
1361 }
1362
zone_set_nid(struct zone * zone,int nid)1363 static inline void zone_set_nid(struct zone *zone, int nid)
1364 {
1365 zone->node = nid;
1366 }
1367 #else
zone_to_nid(struct zone * zone)1368 static inline int zone_to_nid(struct zone *zone)
1369 {
1370 return 0;
1371 }
1372
zone_set_nid(struct zone * zone,int nid)1373 static inline void zone_set_nid(struct zone *zone, int nid) {}
1374 #endif
1375
1376 extern int movable_zone;
1377
is_highmem_idx(enum zone_type idx)1378 static inline int is_highmem_idx(enum zone_type idx)
1379 {
1380 #ifdef CONFIG_HIGHMEM
1381 return (idx == ZONE_HIGHMEM ||
1382 (idx == ZONE_MOVABLE && movable_zone == ZONE_HIGHMEM));
1383 #else
1384 return 0;
1385 #endif
1386 }
1387
1388 #ifdef CONFIG_ZONE_DMA
1389 bool has_managed_dma(void);
1390 #else
has_managed_dma(void)1391 static inline bool has_managed_dma(void)
1392 {
1393 return false;
1394 }
1395 #endif
1396
1397 /**
1398 * is_highmem - helper function to quickly check if a struct zone is a
1399 * highmem zone or not. This is an attempt to keep references
1400 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
1401 * @zone: pointer to struct zone variable
1402 * Return: 1 for a highmem zone, 0 otherwise
1403 */
is_highmem(struct zone * zone)1404 static inline int is_highmem(struct zone *zone)
1405 {
1406 #ifdef CONFIG_HIGHMEM
1407 return is_highmem_idx(zone_idx(zone));
1408 #else
1409 return 0;
1410 #endif
1411 }
1412
1413 /* These two functions are used to setup the per zone pages min values */
1414 struct ctl_table;
1415
1416 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, void *, size_t *,
1417 loff_t *);
1418 int watermark_scale_factor_sysctl_handler(struct ctl_table *, int, void *,
1419 size_t *, loff_t *);
1420 extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES];
1421 int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, void *,
1422 size_t *, loff_t *);
1423 int percpu_pagelist_high_fraction_sysctl_handler(struct ctl_table *, int,
1424 void *, size_t *, loff_t *);
1425 int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
1426 void *, size_t *, loff_t *);
1427 int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
1428 void *, size_t *, loff_t *);
1429 int numa_zonelist_order_handler(struct ctl_table *, int,
1430 void *, size_t *, loff_t *);
1431 extern int percpu_pagelist_high_fraction;
1432 extern char numa_zonelist_order[];
1433 #define NUMA_ZONELIST_ORDER_LEN 16
1434
1435 #ifndef CONFIG_NUMA
1436
1437 extern struct pglist_data contig_page_data;
NODE_DATA(int nid)1438 static inline struct pglist_data *NODE_DATA(int nid)
1439 {
1440 return &contig_page_data;
1441 }
1442 #define NODE_MEM_MAP(nid) mem_map
1443
1444 #else /* CONFIG_NUMA */
1445
1446 #include <asm/mmzone.h>
1447
1448 #endif /* !CONFIG_NUMA */
1449
1450 extern struct pglist_data *first_online_pgdat(void);
1451 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
1452 extern struct zone *next_zone(struct zone *zone);
1453 extern int isolate_anon_lru_page(struct page *page);
1454
1455 /**
1456 * for_each_online_pgdat - helper macro to iterate over all online nodes
1457 * @pgdat: pointer to a pg_data_t variable
1458 */
1459 #define for_each_online_pgdat(pgdat) \
1460 for (pgdat = first_online_pgdat(); \
1461 pgdat; \
1462 pgdat = next_online_pgdat(pgdat))
1463 /**
1464 * for_each_zone - helper macro to iterate over all memory zones
1465 * @zone: pointer to struct zone variable
1466 *
1467 * The user only needs to declare the zone variable, for_each_zone
1468 * fills it in.
1469 */
1470 #define for_each_zone(zone) \
1471 for (zone = (first_online_pgdat())->node_zones; \
1472 zone; \
1473 zone = next_zone(zone))
1474
1475 #define for_each_populated_zone(zone) \
1476 for (zone = (first_online_pgdat())->node_zones; \
1477 zone; \
1478 zone = next_zone(zone)) \
1479 if (!populated_zone(zone)) \
1480 ; /* do nothing */ \
1481 else
1482
zonelist_zone(struct zoneref * zoneref)1483 static inline struct zone *zonelist_zone(struct zoneref *zoneref)
1484 {
1485 return zoneref->zone;
1486 }
1487
zonelist_zone_idx(struct zoneref * zoneref)1488 static inline int zonelist_zone_idx(struct zoneref *zoneref)
1489 {
1490 return zoneref->zone_idx;
1491 }
1492
zonelist_node_idx(struct zoneref * zoneref)1493 static inline int zonelist_node_idx(struct zoneref *zoneref)
1494 {
1495 return zone_to_nid(zoneref->zone);
1496 }
1497
1498 struct zoneref *__next_zones_zonelist(struct zoneref *z,
1499 enum zone_type highest_zoneidx,
1500 nodemask_t *nodes);
1501
1502 /**
1503 * 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
1504 * @z: The cursor used as a starting point for the search
1505 * @highest_zoneidx: The zone index of the highest zone to return
1506 * @nodes: An optional nodemask to filter the zonelist with
1507 *
1508 * This function returns the next zone at or below a given zone index that is
1509 * within the allowed nodemask using a cursor as the starting point for the
1510 * search. The zoneref returned is a cursor that represents the current zone
1511 * being examined. It should be advanced by one before calling
1512 * next_zones_zonelist again.
1513 *
1514 * Return: the next zone at or below highest_zoneidx within the allowed
1515 * nodemask using a cursor within a zonelist as a starting point
1516 */
next_zones_zonelist(struct zoneref * z,enum zone_type highest_zoneidx,nodemask_t * nodes)1517 static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z,
1518 enum zone_type highest_zoneidx,
1519 nodemask_t *nodes)
1520 {
1521 if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx))
1522 return z;
1523 return __next_zones_zonelist(z, highest_zoneidx, nodes);
1524 }
1525
1526 /**
1527 * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
1528 * @zonelist: The zonelist to search for a suitable zone
1529 * @highest_zoneidx: The zone index of the highest zone to return
1530 * @nodes: An optional nodemask to filter the zonelist with
1531 *
1532 * This function returns the first zone at or below a given zone index that is
1533 * within the allowed nodemask. The zoneref returned is a cursor that can be
1534 * used to iterate the zonelist with next_zones_zonelist by advancing it by
1535 * one before calling.
1536 *
1537 * When no eligible zone is found, zoneref->zone is NULL (zoneref itself is
1538 * never NULL). This may happen either genuinely, or due to concurrent nodemask
1539 * update due to cpuset modification.
1540 *
1541 * Return: Zoneref pointer for the first suitable zone found
1542 */
first_zones_zonelist(struct zonelist * zonelist,enum zone_type highest_zoneidx,nodemask_t * nodes)1543 static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
1544 enum zone_type highest_zoneidx,
1545 nodemask_t *nodes)
1546 {
1547 return next_zones_zonelist(zonelist->_zonerefs,
1548 highest_zoneidx, nodes);
1549 }
1550
1551 /**
1552 * 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
1553 * @zone: The current zone in the iterator
1554 * @z: The current pointer within zonelist->_zonerefs being iterated
1555 * @zlist: The zonelist being iterated
1556 * @highidx: The zone index of the highest zone to return
1557 * @nodemask: Nodemask allowed by the allocator
1558 *
1559 * This iterator iterates though all zones at or below a given zone index and
1560 * within a given nodemask
1561 */
1562 #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
1563 for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \
1564 zone; \
1565 z = next_zones_zonelist(++z, highidx, nodemask), \
1566 zone = zonelist_zone(z))
1567
1568 #define for_next_zone_zonelist_nodemask(zone, z, highidx, nodemask) \
1569 for (zone = z->zone; \
1570 zone; \
1571 z = next_zones_zonelist(++z, highidx, nodemask), \
1572 zone = zonelist_zone(z))
1573
1574
1575 /**
1576 * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
1577 * @zone: The current zone in the iterator
1578 * @z: The current pointer within zonelist->zones being iterated
1579 * @zlist: The zonelist being iterated
1580 * @highidx: The zone index of the highest zone to return
1581 *
1582 * This iterator iterates though all zones at or below a given zone index.
1583 */
1584 #define for_each_zone_zonelist(zone, z, zlist, highidx) \
1585 for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
1586
1587 #ifdef CONFIG_SPARSEMEM
1588 #include <asm/sparsemem.h>
1589 #endif
1590
1591 #ifdef CONFIG_FLATMEM
1592 #define pfn_to_nid(pfn) (0)
1593 #endif
1594
1595 #ifdef CONFIG_SPARSEMEM
1596
1597 /*
1598 * PA_SECTION_SHIFT physical address to/from section number
1599 * PFN_SECTION_SHIFT pfn to/from section number
1600 */
1601 #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
1602 #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
1603
1604 #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
1605
1606 #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
1607 #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
1608
1609 #define SECTION_BLOCKFLAGS_BITS \
1610 ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
1611
1612 #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
1613 #error Allocator MAX_ORDER exceeds SECTION_SIZE
1614 #endif
1615
pfn_to_section_nr(unsigned long pfn)1616 static inline unsigned long pfn_to_section_nr(unsigned long pfn)
1617 {
1618 return pfn >> PFN_SECTION_SHIFT;
1619 }
section_nr_to_pfn(unsigned long sec)1620 static inline unsigned long section_nr_to_pfn(unsigned long sec)
1621 {
1622 return sec << PFN_SECTION_SHIFT;
1623 }
1624
1625 #define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK)
1626 #define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK)
1627
1628 #define SUBSECTION_SHIFT 21
1629 #define SUBSECTION_SIZE (1UL << SUBSECTION_SHIFT)
1630
1631 #define PFN_SUBSECTION_SHIFT (SUBSECTION_SHIFT - PAGE_SHIFT)
1632 #define PAGES_PER_SUBSECTION (1UL << PFN_SUBSECTION_SHIFT)
1633 #define PAGE_SUBSECTION_MASK (~(PAGES_PER_SUBSECTION-1))
1634
1635 #if SUBSECTION_SHIFT > SECTION_SIZE_BITS
1636 #error Subsection size exceeds section size
1637 #else
1638 #define SUBSECTIONS_PER_SECTION (1UL << (SECTION_SIZE_BITS - SUBSECTION_SHIFT))
1639 #endif
1640
1641 #define SUBSECTION_ALIGN_UP(pfn) ALIGN((pfn), PAGES_PER_SUBSECTION)
1642 #define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK)
1643
1644 struct mem_section_usage {
1645 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1646 DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION);
1647 #endif
1648 /* See declaration of similar field in struct zone */
1649 unsigned long pageblock_flags[0];
1650 };
1651
1652 void subsection_map_init(unsigned long pfn, unsigned long nr_pages);
1653
1654 struct page;
1655 struct page_ext;
1656 struct mem_section {
1657 /*
1658 * This is, logically, a pointer to an array of struct
1659 * pages. However, it is stored with some other magic.
1660 * (see sparse.c::sparse_init_one_section())
1661 *
1662 * Additionally during early boot we encode node id of
1663 * the location of the section here to guide allocation.
1664 * (see sparse.c::memory_present())
1665 *
1666 * Making it a UL at least makes someone do a cast
1667 * before using it wrong.
1668 */
1669 unsigned long section_mem_map;
1670
1671 struct mem_section_usage *usage;
1672 #ifdef CONFIG_PAGE_EXTENSION
1673 /*
1674 * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use
1675 * section. (see page_ext.h about this.)
1676 */
1677 struct page_ext *page_ext;
1678 unsigned long pad;
1679 #endif
1680 /*
1681 * WARNING: mem_section must be a power-of-2 in size for the
1682 * calculation and use of SECTION_ROOT_MASK to make sense.
1683 */
1684 };
1685
1686 #ifdef CONFIG_SPARSEMEM_EXTREME
1687 #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
1688 #else
1689 #define SECTIONS_PER_ROOT 1
1690 #endif
1691
1692 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
1693 #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT)
1694 #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
1695
1696 #ifdef CONFIG_SPARSEMEM_EXTREME
1697 extern struct mem_section **mem_section;
1698 #else
1699 extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
1700 #endif
1701
section_to_usemap(struct mem_section * ms)1702 static inline unsigned long *section_to_usemap(struct mem_section *ms)
1703 {
1704 return ms->usage->pageblock_flags;
1705 }
1706
__nr_to_section(unsigned long nr)1707 static inline struct mem_section *__nr_to_section(unsigned long nr)
1708 {
1709 unsigned long root = SECTION_NR_TO_ROOT(nr);
1710
1711 if (unlikely(root >= NR_SECTION_ROOTS))
1712 return NULL;
1713
1714 #ifdef CONFIG_SPARSEMEM_EXTREME
1715 if (!mem_section || !mem_section[root])
1716 return NULL;
1717 #endif
1718 return &mem_section[root][nr & SECTION_ROOT_MASK];
1719 }
1720 extern size_t mem_section_usage_size(void);
1721
1722 /*
1723 * We use the lower bits of the mem_map pointer to store
1724 * a little bit of information. The pointer is calculated
1725 * as mem_map - section_nr_to_pfn(pnum). The result is
1726 * aligned to the minimum alignment of the two values:
1727 * 1. All mem_map arrays are page-aligned.
1728 * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT
1729 * lowest bits. PFN_SECTION_SHIFT is arch-specific
1730 * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the
1731 * worst combination is powerpc with 256k pages,
1732 * which results in PFN_SECTION_SHIFT equal 6.
1733 * To sum it up, at least 6 bits are available.
1734 */
1735 #define SECTION_MARKED_PRESENT (1UL<<0)
1736 #define SECTION_HAS_MEM_MAP (1UL<<1)
1737 #define SECTION_IS_ONLINE (1UL<<2)
1738 #define SECTION_IS_EARLY (1UL<<3)
1739 #define SECTION_TAINT_ZONE_DEVICE (1UL<<4)
1740 #define SECTION_MAP_LAST_BIT (1UL<<5)
1741 #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
1742 #define SECTION_NID_SHIFT 6
1743
__section_mem_map_addr(struct mem_section * section)1744 static inline struct page *__section_mem_map_addr(struct mem_section *section)
1745 {
1746 unsigned long map = section->section_mem_map;
1747 map &= SECTION_MAP_MASK;
1748 return (struct page *)map;
1749 }
1750
present_section(struct mem_section * section)1751 static inline int present_section(struct mem_section *section)
1752 {
1753 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
1754 }
1755
present_section_nr(unsigned long nr)1756 static inline int present_section_nr(unsigned long nr)
1757 {
1758 return present_section(__nr_to_section(nr));
1759 }
1760
valid_section(struct mem_section * section)1761 static inline int valid_section(struct mem_section *section)
1762 {
1763 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
1764 }
1765
early_section(struct mem_section * section)1766 static inline int early_section(struct mem_section *section)
1767 {
1768 return (section && (section->section_mem_map & SECTION_IS_EARLY));
1769 }
1770
valid_section_nr(unsigned long nr)1771 static inline int valid_section_nr(unsigned long nr)
1772 {
1773 return valid_section(__nr_to_section(nr));
1774 }
1775
online_section(struct mem_section * section)1776 static inline int online_section(struct mem_section *section)
1777 {
1778 return (section && (section->section_mem_map & SECTION_IS_ONLINE));
1779 }
1780
online_device_section(struct mem_section * section)1781 static inline int online_device_section(struct mem_section *section)
1782 {
1783 unsigned long flags = SECTION_IS_ONLINE | SECTION_TAINT_ZONE_DEVICE;
1784
1785 return section && ((section->section_mem_map & flags) == flags);
1786 }
1787
online_section_nr(unsigned long nr)1788 static inline int online_section_nr(unsigned long nr)
1789 {
1790 return online_section(__nr_to_section(nr));
1791 }
1792
1793 #ifdef CONFIG_MEMORY_HOTPLUG
1794 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1795 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1796 #endif
1797
__pfn_to_section(unsigned long pfn)1798 static inline struct mem_section *__pfn_to_section(unsigned long pfn)
1799 {
1800 return __nr_to_section(pfn_to_section_nr(pfn));
1801 }
1802
1803 extern unsigned long __highest_present_section_nr;
1804
subsection_map_index(unsigned long pfn)1805 static inline int subsection_map_index(unsigned long pfn)
1806 {
1807 return (pfn & ~(PAGE_SECTION_MASK)) / PAGES_PER_SUBSECTION;
1808 }
1809
1810 #ifdef CONFIG_SPARSEMEM_VMEMMAP
pfn_section_valid(struct mem_section * ms,unsigned long pfn)1811 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1812 {
1813 int idx = subsection_map_index(pfn);
1814
1815 return test_bit(idx, ms->usage->subsection_map);
1816 }
1817 #else
pfn_section_valid(struct mem_section * ms,unsigned long pfn)1818 static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1819 {
1820 return 1;
1821 }
1822 #endif
1823
1824 #ifndef CONFIG_HAVE_ARCH_PFN_VALID
1825 /**
1826 * pfn_valid - check if there is a valid memory map entry for a PFN
1827 * @pfn: the page frame number to check
1828 *
1829 * Check if there is a valid memory map entry aka struct page for the @pfn.
1830 * Note, that availability of the memory map entry does not imply that
1831 * there is actual usable memory at that @pfn. The struct page may
1832 * represent a hole or an unusable page frame.
1833 *
1834 * Return: 1 for PFNs that have memory map entries and 0 otherwise
1835 */
pfn_valid(unsigned long pfn)1836 static inline int pfn_valid(unsigned long pfn)
1837 {
1838 struct mem_section *ms;
1839
1840 /*
1841 * Ensure the upper PAGE_SHIFT bits are clear in the
1842 * pfn. Else it might lead to false positives when
1843 * some of the upper bits are set, but the lower bits
1844 * match a valid pfn.
1845 */
1846 if (PHYS_PFN(PFN_PHYS(pfn)) != pfn)
1847 return 0;
1848
1849 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1850 return 0;
1851 ms = __pfn_to_section(pfn);
1852 if (!valid_section(ms))
1853 return 0;
1854 /*
1855 * Traditionally early sections always returned pfn_valid() for
1856 * the entire section-sized span.
1857 */
1858 return early_section(ms) || pfn_section_valid(ms, pfn);
1859 }
1860 #endif
1861
pfn_in_present_section(unsigned long pfn)1862 static inline int pfn_in_present_section(unsigned long pfn)
1863 {
1864 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1865 return 0;
1866 return present_section(__pfn_to_section(pfn));
1867 }
1868
next_present_section_nr(unsigned long section_nr)1869 static inline unsigned long next_present_section_nr(unsigned long section_nr)
1870 {
1871 while (++section_nr <= __highest_present_section_nr) {
1872 if (present_section_nr(section_nr))
1873 return section_nr;
1874 }
1875
1876 return -1;
1877 }
1878
1879 /*
1880 * These are _only_ used during initialisation, therefore they
1881 * can use __initdata ... They could have names to indicate
1882 * this restriction.
1883 */
1884 #ifdef CONFIG_NUMA
1885 #define pfn_to_nid(pfn) \
1886 ({ \
1887 unsigned long __pfn_to_nid_pfn = (pfn); \
1888 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
1889 })
1890 #else
1891 #define pfn_to_nid(pfn) (0)
1892 #endif
1893
1894 void sparse_init(void);
1895 #else
1896 #define sparse_init() do {} while (0)
1897 #define sparse_index_init(_sec, _nid) do {} while (0)
1898 #define pfn_in_present_section pfn_valid
1899 #define subsection_map_init(_pfn, _nr_pages) do {} while (0)
1900 #endif /* CONFIG_SPARSEMEM */
1901
1902 #endif /* !__GENERATING_BOUNDS.H */
1903 #endif /* !__ASSEMBLY__ */
1904 #endif /* _LINUX_MMZONE_H */
1905