mmzone.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #ifndef _LINUX_MMZONE_H
  2. #define _LINUX_MMZONE_H
  3. #ifdef __KERNEL__
  4. #ifndef __ASSEMBLY__
  5. #include <linux/config.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/list.h>
  8. #include <linux/wait.h>
  9. #include <linux/cache.h>
  10. #include <linux/threads.h>
  11. #include <linux/numa.h>
  12. #include <linux/init.h>
  13. #include <asm/atomic.h>
  14. /* Free memory management - zoned buddy allocator. */
  15. #ifndef CONFIG_FORCE_MAX_ZONEORDER
  16. #define MAX_ORDER 11
  17. #else
  18. #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
  19. #endif
  20. struct free_area {
  21. struct list_head free_list;
  22. unsigned long nr_free;
  23. };
  24. struct pglist_data;
  25. /*
  26. * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
  27. * So add a wild amount of padding here to ensure that they fall into separate
  28. * cachelines. There are very few zone structures in the machine, so space
  29. * consumption is not a concern here.
  30. */
  31. #if defined(CONFIG_SMP)
  32. struct zone_padding {
  33. char x[0];
  34. } ____cacheline_maxaligned_in_smp;
  35. #define ZONE_PADDING(name) struct zone_padding name;
  36. #else
  37. #define ZONE_PADDING(name)
  38. #endif
  39. struct per_cpu_pages {
  40. int count; /* number of pages in the list */
  41. int low; /* low watermark, refill needed */
  42. int high; /* high watermark, emptying needed */
  43. int batch; /* chunk size for buddy add/remove */
  44. struct list_head list; /* the list of pages */
  45. };
  46. struct per_cpu_pageset {
  47. struct per_cpu_pages pcp[2]; /* 0: hot. 1: cold */
  48. #ifdef CONFIG_NUMA
  49. unsigned long numa_hit; /* allocated in intended node */
  50. unsigned long numa_miss; /* allocated in non intended node */
  51. unsigned long numa_foreign; /* was intended here, hit elsewhere */
  52. unsigned long interleave_hit; /* interleaver prefered this zone */
  53. unsigned long local_node; /* allocation from local node */
  54. unsigned long other_node; /* allocation from other node */
  55. #endif
  56. } ____cacheline_aligned_in_smp;
  57. #define ZONE_DMA 0
  58. #define ZONE_NORMAL 1
  59. #define ZONE_HIGHMEM 2
  60. #define MAX_NR_ZONES 3 /* Sync this with ZONES_SHIFT */
  61. #define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */
  62. /*
  63. * When a memory allocation must conform to specific limitations (such
  64. * as being suitable for DMA) the caller will pass in hints to the
  65. * allocator in the gfp_mask, in the zone modifier bits. These bits
  66. * are used to select a priority ordered list of memory zones which
  67. * match the requested limits. GFP_ZONEMASK defines which bits within
  68. * the gfp_mask should be considered as zone modifiers. Each valid
  69. * combination of the zone modifier bits has a corresponding list
  70. * of zones (in node_zonelists). Thus for two zone modifiers there
  71. * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
  72. * be 8 (2 ** 3) zonelists. GFP_ZONETYPES defines the number of possible
  73. * combinations of zone modifiers in "zone modifier space".
  74. */
  75. #define GFP_ZONEMASK 0x03
  76. /*
  77. * As an optimisation any zone modifier bits which are only valid when
  78. * no other zone modifier bits are set (loners) should be placed in
  79. * the highest order bits of this field. This allows us to reduce the
  80. * extent of the zonelists thus saving space. For example in the case
  81. * of three zone modifier bits, we could require up to eight zonelists.
  82. * If the left most zone modifier is a "loner" then the highest valid
  83. * zonelist would be four allowing us to allocate only five zonelists.
  84. * Use the first form when the left most bit is not a "loner", otherwise
  85. * use the second.
  86. */
  87. /* #define GFP_ZONETYPES (GFP_ZONEMASK + 1) */ /* Non-loner */
  88. #define GFP_ZONETYPES ((GFP_ZONEMASK + 1) / 2 + 1) /* Loner */
  89. /*
  90. * On machines where it is needed (eg PCs) we divide physical memory
  91. * into multiple physical zones. On a PC we have 3 zones:
  92. *
  93. * ZONE_DMA < 16 MB ISA DMA capable memory
  94. * ZONE_NORMAL 16-896 MB direct mapped by the kernel
  95. * ZONE_HIGHMEM > 896 MB only page cache and user processes
  96. */
  97. struct zone {
  98. /* Fields commonly accessed by the page allocator */
  99. unsigned long free_pages;
  100. unsigned long pages_min, pages_low, pages_high;
  101. /*
  102. * We don't know if the memory that we're going to allocate will be freeable
  103. * or/and it will be released eventually, so to avoid totally wasting several
  104. * GB of ram we must reserve some of the lower zone memory (otherwise we risk
  105. * to run OOM on the lower zones despite there's tons of freeable ram
  106. * on the higher zones). This array is recalculated at runtime if the
  107. * sysctl_lowmem_reserve_ratio sysctl changes.
  108. */
  109. unsigned long lowmem_reserve[MAX_NR_ZONES];
  110. struct per_cpu_pageset pageset[NR_CPUS];
  111. /*
  112. * free areas of different sizes
  113. */
  114. spinlock_t lock;
  115. struct free_area free_area[MAX_ORDER];
  116. ZONE_PADDING(_pad1_)
  117. /* Fields commonly accessed by the page reclaim scanner */
  118. spinlock_t lru_lock;
  119. struct list_head active_list;
  120. struct list_head inactive_list;
  121. unsigned long nr_scan_active;
  122. unsigned long nr_scan_inactive;
  123. unsigned long nr_active;
  124. unsigned long nr_inactive;
  125. unsigned long pages_scanned; /* since last reclaim */
  126. int all_unreclaimable; /* All pages pinned */
  127. /*
  128. * prev_priority holds the scanning priority for this zone. It is
  129. * defined as the scanning priority at which we achieved our reclaim
  130. * target at the previous try_to_free_pages() or balance_pgdat()
  131. * invokation.
  132. *
  133. * We use prev_priority as a measure of how much stress page reclaim is
  134. * under - it drives the swappiness decision: whether to unmap mapped
  135. * pages.
  136. *
  137. * temp_priority is used to remember the scanning priority at which
  138. * this zone was successfully refilled to free_pages == pages_high.
  139. *
  140. * Access to both these fields is quite racy even on uniprocessor. But
  141. * it is expected to average out OK.
  142. */
  143. int temp_priority;
  144. int prev_priority;
  145. ZONE_PADDING(_pad2_)
  146. /* Rarely used or read-mostly fields */
  147. /*
  148. * wait_table -- the array holding the hash table
  149. * wait_table_size -- the size of the hash table array
  150. * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
  151. *
  152. * The purpose of all these is to keep track of the people
  153. * waiting for a page to become available and make them
  154. * runnable again when possible. The trouble is that this
  155. * consumes a lot of space, especially when so few things
  156. * wait on pages at a given time. So instead of using
  157. * per-page waitqueues, we use a waitqueue hash table.
  158. *
  159. * The bucket discipline is to sleep on the same queue when
  160. * colliding and wake all in that wait queue when removing.
  161. * When something wakes, it must check to be sure its page is
  162. * truly available, a la thundering herd. The cost of a
  163. * collision is great, but given the expected load of the
  164. * table, they should be so rare as to be outweighed by the
  165. * benefits from the saved space.
  166. *
  167. * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
  168. * primary users of these fields, and in mm/page_alloc.c
  169. * free_area_init_core() performs the initialization of them.
  170. */
  171. wait_queue_head_t * wait_table;
  172. unsigned long wait_table_size;
  173. unsigned long wait_table_bits;
  174. /*
  175. * Discontig memory support fields.
  176. */
  177. struct pglist_data *zone_pgdat;
  178. struct page *zone_mem_map;
  179. /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
  180. unsigned long zone_start_pfn;
  181. unsigned long spanned_pages; /* total size, including holes */
  182. unsigned long present_pages; /* amount of memory (excluding holes) */
  183. /*
  184. * rarely used fields:
  185. */
  186. char *name;
  187. } ____cacheline_maxaligned_in_smp;
  188. /*
  189. * The "priority" of VM scanning is how much of the queues we will scan in one
  190. * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
  191. * queues ("queue_length >> 12") during an aging round.
  192. */
  193. #define DEF_PRIORITY 12
  194. /*
  195. * One allocation request operates on a zonelist. A zonelist
  196. * is a list of zones, the first one is the 'goal' of the
  197. * allocation, the other zones are fallback zones, in decreasing
  198. * priority.
  199. *
  200. * Right now a zonelist takes up less than a cacheline. We never
  201. * modify it apart from boot-up, and only a few indices are used,
  202. * so despite the zonelist table being relatively big, the cache
  203. * footprint of this construct is very small.
  204. */
  205. struct zonelist {
  206. struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
  207. };
  208. /*
  209. * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
  210. * (mostly NUMA machines?) to denote a higher-level memory zone than the
  211. * zone denotes.
  212. *
  213. * On NUMA machines, each NUMA node would have a pg_data_t to describe
  214. * it's memory layout.
  215. *
  216. * Memory statistics and page replacement data structures are maintained on a
  217. * per-zone basis.
  218. */
  219. struct bootmem_data;
  220. typedef struct pglist_data {
  221. struct zone node_zones[MAX_NR_ZONES];
  222. struct zonelist node_zonelists[GFP_ZONETYPES];
  223. int nr_zones;
  224. struct page *node_mem_map;
  225. struct bootmem_data *bdata;
  226. unsigned long node_start_pfn;
  227. unsigned long node_present_pages; /* total number of physical pages */
  228. unsigned long node_spanned_pages; /* total size of physical page
  229. range, including holes */
  230. int node_id;
  231. struct pglist_data *pgdat_next;
  232. wait_queue_head_t kswapd_wait;
  233. struct task_struct *kswapd;
  234. int kswapd_max_order;
  235. } pg_data_t;
  236. #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
  237. #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
  238. extern struct pglist_data *pgdat_list;
  239. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  240. unsigned long *free, struct pglist_data *pgdat);
  241. void get_zone_counts(unsigned long *active, unsigned long *inactive,
  242. unsigned long *free);
  243. void build_all_zonelists(void);
  244. void wakeup_kswapd(struct zone *zone, int order);
  245. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  246. int alloc_type, int can_try_harder, int gfp_high);
  247. #ifdef CONFIG_HAVE_MEMORY_PRESENT
  248. void memory_present(int nid, unsigned long start, unsigned long end);
  249. #else
  250. static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
  251. #endif
  252. #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
  253. unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
  254. #endif
  255. /*
  256. * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
  257. */
  258. #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
  259. /**
  260. * for_each_pgdat - helper macro to iterate over all nodes
  261. * @pgdat - pointer to a pg_data_t variable
  262. *
  263. * Meant to help with common loops of the form
  264. * pgdat = pgdat_list;
  265. * while(pgdat) {
  266. * ...
  267. * pgdat = pgdat->pgdat_next;
  268. * }
  269. */
  270. #define for_each_pgdat(pgdat) \
  271. for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
  272. /*
  273. * next_zone - helper magic for for_each_zone()
  274. * Thanks to William Lee Irwin III for this piece of ingenuity.
  275. */
  276. static inline struct zone *next_zone(struct zone *zone)
  277. {
  278. pg_data_t *pgdat = zone->zone_pgdat;
  279. if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
  280. zone++;
  281. else if (pgdat->pgdat_next) {
  282. pgdat = pgdat->pgdat_next;
  283. zone = pgdat->node_zones;
  284. } else
  285. zone = NULL;
  286. return zone;
  287. }
  288. /**
  289. * for_each_zone - helper macro to iterate over all memory zones
  290. * @zone - pointer to struct zone variable
  291. *
  292. * The user only needs to declare the zone variable, for_each_zone
  293. * fills it in. This basically means for_each_zone() is an
  294. * easier to read version of this piece of code:
  295. *
  296. * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
  297. * for (i = 0; i < MAX_NR_ZONES; ++i) {
  298. * struct zone * z = pgdat->node_zones + i;
  299. * ...
  300. * }
  301. * }
  302. */
  303. #define for_each_zone(zone) \
  304. for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
  305. static inline int is_highmem_idx(int idx)
  306. {
  307. return (idx == ZONE_HIGHMEM);
  308. }
  309. static inline int is_normal_idx(int idx)
  310. {
  311. return (idx == ZONE_NORMAL);
  312. }
  313. /**
  314. * is_highmem - helper function to quickly check if a struct zone is a
  315. * highmem zone or not. This is an attempt to keep references
  316. * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
  317. * @zone - pointer to struct zone variable
  318. */
  319. static inline int is_highmem(struct zone *zone)
  320. {
  321. return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM;
  322. }
  323. static inline int is_normal(struct zone *zone)
  324. {
  325. return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
  326. }
  327. /* These two functions are used to setup the per zone pages min values */
  328. struct ctl_table;
  329. struct file;
  330. int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
  331. void __user *, size_t *, loff_t *);
  332. extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
  333. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
  334. void __user *, size_t *, loff_t *);
  335. #include <linux/topology.h>
  336. /* Returns the number of the current Node. */
  337. #define numa_node_id() (cpu_to_node(_smp_processor_id()))
  338. #ifndef CONFIG_DISCONTIGMEM
  339. extern struct pglist_data contig_page_data;
  340. #define NODE_DATA(nid) (&contig_page_data)
  341. #define NODE_MEM_MAP(nid) mem_map
  342. #define MAX_NODES_SHIFT 1
  343. #define pfn_to_nid(pfn) (0)
  344. #else /* CONFIG_DISCONTIGMEM */
  345. #include <asm/mmzone.h>
  346. #if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
  347. /*
  348. * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
  349. * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
  350. */
  351. #define MAX_NODES_SHIFT 6
  352. #elif BITS_PER_LONG == 64
  353. /*
  354. * with 64 bit flags field, there's plenty of room.
  355. */
  356. #define MAX_NODES_SHIFT 10
  357. #endif
  358. #endif /* !CONFIG_DISCONTIGMEM */
  359. #if NODES_SHIFT > MAX_NODES_SHIFT
  360. #error NODES_SHIFT > MAX_NODES_SHIFT
  361. #endif
  362. /* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
  363. #define MAX_ZONES_SHIFT 2
  364. #if ZONES_SHIFT > MAX_ZONES_SHIFT
  365. #error ZONES_SHIFT > MAX_ZONES_SHIFT
  366. #endif
  367. #endif /* !__ASSEMBLY__ */
  368. #endif /* __KERNEL__ */
  369. #endif /* _LINUX_MMZONE_H */