Merge branch 'for-linus' of git://one.firstfloor.org/home/andi/git/linux-2.6
[sfrench/cifs-2.6.git] / mm / slab.c
index 7a48eb1a60c834bf3a3b9698f43a82d8ebcae1e8..3c4a7e34eddc4de763feea96ce1b654f06bd9052 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -86,7 +86,6 @@
  *     All object allocations for a node occur from node specific slab lists.
  */
 
-#include       <linux/config.h>
 #include       <linux/slab.h>
 #include       <linux/mm.h>
 #include       <linux/poison.h>
@@ -884,7 +883,7 @@ static void init_reap_node(int cpu)
        if (node == MAX_NUMNODES)
                node = first_node(node_online_map);
 
-       __get_cpu_var(reap_node) = node;
+       per_cpu(reap_node, cpu) = node;
 }
 
 static void next_reap_node(void)
@@ -972,7 +971,39 @@ static int transfer_objects(struct array_cache *to,
        return nr;
 }
 
-#ifdef CONFIG_NUMA
+#ifndef CONFIG_NUMA
+
+#define drain_alien_cache(cachep, alien) do { } while (0)
+#define reap_alien(cachep, l3) do { } while (0)
+
+static inline struct array_cache **alloc_alien_cache(int node, int limit)
+{
+       return (struct array_cache **)BAD_ALIEN_MAGIC;
+}
+
+static inline void free_alien_cache(struct array_cache **ac_ptr)
+{
+}
+
+static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
+{
+       return 0;
+}
+
+static inline void *alternate_node_alloc(struct kmem_cache *cachep,
+               gfp_t flags)
+{
+       return NULL;
+}
+
+static inline void *__cache_alloc_node(struct kmem_cache *cachep,
+                gfp_t flags, int nodeid)
+{
+       return NULL;
+}
+
+#else  /* CONFIG_NUMA */
+
 static void *__cache_alloc_node(struct kmem_cache *, gfp_t, int);
 static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
 
@@ -1075,15 +1106,18 @@ static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
        int nodeid = slabp->nodeid;
        struct kmem_list3 *l3;
        struct array_cache *alien = NULL;
+       int node;
+
+       node = numa_node_id();
 
        /*
         * Make sure we are not freeing a object from another node to the array
         * cache on this cpu.
         */
-       if (likely(slabp->nodeid == numa_node_id()))
+       if (likely(slabp->nodeid == node))
                return 0;
 
-       l3 = cachep->nodelists[numa_node_id()];
+       l3 = cachep->nodelists[node];
        STATS_INC_NODEFREES(cachep);
        if (l3->alien && l3->alien[nodeid]) {
                alien = l3->alien[nodeid];
@@ -1101,26 +1135,6 @@ static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
        }
        return 1;
 }
-
-#else
-
-#define drain_alien_cache(cachep, alien) do { } while (0)
-#define reap_alien(cachep, l3) do { } while (0)
-
-static inline struct array_cache **alloc_alien_cache(int node, int limit)
-{
-       return (struct array_cache **)BAD_ALIEN_MAGIC;
-}
-
-static inline void free_alien_cache(struct array_cache **ac_ptr)
-{
-}
-
-static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
-{
-       return 0;
-}
-
 #endif
 
 static int __cpuinit cpuup_callback(struct notifier_block *nfb,
@@ -1314,7 +1328,6 @@ static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
 {
        struct kmem_list3 *ptr;
 
-       BUG_ON(cachep->nodelists[nodeid] != list);
        ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
        BUG_ON(!ptr);
 
@@ -1341,6 +1354,7 @@ void __init kmem_cache_init(void)
        struct cache_names *names;
        int i;
        int order;
+       int node;
 
        for (i = 0; i < NUM_INIT_LISTS; i++) {
                kmem_list3_init(&initkmem_list3[i]);
@@ -1375,12 +1389,14 @@ void __init kmem_cache_init(void)
         * 6) Resize the head arrays of the kmalloc caches to their final sizes.
         */
 
+       node = numa_node_id();
+
        /* 1) create the cache_cache */
        INIT_LIST_HEAD(&cache_chain);
        list_add(&cache_cache.next, &cache_chain);
        cache_cache.colour_off = cache_line_size();
        cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
-       cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
+       cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
 
        cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
                                        cache_line_size());
@@ -1485,19 +1501,18 @@ void __init kmem_cache_init(void)
        }
        /* 5) Replace the bootstrap kmem_list3's */
        {
-               int node;
+               int nid;
+
                /* Replace the static kmem_list3 structures for the boot cpu */
-               init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
-                         numa_node_id());
+               init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
 
-               for_each_online_node(node) {
+               for_each_online_node(nid) {
                        init_list(malloc_sizes[INDEX_AC].cs_cachep,
-                                 &initkmem_list3[SIZE_AC + node], node);
+                                 &initkmem_list3[SIZE_AC + nid], nid);
 
                        if (INDEX_AC != INDEX_L3) {
                                init_list(malloc_sizes[INDEX_L3].cs_cachep,
-                                         &initkmem_list3[SIZE_L3 + node],
-                                         node);
+                                         &initkmem_list3[SIZE_L3 + nid], nid);
                        }
                }
        }
@@ -1564,7 +1579,13 @@ static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
         */
        flags |= __GFP_COMP;
 #endif
-       flags |= cachep->gfpflags;
+
+       /*
+        * Under NUMA we want memory on the indicated node. We will handle
+        * the needed fallback ourselves since we want to serve from our
+        * per node object lists first for other nodes.
+        */
+       flags |= cachep->gfpflags | GFP_THISNODE;
 
        page = alloc_pages_node(nodeid, flags, cachep->gfporder);
        if (!page)
@@ -1665,10 +1686,32 @@ static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
 static void dump_line(char *data, int offset, int limit)
 {
        int i;
+       unsigned char error = 0;
+       int bad_count = 0;
+
        printk(KERN_ERR "%03x:", offset);
-       for (i = 0; i < limit; i++)
+       for (i = 0; i < limit; i++) {
+               if (data[offset + i] != POISON_FREE) {
+                       error = data[offset + i];
+                       bad_count++;
+               }
                printk(" %02x", (unsigned char)data[offset + i]);
+       }
        printk("\n");
+
+       if (bad_count == 1) {
+               error ^= POISON_FREE;
+               if (!(error & (error - 1))) {
+                       printk(KERN_ERR "Single bit error detected. Probably "
+                                       "bad RAM.\n");
+#ifdef CONFIG_X86
+                       printk(KERN_ERR "Run memtest86+ or a similar memory "
+                                       "test tool.\n");
+#else
+                       printk(KERN_ERR "Run a memory test tool.\n");
+#endif
+               }
+       }
 }
 #endif
 
@@ -2442,7 +2485,6 @@ EXPORT_SYMBOL(kmem_cache_shrink);
  * @cachep: the cache to destroy
  *
  * Remove a struct kmem_cache object from the slab cache.
- * Returns 0 on success.
  *
  * It is expected this function will be called by a module when it is
  * unloaded.  This will remove the cache completely, and avoid a duplicate
@@ -2454,7 +2496,7 @@ EXPORT_SYMBOL(kmem_cache_shrink);
  * The caller must guarantee that noone will allocate memory from the cache
  * during the kmem_cache_destroy().
  */
-int kmem_cache_destroy(struct kmem_cache *cachep)
+void kmem_cache_destroy(struct kmem_cache *cachep)
 {
        BUG_ON(!cachep || in_interrupt());
 
@@ -2475,7 +2517,7 @@ int kmem_cache_destroy(struct kmem_cache *cachep)
                list_add(&cachep->next, &cache_chain);
                mutex_unlock(&cache_chain_mutex);
                unlock_cpu_hotplug();
-               return 1;
+               return;
        }
 
        if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
@@ -2483,7 +2525,6 @@ int kmem_cache_destroy(struct kmem_cache *cachep)
 
        __kmem_cache_destroy(cachep);
        unlock_cpu_hotplug();
-       return 0;
 }
 EXPORT_SYMBOL(kmem_cache_destroy);
 
@@ -2881,6 +2922,9 @@ static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
        int batchcount;
        struct kmem_list3 *l3;
        struct array_cache *ac;
+       int node;
+
+       node = numa_node_id();
 
        check_irq_off();
        ac = cpu_cache_get(cachep);
@@ -2894,7 +2938,7 @@ retry:
                 */
                batchcount = BATCHREFILL_LIMIT;
        }
-       l3 = cachep->nodelists[numa_node_id()];
+       l3 = cachep->nodelists[node];
 
        BUG_ON(ac->avail > 0 || !l3);
        spin_lock(&l3->list_lock);
@@ -2924,7 +2968,7 @@ retry:
                        STATS_SET_HIGH(cachep);
 
                        ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
-                                                           numa_node_id());
+                                                           node);
                }
                check_slabp(cachep, slabp);
 
@@ -2943,7 +2987,7 @@ alloc_done:
 
        if (unlikely(!ac->avail)) {
                int x;
-               x = cache_grow(cachep, flags, numa_node_id());
+               x = cache_grow(cachep, flags, node);
 
                /* cache_grow can reenable interrupts, then ac could change. */
                ac = cpu_cache_get(cachep);
@@ -3030,14 +3074,6 @@ static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
        void *objp;
        struct array_cache *ac;
 
-#ifdef CONFIG_NUMA
-       if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
-               objp = alternate_node_alloc(cachep, flags);
-               if (objp != NULL)
-                       return objp;
-       }
-#endif
-
        check_irq_off();
        ac = cpu_cache_get(cachep);
        if (likely(ac->avail)) {
@@ -3055,12 +3091,24 @@ static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
                                                gfp_t flags, void *caller)
 {
        unsigned long save_flags;
-       void *objp;
+       void *objp = NULL;
 
        cache_alloc_debugcheck_before(cachep, flags);
 
        local_irq_save(save_flags);
-       objp = ____cache_alloc(cachep, flags);
+
+       if (unlikely(NUMA_BUILD &&
+                       current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY)))
+               objp = alternate_node_alloc(cachep, flags);
+
+       if (!objp)
+               objp = ____cache_alloc(cachep, flags);
+       /*
+        * We may just have run out of memory on the local node.
+        * __cache_alloc_node() knows how to locate memory on other nodes
+        */
+       if (NUMA_BUILD && !objp)
+               objp = __cache_alloc_node(cachep, flags, numa_node_id());
        local_irq_restore(save_flags);
        objp = cache_alloc_debugcheck_after(cachep, flags, objp,
                                            caller);
@@ -3079,7 +3127,7 @@ static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
        int nid_alloc, nid_here;
 
-       if (in_interrupt())
+       if (in_interrupt() || (flags & __GFP_THISNODE))
                return NULL;
        nid_alloc = nid_here = numa_node_id();
        if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
@@ -3091,6 +3139,31 @@ static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
        return NULL;
 }
 
+/*
+ * Fallback function if there was no memory available and no objects on a
+ * certain node and we are allowed to fall back. We mimick the behavior of
+ * the page allocator. We fall back according to a zonelist determined by
+ * the policy layer while obeying cpuset constraints.
+ */
+void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
+{
+       struct zonelist *zonelist = &NODE_DATA(slab_node(current->mempolicy))
+                                       ->node_zonelists[gfp_zone(flags)];
+       struct zone **z;
+       void *obj = NULL;
+
+       for (z = zonelist->zones; *z && !obj; z++) {
+               int nid = zone_to_nid(*z);
+
+               if (zone_idx(*z) <= ZONE_NORMAL &&
+                               cpuset_zone_allowed(*z, flags) &&
+                               cache->nodelists[nid])
+                       obj = __cache_alloc_node(cache,
+                                       flags | __GFP_THISNODE, nid);
+       }
+       return obj;
+}
+
 /*
  * A interface to enable slab creation on nodeid
  */
@@ -3144,11 +3217,15 @@ retry:
 must_grow:
        spin_unlock(&l3->list_lock);
        x = cache_grow(cachep, flags, nodeid);
+       if (x)
+               goto retry;
 
-       if (!x)
-               return NULL;
+       if (!(flags & __GFP_THISNODE))
+               /* Unable to grow the cache. Fall back to other nodes. */
+               return fallback_alloc(cachep, flags);
+
+       return NULL;
 
-       goto retry;
 done:
        return obj;
 }
@@ -3420,22 +3497,25 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
 }
 
 
+#ifdef CONFIG_DEBUG_SLAB
 void *__kmalloc(size_t size, gfp_t flags)
 {
-#ifndef CONFIG_DEBUG_SLAB
-       return __do_kmalloc(size, flags, NULL);
-#else
        return __do_kmalloc(size, flags, __builtin_return_address(0));
-#endif
 }
 EXPORT_SYMBOL(__kmalloc);
 
-#ifdef CONFIG_DEBUG_SLAB
 void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
 {
        return __do_kmalloc(size, flags, caller);
 }
 EXPORT_SYMBOL(__kmalloc_track_caller);
+
+#else
+void *__kmalloc(size_t size, gfp_t flags)
+{
+       return __do_kmalloc(size, flags, NULL);
+}
+EXPORT_SYMBOL(__kmalloc);
 #endif
 
 /**