mmzone.h 31 KB

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