Slab allocators: Replace explicit zeroing with __GFP_ZERO
[linux-2.6-microblaze.git] / mm / slab.c
index 1dc0ce1..3505639 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
 
 /* Shouldn't this be in a header file somewhere? */
 #define        BYTES_PER_WORD          sizeof(void *)
+#define        REDZONE_ALIGN           max(BYTES_PER_WORD, __alignof__(unsigned long long))
 
 #ifndef cache_line_size
 #define cache_line_size()      L1_CACHE_BYTES
@@ -547,7 +548,7 @@ static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
        if (cachep->flags & SLAB_STORE_USER)
                return (unsigned long long *)(objp + cachep->buffer_size -
                                              sizeof(unsigned long long) -
-                                             BYTES_PER_WORD);
+                                             REDZONE_ALIGN);
        return (unsigned long long *) (objp + cachep->buffer_size -
                                       sizeof(unsigned long long));
 }
@@ -568,21 +569,6 @@ static void **dbg_userword(struct kmem_cache *cachep, void *objp)
 
 #endif
 
-/*
- * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
- * order.
- */
-#if defined(CONFIG_LARGE_ALLOCS)
-#define        MAX_OBJ_ORDER   13      /* up to 32Mb */
-#define        MAX_GFP_ORDER   13      /* up to 32Mb */
-#elif defined(CONFIG_MMU)
-#define        MAX_OBJ_ORDER   5       /* 32 pages */
-#define        MAX_GFP_ORDER   5       /* 32 pages */
-#else
-#define        MAX_OBJ_ORDER   8       /* up to 1Mb */
-#define        MAX_GFP_ORDER   8       /* up to 1Mb */
-#endif
-
 /*
  * Do not go above this order unless 0 objects fit into the slab.
  */
@@ -789,7 +775,9 @@ static inline struct kmem_cache *__find_general_cachep(size_t size,
         */
        BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
 #endif
-       WARN_ON_ONCE(size == 0);
+       if (!size)
+               return ZERO_SIZE_PTR;
+
        while (size > csizep->cs_size)
                csizep++;
 
@@ -944,7 +932,7 @@ static void next_reap_node(void)
  * the CPUs getting into lockstep and contending for the global cache chain
  * lock.
  */
-static void __devinit start_cpu_timer(int cpu)
+static void __cpuinit start_cpu_timer(int cpu)
 {
        struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
 
@@ -2002,7 +1990,7 @@ static size_t calculate_slab_order(struct kmem_cache *cachep,
        size_t left_over = 0;
        int gfporder;
 
-       for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
+       for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
                unsigned int num;
                size_t remainder;
 
@@ -2052,7 +2040,7 @@ static size_t calculate_slab_order(struct kmem_cache *cachep,
        return left_over;
 }
 
-static int setup_cpu_cache(struct kmem_cache *cachep)
+static int __init_refok setup_cpu_cache(struct kmem_cache *cachep)
 {
        if (g_cpucache_up == FULL)
                return enable_cpucache(cachep);
@@ -2148,7 +2136,7 @@ kmem_cache_create (const char *name, size_t size, size_t align,
         * Sanity checks... these are all serious usage bugs.
         */
        if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
-           (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || dtor) {
+           size > KMALLOC_MAX_SIZE || dtor) {
                printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
                                name);
                BUG();
@@ -2194,7 +2182,8 @@ kmem_cache_create (const char *name, size_t size, size_t align,
         * above the next power of two: caches with object sizes just above a
         * power of two have a significant amount of internal fragmentation.
         */
-       if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
+       if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
+                                               2 * sizeof(unsigned long long)))
                flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
        if (!(flags & SLAB_DESTROY_BY_RCU))
                flags |= SLAB_POISON;
@@ -2235,12 +2224,20 @@ kmem_cache_create (const char *name, size_t size, size_t align,
        }
 
        /*
-        * Redzoning and user store require word alignment. Note this will be
-        * overridden by architecture or caller mandated alignment if either
-        * is greater than BYTES_PER_WORD.
+        * Redzoning and user store require word alignment or possibly larger.
+        * Note this will be overridden by architecture or caller mandated
+        * alignment if either is greater than BYTES_PER_WORD.
         */
-       if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
-               ralign = __alignof__(unsigned long long);
+       if (flags & SLAB_STORE_USER)
+               ralign = BYTES_PER_WORD;
+
+       if (flags & SLAB_RED_ZONE) {
+               ralign = REDZONE_ALIGN;
+               /* If redzoning, ensure that the second redzone is suitably
+                * aligned, by adjusting the object size accordingly. */
+               size += REDZONE_ALIGN - 1;
+               size &= ~(REDZONE_ALIGN - 1);
+       }
 
        /* 2) arch mandated alignment */
        if (ralign < ARCH_SLAB_MINALIGN) {
@@ -2277,9 +2274,13 @@ kmem_cache_create (const char *name, size_t size, size_t align,
        }
        if (flags & SLAB_STORE_USER) {
                /* user store requires one word storage behind the end of
-                * the real object.
+                * the real object. But if the second red zone needs to be
+                * aligned to 64 bits, we must allow that much space.
                 */
-               size += BYTES_PER_WORD;
+               if (flags & SLAB_RED_ZONE)
+                       size += REDZONE_ALIGN;
+               else
+                       size += BYTES_PER_WORD;
        }
 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
        if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
@@ -2353,7 +2354,7 @@ kmem_cache_create (const char *name, size_t size, size_t align,
                 * this should not happen at all.
                 * But leave a BUG_ON for some lucky dude.
                 */
-               BUG_ON(!cachep->slabp_cache);
+               BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
        }
        cachep->ctor = ctor;
        cachep->name = name;
@@ -2745,7 +2746,7 @@ static int cache_grow(struct kmem_cache *cachep,
         * Be lazy and only check for valid flags here,  keeping it out of the
         * critical path in kmem_cache_alloc().
         */
-       BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
+       BUG_ON(flags & ~(GFP_DMA | __GFP_ZERO | GFP_LEVEL_MASK));
 
        local_flags = (flags & GFP_LEVEL_MASK);
        /* Take the l3 list lock to change the colour_next on this node */
@@ -3391,6 +3392,9 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
        local_irq_restore(save_flags);
        ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
 
+       if (unlikely((flags & __GFP_ZERO) && ptr))
+               memset(ptr, 0, obj_size(cachep));
+
        return ptr;
 }
 
@@ -3442,6 +3446,9 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
        objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
        prefetchw(objp);
 
+       if (unlikely((flags & __GFP_ZERO) && objp))
+               memset(objp, 0, obj_size(cachep));
+
        return objp;
 }
 
@@ -3554,7 +3561,7 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp)
        check_irq_off();
        objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
 
-       if (use_alien_caches && cache_free_alien(cachep, objp))
+       if (cache_free_alien(cachep, objp))
                return;
 
        if (likely(ac->avail < ac->limit)) {
@@ -3582,23 +3589,6 @@ void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 }
 EXPORT_SYMBOL(kmem_cache_alloc);
 
-/**
- * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
- * @cache: The cache to allocate from.
- * @flags: See kmalloc().
- *
- * Allocate an object from this cache and set the allocated memory to zero.
- * The flags are only relevant if the cache has no available objects.
- */
-void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
-{
-       void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
-       if (ret)
-               memset(ret, 0, obj_size(cache));
-       return ret;
-}
-EXPORT_SYMBOL(kmem_cache_zalloc);
-
 /**
  * kmem_ptr_validate - check if an untrusted pointer might
  *     be a slab entry.
@@ -3655,8 +3645,8 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
        struct kmem_cache *cachep;
 
        cachep = kmem_find_general_cachep(size, flags);
-       if (unlikely(cachep == NULL))
-               return NULL;
+       if (unlikely(ZERO_OR_NULL_PTR(cachep)))
+               return cachep;
        return kmem_cache_alloc_node(cachep, flags, node);
 }
 
@@ -3727,52 +3717,6 @@ void *__kmalloc(size_t size, gfp_t flags)
 EXPORT_SYMBOL(__kmalloc);
 #endif
 
-/**
- * krealloc - reallocate memory. The contents will remain unchanged.
- * @p: object to reallocate memory for.
- * @new_size: how many bytes of memory are required.
- * @flags: the type of memory to allocate.
- *
- * The contents of the object pointed to are preserved up to the
- * lesser of the new and old sizes.  If @p is %NULL, krealloc()
- * behaves exactly like kmalloc().  If @size is 0 and @p is not a
- * %NULL pointer, the object pointed to is freed.
- */
-void *krealloc(const void *p, size_t new_size, gfp_t flags)
-{
-       struct kmem_cache *cache, *new_cache;
-       void *ret;
-
-       if (unlikely(!p))
-               return kmalloc_track_caller(new_size, flags);
-
-       if (unlikely(!new_size)) {
-               kfree(p);
-               return NULL;
-       }
-
-       cache = virt_to_cache(p);
-       new_cache = __find_general_cachep(new_size, flags);
-
-       /*
-        * If new size fits in the current cache, bail out.
-        */
-       if (likely(cache == new_cache))
-               return (void *)p;
-
-       /*
-        * We are on the slow-path here so do not use __cache_alloc
-        * because it bloats kernel text.
-        */
-       ret = kmalloc_track_caller(new_size, flags);
-       if (ret) {
-               memcpy(ret, p, min(new_size, ksize(p)));
-               kfree(p);
-       }
-       return ret;
-}
-EXPORT_SYMBOL(krealloc);
-
 /**
  * kmem_cache_free - Deallocate an object
  * @cachep: The cache the allocation was from.
@@ -3808,7 +3752,7 @@ void kfree(const void *objp)
        struct kmem_cache *c;
        unsigned long flags;
 
-       if (unlikely(!objp))
+       if (unlikely(ZERO_OR_NULL_PTR(objp)))
                return;
        local_irq_save(flags);
        kfree_debugcheck(objp);
@@ -4159,26 +4103,17 @@ static void print_slabinfo_header(struct seq_file *m)
 static void *s_start(struct seq_file *m, loff_t *pos)
 {
        loff_t n = *pos;
-       struct list_head *p;
 
        mutex_lock(&cache_chain_mutex);
        if (!n)
                print_slabinfo_header(m);
-       p = cache_chain.next;
-       while (n--) {
-               p = p->next;
-               if (p == &cache_chain)
-                       return NULL;
-       }
-       return list_entry(p, struct kmem_cache, next);
+
+       return seq_list_start(&cache_chain, *pos);
 }
 
 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
 {
-       struct kmem_cache *cachep = p;
-       ++*pos;
-       return cachep->next.next == &cache_chain ?
-               NULL : list_entry(cachep->next.next, struct kmem_cache, next);
+       return seq_list_next(p, &cache_chain, pos);
 }
 
 static void s_stop(struct seq_file *m, void *p)
@@ -4188,7 +4123,7 @@ static void s_stop(struct seq_file *m, void *p)
 
 static int s_show(struct seq_file *m, void *p)
 {
-       struct kmem_cache *cachep = p;
+       struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
        struct slab *slabp;
        unsigned long active_objs;
        unsigned long num_objs;
@@ -4357,17 +4292,8 @@ ssize_t slabinfo_write(struct file *file, const char __user * buffer,
 
 static void *leaks_start(struct seq_file *m, loff_t *pos)
 {
-       loff_t n = *pos;
-       struct list_head *p;
-
        mutex_lock(&cache_chain_mutex);
-       p = cache_chain.next;
-       while (n--) {
-               p = p->next;
-               if (p == &cache_chain)
-                       return NULL;
-       }
-       return list_entry(p, struct kmem_cache, next);
+       return seq_list_start(&cache_chain, *pos);
 }
 
 static inline int add_caller(unsigned long *n, unsigned long v)
@@ -4432,7 +4358,7 @@ static void show_symbol(struct seq_file *m, unsigned long address)
 
 static int leaks_show(struct seq_file *m, void *p)
 {
-       struct kmem_cache *cachep = p;
+       struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
        struct slab *slabp;
        struct kmem_list3 *l3;
        const char *name;
@@ -4513,7 +4439,7 @@ const struct seq_operations slabstats_op = {
  */
 size_t ksize(const void *objp)
 {
-       if (unlikely(objp == NULL))
+       if (unlikely(ZERO_OR_NULL_PTR(objp)))
                return 0;
 
        return obj_size(virt_to_cache(objp));