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