mmzone.h 40 KB

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