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