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 struct list_head free_list
;
31 * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
32 * So add a wild amount of padding here to ensure that they fall into separate
33 * cachelines. There are very few zone structures in the machine, so space
34 * consumption is not a concern here.
36 #if defined(CONFIG_SMP)
39 } ____cacheline_maxaligned_in_smp
;
40 #define ZONE_PADDING(name) struct zone_padding name;
42 #define ZONE_PADDING(name)
45 struct per_cpu_pages
{
46 int count
; /* number of pages in the list */
47 int low
; /* low watermark, refill needed */
48 int high
; /* high watermark, emptying needed */
49 int batch
; /* chunk size for buddy add/remove */
50 struct list_head list
; /* the list of pages */
53 struct per_cpu_pageset
{
54 struct per_cpu_pages pcp
[2]; /* 0: hot. 1: cold */
56 unsigned long numa_hit
; /* allocated in intended node */
57 unsigned long numa_miss
; /* allocated in non intended node */
58 unsigned long numa_foreign
; /* was intended here, hit elsewhere */
59 unsigned long interleave_hit
; /* interleaver prefered this zone */
60 unsigned long local_node
; /* allocation from local node */
61 unsigned long other_node
; /* allocation from other node */
63 } ____cacheline_aligned_in_smp
;
67 #define ZONE_HIGHMEM 2
69 #define MAX_NR_ZONES 3 /* Sync this with ZONES_SHIFT */
70 #define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */
74 * When a memory allocation must conform to specific limitations (such
75 * as being suitable for DMA) the caller will pass in hints to the
76 * allocator in the gfp_mask, in the zone modifier bits. These bits
77 * are used to select a priority ordered list of memory zones which
78 * match the requested limits. GFP_ZONEMASK defines which bits within
79 * the gfp_mask should be considered as zone modifiers. Each valid
80 * combination of the zone modifier bits has a corresponding list
81 * of zones (in node_zonelists). Thus for two zone modifiers there
82 * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
83 * be 8 (2 ** 3) zonelists. GFP_ZONETYPES defines the number of possible
84 * combinations of zone modifiers in "zone modifier space".
86 #define GFP_ZONEMASK 0x03
88 * As an optimisation any zone modifier bits which are only valid when
89 * no other zone modifier bits are set (loners) should be placed in
90 * the highest order bits of this field. This allows us to reduce the
91 * extent of the zonelists thus saving space. For example in the case
92 * of three zone modifier bits, we could require up to eight zonelists.
93 * If the left most zone modifier is a "loner" then the highest valid
94 * zonelist would be four allowing us to allocate only five zonelists.
95 * Use the first form when the left most bit is not a "loner", otherwise
98 /* #define GFP_ZONETYPES (GFP_ZONEMASK + 1) */ /* Non-loner */
99 #define GFP_ZONETYPES ((GFP_ZONEMASK + 1) / 2 + 1) /* Loner */
102 * On machines where it is needed (eg PCs) we divide physical memory
103 * into multiple physical zones. On a PC we have 3 zones:
105 * ZONE_DMA < 16 MB ISA DMA capable memory
106 * ZONE_NORMAL 16-896 MB direct mapped by the kernel
107 * ZONE_HIGHMEM > 896 MB only page cache and user processes
112 * Commonly accessed fields:
115 unsigned long free_pages
;
116 unsigned long pages_min
, pages_low
, pages_high
;
118 * protection[] is a pre-calculated number of extra pages that must be
119 * available in a zone in order for __alloc_pages() to allocate memory
120 * from the zone. i.e., for a GFP_KERNEL alloc of "order" there must
121 * be "(1<<order) + protection[ZONE_NORMAL]" free pages in the zone
122 * for us to choose to allocate the page from that zone.
124 * It uses both min_free_kbytes and sysctl_lower_zone_protection.
125 * The protection values are recalculated if either of these values
126 * change. The array elements are in zonelist order:
127 * [0] == GFP_DMA, [1] == GFP_KERNEL, [2] == GFP_HIGHMEM.
129 unsigned long protection
[MAX_NR_ZONES
];
134 struct list_head active_list
;
135 struct list_head inactive_list
;
136 unsigned long nr_scan_active
;
137 unsigned long nr_scan_inactive
;
138 unsigned long nr_active
;
139 unsigned long nr_inactive
;
140 int all_unreclaimable
; /* All pages pinned */
141 unsigned long pages_scanned
; /* since last reclaim */
146 * prev_priority holds the scanning priority for this zone. It is
147 * defined as the scanning priority at which we achieved our reclaim
148 * target at the previous try_to_free_pages() or balance_pgdat()
151 * We use prev_priority as a measure of how much stress page reclaim is
152 * under - it drives the swappiness decision: whether to unmap mapped
155 * temp_priority is used to remember the scanning priority at which
156 * this zone was successfully refilled to free_pages == pages_high.
158 * Access to both these fields is quite racy even on uniprocessor. But
159 * it is expected to average out OK.
165 * free areas of different sizes
167 struct free_area free_area
[MAX_ORDER
];
170 * wait_table -- the array holding the hash table
171 * wait_table_size -- the size of the hash table array
172 * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
174 * The purpose of all these is to keep track of the people
175 * waiting for a page to become available and make them
176 * runnable again when possible. The trouble is that this
177 * consumes a lot of space, especially when so few things
178 * wait on pages at a given time. So instead of using
179 * per-page waitqueues, we use a waitqueue hash table.
181 * The bucket discipline is to sleep on the same queue when
182 * colliding and wake all in that wait queue when removing.
183 * When something wakes, it must check to be sure its page is
184 * truly available, a la thundering herd. The cost of a
185 * collision is great, but given the expected load of the
186 * table, they should be so rare as to be outweighed by the
187 * benefits from the saved space.
189 * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
190 * primary users of these fields, and in mm/page_alloc.c
191 * free_area_init_core() performs the initialization of them.
193 wait_queue_head_t
* wait_table
;
194 unsigned long wait_table_size
;
195 unsigned long wait_table_bits
;
199 struct per_cpu_pageset pageset
[NR_CPUS
];
202 * Discontig memory support fields.
204 struct pglist_data
*zone_pgdat
;
205 struct page
*zone_mem_map
;
206 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
207 unsigned long zone_start_pfn
;
210 * rarely used fields:
213 unsigned long spanned_pages
; /* total size, including holes */
214 unsigned long present_pages
; /* amount of memory (excluding holes) */
215 } ____cacheline_maxaligned_in_smp
;
219 * The "priority" of VM scanning is how much of the queues we will scan in one
220 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
221 * queues ("queue_length >> 12") during an aging round.
223 #define DEF_PRIORITY 12
226 * One allocation request operates on a zonelist. A zonelist
227 * is a list of zones, the first one is the 'goal' of the
228 * allocation, the other zones are fallback zones, in decreasing
231 * Right now a zonelist takes up less than a cacheline. We never
232 * modify it apart from boot-up, and only a few indices are used,
233 * so despite the zonelist table being relatively big, the cache
234 * footprint of this construct is very small.
237 struct zone
*zones
[MAX_NUMNODES
* MAX_NR_ZONES
+ 1]; // NULL delimited
242 * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
243 * (mostly NUMA machines?) to denote a higher-level memory zone than the
246 * On NUMA machines, each NUMA node would have a pg_data_t to describe
247 * it's memory layout.
249 * Memory statistics and page replacement data structures are maintained on a
253 typedef struct pglist_data
{
254 struct zone node_zones
[MAX_NR_ZONES
];
255 struct zonelist node_zonelists
[GFP_ZONETYPES
];
257 struct page
*node_mem_map
;
258 struct bootmem_data
*bdata
;
259 unsigned long node_start_pfn
;
260 unsigned long node_present_pages
; /* total number of physical pages */
261 unsigned long node_spanned_pages
; /* total size of physical page
262 range, including holes */
264 struct pglist_data
*pgdat_next
;
265 wait_queue_head_t kswapd_wait
;
266 struct task_struct
*kswapd
;
269 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
270 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
273 extern struct pglist_data
*pgdat_list
;
275 void __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
276 unsigned long *free
, struct pglist_data
*pgdat
);
277 void get_zone_counts(unsigned long *active
, unsigned long *inactive
,
278 unsigned long *free
);
279 void build_all_zonelists(void);
280 void wakeup_kswapd(struct zone
*zone
);
283 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
285 #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
288 * for_each_pgdat - helper macro to iterate over all nodes
289 * @pgdat - pointer to a pg_data_t variable
291 * Meant to help with common loops of the form
292 * pgdat = pgdat_list;
295 * pgdat = pgdat->pgdat_next;
298 #define for_each_pgdat(pgdat) \
299 for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
302 * next_zone - helper magic for for_each_zone()
303 * Thanks to William Lee Irwin III for this piece of ingenuity.
305 static inline struct zone
*next_zone(struct zone
*zone
)
307 pg_data_t
*pgdat
= zone
->zone_pgdat
;
309 if (zone
- pgdat
->node_zones
< MAX_NR_ZONES
- 1)
311 else if (pgdat
->pgdat_next
) {
312 pgdat
= pgdat
->pgdat_next
;
313 zone
= pgdat
->node_zones
;
321 * for_each_zone - helper macro to iterate over all memory zones
322 * @zone - pointer to struct zone variable
324 * The user only needs to declare the zone variable, for_each_zone
325 * fills it in. This basically means for_each_zone() is an
326 * easier to read version of this piece of code:
328 * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
329 * for (i = 0; i < MAX_NR_ZONES; ++i) {
330 * struct zone * z = pgdat->node_zones + i;
335 #define for_each_zone(zone) \
336 for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
338 static inline int is_highmem_idx(int idx
)
340 return (idx
== ZONE_HIGHMEM
);
343 static inline int is_normal_idx(int idx
)
345 return (idx
== ZONE_NORMAL
);
348 * is_highmem - helper function to quickly check if a struct zone is a
349 * highmem zone or not. This is an attempt to keep references
350 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
351 * @zone - pointer to struct zone variable
353 static inline int is_highmem(struct zone
*zone
)
355 return (is_highmem_idx(zone
- zone
->zone_pgdat
->node_zones
));
358 static inline int is_normal(struct zone
*zone
)
360 return (is_normal_idx(zone
- zone
->zone_pgdat
->node_zones
));
363 /* These two functions are used to setup the per zone pages min values */
366 int min_free_kbytes_sysctl_handler(struct ctl_table
*, int, struct file
*,
367 void __user
*, size_t *, loff_t
*);
368 int lower_zone_protection_sysctl_handler(struct ctl_table
*, int, struct file
*,
369 void __user
*, size_t *, loff_t
*);
371 #include <linux/topology.h>
372 /* Returns the number of the current Node. */
373 #define numa_node_id() (cpu_to_node(smp_processor_id()))
375 #ifndef CONFIG_DISCONTIGMEM
377 extern struct pglist_data contig_page_data
;
378 #define NODE_DATA(nid) (&contig_page_data)
379 #define NODE_MEM_MAP(nid) mem_map
380 #define MAX_NODES_SHIFT 1
381 #define pfn_to_nid(pfn) (0)
383 #else /* CONFIG_DISCONTIGMEM */
385 #include <asm/mmzone.h>
387 #if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
389 * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
390 * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
392 #define MAX_NODES_SHIFT 6
393 #elif BITS_PER_LONG == 64
395 * with 64 bit flags field, there's plenty of room.
397 #define MAX_NODES_SHIFT 10
400 #endif /* !CONFIG_DISCONTIGMEM */
402 #if NODES_SHIFT > MAX_NODES_SHIFT
403 #error NODES_SHIFT > MAX_NODES_SHIFT
406 /* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
407 #define MAX_ZONES_SHIFT 2
409 #if ZONES_SHIFT > MAX_ZONES_SHIFT
410 #error ZONES_SHIFT > MAX_ZONES_SHIFT
413 extern DECLARE_BITMAP(node_online_map
, MAX_NUMNODES
);
415 #if defined(CONFIG_DISCONTIGMEM) || defined(CONFIG_NUMA)
417 #define node_online(node) test_bit(node, node_online_map)
418 #define node_set_online(node) set_bit(node, node_online_map)
419 #define node_set_offline(node) clear_bit(node, node_online_map)
420 static inline unsigned int num_online_nodes(void)
424 for(i
= 0; i
< MAX_NUMNODES
; i
++){
431 #else /* !CONFIG_DISCONTIGMEM && !CONFIG_NUMA */
433 #define node_online(node) \
434 ({ BUG_ON((node) != 0); test_bit(node, node_online_map); })
435 #define node_set_online(node) \
436 ({ BUG_ON((node) != 0); set_bit(node, node_online_map); })
437 #define node_set_offline(node) \
438 ({ BUG_ON((node) != 0); clear_bit(node, node_online_map); })
439 #define num_online_nodes() 1
441 #endif /* CONFIG_DISCONTIGMEM || CONFIG_NUMA */
442 #endif /* !__ASSEMBLY__ */
443 #endif /* __KERNEL__ */
444 #endif /* _LINUX_MMZONE_H */