linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / include / linux / mmzone.h
index 8a23709..ebfc238 100644 (file)
@@ -11,6 +11,8 @@
 #include <linux/cache.h>
 #include <linux/threads.h>
 #include <linux/numa.h>
+#include <linux/init.h>
+#include <linux/seqlock.h>
 #include <asm/atomic.h>
 
 /* Free memory management - zoned buddy allocator.  */
 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
 #endif
 
-/*
- * system hash table size limits
- * - on large memory machines, we may want to allocate a bigger hash than that
- *   permitted by MAX_ORDER, so we allocate with the bootmem allocator, and are
- *   limited to this size
- */
-#if MAX_ORDER > 14
-#define MAX_SYS_HASH_TABLE_ORDER MAX_ORDER
-#else
-#define MAX_SYS_HASH_TABLE_ORDER 14
-#endif
-
 struct free_area {
        struct list_head        free_list;
-       unsigned long           *map;
+       unsigned long           nr_free;
 };
 
 struct pglist_data;
@@ -47,8 +37,8 @@ struct pglist_data;
  */
 #if defined(CONFIG_SMP)
 struct zone_padding {
-       int x;
-} ____cacheline_maxaligned_in_smp;
+       char x[0];
+} ____cacheline_internodealigned_in_smp;
 #define ZONE_PADDING(name)     struct zone_padding name;
 #else
 #define ZONE_PADDING(name)
@@ -56,7 +46,6 @@ struct zone_padding {
 
 struct per_cpu_pages {
        int count;              /* number of pages in the list */
-       int low;                /* low watermark, refill needed */
        int high;               /* high watermark, emptying needed */
        int batch;              /* chunk size for buddy add/remove */
        struct list_head list;  /* the list of pages */
@@ -74,11 +63,18 @@ struct per_cpu_pageset {
 #endif
 } ____cacheline_aligned_in_smp;
 
+#ifdef CONFIG_NUMA
+#define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)])
+#else
+#define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)])
+#endif
+
 #define ZONE_DMA               0
-#define ZONE_NORMAL            1
-#define ZONE_HIGHMEM           2
+#define ZONE_DMA32             1
+#define ZONE_NORMAL            2
+#define ZONE_HIGHMEM           3
 
-#define MAX_NR_ZONES           3       /* Sync this with ZONES_SHIFT */
+#define MAX_NR_ZONES           4       /* Sync this with ZONES_SHIFT */
 #define ZONES_SHIFT            2       /* ceil(log2(MAX_NR_ZONES)) */
 
 
@@ -94,9 +90,7 @@ struct per_cpu_pageset {
  * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
  * be 8 (2 ** 3) zonelists.  GFP_ZONETYPES defines the number of possible
  * combinations of zone modifiers in "zone modifier space".
- */
-#define GFP_ZONEMASK   0x03
-/*
+ *
  * As an optimisation any zone modifier bits which are only valid when
  * no other zone modifier bits are set (loners) should be placed in
  * the highest order bits of this field.  This allows us to reduce the
@@ -104,44 +98,58 @@ struct per_cpu_pageset {
  * of three zone modifier bits, we could require up to eight zonelists.
  * If the left most zone modifier is a "loner" then the highest valid
  * zonelist would be four allowing us to allocate only five zonelists.
- * Use the first form when the left most bit is not a "loner", otherwise
- * use the second.
+ * Use the first form for GFP_ZONETYPES when the left most bit is not
+ * a "loner", otherwise use the second.
+ *
+ * NOTE! Make sure this matches the zones in <linux/gfp.h>
  */
-/* #define GFP_ZONETYPES       (GFP_ZONEMASK + 1) */           /* Non-loner */
-#define GFP_ZONETYPES  ((GFP_ZONEMASK + 1) / 2 + 1)            /* Loner */
+#define GFP_ZONEMASK   0x07
+/* #define GFP_ZONETYPES       (GFP_ZONEMASK + 1) */           /* Non-loner */
+#define GFP_ZONETYPES  ((GFP_ZONEMASK + 1) / 2 + 1)            /* Loner */
 
 /*
  * On machines where it is needed (eg PCs) we divide physical memory
- * into multiple physical zones. On a PC we have 3 zones:
+ * into multiple physical zones. On a 32bit PC we have 4 zones:
  *
  * ZONE_DMA      < 16 MB       ISA DMA capable memory
+ * ZONE_DMA32       0 MB       Empty
  * ZONE_NORMAL 16-896 MB       direct mapped by the kernel
  * ZONE_HIGHMEM         > 896 MB       only page cache and user processes
  */
 
 struct zone {
-       /*
-        * Commonly accessed fields:
-        */
-       spinlock_t              lock;
+       /* Fields commonly accessed by the page allocator */
        unsigned long           free_pages;
        unsigned long           pages_min, pages_low, pages_high;
        /*
-        * protection[] is a pre-calculated number of extra pages that must be
-        * available in a zone in order for __alloc_pages() to allocate memory
-        * from the zone. i.e., for a GFP_KERNEL alloc of "order" there must
-        * be "(1<<order) + protection[ZONE_NORMAL]" free pages in the zone
-        * for us to choose to allocate the page from that zone.
-        *
-        * It uses both min_free_kbytes and sysctl_lower_zone_protection.
-        * The protection values are recalculated if either of these values
-        * change.  The array elements are in zonelist order:
-        *      [0] == GFP_DMA, [1] == GFP_KERNEL, [2] == GFP_HIGHMEM.
+        * We don't know if the memory that we're going to allocate will be freeable
+        * or/and it will be released eventually, so to avoid totally wasting several
+        * GB of ram we must reserve some of the lower zone memory (otherwise we risk
+        * to run OOM on the lower zones despite there's tons of freeable ram
+        * on the higher zones). This array is recalculated at runtime if the
+        * sysctl_lowmem_reserve_ratio sysctl changes.
         */
-       unsigned long           protection[MAX_NR_ZONES];
+       unsigned long           lowmem_reserve[MAX_NR_ZONES];
+
+#ifdef CONFIG_NUMA
+       struct per_cpu_pageset  *pageset[NR_CPUS];
+#else
+       struct per_cpu_pageset  pageset[NR_CPUS];
+#endif
+       /*
+        * free areas of different sizes
+        */
+       spinlock_t              lock;
+#ifdef CONFIG_MEMORY_HOTPLUG
+       /* see spanned/present_pages for more description */
+       seqlock_t               span_seqlock;
+#endif
+       struct free_area        free_area[MAX_ORDER];
+
 
        ZONE_PADDING(_pad1_)
 
+       /* Fields commonly accessed by the page reclaim scanner */
        spinlock_t              lru_lock;       
        struct list_head        active_list;
        struct list_head        inactive_list;
@@ -149,10 +157,18 @@ struct zone {
        unsigned long           nr_scan_inactive;
        unsigned long           nr_active;
        unsigned long           nr_inactive;
-       int                     all_unreclaimable; /* All pages pinned */
        unsigned long           pages_scanned;     /* since last reclaim */
+       int                     all_unreclaimable; /* All pages pinned */
 
-       ZONE_PADDING(_pad2_)
+       /* A count of how many reclaimers are scanning this zone */
+       atomic_t                reclaim_in_progress;
+
+       /*
+        * timestamp (in jiffies) of the last zone reclaim that did not
+        * result in freeing of pages. This is used to avoid repeated scans
+        * if all memory in the zone is in use.
+        */
+       unsigned long           last_unsuccessful_zone_reclaim;
 
        /*
         * prev_priority holds the scanning priority for this zone.  It is
@@ -173,10 +189,9 @@ struct zone {
        int temp_priority;
        int prev_priority;
 
-       /*
-        * free areas of different sizes
-        */
-       struct free_area        free_area[MAX_ORDER];
+
+       ZONE_PADDING(_pad2_)
+       /* Rarely used or read-mostly fields */
 
        /*
         * wait_table           -- the array holding the hash table
@@ -206,10 +221,6 @@ struct zone {
        unsigned long           wait_table_size;
        unsigned long           wait_table_bits;
 
-       ZONE_PADDING(_pad3_)
-
-       struct per_cpu_pageset  pageset[NR_CPUS];
-
        /*
         * Discontig memory support fields.
         */
@@ -219,12 +230,23 @@ struct zone {
        unsigned long           zone_start_pfn;
 
        /*
-        * rarely used fields:
+        * zone_start_pfn, spanned_pages and present_pages are all
+        * protected by span_seqlock.  It is a seqlock because it has
+        * to be read outside of zone->lock, and it is done in the main
+        * allocator path.  But, it is written quite infrequently.
+        *
+        * The lock is declared along with zone->lock because it is
+        * frequently read in proximity to zone->lock.  It's good to
+        * give them a chance of being in the same cacheline.
         */
-       char                    *name;
        unsigned long           spanned_pages;  /* total size, including holes */
        unsigned long           present_pages;  /* amount of memory (excluding holes) */
-} ____cacheline_maxaligned_in_smp;
+
+       /*
+        * rarely used fields:
+        */
+       char                    *name;
+} ____cacheline_internodealigned_in_smp;
 
 
 /*
@@ -266,28 +288,62 @@ typedef struct pglist_data {
        struct zone node_zones[MAX_NR_ZONES];
        struct zonelist node_zonelists[GFP_ZONETYPES];
        int nr_zones;
+#ifdef CONFIG_FLAT_NODE_MEM_MAP
        struct page *node_mem_map;
+#endif
        struct bootmem_data *bdata;
+#ifdef CONFIG_MEMORY_HOTPLUG
+       /*
+        * Must be held any time you expect node_start_pfn, node_present_pages
+        * or node_spanned_pages stay constant.  Holding this will also
+        * guarantee that any pfn_valid() stays that way.
+        *
+        * Nests above zone->lock and zone->size_seqlock.
+        */
+       spinlock_t node_size_lock;
+#endif
        unsigned long node_start_pfn;
        unsigned long node_present_pages; /* total number of physical pages */
        unsigned long node_spanned_pages; /* total size of physical page
                                             range, including holes */
        int node_id;
        struct pglist_data *pgdat_next;
-       wait_queue_head_t       kswapd_wait;
+       wait_queue_head_t kswapd_wait;
        struct task_struct *kswapd;
+       int kswapd_max_order;
 } pg_data_t;
 
 #define node_present_pages(nid)        (NODE_DATA(nid)->node_present_pages)
 #define node_spanned_pages(nid)        (NODE_DATA(nid)->node_spanned_pages)
+#ifdef CONFIG_FLAT_NODE_MEM_MAP
+#define pgdat_page_nr(pgdat, pagenr)   ((pgdat)->node_mem_map + (pagenr))
+#else
+#define pgdat_page_nr(pgdat, pagenr)   pfn_to_page((pgdat)->node_start_pfn + (pagenr))
+#endif
+#define nid_page_nr(nid, pagenr)       pgdat_page_nr(NODE_DATA(nid),(pagenr))
+
+#include <linux/memory_hotplug.h>
 
-extern int numnodes;
 extern struct pglist_data *pgdat_list;
 
+void __get_zone_counts(unsigned long *active, unsigned long *inactive,
+                       unsigned long *free, struct pglist_data *pgdat);
 void get_zone_counts(unsigned long *active, unsigned long *inactive,
                        unsigned long *free);
 void build_all_zonelists(void);
-void wakeup_kswapd(struct zone *zone);
+void wakeup_kswapd(struct zone *zone, int order);
+int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
+               int classzone_idx, int alloc_flags);
+
+#ifdef CONFIG_HAVE_MEMORY_PRESENT
+void memory_present(int nid, unsigned long start, unsigned long end);
+#else
+static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
+#endif
+
+#ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
+unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
+#endif
 
 /*
  * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
@@ -316,7 +372,7 @@ static inline struct zone *next_zone(struct zone *zone)
 {
        pg_data_t *pgdat = zone->zone_pgdat;
 
-       if (zone - pgdat->node_zones < MAX_NR_ZONES - 1)
+       if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
                zone++;
        else if (pgdat->pgdat_next) {
                pgdat = pgdat->pgdat_next;
@@ -345,6 +401,11 @@ static inline struct zone *next_zone(struct zone *zone)
 #define for_each_zone(zone) \
        for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
 
+static inline int populated_zone(struct zone *zone)
+{
+       return (!!zone->present_pages);
+}
+
 static inline int is_highmem_idx(int idx)
 {
        return (idx == ZONE_HIGHMEM);
@@ -354,6 +415,7 @@ static inline int is_normal_idx(int idx)
 {
        return (idx == ZONE_NORMAL);
 }
+
 /**
  * is_highmem - helper function to quickly check if a struct zone is a 
  *              highmem zone or not.  This is an attempt to keep references
@@ -362,12 +424,22 @@ static inline int is_normal_idx(int idx)
  */
 static inline int is_highmem(struct zone *zone)
 {
-       return (is_highmem_idx(zone - zone->zone_pgdat->node_zones));
+       return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM;
 }
 
 static inline int is_normal(struct zone *zone)
 {
-       return (is_normal_idx(zone - zone->zone_pgdat->node_zones));
+       return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
+}
+
+static inline int is_dma32(struct zone *zone)
+{
+       return zone == zone->zone_pgdat->node_zones + ZONE_DMA32;
+}
+
+static inline int is_dma(struct zone *zone)
+{
+       return zone == zone->zone_pgdat->node_zones + ZONE_DMA;
 }
 
 /* These two functions are used to setup the per zone pages min values */
@@ -375,80 +447,208 @@ struct ctl_table;
 struct file;
 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *, 
                                        void __user *, size_t *, loff_t *);
-int lower_zone_protection_sysctl_handler(struct ctl_table *, int, struct file *,
+extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
+int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
+                                       void __user *, size_t *, loff_t *);
+int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *, int, struct file *,
                                        void __user *, size_t *, loff_t *);
 
 #include <linux/topology.h>
 /* Returns the number of the current Node. */
-#define numa_node_id()         (cpu_to_node(smp_processor_id()))
+#ifndef numa_node_id
+#define numa_node_id()         (cpu_to_node(raw_smp_processor_id()))
+#endif
 
-#ifndef CONFIG_DISCONTIGMEM
+#ifndef CONFIG_NEED_MULTIPLE_NODES
 
 extern struct pglist_data contig_page_data;
 #define NODE_DATA(nid)         (&contig_page_data)
 #define NODE_MEM_MAP(nid)      mem_map
 #define MAX_NODES_SHIFT                1
-#define pfn_to_nid(pfn)                (0)
 
-#else /* CONFIG_DISCONTIGMEM */
+#else /* CONFIG_NEED_MULTIPLE_NODES */
 
 #include <asm/mmzone.h>
 
-#if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
+#endif /* !CONFIG_NEED_MULTIPLE_NODES */
+
+#ifdef CONFIG_SPARSEMEM
+#include <asm/sparsemem.h>
+#endif
+
+#if BITS_PER_LONG == 32
 /*
- * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
- * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
+ * with 32 bit page->flags field, we reserve 9 bits for node/zone info.
+ * there are 4 zones (3 bits) and this leaves 9-3=6 bits for nodes.
  */
-#define MAX_NODES_SHIFT                6
+#define FLAGS_RESERVED         9
+
 #elif BITS_PER_LONG == 64
 /*
  * with 64 bit flags field, there's plenty of room.
  */
-#define MAX_NODES_SHIFT                10
+#define FLAGS_RESERVED         32
+
+#else
+
+#error BITS_PER_LONG not defined
+
+#endif
+
+#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
+#define early_pfn_to_nid(nid)  (0UL)
+#endif
+
+#ifdef CONFIG_FLATMEM
+#define pfn_to_nid(pfn)                (0)
+#endif
+
+#define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
+#define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
+
+#ifdef CONFIG_SPARSEMEM
+
+/*
+ * SECTION_SHIFT               #bits space required to store a section #
+ *
+ * PA_SECTION_SHIFT            physical address to/from section number
+ * PFN_SECTION_SHIFT           pfn to/from section number
+ */
+#define SECTIONS_SHIFT         (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
+
+#define PA_SECTION_SHIFT       (SECTION_SIZE_BITS)
+#define PFN_SECTION_SHIFT      (SECTION_SIZE_BITS - PAGE_SHIFT)
+
+#define NR_MEM_SECTIONS                (1UL << SECTIONS_SHIFT)
+
+#define PAGES_PER_SECTION       (1UL << PFN_SECTION_SHIFT)
+#define PAGE_SECTION_MASK      (~(PAGES_PER_SECTION-1))
+
+#if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
+#error Allocator MAX_ORDER exceeds SECTION_SIZE
 #endif
 
-#endif /* !CONFIG_DISCONTIGMEM */
+struct page;
+struct mem_section {
+       /*
+        * This is, logically, a pointer to an array of struct
+        * pages.  However, it is stored with some other magic.
+        * (see sparse.c::sparse_init_one_section())
+        *
+        * Making it a UL at least makes someone do a cast
+        * before using it wrong.
+        */
+       unsigned long section_mem_map;
+};
 
-#if NODES_SHIFT > MAX_NODES_SHIFT
-#error NODES_SHIFT > MAX_NODES_SHIFT
+#ifdef CONFIG_SPARSEMEM_EXTREME
+#define SECTIONS_PER_ROOT       (PAGE_SIZE / sizeof (struct mem_section))
+#else
+#define SECTIONS_PER_ROOT      1
 #endif
 
-/* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
-#define MAX_ZONES_SHIFT                2
+#define SECTION_NR_TO_ROOT(sec)        ((sec) / SECTIONS_PER_ROOT)
+#define NR_SECTION_ROOTS       (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
+#define SECTION_ROOT_MASK      (SECTIONS_PER_ROOT - 1)
 
-#if ZONES_SHIFT > MAX_ZONES_SHIFT
-#error ZONES_SHIFT > MAX_ZONES_SHIFT
+#ifdef CONFIG_SPARSEMEM_EXTREME
+extern struct mem_section *mem_section[NR_SECTION_ROOTS];
+#else
+extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
 #endif
 
-extern DECLARE_BITMAP(node_online_map, MAX_NUMNODES);
+static inline struct mem_section *__nr_to_section(unsigned long nr)
+{
+       if (!mem_section[SECTION_NR_TO_ROOT(nr)])
+               return NULL;
+       return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
+}
+extern int __section_nr(struct mem_section* ms);
+
+/*
+ * We use the lower bits of the mem_map pointer to store
+ * a little bit of information.  There should be at least
+ * 3 bits here due to 32-bit alignment.
+ */
+#define        SECTION_MARKED_PRESENT  (1UL<<0)
+#define SECTION_HAS_MEM_MAP    (1UL<<1)
+#define SECTION_MAP_LAST_BIT   (1UL<<2)
+#define SECTION_MAP_MASK       (~(SECTION_MAP_LAST_BIT-1))
+
+static inline struct page *__section_mem_map_addr(struct mem_section *section)
+{
+       unsigned long map = section->section_mem_map;
+       map &= SECTION_MAP_MASK;
+       return (struct page *)map;
+}
+
+static inline int valid_section(struct mem_section *section)
+{
+       return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
+}
+
+static inline int section_has_mem_map(struct mem_section *section)
+{
+       return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
+}
 
-#if defined(CONFIG_DISCONTIGMEM) || defined(CONFIG_NUMA)
+static inline int valid_section_nr(unsigned long nr)
+{
+       return valid_section(__nr_to_section(nr));
+}
 
-#define node_online(node)      test_bit(node, node_online_map)
-#define node_set_online(node)  set_bit(node, node_online_map)
-#define node_set_offline(node) clear_bit(node, node_online_map)
-static inline unsigned int num_online_nodes(void)
+static inline struct mem_section *__pfn_to_section(unsigned long pfn)
 {
-       int i, num = 0;
+       return __nr_to_section(pfn_to_section_nr(pfn));
+}
 
-       for(i = 0; i < MAX_NUMNODES; i++){
-               if (node_online(i))
-                       num++;
-       }
-       return num;
+#define pfn_to_page(pfn)                                               \
+({                                                                     \
+       unsigned long __pfn = (pfn);                                    \
+       __section_mem_map_addr(__pfn_to_section(__pfn)) + __pfn;        \
+})
+#define page_to_pfn(page)                                              \
+({                                                                     \
+       page - __section_mem_map_addr(__nr_to_section(                  \
+               page_to_section(page)));                                \
+})
+
+static inline int pfn_valid(unsigned long pfn)
+{
+       if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
+               return 0;
+       return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
 }
 
-#else /* !CONFIG_DISCONTIGMEM && !CONFIG_NUMA */
+/*
+ * These are _only_ used during initialisation, therefore they
+ * can use __initdata ...  They could have names to indicate
+ * this restriction.
+ */
+#ifdef CONFIG_NUMA
+#define pfn_to_nid(pfn)                                                        \
+({                                                                     \
+       unsigned long __pfn_to_nid_pfn = (pfn);                         \
+       page_to_nid(pfn_to_page(__pfn_to_nid_pfn));                     \
+})
+#else
+#define pfn_to_nid(pfn)                (0)
+#endif
+
+#define early_pfn_valid(pfn)   pfn_valid(pfn)
+void sparse_init(void);
+#else
+#define sparse_init()  do {} while (0)
+#define sparse_index_init(_sec, _nid)  do {} while (0)
+#endif /* CONFIG_SPARSEMEM */
+
+#ifndef early_pfn_valid
+#define early_pfn_valid(pfn)   (1)
+#endif
 
-#define node_online(node) \
-       ({ BUG_ON((node) != 0); test_bit(node, node_online_map); })
-#define node_set_online(node) \
-       ({ BUG_ON((node) != 0); set_bit(node, node_online_map); })
-#define node_set_offline(node) \
-       ({ BUG_ON((node) != 0); clear_bit(node, node_online_map); })
-#define num_online_nodes()     1
+void memory_present(int nid, unsigned long start, unsigned long end);
+unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
 
-#endif /* CONFIG_DISCONTIGMEM || CONFIG_NUMA */
 #endif /* !__ASSEMBLY__ */
 #endif /* __KERNEL__ */
 #endif /* _LINUX_MMZONE_H */