mmzone.h 27 KB

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