]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - mm/slub.c
SLUB: Do not use length parameter in slab_alloc()
[linux-2.6-omap-h63xx.git] / mm / slub.c
index 1b0a95d75dbb8f57d2187875212a11ca13e94fb9..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.
@@ -252,9 +252,10 @@ static int sysfs_slab_add(struct kmem_cache *);
 static int sysfs_slab_alias(struct kmem_cache *, const char *);
 static void sysfs_slab_remove(struct kmem_cache *);
 #else
-static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
-static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
-static void sysfs_slab_remove(struct kmem_cache *s) {}
+static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
+static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
+                                                       { return 0; }
+static inline void sysfs_slab_remove(struct kmem_cache *s) {}
 #endif
 
 /********************************************************************
@@ -1077,7 +1078,7 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
        void *last;
        void *p;
 
-       BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
+       BUG_ON(flags & ~(GFP_DMA | __GFP_ZERO | GFP_LEVEL_MASK));
 
        if (flags & __GFP_WAIT)
                local_irq_enable();
@@ -1395,7 +1396,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
        unfreeze_slab(s, page);
 }
 
-static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
+static inline void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
 {
        slab_lock(page);
        deactivate_slab(s, page, cpu);
@@ -1405,7 +1406,7 @@ static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
  * Flush cpu slab.
  * Called from IPI handler with interrupts disabled.
  */
-static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
+static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
 {
        struct page *page = s->cpu_slab[cpu];
 
@@ -1540,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)
+               gfp_t gfpflags, int node, void *addr)
 {
        struct page *page;
        void **object;
@@ -1558,6 +1559,10 @@ static void __always_inline *slab_alloc(struct kmem_cache *s,
                page->lockless_freelist = object[page->offset];
        }
        local_irq_restore(flags);
+
+       if (unlikely((gfpflags & __GFP_ZERO) && object))
+               memset(object, 0, s->objsize);
+
        return object;
 }
 
@@ -2159,7 +2164,7 @@ static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
 /*
  * Release all resources used by a slab cache.
  */
-static int kmem_cache_close(struct kmem_cache *s)
+static inline int kmem_cache_close(struct kmem_cache *s)
 {
        int node;
 
@@ -2265,47 +2270,92 @@ panic:
        panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
 }
 
-static struct kmem_cache *get_slab(size_t size, gfp_t flags)
+#ifdef CONFIG_ZONE_DMA
+static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
 {
-       int index = kmalloc_index(size);
-
-       if (!index)
-               return NULL;
+       struct kmem_cache *s;
+       struct kmem_cache *x;
+       char *text;
+       size_t realsize;
 
-       /* Allocation too large? */
-       BUG_ON(index < 0);
+       s = kmalloc_caches_dma[index];
+       if (s)
+               return s;
 
-#ifdef CONFIG_ZONE_DMA
-       if ((flags & SLUB_DMA)) {
-               struct kmem_cache *s;
-               struct kmem_cache *x;
-               char *text;
-               size_t realsize;
-
-               s = kmalloc_caches_dma[index];
-               if (s)
-                       return s;
-
-               /* Dynamically create dma cache */
-               x = kmalloc(kmem_size, flags & ~SLUB_DMA);
-               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;
-               }
+       /* Dynamically create dma cache */
+       x = kmalloc(kmem_size, flags & ~SLUB_DMA);
+       if (!x)
+               panic("Unable to allocate memory for dma cache\n");
 
-               text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
-                               (unsigned int)realsize);
-               s = create_kmalloc_cache(x, text, realsize, flags);
+       realsize = kmalloc_caches[index].objsize;
+       text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
+                       (unsigned int)realsize);
+       s = create_kmalloc_cache(x, text, realsize, flags);
+       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;
+
+       if (size <= 192) {
+               if (!size)
+                       return ZERO_SIZE_PTR;
+
+               index = size_index[(size - 1) / 8];
+       } else {
+               if (size > KMALLOC_MAX_SIZE)
+                       return NULL;
+
+               index = fls(size - 1);
+       }
+
+#ifdef CONFIG_ZONE_DMA
+       if (unlikely((flags & SLUB_DMA)))
+               return dma_kmalloc_cache(index, flags);
+
 #endif
        return &kmalloc_caches[index];
 }
@@ -2314,9 +2364,10 @@ void *__kmalloc(size_t size, gfp_t flags)
 {
        struct kmem_cache *s = get_slab(size, flags);
 
-       if (s)
-               return slab_alloc(s, flags, -1, __builtin_return_address(0));
-       return ZERO_SIZE_PTR;
+       if (ZERO_OR_NULL_PTR(s))
+               return s;
+
+       return slab_alloc(s, flags, -1, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(__kmalloc);
 
@@ -2325,9 +2376,10 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
 {
        struct kmem_cache *s = get_slab(size, flags);
 
-       if (s)
-               return slab_alloc(s, flags, node, __builtin_return_address(0));
-       return ZERO_SIZE_PTR;
+       if (ZERO_OR_NULL_PTR(s))
+               return s;
+
+       return slab_alloc(s, flags, node, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(__kmalloc_node);
 #endif
@@ -2378,7 +2430,7 @@ void kfree(const void *x)
         * this comparison would be true for all "negative" pointers
         * (which would cover the whole upper half of the address space).
         */
-       if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
+       if (ZERO_OR_NULL_PTR(x))
                return;
 
        page = virt_to_head_page(x);
@@ -2509,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 */
@@ -2687,8 +2757,8 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
 {
        struct kmem_cache *s = get_slab(size, gfpflags);
 
-       if (!s)
-               return ZERO_SIZE_PTR;
+       if (ZERO_OR_NULL_PTR(s))
+               return s;
 
        return slab_alloc(s, gfpflags, -1, caller);
 }
@@ -2698,8 +2768,8 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
 {
        struct kmem_cache *s = get_slab(size, gfpflags);
 
-       if (!s)
-               return ZERO_SIZE_PTR;
+       if (ZERO_OR_NULL_PTR(s))
+               return s;
 
        return slab_alloc(s, gfpflags, node, caller);
 }
@@ -3566,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
 
@@ -3664,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)
 {