mmzone.h 31 KB

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