mmzone.h 41 KB

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