1
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
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
35
36
37
38
39#define PAGE_ALLOC_COSTLY_ORDER 3
40
41enum migratetype {
42 MIGRATE_UNMOVABLE,
43 MIGRATE_MOVABLE,
44 MIGRATE_RECLAIMABLE,
45 MIGRATE_PCPTYPES,
46 MIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES,
47#ifdef CONFIG_CMA
48
49
50
51
52
53
54
55
56
57
58
59
60
61 MIGRATE_CMA,
62#endif
63#ifdef CONFIG_MEMORY_ISOLATION
64 MIGRATE_ISOLATE,
65#endif
66 MIGRATE_TYPES
67};
68
69
70extern const char * const migratetype_names[MIGRATE_TYPES];
71
72#ifdef CONFIG_CMA
73# define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)
74# define is_migrate_cma_page(_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA)
75#else
76# define is_migrate_cma(migratetype) false
77# define is_migrate_cma_page(_page) false
78#endif
79
80static inline bool is_migrate_movable(int mt)
81{
82 return is_migrate_cma(mt) || mt == MIGRATE_MOVABLE;
83}
84
85#define for_each_migratetype_order(order, type) \
86 for (order = 0; order < MAX_ORDER; order++) \
87 for (type = 0; type < MIGRATE_TYPES; type++)
88
89extern int page_group_by_mobility_disabled;
90
91#define MIGRATETYPE_MASK ((1UL << PB_migratetype_bits) - 1)
92
93#define get_pageblock_migratetype(page) \
94 get_pfnblock_flags_mask(page, page_to_pfn(page), MIGRATETYPE_MASK)
95
96struct free_area {
97 struct list_head free_list[MIGRATE_TYPES];
98 unsigned long nr_free;
99};
100
101static inline struct page *get_page_from_free_area(struct free_area *area,
102 int migratetype)
103{
104 return list_first_entry_or_null(&area->free_list[migratetype],
105 struct page, lru);
106}
107
108static inline bool free_area_empty(struct free_area *area, int migratetype)
109{
110 return list_empty(&area->free_list[migratetype]);
111}
112
113struct pglist_data;
114
115
116
117
118
119
120#if defined(CONFIG_SMP)
121struct zone_padding {
122 char x[0];
123} ____cacheline_internodealigned_in_smp;
124#define ZONE_PADDING(name) struct zone_padding name;
125#else
126#define ZONE_PADDING(name)
127#endif
128
129#ifdef CONFIG_NUMA
130enum numa_stat_item {
131 NUMA_HIT,
132 NUMA_MISS,
133 NUMA_FOREIGN,
134 NUMA_INTERLEAVE_HIT,
135 NUMA_LOCAL,
136 NUMA_OTHER,
137 NR_VM_NUMA_STAT_ITEMS
138};
139#else
140#define NR_VM_NUMA_STAT_ITEMS 0
141#endif
142
143enum zone_stat_item {
144
145 NR_FREE_PAGES,
146 NR_ZONE_LRU_BASE,
147 NR_ZONE_INACTIVE_ANON = NR_ZONE_LRU_BASE,
148 NR_ZONE_ACTIVE_ANON,
149 NR_ZONE_INACTIVE_FILE,
150 NR_ZONE_ACTIVE_FILE,
151 NR_ZONE_UNEVICTABLE,
152 NR_ZONE_WRITE_PENDING,
153 NR_MLOCK,
154
155 NR_BOUNCE,
156#if IS_ENABLED(CONFIG_ZSMALLOC)
157 NR_ZSPAGES,
158#endif
159 NR_FREE_CMA_PAGES,
160 NR_VM_ZONE_STAT_ITEMS };
161
162enum node_stat_item {
163 NR_LRU_BASE,
164 NR_INACTIVE_ANON = NR_LRU_BASE,
165 NR_ACTIVE_ANON,
166 NR_INACTIVE_FILE,
167 NR_ACTIVE_FILE,
168 NR_UNEVICTABLE,
169 NR_SLAB_RECLAIMABLE_B,
170 NR_SLAB_UNRECLAIMABLE_B,
171 NR_ISOLATED_ANON,
172 NR_ISOLATED_FILE,
173 WORKINGSET_NODES,
174 WORKINGSET_REFAULT_BASE,
175 WORKINGSET_REFAULT_ANON = WORKINGSET_REFAULT_BASE,
176 WORKINGSET_REFAULT_FILE,
177 WORKINGSET_ACTIVATE_BASE,
178 WORKINGSET_ACTIVATE_ANON = WORKINGSET_ACTIVATE_BASE,
179 WORKINGSET_ACTIVATE_FILE,
180 WORKINGSET_RESTORE_BASE,
181 WORKINGSET_RESTORE_ANON = WORKINGSET_RESTORE_BASE,
182 WORKINGSET_RESTORE_FILE,
183 WORKINGSET_NODERECLAIM,
184 NR_ANON_MAPPED,
185 NR_FILE_MAPPED,
186
187 NR_FILE_PAGES,
188 NR_FILE_DIRTY,
189 NR_WRITEBACK,
190 NR_WRITEBACK_TEMP,
191 NR_SHMEM,
192 NR_SHMEM_THPS,
193 NR_SHMEM_PMDMAPPED,
194 NR_FILE_THPS,
195 NR_FILE_PMDMAPPED,
196 NR_ANON_THPS,
197 NR_VMSCAN_WRITE,
198 NR_VMSCAN_IMMEDIATE,
199 NR_DIRTIED,
200 NR_WRITTEN,
201 NR_KERNEL_MISC_RECLAIMABLE,
202 NR_FOLL_PIN_ACQUIRED,
203 NR_FOLL_PIN_RELEASED,
204 NR_KERNEL_STACK_KB,
205#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
206 NR_KERNEL_SCS_KB,
207#endif
208 NR_PAGETABLE,
209#ifdef CONFIG_SWAP
210 NR_SWAPCACHE,
211#endif
212 NR_VM_NODE_STAT_ITEMS
213};
214
215
216
217
218
219
220static __always_inline bool vmstat_item_print_in_thp(enum node_stat_item item)
221{
222 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
223 return false;
224
225 return item == NR_ANON_THPS ||
226 item == NR_FILE_THPS ||
227 item == NR_SHMEM_THPS ||
228 item == NR_SHMEM_PMDMAPPED ||
229 item == NR_FILE_PMDMAPPED;
230}
231
232
233
234
235
236
237static __always_inline bool vmstat_item_in_bytes(int idx)
238{
239
240
241
242
243
244
245
246
247
248 return (idx == NR_SLAB_RECLAIMABLE_B ||
249 idx == NR_SLAB_UNRECLAIMABLE_B);
250}
251
252
253
254
255
256
257
258
259
260
261#define LRU_BASE 0
262#define LRU_ACTIVE 1
263#define LRU_FILE 2
264
265enum lru_list {
266 LRU_INACTIVE_ANON = LRU_BASE,
267 LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE,
268 LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
269 LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
270 LRU_UNEVICTABLE,
271 NR_LRU_LISTS
272};
273
274#define for_each_lru(lru) for (lru = 0; lru < NR_LRU_LISTS; lru++)
275
276#define for_each_evictable_lru(lru) for (lru = 0; lru <= LRU_ACTIVE_FILE; lru++)
277
278static inline bool is_file_lru(enum lru_list lru)
279{
280 return (lru == LRU_INACTIVE_FILE || lru == LRU_ACTIVE_FILE);
281}
282
283static inline bool is_active_lru(enum lru_list lru)
284{
285 return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE);
286}
287
288#define ANON_AND_FILE 2
289
290enum lruvec_flags {
291 LRUVEC_CONGESTED,
292
293
294};
295
296struct lruvec {
297 struct list_head lists[NR_LRU_LISTS];
298
299 spinlock_t lru_lock;
300
301
302
303
304
305 unsigned long anon_cost;
306 unsigned long file_cost;
307
308 atomic_long_t nonresident_age;
309
310 unsigned long refaults[ANON_AND_FILE];
311
312 unsigned long flags;
313#ifdef CONFIG_MEMCG
314 struct pglist_data *pgdat;
315#endif
316};
317
318
319#define ISOLATE_UNMAPPED ((__force isolate_mode_t)0x2)
320
321#define ISOLATE_ASYNC_MIGRATE ((__force isolate_mode_t)0x4)
322
323#define ISOLATE_UNEVICTABLE ((__force isolate_mode_t)0x8)
324
325
326typedef unsigned __bitwise isolate_mode_t;
327
328enum zone_watermarks {
329 WMARK_MIN,
330 WMARK_LOW,
331 WMARK_HIGH,
332 NR_WMARK
333};
334
335#define min_wmark_pages(z) (z->_watermark[WMARK_MIN] + z->watermark_boost)
336#define low_wmark_pages(z) (z->_watermark[WMARK_LOW] + z->watermark_boost)
337#define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost)
338#define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost)
339
340struct per_cpu_pages {
341 int count;
342 int high;
343 int batch;
344
345
346 struct list_head lists[MIGRATE_PCPTYPES];
347};
348
349struct per_cpu_pageset {
350 struct per_cpu_pages pcp;
351#ifdef CONFIG_NUMA
352 s8 expire;
353 u16 vm_numa_stat_diff[NR_VM_NUMA_STAT_ITEMS];
354#endif
355#ifdef CONFIG_SMP
356 s8 stat_threshold;
357 s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
358#endif
359};
360
361struct per_cpu_nodestat {
362 s8 stat_threshold;
363 s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
364};
365
366#endif
367
368enum zone_type {
369
370
371
372
373
374
375
376
377
378
379#ifdef CONFIG_ZONE_DMA
380 ZONE_DMA,
381#endif
382#ifdef CONFIG_ZONE_DMA32
383 ZONE_DMA32,
384#endif
385
386
387
388
389
390 ZONE_NORMAL,
391#ifdef CONFIG_HIGHMEM
392
393
394
395
396
397
398
399
400 ZONE_HIGHMEM,
401#endif
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437 ZONE_MOVABLE,
438#ifdef CONFIG_ZONE_DEVICE
439 ZONE_DEVICE,
440#endif
441 __MAX_NR_ZONES
442
443};
444
445#ifndef __GENERATING_BOUNDS_H
446
447#define ASYNC_AND_SYNC 2
448
449struct zone {
450
451
452
453 unsigned long _watermark[NR_WMARK];
454 unsigned long watermark_boost;
455
456 unsigned long nr_reserved_highatomic;
457
458
459
460
461
462
463
464
465
466
467 long lowmem_reserve[MAX_NR_ZONES];
468
469#ifdef CONFIG_NUMA
470 int node;
471#endif
472 struct pglist_data *zone_pgdat;
473 struct per_cpu_pageset __percpu *pageset;
474
475
476
477
478 int pageset_high;
479 int pageset_batch;
480
481#ifndef CONFIG_SPARSEMEM
482
483
484
485
486 unsigned long *pageblock_flags;
487#endif
488
489
490 unsigned long zone_start_pfn;
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530 atomic_long_t managed_pages;
531 unsigned long spanned_pages;
532 unsigned long present_pages;
533#ifdef CONFIG_CMA
534 unsigned long cma_pages;
535#endif
536
537 const char *name;
538
539#ifdef CONFIG_MEMORY_ISOLATION
540
541
542
543
544
545 unsigned long nr_isolate_pageblock;
546#endif
547
548#ifdef CONFIG_MEMORY_HOTPLUG
549
550 seqlock_t span_seqlock;
551#endif
552
553 int initialized;
554
555
556 ZONE_PADDING(_pad1_)
557
558
559 struct free_area free_area[MAX_ORDER];
560
561
562 unsigned long flags;
563
564
565 spinlock_t lock;
566
567
568 ZONE_PADDING(_pad2_)
569
570
571
572
573
574
575 unsigned long percpu_drift_mark;
576
577#if defined CONFIG_COMPACTION || defined CONFIG_CMA
578
579 unsigned long compact_cached_free_pfn;
580
581 unsigned long compact_cached_migrate_pfn[ASYNC_AND_SYNC];
582 unsigned long compact_init_migrate_pfn;
583 unsigned long compact_init_free_pfn;
584#endif
585
586#ifdef CONFIG_COMPACTION
587
588
589
590
591
592
593 unsigned int compact_considered;
594 unsigned int compact_defer_shift;
595 int compact_order_failed;
596#endif
597
598#if defined CONFIG_COMPACTION || defined CONFIG_CMA
599
600 bool compact_blockskip_flush;
601#endif
602
603 bool contiguous;
604
605 ZONE_PADDING(_pad3_)
606
607 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
608 atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS];
609} ____cacheline_internodealigned_in_smp;
610
611enum pgdat_flags {
612 PGDAT_DIRTY,
613
614
615
616 PGDAT_WRITEBACK,
617
618
619 PGDAT_RECLAIM_LOCKED,
620};
621
622enum zone_flags {
623 ZONE_BOOSTED_WATERMARK,
624
625
626};
627
628static inline unsigned long zone_managed_pages(struct zone *zone)
629{
630 return (unsigned long)atomic_long_read(&zone->managed_pages);
631}
632
633static inline unsigned long zone_cma_pages(struct zone *zone)
634{
635#ifdef CONFIG_CMA
636 return zone->cma_pages;
637#else
638 return 0;
639#endif
640}
641
642static inline unsigned long zone_end_pfn(const struct zone *zone)
643{
644 return zone->zone_start_pfn + zone->spanned_pages;
645}
646
647static inline bool zone_spans_pfn(const struct zone *zone, unsigned long pfn)
648{
649 return zone->zone_start_pfn <= pfn && pfn < zone_end_pfn(zone);
650}
651
652static inline bool zone_is_initialized(struct zone *zone)
653{
654 return zone->initialized;
655}
656
657static inline bool zone_is_empty(struct zone *zone)
658{
659 return zone->spanned_pages == 0;
660}
661
662
663
664
665
666static inline bool zone_intersects(struct zone *zone,
667 unsigned long start_pfn, unsigned long nr_pages)
668{
669 if (zone_is_empty(zone))
670 return false;
671 if (start_pfn >= zone_end_pfn(zone) ||
672 start_pfn + nr_pages <= zone->zone_start_pfn)
673 return false;
674
675 return true;
676}
677
678
679
680
681
682
683#define DEF_PRIORITY 12
684
685
686#define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
687
688enum {
689 ZONELIST_FALLBACK,
690#ifdef CONFIG_NUMA
691
692
693
694
695 ZONELIST_NOFALLBACK,
696#endif
697 MAX_ZONELISTS
698};
699
700
701
702
703
704struct zoneref {
705 struct zone *zone;
706 int zone_idx;
707};
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723struct zonelist {
724 struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
725};
726
727#ifndef CONFIG_DISCONTIGMEM
728
729extern struct page *mem_map;
730#endif
731
732#ifdef CONFIG_TRANSPARENT_HUGEPAGE
733struct deferred_split {
734 spinlock_t split_queue_lock;
735 struct list_head split_queue;
736 unsigned long split_queue_len;
737};
738#endif
739
740
741
742
743
744
745
746
747
748typedef struct pglist_data {
749
750
751
752
753
754 struct zone node_zones[MAX_NR_ZONES];
755
756
757
758
759
760
761 struct zonelist node_zonelists[MAX_ZONELISTS];
762
763 int nr_zones;
764#ifdef CONFIG_FLAT_NODE_MEM_MAP
765 struct page *node_mem_map;
766#ifdef CONFIG_PAGE_EXTENSION
767 struct page_ext *node_page_ext;
768#endif
769#endif
770#if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
771
772
773
774
775
776
777
778
779
780
781
782
783 spinlock_t node_size_lock;
784#endif
785 unsigned long node_start_pfn;
786 unsigned long node_present_pages;
787 unsigned long node_spanned_pages;
788
789 int node_id;
790 wait_queue_head_t kswapd_wait;
791 wait_queue_head_t pfmemalloc_wait;
792 struct task_struct *kswapd;
793
794 int kswapd_order;
795 enum zone_type kswapd_highest_zoneidx;
796
797 int kswapd_failures;
798
799#ifdef CONFIG_COMPACTION
800 int kcompactd_max_order;
801 enum zone_type kcompactd_highest_zoneidx;
802 wait_queue_head_t kcompactd_wait;
803 struct task_struct *kcompactd;
804#endif
805
806
807
808
809 unsigned long totalreserve_pages;
810
811#ifdef CONFIG_NUMA
812
813
814
815 unsigned long min_unmapped_pages;
816 unsigned long min_slab_pages;
817#endif
818
819
820 ZONE_PADDING(_pad1_)
821
822#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
823
824
825
826
827 unsigned long first_deferred_pfn;
828#endif
829
830#ifdef CONFIG_TRANSPARENT_HUGEPAGE
831 struct deferred_split deferred_split_queue;
832#endif
833
834
835
836
837
838
839
840
841 struct lruvec __lruvec;
842
843 unsigned long flags;
844
845 ZONE_PADDING(_pad2_)
846
847
848 struct per_cpu_nodestat __percpu *per_cpu_nodestats;
849 atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS];
850} pg_data_t;
851
852#define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
853#define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
854#ifdef CONFIG_FLAT_NODE_MEM_MAP
855#define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
856#else
857#define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
858#endif
859#define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
860
861#define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn)
862#define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid))
863
864static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
865{
866 return pgdat->node_start_pfn + pgdat->node_spanned_pages;
867}
868
869static inline bool pgdat_is_empty(pg_data_t *pgdat)
870{
871 return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
872}
873
874#include <linux/memory_hotplug.h>
875
876void build_all_zonelists(pg_data_t *pgdat);
877void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
878 enum zone_type highest_zoneidx);
879bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
880 int highest_zoneidx, unsigned int alloc_flags,
881 long free_pages);
882bool zone_watermark_ok(struct zone *z, unsigned int order,
883 unsigned long mark, int highest_zoneidx,
884 unsigned int alloc_flags);
885bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
886 unsigned long mark, int highest_zoneidx);
887
888
889
890
891enum meminit_context {
892 MEMINIT_EARLY,
893 MEMINIT_HOTPLUG,
894};
895
896extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
897 unsigned long size);
898
899extern void lruvec_init(struct lruvec *lruvec);
900
901static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec)
902{
903#ifdef CONFIG_MEMCG
904 return lruvec->pgdat;
905#else
906 return container_of(lruvec, struct pglist_data, __lruvec);
907#endif
908}
909
910#ifdef CONFIG_HAVE_MEMORYLESS_NODES
911int local_memory_node(int node_id);
912#else
913static inline int local_memory_node(int node_id) { return node_id; };
914#endif
915
916
917
918
919#define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
920
921#ifdef CONFIG_ZONE_DEVICE
922static inline bool zone_is_zone_device(struct zone *zone)
923{
924 return zone_idx(zone) == ZONE_DEVICE;
925}
926#else
927static inline bool zone_is_zone_device(struct zone *zone)
928{
929 return false;
930}
931#endif
932
933
934
935
936
937
938
939static inline bool managed_zone(struct zone *zone)
940{
941 return zone_managed_pages(zone);
942}
943
944
945static inline bool populated_zone(struct zone *zone)
946{
947 return zone->present_pages;
948}
949
950#ifdef CONFIG_NUMA
951static inline int zone_to_nid(struct zone *zone)
952{
953 return zone->node;
954}
955
956static inline void zone_set_nid(struct zone *zone, int nid)
957{
958 zone->node = nid;
959}
960#else
961static inline int zone_to_nid(struct zone *zone)
962{
963 return 0;
964}
965
966static inline void zone_set_nid(struct zone *zone, int nid) {}
967#endif
968
969extern int movable_zone;
970
971#ifdef CONFIG_HIGHMEM
972static inline int zone_movable_is_highmem(void)
973{
974#ifdef CONFIG_NEED_MULTIPLE_NODES
975 return movable_zone == ZONE_HIGHMEM;
976#else
977 return (ZONE_MOVABLE - 1) == ZONE_HIGHMEM;
978#endif
979}
980#endif
981
982static inline int is_highmem_idx(enum zone_type idx)
983{
984#ifdef CONFIG_HIGHMEM
985 return (idx == ZONE_HIGHMEM ||
986 (idx == ZONE_MOVABLE && zone_movable_is_highmem()));
987#else
988 return 0;
989#endif
990}
991
992
993
994
995
996
997
998static inline int is_highmem(struct zone *zone)
999{
1000#ifdef CONFIG_HIGHMEM
1001 return is_highmem_idx(zone_idx(zone));
1002#else
1003 return 0;
1004#endif
1005}
1006
1007
1008struct ctl_table;
1009
1010int min_free_kbytes_sysctl_handler(struct ctl_table *, int, void *, size_t *,
1011 loff_t *);
1012int watermark_scale_factor_sysctl_handler(struct ctl_table *, int, void *,
1013 size_t *, loff_t *);
1014extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES];
1015int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, void *,
1016 size_t *, loff_t *);
1017int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int,
1018 void *, size_t *, loff_t *);
1019int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
1020 void *, size_t *, loff_t *);
1021int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
1022 void *, size_t *, loff_t *);
1023int numa_zonelist_order_handler(struct ctl_table *, int,
1024 void *, size_t *, loff_t *);
1025extern int percpu_pagelist_fraction;
1026extern char numa_zonelist_order[];
1027#define NUMA_ZONELIST_ORDER_LEN 16
1028
1029#ifndef CONFIG_NEED_MULTIPLE_NODES
1030
1031extern struct pglist_data contig_page_data;
1032#define NODE_DATA(nid) (&contig_page_data)
1033#define NODE_MEM_MAP(nid) mem_map
1034
1035#else
1036
1037#include <asm/mmzone.h>
1038
1039#endif
1040
1041extern struct pglist_data *first_online_pgdat(void);
1042extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
1043extern struct zone *next_zone(struct zone *zone);
1044
1045
1046
1047
1048
1049#define for_each_online_pgdat(pgdat) \
1050 for (pgdat = first_online_pgdat(); \
1051 pgdat; \
1052 pgdat = next_online_pgdat(pgdat))
1053
1054
1055
1056
1057
1058
1059
1060#define for_each_zone(zone) \
1061 for (zone = (first_online_pgdat())->node_zones; \
1062 zone; \
1063 zone = next_zone(zone))
1064
1065#define for_each_populated_zone(zone) \
1066 for (zone = (first_online_pgdat())->node_zones; \
1067 zone; \
1068 zone = next_zone(zone)) \
1069 if (!populated_zone(zone)) \
1070 ; \
1071 else
1072
1073static inline struct zone *zonelist_zone(struct zoneref *zoneref)
1074{
1075 return zoneref->zone;
1076}
1077
1078static inline int zonelist_zone_idx(struct zoneref *zoneref)
1079{
1080 return zoneref->zone_idx;
1081}
1082
1083static inline int zonelist_node_idx(struct zoneref *zoneref)
1084{
1085 return zone_to_nid(zoneref->zone);
1086}
1087
1088struct zoneref *__next_zones_zonelist(struct zoneref *z,
1089 enum zone_type highest_zoneidx,
1090 nodemask_t *nodes);
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z,
1105 enum zone_type highest_zoneidx,
1106 nodemask_t *nodes)
1107{
1108 if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx))
1109 return z;
1110 return __next_zones_zonelist(z, highest_zoneidx, nodes);
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
1130 enum zone_type highest_zoneidx,
1131 nodemask_t *nodes)
1132{
1133 return next_zones_zonelist(zonelist->_zonerefs,
1134 highest_zoneidx, nodes);
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148#define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
1149 for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \
1150 zone; \
1151 z = next_zones_zonelist(++z, highidx, nodemask), \
1152 zone = zonelist_zone(z))
1153
1154#define for_next_zone_zonelist_nodemask(zone, z, highidx, nodemask) \
1155 for (zone = z->zone; \
1156 zone; \
1157 z = next_zones_zonelist(++z, highidx, nodemask), \
1158 zone = zonelist_zone(z))
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170#define for_each_zone_zonelist(zone, z, zlist, highidx) \
1171 for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
1172
1173#ifdef CONFIG_SPARSEMEM
1174#include <asm/sparsemem.h>
1175#endif
1176
1177#ifdef CONFIG_FLATMEM
1178#define pfn_to_nid(pfn) (0)
1179#endif
1180
1181#ifdef CONFIG_SPARSEMEM
1182
1183
1184
1185
1186
1187
1188
1189#define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
1190#define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
1191
1192#define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
1193
1194#define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
1195#define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
1196
1197#define SECTION_BLOCKFLAGS_BITS \
1198 ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
1199
1200#if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
1201#error Allocator MAX_ORDER exceeds SECTION_SIZE
1202#endif
1203
1204static inline unsigned long pfn_to_section_nr(unsigned long pfn)
1205{
1206 return pfn >> PFN_SECTION_SHIFT;
1207}
1208static inline unsigned long section_nr_to_pfn(unsigned long sec)
1209{
1210 return sec << PFN_SECTION_SHIFT;
1211}
1212
1213#define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK)
1214#define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK)
1215
1216#define SUBSECTION_SHIFT 21
1217#define SUBSECTION_SIZE (1UL << SUBSECTION_SHIFT)
1218
1219#define PFN_SUBSECTION_SHIFT (SUBSECTION_SHIFT - PAGE_SHIFT)
1220#define PAGES_PER_SUBSECTION (1UL << PFN_SUBSECTION_SHIFT)
1221#define PAGE_SUBSECTION_MASK (~(PAGES_PER_SUBSECTION-1))
1222
1223#if SUBSECTION_SHIFT > SECTION_SIZE_BITS
1224#error Subsection size exceeds section size
1225#else
1226#define SUBSECTIONS_PER_SECTION (1UL << (SECTION_SIZE_BITS - SUBSECTION_SHIFT))
1227#endif
1228
1229#define SUBSECTION_ALIGN_UP(pfn) ALIGN((pfn), PAGES_PER_SUBSECTION)
1230#define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK)
1231
1232struct mem_section_usage {
1233#ifdef CONFIG_SPARSEMEM_VMEMMAP
1234 DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION);
1235#endif
1236
1237 unsigned long pageblock_flags[0];
1238};
1239
1240void subsection_map_init(unsigned long pfn, unsigned long nr_pages);
1241
1242struct page;
1243struct page_ext;
1244struct mem_section {
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257 unsigned long section_mem_map;
1258
1259 struct mem_section_usage *usage;
1260#ifdef CONFIG_PAGE_EXTENSION
1261
1262
1263
1264
1265 struct page_ext *page_ext;
1266 unsigned long pad;
1267#endif
1268
1269
1270
1271
1272};
1273
1274#ifdef CONFIG_SPARSEMEM_EXTREME
1275#define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
1276#else
1277#define SECTIONS_PER_ROOT 1
1278#endif
1279
1280#define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
1281#define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT)
1282#define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
1283
1284#ifdef CONFIG_SPARSEMEM_EXTREME
1285extern struct mem_section **mem_section;
1286#else
1287extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
1288#endif
1289
1290static inline unsigned long *section_to_usemap(struct mem_section *ms)
1291{
1292 return ms->usage->pageblock_flags;
1293}
1294
1295static inline struct mem_section *__nr_to_section(unsigned long nr)
1296{
1297#ifdef CONFIG_SPARSEMEM_EXTREME
1298 if (!mem_section)
1299 return NULL;
1300#endif
1301 if (!mem_section[SECTION_NR_TO_ROOT(nr)])
1302 return NULL;
1303 return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
1304}
1305extern unsigned long __section_nr(struct mem_section *ms);
1306extern size_t mem_section_usage_size(void);
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321#define SECTION_MARKED_PRESENT (1UL<<0)
1322#define SECTION_HAS_MEM_MAP (1UL<<1)
1323#define SECTION_IS_ONLINE (1UL<<2)
1324#define SECTION_IS_EARLY (1UL<<3)
1325#define SECTION_TAINT_ZONE_DEVICE (1UL<<4)
1326#define SECTION_MAP_LAST_BIT (1UL<<5)
1327#define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
1328#define SECTION_NID_SHIFT 3
1329
1330static inline struct page *__section_mem_map_addr(struct mem_section *section)
1331{
1332 unsigned long map = section->section_mem_map;
1333 map &= SECTION_MAP_MASK;
1334 return (struct page *)map;
1335}
1336
1337static inline int present_section(struct mem_section *section)
1338{
1339 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
1340}
1341
1342static inline int present_section_nr(unsigned long nr)
1343{
1344 return present_section(__nr_to_section(nr));
1345}
1346
1347static inline int valid_section(struct mem_section *section)
1348{
1349 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
1350}
1351
1352static inline int early_section(struct mem_section *section)
1353{
1354 return (section && (section->section_mem_map & SECTION_IS_EARLY));
1355}
1356
1357static inline int valid_section_nr(unsigned long nr)
1358{
1359 return valid_section(__nr_to_section(nr));
1360}
1361
1362static inline int online_section(struct mem_section *section)
1363{
1364 return (section && (section->section_mem_map & SECTION_IS_ONLINE));
1365}
1366
1367static inline int online_device_section(struct mem_section *section)
1368{
1369 unsigned long flags = SECTION_IS_ONLINE | SECTION_TAINT_ZONE_DEVICE;
1370
1371 return section && ((section->section_mem_map & flags) == flags);
1372}
1373
1374static inline int online_section_nr(unsigned long nr)
1375{
1376 return online_section(__nr_to_section(nr));
1377}
1378
1379#ifdef CONFIG_MEMORY_HOTPLUG
1380void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1381#ifdef CONFIG_MEMORY_HOTREMOVE
1382void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
1383#endif
1384#endif
1385
1386static inline struct mem_section *__pfn_to_section(unsigned long pfn)
1387{
1388 return __nr_to_section(pfn_to_section_nr(pfn));
1389}
1390
1391extern unsigned long __highest_present_section_nr;
1392
1393static inline int subsection_map_index(unsigned long pfn)
1394{
1395 return (pfn & ~(PAGE_SECTION_MASK)) / PAGES_PER_SUBSECTION;
1396}
1397
1398#ifdef CONFIG_SPARSEMEM_VMEMMAP
1399static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1400{
1401 int idx = subsection_map_index(pfn);
1402
1403 return test_bit(idx, ms->usage->subsection_map);
1404}
1405#else
1406static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
1407{
1408 return 1;
1409}
1410#endif
1411
1412#ifndef CONFIG_HAVE_ARCH_PFN_VALID
1413static inline int pfn_valid(unsigned long pfn)
1414{
1415 struct mem_section *ms;
1416
1417 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1418 return 0;
1419 ms = __nr_to_section(pfn_to_section_nr(pfn));
1420 if (!valid_section(ms))
1421 return 0;
1422
1423
1424
1425
1426 return early_section(ms) || pfn_section_valid(ms, pfn);
1427}
1428#endif
1429
1430static inline int pfn_in_present_section(unsigned long pfn)
1431{
1432 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
1433 return 0;
1434 return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
1435}
1436
1437static inline unsigned long next_present_section_nr(unsigned long section_nr)
1438{
1439 while (++section_nr <= __highest_present_section_nr) {
1440 if (present_section_nr(section_nr))
1441 return section_nr;
1442 }
1443
1444 return -1;
1445}
1446
1447
1448
1449
1450
1451
1452#ifdef CONFIG_NUMA
1453#define pfn_to_nid(pfn) \
1454({ \
1455 unsigned long __pfn_to_nid_pfn = (pfn); \
1456 page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
1457})
1458#else
1459#define pfn_to_nid(pfn) (0)
1460#endif
1461
1462void sparse_init(void);
1463#else
1464#define sparse_init() do {} while (0)
1465#define sparse_index_init(_sec, _nid) do {} while (0)
1466#define pfn_in_present_section pfn_valid
1467#define subsection_map_init(_pfn, _nr_pages) do {} while (0)
1468#endif
1469
1470
1471
1472
1473
1474
1475
1476#ifdef CONFIG_HOLES_IN_ZONE
1477#define pfn_valid_within(pfn) pfn_valid(pfn)
1478#else
1479#define pfn_valid_within(pfn) (1)
1480#endif
1481
1482#endif
1483#endif
1484#endif
1485