]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - mm/slub.c
Slab allocators: consolidate code for krealloc in mm/util.c
[linux-2.6.git] / mm / slub.c
index 3ca164f339651416515aa6fec9b3f88bd9b98137..1b0a95d75dbb8f57d2187875212a11ca13e94fb9 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -205,6 +205,11 @@ static inline void ClearSlabDebug(struct page *page)
 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
 #endif
 
+/*
+ * The page->inuse field is 16 bit thus we have this limitation
+ */
+#define MAX_OBJECTS_PER_SLAB 65535
+
 /* Internal SLUB flags */
 #define __OBJECT_POISON 0x80000000     /* Poison object */
 
@@ -323,7 +328,11 @@ static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
 /*
  * Debug settings:
  */
+#ifdef CONFIG_SLUB_DEBUG_ON
+static int slub_debug = DEBUG_DEFAULT_FLAGS;
+#else
 static int slub_debug;
+#endif
 
 static char *slub_debug_slabs;
 
@@ -340,7 +349,7 @@ static void print_section(char *text, u8 *addr, unsigned int length)
 
        for (i = 0; i < length; i++) {
                if (newline) {
-                       printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
+                       printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
                        newline = 0;
                }
                printk(" %02x", addr[i]);
@@ -397,10 +406,11 @@ static void set_track(struct kmem_cache *s, void *object,
 
 static void init_tracking(struct kmem_cache *s, void *object)
 {
-       if (s->flags & SLAB_STORE_USER) {
-               set_track(s, object, TRACK_FREE, NULL);
-               set_track(s, object, TRACK_ALLOC, NULL);
-       }
+       if (!(s->flags & SLAB_STORE_USER))
+               return;
+
+       set_track(s, object, TRACK_FREE, NULL);
+       set_track(s, object, TRACK_ALLOC, NULL);
 }
 
 static void print_track(const char *s, struct track *t)
@@ -408,65 +418,106 @@ static void print_track(const char *s, struct track *t)
        if (!t->addr)
                return;
 
-       printk(KERN_ERR "%s: ", s);
+       printk(KERN_ERR "INFO: %s in ", s);
        __print_symbol("%s", (unsigned long)t->addr);
-       printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
+       printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
+}
+
+static void print_tracking(struct kmem_cache *s, void *object)
+{
+       if (!(s->flags & SLAB_STORE_USER))
+               return;
+
+       print_track("Allocated", get_track(s, object, TRACK_ALLOC));
+       print_track("Freed", get_track(s, object, TRACK_FREE));
 }
 
-static void print_trailer(struct kmem_cache *s, u8 *p)
+static void print_page_info(struct page *page)
+{
+       printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
+               page, page->inuse, page->freelist, page->flags);
+
+}
+
+static void slab_bug(struct kmem_cache *s, char *fmt, ...)
+{
+       va_list args;
+       char buf[100];
+
+       va_start(args, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, args);
+       va_end(args);
+       printk(KERN_ERR "========================================"
+                       "=====================================\n");
+       printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
+       printk(KERN_ERR "----------------------------------------"
+                       "-------------------------------------\n\n");
+}
+
+static void slab_fix(struct kmem_cache *s, char *fmt, ...)
+{
+       va_list args;
+       char buf[100];
+
+       va_start(args, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, args);
+       va_end(args);
+       printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
+}
+
+static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
 {
        unsigned int off;       /* Offset of last byte */
+       u8 *addr = page_address(page);
+
+       print_tracking(s, p);
+
+       print_page_info(page);
+
+       printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
+                       p, p - addr, get_freepointer(s, p));
+
+       if (p > addr + 16)
+               print_section("Bytes b4", p - 16, 16);
+
+       print_section("Object", p, min(s->objsize, 128));
 
        if (s->flags & SLAB_RED_ZONE)
                print_section("Redzone", p + s->objsize,
                        s->inuse - s->objsize);
 
-       printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
-                       p + s->offset,
-                       get_freepointer(s, p));
-
        if (s->offset)
                off = s->offset + sizeof(void *);
        else
                off = s->inuse;
 
-       if (s->flags & SLAB_STORE_USER) {
-               print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
-               print_track("Last free ", get_track(s, p, TRACK_FREE));
+       if (s->flags & SLAB_STORE_USER)
                off += 2 * sizeof(struct track);
-       }
 
        if (off != s->size)
                /* Beginning of the filler is the free pointer */
-               print_section("Filler", p + off, s->size - off);
+               print_section("Padding", p + off, s->size - off);
+
+       dump_stack();
 }
 
 static void object_err(struct kmem_cache *s, struct page *page,
                        u8 *object, char *reason)
 {
-       u8 *addr = page_address(page);
-
-       printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
-                       s->name, reason, object, page);
-       printk(KERN_ERR "    offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
-               object - addr, page->flags, page->inuse, page->freelist);
-       if (object > addr + 16)
-               print_section("Bytes b4", object - 16, 16);
-       print_section("Object", object, min(s->objsize, 128));
-       print_trailer(s, object);
-       dump_stack();
+       slab_bug(s, reason);
+       print_trailer(s, page, object);
 }
 
-static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
+static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
 {
        va_list args;
        char buf[100];
 
-       va_start(args, reason);
-       vsnprintf(buf, sizeof(buf), reason, args);
+       va_start(args, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, args);
        va_end(args);
-       printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
-               page);
+       slab_bug(s, fmt);
+       print_page_info(page);
        dump_stack();
 }
 
@@ -485,15 +536,46 @@ static void init_object(struct kmem_cache *s, void *object, int active)
                        s->inuse - s->objsize);
 }
 
-static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
+static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
 {
        while (bytes) {
                if (*start != (u8)value)
-                       return 0;
+                       return start;
                start++;
                bytes--;
        }
-       return 1;
+       return NULL;
+}
+
+static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
+                                               void *from, void *to)
+{
+       slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
+       memset(from, data, to - from);
+}
+
+static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
+                       u8 *object, char *what,
+                       u8* start, unsigned int value, unsigned int bytes)
+{
+       u8 *fault;
+       u8 *end;
+
+       fault = check_bytes(start, value, bytes);
+       if (!fault)
+               return 1;
+
+       end = start + bytes;
+       while (end > fault && end[-1] == value)
+               end--;
+
+       slab_bug(s, "%s overwritten", what);
+       printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
+                                       fault, end - 1, fault[0], value);
+       print_trailer(s, page, object);
+
+       restore_bytes(s, what, value, fault, end);
+       return 0;
 }
 
 /*
@@ -534,14 +616,6 @@ static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  * may be used with merged slabcaches.
  */
 
-static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
-                                               void *from, void *to)
-{
-       printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
-               s->name, message, data, from, to - 1);
-       memset(from, data, to - from);
-}
-
 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
 {
        unsigned long off = s->inuse;   /* The end of info */
@@ -557,39 +631,39 @@ static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
        if (s->size == off)
                return 1;
 
-       if (check_bytes(p + off, POISON_INUSE, s->size - off))
-               return 1;
-
-       object_err(s, page, p, "Object padding check fails");
-
-       /*
-        * Restore padding
-        */
-       restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
-       return 0;
+       return check_bytes_and_report(s, page, p, "Object padding",
+                               p + off, POISON_INUSE, s->size - off);
 }
 
 static int slab_pad_check(struct kmem_cache *s, struct page *page)
 {
-       u8 *p;
-       int length, remainder;
+       u8 *start;
+       u8 *fault;
+       u8 *end;
+       int length;
+       int remainder;
 
        if (!(s->flags & SLAB_POISON))
                return 1;
 
-       p = page_address(page);
+       start = page_address(page);
+       end = start + (PAGE_SIZE << s->order);
        length = s->objects * s->size;
-       remainder = (PAGE_SIZE << s->order) - length;
+       remainder = end - (start + length);
        if (!remainder)
                return 1;
 
-       if (!check_bytes(p + length, POISON_INUSE, remainder)) {
-               slab_err(s, page, "Padding check failed");
-               restore_bytes(s, "slab padding", POISON_INUSE, p + length,
-                       p + length + remainder);
-               return 0;
-       }
-       return 1;
+       fault = check_bytes(start + length, POISON_INUSE, remainder);
+       if (!fault)
+               return 1;
+       while (end > fault && end[-1] == POISON_INUSE)
+               end--;
+
+       slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
+       print_section("Padding", start, length);
+
+       restore_bytes(s, "slab padding", POISON_INUSE, start, end);
+       return 0;
 }
 
 static int check_object(struct kmem_cache *s, struct page *page,
@@ -602,41 +676,22 @@ static int check_object(struct kmem_cache *s, struct page *page,
                unsigned int red =
                        active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
 
-               if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
-                       object_err(s, page, object,
-                       active ? "Redzone Active" : "Redzone Inactive");
-                       restore_bytes(s, "redzone", red,
-                               endobject, object + s->inuse);
+               if (!check_bytes_and_report(s, page, object, "Redzone",
+                       endobject, red, s->inuse - s->objsize))
                        return 0;
-               }
        } else {
-               if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
-                       !check_bytes(endobject, POISON_INUSE,
-                                       s->inuse - s->objsize)) {
-               object_err(s, page, p, "Alignment padding check fails");
-               /*
-                * Fix it so that there will not be another report.
-                *
-                * Hmmm... We may be corrupting an object that now expects
-                * to be longer than allowed.
-                */
-               restore_bytes(s, "alignment padding", POISON_INUSE,
-                       endobject, object + s->inuse);
-               }
+               if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
+                       check_bytes_and_report(s, page, p, "Alignment padding", endobject,
+                               POISON_INUSE, s->inuse - s->objsize);
        }
 
        if (s->flags & SLAB_POISON) {
                if (!active && (s->flags & __OBJECT_POISON) &&
-                       (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
-                               p[s->objsize - 1] != POISON_END)) {
-
-                       object_err(s, page, p, "Poison check failed");
-                       restore_bytes(s, "Poison", POISON_FREE,
-                                               p, p + s->objsize -1);
-                       restore_bytes(s, "Poison", POISON_END,
-                                       p + s->objsize - 1, p + s->objsize);
+                       (!check_bytes_and_report(s, page, p, "Poison", p,
+                                       POISON_FREE, s->objsize - 1) ||
+                        !check_bytes_and_report(s, page, p, "Poison",
+                               p + s->objsize -1, POISON_END, 1)))
                        return 0;
-               }
                /*
                 * check_pad_bytes cleans up on its own.
                 */
@@ -669,25 +724,17 @@ static int check_slab(struct kmem_cache *s, struct page *page)
        VM_BUG_ON(!irqs_disabled());
 
        if (!PageSlab(page)) {
-               slab_err(s, page, "Not a valid slab page flags=%lx "
-                       "mapping=0x%p count=%d", page->flags, page->mapping,
-                       page_count(page));
+               slab_err(s, page, "Not a valid slab page");
                return 0;
        }
        if (page->offset * sizeof(void *) != s->offset) {
-               slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
-                       "mapping=0x%p count=%d",
-                       (unsigned long)(page->offset * sizeof(void *)),
-                       page->flags,
-                       page->mapping,
-                       page_count(page));
+               slab_err(s, page, "Corrupted offset %lu",
+                       (unsigned long)(page->offset * sizeof(void *)));
                return 0;
        }
        if (page->inuse > s->objects) {
-               slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
-                       "mapping=0x%p count=%d",
-                       s->name, page->inuse, s->objects, page->flags,
-                       page->mapping, page_count(page));
+               slab_err(s, page, "inuse %u > max %u",
+                       s->name, page->inuse, s->objects);
                return 0;
        }
        /* Slab_pad_check fixes things up after itself */
@@ -715,13 +762,10 @@ static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
                                set_freepointer(s, object, NULL);
                                break;
                        } else {
-                               slab_err(s, page, "Freepointer 0x%p corrupt",
-                                                                       fp);
+                               slab_err(s, page, "Freepointer corrupt");
                                page->freelist = NULL;
                                page->inuse = s->objects;
-                               printk(KERN_ERR "@@@ SLUB %s: Freelist "
-                                       "cleared. Slab 0x%p\n",
-                                       s->name, page);
+                               slab_fix(s, "Freelist cleared");
                                return 0;
                        }
                        break;
@@ -733,15 +777,29 @@ static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
 
        if (page->inuse != s->objects - nr) {
                slab_err(s, page, "Wrong object count. Counter is %d but "
-                       "counted were %d", s, page, page->inuse,
-                                                       s->objects - nr);
+                       "counted were %d", page->inuse, s->objects - nr);
                page->inuse = s->objects - nr;
-               printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
-                       "Slab @0x%p\n", s->name, page);
+               slab_fix(s, "Object count adjusted.");
        }
        return search == NULL;
 }
 
+static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
+{
+       if (s->flags & SLAB_TRACE) {
+               printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
+                       s->name,
+                       alloc ? "alloc" : "free",
+                       object, page->inuse,
+                       page->freelist);
+
+               if (!alloc)
+                       print_section("Object", (void *)object, s->objsize);
+
+               dump_stack();
+       }
+}
+
 /*
  * Tracking of fully allocated slabs for debugging purposes.
  */
@@ -766,14 +824,24 @@ static void remove_full(struct kmem_cache *s, struct page *page)
        spin_unlock(&n->list_lock);
 }
 
-static int alloc_object_checks(struct kmem_cache *s, struct page *page,
-                                                       void *object)
+static void setup_object_debug(struct kmem_cache *s, struct page *page,
+                                                               void *object)
+{
+       if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
+               return;
+
+       init_object(s, object, 0);
+       init_tracking(s, object);
+}
+
+static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
+                                               void *object, void *addr)
 {
        if (!check_slab(s, page))
                goto bad;
 
        if (object && !on_freelist(s, page, object)) {
-               slab_err(s, page, "Object 0x%p already allocated", object);
+               object_err(s, page, object, "Object already allocated");
                goto bad;
        }
 
@@ -782,13 +850,16 @@ static int alloc_object_checks(struct kmem_cache *s, struct page *page,
                goto bad;
        }
 
-       if (!object)
-               return 1;
-
-       if (!check_object(s, page, object, 0))
+       if (object && !check_object(s, page, object, 0))
                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);
        return 1;
+
 bad:
        if (PageSlab(page)) {
                /*
@@ -796,8 +867,7 @@ bad:
                 * to avoid issues in the future. Marking all objects
                 * as used avoids touching the remaining objects.
                 */
-               printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
-                       s->name, page);
+               slab_fix(s, "Marking all objects used");
                page->inuse = s->objects;
                page->freelist = NULL;
                /* Fix up fields that may be corrupted */
@@ -806,8 +876,8 @@ bad:
        return 0;
 }
 
-static int free_object_checks(struct kmem_cache *s, struct page *page,
-                                                       void *object)
+static int free_debug_processing(struct kmem_cache *s, struct page *page,
+                                               void *object, void *addr)
 {
        if (!check_slab(s, page))
                goto fail;
@@ -818,7 +888,7 @@ static int free_object_checks(struct kmem_cache *s, struct page *page,
        }
 
        if (on_freelist(s, page, object)) {
-               slab_err(s, page, "Object 0x%p already free", object);
+               object_err(s, page, object, "Object already free");
                goto fail;
        }
 
@@ -837,67 +907,78 @@ static int free_object_checks(struct kmem_cache *s, struct page *page,
                        dump_stack();
                }
                else
-                       slab_err(s, page, "object at 0x%p belongs "
-                               "to slab %s", object, page->slab->name);
+                       object_err(s, page, object,
+                                       "page slab pointer corrupt.");
                goto fail;
        }
+
+       /* Special debug activities for freeing objects */
+       if (!SlabFrozen(page) && !page->freelist)
+               remove_full(s, page);
+       if (s->flags & SLAB_STORE_USER)
+               set_track(s, object, TRACK_FREE, addr);
+       trace(s, page, object, 0);
+       init_object(s, object, 0);
        return 1;
+
 fail:
-       printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
-               s->name, page, object);
+       slab_fix(s, "Object at 0x%p not freed", object);
        return 0;
 }
 
-static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
+static int __init setup_slub_debug(char *str)
 {
-       if (s->flags & SLAB_TRACE) {
-               printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
-                       s->name,
-                       alloc ? "alloc" : "free",
-                       object, page->inuse,
-                       page->freelist);
+       slub_debug = DEBUG_DEFAULT_FLAGS;
+       if (*str++ != '=' || !*str)
+               /*
+                * No options specified. Switch on full debugging.
+                */
+               goto out;
 
-               if (!alloc)
-                       print_section("Object", (void *)object, s->objsize);
+       if (*str == ',')
+               /*
+                * No options but restriction on slabs. This means full
+                * debugging for slabs matching a pattern.
+                */
+               goto check_slabs;
 
-               dump_stack();
-       }
-}
+       slub_debug = 0;
+       if (*str == '-')
+               /*
+                * Switch off all debugging measures.
+                */
+               goto out;
 
-static int __init setup_slub_debug(char *str)
-{
-       if (!str || *str != '=')
-               slub_debug = DEBUG_DEFAULT_FLAGS;
-       else {
-               str++;
-               if (*str == 0 || *str == ',')
-                       slub_debug = DEBUG_DEFAULT_FLAGS;
-               else
-               for( ;*str && *str != ','; str++)
-                       switch (*str) {
-                       case 'f' : case 'F' :
-                               slub_debug |= SLAB_DEBUG_FREE;
-                               break;
-                       case 'z' : case 'Z' :
-                               slub_debug |= SLAB_RED_ZONE;
-                               break;
-                       case 'p' : case 'P' :
-                               slub_debug |= SLAB_POISON;
-                               break;
-                       case 'u' : case 'U' :
-                               slub_debug |= SLAB_STORE_USER;
-                               break;
-                       case 't' : case 'T' :
-                               slub_debug |= SLAB_TRACE;
-                               break;
-                       default:
-                               printk(KERN_ERR "slub_debug option '%c' "
-                                       "unknown. skipped\n",*str);
-                       }
+       /*
+        * Determine which debug features should be switched on
+        */
+       for ( ;*str && *str != ','; str++) {
+               switch (tolower(*str)) {
+               case 'f':
+                       slub_debug |= SLAB_DEBUG_FREE;
+                       break;
+               case 'z':
+                       slub_debug |= SLAB_RED_ZONE;
+                       break;
+               case 'p':
+                       slub_debug |= SLAB_POISON;
+                       break;
+               case 'u':
+                       slub_debug |= SLAB_STORE_USER;
+                       break;
+               case 't':
+                       slub_debug |= SLAB_TRACE;
+                       break;
+               default:
+                       printk(KERN_ERR "slub_debug option '%c' "
+                               "unknown. skipped\n",*str);
+               }
        }
 
+check_slabs:
        if (*str == ',')
                slub_debug_slabs = str + 1;
+out:
        return 1;
 }
 
@@ -917,7 +998,7 @@ static void kmem_cache_open_debug_check(struct kmem_cache *s)
         * Debugging or ctor may create a need to move the free
         * pointer. Fail if this happens.
         */
-       if (s->size >= 65535 * sizeof(void *)) {
+       if (s->objsize >= 65535 * sizeof(void *)) {
                BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
                                SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
                BUG_ON(s->ctor);
@@ -932,26 +1013,20 @@ static void kmem_cache_open_debug_check(struct kmem_cache *s)
                                s->flags |= slub_debug;
 }
 #else
+static inline void setup_object_debug(struct kmem_cache *s,
+                       struct page *page, void *object) {}
 
-static inline int alloc_object_checks(struct kmem_cache *s,
-               struct page *page, void *object) { return 0; }
+static inline int alloc_debug_processing(struct kmem_cache *s,
+       struct page *page, void *object, void *addr) { return 0; }
 
-static inline int free_object_checks(struct kmem_cache *s,
-               struct page *page, void *object) { return 0; }
+static inline int free_debug_processing(struct kmem_cache *s,
+       struct page *page, void *object, void *addr) { return 0; }
 
-static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
-static inline void remove_full(struct kmem_cache *s, struct page *page) {}
-static inline void trace(struct kmem_cache *s, struct page *page,
-                       void *object, int alloc) {}
-static inline void init_object(struct kmem_cache *s,
-                       void *object, int active) {}
-static inline void init_tracking(struct kmem_cache *s, void *object) {}
 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; }
-static inline void set_track(struct kmem_cache *s, void *object,
-                       enum track_item alloc, void *addr) {}
+static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
 static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
 #define slub_debug 0
 #endif
@@ -988,13 +1063,9 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 static void setup_object(struct kmem_cache *s, struct page *page,
                                void *object)
 {
-       if (SlabDebug(page)) {
-               init_object(s, object, 0);
-               init_tracking(s, object);
-       }
-
+       setup_object_debug(s, page, object);
        if (unlikely(s->ctor))
-               s->ctor(object, s, SLAB_CTOR_CONSTRUCTOR);
+               s->ctor(object, s, 0);
 }
 
 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
@@ -1449,12 +1520,8 @@ new_slab:
        return NULL;
 debug:
        object = page->freelist;
-       if (!alloc_object_checks(s, page, object))
+       if (!alloc_debug_processing(s, page, object, addr))
                goto another_slab;
-       if (s->flags & SLAB_STORE_USER)
-               set_track(s, object, TRACK_ALLOC, addr);
-       trace(s, page, object, 1);
-       init_object(s, object, 1);
 
        page->inuse++;
        page->freelist = object[page->offset];
@@ -1561,14 +1628,8 @@ slab_empty:
        return;
 
 debug:
-       if (!free_object_checks(s, page, x))
+       if (!free_debug_processing(s, page, x, addr))
                goto out_unlock;
-       if (!SlabFrozen(page) && !page->freelist)
-               remove_full(s, page);
-       if (s->flags & SLAB_STORE_USER)
-               set_track(s, x, TRACK_FREE, addr);
-       trace(s, page, object, 0);
-       init_object(s, object, 0);
        goto checks_ok;
 }
 
@@ -1680,8 +1741,17 @@ static inline int slab_order(int size, int min_objects,
 {
        int order;
        int rem;
+       int min_order = slub_min_order;
 
-       for (order = max(slub_min_order,
+       /*
+        * If we would create too many object per slab then reduce
+        * the slab order even if it goes below slub_min_order.
+        */
+       while (min_order > 0 &&
+               (PAGE_SIZE << min_order) >= MAX_OBJECTS_PER_SLAB * size)
+                       min_order--;
+
+       for (order = max(min_order,
                                fls(min_objects * size - 1) - PAGE_SHIFT);
                        order <= max_order; order++) {
 
@@ -1695,6 +1765,9 @@ static inline int slab_order(int size, int min_objects,
                if (rem <= slab_size / fract_leftover)
                        break;
 
+               /* If the next size is too high then exit now */
+               if (slab_size * 2 >= MAX_OBJECTS_PER_SLAB * size)
+                       break;
        }
 
        return order;
@@ -1796,8 +1869,6 @@ static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflag
        BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
 
        page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
-       /* new_slab() disables interupts */
-       local_irq_enable();
 
        BUG_ON(!page);
        n = page->freelist;
@@ -1806,9 +1877,16 @@ static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflag
        page->inuse++;
        kmalloc_caches->node[node] = n;
        init_object(kmalloc_caches, n, 1);
+       init_tracking(kmalloc_caches, n);
        init_kmem_cache_node(n);
        atomic_long_inc(&n->nr_slabs);
        add_partial(n, page);
+
+       /*
+        * new_slab() disables interupts. If we do not reenable interrupts here
+        * then bootup would continue with interrupts disabled.
+        */
+       local_irq_enable();
        return n;
 }
 
@@ -1915,7 +1993,6 @@ static int calculate_sizes(struct kmem_cache *s)
         */
        s->inuse = size;
 
-#ifdef CONFIG_SLUB_DEBUG
        if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
                s->ctor)) {
                /*
@@ -1930,6 +2007,7 @@ static int calculate_sizes(struct kmem_cache *s)
                size += sizeof(void *);
        }
 
+#ifdef CONFIG_SLUB_DEBUG
        if (flags & SLAB_STORE_USER)
                /*
                 * Need to store information about allocs and frees after
@@ -1977,7 +2055,7 @@ static int calculate_sizes(struct kmem_cache *s)
         * The page->inuse field is only 16 bit wide! So we cannot have
         * more than 64k objects per slab.
         */
-       if (!s->objects || s->objects > 65535)
+       if (!s->objects || s->objects > MAX_OBJECTS_PER_SLAB)
                return 0;
        return 1;
 
@@ -2014,7 +2092,6 @@ error:
                        s->offset, flags);
        return 0;
 }
-EXPORT_SYMBOL(kmem_cache_open);
 
 /*
  * Check if a given pointer is valid
@@ -2239,7 +2316,7 @@ void *__kmalloc(size_t size, gfp_t flags)
 
        if (s)
                return slab_alloc(s, flags, -1, __builtin_return_address(0));
-       return NULL;
+       return ZERO_SIZE_PTR;
 }
 EXPORT_SYMBOL(__kmalloc);
 
@@ -2250,16 +2327,20 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
 
        if (s)
                return slab_alloc(s, flags, node, __builtin_return_address(0));
-       return NULL;
+       return ZERO_SIZE_PTR;
 }
 EXPORT_SYMBOL(__kmalloc_node);
 #endif
 
 size_t ksize(const void *object)
 {
-       struct page *page = get_object_page(object);
+       struct page *page;
        struct kmem_cache *s;
 
+       if (object == ZERO_SIZE_PTR)
+               return 0;
+
+       page = get_object_page(object);
        BUG_ON(!page);
        s = page->slab;
        BUG_ON(!s);
@@ -2291,7 +2372,13 @@ void kfree(const void *x)
        struct kmem_cache *s;
        struct page *page;
 
-       if (!x)
+       /*
+        * This has to be an unsigned comparison. According to Linus
+        * some gcc version treat a pointer as a signed entity. Then
+        * this comparison would be true for all "negative" pointers
+        * (which would cover the whole upper half of the address space).
+        */
+       if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
                return;
 
        page = virt_to_head_page(x);
@@ -2380,43 +2467,6 @@ int kmem_cache_shrink(struct kmem_cache *s)
 }
 EXPORT_SYMBOL(kmem_cache_shrink);
 
-/**
- * krealloc - reallocate memory. The contents will remain unchanged.
- * @p: object to reallocate memory for.
- * @new_size: how many bytes of memory are required.
- * @flags: the type of memory to allocate.
- *
- * The contents of the object pointed to are preserved up to the
- * lesser of the new and old sizes.  If @p is %NULL, krealloc()
- * behaves exactly like kmalloc().  If @size is 0 and @p is not a
- * %NULL pointer, the object pointed to is freed.
- */
-void *krealloc(const void *p, size_t new_size, gfp_t flags)
-{
-       void *ret;
-       size_t ks;
-
-       if (unlikely(!p))
-               return kmalloc(new_size, flags);
-
-       if (unlikely(!new_size)) {
-               kfree(p);
-               return NULL;
-       }
-
-       ks = ksize(p);
-       if (ks >= new_size)
-               return (void *)p;
-
-       ret = kmalloc(new_size, flags);
-       if (ret) {
-               memcpy(ret, p, min(new_size, ks));
-               kfree(p);
-       }
-       return ret;
-}
-EXPORT_SYMBOL(krealloc);
-
 /********************************************************************
  *                     Basic setup of slabs
  *******************************************************************/
@@ -2424,6 +2474,7 @@ EXPORT_SYMBOL(krealloc);
 void __init kmem_cache_init(void)
 {
        int i;
+       int caches = 0;
 
 #ifdef CONFIG_NUMA
        /*
@@ -2433,20 +2484,30 @@ void __init kmem_cache_init(void)
         */
        create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
                sizeof(struct kmem_cache_node), GFP_KERNEL);
+       kmalloc_caches[0].refcount = -1;
+       caches++;
 #endif
 
        /* Able to allocate the per node structures */
        slab_state = PARTIAL;
 
        /* Caches that are not of the two-to-the-power-of size */
-       create_kmalloc_cache(&kmalloc_caches[1],
+       if (KMALLOC_MIN_SIZE <= 64) {
+               create_kmalloc_cache(&kmalloc_caches[1],
                                "kmalloc-96", 96, GFP_KERNEL);
-       create_kmalloc_cache(&kmalloc_caches[2],
+               caches++;
+       }
+       if (KMALLOC_MIN_SIZE <= 128) {
+               create_kmalloc_cache(&kmalloc_caches[2],
                                "kmalloc-192", 192, GFP_KERNEL);
+               caches++;
+       }
 
-       for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
+       for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
                create_kmalloc_cache(&kmalloc_caches[i],
                        "kmalloc", 1 << i, GFP_KERNEL);
+               caches++;
+       }
 
        slab_state = UP;
 
@@ -2463,8 +2524,8 @@ void __init kmem_cache_init(void)
                                nr_cpu_ids * sizeof(struct page *);
 
        printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
-               " Processors=%d, Nodes=%d\n",
-               KMALLOC_SHIFT_HIGH, cache_line_size(),
+               " CPUs=%d, Nodes=%d\n",
+               caches, cache_line_size(),
                slub_min_order, slub_max_order, slub_min_objects,
                nr_cpu_ids, nr_node_ids);
 }
@@ -2480,6 +2541,12 @@ static int slab_unmergeable(struct kmem_cache *s)
        if (s->ctor)
                return 1;
 
+       /*
+        * We may have set a slab to be unmergeable during bootstrap.
+        */
+       if (s->refcount < 0)
+               return 1;
+
        return 0;
 }
 
@@ -2487,7 +2554,7 @@ static struct kmem_cache *find_mergeable(size_t size,
                size_t align, unsigned long flags,
                void (*ctor)(void *, struct kmem_cache *, unsigned long))
 {
-       struct list_head *h;
+       struct kmem_cache *s;
 
        if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
                return NULL;
@@ -2499,10 +2566,7 @@ static struct kmem_cache *find_mergeable(size_t size,
        align = calculate_alignment(flags, align, size);
        size = ALIGN(size, align);
 
-       list_for_each(h, &slab_caches) {
-               struct kmem_cache *s =
-                       container_of(h, struct kmem_cache, list);
-
+       list_for_each_entry(s, &slab_caches, list) {
                if (slab_unmergeable(s))
                        continue;
 
@@ -2584,20 +2648,6 @@ void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
 EXPORT_SYMBOL(kmem_cache_zalloc);
 
 #ifdef CONFIG_SMP
-static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
-{
-       struct list_head *h;
-
-       down_read(&slub_lock);
-       list_for_each(h, &slab_caches) {
-               struct kmem_cache *s =
-                       container_of(h, struct kmem_cache, list);
-
-               func(s, cpu);
-       }
-       up_read(&slub_lock);
-}
-
 /*
  * Use the cpu notifier to insure that the cpu slabs are flushed when
  * necessary.
@@ -2606,13 +2656,21 @@ static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
                unsigned long action, void *hcpu)
 {
        long cpu = (long)hcpu;
+       struct kmem_cache *s;
+       unsigned long flags;
 
        switch (action) {
        case CPU_UP_CANCELED:
        case CPU_UP_CANCELED_FROZEN:
        case CPU_DEAD:
        case CPU_DEAD_FROZEN:
-               for_all_slabs(__flush_cpu_slab, cpu);
+               down_read(&slub_lock);
+               list_for_each_entry(s, &slab_caches, list) {
+                       local_irq_save(flags);
+                       __flush_cpu_slab(s, cpu);
+                       local_irq_restore(flags);
+               }
+               up_read(&slub_lock);
                break;
        default:
                break;
@@ -2630,7 +2688,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
        struct kmem_cache *s = get_slab(size, gfpflags);
 
        if (!s)
-               return NULL;
+               return ZERO_SIZE_PTR;
 
        return slab_alloc(s, gfpflags, -1, caller);
 }
@@ -2641,7 +2699,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
        struct kmem_cache *s = get_slab(size, gfpflags);
 
        if (!s)
-               return NULL;
+               return ZERO_SIZE_PTR;
 
        return slab_alloc(s, gfpflags, node, caller);
 }
@@ -2825,18 +2883,14 @@ static void free_loc_track(struct loc_track *t)
                        get_order(sizeof(struct location) * t->max));
 }
 
-static int alloc_loc_track(struct loc_track *t, unsigned long max)
+static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
 {
        struct location *l;
        int order;
 
-       if (!max)
-               max = PAGE_SIZE / sizeof(struct location);
-
        order = get_order(sizeof(struct location) * max);
 
-       l = (void *)__get_free_pages(GFP_KERNEL, order);
-
+       l = (void *)__get_free_pages(flags, order);
        if (!l)
                return 0;
 
@@ -2902,7 +2956,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
        /*
         * Not found. Insert new tracking element.
         */
-       if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
+       if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
                return 0;
 
        l = t->loc + pos;
@@ -2945,11 +2999,12 @@ static int list_locations(struct kmem_cache *s, char *buf,
 {
        int n = 0;
        unsigned long i;
-       struct loc_track t;
+       struct loc_track t = { 0, 0, NULL };
        int node;
 
-       t.count = 0;
-       t.max = 0;
+       if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
+                       GFP_KERNEL))
+               return sprintf(buf, "Out of memory\n");
 
        /* Push back cpu slabs */
        flush_all(s);
@@ -3000,13 +3055,15 @@ static int list_locations(struct kmem_cache *s, char *buf,
                        n += sprintf(buf + n, " pid=%ld",
                                l->min_pid);
 
-               if (num_online_cpus() > 1 && !cpus_empty(l->cpus)) {
+               if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
+                               n < PAGE_SIZE - 60) {
                        n += sprintf(buf + n, " cpus=");
                        n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
                                        l->cpus);
                }
 
-               if (num_online_nodes() > 1 && !nodes_empty(l->nodes)) {
+               if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
+                               n < PAGE_SIZE - 60) {
                        n += sprintf(buf + n, " nodes=");
                        n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
                                        l->nodes);
@@ -3635,7 +3692,7 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
 
 static int __init slab_sysfs_init(void)
 {
-       struct list_head *h;
+       struct kmem_cache *s;
        int err;
 
        err = subsystem_register(&slab_subsys);
@@ -3646,10 +3703,7 @@ static int __init slab_sysfs_init(void)
 
        slab_state = SYSFS;
 
-       list_for_each(h, &slab_caches) {
-               struct kmem_cache *s =
-                       container_of(h, struct kmem_cache, list);
-
+       list_for_each_entry(s, &slab_caches, list) {
                err = sysfs_slab_add(s);
                BUG_ON(err);
        }