linux/mm/slub.c
<<
>>
Prefs
   1/*
   2 * SLUB: A slab allocator that limits cache line use instead of queuing
   3 * objects in per cpu and per node lists.
   4 *
   5 * The allocator synchronizes using per slab locks and only
   6 * uses a centralized lock to manage a pool of partial slabs.
   7 *
   8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
   9 */
  10
  11#include <linux/mm.h>
  12#include <linux/module.h>
  13#include <linux/bit_spinlock.h>
  14#include <linux/interrupt.h>
  15#include <linux/bitops.h>
  16#include <linux/slab.h>
  17#include <linux/seq_file.h>
  18#include <linux/cpu.h>
  19#include <linux/cpuset.h>
  20#include <linux/mempolicy.h>
  21#include <linux/ctype.h>
  22#include <linux/kallsyms.h>
  23
  24/*
  25 * Lock order:
  26 *   1. slab_lock(page)
  27 *   2. slab->list_lock
  28 *
  29 *   The slab_lock protects operations on the object of a particular
  30 *   slab and its metadata in the page struct. If the slab lock
  31 *   has been taken then no allocations nor frees can be performed
  32 *   on the objects in the slab nor can the slab be added or removed
  33 *   from the partial or full lists since this would mean modifying
  34 *   the page_struct of the slab.
  35 *
  36 *   The list_lock protects the partial and full list on each node and
  37 *   the partial slab counter. If taken then no new slabs may be added or
  38 *   removed from the lists nor make the number of partial slabs be modified.
  39 *   (Note that the total number of slabs is an atomic value that may be
  40 *   modified without taking the list lock).
  41 *
  42 *   The list_lock is a centralized lock and thus we avoid taking it as
  43 *   much as possible. As long as SLUB does not have to handle partial
  44 *   slabs, operations can continue without any centralized lock. F.e.
  45 *   allocating a long series of objects that fill up slabs does not require
  46 *   the list lock.
  47 *
  48 *   The lock order is sometimes inverted when we are trying to get a slab
  49 *   off a list. We take the list_lock and then look for a page on the list
  50 *   to use. While we do that objects in the slabs may be freed. We can
  51 *   only operate on the slab if we have also taken the slab_lock. So we use
  52 *   a slab_trylock() on the slab. If trylock was successful then no frees
  53 *   can occur anymore and we can use the slab for allocations etc. If the
  54 *   slab_trylock() does not succeed then frees are in progress in the slab and
  55 *   we must stay away from it for a while since we may cause a bouncing
  56 *   cacheline if we try to acquire the lock. So go onto the next slab.
  57 *   If all pages are busy then we may allocate a new slab instead of reusing
  58 *   a partial slab. A new slab has noone operating on it and thus there is
  59 *   no danger of cacheline contention.
  60 *
  61 *   Interrupts are disabled during allocation and deallocation in order to
  62 *   make the slab allocator safe to use in the context of an irq. In addition
  63 *   interrupts are disabled to ensure that the processor does not change
  64 *   while handling per_cpu slabs, due to kernel preemption.
  65 *
  66 * SLUB assigns one slab for allocation to each processor.
  67 * Allocations only occur from these slabs called cpu slabs.
  68 *
  69 * Slabs with free elements are kept on a partial list and during regular
  70 * operations no list for full slabs is used. If an object in a full slab is
  71 * freed then the slab will show up again on the partial lists.
  72 * We track full slabs for debugging purposes though because otherwise we
  73 * cannot scan all objects.
  74 *
  75 * Slabs are freed when they become empty. Teardown and setup is
  76 * minimal so we rely on the page allocators per cpu caches for
  77 * fast frees and allocs.
  78 *
  79 * Overloading of page flags that are otherwise used for LRU management.
  80 *
  81 * PageActive           The slab is frozen and exempt from list processing.
  82 *                      This means that the slab is dedicated to a purpose
  83 *                      such as satisfying allocations for a specific
  84 *                      processor. Objects may be freed in the slab while
  85 *                      it is frozen but slab_free will then skip the usual
  86 *                      list operations. It is up to the processor holding
  87 *                      the slab to integrate the slab into the slab lists
  88 *                      when the slab is no longer needed.
  89 *
  90 *                      One use of this flag is to mark slabs that are
  91 *                      used for allocations. Then such a slab becomes a cpu
  92 *                      slab. The cpu slab may be equipped with an additional
  93 *                      lockless_freelist that allows lockless access to
  94 *                      free objects in addition to the regular freelist
  95 *                      that requires the slab lock.
  96 *
  97 * PageError            Slab requires special handling due to debug
  98 *                      options set. This moves slab handling out of
  99 *                      the fast path and disables lockless freelists.
 100 */
 101
 102#define FROZEN (1 << PG_active)
 103
 104#ifdef CONFIG_SLUB_DEBUG
 105#define SLABDEBUG (1 << PG_error)
 106#else
 107#define SLABDEBUG 0
 108#endif
 109
 110static inline int SlabFrozen(struct page *page)
 111{
 112        return page->flags & FROZEN;
 113}
 114
 115static inline void SetSlabFrozen(struct page *page)
 116{
 117        page->flags |= FROZEN;
 118}
 119
 120static inline void ClearSlabFrozen(struct page *page)
 121{
 122        page->flags &= ~FROZEN;
 123}
 124
 125static inline int SlabDebug(struct page *page)
 126{
 127        return page->flags & SLABDEBUG;
 128}
 129
 130static inline void SetSlabDebug(struct page *page)
 131{
 132        page->flags |= SLABDEBUG;
 133}
 134
 135static inline void ClearSlabDebug(struct page *page)
 136{
 137        page->flags &= ~SLABDEBUG;
 138}
 139
 140/*
 141 * Issues still to be resolved:
 142 *
 143 * - The per cpu array is updated for each new slab and and is a remote
 144 *   cacheline for most nodes. This could become a bouncing cacheline given
 145 *   enough frequent updates. There are 16 pointers in a cacheline, so at
 146 *   max 16 cpus could compete for the cacheline which may be okay.
 147 *
 148 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
 149 *
 150 * - Variable sizing of the per node arrays
 151 */
 152
 153/* Enable to test recovery from slab corruption on boot */
 154#undef SLUB_RESILIENCY_TEST
 155
 156#if PAGE_SHIFT <= 12
 157
 158/*
 159 * Small page size. Make sure that we do not fragment memory
 160 */
 161#define DEFAULT_MAX_ORDER 1
 162#define DEFAULT_MIN_OBJECTS 4
 163
 164#else
 165
 166/*
 167 * Large page machines are customarily able to handle larger
 168 * page orders.
 169 */
 170#define DEFAULT_MAX_ORDER 2
 171#define DEFAULT_MIN_OBJECTS 8
 172
 173#endif
 174
 175/*
 176 * Mininum number of partial slabs. These will be left on the partial
 177 * lists even if they are empty. kmem_cache_shrink may reclaim them.
 178 */
 179#define MIN_PARTIAL 2
 180
 181/*
 182 * Maximum number of desirable partial slabs.
 183 * The existence of more partial slabs makes kmem_cache_shrink
 184 * sort the partial list by the number of objects in the.
 185 */
 186#define MAX_PARTIAL 10
 187
 188#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
 189                                SLAB_POISON | SLAB_STORE_USER)
 190
 191/*
 192 * Set of flags that will prevent slab merging
 193 */
 194#define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
 195                SLAB_TRACE | SLAB_DESTROY_BY_RCU)
 196
 197#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
 198                SLAB_CACHE_DMA)
 199
 200#ifndef ARCH_KMALLOC_MINALIGN
 201#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
 202#endif
 203
 204#ifndef ARCH_SLAB_MINALIGN
 205#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
 206#endif
 207
 208/* Internal SLUB flags */
 209#define __OBJECT_POISON 0x80000000      /* Poison object */
 210
 211/* Not all arches define cache_line_size */
 212#ifndef cache_line_size
 213#define cache_line_size()       L1_CACHE_BYTES
 214#endif
 215
 216static int kmem_size = sizeof(struct kmem_cache);
 217
 218#ifdef CONFIG_SMP
 219static struct notifier_block slab_notifier;
 220#endif
 221
 222static enum {
 223        DOWN,           /* No slab functionality available */
 224        PARTIAL,        /* kmem_cache_open() works but kmalloc does not */
 225        UP,             /* Everything works but does not show up in sysfs */
 226        SYSFS           /* Sysfs up */
 227} slab_state = DOWN;
 228
 229/* A list of all slab caches on the system */
 230static DECLARE_RWSEM(slub_lock);
 231LIST_HEAD(slab_caches);
 232
 233/*
 234 * Tracking user of a slab.
 235 */
 236struct track {
 237        void *addr;             /* Called from address */
 238        int cpu;                /* Was running on cpu */
 239        int pid;                /* Pid context */
 240        unsigned long when;     /* When did the operation occur */
 241};
 242
 243enum track_item { TRACK_ALLOC, TRACK_FREE };
 244
 245#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
 246static int sysfs_slab_add(struct kmem_cache *);
 247static int sysfs_slab_alias(struct kmem_cache *, const char *);
 248static void sysfs_slab_remove(struct kmem_cache *);
 249#else
 250static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
 251static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
 252static void sysfs_slab_remove(struct kmem_cache *s) {}
 253#endif
 254
 255/********************************************************************
 256 *                      Core slab cache functions
 257 *******************************************************************/
 258
 259int slab_is_available(void)
 260{
 261        return slab_state >= UP;
 262}
 263
 264static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
 265{
 266#ifdef CONFIG_NUMA
 267        return s->node[node];
 268#else
 269        return &s->local_node;
 270#endif
 271}
 272
 273static inline int check_valid_pointer(struct kmem_cache *s,
 274                                struct page *page, const void *object)
 275{
 276        void *base;
 277
 278        if (!object)
 279                return 1;
 280
 281        base = page_address(page);
 282        if (object < base || object >= base + s->objects * s->size ||
 283                (object - base) % s->size) {
 284                return 0;
 285        }
 286
 287        return 1;
 288}
 289
 290/*
 291 * Slow version of get and set free pointer.
 292 *
 293 * This version requires touching the cache lines of kmem_cache which
 294 * we avoid to do in the fast alloc free paths. There we obtain the offset
 295 * from the page struct.
 296 */
 297static inline void *get_freepointer(struct kmem_cache *s, void *object)
 298{
 299        return *(void **)(object + s->offset);
 300}
 301
 302static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
 303{
 304        *(void **)(object + s->offset) = fp;
 305}
 306
 307/* Loop over all objects in a slab */
 308#define for_each_object(__p, __s, __addr) \
 309        for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
 310                        __p += (__s)->size)
 311
 312/* Scan freelist */
 313#define for_each_free_object(__p, __s, __free) \
 314        for (__p = (__free); __p; __p = get_freepointer((__s), __p))
 315
 316/* Determine object index from a given position */
 317static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
 318{
 319        return (p - addr) / s->size;
 320}
 321
 322#ifdef CONFIG_SLUB_DEBUG
 323/*
 324 * Debug settings:
 325 */
 326static int slub_debug;
 327
 328static char *slub_debug_slabs;
 329
 330/*
 331 * Object debugging
 332 */
 333static void print_section(char *text, u8 *addr, unsigned int length)
 334{
 335        int i, offset;
 336        int newline = 1;
 337        char ascii[17];
 338
 339        ascii[16] = 0;
 340
 341        for (i = 0; i < length; i++) {
 342                if (newline) {
 343                        printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
 344                        newline = 0;
 345                }
 346                printk(" %02x", addr[i]);
 347                offset = i % 16;
 348                ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
 349                if (offset == 15) {
 350                        printk(" %s\n",ascii);
 351                        newline = 1;
 352                }
 353        }
 354        if (!newline) {
 355                i %= 16;
 356                while (i < 16) {
 357                        printk("   ");
 358                        ascii[i] = ' ';
 359                        i++;
 360                }
 361                printk(" %s\n", ascii);
 362        }
 363}
 364
 365static struct track *get_track(struct kmem_cache *s, void *object,
 366        enum track_item alloc)
 367{
 368        struct track *p;
 369
 370        if (s->offset)
 371                p = object + s->offset + sizeof(void *);
 372        else
 373                p = object + s->inuse;
 374
 375        return p + alloc;
 376}
 377
 378static void set_track(struct kmem_cache *s, void *object,
 379                                enum track_item alloc, void *addr)
 380{
 381        struct track *p;
 382
 383        if (s->offset)
 384                p = object + s->offset + sizeof(void *);
 385        else
 386                p = object + s->inuse;
 387
 388        p += alloc;
 389        if (addr) {
 390                p->addr = addr;
 391                p->cpu = smp_processor_id();
 392                p->pid = current ? current->pid : -1;
 393                p->when = jiffies;
 394        } else
 395                memset(p, 0, sizeof(struct track));
 396}
 397
 398static void init_tracking(struct kmem_cache *s, void *object)
 399{
 400        if (s->flags & SLAB_STORE_USER) {
 401                set_track(s, object, TRACK_FREE, NULL);
 402                set_track(s, object, TRACK_ALLOC, NULL);
 403        }
 404}
 405
 406static void print_track(const char *s, struct track *t)
 407{
 408        if (!t->addr)
 409                return;
 410
 411        printk(KERN_ERR "%s: ", s);
 412        __print_symbol("%s", (unsigned long)t->addr);
 413        printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
 414}
 415
 416static void print_trailer(struct kmem_cache *s, u8 *p)
 417{
 418        unsigned int off;       /* Offset of last byte */
 419
 420        if (s->flags & SLAB_RED_ZONE)
 421                print_section("Redzone", p + s->objsize,
 422                        s->inuse - s->objsize);
 423
 424        printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
 425                        p + s->offset,
 426                        get_freepointer(s, p));
 427
 428        if (s->offset)
 429                off = s->offset + sizeof(void *);
 430        else
 431                off = s->inuse;
 432
 433        if (s->flags & SLAB_STORE_USER) {
 434                print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
 435                print_track("Last free ", get_track(s, p, TRACK_FREE));
 436                off += 2 * sizeof(struct track);
 437        }
 438
 439        if (off != s->size)
 440                /* Beginning of the filler is the free pointer */
 441                print_section("Filler", p + off, s->size - off);
 442}
 443
 444static void object_err(struct kmem_cache *s, struct page *page,
 445                        u8 *object, char *reason)
 446{
 447        u8 *addr = page_address(page);
 448
 449        printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
 450                        s->name, reason, object, page);
 451        printk(KERN_ERR "    offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
 452                object - addr, page->flags, page->inuse, page->freelist);
 453        if (object > addr + 16)
 454                print_section("Bytes b4", object - 16, 16);
 455        print_section("Object", object, min(s->objsize, 128));
 456        print_trailer(s, object);
 457        dump_stack();
 458}
 459
 460static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
 461{
 462        va_list args;
 463        char buf[100];
 464
 465        va_start(args, reason);
 466        vsnprintf(buf, sizeof(buf), reason, args);
 467        va_end(args);
 468        printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
 469                page);
 470        dump_stack();
 471}
 472
 473static void init_object(struct kmem_cache *s, void *object, int active)
 474{
 475        u8 *p = object;
 476
 477        if (s->flags & __OBJECT_POISON) {
 478                memset(p, POISON_FREE, s->objsize - 1);
 479                p[s->objsize -1] = POISON_END;
 480        }
 481
 482        if (s->flags & SLAB_RED_ZONE)
 483                memset(p + s->objsize,
 484                        active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
 485                        s->inuse - s->objsize);
 486}
 487
 488static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
 489{
 490        while (bytes) {
 491                if (*start != (u8)value)
 492                        return 0;
 493                start++;
 494                bytes--;
 495        }
 496        return 1;
 497}
 498
 499/*
 500 * Object layout:
 501 *
 502 * object address
 503 *      Bytes of the object to be managed.
 504 *      If the freepointer may overlay the object then the free
 505 *      pointer is the first word of the object.
 506 *
 507 *      Poisoning uses 0x6b (POISON_FREE) and the last byte is
 508 *      0xa5 (POISON_END)
 509 *
 510 * object + s->objsize
 511 *      Padding to reach word boundary. This is also used for Redzoning.
 512 *      Padding is extended by another word if Redzoning is enabled and
 513 *      objsize == inuse.
 514 *
 515 *      We fill with 0xbb (RED_INACTIVE) for inactive objects and with
 516 *      0xcc (RED_ACTIVE) for objects in use.
 517 *
 518 * object + s->inuse
 519 *      Meta data starts here.
 520 *
 521 *      A. Free pointer (if we cannot overwrite object on free)
 522 *      B. Tracking data for SLAB_STORE_USER
 523 *      C. Padding to reach required alignment boundary or at mininum
 524 *              one word if debuggin is on to be able to detect writes
 525 *              before the word boundary.
 526 *
 527 *      Padding is done using 0x5a (POISON_INUSE)
 528 *
 529 * object + s->size
 530 *      Nothing is used beyond s->size.
 531 *
 532 * If slabcaches are merged then the objsize and inuse boundaries are mostly
 533 * ignored. And therefore no slab options that rely on these boundaries
 534 * may be used with merged slabcaches.
 535 */
 536
 537static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
 538                                                void *from, void *to)
 539{
 540        printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
 541                s->name, message, data, from, to - 1);
 542        memset(from, data, to - from);
 543}
 544
 545static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
 546{
 547        unsigned long off = s->inuse;   /* The end of info */
 548
 549        if (s->offset)
 550                /* Freepointer is placed after the object. */
 551                off += sizeof(void *);
 552
 553        if (s->flags & SLAB_STORE_USER)
 554                /* We also have user information there */
 555                off += 2 * sizeof(struct track);
 556
 557        if (s->size == off)
 558                return 1;
 559
 560        if (check_bytes(p + off, POISON_INUSE, s->size - off))
 561                return 1;
 562
 563        object_err(s, page, p, "Object padding check fails");
 564
 565        /*
 566         * Restore padding
 567         */
 568        restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
 569        return 0;
 570}
 571
 572static int slab_pad_check(struct kmem_cache *s, struct page *page)
 573{
 574        u8 *p;
 575        int length, remainder;
 576
 577        if (!(s->flags & SLAB_POISON))
 578                return 1;
 579
 580        p = page_address(page);
 581        length = s->objects * s->size;
 582        remainder = (PAGE_SIZE << s->order) - length;
 583        if (!remainder)
 584                return 1;
 585
 586        if (!check_bytes(p + length, POISON_INUSE, remainder)) {
 587                slab_err(s, page, "Padding check failed");
 588                restore_bytes(s, "slab padding", POISON_INUSE, p + length,
 589                        p + length + remainder);
 590                return 0;
 591        }
 592        return 1;
 593}
 594
 595static int check_object(struct kmem_cache *s, struct page *page,
 596                                        void *object, int active)
 597{
 598        u8 *p = object;
 599        u8 *endobject = object + s->objsize;
 600
 601        if (s->flags & SLAB_RED_ZONE) {
 602                unsigned int red =
 603                        active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
 604
 605                if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
 606                        object_err(s, page, object,
 607                        active ? "Redzone Active" : "Redzone Inactive");
 608                        restore_bytes(s, "redzone", red,
 609                                endobject, object + s->inuse);
 610                        return 0;
 611                }
 612        } else {
 613                if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
 614                        !check_bytes(endobject, POISON_INUSE,
 615                                        s->inuse - s->objsize)) {
 616                object_err(s, page, p, "Alignment padding check fails");
 617                /*
 618                 * Fix it so that there will not be another report.
 619                 *
 620                 * Hmmm... We may be corrupting an object that now expects
 621                 * to be longer than allowed.
 622                 */
 623                restore_bytes(s, "alignment padding", POISON_INUSE,
 624                        endobject, object + s->inuse);
 625                }
 626        }
 627
 628        if (s->flags & SLAB_POISON) {
 629                if (!active && (s->flags & __OBJECT_POISON) &&
 630                        (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
 631                                p[s->objsize - 1] != POISON_END)) {
 632
 633                        object_err(s, page, p, "Poison check failed");
 634                        restore_bytes(s, "Poison", POISON_FREE,
 635                                                p, p + s->objsize -1);
 636                        restore_bytes(s, "Poison", POISON_END,
 637                                        p + s->objsize - 1, p + s->objsize);
 638                        return 0;
 639                }
 640                /*
 641                 * check_pad_bytes cleans up on its own.
 642                 */
 643                check_pad_bytes(s, page, p);
 644        }
 645
 646        if (!s->offset && active)
 647                /*
 648                 * Object and freepointer overlap. Cannot check
 649                 * freepointer while object is allocated.
 650                 */
 651                return 1;
 652
 653        /* Check free pointer validity */
 654        if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
 655                object_err(s, page, p, "Freepointer corrupt");
 656                /*
 657                 * No choice but to zap it and thus loose the remainder
 658                 * of the free objects in this slab. May cause
 659                 * another error because the object count is now wrong.
 660                 */
 661                set_freepointer(s, p, NULL);
 662                return 0;
 663        }
 664        return 1;
 665}
 666
 667static int check_slab(struct kmem_cache *s, struct page *page)
 668{
 669        VM_BUG_ON(!irqs_disabled());
 670
 671        if (!PageSlab(page)) {
 672                slab_err(s, page, "Not a valid slab page flags=%lx "
 673                        "mapping=0x%p count=%d", page->flags, page->mapping,
 674                        page_count(page));
 675                return 0;
 676        }
 677        if (page->offset * sizeof(void *) != s->offset) {
 678                slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
 679                        "mapping=0x%p count=%d",
 680                        (unsigned long)(page->offset * sizeof(void *)),
 681                        page->flags,
 682                        page->mapping,
 683                        page_count(page));
 684                return 0;
 685        }
 686        if (page->inuse > s->objects) {
 687                slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
 688                        "mapping=0x%p count=%d",
 689                        s->name, page->inuse, s->objects, page->flags,
 690                        page->mapping, page_count(page));
 691                return 0;
 692        }
 693        /* Slab_pad_check fixes things up after itself */
 694        slab_pad_check(s, page);
 695        return 1;
 696}
 697
 698/*
 699 * Determine if a certain object on a page is on the freelist. Must hold the
 700 * slab lock to guarantee that the chains are in a consistent state.
 701 */
 702static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
 703{
 704        int nr = 0;
 705        void *fp = page->freelist;
 706        void *object = NULL;
 707
 708        while (fp && nr <= s->objects) {
 709                if (fp == search)
 710                        return 1;
 711                if (!check_valid_pointer(s, page, fp)) {
 712                        if (object) {
 713                                object_err(s, page, object,
 714                                        "Freechain corrupt");
 715                                set_freepointer(s, object, NULL);
 716                                break;
 717                        } else {
 718                                slab_err(s, page, "Freepointer 0x%p corrupt",
 719                                                                        fp);
 720                                page->freelist = NULL;
 721                                page->inuse = s->objects;
 722                                printk(KERN_ERR "@@@ SLUB %s: Freelist "
 723                                        "cleared. Slab 0x%p\n",
 724                                        s->name, page);
 725                                return 0;
 726                        }
 727                        break;
 728                }
 729                object = fp;
 730                fp = get_freepointer(s, object);
 731                nr++;
 732        }
 733
 734        if (page->inuse != s->objects - nr) {
 735                slab_err(s, page, "Wrong object count. Counter is %d but "
 736                        "counted were %d", s, page, page->inuse,
 737                                                        s->objects - nr);
 738                page->inuse = s->objects - nr;
 739                printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
 740                        "Slab @0x%p\n", s->name, page);
 741        }
 742        return search == NULL;
 743}
 744
 745static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
 746{
 747        if (s->flags & SLAB_TRACE) {
 748                printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
 749                        s->name,
 750                        alloc ? "alloc" : "free",
 751                        object, page->inuse,
 752                        page->freelist);
 753
 754                if (!alloc)
 755                        print_section("Object", (void *)object, s->objsize);
 756
 757                dump_stack();
 758        }
 759}
 760
 761/*
 762 * Tracking of fully allocated slabs for debugging purposes.
 763 */
 764static void add_full(struct kmem_cache_node *n, struct page *page)
 765{
 766        spin_lock(&n->list_lock);
 767        list_add(&page->lru, &n->full);
 768        spin_unlock(&n->list_lock);
 769}
 770
 771static void remove_full(struct kmem_cache *s, struct page *page)
 772{
 773        struct kmem_cache_node *n;
 774
 775        if (!(s->flags & SLAB_STORE_USER))
 776                return;
 777
 778        n = get_node(s, page_to_nid(page));
 779
 780        spin_lock(&n->list_lock);
 781        list_del(&page->lru);
 782        spin_unlock(&n->list_lock);
 783}
 784
 785static void setup_object_debug(struct kmem_cache *s, struct page *page,
 786                                                                void *object)
 787{
 788        if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
 789                return;
 790
 791        init_object(s, object, 0);
 792        init_tracking(s, object);
 793}
 794
 795static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
 796                                                void *object, void *addr)
 797{
 798        if (!check_slab(s, page))
 799                goto bad;
 800
 801        if (object && !on_freelist(s, page, object)) {
 802                slab_err(s, page, "Object 0x%p already allocated", object);
 803                goto bad;
 804        }
 805
 806        if (!check_valid_pointer(s, page, object)) {
 807                object_err(s, page, object, "Freelist Pointer check fails");
 808                goto bad;
 809        }
 810
 811        if (object && !check_object(s, page, object, 0))
 812                goto bad;
 813
 814        /* Success perform special debug activities for allocs */
 815        if (s->flags & SLAB_STORE_USER)
 816                set_track(s, object, TRACK_ALLOC, addr);
 817        trace(s, page, object, 1);
 818        init_object(s, object, 1);
 819        return 1;
 820
 821bad:
 822        if (PageSlab(page)) {
 823                /*
 824                 * If this is a slab page then lets do the best we can
 825                 * to avoid issues in the future. Marking all objects
 826                 * as used avoids touching the remaining objects.
 827                 */
 828                printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
 829                        s->name, page);
 830                page->inuse = s->objects;
 831                page->freelist = NULL;
 832                /* Fix up fields that may be corrupted */
 833                page->offset = s->offset / sizeof(void *);
 834        }
 835        return 0;
 836}
 837
 838static int free_debug_processing(struct kmem_cache *s, struct page *page,
 839                                                void *object, void *addr)
 840{
 841        if (!check_slab(s, page))
 842                goto fail;
 843
 844        if (!check_valid_pointer(s, page, object)) {
 845                slab_err(s, page, "Invalid object pointer 0x%p", object);
 846                goto fail;
 847        }
 848
 849        if (on_freelist(s, page, object)) {
 850                slab_err(s, page, "Object 0x%p already free", object);
 851                goto fail;
 852        }
 853
 854        if (!check_object(s, page, object, 1))
 855                return 0;
 856
 857        if (unlikely(s != page->slab)) {
 858                if (!PageSlab(page))
 859                        slab_err(s, page, "Attempt to free object(0x%p) "
 860                                "outside of slab", object);
 861                else
 862                if (!page->slab) {
 863                        printk(KERN_ERR
 864                                "SLUB <none>: no slab for object 0x%p.\n",
 865                                                object);
 866                        dump_stack();
 867                }
 868                else
 869                        slab_err(s, page, "object at 0x%p belongs "
 870                                "to slab %s", object, page->slab->name);
 871                goto fail;
 872        }
 873
 874        /* Special debug activities for freeing objects */
 875        if (!SlabFrozen(page) && !page->freelist)
 876                remove_full(s, page);
 877        if (s->flags & SLAB_STORE_USER)
 878                set_track(s, object, TRACK_FREE, addr);
 879        trace(s, page, object, 0);
 880        init_object(s, object, 0);
 881        return 1;
 882
 883fail:
 884        printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
 885                s->name, page, object);
 886        return 0;
 887}
 888
 889static int __init setup_slub_debug(char *str)
 890{
 891        if (!str || *str != '=')
 892                slub_debug = DEBUG_DEFAULT_FLAGS;
 893        else {
 894                str++;
 895                if (*str == 0 || *str == ',')
 896                        slub_debug = DEBUG_DEFAULT_FLAGS;
 897                else
 898                for( ;*str && *str != ','; str++)
 899                        switch (*str) {
 900                        case 'f' : case 'F' :
 901                                slub_debug |= SLAB_DEBUG_FREE;
 902                                break;
 903                        case 'z' : case 'Z' :
 904                                slub_debug |= SLAB_RED_ZONE;
 905                                break;
 906                        case 'p' : case 'P' :
 907                                slub_debug |= SLAB_POISON;
 908                                break;
 909                        case 'u' : case 'U' :
 910                                slub_debug |= SLAB_STORE_USER;
 911                                break;
 912                        case 't' : case 'T' :
 913                                slub_debug |= SLAB_TRACE;
 914                                break;
 915                        default:
 916                                printk(KERN_ERR "slub_debug option '%c' "
 917                                        "unknown. skipped\n",*str);
 918                        }
 919        }
 920
 921        if (*str == ',')
 922                slub_debug_slabs = str + 1;
 923        return 1;
 924}
 925
 926__setup("slub_debug", setup_slub_debug);
 927
 928static void kmem_cache_open_debug_check(struct kmem_cache *s)
 929{
 930        /*
 931         * The page->offset field is only 16 bit wide. This is an offset
 932         * in units of words from the beginning of an object. If the slab
 933         * size is bigger then we cannot move the free pointer behind the
 934         * object anymore.
 935         *
 936         * On 32 bit platforms the limit is 256k. On 64bit platforms
 937         * the limit is 512k.
 938         *
 939         * Debugging or ctor may create a need to move the free
 940         * pointer. Fail if this happens.
 941         */
 942        if (s->objsize >= 65535 * sizeof(void *)) {
 943                BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
 944                                SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
 945                BUG_ON(s->ctor);
 946        }
 947        else
 948                /*
 949                 * Enable debugging if selected on the kernel commandline.
 950                 */
 951                if (slub_debug && (!slub_debug_slabs ||
 952                    strncmp(slub_debug_slabs, s->name,
 953                        strlen(slub_debug_slabs)) == 0))
 954                                s->flags |= slub_debug;
 955}
 956#else
 957static inline void setup_object_debug(struct kmem_cache *s,
 958                        struct page *page, void *object) {}
 959
 960static inline int alloc_debug_processing(struct kmem_cache *s,
 961        struct page *page, void *object, void *addr) { return 0; }
 962
 963static inline int free_debug_processing(struct kmem_cache *s,
 964        struct page *page, void *object, void *addr) { return 0; }
 965
 966static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
 967                        { return 1; }
 968static inline int check_object(struct kmem_cache *s, struct page *page,
 969                        void *object, int active) { return 1; }
 970static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
 971static inline void kmem_cache_open_debug_check(struct kmem_cache *s) {}
 972#define slub_debug 0
 973#endif
 974/*
 975 * Slab allocation and freeing
 976 */
 977static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 978{
 979        struct page * page;
 980        int pages = 1 << s->order;
 981
 982        if (s->order)
 983                flags |= __GFP_COMP;
 984
 985        if (s->flags & SLAB_CACHE_DMA)
 986                flags |= SLUB_DMA;
 987
 988        if (node == -1)
 989                page = alloc_pages(flags, s->order);
 990        else
 991                page = alloc_pages_node(node, flags, s->order);
 992
 993        if (!page)
 994                return NULL;
 995
 996        mod_zone_page_state(page_zone(page),
 997                (s->flags & SLAB_RECLAIM_ACCOUNT) ?
 998                NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
 999                pages);
1000
1001        return page;
1002}
1003
1004static void setup_object(struct kmem_cache *s, struct page *page,
1005                                void *object)
1006{
1007        setup_object_debug(s, page, object);
1008        if (unlikely(s->ctor))
1009                s->ctor(object, s, 0);
1010}
1011
1012static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1013{
1014        struct page *page;
1015        struct kmem_cache_node *n;
1016        void *start;
1017        void *end;
1018        void *last;
1019        void *p;
1020
1021        BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
1022
1023        if (flags & __GFP_WAIT)
1024                local_irq_enable();
1025
1026        page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
1027        if (!page)
1028                goto out;
1029
1030        n = get_node(s, page_to_nid(page));
1031        if (n)
1032                atomic_long_inc(&n->nr_slabs);
1033        page->offset = s->offset / sizeof(void *);
1034        page->slab = s;
1035        page->flags |= 1 << PG_slab;
1036        if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1037                        SLAB_STORE_USER | SLAB_TRACE))
1038                SetSlabDebug(page);
1039
1040        start = page_address(page);
1041        end = start + s->objects * s->size;
1042
1043        if (unlikely(s->flags & SLAB_POISON))
1044                memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1045
1046        last = start;
1047        for_each_object(p, s, start) {
1048                setup_object(s, page, last);
1049                set_freepointer(s, last, p);
1050                last = p;
1051        }
1052        setup_object(s, page, last);
1053        set_freepointer(s, last, NULL);
1054
1055        page->freelist = start;
1056        page->lockless_freelist = NULL;
1057        page->inuse = 0;
1058out:
1059        if (flags & __GFP_WAIT)
1060                local_irq_disable();
1061        return page;
1062}
1063
1064static void __free_slab(struct kmem_cache *s, struct page *page)
1065{
1066        int pages = 1 << s->order;
1067
1068        if (unlikely(SlabDebug(page))) {
1069                void *p;
1070
1071                slab_pad_check(s, page);
1072                for_each_object(p, s, page_address(page))
1073                        check_object(s, page, p, 0);
1074        }
1075
1076        mod_zone_page_state(page_zone(page),
1077                (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1078                NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1079                - pages);
1080
1081        page->mapping = NULL;
1082        __free_pages(page, s->order);
1083}
1084
1085static void rcu_free_slab(struct rcu_head *h)
1086{
1087        struct page *page;
1088
1089        page = container_of((struct list_head *)h, struct page, lru);
1090        __free_slab(page->slab, page);
1091}
1092
1093static void free_slab(struct kmem_cache *s, struct page *page)
1094{
1095        if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1096                /*
1097                 * RCU free overloads the RCU head over the LRU
1098                 */
1099                struct rcu_head *head = (void *)&page->lru;
1100
1101                call_rcu(head, rcu_free_slab);
1102        } else
1103                __free_slab(s, page);
1104}
1105
1106static void discard_slab(struct kmem_cache *s, struct page *page)
1107{
1108        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1109
1110        atomic_long_dec(&n->nr_slabs);
1111        reset_page_mapcount(page);
1112        ClearSlabDebug(page);
1113        __ClearPageSlab(page);
1114        free_slab(s, page);
1115}
1116
1117/*
1118 * Per slab locking using the pagelock
1119 */
1120static __always_inline void slab_lock(struct page *page)
1121{
1122        bit_spin_lock(PG_locked, &page->flags);
1123}
1124
1125static __always_inline void slab_unlock(struct page *page)
1126{
1127        bit_spin_unlock(PG_locked, &page->flags);
1128}
1129
1130static __always_inline int slab_trylock(struct page *page)
1131{
1132        int rc = 1;
1133
1134        rc = bit_spin_trylock(PG_locked, &page->flags);
1135        return rc;
1136}
1137
1138/*
1139 * Management of partially allocated slabs
1140 */
1141static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
1142{
1143        spin_lock(&n->list_lock);
1144        n->nr_partial++;
1145        list_add_tail(&page->lru, &n->partial);
1146        spin_unlock(&n->list_lock);
1147}
1148
1149static void add_partial(struct kmem_cache_node *n, struct page *page)
1150{
1151        spin_lock(&n->list_lock);
1152        n->nr_partial++;
1153        list_add(&page->lru, &n->partial);
1154        spin_unlock(&n->list_lock);
1155}
1156
1157static void remove_partial(struct kmem_cache *s,
1158                                                struct page *page)
1159{
1160        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1161
1162        spin_lock(&n->list_lock);
1163        list_del(&page->lru);
1164        n->nr_partial--;
1165        spin_unlock(&n->list_lock);
1166}
1167
1168/*
1169 * Lock slab and remove from the partial list.
1170 *
1171 * Must hold list_lock.
1172 */
1173static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1174{
1175        if (slab_trylock(page)) {
1176                list_del(&page->lru);
1177                n->nr_partial--;
1178                SetSlabFrozen(page);
1179                return 1;
1180        }
1181        return 0;
1182}
1183
1184/*
1185 * Try to allocate a partial slab from a specific node.
1186 */
1187static struct page *get_partial_node(struct kmem_cache_node *n)
1188{
1189        struct page *page;
1190
1191        /*
1192         * Racy check. If we mistakenly see no partial slabs then we
1193         * just allocate an empty slab. If we mistakenly try to get a
1194         * partial slab and there is none available then get_partials()
1195         * will return NULL.
1196         */
1197        if (!n || !n->nr_partial)
1198                return NULL;
1199
1200        spin_lock(&n->list_lock);
1201        list_for_each_entry(page, &n->partial, lru)
1202                if (lock_and_freeze_slab(n, page))
1203                        goto out;
1204        page = NULL;
1205out:
1206        spin_unlock(&n->list_lock);
1207        return page;
1208}
1209
1210/*
1211 * Get a page from somewhere. Search in increasing NUMA distances.
1212 */
1213static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1214{
1215#ifdef CONFIG_NUMA
1216        struct zonelist *zonelist;
1217        struct zone **z;
1218        struct page *page;
1219
1220        /*
1221         * The defrag ratio allows a configuration of the tradeoffs between
1222         * inter node defragmentation and node local allocations. A lower
1223         * defrag_ratio increases the tendency to do local allocations
1224         * instead of attempting to obtain partial slabs from other nodes.
1225         *
1226         * If the defrag_ratio is set to 0 then kmalloc() always
1227         * returns node local objects. If the ratio is higher then kmalloc()
1228         * may return off node objects because partial slabs are obtained
1229         * from other nodes and filled up.
1230         *
1231         * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1232         * defrag_ratio = 1000) then every (well almost) allocation will
1233         * first attempt to defrag slab caches on other nodes. This means
1234         * scanning over all nodes to look for partial slabs which may be
1235         * expensive if we do it every time we are trying to find a slab
1236         * with available objects.
1237         */
1238        if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1239                return NULL;
1240
1241        zonelist = &NODE_DATA(slab_node(current->mempolicy))
1242                                        ->node_zonelists[gfp_zone(flags)];
1243        for (z = zonelist->zones; *z; z++) {
1244                struct kmem_cache_node *n;
1245
1246                n = get_node(s, zone_to_nid(*z));
1247
1248                if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1249                                n->nr_partial > MIN_PARTIAL) {
1250                        page = get_partial_node(n);
1251                        if (page)
1252                                return page;
1253                }
1254        }
1255#endif
1256        return NULL;
1257}
1258
1259/*
1260 * Get a partial page, lock it and return it.
1261 */
1262static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1263{
1264        struct page *page;
1265        int searchnode = (node == -1) ? numa_node_id() : node;
1266
1267        page = get_partial_node(get_node(s, searchnode));
1268        if (page || (flags & __GFP_THISNODE))
1269                return page;
1270
1271        return get_any_partial(s, flags);
1272}
1273
1274/*
1275 * Move a page back to the lists.
1276 *
1277 * Must be called with the slab lock held.
1278 *
1279 * On exit the slab lock will have been dropped.
1280 */
1281static void unfreeze_slab(struct kmem_cache *s, struct page *page)
1282{
1283        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1284
1285        ClearSlabFrozen(page);
1286        if (page->inuse) {
1287
1288                if (page->freelist)
1289                        add_partial(n, page);
1290                else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1291                        add_full(n, page);
1292                slab_unlock(page);
1293
1294        } else {
1295                if (n->nr_partial < MIN_PARTIAL) {
1296                        /*
1297                         * Adding an empty slab to the partial slabs in order
1298                         * to avoid page allocator overhead. This slab needs
1299                         * to come after the other slabs with objects in
1300                         * order to fill them up. That way the size of the
1301                         * partial list stays small. kmem_cache_shrink can
1302                         * reclaim empty slabs from the partial list.
1303                         */
1304                        add_partial_tail(n, page);
1305                        slab_unlock(page);
1306                } else {
1307                        slab_unlock(page);
1308                        discard_slab(s, page);
1309                }
1310        }
1311}
1312
1313/*
1314 * Remove the cpu slab
1315 */
1316static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1317{
1318        /*
1319         * Merge cpu freelist into freelist. Typically we get here
1320         * because both freelists are empty. So this is unlikely
1321         * to occur.
1322         */
1323        while (unlikely(page->lockless_freelist)) {
1324                void **object;
1325
1326                /* Retrieve object from cpu_freelist */
1327                object = page->lockless_freelist;
1328                page->lockless_freelist = page->lockless_freelist[page->offset];
1329
1330                /* And put onto the regular freelist */
1331                object[page->offset] = page->freelist;
1332                page->freelist = object;
1333                page->inuse--;
1334        }
1335        s->cpu_slab[cpu] = NULL;
1336        unfreeze_slab(s, page);
1337}
1338
1339static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1340{
1341        slab_lock(page);
1342        deactivate_slab(s, page, cpu);
1343}
1344
1345/*
1346 * Flush cpu slab.
1347 * Called from IPI handler with interrupts disabled.
1348 */
1349static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1350{
1351        struct page *page = s->cpu_slab[cpu];
1352
1353        if (likely(page))
1354                flush_slab(s, page, cpu);
1355}
1356
1357static void flush_cpu_slab(void *d)
1358{
1359        struct kmem_cache *s = d;
1360        int cpu = smp_processor_id();
1361
1362        __flush_cpu_slab(s, cpu);
1363}
1364
1365static void flush_all(struct kmem_cache *s)
1366{
1367#ifdef CONFIG_SMP
1368        on_each_cpu(flush_cpu_slab, s, 1, 1);
1369#else
1370        unsigned long flags;
1371
1372        local_irq_save(flags);
1373        flush_cpu_slab(s);
1374        local_irq_restore(flags);
1375#endif
1376}
1377
1378/*
1379 * Slow path. The lockless freelist is empty or we need to perform
1380 * debugging duties.
1381 *
1382 * Interrupts are disabled.
1383 *
1384 * Processing is still very fast if new objects have been freed to the
1385 * regular freelist. In that case we simply take over the regular freelist
1386 * as the lockless freelist and zap the regular freelist.
1387 *
1388 * If that is not working then we fall back to the partial lists. We take the
1389 * first element of the freelist as the object to allocate now and move the
1390 * rest of the freelist to the lockless freelist.
1391 *
1392 * And if we were unable to get a new slab from the partial slab lists then
1393 * we need to allocate a new slab. This is slowest path since we may sleep.
1394 */
1395static void *__slab_alloc(struct kmem_cache *s,
1396                gfp_t gfpflags, int node, void *addr, struct page *page)
1397{
1398        void **object;
1399        int cpu = smp_processor_id();
1400
1401        if (!page)
1402                goto new_slab;
1403
1404        slab_lock(page);
1405        if (unlikely(node != -1 && page_to_nid(page) != node))
1406                goto another_slab;
1407load_freelist:
1408        object = page->freelist;
1409        if (unlikely(!object))
1410                goto another_slab;
1411        if (unlikely(SlabDebug(page)))
1412                goto debug;
1413
1414        object = page->freelist;
1415        page->lockless_freelist = object[page->offset];
1416        page->inuse = s->objects;
1417        page->freelist = NULL;
1418        slab_unlock(page);
1419        return object;
1420
1421another_slab:
1422        deactivate_slab(s, page, cpu);
1423
1424new_slab:
1425        page = get_partial(s, gfpflags, node);
1426        if (page) {
1427                s->cpu_slab[cpu] = page;
1428                goto load_freelist;
1429        }
1430
1431        page = new_slab(s, gfpflags, node);
1432        if (page) {
1433                cpu = smp_processor_id();
1434                if (s->cpu_slab[cpu])
1435                        flush_slab(s, s->cpu_slab[cpu], cpu);
1436                slab_lock(page);
1437                SetSlabFrozen(page);
1438                s->cpu_slab[cpu] = page;
1439                goto load_freelist;
1440        }
1441        return NULL;
1442debug:
1443        object = page->freelist;
1444        if (!alloc_debug_processing(s, page, object, addr))
1445                goto another_slab;
1446
1447        page->inuse++;
1448        page->freelist = object[page->offset];
1449        slab_unlock(page);
1450        return object;
1451}
1452
1453/*
1454 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1455 * have the fastpath folded into their functions. So no function call
1456 * overhead for requests that can be satisfied on the fastpath.
1457 *
1458 * The fastpath works by first checking if the lockless freelist can be used.
1459 * If not then __slab_alloc is called for slow processing.
1460 *
1461 * Otherwise we can simply pick the next object from the lockless free list.
1462 */
1463static void __always_inline *slab_alloc(struct kmem_cache *s,
1464                                gfp_t gfpflags, int node, void *addr)
1465{
1466        struct page *page;
1467        void **object;
1468        unsigned long flags;
1469
1470        local_irq_save(flags);
1471        page = s->cpu_slab[smp_processor_id()];
1472        if (unlikely(!page || !page->lockless_freelist ||
1473                        (node != -1 && page_to_nid(page) != node)))
1474
1475                object = __slab_alloc(s, gfpflags, node, addr, page);
1476
1477        else {
1478                object = page->lockless_freelist;
1479                page->lockless_freelist = object[page->offset];
1480        }
1481        local_irq_restore(flags);
1482        return object;
1483}
1484
1485void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1486{
1487        return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1488}
1489EXPORT_SYMBOL(kmem_cache_alloc);
1490
1491#ifdef CONFIG_NUMA
1492void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1493{
1494        return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1495}
1496EXPORT_SYMBOL(kmem_cache_alloc_node);
1497#endif
1498
1499/*
1500 * Slow patch handling. This may still be called frequently since objects
1501 * have a longer lifetime than the cpu slabs in most processing loads.
1502 *
1503 * So we still attempt to reduce cache line usage. Just take the slab
1504 * lock and free the item. If there is no additional partial page
1505 * handling required then we can return immediately.
1506 */
1507static void __slab_free(struct kmem_cache *s, struct page *page,
1508                                        void *x, void *addr)
1509{
1510        void *prior;
1511        void **object = (void *)x;
1512
1513        slab_lock(page);
1514
1515        if (unlikely(SlabDebug(page)))
1516                goto debug;
1517checks_ok:
1518        prior = object[page->offset] = page->freelist;
1519        page->freelist = object;
1520        page->inuse--;
1521
1522        if (unlikely(SlabFrozen(page)))
1523                goto out_unlock;
1524
1525        if (unlikely(!page->inuse))
1526                goto slab_empty;
1527
1528        /*
1529         * Objects left in the slab. If it
1530         * was not on the partial list before
1531         * then add it.
1532         */
1533        if (unlikely(!prior))
1534                add_partial(get_node(s, page_to_nid(page)), page);
1535
1536out_unlock:
1537        slab_unlock(page);
1538        return;
1539
1540slab_empty:
1541        if (prior)
1542                /*
1543                 * Slab still on the partial list.
1544                 */
1545                remove_partial(s, page);
1546
1547        slab_unlock(page);
1548        discard_slab(s, page);
1549        return;
1550
1551debug:
1552        if (!free_debug_processing(s, page, x, addr))
1553                goto out_unlock;
1554        goto checks_ok;
1555}
1556
1557/*
1558 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1559 * can perform fastpath freeing without additional function calls.
1560 *
1561 * The fastpath is only possible if we are freeing to the current cpu slab
1562 * of this processor. This typically the case if we have just allocated
1563 * the item before.
1564 *
1565 * If fastpath is not possible then fall back to __slab_free where we deal
1566 * with all sorts of special processing.
1567 */
1568static void __always_inline slab_free(struct kmem_cache *s,
1569                        struct page *page, void *x, void *addr)
1570{
1571        void **object = (void *)x;
1572        unsigned long flags;
1573
1574        local_irq_save(flags);
1575        if (likely(page == s->cpu_slab[smp_processor_id()] &&
1576                                                !SlabDebug(page))) {
1577                object[page->offset] = page->lockless_freelist;
1578                page->lockless_freelist = object;
1579        } else
1580                __slab_free(s, page, x, addr);
1581
1582        local_irq_restore(flags);
1583}
1584
1585void kmem_cache_free(struct kmem_cache *s, void *x)
1586{
1587        struct page *page;
1588
1589        page = virt_to_head_page(x);
1590
1591        slab_free(s, page, x, __builtin_return_address(0));
1592}
1593EXPORT_SYMBOL(kmem_cache_free);
1594
1595/* Figure out on which slab object the object resides */
1596static struct page *get_object_page(const void *x)
1597{
1598        struct page *page = virt_to_head_page(x);
1599
1600        if (!PageSlab(page))
1601                return NULL;
1602
1603        return page;
1604}
1605
1606/*
1607 * Object placement in a slab is made very easy because we always start at
1608 * offset 0. If we tune the size of the object to the alignment then we can
1609 * get the required alignment by putting one properly sized object after
1610 * another.
1611 *
1612 * Notice that the allocation order determines the sizes of the per cpu
1613 * caches. Each processor has always one slab available for allocations.
1614 * Increasing the allocation order reduces the number of times that slabs
1615 * must be moved on and off the partial lists and is therefore a factor in
1616 * locking overhead.
1617 */
1618
1619/*
1620 * Mininum / Maximum order of slab pages. This influences locking overhead
1621 * and slab fragmentation. A higher order reduces the number of partial slabs
1622 * and increases the number of allocations possible without having to
1623 * take the list_lock.
1624 */
1625static int slub_min_order;
1626static int slub_max_order = DEFAULT_MAX_ORDER;
1627static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1628
1629/*
1630 * Merge control. If this is set then no merging of slab caches will occur.
1631 * (Could be removed. This was introduced to pacify the merge skeptics.)
1632 */
1633static int slub_nomerge;
1634
1635/*
1636 * Calculate the order of allocation given an slab object size.
1637 *
1638 * The order of allocation has significant impact on performance and other
1639 * system components. Generally order 0 allocations should be preferred since
1640 * order 0 does not cause fragmentation in the page allocator. Larger objects
1641 * be problematic to put into order 0 slabs because there may be too much
1642 * unused space left. We go to a higher order if more than 1/8th of the slab
1643 * would be wasted.
1644 *
1645 * In order to reach satisfactory performance we must ensure that a minimum
1646 * number of objects is in one slab. Otherwise we may generate too much
1647 * activity on the partial lists which requires taking the list_lock. This is
1648 * less a concern for large slabs though which are rarely used.
1649 *
1650 * slub_max_order specifies the order where we begin to stop considering the
1651 * number of objects in a slab as critical. If we reach slub_max_order then
1652 * we try to keep the page order as low as possible. So we accept more waste
1653 * of space in favor of a small page order.
1654 *
1655 * Higher order allocations also allow the placement of more objects in a
1656 * slab and thereby reduce object handling overhead. If the user has
1657 * requested a higher mininum order then we start with that one instead of
1658 * the smallest order which will fit the object.
1659 */
1660static inline int slab_order(int size, int min_objects,
1661                                int max_order, int fract_leftover)
1662{
1663        int order;
1664        int rem;
1665
1666        for (order = max(slub_min_order,
1667                                fls(min_objects * size - 1) - PAGE_SHIFT);
1668                        order <= max_order; order++) {
1669
1670                unsigned long slab_size = PAGE_SIZE << order;
1671
1672                if (slab_size < min_objects * size)
1673                        continue;
1674
1675                rem = slab_size % size;
1676
1677                if (rem <= slab_size / fract_leftover)
1678                        break;
1679
1680        }
1681
1682        return order;
1683}
1684
1685static inline int calculate_order(int size)
1686{
1687        int order;
1688        int min_objects;
1689        int fraction;
1690
1691        /*
1692         * Attempt to find best configuration for a slab. This
1693         * works by first attempting to generate a layout with
1694         * the best configuration and backing off gradually.
1695         *
1696         * First we reduce the acceptable waste in a slab. Then
1697         * we reduce the minimum objects required in a slab.
1698         */
1699        min_objects = slub_min_objects;
1700        while (min_objects > 1) {
1701                fraction = 8;
1702                while (fraction >= 4) {
1703                        order = slab_order(size, min_objects,
1704                                                slub_max_order, fraction);
1705                        if (order <= slub_max_order)
1706                                return order;
1707                        fraction /= 2;
1708                }
1709                min_objects /= 2;
1710        }
1711
1712        /*
1713         * We were unable to place multiple objects in a slab. Now
1714         * lets see if we can place a single object there.
1715         */
1716        order = slab_order(size, 1, slub_max_order, 1);
1717        if (order <= slub_max_order)
1718                return order;
1719
1720        /*
1721         * Doh this slab cannot be placed using slub_max_order.
1722         */
1723        order = slab_order(size, 1, MAX_ORDER, 1);
1724        if (order <= MAX_ORDER)
1725                return order;
1726        return -ENOSYS;
1727}
1728
1729/*
1730 * Figure out what the alignment of the objects will be.
1731 */
1732static unsigned long calculate_alignment(unsigned long flags,
1733                unsigned long align, unsigned long size)
1734{
1735        /*
1736         * If the user wants hardware cache aligned objects then
1737         * follow that suggestion if the object is sufficiently
1738         * large.
1739         *
1740         * The hardware cache alignment cannot override the
1741         * specified alignment though. If that is greater
1742         * then use it.
1743         */
1744        if ((flags & SLAB_HWCACHE_ALIGN) &&
1745                        size > cache_line_size() / 2)
1746                return max_t(unsigned long, align, cache_line_size());
1747
1748        if (align < ARCH_SLAB_MINALIGN)
1749                return ARCH_SLAB_MINALIGN;
1750
1751        return ALIGN(align, sizeof(void *));
1752}
1753
1754static void init_kmem_cache_node(struct kmem_cache_node *n)
1755{
1756        n->nr_partial = 0;
1757        atomic_long_set(&n->nr_slabs, 0);
1758        spin_lock_init(&n->list_lock);
1759        INIT_LIST_HEAD(&n->partial);
1760        INIT_LIST_HEAD(&n->full);
1761}
1762
1763#ifdef CONFIG_NUMA
1764/*
1765 * No kmalloc_node yet so do it by hand. We know that this is the first
1766 * slab on the node for this slabcache. There are no concurrent accesses
1767 * possible.
1768 *
1769 * Note that this function only works on the kmalloc_node_cache
1770 * when allocating for the kmalloc_node_cache.
1771 */
1772static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1773                                                                int node)
1774{
1775        struct page *page;
1776        struct kmem_cache_node *n;
1777
1778        BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1779
1780        page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1781
1782        BUG_ON(!page);
1783        n = page->freelist;
1784        BUG_ON(!n);
1785        page->freelist = get_freepointer(kmalloc_caches, n);
1786        page->inuse++;
1787        kmalloc_caches->node[node] = n;
1788        setup_object_debug(kmalloc_caches, page, n);
1789        init_kmem_cache_node(n);
1790        atomic_long_inc(&n->nr_slabs);
1791        add_partial(n, page);
1792
1793        /*
1794         * new_slab() disables interupts. If we do not reenable interrupts here
1795         * then bootup would continue with interrupts disabled.
1796         */
1797        local_irq_enable();
1798        return n;
1799}
1800
1801static void free_kmem_cache_nodes(struct kmem_cache *s)
1802{
1803        int node;
1804
1805        for_each_online_node(node) {
1806                struct kmem_cache_node *n = s->node[node];
1807                if (n && n != &s->local_node)
1808                        kmem_cache_free(kmalloc_caches, n);
1809                s->node[node] = NULL;
1810        }
1811}
1812
1813static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1814{
1815        int node;
1816        int local_node;
1817
1818        if (slab_state >= UP)
1819                local_node = page_to_nid(virt_to_page(s));
1820        else
1821                local_node = 0;
1822
1823        for_each_online_node(node) {
1824                struct kmem_cache_node *n;
1825
1826                if (local_node == node)
1827                        n = &s->local_node;
1828                else {
1829                        if (slab_state == DOWN) {
1830                                n = early_kmem_cache_node_alloc(gfpflags,
1831                                                                node);
1832                                continue;
1833                        }
1834                        n = kmem_cache_alloc_node(kmalloc_caches,
1835                                                        gfpflags, node);
1836
1837                        if (!n) {
1838                                free_kmem_cache_nodes(s);
1839                                return 0;
1840                        }
1841
1842                }
1843                s->node[node] = n;
1844                init_kmem_cache_node(n);
1845        }
1846        return 1;
1847}
1848#else
1849static void free_kmem_cache_nodes(struct kmem_cache *s)
1850{
1851}
1852
1853static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1854{
1855        init_kmem_cache_node(&s->local_node);
1856        return 1;
1857}
1858#endif
1859
1860/*
1861 * calculate_sizes() determines the order and the distribution of data within
1862 * a slab object.
1863 */
1864static int calculate_sizes(struct kmem_cache *s)
1865{
1866        unsigned long flags = s->flags;
1867        unsigned long size = s->objsize;
1868        unsigned long align = s->align;
1869
1870        /*
1871         * Determine if we can poison the object itself. If the user of
1872         * the slab may touch the object after free or before allocation
1873         * then we should never poison the object itself.
1874         */
1875        if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1876                        !s->ctor)
1877                s->flags |= __OBJECT_POISON;
1878        else
1879                s->flags &= ~__OBJECT_POISON;
1880
1881        /*
1882         * Round up object size to the next word boundary. We can only
1883         * place the free pointer at word boundaries and this determines
1884         * the possible location of the free pointer.
1885         */
1886        size = ALIGN(size, sizeof(void *));
1887
1888#ifdef CONFIG_SLUB_DEBUG
1889        /*
1890         * If we are Redzoning then check if there is some space between the
1891         * end of the object and the free pointer. If not then add an
1892         * additional word to have some bytes to store Redzone information.
1893         */
1894        if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1895                size += sizeof(void *);
1896#endif
1897
1898        /*
1899         * With that we have determined the number of bytes in actual use
1900         * by the object. This is the potential offset to the free pointer.
1901         */
1902        s->inuse = size;
1903
1904        if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1905                s->ctor)) {
1906                /*
1907                 * Relocate free pointer after the object if it is not
1908                 * permitted to overwrite the first word of the object on
1909                 * kmem_cache_free.
1910                 *
1911                 * This is the case if we do RCU, have a constructor or
1912                 * destructor or are poisoning the objects.
1913                 */
1914                s->offset = size;
1915                size += sizeof(void *);
1916        }
1917
1918#ifdef CONFIG_SLUB_DEBUG
1919        if (flags & SLAB_STORE_USER)
1920                /*
1921                 * Need to store information about allocs and frees after
1922                 * the object.
1923                 */
1924                size += 2 * sizeof(struct track);
1925
1926        if (flags & SLAB_RED_ZONE)
1927                /*
1928                 * Add some empty padding so that we can catch
1929                 * overwrites from earlier objects rather than let
1930                 * tracking information or the free pointer be
1931                 * corrupted if an user writes before the start
1932                 * of the object.
1933                 */
1934                size += sizeof(void *);
1935#endif
1936
1937        /*
1938         * Determine the alignment based on various parameters that the
1939         * user specified and the dynamic determination of cache line size
1940         * on bootup.
1941         */
1942        align = calculate_alignment(flags, align, s->objsize);
1943
1944        /*
1945         * SLUB stores one object immediately after another beginning from
1946         * offset 0. In order to align the objects we have to simply size
1947         * each object to conform to the alignment.
1948         */
1949        size = ALIGN(size, align);
1950        s->size = size;
1951
1952        s->order = calculate_order(size);
1953        if (s->order < 0)
1954                return 0;
1955
1956        /*
1957         * Determine the number of objects per slab
1958         */
1959        s->objects = (PAGE_SIZE << s->order) / size;
1960
1961        /*
1962         * Verify that the number of objects is within permitted limits.
1963         * The page->inuse field is only 16 bit wide! So we cannot have
1964         * more than 64k objects per slab.
1965         */
1966        if (!s->objects || s->objects > 65535)
1967                return 0;
1968        return 1;
1969
1970}
1971
1972static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1973                const char *name, size_t size,
1974                size_t align, unsigned long flags,
1975                void (*ctor)(void *, struct kmem_cache *, unsigned long))
1976{
1977        memset(s, 0, kmem_size);
1978        s->name = name;
1979        s->ctor = ctor;
1980        s->objsize = size;
1981        s->flags = flags;
1982        s->align = align;
1983        kmem_cache_open_debug_check(s);
1984
1985        if (!calculate_sizes(s))
1986                goto error;
1987
1988        s->refcount = 1;
1989#ifdef CONFIG_NUMA
1990        s->defrag_ratio = 100;
1991#endif
1992
1993        if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1994                return 1;
1995error:
1996        if (flags & SLAB_PANIC)
1997                panic("Cannot create slab %s size=%lu realsize=%u "
1998                        "order=%u offset=%u flags=%lx\n",
1999                        s->name, (unsigned long)size, s->size, s->order,
2000                        s->offset, flags);
2001        return 0;
2002}
2003
2004/*
2005 * Check if a given pointer is valid
2006 */
2007int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2008{
2009        struct page * page;
2010
2011        page = get_object_page(object);
2012
2013        if (!page || s != page->slab)
2014                /* No slab or wrong slab */
2015                return 0;
2016
2017        if (!check_valid_pointer(s, page, object))
2018                return 0;
2019
2020        /*
2021         * We could also check if the object is on the slabs freelist.
2022         * But this would be too expensive and it seems that the main
2023         * purpose of kmem_ptr_valid is to check if the object belongs
2024         * to a certain slab.
2025         */
2026        return 1;
2027}
2028EXPORT_SYMBOL(kmem_ptr_validate);
2029
2030/*
2031 * Determine the size of a slab object
2032 */
2033unsigned int kmem_cache_size(struct kmem_cache *s)
2034{
2035        return s->objsize;
2036}
2037EXPORT_SYMBOL(kmem_cache_size);
2038
2039const char *kmem_cache_name(struct kmem_cache *s)
2040{
2041        return s->name;
2042}
2043EXPORT_SYMBOL(kmem_cache_name);
2044
2045/*
2046 * Attempt to free all slabs on a node. Return the number of slabs we
2047 * were unable to free.
2048 */
2049static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2050                        struct list_head *list)
2051{
2052        int slabs_inuse = 0;
2053        unsigned long flags;
2054        struct page *page, *h;
2055
2056        spin_lock_irqsave(&n->list_lock, flags);
2057        list_for_each_entry_safe(page, h, list, lru)
2058                if (!page->inuse) {
2059                        list_del(&page->lru);
2060                        discard_slab(s, page);
2061                } else
2062                        slabs_inuse++;
2063        spin_unlock_irqrestore(&n->list_lock, flags);
2064        return slabs_inuse;
2065}
2066
2067/*
2068 * Release all resources used by a slab cache.
2069 */
2070static int kmem_cache_close(struct kmem_cache *s)
2071{
2072        int node;
2073
2074        flush_all(s);
2075
2076        /* Attempt to free all objects */
2077        for_each_online_node(node) {
2078                struct kmem_cache_node *n = get_node(s, node);
2079
2080                n->nr_partial -= free_list(s, n, &n->partial);
2081                if (atomic_long_read(&n->nr_slabs))
2082                        return 1;
2083        }
2084        free_kmem_cache_nodes(s);
2085        return 0;
2086}
2087
2088/*
2089 * Close a cache and release the kmem_cache structure
2090 * (must be used for caches created using kmem_cache_create)
2091 */
2092void kmem_cache_destroy(struct kmem_cache *s)
2093{
2094        down_write(&slub_lock);
2095        s->refcount--;
2096        if (!s->refcount) {
2097                list_del(&s->list);
2098                if (kmem_cache_close(s))
2099                        WARN_ON(1);
2100                sysfs_slab_remove(s);
2101                kfree(s);
2102        }
2103        up_write(&slub_lock);
2104}
2105EXPORT_SYMBOL(kmem_cache_destroy);
2106
2107/********************************************************************
2108 *              Kmalloc subsystem
2109 *******************************************************************/
2110
2111struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
2112EXPORT_SYMBOL(kmalloc_caches);
2113
2114#ifdef CONFIG_ZONE_DMA
2115static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
2116#endif
2117
2118static int __init setup_slub_min_order(char *str)
2119{
2120        get_option (&str, &slub_min_order);
2121
2122        return 1;
2123}
2124
2125__setup("slub_min_order=", setup_slub_min_order);
2126
2127static int __init setup_slub_max_order(char *str)
2128{
2129        get_option (&str, &slub_max_order);
2130
2131        return 1;
2132}
2133
2134__setup("slub_max_order=", setup_slub_max_order);
2135
2136static int __init setup_slub_min_objects(char *str)
2137{
2138        get_option (&str, &slub_min_objects);
2139
2140        return 1;
2141}
2142
2143__setup("slub_min_objects=", setup_slub_min_objects);
2144
2145static int __init setup_slub_nomerge(char *str)
2146{
2147        slub_nomerge = 1;
2148        return 1;
2149}
2150
2151__setup("slub_nomerge", setup_slub_nomerge);
2152
2153static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2154                const char *name, int size, gfp_t gfp_flags)
2155{
2156        unsigned int flags = 0;
2157
2158        if (gfp_flags & SLUB_DMA)
2159                flags = SLAB_CACHE_DMA;
2160
2161        down_write(&slub_lock);
2162        if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2163                        flags, NULL))
2164                goto panic;
2165
2166        list_add(&s->list, &slab_caches);
2167        up_write(&slub_lock);
2168        if (sysfs_slab_add(s))
2169                goto panic;
2170        return s;
2171
2172panic:
2173        panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2174}
2175
2176static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2177{
2178        int index = kmalloc_index(size);
2179
2180        if (!index)
2181                return NULL;
2182
2183        /* Allocation too large? */
2184        BUG_ON(index < 0);
2185
2186#ifdef CONFIG_ZONE_DMA
2187        if ((flags & SLUB_DMA)) {
2188                struct kmem_cache *s;
2189                struct kmem_cache *x;
2190                char *text;
2191                size_t realsize;
2192
2193                s = kmalloc_caches_dma[index];
2194                if (s)
2195                        return s;
2196
2197                /* Dynamically create dma cache */
2198                x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2199                if (!x)
2200                        panic("Unable to allocate memory for dma cache\n");
2201
2202                if (index <= KMALLOC_SHIFT_HIGH)
2203                        realsize = 1 << index;
2204                else {
2205                        if (index == 1)
2206                                realsize = 96;
2207                        else
2208                                realsize = 192;
2209                }
2210
2211                text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2212                                (unsigned int)realsize);
2213                s = create_kmalloc_cache(x, text, realsize, flags);
2214                kmalloc_caches_dma[index] = s;
2215                return s;
2216        }
2217#endif
2218        return &kmalloc_caches[index];
2219}
2220
2221void *__kmalloc(size_t size, gfp_t flags)
2222{
2223        struct kmem_cache *s = get_slab(size, flags);
2224
2225        if (s)
2226                return slab_alloc(s, flags, -1, __builtin_return_address(0));
2227        return ZERO_SIZE_PTR;
2228}
2229EXPORT_SYMBOL(__kmalloc);
2230
2231#ifdef CONFIG_NUMA
2232void *__kmalloc_node(size_t size, gfp_t flags, int node)
2233{
2234        struct kmem_cache *s = get_slab(size, flags);
2235
2236        if (s)
2237                return slab_alloc(s, flags, node, __builtin_return_address(0));
2238        return ZERO_SIZE_PTR;
2239}
2240EXPORT_SYMBOL(__kmalloc_node);
2241#endif
2242
2243size_t ksize(const void *object)
2244{
2245        struct page *page;
2246        struct kmem_cache *s;
2247
2248        if (object == ZERO_SIZE_PTR)
2249                return 0;
2250
2251        page = get_object_page(object);
2252        BUG_ON(!page);
2253        s = page->slab;
2254        BUG_ON(!s);
2255
2256        /*
2257         * Debugging requires use of the padding between object
2258         * and whatever may come after it.
2259         */
2260        if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2261                return s->objsize;
2262
2263        /*
2264         * If we have the need to store the freelist pointer
2265         * back there or track user information then we can
2266         * only use the space before that information.
2267         */
2268        if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2269                return s->inuse;
2270
2271        /*
2272         * Else we can use all the padding etc for the allocation
2273         */
2274        return s->size;
2275}
2276EXPORT_SYMBOL(ksize);
2277
2278void kfree(const void *x)
2279{
2280        struct kmem_cache *s;
2281        struct page *page;
2282
2283        /*
2284         * This has to be an unsigned comparison. According to Linus
2285         * some gcc version treat a pointer as a signed entity. Then
2286         * this comparison would be true for all "negative" pointers
2287         * (which would cover the whole upper half of the address space).
2288         */
2289        if ((unsigned long)x <= (unsigned long)ZERO_SIZE_PTR)
2290                return;
2291
2292        page = virt_to_head_page(x);
2293        s = page->slab;
2294
2295        slab_free(s, page, (void *)x, __builtin_return_address(0));
2296}
2297EXPORT_SYMBOL(kfree);
2298
2299/*
2300 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2301 * the remaining slabs by the number of items in use. The slabs with the
2302 * most items in use come first. New allocations will then fill those up
2303 * and thus they can be removed from the partial lists.
2304 *
2305 * The slabs with the least items are placed last. This results in them
2306 * being allocated from last increasing the chance that the last objects
2307 * are freed in them.
2308 */
2309int kmem_cache_shrink(struct kmem_cache *s)
2310{
2311        int node;
2312        int i;
2313        struct kmem_cache_node *n;
2314        struct page *page;
2315        struct page *t;
2316        struct list_head *slabs_by_inuse =
2317                kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2318        unsigned long flags;
2319
2320        if (!slabs_by_inuse)
2321                return -ENOMEM;
2322
2323        flush_all(s);
2324        for_each_online_node(node) {
2325                n = get_node(s, node);
2326
2327                if (!n->nr_partial)
2328                        continue;
2329
2330                for (i = 0; i < s->objects; i++)
2331                        INIT_LIST_HEAD(slabs_by_inuse + i);
2332
2333                spin_lock_irqsave(&n->list_lock, flags);
2334
2335                /*
2336                 * Build lists indexed by the items in use in each slab.
2337                 *
2338                 * Note that concurrent frees may occur while we hold the
2339                 * list_lock. page->inuse here is the upper limit.
2340                 */
2341                list_for_each_entry_safe(page, t, &n->partial, lru) {
2342                        if (!page->inuse && slab_trylock(page)) {
2343                                /*
2344                                 * Must hold slab lock here because slab_free
2345                                 * may have freed the last object and be
2346                                 * waiting to release the slab.
2347                                 */
2348                                list_del(&page->lru);
2349                                n->nr_partial--;
2350                                slab_unlock(page);
2351                                discard_slab(s, page);
2352                        } else {
2353                                if (n->nr_partial > MAX_PARTIAL)
2354                                        list_move(&page->lru,
2355                                        slabs_by_inuse + page->inuse);
2356                        }
2357                }
2358
2359                if (n->nr_partial <= MAX_PARTIAL)
2360                        goto out;
2361
2362                /*
2363                 * Rebuild the partial list with the slabs filled up most
2364                 * first and the least used slabs at the end.
2365                 */
2366                for (i = s->objects - 1; i >= 0; i--)
2367                        list_splice(slabs_by_inuse + i, n->partial.prev);
2368
2369        out:
2370                spin_unlock_irqrestore(&n->list_lock, flags);
2371        }
2372
2373        kfree(slabs_by_inuse);
2374        return 0;
2375}
2376EXPORT_SYMBOL(kmem_cache_shrink);
2377
2378/**
2379 * krealloc - reallocate memory. The contents will remain unchanged.
2380 * @p: object to reallocate memory for.
2381 * @new_size: how many bytes of memory are required.
2382 * @flags: the type of memory to allocate.
2383 *
2384 * The contents of the object pointed to are preserved up to the
2385 * lesser of the new and old sizes.  If @p is %NULL, krealloc()
2386 * behaves exactly like kmalloc().  If @size is 0 and @p is not a
2387 * %NULL pointer, the object pointed to is freed.
2388 */
2389void *krealloc(const void *p, size_t new_size, gfp_t flags)
2390{
2391        void *ret;
2392        size_t ks;
2393
2394        if (unlikely(!p || p == ZERO_SIZE_PTR))
2395                return kmalloc(new_size, flags);
2396
2397        if (unlikely(!new_size)) {
2398                kfree(p);
2399                return ZERO_SIZE_PTR;
2400        }
2401
2402        ks = ksize(p);
2403        if (ks >= new_size)
2404                return (void *)p;
2405
2406        ret = kmalloc(new_size, flags);
2407        if (ret) {
2408                memcpy(ret, p, min(new_size, ks));
2409                kfree(p);
2410        }
2411        return ret;
2412}
2413EXPORT_SYMBOL(krealloc);
2414
2415/********************************************************************
2416 *                      Basic setup of slabs
2417 *******************************************************************/
2418
2419void __init kmem_cache_init(void)
2420{
2421        int i;
2422        int caches = 0;
2423
2424#ifdef CONFIG_NUMA
2425        /*
2426         * Must first have the slab cache available for the allocations of the
2427         * struct kmem_cache_node's. There is special bootstrap code in
2428         * kmem_cache_open for slab_state == DOWN.
2429         */
2430        create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2431                sizeof(struct kmem_cache_node), GFP_KERNEL);
2432        kmalloc_caches[0].refcount = -1;
2433        caches++;
2434#endif
2435
2436        /* Able to allocate the per node structures */
2437        slab_state = PARTIAL;
2438
2439        /* Caches that are not of the two-to-the-power-of size */
2440        if (KMALLOC_MIN_SIZE <= 64) {
2441                create_kmalloc_cache(&kmalloc_caches[1],
2442                                "kmalloc-96", 96, GFP_KERNEL);
2443                caches++;
2444        }
2445        if (KMALLOC_MIN_SIZE <= 128) {
2446                create_kmalloc_cache(&kmalloc_caches[2],
2447                                "kmalloc-192", 192, GFP_KERNEL);
2448                caches++;
2449        }
2450
2451        for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
2452                create_kmalloc_cache(&kmalloc_caches[i],
2453                        "kmalloc", 1 << i, GFP_KERNEL);
2454                caches++;
2455        }
2456
2457        slab_state = UP;
2458
2459        /* Provide the correct kmalloc names now that the caches are up */
2460        for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2461                kmalloc_caches[i]. name =
2462                        kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2463
2464#ifdef CONFIG_SMP
2465        register_cpu_notifier(&slab_notifier);
2466#endif
2467
2468        kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2469                                nr_cpu_ids * sizeof(struct page *);
2470
2471        printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2472                " CPUs=%d, Nodes=%d\n",
2473                caches, cache_line_size(),
2474                slub_min_order, slub_max_order, slub_min_objects,
2475                nr_cpu_ids, nr_node_ids);
2476}
2477
2478/*
2479 * Find a mergeable slab cache
2480 */
2481static int slab_unmergeable(struct kmem_cache *s)
2482{
2483        if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2484                return 1;
2485
2486        if (s->ctor)
2487                return 1;
2488
2489        /*
2490         * We may have set a slab to be unmergeable during bootstrap.
2491         */
2492        if (s->refcount < 0)
2493                return 1;
2494
2495        return 0;
2496}
2497
2498static struct kmem_cache *find_mergeable(size_t size,
2499                size_t align, unsigned long flags,
2500                void (*ctor)(void *, struct kmem_cache *, unsigned long))
2501{
2502        struct list_head *h;
2503
2504        if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2505                return NULL;
2506
2507        if (ctor)
2508                return NULL;
2509
2510        size = ALIGN(size, sizeof(void *));
2511        align = calculate_alignment(flags, align, size);
2512        size = ALIGN(size, align);
2513
2514        list_for_each(h, &slab_caches) {
2515                struct kmem_cache *s =
2516                        container_of(h, struct kmem_cache, list);
2517
2518                if (slab_unmergeable(s))
2519                        continue;
2520
2521                if (size > s->size)
2522                        continue;
2523
2524                if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2525                        (s->flags & SLUB_MERGE_SAME))
2526                                continue;
2527                /*
2528                 * Check if alignment is compatible.
2529                 * Courtesy of Adrian Drzewiecki
2530                 */
2531                if ((s->size & ~(align -1)) != s->size)
2532                        continue;
2533
2534                if (s->size - size >= sizeof(void *))
2535                        continue;
2536
2537                return s;
2538        }
2539        return NULL;
2540}
2541
2542struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2543                size_t align, unsigned long flags,
2544                void (*ctor)(void *, struct kmem_cache *, unsigned long),
2545                void (*dtor)(void *, struct kmem_cache *, unsigned long))
2546{
2547        struct kmem_cache *s;
2548
2549        BUG_ON(dtor);
2550        down_write(&slub_lock);
2551        s = find_mergeable(size, align, flags, ctor);
2552        if (s) {
2553                s->refcount++;
2554                /*
2555                 * Adjust the object sizes so that we clear
2556                 * the complete object on kzalloc.
2557                 */
2558                s->objsize = max(s->objsize, (int)size);
2559                s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2560                if (sysfs_slab_alias(s, name))
2561                        goto err;
2562        } else {
2563                s = kmalloc(kmem_size, GFP_KERNEL);
2564                if (s && kmem_cache_open(s, GFP_KERNEL, name,
2565                                size, align, flags, ctor)) {
2566                        if (sysfs_slab_add(s)) {
2567                                kfree(s);
2568                                goto err;
2569                        }
2570                        list_add(&s->list, &slab_caches);
2571                } else
2572                        kfree(s);
2573        }
2574        up_write(&slub_lock);
2575        return s;
2576
2577err:
2578        up_write(&slub_lock);
2579        if (flags & SLAB_PANIC)
2580                panic("Cannot create slabcache %s\n", name);
2581        else
2582                s = NULL;
2583        return s;
2584}
2585EXPORT_SYMBOL(kmem_cache_create);
2586
2587void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2588{
2589        void *x;
2590
2591        x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2592        if (x)
2593                memset(x, 0, s->objsize);
2594        return x;
2595}
2596EXPORT_SYMBOL(kmem_cache_zalloc);
2597
2598#ifdef CONFIG_SMP
2599static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2600{
2601        struct list_head *h;
2602
2603        down_read(&slub_lock);
2604        list_for_each(h, &slab_caches) {
2605                struct kmem_cache *s =
2606                        container_of(h, struct kmem_cache, list);
2607
2608                func(s, cpu);
2609        }
2610        up_read(&slub_lock);
2611}
2612
2613/*
2614 * Version of __flush_cpu_slab for the case that interrupts
2615 * are enabled.
2616 */
2617static void cpu_slab_flush(struct kmem_cache *s, int cpu)
2618{
2619        unsigned long flags;
2620
2621        local_irq_save(flags);
2622        __flush_cpu_slab(s, cpu);
2623        local_irq_restore(flags);
2624}
2625
2626/*
2627 * Use the cpu notifier to insure that the cpu slabs are flushed when
2628 * necessary.
2629 */
2630static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2631                unsigned long action, void *hcpu)
2632{
2633        long cpu = (long)hcpu;
2634
2635        switch (action) {
2636        case CPU_UP_CANCELED:
2637        case CPU_UP_CANCELED_FROZEN:
2638        case CPU_DEAD:
2639        case CPU_DEAD_FROZEN:
2640                for_all_slabs(cpu_slab_flush, cpu);
2641                break;
2642        default:
2643                break;
2644        }
2645        return NOTIFY_OK;
2646}
2647
2648static struct notifier_block __cpuinitdata slab_notifier =
2649        { &slab_cpuup_callback, NULL, 0 };
2650
2651#endif
2652
2653void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2654{
2655        struct kmem_cache *s = get_slab(size, gfpflags);
2656
2657        if (!s)
2658                return ZERO_SIZE_PTR;
2659
2660        return slab_alloc(s, gfpflags, -1, caller);
2661}
2662
2663void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2664                                        int node, void *caller)
2665{
2666        struct kmem_cache *s = get_slab(size, gfpflags);
2667
2668        if (!s)
2669                return ZERO_SIZE_PTR;
2670
2671        return slab_alloc(s, gfpflags, node, caller);
2672}
2673
2674#if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
2675static int validate_slab(struct kmem_cache *s, struct page *page)
2676{
2677        void *p;
2678        void *addr = page_address(page);
2679        DECLARE_BITMAP(map, s->objects);
2680
2681        if (!check_slab(s, page) ||
2682                        !on_freelist(s, page, NULL))
2683                return 0;
2684
2685        /* Now we know that a valid freelist exists */
2686        bitmap_zero(map, s->objects);
2687
2688        for_each_free_object(p, s, page->freelist) {
2689                set_bit(slab_index(p, s, addr), map);
2690                if (!check_object(s, page, p, 0))
2691                        return 0;
2692        }
2693
2694        for_each_object(p, s, addr)
2695                if (!test_bit(slab_index(p, s, addr), map))
2696                        if (!check_object(s, page, p, 1))
2697                                return 0;
2698        return 1;
2699}
2700
2701static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2702{
2703        if (slab_trylock(page)) {
2704                validate_slab(s, page);
2705                slab_unlock(page);
2706        } else
2707                printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2708                        s->name, page);
2709
2710        if (s->flags & DEBUG_DEFAULT_FLAGS) {
2711                if (!SlabDebug(page))
2712                        printk(KERN_ERR "SLUB %s: SlabDebug not set "
2713                                "on slab 0x%p\n", s->name, page);
2714        } else {
2715                if (SlabDebug(page))
2716                        printk(KERN_ERR "SLUB %s: SlabDebug set on "
2717                                "slab 0x%p\n", s->name, page);
2718        }
2719}
2720
2721static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2722{
2723        unsigned long count = 0;
2724        struct page *page;
2725        unsigned long flags;
2726
2727        spin_lock_irqsave(&n->list_lock, flags);
2728
2729        list_for_each_entry(page, &n->partial, lru) {
2730                validate_slab_slab(s, page);
2731                count++;
2732        }
2733        if (count != n->nr_partial)
2734                printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2735                        "counter=%ld\n", s->name, count, n->nr_partial);
2736
2737        if (!(s->flags & SLAB_STORE_USER))
2738                goto out;
2739
2740        list_for_each_entry(page, &n->full, lru) {
2741                validate_slab_slab(s, page);
2742                count++;
2743        }
2744        if (count != atomic_long_read(&n->nr_slabs))
2745                printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2746                        "counter=%ld\n", s->name, count,
2747                        atomic_long_read(&n->nr_slabs));
2748
2749out:
2750        spin_unlock_irqrestore(&n->list_lock, flags);
2751        return count;
2752}
2753
2754static unsigned long validate_slab_cache(struct kmem_cache *s)
2755{
2756        int node;
2757        unsigned long count = 0;
2758
2759        flush_all(s);
2760        for_each_online_node(node) {
2761                struct kmem_cache_node *n = get_node(s, node);
2762
2763                count += validate_slab_node(s, n);
2764        }
2765        return count;
2766}
2767
2768#ifdef SLUB_RESILIENCY_TEST
2769static void resiliency_test(void)
2770{
2771        u8 *p;
2772
2773        printk(KERN_ERR "SLUB resiliency testing\n");
2774        printk(KERN_ERR "-----------------------\n");
2775        printk(KERN_ERR "A. Corruption after allocation\n");
2776
2777        p = kzalloc(16, GFP_KERNEL);
2778        p[16] = 0x12;
2779        printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2780                        " 0x12->0x%p\n\n", p + 16);
2781
2782        validate_slab_cache(kmalloc_caches + 4);
2783
2784        /* Hmmm... The next two are dangerous */
2785        p = kzalloc(32, GFP_KERNEL);
2786        p[32 + sizeof(void *)] = 0x34;
2787        printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2788                        " 0x34 -> -0x%p\n", p);
2789        printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2790
2791        validate_slab_cache(kmalloc_caches + 5);
2792        p = kzalloc(64, GFP_KERNEL);
2793        p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2794        *p = 0x56;
2795        printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2796                                                                        p);
2797        printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2798        validate_slab_cache(kmalloc_caches + 6);
2799
2800        printk(KERN_ERR "\nB. Corruption after free\n");
2801        p = kzalloc(128, GFP_KERNEL);
2802        kfree(p);
2803        *p = 0x78;
2804        printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2805        validate_slab_cache(kmalloc_caches + 7);
2806
2807        p = kzalloc(256, GFP_KERNEL);
2808        kfree(p);
2809        p[50] = 0x9a;
2810        printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2811        validate_slab_cache(kmalloc_caches + 8);
2812
2813        p = kzalloc(512, GFP_KERNEL);
2814        kfree(p);
2815        p[512] = 0xab;
2816        printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2817        validate_slab_cache(kmalloc_caches + 9);
2818}
2819#else
2820static void resiliency_test(void) {};
2821#endif
2822
2823/*
2824 * Generate lists of code addresses where slabcache objects are allocated
2825 * and freed.
2826 */
2827
2828struct location {
2829        unsigned long count;
2830        void *addr;
2831        long long sum_time;
2832        long min_time;
2833        long max_time;
2834        long min_pid;
2835        long max_pid;
2836        cpumask_t cpus;
2837        nodemask_t nodes;
2838};
2839
2840struct loc_track {
2841        unsigned long max;
2842        unsigned long count;
2843        struct location *loc;
2844};
2845
2846static void free_loc_track(struct loc_track *t)
2847{
2848        if (t->max)
2849                free_pages((unsigned long)t->loc,
2850                        get_order(sizeof(struct location) * t->max));
2851}
2852
2853static int alloc_loc_track(struct loc_track *t, unsigned long max)
2854{
2855        struct location *l;
2856        int order;
2857
2858        if (!max)
2859                max = PAGE_SIZE / sizeof(struct location);
2860
2861        order = get_order(sizeof(struct location) * max);
2862
2863        l = (void *)__get_free_pages(GFP_ATOMIC, order);
2864
2865        if (!l)
2866                return 0;
2867
2868        if (t->count) {
2869                memcpy(l, t->loc, sizeof(struct location) * t->count);
2870                free_loc_track(t);
2871        }
2872        t->max = max;
2873        t->loc = l;
2874        return 1;
2875}
2876
2877static int add_location(struct loc_track *t, struct kmem_cache *s,
2878                                const struct track *track)
2879{
2880        long start, end, pos;
2881        struct location *l;
2882        void *caddr;
2883        unsigned long age = jiffies - track->when;
2884
2885        start = -1;
2886        end = t->count;
2887
2888        for ( ; ; ) {
2889                pos = start + (end - start + 1) / 2;
2890
2891                /*
2892                 * There is nothing at "end". If we end up there
2893                 * we need to add something to before end.
2894                 */
2895                if (pos == end)
2896                        break;
2897
2898                caddr = t->loc[pos].addr;
2899                if (track->addr == caddr) {
2900
2901                        l = &t->loc[pos];
2902                        l->count++;
2903                        if (track->when) {
2904                                l->sum_time += age;
2905                                if (age < l->min_time)
2906                                        l->min_time = age;
2907                                if (age > l->max_time)
2908                                        l->max_time = age;
2909
2910                                if (track->pid < l->min_pid)
2911                                        l->min_pid = track->pid;
2912                                if (track->pid > l->max_pid)
2913                                        l->max_pid = track->pid;
2914
2915                                cpu_set(track->cpu, l->cpus);
2916                        }
2917                        node_set(page_to_nid(virt_to_page(track)), l->nodes);
2918                        return 1;
2919                }
2920
2921                if (track->addr < caddr)
2922                        end = pos;
2923                else
2924                        start = pos;
2925        }
2926
2927        /*
2928         * Not found. Insert new tracking element.
2929         */
2930        if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2931                return 0;
2932
2933        l = t->loc + pos;
2934        if (pos < t->count)
2935                memmove(l + 1, l,
2936                        (t->count - pos) * sizeof(struct location));
2937        t->count++;
2938        l->count = 1;
2939        l->addr = track->addr;
2940        l->sum_time = age;
2941        l->min_time = age;
2942        l->max_time = age;
2943        l->min_pid = track->pid;
2944        l->max_pid = track->pid;
2945        cpus_clear(l->cpus);
2946        cpu_set(track->cpu, l->cpus);
2947        nodes_clear(l->nodes);
2948        node_set(page_to_nid(virt_to_page(track)), l->nodes);
2949        return 1;
2950}
2951
2952static void process_slab(struct loc_track *t, struct kmem_cache *s,
2953                struct page *page, enum track_item alloc)
2954{
2955        void *addr = page_address(page);
2956        DECLARE_BITMAP(map, s->objects);
2957        void *p;
2958
2959        bitmap_zero(map, s->objects);
2960        for_each_free_object(p, s, page->freelist)
2961                set_bit(slab_index(p, s, addr), map);
2962
2963        for_each_object(p, s, addr)
2964                if (!test_bit(slab_index(p, s, addr), map))
2965                        add_location(t, s, get_track(s, p, alloc));
2966}
2967
2968static int list_locations(struct kmem_cache *s, char *buf,
2969                                        enum track_item alloc)
2970{
2971        int n = 0;
2972        unsigned long i;
2973        struct loc_track t;
2974        int node;
2975
2976        t.count = 0;
2977        t.max = 0;
2978
2979        /* Push back cpu slabs */
2980        flush_all(s);
2981
2982        for_each_online_node(node) {
2983                struct kmem_cache_node *n = get_node(s, node);
2984                unsigned long flags;
2985                struct page *page;
2986
2987                if (!atomic_read(&n->nr_slabs))
2988                        continue;
2989
2990                spin_lock_irqsave(&n->list_lock, flags);
2991                list_for_each_entry(page, &n->partial, lru)
2992                        process_slab(&t, s, page, alloc);
2993                list_for_each_entry(page, &n->full, lru)
2994                        process_slab(&t, s, page, alloc);
2995                spin_unlock_irqrestore(&n->list_lock, flags);
2996        }
2997
2998        for (i = 0; i < t.count; i++) {
2999                struct location *l = &t.loc[i];
3000
3001                if (n > PAGE_SIZE - 100)
3002                        break;
3003                n += sprintf(buf + n, "%7ld ", l->count);
3004
3005                if (l->addr)
3006                        n += sprint_symbol(buf + n, (unsigned long)l->addr);
3007                else
3008                        n += sprintf(buf + n, "<not-available>");
3009
3010                if (l->sum_time != l->min_time) {
3011                        unsigned long remainder;
3012
3013                        n += sprintf(buf + n, " age=%ld/%ld/%ld",
3014                        l->min_time,
3015                        div_long_long_rem(l->sum_time, l->count, &remainder),
3016                        l->max_time);
3017                } else
3018                        n += sprintf(buf + n, " age=%ld",
3019                                l->min_time);
3020
3021                if (l->min_pid != l->max_pid)
3022                        n += sprintf(buf + n, " pid=%ld-%ld",
3023                                l->min_pid, l->max_pid);
3024                else
3025                        n += sprintf(buf + n, " pid=%ld",
3026                                l->min_pid);
3027
3028                if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3029                                n < PAGE_SIZE - 60) {
3030                        n += sprintf(buf + n, " cpus=");
3031                        n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3032                                        l->cpus);
3033                }
3034
3035                if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3036                                n < PAGE_SIZE - 60) {
3037                        n += sprintf(buf + n, " nodes=");
3038                        n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
3039                                        l->nodes);
3040                }
3041
3042                n += sprintf(buf + n, "\n");
3043        }
3044
3045        free_loc_track(&t);
3046        if (!t.count)
3047                n += sprintf(buf, "No data\n");
3048        return n;
3049}
3050
3051static unsigned long count_partial(struct kmem_cache_node *n)
3052{
3053        unsigned long flags;
3054        unsigned long x = 0;
3055        struct page *page;
3056
3057        spin_lock_irqsave(&n->list_lock, flags);
3058        list_for_each_entry(page, &n->partial, lru)
3059                x += page->inuse;
3060        spin_unlock_irqrestore(&n->list_lock, flags);
3061        return x;
3062}
3063
3064enum slab_stat_type {
3065        SL_FULL,
3066        SL_PARTIAL,
3067        SL_CPU,
3068        SL_OBJECTS
3069};
3070
3071#define SO_FULL         (1 << SL_FULL)
3072#define SO_PARTIAL      (1 << SL_PARTIAL)
3073#define SO_CPU          (1 << SL_CPU)
3074#define SO_OBJECTS      (1 << SL_OBJECTS)
3075
3076static unsigned long slab_objects(struct kmem_cache *s,
3077                        char *buf, unsigned long flags)
3078{
3079        unsigned long total = 0;
3080        int cpu;
3081        int node;
3082        int x;
3083        unsigned long *nodes;
3084        unsigned long *per_cpu;
3085
3086        nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3087        per_cpu = nodes + nr_node_ids;
3088
3089        for_each_possible_cpu(cpu) {
3090                struct page *page = s->cpu_slab[cpu];
3091                int node;
3092
3093                if (page) {
3094                        node = page_to_nid(page);
3095                        if (flags & SO_CPU) {
3096                                int x = 0;
3097
3098                                if (flags & SO_OBJECTS)
3099                                        x = page->inuse;
3100                                else
3101                                        x = 1;
3102                                total += x;
3103                                nodes[node] += x;
3104                        }
3105                        per_cpu[node]++;
3106                }
3107        }
3108
3109        for_each_online_node(node) {
3110                struct kmem_cache_node *n = get_node(s, node);
3111
3112                if (flags & SO_PARTIAL) {
3113                        if (flags & SO_OBJECTS)
3114                                x = count_partial(n);
3115                        else
3116                                x = n->nr_partial;
3117                        total += x;
3118                        nodes[node] += x;
3119                }
3120
3121                if (flags & SO_FULL) {
3122                        int full_slabs = atomic_read(&n->nr_slabs)
3123                                        - per_cpu[node]
3124                                        - n->nr_partial;
3125
3126                        if (flags & SO_OBJECTS)
3127                                x = full_slabs * s->objects;
3128                        else
3129                                x = full_slabs;
3130                        total += x;
3131                        nodes[node] += x;
3132                }
3133        }
3134
3135        x = sprintf(buf, "%lu", total);
3136#ifdef CONFIG_NUMA
3137        for_each_online_node(node)
3138                if (nodes[node])
3139                        x += sprintf(buf + x, " N%d=%lu",
3140                                        node, nodes[node]);
3141#endif
3142        kfree(nodes);
3143        return x + sprintf(buf + x, "\n");
3144}
3145
3146static int any_slab_objects(struct kmem_cache *s)
3147{
3148        int node;
3149        int cpu;
3150
3151        for_each_possible_cpu(cpu)
3152                if (s->cpu_slab[cpu])
3153                        return 1;
3154
3155        for_each_node(node) {
3156                struct kmem_cache_node *n = get_node(s, node);
3157
3158                if (n->nr_partial || atomic_read(&n->nr_slabs))
3159                        return 1;
3160        }
3161        return 0;
3162}
3163
3164#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3165#define to_slab(n) container_of(n, struct kmem_cache, kobj);
3166
3167struct slab_attribute {
3168        struct attribute attr;
3169        ssize_t (*show)(struct kmem_cache *s, char *buf);
3170        ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3171};
3172
3173#define SLAB_ATTR_RO(_name) \
3174        static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3175
3176#define SLAB_ATTR(_name) \
3177        static struct slab_attribute _name##_attr =  \
3178        __ATTR(_name, 0644, _name##_show, _name##_store)
3179
3180static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3181{
3182        return sprintf(buf, "%d\n", s->size);
3183}
3184SLAB_ATTR_RO(slab_size);
3185
3186static ssize_t align_show(struct kmem_cache *s, char *buf)
3187{
3188        return sprintf(buf, "%d\n", s->align);
3189}
3190SLAB_ATTR_RO(align);
3191
3192static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3193{
3194        return sprintf(buf, "%d\n", s->objsize);
3195}
3196SLAB_ATTR_RO(object_size);
3197
3198static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3199{
3200        return sprintf(buf, "%d\n", s->objects);
3201}
3202SLAB_ATTR_RO(objs_per_slab);
3203
3204static ssize_t order_show(struct kmem_cache *s, char *buf)
3205{
3206        return sprintf(buf, "%d\n", s->order);
3207}
3208SLAB_ATTR_RO(order);
3209
3210static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3211{
3212        if (s->ctor) {
3213                int n = sprint_symbol(buf, (unsigned long)s->ctor);
3214
3215                return n + sprintf(buf + n, "\n");
3216        }
3217        return 0;
3218}
3219SLAB_ATTR_RO(ctor);
3220
3221static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3222{
3223        return sprintf(buf, "%d\n", s->refcount - 1);
3224}
3225SLAB_ATTR_RO(aliases);
3226
3227static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3228{
3229        return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3230}
3231SLAB_ATTR_RO(slabs);
3232
3233static ssize_t partial_show(struct kmem_cache *s, char *buf)
3234{
3235        return slab_objects(s, buf, SO_PARTIAL);
3236}
3237SLAB_ATTR_RO(partial);
3238
3239static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3240{
3241        return slab_objects(s, buf, SO_CPU);
3242}
3243SLAB_ATTR_RO(cpu_slabs);
3244
3245static ssize_t objects_show(struct kmem_cache *s, char *buf)
3246{
3247        return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3248}
3249SLAB_ATTR_RO(objects);
3250
3251static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3252{
3253        return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3254}
3255
3256static ssize_t sanity_checks_store(struct kmem_cache *s,
3257                                const char *buf, size_t length)
3258{
3259        s->flags &= ~SLAB_DEBUG_FREE;
3260        if (buf[0] == '1')
3261                s->flags |= SLAB_DEBUG_FREE;
3262        return length;
3263}
3264SLAB_ATTR(sanity_checks);
3265
3266static ssize_t trace_show(struct kmem_cache *s, char *buf)
3267{
3268        return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3269}
3270
3271static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3272                                                        size_t length)
3273{
3274        s->flags &= ~SLAB_TRACE;
3275        if (buf[0] == '1')
3276                s->flags |= SLAB_TRACE;
3277        return length;
3278}
3279SLAB_ATTR(trace);
3280
3281static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3282{
3283        return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3284}
3285
3286static ssize_t reclaim_account_store(struct kmem_cache *s,
3287                                const char *buf, size_t length)
3288{
3289        s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3290        if (buf[0] == '1')
3291                s->flags |= SLAB_RECLAIM_ACCOUNT;
3292        return length;
3293}
3294SLAB_ATTR(reclaim_account);
3295
3296static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3297{
3298        return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3299}
3300SLAB_ATTR_RO(hwcache_align);
3301
3302#ifdef CONFIG_ZONE_DMA
3303static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3304{
3305        return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3306}
3307SLAB_ATTR_RO(cache_dma);
3308#endif
3309
3310static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3311{
3312        return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3313}
3314SLAB_ATTR_RO(destroy_by_rcu);
3315
3316static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3317{
3318        return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3319}
3320
3321static ssize_t red_zone_store(struct kmem_cache *s,
3322                                const char *buf, size_t length)
3323{
3324        if (any_slab_objects(s))
3325                return -EBUSY;
3326
3327        s->flags &= ~SLAB_RED_ZONE;
3328        if (buf[0] == '1')
3329                s->flags |= SLAB_RED_ZONE;
3330        calculate_sizes(s);
3331        return length;
3332}
3333SLAB_ATTR(red_zone);
3334
3335static ssize_t poison_show(struct kmem_cache *s, char *buf)
3336{
3337        return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3338}
3339
3340static ssize_t poison_store(struct kmem_cache *s,
3341                                const char *buf, size_t length)
3342{
3343        if (any_slab_objects(s))
3344                return -EBUSY;
3345
3346        s->flags &= ~SLAB_POISON;
3347        if (buf[0] == '1')
3348                s->flags |= SLAB_POISON;
3349        calculate_sizes(s);
3350        return length;
3351}
3352SLAB_ATTR(poison);
3353
3354static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3355{
3356        return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3357}
3358
3359static ssize_t store_user_store(struct kmem_cache *s,
3360                                const char *buf, size_t length)
3361{
3362        if (any_slab_objects(s))
3363                return -EBUSY;
3364
3365        s->flags &= ~SLAB_STORE_USER;
3366        if (buf[0] == '1')
3367                s->flags |= SLAB_STORE_USER;
3368        calculate_sizes(s);
3369        return length;
3370}
3371SLAB_ATTR(store_user);
3372
3373static ssize_t validate_show(struct kmem_cache *s, char *buf)
3374{
3375        return 0;
3376}
3377
3378static ssize_t validate_store(struct kmem_cache *s,
3379                        const char *buf, size_t length)
3380{
3381        if (buf[0] == '1')
3382                validate_slab_cache(s);
3383        else
3384                return -EINVAL;
3385        return length;
3386}
3387SLAB_ATTR(validate);
3388
3389static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3390{
3391        return 0;
3392}
3393
3394static ssize_t shrink_store(struct kmem_cache *s,
3395                        const char *buf, size_t length)
3396{
3397        if (buf[0] == '1') {
3398                int rc = kmem_cache_shrink(s);
3399
3400                if (rc)
3401                        return rc;
3402        } else
3403                return -EINVAL;
3404        return length;
3405}
3406SLAB_ATTR(shrink);
3407
3408static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3409{
3410        if (!(s->flags & SLAB_STORE_USER))
3411                return -ENOSYS;
3412        return list_locations(s, buf, TRACK_ALLOC);
3413}
3414SLAB_ATTR_RO(alloc_calls);
3415
3416static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3417{
3418        if (!(s->flags & SLAB_STORE_USER))
3419                return -ENOSYS;
3420        return list_locations(s, buf, TRACK_FREE);
3421}
3422SLAB_ATTR_RO(free_calls);
3423
3424#ifdef CONFIG_NUMA
3425static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3426{
3427        return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3428}
3429
3430static ssize_t defrag_ratio_store(struct kmem_cache *s,
3431                                const char *buf, size_t length)
3432{
3433        int n = simple_strtoul(buf, NULL, 10);
3434
3435        if (n < 100)
3436                s->defrag_ratio = n * 10;
3437        return length;
3438}
3439SLAB_ATTR(defrag_ratio);
3440#endif
3441
3442static struct attribute * slab_attrs[] = {
3443        &slab_size_attr.attr,
3444        &object_size_attr.attr,
3445        &objs_per_slab_attr.attr,
3446        &order_attr.attr,
3447        &objects_attr.attr,
3448        &slabs_attr.attr,
3449        &partial_attr.attr,
3450        &cpu_slabs_attr.attr,
3451        &ctor_attr.attr,
3452        &aliases_attr.attr,
3453        &align_attr.attr,
3454        &sanity_checks_attr.attr,
3455        &trace_attr.attr,
3456        &hwcache_align_attr.attr,
3457        &reclaim_account_attr.attr,
3458        &destroy_by_rcu_attr.attr,
3459        &red_zone_attr.attr,
3460        &poison_attr.attr,
3461        &store_user_attr.attr,
3462        &validate_attr.attr,
3463        &shrink_attr.attr,
3464        &alloc_calls_attr.attr,
3465        &free_calls_attr.attr,
3466#ifdef CONFIG_ZONE_DMA
3467        &cache_dma_attr.attr,
3468#endif
3469#ifdef CONFIG_NUMA
3470        &defrag_ratio_attr.attr,
3471#endif
3472        NULL
3473};
3474
3475static struct attribute_group slab_attr_group = {
3476        .attrs = slab_attrs,
3477};
3478
3479static ssize_t slab_attr_show(struct kobject *kobj,
3480                                struct attribute *attr,
3481                                char *buf)
3482{
3483        struct slab_attribute *attribute;
3484        struct kmem_cache *s;
3485        int err;
3486
3487        attribute = to_slab_attr(attr);
3488        s = to_slab(kobj);
3489
3490        if (!attribute->show)
3491                return -EIO;
3492
3493        err = attribute->show(s, buf);
3494
3495        return err;
3496}
3497
3498static ssize_t slab_attr_store(struct kobject *kobj,
3499                                struct attribute *attr,
3500                                const char *buf, size_t len)
3501{
3502        struct slab_attribute *attribute;
3503        struct kmem_cache *s;
3504        int err;
3505
3506        attribute = to_slab_attr(attr);
3507        s = to_slab(kobj);
3508
3509        if (!attribute->store)
3510                return -EIO;
3511
3512        err = attribute->store(s, buf, len);
3513
3514        return err;
3515}
3516
3517static struct sysfs_ops slab_sysfs_ops = {
3518        .show = slab_attr_show,
3519        .store = slab_attr_store,
3520};
3521
3522static struct kobj_type slab_ktype = {
3523        .sysfs_ops = &slab_sysfs_ops,
3524};
3525
3526static int uevent_filter(struct kset *kset, struct kobject *kobj)
3527{
3528        struct kobj_type *ktype = get_ktype(kobj);
3529
3530        if (ktype == &slab_ktype)
3531                return 1;
3532        return 0;
3533}
3534
3535static struct kset_uevent_ops slab_uevent_ops = {
3536        .filter = uevent_filter,
3537};
3538
3539decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3540
3541#define ID_STR_LENGTH 64
3542
3543/* Create a unique string id for a slab cache:
3544 * format
3545 * :[flags-]size:[memory address of kmemcache]
3546 */
3547static char *create_unique_id(struct kmem_cache *s)
3548{
3549        char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3550        char *p = name;
3551
3552        BUG_ON(!name);
3553
3554        *p++ = ':';
3555        /*
3556         * First flags affecting slabcache operations. We will only
3557         * get here for aliasable slabs so we do not need to support
3558         * too many flags. The flags here must cover all flags that
3559         * are matched during merging to guarantee that the id is
3560         * unique.
3561         */
3562        if (s->flags & SLAB_CACHE_DMA)
3563                *p++ = 'd';
3564        if (s->flags & SLAB_RECLAIM_ACCOUNT)
3565                *p++ = 'a';
3566        if (s->flags & SLAB_DEBUG_FREE)
3567                *p++ = 'F';
3568        if (p != name + 1)
3569                *p++ = '-';
3570        p += sprintf(p, "%07d", s->size);
3571        BUG_ON(p > name + ID_STR_LENGTH - 1);
3572        return name;
3573}
3574
3575static int sysfs_slab_add(struct kmem_cache *s)
3576{
3577        int err;
3578        const char *name;
3579        int unmergeable;
3580
3581        if (slab_state < SYSFS)
3582                /* Defer until later */
3583                return 0;
3584
3585        unmergeable = slab_unmergeable(s);
3586        if (unmergeable) {
3587                /*
3588                 * Slabcache can never be merged so we can use the name proper.
3589                 * This is typically the case for debug situations. In that
3590                 * case we can catch duplicate names easily.
3591                 */
3592                sysfs_remove_link(&slab_subsys.kobj, s->name);
3593                name = s->name;
3594        } else {
3595                /*
3596                 * Create a unique name for the slab as a target
3597                 * for the symlinks.
3598                 */
3599                name = create_unique_id(s);
3600        }
3601
3602        kobj_set_kset_s(s, slab_subsys);
3603        kobject_set_name(&s->kobj, name);
3604        kobject_init(&s->kobj);
3605        err = kobject_add(&s->kobj);
3606        if (err)
3607                return err;
3608
3609        err = sysfs_create_group(&s->kobj, &slab_attr_group);
3610        if (err)
3611                return err;
3612        kobject_uevent(&s->kobj, KOBJ_ADD);
3613        if (!unmergeable) {
3614                /* Setup first alias */
3615                sysfs_slab_alias(s, s->name);
3616                kfree(name);
3617        }
3618        return 0;
3619}
3620
3621static void sysfs_slab_remove(struct kmem_cache *s)
3622{
3623        kobject_uevent(&s->kobj, KOBJ_REMOVE);
3624        kobject_del(&s->kobj);
3625}
3626
3627/*
3628 * Need to buffer aliases during bootup until sysfs becomes
3629 * available lest we loose that information.
3630 */
3631struct saved_alias {
3632        struct kmem_cache *s;
3633        const char *name;
3634        struct saved_alias *next;
3635};
3636
3637struct saved_alias *alias_list;
3638
3639static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3640{
3641        struct saved_alias *al;
3642
3643        if (slab_state == SYSFS) {
3644                /*
3645                 * If we have a leftover link then remove it.
3646                 */
3647                sysfs_remove_link(&slab_subsys.kobj, name);
3648                return sysfs_create_link(&slab_subsys.kobj,
3649                                                &s->kobj, name);
3650        }
3651
3652        al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3653        if (!al)
3654                return -ENOMEM;
3655
3656        al->s = s;
3657        al->name = name;
3658        al->next = alias_list;
3659        alias_list = al;
3660        return 0;
3661}
3662
3663static int __init slab_sysfs_init(void)
3664{
3665        struct list_head *h;
3666        int err;
3667
3668        err = subsystem_register(&slab_subsys);
3669        if (err) {
3670                printk(KERN_ERR "Cannot register slab subsystem.\n");
3671                return -ENOSYS;
3672        }
3673
3674        slab_state = SYSFS;
3675
3676        list_for_each(h, &slab_caches) {
3677                struct kmem_cache *s =
3678                        container_of(h, struct kmem_cache, list);
3679
3680                err = sysfs_slab_add(s);
3681                BUG_ON(err);
3682        }
3683
3684        while (alias_list) {
3685                struct saved_alias *al = alias_list;
3686
3687                alias_list = alias_list->next;
3688                err = sysfs_slab_alias(al->s, al->name);
3689                BUG_ON(err);
3690                kfree(al);
3691        }
3692
3693        resiliency_test();
3694        return 0;
3695}
3696
3697__initcall(slab_sysfs_init);
3698#endif
3699
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.