]> err.no Git - linux-2.6/blobdiff - mm/slub.c
SLUB: Do not use length parameter in slab_alloc()
[linux-2.6] / mm / slub.c
index 2b50b97a0fc3fd8bac4bf3e601c52a53e004a6bf..51ddd01604cd0e176045146c690cbed9cf688c00 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -233,7 +233,7 @@ static enum {
 
 /* A list of all slab caches on the system */
 static DECLARE_RWSEM(slub_lock);
-LIST_HEAD(slab_caches);
+static LIST_HEAD(slab_caches);
 
 /*
  * Tracking user of a slab.
@@ -1541,7 +1541,7 @@ debug:
  * Otherwise we can simply pick the next object from the lockless free list.
  */
 static void __always_inline *slab_alloc(struct kmem_cache *s,
-               gfp_t gfpflags, int node, void *addr, int length)
+               gfp_t gfpflags, int node, void *addr)
 {
        struct page *page;
        void **object;
@@ -1561,23 +1561,21 @@ static void __always_inline *slab_alloc(struct kmem_cache *s,
        local_irq_restore(flags);
 
        if (unlikely((gfpflags & __GFP_ZERO) && object))
-               memset(object, 0, length);
+               memset(object, 0, s->objsize);
 
        return object;
 }
 
 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
 {
-       return slab_alloc(s, gfpflags, -1,
-                       __builtin_return_address(0), s->objsize);
+       return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(kmem_cache_alloc);
 
 #ifdef CONFIG_NUMA
 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
 {
-       return slab_alloc(s, gfpflags, node,
-               __builtin_return_address(0), s->objsize);
+       return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(kmem_cache_alloc_node);
 #endif
@@ -2289,37 +2287,75 @@ static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
        if (!x)
                panic("Unable to allocate memory for dma cache\n");
 
-       if (index <= KMALLOC_SHIFT_HIGH)
-               realsize = 1 << index;
-       else {
-               if (index == 1)
-                       realsize = 96;
-               else
-                       realsize = 192;
-       }
-
+       realsize = kmalloc_caches[index].objsize;
        text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
                        (unsigned int)realsize);
        s = create_kmalloc_cache(x, text, realsize, flags);
-       kmalloc_caches_dma[index] = s;
-       return s;
+       down_write(&slub_lock);
+       if (!kmalloc_caches_dma[index]) {
+               kmalloc_caches_dma[index] = s;
+               up_write(&slub_lock);
+               return s;
+       }
+       up_write(&slub_lock);
+       kmem_cache_destroy(s);
+       return kmalloc_caches_dma[index];
 }
 #endif
 
+/*
+ * Conversion table for small slabs sizes / 8 to the index in the
+ * kmalloc array. This is necessary for slabs < 192 since we have non power
+ * of two cache sizes there. The size of larger slabs can be determined using
+ * fls.
+ */
+static s8 size_index[24] = {
+       3,      /* 8 */
+       4,      /* 16 */
+       5,      /* 24 */
+       5,      /* 32 */
+       6,      /* 40 */
+       6,      /* 48 */
+       6,      /* 56 */
+       6,      /* 64 */
+       1,      /* 72 */
+       1,      /* 80 */
+       1,      /* 88 */
+       1,      /* 96 */
+       7,      /* 104 */
+       7,      /* 112 */
+       7,      /* 120 */
+       7,      /* 128 */
+       2,      /* 136 */
+       2,      /* 144 */
+       2,      /* 152 */
+       2,      /* 160 */
+       2,      /* 168 */
+       2,      /* 176 */
+       2,      /* 184 */
+       2       /* 192 */
+};
+
 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
 {
-       int index = kmalloc_index(size);
+       int index;
 
-       if (!index)
-               return ZERO_SIZE_PTR;
+       if (size <= 192) {
+               if (!size)
+                       return ZERO_SIZE_PTR;
 
-       /* Allocation too large? */
-       if (index < 0)
-               return NULL;
+               index = size_index[(size - 1) / 8];
+       } else {
+               if (size > KMALLOC_MAX_SIZE)
+                       return NULL;
+
+               index = fls(size - 1);
+       }
 
 #ifdef CONFIG_ZONE_DMA
-       if ((flags & SLUB_DMA))
+       if (unlikely((flags & SLUB_DMA)))
                return dma_kmalloc_cache(index, flags);
+
 #endif
        return &kmalloc_caches[index];
 }
@@ -2331,7 +2367,7 @@ void *__kmalloc(size_t size, gfp_t flags)
        if (ZERO_OR_NULL_PTR(s))
                return s;
 
-       return slab_alloc(s, flags, -1, __builtin_return_address(0), size);
+       return slab_alloc(s, flags, -1, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(__kmalloc);
 
@@ -2343,7 +2379,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
        if (ZERO_OR_NULL_PTR(s))
                return s;
 
-       return slab_alloc(s, flags, node, __builtin_return_address(0), size);
+       return slab_alloc(s, flags, node, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(__kmalloc_node);
 #endif
@@ -2525,6 +2561,24 @@ void __init kmem_cache_init(void)
                caches++;
        }
 
+
+       /*
+        * Patch up the size_index table if we have strange large alignment
+        * requirements for the kmalloc array. This is only the case for
+        * mips it seems. The standard arches will not generate any code here.
+        *
+        * Largest permitted alignment is 256 bytes due to the way we
+        * handle the index determination for the smaller caches.
+        *
+        * Make sure that nothing crazy happens if someone starts tinkering
+        * around with ARCH_KMALLOC_MINALIGN
+        */
+       BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
+               (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+
+       for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
+               size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
+
        slab_state = UP;
 
        /* Provide the correct kmalloc names now that the caches are up */
@@ -2656,7 +2710,7 @@ void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
 {
        void *x;
 
-       x = slab_alloc(s, flags, -1, __builtin_return_address(0), 0);
+       x = slab_alloc(s, flags, -1, __builtin_return_address(0));
        if (x)
                memset(x, 0, s->objsize);
        return x;
@@ -2706,7 +2760,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
        if (ZERO_OR_NULL_PTR(s))
                return s;
 
-       return slab_alloc(s, gfpflags, -1, caller, size);
+       return slab_alloc(s, gfpflags, -1, caller);
 }
 
 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
@@ -2717,7 +2771,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
        if (ZERO_OR_NULL_PTR(s))
                return s;
 
-       return slab_alloc(s, gfpflags, node, caller, size);
+       return slab_alloc(s, gfpflags, node, caller);
 }
 
 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
@@ -3582,7 +3636,7 @@ static struct kset_uevent_ops slab_uevent_ops = {
        .filter = uevent_filter,
 };
 
-decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
+static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
 
 #define ID_STR_LENGTH 64
 
@@ -3680,7 +3734,7 @@ struct saved_alias {
        struct saved_alias *next;
 };
 
-struct saved_alias *alias_list;
+static struct saved_alias *alias_list;
 
 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
 {