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