mmzone.h 14 KB

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