]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - mm/slub.c
cgroups: add per-thread subsystem callbacks
[linux-2.6.git] / mm / slub.c
index 2cdd235cb80166b000543e3bd4e2a8cd2812320c..7be0223531b090dbd9f716104714bc4d9ca9c7b5 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -17,7 +17,6 @@
 #include <linux/slab.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
-#include <linux/kmemtrace.h>
 #include <linux/kmemcheck.h>
 #include <linux/cpu.h>
 #include <linux/cpuset.h>
@@ -29,6 +28,8 @@
 #include <linux/math64.h>
 #include <linux/fault-inject.h>
 
+#include <trace/events/kmem.h>
+
 /*
  * Lock order:
  *   1. slab_lock(page)
@@ -63,7 +64,7 @@
  *   we must stay away from it for a while since we may cause a bouncing
  *   cacheline if we try to acquire the lock. So go onto the next slab.
  *   If all pages are busy then we may allocate a new slab instead of reusing
- *   a partial slab. A new slab has noone operating on it and thus there is
+ *   a partial slab. A new slab has no one operating on it and thus there is
  *   no danger of cacheline contention.
  *
  *   Interrupts are disabled during allocation and deallocation in order to
  *                     the fast path and disables lockless freelists.
  */
 
+#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
+               SLAB_TRACE | SLAB_DEBUG_FREE)
+
+static inline int kmem_cache_debug(struct kmem_cache *s)
+{
 #ifdef CONFIG_SLUB_DEBUG
-#define SLABDEBUG 1
+       return unlikely(s->flags & SLAB_DEBUG_FLAGS);
 #else
-#define SLABDEBUG 0
+       return 0;
 #endif
+}
 
 /*
  * Issues still to be resolved:
 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
                SLAB_CACHE_DMA | SLAB_NOTRACK)
 
-#ifndef ARCH_KMALLOC_MINALIGN
-#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
-#endif
-
-#ifndef ARCH_SLAB_MINALIGN
-#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
-#endif
-
 #define OO_SHIFT       16
 #define OO_MASK                ((1 << OO_SHIFT) - 1)
 #define MAX_OBJS_PER_PAGE      65535 /* since page.objects is u16 */
 
 /* Internal SLUB flags */
-#define __OBJECT_POISON                0x80000000 /* Poison object */
-#define __SYSFS_ADD_DEFERRED   0x40000000 /* Not yet visible via sysfs */
+#define __OBJECT_POISON                0x80000000UL /* Poison object */
 
 static int kmem_size = sizeof(struct kmem_cache);
 
@@ -181,7 +179,7 @@ static struct notifier_block slab_notifier;
 
 static enum {
        DOWN,           /* No slab functionality available */
-       PARTIAL,        /* kmem_cache_open() works but kmalloc does not */
+       PARTIAL,        /* Kmem_cache_node works */
        UP,             /* Everything works but does not show up in sysfs */
        SYSFS           /* Sysfs up */
 } slab_state = DOWN;
@@ -202,7 +200,7 @@ struct track {
 
 enum track_item { TRACK_ALLOC, TRACK_FREE };
 
-#ifdef CONFIG_SLUB_DEBUG
+#ifdef CONFIG_SYSFS
 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 *);
@@ -213,12 +211,13 @@ 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)
 {
+       kfree(s->name);
        kfree(s);
 }
 
 #endif
 
-static inline void stat(struct kmem_cache *s, enum stat_item si)
+static inline void stat(const struct kmem_cache *s, enum stat_item si)
 {
 #ifdef CONFIG_SLUB_STATS
        __this_cpu_inc(s->cpu_slab->stat[si]);
@@ -236,11 +235,7 @@ int slab_is_available(void)
 
 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
 {
-#ifdef CONFIG_NUMA
        return s->node[node];
-#else
-       return &s->local_node;
-#endif
 }
 
 /* Verify that a pointer has an address that is valid within a slab page */
@@ -266,6 +261,18 @@ static inline void *get_freepointer(struct kmem_cache *s, void *object)
        return *(void **)(object + s->offset);
 }
 
+static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
+{
+       void *p;
+
+#ifdef CONFIG_DEBUG_PAGEALLOC
+       probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
+#else
+       p = get_freepointer(s, object);
+#endif
+       return p;
+}
+
 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
 {
        *(void **)(object + s->offset) = fp;
@@ -276,21 +283,46 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
        for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
                        __p += (__s)->size)
 
-/* Scan freelist */
-#define for_each_free_object(__p, __s, __free) \
-       for (__p = (__free); __p; __p = get_freepointer((__s), __p))
-
 /* Determine object index from a given position */
 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
 {
        return (p - addr) / s->size;
 }
 
+static inline size_t slab_ksize(const struct kmem_cache *s)
+{
+#ifdef CONFIG_SLUB_DEBUG
+       /*
+        * Debugging requires use of the padding between object
+        * and whatever may come after it.
+        */
+       if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
+               return s->objsize;
+
+#endif
+       /*
+        * If we have the need to store the freelist pointer
+        * back there or track user information then we can
+        * only use the space before that information.
+        */
+       if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
+               return s->inuse;
+       /*
+        * Else we can use all the padding etc for the allocation
+        */
+       return s->size;
+}
+
+static inline int order_objects(int order, unsigned long size, int reserved)
+{
+       return ((PAGE_SIZE << order) - reserved) / size;
+}
+
 static inline struct kmem_cache_order_objects oo_make(int order,
-                                               unsigned long size)
+               unsigned long size, int reserved)
 {
        struct kmem_cache_order_objects x = {
-               (order << OO_SHIFT) + (PAGE_SIZE << order) / size
+               (order << OO_SHIFT) + order_objects(order, size, reserved)
        };
 
        return x;
@@ -307,6 +339,21 @@ static inline int oo_objects(struct kmem_cache_order_objects x)
 }
 
 #ifdef CONFIG_SLUB_DEBUG
+/*
+ * Determine a map of object in use on a page.
+ *
+ * Slab lock or node listlock must be held to guarantee that the page does
+ * not vanish from under us.
+ */
+static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
+{
+       void *p;
+       void *addr = page_address(page);
+
+       for (p = page->freelist; p; p = get_freepointer(s, p))
+               set_bit(slab_index(p, s, addr), map);
+}
+
 /*
  * Debug settings:
  */
@@ -497,7 +544,7 @@ static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
        dump_stack();
 }
 
-static void init_object(struct kmem_cache *s, void *object, int active)
+static void init_object(struct kmem_cache *s, void *object, u8 val)
 {
        u8 *p = object;
 
@@ -507,9 +554,7 @@ static void init_object(struct kmem_cache *s, void *object, int active)
        }
 
        if (s->flags & SLAB_RED_ZONE)
-               memset(p + s->objsize,
-                       active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
-                       s->inuse - s->objsize);
+               memset(p + s->objsize, val, s->inuse - s->objsize);
 }
 
 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
@@ -624,7 +669,7 @@ static int slab_pad_check(struct kmem_cache *s, struct page *page)
                return 1;
 
        start = page_address(page);
-       length = (PAGE_SIZE << compound_order(page));
+       length = (PAGE_SIZE << compound_order(page)) - s->reserved;
        end = start + length;
        remainder = length % s->size;
        if (!remainder)
@@ -644,17 +689,14 @@ static int slab_pad_check(struct kmem_cache *s, struct page *page)
 }
 
 static int check_object(struct kmem_cache *s, struct page *page,
-                                       void *object, int active)
+                                       void *object, u8 val)
 {
        u8 *p = object;
        u8 *endobject = object + s->objsize;
 
        if (s->flags & SLAB_RED_ZONE) {
-               unsigned int red =
-                       active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
-
                if (!check_bytes_and_report(s, page, object, "Redzone",
-                       endobject, red, s->inuse - s->objsize))
+                       endobject, val, s->inuse - s->objsize))
                        return 0;
        } else {
                if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
@@ -664,7 +706,7 @@ static int check_object(struct kmem_cache *s, struct page *page,
        }
 
        if (s->flags & SLAB_POISON) {
-               if (!active && (s->flags & __OBJECT_POISON) &&
+               if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
                        (!check_bytes_and_report(s, page, p, "Poison", p,
                                        POISON_FREE, s->objsize - 1) ||
                         !check_bytes_and_report(s, page, p, "Poison",
@@ -676,7 +718,7 @@ static int check_object(struct kmem_cache *s, struct page *page,
                check_pad_bytes(s, page, p);
        }
 
-       if (!s->offset && active)
+       if (!s->offset && val == SLUB_RED_ACTIVE)
                /*
                 * Object and freepointer overlap. Cannot check
                 * freepointer while object is allocated.
@@ -708,7 +750,7 @@ static int check_slab(struct kmem_cache *s, struct page *page)
                return 0;
        }
 
-       maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
+       maxobj = order_objects(compound_order(page), s->size, s->reserved);
        if (page->objects > maxobj) {
                slab_err(s, page, "objects %u > max %u",
                        s->name, page->objects, maxobj);
@@ -758,7 +800,7 @@ static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
                nr++;
        }
 
-       max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
+       max_objects = order_objects(compound_order(page), s->size, s->reserved);
        if (max_objects > MAX_OBJS_PER_PAGE)
                max_objects = MAX_OBJS_PER_PAGE;
 
@@ -794,6 +836,49 @@ static void trace(struct kmem_cache *s, struct page *page, void *object,
        }
 }
 
+/*
+ * Hooks for other subsystems that check memory allocations. In a typical
+ * production configuration these hooks all should produce no code at all.
+ */
+static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
+{
+       flags &= gfp_allowed_mask;
+       lockdep_trace_alloc(flags);
+       might_sleep_if(flags & __GFP_WAIT);
+
+       return should_failslab(s->objsize, flags, s->flags);
+}
+
+static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
+{
+       flags &= gfp_allowed_mask;
+       kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
+       kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
+}
+
+static inline void slab_free_hook(struct kmem_cache *s, void *x)
+{
+       kmemleak_free_recursive(x, s->flags);
+
+       /*
+        * Trouble is that we may no longer disable interupts in the fast path
+        * So in order to make the debug calls that expect irqs to be
+        * disabled we need to disable interrupts temporarily.
+        */
+#if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
+       {
+               unsigned long flags;
+
+               local_irq_save(flags);
+               kmemcheck_slab_free(s, x, s->objsize);
+               debug_check_no_locks_freed(x, s->objsize);
+               local_irq_restore(flags);
+       }
+#endif
+       if (!(s->flags & SLAB_DEBUG_OBJECTS))
+               debug_check_no_obj_freed(x, s->objsize);
+}
+
 /*
  * Tracking of fully allocated slabs for debugging purposes.
  */
@@ -841,7 +926,7 @@ static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
         * dilemma by deferring the increment of the count during
         * bootstrap (see early_kmem_cache_node_alloc).
         */
-       if (!NUMA_BUILD || n) {
+       if (n) {
                atomic_long_inc(&n->nr_slabs);
                atomic_long_add(objects, &n->total_objects);
        }
@@ -861,11 +946,11 @@ static void setup_object_debug(struct kmem_cache *s, struct page *page,
        if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
                return;
 
-       init_object(s, object, 0);
+       init_object(s, object, SLUB_RED_INACTIVE);
        init_tracking(s, object);
 }
 
-static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
+static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
                                        void *object, unsigned long addr)
 {
        if (!check_slab(s, page))
@@ -881,14 +966,14 @@ static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
                goto bad;
        }
 
-       if (!check_object(s, page, object, 0))
+       if (!check_object(s, page, object, SLUB_RED_INACTIVE))
                goto bad;
 
        /* Success perform special debug activities for allocs */
        if (s->flags & SLAB_STORE_USER)
                set_track(s, object, TRACK_ALLOC, addr);
        trace(s, page, object, 1);
-       init_object(s, object, 1);
+       init_object(s, object, SLUB_RED_ACTIVE);
        return 1;
 
 bad:
@@ -905,8 +990,8 @@ bad:
        return 0;
 }
 
-static int free_debug_processing(struct kmem_cache *s, struct page *page,
-                                       void *object, unsigned long addr)
+static noinline int free_debug_processing(struct kmem_cache *s,
+                struct page *page, void *object, unsigned long addr)
 {
        if (!check_slab(s, page))
                goto fail;
@@ -921,7 +1006,7 @@ static int free_debug_processing(struct kmem_cache *s, struct page *page,
                goto fail;
        }
 
-       if (!check_object(s, page, object, 1))
+       if (!check_object(s, page, object, SLUB_RED_ACTIVE))
                return 0;
 
        if (unlikely(s != page->slab)) {
@@ -945,7 +1030,7 @@ static int free_debug_processing(struct kmem_cache *s, struct page *page,
        if (s->flags & SLAB_STORE_USER)
                set_track(s, object, TRACK_FREE, addr);
        trace(s, page, object, 0);
-       init_object(s, object, 0);
+       init_object(s, object, SLUB_RED_INACTIVE);
        return 1;
 
 fail:
@@ -1049,7 +1134,7 @@ static inline int free_debug_processing(struct kmem_cache *s,
 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
                        { return 1; }
 static inline int check_object(struct kmem_cache *s, struct page *page,
-                       void *object, int active) { return 1; }
+                       void *object, u8 val) { return 1; }
 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
 static inline unsigned long kmem_cache_flags(unsigned long objsize,
        unsigned long flags, const char *name,
@@ -1069,7 +1154,16 @@ static inline void inc_slabs_node(struct kmem_cache *s, int node,
                                                        int objects) {}
 static inline void dec_slabs_node(struct kmem_cache *s, int node,
                                                        int objects) {}
-#endif
+
+static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
+                                                       { return 0; }
+
+static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
+               void *object) {}
+
+static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
+
+#endif /* CONFIG_SLUB_DEBUG */
 
 /*
  * Slab allocation and freeing
@@ -1081,7 +1175,7 @@ static inline struct page *alloc_slab_page(gfp_t flags, int node,
 
        flags |= __GFP_NOTRACK;
 
-       if (node == -1)
+       if (node == NUMA_NO_NODE)
                return alloc_pages(flags, order);
        else
                return alloc_pages_exact_node(node, flags, order);
@@ -1165,9 +1259,6 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
        inc_slabs_node(s, page_to_nid(page), page->objects);
        page->slab = s;
        page->flags |= 1 << PG_slab;
-       if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
-                       SLAB_STORE_USER | SLAB_TRACE))
-               __SetPageSlubDebug(page);
 
        start = page_address(page);
 
@@ -1194,14 +1285,13 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
        int order = compound_order(page);
        int pages = 1 << order;
 
-       if (unlikely(SLABDEBUG && PageSlubDebug(page))) {
+       if (kmem_cache_debug(s)) {
                void *p;
 
                slab_pad_check(s, page);
                for_each_object(p, s, page_address(page),
                                                page->objects)
-                       check_object(s, page, p, 0);
-               __ClearPageSlubDebug(page);
+                       check_object(s, page, p, SLUB_RED_INACTIVE);
        }
 
        kmemcheck_free_shadow(page, compound_order(page));
@@ -1218,21 +1308,38 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
        __free_pages(page, order);
 }
 
+#define need_reserve_slab_rcu                                          \
+       (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
+
 static void rcu_free_slab(struct rcu_head *h)
 {
        struct page *page;
 
-       page = container_of((struct list_head *)h, struct page, lru);
+       if (need_reserve_slab_rcu)
+               page = virt_to_head_page(h);
+       else
+               page = container_of((struct list_head *)h, struct page, lru);
+
        __free_slab(page->slab, page);
 }
 
 static void free_slab(struct kmem_cache *s, struct page *page)
 {
        if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
-               /*
-                * RCU free overloads the RCU head over the LRU
-                */
-               struct rcu_head *head = (void *)&page->lru;
+               struct rcu_head *head;
+
+               if (need_reserve_slab_rcu) {
+                       int order = compound_order(page);
+                       int offset = (PAGE_SIZE << order) - s->reserved;
+
+                       VM_BUG_ON(s->reserved != sizeof(*head));
+                       head = page_address(page) + offset;
+               } else {
+                       /*
+                        * RCU free overloads the RCU head over the LRU
+                        */
+                       head = (void *)&page->lru;
+               }
 
                call_rcu(head, rcu_free_slab);
        } else
@@ -1281,13 +1388,19 @@ static void add_partial(struct kmem_cache_node *n,
        spin_unlock(&n->list_lock);
 }
 
+static inline void __remove_partial(struct kmem_cache_node *n,
+                                       struct page *page)
+{
+       list_del(&page->lru);
+       n->nr_partial--;
+}
+
 static void remove_partial(struct kmem_cache *s, struct page *page)
 {
        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
 
        spin_lock(&n->list_lock);
-       list_del(&page->lru);
-       n->nr_partial--;
+       __remove_partial(n, page);
        spin_unlock(&n->list_lock);
 }
 
@@ -1300,8 +1413,7 @@ static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
                                                        struct page *page)
 {
        if (slab_trylock(page)) {
-               list_del(&page->lru);
-               n->nr_partial--;
+               __remove_partial(n, page);
                __SetPageSlubFrozen(page);
                return 1;
        }
@@ -1368,6 +1480,7 @@ static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
                        get_cycles() % 1024 > s->remote_node_defrag_ratio)
                return NULL;
 
+       get_mems_allowed();
        zonelist = node_zonelist(slab_node(current->mempolicy), flags);
        for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
                struct kmem_cache_node *n;
@@ -1377,10 +1490,13 @@ static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
                if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
                                n->nr_partial > s->min_partial) {
                        page = get_partial_node(n);
-                       if (page)
+                       if (page) {
+                               put_mems_allowed();
                                return page;
+                       }
                }
        }
+       put_mems_allowed();
 #endif
        return NULL;
 }
@@ -1391,10 +1507,10 @@ static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
 {
        struct page *page;
-       int searchnode = (node == -1) ? numa_node_id() : node;
+       int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
 
        page = get_partial_node(get_node(s, searchnode));
-       if (page || (flags & __GFP_THISNODE))
+       if (page || node != NUMA_NO_NODE)
                return page;
 
        return get_any_partial(s, flags);
@@ -1408,6 +1524,7 @@ static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  * On exit the slab lock will have been dropped.
  */
 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
+       __releases(bitlock)
 {
        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
 
@@ -1419,8 +1536,7 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
                        stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
                } else {
                        stat(s, DEACTIVATE_FULL);
-                       if (SLABDEBUG && PageSlubDebug(page) &&
-                                               (s->flags & SLAB_STORE_USER))
+                       if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
                                add_full(n, page);
                }
                slab_unlock(page);
@@ -1447,10 +1563,77 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
        }
 }
 
+#ifdef CONFIG_PREEMPT
+/*
+ * Calculate the next globally unique transaction for disambiguiation
+ * during cmpxchg. The transactions start with the cpu number and are then
+ * incremented by CONFIG_NR_CPUS.
+ */
+#define TID_STEP  roundup_pow_of_two(CONFIG_NR_CPUS)
+#else
+/*
+ * No preemption supported therefore also no need to check for
+ * different cpus.
+ */
+#define TID_STEP 1
+#endif
+
+static inline unsigned long next_tid(unsigned long tid)
+{
+       return tid + TID_STEP;
+}
+
+static inline unsigned int tid_to_cpu(unsigned long tid)
+{
+       return tid % TID_STEP;
+}
+
+static inline unsigned long tid_to_event(unsigned long tid)
+{
+       return tid / TID_STEP;
+}
+
+static inline unsigned int init_tid(int cpu)
+{
+       return cpu;
+}
+
+static inline void note_cmpxchg_failure(const char *n,
+               const struct kmem_cache *s, unsigned long tid)
+{
+#ifdef SLUB_DEBUG_CMPXCHG
+       unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
+
+       printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
+
+#ifdef CONFIG_PREEMPT
+       if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
+               printk("due to cpu change %d -> %d\n",
+                       tid_to_cpu(tid), tid_to_cpu(actual_tid));
+       else
+#endif
+       if (tid_to_event(tid) != tid_to_event(actual_tid))
+               printk("due to cpu running other code. Event %ld->%ld\n",
+                       tid_to_event(tid), tid_to_event(actual_tid));
+       else
+               printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
+                       actual_tid, tid, next_tid(tid));
+#endif
+       stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
+}
+
+void init_kmem_cache_cpus(struct kmem_cache *s)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
+}
 /*
  * Remove the cpu slab
  */
 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
+       __releases(bitlock)
 {
        struct page *page = c->page;
        int tail = 1;
@@ -1477,6 +1660,7 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
                page->inuse--;
        }
        c->page = NULL;
+       c->tid = next_tid(c->tid);
        unfreeze_slab(s, page, tail);
 }
 
@@ -1519,7 +1703,7 @@ static void flush_all(struct kmem_cache *s)
 static inline int node_match(struct kmem_cache_cpu *c, int node)
 {
 #ifdef CONFIG_NUMA
-       if (node != -1 && c->node != node)
+       if (node != NUMA_NO_NODE && c->node != node)
                return 0;
 #endif
        return 1;
@@ -1610,33 +1794,46 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
                          unsigned long addr, struct kmem_cache_cpu *c)
 {
        void **object;
-       struct page *new;
+       struct page *page;
+       unsigned long flags;
+
+       local_irq_save(flags);
+#ifdef CONFIG_PREEMPT
+       /*
+        * We may have been preempted and rescheduled on a different
+        * cpu before disabling interrupts. Need to reload cpu area
+        * pointer.
+        */
+       c = this_cpu_ptr(s->cpu_slab);
+#endif
 
        /* We handle __GFP_ZERO in the caller */
        gfpflags &= ~__GFP_ZERO;
 
-       if (!c->page)
+       page = c->page;
+       if (!page)
                goto new_slab;
 
-       slab_lock(c->page);
+       slab_lock(page);
        if (unlikely(!node_match(c, node)))
                goto another_slab;
 
        stat(s, ALLOC_REFILL);
 
 load_freelist:
-       object = c->page->freelist;
+       object = page->freelist;
        if (unlikely(!object))
                goto another_slab;
-       if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
+       if (kmem_cache_debug(s))
                goto debug;
 
        c->freelist = get_freepointer(s, object);
-       c->page->inuse = c->page->objects;
-       c->page->freelist = NULL;
-       c->node = page_to_nid(c->page);
-unlock_out:
-       slab_unlock(c->page);
+       page->inuse = page->objects;
+       page->freelist = NULL;
+
+       slab_unlock(page);
+       c->tid = next_tid(c->tid);
+       local_irq_restore(flags);
        stat(s, ALLOC_SLOWPATH);
        return object;
 
@@ -1644,42 +1841,50 @@ another_slab:
        deactivate_slab(s, c);
 
 new_slab:
-       new = get_partial(s, gfpflags, node);
-       if (new) {
-               c->page = new;
+       page = get_partial(s, gfpflags, node);
+       if (page) {
                stat(s, ALLOC_FROM_PARTIAL);
+               c->node = page_to_nid(page);
+               c->page = page;
                goto load_freelist;
        }
 
+       gfpflags &= gfp_allowed_mask;
        if (gfpflags & __GFP_WAIT)
                local_irq_enable();
 
-       new = new_slab(s, gfpflags, node);
+       page = new_slab(s, gfpflags, node);
 
        if (gfpflags & __GFP_WAIT)
                local_irq_disable();
 
-       if (new) {
+       if (page) {
                c = __this_cpu_ptr(s->cpu_slab);
                stat(s, ALLOC_SLAB);
                if (c->page)
                        flush_slab(s, c);
-               slab_lock(new);
-               __SetPageSlubFrozen(new);
-               c->page = new;
+
+               slab_lock(page);
+               __SetPageSlubFrozen(page);
+               c->node = page_to_nid(page);
+               c->page = page;
                goto load_freelist;
        }
        if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
                slab_out_of_memory(s, gfpflags, node);
+       local_irq_restore(flags);
        return NULL;
 debug:
-       if (!alloc_debug_processing(s, c->page, object, addr))
+       if (!alloc_debug_processing(s, page, object, addr))
                goto another_slab;
 
-       c->page->inuse++;
-       c->page->freelist = get_freepointer(s, object);
-       c->node = -1;
-       goto unlock_out;
+       page->inuse++;
+       page->freelist = get_freepointer(s, object);
+       deactivate_slab(s, c);
+       c->page = NULL;
+       c->node = NUMA_NO_NODE;
+       local_irq_restore(flags);
+       return object;
 }
 
 /*
@@ -1697,41 +1902,70 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
 {
        void **object;
        struct kmem_cache_cpu *c;
-       unsigned long flags;
-
-       gfpflags &= gfp_allowed_mask;
-
-       lockdep_trace_alloc(gfpflags);
-       might_sleep_if(gfpflags & __GFP_WAIT);
+       unsigned long tid;
 
-       if (should_failslab(s->objsize, gfpflags, s->flags))
+       if (slab_pre_alloc_hook(s, gfpflags))
                return NULL;
 
-       local_irq_save(flags);
+redo:
+
+       /*
+        * Must read kmem_cache cpu data via this cpu ptr. Preemption is
+        * enabled. We may switch back and forth between cpus while
+        * reading from one cpu area. That does not matter as long
+        * as we end up on the original cpu again when doing the cmpxchg.
+        */
        c = __this_cpu_ptr(s->cpu_slab);
+
+       /*
+        * The transaction ids are globally unique per cpu and per operation on
+        * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
+        * occurs on the right processor and that there was no operation on the
+        * linked list in between.
+        */
+       tid = c->tid;
+       barrier();
+
        object = c->freelist;
        if (unlikely(!object || !node_match(c, node)))
 
                object = __slab_alloc(s, gfpflags, node, addr, c);
 
        else {
-               c->freelist = get_freepointer(s, object);
+               /*
+                * The cmpxchg will only match if there was no additional
+                * operation and if we are on the right processor.
+                *
+                * The cmpxchg does the following atomically (without lock semantics!)
+                * 1. Relocate first pointer to the current per cpu area.
+                * 2. Verify that tid and freelist have not been changed
+                * 3. If they were not changed replace tid and freelist
+                *
+                * Since this is without lock semantics the protection is only against
+                * code executing on this cpu *not* from access by other cpus.
+                */
+               if (unlikely(!irqsafe_cpu_cmpxchg_double(
+                               s->cpu_slab->freelist, s->cpu_slab->tid,
+                               object, tid,
+                               get_freepointer_safe(s, object), next_tid(tid)))) {
+
+                       note_cmpxchg_failure("slab_alloc", s, tid);
+                       goto redo;
+               }
                stat(s, ALLOC_FASTPATH);
        }
-       local_irq_restore(flags);
 
        if (unlikely(gfpflags & __GFP_ZERO) && object)
                memset(object, 0, s->objsize);
 
-       kmemcheck_slab_alloc(s, gfpflags, object, s->objsize);
-       kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, gfpflags);
+       slab_post_alloc_hook(s, gfpflags, object);
 
        return object;
 }
 
 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
 {
-       void *ret = slab_alloc(s, gfpflags, -1, _RET_IP_);
+       void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
 
        trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
 
@@ -1740,11 +1974,21 @@ void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
 EXPORT_SYMBOL(kmem_cache_alloc);
 
 #ifdef CONFIG_TRACING
-void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
+void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
+{
+       void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
+       trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
+       return ret;
+}
+EXPORT_SYMBOL(kmem_cache_alloc_trace);
+
+void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
 {
-       return slab_alloc(s, gfpflags, -1, _RET_IP_);
+       void *ret = kmalloc_order(size, flags, order);
+       trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
+       return ret;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_notrace);
+EXPORT_SYMBOL(kmalloc_order_trace);
 #endif
 
 #ifdef CONFIG_NUMA
@@ -1758,16 +2002,20 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
        return ret;
 }
 EXPORT_SYMBOL(kmem_cache_alloc_node);
-#endif
 
 #ifdef CONFIG_TRACING
-void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
+void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
                                    gfp_t gfpflags,
-                                   int node)
+                                   int node, size_t size)
 {
-       return slab_alloc(s, gfpflags, node, _RET_IP_);
+       void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
+
+       trace_kmalloc_node(_RET_IP_, ret,
+                          size, s->size, gfpflags, node);
+       return ret;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
+EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
+#endif
 #endif
 
 /*
@@ -1783,14 +2031,15 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 {
        void *prior;
        void **object = (void *)x;
+       unsigned long flags;
 
-       stat(s, FREE_SLOWPATH);
+       local_irq_save(flags);
        slab_lock(page);
+       stat(s, FREE_SLOWPATH);
 
-       if (unlikely(SLABDEBUG && PageSlubDebug(page)))
-               goto debug;
+       if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
+               goto out_unlock;
 
-checks_ok:
        prior = page->freelist;
        set_freepointer(s, object, prior);
        page->freelist = object;
@@ -1815,6 +2064,7 @@ checks_ok:
 
 out_unlock:
        slab_unlock(page);
+       local_irq_restore(flags);
        return;
 
 slab_empty:
@@ -1826,14 +2076,9 @@ slab_empty:
                stat(s, FREE_REMOVE_PARTIAL);
        }
        slab_unlock(page);
+       local_irq_restore(flags);
        stat(s, FREE_SLAB);
        discard_slab(s, page);
-       return;
-
-debug:
-       if (!free_debug_processing(s, page, x, addr))
-               goto out_unlock;
-       goto checks_ok;
 }
 
 /*
@@ -1852,23 +2097,38 @@ static __always_inline void slab_free(struct kmem_cache *s,
 {
        void **object = (void *)x;
        struct kmem_cache_cpu *c;
-       unsigned long flags;
+       unsigned long tid;
 
-       kmemleak_free_recursive(x, s->flags);
-       local_irq_save(flags);
+       slab_free_hook(s, x);
+
+redo:
+
+       /*
+        * Determine the currently cpus per cpu slab.
+        * The cpu may change afterward. However that does not matter since
+        * data is retrieved via this pointer. If we are on the same cpu
+        * during the cmpxchg then the free will succedd.
+        */
        c = __this_cpu_ptr(s->cpu_slab);
-       kmemcheck_slab_free(s, object, s->objsize);
-       debug_check_no_locks_freed(object, s->objsize);
-       if (!(s->flags & SLAB_DEBUG_OBJECTS))
-               debug_check_no_obj_freed(object, s->objsize);
-       if (likely(page == c->page && c->node >= 0)) {
+
+       tid = c->tid;
+       barrier();
+
+       if (likely(page == c->page)) {
                set_freepointer(s, object, c->freelist);
-               c->freelist = object;
+
+               if (unlikely(!irqsafe_cpu_cmpxchg_double(
+                               s->cpu_slab->freelist, s->cpu_slab->tid,
+                               c->freelist, tid,
+                               object, next_tid(tid)))) {
+
+                       note_cmpxchg_failure("slab_free", s, tid);
+                       goto redo;
+               }
                stat(s, FREE_FASTPATH);
        } else
                __slab_free(s, page, x, addr);
 
-       local_irq_restore(flags);
 }
 
 void kmem_cache_free(struct kmem_cache *s, void *x)
@@ -1883,17 +2143,6 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
-/* Figure out on which slab page the object resides */
-static struct page *get_object_page(const void *x)
-{
-       struct page *page = virt_to_head_page(x);
-
-       if (!PageSlab(page))
-               return NULL;
-
-       return page;
-}
-
 /*
  * Object placement in a slab is made very easy because we always start at
  * offset 0. If we tune the size of the object to the alignment then we can
@@ -1949,13 +2198,13 @@ static int slub_nomerge;
  * the smallest order which will fit the object.
  */
 static inline int slab_order(int size, int min_objects,
-                               int max_order, int fract_leftover)
+                               int max_order, int fract_leftover, int reserved)
 {
        int order;
        int rem;
        int min_order = slub_min_order;
 
-       if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
+       if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
                return get_order(size * MAX_OBJS_PER_PAGE) - 1;
 
        for (order = max(min_order,
@@ -1964,10 +2213,10 @@ static inline int slab_order(int size, int min_objects,
 
                unsigned long slab_size = PAGE_SIZE << order;
 
-               if (slab_size < min_objects * size)
+               if (slab_size < min_objects * size + reserved)
                        continue;
 
-               rem = slab_size % size;
+               rem = (slab_size - reserved) % size;
 
                if (rem <= slab_size / fract_leftover)
                        break;
@@ -1977,7 +2226,7 @@ static inline int slab_order(int size, int min_objects,
        return order;
 }
 
-static inline int calculate_order(int size)
+static inline int calculate_order(int size, int reserved)
 {
        int order;
        int min_objects;
@@ -1995,14 +2244,14 @@ static inline int calculate_order(int size)
        min_objects = slub_min_objects;
        if (!min_objects)
                min_objects = 4 * (fls(nr_cpu_ids) + 1);
-       max_objects = (PAGE_SIZE << slub_max_order)/size;
+       max_objects = order_objects(slub_max_order, size, reserved);
        min_objects = min(min_objects, max_objects);
 
        while (min_objects > 1) {
                fraction = 16;
                while (fraction >= 4) {
                        order = slab_order(size, min_objects,
-                                               slub_max_order, fraction);
+                                       slub_max_order, fraction, reserved);
                        if (order <= slub_max_order)
                                return order;
                        fraction /= 2;
@@ -2014,14 +2263,14 @@ static inline int calculate_order(int size)
         * We were unable to place multiple objects in a slab. Now
         * lets see if we can place a single object there.
         */
-       order = slab_order(size, 1, slub_max_order, 1);
+       order = slab_order(size, 1, slub_max_order, 1, reserved);
        if (order <= slub_max_order)
                return order;
 
        /*
         * Doh this slab cannot be placed using slub_max_order.
         */
-       order = slab_order(size, 1, MAX_ORDER, 1);
+       order = slab_order(size, 1, MAX_ORDER, 1, reserved);
        if (order < MAX_ORDER)
                return order;
        return -ENOSYS;
@@ -2066,26 +2315,32 @@ init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
 #endif
 }
 
-static DEFINE_PER_CPU(struct kmem_cache_cpu, kmalloc_percpu[KMALLOC_CACHES]);
-
-static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
+static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
 {
-       if (s < kmalloc_caches + KMALLOC_CACHES && s >= kmalloc_caches)
-               /*
-                * Boot time creation of the kmalloc array. Use static per cpu data
-                * since the per cpu allocator is not available yet.
-                */
-               s->cpu_slab = kmalloc_percpu + (s - kmalloc_caches);
-       else
-               s->cpu_slab =  alloc_percpu(struct kmem_cache_cpu);
+       BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
+                       SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
+
+#ifdef CONFIG_CMPXCHG_LOCAL
+       /*
+        * Must align to double word boundary for the double cmpxchg instructions
+        * to work.
+        */
+       s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu), 2 * sizeof(void *));
+#else
+       /* Regular alignment is sufficient */
+       s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
+#endif
 
        if (!s->cpu_slab)
                return 0;
 
+       init_kmem_cache_cpus(s);
+
        return 1;
 }
 
-#ifdef CONFIG_NUMA
+static struct kmem_cache *kmem_cache_node;
+
 /*
  * No kmalloc_node yet so do it by hand. We know that this is the first
  * slab on the node for this slabcache. There are no concurrent accesses
@@ -2095,15 +2350,15 @@ static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  * memory on a fresh node that has no slab structures yet.
  */
-static void early_kmem_cache_node_alloc(gfp_t gfpflags, int node)
+static void early_kmem_cache_node_alloc(int node)
 {
        struct page *page;
        struct kmem_cache_node *n;
        unsigned long flags;
 
-       BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
+       BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
 
-       page = new_slab(kmalloc_caches, gfpflags, node);
+       page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
 
        BUG_ON(!page);
        if (page_to_nid(page) != node) {
@@ -2115,15 +2370,15 @@ static void early_kmem_cache_node_alloc(gfp_t gfpflags, int node)
 
        n = page->freelist;
        BUG_ON(!n);
-       page->freelist = get_freepointer(kmalloc_caches, n);
+       page->freelist = get_freepointer(kmem_cache_node, n);
        page->inuse++;
-       kmalloc_caches->node[node] = n;
+       kmem_cache_node->node[node] = n;
 #ifdef CONFIG_SLUB_DEBUG
-       init_object(kmalloc_caches, n, 1);
-       init_tracking(kmalloc_caches, n);
+       init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
+       init_tracking(kmem_cache_node, n);
 #endif
-       init_kmem_cache_node(n, kmalloc_caches);
-       inc_slabs_node(kmalloc_caches, node, page->objects);
+       init_kmem_cache_node(n, kmem_cache_node);
+       inc_slabs_node(kmem_cache_node, node, page->objects);
 
        /*
         * lockdep requires consistent irq usage for each lock
@@ -2141,60 +2396,40 @@ static void free_kmem_cache_nodes(struct kmem_cache *s)
 
        for_each_node_state(node, N_NORMAL_MEMORY) {
                struct kmem_cache_node *n = s->node[node];
-               if (n && n != &s->local_node)
-                       kmem_cache_free(kmalloc_caches, n);
+
+               if (n)
+                       kmem_cache_free(kmem_cache_node, n);
+
                s->node[node] = NULL;
        }
 }
 
-static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
+static int init_kmem_cache_nodes(struct kmem_cache *s)
 {
        int node;
-       int local_node;
-
-       if (slab_state >= UP && (s < kmalloc_caches ||
-                       s >= kmalloc_caches + KMALLOC_CACHES))
-               local_node = page_to_nid(virt_to_page(s));
-       else
-               local_node = 0;
 
        for_each_node_state(node, N_NORMAL_MEMORY) {
                struct kmem_cache_node *n;
 
-               if (local_node == node)
-                       n = &s->local_node;
-               else {
-                       if (slab_state == DOWN) {
-                               early_kmem_cache_node_alloc(gfpflags, node);
-                               continue;
-                       }
-                       n = kmem_cache_alloc_node(kmalloc_caches,
-                                                       gfpflags, node);
-
-                       if (!n) {
-                               free_kmem_cache_nodes(s);
-                               return 0;
-                       }
+               if (slab_state == DOWN) {
+                       early_kmem_cache_node_alloc(node);
+                       continue;
+               }
+               n = kmem_cache_alloc_node(kmem_cache_node,
+                                               GFP_KERNEL, node);
 
+               if (!n) {
+                       free_kmem_cache_nodes(s);
+                       return 0;
                }
+
                s->node[node] = n;
                init_kmem_cache_node(n, s);
        }
        return 1;
 }
-#else
-static void free_kmem_cache_nodes(struct kmem_cache *s)
-{
-}
 
-static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
-{
-       init_kmem_cache_node(&s->local_node, s);
-       return 1;
-}
-#endif
-
-static void set_min_partial(struct kmem_cache *s, unsigned long min)
+static void set_min_partial(struct kmem_cache *s, unsigned long min)
 {
        if (min < MIN_PARTIAL)
                min = MIN_PARTIAL;
@@ -2300,7 +2535,7 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order)
        if (forced_order >= 0)
                order = forced_order;
        else
-               order = calculate_order(size);
+               order = calculate_order(size, s->reserved);
 
        if (order < 0)
                return 0;
@@ -2318,8 +2553,8 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order)
        /*
         * Determine the number of objects per slab
         */
-       s->oo = oo_make(order, size);
-       s->min = oo_make(get_order(size), size);
+       s->oo = oo_make(order, size, s->reserved);
+       s->min = oo_make(get_order(size), size, s->reserved);
        if (oo_objects(s->oo) > oo_objects(s->max))
                s->max = s->oo;
 
@@ -2327,7 +2562,7 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order)
 
 }
 
-static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
+static int kmem_cache_open(struct kmem_cache *s,
                const char *name, size_t size,
                size_t align, unsigned long flags,
                void (*ctor)(void *))
@@ -2338,6 +2573,10 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
        s->objsize = size;
        s->align = align;
        s->flags = kmem_cache_flags(size, flags, name, ctor);
+       s->reserved = 0;
+
+       if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
+               s->reserved = sizeof(struct rcu_head);
 
        if (!calculate_sizes(s, -1))
                goto error;
@@ -2363,10 +2602,10 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
 #ifdef CONFIG_NUMA
        s->remote_node_defrag_ratio = 1000;
 #endif
-       if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
+       if (!init_kmem_cache_nodes(s))
                goto error;
 
-       if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
+       if (alloc_kmem_cache_cpus(s))
                return 1;
 
        free_kmem_cache_nodes(s);
@@ -2379,35 +2618,6 @@ error:
        return 0;
 }
 
-/*
- * Check if a given pointer is valid
- */
-int kmem_ptr_validate(struct kmem_cache *s, const void *object)
-{
-       struct page *page;
-
-       if (!kern_ptr_validate(object, s->size))
-               return 0;
-
-       page = get_object_page(object);
-
-       if (!page || s != page->slab)
-               /* No slab or wrong slab */
-               return 0;
-
-       if (!check_valid_pointer(s, page, object))
-               return 0;
-
-       /*
-        * We could also check if the object is on the slabs freelist.
-        * But this would be too expensive and it seems that the main
-        * purpose of kmem_ptr_valid() is to check if the object belongs
-        * to a certain slab.
-        */
-       return 1;
-}
-EXPORT_SYMBOL(kmem_ptr_validate);
-
 /*
  * Determine the size of a slab object
  */
@@ -2417,28 +2627,20 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 }
 EXPORT_SYMBOL(kmem_cache_size);
 
-const char *kmem_cache_name(struct kmem_cache *s)
-{
-       return s->name;
-}
-EXPORT_SYMBOL(kmem_cache_name);
-
 static void list_slab_objects(struct kmem_cache *s, struct page *page,
                                                        const char *text)
 {
 #ifdef CONFIG_SLUB_DEBUG
        void *addr = page_address(page);
        void *p;
-       long *map = kzalloc(BITS_TO_LONGS(page->objects) * sizeof(long),
-                           GFP_ATOMIC);
-
+       unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
+                                    sizeof(long), GFP_ATOMIC);
        if (!map)
                return;
        slab_err(s, page, "%s", text);
        slab_lock(page);
-       for_each_free_object(p, s, page->freelist)
-               set_bit(slab_index(p, s, addr), map);
 
+       get_map(s, page, map);
        for_each_object(p, s, addr, page->objects) {
 
                if (!test_bit(slab_index(p, s, addr), map)) {
@@ -2463,9 +2665,8 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
        spin_lock_irqsave(&n->list_lock, flags);
        list_for_each_entry_safe(page, h, &n->partial, lru) {
                if (!page->inuse) {
-                       list_del(&page->lru);
+                       __remove_partial(n, page);
                        discard_slab(s, page);
-                       n->nr_partial--;
                } else {
                        list_slab_objects(s, page,
                                "Objects remaining on kmem_cache_close()");
@@ -2505,7 +2706,6 @@ void kmem_cache_destroy(struct kmem_cache *s)
        s->refcount--;
        if (!s->refcount) {
                list_del(&s->list);
-               up_write(&slub_lock);
                if (kmem_cache_close(s)) {
                        printk(KERN_ERR "SLUB %s: %s called for cache that "
                                "still has objects.\n", s->name, __func__);
@@ -2514,8 +2714,8 @@ void kmem_cache_destroy(struct kmem_cache *s)
                if (s->flags & SLAB_DESTROY_BY_RCU)
                        rcu_barrier();
                sysfs_slab_remove(s);
-       } else
-               up_write(&slub_lock);
+       }
+       up_write(&slub_lock);
 }
 EXPORT_SYMBOL(kmem_cache_destroy);
 
@@ -2523,9 +2723,15 @@ EXPORT_SYMBOL(kmem_cache_destroy);
  *             Kmalloc subsystem
  *******************************************************************/
 
-struct kmem_cache kmalloc_caches[KMALLOC_CACHES] __cacheline_aligned;
+struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
 EXPORT_SYMBOL(kmalloc_caches);
 
+static struct kmem_cache *kmem_cache;
+
+#ifdef CONFIG_ZONE_DMA
+static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
+#endif
+
 static int __init setup_slub_min_order(char *str)
 {
        get_option(&str, &slub_min_order);
@@ -2562,116 +2768,29 @@ static int __init setup_slub_nomerge(char *str)
 
 __setup("slub_nomerge", setup_slub_nomerge);
 
-static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
-               const char *name, int size, gfp_t gfp_flags)
+static struct kmem_cache *__init create_kmalloc_cache(const char *name,
+                                               int size, unsigned int flags)
 {
-       unsigned int flags = 0;
+       struct kmem_cache *s;
 
-       if (gfp_flags & SLUB_DMA)
-               flags = SLAB_CACHE_DMA;
+       s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
 
        /*
         * This function is called with IRQs disabled during early-boot on
         * single CPU so there's no need to take slub_lock here.
         */
-       if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
+       if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
                                                                flags, NULL))
                goto panic;
 
        list_add(&s->list, &slab_caches);
-
-       if (sysfs_slab_add(s))
-               goto panic;
        return s;
 
 panic:
        panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
+       return NULL;
 }
 
-#ifdef CONFIG_ZONE_DMA
-static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
-
-static void sysfs_add_func(struct work_struct *w)
-{
-       struct kmem_cache *s;
-
-       down_write(&slub_lock);
-       list_for_each_entry(s, &slab_caches, list) {
-               if (s->flags & __SYSFS_ADD_DEFERRED) {
-                       s->flags &= ~__SYSFS_ADD_DEFERRED;
-                       sysfs_slab_add(s);
-               }
-       }
-       up_write(&slub_lock);
-}
-
-static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
-
-static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
-{
-       struct kmem_cache *s;
-       char *text;
-       size_t realsize;
-       unsigned long slabflags;
-       int i;
-
-       s = kmalloc_caches_dma[index];
-       if (s)
-               return s;
-
-       /* Dynamically create dma cache */
-       if (flags & __GFP_WAIT)
-               down_write(&slub_lock);
-       else {
-               if (!down_write_trylock(&slub_lock))
-                       goto out;
-       }
-
-       if (kmalloc_caches_dma[index])
-               goto unlock_out;
-
-       realsize = kmalloc_caches[index].objsize;
-       text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
-                        (unsigned int)realsize);
-
-       s = NULL;
-       for (i = 0; i < KMALLOC_CACHES; i++)
-               if (!kmalloc_caches[i].size)
-                       break;
-
-       BUG_ON(i >= KMALLOC_CACHES);
-       s = kmalloc_caches + i;
-
-       /*
-        * Must defer sysfs creation to a workqueue because we don't know
-        * what context we are called from. Before sysfs comes up, we don't
-        * need to do anything because our sysfs initcall will start by
-        * adding all existing slabs to sysfs.
-        */
-       slabflags = SLAB_CACHE_DMA|SLAB_NOTRACK;
-       if (slab_state >= SYSFS)
-               slabflags |= __SYSFS_ADD_DEFERRED;
-
-       if (!text || !kmem_cache_open(s, flags, text,
-                       realsize, ARCH_KMALLOC_MINALIGN, slabflags, NULL)) {
-               s->size = 0;
-               kfree(text);
-               goto unlock_out;
-       }
-
-       list_add(&s->list, &slab_caches);
-       kmalloc_caches_dma[index] = s;
-
-       if (slab_state >= SYSFS)
-               schedule_work(&sysfs_add_work);
-
-unlock_out:
-       up_write(&slub_lock);
-out:
-       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
@@ -2724,10 +2843,10 @@ static struct kmem_cache *get_slab(size_t size, gfp_t flags)
 
 #ifdef CONFIG_ZONE_DMA
        if (unlikely((flags & SLUB_DMA)))
-               return dma_kmalloc_cache(index, flags);
+               return kmalloc_dma_caches[index];
 
 #endif
-       return &kmalloc_caches[index];
+       return kmalloc_caches[index];
 }
 
 void *__kmalloc(size_t size, gfp_t flags)
@@ -2743,7 +2862,7 @@ void *__kmalloc(size_t size, gfp_t flags)
        if (unlikely(ZERO_OR_NULL_PTR(s)))
                return s;
 
-       ret = slab_alloc(s, flags, -1, _RET_IP_);
+       ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
 
        trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
 
@@ -2751,6 +2870,7 @@ void *__kmalloc(size_t size, gfp_t flags)
 }
 EXPORT_SYMBOL(__kmalloc);
 
+#ifdef CONFIG_NUMA
 static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
 {
        struct page *page;
@@ -2765,7 +2885,6 @@ static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
        return ptr;
 }
 
-#ifdef CONFIG_NUMA
 void *__kmalloc_node(size_t size, gfp_t flags, int node)
 {
        struct kmem_cache *s;
@@ -2798,7 +2917,6 @@ EXPORT_SYMBOL(__kmalloc_node);
 size_t ksize(const void *object)
 {
        struct page *page;
-       struct kmem_cache *s;
 
        if (unlikely(object == ZERO_SIZE_PTR))
                return 0;
@@ -2809,28 +2927,8 @@ size_t ksize(const void *object)
                WARN_ON(!PageCompound(page));
                return PAGE_SIZE << compound_order(page);
        }
-       s = page->slab;
 
-#ifdef CONFIG_SLUB_DEBUG
-       /*
-        * Debugging requires use of the padding between object
-        * and whatever may come after it.
-        */
-       if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
-               return s->objsize;
-
-#endif
-       /*
-        * If we have the need to store the freelist pointer
-        * back there or track user information then we can
-        * only use the space before that information.
-        */
-       if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
-               return s->inuse;
-       /*
-        * Else we can use all the padding etc for the allocation
-        */
-       return s->size;
+       return slab_ksize(page->slab);
 }
 EXPORT_SYMBOL(ksize);
 
@@ -2905,8 +3003,7 @@ int kmem_cache_shrink(struct kmem_cache *s)
                                 * may have freed the last object and be
                                 * waiting to release the slab.
                                 */
-                               list_del(&page->lru);
-                               n->nr_partial--;
+                               __remove_partial(n, page);
                                slab_unlock(page);
                                discard_slab(s, page);
                        } else {
@@ -2930,7 +3027,7 @@ int kmem_cache_shrink(struct kmem_cache *s)
 }
 EXPORT_SYMBOL(kmem_cache_shrink);
 
-#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
+#if defined(CONFIG_MEMORY_HOTPLUG)
 static int slab_mem_going_offline_callback(void *arg)
 {
        struct kmem_cache *s;
@@ -2972,7 +3069,7 @@ static void slab_mem_offline_callback(void *arg)
                        BUG_ON(slabs_node(s, offline_node));
 
                        s->node[offline_node] = NULL;
-                       kmem_cache_free(kmalloc_caches, n);
+                       kmem_cache_free(kmem_cache_node, n);
                }
        }
        up_read(&slub_lock);
@@ -3005,7 +3102,7 @@ static int slab_mem_going_online_callback(void *arg)
                 *      since memory is not yet available from the node that
                 *      is brought up.
                 */
-               n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
+               n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
                if (!n) {
                        ret = -ENOMEM;
                        goto out;
@@ -3051,46 +3148,92 @@ static int slab_memory_callback(struct notifier_block *self,
  *                     Basic setup of slabs
  *******************************************************************/
 
+/*
+ * Used for early kmem_cache structures that were allocated using
+ * the page allocator
+ */
+
+static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
+{
+       int node;
+
+       list_add(&s->list, &slab_caches);
+       s->refcount = -1;
+
+       for_each_node_state(node, N_NORMAL_MEMORY) {
+               struct kmem_cache_node *n = get_node(s, node);
+               struct page *p;
+
+               if (n) {
+                       list_for_each_entry(p, &n->partial, lru)
+                               p->slab = s;
+
+#ifdef CONFIG_SLUB_DEBUG
+                       list_for_each_entry(p, &n->full, lru)
+                               p->slab = s;
+#endif
+               }
+       }
+}
+
 void __init kmem_cache_init(void)
 {
        int i;
        int caches = 0;
+       struct kmem_cache *temp_kmem_cache;
+       int order;
+       struct kmem_cache *temp_kmem_cache_node;
+       unsigned long kmalloc_size;
+
+       kmem_size = offsetof(struct kmem_cache, node) +
+                               nr_node_ids * sizeof(struct kmem_cache_node *);
+
+       /* Allocate two kmem_caches from the page allocator */
+       kmalloc_size = ALIGN(kmem_size, cache_line_size());
+       order = get_order(2 * kmalloc_size);
+       kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
 
-#ifdef CONFIG_NUMA
        /*
         * Must first have the slab cache available for the allocations of the
         * struct kmem_cache_node's. There is special bootstrap code in
         * kmem_cache_open for slab_state == DOWN.
         */
-       create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
-               sizeof(struct kmem_cache_node), GFP_NOWAIT);
-       kmalloc_caches[0].refcount = -1;
-       caches++;
+       kmem_cache_node = (void *)kmem_cache + kmalloc_size;
+
+       kmem_cache_open(kmem_cache_node, "kmem_cache_node",
+               sizeof(struct kmem_cache_node),
+               0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
 
        hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
-#endif
 
        /* Able to allocate the per node structures */
        slab_state = PARTIAL;
 
-       /* Caches that are not of the two-to-the-power-of size */
-       if (KMALLOC_MIN_SIZE <= 32) {
-               create_kmalloc_cache(&kmalloc_caches[1],
-                               "kmalloc-96", 96, GFP_NOWAIT);
-               caches++;
-       }
-       if (KMALLOC_MIN_SIZE <= 64) {
-               create_kmalloc_cache(&kmalloc_caches[2],
-                               "kmalloc-192", 192, GFP_NOWAIT);
-               caches++;
-       }
+       temp_kmem_cache = kmem_cache;
+       kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
+               0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+       kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
+       memcpy(kmem_cache, temp_kmem_cache, kmem_size);
 
-       for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
-               create_kmalloc_cache(&kmalloc_caches[i],
-                       "kmalloc", 1 << i, GFP_NOWAIT);
-               caches++;
-       }
+       /*
+        * Allocate kmem_cache_node properly from the kmem_cache slab.
+        * kmem_cache_node is separately allocated so no need to
+        * update any list pointers.
+        */
+       temp_kmem_cache_node = kmem_cache_node;
+
+       kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
+       memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
+
+       kmem_cache_bootstrap_fixup(kmem_cache_node);
 
+       caches++;
+       kmem_cache_bootstrap_fixup(kmem_cache);
+       caches++;
+       /* Free temporary boot structure */
+       free_pages((unsigned long)temp_kmem_cache, order);
+
+       /* Now we can use the kmem_cache to allocate kmalloc slabs */
 
        /*
         * Patch up the size_index table if we have strange large alignment
@@ -3130,23 +3273,60 @@ void __init kmem_cache_init(void)
                        size_index[size_index_elem(i)] = 8;
        }
 
+       /* Caches that are not of the two-to-the-power-of size */
+       if (KMALLOC_MIN_SIZE <= 32) {
+               kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
+               caches++;
+       }
+
+       if (KMALLOC_MIN_SIZE <= 64) {
+               kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
+               caches++;
+       }
+
+       for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
+               kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
+               caches++;
+       }
+
        slab_state = UP;
 
        /* Provide the correct kmalloc names now that the caches are up */
-       for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
-               kmalloc_caches[i]. name =
-                       kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
+       if (KMALLOC_MIN_SIZE <= 32) {
+               kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
+               BUG_ON(!kmalloc_caches[1]->name);
+       }
+
+       if (KMALLOC_MIN_SIZE <= 64) {
+               kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
+               BUG_ON(!kmalloc_caches[2]->name);
+       }
+
+       for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
+               char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
+
+               BUG_ON(!s);
+               kmalloc_caches[i]->name = s;
+       }
 
 #ifdef CONFIG_SMP
        register_cpu_notifier(&slab_notifier);
 #endif
-#ifdef CONFIG_NUMA
-       kmem_size = offsetof(struct kmem_cache, node) +
-                               nr_node_ids * sizeof(struct kmem_cache_node *);
-#else
-       kmem_size = sizeof(struct kmem_cache);
-#endif
 
+#ifdef CONFIG_ZONE_DMA
+       for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
+               struct kmem_cache *s = kmalloc_caches[i];
+
+               if (s && s->size) {
+                       char *name = kasprintf(GFP_NOWAIT,
+                                "dma-kmalloc-%d", s->objsize);
+
+                       BUG_ON(!name);
+                       kmalloc_dma_caches[i] = create_kmalloc_cache(name,
+                               s->objsize, SLAB_CACHE_DMA);
+               }
+       }
+#endif
        printk(KERN_INFO
                "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
                " CPUs=%d, Nodes=%d\n",
@@ -3224,6 +3404,7 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
                size_t align, unsigned long flags, void (*ctor)(void *))
 {
        struct kmem_cache *s;
+       char *n;
 
        if (WARN_ON(!name))
                return NULL;
@@ -3238,37 +3419,39 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
                 */
                s->objsize = max(s->objsize, (int)size);
                s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
-               up_write(&slub_lock);
 
                if (sysfs_slab_alias(s, name)) {
-                       down_write(&slub_lock);
                        s->refcount--;
-                       up_write(&slub_lock);
                        goto err;
                }
+               up_write(&slub_lock);
                return s;
        }
 
+       n = kstrdup(name, GFP_KERNEL);
+       if (!n)
+               goto err;
+
        s = kmalloc(kmem_size, GFP_KERNEL);
        if (s) {
-               if (kmem_cache_open(s, GFP_KERNEL, name,
+               if (kmem_cache_open(s, n,
                                size, align, flags, ctor)) {
                        list_add(&s->list, &slab_caches);
-                       up_write(&slub_lock);
                        if (sysfs_slab_add(s)) {
-                               down_write(&slub_lock);
                                list_del(&s->list);
-                               up_write(&slub_lock);
+                               kfree(n);
                                kfree(s);
                                goto err;
                        }
+                       up_write(&slub_lock);
                        return s;
                }
+               kfree(n);
                kfree(s);
        }
+err:
        up_write(&slub_lock);
 
-err:
        if (flags & SLAB_PANIC)
                panic("Cannot create slabcache %s\n", name);
        else
@@ -3327,14 +3510,15 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
        if (unlikely(ZERO_OR_NULL_PTR(s)))
                return s;
 
-       ret = slab_alloc(s, gfpflags, -1, caller);
+       ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
 
-       /* Honor the call site pointer we recieved. */
+       /* Honor the call site pointer we received. */
        trace_kmalloc(caller, ret, size, s->size, gfpflags);
 
        return ret;
 }
 
+#ifdef CONFIG_NUMA
 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
                                        int node, unsigned long caller)
 {
@@ -3358,13 +3542,14 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
 
        ret = slab_alloc(s, gfpflags, node, caller);
 
-       /* Honor the call site pointer we recieved. */
+       /* Honor the call site pointer we received. */
        trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
 
        return ret;
 }
+#endif
 
-#ifdef CONFIG_SLUB_DEBUG
+#ifdef CONFIG_SYSFS
 static int count_inuse(struct page *page)
 {
        return page->inuse;
@@ -3374,7 +3559,9 @@ static int count_total(struct page *page)
 {
        return page->objects;
 }
+#endif
 
+#ifdef CONFIG_SLUB_DEBUG
 static int validate_slab(struct kmem_cache *s, struct page *page,
                                                unsigned long *map)
 {
@@ -3388,15 +3575,16 @@ static int validate_slab(struct kmem_cache *s, struct page *page,
        /* Now we know that a valid freelist exists */
        bitmap_zero(map, page->objects);
 
-       for_each_free_object(p, s, page->freelist) {
-               set_bit(slab_index(p, s, addr), map);
-               if (!check_object(s, page, p, 0))
-                       return 0;
+       get_map(s, page, map);
+       for_each_object(p, s, addr, page->objects) {
+               if (test_bit(slab_index(p, s, addr), map))
+                       if (!check_object(s, page, p, SLUB_RED_INACTIVE))
+                               return 0;
        }
 
        for_each_object(p, s, addr, page->objects)
                if (!test_bit(slab_index(p, s, addr), map))
-                       if (!check_object(s, page, p, 1))
+                       if (!check_object(s, page, p, SLUB_RED_ACTIVE))
                                return 0;
        return 1;
 }
@@ -3410,16 +3598,6 @@ static void validate_slab_slab(struct kmem_cache *s, struct page *page,
        } else
                printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
                        s->name, page);
-
-       if (s->flags & DEBUG_DEFAULT_FLAGS) {
-               if (!PageSlubDebug(page))
-                       printk(KERN_ERR "SLUB %s: SlubDebug not set "
-                               "on slab 0x%p\n", s->name, page);
-       } else {
-               if (PageSlubDebug(page))
-                       printk(KERN_ERR "SLUB %s: SlubDebug set on "
-                               "slab 0x%p\n", s->name, page);
-       }
 }
 
 static int validate_slab_node(struct kmem_cache *s,
@@ -3475,65 +3653,6 @@ static long validate_slab_cache(struct kmem_cache *s)
        kfree(map);
        return count;
 }
-
-#ifdef SLUB_RESILIENCY_TEST
-static void resiliency_test(void)
-{
-       u8 *p;
-
-       printk(KERN_ERR "SLUB resiliency testing\n");
-       printk(KERN_ERR "-----------------------\n");
-       printk(KERN_ERR "A. Corruption after allocation\n");
-
-       p = kzalloc(16, GFP_KERNEL);
-       p[16] = 0x12;
-       printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
-                       " 0x12->0x%p\n\n", p + 16);
-
-       validate_slab_cache(kmalloc_caches + 4);
-
-       /* Hmmm... The next two are dangerous */
-       p = kzalloc(32, GFP_KERNEL);
-       p[32 + sizeof(void *)] = 0x34;
-       printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
-                       " 0x34 -> -0x%p\n", p);
-       printk(KERN_ERR
-               "If allocated object is overwritten then not detectable\n\n");
-
-       validate_slab_cache(kmalloc_caches + 5);
-       p = kzalloc(64, GFP_KERNEL);
-       p += 64 + (get_cycles() & 0xff) * sizeof(void *);
-       *p = 0x56;
-       printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
-                                                                       p);
-       printk(KERN_ERR
-               "If allocated object is overwritten then not detectable\n\n");
-       validate_slab_cache(kmalloc_caches + 6);
-
-       printk(KERN_ERR "\nB. Corruption after free\n");
-       p = kzalloc(128, GFP_KERNEL);
-       kfree(p);
-       *p = 0x78;
-       printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
-       validate_slab_cache(kmalloc_caches + 7);
-
-       p = kzalloc(256, GFP_KERNEL);
-       kfree(p);
-       p[50] = 0x9a;
-       printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
-                       p);
-       validate_slab_cache(kmalloc_caches + 8);
-
-       p = kzalloc(512, GFP_KERNEL);
-       kfree(p);
-       p[512] = 0xab;
-       printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
-       validate_slab_cache(kmalloc_caches + 9);
-}
-#else
-static void resiliency_test(void) {};
-#endif
-
 /*
  * Generate lists of code addresses where slabcache objects are allocated
  * and freed.
@@ -3662,14 +3781,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 
 static void process_slab(struct loc_track *t, struct kmem_cache *s,
                struct page *page, enum track_item alloc,
-               long *map)
+               unsigned long *map)
 {
        void *addr = page_address(page);
        void *p;
 
        bitmap_zero(map, page->objects);
-       for_each_free_object(p, s, page->freelist)
-               set_bit(slab_index(p, s, addr), map);
+       get_map(s, page, map);
 
        for_each_object(p, s, addr, page->objects)
                if (!test_bit(slab_index(p, s, addr), map))
@@ -3718,7 +3836,7 @@ static int list_locations(struct kmem_cache *s, char *buf,
                len += sprintf(buf + len, "%7ld ", l->count);
 
                if (l->addr)
-                       len += sprint_symbol(buf + len, (unsigned long)l->addr);
+                       len += sprintf(buf + len, "%pS", (void *)l->addr);
                else
                        len += sprintf(buf + len, "<not-available>");
 
@@ -3762,7 +3880,71 @@ static int list_locations(struct kmem_cache *s, char *buf,
                len += sprintf(buf, "No data\n");
        return len;
 }
+#endif
+
+#ifdef SLUB_RESILIENCY_TEST
+static void resiliency_test(void)
+{
+       u8 *p;
+
+       BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
+
+       printk(KERN_ERR "SLUB resiliency testing\n");
+       printk(KERN_ERR "-----------------------\n");
+       printk(KERN_ERR "A. Corruption after allocation\n");
+
+       p = kzalloc(16, GFP_KERNEL);
+       p[16] = 0x12;
+       printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
+                       " 0x12->0x%p\n\n", p + 16);
+
+       validate_slab_cache(kmalloc_caches[4]);
+
+       /* Hmmm... The next two are dangerous */
+       p = kzalloc(32, GFP_KERNEL);
+       p[32 + sizeof(void *)] = 0x34;
+       printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
+                       " 0x34 -> -0x%p\n", p);
+       printk(KERN_ERR
+               "If allocated object is overwritten then not detectable\n\n");
+
+       validate_slab_cache(kmalloc_caches[5]);
+       p = kzalloc(64, GFP_KERNEL);
+       p += 64 + (get_cycles() & 0xff) * sizeof(void *);
+       *p = 0x56;
+       printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
+                                                                       p);
+       printk(KERN_ERR
+               "If allocated object is overwritten then not detectable\n\n");
+       validate_slab_cache(kmalloc_caches[6]);
+
+       printk(KERN_ERR "\nB. Corruption after free\n");
+       p = kzalloc(128, GFP_KERNEL);
+       kfree(p);
+       *p = 0x78;
+       printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
+       validate_slab_cache(kmalloc_caches[7]);
 
+       p = kzalloc(256, GFP_KERNEL);
+       kfree(p);
+       p[50] = 0x9a;
+       printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
+                       p);
+       validate_slab_cache(kmalloc_caches[8]);
+
+       p = kzalloc(512, GFP_KERNEL);
+       kfree(p);
+       p[512] = 0xab;
+       printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
+       validate_slab_cache(kmalloc_caches[9]);
+}
+#else
+#ifdef CONFIG_SYSFS
+static void resiliency_test(void) {};
+#endif
+#endif
+
+#ifdef CONFIG_SYSFS
 enum slab_stat_type {
        SL_ALL,                 /* All slabs */
        SL_PARTIAL,             /* Only partially allocated slabs */
@@ -3815,6 +3997,8 @@ static ssize_t show_slab_objects(struct kmem_cache *s,
                }
        }
 
+       lock_memory_hotplug();
+#ifdef CONFIG_SLUB_DEBUG
        if (flags & SO_ALL) {
                for_each_node_state(node, N_NORMAL_MEMORY) {
                        struct kmem_cache_node *n = get_node(s, node);
@@ -3831,7 +4015,9 @@ static ssize_t show_slab_objects(struct kmem_cache *s,
                        nodes[node] += x;
                }
 
-       } else if (flags & SO_PARTIAL) {
+       } else
+#endif
+       if (flags & SO_PARTIAL) {
                for_each_node_state(node, N_NORMAL_MEMORY) {
                        struct kmem_cache_node *n = get_node(s, node);
 
@@ -3852,10 +4038,12 @@ static ssize_t show_slab_objects(struct kmem_cache *s,
                        x += sprintf(buf + x, " N%d=%lu",
                                        node, nodes[node]);
 #endif
+       unlock_memory_hotplug();
        kfree(nodes);
        return x + sprintf(buf + x, "\n");
 }
 
+#ifdef CONFIG_SLUB_DEBUG
 static int any_slab_objects(struct kmem_cache *s)
 {
        int node;
@@ -3871,6 +4059,7 @@ static int any_slab_objects(struct kmem_cache *s)
        }
        return 0;
 }
+#endif
 
 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
@@ -3957,12 +4146,9 @@ SLAB_ATTR(min_partial);
 
 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
 {
-       if (s->ctor) {
-               int n = sprint_symbol(buf, (unsigned long)s->ctor);
-
-               return n + sprintf(buf + n, "\n");
-       }
-       return 0;
+       if (!s->ctor)
+               return 0;
+       return sprintf(buf, "%pS\n", s->ctor);
 }
 SLAB_ATTR_RO(ctor);
 
@@ -3972,12 +4158,6 @@ static ssize_t aliases_show(struct kmem_cache *s, char *buf)
 }
 SLAB_ATTR_RO(aliases);
 
-static ssize_t slabs_show(struct kmem_cache *s, char *buf)
-{
-       return show_slab_objects(s, buf, SO_ALL);
-}
-SLAB_ATTR_RO(slabs);
-
 static ssize_t partial_show(struct kmem_cache *s, char *buf)
 {
        return show_slab_objects(s, buf, SO_PARTIAL);
@@ -4002,93 +4182,89 @@ static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
 }
 SLAB_ATTR_RO(objects_partial);
 
-static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
-{
-       return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
-}
-SLAB_ATTR_RO(total_objects);
-
-static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
+static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
 }
 
-static ssize_t sanity_checks_store(struct kmem_cache *s,
+static ssize_t reclaim_account_store(struct kmem_cache *s,
                                const char *buf, size_t length)
 {
-       s->flags &= ~SLAB_DEBUG_FREE;
+       s->flags &= ~SLAB_RECLAIM_ACCOUNT;
        if (buf[0] == '1')
-               s->flags |= SLAB_DEBUG_FREE;
+               s->flags |= SLAB_RECLAIM_ACCOUNT;
        return length;
 }
-SLAB_ATTR(sanity_checks);
+SLAB_ATTR(reclaim_account);
 
-static ssize_t trace_show(struct kmem_cache *s, char *buf)
+static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
 }
+SLAB_ATTR_RO(hwcache_align);
 
-static ssize_t trace_store(struct kmem_cache *s, const char *buf,
-                                                       size_t length)
+#ifdef CONFIG_ZONE_DMA
+static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
 {
-       s->flags &= ~SLAB_TRACE;
-       if (buf[0] == '1')
-               s->flags |= SLAB_TRACE;
-       return length;
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
 }
-SLAB_ATTR(trace);
+SLAB_ATTR_RO(cache_dma);
+#endif
 
-#ifdef CONFIG_FAILSLAB
-static ssize_t failslab_show(struct kmem_cache *s, char *buf)
+static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
 }
+SLAB_ATTR_RO(destroy_by_rcu);
 
-static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
-                                                       size_t length)
+static ssize_t reserved_show(struct kmem_cache *s, char *buf)
 {
-       s->flags &= ~SLAB_FAILSLAB;
-       if (buf[0] == '1')
-               s->flags |= SLAB_FAILSLAB;
-       return length;
+       return sprintf(buf, "%d\n", s->reserved);
 }
-SLAB_ATTR(failslab);
-#endif
+SLAB_ATTR_RO(reserved);
 
-static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
+#ifdef CONFIG_SLUB_DEBUG
+static ssize_t slabs_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
+       return show_slab_objects(s, buf, SO_ALL);
 }
+SLAB_ATTR_RO(slabs);
 
-static ssize_t reclaim_account_store(struct kmem_cache *s,
-                               const char *buf, size_t length)
+static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
 {
-       s->flags &= ~SLAB_RECLAIM_ACCOUNT;
-       if (buf[0] == '1')
-               s->flags |= SLAB_RECLAIM_ACCOUNT;
-       return length;
+       return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
 }
-SLAB_ATTR(reclaim_account);
+SLAB_ATTR_RO(total_objects);
 
-static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
+static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
 }
-SLAB_ATTR_RO(hwcache_align);
 
-#ifdef CONFIG_ZONE_DMA
-static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
+static ssize_t sanity_checks_store(struct kmem_cache *s,
+                               const char *buf, size_t length)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
+       s->flags &= ~SLAB_DEBUG_FREE;
+       if (buf[0] == '1')
+               s->flags |= SLAB_DEBUG_FREE;
+       return length;
 }
-SLAB_ATTR_RO(cache_dma);
-#endif
+SLAB_ATTR(sanity_checks);
 
-static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
+static ssize_t trace_show(struct kmem_cache *s, char *buf)
 {
-       return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
 }
-SLAB_ATTR_RO(destroy_by_rcu);
+
+static ssize_t trace_store(struct kmem_cache *s, const char *buf,
+                                                       size_t length)
+{
+       s->flags &= ~SLAB_TRACE;
+       if (buf[0] == '1')
+               s->flags |= SLAB_TRACE;
+       return length;
+}
+SLAB_ATTR(trace);
 
 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
 {
@@ -4166,6 +4342,40 @@ static ssize_t validate_store(struct kmem_cache *s,
 }
 SLAB_ATTR(validate);
 
+static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
+{
+       if (!(s->flags & SLAB_STORE_USER))
+               return -ENOSYS;
+       return list_locations(s, buf, TRACK_ALLOC);
+}
+SLAB_ATTR_RO(alloc_calls);
+
+static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
+{
+       if (!(s->flags & SLAB_STORE_USER))
+               return -ENOSYS;
+       return list_locations(s, buf, TRACK_FREE);
+}
+SLAB_ATTR_RO(free_calls);
+#endif /* CONFIG_SLUB_DEBUG */
+
+#ifdef CONFIG_FAILSLAB
+static ssize_t failslab_show(struct kmem_cache *s, char *buf)
+{
+       return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
+}
+
+static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
+                                                       size_t length)
+{
+       s->flags &= ~SLAB_FAILSLAB;
+       if (buf[0] == '1')
+               s->flags |= SLAB_FAILSLAB;
+       return length;
+}
+SLAB_ATTR(failslab);
+#endif
+
 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
 {
        return 0;
@@ -4185,22 +4395,6 @@ static ssize_t shrink_store(struct kmem_cache *s,
 }
 SLAB_ATTR(shrink);
 
-static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
-{
-       if (!(s->flags & SLAB_STORE_USER))
-               return -ENOSYS;
-       return list_locations(s, buf, TRACK_ALLOC);
-}
-SLAB_ATTR_RO(alloc_calls);
-
-static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
-{
-       if (!(s->flags & SLAB_STORE_USER))
-               return -ENOSYS;
-       return list_locations(s, buf, TRACK_FREE);
-}
-SLAB_ATTR_RO(free_calls);
-
 #ifdef CONFIG_NUMA
 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
 {
@@ -4306,25 +4500,28 @@ static struct attribute *slab_attrs[] = {
        &min_partial_attr.attr,
        &objects_attr.attr,
        &objects_partial_attr.attr,
-       &total_objects_attr.attr,
-       &slabs_attr.attr,
        &partial_attr.attr,
        &cpu_slabs_attr.attr,
        &ctor_attr.attr,
        &aliases_attr.attr,
        &align_attr.attr,
-       &sanity_checks_attr.attr,
-       &trace_attr.attr,
        &hwcache_align_attr.attr,
        &reclaim_account_attr.attr,
        &destroy_by_rcu_attr.attr,
+       &shrink_attr.attr,
+       &reserved_attr.attr,
+#ifdef CONFIG_SLUB_DEBUG
+       &total_objects_attr.attr,
+       &slabs_attr.attr,
+       &sanity_checks_attr.attr,
+       &trace_attr.attr,
        &red_zone_attr.attr,
        &poison_attr.attr,
        &store_user_attr.attr,
        &validate_attr.attr,
-       &shrink_attr.attr,
        &alloc_calls_attr.attr,
        &free_calls_attr.attr,
+#endif
 #ifdef CONFIG_ZONE_DMA
        &cache_dma_attr.attr,
 #endif
@@ -4404,6 +4601,7 @@ static void kmem_cache_release(struct kobject *kobj)
 {
        struct kmem_cache *s = to_slab(kobj);
 
+       kfree(s->name);
        kfree(s);
 }
 
@@ -4519,6 +4717,13 @@ static int sysfs_slab_add(struct kmem_cache *s)
 
 static void sysfs_slab_remove(struct kmem_cache *s)
 {
+       if (slab_state < SYSFS)
+               /*
+                * Sysfs has not been setup yet so no need to remove the
+                * cache from sysfs.
+                */
+               return;
+
        kobject_uevent(&s->kobj, KOBJ_REMOVE);
        kobject_del(&s->kobj);
        kobject_put(&s->kobj);
@@ -4564,8 +4769,11 @@ static int __init slab_sysfs_init(void)
        struct kmem_cache *s;
        int err;
 
+       down_write(&slub_lock);
+
        slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
        if (!slab_kset) {
+               up_write(&slub_lock);
                printk(KERN_ERR "Cannot register slab subsystem.\n");
                return -ENOSYS;
        }
@@ -4590,12 +4798,13 @@ static int __init slab_sysfs_init(void)
                kfree(al);
        }
 
+       up_write(&slub_lock);
        resiliency_test();
        return 0;
 }
 
 __initcall(slab_sysfs_init);
-#endif
+#endif /* CONFIG_SYSFS */
 
 /*
  * The /proc/slabinfo ABI