mmzone.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. * Does the allocator try to reclaim pages from the zone as soon
  129. * as it fails a watermark_ok() in __alloc_pages?
  130. */
  131. int reclaim_pages;
  132. /* A count of how many reclaimers are scanning this zone */
  133. atomic_t reclaim_in_progress;
  134. /*
  135. * prev_priority holds the scanning priority for this zone. It is
  136. * defined as the scanning priority at which we achieved our reclaim
  137. * target at the previous try_to_free_pages() or balance_pgdat()
  138. * invokation.
  139. *
  140. * We use prev_priority as a measure of how much stress page reclaim is
  141. * under - it drives the swappiness decision: whether to unmap mapped
  142. * pages.
  143. *
  144. * temp_priority is used to remember the scanning priority at which
  145. * this zone was successfully refilled to free_pages == pages_high.
  146. *
  147. * Access to both these fields is quite racy even on uniprocessor. But
  148. * it is expected to average out OK.
  149. */
  150. int temp_priority;
  151. int prev_priority;
  152. ZONE_PADDING(_pad2_)
  153. /* Rarely used or read-mostly fields */
  154. /*
  155. * wait_table -- the array holding the hash table
  156. * wait_table_size -- the size of the hash table array
  157. * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
  158. *
  159. * The purpose of all these is to keep track of the people
  160. * waiting for a page to become available and make them
  161. * runnable again when possible. The trouble is that this
  162. * consumes a lot of space, especially when so few things
  163. * wait on pages at a given time. So instead of using
  164. * per-page waitqueues, we use a waitqueue hash table.
  165. *
  166. * The bucket discipline is to sleep on the same queue when
  167. * colliding and wake all in that wait queue when removing.
  168. * When something wakes, it must check to be sure its page is
  169. * truly available, a la thundering herd. The cost of a
  170. * collision is great, but given the expected load of the
  171. * table, they should be so rare as to be outweighed by the
  172. * benefits from the saved space.
  173. *
  174. * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
  175. * primary users of these fields, and in mm/page_alloc.c
  176. * free_area_init_core() performs the initialization of them.
  177. */
  178. wait_queue_head_t * wait_table;
  179. unsigned long wait_table_size;
  180. unsigned long wait_table_bits;
  181. /*
  182. * Discontig memory support fields.
  183. */
  184. struct pglist_data *zone_pgdat;
  185. struct page *zone_mem_map;
  186. /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
  187. unsigned long zone_start_pfn;
  188. unsigned long spanned_pages; /* total size, including holes */
  189. unsigned long present_pages; /* amount of memory (excluding holes) */
  190. /*
  191. * rarely used fields:
  192. */
  193. char *name;
  194. } ____cacheline_maxaligned_in_smp;
  195. /*
  196. * The "priority" of VM scanning is how much of the queues we will scan in one
  197. * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
  198. * queues ("queue_length >> 12") during an aging round.
  199. */
  200. #define DEF_PRIORITY 12
  201. /*
  202. * One allocation request operates on a zonelist. A zonelist
  203. * is a list of zones, the first one is the 'goal' of the
  204. * allocation, the other zones are fallback zones, in decreasing
  205. * priority.
  206. *
  207. * Right now a zonelist takes up less than a cacheline. We never
  208. * modify it apart from boot-up, and only a few indices are used,
  209. * so despite the zonelist table being relatively big, the cache
  210. * footprint of this construct is very small.
  211. */
  212. struct zonelist {
  213. struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
  214. };
  215. /*
  216. * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
  217. * (mostly NUMA machines?) to denote a higher-level memory zone than the
  218. * zone denotes.
  219. *
  220. * On NUMA machines, each NUMA node would have a pg_data_t to describe
  221. * it's memory layout.
  222. *
  223. * Memory statistics and page replacement data structures are maintained on a
  224. * per-zone basis.
  225. */
  226. struct bootmem_data;
  227. typedef struct pglist_data {
  228. struct zone node_zones[MAX_NR_ZONES];
  229. struct zonelist node_zonelists[GFP_ZONETYPES];
  230. int nr_zones;
  231. struct page *node_mem_map;
  232. struct bootmem_data *bdata;
  233. unsigned long node_start_pfn;
  234. unsigned long node_present_pages; /* total number of physical pages */
  235. unsigned long node_spanned_pages; /* total size of physical page
  236. range, including holes */
  237. int node_id;
  238. struct pglist_data *pgdat_next;
  239. wait_queue_head_t kswapd_wait;
  240. struct task_struct *kswapd;
  241. int kswapd_max_order;
  242. } pg_data_t;
  243. #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
  244. #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
  245. extern struct pglist_data *pgdat_list;
  246. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  247. unsigned long *free, struct pglist_data *pgdat);
  248. void get_zone_counts(unsigned long *active, unsigned long *inactive,
  249. unsigned long *free);
  250. void build_all_zonelists(void);
  251. void wakeup_kswapd(struct zone *zone, int order);
  252. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  253. int alloc_type, int can_try_harder, int gfp_high);
  254. #ifdef CONFIG_HAVE_MEMORY_PRESENT
  255. void memory_present(int nid, unsigned long start, unsigned long end);
  256. #else
  257. static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
  258. #endif
  259. #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
  260. unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
  261. #endif
  262. /*
  263. * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
  264. */
  265. #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
  266. /**
  267. * for_each_pgdat - helper macro to iterate over all nodes
  268. * @pgdat - pointer to a pg_data_t variable
  269. *
  270. * Meant to help with common loops of the form
  271. * pgdat = pgdat_list;
  272. * while(pgdat) {
  273. * ...
  274. * pgdat = pgdat->pgdat_next;
  275. * }
  276. */
  277. #define for_each_pgdat(pgdat) \
  278. for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
  279. /*
  280. * next_zone - helper magic for for_each_zone()
  281. * Thanks to William Lee Irwin III for this piece of ingenuity.
  282. */
  283. static inline struct zone *next_zone(struct zone *zone)
  284. {
  285. pg_data_t *pgdat = zone->zone_pgdat;
  286. if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
  287. zone++;
  288. else if (pgdat->pgdat_next) {
  289. pgdat = pgdat->pgdat_next;
  290. zone = pgdat->node_zones;
  291. } else
  292. zone = NULL;
  293. return zone;
  294. }
  295. /**
  296. * for_each_zone - helper macro to iterate over all memory zones
  297. * @zone - pointer to struct zone variable
  298. *
  299. * The user only needs to declare the zone variable, for_each_zone
  300. * fills it in. This basically means for_each_zone() is an
  301. * easier to read version of this piece of code:
  302. *
  303. * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
  304. * for (i = 0; i < MAX_NR_ZONES; ++i) {
  305. * struct zone * z = pgdat->node_zones + i;
  306. * ...
  307. * }
  308. * }
  309. */
  310. #define for_each_zone(zone) \
  311. for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
  312. static inline int is_highmem_idx(int idx)
  313. {
  314. return (idx == ZONE_HIGHMEM);
  315. }
  316. static inline int is_normal_idx(int idx)
  317. {
  318. return (idx == ZONE_NORMAL);
  319. }
  320. /**
  321. * is_highmem - helper function to quickly check if a struct zone is a
  322. * highmem zone or not. This is an attempt to keep references
  323. * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
  324. * @zone - pointer to struct zone variable
  325. */
  326. static inline int is_highmem(struct zone *zone)
  327. {
  328. return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM;
  329. }
  330. static inline int is_normal(struct zone *zone)
  331. {
  332. return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
  333. }
  334. /* These two functions are used to setup the per zone pages min values */
  335. struct ctl_table;
  336. struct file;
  337. int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
  338. void __user *, size_t *, loff_t *);
  339. extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
  340. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
  341. void __user *, size_t *, loff_t *);
  342. #include <linux/topology.h>
  343. /* Returns the number of the current Node. */
  344. #define numa_node_id() (cpu_to_node(raw_smp_processor_id()))
  345. #ifndef CONFIG_DISCONTIGMEM
  346. extern struct pglist_data contig_page_data;
  347. #define NODE_DATA(nid) (&contig_page_data)
  348. #define NODE_MEM_MAP(nid) mem_map
  349. #define MAX_NODES_SHIFT 1
  350. #define pfn_to_nid(pfn) (0)
  351. #else /* CONFIG_DISCONTIGMEM */
  352. #include <asm/mmzone.h>
  353. #if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
  354. /*
  355. * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
  356. * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
  357. */
  358. #define MAX_NODES_SHIFT 6
  359. #elif BITS_PER_LONG == 64
  360. /*
  361. * with 64 bit flags field, there's plenty of room.
  362. */
  363. #define MAX_NODES_SHIFT 10
  364. #endif
  365. #endif /* !CONFIG_DISCONTIGMEM */
  366. #if NODES_SHIFT > MAX_NODES_SHIFT
  367. #error NODES_SHIFT > MAX_NODES_SHIFT
  368. #endif
  369. /* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
  370. #define MAX_ZONES_SHIFT 2
  371. #if ZONES_SHIFT > MAX_ZONES_SHIFT
  372. #error ZONES_SHIFT > MAX_ZONES_SHIFT
  373. #endif
  374. #endif /* !__ASSEMBLY__ */
  375. #endif /* __KERNEL__ */
  376. #endif /* _LINUX_MMZONE_H */