2 * SLUB: A slab allocator that limits cache line use instead of queuing
3 * objects in per cpu and per node lists.
5 * The allocator synchronizes using per slab locks and only
6 * uses a centralized lock to manage a pool of partial slabs.
8 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
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>
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.
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).
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
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.
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.
66 * SLUB assigns one slab for allocation to each processor.
67 * Allocations only occur from these slabs called cpu slabs.
69 * Slabs with free elements are kept on a partial list.
70 * There is no list for full slabs. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * Otherwise there is no need to track full slabs unless we have to
73 * track full slabs for debugging purposes.
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.
79 * Overloading of page flags that are otherwise used for LRU management.
81 * PageActive The slab is used as a cpu cache. Allocations
82 * may be performed from the slab. The slab is not
83 * on any slab list and cannot be moved onto one.
85 * PageError Slab requires special handling due to debug
86 * options set. This moves slab handling out of
91 * Issues still to be resolved:
93 * - The per cpu array is updated for each new slab and and is a remote
94 * cacheline for most nodes. This could become a bouncing cacheline given
95 * enough frequent updates. There are 16 pointers in a cacheline.so at
96 * max 16 cpus could compete. Likely okay.
98 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
100 * - Support DEBUG_SLAB_LEAK. Trouble is we do not know where the full
103 * - SLAB_DEBUG_INITIAL is not supported but I have never seen a use of
106 * - Variable sizing of the per node arrays
109 /* Enable to test recovery from slab corruption on boot */
110 #undef SLUB_RESILIENCY_TEST
115 * Small page size. Make sure that we do not fragment memory
117 #define DEFAULT_MAX_ORDER 1
118 #define DEFAULT_MIN_OBJECTS 4
123 * Large page machines are customarily able to handle larger
126 #define DEFAULT_MAX_ORDER 2
127 #define DEFAULT_MIN_OBJECTS 8
132 * Flags from the regular SLAB that SLUB does not support:
134 #define SLUB_UNIMPLEMENTED (SLAB_DEBUG_INITIAL)
136 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
137 SLAB_POISON | SLAB_STORE_USER)
139 * Set of flags that will prevent slab merging
141 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
142 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
144 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
147 #ifndef ARCH_KMALLOC_MINALIGN
148 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
151 #ifndef ARCH_SLAB_MINALIGN
152 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
155 /* Internal SLUB flags */
156 #define __OBJECT_POISON 0x80000000 /* Poison object */
158 static int kmem_size = sizeof(struct kmem_cache);
161 static struct notifier_block slab_notifier;
165 DOWN, /* No slab functionality available */
166 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
167 UP, /* Everything works */
171 /* A list of all slab caches on the system */
172 static DECLARE_RWSEM(slub_lock);
173 LIST_HEAD(slab_caches);
176 static int sysfs_slab_add(struct kmem_cache *);
177 static int sysfs_slab_alias(struct kmem_cache *, const char *);
178 static void sysfs_slab_remove(struct kmem_cache *);
180 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
181 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
182 static void sysfs_slab_remove(struct kmem_cache *s) {}
185 /********************************************************************
186 * Core slab cache functions
187 *******************************************************************/
189 int slab_is_available(void)
191 return slab_state >= UP;
194 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
197 return s->node[node];
199 return &s->local_node;
206 static void print_section(char *text, u8 *addr, unsigned int length)
214 for (i = 0; i < length; i++) {
216 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
219 printk(" %02x", addr[i]);
221 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
223 printk(" %s\n",ascii);
234 printk(" %s\n", ascii);
239 * Slow version of get and set free pointer.
241 * This requires touching the cache lines of kmem_cache.
242 * The offset can also be obtained from the page. In that
243 * case it is in the cacheline that we already need to touch.
245 static void *get_freepointer(struct kmem_cache *s, void *object)
247 return *(void **)(object + s->offset);
250 static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
252 *(void **)(object + s->offset) = fp;
256 * Tracking user of a slab.
259 void *addr; /* Called from address */
260 int cpu; /* Was running on cpu */
261 int pid; /* Pid context */
262 unsigned long when; /* When did the operation occur */
265 enum track_item { TRACK_ALLOC, TRACK_FREE };
267 static struct track *get_track(struct kmem_cache *s, void *object,
268 enum track_item alloc)
273 p = object + s->offset + sizeof(void *);
275 p = object + s->inuse;
280 static void set_track(struct kmem_cache *s, void *object,
281 enum track_item alloc, void *addr)
286 p = object + s->offset + sizeof(void *);
288 p = object + s->inuse;
293 p->cpu = smp_processor_id();
294 p->pid = current ? current->pid : -1;
297 memset(p, 0, sizeof(struct track));
300 #define set_tracking(__s, __o, __a) set_track(__s, __o, __a, \
301 __builtin_return_address(0))
303 static void init_tracking(struct kmem_cache *s, void *object)
305 if (s->flags & SLAB_STORE_USER) {
306 set_track(s, object, TRACK_FREE, NULL);
307 set_track(s, object, TRACK_ALLOC, NULL);
311 static void print_track(const char *s, struct track *t)
316 printk(KERN_ERR "%s: ", s);
317 __print_symbol("%s", (unsigned long)t->addr);
318 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
321 static void print_trailer(struct kmem_cache *s, u8 *p)
323 unsigned int off; /* Offset of last byte */
325 if (s->flags & SLAB_RED_ZONE)
326 print_section("Redzone", p + s->objsize,
327 s->inuse - s->objsize);
329 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
331 get_freepointer(s, p));
334 off = s->offset + sizeof(void *);
338 if (s->flags & SLAB_STORE_USER) {
339 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
340 print_track("Last free ", get_track(s, p, TRACK_FREE));
341 off += 2 * sizeof(struct track);
345 /* Beginning of the filler is the free pointer */
346 print_section("Filler", p + off, s->size - off);
349 static void object_err(struct kmem_cache *s, struct page *page,
350 u8 *object, char *reason)
352 u8 *addr = page_address(page);
354 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
355 s->name, reason, object, page);
356 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
357 object - addr, page->flags, page->inuse, page->freelist);
358 if (object > addr + 16)
359 print_section("Bytes b4", object - 16, 16);
360 print_section("Object", object, min(s->objsize, 128));
361 print_trailer(s, object);
365 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
370 va_start(args, reason);
371 vsnprintf(buf, sizeof(buf), reason, args);
373 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
378 static void init_object(struct kmem_cache *s, void *object, int active)
382 if (s->flags & __OBJECT_POISON) {
383 memset(p, POISON_FREE, s->objsize - 1);
384 p[s->objsize -1] = POISON_END;
387 if (s->flags & SLAB_RED_ZONE)
388 memset(p + s->objsize,
389 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
390 s->inuse - s->objsize);
393 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
396 if (*start != (u8)value)
405 static int check_valid_pointer(struct kmem_cache *s, struct page *page,
413 base = page_address(page);
414 if (object < base || object >= base + s->objects * s->size ||
415 (object - base) % s->size) {
426 * Bytes of the object to be managed.
427 * If the freepointer may overlay the object then the free
428 * pointer is the first word of the object.
429 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
432 * object + s->objsize
433 * Padding to reach word boundary. This is also used for Redzoning.
434 * Padding is extended to word size if Redzoning is enabled
435 * and objsize == inuse.
436 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
437 * 0xcc (RED_ACTIVE) for objects in use.
440 * A. Free pointer (if we cannot overwrite object on free)
441 * B. Tracking data for SLAB_STORE_USER
442 * C. Padding to reach required alignment boundary
443 * Padding is done using 0x5a (POISON_INUSE)
447 * If slabcaches are merged then the objsize and inuse boundaries are to
448 * be ignored. And therefore no slab options that rely on these boundaries
449 * may be used with merged slabcaches.
452 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
453 void *from, void *to)
455 printk(KERN_ERR "@@@ SLUB: %s Restoring %s (0x%x) from 0x%p-0x%p\n",
456 s->name, message, data, from, to - 1);
457 memset(from, data, to - from);
460 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
462 unsigned long off = s->inuse; /* The end of info */
465 /* Freepointer is placed after the object. */
466 off += sizeof(void *);
468 if (s->flags & SLAB_STORE_USER)
469 /* We also have user information there */
470 off += 2 * sizeof(struct track);
475 if (check_bytes(p + off, POISON_INUSE, s->size - off))
478 object_err(s, page, p, "Object padding check fails");
483 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
487 static int slab_pad_check(struct kmem_cache *s, struct page *page)
490 int length, remainder;
492 if (!(s->flags & SLAB_POISON))
495 p = page_address(page);
496 length = s->objects * s->size;
497 remainder = (PAGE_SIZE << s->order) - length;
501 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
502 printk(KERN_ERR "SLUB: %s slab 0x%p: Padding fails check\n",
505 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
506 p + length + remainder);
512 static int check_object(struct kmem_cache *s, struct page *page,
513 void *object, int active)
516 u8 *endobject = object + s->objsize;
518 if (s->flags & SLAB_RED_ZONE) {
520 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
522 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
523 object_err(s, page, object,
524 active ? "Redzone Active" : "Redzone Inactive");
525 restore_bytes(s, "redzone", red,
526 endobject, object + s->inuse);
530 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
531 !check_bytes(endobject, POISON_INUSE,
532 s->inuse - s->objsize)) {
533 object_err(s, page, p, "Alignment padding check fails");
535 * Fix it so that there will not be another report.
537 * Hmmm... We may be corrupting an object that now expects
538 * to be longer than allowed.
540 restore_bytes(s, "alignment padding", POISON_INUSE,
541 endobject, object + s->inuse);
545 if (s->flags & SLAB_POISON) {
546 if (!active && (s->flags & __OBJECT_POISON) &&
547 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
548 p[s->objsize - 1] != POISON_END)) {
550 object_err(s, page, p, "Poison check failed");
551 restore_bytes(s, "Poison", POISON_FREE,
552 p, p + s->objsize -1);
553 restore_bytes(s, "Poison", POISON_END,
554 p + s->objsize - 1, p + s->objsize);
558 * check_pad_bytes cleans up on its own.
560 check_pad_bytes(s, page, p);
563 if (!s->offset && active)
565 * Object and freepointer overlap. Cannot check
566 * freepointer while object is allocated.
570 /* Check free pointer validity */
571 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
572 object_err(s, page, p, "Freepointer corrupt");
574 * No choice but to zap it and thus loose the remainder
575 * of the free objects in this slab. May cause
576 * another error because the object count maybe
579 set_freepointer(s, p, NULL);
585 static int check_slab(struct kmem_cache *s, struct page *page)
587 VM_BUG_ON(!irqs_disabled());
589 if (!PageSlab(page)) {
590 printk(KERN_ERR "SLUB: %s Not a valid slab page @0x%p "
591 "flags=%lx mapping=0x%p count=%d \n",
592 s->name, page, page->flags, page->mapping,
596 if (page->offset * sizeof(void *) != s->offset) {
597 printk(KERN_ERR "SLUB: %s Corrupted offset %lu in slab @0x%p"
598 " flags=0x%lx mapping=0x%p count=%d\n",
600 (unsigned long)(page->offset * sizeof(void *)),
608 if (page->inuse > s->objects) {
609 printk(KERN_ERR "SLUB: %s Inuse %u > max %u in slab "
610 "page @0x%p flags=%lx mapping=0x%p count=%d\n",
611 s->name, page->inuse, s->objects, page, page->flags,
612 page->mapping, page_count(page));
616 /* Slab_pad_check fixes things up after itself */
617 slab_pad_check(s, page);
622 * Determine if a certain object on a page is on the freelist and
623 * therefore free. Must hold the slab lock for cpu slabs to
624 * guarantee that the chains are consistent.
626 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
629 void *fp = page->freelist;
632 while (fp && nr <= s->objects) {
635 if (!check_valid_pointer(s, page, fp)) {
637 object_err(s, page, object,
638 "Freechain corrupt");
639 set_freepointer(s, object, NULL);
642 printk(KERN_ERR "SLUB: %s slab 0x%p "
643 "freepointer 0x%p corrupted.\n",
646 page->freelist = NULL;
647 page->inuse = s->objects;
653 fp = get_freepointer(s, object);
657 if (page->inuse != s->objects - nr) {
658 printk(KERN_ERR "slab %s: page 0x%p wrong object count."
659 " counter is %d but counted were %d\n",
660 s->name, page, page->inuse,
662 page->inuse = s->objects - nr;
664 return search == NULL;
667 static int alloc_object_checks(struct kmem_cache *s, struct page *page,
670 if (!check_slab(s, page))
673 if (object && !on_freelist(s, page, object)) {
674 printk(KERN_ERR "SLUB: %s Object 0x%p@0x%p "
675 "already allocated.\n",
676 s->name, object, page);
680 if (!check_valid_pointer(s, page, object)) {
681 object_err(s, page, object, "Freelist Pointer check fails");
688 if (!check_object(s, page, object, 0))
690 init_object(s, object, 1);
692 if (s->flags & SLAB_TRACE) {
693 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
694 s->name, object, page->inuse,
702 if (PageSlab(page)) {
704 * If this is a slab page then lets do the best we can
705 * to avoid issues in the future. Marking all objects
706 * as used avoids touching the remainder.
708 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
710 page->inuse = s->objects;
711 page->freelist = NULL;
712 /* Fix up fields that may be corrupted */
713 page->offset = s->offset / sizeof(void *);
718 static int free_object_checks(struct kmem_cache *s, struct page *page,
721 if (!check_slab(s, page))
724 if (!check_valid_pointer(s, page, object)) {
725 printk(KERN_ERR "SLUB: %s slab 0x%p invalid "
726 "object pointer 0x%p\n",
727 s->name, page, object);
731 if (on_freelist(s, page, object)) {
732 printk(KERN_ERR "SLUB: %s slab 0x%p object "
733 "0x%p already free.\n", s->name, page, object);
737 if (!check_object(s, page, object, 1))
740 if (unlikely(s != page->slab)) {
742 printk(KERN_ERR "slab_free %s size %d: attempt to"
743 "free object(0x%p) outside of slab.\n",
744 s->name, s->size, object);
748 "slab_free : no slab(NULL) for object 0x%p.\n",
751 printk(KERN_ERR "slab_free %s(%d): object at 0x%p"
752 " belongs to slab %s(%d)\n",
753 s->name, s->size, object,
754 page->slab->name, page->slab->size);
757 if (s->flags & SLAB_TRACE) {
758 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
759 s->name, object, page->inuse,
761 print_section("Object", object, s->objsize);
764 init_object(s, object, 0);
768 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
769 s->name, page, object);
774 * Slab allocation and freeing
776 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
779 int pages = 1 << s->order;
784 if (s->flags & SLAB_CACHE_DMA)
788 page = alloc_pages(flags, s->order);
790 page = alloc_pages_node(node, flags, s->order);
795 mod_zone_page_state(page_zone(page),
796 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
797 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
803 static void setup_object(struct kmem_cache *s, struct page *page,
806 if (PageError(page)) {
807 init_object(s, object, 0);
808 init_tracking(s, object);
811 if (unlikely(s->ctor)) {
812 int mode = SLAB_CTOR_CONSTRUCTOR;
814 if (!(s->flags & __GFP_WAIT))
815 mode |= SLAB_CTOR_ATOMIC;
817 s->ctor(object, s, mode);
821 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
824 struct kmem_cache_node *n;
830 if (flags & __GFP_NO_GROW)
833 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
835 if (flags & __GFP_WAIT)
838 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
842 n = get_node(s, page_to_nid(page));
844 atomic_long_inc(&n->nr_slabs);
845 page->offset = s->offset / sizeof(void *);
847 page->flags |= 1 << PG_slab;
848 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
849 SLAB_STORE_USER | SLAB_TRACE))
850 page->flags |= 1 << PG_error;
852 start = page_address(page);
853 end = start + s->objects * s->size;
855 if (unlikely(s->flags & SLAB_POISON))
856 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
859 for (p = start + s->size; p < end; p += s->size) {
860 setup_object(s, page, last);
861 set_freepointer(s, last, p);
864 setup_object(s, page, last);
865 set_freepointer(s, last, NULL);
867 page->freelist = start;
870 if (flags & __GFP_WAIT)
875 static void __free_slab(struct kmem_cache *s, struct page *page)
877 int pages = 1 << s->order;
879 if (unlikely(PageError(page) || s->dtor)) {
880 void *start = page_address(page);
881 void *end = start + (pages << PAGE_SHIFT);
884 slab_pad_check(s, page);
885 for (p = start; p <= end - s->size; p += s->size) {
888 check_object(s, page, p, 0);
892 mod_zone_page_state(page_zone(page),
893 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
894 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
897 page->mapping = NULL;
898 __free_pages(page, s->order);
901 static void rcu_free_slab(struct rcu_head *h)
905 page = container_of((struct list_head *)h, struct page, lru);
906 __free_slab(page->slab, page);
909 static void free_slab(struct kmem_cache *s, struct page *page)
911 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
913 * RCU free overloads the RCU head over the LRU
915 struct rcu_head *head = (void *)&page->lru;
917 call_rcu(head, rcu_free_slab);
919 __free_slab(s, page);
922 static void discard_slab(struct kmem_cache *s, struct page *page)
924 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
926 atomic_long_dec(&n->nr_slabs);
927 reset_page_mapcount(page);
928 page->flags &= ~(1 << PG_slab | 1 << PG_error);
933 * Per slab locking using the pagelock
935 static __always_inline void slab_lock(struct page *page)
937 bit_spin_lock(PG_locked, &page->flags);
940 static __always_inline void slab_unlock(struct page *page)
942 bit_spin_unlock(PG_locked, &page->flags);
945 static __always_inline int slab_trylock(struct page *page)
949 rc = bit_spin_trylock(PG_locked, &page->flags);
954 * Management of partially allocated slabs
956 static void add_partial(struct kmem_cache *s, struct page *page)
958 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
960 spin_lock(&n->list_lock);
962 list_add(&page->lru, &n->partial);
963 spin_unlock(&n->list_lock);
966 static void remove_partial(struct kmem_cache *s,
969 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
971 spin_lock(&n->list_lock);
972 list_del(&page->lru);
974 spin_unlock(&n->list_lock);
978 * Lock page and remove it from the partial list
980 * Must hold list_lock
982 static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
984 if (slab_trylock(page)) {
985 list_del(&page->lru);
993 * Try to get a partial slab from a specific node
995 static struct page *get_partial_node(struct kmem_cache_node *n)
1000 * Racy check. If we mistakenly see no partial slabs then we
1001 * just allocate an empty slab. If we mistakenly try to get a
1002 * partial slab then get_partials() will return NULL.
1004 if (!n || !n->nr_partial)
1007 spin_lock(&n->list_lock);
1008 list_for_each_entry(page, &n->partial, lru)
1009 if (lock_and_del_slab(n, page))
1013 spin_unlock(&n->list_lock);
1018 * Get a page from somewhere. Search in increasing NUMA
1021 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1024 struct zonelist *zonelist;
1029 * The defrag ratio allows to configure the tradeoffs between
1030 * inter node defragmentation and node local allocations.
1031 * A lower defrag_ratio increases the tendency to do local
1032 * allocations instead of scanning throught the partial
1033 * lists on other nodes.
1035 * If defrag_ratio is set to 0 then kmalloc() always
1036 * returns node local objects. If its higher then kmalloc()
1037 * may return off node objects in order to avoid fragmentation.
1039 * A higher ratio means slabs may be taken from other nodes
1040 * thus reducing the number of partial slabs on those nodes.
1042 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1043 * defrag_ratio = 1000) then every (well almost) allocation
1044 * will first attempt to defrag slab caches on other nodes. This
1045 * means scanning over all nodes to look for partial slabs which
1046 * may be a bit expensive to do on every slab allocation.
1048 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1051 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1052 ->node_zonelists[gfp_zone(flags)];
1053 for (z = zonelist->zones; *z; z++) {
1054 struct kmem_cache_node *n;
1056 n = get_node(s, zone_to_nid(*z));
1058 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1059 n->nr_partial > 2) {
1060 page = get_partial_node(n);
1070 * Get a partial page, lock it and return it.
1072 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1075 int searchnode = (node == -1) ? numa_node_id() : node;
1077 page = get_partial_node(get_node(s, searchnode));
1078 if (page || (flags & __GFP_THISNODE))
1081 return get_any_partial(s, flags);
1085 * Move a page back to the lists.
1087 * Must be called with the slab lock held.
1089 * On exit the slab lock will have been dropped.
1091 static void putback_slab(struct kmem_cache *s, struct page *page)
1095 add_partial(s, page);
1099 discard_slab(s, page);
1104 * Remove the cpu slab
1106 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1108 s->cpu_slab[cpu] = NULL;
1109 ClearPageActive(page);
1111 putback_slab(s, page);
1114 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1117 deactivate_slab(s, page, cpu);
1122 * Called from IPI handler with interrupts disabled.
1124 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1126 struct page *page = s->cpu_slab[cpu];
1129 flush_slab(s, page, cpu);
1132 static void flush_cpu_slab(void *d)
1134 struct kmem_cache *s = d;
1135 int cpu = smp_processor_id();
1137 __flush_cpu_slab(s, cpu);
1140 static void flush_all(struct kmem_cache *s)
1143 on_each_cpu(flush_cpu_slab, s, 1, 1);
1145 unsigned long flags;
1147 local_irq_save(flags);
1149 local_irq_restore(flags);
1154 * slab_alloc is optimized to only modify two cachelines on the fast path
1155 * (aside from the stack):
1157 * 1. The page struct
1158 * 2. The first cacheline of the object to be allocated.
1160 * The only cache lines that are read (apart from code) is the
1161 * per cpu array in the kmem_cache struct.
1163 * Fastpath is not possible if we need to get a new slab or have
1164 * debugging enabled (which means all slabs are marked with PageError)
1166 static __always_inline void *slab_alloc(struct kmem_cache *s,
1167 gfp_t gfpflags, int node)
1171 unsigned long flags;
1174 local_irq_save(flags);
1175 cpu = smp_processor_id();
1176 page = s->cpu_slab[cpu];
1181 if (unlikely(node != -1 && page_to_nid(page) != node))
1184 object = page->freelist;
1185 if (unlikely(!object))
1187 if (unlikely(PageError(page)))
1192 page->freelist = object[page->offset];
1194 local_irq_restore(flags);
1198 deactivate_slab(s, page, cpu);
1201 page = get_partial(s, gfpflags, node);
1204 s->cpu_slab[cpu] = page;
1205 SetPageActive(page);
1209 page = new_slab(s, gfpflags, node);
1211 cpu = smp_processor_id();
1212 if (s->cpu_slab[cpu]) {
1214 * Someone else populated the cpu_slab while we enabled
1215 * interrupts, or we have got scheduled on another cpu.
1216 * The page may not be on the requested node.
1219 page_to_nid(s->cpu_slab[cpu]) == node) {
1221 * Current cpuslab is acceptable and we
1222 * want the current one since its cache hot
1224 discard_slab(s, page);
1225 page = s->cpu_slab[cpu];
1229 /* Dump the current slab */
1230 flush_slab(s, s->cpu_slab[cpu], cpu);
1235 local_irq_restore(flags);
1238 if (!alloc_object_checks(s, page, object))
1240 if (s->flags & SLAB_STORE_USER)
1241 set_tracking(s, object, TRACK_ALLOC);
1245 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1247 return slab_alloc(s, gfpflags, -1);
1249 EXPORT_SYMBOL(kmem_cache_alloc);
1252 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1254 return slab_alloc(s, gfpflags, node);
1256 EXPORT_SYMBOL(kmem_cache_alloc_node);
1260 * The fastpath only writes the cacheline of the page struct and the first
1261 * cacheline of the object.
1263 * No special cachelines need to be read
1265 static void slab_free(struct kmem_cache *s, struct page *page, void *x)
1268 void **object = (void *)x;
1269 unsigned long flags;
1271 local_irq_save(flags);
1274 if (unlikely(PageError(page)))
1277 prior = object[page->offset] = page->freelist;
1278 page->freelist = object;
1281 if (unlikely(PageActive(page)))
1283 * Cpu slabs are never on partial lists and are
1288 if (unlikely(!page->inuse))
1292 * Objects left in the slab. If it
1293 * was not on the partial list before
1296 if (unlikely(!prior))
1297 add_partial(s, page);
1301 local_irq_restore(flags);
1307 * Partially used slab that is on the partial list.
1309 remove_partial(s, page);
1312 discard_slab(s, page);
1313 local_irq_restore(flags);
1317 if (free_object_checks(s, page, x))
1322 void kmem_cache_free(struct kmem_cache *s, void *x)
1326 page = virt_to_head_page(x);
1328 if (unlikely(PageError(page) && (s->flags & SLAB_STORE_USER)))
1329 set_tracking(s, x, TRACK_FREE);
1330 slab_free(s, page, x);
1332 EXPORT_SYMBOL(kmem_cache_free);
1334 /* Figure out on which slab object the object resides */
1335 static struct page *get_object_page(const void *x)
1337 struct page *page = virt_to_head_page(x);
1339 if (!PageSlab(page))
1346 * kmem_cache_open produces objects aligned at "size" and the first object
1347 * is placed at offset 0 in the slab (We have no metainformation on the
1348 * slab, all slabs are in essence "off slab").
1350 * In order to get the desired alignment one just needs to align the
1353 * Notice that the allocation order determines the sizes of the per cpu
1354 * caches. Each processor has always one slab available for allocations.
1355 * Increasing the allocation order reduces the number of times that slabs
1356 * must be moved on and off the partial lists and therefore may influence
1359 * The offset is used to relocate the free list link in each object. It is
1360 * therefore possible to move the free list link behind the object. This
1361 * is necessary for RCU to work properly and also useful for debugging.
1365 * Mininum / Maximum order of slab pages. This influences locking overhead
1366 * and slab fragmentation. A higher order reduces the number of partial slabs
1367 * and increases the number of allocations possible without having to
1368 * take the list_lock.
1370 static int slub_min_order;
1371 static int slub_max_order = DEFAULT_MAX_ORDER;
1374 * Minimum number of objects per slab. This is necessary in order to
1375 * reduce locking overhead. Similar to the queue size in SLAB.
1377 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1380 * Merge control. If this is set then no merging of slab caches will occur.
1382 static int slub_nomerge;
1387 static int slub_debug;
1389 static char *slub_debug_slabs;
1392 * Calculate the order of allocation given an slab object size.
1394 * The order of allocation has significant impact on other elements
1395 * of the system. Generally order 0 allocations should be preferred
1396 * since they do not cause fragmentation in the page allocator. Larger
1397 * objects may have problems with order 0 because there may be too much
1398 * space left unused in a slab. We go to a higher order if more than 1/8th
1399 * of the slab would be wasted.
1401 * In order to reach satisfactory performance we must ensure that
1402 * a minimum number of objects is in one slab. Otherwise we may
1403 * generate too much activity on the partial lists. This is less a
1404 * concern for large slabs though. slub_max_order specifies the order
1405 * where we begin to stop considering the number of objects in a slab.
1407 * Higher order allocations also allow the placement of more objects
1408 * in a slab and thereby reduce object handling overhead. If the user
1409 * has requested a higher mininum order then we start with that one
1412 static int calculate_order(int size)
1417 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1418 order < MAX_ORDER; order++) {
1419 unsigned long slab_size = PAGE_SIZE << order;
1421 if (slub_max_order > order &&
1422 slab_size < slub_min_objects * size)
1425 if (slab_size < size)
1428 rem = slab_size % size;
1430 if (rem <= (PAGE_SIZE << order) / 8)
1434 if (order >= MAX_ORDER)
1440 * Function to figure out which alignment to use from the
1441 * various ways of specifying it.
1443 static unsigned long calculate_alignment(unsigned long flags,
1444 unsigned long align, unsigned long size)
1447 * If the user wants hardware cache aligned objects then
1448 * follow that suggestion if the object is sufficiently
1451 * The hardware cache alignment cannot override the
1452 * specified alignment though. If that is greater
1455 if ((flags & (SLAB_MUST_HWCACHE_ALIGN | SLAB_HWCACHE_ALIGN)) &&
1456 size > L1_CACHE_BYTES / 2)
1457 return max_t(unsigned long, align, L1_CACHE_BYTES);
1459 if (align < ARCH_SLAB_MINALIGN)
1460 return ARCH_SLAB_MINALIGN;
1462 return ALIGN(align, sizeof(void *));
1465 static void init_kmem_cache_node(struct kmem_cache_node *n)
1468 atomic_long_set(&n->nr_slabs, 0);
1469 spin_lock_init(&n->list_lock);
1470 INIT_LIST_HEAD(&n->partial);
1475 * No kmalloc_node yet so do it by hand. We know that this is the first
1476 * slab on the node for this slabcache. There are no concurrent accesses
1479 * Note that this function only works on the kmalloc_node_cache
1480 * when allocating for the kmalloc_node_cache.
1482 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1486 struct kmem_cache_node *n;
1488 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1490 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1491 /* new_slab() disables interupts */
1497 page->freelist = get_freepointer(kmalloc_caches, n);
1499 kmalloc_caches->node[node] = n;
1500 init_object(kmalloc_caches, n, 1);
1501 init_kmem_cache_node(n);
1502 atomic_long_inc(&n->nr_slabs);
1503 add_partial(kmalloc_caches, page);
1507 static void free_kmem_cache_nodes(struct kmem_cache *s)
1511 for_each_online_node(node) {
1512 struct kmem_cache_node *n = s->node[node];
1513 if (n && n != &s->local_node)
1514 kmem_cache_free(kmalloc_caches, n);
1515 s->node[node] = NULL;
1519 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1524 if (slab_state >= UP)
1525 local_node = page_to_nid(virt_to_page(s));
1529 for_each_online_node(node) {
1530 struct kmem_cache_node *n;
1532 if (local_node == node)
1535 if (slab_state == DOWN) {
1536 n = early_kmem_cache_node_alloc(gfpflags,
1540 n = kmem_cache_alloc_node(kmalloc_caches,
1544 free_kmem_cache_nodes(s);
1550 init_kmem_cache_node(n);
1555 static void free_kmem_cache_nodes(struct kmem_cache *s)
1559 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1561 init_kmem_cache_node(&s->local_node);
1567 * calculate_sizes() determines the order and the distribution of data within
1570 static int calculate_sizes(struct kmem_cache *s)
1572 unsigned long flags = s->flags;
1573 unsigned long size = s->objsize;
1574 unsigned long align = s->align;
1577 * Determine if we can poison the object itself. If the user of
1578 * the slab may touch the object after free or before allocation
1579 * then we should never poison the object itself.
1581 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1582 !s->ctor && !s->dtor)
1583 s->flags |= __OBJECT_POISON;
1585 s->flags &= ~__OBJECT_POISON;
1588 * Round up object size to the next word boundary. We can only
1589 * place the free pointer at word boundaries and this determines
1590 * the possible location of the free pointer.
1592 size = ALIGN(size, sizeof(void *));
1595 * If we are redzoning then check if there is some space between the
1596 * end of the object and the free pointer. If not then add an
1597 * additional word, so that we can establish a redzone between
1598 * the object and the freepointer to be able to check for overwrites.
1600 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1601 size += sizeof(void *);
1604 * With that we have determined how much of the slab is in actual
1605 * use by the object. This is the potential offset to the free
1610 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1611 s->ctor || s->dtor)) {
1613 * Relocate free pointer after the object if it is not
1614 * permitted to overwrite the first word of the object on
1617 * This is the case if we do RCU, have a constructor or
1618 * destructor or are poisoning the objects.
1621 size += sizeof(void *);
1624 if (flags & SLAB_STORE_USER)
1626 * Need to store information about allocs and frees after
1629 size += 2 * sizeof(struct track);
1631 if (flags & DEBUG_DEFAULT_FLAGS)
1633 * Add some empty padding so that we can catch
1634 * overwrites from earlier objects rather than let
1635 * tracking information or the free pointer be
1636 * corrupted if an user writes before the start
1639 size += sizeof(void *);
1641 * Determine the alignment based on various parameters that the
1642 * user specified (this is unecessarily complex due to the attempt
1643 * to be compatible with SLAB. Should be cleaned up some day).
1645 align = calculate_alignment(flags, align, s->objsize);
1648 * SLUB stores one object immediately after another beginning from
1649 * offset 0. In order to align the objects we have to simply size
1650 * each object to conform to the alignment.
1652 size = ALIGN(size, align);
1655 s->order = calculate_order(size);
1660 * Determine the number of objects per slab
1662 s->objects = (PAGE_SIZE << s->order) / size;
1665 * Verify that the number of objects is within permitted limits.
1666 * The page->inuse field is only 16 bit wide! So we cannot have
1667 * more than 64k objects per slab.
1669 if (!s->objects || s->objects > 65535)
1675 static int __init finish_bootstrap(void)
1677 struct list_head *h;
1682 list_for_each(h, &slab_caches) {
1683 struct kmem_cache *s =
1684 container_of(h, struct kmem_cache, list);
1686 err = sysfs_slab_add(s);
1692 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1693 const char *name, size_t size,
1694 size_t align, unsigned long flags,
1695 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1696 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1698 memset(s, 0, kmem_size);
1706 BUG_ON(flags & SLUB_UNIMPLEMENTED);
1709 * The page->offset field is only 16 bit wide. This is an offset
1710 * in units of words from the beginning of an object. If the slab
1711 * size is bigger then we cannot move the free pointer behind the
1714 * On 32 bit platforms the limit is 256k. On 64bit platforms
1715 * the limit is 512k.
1717 * Debugging or ctor/dtors may create a need to move the free
1718 * pointer. Fail if this happens.
1720 if (s->size >= 65535 * sizeof(void *)) {
1721 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1722 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1723 BUG_ON(ctor || dtor);
1727 * Enable debugging if selected on the kernel commandline.
1729 if (slub_debug && (!slub_debug_slabs ||
1730 strncmp(slub_debug_slabs, name,
1731 strlen(slub_debug_slabs)) == 0))
1732 s->flags |= slub_debug;
1734 if (!calculate_sizes(s))
1739 s->defrag_ratio = 100;
1742 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1745 if (flags & SLAB_PANIC)
1746 panic("Cannot create slab %s size=%lu realsize=%u "
1747 "order=%u offset=%u flags=%lx\n",
1748 s->name, (unsigned long)size, s->size, s->order,
1752 EXPORT_SYMBOL(kmem_cache_open);
1755 * Check if a given pointer is valid
1757 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1762 page = get_object_page(object);
1764 if (!page || s != page->slab)
1765 /* No slab or wrong slab */
1768 addr = page_address(page);
1769 if (object < addr || object >= addr + s->objects * s->size)
1773 if ((object - addr) % s->size)
1774 /* Improperly aligned */
1778 * We could also check if the object is on the slabs freelist.
1779 * But this would be too expensive and it seems that the main
1780 * purpose of kmem_ptr_valid is to check if the object belongs
1781 * to a certain slab.
1785 EXPORT_SYMBOL(kmem_ptr_validate);
1788 * Determine the size of a slab object
1790 unsigned int kmem_cache_size(struct kmem_cache *s)
1794 EXPORT_SYMBOL(kmem_cache_size);
1796 const char *kmem_cache_name(struct kmem_cache *s)
1800 EXPORT_SYMBOL(kmem_cache_name);
1803 * Attempt to free all slabs on a node
1805 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1806 struct list_head *list)
1808 int slabs_inuse = 0;
1809 unsigned long flags;
1810 struct page *page, *h;
1812 spin_lock_irqsave(&n->list_lock, flags);
1813 list_for_each_entry_safe(page, h, list, lru)
1815 list_del(&page->lru);
1816 discard_slab(s, page);
1819 spin_unlock_irqrestore(&n->list_lock, flags);
1824 * Release all resources used by slab cache
1826 static int kmem_cache_close(struct kmem_cache *s)
1832 /* Attempt to free all objects */
1833 for_each_online_node(node) {
1834 struct kmem_cache_node *n = get_node(s, node);
1836 free_list(s, n, &n->partial);
1837 if (atomic_long_read(&n->nr_slabs))
1840 free_kmem_cache_nodes(s);
1845 * Close a cache and release the kmem_cache structure
1846 * (must be used for caches created using kmem_cache_create)
1848 void kmem_cache_destroy(struct kmem_cache *s)
1850 down_write(&slub_lock);
1854 if (kmem_cache_close(s))
1856 sysfs_slab_remove(s);
1859 up_write(&slub_lock);
1861 EXPORT_SYMBOL(kmem_cache_destroy);
1863 /********************************************************************
1865 *******************************************************************/
1867 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1868 EXPORT_SYMBOL(kmalloc_caches);
1870 #ifdef CONFIG_ZONE_DMA
1871 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1874 static int __init setup_slub_min_order(char *str)
1876 get_option (&str, &slub_min_order);
1881 __setup("slub_min_order=", setup_slub_min_order);
1883 static int __init setup_slub_max_order(char *str)
1885 get_option (&str, &slub_max_order);
1890 __setup("slub_max_order=", setup_slub_max_order);
1892 static int __init setup_slub_min_objects(char *str)
1894 get_option (&str, &slub_min_objects);
1899 __setup("slub_min_objects=", setup_slub_min_objects);
1901 static int __init setup_slub_nomerge(char *str)
1907 __setup("slub_nomerge", setup_slub_nomerge);
1909 static int __init setup_slub_debug(char *str)
1911 if (!str || *str != '=')
1912 slub_debug = DEBUG_DEFAULT_FLAGS;
1915 if (*str == 0 || *str == ',')
1916 slub_debug = DEBUG_DEFAULT_FLAGS;
1918 for( ;*str && *str != ','; str++)
1920 case 'f' : case 'F' :
1921 slub_debug |= SLAB_DEBUG_FREE;
1923 case 'z' : case 'Z' :
1924 slub_debug |= SLAB_RED_ZONE;
1926 case 'p' : case 'P' :
1927 slub_debug |= SLAB_POISON;
1929 case 'u' : case 'U' :
1930 slub_debug |= SLAB_STORE_USER;
1932 case 't' : case 'T' :
1933 slub_debug |= SLAB_TRACE;
1936 printk(KERN_ERR "slub_debug option '%c' "
1937 "unknown. skipped\n",*str);
1942 slub_debug_slabs = str + 1;
1946 __setup("slub_debug", setup_slub_debug);
1948 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1949 const char *name, int size, gfp_t gfp_flags)
1951 unsigned int flags = 0;
1953 if (gfp_flags & SLUB_DMA)
1954 flags = SLAB_CACHE_DMA;
1956 down_write(&slub_lock);
1957 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1961 list_add(&s->list, &slab_caches);
1962 up_write(&slub_lock);
1963 if (sysfs_slab_add(s))
1968 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
1971 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
1973 int index = kmalloc_index(size);
1978 /* Allocation too large? */
1981 #ifdef CONFIG_ZONE_DMA
1982 if ((flags & SLUB_DMA)) {
1983 struct kmem_cache *s;
1984 struct kmem_cache *x;
1988 s = kmalloc_caches_dma[index];
1992 /* Dynamically create dma cache */
1993 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
1995 panic("Unable to allocate memory for dma cache\n");
1997 if (index <= KMALLOC_SHIFT_HIGH)
1998 realsize = 1 << index;
2006 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2007 (unsigned int)realsize);
2008 s = create_kmalloc_cache(x, text, realsize, flags);
2009 kmalloc_caches_dma[index] = s;
2013 return &kmalloc_caches[index];
2016 void *__kmalloc(size_t size, gfp_t flags)
2018 struct kmem_cache *s = get_slab(size, flags);
2021 return kmem_cache_alloc(s, flags);
2024 EXPORT_SYMBOL(__kmalloc);
2027 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2029 struct kmem_cache *s = get_slab(size, flags);
2032 return kmem_cache_alloc_node(s, flags, node);
2035 EXPORT_SYMBOL(__kmalloc_node);
2038 size_t ksize(const void *object)
2040 struct page *page = get_object_page(object);
2041 struct kmem_cache *s;
2048 * Debugging requires use of the padding between object
2049 * and whatever may come after it.
2051 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2055 * If we have the need to store the freelist pointer
2056 * back there or track user information then we can
2057 * only use the space before that information.
2059 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2063 * Else we can use all the padding etc for the allocation
2067 EXPORT_SYMBOL(ksize);
2069 void kfree(const void *x)
2071 struct kmem_cache *s;
2077 page = virt_to_head_page(x);
2081 if (unlikely(PageError(page) && (s->flags & SLAB_STORE_USER)))
2082 set_tracking(s, (void *)x, TRACK_FREE);
2083 slab_free(s, page, (void *)x);
2085 EXPORT_SYMBOL(kfree);
2088 * krealloc - reallocate memory. The contents will remain unchanged.
2090 * @p: object to reallocate memory for.
2091 * @new_size: how many bytes of memory are required.
2092 * @flags: the type of memory to allocate.
2094 * The contents of the object pointed to are preserved up to the
2095 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2096 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2097 * %NULL pointer, the object pointed to is freed.
2099 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2101 struct kmem_cache *new_cache;
2106 return kmalloc(new_size, flags);
2108 if (unlikely(!new_size)) {
2113 page = virt_to_head_page(p);
2115 new_cache = get_slab(new_size, flags);
2118 * If new size fits in the current cache, bail out.
2120 if (likely(page->slab == new_cache))
2123 ret = kmalloc(new_size, flags);
2125 memcpy(ret, p, min(new_size, ksize(p)));
2130 EXPORT_SYMBOL(krealloc);
2132 /********************************************************************
2133 * Basic setup of slabs
2134 *******************************************************************/
2136 void __init kmem_cache_init(void)
2142 * Must first have the slab cache available for the allocations of the
2143 * struct kmalloc_cache_node's. There is special bootstrap code in
2144 * kmem_cache_open for slab_state == DOWN.
2146 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2147 sizeof(struct kmem_cache_node), GFP_KERNEL);
2150 /* Able to allocate the per node structures */
2151 slab_state = PARTIAL;
2153 /* Caches that are not of the two-to-the-power-of size */
2154 create_kmalloc_cache(&kmalloc_caches[1],
2155 "kmalloc-96", 96, GFP_KERNEL);
2156 create_kmalloc_cache(&kmalloc_caches[2],
2157 "kmalloc-192", 192, GFP_KERNEL);
2159 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2160 create_kmalloc_cache(&kmalloc_caches[i],
2161 "kmalloc", 1 << i, GFP_KERNEL);
2165 /* Provide the correct kmalloc names now that the caches are up */
2166 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2167 kmalloc_caches[i]. name =
2168 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2171 register_cpu_notifier(&slab_notifier);
2174 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2175 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2176 + nr_cpu_ids * sizeof(struct page *);
2178 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2179 " Processors=%d, Nodes=%d\n",
2180 KMALLOC_SHIFT_HIGH, L1_CACHE_BYTES,
2181 slub_min_order, slub_max_order, slub_min_objects,
2182 nr_cpu_ids, nr_node_ids);
2186 * Find a mergeable slab cache
2188 static int slab_unmergeable(struct kmem_cache *s)
2190 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2193 if (s->ctor || s->dtor)
2199 static struct kmem_cache *find_mergeable(size_t size,
2200 size_t align, unsigned long flags,
2201 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2202 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2204 struct list_head *h;
2206 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2212 size = ALIGN(size, sizeof(void *));
2213 align = calculate_alignment(flags, align, size);
2214 size = ALIGN(size, align);
2216 list_for_each(h, &slab_caches) {
2217 struct kmem_cache *s =
2218 container_of(h, struct kmem_cache, list);
2220 if (slab_unmergeable(s))
2226 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2227 (s->flags & SLUB_MERGE_SAME))
2230 * Check if alignment is compatible.
2231 * Courtesy of Adrian Drzewiecki
2233 if ((s->size & ~(align -1)) != s->size)
2236 if (s->size - size >= sizeof(void *))
2244 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2245 size_t align, unsigned long flags,
2246 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2247 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2249 struct kmem_cache *s;
2251 down_write(&slub_lock);
2252 s = find_mergeable(size, align, flags, dtor, ctor);
2256 * Adjust the object sizes so that we clear
2257 * the complete object on kzalloc.
2259 s->objsize = max(s->objsize, (int)size);
2260 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2261 if (sysfs_slab_alias(s, name))
2264 s = kmalloc(kmem_size, GFP_KERNEL);
2265 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2266 size, align, flags, ctor, dtor)) {
2267 if (sysfs_slab_add(s)) {
2271 list_add(&s->list, &slab_caches);
2275 up_write(&slub_lock);
2279 up_write(&slub_lock);
2280 if (flags & SLAB_PANIC)
2281 panic("Cannot create slabcache %s\n", name);
2286 EXPORT_SYMBOL(kmem_cache_create);
2288 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2292 x = kmem_cache_alloc(s, flags);
2294 memset(x, 0, s->objsize);
2297 EXPORT_SYMBOL(kmem_cache_zalloc);
2300 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2302 struct list_head *h;
2304 down_read(&slub_lock);
2305 list_for_each(h, &slab_caches) {
2306 struct kmem_cache *s =
2307 container_of(h, struct kmem_cache, list);
2311 up_read(&slub_lock);
2315 * Use the cpu notifier to insure that the slab are flushed
2318 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2319 unsigned long action, void *hcpu)
2321 long cpu = (long)hcpu;
2324 case CPU_UP_CANCELED:
2326 for_all_slabs(__flush_cpu_slab, cpu);
2334 static struct notifier_block __cpuinitdata slab_notifier =
2335 { &slab_cpuup_callback, NULL, 0 };
2339 /***************************************************************
2340 * Compatiblility definitions
2341 **************************************************************/
2343 int kmem_cache_shrink(struct kmem_cache *s)
2348 EXPORT_SYMBOL(kmem_cache_shrink);
2352 /*****************************************************************
2353 * Generic reaper used to support the page allocator
2354 * (the cpu slabs are reaped by a per slab workqueue).
2356 * Maybe move this to the page allocator?
2357 ****************************************************************/
2359 static DEFINE_PER_CPU(unsigned long, reap_node);
2361 static void init_reap_node(int cpu)
2365 node = next_node(cpu_to_node(cpu), node_online_map);
2366 if (node == MAX_NUMNODES)
2367 node = first_node(node_online_map);
2369 __get_cpu_var(reap_node) = node;
2372 static void next_reap_node(void)
2374 int node = __get_cpu_var(reap_node);
2377 * Also drain per cpu pages on remote zones
2379 if (node != numa_node_id())
2380 drain_node_pages(node);
2382 node = next_node(node, node_online_map);
2383 if (unlikely(node >= MAX_NUMNODES))
2384 node = first_node(node_online_map);
2385 __get_cpu_var(reap_node) = node;
2388 #define init_reap_node(cpu) do { } while (0)
2389 #define next_reap_node(void) do { } while (0)
2392 #define REAPTIMEOUT_CPUC (2*HZ)
2395 static DEFINE_PER_CPU(struct delayed_work, reap_work);
2397 static void cache_reap(struct work_struct *unused)
2400 refresh_cpu_vm_stats(smp_processor_id());
2401 schedule_delayed_work(&__get_cpu_var(reap_work),
2405 static void __devinit start_cpu_timer(int cpu)
2407 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2410 * When this gets called from do_initcalls via cpucache_init(),
2411 * init_workqueues() has already run, so keventd will be setup
2414 if (keventd_up() && reap_work->work.func == NULL) {
2415 init_reap_node(cpu);
2416 INIT_DELAYED_WORK(reap_work, cache_reap);
2417 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2421 static int __init cpucache_init(void)
2426 * Register the timers that drain pcp pages and update vm statistics
2428 for_each_online_cpu(cpu)
2429 start_cpu_timer(cpu);
2432 __initcall(cpucache_init);
2435 #ifdef SLUB_RESILIENCY_TEST
2436 static unsigned long validate_slab_cache(struct kmem_cache *s);
2438 static void resiliency_test(void)
2442 printk(KERN_ERR "SLUB resiliency testing\n");
2443 printk(KERN_ERR "-----------------------\n");
2444 printk(KERN_ERR "A. Corruption after allocation\n");
2446 p = kzalloc(16, GFP_KERNEL);
2448 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2449 " 0x12->0x%p\n\n", p + 16);
2451 validate_slab_cache(kmalloc_caches + 4);
2453 /* Hmmm... The next two are dangerous */
2454 p = kzalloc(32, GFP_KERNEL);
2455 p[32 + sizeof(void *)] = 0x34;
2456 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2457 " 0x34 -> -0x%p\n", p);
2458 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2460 validate_slab_cache(kmalloc_caches + 5);
2461 p = kzalloc(64, GFP_KERNEL);
2462 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2464 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2466 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2467 validate_slab_cache(kmalloc_caches + 6);
2469 printk(KERN_ERR "\nB. Corruption after free\n");
2470 p = kzalloc(128, GFP_KERNEL);
2473 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2474 validate_slab_cache(kmalloc_caches + 7);
2476 p = kzalloc(256, GFP_KERNEL);
2479 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2480 validate_slab_cache(kmalloc_caches + 8);
2482 p = kzalloc(512, GFP_KERNEL);
2485 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2486 validate_slab_cache(kmalloc_caches + 9);
2489 static void resiliency_test(void) {};
2493 * These are not as efficient as kmalloc for the non debug case.
2494 * We do not have the page struct available so we have to touch one
2495 * cacheline in struct kmem_cache to check slab flags.
2497 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2499 struct kmem_cache *s = get_slab(size, gfpflags);
2505 object = kmem_cache_alloc(s, gfpflags);
2507 if (object && (s->flags & SLAB_STORE_USER))
2508 set_track(s, object, TRACK_ALLOC, caller);
2513 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2514 int node, void *caller)
2516 struct kmem_cache *s = get_slab(size, gfpflags);
2522 object = kmem_cache_alloc_node(s, gfpflags, node);
2524 if (object && (s->flags & SLAB_STORE_USER))
2525 set_track(s, object, TRACK_ALLOC, caller);
2532 static unsigned long count_partial(struct kmem_cache_node *n)
2534 unsigned long flags;
2535 unsigned long x = 0;
2538 spin_lock_irqsave(&n->list_lock, flags);
2539 list_for_each_entry(page, &n->partial, lru)
2541 spin_unlock_irqrestore(&n->list_lock, flags);
2545 enum slab_stat_type {
2552 #define SO_FULL (1 << SL_FULL)
2553 #define SO_PARTIAL (1 << SL_PARTIAL)
2554 #define SO_CPU (1 << SL_CPU)
2555 #define SO_OBJECTS (1 << SL_OBJECTS)
2557 static unsigned long slab_objects(struct kmem_cache *s,
2558 char *buf, unsigned long flags)
2560 unsigned long total = 0;
2564 unsigned long *nodes;
2565 unsigned long *per_cpu;
2567 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2568 per_cpu = nodes + nr_node_ids;
2570 for_each_possible_cpu(cpu) {
2571 struct page *page = s->cpu_slab[cpu];
2575 node = page_to_nid(page);
2576 if (flags & SO_CPU) {
2579 if (flags & SO_OBJECTS)
2590 for_each_online_node(node) {
2591 struct kmem_cache_node *n = get_node(s, node);
2593 if (flags & SO_PARTIAL) {
2594 if (flags & SO_OBJECTS)
2595 x = count_partial(n);
2602 if (flags & SO_FULL) {
2603 int full_slabs = atomic_read(&n->nr_slabs)
2607 if (flags & SO_OBJECTS)
2608 x = full_slabs * s->objects;
2616 x = sprintf(buf, "%lu", total);
2618 for_each_online_node(node)
2620 x += sprintf(buf + x, " N%d=%lu",
2624 return x + sprintf(buf + x, "\n");
2627 static int any_slab_objects(struct kmem_cache *s)
2632 for_each_possible_cpu(cpu)
2633 if (s->cpu_slab[cpu])
2636 for_each_node(node) {
2637 struct kmem_cache_node *n = get_node(s, node);
2639 if (n->nr_partial || atomic_read(&n->nr_slabs))
2645 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2646 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2648 struct slab_attribute {
2649 struct attribute attr;
2650 ssize_t (*show)(struct kmem_cache *s, char *buf);
2651 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2654 #define SLAB_ATTR_RO(_name) \
2655 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2657 #define SLAB_ATTR(_name) \
2658 static struct slab_attribute _name##_attr = \
2659 __ATTR(_name, 0644, _name##_show, _name##_store)
2662 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
2664 return sprintf(buf, "%d\n", s->size);
2666 SLAB_ATTR_RO(slab_size);
2668 static ssize_t align_show(struct kmem_cache *s, char *buf)
2670 return sprintf(buf, "%d\n", s->align);
2672 SLAB_ATTR_RO(align);
2674 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
2676 return sprintf(buf, "%d\n", s->objsize);
2678 SLAB_ATTR_RO(object_size);
2680 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
2682 return sprintf(buf, "%d\n", s->objects);
2684 SLAB_ATTR_RO(objs_per_slab);
2686 static ssize_t order_show(struct kmem_cache *s, char *buf)
2688 return sprintf(buf, "%d\n", s->order);
2690 SLAB_ATTR_RO(order);
2692 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
2695 int n = sprint_symbol(buf, (unsigned long)s->ctor);
2697 return n + sprintf(buf + n, "\n");
2703 static ssize_t dtor_show(struct kmem_cache *s, char *buf)
2706 int n = sprint_symbol(buf, (unsigned long)s->dtor);
2708 return n + sprintf(buf + n, "\n");
2714 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
2716 return sprintf(buf, "%d\n", s->refcount - 1);
2718 SLAB_ATTR_RO(aliases);
2720 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
2722 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
2724 SLAB_ATTR_RO(slabs);
2726 static ssize_t partial_show(struct kmem_cache *s, char *buf)
2728 return slab_objects(s, buf, SO_PARTIAL);
2730 SLAB_ATTR_RO(partial);
2732 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
2734 return slab_objects(s, buf, SO_CPU);
2736 SLAB_ATTR_RO(cpu_slabs);
2738 static ssize_t objects_show(struct kmem_cache *s, char *buf)
2740 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
2742 SLAB_ATTR_RO(objects);
2744 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
2746 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
2749 static ssize_t sanity_checks_store(struct kmem_cache *s,
2750 const char *buf, size_t length)
2752 s->flags &= ~SLAB_DEBUG_FREE;
2754 s->flags |= SLAB_DEBUG_FREE;
2757 SLAB_ATTR(sanity_checks);
2759 static ssize_t trace_show(struct kmem_cache *s, char *buf)
2761 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
2764 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
2767 s->flags &= ~SLAB_TRACE;
2769 s->flags |= SLAB_TRACE;
2774 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
2776 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
2779 static ssize_t reclaim_account_store(struct kmem_cache *s,
2780 const char *buf, size_t length)
2782 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
2784 s->flags |= SLAB_RECLAIM_ACCOUNT;
2787 SLAB_ATTR(reclaim_account);
2789 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
2791 return sprintf(buf, "%d\n", !!(s->flags &
2792 (SLAB_HWCACHE_ALIGN|SLAB_MUST_HWCACHE_ALIGN)));
2794 SLAB_ATTR_RO(hwcache_align);
2796 #ifdef CONFIG_ZONE_DMA
2797 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
2799 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
2801 SLAB_ATTR_RO(cache_dma);
2804 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
2806 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
2808 SLAB_ATTR_RO(destroy_by_rcu);
2810 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
2812 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
2815 static ssize_t red_zone_store(struct kmem_cache *s,
2816 const char *buf, size_t length)
2818 if (any_slab_objects(s))
2821 s->flags &= ~SLAB_RED_ZONE;
2823 s->flags |= SLAB_RED_ZONE;
2827 SLAB_ATTR(red_zone);
2829 static ssize_t poison_show(struct kmem_cache *s, char *buf)
2831 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
2834 static ssize_t poison_store(struct kmem_cache *s,
2835 const char *buf, size_t length)
2837 if (any_slab_objects(s))
2840 s->flags &= ~SLAB_POISON;
2842 s->flags |= SLAB_POISON;
2848 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
2850 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
2853 static ssize_t store_user_store(struct kmem_cache *s,
2854 const char *buf, size_t length)
2856 if (any_slab_objects(s))
2859 s->flags &= ~SLAB_STORE_USER;
2861 s->flags |= SLAB_STORE_USER;
2865 SLAB_ATTR(store_user);
2868 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
2870 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
2873 static ssize_t defrag_ratio_store(struct kmem_cache *s,
2874 const char *buf, size_t length)
2876 int n = simple_strtoul(buf, NULL, 10);
2879 s->defrag_ratio = n * 10;
2882 SLAB_ATTR(defrag_ratio);
2885 static struct attribute * slab_attrs[] = {
2886 &slab_size_attr.attr,
2887 &object_size_attr.attr,
2888 &objs_per_slab_attr.attr,
2893 &cpu_slabs_attr.attr,
2898 &sanity_checks_attr.attr,
2900 &hwcache_align_attr.attr,
2901 &reclaim_account_attr.attr,
2902 &destroy_by_rcu_attr.attr,
2903 &red_zone_attr.attr,
2905 &store_user_attr.attr,
2906 #ifdef CONFIG_ZONE_DMA
2907 &cache_dma_attr.attr,
2910 &defrag_ratio_attr.attr,
2915 static struct attribute_group slab_attr_group = {
2916 .attrs = slab_attrs,
2919 static ssize_t slab_attr_show(struct kobject *kobj,
2920 struct attribute *attr,
2923 struct slab_attribute *attribute;
2924 struct kmem_cache *s;
2927 attribute = to_slab_attr(attr);
2930 if (!attribute->show)
2933 err = attribute->show(s, buf);
2938 static ssize_t slab_attr_store(struct kobject *kobj,
2939 struct attribute *attr,
2940 const char *buf, size_t len)
2942 struct slab_attribute *attribute;
2943 struct kmem_cache *s;
2946 attribute = to_slab_attr(attr);
2949 if (!attribute->store)
2952 err = attribute->store(s, buf, len);
2957 static struct sysfs_ops slab_sysfs_ops = {
2958 .show = slab_attr_show,
2959 .store = slab_attr_store,
2962 static struct kobj_type slab_ktype = {
2963 .sysfs_ops = &slab_sysfs_ops,
2966 static int uevent_filter(struct kset *kset, struct kobject *kobj)
2968 struct kobj_type *ktype = get_ktype(kobj);
2970 if (ktype == &slab_ktype)
2975 static struct kset_uevent_ops slab_uevent_ops = {
2976 .filter = uevent_filter,
2979 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
2981 #define ID_STR_LENGTH 64
2983 /* Create a unique string id for a slab cache:
2985 * :[flags-]size:[memory address of kmemcache]
2987 static char *create_unique_id(struct kmem_cache *s)
2989 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
2996 * First flags affecting slabcache operations. We will only
2997 * get here for aliasable slabs so we do not need to support
2998 * too many flags. The flags here must cover all flags that
2999 * are matched during merging to guarantee that the id is
3002 if (s->flags & SLAB_CACHE_DMA)
3004 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3006 if (s->flags & SLAB_DEBUG_FREE)
3010 p += sprintf(p, "%07d", s->size);
3011 BUG_ON(p > name + ID_STR_LENGTH - 1);
3015 static int sysfs_slab_add(struct kmem_cache *s)
3021 if (slab_state < SYSFS)
3022 /* Defer until later */
3025 unmergeable = slab_unmergeable(s);
3028 * Slabcache can never be merged so we can use the name proper.
3029 * This is typically the case for debug situations. In that
3030 * case we can catch duplicate names easily.
3032 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3036 * Create a unique name for the slab as a target
3039 name = create_unique_id(s);
3042 kobj_set_kset_s(s, slab_subsys);
3043 kobject_set_name(&s->kobj, name);
3044 kobject_init(&s->kobj);
3045 err = kobject_add(&s->kobj);
3049 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3052 kobject_uevent(&s->kobj, KOBJ_ADD);
3054 /* Setup first alias */
3055 sysfs_slab_alias(s, s->name);
3061 static void sysfs_slab_remove(struct kmem_cache *s)
3063 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3064 kobject_del(&s->kobj);
3068 * Need to buffer aliases during bootup until sysfs becomes
3069 * available lest we loose that information.
3071 struct saved_alias {
3072 struct kmem_cache *s;
3074 struct saved_alias *next;
3077 struct saved_alias *alias_list;
3079 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3081 struct saved_alias *al;
3083 if (slab_state == SYSFS) {
3085 * If we have a leftover link then remove it.
3087 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3088 return sysfs_create_link(&slab_subsys.kset.kobj,
3092 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3098 al->next = alias_list;
3103 static int __init slab_sysfs_init(void)
3107 err = subsystem_register(&slab_subsys);
3109 printk(KERN_ERR "Cannot register slab subsystem.\n");
3115 while (alias_list) {
3116 struct saved_alias *al = alias_list;
3118 alias_list = alias_list->next;
3119 err = sysfs_slab_alias(al->s, al->name);
3128 __initcall(slab_sysfs_init);
3130 __initcall(finish_bootstrap);