/* Enable to test recovery from slab corruption on boot */
 #undef SLUB_RESILIENCY_TEST
 
+/*
+ * Currently fastpath is not supported if preemption is enabled.
+ */
+#if defined(CONFIG_FAST_CMPXCHG_LOCAL) && !defined(CONFIG_PREEMPT)
+#define SLUB_FASTPATH
+#endif
+
 #if PAGE_SHIFT <= 12
 
 /*
 {
        void **object;
        struct page *new;
+#ifdef SLUB_FASTPATH
+       unsigned long flags;
 
+       local_irq_save(flags);
+#endif
        if (!c->page)
                goto new_slab;
 
        c->page->inuse = s->objects;
        c->page->freelist = c->page->end;
        c->node = page_to_nid(c->page);
+unlock_out:
        slab_unlock(c->page);
+out:
+#ifdef SLUB_FASTPATH
+       local_irq_restore(flags);
+#endif
        return object;
 
 another_slab:
                c->page = new;
                goto load_freelist;
        }
-       return NULL;
+       object = NULL;
+       goto out;
 debug:
        object = c->page->freelist;
        if (!alloc_debug_processing(s, c->page, object, addr))
        c->page->inuse++;
        c->page->freelist = object[c->offset];
        c->node = -1;
-       slab_unlock(c->page);
-       return object;
+       goto unlock_out;
 }
 
 /*
                gfp_t gfpflags, int node, void *addr)
 {
        void **object;
-       unsigned long flags;
        struct kmem_cache_cpu *c;
 
+/*
+ * The SLUB_FASTPATH path is provisional and is currently disabled if the
+ * kernel is compiled with preemption or if the arch does not support
+ * fast cmpxchg operations. There are a couple of coming changes that will
+ * simplify matters and allow preemption. Ultimately we may end up making
+ * SLUB_FASTPATH the default.
+ *
+ * 1. The introduction of the per cpu allocator will avoid array lookups
+ *    through get_cpu_slab(). A special register can be used instead.
+ *
+ * 2. The introduction of per cpu atomic operations (cpu_ops) means that
+ *    we can realize the logic here entirely with per cpu atomics. The
+ *    per cpu atomic ops will take care of the preemption issues.
+ */
+
+#ifdef SLUB_FASTPATH
+       c = get_cpu_slab(s, raw_smp_processor_id());
+       do {
+               object = c->freelist;
+               if (unlikely(is_end(object) || !node_match(c, node))) {
+                       object = __slab_alloc(s, gfpflags, node, addr, c);
+                       break;
+               }
+       } while (cmpxchg_local(&c->freelist, object, object[c->offset])
+                                                               != object);
+#else
+       unsigned long flags;
+
        local_irq_save(flags);
        c = get_cpu_slab(s, smp_processor_id());
        if (unlikely(is_end(c->freelist) || !node_match(c, node)))
                c->freelist = object[c->offset];
        }
        local_irq_restore(flags);
+#endif
 
        if (unlikely((gfpflags & __GFP_ZERO) && object))
                memset(object, 0, c->objsize);
        void *prior;
        void **object = (void *)x;
 
+#ifdef SLUB_FASTPATH
+       unsigned long flags;
+
+       local_irq_save(flags);
+#endif
        slab_lock(page);
 
        if (unlikely(SlabDebug(page)))
 
 out_unlock:
        slab_unlock(page);
+#ifdef SLUB_FASTPATH
+       local_irq_restore(flags);
+#endif
        return;
 
 slab_empty:
                remove_partial(s, page);
 
        slab_unlock(page);
+#ifdef SLUB_FASTPATH
+       local_irq_restore(flags);
+#endif
        discard_slab(s, page);
        return;
 
                        struct page *page, void *x, void *addr)
 {
        void **object = (void *)x;
-       unsigned long flags;
        struct kmem_cache_cpu *c;
 
+#ifdef SLUB_FASTPATH
+       void **freelist;
+
+       c = get_cpu_slab(s, raw_smp_processor_id());
+       debug_check_no_locks_freed(object, s->objsize);
+       do {
+               freelist = c->freelist;
+               barrier();
+               /*
+                * If the compiler would reorder the retrieval of c->page to
+                * come before c->freelist then an interrupt could
+                * change the cpu slab before we retrieve c->freelist. We
+                * could be matching on a page no longer active and put the
+                * object onto the freelist of the wrong slab.
+                *
+                * On the other hand: If we already have the freelist pointer
+                * then any change of cpu_slab will cause the cmpxchg to fail
+                * since the freelist pointers are unique per slab.
+                */
+               if (unlikely(page != c->page || c->node < 0)) {
+                       __slab_free(s, page, x, addr, c->offset);
+                       break;
+               }
+               object[c->offset] = freelist;
+       } while (cmpxchg_local(&c->freelist, freelist, object) != freelist);
+#else
+       unsigned long flags;
+
        local_irq_save(flags);
        debug_check_no_locks_freed(object, s->objsize);
        c = get_cpu_slab(s, smp_processor_id());
                __slab_free(s, page, x, addr, c->offset);
 
        local_irq_restore(flags);
+#endif
 }
 
 void kmem_cache_free(struct kmem_cache *s, void *x)