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