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

