mmzone.h 35 KB

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