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 * - SLAB_DEBUG_INITIAL is not supported but I have never seen a use of
103 * - Variable sizing of the per node arrays
106 /* Enable to test recovery from slab corruption on boot */
107 #undef SLUB_RESILIENCY_TEST
112 * Small page size. Make sure that we do not fragment memory
114 #define DEFAULT_MAX_ORDER 1
115 #define DEFAULT_MIN_OBJECTS 4
120 * Large page machines are customarily able to handle larger
123 #define DEFAULT_MAX_ORDER 2
124 #define DEFAULT_MIN_OBJECTS 8
129 * Flags from the regular SLAB that SLUB does not support:
131 #define SLUB_UNIMPLEMENTED (SLAB_DEBUG_INITIAL)
134 * Mininum number of partial slabs. These will be left on the partial
135 * lists even if they are empty. kmem_cache_shrink may reclaim them.
137 #define MIN_PARTIAL 2
140 * Maximum number of desirable partial slabs.
141 * The existence of more partial slabs makes kmem_cache_shrink
142 * sort the partial list by the number of objects in the.
144 #define MAX_PARTIAL 10
146 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
147 SLAB_POISON | SLAB_STORE_USER)
149 * Set of flags that will prevent slab merging
151 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
152 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
154 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
157 #ifndef ARCH_KMALLOC_MINALIGN
158 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
161 #ifndef ARCH_SLAB_MINALIGN
162 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
165 /* Internal SLUB flags */
166 #define __OBJECT_POISON 0x80000000 /* Poison object */
168 static int kmem_size = sizeof(struct kmem_cache);
171 static struct notifier_block slab_notifier;
175 DOWN, /* No slab functionality available */
176 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
177 UP, /* Everything works */
181 /* A list of all slab caches on the system */
182 static DECLARE_RWSEM(slub_lock);
183 LIST_HEAD(slab_caches);
186 static int sysfs_slab_add(struct kmem_cache *);
187 static int sysfs_slab_alias(struct kmem_cache *, const char *);
188 static void sysfs_slab_remove(struct kmem_cache *);
190 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
191 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
192 static void sysfs_slab_remove(struct kmem_cache *s) {}
195 /********************************************************************
196 * Core slab cache functions
197 *******************************************************************/
199 int slab_is_available(void)
201 return slab_state >= UP;
204 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
207 return s->node[node];
209 return &s->local_node;
216 static void print_section(char *text, u8 *addr, unsigned int length)
224 for (i = 0; i < length; i++) {
226 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
229 printk(" %02x", addr[i]);
231 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
233 printk(" %s\n",ascii);
244 printk(" %s\n", ascii);
249 * Slow version of get and set free pointer.
251 * This requires touching the cache lines of kmem_cache.
252 * The offset can also be obtained from the page. In that
253 * case it is in the cacheline that we already need to touch.
255 static void *get_freepointer(struct kmem_cache *s, void *object)
257 return *(void **)(object + s->offset);
260 static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
262 *(void **)(object + s->offset) = fp;
266 * Tracking user of a slab.
269 void *addr; /* Called from address */
270 int cpu; /* Was running on cpu */
271 int pid; /* Pid context */
272 unsigned long when; /* When did the operation occur */
275 enum track_item { TRACK_ALLOC, TRACK_FREE };
277 static struct track *get_track(struct kmem_cache *s, void *object,
278 enum track_item alloc)
283 p = object + s->offset + sizeof(void *);
285 p = object + s->inuse;
290 static void set_track(struct kmem_cache *s, void *object,
291 enum track_item alloc, void *addr)
296 p = object + s->offset + sizeof(void *);
298 p = object + s->inuse;
303 p->cpu = smp_processor_id();
304 p->pid = current ? current->pid : -1;
307 memset(p, 0, sizeof(struct track));
310 static void init_tracking(struct kmem_cache *s, void *object)
312 if (s->flags & SLAB_STORE_USER) {
313 set_track(s, object, TRACK_FREE, NULL);
314 set_track(s, object, TRACK_ALLOC, NULL);
318 static void print_track(const char *s, struct track *t)
323 printk(KERN_ERR "%s: ", s);
324 __print_symbol("%s", (unsigned long)t->addr);
325 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
328 static void print_trailer(struct kmem_cache *s, u8 *p)
330 unsigned int off; /* Offset of last byte */
332 if (s->flags & SLAB_RED_ZONE)
333 print_section("Redzone", p + s->objsize,
334 s->inuse - s->objsize);
336 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
338 get_freepointer(s, p));
341 off = s->offset + sizeof(void *);
345 if (s->flags & SLAB_STORE_USER) {
346 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
347 print_track("Last free ", get_track(s, p, TRACK_FREE));
348 off += 2 * sizeof(struct track);
352 /* Beginning of the filler is the free pointer */
353 print_section("Filler", p + off, s->size - off);
356 static void object_err(struct kmem_cache *s, struct page *page,
357 u8 *object, char *reason)
359 u8 *addr = page_address(page);
361 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
362 s->name, reason, object, page);
363 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
364 object - addr, page->flags, page->inuse, page->freelist);
365 if (object > addr + 16)
366 print_section("Bytes b4", object - 16, 16);
367 print_section("Object", object, min(s->objsize, 128));
368 print_trailer(s, object);
372 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
377 va_start(args, reason);
378 vsnprintf(buf, sizeof(buf), reason, args);
380 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
385 static void init_object(struct kmem_cache *s, void *object, int active)
389 if (s->flags & __OBJECT_POISON) {
390 memset(p, POISON_FREE, s->objsize - 1);
391 p[s->objsize -1] = POISON_END;
394 if (s->flags & SLAB_RED_ZONE)
395 memset(p + s->objsize,
396 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
397 s->inuse - s->objsize);
400 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
403 if (*start != (u8)value)
412 static int check_valid_pointer(struct kmem_cache *s, struct page *page,
420 base = page_address(page);
421 if (object < base || object >= base + s->objects * s->size ||
422 (object - base) % s->size) {
433 * Bytes of the object to be managed.
434 * If the freepointer may overlay the object then the free
435 * pointer is the first word of the object.
436 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
439 * object + s->objsize
440 * Padding to reach word boundary. This is also used for Redzoning.
441 * Padding is extended to word size if Redzoning is enabled
442 * and objsize == inuse.
443 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
444 * 0xcc (RED_ACTIVE) for objects in use.
447 * A. Free pointer (if we cannot overwrite object on free)
448 * B. Tracking data for SLAB_STORE_USER
449 * C. Padding to reach required alignment boundary
450 * Padding is done using 0x5a (POISON_INUSE)
454 * If slabcaches are merged then the objsize and inuse boundaries are to
455 * be ignored. And therefore no slab options that rely on these boundaries
456 * may be used with merged slabcaches.
459 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
460 void *from, void *to)
462 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
463 s->name, message, data, from, to - 1);
464 memset(from, data, to - from);
467 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
469 unsigned long off = s->inuse; /* The end of info */
472 /* Freepointer is placed after the object. */
473 off += sizeof(void *);
475 if (s->flags & SLAB_STORE_USER)
476 /* We also have user information there */
477 off += 2 * sizeof(struct track);
482 if (check_bytes(p + off, POISON_INUSE, s->size - off))
485 object_err(s, page, p, "Object padding check fails");
490 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
494 static int slab_pad_check(struct kmem_cache *s, struct page *page)
497 int length, remainder;
499 if (!(s->flags & SLAB_POISON))
502 p = page_address(page);
503 length = s->objects * s->size;
504 remainder = (PAGE_SIZE << s->order) - length;
508 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
509 slab_err(s, page, "Padding check failed");
510 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
511 p + length + remainder);
517 static int check_object(struct kmem_cache *s, struct page *page,
518 void *object, int active)
521 u8 *endobject = object + s->objsize;
523 if (s->flags & SLAB_RED_ZONE) {
525 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
527 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
528 object_err(s, page, object,
529 active ? "Redzone Active" : "Redzone Inactive");
530 restore_bytes(s, "redzone", red,
531 endobject, object + s->inuse);
535 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
536 !check_bytes(endobject, POISON_INUSE,
537 s->inuse - s->objsize)) {
538 object_err(s, page, p, "Alignment padding check fails");
540 * Fix it so that there will not be another report.
542 * Hmmm... We may be corrupting an object that now expects
543 * to be longer than allowed.
545 restore_bytes(s, "alignment padding", POISON_INUSE,
546 endobject, object + s->inuse);
550 if (s->flags & SLAB_POISON) {
551 if (!active && (s->flags & __OBJECT_POISON) &&
552 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
553 p[s->objsize - 1] != POISON_END)) {
555 object_err(s, page, p, "Poison check failed");
556 restore_bytes(s, "Poison", POISON_FREE,
557 p, p + s->objsize -1);
558 restore_bytes(s, "Poison", POISON_END,
559 p + s->objsize - 1, p + s->objsize);
563 * check_pad_bytes cleans up on its own.
565 check_pad_bytes(s, page, p);
568 if (!s->offset && active)
570 * Object and freepointer overlap. Cannot check
571 * freepointer while object is allocated.
575 /* Check free pointer validity */
576 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
577 object_err(s, page, p, "Freepointer corrupt");
579 * No choice but to zap it and thus loose the remainder
580 * of the free objects in this slab. May cause
581 * another error because the object count maybe
584 set_freepointer(s, p, NULL);
590 static int check_slab(struct kmem_cache *s, struct page *page)
592 VM_BUG_ON(!irqs_disabled());
594 if (!PageSlab(page)) {
595 slab_err(s, page, "Not a valid slab page flags=%lx "
596 "mapping=0x%p count=%d", page->flags, page->mapping,
600 if (page->offset * sizeof(void *) != s->offset) {
601 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
602 "mapping=0x%p count=%d",
603 (unsigned long)(page->offset * sizeof(void *)),
609 if (page->inuse > s->objects) {
610 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
611 "mapping=0x%p count=%d",
612 s->name, page->inuse, s->objects, page->flags,
613 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 slab_err(s, page, "Freepointer 0x%p corrupt",
644 page->freelist = NULL;
645 page->inuse = s->objects;
646 printk(KERN_ERR "@@@ SLUB %s: Freelist "
647 "cleared. Slab 0x%p\n",
654 fp = get_freepointer(s, object);
658 if (page->inuse != s->objects - nr) {
659 slab_err(s, page, "Wrong object count. Counter is %d but "
660 "counted were %d", s, page, page->inuse,
662 page->inuse = s->objects - nr;
663 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
664 "Slab @0x%p\n", s->name, page);
666 return search == NULL;
670 * Tracking of fully allocated slabs for debugging
672 static void add_full(struct kmem_cache_node *n, struct page *page)
674 spin_lock(&n->list_lock);
675 list_add(&page->lru, &n->full);
676 spin_unlock(&n->list_lock);
679 static void remove_full(struct kmem_cache *s, struct page *page)
681 struct kmem_cache_node *n;
683 if (!(s->flags & SLAB_STORE_USER))
686 n = get_node(s, page_to_nid(page));
688 spin_lock(&n->list_lock);
689 list_del(&page->lru);
690 spin_unlock(&n->list_lock);
693 static int alloc_object_checks(struct kmem_cache *s, struct page *page,
696 if (!check_slab(s, page))
699 if (object && !on_freelist(s, page, object)) {
700 slab_err(s, page, "Object 0x%p already allocated", object);
704 if (!check_valid_pointer(s, page, object)) {
705 object_err(s, page, object, "Freelist Pointer check fails");
712 if (!check_object(s, page, object, 0))
717 if (PageSlab(page)) {
719 * If this is a slab page then lets do the best we can
720 * to avoid issues in the future. Marking all objects
721 * as used avoids touching the remainder.
723 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
725 page->inuse = s->objects;
726 page->freelist = NULL;
727 /* Fix up fields that may be corrupted */
728 page->offset = s->offset / sizeof(void *);
733 static int free_object_checks(struct kmem_cache *s, struct page *page,
736 if (!check_slab(s, page))
739 if (!check_valid_pointer(s, page, object)) {
740 slab_err(s, page, "Invalid object pointer 0x%p", object);
744 if (on_freelist(s, page, object)) {
745 slab_err(s, page, "Object 0x%p already free", object);
749 if (!check_object(s, page, object, 1))
752 if (unlikely(s != page->slab)) {
754 slab_err(s, page, "Attempt to free object(0x%p) "
755 "outside of slab", object);
759 "SLUB <none>: no slab for object 0x%p.\n",
764 slab_err(s, page, "object at 0x%p belongs "
765 "to slab %s", object, page->slab->name);
770 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
771 s->name, page, object);
776 * Slab allocation and freeing
778 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
781 int pages = 1 << s->order;
786 if (s->flags & SLAB_CACHE_DMA)
790 page = alloc_pages(flags, s->order);
792 page = alloc_pages_node(node, flags, s->order);
797 mod_zone_page_state(page_zone(page),
798 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
799 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
805 static void setup_object(struct kmem_cache *s, struct page *page,
808 if (PageError(page)) {
809 init_object(s, object, 0);
810 init_tracking(s, object);
813 if (unlikely(s->ctor)) {
814 int mode = SLAB_CTOR_CONSTRUCTOR;
816 if (!(s->flags & __GFP_WAIT))
817 mode |= SLAB_CTOR_ATOMIC;
819 s->ctor(object, s, mode);
823 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
826 struct kmem_cache_node *n;
832 if (flags & __GFP_NO_GROW)
835 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
837 if (flags & __GFP_WAIT)
840 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
844 n = get_node(s, page_to_nid(page));
846 atomic_long_inc(&n->nr_slabs);
847 page->offset = s->offset / sizeof(void *);
849 page->flags |= 1 << PG_slab;
850 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
851 SLAB_STORE_USER | SLAB_TRACE))
852 page->flags |= 1 << PG_error;
854 start = page_address(page);
855 end = start + s->objects * s->size;
857 if (unlikely(s->flags & SLAB_POISON))
858 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
861 for (p = start + s->size; p < end; p += s->size) {
862 setup_object(s, page, last);
863 set_freepointer(s, last, p);
866 setup_object(s, page, last);
867 set_freepointer(s, last, NULL);
869 page->freelist = start;
872 if (flags & __GFP_WAIT)
877 static void __free_slab(struct kmem_cache *s, struct page *page)
879 int pages = 1 << s->order;
881 if (unlikely(PageError(page) || s->dtor)) {
882 void *start = page_address(page);
883 void *end = start + (pages << PAGE_SHIFT);
886 slab_pad_check(s, page);
887 for (p = start; p <= end - s->size; p += s->size) {
890 check_object(s, page, p, 0);
894 mod_zone_page_state(page_zone(page),
895 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
896 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
899 page->mapping = NULL;
900 __free_pages(page, s->order);
903 static void rcu_free_slab(struct rcu_head *h)
907 page = container_of((struct list_head *)h, struct page, lru);
908 __free_slab(page->slab, page);
911 static void free_slab(struct kmem_cache *s, struct page *page)
913 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
915 * RCU free overloads the RCU head over the LRU
917 struct rcu_head *head = (void *)&page->lru;
919 call_rcu(head, rcu_free_slab);
921 __free_slab(s, page);
924 static void discard_slab(struct kmem_cache *s, struct page *page)
926 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
928 atomic_long_dec(&n->nr_slabs);
929 reset_page_mapcount(page);
930 page->flags &= ~(1 << PG_slab | 1 << PG_error);
935 * Per slab locking using the pagelock
937 static __always_inline void slab_lock(struct page *page)
939 bit_spin_lock(PG_locked, &page->flags);
942 static __always_inline void slab_unlock(struct page *page)
944 bit_spin_unlock(PG_locked, &page->flags);
947 static __always_inline int slab_trylock(struct page *page)
951 rc = bit_spin_trylock(PG_locked, &page->flags);
956 * Management of partially allocated slabs
958 static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
960 spin_lock(&n->list_lock);
962 list_add_tail(&page->lru, &n->partial);
963 spin_unlock(&n->list_lock);
966 static void add_partial(struct kmem_cache_node *n, struct page *page)
968 spin_lock(&n->list_lock);
970 list_add(&page->lru, &n->partial);
971 spin_unlock(&n->list_lock);
974 static void remove_partial(struct kmem_cache *s,
977 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
979 spin_lock(&n->list_lock);
980 list_del(&page->lru);
982 spin_unlock(&n->list_lock);
986 * Lock page and remove it from the partial list
988 * Must hold list_lock
990 static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
992 if (slab_trylock(page)) {
993 list_del(&page->lru);
1001 * Try to get a partial slab from a specific node
1003 static struct page *get_partial_node(struct kmem_cache_node *n)
1008 * Racy check. If we mistakenly see no partial slabs then we
1009 * just allocate an empty slab. If we mistakenly try to get a
1010 * partial slab then get_partials() will return NULL.
1012 if (!n || !n->nr_partial)
1015 spin_lock(&n->list_lock);
1016 list_for_each_entry(page, &n->partial, lru)
1017 if (lock_and_del_slab(n, page))
1021 spin_unlock(&n->list_lock);
1026 * Get a page from somewhere. Search in increasing NUMA
1029 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1032 struct zonelist *zonelist;
1037 * The defrag ratio allows to configure the tradeoffs between
1038 * inter node defragmentation and node local allocations.
1039 * A lower defrag_ratio increases the tendency to do local
1040 * allocations instead of scanning throught the partial
1041 * lists on other nodes.
1043 * If defrag_ratio is set to 0 then kmalloc() always
1044 * returns node local objects. If its higher then kmalloc()
1045 * may return off node objects in order to avoid fragmentation.
1047 * A higher ratio means slabs may be taken from other nodes
1048 * thus reducing the number of partial slabs on those nodes.
1050 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1051 * defrag_ratio = 1000) then every (well almost) allocation
1052 * will first attempt to defrag slab caches on other nodes. This
1053 * means scanning over all nodes to look for partial slabs which
1054 * may be a bit expensive to do on every slab allocation.
1056 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1059 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1060 ->node_zonelists[gfp_zone(flags)];
1061 for (z = zonelist->zones; *z; z++) {
1062 struct kmem_cache_node *n;
1064 n = get_node(s, zone_to_nid(*z));
1066 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1067 n->nr_partial > MIN_PARTIAL) {
1068 page = get_partial_node(n);
1078 * Get a partial page, lock it and return it.
1080 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1083 int searchnode = (node == -1) ? numa_node_id() : node;
1085 page = get_partial_node(get_node(s, searchnode));
1086 if (page || (flags & __GFP_THISNODE))
1089 return get_any_partial(s, flags);
1093 * Move a page back to the lists.
1095 * Must be called with the slab lock held.
1097 * On exit the slab lock will have been dropped.
1099 static void putback_slab(struct kmem_cache *s, struct page *page)
1101 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1106 add_partial(n, page);
1107 else if (PageError(page) && (s->flags & SLAB_STORE_USER))
1112 if (n->nr_partial < MIN_PARTIAL) {
1114 * Adding an empty page to the partial slabs in order
1115 * to avoid page allocator overhead. This page needs to
1116 * come after all the others that are not fully empty
1117 * in order to make sure that we do maximum
1120 add_partial_tail(n, page);
1124 discard_slab(s, page);
1130 * Remove the cpu slab
1132 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1134 s->cpu_slab[cpu] = NULL;
1135 ClearPageActive(page);
1137 putback_slab(s, page);
1140 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1143 deactivate_slab(s, page, cpu);
1148 * Called from IPI handler with interrupts disabled.
1150 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1152 struct page *page = s->cpu_slab[cpu];
1155 flush_slab(s, page, cpu);
1158 static void flush_cpu_slab(void *d)
1160 struct kmem_cache *s = d;
1161 int cpu = smp_processor_id();
1163 __flush_cpu_slab(s, cpu);
1166 static void flush_all(struct kmem_cache *s)
1169 on_each_cpu(flush_cpu_slab, s, 1, 1);
1171 unsigned long flags;
1173 local_irq_save(flags);
1175 local_irq_restore(flags);
1180 * slab_alloc is optimized to only modify two cachelines on the fast path
1181 * (aside from the stack):
1183 * 1. The page struct
1184 * 2. The first cacheline of the object to be allocated.
1186 * The only cache lines that are read (apart from code) is the
1187 * per cpu array in the kmem_cache struct.
1189 * Fastpath is not possible if we need to get a new slab or have
1190 * debugging enabled (which means all slabs are marked with PageError)
1192 static void *slab_alloc(struct kmem_cache *s,
1193 gfp_t gfpflags, int node, void *addr)
1197 unsigned long flags;
1200 local_irq_save(flags);
1201 cpu = smp_processor_id();
1202 page = s->cpu_slab[cpu];
1207 if (unlikely(node != -1 && page_to_nid(page) != node))
1210 object = page->freelist;
1211 if (unlikely(!object))
1213 if (unlikely(PageError(page)))
1218 page->freelist = object[page->offset];
1220 local_irq_restore(flags);
1224 deactivate_slab(s, page, cpu);
1227 page = get_partial(s, gfpflags, node);
1230 s->cpu_slab[cpu] = page;
1231 SetPageActive(page);
1235 page = new_slab(s, gfpflags, node);
1237 cpu = smp_processor_id();
1238 if (s->cpu_slab[cpu]) {
1240 * Someone else populated the cpu_slab while we enabled
1241 * interrupts, or we have got scheduled on another cpu.
1242 * The page may not be on the requested node.
1245 page_to_nid(s->cpu_slab[cpu]) == node) {
1247 * Current cpuslab is acceptable and we
1248 * want the current one since its cache hot
1250 discard_slab(s, page);
1251 page = s->cpu_slab[cpu];
1255 /* Dump the current slab */
1256 flush_slab(s, s->cpu_slab[cpu], cpu);
1261 local_irq_restore(flags);
1264 if (!alloc_object_checks(s, page, object))
1266 if (s->flags & SLAB_STORE_USER)
1267 set_track(s, object, TRACK_ALLOC, addr);
1268 if (s->flags & SLAB_TRACE) {
1269 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
1270 s->name, object, page->inuse,
1274 init_object(s, object, 1);
1278 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1280 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1282 EXPORT_SYMBOL(kmem_cache_alloc);
1285 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1287 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1289 EXPORT_SYMBOL(kmem_cache_alloc_node);
1293 * The fastpath only writes the cacheline of the page struct and the first
1294 * cacheline of the object.
1296 * No special cachelines need to be read
1298 static void slab_free(struct kmem_cache *s, struct page *page,
1299 void *x, void *addr)
1302 void **object = (void *)x;
1303 unsigned long flags;
1305 local_irq_save(flags);
1308 if (unlikely(PageError(page)))
1311 prior = object[page->offset] = page->freelist;
1312 page->freelist = object;
1315 if (unlikely(PageActive(page)))
1317 * Cpu slabs are never on partial lists and are
1322 if (unlikely(!page->inuse))
1326 * Objects left in the slab. If it
1327 * was not on the partial list before
1330 if (unlikely(!prior))
1331 add_partial(get_node(s, page_to_nid(page)), page);
1335 local_irq_restore(flags);
1341 * Slab on the partial list.
1343 remove_partial(s, page);
1346 discard_slab(s, page);
1347 local_irq_restore(flags);
1351 if (!free_object_checks(s, page, x))
1353 if (!PageActive(page) && !page->freelist)
1354 remove_full(s, page);
1355 if (s->flags & SLAB_STORE_USER)
1356 set_track(s, x, TRACK_FREE, addr);
1357 if (s->flags & SLAB_TRACE) {
1358 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
1359 s->name, object, page->inuse,
1361 print_section("Object", (void *)object, s->objsize);
1364 init_object(s, object, 0);
1368 void kmem_cache_free(struct kmem_cache *s, void *x)
1372 page = virt_to_head_page(x);
1374 slab_free(s, page, x, __builtin_return_address(0));
1376 EXPORT_SYMBOL(kmem_cache_free);
1378 /* Figure out on which slab object the object resides */
1379 static struct page *get_object_page(const void *x)
1381 struct page *page = virt_to_head_page(x);
1383 if (!PageSlab(page))
1390 * kmem_cache_open produces objects aligned at "size" and the first object
1391 * is placed at offset 0 in the slab (We have no metainformation on the
1392 * slab, all slabs are in essence "off slab").
1394 * In order to get the desired alignment one just needs to align the
1397 * Notice that the allocation order determines the sizes of the per cpu
1398 * caches. Each processor has always one slab available for allocations.
1399 * Increasing the allocation order reduces the number of times that slabs
1400 * must be moved on and off the partial lists and therefore may influence
1403 * The offset is used to relocate the free list link in each object. It is
1404 * therefore possible to move the free list link behind the object. This
1405 * is necessary for RCU to work properly and also useful for debugging.
1409 * Mininum / Maximum order of slab pages. This influences locking overhead
1410 * and slab fragmentation. A higher order reduces the number of partial slabs
1411 * and increases the number of allocations possible without having to
1412 * take the list_lock.
1414 static int slub_min_order;
1415 static int slub_max_order = DEFAULT_MAX_ORDER;
1418 * Minimum number of objects per slab. This is necessary in order to
1419 * reduce locking overhead. Similar to the queue size in SLAB.
1421 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1424 * Merge control. If this is set then no merging of slab caches will occur.
1426 static int slub_nomerge;
1431 static int slub_debug;
1433 static char *slub_debug_slabs;
1436 * Calculate the order of allocation given an slab object size.
1438 * The order of allocation has significant impact on other elements
1439 * of the system. Generally order 0 allocations should be preferred
1440 * since they do not cause fragmentation in the page allocator. Larger
1441 * objects may have problems with order 0 because there may be too much
1442 * space left unused in a slab. We go to a higher order if more than 1/8th
1443 * of the slab would be wasted.
1445 * In order to reach satisfactory performance we must ensure that
1446 * a minimum number of objects is in one slab. Otherwise we may
1447 * generate too much activity on the partial lists. This is less a
1448 * concern for large slabs though. slub_max_order specifies the order
1449 * where we begin to stop considering the number of objects in a slab.
1451 * Higher order allocations also allow the placement of more objects
1452 * in a slab and thereby reduce object handling overhead. If the user
1453 * has requested a higher mininum order then we start with that one
1456 static int calculate_order(int size)
1461 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1462 order < MAX_ORDER; order++) {
1463 unsigned long slab_size = PAGE_SIZE << order;
1465 if (slub_max_order > order &&
1466 slab_size < slub_min_objects * size)
1469 if (slab_size < size)
1472 rem = slab_size % size;
1474 if (rem <= (PAGE_SIZE << order) / 8)
1478 if (order >= MAX_ORDER)
1484 * Function to figure out which alignment to use from the
1485 * various ways of specifying it.
1487 static unsigned long calculate_alignment(unsigned long flags,
1488 unsigned long align, unsigned long size)
1491 * If the user wants hardware cache aligned objects then
1492 * follow that suggestion if the object is sufficiently
1495 * The hardware cache alignment cannot override the
1496 * specified alignment though. If that is greater
1499 if ((flags & (SLAB_MUST_HWCACHE_ALIGN | SLAB_HWCACHE_ALIGN)) &&
1500 size > L1_CACHE_BYTES / 2)
1501 return max_t(unsigned long, align, L1_CACHE_BYTES);
1503 if (align < ARCH_SLAB_MINALIGN)
1504 return ARCH_SLAB_MINALIGN;
1506 return ALIGN(align, sizeof(void *));
1509 static void init_kmem_cache_node(struct kmem_cache_node *n)
1512 atomic_long_set(&n->nr_slabs, 0);
1513 spin_lock_init(&n->list_lock);
1514 INIT_LIST_HEAD(&n->partial);
1515 INIT_LIST_HEAD(&n->full);
1520 * No kmalloc_node yet so do it by hand. We know that this is the first
1521 * slab on the node for this slabcache. There are no concurrent accesses
1524 * Note that this function only works on the kmalloc_node_cache
1525 * when allocating for the kmalloc_node_cache.
1527 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1531 struct kmem_cache_node *n;
1533 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1535 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1536 /* new_slab() disables interupts */
1542 page->freelist = get_freepointer(kmalloc_caches, n);
1544 kmalloc_caches->node[node] = n;
1545 init_object(kmalloc_caches, n, 1);
1546 init_kmem_cache_node(n);
1547 atomic_long_inc(&n->nr_slabs);
1548 add_partial(n, page);
1552 static void free_kmem_cache_nodes(struct kmem_cache *s)
1556 for_each_online_node(node) {
1557 struct kmem_cache_node *n = s->node[node];
1558 if (n && n != &s->local_node)
1559 kmem_cache_free(kmalloc_caches, n);
1560 s->node[node] = NULL;
1564 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1569 if (slab_state >= UP)
1570 local_node = page_to_nid(virt_to_page(s));
1574 for_each_online_node(node) {
1575 struct kmem_cache_node *n;
1577 if (local_node == node)
1580 if (slab_state == DOWN) {
1581 n = early_kmem_cache_node_alloc(gfpflags,
1585 n = kmem_cache_alloc_node(kmalloc_caches,
1589 free_kmem_cache_nodes(s);
1595 init_kmem_cache_node(n);
1600 static void free_kmem_cache_nodes(struct kmem_cache *s)
1604 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1606 init_kmem_cache_node(&s->local_node);
1612 * calculate_sizes() determines the order and the distribution of data within
1615 static int calculate_sizes(struct kmem_cache *s)
1617 unsigned long flags = s->flags;
1618 unsigned long size = s->objsize;
1619 unsigned long align = s->align;
1622 * Determine if we can poison the object itself. If the user of
1623 * the slab may touch the object after free or before allocation
1624 * then we should never poison the object itself.
1626 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1627 !s->ctor && !s->dtor)
1628 s->flags |= __OBJECT_POISON;
1630 s->flags &= ~__OBJECT_POISON;
1633 * Round up object size to the next word boundary. We can only
1634 * place the free pointer at word boundaries and this determines
1635 * the possible location of the free pointer.
1637 size = ALIGN(size, sizeof(void *));
1640 * If we are redzoning then check if there is some space between the
1641 * end of the object and the free pointer. If not then add an
1642 * additional word, so that we can establish a redzone between
1643 * the object and the freepointer to be able to check for overwrites.
1645 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1646 size += sizeof(void *);
1649 * With that we have determined how much of the slab is in actual
1650 * use by the object. This is the potential offset to the free
1655 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1656 s->ctor || s->dtor)) {
1658 * Relocate free pointer after the object if it is not
1659 * permitted to overwrite the first word of the object on
1662 * This is the case if we do RCU, have a constructor or
1663 * destructor or are poisoning the objects.
1666 size += sizeof(void *);
1669 if (flags & SLAB_STORE_USER)
1671 * Need to store information about allocs and frees after
1674 size += 2 * sizeof(struct track);
1676 if (flags & DEBUG_DEFAULT_FLAGS)
1678 * Add some empty padding so that we can catch
1679 * overwrites from earlier objects rather than let
1680 * tracking information or the free pointer be
1681 * corrupted if an user writes before the start
1684 size += sizeof(void *);
1686 * Determine the alignment based on various parameters that the
1687 * user specified (this is unecessarily complex due to the attempt
1688 * to be compatible with SLAB. Should be cleaned up some day).
1690 align = calculate_alignment(flags, align, s->objsize);
1693 * SLUB stores one object immediately after another beginning from
1694 * offset 0. In order to align the objects we have to simply size
1695 * each object to conform to the alignment.
1697 size = ALIGN(size, align);
1700 s->order = calculate_order(size);
1705 * Determine the number of objects per slab
1707 s->objects = (PAGE_SIZE << s->order) / size;
1710 * Verify that the number of objects is within permitted limits.
1711 * The page->inuse field is only 16 bit wide! So we cannot have
1712 * more than 64k objects per slab.
1714 if (!s->objects || s->objects > 65535)
1720 static int __init finish_bootstrap(void)
1722 struct list_head *h;
1727 list_for_each(h, &slab_caches) {
1728 struct kmem_cache *s =
1729 container_of(h, struct kmem_cache, list);
1731 err = sysfs_slab_add(s);
1737 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1738 const char *name, size_t size,
1739 size_t align, unsigned long flags,
1740 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1741 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1743 memset(s, 0, kmem_size);
1751 BUG_ON(flags & SLUB_UNIMPLEMENTED);
1754 * The page->offset field is only 16 bit wide. This is an offset
1755 * in units of words from the beginning of an object. If the slab
1756 * size is bigger then we cannot move the free pointer behind the
1759 * On 32 bit platforms the limit is 256k. On 64bit platforms
1760 * the limit is 512k.
1762 * Debugging or ctor/dtors may create a need to move the free
1763 * pointer. Fail if this happens.
1765 if (s->size >= 65535 * sizeof(void *)) {
1766 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1767 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1768 BUG_ON(ctor || dtor);
1772 * Enable debugging if selected on the kernel commandline.
1774 if (slub_debug && (!slub_debug_slabs ||
1775 strncmp(slub_debug_slabs, name,
1776 strlen(slub_debug_slabs)) == 0))
1777 s->flags |= slub_debug;
1779 if (!calculate_sizes(s))
1784 s->defrag_ratio = 100;
1787 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1790 if (flags & SLAB_PANIC)
1791 panic("Cannot create slab %s size=%lu realsize=%u "
1792 "order=%u offset=%u flags=%lx\n",
1793 s->name, (unsigned long)size, s->size, s->order,
1797 EXPORT_SYMBOL(kmem_cache_open);
1800 * Check if a given pointer is valid
1802 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1807 page = get_object_page(object);
1809 if (!page || s != page->slab)
1810 /* No slab or wrong slab */
1813 addr = page_address(page);
1814 if (object < addr || object >= addr + s->objects * s->size)
1818 if ((object - addr) % s->size)
1819 /* Improperly aligned */
1823 * We could also check if the object is on the slabs freelist.
1824 * But this would be too expensive and it seems that the main
1825 * purpose of kmem_ptr_valid is to check if the object belongs
1826 * to a certain slab.
1830 EXPORT_SYMBOL(kmem_ptr_validate);
1833 * Determine the size of a slab object
1835 unsigned int kmem_cache_size(struct kmem_cache *s)
1839 EXPORT_SYMBOL(kmem_cache_size);
1841 const char *kmem_cache_name(struct kmem_cache *s)
1845 EXPORT_SYMBOL(kmem_cache_name);
1848 * Attempt to free all slabs on a node
1850 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1851 struct list_head *list)
1853 int slabs_inuse = 0;
1854 unsigned long flags;
1855 struct page *page, *h;
1857 spin_lock_irqsave(&n->list_lock, flags);
1858 list_for_each_entry_safe(page, h, list, lru)
1860 list_del(&page->lru);
1861 discard_slab(s, page);
1864 spin_unlock_irqrestore(&n->list_lock, flags);
1869 * Release all resources used by slab cache
1871 static int kmem_cache_close(struct kmem_cache *s)
1877 /* Attempt to free all objects */
1878 for_each_online_node(node) {
1879 struct kmem_cache_node *n = get_node(s, node);
1881 n->nr_partial -= free_list(s, n, &n->partial);
1882 if (atomic_long_read(&n->nr_slabs))
1885 free_kmem_cache_nodes(s);
1890 * Close a cache and release the kmem_cache structure
1891 * (must be used for caches created using kmem_cache_create)
1893 void kmem_cache_destroy(struct kmem_cache *s)
1895 down_write(&slub_lock);
1899 if (kmem_cache_close(s))
1901 sysfs_slab_remove(s);
1904 up_write(&slub_lock);
1906 EXPORT_SYMBOL(kmem_cache_destroy);
1908 /********************************************************************
1910 *******************************************************************/
1912 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1913 EXPORT_SYMBOL(kmalloc_caches);
1915 #ifdef CONFIG_ZONE_DMA
1916 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1919 static int __init setup_slub_min_order(char *str)
1921 get_option (&str, &slub_min_order);
1926 __setup("slub_min_order=", setup_slub_min_order);
1928 static int __init setup_slub_max_order(char *str)
1930 get_option (&str, &slub_max_order);
1935 __setup("slub_max_order=", setup_slub_max_order);
1937 static int __init setup_slub_min_objects(char *str)
1939 get_option (&str, &slub_min_objects);
1944 __setup("slub_min_objects=", setup_slub_min_objects);
1946 static int __init setup_slub_nomerge(char *str)
1952 __setup("slub_nomerge", setup_slub_nomerge);
1954 static int __init setup_slub_debug(char *str)
1956 if (!str || *str != '=')
1957 slub_debug = DEBUG_DEFAULT_FLAGS;
1960 if (*str == 0 || *str == ',')
1961 slub_debug = DEBUG_DEFAULT_FLAGS;
1963 for( ;*str && *str != ','; str++)
1965 case 'f' : case 'F' :
1966 slub_debug |= SLAB_DEBUG_FREE;
1968 case 'z' : case 'Z' :
1969 slub_debug |= SLAB_RED_ZONE;
1971 case 'p' : case 'P' :
1972 slub_debug |= SLAB_POISON;
1974 case 'u' : case 'U' :
1975 slub_debug |= SLAB_STORE_USER;
1977 case 't' : case 'T' :
1978 slub_debug |= SLAB_TRACE;
1981 printk(KERN_ERR "slub_debug option '%c' "
1982 "unknown. skipped\n",*str);
1987 slub_debug_slabs = str + 1;
1991 __setup("slub_debug", setup_slub_debug);
1993 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1994 const char *name, int size, gfp_t gfp_flags)
1996 unsigned int flags = 0;
1998 if (gfp_flags & SLUB_DMA)
1999 flags = SLAB_CACHE_DMA;
2001 down_write(&slub_lock);
2002 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2006 list_add(&s->list, &slab_caches);
2007 up_write(&slub_lock);
2008 if (sysfs_slab_add(s))
2013 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2016 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2018 int index = kmalloc_index(size);
2023 /* Allocation too large? */
2026 #ifdef CONFIG_ZONE_DMA
2027 if ((flags & SLUB_DMA)) {
2028 struct kmem_cache *s;
2029 struct kmem_cache *x;
2033 s = kmalloc_caches_dma[index];
2037 /* Dynamically create dma cache */
2038 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2040 panic("Unable to allocate memory for dma cache\n");
2042 if (index <= KMALLOC_SHIFT_HIGH)
2043 realsize = 1 << index;
2051 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2052 (unsigned int)realsize);
2053 s = create_kmalloc_cache(x, text, realsize, flags);
2054 kmalloc_caches_dma[index] = s;
2058 return &kmalloc_caches[index];
2061 void *__kmalloc(size_t size, gfp_t flags)
2063 struct kmem_cache *s = get_slab(size, flags);
2066 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2069 EXPORT_SYMBOL(__kmalloc);
2072 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2074 struct kmem_cache *s = get_slab(size, flags);
2077 return slab_alloc(s, flags, node, __builtin_return_address(0));
2080 EXPORT_SYMBOL(__kmalloc_node);
2083 size_t ksize(const void *object)
2085 struct page *page = get_object_page(object);
2086 struct kmem_cache *s;
2093 * Debugging requires use of the padding between object
2094 * and whatever may come after it.
2096 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2100 * If we have the need to store the freelist pointer
2101 * back there or track user information then we can
2102 * only use the space before that information.
2104 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2108 * Else we can use all the padding etc for the allocation
2112 EXPORT_SYMBOL(ksize);
2114 void kfree(const void *x)
2116 struct kmem_cache *s;
2122 page = virt_to_head_page(x);
2125 slab_free(s, page, (void *)x, __builtin_return_address(0));
2127 EXPORT_SYMBOL(kfree);
2130 * kmem_cache_shrink removes empty slabs from the partial lists
2131 * and then sorts the partially allocated slabs by the number
2132 * of items in use. The slabs with the most items in use
2133 * come first. New allocations will remove these from the
2134 * partial list because they are full. The slabs with the
2135 * least items are placed last. If it happens that the objects
2136 * are freed then the page can be returned to the page allocator.
2138 int kmem_cache_shrink(struct kmem_cache *s)
2142 struct kmem_cache_node *n;
2145 struct list_head *slabs_by_inuse =
2146 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2147 unsigned long flags;
2149 if (!slabs_by_inuse)
2153 for_each_online_node(node) {
2154 n = get_node(s, node);
2159 for (i = 0; i < s->objects; i++)
2160 INIT_LIST_HEAD(slabs_by_inuse + i);
2162 spin_lock_irqsave(&n->list_lock, flags);
2165 * Build lists indexed by the items in use in
2166 * each slab or free slabs if empty.
2168 * Note that concurrent frees may occur while
2169 * we hold the list_lock. page->inuse here is
2172 list_for_each_entry_safe(page, t, &n->partial, lru) {
2173 if (!page->inuse && slab_trylock(page)) {
2175 * Must hold slab lock here because slab_free
2176 * may have freed the last object and be
2177 * waiting to release the slab.
2179 list_del(&page->lru);
2182 discard_slab(s, page);
2184 if (n->nr_partial > MAX_PARTIAL)
2185 list_move(&page->lru,
2186 slabs_by_inuse + page->inuse);
2190 if (n->nr_partial <= MAX_PARTIAL)
2194 * Rebuild the partial list with the slabs filled up
2195 * most first and the least used slabs at the end.
2197 for (i = s->objects - 1; i >= 0; i--)
2198 list_splice(slabs_by_inuse + i, n->partial.prev);
2201 spin_unlock_irqrestore(&n->list_lock, flags);
2204 kfree(slabs_by_inuse);
2207 EXPORT_SYMBOL(kmem_cache_shrink);
2210 * krealloc - reallocate memory. The contents will remain unchanged.
2212 * @p: object to reallocate memory for.
2213 * @new_size: how many bytes of memory are required.
2214 * @flags: the type of memory to allocate.
2216 * The contents of the object pointed to are preserved up to the
2217 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2218 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2219 * %NULL pointer, the object pointed to is freed.
2221 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2223 struct kmem_cache *new_cache;
2228 return kmalloc(new_size, flags);
2230 if (unlikely(!new_size)) {
2235 page = virt_to_head_page(p);
2237 new_cache = get_slab(new_size, flags);
2240 * If new size fits in the current cache, bail out.
2242 if (likely(page->slab == new_cache))
2245 ret = kmalloc(new_size, flags);
2247 memcpy(ret, p, min(new_size, ksize(p)));
2252 EXPORT_SYMBOL(krealloc);
2254 /********************************************************************
2255 * Basic setup of slabs
2256 *******************************************************************/
2258 void __init kmem_cache_init(void)
2264 * Must first have the slab cache available for the allocations of the
2265 * struct kmalloc_cache_node's. There is special bootstrap code in
2266 * kmem_cache_open for slab_state == DOWN.
2268 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2269 sizeof(struct kmem_cache_node), GFP_KERNEL);
2272 /* Able to allocate the per node structures */
2273 slab_state = PARTIAL;
2275 /* Caches that are not of the two-to-the-power-of size */
2276 create_kmalloc_cache(&kmalloc_caches[1],
2277 "kmalloc-96", 96, GFP_KERNEL);
2278 create_kmalloc_cache(&kmalloc_caches[2],
2279 "kmalloc-192", 192, GFP_KERNEL);
2281 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2282 create_kmalloc_cache(&kmalloc_caches[i],
2283 "kmalloc", 1 << i, GFP_KERNEL);
2287 /* Provide the correct kmalloc names now that the caches are up */
2288 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2289 kmalloc_caches[i]. name =
2290 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2293 register_cpu_notifier(&slab_notifier);
2296 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2297 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2298 + nr_cpu_ids * sizeof(struct page *);
2300 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2301 " Processors=%d, Nodes=%d\n",
2302 KMALLOC_SHIFT_HIGH, L1_CACHE_BYTES,
2303 slub_min_order, slub_max_order, slub_min_objects,
2304 nr_cpu_ids, nr_node_ids);
2308 * Find a mergeable slab cache
2310 static int slab_unmergeable(struct kmem_cache *s)
2312 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2315 if (s->ctor || s->dtor)
2321 static struct kmem_cache *find_mergeable(size_t size,
2322 size_t align, unsigned long flags,
2323 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2324 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2326 struct list_head *h;
2328 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2334 size = ALIGN(size, sizeof(void *));
2335 align = calculate_alignment(flags, align, size);
2336 size = ALIGN(size, align);
2338 list_for_each(h, &slab_caches) {
2339 struct kmem_cache *s =
2340 container_of(h, struct kmem_cache, list);
2342 if (slab_unmergeable(s))
2348 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2349 (s->flags & SLUB_MERGE_SAME))
2352 * Check if alignment is compatible.
2353 * Courtesy of Adrian Drzewiecki
2355 if ((s->size & ~(align -1)) != s->size)
2358 if (s->size - size >= sizeof(void *))
2366 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2367 size_t align, unsigned long flags,
2368 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2369 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2371 struct kmem_cache *s;
2373 down_write(&slub_lock);
2374 s = find_mergeable(size, align, flags, dtor, ctor);
2378 * Adjust the object sizes so that we clear
2379 * the complete object on kzalloc.
2381 s->objsize = max(s->objsize, (int)size);
2382 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2383 if (sysfs_slab_alias(s, name))
2386 s = kmalloc(kmem_size, GFP_KERNEL);
2387 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2388 size, align, flags, ctor, dtor)) {
2389 if (sysfs_slab_add(s)) {
2393 list_add(&s->list, &slab_caches);
2397 up_write(&slub_lock);
2401 up_write(&slub_lock);
2402 if (flags & SLAB_PANIC)
2403 panic("Cannot create slabcache %s\n", name);
2408 EXPORT_SYMBOL(kmem_cache_create);
2410 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2414 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2416 memset(x, 0, s->objsize);
2419 EXPORT_SYMBOL(kmem_cache_zalloc);
2422 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2424 struct list_head *h;
2426 down_read(&slub_lock);
2427 list_for_each(h, &slab_caches) {
2428 struct kmem_cache *s =
2429 container_of(h, struct kmem_cache, list);
2433 up_read(&slub_lock);
2437 * Use the cpu notifier to insure that the slab are flushed
2440 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2441 unsigned long action, void *hcpu)
2443 long cpu = (long)hcpu;
2446 case CPU_UP_CANCELED:
2448 for_all_slabs(__flush_cpu_slab, cpu);
2456 static struct notifier_block __cpuinitdata slab_notifier =
2457 { &slab_cpuup_callback, NULL, 0 };
2463 /*****************************************************************
2464 * Generic reaper used to support the page allocator
2465 * (the cpu slabs are reaped by a per slab workqueue).
2467 * Maybe move this to the page allocator?
2468 ****************************************************************/
2470 static DEFINE_PER_CPU(unsigned long, reap_node);
2472 static void init_reap_node(int cpu)
2476 node = next_node(cpu_to_node(cpu), node_online_map);
2477 if (node == MAX_NUMNODES)
2478 node = first_node(node_online_map);
2480 __get_cpu_var(reap_node) = node;
2483 static void next_reap_node(void)
2485 int node = __get_cpu_var(reap_node);
2488 * Also drain per cpu pages on remote zones
2490 if (node != numa_node_id())
2491 drain_node_pages(node);
2493 node = next_node(node, node_online_map);
2494 if (unlikely(node >= MAX_NUMNODES))
2495 node = first_node(node_online_map);
2496 __get_cpu_var(reap_node) = node;
2499 #define init_reap_node(cpu) do { } while (0)
2500 #define next_reap_node(void) do { } while (0)
2503 #define REAPTIMEOUT_CPUC (2*HZ)
2506 static DEFINE_PER_CPU(struct delayed_work, reap_work);
2508 static void cache_reap(struct work_struct *unused)
2511 refresh_cpu_vm_stats(smp_processor_id());
2512 schedule_delayed_work(&__get_cpu_var(reap_work),
2516 static void __devinit start_cpu_timer(int cpu)
2518 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2521 * When this gets called from do_initcalls via cpucache_init(),
2522 * init_workqueues() has already run, so keventd will be setup
2525 if (keventd_up() && reap_work->work.func == NULL) {
2526 init_reap_node(cpu);
2527 INIT_DELAYED_WORK(reap_work, cache_reap);
2528 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2532 static int __init cpucache_init(void)
2537 * Register the timers that drain pcp pages and update vm statistics
2539 for_each_online_cpu(cpu)
2540 start_cpu_timer(cpu);
2543 __initcall(cpucache_init);
2546 #ifdef SLUB_RESILIENCY_TEST
2547 static unsigned long validate_slab_cache(struct kmem_cache *s);
2549 static void resiliency_test(void)
2553 printk(KERN_ERR "SLUB resiliency testing\n");
2554 printk(KERN_ERR "-----------------------\n");
2555 printk(KERN_ERR "A. Corruption after allocation\n");
2557 p = kzalloc(16, GFP_KERNEL);
2559 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2560 " 0x12->0x%p\n\n", p + 16);
2562 validate_slab_cache(kmalloc_caches + 4);
2564 /* Hmmm... The next two are dangerous */
2565 p = kzalloc(32, GFP_KERNEL);
2566 p[32 + sizeof(void *)] = 0x34;
2567 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2568 " 0x34 -> -0x%p\n", p);
2569 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2571 validate_slab_cache(kmalloc_caches + 5);
2572 p = kzalloc(64, GFP_KERNEL);
2573 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2575 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2577 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2578 validate_slab_cache(kmalloc_caches + 6);
2580 printk(KERN_ERR "\nB. Corruption after free\n");
2581 p = kzalloc(128, GFP_KERNEL);
2584 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2585 validate_slab_cache(kmalloc_caches + 7);
2587 p = kzalloc(256, GFP_KERNEL);
2590 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2591 validate_slab_cache(kmalloc_caches + 8);
2593 p = kzalloc(512, GFP_KERNEL);
2596 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2597 validate_slab_cache(kmalloc_caches + 9);
2600 static void resiliency_test(void) {};
2604 * These are not as efficient as kmalloc for the non debug case.
2605 * We do not have the page struct available so we have to touch one
2606 * cacheline in struct kmem_cache to check slab flags.
2608 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2610 struct kmem_cache *s = get_slab(size, gfpflags);
2615 return slab_alloc(s, gfpflags, -1, caller);
2618 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2619 int node, void *caller)
2621 struct kmem_cache *s = get_slab(size, gfpflags);
2626 return slab_alloc(s, gfpflags, node, caller);
2631 static int validate_slab(struct kmem_cache *s, struct page *page)
2634 void *addr = page_address(page);
2635 unsigned long map[BITS_TO_LONGS(s->objects)];
2637 if (!check_slab(s, page) ||
2638 !on_freelist(s, page, NULL))
2641 /* Now we know that a valid freelist exists */
2642 bitmap_zero(map, s->objects);
2644 for(p = page->freelist; p; p = get_freepointer(s, p)) {
2645 set_bit((p - addr) / s->size, map);
2646 if (!check_object(s, page, p, 0))
2650 for(p = addr; p < addr + s->objects * s->size; p += s->size)
2651 if (!test_bit((p - addr) / s->size, map))
2652 if (!check_object(s, page, p, 1))
2657 static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2659 if (slab_trylock(page)) {
2660 validate_slab(s, page);
2663 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2666 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2667 if (!PageError(page))
2668 printk(KERN_ERR "SLUB %s: PageError not set "
2669 "on slab 0x%p\n", s->name, page);
2671 if (PageError(page))
2672 printk(KERN_ERR "SLUB %s: PageError set on "
2673 "slab 0x%p\n", s->name, page);
2677 static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2679 unsigned long count = 0;
2681 unsigned long flags;
2683 spin_lock_irqsave(&n->list_lock, flags);
2685 list_for_each_entry(page, &n->partial, lru) {
2686 validate_slab_slab(s, page);
2689 if (count != n->nr_partial)
2690 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2691 "counter=%ld\n", s->name, count, n->nr_partial);
2693 if (!(s->flags & SLAB_STORE_USER))
2696 list_for_each_entry(page, &n->full, lru) {
2697 validate_slab_slab(s, page);
2700 if (count != atomic_long_read(&n->nr_slabs))
2701 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2702 "counter=%ld\n", s->name, count,
2703 atomic_long_read(&n->nr_slabs));
2706 spin_unlock_irqrestore(&n->list_lock, flags);
2710 static unsigned long validate_slab_cache(struct kmem_cache *s)
2713 unsigned long count = 0;
2716 for_each_online_node(node) {
2717 struct kmem_cache_node *n = get_node(s, node);
2719 count += validate_slab_node(s, n);
2725 * Generate lists of locations where slabcache objects are allocated
2730 unsigned long count;
2736 unsigned long count;
2737 struct location *loc;
2740 static void free_loc_track(struct loc_track *t)
2743 free_pages((unsigned long)t->loc,
2744 get_order(sizeof(struct location) * t->max));
2747 static int alloc_loc_track(struct loc_track *t, unsigned long max)
2753 max = PAGE_SIZE / sizeof(struct location);
2755 order = get_order(sizeof(struct location) * max);
2757 l = (void *)__get_free_pages(GFP_KERNEL, order);
2763 memcpy(l, t->loc, sizeof(struct location) * t->count);
2771 static int add_location(struct loc_track *t, struct kmem_cache *s,
2774 long start, end, pos;
2782 pos = start + (end - start + 1) / 2;
2785 * There is nothing at "end". If we end up there
2786 * we need to add something to before end.
2791 caddr = t->loc[pos].addr;
2792 if (addr == caddr) {
2793 t->loc[pos].count++;
2804 * Not found. Insert new tracking element
2806 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2812 (t->count - pos) * sizeof(struct location));
2819 static void process_slab(struct loc_track *t, struct kmem_cache *s,
2820 struct page *page, enum track_item alloc)
2822 void *addr = page_address(page);
2823 unsigned long map[BITS_TO_LONGS(s->objects)];
2826 bitmap_zero(map, s->objects);
2827 for (p = page->freelist; p; p = get_freepointer(s, p))
2828 set_bit((p - addr) / s->size, map);
2830 for (p = addr; p < addr + s->objects * s->size; p += s->size)
2831 if (!test_bit((p - addr) / s->size, map)) {
2832 void *addr = get_track(s, p, alloc)->addr;
2834 add_location(t, s, addr);
2838 static int list_locations(struct kmem_cache *s, char *buf,
2839 enum track_item alloc)
2849 /* Push back cpu slabs */
2852 for_each_online_node(node) {
2853 struct kmem_cache_node *n = get_node(s, node);
2854 unsigned long flags;
2857 if (!atomic_read(&n->nr_slabs))
2860 spin_lock_irqsave(&n->list_lock, flags);
2861 list_for_each_entry(page, &n->partial, lru)
2862 process_slab(&t, s, page, alloc);
2863 list_for_each_entry(page, &n->full, lru)
2864 process_slab(&t, s, page, alloc);
2865 spin_unlock_irqrestore(&n->list_lock, flags);
2868 for (i = 0; i < t.count; i++) {
2869 void *addr = t.loc[i].addr;
2871 if (n > PAGE_SIZE - 100)
2873 n += sprintf(buf + n, "%7ld ", t.loc[i].count);
2875 n += sprint_symbol(buf + n, (unsigned long)t.loc[i].addr);
2877 n += sprintf(buf + n, "<not-available>");
2878 n += sprintf(buf + n, "\n");
2883 n += sprintf(buf, "No data\n");
2887 static unsigned long count_partial(struct kmem_cache_node *n)
2889 unsigned long flags;
2890 unsigned long x = 0;
2893 spin_lock_irqsave(&n->list_lock, flags);
2894 list_for_each_entry(page, &n->partial, lru)
2896 spin_unlock_irqrestore(&n->list_lock, flags);
2900 enum slab_stat_type {
2907 #define SO_FULL (1 << SL_FULL)
2908 #define SO_PARTIAL (1 << SL_PARTIAL)
2909 #define SO_CPU (1 << SL_CPU)
2910 #define SO_OBJECTS (1 << SL_OBJECTS)
2912 static unsigned long slab_objects(struct kmem_cache *s,
2913 char *buf, unsigned long flags)
2915 unsigned long total = 0;
2919 unsigned long *nodes;
2920 unsigned long *per_cpu;
2922 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2923 per_cpu = nodes + nr_node_ids;
2925 for_each_possible_cpu(cpu) {
2926 struct page *page = s->cpu_slab[cpu];
2930 node = page_to_nid(page);
2931 if (flags & SO_CPU) {
2934 if (flags & SO_OBJECTS)
2945 for_each_online_node(node) {
2946 struct kmem_cache_node *n = get_node(s, node);
2948 if (flags & SO_PARTIAL) {
2949 if (flags & SO_OBJECTS)
2950 x = count_partial(n);
2957 if (flags & SO_FULL) {
2958 int full_slabs = atomic_read(&n->nr_slabs)
2962 if (flags & SO_OBJECTS)
2963 x = full_slabs * s->objects;
2971 x = sprintf(buf, "%lu", total);
2973 for_each_online_node(node)
2975 x += sprintf(buf + x, " N%d=%lu",
2979 return x + sprintf(buf + x, "\n");
2982 static int any_slab_objects(struct kmem_cache *s)
2987 for_each_possible_cpu(cpu)
2988 if (s->cpu_slab[cpu])
2991 for_each_node(node) {
2992 struct kmem_cache_node *n = get_node(s, node);
2994 if (n->nr_partial || atomic_read(&n->nr_slabs))
3000 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3001 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3003 struct slab_attribute {
3004 struct attribute attr;
3005 ssize_t (*show)(struct kmem_cache *s, char *buf);
3006 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3009 #define SLAB_ATTR_RO(_name) \
3010 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3012 #define SLAB_ATTR(_name) \
3013 static struct slab_attribute _name##_attr = \
3014 __ATTR(_name, 0644, _name##_show, _name##_store)
3016 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3018 return sprintf(buf, "%d\n", s->size);
3020 SLAB_ATTR_RO(slab_size);
3022 static ssize_t align_show(struct kmem_cache *s, char *buf)
3024 return sprintf(buf, "%d\n", s->align);
3026 SLAB_ATTR_RO(align);
3028 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3030 return sprintf(buf, "%d\n", s->objsize);
3032 SLAB_ATTR_RO(object_size);
3034 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3036 return sprintf(buf, "%d\n", s->objects);
3038 SLAB_ATTR_RO(objs_per_slab);
3040 static ssize_t order_show(struct kmem_cache *s, char *buf)
3042 return sprintf(buf, "%d\n", s->order);
3044 SLAB_ATTR_RO(order);
3046 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3049 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3051 return n + sprintf(buf + n, "\n");
3057 static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3060 int n = sprint_symbol(buf, (unsigned long)s->dtor);
3062 return n + sprintf(buf + n, "\n");
3068 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3070 return sprintf(buf, "%d\n", s->refcount - 1);
3072 SLAB_ATTR_RO(aliases);
3074 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3076 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3078 SLAB_ATTR_RO(slabs);
3080 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3082 return slab_objects(s, buf, SO_PARTIAL);
3084 SLAB_ATTR_RO(partial);
3086 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3088 return slab_objects(s, buf, SO_CPU);
3090 SLAB_ATTR_RO(cpu_slabs);
3092 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3094 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3096 SLAB_ATTR_RO(objects);
3098 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3100 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3103 static ssize_t sanity_checks_store(struct kmem_cache *s,
3104 const char *buf, size_t length)
3106 s->flags &= ~SLAB_DEBUG_FREE;
3108 s->flags |= SLAB_DEBUG_FREE;
3111 SLAB_ATTR(sanity_checks);
3113 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3115 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3118 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3121 s->flags &= ~SLAB_TRACE;
3123 s->flags |= SLAB_TRACE;
3128 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3130 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3133 static ssize_t reclaim_account_store(struct kmem_cache *s,
3134 const char *buf, size_t length)
3136 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3138 s->flags |= SLAB_RECLAIM_ACCOUNT;
3141 SLAB_ATTR(reclaim_account);
3143 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3145 return sprintf(buf, "%d\n", !!(s->flags &
3146 (SLAB_HWCACHE_ALIGN|SLAB_MUST_HWCACHE_ALIGN)));
3148 SLAB_ATTR_RO(hwcache_align);
3150 #ifdef CONFIG_ZONE_DMA
3151 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3153 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3155 SLAB_ATTR_RO(cache_dma);
3158 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3160 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3162 SLAB_ATTR_RO(destroy_by_rcu);
3164 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3166 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3169 static ssize_t red_zone_store(struct kmem_cache *s,
3170 const char *buf, size_t length)
3172 if (any_slab_objects(s))
3175 s->flags &= ~SLAB_RED_ZONE;
3177 s->flags |= SLAB_RED_ZONE;
3181 SLAB_ATTR(red_zone);
3183 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3185 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3188 static ssize_t poison_store(struct kmem_cache *s,
3189 const char *buf, size_t length)
3191 if (any_slab_objects(s))
3194 s->flags &= ~SLAB_POISON;
3196 s->flags |= SLAB_POISON;
3202 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3204 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3207 static ssize_t store_user_store(struct kmem_cache *s,
3208 const char *buf, size_t length)
3210 if (any_slab_objects(s))
3213 s->flags &= ~SLAB_STORE_USER;
3215 s->flags |= SLAB_STORE_USER;
3219 SLAB_ATTR(store_user);
3221 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3226 static ssize_t validate_store(struct kmem_cache *s,
3227 const char *buf, size_t length)
3230 validate_slab_cache(s);
3235 SLAB_ATTR(validate);
3237 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3242 static ssize_t shrink_store(struct kmem_cache *s,
3243 const char *buf, size_t length)
3245 if (buf[0] == '1') {
3246 int rc = kmem_cache_shrink(s);
3256 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3258 if (!(s->flags & SLAB_STORE_USER))
3260 return list_locations(s, buf, TRACK_ALLOC);
3262 SLAB_ATTR_RO(alloc_calls);
3264 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3266 if (!(s->flags & SLAB_STORE_USER))
3268 return list_locations(s, buf, TRACK_FREE);
3270 SLAB_ATTR_RO(free_calls);
3273 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3275 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3278 static ssize_t defrag_ratio_store(struct kmem_cache *s,
3279 const char *buf, size_t length)
3281 int n = simple_strtoul(buf, NULL, 10);
3284 s->defrag_ratio = n * 10;
3287 SLAB_ATTR(defrag_ratio);
3290 static struct attribute * slab_attrs[] = {
3291 &slab_size_attr.attr,
3292 &object_size_attr.attr,
3293 &objs_per_slab_attr.attr,
3298 &cpu_slabs_attr.attr,
3303 &sanity_checks_attr.attr,
3305 &hwcache_align_attr.attr,
3306 &reclaim_account_attr.attr,
3307 &destroy_by_rcu_attr.attr,
3308 &red_zone_attr.attr,
3310 &store_user_attr.attr,
3311 &validate_attr.attr,
3313 &alloc_calls_attr.attr,
3314 &free_calls_attr.attr,
3315 #ifdef CONFIG_ZONE_DMA
3316 &cache_dma_attr.attr,
3319 &defrag_ratio_attr.attr,
3324 static struct attribute_group slab_attr_group = {
3325 .attrs = slab_attrs,
3328 static ssize_t slab_attr_show(struct kobject *kobj,
3329 struct attribute *attr,
3332 struct slab_attribute *attribute;
3333 struct kmem_cache *s;
3336 attribute = to_slab_attr(attr);
3339 if (!attribute->show)
3342 err = attribute->show(s, buf);
3347 static ssize_t slab_attr_store(struct kobject *kobj,
3348 struct attribute *attr,
3349 const char *buf, size_t len)
3351 struct slab_attribute *attribute;
3352 struct kmem_cache *s;
3355 attribute = to_slab_attr(attr);
3358 if (!attribute->store)
3361 err = attribute->store(s, buf, len);
3366 static struct sysfs_ops slab_sysfs_ops = {
3367 .show = slab_attr_show,
3368 .store = slab_attr_store,
3371 static struct kobj_type slab_ktype = {
3372 .sysfs_ops = &slab_sysfs_ops,
3375 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3377 struct kobj_type *ktype = get_ktype(kobj);
3379 if (ktype == &slab_ktype)
3384 static struct kset_uevent_ops slab_uevent_ops = {
3385 .filter = uevent_filter,
3388 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3390 #define ID_STR_LENGTH 64
3392 /* Create a unique string id for a slab cache:
3394 * :[flags-]size:[memory address of kmemcache]
3396 static char *create_unique_id(struct kmem_cache *s)
3398 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3405 * First flags affecting slabcache operations. We will only
3406 * get here for aliasable slabs so we do not need to support
3407 * too many flags. The flags here must cover all flags that
3408 * are matched during merging to guarantee that the id is
3411 if (s->flags & SLAB_CACHE_DMA)
3413 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3415 if (s->flags & SLAB_DEBUG_FREE)
3419 p += sprintf(p, "%07d", s->size);
3420 BUG_ON(p > name + ID_STR_LENGTH - 1);
3424 static int sysfs_slab_add(struct kmem_cache *s)
3430 if (slab_state < SYSFS)
3431 /* Defer until later */
3434 unmergeable = slab_unmergeable(s);
3437 * Slabcache can never be merged so we can use the name proper.
3438 * This is typically the case for debug situations. In that
3439 * case we can catch duplicate names easily.
3441 sysfs_remove_link(&slab_subsys.kset.kobj, s->name);
3445 * Create a unique name for the slab as a target
3448 name = create_unique_id(s);
3451 kobj_set_kset_s(s, slab_subsys);
3452 kobject_set_name(&s->kobj, name);
3453 kobject_init(&s->kobj);
3454 err = kobject_add(&s->kobj);
3458 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3461 kobject_uevent(&s->kobj, KOBJ_ADD);
3463 /* Setup first alias */
3464 sysfs_slab_alias(s, s->name);
3470 static void sysfs_slab_remove(struct kmem_cache *s)
3472 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3473 kobject_del(&s->kobj);
3477 * Need to buffer aliases during bootup until sysfs becomes
3478 * available lest we loose that information.
3480 struct saved_alias {
3481 struct kmem_cache *s;
3483 struct saved_alias *next;
3486 struct saved_alias *alias_list;
3488 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3490 struct saved_alias *al;
3492 if (slab_state == SYSFS) {
3494 * If we have a leftover link then remove it.
3496 sysfs_remove_link(&slab_subsys.kset.kobj, name);
3497 return sysfs_create_link(&slab_subsys.kset.kobj,
3501 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3507 al->next = alias_list;
3512 static int __init slab_sysfs_init(void)
3516 err = subsystem_register(&slab_subsys);
3518 printk(KERN_ERR "Cannot register slab subsystem.\n");
3524 while (alias_list) {
3525 struct saved_alias *al = alias_list;
3527 alias_list = alias_list->next;
3528 err = sysfs_slab_alias(al->s, al->name);
3537 __initcall(slab_sysfs_init);
3539 __initcall(finish_bootstrap);