]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - mm/slub.c
Revert "unique end pointer" patch
[linux-2.6-omap-h63xx.git] / mm / slub.c
index ccfd41141b6bd54b28cc412bfc7aa4583f869bd4..a873953e5a11f7bb7f923bed60ff214f813df9fa 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -149,13 +149,6 @@ static inline void ClearSlabDebug(struct page *page)
 /* 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
 
 /*
@@ -211,6 +204,8 @@ static inline void ClearSlabDebug(struct page *page)
 /* Internal SLUB flags */
 #define __OBJECT_POISON                0x80000000 /* Poison object */
 #define __SYSFS_ADD_DEFERRED   0x40000000 /* Not yet visible via sysfs */
+#define __KMALLOC_CACHE                0x20000000 /* objects freed using kfree */
+#define __PAGE_ALLOC_FALLBACK  0x10000000 /* Allow fallback to page alloc */
 
 /* Not all arches define cache_line_size */
 #ifndef cache_line_size
@@ -296,32 +291,15 @@ static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
 #endif
 }
 
-/*
- * The end pointer in a slab is special. It points to the first object in the
- * slab but has bit 0 set to mark it.
- *
- * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
- * in the mapping set.
- */
-static inline int is_end(void *addr)
-{
-       return (unsigned long)addr & PAGE_MAPPING_ANON;
-}
-
-static void *slab_address(struct page *page)
-{
-       return page->end - PAGE_MAPPING_ANON;
-}
-
 static inline int check_valid_pointer(struct kmem_cache *s,
                                struct page *page, const void *object)
 {
        void *base;
 
-       if (object == page->end)
+       if (!object)
                return 1;
 
-       base = slab_address(page);
+       base = page_address(page);
        if (object < base || object >= base + s->objects * s->size ||
                (object - base) % s->size) {
                return 0;
@@ -354,8 +332,7 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
 
 /* Scan freelist */
 #define for_each_free_object(__p, __s, __free) \
-       for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
-               __p))
+       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)
@@ -507,7 +484,7 @@ static void slab_fix(struct kmem_cache *s, char *fmt, ...)
 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
 {
        unsigned int off;       /* Offset of last byte */
-       u8 *addr = slab_address(page);
+       u8 *addr = page_address(page);
 
        print_tracking(s, p);
 
@@ -685,7 +662,7 @@ static int slab_pad_check(struct kmem_cache *s, struct page *page)
        if (!(s->flags & SLAB_POISON))
                return 1;
 
-       start = slab_address(page);
+       start = page_address(page);
        end = start + (PAGE_SIZE << s->order);
        length = s->objects * s->size;
        remainder = end - (start + length);
@@ -753,7 +730,7 @@ static int check_object(struct kmem_cache *s, struct page *page,
                 * of the free objects in this slab. May cause
                 * another error because the object count is now wrong.
                 */
-               set_freepointer(s, p, page->end);
+               set_freepointer(s, p, NULL);
                return 0;
        }
        return 1;
@@ -787,18 +764,18 @@ static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
        void *fp = page->freelist;
        void *object = NULL;
 
-       while (fp != page->end && nr <= s->objects) {
+       while (fp && nr <= s->objects) {
                if (fp == search)
                        return 1;
                if (!check_valid_pointer(s, page, fp)) {
                        if (object) {
                                object_err(s, page, object,
                                        "Freechain corrupt");
-                               set_freepointer(s, object, page->end);
+                               set_freepointer(s, object, NULL);
                                break;
                        } else {
                                slab_err(s, page, "Freepointer corrupt");
-                               page->freelist = page->end;
+                               page->freelist = NULL;
                                page->inuse = s->objects;
                                slab_fix(s, "Freelist cleared");
                                return 0;
@@ -904,7 +881,7 @@ bad:
                 */
                slab_fix(s, "Marking all objects used");
                page->inuse = s->objects;
-               page->freelist = page->end;
+               page->freelist = NULL;
        }
        return 0;
 }
@@ -944,7 +921,7 @@ static int free_debug_processing(struct kmem_cache *s, struct page *page,
        }
 
        /* Special debug activities for freeing objects */
-       if (!SlabFrozen(page) && page->freelist == page->end)
+       if (!SlabFrozen(page) && !page->freelist)
                remove_full(s, page);
        if (s->flags & SLAB_STORE_USER)
                set_track(s, object, TRACK_FREE, addr);
@@ -1129,7 +1106,6 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
                SetSlabDebug(page);
 
        start = page_address(page);
-       page->end = start + 1;
 
        if (unlikely(s->flags & SLAB_POISON))
                memset(start, POISON_INUSE, PAGE_SIZE << s->order);
@@ -1141,7 +1117,7 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
                last = p;
        }
        setup_object(s, page, last);
-       set_freepointer(s, last, page->end);
+       set_freepointer(s, last, NULL);
 
        page->freelist = start;
        page->inuse = 0;
@@ -1157,7 +1133,7 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
                void *p;
 
                slab_pad_check(s, page);
-               for_each_object(p, s, slab_address(page))
+               for_each_object(p, s, page_address(page))
                        check_object(s, page, p, 0);
                ClearSlabDebug(page);
        }
@@ -1167,7 +1143,6 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
                NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
                -pages);
 
-       page->mapping = NULL;
        __free_pages(page, s->order);
 }
 
@@ -1371,7 +1346,7 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
        ClearSlabFrozen(page);
        if (page->inuse) {
 
-               if (page->freelist != page->end) {
+               if (page->freelist) {
                        add_partial(n, page, tail);
                        stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
                } else {
@@ -1415,12 +1390,8 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
         * Merge cpu freelist into freelist. Typically we get here
         * because both freelists are empty. So this is unlikely
         * to occur.
-        *
-        * We need to use _is_end here because deactivate slab may
-        * be called for a debug slab. Then c->freelist may contain
-        * a dummy pointer.
         */
-       while (unlikely(!is_end(c->freelist))) {
+       while (unlikely(c->freelist)) {
                void **object;
 
                tail = 0;       /* Hot objects. Put the slab first */
@@ -1512,11 +1483,7 @@ static void *__slab_alloc(struct kmem_cache *s,
 {
        void **object;
        struct page *new;
-#ifdef SLUB_FASTPATH
-       unsigned long flags;
 
-       local_irq_save(flags);
-#endif
        if (!c->page)
                goto new_slab;
 
@@ -1526,7 +1493,7 @@ static void *__slab_alloc(struct kmem_cache *s,
        stat(c, ALLOC_REFILL);
 load_freelist:
        object = c->page->freelist;
-       if (unlikely(object == c->page->end))
+       if (unlikely(!object))
                goto another_slab;
        if (unlikely(SlabDebug(c->page)))
                goto debug;
@@ -1534,15 +1501,11 @@ load_freelist:
        object = c->page->freelist;
        c->freelist = object[c->offset];
        c->page->inuse = s->objects;
-       c->page->freelist = c->page->end;
+       c->page->freelist = NULL;
        c->node = page_to_nid(c->page);
 unlock_out:
        slab_unlock(c->page);
        stat(c, ALLOC_SLOWPATH);
-out:
-#ifdef SLUB_FASTPATH
-       local_irq_restore(flags);
-#endif
        return object;
 
 another_slab:
@@ -1574,8 +1537,22 @@ new_slab:
                c->page = new;
                goto load_freelist;
        }
-       object = NULL;
-       goto out;
+
+       /*
+        * No memory available.
+        *
+        * If the slab uses higher order allocs but the object is
+        * smaller than a page size then we can fallback in emergencies
+        * to the page allocator via kmalloc_large. The page allocator may
+        * have failed to obtain a higher order page and we can try to
+        * allocate a single page if the object fits into a single page.
+        * That is only possible if certain conditions are met that are being
+        * checked when a slab is created.
+        */
+       if (!(gfpflags & __GFP_NORETRY) && (s->flags & __PAGE_ALLOC_FALLBACK))
+               return kmalloc_large(s->objsize, gfpflags);
+
+       return NULL;
 debug:
        object = c->page->freelist;
        if (!alloc_debug_processing(s, c->page, object, addr))
@@ -1602,39 +1579,11 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
 {
        void **object;
        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;
-               }
-               stat(c, ALLOC_FASTPATH);
-       } 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)))
+       if (unlikely(!c->freelist || !node_match(c, node)))
 
                object = __slab_alloc(s, gfpflags, node, addr, c);
 
@@ -1644,7 +1593,6 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
                stat(c, ALLOC_FASTPATH);
        }
        local_irq_restore(flags);
-#endif
 
        if (unlikely((gfpflags & __GFP_ZERO) && object))
                memset(object, 0, c->objsize);
@@ -1681,11 +1629,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
        void **object = (void *)x;
        struct kmem_cache_cpu *c;
 
-#ifdef SLUB_FASTPATH
-       unsigned long flags;
-
-       local_irq_save(flags);
-#endif
        c = get_cpu_slab(s, raw_smp_processor_id());
        stat(c, FREE_SLOWPATH);
        slab_lock(page);
@@ -1710,20 +1653,17 @@ checks_ok:
         * was not on the partial list before
         * then add it.
         */
-       if (unlikely(prior == page->end)) {
+       if (unlikely(!prior)) {
                add_partial(get_node(s, page_to_nid(page)), page, 1);
                stat(c, FREE_ADD_PARTIAL);
        }
 
 out_unlock:
        slab_unlock(page);
-#ifdef SLUB_FASTPATH
-       local_irq_restore(flags);
-#endif
        return;
 
 slab_empty:
-       if (prior != page->end) {
+       if (prior) {
                /*
                 * Slab still on the partial list.
                 */
@@ -1732,9 +1672,6 @@ slab_empty:
        }
        slab_unlock(page);
        stat(c, FREE_SLAB);
-#ifdef SLUB_FASTPATH
-       local_irq_restore(flags);
-#endif
        discard_slab(s, page);
        return;
 
@@ -1760,34 +1697,6 @@ static __always_inline void slab_free(struct kmem_cache *s,
 {
        void **object = (void *)x;
        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;
-               stat(c, FREE_FASTPATH);
-       } while (cmpxchg_local(&c->freelist, freelist, object) != freelist);
-#else
        unsigned long flags;
 
        local_irq_save(flags);
@@ -1801,7 +1710,6 @@ static __always_inline void slab_free(struct kmem_cache *s,
                __slab_free(s, page, x, addr, c->offset);
 
        local_irq_restore(flags);
-#endif
 }
 
 void kmem_cache_free(struct kmem_cache *s, void *x)
@@ -1978,7 +1886,7 @@ static void init_kmem_cache_cpu(struct kmem_cache *s,
                        struct kmem_cache_cpu *c)
 {
        c->page = NULL;
-       c->freelist = (void *)PAGE_MAPPING_ANON;
+       c->freelist = NULL;
        c->node = 0;
        c->offset = s->offset / sizeof(void *);
        c->objsize = s->objsize;
@@ -2322,7 +2230,20 @@ static int calculate_sizes(struct kmem_cache *s)
        size = ALIGN(size, align);
        s->size = size;
 
-       s->order = calculate_order(size);
+       if ((flags & __KMALLOC_CACHE) &&
+                       PAGE_SIZE / size < slub_min_objects) {
+               /*
+                * Kmalloc cache that would not have enough objects in
+                * an order 0 page. Kmalloc slabs can fallback to
+                * page allocator order 0 allocs so take a reasonably large
+                * order that will allows us a good number of objects.
+                */
+               s->order = max(slub_max_order, PAGE_ALLOC_COSTLY_ORDER);
+               s->flags |= __PAGE_ALLOC_FALLBACK;
+               s->allocflags |= __GFP_NOWARN;
+       } else
+               s->order = calculate_order(size);
+
        if (s->order < 0)
                return 0;
 
@@ -2487,11 +2408,11 @@ EXPORT_SYMBOL(kmem_cache_destroy);
  *             Kmalloc subsystem
  *******************************************************************/
 
-struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
+struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
 EXPORT_SYMBOL(kmalloc_caches);
 
 #ifdef CONFIG_ZONE_DMA
-static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
+static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
 #endif
 
 static int __init setup_slub_min_order(char *str)
@@ -2539,7 +2460,7 @@ static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
 
        down_write(&slub_lock);
        if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
-                       flags, NULL))
+                       flags | __KMALLOC_CACHE, NULL))
                goto panic;
 
        list_add(&s->list, &slab_caches);
@@ -2673,7 +2594,7 @@ void *__kmalloc(size_t size, gfp_t flags)
 {
        struct kmem_cache *s;
 
-       if (unlikely(size > PAGE_SIZE / 2))
+       if (unlikely(size > PAGE_SIZE))
                return kmalloc_large(size, flags);
 
        s = get_slab(size, flags);
@@ -2690,7 +2611,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
 {
        struct kmem_cache *s;
 
-       if (unlikely(size > PAGE_SIZE / 2))
+       if (unlikely(size > PAGE_SIZE))
                return kmalloc_large(size, flags);
 
        s = get_slab(size, flags);
@@ -3002,7 +2923,7 @@ void __init kmem_cache_init(void)
                caches++;
        }
 
-       for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
+       for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
                create_kmalloc_cache(&kmalloc_caches[i],
                        "kmalloc", 1 << i, GFP_KERNEL);
                caches++;
@@ -3029,7 +2950,7 @@ void __init kmem_cache_init(void)
        slab_state = UP;
 
        /* Provide the correct kmalloc names now that the caches are up */
-       for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
+       for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
                kmalloc_caches[i]. name =
                        kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
 
@@ -3058,6 +2979,9 @@ static int slab_unmergeable(struct kmem_cache *s)
        if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
                return 1;
 
+       if ((s->flags & __PAGE_ALLOC_FALLBACK))
+               return 1;
+
        if (s->ctor)
                return 1;
 
@@ -3219,7 +3143,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
 {
        struct kmem_cache *s;
 
-       if (unlikely(size > PAGE_SIZE / 2))
+       if (unlikely(size > PAGE_SIZE))
                return kmalloc_large(size, gfpflags);
 
        s = get_slab(size, gfpflags);
@@ -3235,7 +3159,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
 {
        struct kmem_cache *s;
 
-       if (unlikely(size > PAGE_SIZE / 2))
+       if (unlikely(size > PAGE_SIZE))
                return kmalloc_large(size, gfpflags);
 
        s = get_slab(size, gfpflags);
@@ -3251,7 +3175,7 @@ static int validate_slab(struct kmem_cache *s, struct page *page,
                                                unsigned long *map)
 {
        void *p;
-       void *addr = slab_address(page);
+       void *addr = page_address(page);
 
        if (!check_slab(s, page) ||
                        !on_freelist(s, page, NULL))
@@ -3534,7 +3458,7 @@ 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)
 {
-       void *addr = slab_address(page);
+       void *addr = page_address(page);
        DECLARE_BITMAP(map, s->objects);
        void *p;