mmzone.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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. #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. #ifdef CONFIG_NUMA_BALANCING
  593. /* Lock serializing the migrate rate limiting window */
  594. spinlock_t numabalancing_migrate_lock;
  595. /* Rate limiting time interval */
  596. unsigned long numabalancing_migrate_next_window;
  597. /* Number of pages migrated during the rate limiting time interval */
  598. unsigned long numabalancing_migrate_nr_pages;
  599. #endif
  600. /*
  601. * This is a per-node reserve of pages that are not available
  602. * to userspace allocations.
  603. */
  604. unsigned long totalreserve_pages;
  605. #ifdef CONFIG_NUMA
  606. /*
  607. * zone reclaim becomes active if more unmapped pages exist.
  608. */
  609. unsigned long min_unmapped_pages;
  610. unsigned long min_slab_pages;
  611. #endif /* CONFIG_NUMA */
  612. /* Write-intensive fields used by page reclaim */
  613. ZONE_PADDING(_pad1_)
  614. spinlock_t lru_lock;
  615. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  616. /*
  617. * If memory initialisation on large machines is deferred then this
  618. * is the first PFN that needs to be initialised.
  619. */
  620. unsigned long first_deferred_pfn;
  621. /* Number of non-deferred pages */
  622. unsigned long static_init_pgcnt;
  623. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  624. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  625. spinlock_t split_queue_lock;
  626. struct list_head split_queue;
  627. unsigned long split_queue_len;
  628. #endif
  629. /* Fields commonly accessed by the page reclaim scanner */
  630. struct lruvec lruvec;
  631. unsigned long flags;
  632. ZONE_PADDING(_pad2_)
  633. /* Per-node vmstats */
  634. struct per_cpu_nodestat __percpu *per_cpu_nodestats;
  635. atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS];
  636. } pg_data_t;
  637. #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
  638. #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
  639. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  640. #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
  641. #else
  642. #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
  643. #endif
  644. #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
  645. #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn)
  646. #define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid))
  647. static inline spinlock_t *zone_lru_lock(struct zone *zone)
  648. {
  649. return &zone->zone_pgdat->lru_lock;
  650. }
  651. static inline struct lruvec *node_lruvec(struct pglist_data *pgdat)
  652. {
  653. return &pgdat->lruvec;
  654. }
  655. static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
  656. {
  657. return pgdat->node_start_pfn + pgdat->node_spanned_pages;
  658. }
  659. static inline bool pgdat_is_empty(pg_data_t *pgdat)
  660. {
  661. return !pgdat->node_start_pfn && !pgdat->node_spanned_pages;
  662. }
  663. static inline int zone_id(const struct zone *zone)
  664. {
  665. struct pglist_data *pgdat = zone->zone_pgdat;
  666. return zone - pgdat->node_zones;
  667. }
  668. #ifdef CONFIG_ZONE_DEVICE
  669. static inline bool is_dev_zone(const struct zone *zone)
  670. {
  671. return zone_id(zone) == ZONE_DEVICE;
  672. }
  673. #else
  674. static inline bool is_dev_zone(const struct zone *zone)
  675. {
  676. return false;
  677. }
  678. #endif
  679. #include <linux/memory_hotplug.h>
  680. void build_all_zonelists(pg_data_t *pgdat);
  681. void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
  682. enum zone_type classzone_idx);
  683. bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
  684. int classzone_idx, unsigned int alloc_flags,
  685. long free_pages);
  686. bool zone_watermark_ok(struct zone *z, unsigned int order,
  687. unsigned long mark, int classzone_idx,
  688. unsigned int alloc_flags);
  689. bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
  690. unsigned long mark, int classzone_idx);
  691. enum memmap_context {
  692. MEMMAP_EARLY,
  693. MEMMAP_HOTPLUG,
  694. };
  695. extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
  696. unsigned long size);
  697. extern void lruvec_init(struct lruvec *lruvec);
  698. static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec)
  699. {
  700. #ifdef CONFIG_MEMCG
  701. return lruvec->pgdat;
  702. #else
  703. return container_of(lruvec, struct pglist_data, lruvec);
  704. #endif
  705. }
  706. extern unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx);
  707. #ifdef CONFIG_HAVE_MEMORY_PRESENT
  708. void memory_present(int nid, unsigned long start, unsigned long end);
  709. #else
  710. static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
  711. #endif
  712. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  713. int local_memory_node(int node_id);
  714. #else
  715. static inline int local_memory_node(int node_id) { return node_id; };
  716. #endif
  717. /*
  718. * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
  719. */
  720. #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
  721. /*
  722. * Returns true if a zone has pages managed by the buddy allocator.
  723. * All the reclaim decisions have to use this function rather than
  724. * populated_zone(). If the whole zone is reserved then we can easily
  725. * end up with populated_zone() && !managed_zone().
  726. */
  727. static inline bool managed_zone(struct zone *zone)
  728. {
  729. return zone->managed_pages;
  730. }
  731. /* Returns true if a zone has memory */
  732. static inline bool populated_zone(struct zone *zone)
  733. {
  734. return zone->present_pages;
  735. }
  736. extern int movable_zone;
  737. #ifdef CONFIG_HIGHMEM
  738. static inline int zone_movable_is_highmem(void)
  739. {
  740. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  741. return movable_zone == ZONE_HIGHMEM;
  742. #else
  743. return (ZONE_MOVABLE - 1) == ZONE_HIGHMEM;
  744. #endif
  745. }
  746. #endif
  747. static inline int is_highmem_idx(enum zone_type idx)
  748. {
  749. #ifdef CONFIG_HIGHMEM
  750. return (idx == ZONE_HIGHMEM ||
  751. (idx == ZONE_MOVABLE && zone_movable_is_highmem()));
  752. #else
  753. return 0;
  754. #endif
  755. }
  756. /**
  757. * is_highmem - helper function to quickly check if a struct zone is a
  758. * highmem zone or not. This is an attempt to keep references
  759. * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
  760. * @zone - pointer to struct zone variable
  761. */
  762. static inline int is_highmem(struct zone *zone)
  763. {
  764. #ifdef CONFIG_HIGHMEM
  765. return is_highmem_idx(zone_idx(zone));
  766. #else
  767. return 0;
  768. #endif
  769. }
  770. /* These two functions are used to setup the per zone pages min values */
  771. struct ctl_table;
  772. int min_free_kbytes_sysctl_handler(struct ctl_table *, int,
  773. void __user *, size_t *, loff_t *);
  774. int watermark_scale_factor_sysctl_handler(struct ctl_table *, int,
  775. void __user *, size_t *, loff_t *);
  776. extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
  777. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int,
  778. void __user *, size_t *, loff_t *);
  779. int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int,
  780. void __user *, size_t *, loff_t *);
  781. int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
  782. void __user *, size_t *, loff_t *);
  783. int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
  784. void __user *, size_t *, loff_t *);
  785. extern int numa_zonelist_order_handler(struct ctl_table *, int,
  786. void __user *, size_t *, loff_t *);
  787. extern char numa_zonelist_order[];
  788. #define NUMA_ZONELIST_ORDER_LEN 16
  789. #ifndef CONFIG_NEED_MULTIPLE_NODES
  790. extern struct pglist_data contig_page_data;
  791. #define NODE_DATA(nid) (&contig_page_data)
  792. #define NODE_MEM_MAP(nid) mem_map
  793. #else /* CONFIG_NEED_MULTIPLE_NODES */
  794. #include <asm/mmzone.h>
  795. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  796. extern struct pglist_data *first_online_pgdat(void);
  797. extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
  798. extern struct zone *next_zone(struct zone *zone);
  799. /**
  800. * for_each_online_pgdat - helper macro to iterate over all online nodes
  801. * @pgdat - pointer to a pg_data_t variable
  802. */
  803. #define for_each_online_pgdat(pgdat) \
  804. for (pgdat = first_online_pgdat(); \
  805. pgdat; \
  806. pgdat = next_online_pgdat(pgdat))
  807. /**
  808. * for_each_zone - helper macro to iterate over all memory zones
  809. * @zone - pointer to struct zone variable
  810. *
  811. * The user only needs to declare the zone variable, for_each_zone
  812. * fills it in.
  813. */
  814. #define for_each_zone(zone) \
  815. for (zone = (first_online_pgdat())->node_zones; \
  816. zone; \
  817. zone = next_zone(zone))
  818. #define for_each_populated_zone(zone) \
  819. for (zone = (first_online_pgdat())->node_zones; \
  820. zone; \
  821. zone = next_zone(zone)) \
  822. if (!populated_zone(zone)) \
  823. ; /* do nothing */ \
  824. else
  825. static inline struct zone *zonelist_zone(struct zoneref *zoneref)
  826. {
  827. return zoneref->zone;
  828. }
  829. static inline int zonelist_zone_idx(struct zoneref *zoneref)
  830. {
  831. return zoneref->zone_idx;
  832. }
  833. static inline int zonelist_node_idx(struct zoneref *zoneref)
  834. {
  835. #ifdef CONFIG_NUMA
  836. /* zone_to_nid not available in this context */
  837. return zoneref->zone->node;
  838. #else
  839. return 0;
  840. #endif /* CONFIG_NUMA */
  841. }
  842. struct zoneref *__next_zones_zonelist(struct zoneref *z,
  843. enum zone_type highest_zoneidx,
  844. nodemask_t *nodes);
  845. /**
  846. * 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
  847. * @z - The cursor used as a starting point for the search
  848. * @highest_zoneidx - The zone index of the highest zone to return
  849. * @nodes - An optional nodemask to filter the zonelist with
  850. *
  851. * This function returns the next zone at or below a given zone index that is
  852. * within the allowed nodemask using a cursor as the starting point for the
  853. * search. The zoneref returned is a cursor that represents the current zone
  854. * being examined. It should be advanced by one before calling
  855. * next_zones_zonelist again.
  856. */
  857. static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z,
  858. enum zone_type highest_zoneidx,
  859. nodemask_t *nodes)
  860. {
  861. if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx))
  862. return z;
  863. return __next_zones_zonelist(z, highest_zoneidx, nodes);
  864. }
  865. /**
  866. * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
  867. * @zonelist - The zonelist to search for a suitable zone
  868. * @highest_zoneidx - The zone index of the highest zone to return
  869. * @nodes - An optional nodemask to filter the zonelist with
  870. * @return - Zoneref pointer for the first suitable zone found (see below)
  871. *
  872. * This function returns the first zone at or below a given zone index that is
  873. * within the allowed nodemask. The zoneref returned is a cursor that can be
  874. * used to iterate the zonelist with next_zones_zonelist by advancing it by
  875. * one before calling.
  876. *
  877. * When no eligible zone is found, zoneref->zone is NULL (zoneref itself is
  878. * never NULL). This may happen either genuinely, or due to concurrent nodemask
  879. * update due to cpuset modification.
  880. */
  881. static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
  882. enum zone_type highest_zoneidx,
  883. nodemask_t *nodes)
  884. {
  885. return next_zones_zonelist(zonelist->_zonerefs,
  886. highest_zoneidx, nodes);
  887. }
  888. /**
  889. * 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
  890. * @zone - The current zone in the iterator
  891. * @z - The current pointer within zonelist->zones being iterated
  892. * @zlist - The zonelist being iterated
  893. * @highidx - The zone index of the highest zone to return
  894. * @nodemask - Nodemask allowed by the allocator
  895. *
  896. * This iterator iterates though all zones at or below a given zone index and
  897. * within a given nodemask
  898. */
  899. #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
  900. for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \
  901. zone; \
  902. z = next_zones_zonelist(++z, highidx, nodemask), \
  903. zone = zonelist_zone(z))
  904. #define for_next_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
  905. for (zone = z->zone; \
  906. zone; \
  907. z = next_zones_zonelist(++z, highidx, nodemask), \
  908. zone = zonelist_zone(z))
  909. /**
  910. * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
  911. * @zone - The current zone in the iterator
  912. * @z - The current pointer within zonelist->zones being iterated
  913. * @zlist - The zonelist being iterated
  914. * @highidx - The zone index of the highest zone to return
  915. *
  916. * This iterator iterates though all zones at or below a given zone index.
  917. */
  918. #define for_each_zone_zonelist(zone, z, zlist, highidx) \
  919. for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
  920. #ifdef CONFIG_SPARSEMEM
  921. #include <asm/sparsemem.h>
  922. #endif
  923. #if !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) && \
  924. !defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
  925. static inline unsigned long early_pfn_to_nid(unsigned long pfn)
  926. {
  927. BUILD_BUG_ON(IS_ENABLED(CONFIG_NUMA));
  928. return 0;
  929. }
  930. #endif
  931. #ifdef CONFIG_FLATMEM
  932. #define pfn_to_nid(pfn) (0)
  933. #endif
  934. #ifdef CONFIG_SPARSEMEM
  935. /*
  936. * SECTION_SHIFT #bits space required to store a section #
  937. *
  938. * PA_SECTION_SHIFT physical address to/from section number
  939. * PFN_SECTION_SHIFT pfn to/from section number
  940. */
  941. #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
  942. #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
  943. #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
  944. #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
  945. #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
  946. #define SECTION_BLOCKFLAGS_BITS \
  947. ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
  948. #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
  949. #error Allocator MAX_ORDER exceeds SECTION_SIZE
  950. #endif
  951. static inline unsigned long pfn_to_section_nr(unsigned long pfn)
  952. {
  953. return pfn >> PFN_SECTION_SHIFT;
  954. }
  955. static inline unsigned long section_nr_to_pfn(unsigned long sec)
  956. {
  957. return sec << PFN_SECTION_SHIFT;
  958. }
  959. #define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK)
  960. #define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK)
  961. struct page;
  962. struct page_ext;
  963. struct mem_section {
  964. /*
  965. * This is, logically, a pointer to an array of struct
  966. * pages. However, it is stored with some other magic.
  967. * (see sparse.c::sparse_init_one_section())
  968. *
  969. * Additionally during early boot we encode node id of
  970. * the location of the section here to guide allocation.
  971. * (see sparse.c::memory_present())
  972. *
  973. * Making it a UL at least makes someone do a cast
  974. * before using it wrong.
  975. */
  976. unsigned long section_mem_map;
  977. /* See declaration of similar field in struct zone */
  978. unsigned long *pageblock_flags;
  979. #ifdef CONFIG_PAGE_EXTENSION
  980. /*
  981. * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use
  982. * section. (see page_ext.h about this.)
  983. */
  984. struct page_ext *page_ext;
  985. unsigned long pad;
  986. #endif
  987. /*
  988. * WARNING: mem_section must be a power-of-2 in size for the
  989. * calculation and use of SECTION_ROOT_MASK to make sense.
  990. */
  991. };
  992. #ifdef CONFIG_SPARSEMEM_EXTREME
  993. #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
  994. #else
  995. #define SECTIONS_PER_ROOT 1
  996. #endif
  997. #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
  998. #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT)
  999. #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
  1000. #ifdef CONFIG_SPARSEMEM_EXTREME
  1001. extern struct mem_section **mem_section;
  1002. #else
  1003. extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
  1004. #endif
  1005. static inline struct mem_section *__nr_to_section(unsigned long nr)
  1006. {
  1007. #ifdef CONFIG_SPARSEMEM_EXTREME
  1008. if (!mem_section)
  1009. return NULL;
  1010. #endif
  1011. if (!mem_section[SECTION_NR_TO_ROOT(nr)])
  1012. return NULL;
  1013. return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
  1014. }
  1015. extern int __section_nr(struct mem_section* ms);
  1016. extern unsigned long usemap_size(void);
  1017. /*
  1018. * We use the lower bits of the mem_map pointer to store
  1019. * a little bit of information. The pointer is calculated
  1020. * as mem_map - section_nr_to_pfn(pnum). The result is
  1021. * aligned to the minimum alignment of the two values:
  1022. * 1. All mem_map arrays are page-aligned.
  1023. * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT
  1024. * lowest bits. PFN_SECTION_SHIFT is arch-specific
  1025. * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the
  1026. * worst combination is powerpc with 256k pages,
  1027. * which results in PFN_SECTION_SHIFT equal 6.
  1028. * To sum it up, at least 6 bits are available.
  1029. */
  1030. #define SECTION_MARKED_PRESENT (1UL<<0)
  1031. #define SECTION_HAS_MEM_MAP (1UL<<1)
  1032. #define SECTION_IS_ONLINE (1UL<<2)
  1033. #define SECTION_MAP_LAST_BIT (1UL<<3)
  1034. #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
  1035. #define SECTION_NID_SHIFT 3
  1036. static inline struct page *__section_mem_map_addr(struct mem_section *section)
  1037. {
  1038. unsigned long map = section->section_mem_map;
  1039. map &= SECTION_MAP_MASK;
  1040. return (struct page *)map;
  1041. }
  1042. static inline int present_section(struct mem_section *section)
  1043. {
  1044. return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
  1045. }
  1046. static inline int present_section_nr(unsigned long nr)
  1047. {
  1048. return present_section(__nr_to_section(nr));
  1049. }
  1050. static inline int valid_section(struct mem_section *section)
  1051. {
  1052. return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
  1053. }
  1054. static inline int valid_section_nr(unsigned long nr)
  1055. {
  1056. return valid_section(__nr_to_section(nr));
  1057. }
  1058. static inline int online_section(struct mem_section *section)
  1059. {
  1060. return (section && (section->section_mem_map & SECTION_IS_ONLINE));
  1061. }
  1062. static inline int online_section_nr(unsigned long nr)
  1063. {
  1064. return online_section(__nr_to_section(nr));
  1065. }
  1066. #ifdef CONFIG_MEMORY_HOTPLUG
  1067. void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
  1068. #ifdef CONFIG_MEMORY_HOTREMOVE
  1069. void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
  1070. #endif
  1071. #endif
  1072. static inline struct mem_section *__pfn_to_section(unsigned long pfn)
  1073. {
  1074. return __nr_to_section(pfn_to_section_nr(pfn));
  1075. }
  1076. extern int __highest_present_section_nr;
  1077. #ifndef CONFIG_HAVE_ARCH_PFN_VALID
  1078. static inline int pfn_valid(unsigned long pfn)
  1079. {
  1080. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  1081. return 0;
  1082. return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
  1083. }
  1084. #endif
  1085. static inline int pfn_present(unsigned long pfn)
  1086. {
  1087. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  1088. return 0;
  1089. return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
  1090. }
  1091. /*
  1092. * These are _only_ used during initialisation, therefore they
  1093. * can use __initdata ... They could have names to indicate
  1094. * this restriction.
  1095. */
  1096. #ifdef CONFIG_NUMA
  1097. #define pfn_to_nid(pfn) \
  1098. ({ \
  1099. unsigned long __pfn_to_nid_pfn = (pfn); \
  1100. page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
  1101. })
  1102. #else
  1103. #define pfn_to_nid(pfn) (0)
  1104. #endif
  1105. #define early_pfn_valid(pfn) pfn_valid(pfn)
  1106. void sparse_init(void);
  1107. #else
  1108. #define sparse_init() do {} while (0)
  1109. #define sparse_index_init(_sec, _nid) do {} while (0)
  1110. #endif /* CONFIG_SPARSEMEM */
  1111. /*
  1112. * During memory init memblocks map pfns to nids. The search is expensive and
  1113. * this caches recent lookups. The implementation of __early_pfn_to_nid
  1114. * may treat start/end as pfns or sections.
  1115. */
  1116. struct mminit_pfnnid_cache {
  1117. unsigned long last_start;
  1118. unsigned long last_end;
  1119. int last_nid;
  1120. };
  1121. #ifndef early_pfn_valid
  1122. #define early_pfn_valid(pfn) (1)
  1123. #endif
  1124. void memory_present(int nid, unsigned long start, unsigned long end);
  1125. /*
  1126. * If it is possible to have holes within a MAX_ORDER_NR_PAGES, then we
  1127. * need to check pfn validility within that MAX_ORDER_NR_PAGES block.
  1128. * pfn_valid_within() should be used in this case; we optimise this away
  1129. * when we have no holes within a MAX_ORDER_NR_PAGES block.
  1130. */
  1131. #ifdef CONFIG_HOLES_IN_ZONE
  1132. #define pfn_valid_within(pfn) pfn_valid(pfn)
  1133. #else
  1134. #define pfn_valid_within(pfn) (1)
  1135. #endif
  1136. #ifdef CONFIG_ARCH_HAS_HOLES_MEMORYMODEL
  1137. /*
  1138. * pfn_valid() is meant to be able to tell if a given PFN has valid memmap
  1139. * associated with it or not. This means that a struct page exists for this
  1140. * pfn. The caller cannot assume the page is fully initialized in general.
  1141. * Hotplugable pages might not have been onlined yet. pfn_to_online_page()
  1142. * will ensure the struct page is fully online and initialized. Special pages
  1143. * (e.g. ZONE_DEVICE) are never onlined and should be treated accordingly.
  1144. *
  1145. * In FLATMEM, it is expected that holes always have valid memmap as long as
  1146. * there is valid PFNs either side of the hole. In SPARSEMEM, it is assumed
  1147. * that a valid section has a memmap for the entire section.
  1148. *
  1149. * However, an ARM, and maybe other embedded architectures in the future
  1150. * free memmap backing holes to save memory on the assumption the memmap is
  1151. * never used. The page_zone linkages are then broken even though pfn_valid()
  1152. * returns true. A walker of the full memmap must then do this additional
  1153. * check to ensure the memmap they are looking at is sane by making sure
  1154. * the zone and PFN linkages are still valid. This is expensive, but walkers
  1155. * of the full memmap are extremely rare.
  1156. */
  1157. bool memmap_valid_within(unsigned long pfn,
  1158. struct page *page, struct zone *zone);
  1159. #else
  1160. static inline bool memmap_valid_within(unsigned long pfn,
  1161. struct page *page, struct zone *zone)
  1162. {
  1163. return true;
  1164. }
  1165. #endif /* CONFIG_ARCH_HAS_HOLES_MEMORYMODEL */
  1166. #endif /* !__GENERATING_BOUNDS.H */
  1167. #endif /* !__ASSEMBLY__ */
  1168. #endif /* _LINUX_MMZONE_H */