memcg: change page_cgroup_zoneinfo signature
[sfrench/cifs-2.6.git] / mm / slub.c
index ebba3eb193692cac5cdd3af0e472fa3c1800f80c..93de30db95f5943037b1a37a9fae1e2578f06287 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -217,7 +217,7 @@ static inline void sysfs_slab_remove(struct kmem_cache *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]);
@@ -281,6 +281,30 @@ 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;
@@ -805,21 +829,31 @@ static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t 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, s->objsize);
+       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);
-}
 
-static inline void slab_free_hook_irq(struct kmem_cache *s, void *object)
-{
-       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);
+       /*
+        * 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);
+               if (!(s->flags & SLAB_DEBUG_OBJECTS))
+                       debug_check_no_obj_freed(x, s->objsize);
+               local_irq_restore(flags);
+       }
+#endif
 }
 
 /*
@@ -1106,9 +1140,6 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
 
 static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
 
-static inline void slab_free_hook_irq(struct kmem_cache *s,
-               void *object) {}
-
 #endif /* CONFIG_SLUB_DEBUG */
 
 /*
@@ -1509,6 +1540,78 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
        }
 }
 
+#ifdef CONFIG_CMPXCHG_LOCAL
+#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);
+}
+
+#endif
+
+void init_kmem_cache_cpus(struct kmem_cache *s)
+{
+#if defined(CONFIG_CMPXCHG_LOCAL) && defined(CONFIG_PREEMPT)
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
+#endif
+
+}
 /*
  * Remove the cpu slab
  */
@@ -1540,6 +1643,9 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
                page->inuse--;
        }
        c->page = NULL;
+#ifdef CONFIG_CMPXCHG_LOCAL
+       c->tid = next_tid(c->tid);
+#endif
        unfreeze_slab(s, page, tail);
 }
 
@@ -1674,6 +1780,19 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 {
        void **object;
        struct page *new;
+#ifdef CONFIG_CMPXCHG_LOCAL
+       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
+#endif
 
        /* We handle __GFP_ZERO in the caller */
        gfpflags &= ~__GFP_ZERO;
@@ -1700,6 +1819,10 @@ load_freelist:
        c->node = page_to_nid(c->page);
 unlock_out:
        slab_unlock(c->page);
+#ifdef CONFIG_CMPXCHG_LOCAL
+       c->tid = next_tid(c->tid);
+       local_irq_restore(flags);
+#endif
        stat(s, ALLOC_SLOWPATH);
        return object;
 
@@ -1735,6 +1858,9 @@ new_slab:
        }
        if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
                slab_out_of_memory(s, gfpflags, node);
+#ifdef CONFIG_CMPXCHG_LOCAL
+       local_irq_restore(flags);
+#endif
        return NULL;
 debug:
        if (!alloc_debug_processing(s, c->page, object, addr))
@@ -1761,23 +1887,76 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
 {
        void **object;
        struct kmem_cache_cpu *c;
+#ifdef CONFIG_CMPXCHG_LOCAL
+       unsigned long tid;
+#else
        unsigned long flags;
+#endif
 
        if (slab_pre_alloc_hook(s, gfpflags))
                return NULL;
 
+#ifndef CONFIG_CMPXCHG_LOCAL
        local_irq_save(flags);
+#else
+redo:
+#endif
+
+       /*
+        * 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);
+
+#ifdef CONFIG_CMPXCHG_LOCAL
+       /*
+        * 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();
+#endif
+
        object = c->freelist;
        if (unlikely(!object || !node_match(c, node)))
 
                object = __slab_alloc(s, gfpflags, node, addr, c);
 
        else {
+#ifdef CONFIG_CMPXCHG_LOCAL
+               /*
+                * The cmpxchg will only match if there was no additonal
+                * 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(!this_cpu_cmpxchg_double(
+                               s->cpu_slab->freelist, s->cpu_slab->tid,
+                               object, tid,
+                               get_freepointer(s, object), next_tid(tid)))) {
+
+                       note_cmpxchg_failure("slab_alloc", s, tid);
+                       goto redo;
+               }
+#else
                c->freelist = get_freepointer(s, object);
+#endif
                stat(s, ALLOC_FASTPATH);
        }
+
+#ifndef CONFIG_CMPXCHG_LOCAL
        local_irq_restore(flags);
+#endif
 
        if (unlikely(gfpflags & __GFP_ZERO) && object)
                memset(object, 0, s->objsize);
@@ -1855,9 +2034,13 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 {
        void *prior;
        void **object = (void *)x;
+#ifdef CONFIG_CMPXCHG_LOCAL
+       unsigned long flags;
 
-       stat(s, FREE_SLOWPATH);
+       local_irq_save(flags);
+#endif
        slab_lock(page);
+       stat(s, FREE_SLOWPATH);
 
        if (kmem_cache_debug(s))
                goto debug;
@@ -1887,6 +2070,9 @@ checks_ok:
 
 out_unlock:
        slab_unlock(page);
+#ifdef CONFIG_CMPXCHG_LOCAL
+       local_irq_restore(flags);
+#endif
        return;
 
 slab_empty:
@@ -1898,6 +2084,9 @@ slab_empty:
                stat(s, FREE_REMOVE_PARTIAL);
        }
        slab_unlock(page);
+#ifdef CONFIG_CMPXCHG_LOCAL
+       local_irq_restore(flags);
+#endif
        stat(s, FREE_SLAB);
        discard_slab(s, page);
        return;
@@ -1924,23 +2113,56 @@ static __always_inline void slab_free(struct kmem_cache *s,
 {
        void **object = (void *)x;
        struct kmem_cache_cpu *c;
+#ifdef CONFIG_CMPXCHG_LOCAL
+       unsigned long tid;
+#else
        unsigned long flags;
+#endif
 
        slab_free_hook(s, x);
 
+#ifndef CONFIG_CMPXCHG_LOCAL
        local_irq_save(flags);
+
+#else
+redo:
+#endif
+
+       /*
+        * 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);
 
-       slab_free_hook_irq(s, x);
+#ifdef CONFIG_CMPXCHG_LOCAL
+       tid = c->tid;
+       barrier();
+#endif
 
        if (likely(page == c->page && c->node != NUMA_NO_NODE)) {
                set_freepointer(s, object, c->freelist);
+
+#ifdef CONFIG_CMPXCHG_LOCAL
+               if (unlikely(!this_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;
+               }
+#else
                c->freelist = object;
+#endif
                stat(s, FREE_FASTPATH);
        } else
                __slab_free(s, page, x, addr);
 
+#ifndef CONFIG_CMPXCHG_LOCAL
        local_irq_restore(flags);
+#endif
 }
 
 void kmem_cache_free(struct kmem_cache *s, void *x)
@@ -2132,9 +2354,23 @@ static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
        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;
 
-       return s->cpu_slab != NULL;
+       init_kmem_cache_cpus(s);
+
+       return 1;
 }
 
 static struct kmem_cache *kmem_cache_node;
@@ -2425,12 +2661,6 @@ 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)
 {
@@ -2722,7 +2952,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;
@@ -2733,28 +2962,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);