mmzone.h 25 KB

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