SLUB: extract dma_kmalloc_cache from get_cache.
[deliverable/linux.git] / mm / slub.c
index 1b0a95d75dbb8f57d2187875212a11ca13e94fb9..2b50b97a0fc3fd8bac4bf3e601c52a53e004a6bf 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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, int length)
 {
        struct page *page;
        void **object;
@@ -1558,19 +1559,25 @@ 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, length);
+
        return object;
 }
 
 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
 {
-       return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
+       return slab_alloc(s, gfpflags, -1,
+                       __builtin_return_address(0), s->objsize);
 }
 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));
+       return slab_alloc(s, gfpflags, node,
+               __builtin_return_address(0), s->objsize);
 }
 EXPORT_SYMBOL(kmem_cache_alloc_node);
 #endif
@@ -2159,7 +2166,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 +2272,54 @@ panic:
        panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
 }
 
+#ifdef CONFIG_ZONE_DMA
+static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
+{
+       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;
+       }
+
+       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;
+}
+#endif
+
 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
 {
        int index = kmalloc_index(size);
 
        if (!index)
-               return NULL;
+               return ZERO_SIZE_PTR;
 
        /* Allocation too large? */
-       BUG_ON(index < 0);
+       if (index < 0)
+               return NULL;
 
 #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;
-               }
-
-               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;
-       }
+       if ((flags & SLUB_DMA))
+               return dma_kmalloc_cache(index, flags);
 #endif
        return &kmalloc_caches[index];
 }
@@ -2314,9 +2328,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), size);
 }
 EXPORT_SYMBOL(__kmalloc);
 
@@ -2325,9 +2340,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), size);
 }
 EXPORT_SYMBOL(__kmalloc_node);
 #endif
@@ -2378,7 +2394,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);
@@ -2640,7 +2656,7 @@ void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
 {
        void *x;
 
-       x = slab_alloc(s, flags, -1, __builtin_return_address(0));
+       x = slab_alloc(s, flags, -1, __builtin_return_address(0), 0);
        if (x)
                memset(x, 0, s->objsize);
        return x;
@@ -2687,10 +2703,10 @@ 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);
+       return slab_alloc(s, gfpflags, -1, caller, size);
 }
 
 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
@@ -2698,10 +2714,10 @@ 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);
+       return slab_alloc(s, gfpflags, node, caller, size);
 }
 
 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
This page took 0.100149 seconds and 5 git commands to generate.