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
153 * Currently fastpath is not supported if preemption is enabled.
155 #if defined(CONFIG_FAST_CMPXCHG_LOCAL) && !defined(CONFIG_PREEMPT)
156 #define SLUB_FASTPATH
162 * Small page size. Make sure that we do not fragment memory
164 #define DEFAULT_MAX_ORDER 1
165 #define DEFAULT_MIN_OBJECTS 4
170 * Large page machines are customarily able to handle larger
173 #define DEFAULT_MAX_ORDER 2
174 #define DEFAULT_MIN_OBJECTS 8
179 * Mininum number of partial slabs. These will be left on the partial
180 * lists even if they are empty. kmem_cache_shrink may reclaim them.
182 #define MIN_PARTIAL 5
185 * Maximum number of desirable partial slabs.
186 * The existence of more partial slabs makes kmem_cache_shrink
187 * sort the partial list by the number of objects in the.
189 #define MAX_PARTIAL 10
191 #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
192 SLAB_POISON | SLAB_STORE_USER)
195 * Set of flags that will prevent slab merging
197 #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
198 SLAB_TRACE | SLAB_DESTROY_BY_RCU)
200 #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
203 #ifndef ARCH_KMALLOC_MINALIGN
204 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
207 #ifndef ARCH_SLAB_MINALIGN
208 #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
211 /* Internal SLUB flags */
212 #define __OBJECT_POISON 0x80000000 /* Poison object */
213 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
215 /* Not all arches define cache_line_size */
216 #ifndef cache_line_size
217 #define cache_line_size() L1_CACHE_BYTES
220 static int kmem_size = sizeof(struct kmem_cache);
223 static struct notifier_block slab_notifier;
227 DOWN, /* No slab functionality available */
228 PARTIAL, /* kmem_cache_open() works but kmalloc does not */
229 UP, /* Everything works but does not show up in sysfs */
233 /* A list of all slab caches on the system */
234 static DECLARE_RWSEM(slub_lock);
235 static LIST_HEAD(slab_caches);
238 * Tracking user of a slab.
241 void *addr; /* Called from address */
242 int cpu; /* Was running on cpu */
243 int pid; /* Pid context */
244 unsigned long when; /* When did the operation occur */
247 enum track_item { TRACK_ALLOC, TRACK_FREE };
249 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
250 static int sysfs_slab_add(struct kmem_cache *);
251 static int sysfs_slab_alias(struct kmem_cache *, const char *);
252 static void sysfs_slab_remove(struct kmem_cache *);
254 static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
255 static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
257 static inline void sysfs_slab_remove(struct kmem_cache *s)
263 /********************************************************************
264 * Core slab cache functions
265 *******************************************************************/
267 int slab_is_available(void)
269 return slab_state >= UP;
272 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
275 return s->node[node];
277 return &s->local_node;
281 static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
284 return s->cpu_slab[cpu];
291 * The end pointer in a slab is special. It points to the first object in the
292 * slab but has bit 0 set to mark it.
294 * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
295 * in the mapping set.
297 static inline int is_end(void *addr)
299 return (unsigned long)addr & PAGE_MAPPING_ANON;
302 void *slab_address(struct page *page)
304 return page->end - PAGE_MAPPING_ANON;
307 static inline int check_valid_pointer(struct kmem_cache *s,
308 struct page *page, const void *object)
312 if (object == page->end)
315 base = slab_address(page);
316 if (object < base || object >= base + s->objects * s->size ||
317 (object - base) % s->size) {
325 * Slow version of get and set free pointer.
327 * This version requires touching the cache lines of kmem_cache which
328 * we avoid to do in the fast alloc free paths. There we obtain the offset
329 * from the page struct.
331 static inline void *get_freepointer(struct kmem_cache *s, void *object)
333 return *(void **)(object + s->offset);
336 static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
338 *(void **)(object + s->offset) = fp;
341 /* Loop over all objects in a slab */
342 #define for_each_object(__p, __s, __addr) \
343 for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
347 #define for_each_free_object(__p, __s, __free) \
348 for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
351 /* Determine object index from a given position */
352 static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
354 return (p - addr) / s->size;
357 #ifdef CONFIG_SLUB_DEBUG
361 #ifdef CONFIG_SLUB_DEBUG_ON
362 static int slub_debug = DEBUG_DEFAULT_FLAGS;
364 static int slub_debug;
367 static char *slub_debug_slabs;
372 static void print_section(char *text, u8 *addr, unsigned int length)
380 for (i = 0; i < length; i++) {
382 printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
385 printk(KERN_CONT " %02x", addr[i]);
387 ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
389 printk(KERN_CONT " %s\n", ascii);
396 printk(KERN_CONT " ");
400 printk(KERN_CONT " %s\n", ascii);
404 static struct track *get_track(struct kmem_cache *s, void *object,
405 enum track_item alloc)
410 p = object + s->offset + sizeof(void *);
412 p = object + s->inuse;
417 static void set_track(struct kmem_cache *s, void *object,
418 enum track_item alloc, void *addr)
423 p = object + s->offset + sizeof(void *);
425 p = object + s->inuse;
430 p->cpu = smp_processor_id();
431 p->pid = current ? current->pid : -1;
434 memset(p, 0, sizeof(struct track));
437 static void init_tracking(struct kmem_cache *s, void *object)
439 if (!(s->flags & SLAB_STORE_USER))
442 set_track(s, object, TRACK_FREE, NULL);
443 set_track(s, object, TRACK_ALLOC, NULL);
446 static void print_track(const char *s, struct track *t)
451 printk(KERN_ERR "INFO: %s in ", s);
452 __print_symbol("%s", (unsigned long)t->addr);
453 printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
456 static void print_tracking(struct kmem_cache *s, void *object)
458 if (!(s->flags & SLAB_STORE_USER))
461 print_track("Allocated", get_track(s, object, TRACK_ALLOC));
462 print_track("Freed", get_track(s, object, TRACK_FREE));
465 static void print_page_info(struct page *page)
467 printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
468 page, page->inuse, page->freelist, page->flags);
472 static void slab_bug(struct kmem_cache *s, char *fmt, ...)
478 vsnprintf(buf, sizeof(buf), fmt, args);
480 printk(KERN_ERR "========================================"
481 "=====================================\n");
482 printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
483 printk(KERN_ERR "----------------------------------------"
484 "-------------------------------------\n\n");
487 static void slab_fix(struct kmem_cache *s, char *fmt, ...)
493 vsnprintf(buf, sizeof(buf), fmt, args);
495 printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
498 static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
500 unsigned int off; /* Offset of last byte */
501 u8 *addr = slab_address(page);
503 print_tracking(s, p);
505 print_page_info(page);
507 printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
508 p, p - addr, get_freepointer(s, p));
511 print_section("Bytes b4", p - 16, 16);
513 print_section("Object", p, min(s->objsize, 128));
515 if (s->flags & SLAB_RED_ZONE)
516 print_section("Redzone", p + s->objsize,
517 s->inuse - s->objsize);
520 off = s->offset + sizeof(void *);
524 if (s->flags & SLAB_STORE_USER)
525 off += 2 * sizeof(struct track);
528 /* Beginning of the filler is the free pointer */
529 print_section("Padding", p + off, s->size - off);
534 static void object_err(struct kmem_cache *s, struct page *page,
535 u8 *object, char *reason)
538 print_trailer(s, page, object);
541 static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
547 vsnprintf(buf, sizeof(buf), fmt, args);
550 print_page_info(page);
554 static void init_object(struct kmem_cache *s, void *object, int active)
558 if (s->flags & __OBJECT_POISON) {
559 memset(p, POISON_FREE, s->objsize - 1);
560 p[s->objsize - 1] = POISON_END;
563 if (s->flags & SLAB_RED_ZONE)
564 memset(p + s->objsize,
565 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
566 s->inuse - s->objsize);
569 static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
572 if (*start != (u8)value)
580 static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
581 void *from, void *to)
583 slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
584 memset(from, data, to - from);
587 static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
588 u8 *object, char *what,
589 u8 *start, unsigned int value, unsigned int bytes)
594 fault = check_bytes(start, value, bytes);
599 while (end > fault && end[-1] == value)
602 slab_bug(s, "%s overwritten", what);
603 printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
604 fault, end - 1, fault[0], value);
605 print_trailer(s, page, object);
607 restore_bytes(s, what, value, fault, end);
615 * Bytes of the object to be managed.
616 * If the freepointer may overlay the object then the free
617 * pointer is the first word of the object.
619 * Poisoning uses 0x6b (POISON_FREE) and the last byte is
622 * object + s->objsize
623 * Padding to reach word boundary. This is also used for Redzoning.
624 * Padding is extended by another word if Redzoning is enabled and
627 * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
628 * 0xcc (RED_ACTIVE) for objects in use.
631 * Meta data starts here.
633 * A. Free pointer (if we cannot overwrite object on free)
634 * B. Tracking data for SLAB_STORE_USER
635 * C. Padding to reach required alignment boundary or at mininum
636 * one word if debuggin is on to be able to detect writes
637 * before the word boundary.
639 * Padding is done using 0x5a (POISON_INUSE)
642 * Nothing is used beyond s->size.
644 * If slabcaches are merged then the objsize and inuse boundaries are mostly
645 * ignored. And therefore no slab options that rely on these boundaries
646 * may be used with merged slabcaches.
649 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
651 unsigned long off = s->inuse; /* The end of info */
654 /* Freepointer is placed after the object. */
655 off += sizeof(void *);
657 if (s->flags & SLAB_STORE_USER)
658 /* We also have user information there */
659 off += 2 * sizeof(struct track);
664 return check_bytes_and_report(s, page, p, "Object padding",
665 p + off, POISON_INUSE, s->size - off);
668 static int slab_pad_check(struct kmem_cache *s, struct page *page)
676 if (!(s->flags & SLAB_POISON))
679 start = slab_address(page);
680 end = start + (PAGE_SIZE << s->order);
681 length = s->objects * s->size;
682 remainder = end - (start + length);
686 fault = check_bytes(start + length, POISON_INUSE, remainder);
689 while (end > fault && end[-1] == POISON_INUSE)
692 slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
693 print_section("Padding", start, length);
695 restore_bytes(s, "slab padding", POISON_INUSE, start, end);
699 static int check_object(struct kmem_cache *s, struct page *page,
700 void *object, int active)
703 u8 *endobject = object + s->objsize;
705 if (s->flags & SLAB_RED_ZONE) {
707 active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
709 if (!check_bytes_and_report(s, page, object, "Redzone",
710 endobject, red, s->inuse - s->objsize))
713 if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
714 check_bytes_and_report(s, page, p, "Alignment padding", endobject,
715 POISON_INUSE, s->inuse - s->objsize);
718 if (s->flags & SLAB_POISON) {
719 if (!active && (s->flags & __OBJECT_POISON) &&
720 (!check_bytes_and_report(s, page, p, "Poison", p,
721 POISON_FREE, s->objsize - 1) ||
722 !check_bytes_and_report(s, page, p, "Poison",
723 p + s->objsize - 1, POISON_END, 1)))
726 * check_pad_bytes cleans up on its own.
728 check_pad_bytes(s, page, p);
731 if (!s->offset && active)
733 * Object and freepointer overlap. Cannot check
734 * freepointer while object is allocated.
738 /* Check free pointer validity */
739 if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
740 object_err(s, page, p, "Freepointer corrupt");
742 * No choice but to zap it and thus loose the remainder
743 * of the free objects in this slab. May cause
744 * another error because the object count is now wrong.
746 set_freepointer(s, p, page->end);
752 static int check_slab(struct kmem_cache *s, struct page *page)
754 VM_BUG_ON(!irqs_disabled());
756 if (!PageSlab(page)) {
757 slab_err(s, page, "Not a valid slab page");
760 if (page->inuse > s->objects) {
761 slab_err(s, page, "inuse %u > max %u",
762 s->name, page->inuse, s->objects);
765 /* Slab_pad_check fixes things up after itself */
766 slab_pad_check(s, page);
771 * Determine if a certain object on a page is on the freelist. Must hold the
772 * slab lock to guarantee that the chains are in a consistent state.
774 static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
777 void *fp = page->freelist;
780 while (fp != page->end && nr <= s->objects) {
783 if (!check_valid_pointer(s, page, fp)) {
785 object_err(s, page, object,
786 "Freechain corrupt");
787 set_freepointer(s, object, page->end);
790 slab_err(s, page, "Freepointer corrupt");
791 page->freelist = page->end;
792 page->inuse = s->objects;
793 slab_fix(s, "Freelist cleared");
799 fp = get_freepointer(s, object);
803 if (page->inuse != s->objects - nr) {
804 slab_err(s, page, "Wrong object count. Counter is %d but "
805 "counted were %d", page->inuse, s->objects - nr);
806 page->inuse = s->objects - nr;
807 slab_fix(s, "Object count adjusted.");
809 return search == NULL;
812 static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
814 if (s->flags & SLAB_TRACE) {
815 printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
817 alloc ? "alloc" : "free",
822 print_section("Object", (void *)object, s->objsize);
829 * Tracking of fully allocated slabs for debugging purposes.
831 static void add_full(struct kmem_cache_node *n, struct page *page)
833 spin_lock(&n->list_lock);
834 list_add(&page->lru, &n->full);
835 spin_unlock(&n->list_lock);
838 static void remove_full(struct kmem_cache *s, struct page *page)
840 struct kmem_cache_node *n;
842 if (!(s->flags & SLAB_STORE_USER))
845 n = get_node(s, page_to_nid(page));
847 spin_lock(&n->list_lock);
848 list_del(&page->lru);
849 spin_unlock(&n->list_lock);
852 static void setup_object_debug(struct kmem_cache *s, struct page *page,
855 if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
858 init_object(s, object, 0);
859 init_tracking(s, object);
862 static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
863 void *object, void *addr)
865 if (!check_slab(s, page))
868 if (object && !on_freelist(s, page, object)) {
869 object_err(s, page, object, "Object already allocated");
873 if (!check_valid_pointer(s, page, object)) {
874 object_err(s, page, object, "Freelist Pointer check fails");
878 if (object && !check_object(s, page, object, 0))
881 /* Success perform special debug activities for allocs */
882 if (s->flags & SLAB_STORE_USER)
883 set_track(s, object, TRACK_ALLOC, addr);
884 trace(s, page, object, 1);
885 init_object(s, object, 1);
889 if (PageSlab(page)) {
891 * If this is a slab page then lets do the best we can
892 * to avoid issues in the future. Marking all objects
893 * as used avoids touching the remaining objects.
895 slab_fix(s, "Marking all objects used");
896 page->inuse = s->objects;
897 page->freelist = page->end;
902 static int free_debug_processing(struct kmem_cache *s, struct page *page,
903 void *object, void *addr)
905 if (!check_slab(s, page))
908 if (!check_valid_pointer(s, page, object)) {
909 slab_err(s, page, "Invalid object pointer 0x%p", object);
913 if (on_freelist(s, page, object)) {
914 object_err(s, page, object, "Object already free");
918 if (!check_object(s, page, object, 1))
921 if (unlikely(s != page->slab)) {
923 slab_err(s, page, "Attempt to free object(0x%p) "
924 "outside of slab", object);
928 "SLUB <none>: no slab for object 0x%p.\n",
932 object_err(s, page, object,
933 "page slab pointer corrupt.");
937 /* Special debug activities for freeing objects */
938 if (!SlabFrozen(page) && page->freelist == page->end)
939 remove_full(s, page);
940 if (s->flags & SLAB_STORE_USER)
941 set_track(s, object, TRACK_FREE, addr);
942 trace(s, page, object, 0);
943 init_object(s, object, 0);
947 slab_fix(s, "Object at 0x%p not freed", object);
951 static int __init setup_slub_debug(char *str)
953 slub_debug = DEBUG_DEFAULT_FLAGS;
954 if (*str++ != '=' || !*str)
956 * No options specified. Switch on full debugging.
962 * No options but restriction on slabs. This means full
963 * debugging for slabs matching a pattern.
970 * Switch off all debugging measures.
975 * Determine which debug features should be switched on
977 for (; *str && *str != ','; str++) {
978 switch (tolower(*str)) {
980 slub_debug |= SLAB_DEBUG_FREE;
983 slub_debug |= SLAB_RED_ZONE;
986 slub_debug |= SLAB_POISON;
989 slub_debug |= SLAB_STORE_USER;
992 slub_debug |= SLAB_TRACE;
995 printk(KERN_ERR "slub_debug option '%c' "
996 "unknown. skipped\n", *str);
1002 slub_debug_slabs = str + 1;
1007 __setup("slub_debug", setup_slub_debug);
1009 static unsigned long kmem_cache_flags(unsigned long objsize,
1010 unsigned long flags, const char *name,
1011 void (*ctor)(struct kmem_cache *, void *))
1014 * The page->offset field is only 16 bit wide. This is an offset
1015 * in units of words from the beginning of an object. If the slab
1016 * size is bigger then we cannot move the free pointer behind the
1019 * On 32 bit platforms the limit is 256k. On 64bit platforms
1020 * the limit is 512k.
1022 * Debugging or ctor may create a need to move the free
1023 * pointer. Fail if this happens.
1025 if (objsize >= 65535 * sizeof(void *)) {
1026 BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
1027 SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
1031 * Enable debugging if selected on the kernel commandline.
1033 if (slub_debug && (!slub_debug_slabs ||
1034 strncmp(slub_debug_slabs, name,
1035 strlen(slub_debug_slabs)) == 0))
1036 flags |= slub_debug;
1042 static inline void setup_object_debug(struct kmem_cache *s,
1043 struct page *page, void *object) {}
1045 static inline int alloc_debug_processing(struct kmem_cache *s,
1046 struct page *page, void *object, void *addr) { return 0; }
1048 static inline int free_debug_processing(struct kmem_cache *s,
1049 struct page *page, void *object, void *addr) { return 0; }
1051 static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
1053 static inline int check_object(struct kmem_cache *s, struct page *page,
1054 void *object, int active) { return 1; }
1055 static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
1056 static inline unsigned long kmem_cache_flags(unsigned long objsize,
1057 unsigned long flags, const char *name,
1058 void (*ctor)(struct kmem_cache *, void *))
1062 #define slub_debug 0
1065 * Slab allocation and freeing
1067 static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1070 int pages = 1 << s->order;
1073 flags |= __GFP_COMP;
1075 if (s->flags & SLAB_CACHE_DMA)
1078 if (s->flags & SLAB_RECLAIM_ACCOUNT)
1079 flags |= __GFP_RECLAIMABLE;
1082 page = alloc_pages(flags, s->order);
1084 page = alloc_pages_node(node, flags, s->order);
1089 mod_zone_page_state(page_zone(page),
1090 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1091 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1097 static void setup_object(struct kmem_cache *s, struct page *page,
1100 setup_object_debug(s, page, object);
1101 if (unlikely(s->ctor))
1105 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
1108 struct kmem_cache_node *n;
1113 BUG_ON(flags & GFP_SLAB_BUG_MASK);
1115 page = allocate_slab(s,
1116 flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
1120 n = get_node(s, page_to_nid(page));
1122 atomic_long_inc(&n->nr_slabs);
1124 page->flags |= 1 << PG_slab;
1125 if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
1126 SLAB_STORE_USER | SLAB_TRACE))
1129 start = page_address(page);
1130 page->end = start + 1;
1132 if (unlikely(s->flags & SLAB_POISON))
1133 memset(start, POISON_INUSE, PAGE_SIZE << s->order);
1136 for_each_object(p, s, start) {
1137 setup_object(s, page, last);
1138 set_freepointer(s, last, p);
1141 setup_object(s, page, last);
1142 set_freepointer(s, last, page->end);
1144 page->freelist = start;
1150 static void __free_slab(struct kmem_cache *s, struct page *page)
1152 int pages = 1 << s->order;
1154 if (unlikely(SlabDebug(page))) {
1157 slab_pad_check(s, page);
1158 for_each_object(p, s, slab_address(page))
1159 check_object(s, page, p, 0);
1160 ClearSlabDebug(page);
1163 mod_zone_page_state(page_zone(page),
1164 (s->flags & SLAB_RECLAIM_ACCOUNT) ?
1165 NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
1168 page->mapping = NULL;
1169 __free_pages(page, s->order);
1172 static void rcu_free_slab(struct rcu_head *h)
1176 page = container_of((struct list_head *)h, struct page, lru);
1177 __free_slab(page->slab, page);
1180 static void free_slab(struct kmem_cache *s, struct page *page)
1182 if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
1184 * RCU free overloads the RCU head over the LRU
1186 struct rcu_head *head = (void *)&page->lru;
1188 call_rcu(head, rcu_free_slab);
1190 __free_slab(s, page);
1193 static void discard_slab(struct kmem_cache *s, struct page *page)
1195 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1197 atomic_long_dec(&n->nr_slabs);
1198 reset_page_mapcount(page);
1199 __ClearPageSlab(page);
1204 * Per slab locking using the pagelock
1206 static __always_inline void slab_lock(struct page *page)
1208 bit_spin_lock(PG_locked, &page->flags);
1211 static __always_inline void slab_unlock(struct page *page)
1213 bit_spin_unlock(PG_locked, &page->flags);
1216 static __always_inline int slab_trylock(struct page *page)
1220 rc = bit_spin_trylock(PG_locked, &page->flags);
1225 * Management of partially allocated slabs
1227 static void add_partial(struct kmem_cache_node *n,
1228 struct page *page, int tail)
1230 spin_lock(&n->list_lock);
1233 list_add_tail(&page->lru, &n->partial);
1235 list_add(&page->lru, &n->partial);
1236 spin_unlock(&n->list_lock);
1239 static void remove_partial(struct kmem_cache *s,
1242 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1244 spin_lock(&n->list_lock);
1245 list_del(&page->lru);
1247 spin_unlock(&n->list_lock);
1251 * Lock slab and remove from the partial list.
1253 * Must hold list_lock.
1255 static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
1257 if (slab_trylock(page)) {
1258 list_del(&page->lru);
1260 SetSlabFrozen(page);
1267 * Try to allocate a partial slab from a specific node.
1269 static struct page *get_partial_node(struct kmem_cache_node *n)
1274 * Racy check. If we mistakenly see no partial slabs then we
1275 * just allocate an empty slab. If we mistakenly try to get a
1276 * partial slab and there is none available then get_partials()
1279 if (!n || !n->nr_partial)
1282 spin_lock(&n->list_lock);
1283 list_for_each_entry(page, &n->partial, lru)
1284 if (lock_and_freeze_slab(n, page))
1288 spin_unlock(&n->list_lock);
1293 * Get a page from somewhere. Search in increasing NUMA distances.
1295 static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1298 struct zonelist *zonelist;
1303 * The defrag ratio allows a configuration of the tradeoffs between
1304 * inter node defragmentation and node local allocations. A lower
1305 * defrag_ratio increases the tendency to do local allocations
1306 * instead of attempting to obtain partial slabs from other nodes.
1308 * If the defrag_ratio is set to 0 then kmalloc() always
1309 * returns node local objects. If the ratio is higher then kmalloc()
1310 * may return off node objects because partial slabs are obtained
1311 * from other nodes and filled up.
1313 * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
1314 * defrag_ratio = 1000) then every (well almost) allocation will
1315 * first attempt to defrag slab caches on other nodes. This means
1316 * scanning over all nodes to look for partial slabs which may be
1317 * expensive if we do it every time we are trying to find a slab
1318 * with available objects.
1320 if (!s->remote_node_defrag_ratio ||
1321 get_cycles() % 1024 > s->remote_node_defrag_ratio)
1324 zonelist = &NODE_DATA(slab_node(current->mempolicy))
1325 ->node_zonelists[gfp_zone(flags)];
1326 for (z = zonelist->zones; *z; z++) {
1327 struct kmem_cache_node *n;
1329 n = get_node(s, zone_to_nid(*z));
1331 if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
1332 n->nr_partial > MIN_PARTIAL) {
1333 page = get_partial_node(n);
1343 * Get a partial page, lock it and return it.
1345 static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1348 int searchnode = (node == -1) ? numa_node_id() : node;
1350 page = get_partial_node(get_node(s, searchnode));
1351 if (page || (flags & __GFP_THISNODE))
1354 return get_any_partial(s, flags);
1358 * Move a page back to the lists.
1360 * Must be called with the slab lock held.
1362 * On exit the slab lock will have been dropped.
1364 static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1366 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1368 ClearSlabFrozen(page);
1371 if (page->freelist != page->end)
1372 add_partial(n, page, tail);
1373 else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
1378 if (n->nr_partial < MIN_PARTIAL) {
1380 * Adding an empty slab to the partial slabs in order
1381 * to avoid page allocator overhead. This slab needs
1382 * to come after the other slabs with objects in
1383 * order to fill them up. That way the size of the
1384 * partial list stays small. kmem_cache_shrink can
1385 * reclaim empty slabs from the partial list.
1387 add_partial(n, page, 1);
1391 discard_slab(s, page);
1397 * Remove the cpu slab
1399 static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1401 struct page *page = c->page;
1404 * Merge cpu freelist into freelist. Typically we get here
1405 * because both freelists are empty. So this is unlikely
1408 * We need to use _is_end here because deactivate slab may
1409 * be called for a debug slab. Then c->freelist may contain
1412 while (unlikely(!is_end(c->freelist))) {
1415 tail = 0; /* Hot objects. Put the slab first */
1417 /* Retrieve object from cpu_freelist */
1418 object = c->freelist;
1419 c->freelist = c->freelist[c->offset];
1421 /* And put onto the regular freelist */
1422 object[c->offset] = page->freelist;
1423 page->freelist = object;
1427 unfreeze_slab(s, page, tail);
1430 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1433 deactivate_slab(s, c);
1438 * Called from IPI handler with interrupts disabled.
1440 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1442 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
1444 if (likely(c && c->page))
1448 static void flush_cpu_slab(void *d)
1450 struct kmem_cache *s = d;
1452 __flush_cpu_slab(s, smp_processor_id());
1455 static void flush_all(struct kmem_cache *s)
1458 on_each_cpu(flush_cpu_slab, s, 1, 1);
1460 unsigned long flags;
1462 local_irq_save(flags);
1464 local_irq_restore(flags);
1469 * Check if the objects in a per cpu structure fit numa
1470 * locality expectations.
1472 static inline int node_match(struct kmem_cache_cpu *c, int node)
1475 if (node != -1 && c->node != node)
1482 * Slow path. The lockless freelist is empty or we need to perform
1485 * Interrupts are disabled.
1487 * Processing is still very fast if new objects have been freed to the
1488 * regular freelist. In that case we simply take over the regular freelist
1489 * as the lockless freelist and zap the regular freelist.
1491 * If that is not working then we fall back to the partial lists. We take the
1492 * first element of the freelist as the object to allocate now and move the
1493 * rest of the freelist to the lockless freelist.
1495 * And if we were unable to get a new slab from the partial slab lists then
1496 * we need to allocate a new slab. This is slowest path since we may sleep.
1498 static void *__slab_alloc(struct kmem_cache *s,
1499 gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
1503 #ifdef SLUB_FASTPATH
1504 unsigned long flags;
1506 local_irq_save(flags);
1512 if (unlikely(!node_match(c, node)))
1515 object = c->page->freelist;
1516 if (unlikely(object == c->page->end))
1518 if (unlikely(SlabDebug(c->page)))
1521 object = c->page->freelist;
1522 c->freelist = object[c->offset];
1523 c->page->inuse = s->objects;
1524 c->page->freelist = c->page->end;
1525 c->node = page_to_nid(c->page);
1527 slab_unlock(c->page);
1529 #ifdef SLUB_FASTPATH
1530 local_irq_restore(flags);
1535 deactivate_slab(s, c);
1538 new = get_partial(s, gfpflags, node);
1544 if (gfpflags & __GFP_WAIT)
1547 new = new_slab(s, gfpflags, node);
1549 if (gfpflags & __GFP_WAIT)
1550 local_irq_disable();
1553 c = get_cpu_slab(s, smp_processor_id());
1564 object = c->page->freelist;
1565 if (!alloc_debug_processing(s, c->page, object, addr))
1569 c->page->freelist = object[c->offset];
1575 * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
1576 * have the fastpath folded into their functions. So no function call
1577 * overhead for requests that can be satisfied on the fastpath.
1579 * The fastpath works by first checking if the lockless freelist can be used.
1580 * If not then __slab_alloc is called for slow processing.
1582 * Otherwise we can simply pick the next object from the lockless free list.
1584 static __always_inline void *slab_alloc(struct kmem_cache *s,
1585 gfp_t gfpflags, int node, void *addr)
1588 struct kmem_cache_cpu *c;
1591 * The SLUB_FASTPATH path is provisional and is currently disabled if the
1592 * kernel is compiled with preemption or if the arch does not support
1593 * fast cmpxchg operations. There are a couple of coming changes that will
1594 * simplify matters and allow preemption. Ultimately we may end up making
1595 * SLUB_FASTPATH the default.
1597 * 1. The introduction of the per cpu allocator will avoid array lookups
1598 * through get_cpu_slab(). A special register can be used instead.
1600 * 2. The introduction of per cpu atomic operations (cpu_ops) means that
1601 * we can realize the logic here entirely with per cpu atomics. The
1602 * per cpu atomic ops will take care of the preemption issues.
1605 #ifdef SLUB_FASTPATH
1606 c = get_cpu_slab(s, raw_smp_processor_id());
1608 object = c->freelist;
1609 if (unlikely(is_end(object) || !node_match(c, node))) {
1610 object = __slab_alloc(s, gfpflags, node, addr, c);
1613 } while (cmpxchg_local(&c->freelist, object, object[c->offset])
1616 unsigned long flags;
1618 local_irq_save(flags);
1619 c = get_cpu_slab(s, smp_processor_id());
1620 if (unlikely(is_end(c->freelist) || !node_match(c, node)))
1622 object = __slab_alloc(s, gfpflags, node, addr, c);
1625 object = c->freelist;
1626 c->freelist = object[c->offset];
1628 local_irq_restore(flags);
1631 if (unlikely((gfpflags & __GFP_ZERO) && object))
1632 memset(object, 0, c->objsize);
1637 void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
1639 return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
1641 EXPORT_SYMBOL(kmem_cache_alloc);
1644 void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
1646 return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
1648 EXPORT_SYMBOL(kmem_cache_alloc_node);
1652 * Slow patch handling. This may still be called frequently since objects
1653 * have a longer lifetime than the cpu slabs in most processing loads.
1655 * So we still attempt to reduce cache line usage. Just take the slab
1656 * lock and free the item. If there is no additional partial page
1657 * handling required then we can return immediately.
1659 static void __slab_free(struct kmem_cache *s, struct page *page,
1660 void *x, void *addr, unsigned int offset)
1663 void **object = (void *)x;
1665 #ifdef SLUB_FASTPATH
1666 unsigned long flags;
1668 local_irq_save(flags);
1672 if (unlikely(SlabDebug(page)))
1675 prior = object[offset] = page->freelist;
1676 page->freelist = object;
1679 if (unlikely(SlabFrozen(page)))
1682 if (unlikely(!page->inuse))
1686 * Objects left in the slab. If it
1687 * was not on the partial list before
1690 if (unlikely(prior == page->end))
1691 add_partial(get_node(s, page_to_nid(page)), page, 1);
1695 #ifdef SLUB_FASTPATH
1696 local_irq_restore(flags);
1701 if (prior != page->end)
1703 * Slab still on the partial list.
1705 remove_partial(s, page);
1708 #ifdef SLUB_FASTPATH
1709 local_irq_restore(flags);
1711 discard_slab(s, page);
1715 if (!free_debug_processing(s, page, x, addr))
1721 * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
1722 * can perform fastpath freeing without additional function calls.
1724 * The fastpath is only possible if we are freeing to the current cpu slab
1725 * of this processor. This typically the case if we have just allocated
1728 * If fastpath is not possible then fall back to __slab_free where we deal
1729 * with all sorts of special processing.
1731 static __always_inline void slab_free(struct kmem_cache *s,
1732 struct page *page, void *x, void *addr)
1734 void **object = (void *)x;
1735 struct kmem_cache_cpu *c;
1737 #ifdef SLUB_FASTPATH
1740 c = get_cpu_slab(s, raw_smp_processor_id());
1741 debug_check_no_locks_freed(object, s->objsize);
1743 freelist = c->freelist;
1746 * If the compiler would reorder the retrieval of c->page to
1747 * come before c->freelist then an interrupt could
1748 * change the cpu slab before we retrieve c->freelist. We
1749 * could be matching on a page no longer active and put the
1750 * object onto the freelist of the wrong slab.
1752 * On the other hand: If we already have the freelist pointer
1753 * then any change of cpu_slab will cause the cmpxchg to fail
1754 * since the freelist pointers are unique per slab.
1756 if (unlikely(page != c->page || c->node < 0)) {
1757 __slab_free(s, page, x, addr, c->offset);
1760 object[c->offset] = freelist;
1761 } while (cmpxchg_local(&c->freelist, freelist, object) != freelist);
1763 unsigned long flags;
1765 local_irq_save(flags);
1766 debug_check_no_locks_freed(object, s->objsize);
1767 c = get_cpu_slab(s, smp_processor_id());
1768 if (likely(page == c->page && c->node >= 0)) {
1769 object[c->offset] = c->freelist;
1770 c->freelist = object;
1772 __slab_free(s, page, x, addr, c->offset);
1774 local_irq_restore(flags);
1778 void kmem_cache_free(struct kmem_cache *s, void *x)
1782 page = virt_to_head_page(x);
1784 slab_free(s, page, x, __builtin_return_address(0));
1786 EXPORT_SYMBOL(kmem_cache_free);
1788 /* Figure out on which slab object the object resides */
1789 static struct page *get_object_page(const void *x)
1791 struct page *page = virt_to_head_page(x);
1793 if (!PageSlab(page))
1800 * Object placement in a slab is made very easy because we always start at
1801 * offset 0. If we tune the size of the object to the alignment then we can
1802 * get the required alignment by putting one properly sized object after
1805 * Notice that the allocation order determines the sizes of the per cpu
1806 * caches. Each processor has always one slab available for allocations.
1807 * Increasing the allocation order reduces the number of times that slabs
1808 * must be moved on and off the partial lists and is therefore a factor in
1813 * Mininum / Maximum order of slab pages. This influences locking overhead
1814 * and slab fragmentation. A higher order reduces the number of partial slabs
1815 * and increases the number of allocations possible without having to
1816 * take the list_lock.
1818 static int slub_min_order;
1819 static int slub_max_order = DEFAULT_MAX_ORDER;
1820 static int slub_min_objects = DEFAULT_MIN_OBJECTS;
1823 * Merge control. If this is set then no merging of slab caches will occur.
1824 * (Could be removed. This was introduced to pacify the merge skeptics.)
1826 static int slub_nomerge;
1829 * Calculate the order of allocation given an slab object size.
1831 * The order of allocation has significant impact on performance and other
1832 * system components. Generally order 0 allocations should be preferred since
1833 * order 0 does not cause fragmentation in the page allocator. Larger objects
1834 * be problematic to put into order 0 slabs because there may be too much
1835 * unused space left. We go to a higher order if more than 1/8th of the slab
1838 * In order to reach satisfactory performance we must ensure that a minimum
1839 * number of objects is in one slab. Otherwise we may generate too much
1840 * activity on the partial lists which requires taking the list_lock. This is
1841 * less a concern for large slabs though which are rarely used.
1843 * slub_max_order specifies the order where we begin to stop considering the
1844 * number of objects in a slab as critical. If we reach slub_max_order then
1845 * we try to keep the page order as low as possible. So we accept more waste
1846 * of space in favor of a small page order.
1848 * Higher order allocations also allow the placement of more objects in a
1849 * slab and thereby reduce object handling overhead. If the user has
1850 * requested a higher mininum order then we start with that one instead of
1851 * the smallest order which will fit the object.
1853 static inline int slab_order(int size, int min_objects,
1854 int max_order, int fract_leftover)
1858 int min_order = slub_min_order;
1860 for (order = max(min_order,
1861 fls(min_objects * size - 1) - PAGE_SHIFT);
1862 order <= max_order; order++) {
1864 unsigned long slab_size = PAGE_SIZE << order;
1866 if (slab_size < min_objects * size)
1869 rem = slab_size % size;
1871 if (rem <= slab_size / fract_leftover)
1879 static inline int calculate_order(int size)
1886 * Attempt to find best configuration for a slab. This
1887 * works by first attempting to generate a layout with
1888 * the best configuration and backing off gradually.
1890 * First we reduce the acceptable waste in a slab. Then
1891 * we reduce the minimum objects required in a slab.
1893 min_objects = slub_min_objects;
1894 while (min_objects > 1) {
1896 while (fraction >= 4) {
1897 order = slab_order(size, min_objects,
1898 slub_max_order, fraction);
1899 if (order <= slub_max_order)
1907 * We were unable to place multiple objects in a slab. Now
1908 * lets see if we can place a single object there.
1910 order = slab_order(size, 1, slub_max_order, 1);
1911 if (order <= slub_max_order)
1915 * Doh this slab cannot be placed using slub_max_order.
1917 order = slab_order(size, 1, MAX_ORDER, 1);
1918 if (order <= MAX_ORDER)
1924 * Figure out what the alignment of the objects will be.
1926 static unsigned long calculate_alignment(unsigned long flags,
1927 unsigned long align, unsigned long size)
1930 * If the user wants hardware cache aligned objects then
1931 * follow that suggestion if the object is sufficiently
1934 * The hardware cache alignment cannot override the
1935 * specified alignment though. If that is greater
1938 if ((flags & SLAB_HWCACHE_ALIGN) &&
1939 size > cache_line_size() / 2)
1940 return max_t(unsigned long, align, cache_line_size());
1942 if (align < ARCH_SLAB_MINALIGN)
1943 return ARCH_SLAB_MINALIGN;
1945 return ALIGN(align, sizeof(void *));
1948 static void init_kmem_cache_cpu(struct kmem_cache *s,
1949 struct kmem_cache_cpu *c)
1952 c->freelist = (void *)PAGE_MAPPING_ANON;
1954 c->offset = s->offset / sizeof(void *);
1955 c->objsize = s->objsize;
1958 static void init_kmem_cache_node(struct kmem_cache_node *n)
1961 atomic_long_set(&n->nr_slabs, 0);
1962 spin_lock_init(&n->list_lock);
1963 INIT_LIST_HEAD(&n->partial);
1964 #ifdef CONFIG_SLUB_DEBUG
1965 INIT_LIST_HEAD(&n->full);
1971 * Per cpu array for per cpu structures.
1973 * The per cpu array places all kmem_cache_cpu structures from one processor
1974 * close together meaning that it becomes possible that multiple per cpu
1975 * structures are contained in one cacheline. This may be particularly
1976 * beneficial for the kmalloc caches.
1978 * A desktop system typically has around 60-80 slabs. With 100 here we are
1979 * likely able to get per cpu structures for all caches from the array defined
1980 * here. We must be able to cover all kmalloc caches during bootstrap.
1982 * If the per cpu array is exhausted then fall back to kmalloc
1983 * of individual cachelines. No sharing is possible then.
1985 #define NR_KMEM_CACHE_CPU 100
1987 static DEFINE_PER_CPU(struct kmem_cache_cpu,
1988 kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
1990 static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
1991 static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
1993 static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
1994 int cpu, gfp_t flags)
1996 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
1999 per_cpu(kmem_cache_cpu_free, cpu) =
2000 (void *)c->freelist;
2002 /* Table overflow: So allocate ourselves */
2004 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2005 flags, cpu_to_node(cpu));
2010 init_kmem_cache_cpu(s, c);
2014 static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2016 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2017 c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2021 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2022 per_cpu(kmem_cache_cpu_free, cpu) = c;
2025 static void free_kmem_cache_cpus(struct kmem_cache *s)
2029 for_each_online_cpu(cpu) {
2030 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2033 s->cpu_slab[cpu] = NULL;
2034 free_kmem_cache_cpu(c, cpu);
2039 static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2043 for_each_online_cpu(cpu) {
2044 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2049 c = alloc_kmem_cache_cpu(s, cpu, flags);
2051 free_kmem_cache_cpus(s);
2054 s->cpu_slab[cpu] = c;
2060 * Initialize the per cpu array.
2062 static void init_alloc_cpu_cpu(int cpu)
2066 if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
2069 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2070 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2072 cpu_set(cpu, kmem_cach_cpu_free_init_once);
2075 static void __init init_alloc_cpu(void)
2079 for_each_online_cpu(cpu)
2080 init_alloc_cpu_cpu(cpu);
2084 static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
2085 static inline void init_alloc_cpu(void) {}
2087 static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2089 init_kmem_cache_cpu(s, &s->cpu_slab);
2096 * No kmalloc_node yet so do it by hand. We know that this is the first
2097 * slab on the node for this slabcache. There are no concurrent accesses
2100 * Note that this function only works on the kmalloc_node_cache
2101 * when allocating for the kmalloc_node_cache. This is used for bootstrapping
2102 * memory on a fresh node that has no slab structures yet.
2104 static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
2108 struct kmem_cache_node *n;
2109 unsigned long flags;
2111 BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
2113 page = new_slab(kmalloc_caches, gfpflags, node);
2116 if (page_to_nid(page) != node) {
2117 printk(KERN_ERR "SLUB: Unable to allocate memory from "
2119 printk(KERN_ERR "SLUB: Allocating a useless per node structure "
2120 "in order to be able to continue\n");
2125 page->freelist = get_freepointer(kmalloc_caches, n);
2127 kmalloc_caches->node[node] = n;
2128 #ifdef CONFIG_SLUB_DEBUG
2129 init_object(kmalloc_caches, n, 1);
2130 init_tracking(kmalloc_caches, n);
2132 init_kmem_cache_node(n);
2133 atomic_long_inc(&n->nr_slabs);
2135 * lockdep requires consistent irq usage for each lock
2136 * so even though there cannot be a race this early in
2137 * the boot sequence, we still disable irqs.
2139 local_irq_save(flags);
2140 add_partial(n, page, 0);
2141 local_irq_restore(flags);
2145 static void free_kmem_cache_nodes(struct kmem_cache *s)
2149 for_each_node_state(node, N_NORMAL_MEMORY) {
2150 struct kmem_cache_node *n = s->node[node];
2151 if (n && n != &s->local_node)
2152 kmem_cache_free(kmalloc_caches, n);
2153 s->node[node] = NULL;
2157 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2162 if (slab_state >= UP)
2163 local_node = page_to_nid(virt_to_page(s));
2167 for_each_node_state(node, N_NORMAL_MEMORY) {
2168 struct kmem_cache_node *n;
2170 if (local_node == node)
2173 if (slab_state == DOWN) {
2174 n = early_kmem_cache_node_alloc(gfpflags,
2178 n = kmem_cache_alloc_node(kmalloc_caches,
2182 free_kmem_cache_nodes(s);
2188 init_kmem_cache_node(n);
2193 static void free_kmem_cache_nodes(struct kmem_cache *s)
2197 static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2199 init_kmem_cache_node(&s->local_node);
2205 * calculate_sizes() determines the order and the distribution of data within
2208 static int calculate_sizes(struct kmem_cache *s)
2210 unsigned long flags = s->flags;
2211 unsigned long size = s->objsize;
2212 unsigned long align = s->align;
2215 * Determine if we can poison the object itself. If the user of
2216 * the slab may touch the object after free or before allocation
2217 * then we should never poison the object itself.
2219 if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
2221 s->flags |= __OBJECT_POISON;
2223 s->flags &= ~__OBJECT_POISON;
2226 * Round up object size to the next word boundary. We can only
2227 * place the free pointer at word boundaries and this determines
2228 * the possible location of the free pointer.
2230 size = ALIGN(size, sizeof(void *));
2232 #ifdef CONFIG_SLUB_DEBUG
2234 * If we are Redzoning then check if there is some space between the
2235 * end of the object and the free pointer. If not then add an
2236 * additional word to have some bytes to store Redzone information.
2238 if ((flags & SLAB_RED_ZONE) && size == s->objsize)
2239 size += sizeof(void *);
2243 * With that we have determined the number of bytes in actual use
2244 * by the object. This is the potential offset to the free pointer.
2248 if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
2251 * Relocate free pointer after the object if it is not
2252 * permitted to overwrite the first word of the object on
2255 * This is the case if we do RCU, have a constructor or
2256 * destructor or are poisoning the objects.
2259 size += sizeof(void *);
2262 #ifdef CONFIG_SLUB_DEBUG
2263 if (flags & SLAB_STORE_USER)
2265 * Need to store information about allocs and frees after
2268 size += 2 * sizeof(struct track);
2270 if (flags & SLAB_RED_ZONE)
2272 * Add some empty padding so that we can catch
2273 * overwrites from earlier objects rather than let
2274 * tracking information or the free pointer be
2275 * corrupted if an user writes before the start
2278 size += sizeof(void *);
2282 * Determine the alignment based on various parameters that the
2283 * user specified and the dynamic determination of cache line size
2286 align = calculate_alignment(flags, align, s->objsize);
2289 * SLUB stores one object immediately after another beginning from
2290 * offset 0. In order to align the objects we have to simply size
2291 * each object to conform to the alignment.
2293 size = ALIGN(size, align);
2296 s->order = calculate_order(size);
2301 * Determine the number of objects per slab
2303 s->objects = (PAGE_SIZE << s->order) / size;
2305 return !!s->objects;
2309 static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2310 const char *name, size_t size,
2311 size_t align, unsigned long flags,
2312 void (*ctor)(struct kmem_cache *, void *))
2314 memset(s, 0, kmem_size);
2319 s->flags = kmem_cache_flags(size, flags, name, ctor);
2321 if (!calculate_sizes(s))
2326 s->remote_node_defrag_ratio = 100;
2328 if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
2331 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2333 free_kmem_cache_nodes(s);
2335 if (flags & SLAB_PANIC)
2336 panic("Cannot create slab %s size=%lu realsize=%u "
2337 "order=%u offset=%u flags=%lx\n",
2338 s->name, (unsigned long)size, s->size, s->order,
2344 * Check if a given pointer is valid
2346 int kmem_ptr_validate(struct kmem_cache *s, const void *object)
2350 page = get_object_page(object);
2352 if (!page || s != page->slab)
2353 /* No slab or wrong slab */
2356 if (!check_valid_pointer(s, page, object))
2360 * We could also check if the object is on the slabs freelist.
2361 * But this would be too expensive and it seems that the main
2362 * purpose of kmem_ptr_valid is to check if the object belongs
2363 * to a certain slab.
2367 EXPORT_SYMBOL(kmem_ptr_validate);
2370 * Determine the size of a slab object
2372 unsigned int kmem_cache_size(struct kmem_cache *s)
2376 EXPORT_SYMBOL(kmem_cache_size);
2378 const char *kmem_cache_name(struct kmem_cache *s)
2382 EXPORT_SYMBOL(kmem_cache_name);
2385 * Attempt to free all slabs on a node. Return the number of slabs we
2386 * were unable to free.
2388 static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
2389 struct list_head *list)
2391 int slabs_inuse = 0;
2392 unsigned long flags;
2393 struct page *page, *h;
2395 spin_lock_irqsave(&n->list_lock, flags);
2396 list_for_each_entry_safe(page, h, list, lru)
2398 list_del(&page->lru);
2399 discard_slab(s, page);
2402 spin_unlock_irqrestore(&n->list_lock, flags);
2407 * Release all resources used by a slab cache.
2409 static inline int kmem_cache_close(struct kmem_cache *s)
2415 /* Attempt to free all objects */
2416 free_kmem_cache_cpus(s);
2417 for_each_node_state(node, N_NORMAL_MEMORY) {
2418 struct kmem_cache_node *n = get_node(s, node);
2420 n->nr_partial -= free_list(s, n, &n->partial);
2421 if (atomic_long_read(&n->nr_slabs))
2424 free_kmem_cache_nodes(s);
2429 * Close a cache and release the kmem_cache structure
2430 * (must be used for caches created using kmem_cache_create)
2432 void kmem_cache_destroy(struct kmem_cache *s)
2434 down_write(&slub_lock);
2438 up_write(&slub_lock);
2439 if (kmem_cache_close(s))
2441 sysfs_slab_remove(s);
2443 up_write(&slub_lock);
2445 EXPORT_SYMBOL(kmem_cache_destroy);
2447 /********************************************************************
2449 *******************************************************************/
2451 struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
2452 EXPORT_SYMBOL(kmalloc_caches);
2454 #ifdef CONFIG_ZONE_DMA
2455 static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
2458 static int __init setup_slub_min_order(char *str)
2460 get_option(&str, &slub_min_order);
2465 __setup("slub_min_order=", setup_slub_min_order);
2467 static int __init setup_slub_max_order(char *str)
2469 get_option(&str, &slub_max_order);
2474 __setup("slub_max_order=", setup_slub_max_order);
2476 static int __init setup_slub_min_objects(char *str)
2478 get_option(&str, &slub_min_objects);
2483 __setup("slub_min_objects=", setup_slub_min_objects);
2485 static int __init setup_slub_nomerge(char *str)
2491 __setup("slub_nomerge", setup_slub_nomerge);
2493 static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
2494 const char *name, int size, gfp_t gfp_flags)
2496 unsigned int flags = 0;
2498 if (gfp_flags & SLUB_DMA)
2499 flags = SLAB_CACHE_DMA;
2501 down_write(&slub_lock);
2502 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2506 list_add(&s->list, &slab_caches);
2507 up_write(&slub_lock);
2508 if (sysfs_slab_add(s))
2513 panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
2516 #ifdef CONFIG_ZONE_DMA
2518 static void sysfs_add_func(struct work_struct *w)
2520 struct kmem_cache *s;
2522 down_write(&slub_lock);
2523 list_for_each_entry(s, &slab_caches, list) {
2524 if (s->flags & __SYSFS_ADD_DEFERRED) {
2525 s->flags &= ~__SYSFS_ADD_DEFERRED;
2529 up_write(&slub_lock);
2532 static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
2534 static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2536 struct kmem_cache *s;
2540 s = kmalloc_caches_dma[index];
2544 /* Dynamically create dma cache */
2545 if (flags & __GFP_WAIT)
2546 down_write(&slub_lock);
2548 if (!down_write_trylock(&slub_lock))
2552 if (kmalloc_caches_dma[index])
2555 realsize = kmalloc_caches[index].objsize;
2556 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
2557 s = kmalloc(kmem_size, flags & ~SLUB_DMA);
2559 if (!s || !text || !kmem_cache_open(s, flags, text,
2560 realsize, ARCH_KMALLOC_MINALIGN,
2561 SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
2567 list_add(&s->list, &slab_caches);
2568 kmalloc_caches_dma[index] = s;
2570 schedule_work(&sysfs_add_work);
2573 up_write(&slub_lock);
2575 return kmalloc_caches_dma[index];
2580 * Conversion table for small slabs sizes / 8 to the index in the
2581 * kmalloc array. This is necessary for slabs < 192 since we have non power
2582 * of two cache sizes there. The size of larger slabs can be determined using
2585 static s8 size_index[24] = {
2612 static struct kmem_cache *get_slab(size_t size, gfp_t flags)
2618 return ZERO_SIZE_PTR;
2620 index = size_index[(size - 1) / 8];
2622 index = fls(size - 1);
2624 #ifdef CONFIG_ZONE_DMA
2625 if (unlikely((flags & SLUB_DMA)))
2626 return dma_kmalloc_cache(index, flags);
2629 return &kmalloc_caches[index];
2632 void *__kmalloc(size_t size, gfp_t flags)
2634 struct kmem_cache *s;
2636 if (unlikely(size > PAGE_SIZE / 2))
2637 return (void *)__get_free_pages(flags | __GFP_COMP,
2640 s = get_slab(size, flags);
2642 if (unlikely(ZERO_OR_NULL_PTR(s)))
2645 return slab_alloc(s, flags, -1, __builtin_return_address(0));
2647 EXPORT_SYMBOL(__kmalloc);
2650 void *__kmalloc_node(size_t size, gfp_t flags, int node)
2652 struct kmem_cache *s;
2654 if (unlikely(size > PAGE_SIZE / 2))
2655 return (void *)__get_free_pages(flags | __GFP_COMP,
2658 s = get_slab(size, flags);
2660 if (unlikely(ZERO_OR_NULL_PTR(s)))
2663 return slab_alloc(s, flags, node, __builtin_return_address(0));
2665 EXPORT_SYMBOL(__kmalloc_node);
2668 size_t ksize(const void *object)
2671 struct kmem_cache *s;
2674 if (unlikely(object == ZERO_SIZE_PTR))
2677 page = virt_to_head_page(object);
2680 if (unlikely(!PageSlab(page)))
2681 return PAGE_SIZE << compound_order(page);
2687 * Debugging requires use of the padding between object
2688 * and whatever may come after it.
2690 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
2694 * If we have the need to store the freelist pointer
2695 * back there or track user information then we can
2696 * only use the space before that information.
2698 if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
2702 * Else we can use all the padding etc for the allocation
2706 EXPORT_SYMBOL(ksize);
2708 void kfree(const void *x)
2711 void *object = (void *)x;
2713 if (unlikely(ZERO_OR_NULL_PTR(x)))
2716 page = virt_to_head_page(x);
2717 if (unlikely(!PageSlab(page))) {
2721 slab_free(page->slab, page, object, __builtin_return_address(0));
2723 EXPORT_SYMBOL(kfree);
2725 static unsigned long count_partial(struct kmem_cache_node *n)
2727 unsigned long flags;
2728 unsigned long x = 0;
2731 spin_lock_irqsave(&n->list_lock, flags);
2732 list_for_each_entry(page, &n->partial, lru)
2734 spin_unlock_irqrestore(&n->list_lock, flags);
2739 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
2740 * the remaining slabs by the number of items in use. The slabs with the
2741 * most items in use come first. New allocations will then fill those up
2742 * and thus they can be removed from the partial lists.
2744 * The slabs with the least items are placed last. This results in them
2745 * being allocated from last increasing the chance that the last objects
2746 * are freed in them.
2748 int kmem_cache_shrink(struct kmem_cache *s)
2752 struct kmem_cache_node *n;
2755 struct list_head *slabs_by_inuse =
2756 kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
2757 unsigned long flags;
2759 if (!slabs_by_inuse)
2763 for_each_node_state(node, N_NORMAL_MEMORY) {
2764 n = get_node(s, node);
2769 for (i = 0; i < s->objects; i++)
2770 INIT_LIST_HEAD(slabs_by_inuse + i);
2772 spin_lock_irqsave(&n->list_lock, flags);
2775 * Build lists indexed by the items in use in each slab.
2777 * Note that concurrent frees may occur while we hold the
2778 * list_lock. page->inuse here is the upper limit.
2780 list_for_each_entry_safe(page, t, &n->partial, lru) {
2781 if (!page->inuse && slab_trylock(page)) {
2783 * Must hold slab lock here because slab_free
2784 * may have freed the last object and be
2785 * waiting to release the slab.
2787 list_del(&page->lru);
2790 discard_slab(s, page);
2792 list_move(&page->lru,
2793 slabs_by_inuse + page->inuse);
2798 * Rebuild the partial list with the slabs filled up most
2799 * first and the least used slabs at the end.
2801 for (i = s->objects - 1; i >= 0; i--)
2802 list_splice(slabs_by_inuse + i, n->partial.prev);
2804 spin_unlock_irqrestore(&n->list_lock, flags);
2807 kfree(slabs_by_inuse);
2810 EXPORT_SYMBOL(kmem_cache_shrink);
2812 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
2813 static int slab_mem_going_offline_callback(void *arg)
2815 struct kmem_cache *s;
2817 down_read(&slub_lock);
2818 list_for_each_entry(s, &slab_caches, list)
2819 kmem_cache_shrink(s);
2820 up_read(&slub_lock);
2825 static void slab_mem_offline_callback(void *arg)
2827 struct kmem_cache_node *n;
2828 struct kmem_cache *s;
2829 struct memory_notify *marg = arg;
2832 offline_node = marg->status_change_nid;
2835 * If the node still has available memory. we need kmem_cache_node
2838 if (offline_node < 0)
2841 down_read(&slub_lock);
2842 list_for_each_entry(s, &slab_caches, list) {
2843 n = get_node(s, offline_node);
2846 * if n->nr_slabs > 0, slabs still exist on the node
2847 * that is going down. We were unable to free them,
2848 * and offline_pages() function shoudn't call this
2849 * callback. So, we must fail.
2851 BUG_ON(atomic_long_read(&n->nr_slabs));
2853 s->node[offline_node] = NULL;
2854 kmem_cache_free(kmalloc_caches, n);
2857 up_read(&slub_lock);
2860 static int slab_mem_going_online_callback(void *arg)
2862 struct kmem_cache_node *n;
2863 struct kmem_cache *s;
2864 struct memory_notify *marg = arg;
2865 int nid = marg->status_change_nid;
2869 * If the node's memory is already available, then kmem_cache_node is
2870 * already created. Nothing to do.
2876 * We are bringing a node online. No memory is availabe yet. We must
2877 * allocate a kmem_cache_node structure in order to bring the node
2880 down_read(&slub_lock);
2881 list_for_each_entry(s, &slab_caches, list) {
2883 * XXX: kmem_cache_alloc_node will fallback to other nodes
2884 * since memory is not yet available from the node that
2887 n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
2892 init_kmem_cache_node(n);
2896 up_read(&slub_lock);
2900 static int slab_memory_callback(struct notifier_block *self,
2901 unsigned long action, void *arg)
2906 case MEM_GOING_ONLINE:
2907 ret = slab_mem_going_online_callback(arg);
2909 case MEM_GOING_OFFLINE:
2910 ret = slab_mem_going_offline_callback(arg);
2913 case MEM_CANCEL_ONLINE:
2914 slab_mem_offline_callback(arg);
2917 case MEM_CANCEL_OFFLINE:
2921 ret = notifier_from_errno(ret);
2925 #endif /* CONFIG_MEMORY_HOTPLUG */
2927 /********************************************************************
2928 * Basic setup of slabs
2929 *******************************************************************/
2931 void __init kmem_cache_init(void)
2940 * Must first have the slab cache available for the allocations of the
2941 * struct kmem_cache_node's. There is special bootstrap code in
2942 * kmem_cache_open for slab_state == DOWN.
2944 create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
2945 sizeof(struct kmem_cache_node), GFP_KERNEL);
2946 kmalloc_caches[0].refcount = -1;
2949 hotplug_memory_notifier(slab_memory_callback, 1);
2952 /* Able to allocate the per node structures */
2953 slab_state = PARTIAL;
2955 /* Caches that are not of the two-to-the-power-of size */
2956 if (KMALLOC_MIN_SIZE <= 64) {
2957 create_kmalloc_cache(&kmalloc_caches[1],
2958 "kmalloc-96", 96, GFP_KERNEL);
2961 if (KMALLOC_MIN_SIZE <= 128) {
2962 create_kmalloc_cache(&kmalloc_caches[2],
2963 "kmalloc-192", 192, GFP_KERNEL);
2967 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
2968 create_kmalloc_cache(&kmalloc_caches[i],
2969 "kmalloc", 1 << i, GFP_KERNEL);
2975 * Patch up the size_index table if we have strange large alignment
2976 * requirements for the kmalloc array. This is only the case for
2977 * mips it seems. The standard arches will not generate any code here.
2979 * Largest permitted alignment is 256 bytes due to the way we
2980 * handle the index determination for the smaller caches.
2982 * Make sure that nothing crazy happens if someone starts tinkering
2983 * around with ARCH_KMALLOC_MINALIGN
2985 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
2986 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
2988 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
2989 size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
2993 /* Provide the correct kmalloc names now that the caches are up */
2994 for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
2995 kmalloc_caches[i]. name =
2996 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
2999 register_cpu_notifier(&slab_notifier);
3000 kmem_size = offsetof(struct kmem_cache, cpu_slab) +
3001 nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
3003 kmem_size = sizeof(struct kmem_cache);
3007 printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
3008 " CPUs=%d, Nodes=%d\n",
3009 caches, cache_line_size(),
3010 slub_min_order, slub_max_order, slub_min_objects,
3011 nr_cpu_ids, nr_node_ids);
3015 * Find a mergeable slab cache
3017 static int slab_unmergeable(struct kmem_cache *s)
3019 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
3026 * We may have set a slab to be unmergeable during bootstrap.
3028 if (s->refcount < 0)
3034 static struct kmem_cache *find_mergeable(size_t size,
3035 size_t align, unsigned long flags, const char *name,
3036 void (*ctor)(struct kmem_cache *, void *))
3038 struct kmem_cache *s;
3040 if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
3046 size = ALIGN(size, sizeof(void *));
3047 align = calculate_alignment(flags, align, size);
3048 size = ALIGN(size, align);
3049 flags = kmem_cache_flags(size, flags, name, NULL);
3051 list_for_each_entry(s, &slab_caches, list) {
3052 if (slab_unmergeable(s))
3058 if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
3061 * Check if alignment is compatible.
3062 * Courtesy of Adrian Drzewiecki
3064 if ((s->size & ~(align - 1)) != s->size)
3067 if (s->size - size >= sizeof(void *))
3075 struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3076 size_t align, unsigned long flags,
3077 void (*ctor)(struct kmem_cache *, void *))
3079 struct kmem_cache *s;
3081 down_write(&slub_lock);
3082 s = find_mergeable(size, align, flags, name, ctor);
3088 * Adjust the object sizes so that we clear
3089 * the complete object on kzalloc.
3091 s->objsize = max(s->objsize, (int)size);
3094 * And then we need to update the object size in the
3095 * per cpu structures
3097 for_each_online_cpu(cpu)
3098 get_cpu_slab(s, cpu)->objsize = s->objsize;
3099 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3100 up_write(&slub_lock);
3101 if (sysfs_slab_alias(s, name))
3105 s = kmalloc(kmem_size, GFP_KERNEL);
3107 if (kmem_cache_open(s, GFP_KERNEL, name,
3108 size, align, flags, ctor)) {
3109 list_add(&s->list, &slab_caches);
3110 up_write(&slub_lock);
3111 if (sysfs_slab_add(s))
3117 up_write(&slub_lock);
3120 if (flags & SLAB_PANIC)
3121 panic("Cannot create slabcache %s\n", name);
3126 EXPORT_SYMBOL(kmem_cache_create);
3130 * Use the cpu notifier to insure that the cpu slabs are flushed when
3133 static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3134 unsigned long action, void *hcpu)
3136 long cpu = (long)hcpu;
3137 struct kmem_cache *s;
3138 unsigned long flags;
3141 case CPU_UP_PREPARE:
3142 case CPU_UP_PREPARE_FROZEN:
3143 init_alloc_cpu_cpu(cpu);
3144 down_read(&slub_lock);
3145 list_for_each_entry(s, &slab_caches, list)
3146 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3148 up_read(&slub_lock);
3151 case CPU_UP_CANCELED:
3152 case CPU_UP_CANCELED_FROZEN:
3154 case CPU_DEAD_FROZEN:
3155 down_read(&slub_lock);
3156 list_for_each_entry(s, &slab_caches, list) {
3157 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3159 local_irq_save(flags);
3160 __flush_cpu_slab(s, cpu);
3161 local_irq_restore(flags);
3162 free_kmem_cache_cpu(c, cpu);
3163 s->cpu_slab[cpu] = NULL;
3165 up_read(&slub_lock);
3173 static struct notifier_block __cpuinitdata slab_notifier = {
3174 &slab_cpuup_callback, NULL, 0
3179 void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
3181 struct kmem_cache *s;
3183 if (unlikely(size > PAGE_SIZE / 2))
3184 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
3186 s = get_slab(size, gfpflags);
3188 if (unlikely(ZERO_OR_NULL_PTR(s)))
3191 return slab_alloc(s, gfpflags, -1, caller);
3194 void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3195 int node, void *caller)
3197 struct kmem_cache *s;
3199 if (unlikely(size > PAGE_SIZE / 2))
3200 return (void *)__get_free_pages(gfpflags | __GFP_COMP,
3202 s = get_slab(size, gfpflags);
3204 if (unlikely(ZERO_OR_NULL_PTR(s)))
3207 return slab_alloc(s, gfpflags, node, caller);
3210 #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
3211 static int validate_slab(struct kmem_cache *s, struct page *page,
3215 void *addr = slab_address(page);
3217 if (!check_slab(s, page) ||
3218 !on_freelist(s, page, NULL))
3221 /* Now we know that a valid freelist exists */
3222 bitmap_zero(map, s->objects);
3224 for_each_free_object(p, s, page->freelist) {
3225 set_bit(slab_index(p, s, addr), map);
3226 if (!check_object(s, page, p, 0))
3230 for_each_object(p, s, addr)
3231 if (!test_bit(slab_index(p, s, addr), map))
3232 if (!check_object(s, page, p, 1))
3237 static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3240 if (slab_trylock(page)) {
3241 validate_slab(s, page, map);
3244 printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3247 if (s->flags & DEBUG_DEFAULT_FLAGS) {
3248 if (!SlabDebug(page))
3249 printk(KERN_ERR "SLUB %s: SlabDebug not set "
3250 "on slab 0x%p\n", s->name, page);
3252 if (SlabDebug(page))
3253 printk(KERN_ERR "SLUB %s: SlabDebug set on "
3254 "slab 0x%p\n", s->name, page);
3258 static int validate_slab_node(struct kmem_cache *s,
3259 struct kmem_cache_node *n, unsigned long *map)
3261 unsigned long count = 0;
3263 unsigned long flags;
3265 spin_lock_irqsave(&n->list_lock, flags);
3267 list_for_each_entry(page, &n->partial, lru) {
3268 validate_slab_slab(s, page, map);
3271 if (count != n->nr_partial)
3272 printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
3273 "counter=%ld\n", s->name, count, n->nr_partial);
3275 if (!(s->flags & SLAB_STORE_USER))
3278 list_for_each_entry(page, &n->full, lru) {
3279 validate_slab_slab(s, page, map);
3282 if (count != atomic_long_read(&n->nr_slabs))
3283 printk(KERN_ERR "SLUB: %s %ld slabs counted but "
3284 "counter=%ld\n", s->name, count,
3285 atomic_long_read(&n->nr_slabs));
3288 spin_unlock_irqrestore(&n->list_lock, flags);
3292 static long validate_slab_cache(struct kmem_cache *s)
3295 unsigned long count = 0;
3296 unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
3297 sizeof(unsigned long), GFP_KERNEL);
3303 for_each_node_state(node, N_NORMAL_MEMORY) {
3304 struct kmem_cache_node *n = get_node(s, node);
3306 count += validate_slab_node(s, n, map);
3312 #ifdef SLUB_RESILIENCY_TEST
3313 static void resiliency_test(void)
3317 printk(KERN_ERR "SLUB resiliency testing\n");
3318 printk(KERN_ERR "-----------------------\n");
3319 printk(KERN_ERR "A. Corruption after allocation\n");
3321 p = kzalloc(16, GFP_KERNEL);
3323 printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
3324 " 0x12->0x%p\n\n", p + 16);
3326 validate_slab_cache(kmalloc_caches + 4);
3328 /* Hmmm... The next two are dangerous */
3329 p = kzalloc(32, GFP_KERNEL);
3330 p[32 + sizeof(void *)] = 0x34;
3331 printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
3332 " 0x34 -> -0x%p\n", p);
3333 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3335 validate_slab_cache(kmalloc_caches + 5);
3336 p = kzalloc(64, GFP_KERNEL);
3337 p += 64 + (get_cycles() & 0xff) * sizeof(void *);
3339 printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
3341 printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
3342 validate_slab_cache(kmalloc_caches + 6);
3344 printk(KERN_ERR "\nB. Corruption after free\n");
3345 p = kzalloc(128, GFP_KERNEL);
3348 printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
3349 validate_slab_cache(kmalloc_caches + 7);
3351 p = kzalloc(256, GFP_KERNEL);
3354 printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
3355 validate_slab_cache(kmalloc_caches + 8);
3357 p = kzalloc(512, GFP_KERNEL);
3360 printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
3361 validate_slab_cache(kmalloc_caches + 9);
3364 static void resiliency_test(void) {};
3368 * Generate lists of code addresses where slabcache objects are allocated
3373 unsigned long count;
3386 unsigned long count;
3387 struct location *loc;
3390 static void free_loc_track(struct loc_track *t)
3393 free_pages((unsigned long)t->loc,
3394 get_order(sizeof(struct location) * t->max));
3397 static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
3402 order = get_order(sizeof(struct location) * max);
3404 l = (void *)__get_free_pages(flags, order);
3409 memcpy(l, t->loc, sizeof(struct location) * t->count);
3417 static int add_location(struct loc_track *t, struct kmem_cache *s,
3418 const struct track *track)
3420 long start, end, pos;
3423 unsigned long age = jiffies - track->when;
3429 pos = start + (end - start + 1) / 2;
3432 * There is nothing at "end". If we end up there
3433 * we need to add something to before end.
3438 caddr = t->loc[pos].addr;
3439 if (track->addr == caddr) {
3445 if (age < l->min_time)
3447 if (age > l->max_time)
3450 if (track->pid < l->min_pid)
3451 l->min_pid = track->pid;
3452 if (track->pid > l->max_pid)
3453 l->max_pid = track->pid;
3455 cpu_set(track->cpu, l->cpus);
3457 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3461 if (track->addr < caddr)
3468 * Not found. Insert new tracking element.
3470 if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
3476 (t->count - pos) * sizeof(struct location));
3479 l->addr = track->addr;
3483 l->min_pid = track->pid;
3484 l->max_pid = track->pid;
3485 cpus_clear(l->cpus);
3486 cpu_set(track->cpu, l->cpus);
3487 nodes_clear(l->nodes);
3488 node_set(page_to_nid(virt_to_page(track)), l->nodes);
3492 static void process_slab(struct loc_track *t, struct kmem_cache *s,
3493 struct page *page, enum track_item alloc)
3495 void *addr = slab_address(page);
3496 DECLARE_BITMAP(map, s->objects);
3499 bitmap_zero(map, s->objects);
3500 for_each_free_object(p, s, page->freelist)
3501 set_bit(slab_index(p, s, addr), map);
3503 for_each_object(p, s, addr)
3504 if (!test_bit(slab_index(p, s, addr), map))
3505 add_location(t, s, get_track(s, p, alloc));
3508 static int list_locations(struct kmem_cache *s, char *buf,
3509 enum track_item alloc)
3513 struct loc_track t = { 0, 0, NULL };
3516 if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
3518 return sprintf(buf, "Out of memory\n");
3520 /* Push back cpu slabs */
3523 for_each_node_state(node, N_NORMAL_MEMORY) {
3524 struct kmem_cache_node *n = get_node(s, node);
3525 unsigned long flags;
3528 if (!atomic_long_read(&n->nr_slabs))
3531 spin_lock_irqsave(&n->list_lock, flags);
3532 list_for_each_entry(page, &n->partial, lru)
3533 process_slab(&t, s, page, alloc);
3534 list_for_each_entry(page, &n->full, lru)
3535 process_slab(&t, s, page, alloc);
3536 spin_unlock_irqrestore(&n->list_lock, flags);
3539 for (i = 0; i < t.count; i++) {
3540 struct location *l = &t.loc[i];
3542 if (len > PAGE_SIZE - 100)
3544 len += sprintf(buf + len, "%7ld ", l->count);
3547 len += sprint_symbol(buf + len, (unsigned long)l->addr);
3549 len += sprintf(buf + len, "<not-available>");
3551 if (l->sum_time != l->min_time) {
3552 unsigned long remainder;
3554 len += sprintf(buf + len, " age=%ld/%ld/%ld",
3556 div_long_long_rem(l->sum_time, l->count, &remainder),
3559 len += sprintf(buf + len, " age=%ld",
3562 if (l->min_pid != l->max_pid)
3563 len += sprintf(buf + len, " pid=%ld-%ld",
3564 l->min_pid, l->max_pid);
3566 len += sprintf(buf + len, " pid=%ld",
3569 if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
3570 len < PAGE_SIZE - 60) {
3571 len += sprintf(buf + len, " cpus=");
3572 len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3576 if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
3577 len < PAGE_SIZE - 60) {
3578 len += sprintf(buf + len, " nodes=");
3579 len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
3583 len += sprintf(buf + len, "\n");
3588 len += sprintf(buf, "No data\n");
3592 enum slab_stat_type {
3599 #define SO_FULL (1 << SL_FULL)
3600 #define SO_PARTIAL (1 << SL_PARTIAL)
3601 #define SO_CPU (1 << SL_CPU)
3602 #define SO_OBJECTS (1 << SL_OBJECTS)
3604 static unsigned long slab_objects(struct kmem_cache *s,
3605 char *buf, unsigned long flags)
3607 unsigned long total = 0;
3611 unsigned long *nodes;
3612 unsigned long *per_cpu;
3614 nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
3615 per_cpu = nodes + nr_node_ids;
3617 for_each_possible_cpu(cpu) {
3619 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3629 if (flags & SO_CPU) {
3630 if (flags & SO_OBJECTS)
3641 for_each_node_state(node, N_NORMAL_MEMORY) {
3642 struct kmem_cache_node *n = get_node(s, node);
3644 if (flags & SO_PARTIAL) {
3645 if (flags & SO_OBJECTS)
3646 x = count_partial(n);
3653 if (flags & SO_FULL) {
3654 int full_slabs = atomic_long_read(&n->nr_slabs)
3658 if (flags & SO_OBJECTS)
3659 x = full_slabs * s->objects;
3667 x = sprintf(buf, "%lu", total);
3669 for_each_node_state(node, N_NORMAL_MEMORY)
3671 x += sprintf(buf + x, " N%d=%lu",
3675 return x + sprintf(buf + x, "\n");
3678 static int any_slab_objects(struct kmem_cache *s)
3683 for_each_possible_cpu(cpu) {
3684 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3690 for_each_online_node(node) {
3691 struct kmem_cache_node *n = get_node(s, node);
3696 if (n->nr_partial || atomic_long_read(&n->nr_slabs))
3702 #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
3703 #define to_slab(n) container_of(n, struct kmem_cache, kobj);
3705 struct slab_attribute {
3706 struct attribute attr;
3707 ssize_t (*show)(struct kmem_cache *s, char *buf);
3708 ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
3711 #define SLAB_ATTR_RO(_name) \
3712 static struct slab_attribute _name##_attr = __ATTR_RO(_name)
3714 #define SLAB_ATTR(_name) \
3715 static struct slab_attribute _name##_attr = \
3716 __ATTR(_name, 0644, _name##_show, _name##_store)
3718 static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
3720 return sprintf(buf, "%d\n", s->size);
3722 SLAB_ATTR_RO(slab_size);
3724 static ssize_t align_show(struct kmem_cache *s, char *buf)
3726 return sprintf(buf, "%d\n", s->align);
3728 SLAB_ATTR_RO(align);
3730 static ssize_t object_size_show(struct kmem_cache *s, char *buf)
3732 return sprintf(buf, "%d\n", s->objsize);
3734 SLAB_ATTR_RO(object_size);
3736 static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
3738 return sprintf(buf, "%d\n", s->objects);
3740 SLAB_ATTR_RO(objs_per_slab);
3742 static ssize_t order_show(struct kmem_cache *s, char *buf)
3744 return sprintf(buf, "%d\n", s->order);
3746 SLAB_ATTR_RO(order);
3748 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3751 int n = sprint_symbol(buf, (unsigned long)s->ctor);
3753 return n + sprintf(buf + n, "\n");
3759 static ssize_t aliases_show(struct kmem_cache *s, char *buf)
3761 return sprintf(buf, "%d\n", s->refcount - 1);
3763 SLAB_ATTR_RO(aliases);
3765 static ssize_t slabs_show(struct kmem_cache *s, char *buf)
3767 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
3769 SLAB_ATTR_RO(slabs);
3771 static ssize_t partial_show(struct kmem_cache *s, char *buf)
3773 return slab_objects(s, buf, SO_PARTIAL);
3775 SLAB_ATTR_RO(partial);
3777 static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
3779 return slab_objects(s, buf, SO_CPU);
3781 SLAB_ATTR_RO(cpu_slabs);
3783 static ssize_t objects_show(struct kmem_cache *s, char *buf)
3785 return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
3787 SLAB_ATTR_RO(objects);
3789 static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
3791 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
3794 static ssize_t sanity_checks_store(struct kmem_cache *s,
3795 const char *buf, size_t length)
3797 s->flags &= ~SLAB_DEBUG_FREE;
3799 s->flags |= SLAB_DEBUG_FREE;
3802 SLAB_ATTR(sanity_checks);
3804 static ssize_t trace_show(struct kmem_cache *s, char *buf)
3806 return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
3809 static ssize_t trace_store(struct kmem_cache *s, const char *buf,
3812 s->flags &= ~SLAB_TRACE;
3814 s->flags |= SLAB_TRACE;
3819 static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
3821 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
3824 static ssize_t reclaim_account_store(struct kmem_cache *s,
3825 const char *buf, size_t length)
3827 s->flags &= ~SLAB_RECLAIM_ACCOUNT;
3829 s->flags |= SLAB_RECLAIM_ACCOUNT;
3832 SLAB_ATTR(reclaim_account);
3834 static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
3836 return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
3838 SLAB_ATTR_RO(hwcache_align);
3840 #ifdef CONFIG_ZONE_DMA
3841 static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
3843 return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
3845 SLAB_ATTR_RO(cache_dma);
3848 static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
3850 return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
3852 SLAB_ATTR_RO(destroy_by_rcu);
3854 static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
3856 return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
3859 static ssize_t red_zone_store(struct kmem_cache *s,
3860 const char *buf, size_t length)
3862 if (any_slab_objects(s))
3865 s->flags &= ~SLAB_RED_ZONE;
3867 s->flags |= SLAB_RED_ZONE;
3871 SLAB_ATTR(red_zone);
3873 static ssize_t poison_show(struct kmem_cache *s, char *buf)
3875 return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
3878 static ssize_t poison_store(struct kmem_cache *s,
3879 const char *buf, size_t length)
3881 if (any_slab_objects(s))
3884 s->flags &= ~SLAB_POISON;
3886 s->flags |= SLAB_POISON;
3892 static ssize_t store_user_show(struct kmem_cache *s, char *buf)
3894 return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
3897 static ssize_t store_user_store(struct kmem_cache *s,
3898 const char *buf, size_t length)
3900 if (any_slab_objects(s))
3903 s->flags &= ~SLAB_STORE_USER;
3905 s->flags |= SLAB_STORE_USER;
3909 SLAB_ATTR(store_user);
3911 static ssize_t validate_show(struct kmem_cache *s, char *buf)
3916 static ssize_t validate_store(struct kmem_cache *s,
3917 const char *buf, size_t length)
3921 if (buf[0] == '1') {
3922 ret = validate_slab_cache(s);
3928 SLAB_ATTR(validate);
3930 static ssize_t shrink_show(struct kmem_cache *s, char *buf)
3935 static ssize_t shrink_store(struct kmem_cache *s,
3936 const char *buf, size_t length)
3938 if (buf[0] == '1') {
3939 int rc = kmem_cache_shrink(s);
3949 static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
3951 if (!(s->flags & SLAB_STORE_USER))
3953 return list_locations(s, buf, TRACK_ALLOC);
3955 SLAB_ATTR_RO(alloc_calls);
3957 static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
3959 if (!(s->flags & SLAB_STORE_USER))
3961 return list_locations(s, buf, TRACK_FREE);
3963 SLAB_ATTR_RO(free_calls);
3966 static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
3968 return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
3971 static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
3972 const char *buf, size_t length)
3974 int n = simple_strtoul(buf, NULL, 10);
3977 s->remote_node_defrag_ratio = n * 10;
3980 SLAB_ATTR(remote_node_defrag_ratio);
3983 static struct attribute *slab_attrs[] = {
3984 &slab_size_attr.attr,
3985 &object_size_attr.attr,
3986 &objs_per_slab_attr.attr,
3991 &cpu_slabs_attr.attr,
3995 &sanity_checks_attr.attr,
3997 &hwcache_align_attr.attr,
3998 &reclaim_account_attr.attr,
3999 &destroy_by_rcu_attr.attr,
4000 &red_zone_attr.attr,
4002 &store_user_attr.attr,
4003 &validate_attr.attr,
4005 &alloc_calls_attr.attr,
4006 &free_calls_attr.attr,
4007 #ifdef CONFIG_ZONE_DMA
4008 &cache_dma_attr.attr,
4011 &remote_node_defrag_ratio_attr.attr,
4016 static struct attribute_group slab_attr_group = {
4017 .attrs = slab_attrs,
4020 static ssize_t slab_attr_show(struct kobject *kobj,
4021 struct attribute *attr,
4024 struct slab_attribute *attribute;
4025 struct kmem_cache *s;
4028 attribute = to_slab_attr(attr);
4031 if (!attribute->show)
4034 err = attribute->show(s, buf);
4039 static ssize_t slab_attr_store(struct kobject *kobj,
4040 struct attribute *attr,
4041 const char *buf, size_t len)
4043 struct slab_attribute *attribute;
4044 struct kmem_cache *s;
4047 attribute = to_slab_attr(attr);
4050 if (!attribute->store)
4053 err = attribute->store(s, buf, len);
4058 static void kmem_cache_release(struct kobject *kobj)
4060 struct kmem_cache *s = to_slab(kobj);
4065 static struct sysfs_ops slab_sysfs_ops = {
4066 .show = slab_attr_show,
4067 .store = slab_attr_store,
4070 static struct kobj_type slab_ktype = {
4071 .sysfs_ops = &slab_sysfs_ops,
4072 .release = kmem_cache_release
4075 static int uevent_filter(struct kset *kset, struct kobject *kobj)
4077 struct kobj_type *ktype = get_ktype(kobj);
4079 if (ktype == &slab_ktype)
4084 static struct kset_uevent_ops slab_uevent_ops = {
4085 .filter = uevent_filter,
4088 static struct kset *slab_kset;
4090 #define ID_STR_LENGTH 64
4092 /* Create a unique string id for a slab cache:
4094 * :[flags-]size:[memory address of kmemcache]
4096 static char *create_unique_id(struct kmem_cache *s)
4098 char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
4105 * First flags affecting slabcache operations. We will only
4106 * get here for aliasable slabs so we do not need to support
4107 * too many flags. The flags here must cover all flags that
4108 * are matched during merging to guarantee that the id is
4111 if (s->flags & SLAB_CACHE_DMA)
4113 if (s->flags & SLAB_RECLAIM_ACCOUNT)
4115 if (s->flags & SLAB_DEBUG_FREE)
4119 p += sprintf(p, "%07d", s->size);
4120 BUG_ON(p > name + ID_STR_LENGTH - 1);
4124 static int sysfs_slab_add(struct kmem_cache *s)
4130 if (slab_state < SYSFS)
4131 /* Defer until later */
4134 unmergeable = slab_unmergeable(s);
4137 * Slabcache can never be merged so we can use the name proper.
4138 * This is typically the case for debug situations. In that
4139 * case we can catch duplicate names easily.
4141 sysfs_remove_link(&slab_kset->kobj, s->name);
4145 * Create a unique name for the slab as a target
4148 name = create_unique_id(s);
4151 s->kobj.kset = slab_kset;
4152 err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
4154 kobject_put(&s->kobj);
4158 err = sysfs_create_group(&s->kobj, &slab_attr_group);
4161 kobject_uevent(&s->kobj, KOBJ_ADD);
4163 /* Setup first alias */
4164 sysfs_slab_alias(s, s->name);
4170 static void sysfs_slab_remove(struct kmem_cache *s)
4172 kobject_uevent(&s->kobj, KOBJ_REMOVE);
4173 kobject_del(&s->kobj);
4174 kobject_put(&s->kobj);
4178 * Need to buffer aliases during bootup until sysfs becomes
4179 * available lest we loose that information.
4181 struct saved_alias {
4182 struct kmem_cache *s;
4184 struct saved_alias *next;
4187 static struct saved_alias *alias_list;
4189 static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
4191 struct saved_alias *al;
4193 if (slab_state == SYSFS) {
4195 * If we have a leftover link then remove it.
4197 sysfs_remove_link(&slab_kset->kobj, name);
4198 return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
4201 al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
4207 al->next = alias_list;
4212 static int __init slab_sysfs_init(void)
4214 struct kmem_cache *s;
4217 slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
4219 printk(KERN_ERR "Cannot register slab subsystem.\n");
4225 list_for_each_entry(s, &slab_caches, list) {
4226 err = sysfs_slab_add(s);
4228 printk(KERN_ERR "SLUB: Unable to add boot slab %s"
4229 " to sysfs\n", s->name);
4232 while (alias_list) {
4233 struct saved_alias *al = alias_list;
4235 alias_list = alias_list->next;
4236 err = sysfs_slab_alias(al->s, al->name);
4238 printk(KERN_ERR "SLUB: Unable to add boot slab alias"
4239 " %s to sysfs\n", s->name);
4247 __initcall(slab_sysfs_init);
4251 * The /proc/slabinfo ABI
4253 #ifdef CONFIG_SLABINFO
4255 ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4256 size_t count, loff_t *ppos)
4262 static void print_slabinfo_header(struct seq_file *m)
4264 seq_puts(m, "slabinfo - version: 2.1\n");
4265 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4266 "<objperslab> <pagesperslab>");
4267 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4268 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4272 static void *s_start(struct seq_file *m, loff_t *pos)
4276 down_read(&slub_lock);
4278 print_slabinfo_header(m);
4280 return seq_list_start(&slab_caches, *pos);
4283 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4285 return seq_list_next(p, &slab_caches, pos);
4288 static void s_stop(struct seq_file *m, void *p)
4290 up_read(&slub_lock);
4293 static int s_show(struct seq_file *m, void *p)
4295 unsigned long nr_partials = 0;
4296 unsigned long nr_slabs = 0;
4297 unsigned long nr_inuse = 0;
4298 unsigned long nr_objs;
4299 struct kmem_cache *s;
4302 s = list_entry(p, struct kmem_cache, list);
4304 for_each_online_node(node) {
4305 struct kmem_cache_node *n = get_node(s, node);
4310 nr_partials += n->nr_partial;
4311 nr_slabs += atomic_long_read(&n->nr_slabs);
4312 nr_inuse += count_partial(n);
4315 nr_objs = nr_slabs * s->objects;
4316 nr_inuse += (nr_slabs - nr_partials) * s->objects;
4318 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
4319 nr_objs, s->size, s->objects, (1 << s->order));
4320 seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
4321 seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
4327 const struct seq_operations slabinfo_op = {
4334 #endif /* CONFIG_SLABINFO */