mmzone.h 31 KB

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