mmzone.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. ZONE_PADDING(_pad2_)
  290. /* Rarely used or read-mostly fields */
  291. /*
  292. * wait_table -- the array holding the hash table
  293. * wait_table_hash_nr_entries -- the size of the hash table array
  294. * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
  295. *
  296. * The purpose of all these is to keep track of the people
  297. * waiting for a page to become available and make them
  298. * runnable again when possible. The trouble is that this
  299. * consumes a lot of space, especially when so few things
  300. * wait on pages at a given time. So instead of using
  301. * per-page waitqueues, we use a waitqueue hash table.
  302. *
  303. * The bucket discipline is to sleep on the same queue when
  304. * colliding and wake all in that wait queue when removing.
  305. * When something wakes, it must check to be sure its page is
  306. * truly available, a la thundering herd. The cost of a
  307. * collision is great, but given the expected load of the
  308. * table, they should be so rare as to be outweighed by the
  309. * benefits from the saved space.
  310. *
  311. * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
  312. * primary users of these fields, and in mm/page_alloc.c
  313. * free_area_init_core() performs the initialization of them.
  314. */
  315. wait_queue_head_t * wait_table;
  316. unsigned long wait_table_hash_nr_entries;
  317. unsigned long wait_table_bits;
  318. /*
  319. * Discontig memory support fields.
  320. */
  321. struct pglist_data *zone_pgdat;
  322. /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
  323. unsigned long zone_start_pfn;
  324. /*
  325. * zone_start_pfn, spanned_pages and present_pages are all
  326. * protected by span_seqlock. It is a seqlock because it has
  327. * to be read outside of zone->lock, and it is done in the main
  328. * allocator path. But, it is written quite infrequently.
  329. *
  330. * The lock is declared along with zone->lock because it is
  331. * frequently read in proximity to zone->lock. It's good to
  332. * give them a chance of being in the same cacheline.
  333. */
  334. unsigned long spanned_pages; /* total size, including holes */
  335. unsigned long present_pages; /* amount of memory (excluding holes) */
  336. /*
  337. * rarely used fields:
  338. */
  339. const char *name;
  340. } ____cacheline_internodealigned_in_smp;
  341. typedef enum {
  342. ZONE_ALL_UNRECLAIMABLE, /* all pages pinned */
  343. ZONE_RECLAIM_LOCKED, /* prevents concurrent reclaim */
  344. ZONE_OOM_LOCKED, /* zone is in OOM killer zonelist */
  345. } zone_flags_t;
  346. static inline void zone_set_flag(struct zone *zone, zone_flags_t flag)
  347. {
  348. set_bit(flag, &zone->flags);
  349. }
  350. static inline int zone_test_and_set_flag(struct zone *zone, zone_flags_t flag)
  351. {
  352. return test_and_set_bit(flag, &zone->flags);
  353. }
  354. static inline void zone_clear_flag(struct zone *zone, zone_flags_t flag)
  355. {
  356. clear_bit(flag, &zone->flags);
  357. }
  358. static inline int zone_is_all_unreclaimable(const struct zone *zone)
  359. {
  360. return test_bit(ZONE_ALL_UNRECLAIMABLE, &zone->flags);
  361. }
  362. static inline int zone_is_reclaim_locked(const struct zone *zone)
  363. {
  364. return test_bit(ZONE_RECLAIM_LOCKED, &zone->flags);
  365. }
  366. static inline int zone_is_oom_locked(const struct zone *zone)
  367. {
  368. return test_bit(ZONE_OOM_LOCKED, &zone->flags);
  369. }
  370. /*
  371. * The "priority" of VM scanning is how much of the queues we will scan in one
  372. * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
  373. * queues ("queue_length >> 12") during an aging round.
  374. */
  375. #define DEF_PRIORITY 12
  376. /* Maximum number of zones on a zonelist */
  377. #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
  378. #ifdef CONFIG_NUMA
  379. /*
  380. * The NUMA zonelists are doubled becausse we need zonelists that restrict the
  381. * allocations to a single node for GFP_THISNODE.
  382. *
  383. * [0] : Zonelist with fallback
  384. * [1] : No fallback (GFP_THISNODE)
  385. */
  386. #define MAX_ZONELISTS 2
  387. /*
  388. * We cache key information from each zonelist for smaller cache
  389. * footprint when scanning for free pages in get_page_from_freelist().
  390. *
  391. * 1) The BITMAP fullzones tracks which zones in a zonelist have come
  392. * up short of free memory since the last time (last_fullzone_zap)
  393. * we zero'd fullzones.
  394. * 2) The array z_to_n[] maps each zone in the zonelist to its node
  395. * id, so that we can efficiently evaluate whether that node is
  396. * set in the current tasks mems_allowed.
  397. *
  398. * Both fullzones and z_to_n[] are one-to-one with the zonelist,
  399. * indexed by a zones offset in the zonelist zones[] array.
  400. *
  401. * The get_page_from_freelist() routine does two scans. During the
  402. * first scan, we skip zones whose corresponding bit in 'fullzones'
  403. * is set or whose corresponding node in current->mems_allowed (which
  404. * comes from cpusets) is not set. During the second scan, we bypass
  405. * this zonelist_cache, to ensure we look methodically at each zone.
  406. *
  407. * Once per second, we zero out (zap) fullzones, forcing us to
  408. * reconsider nodes that might have regained more free memory.
  409. * The field last_full_zap is the time we last zapped fullzones.
  410. *
  411. * This mechanism reduces the amount of time we waste repeatedly
  412. * reexaming zones for free memory when they just came up low on
  413. * memory momentarilly ago.
  414. *
  415. * The zonelist_cache struct members logically belong in struct
  416. * zonelist. However, the mempolicy zonelists constructed for
  417. * MPOL_BIND are intentionally variable length (and usually much
  418. * shorter). A general purpose mechanism for handling structs with
  419. * multiple variable length members is more mechanism than we want
  420. * here. We resort to some special case hackery instead.
  421. *
  422. * The MPOL_BIND zonelists don't need this zonelist_cache (in good
  423. * part because they are shorter), so we put the fixed length stuff
  424. * at the front of the zonelist struct, ending in a variable length
  425. * zones[], as is needed by MPOL_BIND.
  426. *
  427. * Then we put the optional zonelist cache on the end of the zonelist
  428. * struct. This optional stuff is found by a 'zlcache_ptr' pointer in
  429. * the fixed length portion at the front of the struct. This pointer
  430. * both enables us to find the zonelist cache, and in the case of
  431. * MPOL_BIND zonelists, (which will just set the zlcache_ptr to NULL)
  432. * to know that the zonelist cache is not there.
  433. *
  434. * The end result is that struct zonelists come in two flavors:
  435. * 1) The full, fixed length version, shown below, and
  436. * 2) The custom zonelists for MPOL_BIND.
  437. * The custom MPOL_BIND zonelists have a NULL zlcache_ptr and no zlcache.
  438. *
  439. * Even though there may be multiple CPU cores on a node modifying
  440. * fullzones or last_full_zap in the same zonelist_cache at the same
  441. * time, we don't lock it. This is just hint data - if it is wrong now
  442. * and then, the allocator will still function, perhaps a bit slower.
  443. */
  444. struct zonelist_cache {
  445. unsigned short z_to_n[MAX_ZONES_PER_ZONELIST]; /* zone->nid */
  446. DECLARE_BITMAP(fullzones, MAX_ZONES_PER_ZONELIST); /* zone full? */
  447. unsigned long last_full_zap; /* when last zap'd (jiffies) */
  448. };
  449. #else
  450. #define MAX_ZONELISTS 1
  451. struct zonelist_cache;
  452. #endif
  453. /*
  454. * This struct contains information about a zone in a zonelist. It is stored
  455. * here to avoid dereferences into large structures and lookups of tables
  456. */
  457. struct zoneref {
  458. struct zone *zone; /* Pointer to actual zone */
  459. int zone_idx; /* zone_idx(zoneref->zone) */
  460. };
  461. /*
  462. * One allocation request operates on a zonelist. A zonelist
  463. * is a list of zones, the first one is the 'goal' of the
  464. * allocation, the other zones are fallback zones, in decreasing
  465. * priority.
  466. *
  467. * If zlcache_ptr is not NULL, then it is just the address of zlcache,
  468. * as explained above. If zlcache_ptr is NULL, there is no zlcache.
  469. * *
  470. * To speed the reading of the zonelist, the zonerefs contain the zone index
  471. * of the entry being read. Helper functions to access information given
  472. * a struct zoneref are
  473. *
  474. * zonelist_zone() - Return the struct zone * for an entry in _zonerefs
  475. * zonelist_zone_idx() - Return the index of the zone for an entry
  476. * zonelist_node_idx() - Return the index of the node for an entry
  477. */
  478. struct zonelist {
  479. struct zonelist_cache *zlcache_ptr; // NULL or &zlcache
  480. struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
  481. #ifdef CONFIG_NUMA
  482. struct zonelist_cache zlcache; // optional ...
  483. #endif
  484. };
  485. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  486. struct node_active_region {
  487. unsigned long start_pfn;
  488. unsigned long end_pfn;
  489. int nid;
  490. };
  491. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  492. #ifndef CONFIG_DISCONTIGMEM
  493. /* The array of struct pages - for discontigmem use pgdat->lmem_map */
  494. extern struct page *mem_map;
  495. #endif
  496. /*
  497. * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
  498. * (mostly NUMA machines?) to denote a higher-level memory zone than the
  499. * zone denotes.
  500. *
  501. * On NUMA machines, each NUMA node would have a pg_data_t to describe
  502. * it's memory layout.
  503. *
  504. * Memory statistics and page replacement data structures are maintained on a
  505. * per-zone basis.
  506. */
  507. struct bootmem_data;
  508. typedef struct pglist_data {
  509. struct zone node_zones[MAX_NR_ZONES];
  510. struct zonelist node_zonelists[MAX_ZONELISTS];
  511. int nr_zones;
  512. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  513. struct page *node_mem_map;
  514. #endif
  515. struct bootmem_data *bdata;
  516. #ifdef CONFIG_MEMORY_HOTPLUG
  517. /*
  518. * Must be held any time you expect node_start_pfn, node_present_pages
  519. * or node_spanned_pages stay constant. Holding this will also
  520. * guarantee that any pfn_valid() stays that way.
  521. *
  522. * Nests above zone->lock and zone->size_seqlock.
  523. */
  524. spinlock_t node_size_lock;
  525. #endif
  526. unsigned long node_start_pfn;
  527. unsigned long node_present_pages; /* total number of physical pages */
  528. unsigned long node_spanned_pages; /* total size of physical page
  529. range, including holes */
  530. int node_id;
  531. wait_queue_head_t kswapd_wait;
  532. struct task_struct *kswapd;
  533. int kswapd_max_order;
  534. } pg_data_t;
  535. #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
  536. #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
  537. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  538. #define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
  539. #else
  540. #define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
  541. #endif
  542. #define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
  543. #include <linux/memory_hotplug.h>
  544. void get_zone_counts(unsigned long *active, unsigned long *inactive,
  545. unsigned long *free);
  546. void build_all_zonelists(void);
  547. void wakeup_kswapd(struct zone *zone, int order);
  548. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  549. int classzone_idx, int alloc_flags);
  550. enum memmap_context {
  551. MEMMAP_EARLY,
  552. MEMMAP_HOTPLUG,
  553. };
  554. extern int init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
  555. unsigned long size,
  556. enum memmap_context context);
  557. #ifdef CONFIG_HAVE_MEMORY_PRESENT
  558. void memory_present(int nid, unsigned long start, unsigned long end);
  559. #else
  560. static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
  561. #endif
  562. #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
  563. unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
  564. #endif
  565. /*
  566. * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
  567. */
  568. #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
  569. static inline int populated_zone(struct zone *zone)
  570. {
  571. return (!!zone->present_pages);
  572. }
  573. extern int movable_zone;
  574. static inline int zone_movable_is_highmem(void)
  575. {
  576. #if defined(CONFIG_HIGHMEM) && defined(CONFIG_ARCH_POPULATES_NODE_MAP)
  577. return movable_zone == ZONE_HIGHMEM;
  578. #else
  579. return 0;
  580. #endif
  581. }
  582. static inline int is_highmem_idx(enum zone_type idx)
  583. {
  584. #ifdef CONFIG_HIGHMEM
  585. return (idx == ZONE_HIGHMEM ||
  586. (idx == ZONE_MOVABLE && zone_movable_is_highmem()));
  587. #else
  588. return 0;
  589. #endif
  590. }
  591. static inline int is_normal_idx(enum zone_type idx)
  592. {
  593. return (idx == ZONE_NORMAL);
  594. }
  595. /**
  596. * is_highmem - helper function to quickly check if a struct zone is a
  597. * highmem zone or not. This is an attempt to keep references
  598. * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
  599. * @zone - pointer to struct zone variable
  600. */
  601. static inline int is_highmem(struct zone *zone)
  602. {
  603. #ifdef CONFIG_HIGHMEM
  604. int zone_off = (char *)zone - (char *)zone->zone_pgdat->node_zones;
  605. return zone_off == ZONE_HIGHMEM * sizeof(*zone) ||
  606. (zone_off == ZONE_MOVABLE * sizeof(*zone) &&
  607. zone_movable_is_highmem());
  608. #else
  609. return 0;
  610. #endif
  611. }
  612. static inline int is_normal(struct zone *zone)
  613. {
  614. return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
  615. }
  616. static inline int is_dma32(struct zone *zone)
  617. {
  618. #ifdef CONFIG_ZONE_DMA32
  619. return zone == zone->zone_pgdat->node_zones + ZONE_DMA32;
  620. #else
  621. return 0;
  622. #endif
  623. }
  624. static inline int is_dma(struct zone *zone)
  625. {
  626. #ifdef CONFIG_ZONE_DMA
  627. return zone == zone->zone_pgdat->node_zones + ZONE_DMA;
  628. #else
  629. return 0;
  630. #endif
  631. }
  632. /* These two functions are used to setup the per zone pages min values */
  633. struct ctl_table;
  634. struct file;
  635. int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
  636. void __user *, size_t *, loff_t *);
  637. extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
  638. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
  639. void __user *, size_t *, loff_t *);
  640. int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file *,
  641. void __user *, size_t *, loff_t *);
  642. int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
  643. struct file *, void __user *, size_t *, loff_t *);
  644. int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
  645. struct file *, void __user *, size_t *, loff_t *);
  646. extern int numa_zonelist_order_handler(struct ctl_table *, int,
  647. struct file *, void __user *, size_t *, loff_t *);
  648. extern char numa_zonelist_order[];
  649. #define NUMA_ZONELIST_ORDER_LEN 16 /* string buffer size */
  650. #include <linux/topology.h>
  651. /* Returns the number of the current Node. */
  652. #ifndef numa_node_id
  653. #define numa_node_id() (cpu_to_node(raw_smp_processor_id()))
  654. #endif
  655. #ifndef CONFIG_NEED_MULTIPLE_NODES
  656. extern struct pglist_data contig_page_data;
  657. #define NODE_DATA(nid) (&contig_page_data)
  658. #define NODE_MEM_MAP(nid) mem_map
  659. #else /* CONFIG_NEED_MULTIPLE_NODES */
  660. #include <asm/mmzone.h>
  661. #endif /* !CONFIG_NEED_MULTIPLE_NODES */
  662. extern struct pglist_data *first_online_pgdat(void);
  663. extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
  664. extern struct zone *next_zone(struct zone *zone);
  665. /**
  666. * for_each_online_pgdat - helper macro to iterate over all online nodes
  667. * @pgdat - pointer to a pg_data_t variable
  668. */
  669. #define for_each_online_pgdat(pgdat) \
  670. for (pgdat = first_online_pgdat(); \
  671. pgdat; \
  672. pgdat = next_online_pgdat(pgdat))
  673. /**
  674. * for_each_zone - helper macro to iterate over all memory zones
  675. * @zone - pointer to struct zone variable
  676. *
  677. * The user only needs to declare the zone variable, for_each_zone
  678. * fills it in.
  679. */
  680. #define for_each_zone(zone) \
  681. for (zone = (first_online_pgdat())->node_zones; \
  682. zone; \
  683. zone = next_zone(zone))
  684. static inline struct zone *zonelist_zone(struct zoneref *zoneref)
  685. {
  686. return zoneref->zone;
  687. }
  688. static inline int zonelist_zone_idx(struct zoneref *zoneref)
  689. {
  690. return zoneref->zone_idx;
  691. }
  692. static inline int zonelist_node_idx(struct zoneref *zoneref)
  693. {
  694. #ifdef CONFIG_NUMA
  695. /* zone_to_nid not available in this context */
  696. return zoneref->zone->node;
  697. #else
  698. return 0;
  699. #endif /* CONFIG_NUMA */
  700. }
  701. /**
  702. * 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
  703. * @z - The cursor used as a starting point for the search
  704. * @highest_zoneidx - The zone index of the highest zone to return
  705. * @nodes - An optional nodemask to filter the zonelist with
  706. * @zone - The first suitable zone found is returned via this parameter
  707. *
  708. * This function returns the next zone at or below a given zone index that is
  709. * within the allowed nodemask using a cursor as the starting point for the
  710. * search. The zoneref returned is a cursor that represents the current zone
  711. * being examined. It should be advanced by one before calling
  712. * next_zones_zonelist again.
  713. */
  714. struct zoneref *next_zones_zonelist(struct zoneref *z,
  715. enum zone_type highest_zoneidx,
  716. nodemask_t *nodes,
  717. struct zone **zone);
  718. /**
  719. * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
  720. * @zonelist - The zonelist to search for a suitable zone
  721. * @highest_zoneidx - The zone index of the highest zone to return
  722. * @nodes - An optional nodemask to filter the zonelist with
  723. * @zone - The first suitable zone found is returned via this parameter
  724. *
  725. * This function returns the first zone at or below a given zone index that is
  726. * within the allowed nodemask. The zoneref returned is a cursor that can be
  727. * used to iterate the zonelist with next_zones_zonelist by advancing it by
  728. * one before calling.
  729. */
  730. static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
  731. enum zone_type highest_zoneidx,
  732. nodemask_t *nodes,
  733. struct zone **zone)
  734. {
  735. return next_zones_zonelist(zonelist->_zonerefs, highest_zoneidx, nodes,
  736. zone);
  737. }
  738. /**
  739. * 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
  740. * @zone - The current zone in the iterator
  741. * @z - The current pointer within zonelist->zones being iterated
  742. * @zlist - The zonelist being iterated
  743. * @highidx - The zone index of the highest zone to return
  744. * @nodemask - Nodemask allowed by the allocator
  745. *
  746. * This iterator iterates though all zones at or below a given zone index and
  747. * within a given nodemask
  748. */
  749. #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
  750. for (z = first_zones_zonelist(zlist, highidx, nodemask, &zone); \
  751. zone; \
  752. z = next_zones_zonelist(++z, highidx, nodemask, &zone)) \
  753. /**
  754. * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
  755. * @zone - The current zone in the iterator
  756. * @z - The current pointer within zonelist->zones being iterated
  757. * @zlist - The zonelist being iterated
  758. * @highidx - The zone index of the highest zone to return
  759. *
  760. * This iterator iterates though all zones at or below a given zone index.
  761. */
  762. #define for_each_zone_zonelist(zone, z, zlist, highidx) \
  763. for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
  764. #ifdef CONFIG_SPARSEMEM
  765. #include <asm/sparsemem.h>
  766. #endif
  767. #if !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) && \
  768. !defined(CONFIG_ARCH_POPULATES_NODE_MAP)
  769. static inline unsigned long early_pfn_to_nid(unsigned long pfn)
  770. {
  771. return 0;
  772. }
  773. #endif
  774. #ifdef CONFIG_FLATMEM
  775. #define pfn_to_nid(pfn) (0)
  776. #endif
  777. #define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
  778. #define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
  779. #ifdef CONFIG_SPARSEMEM
  780. /*
  781. * SECTION_SHIFT #bits space required to store a section #
  782. *
  783. * PA_SECTION_SHIFT physical address to/from section number
  784. * PFN_SECTION_SHIFT pfn to/from section number
  785. */
  786. #define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
  787. #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
  788. #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
  789. #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
  790. #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
  791. #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
  792. #define SECTION_BLOCKFLAGS_BITS \
  793. ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
  794. #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
  795. #error Allocator MAX_ORDER exceeds SECTION_SIZE
  796. #endif
  797. struct page;
  798. struct mem_section {
  799. /*
  800. * This is, logically, a pointer to an array of struct
  801. * pages. However, it is stored with some other magic.
  802. * (see sparse.c::sparse_init_one_section())
  803. *
  804. * Additionally during early boot we encode node id of
  805. * the location of the section here to guide allocation.
  806. * (see sparse.c::memory_present())
  807. *
  808. * Making it a UL at least makes someone do a cast
  809. * before using it wrong.
  810. */
  811. unsigned long section_mem_map;
  812. /* See declaration of similar field in struct zone */
  813. unsigned long *pageblock_flags;
  814. };
  815. #ifdef CONFIG_SPARSEMEM_EXTREME
  816. #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
  817. #else
  818. #define SECTIONS_PER_ROOT 1
  819. #endif
  820. #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
  821. #define NR_SECTION_ROOTS (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
  822. #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
  823. #ifdef CONFIG_SPARSEMEM_EXTREME
  824. extern struct mem_section *mem_section[NR_SECTION_ROOTS];
  825. #else
  826. extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
  827. #endif
  828. static inline struct mem_section *__nr_to_section(unsigned long nr)
  829. {
  830. if (!mem_section[SECTION_NR_TO_ROOT(nr)])
  831. return NULL;
  832. return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
  833. }
  834. extern int __section_nr(struct mem_section* ms);
  835. extern unsigned long usemap_size(void);
  836. /*
  837. * We use the lower bits of the mem_map pointer to store
  838. * a little bit of information. There should be at least
  839. * 3 bits here due to 32-bit alignment.
  840. */
  841. #define SECTION_MARKED_PRESENT (1UL<<0)
  842. #define SECTION_HAS_MEM_MAP (1UL<<1)
  843. #define SECTION_MAP_LAST_BIT (1UL<<2)
  844. #define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
  845. #define SECTION_NID_SHIFT 2
  846. static inline struct page *__section_mem_map_addr(struct mem_section *section)
  847. {
  848. unsigned long map = section->section_mem_map;
  849. map &= SECTION_MAP_MASK;
  850. return (struct page *)map;
  851. }
  852. static inline int present_section(struct mem_section *section)
  853. {
  854. return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
  855. }
  856. static inline int present_section_nr(unsigned long nr)
  857. {
  858. return present_section(__nr_to_section(nr));
  859. }
  860. static inline int valid_section(struct mem_section *section)
  861. {
  862. return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
  863. }
  864. static inline int valid_section_nr(unsigned long nr)
  865. {
  866. return valid_section(__nr_to_section(nr));
  867. }
  868. static inline struct mem_section *__pfn_to_section(unsigned long pfn)
  869. {
  870. return __nr_to_section(pfn_to_section_nr(pfn));
  871. }
  872. static inline int pfn_valid(unsigned long pfn)
  873. {
  874. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  875. return 0;
  876. return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
  877. }
  878. static inline int pfn_present(unsigned long pfn)
  879. {
  880. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  881. return 0;
  882. return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
  883. }
  884. /*
  885. * These are _only_ used during initialisation, therefore they
  886. * can use __initdata ... They could have names to indicate
  887. * this restriction.
  888. */
  889. #ifdef CONFIG_NUMA
  890. #define pfn_to_nid(pfn) \
  891. ({ \
  892. unsigned long __pfn_to_nid_pfn = (pfn); \
  893. page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
  894. })
  895. #else
  896. #define pfn_to_nid(pfn) (0)
  897. #endif
  898. #define early_pfn_valid(pfn) pfn_valid(pfn)
  899. void sparse_init(void);
  900. #else
  901. #define sparse_init() do {} while (0)
  902. #define sparse_index_init(_sec, _nid) do {} while (0)
  903. #endif /* CONFIG_SPARSEMEM */
  904. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  905. #define early_pfn_in_nid(pfn, nid) (early_pfn_to_nid(pfn) == (nid))
  906. #else
  907. #define early_pfn_in_nid(pfn, nid) (1)
  908. #endif
  909. #ifndef early_pfn_valid
  910. #define early_pfn_valid(pfn) (1)
  911. #endif
  912. void memory_present(int nid, unsigned long start, unsigned long end);
  913. unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
  914. /*
  915. * If it is possible to have holes within a MAX_ORDER_NR_PAGES, then we
  916. * need to check pfn validility within that MAX_ORDER_NR_PAGES block.
  917. * pfn_valid_within() should be used in this case; we optimise this away
  918. * when we have no holes within a MAX_ORDER_NR_PAGES block.
  919. */
  920. #ifdef CONFIG_HOLES_IN_ZONE
  921. #define pfn_valid_within(pfn) pfn_valid(pfn)
  922. #else
  923. #define pfn_valid_within(pfn) (1)
  924. #endif
  925. #endif /* !__GENERATING_BOUNDS.H */
  926. #endif /* !__ASSEMBLY__ */
  927. #endif /* _LINUX_MMZONE_H */