mmzone.h 33 KB

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