1 #ifndef _LINUX_MMZONE_H
2 #define _LINUX_MMZONE_H
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>
16 /* Free memory management - zoned buddy allocator. */
17 #ifndef CONFIG_FORCE_MAX_ZONEORDER
20 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
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
30 #define MAX_SYS_HASH_TABLE_ORDER MAX_ORDER
32 #define MAX_SYS_HASH_TABLE_ORDER 14
36 struct list_head free_list;
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.
48 #if defined(CONFIG_SMP)
51 } ____cacheline_maxaligned_in_smp;
52 #define ZONE_PADDING(name) struct zone_padding name;
54 #define ZONE_PADDING(name)
57 struct 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 */
65 struct per_cpu_pageset {
66 struct per_cpu_pages pcp[2]; /* 0: hot. 1: cold */
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 */
75 } ____cacheline_aligned_in_smp;
79 #define ZONE_HIGHMEM 2
81 #define MAX_NR_ZONES 3 /* Sync this with ZONES_SHIFT */
82 #define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */
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".
98 #define GFP_ZONEMASK 0x03
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
110 /* #define GFP_ZONETYPES (GFP_ZONEMASK + 1) */ /* Non-loner */
111 #define GFP_ZONETYPES ((GFP_ZONEMASK + 1) / 2 + 1) /* Loner */
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:
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
124 * Commonly accessed fields:
127 unsigned long free_pages;
128 unsigned long pages_min, pages_low, pages_high;
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.
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.
141 unsigned long protection[MAX_NR_ZONES];
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 */
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()
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
167 * temp_priority is used to remember the scanning priority at which
168 * this zone was successfully refilled to free_pages == pages_high.
170 * Access to both these fields is quite racy even on uniprocessor. But
171 * it is expected to average out OK.
177 * free areas of different sizes
179 struct free_area free_area[MAX_ORDER];
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)
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.
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.
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.
205 wait_queue_head_t * wait_table;
206 unsigned long wait_table_size;
207 unsigned long wait_table_bits;
211 struct per_cpu_pageset pageset[NR_CPUS];
214 * Discontig memory support fields.
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;
222 * rarely used fields:
225 unsigned long spanned_pages; /* total size, including holes */
226 unsigned long present_pages; /* amount of memory (excluding holes) */
227 } ____cacheline_maxaligned_in_smp;
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.
235 #define DEF_PRIORITY 12
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
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.
249 struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
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
258 * On NUMA machines, each NUMA node would have a pg_data_t to describe
259 * it's memory layout.
261 * Memory statistics and page replacement data structures are maintained on a
265 typedef struct pglist_data {
266 struct zone node_zones[MAX_NR_ZONES];
267 struct zonelist node_zonelists[GFP_ZONETYPES];
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 */
276 struct pglist_data *pgdat_next;
277 wait_queue_head_t kswapd_wait;
278 struct task_struct *kswapd;
281 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
282 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
285 extern struct pglist_data *pgdat_list;
287 void get_zone_counts(unsigned long *active, unsigned long *inactive,
288 unsigned long *free);
289 void build_all_zonelists(void);
290 void wakeup_kswapd(struct zone *zone);
293 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
295 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
298 * for_each_pgdat - helper macro to iterate over all nodes
299 * @pgdat - pointer to a pg_data_t variable
301 * Meant to help with common loops of the form
302 * pgdat = pgdat_list;
305 * pgdat = pgdat->pgdat_next;
308 #define for_each_pgdat(pgdat) \
309 for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
312 * next_zone - helper magic for for_each_zone()
313 * Thanks to William Lee Irwin III for this piece of ingenuity.
315 static inline struct zone *next_zone(struct zone *zone)
317 pg_data_t *pgdat = zone->zone_pgdat;
319 if (zone - pgdat->node_zones < MAX_NR_ZONES - 1)
321 else if (pgdat->pgdat_next) {
322 pgdat = pgdat->pgdat_next;
323 zone = pgdat->node_zones;
331 * for_each_zone - helper macro to iterate over all memory zones
332 * @zone - pointer to struct zone variable
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:
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;
345 #define for_each_zone(zone) \
346 for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
348 static inline int is_highmem_idx(int idx)
350 return (idx == ZONE_HIGHMEM);
353 static inline int is_normal_idx(int idx)
355 return (idx == ZONE_NORMAL);
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
363 static inline int is_highmem(struct zone *zone)
365 return (is_highmem_idx(zone - zone->zone_pgdat->node_zones));
368 static inline int is_normal(struct zone *zone)
370 return (is_normal_idx(zone - zone->zone_pgdat->node_zones));
373 /* These two functions are used to setup the per zone pages min values */
376 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
377 void __user *, size_t *, loff_t *);
378 int lower_zone_protection_sysctl_handler(struct ctl_table *, int, struct file *,
379 void __user *, size_t *, loff_t *);
381 #include <linux/topology.h>
382 /* Returns the number of the current Node. */
383 #define numa_node_id() (cpu_to_node(smp_processor_id()))
385 #ifndef CONFIG_DISCONTIGMEM
387 extern 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)
393 #else /* CONFIG_DISCONTIGMEM */
395 #include <asm/mmzone.h>
397 #if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
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.
402 #define MAX_NODES_SHIFT 6
403 #elif BITS_PER_LONG == 64
405 * with 64 bit flags field, there's plenty of room.
407 #define MAX_NODES_SHIFT 10
410 #endif /* !CONFIG_DISCONTIGMEM */
412 #if NODES_SHIFT > MAX_NODES_SHIFT
413 #error NODES_SHIFT > MAX_NODES_SHIFT
416 /* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
417 #define MAX_ZONES_SHIFT 2
419 #if ZONES_SHIFT > MAX_ZONES_SHIFT
420 #error ZONES_SHIFT > MAX_ZONES_SHIFT
423 extern DECLARE_BITMAP(node_online_map, MAX_NUMNODES);
425 #if defined(CONFIG_DISCONTIGMEM) || defined(CONFIG_NUMA)
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)
430 static inline unsigned int num_online_nodes(void)
434 for(i = 0; i < MAX_NUMNODES; i++){
441 #else /* !CONFIG_DISCONTIGMEM && !CONFIG_NUMA */
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
451 #endif /* CONFIG_DISCONTIGMEM || CONFIG_NUMA */
452 #endif /* !__ASSEMBLY__ */
453 #endif /* __KERNEL__ */
454 #endif /* _LINUX_MMZONE_H */