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>
23 #include <linux/memory.h>
30 * The slab_lock protects operations on the object of a particular
31 * slab and its metadata in the page struct. If the slab lock
32 * has been taken then no allocations nor frees can be performed
33 * on the objects in the slab nor can the slab be added or removed
34 * from the partial or full lists since this would mean modifying
35 * the page_struct of the slab.
37 * The list_lock protects the partial and full list on each node and
38 * the partial slab counter. If taken then no new slabs may be added or
39 * removed from the lists nor make the number of partial slabs be modified.
40 * (Note that the total number of slabs is an atomic value that may be
41 * modified without taking the list lock).
43 * The list_lock is a centralized lock and thus we avoid taking it as
44 * much as possible. As long as SLUB does not have to handle partial
45 * slabs, operations can continue without any centralized lock. F.e.
46 * allocating a long series of objects that fill up slabs does not require
49 * The lock order is sometimes inverted when we are trying to get a slab
50 * off a list. We take the list_lock and then look for a page on the list
51 * to use. While we do that objects in the slabs may be freed. We can
52 * only operate on the slab if we have also taken the slab_lock. So we use
53 * a slab_trylock() on the slab. If trylock was successful then no frees
54 * can occur anymore and we can use the slab for allocations etc. If the
55 * slab_trylock() does not succeed then frees are in progress in the slab and
56 * we must stay away from it for a while since we may cause a bouncing
57 * cacheline if we try to acquire the lock. So go onto the next slab.
58 * If all pages are busy then we may allocate a new slab instead of reusing
59 * a partial slab. A new slab has noone operating on it and thus there is
60 * no danger of cacheline contention.
62 * Interrupts are disabled during allocation and deallocation in order to
63 * make the slab allocator safe to use in the context of an irq. In addition
64 * interrupts are disabled to ensure that the processor does not change
65 * while handling per_cpu slabs, due to kernel preemption.
67 * SLUB assigns one slab for allocation to each processor.
68 * Allocations only occur from these slabs called cpu slabs.
70 * Slabs with free elements are kept on a partial list and during regular
71 * operations no list for full slabs is used. If an object in a full slab is
72 * freed then the slab will show up again on the partial lists.
73 * We track full slabs for debugging purposes though because otherwise we
74 * cannot scan all objects.
76 * Slabs are freed when they become empty. Teardown and setup is
77 * minimal so we rely on the page allocators per cpu caches for
78 * fast frees and allocs.
80 * Overloading of page flags that are otherwise used for LRU management.
82 * PageActive The slab is frozen and exempt from list processing.
83 * This means that the slab is dedicated to a purpose
84 * such as satisfying allocations for a specific
85 * processor. Objects may be freed in the slab while
86 * it is frozen but slab_free will then skip the usual
87 * list operations. It is up to the processor holding
88 * the slab to integrate the slab into the slab lists
89 * when the slab is no longer needed.
91 * One use of this flag is to mark slabs that are
92 * used for allocations. Then such a slab becomes a cpu
93 * slab. The cpu slab may be equipped with an additional
94 * freelist that allows lockless access to
95 * free objects in addition to the regular freelist
96 * that requires the slab lock.
98 * PageError Slab requires special handling due to debug
99 * options set. This moves slab handling out of
100 * the fast path and disables lockless freelists.
103 #define FROZEN (1 << PG_active)
105 #ifdef CONFIG_SLUB_DEBUG
106 #define SLABDEBUG (1 << PG_error)
111 static inline int SlabFrozen(struct page *page)
113 return page->flags & FROZEN;
116 static inline void SetSlabFrozen(struct page *page)
118 page->flags |= FROZEN;
121 static inline void ClearSlabFrozen(struct page *page)
123 page->flags &= ~FROZEN;
126 static inline int SlabDebug(struct page *page)
128 return page->flags & SLABDEBUG;
131 static inline void SetSlabDebug(struct page *page)
133 page->flags |= SLABDEBUG;
136 static inline void ClearSlabDebug(struct page *page)
138 page->flags &= ~SLABDEBUG;
142 * Issues still to be resolved:
144 * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
146 * - Variable sizing of the per node arrays
149 /* Enable to test recovery from slab corruption on boot */
150 #undef SLUB_RESILIENCY_TEST
155 * Small page size. Make sure that we do not fragment memory
157 #define DEFAULT_MAX_ORDER 1
158 #define DEFAULT_MIN_OBJECTS 4
163 * Large page machines are customarily able to handle larger
166 #define DEFAULT_MAX_ORDER 2
167 #define DEFAULT_MIN_OBJECTS 8
172 * Mininum number of partial slabs. These will be left on the partial
173 * lists even if they are empty. kmem_cache_shrink may reclaim them.
175 #define MIN_PARTIAL 5
178 * Maximum number of desirable partial slabs.
179 * The existence of more partial slabs makes kmem_cache_shrink
180 * sort the partial list by the number of objects in the.
182 #define MAX_PARTIAL 10
184 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
185 SLAB_POISON | SLAB_STORE_USER)
188 * Set of flags that will prevent slab merging
190 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
191 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
193 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
196 #ifndef ARCH_KMALLOC_MINALIGN
197 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
200 #ifndef ARCH_SLAB_MINALIGN
201 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
204 /* Internal SLUB flags */
205 #define __OBJECT_POISON 0x80000000 /* Poison object */
206 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
208 /* Not all arches define cache_line_size */
209 #ifndef cache_line_size
210 #define cache_line_size() L1_CACHE_BYTES
213 static int kmem_size = sizeof(struct kmem_cache);
216 static struct notifier_block slab_notifier;
220 DOWN, /* No slab functionality available */
221 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
222 UP, /* Everything works but does not show up in sysfs */
226 /* A list of all slab caches on the system */
227 static DECLARE_RWSEM(slub_lock);
228 static LIST_HEAD(slab_caches);
231 * Tracking user of a slab.
234 void *addr; /* Called from address */
235 int cpu; /* Was running on cpu */
236 int pid; /* Pid context */
237 unsigned long when; /* When did the operation occur */
240 enum track_item { TRACK_ALLOC, TRACK_FREE };
242 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
243 static int sysfs_slab_add(struct kmem_cache *);
244 static int sysfs_slab_alias(struct kmem_cache *, const char *);
245 static void sysfs_slab_remove(struct kmem_cache *);
247 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
248 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
250 static inline void sysfs_slab_remove(struct kmem_cache *s)
256 /********************************************************************
257 * Core slab cache functions
258 *******************************************************************/
260 int slab_is_available(void)
262 return slab_state >= UP;
265 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
268 return s->node[node];
270 return &s->local_node;
274 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
277 return s->cpu_slab[cpu];
284 * The end pointer in a slab is special. It points to the first object in the
285 * slab but has bit 0 set to mark it.
287 * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
288 * in the mapping set.
290 static inline int is_end(void *addr)
292 return (unsigned long)addr & PAGE_MAPPING_ANON;
295 void *slab_address(struct page *page)
297 return page->end - PAGE_MAPPING_ANON;
300 static inline int check_valid_pointer(struct kmem_cache *s,
301 struct page *page, const void *object)
305 if (object == page->end)
308 base = slab_address(page);
309 if (object < base || object >= base + s->objects * s->size ||
310 (object - base) % s->size) {
318 * Slow version of get and set free pointer.
320 * This version requires touching the cache lines of kmem_cache which
321 * we avoid to do in the fast alloc free paths. There we obtain the offset
322 * from the page struct.
324 static inline void *get_freepointer(struct kmem_cache *s, void *object)
326 return *(void **)(object + s->offset);
329 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
331 *(void **)(object + s->offset) = fp;
334 /* Loop over all objects in a slab */
335 #define for_each_object(__p, __s, __addr) \
336 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
340 #define for_each_free_object(__p, __s, __free) \
341 for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
344 /* Determine object index from a given position */
345 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
347 return (p - addr) / s->size;
350 #ifdef CONFIG_SLUB_DEBUG
354 #ifdef CONFIG_SLUB_DEBUG_ON
355 static int slub_debug = DEBUG_DEFAULT_FLAGS;
357 static int slub_debug;
360 static char *slub_debug_slabs;
365 static void print_section(char *text, u8 *addr, unsigned int length)
373 for (i = 0; i < length; i++) {
375 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
378 printk(KERN_CONT " %02x", addr[i]);
380 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
382 printk(KERN_CONT " %s\n", ascii);
389 printk(KERN_CONT " ");
393 printk(KERN_CONT " %s\n", ascii);
397 static struct track *get_track(struct kmem_cache *s, void *object,
398 enum track_item alloc)
403 p = object + s->offset + sizeof(void *);
405 p = object + s->inuse;
410 static void set_track(struct kmem_cache *s, void *object,
411 enum track_item alloc, void *addr)
416 p = object + s->offset + sizeof(void *);
418 p = object + s->inuse;
423 p->cpu = smp_processor_id();
424 p->pid = current ? current->pid : -1;
427 memset(p, 0, sizeof(struct track));
430 static void init_tracking(struct kmem_cache *s, void *object)
432 if (!(s->flags & SLAB_STORE_USER))
435 set_track(s, object, TRACK_FREE, NULL);
436 set_track(s, object, TRACK_ALLOC, NULL);
439 static void print_track(const char *s, struct track *t)
444 printk(KERN_ERR "INFO: %s in ", s);
445 __print_symbol("%s", (unsigned long)t->addr);
446 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
449 static void print_tracking(struct kmem_cache *s, void *object)
451 if (!(s->flags & SLAB_STORE_USER))
454 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
455 print_track("Freed", get_track(s, object, TRACK_FREE));
458 static void print_page_info(struct page *page)
460 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
461 page, page->inuse, page->freelist, page->flags);
465 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
471 vsnprintf(buf, sizeof(buf), fmt, args);
473 printk(KERN_ERR "========================================"
474 "=====================================\n");
475 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
476 printk(KERN_ERR "----------------------------------------"
477 "-------------------------------------\n\n");
480 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
486 vsnprintf(buf, sizeof(buf), fmt, args);
488 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
491 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
493 unsigned int off; /* Offset of last byte */
494 u8 *addr = slab_address(page);
496 print_tracking(s, p);
498 print_page_info(page);
500 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
501 p, p - addr, get_freepointer(s, p));
504 print_section("Bytes b4", p - 16, 16);
506 print_section("Object", p, min(s->objsize, 128));
508 if (s->flags & SLAB_RED_ZONE)
509 print_section("Redzone", p + s->objsize,
510 s->inuse - s->objsize);
513 off = s->offset + sizeof(void *);
517 if (s->flags & SLAB_STORE_USER)
518 off += 2 * sizeof(struct track);
521 /* Beginning of the filler is the free pointer */
522 print_section("Padding", p + off, s->size - off);
527 static void object_err(struct kmem_cache *s, struct page *page,
528 u8 *object, char *reason)
531 print_trailer(s, page, object);
534 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
540 vsnprintf(buf, sizeof(buf), fmt, args);
543 print_page_info(page);
547 static void init_object(struct kmem_cache *s, void *object, int active)
551 if (s->flags & __OBJECT_POISON) {
552 memset(p, POISON_FREE, s->objsize - 1);
553 p[s->objsize - 1] = POISON_END;
556 if (s->flags & SLAB_RED_ZONE)
557 memset(p + s->objsize,
558 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
559 s->inuse - s->objsize);
562 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
565 if (*start != (u8)value)
573 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
574 void *from, void *to)
576 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
577 memset(from, data, to - from);
580 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
581 u8 *object, char *what,
582 u8 *start, unsigned int value, unsigned int bytes)
587 fault = check_bytes(start, value, bytes);
592 while (end > fault && end[-1] == value)
595 slab_bug(s, "%s overwritten", what);
596 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
597 fault, end - 1, fault[0], value);
598 print_trailer(s, page, object);
600 restore_bytes(s, what, value, fault, end);
608 * Bytes of the object to be managed.
609 * If the freepointer may overlay the object then the free
610 * pointer is the first word of the object.
612 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
615 * object + s->objsize
616 * Padding to reach word boundary. This is also used for Redzoning.
617 * Padding is extended by another word if Redzoning is enabled and
620 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
621 * 0xcc (RED_ACTIVE) for objects in use.
624 * Meta data starts here.
626 * A. Free pointer (if we cannot overwrite object on free)
627 * B. Tracking data for SLAB_STORE_USER
628 * C. Padding to reach required alignment boundary or at mininum
629 * one word if debuggin is on to be able to detect writes
630 * before the word boundary.
632 * Padding is done using 0x5a (POISON_INUSE)
635 * Nothing is used beyond s->size.
637 * If slabcaches are merged then the objsize and inuse boundaries are mostly
638 * ignored. And therefore no slab options that rely on these boundaries
639 * may be used with merged slabcaches.
642 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
644 unsigned long off = s->inuse; /* The end of info */
647 /* Freepointer is placed after the object. */
648 off += sizeof(void *);
650 if (s->flags & SLAB_STORE_USER)
651 /* We also have user information there */
652 off += 2 * sizeof(struct track);
657 return check_bytes_and_report(s, page, p, "Object padding",
658 p + off, POISON_INUSE, s->size - off);
661 static int slab_pad_check(struct kmem_cache *s, struct page *page)
669 if (!(s->flags & SLAB_POISON))
672 start = slab_address(page);
673 end = start + (PAGE_SIZE << s->order);
674 length = s->objects * s->size;
675 remainder = end - (start + length);
679 fault = check_bytes(start + length, POISON_INUSE, remainder);
682 while (end > fault && end[-1] == POISON_INUSE)
685 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
686 print_section("Padding", start, length);
688 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
692 static int check_object(struct kmem_cache *s, struct page *page,
693 void *object, int active)
696 u8 *endobject = object + s->objsize;
698 if (s->flags & SLAB_RED_ZONE) {
700 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
702 if (!check_bytes_and_report(s, page, object, "Redzone",
703 endobject, red, s->inuse - s->objsize))
706 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
707 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
708 POISON_INUSE, s->inuse - s->objsize);
711 if (s->flags & SLAB_POISON) {
712 if (!active && (s->flags & __OBJECT_POISON) &&
713 (!check_bytes_and_report(s, page, p, "Poison", p,
714 POISON_FREE, s->objsize - 1) ||
715 !check_bytes_and_report(s, page, p, "Poison",
716 p + s->objsize - 1, POISON_END, 1)))
719 * check_pad_bytes cleans up on its own.
721 check_pad_bytes(s, page, p);
724 if (!s->offset && active)
726 * Object and freepointer overlap. Cannot check
727 * freepointer while object is allocated.
731 /* Check free pointer validity */
732 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
733 object_err(s, page, p, "Freepointer corrupt");
735 * No choice but to zap it and thus loose the remainder
736 * of the free objects in this slab. May cause
737 * another error because the object count is now wrong.
739 set_freepointer(s, p, page->end);
745 static int check_slab(struct kmem_cache *s, struct page *page)
747 VM_BUG_ON(!irqs_disabled());
749 if (!PageSlab(page)) {
750 slab_err(s, page, "Not a valid slab page");
753 if (page->inuse > s->objects) {
754 slab_err(s, page, "inuse %u > max %u",
755 s->name, page->inuse, s->objects);
758 /* Slab_pad_check fixes things up after itself */
759 slab_pad_check(s, page);
764 * Determine if a certain object on a page is on the freelist. Must hold the
765 * slab lock to guarantee that the chains are in a consistent state.
767 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
770 void *fp = page->freelist;
773 while (fp != page->end && nr <= s->objects) {
776 if (!check_valid_pointer(s, page, fp)) {
778 object_err(s, page, object,
779 "Freechain corrupt");
780 set_freepointer(s, object, page->end);
783 slab_err(s, page, "Freepointer corrupt");
784 page->freelist = page->end;
785 page->inuse = s->objects;
786 slab_fix(s, "Freelist cleared");
792 fp = get_freepointer(s, object);
796 if (page->inuse != s->objects - nr) {
797 slab_err(s, page, "Wrong object count. Counter is %d but "
798 "counted were %d", page->inuse, s->objects - nr);
799 page->inuse = s->objects - nr;
800 slab_fix(s, "Object count adjusted.");
802 return search == NULL;
805 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
807 if (s->flags & SLAB_TRACE) {
808 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
810 alloc ? "alloc" : "free",
815 print_section("Object", (void *)object, s->objsize);
822 * Tracking of fully allocated slabs for debugging purposes.
824 static void add_full(struct kmem_cache_node *n, struct page *page)
826 spin_lock(&n->list_lock);
827 list_add(&page->lru, &n->full);
828 spin_unlock(&n->list_lock);
831 static void remove_full(struct kmem_cache *s, struct page *page)
833 struct kmem_cache_node *n;
835 if (!(s->flags & SLAB_STORE_USER))
838 n = get_node(s, page_to_nid(page));
840 spin_lock(&n->list_lock);
841 list_del(&page->lru);
842 spin_unlock(&n->list_lock);
845 static void setup_object_debug(struct kmem_cache *s, struct page *page,
848 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
851 init_object(s, object, 0);
852 init_tracking(s, object);
855 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
856 void *object, void *addr)
858 if (!check_slab(s, page))
861 if (object && !on_freelist(s, page, object)) {
862 object_err(s, page, object, "Object already allocated");
866 if (!check_valid_pointer(s, page, object)) {
867 object_err(s, page, object, "Freelist Pointer check fails");
871 if (object && !check_object(s, page, object, 0))
874 /* Success perform special debug activities for allocs */
875 if (s->flags & SLAB_STORE_USER)
876 set_track(s, object, TRACK_ALLOC, addr);
877 trace(s, page, object, 1);
878 init_object(s, object, 1);
882 if (PageSlab(page)) {
884 * If this is a slab page then lets do the best we can
885 * to avoid issues in the future. Marking all objects
886 * as used avoids touching the remaining objects.
888 slab_fix(s, "Marking all objects used");
889 page->inuse = s->objects;
890 page->freelist = page->end;
895 static int free_debug_processing(struct kmem_cache *s, struct page *page,
896 void *object, void *addr)
898 if (!check_slab(s, page))
901 if (!check_valid_pointer(s, page, object)) {
902 slab_err(s, page, "Invalid object pointer 0x%p", object);
906 if (on_freelist(s, page, object)) {
907 object_err(s, page, object, "Object already free");
911 if (!check_object(s, page, object, 1))
914 if (unlikely(s != page->slab)) {
916 slab_err(s, page, "Attempt to free object(0x%p) "
917 "outside of slab", object);
921 "SLUB <none>: no slab for object 0x%p.\n",
925 object_err(s, page, object,
926 "page slab pointer corrupt.");
930 /* Special debug activities for freeing objects */
931 if (!SlabFrozen(page) && page->freelist == page->end)
932 remove_full(s, page);
933 if (s->flags & SLAB_STORE_USER)
934 set_track(s, object, TRACK_FREE, addr);
935 trace(s, page, object, 0);
936 init_object(s, object, 0);
940 slab_fix(s, "Object at 0x%p not freed", object);
944 static int __init setup_slub_debug(char *str)
946 slub_debug = DEBUG_DEFAULT_FLAGS;
947 if (*str++ != '=' || !*str)
949 * No options specified. Switch on full debugging.
955 * No options but restriction on slabs. This means full
956 * debugging for slabs matching a pattern.
963 * Switch off all debugging measures.
968 * Determine which debug features should be switched on
970 for (; *str && *str != ','; str++) {
971 switch (tolower(*str)) {
973 slub_debug |= SLAB_DEBUG_FREE;
976 slub_debug |= SLAB_RED_ZONE;
979 slub_debug |= SLAB_POISON;
982 slub_debug |= SLAB_STORE_USER;
985 slub_debug |= SLAB_TRACE;
988 printk(KERN_ERR "slub_debug option '%c' "
989 "unknown. skipped\n", *str);
995 slub_debug_slabs = str + 1;
1000 __setup("slub_debug", setup_slub_debug);
1002 static unsigned long kmem_cache_flags(unsigned long objsize,
1003 unsigned long flags, const char *name,
1004 void (*ctor)(struct kmem_cache *, void *))
1007 * The page->offset field is only 16 bit wide. This is an offset
1008 * in units of words from the beginning of an object. If the slab
1009 * size is bigger then we cannot move the free pointer behind the
1012 * On 32 bit platforms the limit is 256k. On 64bit platforms
1013 * the limit is 512k.
1015 * Debugging or ctor may create a need to move the free
1016 * pointer. Fail if this happens.
1018 if (objsize >= 65535 * sizeof(void *)) {
1019 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1020 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1024 * Enable debugging if selected on the kernel commandline.
1026 if (slub_debug && (!slub_debug_slabs ||
1027 strncmp(slub_debug_slabs, name,
1028 strlen(slub_debug_slabs)) == 0))
1029 flags |= slub_debug;
1035 static inline void setup_object_debug(struct kmem_cache *s,
1036 struct page *page, void *object) {}
1038 static inline int alloc_debug_processing(struct kmem_cache *s,
1039 struct page *page, void *object, void *addr) { return 0; }
1041 static inline int free_debug_processing(struct kmem_cache *s,
1042 struct page *page, void *object, void *addr) { return 0; }
1044 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1046 static inline int check_object(struct kmem_cache *s, struct page *page,
1047 void *object, int active) { return 1; }
1048 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1049 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1050 unsigned long flags, const char *name,
1051 void (*ctor)(struct kmem_cache *, void *))
1055 #define slub_debug 0
1058 * Slab allocation and freeing
1060 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1063 int pages = 1 << s->order;
1066 flags |= __GFP_COMP;
1068 if (s->flags & SLAB_CACHE_DMA)
1071 if (s->flags & SLAB_RECLAIM_ACCOUNT)
1072 flags |= __GFP_RECLAIMABLE;
1075 page = alloc_pages(flags, s->order);
1077 page = alloc_pages_node(node, flags, s->order);
1082 mod_zone_page_state(page_zone(page),
1083 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1084 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1090 static void setup_object(struct kmem_cache *s, struct page *page,
1093 setup_object_debug(s, page, object);
1094 if (unlikely(s->ctor))
1098 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1101 struct kmem_cache_node *n;
1106 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1108 page = allocate_slab(s,
1109 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1113 n = get_node(s, page_to_nid(page));
1115 atomic_long_inc(&n->nr_slabs);
1117 page->flags |= 1 << PG_slab;
1118 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1119 SLAB_STORE_USER | SLAB_TRACE))
1122 start = page_address(page);
1123 page->end = start + 1;
1125 if (unlikely(s->flags & SLAB_POISON))
1126 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1129 for_each_object(p, s, start) {
1130 setup_object(s, page, last);
1131 set_freepointer(s, last, p);
1134 setup_object(s, page, last);
1135 set_freepointer(s, last, page->end);
1137 page->freelist = start;
1143 static void __free_slab(struct kmem_cache *s, struct page *page)
1145 int pages = 1 << s->order;
1147 if (unlikely(SlabDebug(page))) {
1150 slab_pad_check(s, page);
1151 for_each_object(p, s, slab_address(page))
1152 check_object(s, page, p, 0);
1153 ClearSlabDebug(page);
1156 mod_zone_page_state(page_zone(page),
1157 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1158 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1161 page->mapping = NULL;
1162 __free_pages(page, s->order);
1165 static void rcu_free_slab(struct rcu_head *h)
1169 page = container_of((struct list_head *)h, struct page, lru);
1170 __free_slab(page->slab, page);
1173 static void free_slab(struct kmem_cache *s, struct page *page)
1175 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1177 * RCU free overloads the RCU head over the LRU
1179 struct rcu_head *head = (void *)&page->lru;
1181 call_rcu(head, rcu_free_slab);
1183 __free_slab(s, page);
1186 static void discard_slab(struct kmem_cache *s, struct page *page)
1188 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1190 atomic_long_dec(&n->nr_slabs);
1191 reset_page_mapcount(page);
1192 __ClearPageSlab(page);
1197 * Per slab locking using the pagelock
1199 static __always_inline void slab_lock(struct page *page)
1201 bit_spin_lock(PG_locked, &page->flags);
1204 static __always_inline void slab_unlock(struct page *page)
1206 bit_spin_unlock(PG_locked, &page->flags);
1209 static __always_inline int slab_trylock(struct page *page)
1213 rc = bit_spin_trylock(PG_locked, &page->flags);
1218 * Management of partially allocated slabs
1220 static void add_partial(struct kmem_cache_node *n,
1221 struct page *page, int tail)
1223 spin_lock(&n->list_lock);
1226 list_add_tail(&page->lru, &n->partial);
1228 list_add(&page->lru, &n->partial);
1229 spin_unlock(&n->list_lock);
1232 static void remove_partial(struct kmem_cache *s,
1235 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1237 spin_lock(&n->list_lock);
1238 list_del(&page->lru);
1240 spin_unlock(&n->list_lock);
1244 * Lock slab and remove from the partial list.
1246 * Must hold list_lock.
1248 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1250 if (slab_trylock(page)) {
1251 list_del(&page->lru);
1253 SetSlabFrozen(page);
1260 * Try to allocate a partial slab from a specific node.
1262 static struct page *get_partial_node(struct kmem_cache_node *n)
1267 * Racy check. If we mistakenly see no partial slabs then we
1268 * just allocate an empty slab. If we mistakenly try to get a
1269 * partial slab and there is none available then get_partials()
1272 if (!n || !n->nr_partial)
1275 spin_lock(&n->list_lock);
1276 list_for_each_entry(page, &n->partial, lru)
1277 if (lock_and_freeze_slab(n, page))
1281 spin_unlock(&n->list_lock);
1286 * Get a page from somewhere. Search in increasing NUMA distances.
1288 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1291 struct zonelist *zonelist;
1296 * The defrag ratio allows a configuration of the tradeoffs between
1297 * inter node defragmentation and node local allocations. A lower
1298 * defrag_ratio increases the tendency to do local allocations
1299 * instead of attempting to obtain partial slabs from other nodes.
1301 * If the defrag_ratio is set to 0 then kmalloc() always
1302 * returns node local objects. If the ratio is higher then kmalloc()
1303 * may return off node objects because partial slabs are obtained
1304 * from other nodes and filled up.
1306 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1307 * defrag_ratio = 1000) then every (well almost) allocation will
1308 * first attempt to defrag slab caches on other nodes. This means
1309 * scanning over all nodes to look for partial slabs which may be
1310 * expensive if we do it every time we are trying to find a slab
1311 * with available objects.
1313 if (!s->remote_node_defrag_ratio ||
1314 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1317 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1318 ->node_zonelists[gfp_zone(flags)];
1319 for (z = zonelist->zones; *z; z++) {
1320 struct kmem_cache_node *n;
1322 n = get_node(s, zone_to_nid(*z));
1324 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1325 n->nr_partial > MIN_PARTIAL) {
1326 page = get_partial_node(n);
1336 * Get a partial page, lock it and return it.
1338 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1341 int searchnode = (node == -1) ? numa_node_id() : node;
1343 page = get_partial_node(get_node(s, searchnode));
1344 if (page || (flags & __GFP_THISNODE))
1347 return get_any_partial(s, flags);
1351 * Move a page back to the lists.
1353 * Must be called with the slab lock held.
1355 * On exit the slab lock will have been dropped.
1357 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1359 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1361 ClearSlabFrozen(page);
1364 if (page->freelist != page->end)
1365 add_partial(n, page, tail);
1366 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1371 if (n->nr_partial < MIN_PARTIAL) {
1373 * Adding an empty slab to the partial slabs in order
1374 * to avoid page allocator overhead. This slab needs
1375 * to come after the other slabs with objects in
1376 * order to fill them up. That way the size of the
1377 * partial list stays small. kmem_cache_shrink can
1378 * reclaim empty slabs from the partial list.
1380 add_partial(n, page, 1);
1384 discard_slab(s, page);
1390 * Remove the cpu slab
1392 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1394 struct page *page = c->page;
1397 * Merge cpu freelist into freelist. Typically we get here
1398 * because both freelists are empty. So this is unlikely
1401 * We need to use _is_end here because deactivate slab may
1402 * be called for a debug slab. Then c->freelist may contain
1405 while (unlikely(!is_end(c->freelist))) {
1408 tail = 0; /* Hot objects. Put the slab first */
1410 /* Retrieve object from cpu_freelist */
1411 object = c->freelist;
1412 c->freelist = c->freelist[c->offset];
1414 /* And put onto the regular freelist */
1415 object[c->offset] = page->freelist;
1416 page->freelist = object;
1420 unfreeze_slab(s, page, tail);
1423 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1426 deactivate_slab(s, c);
1431 * Called from IPI handler with interrupts disabled.
1433 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1435 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1437 if (likely(c && c->page))
1441 static void flush_cpu_slab(void *d)
1443 struct kmem_cache *s = d;
1445 __flush_cpu_slab(s, smp_processor_id());
1448 static void flush_all(struct kmem_cache *s)
1451 on_each_cpu(flush_cpu_slab, s, 1, 1);
1453 unsigned long flags;
1455 local_irq_save(flags);
1457 local_irq_restore(flags);
1462 * Check if the objects in a per cpu structure fit numa
1463 * locality expectations.
1465 static inline int node_match(struct kmem_cache_cpu *c, int node)
1468 if (node != -1 && c->node != node)
1475 * Slow path. The lockless freelist is empty or we need to perform
1478 * Interrupts are disabled.
1480 * Processing is still very fast if new objects have been freed to the
1481 * regular freelist. In that case we simply take over the regular freelist
1482 * as the lockless freelist and zap the regular freelist.
1484 * If that is not working then we fall back to the partial lists. We take the
1485 * first element of the freelist as the object to allocate now and move the
1486 * rest of the freelist to the lockless freelist.
1488 * And if we were unable to get a new slab from the partial slab lists then
1489 * we need to allocate a new slab. This is slowest path since we may sleep.
1491 static void *__slab_alloc(struct kmem_cache *s,
1492 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
1501 if (unlikely(!node_match(c, node)))
1504 object = c->page->freelist;
1505 if (unlikely(object == c->page->end))
1507 if (unlikely(SlabDebug(c->page)))
1510 object = c->page->freelist;
1511 c->freelist = object[c->offset];
1512 c->page->inuse = s->objects;
1513 c->page->freelist = c->page->end;
1514 c->node = page_to_nid(c->page);
1515 slab_unlock(c->page);
1519 deactivate_slab(s, c);
1522 new = get_partial(s, gfpflags, node);
1528 if (gfpflags & __GFP_WAIT)
1531 new = new_slab(s, gfpflags, node);
1533 if (gfpflags & __GFP_WAIT)
1534 local_irq_disable();
1537 c = get_cpu_slab(s, smp_processor_id());
1547 object = c->page->freelist;
1548 if (!alloc_debug_processing(s, c->page, object, addr))
1552 c->page->freelist = object[c->offset];
1554 slab_unlock(c->page);
1559 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1560 * have the fastpath folded into their functions. So no function call
1561 * overhead for requests that can be satisfied on the fastpath.
1563 * The fastpath works by first checking if the lockless freelist can be used.
1564 * If not then __slab_alloc is called for slow processing.
1566 * Otherwise we can simply pick the next object from the lockless free list.
1568 static __always_inline void *slab_alloc(struct kmem_cache *s,
1569 gfp_t gfpflags, int node, void *addr)
1572 unsigned long flags;
1573 struct kmem_cache_cpu *c;
1575 local_irq_save(flags);
1576 c = get_cpu_slab(s, smp_processor_id());
1577 if (unlikely(is_end(c->freelist) || !node_match(c, node)))
1579 object = __slab_alloc(s, gfpflags, node, addr, c);
1582 object = c->freelist;
1583 c->freelist = object[c->offset];
1585 local_irq_restore(flags);
1587 if (unlikely((gfpflags & __GFP_ZERO) && object))
1588 memset(object, 0, c->objsize);
1593 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1595 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1597 EXPORT_SYMBOL(kmem_cache_alloc);
1600 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1602 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1604 EXPORT_SYMBOL(kmem_cache_alloc_node);
1608 * Slow patch handling. This may still be called frequently since objects
1609 * have a longer lifetime than the cpu slabs in most processing loads.
1611 * So we still attempt to reduce cache line usage. Just take the slab
1612 * lock and free the item. If there is no additional partial page
1613 * handling required then we can return immediately.
1615 static void __slab_free(struct kmem_cache *s, struct page *page,
1616 void *x, void *addr, unsigned int offset)
1619 void **object = (void *)x;
1623 if (unlikely(SlabDebug(page)))
1626 prior = object[offset] = page->freelist;
1627 page->freelist = object;
1630 if (unlikely(SlabFrozen(page)))
1633 if (unlikely(!page->inuse))
1637 * Objects left in the slab. If it
1638 * was not on the partial list before
1641 if (unlikely(prior == page->end))
1642 add_partial(get_node(s, page_to_nid(page)), page, 1);
1649 if (prior != page->end)
1651 * Slab still on the partial list.
1653 remove_partial(s, page);
1656 discard_slab(s, page);
1660 if (!free_debug_processing(s, page, x, addr))
1666 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1667 * can perform fastpath freeing without additional function calls.
1669 * The fastpath is only possible if we are freeing to the current cpu slab
1670 * of this processor. This typically the case if we have just allocated
1673 * If fastpath is not possible then fall back to __slab_free where we deal
1674 * with all sorts of special processing.
1676 static __always_inline void slab_free(struct kmem_cache *s,
1677 struct page *page, void *x, void *addr)
1679 void **object = (void *)x;
1680 unsigned long flags;
1681 struct kmem_cache_cpu *c;
1683 local_irq_save(flags);
1684 debug_check_no_locks_freed(object, s->objsize);
1685 c = get_cpu_slab(s, smp_processor_id());
1686 if (likely(page == c->page && c->node >= 0)) {
1687 object[c->offset] = c->freelist;
1688 c->freelist = object;
1690 __slab_free(s, page, x, addr, c->offset);
1692 local_irq_restore(flags);
1695 void kmem_cache_free(struct kmem_cache *s, void *x)
1699 page = virt_to_head_page(x);
1701 slab_free(s, page, x, __builtin_return_address(0));
1703 EXPORT_SYMBOL(kmem_cache_free);
1705 /* Figure out on which slab object the object resides */
1706 static struct page *get_object_page(const void *x)
1708 struct page *page = virt_to_head_page(x);
1710 if (!PageSlab(page))
1717 * Object placement in a slab is made very easy because we always start at
1718 * offset 0. If we tune the size of the object to the alignment then we can
1719 * get the required alignment by putting one properly sized object after
1722 * Notice that the allocation order determines the sizes of the per cpu
1723 * caches. Each processor has always one slab available for allocations.
1724 * Increasing the allocation order reduces the number of times that slabs
1725 * must be moved on and off the partial lists and is therefore a factor in
1730 * Mininum / Maximum order of slab pages. This influences locking overhead
1731 * and slab fragmentation. A higher order reduces the number of partial slabs
1732 * and increases the number of allocations possible without having to
1733 * take the list_lock.
1735 static int slub_min_order;
1736 static int slub_max_order = DEFAULT_MAX_ORDER;
1737 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1740 * Merge control. If this is set then no merging of slab caches will occur.
1741 * (Could be removed. This was introduced to pacify the merge skeptics.)
1743 static int slub_nomerge;
1746 * Calculate the order of allocation given an slab object size.
1748 * The order of allocation has significant impact on performance and other
1749 * system components. Generally order 0 allocations should be preferred since
1750 * order 0 does not cause fragmentation in the page allocator. Larger objects
1751 * be problematic to put into order 0 slabs because there may be too much
1752 * unused space left. We go to a higher order if more than 1/8th of the slab
1755 * In order to reach satisfactory performance we must ensure that a minimum
1756 * number of objects is in one slab. Otherwise we may generate too much
1757 * activity on the partial lists which requires taking the list_lock. This is
1758 * less a concern for large slabs though which are rarely used.
1760 * slub_max_order specifies the order where we begin to stop considering the
1761 * number of objects in a slab as critical. If we reach slub_max_order then
1762 * we try to keep the page order as low as possible. So we accept more waste
1763 * of space in favor of a small page order.
1765 * Higher order allocations also allow the placement of more objects in a
1766 * slab and thereby reduce object handling overhead. If the user has
1767 * requested a higher mininum order then we start with that one instead of
1768 * the smallest order which will fit the object.
1770 static inline int slab_order(int size, int min_objects,
1771 int max_order, int fract_leftover)
1775 int min_order = slub_min_order;
1777 for (order = max(min_order,
1778 fls(min_objects * size - 1) - PAGE_SHIFT);
1779 order <= max_order; order++) {
1781 unsigned long slab_size = PAGE_SIZE << order;
1783 if (slab_size < min_objects * size)
1786 rem = slab_size % size;
1788 if (rem <= slab_size / fract_leftover)
1796 static inline int calculate_order(int size)
1803 * Attempt to find best configuration for a slab. This
1804 * works by first attempting to generate a layout with
1805 * the best configuration and backing off gradually.
1807 * First we reduce the acceptable waste in a slab. Then
1808 * we reduce the minimum objects required in a slab.
1810 min_objects = slub_min_objects;
1811 while (min_objects > 1) {
1813 while (fraction >= 4) {
1814 order = slab_order(size, min_objects,
1815 slub_max_order, fraction);
1816 if (order <= slub_max_order)
1824 * We were unable to place multiple objects in a slab. Now
1825 * lets see if we can place a single object there.
1827 order = slab_order(size, 1, slub_max_order, 1);
1828 if (order <= slub_max_order)
1832 * Doh this slab cannot be placed using slub_max_order.
1834 order = slab_order(size, 1, MAX_ORDER, 1);
1835 if (order <= MAX_ORDER)
1841 * Figure out what the alignment of the objects will be.
1843 static unsigned long calculate_alignment(unsigned long flags,
1844 unsigned long align, unsigned long size)
1847 * If the user wants hardware cache aligned objects then
1848 * follow that suggestion if the object is sufficiently
1851 * The hardware cache alignment cannot override the
1852 * specified alignment though. If that is greater
1855 if ((flags & SLAB_HWCACHE_ALIGN) &&
1856 size > cache_line_size() / 2)
1857 return max_t(unsigned long, align, cache_line_size());
1859 if (align < ARCH_SLAB_MINALIGN)
1860 return ARCH_SLAB_MINALIGN;
1862 return ALIGN(align, sizeof(void *));
1865 static void init_kmem_cache_cpu(struct kmem_cache *s,
1866 struct kmem_cache_cpu *c)
1869 c->freelist = (void *)PAGE_MAPPING_ANON;
1871 c->offset = s->offset / sizeof(void *);
1872 c->objsize = s->objsize;
1875 static void init_kmem_cache_node(struct kmem_cache_node *n)
1878 atomic_long_set(&n->nr_slabs, 0);
1879 spin_lock_init(&n->list_lock);
1880 INIT_LIST_HEAD(&n->partial);
1881 #ifdef CONFIG_SLUB_DEBUG
1882 INIT_LIST_HEAD(&n->full);
1888 * Per cpu array for per cpu structures.
1890 * The per cpu array places all kmem_cache_cpu structures from one processor
1891 * close together meaning that it becomes possible that multiple per cpu
1892 * structures are contained in one cacheline. This may be particularly
1893 * beneficial for the kmalloc caches.
1895 * A desktop system typically has around 60-80 slabs. With 100 here we are
1896 * likely able to get per cpu structures for all caches from the array defined
1897 * here. We must be able to cover all kmalloc caches during bootstrap.
1899 * If the per cpu array is exhausted then fall back to kmalloc
1900 * of individual cachelines. No sharing is possible then.
1902 #define NR_KMEM_CACHE_CPU 100
1904 static DEFINE_PER_CPU(struct kmem_cache_cpu,
1905 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1907 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1908 static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1910 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1911 int cpu, gfp_t flags)
1913 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1916 per_cpu(kmem_cache_cpu_free, cpu) =
1917 (void *)c->freelist;
1919 /* Table overflow: So allocate ourselves */
1921 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
1922 flags, cpu_to_node(cpu));
1927 init_kmem_cache_cpu(s, c);
1931 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
1933 if (c < per_cpu(kmem_cache_cpu, cpu) ||
1934 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
1938 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
1939 per_cpu(kmem_cache_cpu_free, cpu) = c;
1942 static void free_kmem_cache_cpus(struct kmem_cache *s)
1946 for_each_online_cpu(cpu) {
1947 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1950 s->cpu_slab[cpu] = NULL;
1951 free_kmem_cache_cpu(c, cpu);
1956 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
1960 for_each_online_cpu(cpu) {
1961 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1966 c = alloc_kmem_cache_cpu(s, cpu, flags);
1968 free_kmem_cache_cpus(s);
1971 s->cpu_slab[cpu] = c;
1977 * Initialize the per cpu array.
1979 static void init_alloc_cpu_cpu(int cpu)
1983 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
1986 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
1987 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
1989 cpu_set(cpu, kmem_cach_cpu_free_init_once);
1992 static void __init init_alloc_cpu(void)
1996 for_each_online_cpu(cpu)
1997 init_alloc_cpu_cpu(cpu);
2001 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2002 static inline void init_alloc_cpu(void) {}
2004 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2006 init_kmem_cache_cpu(s, &s->cpu_slab);
2013 * No kmalloc_node yet so do it by hand. We know that this is the first
2014 * slab on the node for this slabcache. There are no concurrent accesses
2017 * Note that this function only works on the kmalloc_node_cache
2018 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2019 * memory on a fresh node that has no slab structures yet.
2021 static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2025 struct kmem_cache_node *n;
2026 unsigned long flags;
2028 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2030 page = new_slab(kmalloc_caches, gfpflags, node);
2033 if (page_to_nid(page) != node) {
2034 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2036 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2037 "in order to be able to continue\n");
2042 page->freelist = get_freepointer(kmalloc_caches, n);
2044 kmalloc_caches->node[node] = n;
2045 #ifdef CONFIG_SLUB_DEBUG
2046 init_object(kmalloc_caches, n, 1);
2047 init_tracking(kmalloc_caches, n);
2049 init_kmem_cache_node(n);
2050 atomic_long_inc(&n->nr_slabs);
2052 * lockdep requires consistent irq usage for each lock
2053 * so even though there cannot be a race this early in
2054 * the boot sequence, we still disable irqs.
2056 local_irq_save(flags);
2057 add_partial(n, page, 0);
2058 local_irq_restore(flags);
2062 static void free_kmem_cache_nodes(struct kmem_cache *s)
2066 for_each_node_state(node, N_NORMAL_MEMORY) {
2067 struct kmem_cache_node *n = s->node[node];
2068 if (n && n != &s->local_node)
2069 kmem_cache_free(kmalloc_caches, n);
2070 s->node[node] = NULL;
2074 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2079 if (slab_state >= UP)
2080 local_node = page_to_nid(virt_to_page(s));
2084 for_each_node_state(node, N_NORMAL_MEMORY) {
2085 struct kmem_cache_node *n;
2087 if (local_node == node)
2090 if (slab_state == DOWN) {
2091 n = early_kmem_cache_node_alloc(gfpflags,
2095 n = kmem_cache_alloc_node(kmalloc_caches,
2099 free_kmem_cache_nodes(s);
2105 init_kmem_cache_node(n);
2110 static void free_kmem_cache_nodes(struct kmem_cache *s)
2114 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2116 init_kmem_cache_node(&s->local_node);
2122 * calculate_sizes() determines the order and the distribution of data within
2125 static int calculate_sizes(struct kmem_cache *s)
2127 unsigned long flags = s->flags;
2128 unsigned long size = s->objsize;
2129 unsigned long align = s->align;
2132 * Determine if we can poison the object itself. If the user of
2133 * the slab may touch the object after free or before allocation
2134 * then we should never poison the object itself.
2136 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2138 s->flags |= __OBJECT_POISON;
2140 s->flags &= ~__OBJECT_POISON;
2143 * Round up object size to the next word boundary. We can only
2144 * place the free pointer at word boundaries and this determines
2145 * the possible location of the free pointer.
2147 size = ALIGN(size, sizeof(void *));
2149 #ifdef CONFIG_SLUB_DEBUG
2151 * If we are Redzoning then check if there is some space between the
2152 * end of the object and the free pointer. If not then add an
2153 * additional word to have some bytes to store Redzone information.
2155 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2156 size += sizeof(void *);
2160 * With that we have determined the number of bytes in actual use
2161 * by the object. This is the potential offset to the free pointer.
2165 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2168 * Relocate free pointer after the object if it is not
2169 * permitted to overwrite the first word of the object on
2172 * This is the case if we do RCU, have a constructor or
2173 * destructor or are poisoning the objects.
2176 size += sizeof(void *);
2179 #ifdef CONFIG_SLUB_DEBUG
2180 if (flags & SLAB_STORE_USER)
2182 * Need to store information about allocs and frees after
2185 size += 2 * sizeof(struct track);
2187 if (flags & SLAB_RED_ZONE)
2189 * Add some empty padding so that we can catch
2190 * overwrites from earlier objects rather than let
2191 * tracking information or the free pointer be
2192 * corrupted if an user writes before the start
2195 size += sizeof(void *);
2199 * Determine the alignment based on various parameters that the
2200 * user specified and the dynamic determination of cache line size
2203 align = calculate_alignment(flags, align, s->objsize);
2206 * SLUB stores one object immediately after another beginning from
2207 * offset 0. In order to align the objects we have to simply size
2208 * each object to conform to the alignment.
2210 size = ALIGN(size, align);
2213 s->order = calculate_order(size);
2218 * Determine the number of objects per slab
2220 s->objects = (PAGE_SIZE << s->order) / size;
2222 return !!s->objects;
2226 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2227 const char *name, size_t size,
2228 size_t align, unsigned long flags,
2229 void (*ctor)(struct kmem_cache *, void *))
2231 memset(s, 0, kmem_size);
2236 s->flags = kmem_cache_flags(size, flags, name, ctor);
2238 if (!calculate_sizes(s))
2243 s->remote_node_defrag_ratio = 100;
2245 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2248 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2250 free_kmem_cache_nodes(s);
2252 if (flags & SLAB_PANIC)
2253 panic("Cannot create slab %s size=%lu realsize=%u "
2254 "order=%u offset=%u flags=%lx\n",
2255 s->name, (unsigned long)size, s->size, s->order,
2261 * Check if a given pointer is valid
2263 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2267 page = get_object_page(object);
2269 if (!page || s != page->slab)
2270 /* No slab or wrong slab */
2273 if (!check_valid_pointer(s, page, object))
2277 * We could also check if the object is on the slabs freelist.
2278 * But this would be too expensive and it seems that the main
2279 * purpose of kmem_ptr_valid is to check if the object belongs
2280 * to a certain slab.
2284 EXPORT_SYMBOL(kmem_ptr_validate);
2287 * Determine the size of a slab object
2289 unsigned int kmem_cache_size(struct kmem_cache *s)
2293 EXPORT_SYMBOL(kmem_cache_size);
2295 const char *kmem_cache_name(struct kmem_cache *s)
2299 EXPORT_SYMBOL(kmem_cache_name);
2302 * Attempt to free all slabs on a node. Return the number of slabs we
2303 * were unable to free.
2305 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2306 struct list_head *list)
2308 int slabs_inuse = 0;
2309 unsigned long flags;
2310 struct page *page, *h;
2312 spin_lock_irqsave(&n->list_lock, flags);
2313 list_for_each_entry_safe(page, h, list, lru)
2315 list_del(&page->lru);
2316 discard_slab(s, page);
2319 spin_unlock_irqrestore(&n->list_lock, flags);
2324 * Release all resources used by a slab cache.
2326 static inline int kmem_cache_close(struct kmem_cache *s)
2332 /* Attempt to free all objects */
2333 free_kmem_cache_cpus(s);
2334 for_each_node_state(node, N_NORMAL_MEMORY) {
2335 struct kmem_cache_node *n = get_node(s, node);
2337 n->nr_partial -= free_list(s, n, &n->partial);
2338 if (atomic_long_read(&n->nr_slabs))
2341 free_kmem_cache_nodes(s);
2346 * Close a cache and release the kmem_cache structure
2347 * (must be used for caches created using kmem_cache_create)
2349 void kmem_cache_destroy(struct kmem_cache *s)
2351 down_write(&slub_lock);
2355 up_write(&slub_lock);
2356 if (kmem_cache_close(s))
2358 sysfs_slab_remove(s);
2360 up_write(&slub_lock);
2362 EXPORT_SYMBOL(kmem_cache_destroy);
2364 /********************************************************************
2366 *******************************************************************/
2368 struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
2369 EXPORT_SYMBOL(kmalloc_caches);
2371 #ifdef CONFIG_ZONE_DMA
2372 static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
2375 static int __init setup_slub_min_order(char *str)
2377 get_option(&str, &slub_min_order);
2382 __setup("slub_min_order=", setup_slub_min_order);
2384 static int __init setup_slub_max_order(char *str)
2386 get_option(&str, &slub_max_order);
2391 __setup("slub_max_order=", setup_slub_max_order);
2393 static int __init setup_slub_min_objects(char *str)
2395 get_option(&str, &slub_min_objects);
2400 __setup("slub_min_objects=", setup_slub_min_objects);
2402 static int __init setup_slub_nomerge(char *str)
2408 __setup("slub_nomerge", setup_slub_nomerge);
2410 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2411 const char *name, int size, gfp_t gfp_flags)
2413 unsigned int flags = 0;
2415 if (gfp_flags & SLUB_DMA)
2416 flags = SLAB_CACHE_DMA;
2418 down_write(&slub_lock);
2419 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2423 list_add(&s->list, &slab_caches);
2424 up_write(&slub_lock);
2425 if (sysfs_slab_add(s))
2430 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2433 #ifdef CONFIG_ZONE_DMA
2435 static void sysfs_add_func(struct work_struct *w)
2437 struct kmem_cache *s;
2439 down_write(&slub_lock);
2440 list_for_each_entry(s, &slab_caches, list) {
2441 if (s->flags & __SYSFS_ADD_DEFERRED) {
2442 s->flags &= ~__SYSFS_ADD_DEFERRED;
2446 up_write(&slub_lock);
2449 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2451 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2453 struct kmem_cache *s;
2457 s = kmalloc_caches_dma[index];
2461 /* Dynamically create dma cache */
2462 if (flags & __GFP_WAIT)
2463 down_write(&slub_lock);
2465 if (!down_write_trylock(&slub_lock))
2469 if (kmalloc_caches_dma[index])
2472 realsize = kmalloc_caches[index].objsize;
2473 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2474 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2476 if (!s || !text || !kmem_cache_open(s, flags, text,
2477 realsize, ARCH_KMALLOC_MINALIGN,
2478 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2484 list_add(&s->list, &slab_caches);
2485 kmalloc_caches_dma[index] = s;
2487 schedule_work(&sysfs_add_work);
2490 up_write(&slub_lock);
2492 return kmalloc_caches_dma[index];
2497 * Conversion table for small slabs sizes / 8 to the index in the
2498 * kmalloc array. This is necessary for slabs < 192 since we have non power
2499 * of two cache sizes there. The size of larger slabs can be determined using
2502 static s8 size_index[24] = {
2529 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2535 return ZERO_SIZE_PTR;
2537 index = size_index[(size - 1) / 8];
2539 index = fls(size - 1);
2541 #ifdef CONFIG_ZONE_DMA
2542 if (unlikely((flags & SLUB_DMA)))
2543 return dma_kmalloc_cache(index, flags);
2546 return &kmalloc_caches[index];
2549 void *__kmalloc(size_t size, gfp_t flags)
2551 struct kmem_cache *s;
2553 if (unlikely(size > PAGE_SIZE / 2))
2554 return (void *)__get_free_pages(flags | __GFP_COMP,
2557 s = get_slab(size, flags);
2559 if (unlikely(ZERO_OR_NULL_PTR(s)))
2562 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2564 EXPORT_SYMBOL(__kmalloc);
2567 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2569 struct kmem_cache *s;
2571 if (unlikely(size > PAGE_SIZE / 2))
2572 return (void *)__get_free_pages(flags | __GFP_COMP,
2575 s = get_slab(size, flags);
2577 if (unlikely(ZERO_OR_NULL_PTR(s)))
2580 return slab_alloc(s, flags, node, __builtin_return_address(0));
2582 EXPORT_SYMBOL(__kmalloc_node);
2585 size_t ksize(const void *object)
2588 struct kmem_cache *s;
2591 if (unlikely(object == ZERO_SIZE_PTR))
2594 page = virt_to_head_page(object);
2597 if (unlikely(!PageSlab(page)))
2598 return PAGE_SIZE << compound_order(page);
2604 * Debugging requires use of the padding between object
2605 * and whatever may come after it.
2607 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2611 * If we have the need to store the freelist pointer
2612 * back there or track user information then we can
2613 * only use the space before that information.
2615 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2619 * Else we can use all the padding etc for the allocation
2623 EXPORT_SYMBOL(ksize);
2625 void kfree(const void *x)
2628 void *object = (void *)x;
2630 if (unlikely(ZERO_OR_NULL_PTR(x)))
2633 page = virt_to_head_page(x);
2634 if (unlikely(!PageSlab(page))) {
2638 slab_free(page->slab, page, object, __builtin_return_address(0));
2640 EXPORT_SYMBOL(kfree);
2642 static unsigned long count_partial(struct kmem_cache_node *n)
2644 unsigned long flags;
2645 unsigned long x = 0;
2648 spin_lock_irqsave(&n->list_lock, flags);
2649 list_for_each_entry(page, &n->partial, lru)
2651 spin_unlock_irqrestore(&n->list_lock, flags);
2656 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2657 * the remaining slabs by the number of items in use. The slabs with the
2658 * most items in use come first. New allocations will then fill those up
2659 * and thus they can be removed from the partial lists.
2661 * The slabs with the least items are placed last. This results in them
2662 * being allocated from last increasing the chance that the last objects
2663 * are freed in them.
2665 int kmem_cache_shrink(struct kmem_cache *s)
2669 struct kmem_cache_node *n;
2672 struct list_head *slabs_by_inuse =
2673 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2674 unsigned long flags;
2676 if (!slabs_by_inuse)
2680 for_each_node_state(node, N_NORMAL_MEMORY) {
2681 n = get_node(s, node);
2686 for (i = 0; i < s->objects; i++)
2687 INIT_LIST_HEAD(slabs_by_inuse + i);
2689 spin_lock_irqsave(&n->list_lock, flags);
2692 * Build lists indexed by the items in use in each slab.
2694 * Note that concurrent frees may occur while we hold the
2695 * list_lock. page->inuse here is the upper limit.
2697 list_for_each_entry_safe(page, t, &n->partial, lru) {
2698 if (!page->inuse && slab_trylock(page)) {
2700 * Must hold slab lock here because slab_free
2701 * may have freed the last object and be
2702 * waiting to release the slab.
2704 list_del(&page->lru);
2707 discard_slab(s, page);
2709 list_move(&page->lru,
2710 slabs_by_inuse + page->inuse);
2715 * Rebuild the partial list with the slabs filled up most
2716 * first and the least used slabs at the end.
2718 for (i = s->objects - 1; i >= 0; i--)
2719 list_splice(slabs_by_inuse + i, n->partial.prev);
2721 spin_unlock_irqrestore(&n->list_lock, flags);
2724 kfree(slabs_by_inuse);
2727 EXPORT_SYMBOL(kmem_cache_shrink);
2729 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2730 static int slab_mem_going_offline_callback(void *arg)
2732 struct kmem_cache *s;
2734 down_read(&slub_lock);
2735 list_for_each_entry(s, &slab_caches, list)
2736 kmem_cache_shrink(s);
2737 up_read(&slub_lock);
2742 static void slab_mem_offline_callback(void *arg)
2744 struct kmem_cache_node *n;
2745 struct kmem_cache *s;
2746 struct memory_notify *marg = arg;
2749 offline_node = marg->status_change_nid;
2752 * If the node still has available memory. we need kmem_cache_node
2755 if (offline_node < 0)
2758 down_read(&slub_lock);
2759 list_for_each_entry(s, &slab_caches, list) {
2760 n = get_node(s, offline_node);
2763 * if n->nr_slabs > 0, slabs still exist on the node
2764 * that is going down. We were unable to free them,
2765 * and offline_pages() function shoudn't call this
2766 * callback. So, we must fail.
2768 BUG_ON(atomic_long_read(&n->nr_slabs));
2770 s->node[offline_node] = NULL;
2771 kmem_cache_free(kmalloc_caches, n);
2774 up_read(&slub_lock);
2777 static int slab_mem_going_online_callback(void *arg)
2779 struct kmem_cache_node *n;
2780 struct kmem_cache *s;
2781 struct memory_notify *marg = arg;
2782 int nid = marg->status_change_nid;
2786 * If the node's memory is already available, then kmem_cache_node is
2787 * already created. Nothing to do.
2793 * We are bringing a node online. No memory is availabe yet. We must
2794 * allocate a kmem_cache_node structure in order to bring the node
2797 down_read(&slub_lock);
2798 list_for_each_entry(s, &slab_caches, list) {
2800 * XXX: kmem_cache_alloc_node will fallback to other nodes
2801 * since memory is not yet available from the node that
2804 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2809 init_kmem_cache_node(n);
2813 up_read(&slub_lock);
2817 static int slab_memory_callback(struct notifier_block *self,
2818 unsigned long action, void *arg)
2823 case MEM_GOING_ONLINE:
2824 ret = slab_mem_going_online_callback(arg);
2826 case MEM_GOING_OFFLINE:
2827 ret = slab_mem_going_offline_callback(arg);
2830 case MEM_CANCEL_ONLINE:
2831 slab_mem_offline_callback(arg);
2834 case MEM_CANCEL_OFFLINE:
2838 ret = notifier_from_errno(ret);
2842 #endif /* CONFIG_MEMORY_HOTPLUG */
2844 /********************************************************************
2845 * Basic setup of slabs
2846 *******************************************************************/
2848 void __init kmem_cache_init(void)
2857 * Must first have the slab cache available for the allocations of the
2858 * struct kmem_cache_node's. There is special bootstrap code in
2859 * kmem_cache_open for slab_state == DOWN.
2861 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2862 sizeof(struct kmem_cache_node), GFP_KERNEL);
2863 kmalloc_caches[0].refcount = -1;
2866 hotplug_memory_notifier(slab_memory_callback, 1);
2869 /* Able to allocate the per node structures */
2870 slab_state = PARTIAL;
2872 /* Caches that are not of the two-to-the-power-of size */
2873 if (KMALLOC_MIN_SIZE <= 64) {
2874 create_kmalloc_cache(&kmalloc_caches[1],
2875 "kmalloc-96", 96, GFP_KERNEL);
2878 if (KMALLOC_MIN_SIZE <= 128) {
2879 create_kmalloc_cache(&kmalloc_caches[2],
2880 "kmalloc-192", 192, GFP_KERNEL);
2884 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
2885 create_kmalloc_cache(&kmalloc_caches[i],
2886 "kmalloc", 1 << i, GFP_KERNEL);
2892 * Patch up the size_index table if we have strange large alignment
2893 * requirements for the kmalloc array. This is only the case for
2894 * mips it seems. The standard arches will not generate any code here.
2896 * Largest permitted alignment is 256 bytes due to the way we
2897 * handle the index determination for the smaller caches.
2899 * Make sure that nothing crazy happens if someone starts tinkering
2900 * around with ARCH_KMALLOC_MINALIGN
2902 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2903 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2905 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
2906 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2910 /* Provide the correct kmalloc names now that the caches are up */
2911 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
2912 kmalloc_caches[i]. name =
2913 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2916 register_cpu_notifier(&slab_notifier);
2917 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
2918 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
2920 kmem_size = sizeof(struct kmem_cache);
2924 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
2925 " CPUs=%d, Nodes=%d\n",
2926 caches, cache_line_size(),
2927 slub_min_order, slub_max_order, slub_min_objects,
2928 nr_cpu_ids, nr_node_ids);
2932 * Find a mergeable slab cache
2934 static int slab_unmergeable(struct kmem_cache *s)
2936 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
2943 * We may have set a slab to be unmergeable during bootstrap.
2945 if (s->refcount < 0)
2951 static struct kmem_cache *find_mergeable(size_t size,
2952 size_t align, unsigned long flags, const char *name,
2953 void (*ctor)(struct kmem_cache *, void *))
2955 struct kmem_cache *s;
2957 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
2963 size = ALIGN(size, sizeof(void *));
2964 align = calculate_alignment(flags, align, size);
2965 size = ALIGN(size, align);
2966 flags = kmem_cache_flags(size, flags, name, NULL);
2968 list_for_each_entry(s, &slab_caches, list) {
2969 if (slab_unmergeable(s))
2975 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
2978 * Check if alignment is compatible.
2979 * Courtesy of Adrian Drzewiecki
2981 if ((s->size & ~(align - 1)) != s->size)
2984 if (s->size - size >= sizeof(void *))
2992 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
2993 size_t align, unsigned long flags,
2994 void (*ctor)(struct kmem_cache *, void *))
2996 struct kmem_cache *s;
2998 down_write(&slub_lock);
2999 s = find_mergeable(size, align, flags, name, ctor);
3005 * Adjust the object sizes so that we clear
3006 * the complete object on kzalloc.
3008 s->objsize = max(s->objsize, (int)size);
3011 * And then we need to update the object size in the
3012 * per cpu structures
3014 for_each_online_cpu(cpu)
3015 get_cpu_slab(s, cpu)->objsize = s->objsize;
3016 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3017 up_write(&slub_lock);
3018 if (sysfs_slab_alias(s, name))
3022 s = kmalloc(kmem_size, GFP_KERNEL);
3024 if (kmem_cache_open(s, GFP_KERNEL, name,
3025 size, align, flags, ctor)) {
3026 list_add(&s->list, &slab_caches);
3027 up_write(&slub_lock);
3028 if (sysfs_slab_add(s))
3034 up_write(&slub_lock);
3037 if (flags & SLAB_PANIC)
3038 panic("Cannot create slabcache %s\n", name);
3043 EXPORT_SYMBOL(kmem_cache_create);
3047 * Use the cpu notifier to insure that the cpu slabs are flushed when
3050 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3051 unsigned long action, void *hcpu)
3053 long cpu = (long)hcpu;
3054 struct kmem_cache *s;
3055 unsigned long flags;
3058 case CPU_UP_PREPARE:
3059 case CPU_UP_PREPARE_FROZEN:
3060 init_alloc_cpu_cpu(cpu);
3061 down_read(&slub_lock);
3062 list_for_each_entry(s, &slab_caches, list)
3063 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3065 up_read(&slub_lock);
3068 case CPU_UP_CANCELED:
3069 case CPU_UP_CANCELED_FROZEN:
3071 case CPU_DEAD_FROZEN:
3072 down_read(&slub_lock);
3073 list_for_each_entry(s, &slab_caches, list) {
3074 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3076 local_irq_save(flags);
3077 __flush_cpu_slab(s, cpu);
3078 local_irq_restore(flags);
3079 free_kmem_cache_cpu(c, cpu);
3080 s->cpu_slab[cpu] = NULL;
3082 up_read(&slub_lock);
3090 static struct notifier_block __cpuinitdata slab_notifier = {
3091 &slab_cpuup_callback, NULL, 0
3096 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3098 struct kmem_cache *s;
3100 if (unlikely(size > PAGE_SIZE / 2))
3101 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
3103 s = get_slab(size, gfpflags);
3105 if (unlikely(ZERO_OR_NULL_PTR(s)))
3108 return slab_alloc(s, gfpflags, -1, caller);
3111 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3112 int node, void *caller)
3114 struct kmem_cache *s;
3116 if (unlikely(size > PAGE_SIZE / 2))
3117 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
3119 s = get_slab(size, gfpflags);
3121 if (unlikely(ZERO_OR_NULL_PTR(s)))
3124 return slab_alloc(s, gfpflags, node, caller);
3127 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
3128 static int validate_slab(struct kmem_cache *s, struct page *page,
3132 void *addr = slab_address(page);
3134 if (!check_slab(s, page) ||
3135 !on_freelist(s, page, NULL))
3138 /* Now we know that a valid freelist exists */
3139 bitmap_zero(map, s->objects);
3141 for_each_free_object(p, s, page->freelist) {
3142 set_bit(slab_index(p, s, addr), map);
3143 if (!check_object(s, page, p, 0))
3147 for_each_object(p, s, addr)
3148 if (!test_bit(slab_index(p, s, addr), map))
3149 if (!check_object(s, page, p, 1))
3154 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3157 if (slab_trylock(page)) {
3158 validate_slab(s, page, map);
3161 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3164 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3165 if (!SlabDebug(page))
3166 printk(KERN_ERR "SLUB %s: SlabDebug not set "
3167 "on slab 0x%p\n", s->name, page);
3169 if (SlabDebug(page))
3170 printk(KERN_ERR "SLUB %s: SlabDebug set on "
3171 "slab 0x%p\n", s->name, page);
3175 static int validate_slab_node(struct kmem_cache *s,
3176 struct kmem_cache_node *n, unsigned long *map)
3178 unsigned long count = 0;
3180 unsigned long flags;
3182 spin_lock_irqsave(&n->list_lock, flags);
3184 list_for_each_entry(page, &n->partial, lru) {
3185 validate_slab_slab(s, page, map);
3188 if (count != n->nr_partial)
3189 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3190 "counter=%ld\n", s->name, count, n->nr_partial);
3192 if (!(s->flags & SLAB_STORE_USER))
3195 list_for_each_entry(page, &n->full, lru) {
3196 validate_slab_slab(s, page, map);
3199 if (count != atomic_long_read(&n->nr_slabs))
3200 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3201 "counter=%ld\n", s->name, count,
3202 atomic_long_read(&n->nr_slabs));
3205 spin_unlock_irqrestore(&n->list_lock, flags);
3209 static long validate_slab_cache(struct kmem_cache *s)
3212 unsigned long count = 0;
3213 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3214 sizeof(unsigned long), GFP_KERNEL);
3220 for_each_node_state(node, N_NORMAL_MEMORY) {
3221 struct kmem_cache_node *n = get_node(s, node);
3223 count += validate_slab_node(s, n, map);
3229 #ifdef SLUB_RESILIENCY_TEST
3230 static void resiliency_test(void)
3234 printk(KERN_ERR "SLUB resiliency testing\n");
3235 printk(KERN_ERR "-----------------------\n");
3236 printk(KERN_ERR "A. Corruption after allocation\n");
3238 p = kzalloc(16, GFP_KERNEL);
3240 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3241 " 0x12->0x%p\n\n", p + 16);
3243 validate_slab_cache(kmalloc_caches + 4);
3245 /* Hmmm... The next two are dangerous */
3246 p = kzalloc(32, GFP_KERNEL);
3247 p[32 + sizeof(void *)] = 0x34;
3248 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3249 " 0x34 -> -0x%p\n", p);
3250 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3252 validate_slab_cache(kmalloc_caches + 5);
3253 p = kzalloc(64, GFP_KERNEL);
3254 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3256 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3258 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3259 validate_slab_cache(kmalloc_caches + 6);
3261 printk(KERN_ERR "\nB. Corruption after free\n");
3262 p = kzalloc(128, GFP_KERNEL);
3265 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3266 validate_slab_cache(kmalloc_caches + 7);
3268 p = kzalloc(256, GFP_KERNEL);
3271 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
3272 validate_slab_cache(kmalloc_caches + 8);
3274 p = kzalloc(512, GFP_KERNEL);
3277 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3278 validate_slab_cache(kmalloc_caches + 9);
3281 static void resiliency_test(void) {};
3285 * Generate lists of code addresses where slabcache objects are allocated
3290 unsigned long count;
3303 unsigned long count;
3304 struct location *loc;
3307 static void free_loc_track(struct loc_track *t)
3310 free_pages((unsigned long)t->loc,
3311 get_order(sizeof(struct location) * t->max));
3314 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3319 order = get_order(sizeof(struct location) * max);
3321 l = (void *)__get_free_pages(flags, order);
3326 memcpy(l, t->loc, sizeof(struct location) * t->count);
3334 static int add_location(struct loc_track *t, struct kmem_cache *s,
3335 const struct track *track)
3337 long start, end, pos;
3340 unsigned long age = jiffies - track->when;
3346 pos = start + (end - start + 1) / 2;
3349 * There is nothing at "end". If we end up there
3350 * we need to add something to before end.
3355 caddr = t->loc[pos].addr;
3356 if (track->addr == caddr) {
3362 if (age < l->min_time)
3364 if (age > l->max_time)
3367 if (track->pid < l->min_pid)
3368 l->min_pid = track->pid;
3369 if (track->pid > l->max_pid)
3370 l->max_pid = track->pid;
3372 cpu_set(track->cpu, l->cpus);
3374 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3378 if (track->addr < caddr)
3385 * Not found. Insert new tracking element.
3387 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3393 (t->count - pos) * sizeof(struct location));
3396 l->addr = track->addr;
3400 l->min_pid = track->pid;
3401 l->max_pid = track->pid;
3402 cpus_clear(l->cpus);
3403 cpu_set(track->cpu, l->cpus);
3404 nodes_clear(l->nodes);
3405 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3409 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3410 struct page *page, enum track_item alloc)
3412 void *addr = slab_address(page);
3413 DECLARE_BITMAP(map, s->objects);
3416 bitmap_zero(map, s->objects);
3417 for_each_free_object(p, s, page->freelist)
3418 set_bit(slab_index(p, s, addr), map);
3420 for_each_object(p, s, addr)
3421 if (!test_bit(slab_index(p, s, addr), map))
3422 add_location(t, s, get_track(s, p, alloc));
3425 static int list_locations(struct kmem_cache *s, char *buf,
3426 enum track_item alloc)
3430 struct loc_track t = { 0, 0, NULL };
3433 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3435 return sprintf(buf, "Out of memory\n");
3437 /* Push back cpu slabs */
3440 for_each_node_state(node, N_NORMAL_MEMORY) {
3441 struct kmem_cache_node *n = get_node(s, node);
3442 unsigned long flags;
3445 if (!atomic_long_read(&n->nr_slabs))
3448 spin_lock_irqsave(&n->list_lock, flags);
3449 list_for_each_entry(page, &n->partial, lru)
3450 process_slab(&t, s, page, alloc);
3451 list_for_each_entry(page, &n->full, lru)
3452 process_slab(&t, s, page, alloc);
3453 spin_unlock_irqrestore(&n->list_lock, flags);
3456 for (i = 0; i < t.count; i++) {
3457 struct location *l = &t.loc[i];
3459 if (len > PAGE_SIZE - 100)
3461 len += sprintf(buf + len, "%7ld ", l->count);
3464 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3466 len += sprintf(buf + len, "<not-available>");
3468 if (l->sum_time != l->min_time) {
3469 unsigned long remainder;
3471 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3473 div_long_long_rem(l->sum_time, l->count, &remainder),
3476 len += sprintf(buf + len, " age=%ld",
3479 if (l->min_pid != l->max_pid)
3480 len += sprintf(buf + len, " pid=%ld-%ld",
3481 l->min_pid, l->max_pid);
3483 len += sprintf(buf + len, " pid=%ld",
3486 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3487 len < PAGE_SIZE - 60) {
3488 len += sprintf(buf + len, " cpus=");
3489 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3493 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3494 len < PAGE_SIZE - 60) {
3495 len += sprintf(buf + len, " nodes=");
3496 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3500 len += sprintf(buf + len, "\n");
3505 len += sprintf(buf, "No data\n");
3509 enum slab_stat_type {
3516 #define SO_FULL (1 << SL_FULL)
3517 #define SO_PARTIAL (1 << SL_PARTIAL)
3518 #define SO_CPU (1 << SL_CPU)
3519 #define SO_OBJECTS (1 << SL_OBJECTS)
3521 static unsigned long slab_objects(struct kmem_cache *s,
3522 char *buf, unsigned long flags)
3524 unsigned long total = 0;
3528 unsigned long *nodes;
3529 unsigned long *per_cpu;
3531 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3532 per_cpu = nodes + nr_node_ids;
3534 for_each_possible_cpu(cpu) {
3536 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3546 if (flags & SO_CPU) {
3547 if (flags & SO_OBJECTS)
3558 for_each_node_state(node, N_NORMAL_MEMORY) {
3559 struct kmem_cache_node *n = get_node(s, node);
3561 if (flags & SO_PARTIAL) {
3562 if (flags & SO_OBJECTS)
3563 x = count_partial(n);
3570 if (flags & SO_FULL) {
3571 int full_slabs = atomic_long_read(&n->nr_slabs)
3575 if (flags & SO_OBJECTS)
3576 x = full_slabs * s->objects;
3584 x = sprintf(buf, "%lu", total);
3586 for_each_node_state(node, N_NORMAL_MEMORY)
3588 x += sprintf(buf + x, " N%d=%lu",
3592 return x + sprintf(buf + x, "\n");
3595 static int any_slab_objects(struct kmem_cache *s)
3600 for_each_possible_cpu(cpu) {
3601 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3607 for_each_online_node(node) {
3608 struct kmem_cache_node *n = get_node(s, node);
3613 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
3619 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3620 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3622 struct slab_attribute {
3623 struct attribute attr;
3624 ssize_t (*show)(struct kmem_cache *s, char *buf);
3625 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3628 #define SLAB_ATTR_RO(_name) \
3629 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3631 #define SLAB_ATTR(_name) \
3632 static struct slab_attribute _name##_attr = \
3633 __ATTR(_name, 0644, _name##_show, _name##_store)
3635 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3637 return sprintf(buf, "%d\n", s->size);
3639 SLAB_ATTR_RO(slab_size);
3641 static ssize_t align_show(struct kmem_cache *s, char *buf)
3643 return sprintf(buf, "%d\n", s->align);
3645 SLAB_ATTR_RO(align);
3647 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3649 return sprintf(buf, "%d\n", s->objsize);
3651 SLAB_ATTR_RO(object_size);
3653 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3655 return sprintf(buf, "%d\n", s->objects);
3657 SLAB_ATTR_RO(objs_per_slab);
3659 static ssize_t order_show(struct kmem_cache *s, char *buf)
3661 return sprintf(buf, "%d\n", s->order);
3663 SLAB_ATTR_RO(order);
3665 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3668 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3670 return n + sprintf(buf + n, "\n");
3676 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3678 return sprintf(buf, "%d\n", s->refcount - 1);
3680 SLAB_ATTR_RO(aliases);
3682 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3684 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3686 SLAB_ATTR_RO(slabs);
3688 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3690 return slab_objects(s, buf, SO_PARTIAL);
3692 SLAB_ATTR_RO(partial);
3694 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3696 return slab_objects(s, buf, SO_CPU);
3698 SLAB_ATTR_RO(cpu_slabs);
3700 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3702 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3704 SLAB_ATTR_RO(objects);
3706 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3708 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3711 static ssize_t sanity_checks_store(struct kmem_cache *s,
3712 const char *buf, size_t length)
3714 s->flags &= ~SLAB_DEBUG_FREE;
3716 s->flags |= SLAB_DEBUG_FREE;
3719 SLAB_ATTR(sanity_checks);
3721 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3723 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3726 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3729 s->flags &= ~SLAB_TRACE;
3731 s->flags |= SLAB_TRACE;
3736 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3738 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3741 static ssize_t reclaim_account_store(struct kmem_cache *s,
3742 const char *buf, size_t length)
3744 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3746 s->flags |= SLAB_RECLAIM_ACCOUNT;
3749 SLAB_ATTR(reclaim_account);
3751 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3753 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3755 SLAB_ATTR_RO(hwcache_align);
3757 #ifdef CONFIG_ZONE_DMA
3758 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3760 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3762 SLAB_ATTR_RO(cache_dma);
3765 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3767 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3769 SLAB_ATTR_RO(destroy_by_rcu);
3771 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3773 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3776 static ssize_t red_zone_store(struct kmem_cache *s,
3777 const char *buf, size_t length)
3779 if (any_slab_objects(s))
3782 s->flags &= ~SLAB_RED_ZONE;
3784 s->flags |= SLAB_RED_ZONE;
3788 SLAB_ATTR(red_zone);
3790 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3792 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3795 static ssize_t poison_store(struct kmem_cache *s,
3796 const char *buf, size_t length)
3798 if (any_slab_objects(s))
3801 s->flags &= ~SLAB_POISON;
3803 s->flags |= SLAB_POISON;
3809 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3811 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3814 static ssize_t store_user_store(struct kmem_cache *s,
3815 const char *buf, size_t length)
3817 if (any_slab_objects(s))
3820 s->flags &= ~SLAB_STORE_USER;
3822 s->flags |= SLAB_STORE_USER;
3826 SLAB_ATTR(store_user);
3828 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3833 static ssize_t validate_store(struct kmem_cache *s,
3834 const char *buf, size_t length)
3838 if (buf[0] == '1') {
3839 ret = validate_slab_cache(s);
3845 SLAB_ATTR(validate);
3847 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3852 static ssize_t shrink_store(struct kmem_cache *s,
3853 const char *buf, size_t length)
3855 if (buf[0] == '1') {
3856 int rc = kmem_cache_shrink(s);
3866 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3868 if (!(s->flags & SLAB_STORE_USER))
3870 return list_locations(s, buf, TRACK_ALLOC);
3872 SLAB_ATTR_RO(alloc_calls);
3874 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3876 if (!(s->flags & SLAB_STORE_USER))
3878 return list_locations(s, buf, TRACK_FREE);
3880 SLAB_ATTR_RO(free_calls);
3883 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
3885 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
3888 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
3889 const char *buf, size_t length)
3891 int n = simple_strtoul(buf, NULL, 10);
3894 s->remote_node_defrag_ratio = n * 10;
3897 SLAB_ATTR(remote_node_defrag_ratio);
3900 static struct attribute *slab_attrs[] = {
3901 &slab_size_attr.attr,
3902 &object_size_attr.attr,
3903 &objs_per_slab_attr.attr,
3908 &cpu_slabs_attr.attr,
3912 &sanity_checks_attr.attr,
3914 &hwcache_align_attr.attr,
3915 &reclaim_account_attr.attr,
3916 &destroy_by_rcu_attr.attr,
3917 &red_zone_attr.attr,
3919 &store_user_attr.attr,
3920 &validate_attr.attr,
3922 &alloc_calls_attr.attr,
3923 &free_calls_attr.attr,
3924 #ifdef CONFIG_ZONE_DMA
3925 &cache_dma_attr.attr,
3928 &remote_node_defrag_ratio_attr.attr,
3933 static struct attribute_group slab_attr_group = {
3934 .attrs = slab_attrs,
3937 static ssize_t slab_attr_show(struct kobject *kobj,
3938 struct attribute *attr,
3941 struct slab_attribute *attribute;
3942 struct kmem_cache *s;
3945 attribute = to_slab_attr(attr);
3948 if (!attribute->show)
3951 err = attribute->show(s, buf);
3956 static ssize_t slab_attr_store(struct kobject *kobj,
3957 struct attribute *attr,
3958 const char *buf, size_t len)
3960 struct slab_attribute *attribute;
3961 struct kmem_cache *s;
3964 attribute = to_slab_attr(attr);
3967 if (!attribute->store)
3970 err = attribute->store(s, buf, len);
3975 static void kmem_cache_release(struct kobject *kobj)
3977 struct kmem_cache *s = to_slab(kobj);
3982 static struct sysfs_ops slab_sysfs_ops = {
3983 .show = slab_attr_show,
3984 .store = slab_attr_store,
3987 static struct kobj_type slab_ktype = {
3988 .sysfs_ops = &slab_sysfs_ops,
3989 .release = kmem_cache_release
3992 static int uevent_filter(struct kset *kset, struct kobject *kobj)
3994 struct kobj_type *ktype = get_ktype(kobj);
3996 if (ktype == &slab_ktype)
4001 static struct kset_uevent_ops slab_uevent_ops = {
4002 .filter = uevent_filter,
4005 static struct kset *slab_kset;
4007 #define ID_STR_LENGTH 64
4009 /* Create a unique string id for a slab cache:
4011 * :[flags-]size:[memory address of kmemcache]
4013 static char *create_unique_id(struct kmem_cache *s)
4015 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4022 * First flags affecting slabcache operations. We will only
4023 * get here for aliasable slabs so we do not need to support
4024 * too many flags. The flags here must cover all flags that
4025 * are matched during merging to guarantee that the id is
4028 if (s->flags & SLAB_CACHE_DMA)
4030 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4032 if (s->flags & SLAB_DEBUG_FREE)
4036 p += sprintf(p, "%07d", s->size);
4037 BUG_ON(p > name + ID_STR_LENGTH - 1);
4041 static int sysfs_slab_add(struct kmem_cache *s)
4047 if (slab_state < SYSFS)
4048 /* Defer until later */
4051 unmergeable = slab_unmergeable(s);
4054 * Slabcache can never be merged so we can use the name proper.
4055 * This is typically the case for debug situations. In that
4056 * case we can catch duplicate names easily.
4058 sysfs_remove_link(&slab_kset->kobj, s->name);
4062 * Create a unique name for the slab as a target
4065 name = create_unique_id(s);
4068 s->kobj.kset = slab_kset;
4069 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4071 kobject_put(&s->kobj);
4075 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4078 kobject_uevent(&s->kobj, KOBJ_ADD);
4080 /* Setup first alias */
4081 sysfs_slab_alias(s, s->name);
4087 static void sysfs_slab_remove(struct kmem_cache *s)
4089 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4090 kobject_del(&s->kobj);
4091 kobject_put(&s->kobj);
4095 * Need to buffer aliases during bootup until sysfs becomes
4096 * available lest we loose that information.
4098 struct saved_alias {
4099 struct kmem_cache *s;
4101 struct saved_alias *next;
4104 static struct saved_alias *alias_list;
4106 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4108 struct saved_alias *al;
4110 if (slab_state == SYSFS) {
4112 * If we have a leftover link then remove it.
4114 sysfs_remove_link(&slab_kset->kobj, name);
4115 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4118 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4124 al->next = alias_list;
4129 static int __init slab_sysfs_init(void)
4131 struct kmem_cache *s;
4134 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4136 printk(KERN_ERR "Cannot register slab subsystem.\n");
4142 list_for_each_entry(s, &slab_caches, list) {
4143 err = sysfs_slab_add(s);
4145 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4146 " to sysfs\n", s->name);
4149 while (alias_list) {
4150 struct saved_alias *al = alias_list;
4152 alias_list = alias_list->next;
4153 err = sysfs_slab_alias(al->s, al->name);
4155 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4156 " %s to sysfs\n", s->name);
4164 __initcall(slab_sysfs_init);
4168 * The /proc/slabinfo ABI
4170 #ifdef CONFIG_SLABINFO
4172 ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4173 size_t count, loff_t *ppos)
4179 static void print_slabinfo_header(struct seq_file *m)
4181 seq_puts(m, "slabinfo - version: 2.1\n");
4182 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4183 "<objperslab> <pagesperslab>");
4184 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4185 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4189 static void *s_start(struct seq_file *m, loff_t *pos)
4193 down_read(&slub_lock);
4195 print_slabinfo_header(m);
4197 return seq_list_start(&slab_caches, *pos);
4200 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4202 return seq_list_next(p, &slab_caches, pos);
4205 static void s_stop(struct seq_file *m, void *p)
4207 up_read(&slub_lock);
4210 static int s_show(struct seq_file *m, void *p)
4212 unsigned long nr_partials = 0;
4213 unsigned long nr_slabs = 0;
4214 unsigned long nr_inuse = 0;
4215 unsigned long nr_objs;
4216 struct kmem_cache *s;
4219 s = list_entry(p, struct kmem_cache, list);
4221 for_each_online_node(node) {
4222 struct kmem_cache_node *n = get_node(s, node);
4227 nr_partials += n->nr_partial;
4228 nr_slabs += atomic_long_read(&n->nr_slabs);
4229 nr_inuse += count_partial(n);
4232 nr_objs = nr_slabs * s->objects;
4233 nr_inuse += (nr_slabs - nr_partials) * s->objects;
4235 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4236 nr_objs, s->size, s->objects, (1 << s->order));
4237 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4238 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4244 const struct seq_operations slabinfo_op = {
4251 #endif /* CONFIG_SLABINFO */