]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/mmzone.h
vmscan: Use an indexed array for LRU variables
[linux-2.6-omap-h63xx.git] / include / linux / mmzone.h
1 #ifndef _LINUX_MMZONE_H
2 #define _LINUX_MMZONE_H
3
4 #ifndef __ASSEMBLY__
5 #ifndef __GENERATING_BOUNDS_H
6
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/wait.h>
10 #include <linux/bitops.h>
11 #include <linux/cache.h>
12 #include <linux/threads.h>
13 #include <linux/numa.h>
14 #include <linux/init.h>
15 #include <linux/seqlock.h>
16 #include <linux/nodemask.h>
17 #include <linux/pageblock-flags.h>
18 #include <linux/bounds.h>
19 #include <asm/atomic.h>
20 #include <asm/page.h>
21
22 /* Free memory management - zoned buddy allocator.  */
23 #ifndef CONFIG_FORCE_MAX_ZONEORDER
24 #define MAX_ORDER 11
25 #else
26 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
27 #endif
28 #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
29
30 /*
31  * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
32  * costly to service.  That is between allocation orders which should
33  * coelesce naturally under reasonable reclaim pressure and those which
34  * will not.
35  */
36 #define PAGE_ALLOC_COSTLY_ORDER 3
37
38 #define MIGRATE_UNMOVABLE     0
39 #define MIGRATE_RECLAIMABLE   1
40 #define MIGRATE_MOVABLE       2
41 #define MIGRATE_RESERVE       3
42 #define MIGRATE_ISOLATE       4 /* can't allocate from here */
43 #define MIGRATE_TYPES         5
44
45 #define for_each_migratetype_order(order, type) \
46         for (order = 0; order < MAX_ORDER; order++) \
47                 for (type = 0; type < MIGRATE_TYPES; type++)
48
49 extern int page_group_by_mobility_disabled;
50
51 static inline int get_pageblock_migratetype(struct page *page)
52 {
53         if (unlikely(page_group_by_mobility_disabled))
54                 return MIGRATE_UNMOVABLE;
55
56         return get_pageblock_flags_group(page, PB_migrate, PB_migrate_end);
57 }
58
59 struct free_area {
60         struct list_head        free_list[MIGRATE_TYPES];
61         unsigned long           nr_free;
62 };
63
64 struct pglist_data;
65
66 /*
67  * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
68  * So add a wild amount of padding here to ensure that they fall into separate
69  * cachelines.  There are very few zone structures in the machine, so space
70  * consumption is not a concern here.
71  */
72 #if defined(CONFIG_SMP)
73 struct zone_padding {
74         char x[0];
75 } ____cacheline_internodealigned_in_smp;
76 #define ZONE_PADDING(name)      struct zone_padding name;
77 #else
78 #define ZONE_PADDING(name)
79 #endif
80
81 enum zone_stat_item {
82         /* First 128 byte cacheline (assuming 64 bit words) */
83         NR_FREE_PAGES,
84         NR_LRU_BASE,
85         NR_INACTIVE = NR_LRU_BASE, /* must match order of LRU_[IN]ACTIVE */
86         NR_ACTIVE,      /*  "     "     "   "       "         */
87         NR_ANON_PAGES,  /* Mapped anonymous pages */
88         NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
89                            only modified from process context */
90         NR_FILE_PAGES,
91         NR_FILE_DIRTY,
92         NR_WRITEBACK,
93         /* Second 128 byte cacheline */
94         NR_SLAB_RECLAIMABLE,
95         NR_SLAB_UNRECLAIMABLE,
96         NR_PAGETABLE,           /* used for pagetables */
97         NR_UNSTABLE_NFS,        /* NFS unstable pages */
98         NR_BOUNCE,
99         NR_VMSCAN_WRITE,
100         NR_WRITEBACK_TEMP,      /* Writeback using temporary buffers */
101 #ifdef CONFIG_NUMA
102         NUMA_HIT,               /* allocated in intended node */
103         NUMA_MISS,              /* allocated in non intended node */
104         NUMA_FOREIGN,           /* was intended here, hit elsewhere */
105         NUMA_INTERLEAVE_HIT,    /* interleaver preferred this zone */
106         NUMA_LOCAL,             /* allocation from local node */
107         NUMA_OTHER,             /* allocation from other node */
108 #endif
109         NR_VM_ZONE_STAT_ITEMS };
110
111 enum lru_list {
112         LRU_BASE,
113         LRU_INACTIVE=LRU_BASE,  /* must match order of NR_[IN]ACTIVE */
114         LRU_ACTIVE,             /*  "     "     "   "       "        */
115         NR_LRU_LISTS };
116
117 #define for_each_lru(l) for (l = 0; l < NR_LRU_LISTS; l++)
118
119 static inline int is_active_lru(enum lru_list l)
120 {
121         return (l == LRU_ACTIVE);
122 }
123
124 struct per_cpu_pages {
125         int count;              /* number of pages in the list */
126         int high;               /* high watermark, emptying needed */
127         int batch;              /* chunk size for buddy add/remove */
128         struct list_head list;  /* the list of pages */
129 };
130
131 struct per_cpu_pageset {
132         struct per_cpu_pages pcp;
133 #ifdef CONFIG_NUMA
134         s8 expire;
135 #endif
136 #ifdef CONFIG_SMP
137         s8 stat_threshold;
138         s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
139 #endif
140 } ____cacheline_aligned_in_smp;
141
142 #ifdef CONFIG_NUMA
143 #define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)])
144 #else
145 #define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)])
146 #endif
147
148 #endif /* !__GENERATING_BOUNDS.H */
149
150 enum zone_type {
151 #ifdef CONFIG_ZONE_DMA
152         /*
153          * ZONE_DMA is used when there are devices that are not able
154          * to do DMA to all of addressable memory (ZONE_NORMAL). Then we
155          * carve out the portion of memory that is needed for these devices.
156          * The range is arch specific.
157          *
158          * Some examples
159          *
160          * Architecture         Limit
161          * ---------------------------
162          * parisc, ia64, sparc  <4G
163          * s390                 <2G
164          * arm                  Various
165          * alpha                Unlimited or 0-16MB.
166          *
167          * i386, x86_64 and multiple other arches
168          *                      <16M.
169          */
170         ZONE_DMA,
171 #endif
172 #ifdef CONFIG_ZONE_DMA32
173         /*
174          * x86_64 needs two ZONE_DMAs because it supports devices that are
175          * only able to do DMA to the lower 16M but also 32 bit devices that
176          * can only do DMA areas below 4G.
177          */
178         ZONE_DMA32,
179 #endif
180         /*
181          * Normal addressable memory is in ZONE_NORMAL. DMA operations can be
182          * performed on pages in ZONE_NORMAL if the DMA devices support
183          * transfers to all addressable memory.
184          */
185         ZONE_NORMAL,
186 #ifdef CONFIG_HIGHMEM
187         /*
188          * A memory area that is only addressable by the kernel through
189          * mapping portions into its own address space. This is for example
190          * used by i386 to allow the kernel to address the memory beyond
191          * 900MB. The kernel will set up special mappings (page
192          * table entries on i386) for each page that the kernel needs to
193          * access.
194          */
195         ZONE_HIGHMEM,
196 #endif
197         ZONE_MOVABLE,
198         __MAX_NR_ZONES
199 };
200
201 #ifndef __GENERATING_BOUNDS_H
202
203 /*
204  * When a memory allocation must conform to specific limitations (such
205  * as being suitable for DMA) the caller will pass in hints to the
206  * allocator in the gfp_mask, in the zone modifier bits.  These bits
207  * are used to select a priority ordered list of memory zones which
208  * match the requested limits. See gfp_zone() in include/linux/gfp.h
209  */
210
211 #if MAX_NR_ZONES < 2
212 #define ZONES_SHIFT 0
213 #elif MAX_NR_ZONES <= 2
214 #define ZONES_SHIFT 1
215 #elif MAX_NR_ZONES <= 4
216 #define ZONES_SHIFT 2
217 #else
218 #error ZONES_SHIFT -- too many zones configured adjust calculation
219 #endif
220
221 struct zone {
222         /* Fields commonly accessed by the page allocator */
223         unsigned long           pages_min, pages_low, pages_high;
224         /*
225          * We don't know if the memory that we're going to allocate will be freeable
226          * or/and it will be released eventually, so to avoid totally wasting several
227          * GB of ram we must reserve some of the lower zone memory (otherwise we risk
228          * to run OOM on the lower zones despite there's tons of freeable ram
229          * on the higher zones). This array is recalculated at runtime if the
230          * sysctl_lowmem_reserve_ratio sysctl changes.
231          */
232         unsigned long           lowmem_reserve[MAX_NR_ZONES];
233
234 #ifdef CONFIG_NUMA
235         int node;
236         /*
237          * zone reclaim becomes active if more unmapped pages exist.
238          */
239         unsigned long           min_unmapped_pages;
240         unsigned long           min_slab_pages;
241         struct per_cpu_pageset  *pageset[NR_CPUS];
242 #else
243         struct per_cpu_pageset  pageset[NR_CPUS];
244 #endif
245         /*
246          * free areas of different sizes
247          */
248         spinlock_t              lock;
249 #ifdef CONFIG_MEMORY_HOTPLUG
250         /* see spanned/present_pages for more description */
251         seqlock_t               span_seqlock;
252 #endif
253         struct free_area        free_area[MAX_ORDER];
254
255 #ifndef CONFIG_SPARSEMEM
256         /*
257          * Flags for a pageblock_nr_pages block. See pageblock-flags.h.
258          * In SPARSEMEM, this map is stored in struct mem_section
259          */
260         unsigned long           *pageblock_flags;
261 #endif /* CONFIG_SPARSEMEM */
262
263
264         ZONE_PADDING(_pad1_)
265
266         /* Fields commonly accessed by the page reclaim scanner */
267         spinlock_t              lru_lock;       
268         struct {
269                 struct list_head list;
270                 unsigned long nr_scan;
271         } lru[NR_LRU_LISTS];
272         unsigned long           pages_scanned;     /* since last reclaim */
273         unsigned long           flags;             /* zone flags, see below */
274
275         /* Zone statistics */
276         atomic_long_t           vm_stat[NR_VM_ZONE_STAT_ITEMS];
277
278         /*
279          * prev_priority holds the scanning priority for this zone.  It is
280          * defined as the scanning priority at which we achieved our reclaim
281          * target at the previous try_to_free_pages() or balance_pgdat()
282          * invokation.
283          *
284          * We use prev_priority as a measure of how much stress page reclaim is
285          * under - it drives the swappiness decision: whether to unmap mapped
286          * pages.
287          *
288          * Access to both this field is quite racy even on uniprocessor.  But
289          * it is expected to average out OK.
290          */
291         int prev_priority;
292
293
294         ZONE_PADDING(_pad2_)
295         /* Rarely used or read-mostly fields */
296
297         /*
298          * wait_table           -- the array holding the hash table
299          * wait_table_hash_nr_entries   -- the size of the hash table array
300          * wait_table_bits      -- wait_table_size == (1 << wait_table_bits)
301          *
302          * The purpose of all these is to keep track of the people
303          * waiting for a page to become available and make them
304          * runnable again when possible. The trouble is that this
305          * consumes a lot of space, especially when so few things
306          * wait on pages at a given time. So instead of using
307          * per-page waitqueues, we use a waitqueue hash table.
308          *
309          * The bucket discipline is to sleep on the same queue when
310          * colliding and wake all in that wait queue when removing.
311          * When something wakes, it must check to be sure its page is
312          * truly available, a la thundering herd. The cost of a
313          * collision is great, but given the expected load of the
314          * table, they should be so rare as to be outweighed by the
315          * benefits from the saved space.
316          *
317          * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
318          * primary users of these fields, and in mm/page_alloc.c
319          * free_area_init_core() performs the initialization of them.
320          */
321         wait_queue_head_t       * wait_table;
322         unsigned long           wait_table_hash_nr_entries;
323         unsigned long           wait_table_bits;
324
325         /*
326          * Discontig memory support fields.
327          */
328         struct pglist_data      *zone_pgdat;
329         /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
330         unsigned long           zone_start_pfn;
331
332         /*
333          * zone_start_pfn, spanned_pages and present_pages are all
334          * protected by span_seqlock.  It is a seqlock because it has
335          * to be read outside of zone->lock, and it is done in the main
336          * allocator path.  But, it is written quite infrequently.
337          *
338          * The lock is declared along with zone->lock because it is
339          * frequently read in proximity to zone->lock.  It's good to
340          * give them a chance of being in the same cacheline.
341          */
342         unsigned long           spanned_pages;  /* total size, including holes */
343         unsigned long           present_pages;  /* amount of memory (excluding holes) */
344
345         /*
346          * rarely used fields:
347          */
348         const char              *name;
349 } ____cacheline_internodealigned_in_smp;
350
351 typedef enum {
352         ZONE_ALL_UNRECLAIMABLE,         /* all pages pinned */
353         ZONE_RECLAIM_LOCKED,            /* prevents concurrent reclaim */
354         ZONE_OOM_LOCKED,                /* zone is in OOM killer zonelist */
355 } zone_flags_t;
356
357 static inline void zone_set_flag(struct zone *zone, zone_flags_t flag)
358 {
359         set_bit(flag, &zone->flags);
360 }
361
362 static inline int zone_test_and_set_flag(struct zone *zone, zone_flags_t flag)
363 {
364         return test_and_set_bit(flag, &zone->flags);
365 }
366
367 static inline void zone_clear_flag(struct zone *zone, zone_flags_t flag)
368 {
369         clear_bit(flag, &zone->flags);
370 }
371
372 static inline int zone_is_all_unreclaimable(const struct zone *zone)
373 {
374         return test_bit(ZONE_ALL_UNRECLAIMABLE, &zone->flags);
375 }
376
377 static inline int zone_is_reclaim_locked(const struct zone *zone)
378 {
379         return test_bit(ZONE_RECLAIM_LOCKED, &zone->flags);
380 }
381
382 static inline int zone_is_oom_locked(const struct zone *zone)
383 {
384         return test_bit(ZONE_OOM_LOCKED, &zone->flags);
385 }
386
387 /*
388  * The "priority" of VM scanning is how much of the queues we will scan in one
389  * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
390  * queues ("queue_length >> 12") during an aging round.
391  */
392 #define DEF_PRIORITY 12
393
394 /* Maximum number of zones on a zonelist */
395 #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
396
397 #ifdef CONFIG_NUMA
398
399 /*
400  * The NUMA zonelists are doubled becausse we need zonelists that restrict the
401  * allocations to a single node for GFP_THISNODE.
402  *
403  * [0]  : Zonelist with fallback
404  * [1]  : No fallback (GFP_THISNODE)
405  */
406 #define MAX_ZONELISTS 2
407
408
409 /*
410  * We cache key information from each zonelist for smaller cache
411  * footprint when scanning for free pages in get_page_from_freelist().
412  *
413  * 1) The BITMAP fullzones tracks which zones in a zonelist have come
414  *    up short of free memory since the last time (last_fullzone_zap)
415  *    we zero'd fullzones.
416  * 2) The array z_to_n[] maps each zone in the zonelist to its node
417  *    id, so that we can efficiently evaluate whether that node is
418  *    set in the current tasks mems_allowed.
419  *
420  * Both fullzones and z_to_n[] are one-to-one with the zonelist,
421  * indexed by a zones offset in the zonelist zones[] array.
422  *
423  * The get_page_from_freelist() routine does two scans.  During the
424  * first scan, we skip zones whose corresponding bit in 'fullzones'
425  * is set or whose corresponding node in current->mems_allowed (which
426  * comes from cpusets) is not set.  During the second scan, we bypass
427  * this zonelist_cache, to ensure we look methodically at each zone.
428  *
429  * Once per second, we zero out (zap) fullzones, forcing us to
430  * reconsider nodes that might have regained more free memory.
431  * The field last_full_zap is the time we last zapped fullzones.
432  *
433  * This mechanism reduces the amount of time we waste repeatedly
434  * reexaming zones for free memory when they just came up low on
435  * memory momentarilly ago.
436  *
437  * The zonelist_cache struct members logically belong in struct
438  * zonelist.  However, the mempolicy zonelists constructed for
439  * MPOL_BIND are intentionally variable length (and usually much
440  * shorter).  A general purpose mechanism for handling structs with
441  * multiple variable length members is more mechanism than we want
442  * here.  We resort to some special case hackery instead.
443  *
444  * The MPOL_BIND zonelists don't need this zonelist_cache (in good
445  * part because they are shorter), so we put the fixed length stuff
446  * at the front of the zonelist struct, ending in a variable length
447  * zones[], as is needed by MPOL_BIND.
448  *
449  * Then we put the optional zonelist cache on the end of the zonelist
450  * struct.  This optional stuff is found by a 'zlcache_ptr' pointer in
451  * the fixed length portion at the front of the struct.  This pointer
452  * both enables us to find the zonelist cache, and in the case of
453  * MPOL_BIND zonelists, (which will just set the zlcache_ptr to NULL)
454  * to know that the zonelist cache is not there.
455  *
456  * The end result is that struct zonelists come in two flavors:
457  *  1) The full, fixed length version, shown below, and
458  *  2) The custom zonelists for MPOL_BIND.
459  * The custom MPOL_BIND zonelists have a NULL zlcache_ptr and no zlcache.
460  *
461  * Even though there may be multiple CPU cores on a node modifying
462  * fullzones or last_full_zap in the same zonelist_cache at the same
463  * time, we don't lock it.  This is just hint data - if it is wrong now
464  * and then, the allocator will still function, perhaps a bit slower.
465  */
466
467
468 struct zonelist_cache {
469         unsigned short z_to_n[MAX_ZONES_PER_ZONELIST];          /* zone->nid */
470         DECLARE_BITMAP(fullzones, MAX_ZONES_PER_ZONELIST);      /* zone full? */
471         unsigned long last_full_zap;            /* when last zap'd (jiffies) */
472 };
473 #else
474 #define MAX_ZONELISTS 1
475 struct zonelist_cache;
476 #endif
477
478 /*
479  * This struct contains information about a zone in a zonelist. It is stored
480  * here to avoid dereferences into large structures and lookups of tables
481  */
482 struct zoneref {
483         struct zone *zone;      /* Pointer to actual zone */
484         int zone_idx;           /* zone_idx(zoneref->zone) */
485 };
486
487 /*
488  * One allocation request operates on a zonelist. A zonelist
489  * is a list of zones, the first one is the 'goal' of the
490  * allocation, the other zones are fallback zones, in decreasing
491  * priority.
492  *
493  * If zlcache_ptr is not NULL, then it is just the address of zlcache,
494  * as explained above.  If zlcache_ptr is NULL, there is no zlcache.
495  * *
496  * To speed the reading of the zonelist, the zonerefs contain the zone index
497  * of the entry being read. Helper functions to access information given
498  * a struct zoneref are
499  *
500  * zonelist_zone()      - Return the struct zone * for an entry in _zonerefs
501  * zonelist_zone_idx()  - Return the index of the zone for an entry
502  * zonelist_node_idx()  - Return the index of the node for an entry
503  */
504 struct zonelist {
505         struct zonelist_cache *zlcache_ptr;                  // NULL or &zlcache
506         struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
507 #ifdef CONFIG_NUMA
508         struct zonelist_cache zlcache;                       // optional ...
509 #endif
510 };
511
512 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
513 struct node_active_region {
514         unsigned long start_pfn;
515         unsigned long end_pfn;
516         int nid;
517 };
518 #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
519
520 #ifndef CONFIG_DISCONTIGMEM
521 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
522 extern struct page *mem_map;
523 #endif
524
525 /*
526  * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
527  * (mostly NUMA machines?) to denote a higher-level memory zone than the
528  * zone denotes.
529  *
530  * On NUMA machines, each NUMA node would have a pg_data_t to describe
531  * it's memory layout.
532  *
533  * Memory statistics and page replacement data structures are maintained on a
534  * per-zone basis.
535  */
536 struct bootmem_data;
537 typedef struct pglist_data {
538         struct zone node_zones[MAX_NR_ZONES];
539         struct zonelist node_zonelists[MAX_ZONELISTS];
540         int nr_zones;
541 #ifdef CONFIG_FLAT_NODE_MEM_MAP
542         struct page *node_mem_map;
543 #endif
544         struct bootmem_data *bdata;
545 #ifdef CONFIG_MEMORY_HOTPLUG
546         /*
547          * Must be held any time you expect node_start_pfn, node_present_pages
548          * or node_spanned_pages stay constant.  Holding this will also
549          * guarantee that any pfn_valid() stays that way.
550          *
551          * Nests above zone->lock and zone->size_seqlock.
552          */
553         spinlock_t node_size_lock;
554 #endif
555         unsigned long node_start_pfn;
556         unsigned long node_present_pages; /* total number of physical pages */
557         unsigned long node_spanned_pages; /* total size of physical page
558                                              range, including holes */
559         int node_id;
560         wait_queue_head_t kswapd_wait;
561         struct task_struct *kswapd;
562         int kswapd_max_order;
563 } pg_data_t;
564
565 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
566 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
567 #ifdef CONFIG_FLAT_NODE_MEM_MAP
568 #define pgdat_page_nr(pgdat, pagenr)    ((pgdat)->node_mem_map + (pagenr))
569 #else
570 #define pgdat_page_nr(pgdat, pagenr)    pfn_to_page((pgdat)->node_start_pfn + (pagenr))
571 #endif
572 #define nid_page_nr(nid, pagenr)        pgdat_page_nr(NODE_DATA(nid),(pagenr))
573
574 #include <linux/memory_hotplug.h>
575
576 void get_zone_counts(unsigned long *active, unsigned long *inactive,
577                         unsigned long *free);
578 void build_all_zonelists(void);
579 void wakeup_kswapd(struct zone *zone, int order);
580 int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
581                 int classzone_idx, int alloc_flags);
582 enum memmap_context {
583         MEMMAP_EARLY,
584         MEMMAP_HOTPLUG,
585 };
586 extern int init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
587                                      unsigned long size,
588                                      enum memmap_context context);
589
590 #ifdef CONFIG_HAVE_MEMORY_PRESENT
591 void memory_present(int nid, unsigned long start, unsigned long end);
592 #else
593 static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
594 #endif
595
596 #ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
597 unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
598 #endif
599
600 /*
601  * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
602  */
603 #define zone_idx(zone)          ((zone) - (zone)->zone_pgdat->node_zones)
604
605 static inline int populated_zone(struct zone *zone)
606 {
607         return (!!zone->present_pages);
608 }
609
610 extern int movable_zone;
611
612 static inline int zone_movable_is_highmem(void)
613 {
614 #if defined(CONFIG_HIGHMEM) && defined(CONFIG_ARCH_POPULATES_NODE_MAP)
615         return movable_zone == ZONE_HIGHMEM;
616 #else
617         return 0;
618 #endif
619 }
620
621 static inline int is_highmem_idx(enum zone_type idx)
622 {
623 #ifdef CONFIG_HIGHMEM
624         return (idx == ZONE_HIGHMEM ||
625                 (idx == ZONE_MOVABLE && zone_movable_is_highmem()));
626 #else
627         return 0;
628 #endif
629 }
630
631 static inline int is_normal_idx(enum zone_type idx)
632 {
633         return (idx == ZONE_NORMAL);
634 }
635
636 /**
637  * is_highmem - helper function to quickly check if a struct zone is a 
638  *              highmem zone or not.  This is an attempt to keep references
639  *              to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
640  * @zone - pointer to struct zone variable
641  */
642 static inline int is_highmem(struct zone *zone)
643 {
644 #ifdef CONFIG_HIGHMEM
645         int zone_off = (char *)zone - (char *)zone->zone_pgdat->node_zones;
646         return zone_off == ZONE_HIGHMEM * sizeof(*zone) ||
647                (zone_off == ZONE_MOVABLE * sizeof(*zone) &&
648                 zone_movable_is_highmem());
649 #else
650         return 0;
651 #endif
652 }
653
654 static inline int is_normal(struct zone *zone)
655 {
656         return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
657 }
658
659 static inline int is_dma32(struct zone *zone)
660 {
661 #ifdef CONFIG_ZONE_DMA32
662         return zone == zone->zone_pgdat->node_zones + ZONE_DMA32;
663 #else
664         return 0;
665 #endif
666 }
667
668 static inline int is_dma(struct zone *zone)
669 {
670 #ifdef CONFIG_ZONE_DMA
671         return zone == zone->zone_pgdat->node_zones + ZONE_DMA;
672 #else
673         return 0;
674 #endif
675 }
676
677 /* These two functions are used to setup the per zone pages min values */
678 struct ctl_table;
679 struct file;
680 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *, 
681                                         void __user *, size_t *, loff_t *);
682 extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
683 int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
684                                         void __user *, size_t *, loff_t *);
685 int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file *,
686                                         void __user *, size_t *, loff_t *);
687 int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
688                         struct file *, void __user *, size_t *, loff_t *);
689 int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
690                         struct file *, void __user *, size_t *, loff_t *);
691
692 extern int numa_zonelist_order_handler(struct ctl_table *, int,
693                         struct file *, void __user *, size_t *, loff_t *);
694 extern char numa_zonelist_order[];
695 #define NUMA_ZONELIST_ORDER_LEN 16      /* string buffer size */
696
697 #include <linux/topology.h>
698 /* Returns the number of the current Node. */
699 #ifndef numa_node_id
700 #define numa_node_id()          (cpu_to_node(raw_smp_processor_id()))
701 #endif
702
703 #ifndef CONFIG_NEED_MULTIPLE_NODES
704
705 extern struct pglist_data contig_page_data;
706 #define NODE_DATA(nid)          (&contig_page_data)
707 #define NODE_MEM_MAP(nid)       mem_map
708
709 #else /* CONFIG_NEED_MULTIPLE_NODES */
710
711 #include <asm/mmzone.h>
712
713 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
714
715 extern struct pglist_data *first_online_pgdat(void);
716 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
717 extern struct zone *next_zone(struct zone *zone);
718
719 /**
720  * for_each_online_pgdat - helper macro to iterate over all online nodes
721  * @pgdat - pointer to a pg_data_t variable
722  */
723 #define for_each_online_pgdat(pgdat)                    \
724         for (pgdat = first_online_pgdat();              \
725              pgdat;                                     \
726              pgdat = next_online_pgdat(pgdat))
727 /**
728  * for_each_zone - helper macro to iterate over all memory zones
729  * @zone - pointer to struct zone variable
730  *
731  * The user only needs to declare the zone variable, for_each_zone
732  * fills it in.
733  */
734 #define for_each_zone(zone)                             \
735         for (zone = (first_online_pgdat())->node_zones; \
736              zone;                                      \
737              zone = next_zone(zone))
738
739 static inline struct zone *zonelist_zone(struct zoneref *zoneref)
740 {
741         return zoneref->zone;
742 }
743
744 static inline int zonelist_zone_idx(struct zoneref *zoneref)
745 {
746         return zoneref->zone_idx;
747 }
748
749 static inline int zonelist_node_idx(struct zoneref *zoneref)
750 {
751 #ifdef CONFIG_NUMA
752         /* zone_to_nid not available in this context */
753         return zoneref->zone->node;
754 #else
755         return 0;
756 #endif /* CONFIG_NUMA */
757 }
758
759 /**
760  * next_zones_zonelist - Returns the next zone at or below highest_zoneidx within the allowed nodemask using a cursor within a zonelist as a starting point
761  * @z - The cursor used as a starting point for the search
762  * @highest_zoneidx - The zone index of the highest zone to return
763  * @nodes - An optional nodemask to filter the zonelist with
764  * @zone - The first suitable zone found is returned via this parameter
765  *
766  * This function returns the next zone at or below a given zone index that is
767  * within the allowed nodemask using a cursor as the starting point for the
768  * search. The zoneref returned is a cursor that represents the current zone
769  * being examined. It should be advanced by one before calling
770  * next_zones_zonelist again.
771  */
772 struct zoneref *next_zones_zonelist(struct zoneref *z,
773                                         enum zone_type highest_zoneidx,
774                                         nodemask_t *nodes,
775                                         struct zone **zone);
776
777 /**
778  * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
779  * @zonelist - The zonelist to search for a suitable zone
780  * @highest_zoneidx - The zone index of the highest zone to return
781  * @nodes - An optional nodemask to filter the zonelist with
782  * @zone - The first suitable zone found is returned via this parameter
783  *
784  * This function returns the first zone at or below a given zone index that is
785  * within the allowed nodemask. The zoneref returned is a cursor that can be
786  * used to iterate the zonelist with next_zones_zonelist by advancing it by
787  * one before calling.
788  */
789 static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
790                                         enum zone_type highest_zoneidx,
791                                         nodemask_t *nodes,
792                                         struct zone **zone)
793 {
794         return next_zones_zonelist(zonelist->_zonerefs, highest_zoneidx, nodes,
795                                                                 zone);
796 }
797
798 /**
799  * for_each_zone_zonelist_nodemask - helper macro to iterate over valid zones in a zonelist at or below a given zone index and within a nodemask
800  * @zone - The current zone in the iterator
801  * @z - The current pointer within zonelist->zones being iterated
802  * @zlist - The zonelist being iterated
803  * @highidx - The zone index of the highest zone to return
804  * @nodemask - Nodemask allowed by the allocator
805  *
806  * This iterator iterates though all zones at or below a given zone index and
807  * within a given nodemask
808  */
809 #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
810         for (z = first_zones_zonelist(zlist, highidx, nodemask, &zone); \
811                 zone;                                                   \
812                 z = next_zones_zonelist(++z, highidx, nodemask, &zone)) \
813
814 /**
815  * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
816  * @zone - The current zone in the iterator
817  * @z - The current pointer within zonelist->zones being iterated
818  * @zlist - The zonelist being iterated
819  * @highidx - The zone index of the highest zone to return
820  *
821  * This iterator iterates though all zones at or below a given zone index.
822  */
823 #define for_each_zone_zonelist(zone, z, zlist, highidx) \
824         for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
825
826 #ifdef CONFIG_SPARSEMEM
827 #include <asm/sparsemem.h>
828 #endif
829
830 #if !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) && \
831         !defined(CONFIG_ARCH_POPULATES_NODE_MAP)
832 static inline unsigned long early_pfn_to_nid(unsigned long pfn)
833 {
834         return 0;
835 }
836 #endif
837
838 #ifdef CONFIG_FLATMEM
839 #define pfn_to_nid(pfn)         (0)
840 #endif
841
842 #define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
843 #define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
844
845 #ifdef CONFIG_SPARSEMEM
846
847 /*
848  * SECTION_SHIFT                #bits space required to store a section #
849  *
850  * PA_SECTION_SHIFT             physical address to/from section number
851  * PFN_SECTION_SHIFT            pfn to/from section number
852  */
853 #define SECTIONS_SHIFT          (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
854
855 #define PA_SECTION_SHIFT        (SECTION_SIZE_BITS)
856 #define PFN_SECTION_SHIFT       (SECTION_SIZE_BITS - PAGE_SHIFT)
857
858 #define NR_MEM_SECTIONS         (1UL << SECTIONS_SHIFT)
859
860 #define PAGES_PER_SECTION       (1UL << PFN_SECTION_SHIFT)
861 #define PAGE_SECTION_MASK       (~(PAGES_PER_SECTION-1))
862
863 #define SECTION_BLOCKFLAGS_BITS \
864         ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
865
866 #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
867 #error Allocator MAX_ORDER exceeds SECTION_SIZE
868 #endif
869
870 struct page;
871 struct mem_section {
872         /*
873          * This is, logically, a pointer to an array of struct
874          * pages.  However, it is stored with some other magic.
875          * (see sparse.c::sparse_init_one_section())
876          *
877          * Additionally during early boot we encode node id of
878          * the location of the section here to guide allocation.
879          * (see sparse.c::memory_present())
880          *
881          * Making it a UL at least makes someone do a cast
882          * before using it wrong.
883          */
884         unsigned long section_mem_map;
885
886         /* See declaration of similar field in struct zone */
887         unsigned long *pageblock_flags;
888 };
889
890 #ifdef CONFIG_SPARSEMEM_EXTREME
891 #define SECTIONS_PER_ROOT       (PAGE_SIZE / sizeof (struct mem_section))
892 #else
893 #define SECTIONS_PER_ROOT       1
894 #endif
895
896 #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
897 #define NR_SECTION_ROOTS        (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
898 #define SECTION_ROOT_MASK       (SECTIONS_PER_ROOT - 1)
899
900 #ifdef CONFIG_SPARSEMEM_EXTREME
901 extern struct mem_section *mem_section[NR_SECTION_ROOTS];
902 #else
903 extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
904 #endif
905
906 static inline struct mem_section *__nr_to_section(unsigned long nr)
907 {
908         if (!mem_section[SECTION_NR_TO_ROOT(nr)])
909                 return NULL;
910         return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
911 }
912 extern int __section_nr(struct mem_section* ms);
913 extern unsigned long usemap_size(void);
914
915 /*
916  * We use the lower bits of the mem_map pointer to store
917  * a little bit of information.  There should be at least
918  * 3 bits here due to 32-bit alignment.
919  */
920 #define SECTION_MARKED_PRESENT  (1UL<<0)
921 #define SECTION_HAS_MEM_MAP     (1UL<<1)
922 #define SECTION_MAP_LAST_BIT    (1UL<<2)
923 #define SECTION_MAP_MASK        (~(SECTION_MAP_LAST_BIT-1))
924 #define SECTION_NID_SHIFT       2
925
926 static inline struct page *__section_mem_map_addr(struct mem_section *section)
927 {
928         unsigned long map = section->section_mem_map;
929         map &= SECTION_MAP_MASK;
930         return (struct page *)map;
931 }
932
933 static inline int present_section(struct mem_section *section)
934 {
935         return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
936 }
937
938 static inline int present_section_nr(unsigned long nr)
939 {
940         return present_section(__nr_to_section(nr));
941 }
942
943 static inline int valid_section(struct mem_section *section)
944 {
945         return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
946 }
947
948 static inline int valid_section_nr(unsigned long nr)
949 {
950         return valid_section(__nr_to_section(nr));
951 }
952
953 static inline struct mem_section *__pfn_to_section(unsigned long pfn)
954 {
955         return __nr_to_section(pfn_to_section_nr(pfn));
956 }
957
958 static inline int pfn_valid(unsigned long pfn)
959 {
960         if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
961                 return 0;
962         return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
963 }
964
965 static inline int pfn_present(unsigned long pfn)
966 {
967         if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
968                 return 0;
969         return present_section(__nr_to_section(pfn_to_section_nr(pfn)));
970 }
971
972 /*
973  * These are _only_ used during initialisation, therefore they
974  * can use __initdata ...  They could have names to indicate
975  * this restriction.
976  */
977 #ifdef CONFIG_NUMA
978 #define pfn_to_nid(pfn)                                                 \
979 ({                                                                      \
980         unsigned long __pfn_to_nid_pfn = (pfn);                         \
981         page_to_nid(pfn_to_page(__pfn_to_nid_pfn));                     \
982 })
983 #else
984 #define pfn_to_nid(pfn)         (0)
985 #endif
986
987 #define early_pfn_valid(pfn)    pfn_valid(pfn)
988 void sparse_init(void);
989 #else
990 #define sparse_init()   do {} while (0)
991 #define sparse_index_init(_sec, _nid)  do {} while (0)
992 #endif /* CONFIG_SPARSEMEM */
993
994 #ifdef CONFIG_NODES_SPAN_OTHER_NODES
995 #define early_pfn_in_nid(pfn, nid)      (early_pfn_to_nid(pfn) == (nid))
996 #else
997 #define early_pfn_in_nid(pfn, nid)      (1)
998 #endif
999
1000 #ifndef early_pfn_valid
1001 #define early_pfn_valid(pfn)    (1)
1002 #endif
1003
1004 void memory_present(int nid, unsigned long start, unsigned long end);
1005 unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
1006
1007 /*
1008  * If it is possible to have holes within a MAX_ORDER_NR_PAGES, then we
1009  * need to check pfn validility within that MAX_ORDER_NR_PAGES block.
1010  * pfn_valid_within() should be used in this case; we optimise this away
1011  * when we have no holes within a MAX_ORDER_NR_PAGES block.
1012  */
1013 #ifdef CONFIG_HOLES_IN_ZONE
1014 #define pfn_valid_within(pfn) pfn_valid(pfn)
1015 #else
1016 #define pfn_valid_within(pfn) (1)
1017 #endif
1018
1019 #endif /* !__GENERATING_BOUNDS.H */
1020 #endif /* !__ASSEMBLY__ */
1021 #endif /* _LINUX_MMZONE_H */