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 and during regular
70 * operations no list for full slabs is used. If an object in a full slab is
71 * freed then the slab will show up again on the partial lists.
72 * We track full slabs for debugging purposes though because otherwise we
73 * cannot scan all objects.
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
90 static inline int SlabDebug(struct page *page)
92 return PageError(page);
95 static inline void SetSlabDebug(struct page *page)
100 static inline void ClearSlabDebug(struct page *page)
102 ClearPageError(page);
106 * Issues still to be resolved:
108 * - The per cpu array is updated for each new slab and and is a remote
109 * cacheline for most nodes. This could become a bouncing cacheline given
110 * enough frequent updates. There are 16 pointers in a cacheline, so at
111 * max 16 cpus could compete for the cacheline which may be okay.
113 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
115 * - Variable sizing of the per node arrays
118 /* Enable to test recovery from slab corruption on boot */
119 #undef SLUB_RESILIENCY_TEST
124 * Small page size. Make sure that we do not fragment memory
126 #define DEFAULT_MAX_ORDER 1
127 #define DEFAULT_MIN_OBJECTS 4
132 * Large page machines are customarily able to handle larger
135 #define DEFAULT_MAX_ORDER 2
136 #define DEFAULT_MIN_OBJECTS 8
141 * Mininum number of partial slabs. These will be left on the partial
142 * lists even if they are empty. kmem_cache_shrink may reclaim them.
144 #define MIN_PARTIAL 2
147 * Maximum number of desirable partial slabs.
148 * The existence of more partial slabs makes kmem_cache_shrink
149 * sort the partial list by the number of objects in the.
151 #define MAX_PARTIAL 10
153 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
154 SLAB_POISON | SLAB_STORE_USER)
157 * Set of flags that will prevent slab merging
159 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
160 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
162 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
165 #ifndef ARCH_KMALLOC_MINALIGN
166 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
169 #ifndef ARCH_SLAB_MINALIGN
170 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
173 /* Internal SLUB flags */
174 #define __OBJECT_POISON 0x80000000 /* Poison object */
176 /* Not all arches define cache_line_size */
177 #ifndef cache_line_size
178 #define cache_line_size() L1_CACHE_BYTES
181 static int kmem_size = sizeof(struct kmem_cache);
184 static struct notifier_block slab_notifier;
188 DOWN, /* No slab functionality available */
189 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
190 UP, /* Everything works but does not show up in sysfs */
194 /* A list of all slab caches on the system */
195 static DECLARE_RWSEM(slub_lock);
196 LIST_HEAD(slab_caches);
199 static int sysfs_slab_add(struct kmem_cache *);
200 static int sysfs_slab_alias(struct kmem_cache *, const char *);
201 static void sysfs_slab_remove(struct kmem_cache *);
203 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
204 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
205 static void sysfs_slab_remove(struct kmem_cache *s) {}
208 /********************************************************************
209 * Core slab cache functions
210 *******************************************************************/
212 int slab_is_available(void)
214 return slab_state >= UP;
217 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
220 return s->node[node];
222 return &s->local_node;
227 * Slow version of get and set free pointer.
229 * This version requires touching the cache lines of kmem_cache which
230 * we avoid to do in the fast alloc free paths. There we obtain the offset
231 * from the page struct.
233 static inline void *get_freepointer(struct kmem_cache *s, void *object)
235 return *(void **)(object + s->offset);
238 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
240 *(void **)(object + s->offset) = fp;
243 /* Loop over all objects in a slab */
244 #define for_each_object(__p, __s, __addr) \
245 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
249 #define for_each_free_object(__p, __s, __free) \
250 for (__p = (__free); __p; __p = get_freepointer((__s), __p))
252 /* Determine object index from a given position */
253 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
255 return (p - addr) / s->size;
261 static void print_section(char *text, u8 *addr, unsigned int length)
269 for (i = 0; i < length; i++) {
271 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
274 printk(" %02x", addr[i]);
276 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
278 printk(" %s\n",ascii);
289 printk(" %s\n", ascii);
294 * Tracking user of a slab.
297 void *addr; /* Called from address */
298 int cpu; /* Was running on cpu */
299 int pid; /* Pid context */
300 unsigned long when; /* When did the operation occur */
303 enum track_item { TRACK_ALLOC, TRACK_FREE };
305 static struct track *get_track(struct kmem_cache *s, void *object,
306 enum track_item alloc)
311 p = object + s->offset + sizeof(void *);
313 p = object + s->inuse;
318 static void set_track(struct kmem_cache *s, void *object,
319 enum track_item alloc, void *addr)
324 p = object + s->offset + sizeof(void *);
326 p = object + s->inuse;
331 p->cpu = smp_processor_id();
332 p->pid = current ? current->pid : -1;
335 memset(p, 0, sizeof(struct track));
338 static void init_tracking(struct kmem_cache *s, void *object)
340 if (s->flags & SLAB_STORE_USER) {
341 set_track(s, object, TRACK_FREE, NULL);
342 set_track(s, object, TRACK_ALLOC, NULL);
346 static void print_track(const char *s, struct track *t)
351 printk(KERN_ERR "%s: ", s);
352 __print_symbol("%s", (unsigned long)t->addr);
353 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
356 static void print_trailer(struct kmem_cache *s, u8 *p)
358 unsigned int off; /* Offset of last byte */
360 if (s->flags & SLAB_RED_ZONE)
361 print_section("Redzone", p + s->objsize,
362 s->inuse - s->objsize);
364 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
366 get_freepointer(s, p));
369 off = s->offset + sizeof(void *);
373 if (s->flags & SLAB_STORE_USER) {
374 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
375 print_track("Last free ", get_track(s, p, TRACK_FREE));
376 off += 2 * sizeof(struct track);
380 /* Beginning of the filler is the free pointer */
381 print_section("Filler", p + off, s->size - off);
384 static void object_err(struct kmem_cache *s, struct page *page,
385 u8 *object, char *reason)
387 u8 *addr = page_address(page);
389 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
390 s->name, reason, object, page);
391 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
392 object - addr, page->flags, page->inuse, page->freelist);
393 if (object > addr + 16)
394 print_section("Bytes b4", object - 16, 16);
395 print_section("Object", object, min(s->objsize, 128));
396 print_trailer(s, object);
400 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
405 va_start(args, reason);
406 vsnprintf(buf, sizeof(buf), reason, args);
408 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
413 static void init_object(struct kmem_cache *s, void *object, int active)
417 if (s->flags & __OBJECT_POISON) {
418 memset(p, POISON_FREE, s->objsize - 1);
419 p[s->objsize -1] = POISON_END;
422 if (s->flags & SLAB_RED_ZONE)
423 memset(p + s->objsize,
424 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
425 s->inuse - s->objsize);
428 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
431 if (*start != (u8)value)
439 static inline int check_valid_pointer(struct kmem_cache *s,
440 struct page *page, const void *object)
447 base = page_address(page);
448 if (object < base || object >= base + s->objects * s->size ||
449 (object - base) % s->size) {
460 * Bytes of the object to be managed.
461 * If the freepointer may overlay the object then the free
462 * pointer is the first word of the object.
464 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
467 * object + s->objsize
468 * Padding to reach word boundary. This is also used for Redzoning.
469 * Padding is extended by another word if Redzoning is enabled and
472 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
473 * 0xcc (RED_ACTIVE) for objects in use.
476 * Meta data starts here.
478 * A. Free pointer (if we cannot overwrite object on free)
479 * B. Tracking data for SLAB_STORE_USER
480 * C. Padding to reach required alignment boundary or at mininum
481 * one word if debuggin is on to be able to detect writes
482 * before the word boundary.
484 * Padding is done using 0x5a (POISON_INUSE)
487 * Nothing is used beyond s->size.
489 * If slabcaches are merged then the objsize and inuse boundaries are mostly
490 * ignored. And therefore no slab options that rely on these boundaries
491 * may be used with merged slabcaches.
494 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
495 void *from, void *to)
497 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
498 s->name, message, data, from, to - 1);
499 memset(from, data, to - from);
502 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
504 unsigned long off = s->inuse; /* The end of info */
507 /* Freepointer is placed after the object. */
508 off += sizeof(void *);
510 if (s->flags & SLAB_STORE_USER)
511 /* We also have user information there */
512 off += 2 * sizeof(struct track);
517 if (check_bytes(p + off, POISON_INUSE, s->size - off))
520 object_err(s, page, p, "Object padding check fails");
525 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
529 static int slab_pad_check(struct kmem_cache *s, struct page *page)
532 int length, remainder;
534 if (!(s->flags & SLAB_POISON))
537 p = page_address(page);
538 length = s->objects * s->size;
539 remainder = (PAGE_SIZE << s->order) - length;
543 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
544 slab_err(s, page, "Padding check failed");
545 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
546 p + length + remainder);
552 static int check_object(struct kmem_cache *s, struct page *page,
553 void *object, int active)
556 u8 *endobject = object + s->objsize;
558 if (s->flags & SLAB_RED_ZONE) {
560 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
562 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
563 object_err(s, page, object,
564 active ? "Redzone Active" : "Redzone Inactive");
565 restore_bytes(s, "redzone", red,
566 endobject, object + s->inuse);
570 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
571 !check_bytes(endobject, POISON_INUSE,
572 s->inuse - s->objsize)) {
573 object_err(s, page, p, "Alignment padding check fails");
575 * Fix it so that there will not be another report.
577 * Hmmm... We may be corrupting an object that now expects
578 * to be longer than allowed.
580 restore_bytes(s, "alignment padding", POISON_INUSE,
581 endobject, object + s->inuse);
585 if (s->flags & SLAB_POISON) {
586 if (!active && (s->flags & __OBJECT_POISON) &&
587 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
588 p[s->objsize - 1] != POISON_END)) {
590 object_err(s, page, p, "Poison check failed");
591 restore_bytes(s, "Poison", POISON_FREE,
592 p, p + s->objsize -1);
593 restore_bytes(s, "Poison", POISON_END,
594 p + s->objsize - 1, p + s->objsize);
598 * check_pad_bytes cleans up on its own.
600 check_pad_bytes(s, page, p);
603 if (!s->offset && active)
605 * Object and freepointer overlap. Cannot check
606 * freepointer while object is allocated.
610 /* Check free pointer validity */
611 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
612 object_err(s, page, p, "Freepointer corrupt");
614 * No choice but to zap it and thus loose the remainder
615 * of the free objects in this slab. May cause
616 * another error because the object count is now wrong.
618 set_freepointer(s, p, NULL);
624 static int check_slab(struct kmem_cache *s, struct page *page)
626 VM_BUG_ON(!irqs_disabled());
628 if (!PageSlab(page)) {
629 slab_err(s, page, "Not a valid slab page flags=%lx "
630 "mapping=0x%p count=%d", page->flags, page->mapping,
634 if (page->offset * sizeof(void *) != s->offset) {
635 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
636 "mapping=0x%p count=%d",
637 (unsigned long)(page->offset * sizeof(void *)),
643 if (page->inuse > s->objects) {
644 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
645 "mapping=0x%p count=%d",
646 s->name, page->inuse, s->objects, page->flags,
647 page->mapping, page_count(page));
650 /* Slab_pad_check fixes things up after itself */
651 slab_pad_check(s, page);
656 * Determine if a certain object on a page is on the freelist. Must hold the
657 * slab lock to guarantee that the chains are in a consistent state.
659 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
662 void *fp = page->freelist;
665 while (fp && nr <= s->objects) {
668 if (!check_valid_pointer(s, page, fp)) {
670 object_err(s, page, object,
671 "Freechain corrupt");
672 set_freepointer(s, object, NULL);
675 slab_err(s, page, "Freepointer 0x%p corrupt",
677 page->freelist = NULL;
678 page->inuse = s->objects;
679 printk(KERN_ERR "@@@ SLUB %s: Freelist "
680 "cleared. Slab 0x%p\n",
687 fp = get_freepointer(s, object);
691 if (page->inuse != s->objects - nr) {
692 slab_err(s, page, "Wrong object count. Counter is %d but "
693 "counted were %d", s, page, page->inuse,
695 page->inuse = s->objects - nr;
696 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
697 "Slab @0x%p\n", s->name, page);
699 return search == NULL;
703 * Tracking of fully allocated slabs for debugging purposes.
705 static void add_full(struct kmem_cache_node *n, struct page *page)
707 spin_lock(&n->list_lock);
708 list_add(&page->lru, &n->full);
709 spin_unlock(&n->list_lock);
712 static void remove_full(struct kmem_cache *s, struct page *page)
714 struct kmem_cache_node *n;
716 if (!(s->flags & SLAB_STORE_USER))
719 n = get_node(s, page_to_nid(page));
721 spin_lock(&n->list_lock);
722 list_del(&page->lru);
723 spin_unlock(&n->list_lock);
726 static int alloc_object_checks(struct kmem_cache *s, struct page *page,
729 if (!check_slab(s, page))
732 if (object && !on_freelist(s, page, object)) {
733 slab_err(s, page, "Object 0x%p already allocated", object);
737 if (!check_valid_pointer(s, page, object)) {
738 object_err(s, page, object, "Freelist Pointer check fails");
745 if (!check_object(s, page, object, 0))
750 if (PageSlab(page)) {
752 * If this is a slab page then lets do the best we can
753 * to avoid issues in the future. Marking all objects
754 * as used avoids touching the remaining objects.
756 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
758 page->inuse = s->objects;
759 page->freelist = NULL;
760 /* Fix up fields that may be corrupted */
761 page->offset = s->offset / sizeof(void *);
766 static int free_object_checks(struct kmem_cache *s, struct page *page,
769 if (!check_slab(s, page))
772 if (!check_valid_pointer(s, page, object)) {
773 slab_err(s, page, "Invalid object pointer 0x%p", object);
777 if (on_freelist(s, page, object)) {
778 slab_err(s, page, "Object 0x%p already free", object);
782 if (!check_object(s, page, object, 1))
785 if (unlikely(s != page->slab)) {
787 slab_err(s, page, "Attempt to free object(0x%p) "
788 "outside of slab", object);
792 "SLUB <none>: no slab for object 0x%p.\n",
797 slab_err(s, page, "object at 0x%p belongs "
798 "to slab %s", object, page->slab->name);
803 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
804 s->name, page, object);
808 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
810 if (s->flags & SLAB_TRACE) {
811 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
813 alloc ? "alloc" : "free",
818 print_section("Object", (void *)object, s->objsize);
825 * Slab allocation and freeing
827 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
830 int pages = 1 << s->order;
835 if (s->flags & SLAB_CACHE_DMA)
839 page = alloc_pages(flags, s->order);
841 page = alloc_pages_node(node, flags, s->order);
846 mod_zone_page_state(page_zone(page),
847 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
848 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
854 static void setup_object(struct kmem_cache *s, struct page *page,
857 if (SlabDebug(page)) {
858 init_object(s, object, 0);
859 init_tracking(s, object);
862 if (unlikely(s->ctor))
863 s->ctor(object, s, SLAB_CTOR_CONSTRUCTOR);
866 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
869 struct kmem_cache_node *n;
875 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
877 if (flags & __GFP_WAIT)
880 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
884 n = get_node(s, page_to_nid(page));
886 atomic_long_inc(&n->nr_slabs);
887 page->offset = s->offset / sizeof(void *);
889 page->flags |= 1 << PG_slab;
890 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
891 SLAB_STORE_USER | SLAB_TRACE))
894 start = page_address(page);
895 end = start + s->objects * s->size;
897 if (unlikely(s->flags & SLAB_POISON))
898 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
901 for_each_object(p, s, start) {
902 setup_object(s, page, last);
903 set_freepointer(s, last, p);
906 setup_object(s, page, last);
907 set_freepointer(s, last, NULL);
909 page->freelist = start;
912 if (flags & __GFP_WAIT)
917 static void __free_slab(struct kmem_cache *s, struct page *page)
919 int pages = 1 << s->order;
921 if (unlikely(SlabDebug(page) || s->dtor)) {
924 slab_pad_check(s, page);
925 for_each_object(p, s, page_address(page)) {
928 check_object(s, page, p, 0);
932 mod_zone_page_state(page_zone(page),
933 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
934 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
937 page->mapping = NULL;
938 __free_pages(page, s->order);
941 static void rcu_free_slab(struct rcu_head *h)
945 page = container_of((struct list_head *)h, struct page, lru);
946 __free_slab(page->slab, page);
949 static void free_slab(struct kmem_cache *s, struct page *page)
951 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
953 * RCU free overloads the RCU head over the LRU
955 struct rcu_head *head = (void *)&page->lru;
957 call_rcu(head, rcu_free_slab);
959 __free_slab(s, page);
962 static void discard_slab(struct kmem_cache *s, struct page *page)
964 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
966 atomic_long_dec(&n->nr_slabs);
967 reset_page_mapcount(page);
968 ClearSlabDebug(page);
969 __ClearPageSlab(page);
974 * Per slab locking using the pagelock
976 static __always_inline void slab_lock(struct page *page)
978 bit_spin_lock(PG_locked, &page->flags);
981 static __always_inline void slab_unlock(struct page *page)
983 bit_spin_unlock(PG_locked, &page->flags);
986 static __always_inline int slab_trylock(struct page *page)
990 rc = bit_spin_trylock(PG_locked, &page->flags);
995 * Management of partially allocated slabs
997 static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
999 spin_lock(&n->list_lock);
1001 list_add_tail(&page->lru, &n->partial);
1002 spin_unlock(&n->list_lock);
1005 static void add_partial(struct kmem_cache_node *n, struct page *page)
1007 spin_lock(&n->list_lock);
1009 list_add(&page->lru, &n->partial);
1010 spin_unlock(&n->list_lock);
1013 static void remove_partial(struct kmem_cache *s,
1016 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1018 spin_lock(&n->list_lock);
1019 list_del(&page->lru);
1021 spin_unlock(&n->list_lock);
1025 * Lock slab and remove from the partial list.
1027 * Must hold list_lock.
1029 static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
1031 if (slab_trylock(page)) {
1032 list_del(&page->lru);
1040 * Try to allocate a partial slab from a specific node.
1042 static struct page *get_partial_node(struct kmem_cache_node *n)
1047 * Racy check. If we mistakenly see no partial slabs then we
1048 * just allocate an empty slab. If we mistakenly try to get a
1049 * partial slab and there is none available then get_partials()
1052 if (!n || !n->nr_partial)
1055 spin_lock(&n->list_lock);
1056 list_for_each_entry(page, &n->partial, lru)
1057 if (lock_and_del_slab(n, page))
1061 spin_unlock(&n->list_lock);
1066 * Get a page from somewhere. Search in increasing NUMA distances.
1068 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1071 struct zonelist *zonelist;
1076 * The defrag ratio allows a configuration of the tradeoffs between
1077 * inter node defragmentation and node local allocations. A lower
1078 * defrag_ratio increases the tendency to do local allocations
1079 * instead of attempting to obtain partial slabs from other nodes.
1081 * If the defrag_ratio is set to 0 then kmalloc() always
1082 * returns node local objects. If the ratio is higher then kmalloc()
1083 * may return off node objects because partial slabs are obtained
1084 * from other nodes and filled up.
1086 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1087 * defrag_ratio = 1000) then every (well almost) allocation will
1088 * first attempt to defrag slab caches on other nodes. This means
1089 * scanning over all nodes to look for partial slabs which may be
1090 * expensive if we do it every time we are trying to find a slab
1091 * with available objects.
1093 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1096 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1097 ->node_zonelists[gfp_zone(flags)];
1098 for (z = zonelist->zones; *z; z++) {
1099 struct kmem_cache_node *n;
1101 n = get_node(s, zone_to_nid(*z));
1103 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1104 n->nr_partial > MIN_PARTIAL) {
1105 page = get_partial_node(n);
1115 * Get a partial page, lock it and return it.
1117 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1120 int searchnode = (node == -1) ? numa_node_id() : node;
1122 page = get_partial_node(get_node(s, searchnode));
1123 if (page || (flags & __GFP_THISNODE))
1126 return get_any_partial(s, flags);
1130 * Move a page back to the lists.
1132 * Must be called with the slab lock held.
1134 * On exit the slab lock will have been dropped.
1136 static void putback_slab(struct kmem_cache *s, struct page *page)
1138 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1143 add_partial(n, page);
1144 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1149 if (n->nr_partial < MIN_PARTIAL) {
1151 * Adding an empty slab to the partial slabs in order
1152 * to avoid page allocator overhead. This slab needs
1153 * to come after the other slabs with objects in
1154 * order to fill them up. That way the size of the
1155 * partial list stays small. kmem_cache_shrink can
1156 * reclaim empty slabs from the partial list.
1158 add_partial_tail(n, page);
1162 discard_slab(s, page);
1168 * Remove the cpu slab
1170 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1172 s->cpu_slab[cpu] = NULL;
1173 ClearPageActive(page);
1175 putback_slab(s, page);
1178 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1181 deactivate_slab(s, page, cpu);
1186 * Called from IPI handler with interrupts disabled.
1188 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1190 struct page *page = s->cpu_slab[cpu];
1193 flush_slab(s, page, cpu);
1196 static void flush_cpu_slab(void *d)
1198 struct kmem_cache *s = d;
1199 int cpu = smp_processor_id();
1201 __flush_cpu_slab(s, cpu);
1204 static void flush_all(struct kmem_cache *s)
1207 on_each_cpu(flush_cpu_slab, s, 1, 1);
1209 unsigned long flags;
1211 local_irq_save(flags);
1213 local_irq_restore(flags);
1218 * slab_alloc is optimized to only modify two cachelines on the fast path
1219 * (aside from the stack):
1221 * 1. The page struct
1222 * 2. The first cacheline of the object to be allocated.
1224 * The only other cache lines that are read (apart from code) is the
1225 * per cpu array in the kmem_cache struct.
1227 * Fastpath is not possible if we need to get a new slab or have
1228 * debugging enabled (which means all slabs are marked with SlabDebug)
1230 static void *slab_alloc(struct kmem_cache *s,
1231 gfp_t gfpflags, int node, void *addr)
1235 unsigned long flags;
1238 local_irq_save(flags);
1239 cpu = smp_processor_id();
1240 page = s->cpu_slab[cpu];
1245 if (unlikely(node != -1 && page_to_nid(page) != node))
1248 object = page->freelist;
1249 if (unlikely(!object))
1251 if (unlikely(SlabDebug(page)))
1256 page->freelist = object[page->offset];
1258 local_irq_restore(flags);
1262 deactivate_slab(s, page, cpu);
1265 page = get_partial(s, gfpflags, node);
1268 s->cpu_slab[cpu] = page;
1269 SetPageActive(page);
1273 page = new_slab(s, gfpflags, node);
1275 cpu = smp_processor_id();
1276 if (s->cpu_slab[cpu]) {
1278 * Someone else populated the cpu_slab while we
1279 * enabled interrupts, or we have gotten scheduled
1280 * on another cpu. The page may not be on the
1281 * requested node even if __GFP_THISNODE was
1282 * specified. So we need to recheck.
1285 page_to_nid(s->cpu_slab[cpu]) == node) {
1287 * Current cpuslab is acceptable and we
1288 * want the current one since its cache hot
1290 discard_slab(s, page);
1291 page = s->cpu_slab[cpu];
1295 /* New slab does not fit our expectations */
1296 flush_slab(s, s->cpu_slab[cpu], cpu);
1301 local_irq_restore(flags);
1304 if (!alloc_object_checks(s, page, object))
1306 if (s->flags & SLAB_STORE_USER)
1307 set_track(s, object, TRACK_ALLOC, addr);
1308 trace(s, page, object, 1);
1309 init_object(s, object, 1);
1313 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1315 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1317 EXPORT_SYMBOL(kmem_cache_alloc);
1320 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1322 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1324 EXPORT_SYMBOL(kmem_cache_alloc_node);
1328 * The fastpath only writes the cacheline of the page struct and the first
1329 * cacheline of the object.
1331 * We read the cpu_slab cacheline to check if the slab is the per cpu
1332 * slab for this processor.
1334 static void slab_free(struct kmem_cache *s, struct page *page,
1335 void *x, void *addr)
1338 void **object = (void *)x;
1339 unsigned long flags;
1341 local_irq_save(flags);
1344 if (unlikely(SlabDebug(page)))
1347 prior = object[page->offset] = page->freelist;
1348 page->freelist = object;
1351 if (unlikely(PageActive(page)))
1353 * Cpu slabs are never on partial lists and are
1358 if (unlikely(!page->inuse))
1362 * Objects left in the slab. If it
1363 * was not on the partial list before
1366 if (unlikely(!prior))
1367 add_partial(get_node(s, page_to_nid(page)), page);
1371 local_irq_restore(flags);
1377 * Slab still on the partial list.
1379 remove_partial(s, page);
1382 discard_slab(s, page);
1383 local_irq_restore(flags);
1387 if (!free_object_checks(s, page, x))
1389 if (!PageActive(page) && !page->freelist)
1390 remove_full(s, page);
1391 if (s->flags & SLAB_STORE_USER)
1392 set_track(s, x, TRACK_FREE, addr);
1393 trace(s, page, object, 0);
1394 init_object(s, object, 0);
1398 void kmem_cache_free(struct kmem_cache *s, void *x)
1402 page = virt_to_head_page(x);
1404 slab_free(s, page, x, __builtin_return_address(0));
1406 EXPORT_SYMBOL(kmem_cache_free);
1408 /* Figure out on which slab object the object resides */
1409 static struct page *get_object_page(const void *x)
1411 struct page *page = virt_to_head_page(x);
1413 if (!PageSlab(page))
1420 * Object placement in a slab is made very easy because we always start at
1421 * offset 0. If we tune the size of the object to the alignment then we can
1422 * get the required alignment by putting one properly sized object after
1425 * Notice that the allocation order determines the sizes of the per cpu
1426 * caches. Each processor has always one slab available for allocations.
1427 * Increasing the allocation order reduces the number of times that slabs
1428 * must be moved on and off the partial lists and is therefore a factor in
1433 * Mininum / Maximum order of slab pages. This influences locking overhead
1434 * and slab fragmentation. A higher order reduces the number of partial slabs
1435 * and increases the number of allocations possible without having to
1436 * take the list_lock.
1438 static int slub_min_order;
1439 static int slub_max_order = DEFAULT_MAX_ORDER;
1440 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1443 * Merge control. If this is set then no merging of slab caches will occur.
1444 * (Could be removed. This was introduced to pacify the merge skeptics.)
1446 static int slub_nomerge;
1451 static int slub_debug;
1453 static char *slub_debug_slabs;
1456 * Calculate the order of allocation given an slab object size.
1458 * The order of allocation has significant impact on performance and other
1459 * system components. Generally order 0 allocations should be preferred since
1460 * order 0 does not cause fragmentation in the page allocator. Larger objects
1461 * be problematic to put into order 0 slabs because there may be too much
1462 * unused space left. We go to a higher order if more than 1/8th of the slab
1465 * In order to reach satisfactory performance we must ensure that a minimum
1466 * number of objects is in one slab. Otherwise we may generate too much
1467 * activity on the partial lists which requires taking the list_lock. This is
1468 * less a concern for large slabs though which are rarely used.
1470 * slub_max_order specifies the order where we begin to stop considering the
1471 * number of objects in a slab as critical. If we reach slub_max_order then
1472 * we try to keep the page order as low as possible. So we accept more waste
1473 * of space in favor of a small page order.
1475 * Higher order allocations also allow the placement of more objects in a
1476 * slab and thereby reduce object handling overhead. If the user has
1477 * requested a higher mininum order then we start with that one instead of
1478 * the smallest order which will fit the object.
1480 static int calculate_order(int size)
1485 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1486 order < MAX_ORDER; order++) {
1487 unsigned long slab_size = PAGE_SIZE << order;
1489 if (slub_max_order > order &&
1490 slab_size < slub_min_objects * size)
1493 if (slab_size < size)
1496 rem = slab_size % size;
1498 if (rem <= slab_size / 8)
1502 if (order >= MAX_ORDER)
1509 * Figure out what the alignment of the objects will be.
1511 static unsigned long calculate_alignment(unsigned long flags,
1512 unsigned long align, unsigned long size)
1515 * If the user wants hardware cache aligned objects then
1516 * follow that suggestion if the object is sufficiently
1519 * The hardware cache alignment cannot override the
1520 * specified alignment though. If that is greater
1523 if ((flags & SLAB_HWCACHE_ALIGN) &&
1524 size > cache_line_size() / 2)
1525 return max_t(unsigned long, align, cache_line_size());
1527 if (align < ARCH_SLAB_MINALIGN)
1528 return ARCH_SLAB_MINALIGN;
1530 return ALIGN(align, sizeof(void *));
1533 static void init_kmem_cache_node(struct kmem_cache_node *n)
1536 atomic_long_set(&n->nr_slabs, 0);
1537 spin_lock_init(&n->list_lock);
1538 INIT_LIST_HEAD(&n->partial);
1539 INIT_LIST_HEAD(&n->full);
1544 * No kmalloc_node yet so do it by hand. We know that this is the first
1545 * slab on the node for this slabcache. There are no concurrent accesses
1548 * Note that this function only works on the kmalloc_node_cache
1549 * when allocating for the kmalloc_node_cache.
1551 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1555 struct kmem_cache_node *n;
1557 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1559 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1560 /* new_slab() disables interupts */
1566 page->freelist = get_freepointer(kmalloc_caches, n);
1568 kmalloc_caches->node[node] = n;
1569 init_object(kmalloc_caches, n, 1);
1570 init_kmem_cache_node(n);
1571 atomic_long_inc(&n->nr_slabs);
1572 add_partial(n, page);
1576 static void free_kmem_cache_nodes(struct kmem_cache *s)
1580 for_each_online_node(node) {
1581 struct kmem_cache_node *n = s->node[node];
1582 if (n && n != &s->local_node)
1583 kmem_cache_free(kmalloc_caches, n);
1584 s->node[node] = NULL;
1588 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1593 if (slab_state >= UP)
1594 local_node = page_to_nid(virt_to_page(s));
1598 for_each_online_node(node) {
1599 struct kmem_cache_node *n;
1601 if (local_node == node)
1604 if (slab_state == DOWN) {
1605 n = early_kmem_cache_node_alloc(gfpflags,
1609 n = kmem_cache_alloc_node(kmalloc_caches,
1613 free_kmem_cache_nodes(s);
1619 init_kmem_cache_node(n);
1624 static void free_kmem_cache_nodes(struct kmem_cache *s)
1628 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1630 init_kmem_cache_node(&s->local_node);
1636 * calculate_sizes() determines the order and the distribution of data within
1639 static int calculate_sizes(struct kmem_cache *s)
1641 unsigned long flags = s->flags;
1642 unsigned long size = s->objsize;
1643 unsigned long align = s->align;
1646 * Determine if we can poison the object itself. If the user of
1647 * the slab may touch the object after free or before allocation
1648 * then we should never poison the object itself.
1650 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1651 !s->ctor && !s->dtor)
1652 s->flags |= __OBJECT_POISON;
1654 s->flags &= ~__OBJECT_POISON;
1657 * Round up object size to the next word boundary. We can only
1658 * place the free pointer at word boundaries and this determines
1659 * the possible location of the free pointer.
1661 size = ALIGN(size, sizeof(void *));
1664 * If we are Redzoning then check if there is some space between the
1665 * end of the object and the free pointer. If not then add an
1666 * additional word to have some bytes to store Redzone information.
1668 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1669 size += sizeof(void *);
1672 * With that we have determined the number of bytes in actual use
1673 * by the object. This is the potential offset to the free pointer.
1677 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1678 s->ctor || s->dtor)) {
1680 * Relocate free pointer after the object if it is not
1681 * permitted to overwrite the first word of the object on
1684 * This is the case if we do RCU, have a constructor or
1685 * destructor or are poisoning the objects.
1688 size += sizeof(void *);
1691 if (flags & SLAB_STORE_USER)
1693 * Need to store information about allocs and frees after
1696 size += 2 * sizeof(struct track);
1698 if (flags & SLAB_RED_ZONE)
1700 * Add some empty padding so that we can catch
1701 * overwrites from earlier objects rather than let
1702 * tracking information or the free pointer be
1703 * corrupted if an user writes before the start
1706 size += sizeof(void *);
1709 * Determine the alignment based on various parameters that the
1710 * user specified and the dynamic determination of cache line size
1713 align = calculate_alignment(flags, align, s->objsize);
1716 * SLUB stores one object immediately after another beginning from
1717 * offset 0. In order to align the objects we have to simply size
1718 * each object to conform to the alignment.
1720 size = ALIGN(size, align);
1723 s->order = calculate_order(size);
1728 * Determine the number of objects per slab
1730 s->objects = (PAGE_SIZE << s->order) / size;
1733 * Verify that the number of objects is within permitted limits.
1734 * The page->inuse field is only 16 bit wide! So we cannot have
1735 * more than 64k objects per slab.
1737 if (!s->objects || s->objects > 65535)
1743 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1744 const char *name, size_t size,
1745 size_t align, unsigned long flags,
1746 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1747 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1749 memset(s, 0, kmem_size);
1758 * The page->offset field is only 16 bit wide. This is an offset
1759 * in units of words from the beginning of an object. If the slab
1760 * size is bigger then we cannot move the free pointer behind the
1763 * On 32 bit platforms the limit is 256k. On 64bit platforms
1764 * the limit is 512k.
1766 * Debugging or ctor/dtors may create a need to move the free
1767 * pointer. Fail if this happens.
1769 if (s->size >= 65535 * sizeof(void *)) {
1770 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1771 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1772 BUG_ON(ctor || dtor);
1776 * Enable debugging if selected on the kernel commandline.
1778 if (slub_debug && (!slub_debug_slabs ||
1779 strncmp(slub_debug_slabs, name,
1780 strlen(slub_debug_slabs)) == 0))
1781 s->flags |= slub_debug;
1783 if (!calculate_sizes(s))
1788 s->defrag_ratio = 100;
1791 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1794 if (flags & SLAB_PANIC)
1795 panic("Cannot create slab %s size=%lu realsize=%u "
1796 "order=%u offset=%u flags=%lx\n",
1797 s->name, (unsigned long)size, s->size, s->order,
1801 EXPORT_SYMBOL(kmem_cache_open);
1804 * Check if a given pointer is valid
1806 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1810 page = get_object_page(object);
1812 if (!page || s != page->slab)
1813 /* No slab or wrong slab */
1816 if (!check_valid_pointer(s, page, object))
1820 * We could also check if the object is on the slabs freelist.
1821 * But this would be too expensive and it seems that the main
1822 * purpose of kmem_ptr_valid is to check if the object belongs
1823 * to a certain slab.
1827 EXPORT_SYMBOL(kmem_ptr_validate);
1830 * Determine the size of a slab object
1832 unsigned int kmem_cache_size(struct kmem_cache *s)
1836 EXPORT_SYMBOL(kmem_cache_size);
1838 const char *kmem_cache_name(struct kmem_cache *s)
1842 EXPORT_SYMBOL(kmem_cache_name);
1845 * Attempt to free all slabs on a node. Return the number of slabs we
1846 * were unable to free.
1848 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1849 struct list_head *list)
1851 int slabs_inuse = 0;
1852 unsigned long flags;
1853 struct page *page, *h;
1855 spin_lock_irqsave(&n->list_lock, flags);
1856 list_for_each_entry_safe(page, h, list, lru)
1858 list_del(&page->lru);
1859 discard_slab(s, page);
1862 spin_unlock_irqrestore(&n->list_lock, flags);
1867 * Release all resources used by a slab cache.
1869 static int kmem_cache_close(struct kmem_cache *s)
1875 /* Attempt to free all objects */
1876 for_each_online_node(node) {
1877 struct kmem_cache_node *n = get_node(s, node);
1879 n->nr_partial -= free_list(s, n, &n->partial);
1880 if (atomic_long_read(&n->nr_slabs))
1883 free_kmem_cache_nodes(s);
1888 * Close a cache and release the kmem_cache structure
1889 * (must be used for caches created using kmem_cache_create)
1891 void kmem_cache_destroy(struct kmem_cache *s)
1893 down_write(&slub_lock);
1897 if (kmem_cache_close(s))
1899 sysfs_slab_remove(s);
1902 up_write(&slub_lock);
1904 EXPORT_SYMBOL(kmem_cache_destroy);
1906 /********************************************************************
1908 *******************************************************************/
1910 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1911 EXPORT_SYMBOL(kmalloc_caches);
1913 #ifdef CONFIG_ZONE_DMA
1914 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1917 static int __init setup_slub_min_order(char *str)
1919 get_option (&str, &slub_min_order);
1924 __setup("slub_min_order=", setup_slub_min_order);
1926 static int __init setup_slub_max_order(char *str)
1928 get_option (&str, &slub_max_order);
1933 __setup("slub_max_order=", setup_slub_max_order);
1935 static int __init setup_slub_min_objects(char *str)
1937 get_option (&str, &slub_min_objects);
1942 __setup("slub_min_objects=", setup_slub_min_objects);
1944 static int __init setup_slub_nomerge(char *str)
1950 __setup("slub_nomerge", setup_slub_nomerge);
1952 static int __init setup_slub_debug(char *str)
1954 if (!str || *str != '=')
1955 slub_debug = DEBUG_DEFAULT_FLAGS;
1958 if (*str == 0 || *str == ',')
1959 slub_debug = DEBUG_DEFAULT_FLAGS;
1961 for( ;*str && *str != ','; str++)
1963 case 'f' : case 'F' :
1964 slub_debug |= SLAB_DEBUG_FREE;
1966 case 'z' : case 'Z' :
1967 slub_debug |= SLAB_RED_ZONE;
1969 case 'p' : case 'P' :
1970 slub_debug |= SLAB_POISON;
1972 case 'u' : case 'U' :
1973 slub_debug |= SLAB_STORE_USER;
1975 case 't' : case 'T' :
1976 slub_debug |= SLAB_TRACE;
1979 printk(KERN_ERR "slub_debug option '%c' "
1980 "unknown. skipped\n",*str);
1985 slub_debug_slabs = str + 1;
1989 __setup("slub_debug", setup_slub_debug);
1991 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1992 const char *name, int size, gfp_t gfp_flags)
1994 unsigned int flags = 0;
1996 if (gfp_flags & SLUB_DMA)
1997 flags = SLAB_CACHE_DMA;
1999 down_write(&slub_lock);
2000 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2004 list_add(&s->list, &slab_caches);
2005 up_write(&slub_lock);
2006 if (sysfs_slab_add(s))
2011 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2014 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2016 int index = kmalloc_index(size);
2021 /* Allocation too large? */
2024 #ifdef CONFIG_ZONE_DMA
2025 if ((flags & SLUB_DMA)) {
2026 struct kmem_cache *s;
2027 struct kmem_cache *x;
2031 s = kmalloc_caches_dma[index];
2035 /* Dynamically create dma cache */
2036 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2038 panic("Unable to allocate memory for dma cache\n");
2040 if (index <= KMALLOC_SHIFT_HIGH)
2041 realsize = 1 << index;
2049 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2050 (unsigned int)realsize);
2051 s = create_kmalloc_cache(x, text, realsize, flags);
2052 kmalloc_caches_dma[index] = s;
2056 return &kmalloc_caches[index];
2059 void *__kmalloc(size_t size, gfp_t flags)
2061 struct kmem_cache *s = get_slab(size, flags);
2064 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2067 EXPORT_SYMBOL(__kmalloc);
2070 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2072 struct kmem_cache *s = get_slab(size, flags);
2075 return slab_alloc(s, flags, node, __builtin_return_address(0));
2078 EXPORT_SYMBOL(__kmalloc_node);
2081 size_t ksize(const void *object)
2083 struct page *page = get_object_page(object);
2084 struct kmem_cache *s;
2091 * Debugging requires use of the padding between object
2092 * and whatever may come after it.
2094 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2098 * If we have the need to store the freelist pointer
2099 * back there or track user information then we can
2100 * only use the space before that information.
2102 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2106 * Else we can use all the padding etc for the allocation
2110 EXPORT_SYMBOL(ksize);
2112 void kfree(const void *x)
2114 struct kmem_cache *s;
2120 page = virt_to_head_page(x);
2123 slab_free(s, page, (void *)x, __builtin_return_address(0));
2125 EXPORT_SYMBOL(kfree);
2128 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2129 * the remaining slabs by the number of items in use. The slabs with the
2130 * most items in use come first. New allocations will then fill those up
2131 * and thus they can be removed from the partial lists.
2133 * The slabs with the least items are placed last. This results in them
2134 * being allocated from last increasing the chance that the last objects
2135 * are freed in them.
2137 int kmem_cache_shrink(struct kmem_cache *s)
2141 struct kmem_cache_node *n;
2144 struct list_head *slabs_by_inuse =
2145 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2146 unsigned long flags;
2148 if (!slabs_by_inuse)
2152 for_each_online_node(node) {
2153 n = get_node(s, node);
2158 for (i = 0; i < s->objects; i++)
2159 INIT_LIST_HEAD(slabs_by_inuse + i);
2161 spin_lock_irqsave(&n->list_lock, flags);
2164 * Build lists indexed by the items in use in each slab.
2166 * Note that concurrent frees may occur while we hold the
2167 * list_lock. page->inuse here is the upper limit.
2169 list_for_each_entry_safe(page, t, &n->partial, lru) {
2170 if (!page->inuse && slab_trylock(page)) {
2172 * Must hold slab lock here because slab_free
2173 * may have freed the last object and be
2174 * waiting to release the slab.
2176 list_del(&page->lru);
2179 discard_slab(s, page);
2181 if (n->nr_partial > MAX_PARTIAL)
2182 list_move(&page->lru,
2183 slabs_by_inuse + page->inuse);
2187 if (n->nr_partial <= MAX_PARTIAL)
2191 * Rebuild the partial list with the slabs filled up most
2192 * first and the least used slabs at the end.
2194 for (i = s->objects - 1; i >= 0; i--)
2195 list_splice(slabs_by_inuse + i, n->partial.prev);
2198 spin_unlock_irqrestore(&n->list_lock, flags);
2201 kfree(slabs_by_inuse);
2204 EXPORT_SYMBOL(kmem_cache_shrink);
2207 * krealloc - reallocate memory. The contents will remain unchanged.
2209 * @p: object to reallocate memory for.
2210 * @new_size: how many bytes of memory are required.
2211 * @flags: the type of memory to allocate.
2213 * The contents of the object pointed to are preserved up to the
2214 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2215 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2216 * %NULL pointer, the object pointed to is freed.
2218 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2224 return kmalloc(new_size, flags);
2226 if (unlikely(!new_size)) {
2235 ret = kmalloc(new_size, flags);
2237 memcpy(ret, p, min(new_size, ks));
2242 EXPORT_SYMBOL(krealloc);
2244 /********************************************************************
2245 * Basic setup of slabs
2246 *******************************************************************/
2248 void __init kmem_cache_init(void)
2254 * Must first have the slab cache available for the allocations of the
2255 * struct kmem_cache_node's. There is special bootstrap code in
2256 * kmem_cache_open for slab_state == DOWN.
2258 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2259 sizeof(struct kmem_cache_node), GFP_KERNEL);
2262 /* Able to allocate the per node structures */
2263 slab_state = PARTIAL;
2265 /* Caches that are not of the two-to-the-power-of size */
2266 create_kmalloc_cache(&kmalloc_caches[1],
2267 "kmalloc-96", 96, GFP_KERNEL);
2268 create_kmalloc_cache(&kmalloc_caches[2],
2269 "kmalloc-192", 192, GFP_KERNEL);
2271 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2272 create_kmalloc_cache(&kmalloc_caches[i],
2273 "kmalloc", 1 << i, GFP_KERNEL);
2277 /* Provide the correct kmalloc names now that the caches are up */
2278 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2279 kmalloc_caches[i]. name =
2280 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2283 register_cpu_notifier(&slab_notifier);
2286 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2287 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2288 + nr_cpu_ids * sizeof(struct page *);
2290 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2291 " Processors=%d, Nodes=%d\n",
2292 KMALLOC_SHIFT_HIGH, cache_line_size(),
2293 slub_min_order, slub_max_order, slub_min_objects,
2294 nr_cpu_ids, nr_node_ids);
2298 * Find a mergeable slab cache
2300 static int slab_unmergeable(struct kmem_cache *s)
2302 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2305 if (s->ctor || s->dtor)
2311 static struct kmem_cache *find_mergeable(size_t size,
2312 size_t align, unsigned long flags,
2313 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2314 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2316 struct list_head *h;
2318 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2324 size = ALIGN(size, sizeof(void *));
2325 align = calculate_alignment(flags, align, size);
2326 size = ALIGN(size, align);
2328 list_for_each(h, &slab_caches) {
2329 struct kmem_cache *s =
2330 container_of(h, struct kmem_cache, list);
2332 if (slab_unmergeable(s))
2338 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2339 (s->flags & SLUB_MERGE_SAME))
2342 * Check if alignment is compatible.
2343 * Courtesy of Adrian Drzewiecki
2345 if ((s->size & ~(align -1)) != s->size)
2348 if (s->size - size >= sizeof(void *))
2356 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2357 size_t align, unsigned long flags,
2358 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2359 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2361 struct kmem_cache *s;
2363 down_write(&slub_lock);
2364 s = find_mergeable(size, align, flags, dtor, ctor);
2368 * Adjust the object sizes so that we clear
2369 * the complete object on kzalloc.
2371 s->objsize = max(s->objsize, (int)size);
2372 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2373 if (sysfs_slab_alias(s, name))
2376 s = kmalloc(kmem_size, GFP_KERNEL);
2377 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2378 size, align, flags, ctor, dtor)) {
2379 if (sysfs_slab_add(s)) {
2383 list_add(&s->list, &slab_caches);
2387 up_write(&slub_lock);
2391 up_write(&slub_lock);
2392 if (flags & SLAB_PANIC)
2393 panic("Cannot create slabcache %s\n", name);
2398 EXPORT_SYMBOL(kmem_cache_create);
2400 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2404 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2406 memset(x, 0, s->objsize);
2409 EXPORT_SYMBOL(kmem_cache_zalloc);
2412 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2414 struct list_head *h;
2416 down_read(&slub_lock);
2417 list_for_each(h, &slab_caches) {
2418 struct kmem_cache *s =
2419 container_of(h, struct kmem_cache, list);
2423 up_read(&slub_lock);
2427 * Use the cpu notifier to insure that the cpu slabs are flushed when
2430 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2431 unsigned long action, void *hcpu)
2433 long cpu = (long)hcpu;
2436 case CPU_UP_CANCELED:
2438 for_all_slabs(__flush_cpu_slab, cpu);
2446 static struct notifier_block __cpuinitdata slab_notifier =
2447 { &slab_cpuup_callback, NULL, 0 };
2453 /*****************************************************************
2454 * Generic reaper used to support the page allocator
2455 * (the cpu slabs are reaped by a per slab workqueue).
2457 * Maybe move this to the page allocator?
2458 ****************************************************************/
2460 static DEFINE_PER_CPU(unsigned long, reap_node);
2462 static void init_reap_node(int cpu)
2466 node = next_node(cpu_to_node(cpu), node_online_map);
2467 if (node == MAX_NUMNODES)
2468 node = first_node(node_online_map);
2470 __get_cpu_var(reap_node) = node;
2473 static void next_reap_node(void)
2475 int node = __get_cpu_var(reap_node);
2478 * Also drain per cpu pages on remote zones
2480 if (node != numa_node_id())
2481 drain_node_pages(node);
2483 node = next_node(node, node_online_map);
2484 if (unlikely(node >= MAX_NUMNODES))
2485 node = first_node(node_online_map);
2486 __get_cpu_var(reap_node) = node;
2489 #define init_reap_node(cpu) do { } while (0)
2490 #define next_reap_node(void) do { } while (0)
2493 #define REAPTIMEOUT_CPUC (2*HZ)
2496 static DEFINE_PER_CPU(struct delayed_work, reap_work);
2498 static void cache_reap(struct work_struct *unused)
2501 refresh_cpu_vm_stats(smp_processor_id());
2502 schedule_delayed_work(&__get_cpu_var(reap_work),
2506 static void __devinit start_cpu_timer(int cpu)
2508 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2511 * When this gets called from do_initcalls via cpucache_init(),
2512 * init_workqueues() has already run, so keventd will be setup
2515 if (keventd_up() && reap_work->work.func == NULL) {
2516 init_reap_node(cpu);
2517 INIT_DELAYED_WORK(reap_work, cache_reap);
2518 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2522 static int __init cpucache_init(void)
2527 * Register the timers that drain pcp pages and update vm statistics
2529 for_each_online_cpu(cpu)
2530 start_cpu_timer(cpu);
2533 __initcall(cpucache_init);
2536 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2538 struct kmem_cache *s = get_slab(size, gfpflags);
2543 return slab_alloc(s, gfpflags, -1, caller);
2546 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2547 int node, void *caller)
2549 struct kmem_cache *s = get_slab(size, gfpflags);
2554 return slab_alloc(s, gfpflags, node, caller);
2559 static int validate_slab(struct kmem_cache *s, struct page *page)
2562 void *addr = page_address(page);
2563 DECLARE_BITMAP(map, s->objects);
2565 if (!check_slab(s, page) ||
2566 !on_freelist(s, page, NULL))
2569 /* Now we know that a valid freelist exists */
2570 bitmap_zero(map, s->objects);
2572 for_each_free_object(p, s, page->freelist) {
2573 set_bit(slab_index(p, s, addr), map);
2574 if (!check_object(s, page, p, 0))
2578 for_each_object(p, s, addr)
2579 if (!test_bit(slab_index(p, s, addr), map))
2580 if (!check_object(s, page, p, 1))
2585 static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2587 if (slab_trylock(page)) {
2588 validate_slab(s, page);
2591 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2594 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2595 if (!SlabDebug(page))
2596 printk(KERN_ERR "SLUB %s: SlabDebug not set "
2597 "on slab 0x%p\n", s->name, page);
2599 if (SlabDebug(page))
2600 printk(KERN_ERR "SLUB %s: SlabDebug set on "
2601 "slab 0x%p\n", s->name, page);
2605 static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2607 unsigned long count = 0;
2609 unsigned long flags;
2611 spin_lock_irqsave(&n->list_lock, flags);
2613 list_for_each_entry(page, &n->partial, lru) {
2614 validate_slab_slab(s, page);
2617 if (count != n->nr_partial)
2618 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2619 "counter=%ld\n", s->name, count, n->nr_partial);
2621 if (!(s->flags & SLAB_STORE_USER))
2624 list_for_each_entry(page, &n->full, lru) {
2625 validate_slab_slab(s, page);
2628 if (count != atomic_long_read(&n->nr_slabs))
2629 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2630 "counter=%ld\n", s->name, count,
2631 atomic_long_read(&n->nr_slabs));
2634 spin_unlock_irqrestore(&n->list_lock, flags);
2638 static unsigned long validate_slab_cache(struct kmem_cache *s)
2641 unsigned long count = 0;
2644 for_each_online_node(node) {
2645 struct kmem_cache_node *n = get_node(s, node);
2647 count += validate_slab_node(s, n);
2652 #ifdef SLUB_RESILIENCY_TEST
2653 static void resiliency_test(void)
2657 printk(KERN_ERR "SLUB resiliency testing\n");
2658 printk(KERN_ERR "-----------------------\n");
2659 printk(KERN_ERR "A. Corruption after allocation\n");
2661 p = kzalloc(16, GFP_KERNEL);
2663 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2664 " 0x12->0x%p\n\n", p + 16);
2666 validate_slab_cache(kmalloc_caches + 4);
2668 /* Hmmm... The next two are dangerous */
2669 p = kzalloc(32, GFP_KERNEL);
2670 p[32 + sizeof(void *)] = 0x34;
2671 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2672 " 0x34 -> -0x%p\n", p);
2673 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2675 validate_slab_cache(kmalloc_caches + 5);
2676 p = kzalloc(64, GFP_KERNEL);
2677 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2679 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2681 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2682 validate_slab_cache(kmalloc_caches + 6);
2684 printk(KERN_ERR "\nB. Corruption after free\n");
2685 p = kzalloc(128, GFP_KERNEL);
2688 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2689 validate_slab_cache(kmalloc_caches + 7);
2691 p = kzalloc(256, GFP_KERNEL);
2694 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2695 validate_slab_cache(kmalloc_caches + 8);
2697 p = kzalloc(512, GFP_KERNEL);
2700 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2701 validate_slab_cache(kmalloc_caches + 9);
2704 static void resiliency_test(void) {};
2708 * Generate lists of code addresses where slabcache objects are allocated
2713 unsigned long count;
2719 unsigned long count;
2720 struct location *loc;
2723 static void free_loc_track(struct loc_track *t)
2726 free_pages((unsigned long)t->loc,
2727 get_order(sizeof(struct location) * t->max));
2730 static int alloc_loc_track(struct loc_track *t, unsigned long max)
2736 max = PAGE_SIZE / sizeof(struct location);
2738 order = get_order(sizeof(struct location) * max);
2740 l = (void *)__get_free_pages(GFP_KERNEL, order);
2746 memcpy(l, t->loc, sizeof(struct location) * t->count);
2754 static int add_location(struct loc_track *t, struct kmem_cache *s,
2757 long start, end, pos;
2765 pos = start + (end - start + 1) / 2;
2768 * There is nothing at "end". If we end up there
2769 * we need to add something to before end.
2774 caddr = t->loc[pos].addr;
2775 if (addr == caddr) {
2776 t->loc[pos].count++;
2787 * Not found. Insert new tracking element.
2789 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2795 (t->count - pos) * sizeof(struct location));
2802 static void process_slab(struct loc_track *t, struct kmem_cache *s,
2803 struct page *page, enum track_item alloc)
2805 void *addr = page_address(page);
2806 DECLARE_BITMAP(map, s->objects);
2809 bitmap_zero(map, s->objects);
2810 for_each_free_object(p, s, page->freelist)
2811 set_bit(slab_index(p, s, addr), map);
2813 for_each_object(p, s, addr)
2814 if (!test_bit(slab_index(p, s, addr), map)) {
2815 void *addr = get_track(s, p, alloc)->addr;
2817 add_location(t, s, addr);
2821 static int list_locations(struct kmem_cache *s, char *buf,
2822 enum track_item alloc)
2832 /* Push back cpu slabs */
2835 for_each_online_node(node) {
2836 struct kmem_cache_node *n = get_node(s, node);
2837 unsigned long flags;
2840 if (!atomic_read(&n->nr_slabs))
2843 spin_lock_irqsave(&n->list_lock, flags);
2844 list_for_each_entry(page, &n->partial, lru)
2845 process_slab(&t, s, page, alloc);
2846 list_for_each_entry(page, &n->full, lru)
2847 process_slab(&t, s, page, alloc);
2848 spin_unlock_irqrestore(&n->list_lock, flags);
2851 for (i = 0; i < t.count; i++) {
2852 void *addr = t.loc[i].addr;
2854 if (n > PAGE_SIZE - 100)
2856 n += sprintf(buf + n, "%7ld ", t.loc[i].count);
2858 n += sprint_symbol(buf + n, (unsigned long)t.loc[i].addr);
2860 n += sprintf(buf + n, "<not-available>");
2861 n += sprintf(buf + n, "\n");
2866 n += sprintf(buf, "No data\n");
2870 static unsigned long count_partial(struct kmem_cache_node *n)
2872 unsigned long flags;
2873 unsigned long x = 0;
2876 spin_lock_irqsave(&n->list_lock, flags);
2877 list_for_each_entry(page, &n->partial, lru)
2879 spin_unlock_irqrestore(&n->list_lock, flags);
2883 enum slab_stat_type {
2890 #define SO_FULL (1 << SL_FULL)
2891 #define SO_PARTIAL (1 << SL_PARTIAL)
2892 #define SO_CPU (1 << SL_CPU)
2893 #define SO_OBJECTS (1 << SL_OBJECTS)
2895 static unsigned long slab_objects(struct kmem_cache *s,
2896 char *buf, unsigned long flags)
2898 unsigned long total = 0;
2902 unsigned long *nodes;
2903 unsigned long *per_cpu;
2905 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2906 per_cpu = nodes + nr_node_ids;
2908 for_each_possible_cpu(cpu) {
2909 struct page *page = s->cpu_slab[cpu];
2913 node = page_to_nid(page);
2914 if (flags & SO_CPU) {
2917 if (flags & SO_OBJECTS)
2928 for_each_online_node(node) {
2929 struct kmem_cache_node *n = get_node(s, node);
2931 if (flags & SO_PARTIAL) {
2932 if (flags & SO_OBJECTS)
2933 x = count_partial(n);
2940 if (flags & SO_FULL) {
2941 int full_slabs = atomic_read(&n->nr_slabs)
2945 if (flags & SO_OBJECTS)
2946 x = full_slabs * s->objects;
2954 x = sprintf(buf, "%lu", total);
2956 for_each_online_node(node)
2958 x += sprintf(buf + x, " N%d=%lu",
2962 return x + sprintf(buf + x, "\n");
2965 static int any_slab_objects(struct kmem_cache *s)
2970 for_each_possible_cpu(cpu)
2971 if (s->cpu_slab[cpu])
2974 for_each_node(node) {
2975 struct kmem_cache_node *n = get_node(s, node);
2977 if (n->nr_partial || atomic_read(&n->nr_slabs))
2983 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2984 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2986 struct slab_attribute {
2987 struct attribute attr;
2988 ssize_t (*show)(struct kmem_cache *s, char *buf);
2989 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2992 #define SLAB_ATTR_RO(_name) \
2993 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2995 #define SLAB_ATTR(_name) \
2996 static struct slab_attribute _name##_attr = \
2997 __ATTR(_name, 0644, _name##_show, _name##_store)
2999 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3001 return sprintf(buf, "%d\n", s->size);
3003 SLAB_ATTR_RO(slab_size);
3005 static ssize_t align_show(struct kmem_cache *s, char *buf)
3007 return sprintf(buf, "%d\n", s->align);
3009 SLAB_ATTR_RO(align);
3011 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3013 return sprintf(buf, "%d\n", s->objsize);
3015 SLAB_ATTR_RO(object_size);
3017 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3019 return sprintf(buf, "%d\n", s->objects);
3021 SLAB_ATTR_RO(objs_per_slab);
3023 static ssize_t order_show(struct kmem_cache *s, char *buf)
3025 return sprintf(buf, "%d\n", s->order);
3027 SLAB_ATTR_RO(order);
3029 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3032 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3034 return n + sprintf(buf + n, "\n");
3040 static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3043 int n = sprint_symbol(buf, (unsigned long)s->dtor);
3045 return n + sprintf(buf + n, "\n");
3051 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3053 return sprintf(buf, "%d\n", s->refcount - 1);
3055 SLAB_ATTR_RO(aliases);
3057 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3059 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3061 SLAB_ATTR_RO(slabs);
3063 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3065 return slab_objects(s, buf, SO_PARTIAL);
3067 SLAB_ATTR_RO(partial);
3069 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3071 return slab_objects(s, buf, SO_CPU);
3073 SLAB_ATTR_RO(cpu_slabs);
3075 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3077 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3079 SLAB_ATTR_RO(objects);
3081 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3083 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3086 static ssize_t sanity_checks_store(struct kmem_cache *s,
3087 const char *buf, size_t length)
3089 s->flags &= ~SLAB_DEBUG_FREE;
3091 s->flags |= SLAB_DEBUG_FREE;
3094 SLAB_ATTR(sanity_checks);
3096 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3098 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3101 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3104 s->flags &= ~SLAB_TRACE;
3106 s->flags |= SLAB_TRACE;
3111 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3113 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3116 static ssize_t reclaim_account_store(struct kmem_cache *s,
3117 const char *buf, size_t length)
3119 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3121 s->flags |= SLAB_RECLAIM_ACCOUNT;
3124 SLAB_ATTR(reclaim_account);
3126 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3128 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3130 SLAB_ATTR_RO(hwcache_align);
3132 #ifdef CONFIG_ZONE_DMA
3133 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3135 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3137 SLAB_ATTR_RO(cache_dma);
3140 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3142 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3144 SLAB_ATTR_RO(destroy_by_rcu);
3146 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3148 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3151 static ssize_t red_zone_store(struct kmem_cache *s,
3152 const char *buf, size_t length)
3154 if (any_slab_objects(s))
3157 s->flags &= ~SLAB_RED_ZONE;
3159 s->flags |= SLAB_RED_ZONE;
3163 SLAB_ATTR(red_zone);
3165 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3167 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3170 static ssize_t poison_store(struct kmem_cache *s,
3171 const char *buf, size_t length)
3173 if (any_slab_objects(s))
3176 s->flags &= ~SLAB_POISON;
3178 s->flags |= SLAB_POISON;
3184 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3186 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3189 static ssize_t store_user_store(struct kmem_cache *s,
3190 const char *buf, size_t length)
3192 if (any_slab_objects(s))
3195 s->flags &= ~SLAB_STORE_USER;
3197 s->flags |= SLAB_STORE_USER;
3201 SLAB_ATTR(store_user);
3203 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3208 static ssize_t validate_store(struct kmem_cache *s,
3209 const char *buf, size_t length)
3212 validate_slab_cache(s);
3217 SLAB_ATTR(validate);
3219 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3224 static ssize_t shrink_store(struct kmem_cache *s,
3225 const char *buf, size_t length)
3227 if (buf[0] == '1') {
3228 int rc = kmem_cache_shrink(s);
3238 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3240 if (!(s->flags & SLAB_STORE_USER))
3242 return list_locations(s, buf, TRACK_ALLOC);
3244 SLAB_ATTR_RO(alloc_calls);
3246 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3248 if (!(s->flags & SLAB_STORE_USER))
3250 return list_locations(s, buf, TRACK_FREE);
3252 SLAB_ATTR_RO(free_calls);
3255 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3257 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3260 static ssize_t defrag_ratio_store(struct kmem_cache *s,
3261 const char *buf, size_t length)
3263 int n = simple_strtoul(buf, NULL, 10);
3266 s->defrag_ratio = n * 10;
3269 SLAB_ATTR(defrag_ratio);
3272 static struct attribute * slab_attrs[] = {
3273 &slab_size_attr.attr,
3274 &object_size_attr.attr,
3275 &objs_per_slab_attr.attr,
3280 &cpu_slabs_attr.attr,
3285 &sanity_checks_attr.attr,
3287 &hwcache_align_attr.attr,
3288 &reclaim_account_attr.attr,
3289 &destroy_by_rcu_attr.attr,
3290 &red_zone_attr.attr,
3292 &store_user_attr.attr,
3293 &validate_attr.attr,
3295 &alloc_calls_attr.attr,
3296 &free_calls_attr.attr,
3297 #ifdef CONFIG_ZONE_DMA
3298 &cache_dma_attr.attr,
3301 &defrag_ratio_attr.attr,
3306 static struct attribute_group slab_attr_group = {
3307 .attrs = slab_attrs,
3310 static ssize_t slab_attr_show(struct kobject *kobj,
3311 struct attribute *attr,
3314 struct slab_attribute *attribute;
3315 struct kmem_cache *s;
3318 attribute = to_slab_attr(attr);
3321 if (!attribute->show)
3324 err = attribute->show(s, buf);
3329 static ssize_t slab_attr_store(struct kobject *kobj,
3330 struct attribute *attr,
3331 const char *buf, size_t len)
3333 struct slab_attribute *attribute;
3334 struct kmem_cache *s;
3337 attribute = to_slab_attr(attr);
3340 if (!attribute->store)
3343 err = attribute->store(s, buf, len);
3348 static struct sysfs_ops slab_sysfs_ops = {
3349 .show = slab_attr_show,
3350 .store = slab_attr_store,
3353 static struct kobj_type slab_ktype = {
3354 .sysfs_ops = &slab_sysfs_ops,
3357 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3359 struct kobj_type *ktype = get_ktype(kobj);
3361 if (ktype == &slab_ktype)
3366 static struct kset_uevent_ops slab_uevent_ops = {
3367 .filter = uevent_filter,
3370 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3372 #define ID_STR_LENGTH 64
3374 /* Create a unique string id for a slab cache:
3376 * :[flags-]size:[memory address of kmemcache]
3378 static char *create_unique_id(struct kmem_cache *s)
3380 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3387 * First flags affecting slabcache operations. We will only
3388 * get here for aliasable slabs so we do not need to support
3389 * too many flags. The flags here must cover all flags that
3390 * are matched during merging to guarantee that the id is
3393 if (s->flags & SLAB_CACHE_DMA)
3395 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3397 if (s->flags & SLAB_DEBUG_FREE)
3401 p += sprintf(p, "%07d", s->size);
3402 BUG_ON(p > name + ID_STR_LENGTH - 1);
3406 static int sysfs_slab_add(struct kmem_cache *s)
3412 if (slab_state < SYSFS)
3413 /* Defer until later */
3416 unmergeable = slab_unmergeable(s);
3419 * Slabcache can never be merged so we can use the name proper.
3420 * This is typically the case for debug situations. In that
3421 * case we can catch duplicate names easily.
3423 sysfs_remove_link(&slab_subsys.kobj, s->name);
3427 * Create a unique name for the slab as a target
3430 name = create_unique_id(s);
3433 kobj_set_kset_s(s, slab_subsys);
3434 kobject_set_name(&s->kobj, name);
3435 kobject_init(&s->kobj);
3436 err = kobject_add(&s->kobj);
3440 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3443 kobject_uevent(&s->kobj, KOBJ_ADD);
3445 /* Setup first alias */
3446 sysfs_slab_alias(s, s->name);
3452 static void sysfs_slab_remove(struct kmem_cache *s)
3454 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3455 kobject_del(&s->kobj);
3459 * Need to buffer aliases during bootup until sysfs becomes
3460 * available lest we loose that information.
3462 struct saved_alias {
3463 struct kmem_cache *s;
3465 struct saved_alias *next;
3468 struct saved_alias *alias_list;
3470 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3472 struct saved_alias *al;
3474 if (slab_state == SYSFS) {
3476 * If we have a leftover link then remove it.
3478 sysfs_remove_link(&slab_subsys.kobj, name);
3479 return sysfs_create_link(&slab_subsys.kobj,
3483 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3489 al->next = alias_list;
3494 static int __init slab_sysfs_init(void)
3496 struct list_head *h;
3499 err = subsystem_register(&slab_subsys);
3501 printk(KERN_ERR "Cannot register slab subsystem.\n");
3507 list_for_each(h, &slab_caches) {
3508 struct kmem_cache *s =
3509 container_of(h, struct kmem_cache, list);
3511 err = sysfs_slab_add(s);
3515 while (alias_list) {
3516 struct saved_alias *al = alias_list;
3518 alias_list = alias_list->next;
3519 err = sysfs_slab_alias(al->s, al->name);
3528 __initcall(slab_sysfs_init);