mm/slab: remove object status buffer for DEBUG_SLAB_LEAK
[linux-2.6-block.git] / mm / slab.c
index 621fbcb35a366abf0d979cf48c5476cff1e609f5..02be9d9776adb144fd806fbef2e70ada24c7757f 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -224,16 +224,6 @@ static inline void clear_obj_pfmemalloc(void **objp)
        *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
 }
 
-/*
- * bootstrap: The caches do not work without cpuarrays anymore, but the
- * cpuarrays are allocated from the generic caches...
- */
-#define BOOT_CPUCACHE_ENTRIES  1
-struct arraycache_init {
-       struct array_cache cache;
-       void *entries[BOOT_CPUCACHE_ENTRIES];
-};
-
 /*
  * Need this for bootstrapping a per node allocator.
  */
@@ -390,36 +380,26 @@ static void **dbg_userword(struct kmem_cache *cachep, void *objp)
 
 #endif
 
-#define OBJECT_FREE (0)
-#define OBJECT_ACTIVE (1)
-
 #ifdef CONFIG_DEBUG_SLAB_LEAK
 
-static void set_obj_status(struct page *page, int idx, int val)
+static inline bool is_store_user_clean(struct kmem_cache *cachep)
 {
-       int freelist_size;
-       char *status;
-       struct kmem_cache *cachep = page->slab_cache;
-
-       freelist_size = cachep->num * sizeof(freelist_idx_t);
-       status = (char *)page->freelist + freelist_size;
-       status[idx] = val;
+       return atomic_read(&cachep->store_user_clean) == 1;
 }
 
-static inline unsigned int get_obj_status(struct page *page, int idx)
+static inline void set_store_user_clean(struct kmem_cache *cachep)
 {
-       int freelist_size;
-       char *status;
-       struct kmem_cache *cachep = page->slab_cache;
-
-       freelist_size = cachep->num * sizeof(freelist_idx_t);
-       status = (char *)page->freelist + freelist_size;
+       atomic_set(&cachep->store_user_clean, 1);
+}
 
-       return status[idx];
+static inline void set_store_user_dirty(struct kmem_cache *cachep)
+{
+       if (is_store_user_clean(cachep))
+               atomic_set(&cachep->store_user_clean, 0);
 }
 
 #else
-static inline void set_obj_status(struct page *page, int idx, int val) {}
+static inline void set_store_user_dirty(struct kmem_cache *cachep) {}
 
 #endif
 
@@ -457,6 +437,7 @@ static inline unsigned int obj_to_index(const struct kmem_cache *cache,
        return reciprocal_divide(offset, cache->reciprocal_buffer_size);
 }
 
+#define BOOT_CPUCACHE_ENTRIES  1
 /* internal cache of cache description objs */
 static struct kmem_cache kmem_cache_boot = {
        .batchcount = 1,
@@ -480,9 +461,6 @@ static size_t calculate_freelist_size(int nr_objs, size_t align)
        size_t freelist_size;
 
        freelist_size = nr_objs * sizeof(freelist_idx_t);
-       if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
-               freelist_size += nr_objs * sizeof(char);
-
        if (align)
                freelist_size = ALIGN(freelist_size, align);
 
@@ -495,10 +473,7 @@ static int calculate_nr_objs(size_t slab_size, size_t buffer_size,
        int nr_objs;
        size_t remained_size;
        size_t freelist_size;
-       int extra_space = 0;
 
-       if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
-               extra_space = sizeof(char);
        /*
         * Ignore padding for the initial guess. The padding
         * is at most @align-1 bytes, and @buffer_size is at
@@ -507,7 +482,7 @@ static int calculate_nr_objs(size_t slab_size, size_t buffer_size,
         * into the memory allocation when taking the padding
         * into account.
         */
-       nr_objs = slab_size / (buffer_size + idx_size + extra_space);
+       nr_objs = slab_size / (buffer_size + idx_size);
 
        /*
         * This calculated number will be either the right
@@ -537,7 +512,7 @@ static void cache_estimate(unsigned long gfporder, size_t buffer_size,
         * on it. For the latter case, the memory allocated for a
         * slab is used for:
         *
-        * - One unsigned int for each object
+        * - One freelist_idx_t for each object
         * - Padding to respect alignment of @align
         * - @buffer_size bytes for each object
         *
@@ -1670,6 +1645,14 @@ static void kmem_rcu_free(struct rcu_head *head)
 }
 
 #if DEBUG
+static bool is_debug_pagealloc_cache(struct kmem_cache *cachep)
+{
+       if (debug_pagealloc_enabled() && OFF_SLAB(cachep) &&
+               (cachep->size % PAGE_SIZE) == 0)
+               return true;
+
+       return false;
+}
 
 #ifdef CONFIG_DEBUG_PAGEALLOC
 static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
@@ -1703,6 +1686,23 @@ static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
        }
        *addr++ = 0x87654321;
 }
+
+static void slab_kernel_map(struct kmem_cache *cachep, void *objp,
+                               int map, unsigned long caller)
+{
+       if (!is_debug_pagealloc_cache(cachep))
+               return;
+
+       if (caller)
+               store_stackinfo(cachep, objp, caller);
+
+       kernel_map_pages(virt_to_page(objp), cachep->size / PAGE_SIZE, map);
+}
+
+#else
+static inline void slab_kernel_map(struct kmem_cache *cachep, void *objp,
+                               int map, unsigned long caller) {}
+
 #endif
 
 static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
@@ -1781,6 +1781,9 @@ static void check_poison_obj(struct kmem_cache *cachep, void *objp)
        int size, i;
        int lines = 0;
 
+       if (is_debug_pagealloc_cache(cachep))
+               return;
+
        realobj = (char *)objp + obj_offset(cachep);
        size = cachep->object_size;
 
@@ -1846,16 +1849,8 @@ static void slab_destroy_debugcheck(struct kmem_cache *cachep,
                void *objp = index_to_obj(cachep, page, i);
 
                if (cachep->flags & SLAB_POISON) {
-#ifdef CONFIG_DEBUG_PAGEALLOC
-                       if (cachep->size % PAGE_SIZE == 0 &&
-                                       OFF_SLAB(cachep))
-                               kernel_map_pages(virt_to_page(objp),
-                                       cachep->size / PAGE_SIZE, 1);
-                       else
-                               check_poison_obj(cachep, objp);
-#else
                        check_poison_obj(cachep, objp);
-#endif
+                       slab_kernel_map(cachep, objp, 1, 0);
                }
                if (cachep->flags & SLAB_RED_ZONE) {
                        if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
@@ -1945,16 +1940,13 @@ static size_t calculate_slab_order(struct kmem_cache *cachep,
                        break;
 
                if (flags & CFLGS_OFF_SLAB) {
-                       size_t freelist_size_per_obj = sizeof(freelist_idx_t);
                        /*
                         * Max number of objs-per-slab for caches which
                         * use off-slab slabs. Needed to avoid a possible
                         * looping condition in cache_grow().
                         */
-                       if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
-                               freelist_size_per_obj += sizeof(char);
                        offslab_limit = size;
-                       offslab_limit /= freelist_size_per_obj;
+                       offslab_limit /= sizeof(freelist_idx_t);
 
                        if (num > offslab_limit)
                                break;
@@ -2119,8 +2111,6 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
        if (!(flags & SLAB_DESTROY_BY_RCU))
                flags |= SLAB_POISON;
 #endif
-       if (flags & SLAB_DESTROY_BY_RCU)
-               BUG_ON(flags & SLAB_POISON);
 #endif
 
        /*
@@ -2179,7 +2169,6 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
                else
                        size += BYTES_PER_WORD;
        }
-#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
        /*
         * To activate debug pagealloc, off-slab management is necessary
         * requirement. In early phase of initialization, small sized slab
@@ -2187,13 +2176,13 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
         * to check size >= 256. It guarantees that all necessary small
         * sized slab is initialized in current slab initialization sequence.
         */
-       if (!slab_early_init && size >= kmalloc_size(INDEX_NODE) &&
+       if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
+               !slab_early_init && size >= kmalloc_size(INDEX_NODE) &&
                size >= 256 && cachep->object_size > cache_line_size() &&
                ALIGN(size, cachep->align) < PAGE_SIZE) {
                cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
                size = PAGE_SIZE;
        }
-#endif
 #endif
 
        /*
@@ -2237,15 +2226,6 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
        if (flags & CFLGS_OFF_SLAB) {
                /* really off slab. No need for manual alignment */
                freelist_size = calculate_freelist_size(cachep->num, 0);
-
-#ifdef CONFIG_PAGE_POISONING
-               /* If we're going to use the generic kernel_map_pages()
-                * poisoning, then it's going to smash the contents of
-                * the redzone and userword anyhow, so switch them off.
-                */
-               if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
-                       flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
-#endif
        }
 
        cachep->colour_off = cache_line_size();
@@ -2261,7 +2241,19 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
        cachep->size = size;
        cachep->reciprocal_buffer_size = reciprocal_value(size);
 
-       if (flags & CFLGS_OFF_SLAB) {
+#if DEBUG
+       /*
+        * If we're going to use the generic kernel_map_pages()
+        * poisoning, then it's going to smash the contents of
+        * the redzone and userword anyhow, so switch them off.
+        */
+       if (IS_ENABLED(CONFIG_PAGE_POISONING) &&
+               (cachep->flags & SLAB_POISON) &&
+               is_debug_pagealloc_cache(cachep))
+               cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
+#endif
+
+       if (OFF_SLAB(cachep)) {
                cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
                /*
                 * This is a possibility for one of the kmalloc_{dma,}_caches.
@@ -2377,9 +2369,6 @@ static int drain_freelist(struct kmem_cache *cache,
                }
 
                page = list_entry(p, struct page, lru);
-#if DEBUG
-               BUG_ON(page->active);
-#endif
                list_del(&page->lru);
                /*
                 * Safe to drop the lock. The slab is no longer linked
@@ -2488,9 +2477,6 @@ static void cache_init_objs(struct kmem_cache *cachep,
        for (i = 0; i < cachep->num; i++) {
                void *objp = index_to_obj(cachep, page, i);
 #if DEBUG
-               /* need to poison the objs? */
-               if (cachep->flags & SLAB_POISON)
-                       poison_obj(cachep, objp, POISON_FREE);
                if (cachep->flags & SLAB_STORE_USER)
                        *dbg_userword(cachep, objp) = NULL;
 
@@ -2514,15 +2500,15 @@ static void cache_init_objs(struct kmem_cache *cachep,
                                slab_error(cachep, "constructor overwrote the"
                                           " start of an object");
                }
-               if ((cachep->size % PAGE_SIZE) == 0 &&
-                           OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
-                       kernel_map_pages(virt_to_page(objp),
-                                        cachep->size / PAGE_SIZE, 0);
+               /* need to poison the objs? */
+               if (cachep->flags & SLAB_POISON) {
+                       poison_obj(cachep, objp, POISON_FREE);
+                       slab_kernel_map(cachep, objp, 0, 0);
+               }
 #else
                if (cachep->ctor)
                        cachep->ctor(objp);
 #endif
-               set_obj_status(page, i, OBJECT_FREE);
                set_free_obj(page, i, i);
        }
 }
@@ -2537,30 +2523,28 @@ static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
        }
 }
 
-static void *slab_get_obj(struct kmem_cache *cachep, struct page *page,
-                               int nodeid)
+static void *slab_get_obj(struct kmem_cache *cachep, struct page *page)
 {
        void *objp;
 
        objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
        page->active++;
+
 #if DEBUG
-       WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
+       if (cachep->flags & SLAB_STORE_USER)
+               set_store_user_dirty(cachep);
 #endif
 
        return objp;
 }
 
-static void slab_put_obj(struct kmem_cache *cachep, struct page *page,
-                               void *objp, int nodeid)
+static void slab_put_obj(struct kmem_cache *cachep,
+                       struct page *page, void *objp)
 {
        unsigned int objnr = obj_to_index(cachep, page, objp);
 #if DEBUG
        unsigned int i;
 
-       /* Verify that the slab belongs to the intended node */
-       WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
-
        /* Verify double free bug */
        for (i = page->active; i < cachep->num; i++) {
                if (get_free_obj(page, i) == objnr) {
@@ -2726,27 +2710,19 @@ static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
                *dbg_redzone1(cachep, objp) = RED_INACTIVE;
                *dbg_redzone2(cachep, objp) = RED_INACTIVE;
        }
-       if (cachep->flags & SLAB_STORE_USER)
+       if (cachep->flags & SLAB_STORE_USER) {
+               set_store_user_dirty(cachep);
                *dbg_userword(cachep, objp) = (void *)caller;
+       }
 
        objnr = obj_to_index(cachep, page, objp);
 
        BUG_ON(objnr >= cachep->num);
        BUG_ON(objp != index_to_obj(cachep, page, objnr));
 
-       set_obj_status(page, objnr, OBJECT_FREE);
        if (cachep->flags & SLAB_POISON) {
-#ifdef CONFIG_DEBUG_PAGEALLOC
-               if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
-                       store_stackinfo(cachep, objp, caller);
-                       kernel_map_pages(virt_to_page(objp),
-                                        cachep->size / PAGE_SIZE, 0);
-               } else {
-                       poison_obj(cachep, objp, POISON_FREE);
-               }
-#else
                poison_obj(cachep, objp, POISON_FREE);
-#endif
+               slab_kernel_map(cachep, objp, 0, caller);
        }
        return objp;
 }
@@ -2826,8 +2802,7 @@ retry:
                        STATS_INC_ACTIVE(cachep);
                        STATS_SET_HIGH(cachep);
 
-                       ac_put_obj(cachep, ac, slab_get_obj(cachep, page,
-                                                                       node));
+                       ac_put_obj(cachep, ac, slab_get_obj(cachep, page));
                }
 
                /* move slabp to correct slabp list: */
@@ -2877,20 +2852,11 @@ static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
 static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
                                gfp_t flags, void *objp, unsigned long caller)
 {
-       struct page *page;
-
        if (!objp)
                return objp;
        if (cachep->flags & SLAB_POISON) {
-#ifdef CONFIG_DEBUG_PAGEALLOC
-               if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
-                       kernel_map_pages(virt_to_page(objp),
-                                        cachep->size / PAGE_SIZE, 1);
-               else
-                       check_poison_obj(cachep, objp);
-#else
                check_poison_obj(cachep, objp);
-#endif
+               slab_kernel_map(cachep, objp, 1, 0);
                poison_obj(cachep, objp, POISON_INUSE);
        }
        if (cachep->flags & SLAB_STORE_USER)
@@ -2910,8 +2876,6 @@ static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
                *dbg_redzone2(cachep, objp) = RED_ACTIVE;
        }
 
-       page = virt_to_head_page(objp);
-       set_obj_status(page, obj_to_index(cachep, page, objp), OBJECT_ACTIVE);
        objp += obj_offset(cachep);
        if (cachep->ctor && cachep->flags & SLAB_POISON)
                cachep->ctor(objp);
@@ -2926,14 +2890,6 @@ static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
 #endif
 
-static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
-{
-       if (unlikely(cachep == kmem_cache))
-               return false;
-
-       return should_failslab(cachep->object_size, flags, cachep->flags);
-}
-
 static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
        void *objp;
@@ -3118,7 +3074,7 @@ retry:
 
        BUG_ON(page->active == cachep->num);
 
-       obj = slab_get_obj(cachep, page, nodeid);
+       obj = slab_get_obj(cachep, page);
        n->free_objects--;
        /* move slabp to correct slabp list: */
        list_del(&page->lru);
@@ -3152,14 +3108,10 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
        int slab_node = numa_mem_id();
 
        flags &= gfp_allowed_mask;
-
-       lockdep_trace_alloc(flags);
-
-       if (slab_should_failslab(cachep, flags))
+       cachep = slab_pre_alloc_hook(cachep, flags);
+       if (unlikely(!cachep))
                return NULL;
 
-       cachep = memcg_kmem_get_cache(cachep, flags);
-
        cache_alloc_debugcheck_before(cachep, flags);
        local_irq_save(save_flags);
 
@@ -3188,16 +3140,11 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
   out:
        local_irq_restore(save_flags);
        ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
-       kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
-                                flags);
 
-       if (likely(ptr)) {
-               kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
-               if (unlikely(flags & __GFP_ZERO))
-                       memset(ptr, 0, cachep->object_size);
-       }
+       if (unlikely(flags & __GFP_ZERO) && ptr)
+               memset(ptr, 0, cachep->object_size);
 
-       memcg_kmem_put_cache(cachep);
+       slab_post_alloc_hook(cachep, flags, 1, &ptr);
        return ptr;
 }
 
@@ -3240,30 +3187,21 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
        void *objp;
 
        flags &= gfp_allowed_mask;
-
-       lockdep_trace_alloc(flags);
-
-       if (slab_should_failslab(cachep, flags))
+       cachep = slab_pre_alloc_hook(cachep, flags);
+       if (unlikely(!cachep))
                return NULL;
 
-       cachep = memcg_kmem_get_cache(cachep, flags);
-
        cache_alloc_debugcheck_before(cachep, flags);
        local_irq_save(save_flags);
        objp = __do_cache_alloc(cachep, flags);
        local_irq_restore(save_flags);
        objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
-       kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
-                                flags);
        prefetchw(objp);
 
-       if (likely(objp)) {
-               kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
-               if (unlikely(flags & __GFP_ZERO))
-                       memset(objp, 0, cachep->object_size);
-       }
+       if (unlikely(flags & __GFP_ZERO) && objp)
+               memset(objp, 0, cachep->object_size);
 
-       memcg_kmem_put_cache(cachep);
+       slab_post_alloc_hook(cachep, flags, 1, &objp);
        return objp;
 }
 
@@ -3287,7 +3225,7 @@ static void free_block(struct kmem_cache *cachep, void **objpp,
                page = virt_to_head_page(objp);
                list_del(&page->lru);
                check_spinlock_acquired_node(cachep, node);
-               slab_put_obj(cachep, page, objp, node);
+               slab_put_obj(cachep, page, objp);
                STATS_DEC_ACTIVE(cachep);
                n->free_objects++;
 
@@ -3317,9 +3255,7 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
        LIST_HEAD(list);
 
        batchcount = ac->batchcount;
-#if DEBUG
-       BUG_ON(!batchcount || batchcount > ac->avail);
-#endif
+
        check_irq_off();
        n = get_node(cachep, node);
        spin_lock(&n->list_lock);
@@ -3411,16 +3347,53 @@ void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 }
 EXPORT_SYMBOL(kmem_cache_alloc);
 
-void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
+static __always_inline void
+cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
+                                 size_t size, void **p, unsigned long caller)
 {
-       __kmem_cache_free_bulk(s, size, p);
+       size_t i;
+
+       for (i = 0; i < size; i++)
+               p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
 }
-EXPORT_SYMBOL(kmem_cache_free_bulk);
 
 int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
-                                                               void **p)
+                         void **p)
 {
-       return __kmem_cache_alloc_bulk(s, flags, size, p);
+       size_t i;
+
+       s = slab_pre_alloc_hook(s, flags);
+       if (!s)
+               return 0;
+
+       cache_alloc_debugcheck_before(s, flags);
+
+       local_irq_disable();
+       for (i = 0; i < size; i++) {
+               void *objp = __do_cache_alloc(s, flags);
+
+               if (unlikely(!objp))
+                       goto error;
+               p[i] = objp;
+       }
+       local_irq_enable();
+
+       cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_);
+
+       /* Clear memory outside IRQ disabled section */
+       if (unlikely(flags & __GFP_ZERO))
+               for (i = 0; i < size; i++)
+                       memset(p[i], 0, s->object_size);
+
+       slab_post_alloc_hook(s, flags, size, p);
+       /* FIXME: Trace call missing. Christoph would like a bulk variant */
+       return size;
+error:
+       local_irq_enable();
+       cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
+       slab_post_alloc_hook(s, flags, i, p);
+       __kmem_cache_free_bulk(s, i, p);
+       return 0;
 }
 EXPORT_SYMBOL(kmem_cache_alloc_bulk);
 
@@ -3567,6 +3540,32 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
+void kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p)
+{
+       struct kmem_cache *s;
+       size_t i;
+
+       local_irq_disable();
+       for (i = 0; i < size; i++) {
+               void *objp = p[i];
+
+               if (!orig_s) /* called via kfree_bulk */
+                       s = virt_to_cache(objp);
+               else
+                       s = cache_from_obj(orig_s, objp);
+
+               debug_check_no_locks_freed(objp, s->object_size);
+               if (!(s->flags & SLAB_DEBUG_OBJECTS))
+                       debug_check_no_obj_freed(objp, s->object_size);
+
+               __cache_free(s, objp, _RET_IP_);
+       }
+       local_irq_enable();
+
+       /* FIXME: add tracing */
+}
+EXPORT_SYMBOL(kmem_cache_free_bulk);
+
 /**
  * kfree - free previously allocated memory
  * @objp: pointer returned by kmalloc.
@@ -4102,15 +4101,34 @@ static void handle_slab(unsigned long *n, struct kmem_cache *c,
                                                struct page *page)
 {
        void *p;
-       int i;
+       int i, j;
+       unsigned long v;
 
        if (n[0] == n[1])
                return;
        for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
-               if (get_obj_status(page, i) != OBJECT_ACTIVE)
+               bool active = true;
+
+               for (j = page->active; j < c->num; j++) {
+                       if (get_free_obj(page, j) == i) {
+                               active = false;
+                               break;
+                       }
+               }
+
+               if (!active)
                        continue;
 
-               if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
+               /*
+                * probe_kernel_read() is used for DEBUG_PAGEALLOC. page table
+                * mapping is established when actual object allocation and
+                * we could mistakenly access the unmapped object in the cpu
+                * cache.
+                */
+               if (probe_kernel_read(&v, dbg_userword(c, p), sizeof(v)))
+                       continue;
+
+               if (!add_caller(n, v))
                        return;
        }
 }
@@ -4146,21 +4164,31 @@ static int leaks_show(struct seq_file *m, void *p)
        if (!(cachep->flags & SLAB_RED_ZONE))
                return 0;
 
-       /* OK, we can do it */
+       /*
+        * Set store_user_clean and start to grab stored user information
+        * for all objects on this cache. If some alloc/free requests comes
+        * during the processing, information would be wrong so restart
+        * whole processing.
+        */
+       do {
+               set_store_user_clean(cachep);
+               drain_cpu_caches(cachep);
 
-       x[1] = 0;
+               x[1] = 0;
 
-       for_each_kmem_cache_node(cachep, node, n) {
+               for_each_kmem_cache_node(cachep, node, n) {
 
-               check_irq_on();
-               spin_lock_irq(&n->list_lock);
+                       check_irq_on();
+                       spin_lock_irq(&n->list_lock);
+
+                       list_for_each_entry(page, &n->slabs_full, lru)
+                               handle_slab(x, cachep, page);
+                       list_for_each_entry(page, &n->slabs_partial, lru)
+                               handle_slab(x, cachep, page);
+                       spin_unlock_irq(&n->list_lock);
+               }
+       } while (!is_store_user_clean(cachep));
 
-               list_for_each_entry(page, &n->slabs_full, lru)
-                       handle_slab(x, cachep, page);
-               list_for_each_entry(page, &n->slabs_partial, lru)
-                       handle_slab(x, cachep, page);
-               spin_unlock_irq(&n->list_lock);
-       }
        name = cachep->name;
        if (x[0] == x[1]) {
                /* Increase the buffer size */