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 * - Variable sizing of the per node arrays
103 /* Enable to test recovery from slab corruption on boot */
104 #undef SLUB_RESILIENCY_TEST
109 * Small page size. Make sure that we do not fragment memory
111 #define DEFAULT_MAX_ORDER 1
112 #define DEFAULT_MIN_OBJECTS 4
117 * Large page machines are customarily able to handle larger
120 #define DEFAULT_MAX_ORDER 2
121 #define DEFAULT_MIN_OBJECTS 8
126 * Mininum number of partial slabs. These will be left on the partial
127 * lists even if they are empty. kmem_cache_shrink may reclaim them.
129 #define MIN_PARTIAL 2
132 * Maximum number of desirable partial slabs.
133 * The existence of more partial slabs makes kmem_cache_shrink
134 * sort the partial list by the number of objects in the.
136 #define MAX_PARTIAL 10
138 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
139 SLAB_POISON | SLAB_STORE_USER)
141 * Set of flags that will prevent slab merging
143 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
144 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
146 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
149 #ifndef ARCH_KMALLOC_MINALIGN
150 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
153 #ifndef ARCH_SLAB_MINALIGN
154 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
157 /* Internal SLUB flags */
158 #define __OBJECT_POISON 0x80000000 /* Poison object */
160 /* Not all arches define cache_line_size */
161 #ifndef cache_line_size
162 #define cache_line_size() L1_CACHE_BYTES
165 static int kmem_size = sizeof(struct kmem_cache);
168 static struct notifier_block slab_notifier;
172 DOWN, /* No slab functionality available */
173 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
174 UP, /* Everything works */
178 /* A list of all slab caches on the system */
179 static DECLARE_RWSEM(slub_lock);
180 LIST_HEAD(slab_caches);
183 static int sysfs_slab_add(struct kmem_cache *);
184 static int sysfs_slab_alias(struct kmem_cache *, const char *);
185 static void sysfs_slab_remove(struct kmem_cache *);
187 static int sysfs_slab_add(struct kmem_cache *s) { return 0; }
188 static int sysfs_slab_alias(struct kmem_cache *s, const char *p) { return 0; }
189 static void sysfs_slab_remove(struct kmem_cache *s) {}
192 /********************************************************************
193 * Core slab cache functions
194 *******************************************************************/
196 int slab_is_available(void)
198 return slab_state >= UP;
201 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
204 return s->node[node];
206 return &s->local_node;
213 static void print_section(char *text, u8 *addr, unsigned int length)
221 for (i = 0; i < length; i++) {
223 printk(KERN_ERR "%10s 0x%p: ", text, addr + i);
226 printk(" %02x", addr[i]);
228 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
230 printk(" %s\n",ascii);
241 printk(" %s\n", ascii);
246 * Slow version of get and set free pointer.
248 * This requires touching the cache lines of kmem_cache.
249 * The offset can also be obtained from the page. In that
250 * case it is in the cacheline that we already need to touch.
252 static void *get_freepointer(struct kmem_cache *s, void *object)
254 return *(void **)(object + s->offset);
257 static void set_freepointer(struct kmem_cache *s, void *object, void *fp)
259 *(void **)(object + s->offset) = fp;
263 * Tracking user of a slab.
266 void *addr; /* Called from address */
267 int cpu; /* Was running on cpu */
268 int pid; /* Pid context */
269 unsigned long when; /* When did the operation occur */
272 enum track_item { TRACK_ALLOC, TRACK_FREE };
274 static struct track *get_track(struct kmem_cache *s, void *object,
275 enum track_item alloc)
280 p = object + s->offset + sizeof(void *);
282 p = object + s->inuse;
287 static void set_track(struct kmem_cache *s, void *object,
288 enum track_item alloc, void *addr)
293 p = object + s->offset + sizeof(void *);
295 p = object + s->inuse;
300 p->cpu = smp_processor_id();
301 p->pid = current ? current->pid : -1;
304 memset(p, 0, sizeof(struct track));
307 static void init_tracking(struct kmem_cache *s, void *object)
309 if (s->flags & SLAB_STORE_USER) {
310 set_track(s, object, TRACK_FREE, NULL);
311 set_track(s, object, TRACK_ALLOC, NULL);
315 static void print_track(const char *s, struct track *t)
320 printk(KERN_ERR "%s: ", s);
321 __print_symbol("%s", (unsigned long)t->addr);
322 printk(" jiffies_ago=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
325 static void print_trailer(struct kmem_cache *s, u8 *p)
327 unsigned int off; /* Offset of last byte */
329 if (s->flags & SLAB_RED_ZONE)
330 print_section("Redzone", p + s->objsize,
331 s->inuse - s->objsize);
333 printk(KERN_ERR "FreePointer 0x%p -> 0x%p\n",
335 get_freepointer(s, p));
338 off = s->offset + sizeof(void *);
342 if (s->flags & SLAB_STORE_USER) {
343 print_track("Last alloc", get_track(s, p, TRACK_ALLOC));
344 print_track("Last free ", get_track(s, p, TRACK_FREE));
345 off += 2 * sizeof(struct track);
349 /* Beginning of the filler is the free pointer */
350 print_section("Filler", p + off, s->size - off);
353 static void object_err(struct kmem_cache *s, struct page *page,
354 u8 *object, char *reason)
356 u8 *addr = page_address(page);
358 printk(KERN_ERR "*** SLUB %s: %s@0x%p slab 0x%p\n",
359 s->name, reason, object, page);
360 printk(KERN_ERR " offset=%tu flags=0x%04lx inuse=%u freelist=0x%p\n",
361 object - addr, page->flags, page->inuse, page->freelist);
362 if (object > addr + 16)
363 print_section("Bytes b4", object - 16, 16);
364 print_section("Object", object, min(s->objsize, 128));
365 print_trailer(s, object);
369 static void slab_err(struct kmem_cache *s, struct page *page, char *reason, ...)
374 va_start(args, reason);
375 vsnprintf(buf, sizeof(buf), reason, args);
377 printk(KERN_ERR "*** SLUB %s: %s in slab @0x%p\n", s->name, buf,
382 static void init_object(struct kmem_cache *s, void *object, int active)
386 if (s->flags & __OBJECT_POISON) {
387 memset(p, POISON_FREE, s->objsize - 1);
388 p[s->objsize -1] = POISON_END;
391 if (s->flags & SLAB_RED_ZONE)
392 memset(p + s->objsize,
393 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
394 s->inuse - s->objsize);
397 static int check_bytes(u8 *start, unsigned int value, unsigned int bytes)
400 if (*start != (u8)value)
408 static inline int check_valid_pointer(struct kmem_cache *s,
409 struct page *page, const void *object)
416 base = page_address(page);
417 if (object < base || object >= base + s->objects * s->size ||
418 (object - base) % s->size) {
429 * Bytes of the object to be managed.
430 * If the freepointer may overlay the object then the free
431 * pointer is the first word of the object.
432 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
435 * object + s->objsize
436 * Padding to reach word boundary. This is also used for Redzoning.
437 * Padding is extended to word size if Redzoning is enabled
438 * and objsize == inuse.
439 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
440 * 0xcc (RED_ACTIVE) for objects in use.
443 * A. Free pointer (if we cannot overwrite object on free)
444 * B. Tracking data for SLAB_STORE_USER
445 * C. Padding to reach required alignment boundary
446 * Padding is done using 0x5a (POISON_INUSE)
450 * If slabcaches are merged then the objsize and inuse boundaries are to
451 * be ignored. And therefore no slab options that rely on these boundaries
452 * may be used with merged slabcaches.
455 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
456 void *from, void *to)
458 printk(KERN_ERR "@@@ SLUB %s: Restoring %s (0x%x) from 0x%p-0x%p\n",
459 s->name, message, data, from, to - 1);
460 memset(from, data, to - from);
463 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
465 unsigned long off = s->inuse; /* The end of info */
468 /* Freepointer is placed after the object. */
469 off += sizeof(void *);
471 if (s->flags & SLAB_STORE_USER)
472 /* We also have user information there */
473 off += 2 * sizeof(struct track);
478 if (check_bytes(p + off, POISON_INUSE, s->size - off))
481 object_err(s, page, p, "Object padding check fails");
486 restore_bytes(s, "object padding", POISON_INUSE, p + off, p + s->size);
490 static int slab_pad_check(struct kmem_cache *s, struct page *page)
493 int length, remainder;
495 if (!(s->flags & SLAB_POISON))
498 p = page_address(page);
499 length = s->objects * s->size;
500 remainder = (PAGE_SIZE << s->order) - length;
504 if (!check_bytes(p + length, POISON_INUSE, remainder)) {
505 slab_err(s, page, "Padding check failed");
506 restore_bytes(s, "slab padding", POISON_INUSE, p + length,
507 p + length + remainder);
513 static int check_object(struct kmem_cache *s, struct page *page,
514 void *object, int active)
517 u8 *endobject = object + s->objsize;
519 if (s->flags & SLAB_RED_ZONE) {
521 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
523 if (!check_bytes(endobject, red, s->inuse - s->objsize)) {
524 object_err(s, page, object,
525 active ? "Redzone Active" : "Redzone Inactive");
526 restore_bytes(s, "redzone", red,
527 endobject, object + s->inuse);
531 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse &&
532 !check_bytes(endobject, POISON_INUSE,
533 s->inuse - s->objsize)) {
534 object_err(s, page, p, "Alignment padding check fails");
536 * Fix it so that there will not be another report.
538 * Hmmm... We may be corrupting an object that now expects
539 * to be longer than allowed.
541 restore_bytes(s, "alignment padding", POISON_INUSE,
542 endobject, object + s->inuse);
546 if (s->flags & SLAB_POISON) {
547 if (!active && (s->flags & __OBJECT_POISON) &&
548 (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
549 p[s->objsize - 1] != POISON_END)) {
551 object_err(s, page, p, "Poison check failed");
552 restore_bytes(s, "Poison", POISON_FREE,
553 p, p + s->objsize -1);
554 restore_bytes(s, "Poison", POISON_END,
555 p + s->objsize - 1, p + s->objsize);
559 * check_pad_bytes cleans up on its own.
561 check_pad_bytes(s, page, p);
564 if (!s->offset && active)
566 * Object and freepointer overlap. Cannot check
567 * freepointer while object is allocated.
571 /* Check free pointer validity */
572 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
573 object_err(s, page, p, "Freepointer corrupt");
575 * No choice but to zap it and thus loose the remainder
576 * of the free objects in this slab. May cause
577 * another error because the object count maybe
580 set_freepointer(s, p, NULL);
586 static int check_slab(struct kmem_cache *s, struct page *page)
588 VM_BUG_ON(!irqs_disabled());
590 if (!PageSlab(page)) {
591 slab_err(s, page, "Not a valid slab page flags=%lx "
592 "mapping=0x%p count=%d", page->flags, page->mapping,
596 if (page->offset * sizeof(void *) != s->offset) {
597 slab_err(s, page, "Corrupted offset %lu flags=0x%lx "
598 "mapping=0x%p count=%d",
599 (unsigned long)(page->offset * sizeof(void *)),
605 if (page->inuse > s->objects) {
606 slab_err(s, page, "inuse %u > max %u @0x%p flags=%lx "
607 "mapping=0x%p count=%d",
608 s->name, page->inuse, s->objects, page->flags,
609 page->mapping, page_count(page));
612 /* Slab_pad_check fixes things up after itself */
613 slab_pad_check(s, page);
618 * Determine if a certain object on a page is on the freelist and
619 * therefore free. Must hold the slab lock for cpu slabs to
620 * guarantee that the chains are consistent.
622 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
625 void *fp = page->freelist;
628 while (fp && nr <= s->objects) {
631 if (!check_valid_pointer(s, page, fp)) {
633 object_err(s, page, object,
634 "Freechain corrupt");
635 set_freepointer(s, object, NULL);
638 slab_err(s, page, "Freepointer 0x%p corrupt",
640 page->freelist = NULL;
641 page->inuse = s->objects;
642 printk(KERN_ERR "@@@ SLUB %s: Freelist "
643 "cleared. Slab 0x%p\n",
650 fp = get_freepointer(s, object);
654 if (page->inuse != s->objects - nr) {
655 slab_err(s, page, "Wrong object count. Counter is %d but "
656 "counted were %d", s, page, page->inuse,
658 page->inuse = s->objects - nr;
659 printk(KERN_ERR "@@@ SLUB %s: Object count adjusted. "
660 "Slab @0x%p\n", s->name, page);
662 return search == NULL;
666 * Tracking of fully allocated slabs for debugging
668 static void add_full(struct kmem_cache_node *n, struct page *page)
670 spin_lock(&n->list_lock);
671 list_add(&page->lru, &n->full);
672 spin_unlock(&n->list_lock);
675 static void remove_full(struct kmem_cache *s, struct page *page)
677 struct kmem_cache_node *n;
679 if (!(s->flags & SLAB_STORE_USER))
682 n = get_node(s, page_to_nid(page));
684 spin_lock(&n->list_lock);
685 list_del(&page->lru);
686 spin_unlock(&n->list_lock);
689 static int alloc_object_checks(struct kmem_cache *s, struct page *page,
692 if (!check_slab(s, page))
695 if (object && !on_freelist(s, page, object)) {
696 slab_err(s, page, "Object 0x%p already allocated", object);
700 if (!check_valid_pointer(s, page, object)) {
701 object_err(s, page, object, "Freelist Pointer check fails");
708 if (!check_object(s, page, object, 0))
713 if (PageSlab(page)) {
715 * If this is a slab page then lets do the best we can
716 * to avoid issues in the future. Marking all objects
717 * as used avoids touching the remainder.
719 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p. Marking all objects used.\n",
721 page->inuse = s->objects;
722 page->freelist = NULL;
723 /* Fix up fields that may be corrupted */
724 page->offset = s->offset / sizeof(void *);
729 static int free_object_checks(struct kmem_cache *s, struct page *page,
732 if (!check_slab(s, page))
735 if (!check_valid_pointer(s, page, object)) {
736 slab_err(s, page, "Invalid object pointer 0x%p", object);
740 if (on_freelist(s, page, object)) {
741 slab_err(s, page, "Object 0x%p already free", object);
745 if (!check_object(s, page, object, 1))
748 if (unlikely(s != page->slab)) {
750 slab_err(s, page, "Attempt to free object(0x%p) "
751 "outside of slab", object);
755 "SLUB <none>: no slab for object 0x%p.\n",
760 slab_err(s, page, "object at 0x%p belongs "
761 "to slab %s", object, page->slab->name);
766 printk(KERN_ERR "@@@ SLUB: %s slab 0x%p object at 0x%p not freed.\n",
767 s->name, page, object);
772 * Slab allocation and freeing
774 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
777 int pages = 1 << s->order;
782 if (s->flags & SLAB_CACHE_DMA)
786 page = alloc_pages(flags, s->order);
788 page = alloc_pages_node(node, flags, s->order);
793 mod_zone_page_state(page_zone(page),
794 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
795 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
801 static void setup_object(struct kmem_cache *s, struct page *page,
804 if (PageError(page)) {
805 init_object(s, object, 0);
806 init_tracking(s, object);
809 if (unlikely(s->ctor))
810 s->ctor(object, s, SLAB_CTOR_CONSTRUCTOR);
813 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
816 struct kmem_cache_node *n;
822 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
824 if (flags & __GFP_WAIT)
827 page = allocate_slab(s, flags & GFP_LEVEL_MASK, node);
831 n = get_node(s, page_to_nid(page));
833 atomic_long_inc(&n->nr_slabs);
834 page->offset = s->offset / sizeof(void *);
836 page->flags |= 1 << PG_slab;
837 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
838 SLAB_STORE_USER | SLAB_TRACE))
839 page->flags |= 1 << PG_error;
841 start = page_address(page);
842 end = start + s->objects * s->size;
844 if (unlikely(s->flags & SLAB_POISON))
845 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
848 for (p = start + s->size; p < end; p += s->size) {
849 setup_object(s, page, last);
850 set_freepointer(s, last, p);
853 setup_object(s, page, last);
854 set_freepointer(s, last, NULL);
856 page->freelist = start;
859 if (flags & __GFP_WAIT)
864 static void __free_slab(struct kmem_cache *s, struct page *page)
866 int pages = 1 << s->order;
868 if (unlikely(PageError(page) || s->dtor)) {
869 void *start = page_address(page);
870 void *end = start + (pages << PAGE_SHIFT);
873 slab_pad_check(s, page);
874 for (p = start; p <= end - s->size; p += s->size) {
877 check_object(s, page, p, 0);
881 mod_zone_page_state(page_zone(page),
882 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
883 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
886 page->mapping = NULL;
887 __free_pages(page, s->order);
890 static void rcu_free_slab(struct rcu_head *h)
894 page = container_of((struct list_head *)h, struct page, lru);
895 __free_slab(page->slab, page);
898 static void free_slab(struct kmem_cache *s, struct page *page)
900 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
902 * RCU free overloads the RCU head over the LRU
904 struct rcu_head *head = (void *)&page->lru;
906 call_rcu(head, rcu_free_slab);
908 __free_slab(s, page);
911 static void discard_slab(struct kmem_cache *s, struct page *page)
913 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
915 atomic_long_dec(&n->nr_slabs);
916 reset_page_mapcount(page);
917 page->flags &= ~(1 << PG_slab | 1 << PG_error);
922 * Per slab locking using the pagelock
924 static __always_inline void slab_lock(struct page *page)
926 bit_spin_lock(PG_locked, &page->flags);
929 static __always_inline void slab_unlock(struct page *page)
931 bit_spin_unlock(PG_locked, &page->flags);
934 static __always_inline int slab_trylock(struct page *page)
938 rc = bit_spin_trylock(PG_locked, &page->flags);
943 * Management of partially allocated slabs
945 static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
947 spin_lock(&n->list_lock);
949 list_add_tail(&page->lru, &n->partial);
950 spin_unlock(&n->list_lock);
953 static void add_partial(struct kmem_cache_node *n, struct page *page)
955 spin_lock(&n->list_lock);
957 list_add(&page->lru, &n->partial);
958 spin_unlock(&n->list_lock);
961 static void remove_partial(struct kmem_cache *s,
964 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
966 spin_lock(&n->list_lock);
967 list_del(&page->lru);
969 spin_unlock(&n->list_lock);
973 * Lock page and remove it from the partial list
975 * Must hold list_lock
977 static int lock_and_del_slab(struct kmem_cache_node *n, struct page *page)
979 if (slab_trylock(page)) {
980 list_del(&page->lru);
988 * Try to get a partial slab from a specific node
990 static struct page *get_partial_node(struct kmem_cache_node *n)
995 * Racy check. If we mistakenly see no partial slabs then we
996 * just allocate an empty slab. If we mistakenly try to get a
997 * partial slab then get_partials() will return NULL.
999 if (!n || !n->nr_partial)
1002 spin_lock(&n->list_lock);
1003 list_for_each_entry(page, &n->partial, lru)
1004 if (lock_and_del_slab(n, page))
1008 spin_unlock(&n->list_lock);
1013 * Get a page from somewhere. Search in increasing NUMA
1016 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1019 struct zonelist *zonelist;
1024 * The defrag ratio allows to configure the tradeoffs between
1025 * inter node defragmentation and node local allocations.
1026 * A lower defrag_ratio increases the tendency to do local
1027 * allocations instead of scanning throught the partial
1028 * lists on other nodes.
1030 * If defrag_ratio is set to 0 then kmalloc() always
1031 * returns node local objects. If its higher then kmalloc()
1032 * may return off node objects in order to avoid fragmentation.
1034 * A higher ratio means slabs may be taken from other nodes
1035 * thus reducing the number of partial slabs on those nodes.
1037 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1038 * defrag_ratio = 1000) then every (well almost) allocation
1039 * will first attempt to defrag slab caches on other nodes. This
1040 * means scanning over all nodes to look for partial slabs which
1041 * may be a bit expensive to do on every slab allocation.
1043 if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
1046 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1047 ->node_zonelists[gfp_zone(flags)];
1048 for (z = zonelist->zones; *z; z++) {
1049 struct kmem_cache_node *n;
1051 n = get_node(s, zone_to_nid(*z));
1053 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1054 n->nr_partial > MIN_PARTIAL) {
1055 page = get_partial_node(n);
1065 * Get a partial page, lock it and return it.
1067 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1070 int searchnode = (node == -1) ? numa_node_id() : node;
1072 page = get_partial_node(get_node(s, searchnode));
1073 if (page || (flags & __GFP_THISNODE))
1076 return get_any_partial(s, flags);
1080 * Move a page back to the lists.
1082 * Must be called with the slab lock held.
1084 * On exit the slab lock will have been dropped.
1086 static void putback_slab(struct kmem_cache *s, struct page *page)
1088 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1093 add_partial(n, page);
1094 else if (PageError(page) && (s->flags & SLAB_STORE_USER))
1099 if (n->nr_partial < MIN_PARTIAL) {
1101 * Adding an empty page to the partial slabs in order
1102 * to avoid page allocator overhead. This page needs to
1103 * come after all the others that are not fully empty
1104 * in order to make sure that we do maximum
1107 add_partial_tail(n, page);
1111 discard_slab(s, page);
1117 * Remove the cpu slab
1119 static void deactivate_slab(struct kmem_cache *s, struct page *page, int cpu)
1121 s->cpu_slab[cpu] = NULL;
1122 ClearPageActive(page);
1124 putback_slab(s, page);
1127 static void flush_slab(struct kmem_cache *s, struct page *page, int cpu)
1130 deactivate_slab(s, page, cpu);
1135 * Called from IPI handler with interrupts disabled.
1137 static void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1139 struct page *page = s->cpu_slab[cpu];
1142 flush_slab(s, page, cpu);
1145 static void flush_cpu_slab(void *d)
1147 struct kmem_cache *s = d;
1148 int cpu = smp_processor_id();
1150 __flush_cpu_slab(s, cpu);
1153 static void flush_all(struct kmem_cache *s)
1156 on_each_cpu(flush_cpu_slab, s, 1, 1);
1158 unsigned long flags;
1160 local_irq_save(flags);
1162 local_irq_restore(flags);
1167 * slab_alloc is optimized to only modify two cachelines on the fast path
1168 * (aside from the stack):
1170 * 1. The page struct
1171 * 2. The first cacheline of the object to be allocated.
1173 * The only cache lines that are read (apart from code) is the
1174 * per cpu array in the kmem_cache struct.
1176 * Fastpath is not possible if we need to get a new slab or have
1177 * debugging enabled (which means all slabs are marked with PageError)
1179 static void *slab_alloc(struct kmem_cache *s,
1180 gfp_t gfpflags, int node, void *addr)
1184 unsigned long flags;
1187 local_irq_save(flags);
1188 cpu = smp_processor_id();
1189 page = s->cpu_slab[cpu];
1194 if (unlikely(node != -1 && page_to_nid(page) != node))
1197 object = page->freelist;
1198 if (unlikely(!object))
1200 if (unlikely(PageError(page)))
1205 page->freelist = object[page->offset];
1207 local_irq_restore(flags);
1211 deactivate_slab(s, page, cpu);
1214 page = get_partial(s, gfpflags, node);
1217 s->cpu_slab[cpu] = page;
1218 SetPageActive(page);
1222 page = new_slab(s, gfpflags, node);
1224 cpu = smp_processor_id();
1225 if (s->cpu_slab[cpu]) {
1227 * Someone else populated the cpu_slab while we enabled
1228 * interrupts, or we have got scheduled on another cpu.
1229 * The page may not be on the requested node.
1232 page_to_nid(s->cpu_slab[cpu]) == node) {
1234 * Current cpuslab is acceptable and we
1235 * want the current one since its cache hot
1237 discard_slab(s, page);
1238 page = s->cpu_slab[cpu];
1242 /* Dump the current slab */
1243 flush_slab(s, s->cpu_slab[cpu], cpu);
1248 local_irq_restore(flags);
1251 if (!alloc_object_checks(s, page, object))
1253 if (s->flags & SLAB_STORE_USER)
1254 set_track(s, object, TRACK_ALLOC, addr);
1255 if (s->flags & SLAB_TRACE) {
1256 printk(KERN_INFO "TRACE %s alloc 0x%p inuse=%d fp=0x%p\n",
1257 s->name, object, page->inuse,
1261 init_object(s, object, 1);
1265 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1267 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1269 EXPORT_SYMBOL(kmem_cache_alloc);
1272 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1274 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1276 EXPORT_SYMBOL(kmem_cache_alloc_node);
1280 * The fastpath only writes the cacheline of the page struct and the first
1281 * cacheline of the object.
1283 * No special cachelines need to be read
1285 static void slab_free(struct kmem_cache *s, struct page *page,
1286 void *x, void *addr)
1289 void **object = (void *)x;
1290 unsigned long flags;
1292 local_irq_save(flags);
1295 if (unlikely(PageError(page)))
1298 prior = object[page->offset] = page->freelist;
1299 page->freelist = object;
1302 if (unlikely(PageActive(page)))
1304 * Cpu slabs are never on partial lists and are
1309 if (unlikely(!page->inuse))
1313 * Objects left in the slab. If it
1314 * was not on the partial list before
1317 if (unlikely(!prior))
1318 add_partial(get_node(s, page_to_nid(page)), page);
1322 local_irq_restore(flags);
1328 * Slab on the partial list.
1330 remove_partial(s, page);
1333 discard_slab(s, page);
1334 local_irq_restore(flags);
1338 if (!free_object_checks(s, page, x))
1340 if (!PageActive(page) && !page->freelist)
1341 remove_full(s, page);
1342 if (s->flags & SLAB_STORE_USER)
1343 set_track(s, x, TRACK_FREE, addr);
1344 if (s->flags & SLAB_TRACE) {
1345 printk(KERN_INFO "TRACE %s free 0x%p inuse=%d fp=0x%p\n",
1346 s->name, object, page->inuse,
1348 print_section("Object", (void *)object, s->objsize);
1351 init_object(s, object, 0);
1355 void kmem_cache_free(struct kmem_cache *s, void *x)
1359 page = virt_to_head_page(x);
1361 slab_free(s, page, x, __builtin_return_address(0));
1363 EXPORT_SYMBOL(kmem_cache_free);
1365 /* Figure out on which slab object the object resides */
1366 static struct page *get_object_page(const void *x)
1368 struct page *page = virt_to_head_page(x);
1370 if (!PageSlab(page))
1377 * kmem_cache_open produces objects aligned at "size" and the first object
1378 * is placed at offset 0 in the slab (We have no metainformation on the
1379 * slab, all slabs are in essence "off slab").
1381 * In order to get the desired alignment one just needs to align the
1384 * Notice that the allocation order determines the sizes of the per cpu
1385 * caches. Each processor has always one slab available for allocations.
1386 * Increasing the allocation order reduces the number of times that slabs
1387 * must be moved on and off the partial lists and therefore may influence
1390 * The offset is used to relocate the free list link in each object. It is
1391 * therefore possible to move the free list link behind the object. This
1392 * is necessary for RCU to work properly and also useful for debugging.
1396 * Mininum / Maximum order of slab pages. This influences locking overhead
1397 * and slab fragmentation. A higher order reduces the number of partial slabs
1398 * and increases the number of allocations possible without having to
1399 * take the list_lock.
1401 static int slub_min_order;
1402 static int slub_max_order = DEFAULT_MAX_ORDER;
1405 * Minimum number of objects per slab. This is necessary in order to
1406 * reduce locking overhead. Similar to the queue size in SLAB.
1408 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1411 * Merge control. If this is set then no merging of slab caches will occur.
1413 static int slub_nomerge;
1418 static int slub_debug;
1420 static char *slub_debug_slabs;
1423 * Calculate the order of allocation given an slab object size.
1425 * The order of allocation has significant impact on other elements
1426 * of the system. Generally order 0 allocations should be preferred
1427 * since they do not cause fragmentation in the page allocator. Larger
1428 * objects may have problems with order 0 because there may be too much
1429 * space left unused in a slab. We go to a higher order if more than 1/8th
1430 * of the slab would be wasted.
1432 * In order to reach satisfactory performance we must ensure that
1433 * a minimum number of objects is in one slab. Otherwise we may
1434 * generate too much activity on the partial lists. This is less a
1435 * concern for large slabs though. slub_max_order specifies the order
1436 * where we begin to stop considering the number of objects in a slab.
1438 * Higher order allocations also allow the placement of more objects
1439 * in a slab and thereby reduce object handling overhead. If the user
1440 * has requested a higher mininum order then we start with that one
1443 static int calculate_order(int size)
1448 for (order = max(slub_min_order, fls(size - 1) - PAGE_SHIFT);
1449 order < MAX_ORDER; order++) {
1450 unsigned long slab_size = PAGE_SIZE << order;
1452 if (slub_max_order > order &&
1453 slab_size < slub_min_objects * size)
1456 if (slab_size < size)
1459 rem = slab_size % size;
1461 if (rem <= (PAGE_SIZE << order) / 8)
1465 if (order >= MAX_ORDER)
1471 * Function to figure out which alignment to use from the
1472 * various ways of specifying it.
1474 static unsigned long calculate_alignment(unsigned long flags,
1475 unsigned long align, unsigned long size)
1478 * If the user wants hardware cache aligned objects then
1479 * follow that suggestion if the object is sufficiently
1482 * The hardware cache alignment cannot override the
1483 * specified alignment though. If that is greater
1486 if ((flags & SLAB_HWCACHE_ALIGN) &&
1487 size > cache_line_size() / 2)
1488 return max_t(unsigned long, align, cache_line_size());
1490 if (align < ARCH_SLAB_MINALIGN)
1491 return ARCH_SLAB_MINALIGN;
1493 return ALIGN(align, sizeof(void *));
1496 static void init_kmem_cache_node(struct kmem_cache_node *n)
1499 atomic_long_set(&n->nr_slabs, 0);
1500 spin_lock_init(&n->list_lock);
1501 INIT_LIST_HEAD(&n->partial);
1502 INIT_LIST_HEAD(&n->full);
1507 * No kmalloc_node yet so do it by hand. We know that this is the first
1508 * slab on the node for this slabcache. There are no concurrent accesses
1511 * Note that this function only works on the kmalloc_node_cache
1512 * when allocating for the kmalloc_node_cache.
1514 static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
1518 struct kmem_cache_node *n;
1520 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
1522 page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
1523 /* new_slab() disables interupts */
1529 page->freelist = get_freepointer(kmalloc_caches, n);
1531 kmalloc_caches->node[node] = n;
1532 init_object(kmalloc_caches, n, 1);
1533 init_kmem_cache_node(n);
1534 atomic_long_inc(&n->nr_slabs);
1535 add_partial(n, page);
1539 static void free_kmem_cache_nodes(struct kmem_cache *s)
1543 for_each_online_node(node) {
1544 struct kmem_cache_node *n = s->node[node];
1545 if (n && n != &s->local_node)
1546 kmem_cache_free(kmalloc_caches, n);
1547 s->node[node] = NULL;
1551 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1556 if (slab_state >= UP)
1557 local_node = page_to_nid(virt_to_page(s));
1561 for_each_online_node(node) {
1562 struct kmem_cache_node *n;
1564 if (local_node == node)
1567 if (slab_state == DOWN) {
1568 n = early_kmem_cache_node_alloc(gfpflags,
1572 n = kmem_cache_alloc_node(kmalloc_caches,
1576 free_kmem_cache_nodes(s);
1582 init_kmem_cache_node(n);
1587 static void free_kmem_cache_nodes(struct kmem_cache *s)
1591 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
1593 init_kmem_cache_node(&s->local_node);
1599 * calculate_sizes() determines the order and the distribution of data within
1602 static int calculate_sizes(struct kmem_cache *s)
1604 unsigned long flags = s->flags;
1605 unsigned long size = s->objsize;
1606 unsigned long align = s->align;
1609 * Determine if we can poison the object itself. If the user of
1610 * the slab may touch the object after free or before allocation
1611 * then we should never poison the object itself.
1613 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1614 !s->ctor && !s->dtor)
1615 s->flags |= __OBJECT_POISON;
1617 s->flags &= ~__OBJECT_POISON;
1620 * Round up object size to the next word boundary. We can only
1621 * place the free pointer at word boundaries and this determines
1622 * the possible location of the free pointer.
1624 size = ALIGN(size, sizeof(void *));
1627 * If we are redzoning then check if there is some space between the
1628 * end of the object and the free pointer. If not then add an
1629 * additional word, so that we can establish a redzone between
1630 * the object and the freepointer to be able to check for overwrites.
1632 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
1633 size += sizeof(void *);
1636 * With that we have determined how much of the slab is in actual
1637 * use by the object. This is the potential offset to the free
1642 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1643 s->ctor || s->dtor)) {
1645 * Relocate free pointer after the object if it is not
1646 * permitted to overwrite the first word of the object on
1649 * This is the case if we do RCU, have a constructor or
1650 * destructor or are poisoning the objects.
1653 size += sizeof(void *);
1656 if (flags & SLAB_STORE_USER)
1658 * Need to store information about allocs and frees after
1661 size += 2 * sizeof(struct track);
1663 if (flags & SLAB_RED_ZONE)
1665 * Add some empty padding so that we can catch
1666 * overwrites from earlier objects rather than let
1667 * tracking information or the free pointer be
1668 * corrupted if an user writes before the start
1671 size += sizeof(void *);
1673 * Determine the alignment based on various parameters that the
1674 * user specified and the dynamic determination of cache line size
1677 align = calculate_alignment(flags, align, s->objsize);
1680 * SLUB stores one object immediately after another beginning from
1681 * offset 0. In order to align the objects we have to simply size
1682 * each object to conform to the alignment.
1684 size = ALIGN(size, align);
1687 s->order = calculate_order(size);
1692 * Determine the number of objects per slab
1694 s->objects = (PAGE_SIZE << s->order) / size;
1697 * Verify that the number of objects is within permitted limits.
1698 * The page->inuse field is only 16 bit wide! So we cannot have
1699 * more than 64k objects per slab.
1701 if (!s->objects || s->objects > 65535)
1707 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
1708 const char *name, size_t size,
1709 size_t align, unsigned long flags,
1710 void (*ctor)(void *, struct kmem_cache *, unsigned long),
1711 void (*dtor)(void *, struct kmem_cache *, unsigned long))
1713 memset(s, 0, kmem_size);
1722 * The page->offset field is only 16 bit wide. This is an offset
1723 * in units of words from the beginning of an object. If the slab
1724 * size is bigger then we cannot move the free pointer behind the
1727 * On 32 bit platforms the limit is 256k. On 64bit platforms
1728 * the limit is 512k.
1730 * Debugging or ctor/dtors may create a need to move the free
1731 * pointer. Fail if this happens.
1733 if (s->size >= 65535 * sizeof(void *)) {
1734 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1735 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1736 BUG_ON(ctor || dtor);
1740 * Enable debugging if selected on the kernel commandline.
1742 if (slub_debug && (!slub_debug_slabs ||
1743 strncmp(slub_debug_slabs, name,
1744 strlen(slub_debug_slabs)) == 0))
1745 s->flags |= slub_debug;
1747 if (!calculate_sizes(s))
1752 s->defrag_ratio = 100;
1755 if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
1758 if (flags & SLAB_PANIC)
1759 panic("Cannot create slab %s size=%lu realsize=%u "
1760 "order=%u offset=%u flags=%lx\n",
1761 s->name, (unsigned long)size, s->size, s->order,
1765 EXPORT_SYMBOL(kmem_cache_open);
1768 * Check if a given pointer is valid
1770 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
1775 page = get_object_page(object);
1777 if (!page || s != page->slab)
1778 /* No slab or wrong slab */
1781 if (!check_valid_pointer(s, page, object))
1785 * We could also check if the object is on the slabs freelist.
1786 * But this would be too expensive and it seems that the main
1787 * purpose of kmem_ptr_valid is to check if the object belongs
1788 * to a certain slab.
1792 EXPORT_SYMBOL(kmem_ptr_validate);
1795 * Determine the size of a slab object
1797 unsigned int kmem_cache_size(struct kmem_cache *s)
1801 EXPORT_SYMBOL(kmem_cache_size);
1803 const char *kmem_cache_name(struct kmem_cache *s)
1807 EXPORT_SYMBOL(kmem_cache_name);
1810 * Attempt to free all slabs on a node
1812 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
1813 struct list_head *list)
1815 int slabs_inuse = 0;
1816 unsigned long flags;
1817 struct page *page, *h;
1819 spin_lock_irqsave(&n->list_lock, flags);
1820 list_for_each_entry_safe(page, h, list, lru)
1822 list_del(&page->lru);
1823 discard_slab(s, page);
1826 spin_unlock_irqrestore(&n->list_lock, flags);
1831 * Release all resources used by slab cache
1833 static int kmem_cache_close(struct kmem_cache *s)
1839 /* Attempt to free all objects */
1840 for_each_online_node(node) {
1841 struct kmem_cache_node *n = get_node(s, node);
1843 n->nr_partial -= free_list(s, n, &n->partial);
1844 if (atomic_long_read(&n->nr_slabs))
1847 free_kmem_cache_nodes(s);
1852 * Close a cache and release the kmem_cache structure
1853 * (must be used for caches created using kmem_cache_create)
1855 void kmem_cache_destroy(struct kmem_cache *s)
1857 down_write(&slub_lock);
1861 if (kmem_cache_close(s))
1863 sysfs_slab_remove(s);
1866 up_write(&slub_lock);
1868 EXPORT_SYMBOL(kmem_cache_destroy);
1870 /********************************************************************
1872 *******************************************************************/
1874 struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
1875 EXPORT_SYMBOL(kmalloc_caches);
1877 #ifdef CONFIG_ZONE_DMA
1878 static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
1881 static int __init setup_slub_min_order(char *str)
1883 get_option (&str, &slub_min_order);
1888 __setup("slub_min_order=", setup_slub_min_order);
1890 static int __init setup_slub_max_order(char *str)
1892 get_option (&str, &slub_max_order);
1897 __setup("slub_max_order=", setup_slub_max_order);
1899 static int __init setup_slub_min_objects(char *str)
1901 get_option (&str, &slub_min_objects);
1906 __setup("slub_min_objects=", setup_slub_min_objects);
1908 static int __init setup_slub_nomerge(char *str)
1914 __setup("slub_nomerge", setup_slub_nomerge);
1916 static int __init setup_slub_debug(char *str)
1918 if (!str || *str != '=')
1919 slub_debug = DEBUG_DEFAULT_FLAGS;
1922 if (*str == 0 || *str == ',')
1923 slub_debug = DEBUG_DEFAULT_FLAGS;
1925 for( ;*str && *str != ','; str++)
1927 case 'f' : case 'F' :
1928 slub_debug |= SLAB_DEBUG_FREE;
1930 case 'z' : case 'Z' :
1931 slub_debug |= SLAB_RED_ZONE;
1933 case 'p' : case 'P' :
1934 slub_debug |= SLAB_POISON;
1936 case 'u' : case 'U' :
1937 slub_debug |= SLAB_STORE_USER;
1939 case 't' : case 'T' :
1940 slub_debug |= SLAB_TRACE;
1943 printk(KERN_ERR "slub_debug option '%c' "
1944 "unknown. skipped\n",*str);
1949 slub_debug_slabs = str + 1;
1953 __setup("slub_debug", setup_slub_debug);
1955 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
1956 const char *name, int size, gfp_t gfp_flags)
1958 unsigned int flags = 0;
1960 if (gfp_flags & SLUB_DMA)
1961 flags = SLAB_CACHE_DMA;
1963 down_write(&slub_lock);
1964 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
1968 list_add(&s->list, &slab_caches);
1969 up_write(&slub_lock);
1970 if (sysfs_slab_add(s))
1975 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
1978 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
1980 int index = kmalloc_index(size);
1985 /* Allocation too large? */
1988 #ifdef CONFIG_ZONE_DMA
1989 if ((flags & SLUB_DMA)) {
1990 struct kmem_cache *s;
1991 struct kmem_cache *x;
1995 s = kmalloc_caches_dma[index];
1999 /* Dynamically create dma cache */
2000 x = kmalloc(kmem_size, flags & ~SLUB_DMA);
2002 panic("Unable to allocate memory for dma cache\n");
2004 if (index <= KMALLOC_SHIFT_HIGH)
2005 realsize = 1 << index;
2013 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2014 (unsigned int)realsize);
2015 s = create_kmalloc_cache(x, text, realsize, flags);
2016 kmalloc_caches_dma[index] = s;
2020 return &kmalloc_caches[index];
2023 void *__kmalloc(size_t size, gfp_t flags)
2025 struct kmem_cache *s = get_slab(size, flags);
2028 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2031 EXPORT_SYMBOL(__kmalloc);
2034 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2036 struct kmem_cache *s = get_slab(size, flags);
2039 return slab_alloc(s, flags, node, __builtin_return_address(0));
2042 EXPORT_SYMBOL(__kmalloc_node);
2045 size_t ksize(const void *object)
2047 struct page *page = get_object_page(object);
2048 struct kmem_cache *s;
2055 * Debugging requires use of the padding between object
2056 * and whatever may come after it.
2058 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2062 * If we have the need to store the freelist pointer
2063 * back there or track user information then we can
2064 * only use the space before that information.
2066 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2070 * Else we can use all the padding etc for the allocation
2074 EXPORT_SYMBOL(ksize);
2076 void kfree(const void *x)
2078 struct kmem_cache *s;
2084 page = virt_to_head_page(x);
2087 slab_free(s, page, (void *)x, __builtin_return_address(0));
2089 EXPORT_SYMBOL(kfree);
2092 * kmem_cache_shrink removes empty slabs from the partial lists
2093 * and then sorts the partially allocated slabs by the number
2094 * of items in use. The slabs with the most items in use
2095 * come first. New allocations will remove these from the
2096 * partial list because they are full. The slabs with the
2097 * least items are placed last. If it happens that the objects
2098 * are freed then the page can be returned to the page allocator.
2100 int kmem_cache_shrink(struct kmem_cache *s)
2104 struct kmem_cache_node *n;
2107 struct list_head *slabs_by_inuse =
2108 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2109 unsigned long flags;
2111 if (!slabs_by_inuse)
2115 for_each_online_node(node) {
2116 n = get_node(s, node);
2121 for (i = 0; i < s->objects; i++)
2122 INIT_LIST_HEAD(slabs_by_inuse + i);
2124 spin_lock_irqsave(&n->list_lock, flags);
2127 * Build lists indexed by the items in use in
2128 * each slab or free slabs if empty.
2130 * Note that concurrent frees may occur while
2131 * we hold the list_lock. page->inuse here is
2134 list_for_each_entry_safe(page, t, &n->partial, lru) {
2135 if (!page->inuse && slab_trylock(page)) {
2137 * Must hold slab lock here because slab_free
2138 * may have freed the last object and be
2139 * waiting to release the slab.
2141 list_del(&page->lru);
2144 discard_slab(s, page);
2146 if (n->nr_partial > MAX_PARTIAL)
2147 list_move(&page->lru,
2148 slabs_by_inuse + page->inuse);
2152 if (n->nr_partial <= MAX_PARTIAL)
2156 * Rebuild the partial list with the slabs filled up
2157 * most first and the least used slabs at the end.
2159 for (i = s->objects - 1; i >= 0; i--)
2160 list_splice(slabs_by_inuse + i, n->partial.prev);
2163 spin_unlock_irqrestore(&n->list_lock, flags);
2166 kfree(slabs_by_inuse);
2169 EXPORT_SYMBOL(kmem_cache_shrink);
2172 * krealloc - reallocate memory. The contents will remain unchanged.
2174 * @p: object to reallocate memory for.
2175 * @new_size: how many bytes of memory are required.
2176 * @flags: the type of memory to allocate.
2178 * The contents of the object pointed to are preserved up to the
2179 * lesser of the new and old sizes. If @p is %NULL, krealloc()
2180 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
2181 * %NULL pointer, the object pointed to is freed.
2183 void *krealloc(const void *p, size_t new_size, gfp_t flags)
2189 return kmalloc(new_size, flags);
2191 if (unlikely(!new_size)) {
2200 ret = kmalloc(new_size, flags);
2202 memcpy(ret, p, min(new_size, ks));
2207 EXPORT_SYMBOL(krealloc);
2209 /********************************************************************
2210 * Basic setup of slabs
2211 *******************************************************************/
2213 void __init kmem_cache_init(void)
2219 * Must first have the slab cache available for the allocations of the
2220 * struct kmalloc_cache_node's. There is special bootstrap code in
2221 * kmem_cache_open for slab_state == DOWN.
2223 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2224 sizeof(struct kmem_cache_node), GFP_KERNEL);
2227 /* Able to allocate the per node structures */
2228 slab_state = PARTIAL;
2230 /* Caches that are not of the two-to-the-power-of size */
2231 create_kmalloc_cache(&kmalloc_caches[1],
2232 "kmalloc-96", 96, GFP_KERNEL);
2233 create_kmalloc_cache(&kmalloc_caches[2],
2234 "kmalloc-192", 192, GFP_KERNEL);
2236 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2237 create_kmalloc_cache(&kmalloc_caches[i],
2238 "kmalloc", 1 << i, GFP_KERNEL);
2242 /* Provide the correct kmalloc names now that the caches are up */
2243 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
2244 kmalloc_caches[i]. name =
2245 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2248 register_cpu_notifier(&slab_notifier);
2251 if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
2252 kmem_size = offsetof(struct kmem_cache, cpu_slab)
2253 + nr_cpu_ids * sizeof(struct page *);
2255 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2256 " Processors=%d, Nodes=%d\n",
2257 KMALLOC_SHIFT_HIGH, cache_line_size(),
2258 slub_min_order, slub_max_order, slub_min_objects,
2259 nr_cpu_ids, nr_node_ids);
2263 * Find a mergeable slab cache
2265 static int slab_unmergeable(struct kmem_cache *s)
2267 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2270 if (s->ctor || s->dtor)
2276 static struct kmem_cache *find_mergeable(size_t size,
2277 size_t align, unsigned long flags,
2278 void (*ctor)(void *, struct kmem_cache *, unsigned long),
2279 void (*dtor)(void *, struct kmem_cache *, unsigned long))
2281 struct list_head *h;
2283 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2289 size = ALIGN(size, sizeof(void *));
2290 align = calculate_alignment(flags, align, size);
2291 size = ALIGN(size, align);
2293 list_for_each(h, &slab_caches) {
2294 struct kmem_cache *s =
2295 container_of(h, struct kmem_cache, list);
2297 if (slab_unmergeable(s))
2303 if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
2304 (s->flags & SLUB_MERGE_SAME))
2307 * Check if alignment is compatible.
2308 * Courtesy of Adrian Drzewiecki
2310 if ((s->size & ~(align -1)) != s->size)
2313 if (s->size - size >= sizeof(void *))
2321 struct kmem_cache *kmem_cache_create(const char *name, 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 kmem_cache *s;
2328 down_write(&slub_lock);
2329 s = find_mergeable(size, align, flags, dtor, ctor);
2333 * Adjust the object sizes so that we clear
2334 * the complete object on kzalloc.
2336 s->objsize = max(s->objsize, (int)size);
2337 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
2338 if (sysfs_slab_alias(s, name))
2341 s = kmalloc(kmem_size, GFP_KERNEL);
2342 if (s && kmem_cache_open(s, GFP_KERNEL, name,
2343 size, align, flags, ctor, dtor)) {
2344 if (sysfs_slab_add(s)) {
2348 list_add(&s->list, &slab_caches);
2352 up_write(&slub_lock);
2356 up_write(&slub_lock);
2357 if (flags & SLAB_PANIC)
2358 panic("Cannot create slabcache %s\n", name);
2363 EXPORT_SYMBOL(kmem_cache_create);
2365 void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
2369 x = slab_alloc(s, flags, -1, __builtin_return_address(0));
2371 memset(x, 0, s->objsize);
2374 EXPORT_SYMBOL(kmem_cache_zalloc);
2377 static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
2379 struct list_head *h;
2381 down_read(&slub_lock);
2382 list_for_each(h, &slab_caches) {
2383 struct kmem_cache *s =
2384 container_of(h, struct kmem_cache, list);
2388 up_read(&slub_lock);
2392 * Use the cpu notifier to insure that the slab are flushed
2395 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
2396 unsigned long action, void *hcpu)
2398 long cpu = (long)hcpu;
2401 case CPU_UP_CANCELED:
2403 for_all_slabs(__flush_cpu_slab, cpu);
2411 static struct notifier_block __cpuinitdata slab_notifier =
2412 { &slab_cpuup_callback, NULL, 0 };
2418 /*****************************************************************
2419 * Generic reaper used to support the page allocator
2420 * (the cpu slabs are reaped by a per slab workqueue).
2422 * Maybe move this to the page allocator?
2423 ****************************************************************/
2425 static DEFINE_PER_CPU(unsigned long, reap_node);
2427 static void init_reap_node(int cpu)
2431 node = next_node(cpu_to_node(cpu), node_online_map);
2432 if (node == MAX_NUMNODES)
2433 node = first_node(node_online_map);
2435 __get_cpu_var(reap_node) = node;
2438 static void next_reap_node(void)
2440 int node = __get_cpu_var(reap_node);
2443 * Also drain per cpu pages on remote zones
2445 if (node != numa_node_id())
2446 drain_node_pages(node);
2448 node = next_node(node, node_online_map);
2449 if (unlikely(node >= MAX_NUMNODES))
2450 node = first_node(node_online_map);
2451 __get_cpu_var(reap_node) = node;
2454 #define init_reap_node(cpu) do { } while (0)
2455 #define next_reap_node(void) do { } while (0)
2458 #define REAPTIMEOUT_CPUC (2*HZ)
2461 static DEFINE_PER_CPU(struct delayed_work, reap_work);
2463 static void cache_reap(struct work_struct *unused)
2466 refresh_cpu_vm_stats(smp_processor_id());
2467 schedule_delayed_work(&__get_cpu_var(reap_work),
2471 static void __devinit start_cpu_timer(int cpu)
2473 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
2476 * When this gets called from do_initcalls via cpucache_init(),
2477 * init_workqueues() has already run, so keventd will be setup
2480 if (keventd_up() && reap_work->work.func == NULL) {
2481 init_reap_node(cpu);
2482 INIT_DELAYED_WORK(reap_work, cache_reap);
2483 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
2487 static int __init cpucache_init(void)
2492 * Register the timers that drain pcp pages and update vm statistics
2494 for_each_online_cpu(cpu)
2495 start_cpu_timer(cpu);
2498 __initcall(cpucache_init);
2501 #ifdef SLUB_RESILIENCY_TEST
2502 static unsigned long validate_slab_cache(struct kmem_cache *s);
2504 static void resiliency_test(void)
2508 printk(KERN_ERR "SLUB resiliency testing\n");
2509 printk(KERN_ERR "-----------------------\n");
2510 printk(KERN_ERR "A. Corruption after allocation\n");
2512 p = kzalloc(16, GFP_KERNEL);
2514 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
2515 " 0x12->0x%p\n\n", p + 16);
2517 validate_slab_cache(kmalloc_caches + 4);
2519 /* Hmmm... The next two are dangerous */
2520 p = kzalloc(32, GFP_KERNEL);
2521 p[32 + sizeof(void *)] = 0x34;
2522 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
2523 " 0x34 -> -0x%p\n", p);
2524 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2526 validate_slab_cache(kmalloc_caches + 5);
2527 p = kzalloc(64, GFP_KERNEL);
2528 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
2530 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
2532 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
2533 validate_slab_cache(kmalloc_caches + 6);
2535 printk(KERN_ERR "\nB. Corruption after free\n");
2536 p = kzalloc(128, GFP_KERNEL);
2539 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
2540 validate_slab_cache(kmalloc_caches + 7);
2542 p = kzalloc(256, GFP_KERNEL);
2545 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
2546 validate_slab_cache(kmalloc_caches + 8);
2548 p = kzalloc(512, GFP_KERNEL);
2551 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
2552 validate_slab_cache(kmalloc_caches + 9);
2555 static void resiliency_test(void) {};
2559 * These are not as efficient as kmalloc for the non debug case.
2560 * We do not have the page struct available so we have to touch one
2561 * cacheline in struct kmem_cache to check slab flags.
2563 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
2565 struct kmem_cache *s = get_slab(size, gfpflags);
2570 return slab_alloc(s, gfpflags, -1, caller);
2573 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
2574 int node, void *caller)
2576 struct kmem_cache *s = get_slab(size, gfpflags);
2581 return slab_alloc(s, gfpflags, node, caller);
2586 static int validate_slab(struct kmem_cache *s, struct page *page)
2589 void *addr = page_address(page);
2590 unsigned long map[BITS_TO_LONGS(s->objects)];
2592 if (!check_slab(s, page) ||
2593 !on_freelist(s, page, NULL))
2596 /* Now we know that a valid freelist exists */
2597 bitmap_zero(map, s->objects);
2599 for(p = page->freelist; p; p = get_freepointer(s, p)) {
2600 set_bit((p - addr) / s->size, map);
2601 if (!check_object(s, page, p, 0))
2605 for(p = addr; p < addr + s->objects * s->size; p += s->size)
2606 if (!test_bit((p - addr) / s->size, map))
2607 if (!check_object(s, page, p, 1))
2612 static void validate_slab_slab(struct kmem_cache *s, struct page *page)
2614 if (slab_trylock(page)) {
2615 validate_slab(s, page);
2618 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
2621 if (s->flags & DEBUG_DEFAULT_FLAGS) {
2622 if (!PageError(page))
2623 printk(KERN_ERR "SLUB %s: PageError not set "
2624 "on slab 0x%p\n", s->name, page);
2626 if (PageError(page))
2627 printk(KERN_ERR "SLUB %s: PageError set on "
2628 "slab 0x%p\n", s->name, page);
2632 static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
2634 unsigned long count = 0;
2636 unsigned long flags;
2638 spin_lock_irqsave(&n->list_lock, flags);
2640 list_for_each_entry(page, &n->partial, lru) {
2641 validate_slab_slab(s, page);
2644 if (count != n->nr_partial)
2645 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
2646 "counter=%ld\n", s->name, count, n->nr_partial);
2648 if (!(s->flags & SLAB_STORE_USER))
2651 list_for_each_entry(page, &n->full, lru) {
2652 validate_slab_slab(s, page);
2655 if (count != atomic_long_read(&n->nr_slabs))
2656 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
2657 "counter=%ld\n", s->name, count,
2658 atomic_long_read(&n->nr_slabs));
2661 spin_unlock_irqrestore(&n->list_lock, flags);
2665 static unsigned long validate_slab_cache(struct kmem_cache *s)
2668 unsigned long count = 0;
2671 for_each_online_node(node) {
2672 struct kmem_cache_node *n = get_node(s, node);
2674 count += validate_slab_node(s, n);
2680 * Generate lists of locations where slabcache objects are allocated
2685 unsigned long count;
2691 unsigned long count;
2692 struct location *loc;
2695 static void free_loc_track(struct loc_track *t)
2698 free_pages((unsigned long)t->loc,
2699 get_order(sizeof(struct location) * t->max));
2702 static int alloc_loc_track(struct loc_track *t, unsigned long max)
2708 max = PAGE_SIZE / sizeof(struct location);
2710 order = get_order(sizeof(struct location) * max);
2712 l = (void *)__get_free_pages(GFP_KERNEL, order);
2718 memcpy(l, t->loc, sizeof(struct location) * t->count);
2726 static int add_location(struct loc_track *t, struct kmem_cache *s,
2729 long start, end, pos;
2737 pos = start + (end - start + 1) / 2;
2740 * There is nothing at "end". If we end up there
2741 * we need to add something to before end.
2746 caddr = t->loc[pos].addr;
2747 if (addr == caddr) {
2748 t->loc[pos].count++;
2759 * Not found. Insert new tracking element
2761 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
2767 (t->count - pos) * sizeof(struct location));
2774 static void process_slab(struct loc_track *t, struct kmem_cache *s,
2775 struct page *page, enum track_item alloc)
2777 void *addr = page_address(page);
2778 unsigned long map[BITS_TO_LONGS(s->objects)];
2781 bitmap_zero(map, s->objects);
2782 for (p = page->freelist; p; p = get_freepointer(s, p))
2783 set_bit((p - addr) / s->size, map);
2785 for (p = addr; p < addr + s->objects * s->size; p += s->size)
2786 if (!test_bit((p - addr) / s->size, map)) {
2787 void *addr = get_track(s, p, alloc)->addr;
2789 add_location(t, s, addr);
2793 static int list_locations(struct kmem_cache *s, char *buf,
2794 enum track_item alloc)
2804 /* Push back cpu slabs */
2807 for_each_online_node(node) {
2808 struct kmem_cache_node *n = get_node(s, node);
2809 unsigned long flags;
2812 if (!atomic_read(&n->nr_slabs))
2815 spin_lock_irqsave(&n->list_lock, flags);
2816 list_for_each_entry(page, &n->partial, lru)
2817 process_slab(&t, s, page, alloc);
2818 list_for_each_entry(page, &n->full, lru)
2819 process_slab(&t, s, page, alloc);
2820 spin_unlock_irqrestore(&n->list_lock, flags);
2823 for (i = 0; i < t.count; i++) {
2824 void *addr = t.loc[i].addr;
2826 if (n > PAGE_SIZE - 100)
2828 n += sprintf(buf + n, "%7ld ", t.loc[i].count);
2830 n += sprint_symbol(buf + n, (unsigned long)t.loc[i].addr);
2832 n += sprintf(buf + n, "<not-available>");
2833 n += sprintf(buf + n, "\n");
2838 n += sprintf(buf, "No data\n");
2842 static unsigned long count_partial(struct kmem_cache_node *n)
2844 unsigned long flags;
2845 unsigned long x = 0;
2848 spin_lock_irqsave(&n->list_lock, flags);
2849 list_for_each_entry(page, &n->partial, lru)
2851 spin_unlock_irqrestore(&n->list_lock, flags);
2855 enum slab_stat_type {
2862 #define SO_FULL (1 << SL_FULL)
2863 #define SO_PARTIAL (1 << SL_PARTIAL)
2864 #define SO_CPU (1 << SL_CPU)
2865 #define SO_OBJECTS (1 << SL_OBJECTS)
2867 static unsigned long slab_objects(struct kmem_cache *s,
2868 char *buf, unsigned long flags)
2870 unsigned long total = 0;
2874 unsigned long *nodes;
2875 unsigned long *per_cpu;
2877 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
2878 per_cpu = nodes + nr_node_ids;
2880 for_each_possible_cpu(cpu) {
2881 struct page *page = s->cpu_slab[cpu];
2885 node = page_to_nid(page);
2886 if (flags & SO_CPU) {
2889 if (flags & SO_OBJECTS)
2900 for_each_online_node(node) {
2901 struct kmem_cache_node *n = get_node(s, node);
2903 if (flags & SO_PARTIAL) {
2904 if (flags & SO_OBJECTS)
2905 x = count_partial(n);
2912 if (flags & SO_FULL) {
2913 int full_slabs = atomic_read(&n->nr_slabs)
2917 if (flags & SO_OBJECTS)
2918 x = full_slabs * s->objects;
2926 x = sprintf(buf, "%lu", total);
2928 for_each_online_node(node)
2930 x += sprintf(buf + x, " N%d=%lu",
2934 return x + sprintf(buf + x, "\n");
2937 static int any_slab_objects(struct kmem_cache *s)
2942 for_each_possible_cpu(cpu)
2943 if (s->cpu_slab[cpu])
2946 for_each_node(node) {
2947 struct kmem_cache_node *n = get_node(s, node);
2949 if (n->nr_partial || atomic_read(&n->nr_slabs))
2955 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
2956 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
2958 struct slab_attribute {
2959 struct attribute attr;
2960 ssize_t (*show)(struct kmem_cache *s, char *buf);
2961 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
2964 #define SLAB_ATTR_RO(_name) \
2965 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
2967 #define SLAB_ATTR(_name) \
2968 static struct slab_attribute _name##_attr = \
2969 __ATTR(_name, 0644, _name##_show, _name##_store)
2971 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
2973 return sprintf(buf, "%d\n", s->size);
2975 SLAB_ATTR_RO(slab_size);
2977 static ssize_t align_show(struct kmem_cache *s, char *buf)
2979 return sprintf(buf, "%d\n", s->align);
2981 SLAB_ATTR_RO(align);
2983 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
2985 return sprintf(buf, "%d\n", s->objsize);
2987 SLAB_ATTR_RO(object_size);
2989 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
2991 return sprintf(buf, "%d\n", s->objects);
2993 SLAB_ATTR_RO(objs_per_slab);
2995 static ssize_t order_show(struct kmem_cache *s, char *buf)
2997 return sprintf(buf, "%d\n", s->order);
2999 SLAB_ATTR_RO(order);
3001 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3004 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3006 return n + sprintf(buf + n, "\n");
3012 static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3015 int n = sprint_symbol(buf, (unsigned long)s->dtor);
3017 return n + sprintf(buf + n, "\n");
3023 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3025 return sprintf(buf, "%d\n", s->refcount - 1);
3027 SLAB_ATTR_RO(aliases);
3029 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3031 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3033 SLAB_ATTR_RO(slabs);
3035 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3037 return slab_objects(s, buf, SO_PARTIAL);
3039 SLAB_ATTR_RO(partial);
3041 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3043 return slab_objects(s, buf, SO_CPU);
3045 SLAB_ATTR_RO(cpu_slabs);
3047 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3049 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3051 SLAB_ATTR_RO(objects);
3053 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3055 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3058 static ssize_t sanity_checks_store(struct kmem_cache *s,
3059 const char *buf, size_t length)
3061 s->flags &= ~SLAB_DEBUG_FREE;
3063 s->flags |= SLAB_DEBUG_FREE;
3066 SLAB_ATTR(sanity_checks);
3068 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3070 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3073 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3076 s->flags &= ~SLAB_TRACE;
3078 s->flags |= SLAB_TRACE;
3083 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3085 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3088 static ssize_t reclaim_account_store(struct kmem_cache *s,
3089 const char *buf, size_t length)
3091 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3093 s->flags |= SLAB_RECLAIM_ACCOUNT;
3096 SLAB_ATTR(reclaim_account);
3098 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3100 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3102 SLAB_ATTR_RO(hwcache_align);
3104 #ifdef CONFIG_ZONE_DMA
3105 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3107 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3109 SLAB_ATTR_RO(cache_dma);
3112 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3114 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3116 SLAB_ATTR_RO(destroy_by_rcu);
3118 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3120 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3123 static ssize_t red_zone_store(struct kmem_cache *s,
3124 const char *buf, size_t length)
3126 if (any_slab_objects(s))
3129 s->flags &= ~SLAB_RED_ZONE;
3131 s->flags |= SLAB_RED_ZONE;
3135 SLAB_ATTR(red_zone);
3137 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3139 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3142 static ssize_t poison_store(struct kmem_cache *s,
3143 const char *buf, size_t length)
3145 if (any_slab_objects(s))
3148 s->flags &= ~SLAB_POISON;
3150 s->flags |= SLAB_POISON;
3156 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3158 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3161 static ssize_t store_user_store(struct kmem_cache *s,
3162 const char *buf, size_t length)
3164 if (any_slab_objects(s))
3167 s->flags &= ~SLAB_STORE_USER;
3169 s->flags |= SLAB_STORE_USER;
3173 SLAB_ATTR(store_user);
3175 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3180 static ssize_t validate_store(struct kmem_cache *s,
3181 const char *buf, size_t length)
3184 validate_slab_cache(s);
3189 SLAB_ATTR(validate);
3191 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3196 static ssize_t shrink_store(struct kmem_cache *s,
3197 const char *buf, size_t length)
3199 if (buf[0] == '1') {
3200 int rc = kmem_cache_shrink(s);
3210 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3212 if (!(s->flags & SLAB_STORE_USER))
3214 return list_locations(s, buf, TRACK_ALLOC);
3216 SLAB_ATTR_RO(alloc_calls);
3218 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3220 if (!(s->flags & SLAB_STORE_USER))
3222 return list_locations(s, buf, TRACK_FREE);
3224 SLAB_ATTR_RO(free_calls);
3227 static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
3229 return sprintf(buf, "%d\n", s->defrag_ratio / 10);
3232 static ssize_t defrag_ratio_store(struct kmem_cache *s,
3233 const char *buf, size_t length)
3235 int n = simple_strtoul(buf, NULL, 10);
3238 s->defrag_ratio = n * 10;
3241 SLAB_ATTR(defrag_ratio);
3244 static struct attribute * slab_attrs[] = {
3245 &slab_size_attr.attr,
3246 &object_size_attr.attr,
3247 &objs_per_slab_attr.attr,
3252 &cpu_slabs_attr.attr,
3257 &sanity_checks_attr.attr,
3259 &hwcache_align_attr.attr,
3260 &reclaim_account_attr.attr,
3261 &destroy_by_rcu_attr.attr,
3262 &red_zone_attr.attr,
3264 &store_user_attr.attr,
3265 &validate_attr.attr,
3267 &alloc_calls_attr.attr,
3268 &free_calls_attr.attr,
3269 #ifdef CONFIG_ZONE_DMA
3270 &cache_dma_attr.attr,
3273 &defrag_ratio_attr.attr,
3278 static struct attribute_group slab_attr_group = {
3279 .attrs = slab_attrs,
3282 static ssize_t slab_attr_show(struct kobject *kobj,
3283 struct attribute *attr,
3286 struct slab_attribute *attribute;
3287 struct kmem_cache *s;
3290 attribute = to_slab_attr(attr);
3293 if (!attribute->show)
3296 err = attribute->show(s, buf);
3301 static ssize_t slab_attr_store(struct kobject *kobj,
3302 struct attribute *attr,
3303 const char *buf, size_t len)
3305 struct slab_attribute *attribute;
3306 struct kmem_cache *s;
3309 attribute = to_slab_attr(attr);
3312 if (!attribute->store)
3315 err = attribute->store(s, buf, len);
3320 static struct sysfs_ops slab_sysfs_ops = {
3321 .show = slab_attr_show,
3322 .store = slab_attr_store,
3325 static struct kobj_type slab_ktype = {
3326 .sysfs_ops = &slab_sysfs_ops,
3329 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3331 struct kobj_type *ktype = get_ktype(kobj);
3333 if (ktype == &slab_ktype)
3338 static struct kset_uevent_ops slab_uevent_ops = {
3339 .filter = uevent_filter,
3342 decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
3344 #define ID_STR_LENGTH 64
3346 /* Create a unique string id for a slab cache:
3348 * :[flags-]size:[memory address of kmemcache]
3350 static char *create_unique_id(struct kmem_cache *s)
3352 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
3359 * First flags affecting slabcache operations. We will only
3360 * get here for aliasable slabs so we do not need to support
3361 * too many flags. The flags here must cover all flags that
3362 * are matched during merging to guarantee that the id is
3365 if (s->flags & SLAB_CACHE_DMA)
3367 if (s->flags & SLAB_RECLAIM_ACCOUNT)
3369 if (s->flags & SLAB_DEBUG_FREE)
3373 p += sprintf(p, "%07d", s->size);
3374 BUG_ON(p > name + ID_STR_LENGTH - 1);
3378 static int sysfs_slab_add(struct kmem_cache *s)
3384 if (slab_state < SYSFS)
3385 /* Defer until later */
3388 unmergeable = slab_unmergeable(s);
3391 * Slabcache can never be merged so we can use the name proper.
3392 * This is typically the case for debug situations. In that
3393 * case we can catch duplicate names easily.
3395 sysfs_remove_link(&slab_subsys.kobj, s->name);
3399 * Create a unique name for the slab as a target
3402 name = create_unique_id(s);
3405 kobj_set_kset_s(s, slab_subsys);
3406 kobject_set_name(&s->kobj, name);
3407 kobject_init(&s->kobj);
3408 err = kobject_add(&s->kobj);
3412 err = sysfs_create_group(&s->kobj, &slab_attr_group);
3415 kobject_uevent(&s->kobj, KOBJ_ADD);
3417 /* Setup first alias */
3418 sysfs_slab_alias(s, s->name);
3424 static void sysfs_slab_remove(struct kmem_cache *s)
3426 kobject_uevent(&s->kobj, KOBJ_REMOVE);
3427 kobject_del(&s->kobj);
3431 * Need to buffer aliases during bootup until sysfs becomes
3432 * available lest we loose that information.
3434 struct saved_alias {
3435 struct kmem_cache *s;
3437 struct saved_alias *next;
3440 struct saved_alias *alias_list;
3442 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
3444 struct saved_alias *al;
3446 if (slab_state == SYSFS) {
3448 * If we have a leftover link then remove it.
3450 sysfs_remove_link(&slab_subsys.kobj, name);
3451 return sysfs_create_link(&slab_subsys.kobj,
3455 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
3461 al->next = alias_list;
3466 static int __init slab_sysfs_init(void)
3468 struct list_head *h;
3471 err = subsystem_register(&slab_subsys);
3473 printk(KERN_ERR "Cannot register slab subsystem.\n");
3479 list_for_each(h, &slab_caches) {
3480 struct kmem_cache *s =
3481 container_of(h, struct kmem_cache, list);
3483 err = sysfs_slab_add(s);
3487 while (alias_list) {
3488 struct saved_alias *al = alias_list;
3490 alias_list = alias_list->next;
3491 err = sysfs_slab_alias(al->s, al->name);
3500 __initcall(slab_sysfs_init);